Allow UI to set up FFmpeg streams again.
[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
29 using std::list;
30 using std::cout;
31 using std::vector;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34
35 Playlist::Playlist ()
36         : _video_from (VIDEO_NONE)
37         , _audio_from (AUDIO_NONE)
38 {
39
40 }
41
42 void
43 Playlist::setup (ContentList content)
44 {
45         _video_from = VIDEO_NONE;
46         _audio_from = AUDIO_NONE;
47
48         _ffmpeg.reset ();
49         _imagemagick.clear ();
50         _sndfile.clear ();
51
52         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
53                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
54                 if (fc) {
55                         assert (!_ffmpeg);
56                         _ffmpeg = fc;
57                         _video_from = VIDEO_FFMPEG;
58                         if (_audio_from == AUDIO_NONE) {
59                                 _audio_from = AUDIO_FFMPEG;
60                         }
61                 }
62                 
63                 shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (*i);
64                 if (ic) {
65                         _imagemagick.push_back (ic);
66                         if (_video_from == VIDEO_NONE) {
67                                 _video_from = VIDEO_IMAGEMAGICK;
68                         }
69                 }
70
71                 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
72                 if (sc) {
73                         _sndfile.push_back (sc);
74                         _audio_from = AUDIO_SNDFILE;
75                 }
76         }
77 }
78
79 ContentAudioFrame
80 Playlist::audio_length () const
81 {
82         switch (_audio_from) {
83         case AUDIO_NONE:
84                 return 0;
85         case AUDIO_FFMPEG:
86                 return _ffmpeg->audio_length ();
87         case AUDIO_SNDFILE:
88         {
89                 ContentAudioFrame l = 0;
90                 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
91                         l += (*i)->audio_length ();
92                 }
93                 return l;
94         }
95         }
96
97         return 0;
98 }
99
100 int
101 Playlist::audio_channels () const
102 {
103         switch (_audio_from) {
104         case AUDIO_NONE:
105                 return 0;
106         case AUDIO_FFMPEG:
107                 return _ffmpeg->audio_channels ();
108         case AUDIO_SNDFILE:
109         {
110                 int c = 0;
111                 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
112                         c += (*i)->audio_channels ();
113                 }
114                 return c;
115         }
116         }
117
118         return 0;
119 }
120
121 int
122 Playlist::audio_frame_rate () const
123 {
124         switch (_audio_from) {
125         case AUDIO_NONE:
126                 return 0;
127         case AUDIO_FFMPEG:
128                 return _ffmpeg->audio_frame_rate ();
129         case AUDIO_SNDFILE:
130                 return _sndfile.front()->audio_frame_rate ();
131         }
132
133         return 0;
134 }
135
136 int64_t
137 Playlist::audio_channel_layout () const
138 {
139         switch (_audio_from) {
140         case AUDIO_NONE:
141                 return 0;
142         case AUDIO_FFMPEG:
143                 return _ffmpeg->audio_channel_layout ();
144         case AUDIO_SNDFILE:
145                 /* XXX */
146                 return 0;
147         }
148
149         return 0;
150 }
151
152 float
153 Playlist::video_frame_rate () const
154 {
155         switch (_video_from) {
156         case VIDEO_NONE:
157                 return 0;
158         case VIDEO_FFMPEG:
159                 return _ffmpeg->video_frame_rate ();
160         case VIDEO_IMAGEMAGICK:
161                 return 24;
162         }
163
164         return 0;
165 }
166
167 libdcp::Size
168 Playlist::video_size () const
169 {
170         switch (_video_from) {
171         case VIDEO_NONE:
172                 return libdcp::Size ();
173         case VIDEO_FFMPEG:
174                 return _ffmpeg->video_size ();
175         case VIDEO_IMAGEMAGICK:
176                 /* XXX */
177                 return _imagemagick.front()->video_size ();
178         }
179
180         return libdcp::Size ();
181 }
182
183 ContentVideoFrame
184 Playlist::video_length () const
185 {
186         switch (_video_from) {
187         case VIDEO_NONE:
188                 return 0;
189         case VIDEO_FFMPEG:
190                 return _ffmpeg->video_length ();
191         case VIDEO_IMAGEMAGICK:
192         {
193                 ContentVideoFrame l = 0;
194                 for (list<shared_ptr<const ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
195                         l += (*i)->video_length ();
196                 }
197                 return l;
198         }
199         }
200
201         return 0;
202 }
203
204 bool
205 Playlist::has_audio () const
206 {
207         return _audio_from != AUDIO_NONE;
208 }
209
210 Player::Player (boost::shared_ptr<const Film> f, boost::shared_ptr<const Playlist> p)
211         : _film (f)
212         , _playlist (p)
213         , _video (true)
214         , _audio (true)
215         , _subtitles (true)
216         , _have_setup_decoders (false)
217         , _ffmpeg_decoder_done (false)
218         , _video_sync (true)
219 {
220
221 }
222                 
223 void
224 Player::disable_video ()
225 {
226         _video = false;
227 }
228
229 void
230 Player::disable_audio ()
231 {
232         _audio = false;
233 }
234
235 void
236 Player::disable_subtitles ()
237 {
238         _subtitles = false;
239 }
240
241 bool
242 Player::pass ()
243 {
244         if (!_have_setup_decoders) {
245                 setup_decoders ();
246                 _have_setup_decoders = true;
247         }
248         
249         bool done = true;
250         
251         if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
252                 if (!_ffmpeg_decoder_done) {
253                         if (_ffmpeg_decoder->pass ()) {
254                                 _ffmpeg_decoder_done = true;
255                         } else {
256                                 done = false;
257                         }
258                 }
259         }
260
261         if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
262                 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
263                         if ((*_imagemagick_decoder)->pass ()) {
264                                 _imagemagick_decoder++;
265                         }
266
267                         if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
268                                 done = false;
269                         }
270                 }
271         }
272
273         /* XXX: sndfile */
274
275         return done;
276 }
277
278 void
279 Player::set_progress (shared_ptr<Job> job)
280 {
281         /* XXX */
282 }
283
284 void
285 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
286 {
287         Video (i, same, s);
288 }
289
290 void
291 Player::process_audio (shared_ptr<AudioBuffers> b)
292 {
293         Audio (b);
294 }
295
296 /** @return true on error */
297 bool
298 Player::seek (double t)
299 {
300         if (!_have_setup_decoders) {
301                 setup_decoders ();
302                 _have_setup_decoders = true;
303         }
304         
305         bool r = false;
306
307         switch (_playlist->video_from()) {
308         case Playlist::VIDEO_NONE:
309                 break;
310         case Playlist::VIDEO_FFMPEG:
311                 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
312                         r = true;
313                 }
314                 break;
315         case Playlist::VIDEO_IMAGEMAGICK:
316                 /* Find the decoder that contains this position */
317                 _imagemagick_decoder = _imagemagick_decoders.begin ();
318                 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
319                         double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
320                         if (t < this_length) {
321                                 break;
322                         }
323                         t -= this_length;
324                         ++_imagemagick_decoder;
325                 }
326
327                 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
328                         (*_imagemagick_decoder)->seek (t);
329                 } else {
330                         r = true;
331                 }
332                 break;
333         }
334
335         /* XXX: don't seek audio because we don't need to... */
336
337         return r;
338 }
339
340 bool
341 Player::seek_to_last ()
342 {
343         if (!_have_setup_decoders) {
344                 setup_decoders ();
345                 _have_setup_decoders = true;
346         }
347
348         bool r = false;
349         
350         switch (_playlist->video_from ()) {
351         case Playlist::VIDEO_NONE:
352                 break;
353         case Playlist::VIDEO_FFMPEG:
354                 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
355                         r = true;
356                 }
357                 break;
358         case Playlist::VIDEO_IMAGEMAGICK:
359                 if ((*_imagemagick_decoder)->seek_to_last ()) {
360                         r = true;
361                 }
362                 break;
363         }
364
365         /* XXX: don't seek audio because we don't need to... */
366
367         return r;
368 }
369
370 void
371 Player::setup_decoders ()
372 {
373         if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
374                 _ffmpeg_decoder.reset (
375                         new FFmpegDecoder (
376                                 _film,
377                                 _playlist->ffmpeg(),
378                                 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
379                                 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
380                                 _subtitles && _film->with_subtitles(),
381                                 _video_sync
382                                 )
383                         );
384         }
385         
386         if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
387                 _ffmpeg_decoder->connect_video (shared_from_this ());
388         }
389
390         if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
391                 _ffmpeg_decoder->connect_audio (shared_from_this ());
392         }
393
394         if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
395                 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
396                 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
397                         shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
398                         _imagemagick_decoders.push_back (d);
399                         d->connect_video (shared_from_this ());
400                 }
401
402                 _imagemagick_decoder = _imagemagick_decoders.begin ();
403         }
404
405         if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
406                 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
407                 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
408                         shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
409                         _sndfile_decoders.push_back (d);
410                         d->connect_audio (shared_from_this ());
411                 }
412         }
413 }
414
415 void
416 Player::disable_video_sync ()
417 {
418         _video_sync = false;
419 }
420
421 double
422 Player::last_video_time () const
423 {
424         switch (_playlist->video_from ()) {
425         case Playlist::VIDEO_NONE:
426                 return 0;
427         case Playlist::VIDEO_FFMPEG:
428                 return _ffmpeg_decoder->last_source_time ();
429         case Playlist::VIDEO_IMAGEMAGICK:
430                 return (*_imagemagick_decoder)->last_source_time ();
431         }
432
433         return 0;
434 }