Merge master.
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/shared_ptr.hpp>
21 #include "playlist.h"
22 #include "sndfile_content.h"
23 #include "sndfile_decoder.h"
24 #include "ffmpeg_content.h"
25 #include "ffmpeg_decoder.h"
26 #include "imagemagick_content.h"
27 #include "imagemagick_decoder.h"
28 #include "job.h"
29
30 using std::list;
31 using std::cout;
32 using std::vector;
33 using std::min;
34 using boost::shared_ptr;
35 using boost::weak_ptr;
36 using boost::dynamic_pointer_cast;
37
38 Playlist::Playlist ()
39         : _video_from (VIDEO_NONE)
40         , _audio_from (AUDIO_NONE)
41 {
42
43 }
44
45 void
46 Playlist::setup (ContentList content)
47 {
48         _video_from = VIDEO_NONE;
49         _audio_from = AUDIO_NONE;
50
51         _ffmpeg.reset ();
52         _imagemagick.clear ();
53         _sndfile.clear ();
54
55         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
56                 i->disconnect ();
57         }
58         
59         _content_connections.clear ();
60
61         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
62                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
63                 if (fc) {
64                         assert (!_ffmpeg);
65                         _ffmpeg = fc;
66                         _video_from = VIDEO_FFMPEG;
67                         if (_audio_from == AUDIO_NONE) {
68                                 _audio_from = AUDIO_FFMPEG;
69                         }
70                 }
71                 
72                 shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (*i);
73                 if (ic) {
74                         _imagemagick.push_back (ic);
75                         if (_video_from == VIDEO_NONE) {
76                                 _video_from = VIDEO_IMAGEMAGICK;
77                         }
78                 }
79
80                 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
81                 if (sc) {
82                         _sndfile.push_back (sc);
83                         _audio_from = AUDIO_SNDFILE;
84                 }
85
86                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
87         }
88
89         Changed ();
90 }
91
92 ContentAudioFrame
93 Playlist::audio_length () const
94 {
95         switch (_audio_from) {
96         case AUDIO_NONE:
97                 return 0;
98         case AUDIO_FFMPEG:
99                 return _ffmpeg->audio_length ();
100         case AUDIO_SNDFILE:
101         {
102                 ContentAudioFrame l = 0;
103                 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
104                         l += (*i)->audio_length ();
105                 }
106                 return l;
107         }
108         }
109
110         return 0;
111 }
112
113 int
114 Playlist::audio_channels () const
115 {
116         switch (_audio_from) {
117         case AUDIO_NONE:
118                 return 0;
119         case AUDIO_FFMPEG:
120                 return _ffmpeg->audio_channels ();
121         case AUDIO_SNDFILE:
122         {
123                 int c = 0;
124                 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
125                         c += (*i)->audio_channels ();
126                 }
127                 return c;
128         }
129         }
130
131         return 0;
132 }
133
134 int
135 Playlist::audio_frame_rate () const
136 {
137         switch (_audio_from) {
138         case AUDIO_NONE:
139                 return 0;
140         case AUDIO_FFMPEG:
141                 return _ffmpeg->audio_frame_rate ();
142         case AUDIO_SNDFILE:
143                 return _sndfile.front()->audio_frame_rate ();
144         }
145
146         return 0;
147 }
148
149 int64_t
150 Playlist::audio_channel_layout () const
151 {
152         switch (_audio_from) {
153         case AUDIO_NONE:
154                 return 0;
155         case AUDIO_FFMPEG:
156                 return _ffmpeg->audio_channel_layout ();
157         case AUDIO_SNDFILE:
158                 /* XXX */
159                 return 0;
160         }
161
162         return 0;
163 }
164
165 float
166 Playlist::video_frame_rate () const
167 {
168         switch (_video_from) {
169         case VIDEO_NONE:
170                 return 0;
171         case VIDEO_FFMPEG:
172                 return _ffmpeg->video_frame_rate ();
173         case VIDEO_IMAGEMAGICK:
174                 return 24;
175         }
176
177         return 0;
178 }
179
180 libdcp::Size
181 Playlist::video_size () const
182 {
183         switch (_video_from) {
184         case VIDEO_NONE:
185                 return libdcp::Size ();
186         case VIDEO_FFMPEG:
187                 return _ffmpeg->video_size ();
188         case VIDEO_IMAGEMAGICK:
189                 /* XXX */
190                 return _imagemagick.front()->video_size ();
191         }
192
193         return libdcp::Size ();
194 }
195
196 ContentVideoFrame
197 Playlist::video_length () const
198 {
199         switch (_video_from) {
200         case VIDEO_NONE:
201                 return 0;
202         case VIDEO_FFMPEG:
203                 return _ffmpeg->video_length ();
204         case VIDEO_IMAGEMAGICK:
205         {
206                 ContentVideoFrame l = 0;
207                 for (list<shared_ptr<const ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
208                         l += (*i)->video_length ();
209                 }
210                 return l;
211         }
212         }
213
214         return 0;
215 }
216
217 bool
218 Playlist::has_audio () const
219 {
220         return _audio_from != AUDIO_NONE;
221 }
222
223 void
224 Playlist::content_changed (weak_ptr<Content> c, int p)
225 {
226         ContentChanged (c, p);
227 }
228
229 AudioMapping
230 Playlist::default_audio_mapping () const
231 {
232         AudioMapping m;
233
234         switch (_audio_from) {
235         case AUDIO_NONE:
236                 break;
237         case AUDIO_FFMPEG:
238                 if (_ffmpeg->audio_channels() == 1) {
239                         /* Map mono sources to centre */
240                         m.add (AudioMapping::Channel (_ffmpeg, 0), libdcp::CENTRE);
241                 } else {
242                         int const N = min (_ffmpeg->audio_channels (), MAX_AUDIO_CHANNELS);
243                         /* Otherwise just start with a 1:1 mapping */
244                         for (int i = 0; i < N; ++i) {
245                                 m.add (AudioMapping::Channel (_ffmpeg, i), (libdcp::Channel) i);
246                         }
247                 }
248                 break;
249
250         case AUDIO_SNDFILE:
251         {
252                 int n = 0;
253                 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
254                         cout << "sndfile " << (*i)->audio_channels() << "\n";
255                         for (int j = 0; j < (*i)->audio_channels(); ++j) {
256                                 m.add (AudioMapping::Channel (*i, j), (libdcp::Channel) n);
257                                 ++n;
258                                 if (n >= MAX_AUDIO_CHANNELS) {
259                                         break;
260                                 }
261                         }
262                         if (n >= MAX_AUDIO_CHANNELS) {
263                                 break;
264                         }
265                 }
266                 break;
267         }
268         }
269
270         return m;
271 }