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.
22 #include "ffmpeg_decoder.h"
23 #include "imagemagick_decoder.h"
24 #include "sndfile_decoder.h"
25 #include "sndfile_content.h"
31 using boost::shared_ptr;
32 using boost::weak_ptr;
33 using boost::dynamic_pointer_cast;
35 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
41 , _have_valid_decoders (false)
42 , _ffmpeg_decoder_done (false)
45 _playlist->Changed.connect (bind (&Player::playlist_changed, this));
46 _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
50 Player::disable_video ()
56 Player::disable_audio ()
62 Player::disable_subtitles ()
70 if (!_have_valid_decoders) {
72 _have_valid_decoders = true;
77 if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
78 if (!_ffmpeg_decoder_done) {
79 if (_ffmpeg_decoder->pass ()) {
80 _ffmpeg_decoder_done = true;
87 if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
88 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
89 if ((*_imagemagick_decoder)->pass ()) {
90 _imagemagick_decoder++;
93 if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
99 if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
100 for (list<shared_ptr<SndfileDecoder> >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) {
101 if (!(*i)->pass ()) {
106 Audio (_sndfile_buffers);
107 _sndfile_buffers.reset ();
114 Player::set_progress (shared_ptr<Job> job)
116 /* Assume progress can be divined from how far through the video we are */
117 switch (_playlist->video_from ()) {
118 case Playlist::VIDEO_NONE:
120 case Playlist::VIDEO_FFMPEG:
121 if (_playlist->video_length ()) {
122 job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
125 case Playlist::VIDEO_IMAGEMAGICK:
128 for (list<shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
129 if (_imagemagick_decoder == i) {
130 job->set_progress (float (n) / _imagemagick_decoders.size ());
140 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
146 Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<AudioBuffers> b)
148 if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
149 AudioMapping mapping = _film->audio_mapping ();
150 if (!_sndfile_buffers) {
151 _sndfile_buffers.reset (new AudioBuffers (mapping.dcp_channels(), b->frames ()));
152 _sndfile_buffers->make_silent ();
155 for (int i = 0; i < b->channels(); ++i) {
156 list<libdcp::Channel> dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i));
157 for (list<libdcp::Channel>::iterator j = dcp.begin(); j != dcp.end(); ++j) {
158 _sndfile_buffers->accumulate (b, i, static_cast<int> (*j));
167 /** @return true on error */
169 Player::seek (double t)
171 if (!_have_valid_decoders) {
173 _have_valid_decoders = true;
178 switch (_playlist->video_from()) {
179 case Playlist::VIDEO_NONE:
181 case Playlist::VIDEO_FFMPEG:
182 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
185 /* We're seeking, so all `all done' bets are off */
186 _ffmpeg_decoder_done = false;
188 case Playlist::VIDEO_IMAGEMAGICK:
189 /* Find the decoder that contains this position */
190 _imagemagick_decoder = _imagemagick_decoders.begin ();
191 while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
192 double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
193 if (t < this_length) {
197 ++_imagemagick_decoder;
200 if (_imagemagick_decoder != _imagemagick_decoders.end()) {
201 (*_imagemagick_decoder)->seek (t);
208 /* XXX: don't seek audio because we don't need to... */
214 Player::seek_to_last ()
216 if (!_have_valid_decoders) {
218 _have_valid_decoders = true;
223 switch (_playlist->video_from ()) {
224 case Playlist::VIDEO_NONE:
226 case Playlist::VIDEO_FFMPEG:
227 if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
231 /* We're seeking, so all `all done' bets are off */
232 _ffmpeg_decoder_done = false;
234 case Playlist::VIDEO_IMAGEMAGICK:
235 if ((*_imagemagick_decoder)->seek_to_last ()) {
241 /* XXX: don't seek audio because we don't need to... */
247 Player::setup_decoders ()
249 if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
250 _ffmpeg_decoder.reset (
254 _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
255 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
256 _subtitles && _film->with_subtitles(),
262 if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
263 _ffmpeg_decoder->connect_video (shared_from_this ());
266 if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
267 _ffmpeg_decoder->Audio.connect (bind (&Player::process_audio, this, _playlist->ffmpeg (), _1));
270 if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
271 list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
272 for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
273 shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
274 _imagemagick_decoders.push_back (d);
275 d->connect_video (shared_from_this ());
278 _imagemagick_decoder = _imagemagick_decoders.begin ();
281 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
282 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
283 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
284 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
285 _sndfile_decoders.push_back (d);
286 d->Audio.connect (bind (&Player::process_audio, this, *i, _1));
292 Player::disable_video_sync ()
298 Player::last_video_time () const
300 switch (_playlist->video_from ()) {
301 case Playlist::VIDEO_NONE:
303 case Playlist::VIDEO_FFMPEG:
304 return _ffmpeg_decoder->last_source_time ();
305 case Playlist::VIDEO_IMAGEMAGICK:
308 for (list<shared_ptr<ImageMagickDecoder> >::const_iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoder; ++i) {
309 t += (*i)->video_length() / (*i)->video_frame_rate ();
312 return t + (*_imagemagick_decoder)->last_source_time ();
320 Player::content_changed (weak_ptr<Content> w, int p)
322 shared_ptr<Content> c = w.lock ();
327 if (p == VideoContentProperty::VIDEO_LENGTH) {
328 if (dynamic_pointer_cast<FFmpegContent> (c)) {
329 _have_valid_decoders = false;
335 Player::playlist_changed ()
337 _have_valid_decoders = false;