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