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