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