Various hacks.
[dcpomatic.git] / src / lib / player.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include "player.h"
23 #include "film.h"
24 #include "ffmpeg_decoder.h"
25 #include "ffmpeg_content.h"
26 #include "imagemagick_decoder.h"
27 #include "imagemagick_content.h"
28 #include "sndfile_decoder.h"
29 #include "sndfile_content.h"
30 #include "playlist.h"
31 #include "job.h"
32 #include "image.h"
33
34 using std::list;
35 using std::cout;
36 using std::min;
37 using std::vector;
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::dynamic_pointer_cast;
41
42 struct Piece
43 {
44         Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
45                 : content (c)
46                 , decoder (d)
47         {}
48         
49         shared_ptr<Content> content;
50         shared_ptr<Decoder> decoder;
51 };
52
53 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
54         : _film (f)
55         , _playlist (p)
56         , _video (true)
57         , _audio (true)
58         , _subtitles (true)
59         , _have_valid_decoders (false)
60         , _position (0)
61         , _audio_buffers (MAX_AUDIO_CHANNELS, 0)
62         , _last_video (0)
63         , _last_was_black (false)
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                 if ((*i)->content->end(_film) < _position) {
105                         continue;
106                 }
107                 
108                 Time const t = (*i)->content->start() + (*i)->decoder->next();
109                 if (t < earliest_t) {
110                         earliest_t = t;
111                         earliest = *i;
112                 }
113         }
114
115         if (!earliest) {
116                 return true;
117         }
118         
119         earliest->decoder->pass ();
120
121         /* Move position to earliest active next emission */
122
123         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
124                 if ((*i)->content->end(_film) < _position) {
125                         continue;
126                 }
127
128                 Time const t = (*i)->content->start() + (*i)->decoder->next();
129
130                 if (t < _position) {
131                         _position = t;
132                 }
133         }
134
135         return false;
136 }
137
138 void
139 Player::process_video (shared_ptr<Piece> piece, shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub, Time time)
140 {
141         time += piece->start ();
142         
143         Video (image, same, sub, time);
144 }
145
146 void
147 Player::process_audio (shared_ptr<Piece> piece, shared_ptr<const AudioBuffers> audio, Time time)
148 {
149         /* XXX: mapping */
150
151         /* The time of this audio may indicate that some of our buffered audio is not going to
152            be added to any more, so it can be emitted.
153         */
154
155         time += piece->start ();
156
157         if (time > _next_audio) {
158                 /* We can emit some audio from our buffers */
159                 OutputAudioFrame const N = min (_film->time_to_audio_frames (time - _next_audio), static_cast<OutputAudioFrame> (_audio_buffers.frames()));
160                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
161                 emit->copy_from (&_audio_buffers, N, 0, 0);
162                 Audio (emit, _next_audio);
163                 _next_audio += _film->audio_frames_to_time (N);
164
165                 /* And remove it from our buffers */
166                 if (_audio_buffers.frames() > N) {
167                         _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
168                 }
169                 _audio_buffers.set_frames (_audio_buffers.frames() - N);
170         }
171
172         /* Now accumulate the new audio into our buffers */
173         _audio_buffers.ensure_size (time - _next_audio + audio->frames());
174         _audio_buffers.accumulate (audio.get(), 0, _film->time_to_audio_frames (time - _next_audio));
175 }
176
177 /** @return true on error */
178 bool
179 Player::seek (Time t)
180 {
181         if (!_have_valid_pieces) {
182                 setup_pieces ();
183                 _have_valid_pieces = true;
184         }
185
186         if (_pieces.empty ()) {
187                 return true;
188         }
189
190         /* XXX: don't seek audio because we don't need to... */
191
192         return false;
193 }
194
195
196 void
197 Player::seek_back ()
198 {
199         /* XXX */
200 }
201
202 void
203 Player::seek_forward ()
204 {
205         /* XXX */
206 }
207
208 struct ContentSorter
209 {
210         bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
211         {
212                 return a->time() < b->time();
213         }
214 };
215
216 void
217 Player::setup_decoders ()
218 {
219         list<shared_ptr<Piece> > old_pieces = _pieces;
220
221         _pieces.clear ();
222
223         Playlist::ContentList content = _playlist->content ();
224         content.sort (ContentSorter ());
225         
226         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
227
228                 shared_ptr<Decoder> decoder;
229                 
230                 /* XXX: into content? */
231
232                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
233                 if (fc) {
234                         shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
235                         
236                         fd->Video.connect (bind (&Player::process_video, this, dr, _1, _2, _3, _4));
237                         fd->Audio.connect (bind (&Player::process_audio, this, dr, _1, _2));
238
239                         decoder = fd;
240                 }
241                 
242                 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
243                 if (ic) {
244                         shared_ptr<ImageMagickDecoder> id;
245                         
246                         /* See if we can re-use an old ImageMagickDecoder */
247                         for (list<shared_ptr<Piece> >::const_iterator i = old_pieces.begin(); i != old_pieces.end(); ++i) {
248                                 shared_ptr<ContentPiece> cp = dynamic_pointer_cast<ContentPiece> (*i);
249                                 if (cp) {
250                                         shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> (cp->decoder ());
251                                         if (imd && imd->content() == ic) {
252                                                 id = imd;
253                                         }
254                                 }
255                         }
256
257                         if (!id) {
258                                 id.reset (new ImageMagickDecoder (_film, ic));
259                                 id->Video.connect (bind (&Player::process_video, this, dr, _1, _2, _3, _4));
260                         }
261
262                         decoder = id;
263                 }
264
265                 shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
266                 if (sc) {
267                         shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
268                         sd->Audio.connect (bind (&Player::process_audio, this, dr, _1, _2));
269
270                         decoder = sd;
271                 }
272
273                 _pieces.push_back (shared_ptr<new ContentPiece> (*i, decoder));
274         }
275
276         /* Fill in visual gaps with black and audio gaps with silence */
277
278         Time video_pos = 0;
279         Time audio_pos = 0;
280         list<shared_ptr<Piece> > pieces_copy = _pieces;
281         for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
282                 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
283                         Time const diff = video_pos - (*i)->content->time();
284                         if (diff > 0) {
285                                 _pieces.push_back (
286                                         shared_ptr<Piece> (
287                                                 shared_ptr<Content> (new NullContent (video_pos, diff)),
288                                                 shared_ptr<Decoder> (new BlackDecoder (video_pos, diff))
289                                                 )
290                                         );
291                         }
292                                                 
293                         video_pos = (*i)->content->time() + (*i)->content->length();
294                 } else {
295                         Time const diff = audio_pos - (*i)->content->time();
296                         if (diff > 0) {
297                                 _pieces.push_back (
298                                         shared_ptr<Piece> (
299                                                 shared_ptr<Content> (new NullContent (audio_pos, diff)),
300                                                 shared_ptr<Decoder> (new SilenceDecoder (audio_pos, diff))
301                                                 )
302                                         );
303                         }
304                         audio_pos = (*i)->content->time() + (*i)->content->length();
305                 }
306         }
307
308         _position = 0;
309 }
310
311 void
312 Player::content_changed (weak_ptr<Content> w, int p)
313 {
314         shared_ptr<Content> c = w.lock ();
315         if (!c) {
316                 return;
317         }
318
319         if (p == VideoContentProperty::VIDEO_LENGTH) {
320                 _have_valid_pieces = false;
321         }
322 }
323
324 void
325 Player::playlist_changed ()
326 {
327         _have_valid_pieces = false;
328 }
329
330 void
331 Player::emit_black_frame ()
332 {
333         shared_ptr<SimpleImage> image (new SimpleImage (AV_PIX_FMT_RGB24, libdcp::Size (128, 128), true));
334         Video (image, _last_was_black, shared_ptr<Subtitle> (), _last_video);
335         _last_video += _film->video_frames_to_time (1);
336 }
337
338 void
339 Player::emit_silence (Time t)
340 {
341         OutputAudioFrame frames = _film->time_to_audio_frames (t);
342         while (frames) {
343                 /* Do this in half-second chunks so we don't overwhelm anybody */
344                 OutputAudioFrame this_time = min (_film->dcp_audio_frame_rate() / 2, frames);
345                 shared_ptr<AudioBuffers> silence (new AudioBuffers (MAX_AUDIO_CHANNELS, this_time));
346                 silence->make_silent ();
347                 Audio (silence, _last_audio);
348                 _last_audio += _film->audio_frames_to_time (this_time);
349                 frames -= this_time;
350         }
351 }