Merge master.
[dcpomatic.git] / src / lib / player.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 <stdint.h>
21 #include "player.h"
22 #include "film.h"
23 #include "ffmpeg_decoder.h"
24 #include "ffmpeg_content.h"
25 #include "imagemagick_decoder.h"
26 #include "imagemagick_content.h"
27 #include "sndfile_decoder.h"
28 #include "sndfile_content.h"
29 #include "playlist.h"
30 #include "job.h"
31 #include "image.h"
32 #include "null_content.h"
33 #include "black_decoder.h"
34 #include "silence_decoder.h"
35
36 using std::list;
37 using std::cout;
38 using std::min;
39 using std::max;
40 using std::vector;
41 using boost::shared_ptr;
42 using boost::weak_ptr;
43 using boost::dynamic_pointer_cast;
44
45 struct Piece
46 {
47         Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
48                 : content (c)
49                 , decoder (d)
50         {}
51         
52         shared_ptr<Content> content;
53         shared_ptr<Decoder> decoder;
54 };
55
56 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
57         : _film (f)
58         , _playlist (p)
59         , _video (true)
60         , _audio (true)
61         , _subtitles (true)
62         , _have_valid_pieces (false)
63         , _position (0)
64         , _audio_buffers (f->dcp_audio_channels(), 0)
65         , _next_audio (0)
66 {
67         _playlist->Changed.connect (bind (&Player::playlist_changed, this));
68         _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
69 }
70
71 void
72 Player::disable_video ()
73 {
74         _video = false;
75 }
76
77 void
78 Player::disable_audio ()
79 {
80         _audio = false;
81 }
82
83 void
84 Player::disable_subtitles ()
85 {
86         _subtitles = false;
87 }
88
89 bool
90 Player::pass ()
91 {
92         if (!_have_valid_pieces) {
93                 setup_pieces ();
94                 _have_valid_pieces = true;
95         }
96
97         /* Here we are just finding the active decoder with the earliest last emission time, then
98            calling pass on it.
99         */
100
101         Time earliest_t = TIME_MAX;
102         shared_ptr<Piece> earliest;
103
104         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
105                 cout << "check " << (*i)->content->file()
106                      << " start=" << (*i)->content->start()
107                      << ", next=" << (*i)->decoder->next()
108                      << ", end=" << (*i)->content->end() << "\n";
109                 
110                 if ((*i)->decoder->done ()) {
111                         continue;
112                 }
113
114                 if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
115                         continue;
116                 }
117                 
118                 Time const t = (*i)->content->start() + (*i)->decoder->next();
119                 if (t < earliest_t) {
120                         cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
121                         earliest_t = t;
122                         earliest = *i;
123                 }
124         }
125
126         if (!earliest) {
127                 flush ();
128                 return true;
129         }
130
131         cout << "PASS:\n";
132         cout << "\tpass " << earliest->content->file() << " ";
133         if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
134                 cout << " FFmpeg.\n";
135         } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
136                 cout << " ImageMagickContent.\n";
137         } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
138                 cout << " SndfileContent.\n";
139         } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
140                 cout << " Black.\n";
141         } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
142                 cout << " Silence.\n";
143         }
144         
145         earliest->decoder->pass ();
146         _position = earliest->content->start() + earliest->decoder->next ();
147         cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
148
149         return false;
150 }
151
152 void
153 Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, Time time)
154 {
155         shared_ptr<Content> content = weak_content.lock ();
156         if (!content) {
157                 return;
158         }
159         
160         time += content->start ();
161         
162         Video (image, same, time);
163 }
164
165 void
166 Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
167 {
168         shared_ptr<Content> content = weak_content.lock ();
169         if (!content) {
170                 return;
171         }
172         
173         /* The time of this audio may indicate that some of our buffered audio is not going to
174            be added to any more, so it can be emitted.
175         */
176
177         time += content->start ();
178
179         if (time > _next_audio) {
180                 /* We can emit some audio from our buffers */
181                 OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
182                 assert (N <= _audio_buffers.frames());
183                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
184                 emit->copy_from (&_audio_buffers, N, 0, 0);
185                 Audio (emit, _next_audio);
186                 _next_audio += _film->audio_frames_to_time (N);
187
188                 /* And remove it from our buffers */
189                 if (_audio_buffers.frames() > N) {
190                         _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
191                 }
192                 _audio_buffers.set_frames (_audio_buffers.frames() - N);
193         }
194
195         /* Now accumulate the new audio into our buffers */
196         _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
197         _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
198         _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames());
199 }
200
201 void
202 Player::flush ()
203 {
204         if (_audio_buffers.frames() > 0) {
205                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames()));
206                 emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0);
207                 Audio (emit, _next_audio);
208                 _next_audio += _film->audio_frames_to_time (_audio_buffers.frames ());
209                 _audio_buffers.set_frames (0);
210         }
211 }
212
213 /** @return true on error */
214 void
215 Player::seek (Time t)
216 {
217         if (!_have_valid_pieces) {
218                 setup_pieces ();
219                 _have_valid_pieces = true;
220         }
221
222         if (_pieces.empty ()) {
223                 return;
224         }
225
226 //      cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
227
228         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
229                 Time s = t - (*i)->content->start ();
230                 s = max (static_cast<Time> (0), s);
231                 s = min ((*i)->content->length(), s);
232 //              cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
233                 (*i)->decoder->seek (s);
234         }
235
236         /* XXX: don't seek audio because we don't need to... */
237 }
238
239
240 void
241 Player::seek_back ()
242 {
243
244 }
245
246 void
247 Player::seek_forward ()
248 {
249
250 }
251
252 void
253 Player::add_black_piece (Time s, Time len)
254 {
255         shared_ptr<NullContent> nc (new NullContent (_film, s, len));
256         nc->set_ratio (_film->container ());
257         shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
258         bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3));
259         _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
260         cout << "\tblack @ " << s << " -- " << (s + len) << "\n";
261 }
262
263 void
264 Player::add_silent_piece (Time s, Time len)
265 {
266         shared_ptr<NullContent> nc (new NullContent (_film, s, len));
267         shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
268         sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
269         _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
270         cout << "\tsilence @ " << s << " -- " << (s + len) << "\n";
271 }
272
273
274 void
275 Player::setup_pieces ()
276 {
277         cout << "----- Player SETUP PIECES.\n";
278
279         list<shared_ptr<Piece> > old_pieces = _pieces;
280
281         _pieces.clear ();
282
283         Playlist::ContentList content = _playlist->content ();
284         sort (content.begin(), content.end(), ContentSorter ());
285         
286         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
287
288                 shared_ptr<Decoder> decoder;
289                 
290                 /* XXX: into content? */
291
292                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
293                 if (fc) {
294                         shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
295                         
296                         fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
297                         fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
298
299                         decoder = fd;
300                         cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
301                 }
302                 
303                 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
304                 if (ic) {
305                         shared_ptr<ImageMagickDecoder> id;
306                         
307                         /* See if we can re-use an old ImageMagickDecoder */
308                         for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
309                                 shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
310                                 if (imd && imd->content() == ic) {
311                                         id = imd;
312                                 }
313                         }
314
315                         if (!id) {
316                                 id.reset (new ImageMagickDecoder (_film, ic));
317                                 id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
318                         }
319
320                         decoder = id;
321                         cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
322                 }
323
324                 shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
325                 if (sc) {
326                         shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
327                         sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
328
329                         decoder = sd;
330                         cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
331                 }
332
333                 _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
334         }
335
336         /* Fill in visual gaps with black and audio gaps with silence */
337
338         Time video_pos = 0;
339         Time audio_pos = 0;
340         list<shared_ptr<Piece> > pieces_copy = _pieces;
341         for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
342                 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
343                         Time const diff = (*i)->content->start() - video_pos;
344                         if (diff > 0) {
345                                 add_black_piece (video_pos, diff);
346                         }
347                         video_pos = (*i)->content->end();
348                 }
349
350                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
351                 if (ac && ac->audio_channels()) {
352                         Time const diff = (*i)->content->start() - audio_pos;
353                         if (diff > 0) {
354                                 add_silent_piece (video_pos, diff);
355                         }
356                         audio_pos = (*i)->content->end();
357                 }
358         }
359
360         if (video_pos < audio_pos) {
361                 add_black_piece (video_pos, audio_pos - video_pos);
362         } else if (audio_pos < video_pos) {
363                 add_silent_piece (audio_pos, video_pos - audio_pos);
364         }
365 }
366
367 void
368 Player::content_changed (weak_ptr<Content> w, int p)
369 {
370         shared_ptr<Content> c = w.lock ();
371         if (!c) {
372                 return;
373         }
374
375         if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
376                 _have_valid_pieces = false;
377         }
378 }
379
380 void
381 Player::playlist_changed ()
382 {
383         _have_valid_pieces = false;
384 }