2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <boost/shared_ptr.hpp>
22 #include "sndfile_content.h"
23 #include "sndfile_decoder.h"
24 #include "ffmpeg_content.h"
25 #include "ffmpeg_decoder.h"
26 #include "imagemagick_content.h"
27 #include "imagemagick_decoder.h"
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
36 : _video_from (VIDEO_NONE)
37 , _audio_from (AUDIO_NONE)
43 Playlist::setup (ContentList content)
45 _video_from = VIDEO_NONE;
46 _audio_from = AUDIO_NONE;
49 _imagemagick.clear ();
52 for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
53 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
57 _video_from = VIDEO_FFMPEG;
58 if (_audio_from == AUDIO_NONE) {
59 _audio_from = AUDIO_FFMPEG;
63 shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (*i);
65 _imagemagick.push_back (ic);
66 if (_video_from == VIDEO_NONE) {
67 _video_from = VIDEO_IMAGEMAGICK;
71 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
73 _sndfile.push_back (sc);
74 _audio_from = AUDIO_SNDFILE;
80 Playlist::audio_length () const
82 switch (_audio_from) {
86 return _ffmpeg->audio_length ();
89 ContentAudioFrame l = 0;
90 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
91 l += (*i)->audio_length ();
101 Playlist::audio_channels () const
103 switch (_audio_from) {
107 return _ffmpeg->audio_channels ();
111 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
112 c += (*i)->audio_channels ();
122 Playlist::audio_frame_rate () const
124 switch (_audio_from) {
128 return _ffmpeg->audio_frame_rate ();
130 return _sndfile.front()->audio_frame_rate ();
137 Playlist::audio_channel_layout () const
139 switch (_audio_from) {
143 return _ffmpeg->audio_channel_layout ();
153 Playlist::video_frame_rate () const
155 switch (_video_from) {
159 return _ffmpeg->video_frame_rate ();
160 case VIDEO_IMAGEMAGICK:
168 Playlist::video_size () const
170 switch (_video_from) {
172 return libdcp::Size ();
174 return _ffmpeg->video_size ();
175 case VIDEO_IMAGEMAGICK:
177 return _imagemagick.front()->video_size ();
180 return libdcp::Size ();
184 Playlist::video_length () const
186 switch (_video_from) {
190 return _ffmpeg->video_length ();
191 case VIDEO_IMAGEMAGICK:
193 ContentVideoFrame l = 0;
194 for (list<shared_ptr<const ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
195 l += (*i)->video_length ();
205 Playlist::has_audio () const
207 return _audio_from != AUDIO_NONE;
210 Player::Player (boost::shared_ptr<const Film> f, boost::shared_ptr<const Playlist> p)
216 , _have_setup_decoders (false)
217 , _ffmpeg_decoder_done (false)
224 Player::disable_video ()
230 Player::disable_audio ()
236 Player::disable_subtitles ()
244 if (!_have_setup_decoders) {
246 _have_setup_decoders = true;
251 if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
252 if (!_ffmpeg_decoder_done) {
253 if (_ffmpeg_decoder->pass ()) {
254 _ffmpeg_decoder_done = true;
261 if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
262 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
263 if ((*_imagemagick_decoder)->pass ()) {
264 _imagemagick_decoder++;
267 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
279 Player::set_progress (shared_ptr<Job> job)
285 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
291 Player::process_audio (shared_ptr<AudioBuffers> b)
296 /** @return true on error */
298 Player::seek (double t)
300 if (!_have_setup_decoders) {
302 _have_setup_decoders = true;
307 switch (_playlist->video_from()) {
308 case Playlist::VIDEO_NONE:
310 case Playlist::VIDEO_FFMPEG:
311 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
315 case Playlist::VIDEO_IMAGEMAGICK:
316 /* Find the decoder that contains this position */
317 _imagemagick_decoder = _imagemagick_decoders.begin ();
318 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
319 double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
320 if (t < this_length) {
324 ++_imagemagick_decoder;
327 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
328 (*_imagemagick_decoder)->seek (t);
335 /* XXX: don't seek audio because we don't need to... */
341 Player::seek_to_last ()
343 if (!_have_setup_decoders) {
345 _have_setup_decoders = true;
350 switch (_playlist->video_from ()) {
351 case Playlist::VIDEO_NONE:
353 case Playlist::VIDEO_FFMPEG:
354 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
358 case Playlist::VIDEO_IMAGEMAGICK:
359 if ((*_imagemagick_decoder)->seek_to_last ()) {
365 /* XXX: don't seek audio because we don't need to... */
371 Player::setup_decoders ()
373 if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
374 _ffmpeg_decoder.reset (
378 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
379 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
380 _subtitles && _film->with_subtitles(),
386 if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
387 _ffmpeg_decoder->connect_video (shared_from_this ());
390 if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
391 _ffmpeg_decoder->connect_audio (shared_from_this ());
394 if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
395 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
396 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
397 shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
398 _imagemagick_decoders.push_back (d);
399 d->connect_video (shared_from_this ());
402 _imagemagick_decoder = _imagemagick_decoders.begin ();
405 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
406 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
407 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
408 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
409 _sndfile_decoders.push_back (d);
410 d->connect_audio (shared_from_this ());
416 Player::disable_video_sync ()
422 Player::last_video_time () const
424 switch (_playlist->video_from ()) {
425 case Playlist::VIDEO_NONE:
427 case Playlist::VIDEO_FFMPEG:
428 return _ffmpeg_decoder->last_source_time ();
429 case Playlist::VIDEO_IMAGEMAGICK:
430 return (*_imagemagick_decoder)->last_source_time ();