Various fixes to make audio analysis sort-of work.
[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         , _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 bool
83 Player::pass ()
84 {
85         if (!_have_valid_pieces) {
86                 setup_pieces ();
87                 _have_valid_pieces = true;
88         }
89
90         /* Here we are just finding the active decoder with the earliest last emission time, then
91            calling pass on it.
92         */
93
94         Time earliest_t = TIME_MAX;
95         shared_ptr<Piece> earliest;
96
97         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
98                 if ((*i)->decoder->done ()) {
99                         continue;
100                 }
101
102                 if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
103                         continue;
104                 }
105                 
106                 Time const t = (*i)->content->start() + (*i)->decoder->position();
107                 if (t < earliest_t) {
108                         earliest_t = t;
109                         earliest = *i;
110                 }
111         }
112
113         if (!earliest) {
114                 flush ();
115                 return true;
116         }
117
118         earliest->decoder->pass ();
119         _position = earliest->content->start() + earliest->decoder->position ();
120
121         return false;
122 }
123
124 void
125 Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, Time time)
126 {
127         shared_ptr<Content> content = weak_content.lock ();
128         if (!content) {
129                 return;
130         }
131         
132         time += content->start ();
133         
134         Video (image, same, time);
135 }
136
137 void
138 Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
139 {
140         shared_ptr<Content> content = weak_content.lock ();
141         if (!content) {
142                 return;
143         }
144         
145         /* The time of this audio may indicate that some of our buffered audio is not going to
146            be added to any more, so it can be emitted.
147         */
148
149         time += content->start ();
150
151         cout << "Player gets " << audio->frames() << " @ " << time << " cf " << _next_audio << "\n";
152
153         if (time > _next_audio) {
154                 /* We can emit some audio from our buffers */
155                 OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
156                 assert (N <= _audio_buffers.frames());
157                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
158                 emit->copy_from (&_audio_buffers, N, 0, 0);
159                 Audio (emit, _next_audio);
160                 _next_audio += _film->audio_frames_to_time (N);
161
162                 /* And remove it from our buffers */
163                 if (_audio_buffers.frames() > N) {
164                         _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
165                 }
166                 _audio_buffers.set_frames (_audio_buffers.frames() - N);
167         }
168
169         /* Now accumulate the new audio into our buffers */
170         _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
171         _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
172         _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames());
173 }
174
175 void
176 Player::flush ()
177 {
178         if (_audio_buffers.frames() > 0) {
179                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames()));
180                 emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0);
181                 Audio (emit, _next_audio);
182                 _next_audio += _film->audio_frames_to_time (_audio_buffers.frames ());
183                 _audio_buffers.set_frames (0);
184         }
185 }
186
187 /** @return true on error */
188 void
189 Player::seek (Time t)
190 {
191         if (!_have_valid_pieces) {
192                 setup_pieces ();
193                 _have_valid_pieces = true;
194         }
195
196         if (_pieces.empty ()) {
197                 return;
198         }
199
200         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
201                 Time s = t - (*i)->content->start ();
202                 s = max (static_cast<Time> (0), s);
203                 s = min ((*i)->content->length(), s);
204                 (*i)->decoder->seek (s);
205         }
206
207         /* XXX: don't seek audio because we don't need to... */
208 }
209
210
211 void
212 Player::seek_back ()
213 {
214
215 }
216
217 void
218 Player::seek_forward ()
219 {
220
221 }
222
223 void
224 Player::add_black_piece (Time s, Time len)
225 {
226         shared_ptr<NullContent> nc (new NullContent (_film, s, len));
227         nc->set_ratio (_film->container ());
228         shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
229         bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3));
230         _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
231 }
232
233 void
234 Player::add_silent_piece (Time s, Time len)
235 {
236         shared_ptr<NullContent> nc (new NullContent (_film, s, len));
237         shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
238         sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
239         _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
240 }
241
242
243 void
244 Player::setup_pieces ()
245 {
246         list<shared_ptr<Piece> > old_pieces = _pieces;
247
248         _pieces.clear ();
249
250         Playlist::ContentList content = _playlist->content ();
251         sort (content.begin(), content.end(), ContentSorter ());
252         
253         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
254
255                 shared_ptr<Decoder> decoder;
256                 
257                 /* XXX: into content? */
258
259                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
260                 if (fc) {
261                         shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio));
262                         
263                         fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
264                         fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
265                         if (_video_container_size) {
266                                 fd->set_video_container_size (_video_container_size.get ());
267                         }
268
269                         decoder = fd;
270                 }
271                 
272                 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
273                 if (ic) {
274                         shared_ptr<ImageMagickDecoder> id;
275                         
276                         /* See if we can re-use an old ImageMagickDecoder */
277                         for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
278                                 shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
279                                 if (imd && imd->content() == ic) {
280                                         id = imd;
281                                 }
282                         }
283
284                         if (!id) {
285                                 id.reset (new ImageMagickDecoder (_film, ic));
286                                 id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
287                                 if (_video_container_size) {
288                                         id->set_video_container_size (_video_container_size.get ());
289                                 }
290                         }
291
292                         decoder = id;
293                 }
294
295                 shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
296                 if (sc) {
297                         shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
298                         sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
299
300                         decoder = sd;
301                 }
302
303                 _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
304         }
305
306         /* Fill in visual gaps with black and audio gaps with silence */
307
308         Time video_pos = 0;
309         Time audio_pos = 0;
310         list<shared_ptr<Piece> > pieces_copy = _pieces;
311         for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
312                 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
313                         Time const diff = (*i)->content->start() - video_pos;
314                         if (diff > 0) {
315                                 add_black_piece (video_pos, diff);
316                         }
317                         video_pos = (*i)->content->end();
318                 }
319
320                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
321                 if (ac && ac->audio_channels()) {
322                         Time const diff = (*i)->content->start() - audio_pos;
323                         if (diff > 0) {
324                                 add_silent_piece (video_pos, diff);
325                         }
326                         audio_pos = (*i)->content->end();
327                 }
328         }
329
330         if (video_pos < audio_pos) {
331                 add_black_piece (video_pos, audio_pos - video_pos);
332         } else if (audio_pos < video_pos) {
333                 add_silent_piece (audio_pos, video_pos - audio_pos);
334         }
335 }
336
337 void
338 Player::content_changed (weak_ptr<Content> w, int p)
339 {
340         shared_ptr<Content> c = w.lock ();
341         if (!c) {
342                 return;
343         }
344
345         if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
346                 _have_valid_pieces = false;
347         }
348 }
349
350 void
351 Player::playlist_changed ()
352 {
353         _have_valid_pieces = false;
354 }
355
356 void
357 Player::set_video_container_size (libdcp::Size s)
358 {
359         _video_container_size = s;
360         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
361                 shared_ptr<VideoDecoder> vd = dynamic_pointer_cast<VideoDecoder> ((*i)->decoder);
362                 if (vd) {
363                         vd->set_video_container_size (s);
364                 }
365         }
366 }