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