Various more 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 #include "null_content.h"
34 #include "black_decoder.h"
35 #include "silence_decoder.h"
36
37 using std::list;
38 using std::cout;
39 using std::min;
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 (MAX_AUDIO_CHANNELS, 0)
65         , _last_video (0)
66         , _last_was_black (false)
67         , _next_audio (0)
68 {
69         _playlist->Changed.connect (bind (&Player::playlist_changed, this));
70         _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
71 }
72
73 void
74 Player::disable_video ()
75 {
76         _video = false;
77 }
78
79 void
80 Player::disable_audio ()
81 {
82         _audio = false;
83 }
84
85 void
86 Player::disable_subtitles ()
87 {
88         _subtitles = false;
89 }
90
91 bool
92 Player::pass ()
93 {
94         if (!_have_valid_pieces) {
95                 setup_pieces ();
96                 _have_valid_pieces = true;
97         }
98
99         /* Here we are just finding the active decoder with the earliest last emission time, then
100            calling pass on it.
101         */
102
103         Time earliest_t = TIME_MAX;
104         shared_ptr<Piece> earliest;
105
106         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
107                 if ((*i)->content->end(_film) < _position) {
108                         continue;
109                 }
110                 
111                 Time const t = (*i)->content->start() + (*i)->decoder->next();
112                 if (t < earliest_t) {
113                         earliest_t = t;
114                         earliest = *i;
115                 }
116         }
117
118         if (!earliest) {
119                 return true;
120         }
121         
122         earliest->decoder->pass ();
123
124         /* Move position to earliest active next emission */
125
126         for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
127                 if ((*i)->content->end(_film) < _position) {
128                         continue;
129                 }
130
131                 Time const t = (*i)->content->start() + (*i)->decoder->next();
132
133                 if (t < _position) {
134                         _position = t;
135                 }
136         }
137
138         return false;
139 }
140
141 void
142 Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub, Time time)
143 {
144         shared_ptr<Content> content = weak_content.lock ();
145         if (!content) {
146                 return;
147         }
148         
149         time += content->start ();
150         
151         Video (image, same, sub, time);
152 }
153
154 void
155 Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
156 {
157         shared_ptr<Content> content = weak_content.lock ();
158         if (!content) {
159                 return;
160         }
161         
162         /* XXX: mapping */
163
164         /* The time of this audio may indicate that some of our buffered audio is not going to
165            be added to any more, so it can be emitted.
166         */
167
168         time += content->start ();
169
170         if (time > _next_audio) {
171                 /* We can emit some audio from our buffers */
172                 OutputAudioFrame const N = min (_film->time_to_audio_frames (time - _next_audio), static_cast<OutputAudioFrame> (_audio_buffers.frames()));
173                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
174                 emit->copy_from (&_audio_buffers, N, 0, 0);
175                 Audio (emit, _next_audio);
176                 _next_audio += _film->audio_frames_to_time (N);
177
178                 /* And remove it from our buffers */
179                 if (_audio_buffers.frames() > N) {
180                         _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
181                 }
182                 _audio_buffers.set_frames (_audio_buffers.frames() - N);
183         }
184
185         /* Now accumulate the new audio into our buffers */
186         _audio_buffers.ensure_size (time - _next_audio + audio->frames());
187         _audio_buffers.accumulate (audio.get(), 0, _film->time_to_audio_frames (time - _next_audio));
188 }
189
190 /** @return true on error */
191 void
192 Player::seek (Time t)
193 {
194         if (!_have_valid_pieces) {
195                 setup_pieces ();
196                 _have_valid_pieces = true;
197         }
198
199         if (_pieces.empty ()) {
200                 return;
201         }
202
203         /* XXX: don't seek audio because we don't need to... */
204 }
205
206
207 void
208 Player::seek_back ()
209 {
210         /* XXX */
211 }
212
213 void
214 Player::seek_forward ()
215 {
216         /* XXX */
217 }
218
219 struct ContentSorter
220 {
221         bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
222         {
223                 return a->start() < b->start();
224         }
225 };
226
227 void
228 Player::setup_pieces ()
229 {
230         list<shared_ptr<Piece> > old_pieces = _pieces;
231
232         _pieces.clear ();
233
234         Playlist::ContentList content = _playlist->content ();
235         sort (content.begin(), content.end(), ContentSorter ());
236         
237         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
238
239                 shared_ptr<Decoder> decoder;
240                 
241                 /* XXX: into content? */
242
243                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
244                 if (fc) {
245                         shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
246                         
247                         fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
248                         fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
249
250                         decoder = fd;
251                 }
252                 
253                 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
254                 if (ic) {
255                         shared_ptr<ImageMagickDecoder> id;
256                         
257                         /* See if we can re-use an old ImageMagickDecoder */
258                         for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
259                                 shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
260                                 if (imd && imd->content() == ic) {
261                                         id = imd;
262                                 }
263                         }
264
265                         if (!id) {
266                                 id.reset (new ImageMagickDecoder (_film, ic));
267                                 id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
268                         }
269
270                         decoder = id;
271                 }
272
273                 shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
274                 if (sc) {
275                         shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
276                         sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
277
278                         decoder = sd;
279                 }
280
281                 _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
282         }
283
284         /* Fill in visual gaps with black and audio gaps with silence */
285
286         Time video_pos = 0;
287         Time audio_pos = 0;
288         list<shared_ptr<Piece> > pieces_copy = _pieces;
289         for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
290                 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
291                         Time const diff = video_pos - (*i)->content->start();
292                         if (diff > 0) {
293                                 shared_ptr<NullContent> nc (new NullContent (video_pos, diff));
294                                 _pieces.push_back (shared_ptr<Piece> (new Piece (nc, shared_ptr<Decoder> (new BlackDecoder (_film, nc)))));
295                         }
296                                                 
297                         video_pos = (*i)->content->start() + (*i)->content->length(_film);
298                 } else {
299                         Time const diff = audio_pos - (*i)->content->start();
300                         if (diff > 0) {
301                                 shared_ptr<NullContent> nc (new NullContent (audio_pos, diff));
302                                 _pieces.push_back (shared_ptr<Piece> (new Piece (nc, shared_ptr<Decoder> (new SilenceDecoder (_film, nc)))));
303                         }
304                         audio_pos = (*i)->content->start() + (*i)->content->length(_film);
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 }