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