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