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 "ffmpeg_content.h"
24 #include "imagemagick_decoder.h"
25 #include "imagemagick_content.h"
26 #include "sndfile_decoder.h"
27 #include "sndfile_content.h"
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
37 Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
43 , _have_valid_decoders (false)
46 _playlist->Changed.connect (bind (&Player::playlist_changed, this));
47 _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
51 Player::disable_video ()
57 Player::disable_audio ()
63 Player::disable_subtitles ()
71 if (!_have_valid_decoders) {
73 _have_valid_decoders = true;
78 if (_video_decoder != _video_decoders.end ()) {
79 if ((*_video_decoder)->pass ()) {
83 if (_video_decoder != _video_decoders.end ()) {
88 if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
89 for (list<shared_ptr<SndfileDecoder> >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) {
95 Audio (_sndfile_buffers);
96 _sndfile_buffers.reset ();
103 Player::set_progress (shared_ptr<Job> job)
105 /* Assume progress can be divined from how far through the video we are */
107 if (_video_decoder == _video_decoders.end() || !_playlist->video_length()) {
111 ContentVideoFrame p = 0;
112 list<shared_ptr<VideoDecoder> >::iterator i = _video_decoders.begin ();
113 while (i != _video_decoders.end() && i != _video_decoder) {
114 p += (*i)->video_length ();
117 job->set_progress (float ((*_video_decoder)->video_frame ()) / _playlist->video_length ());
121 Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
127 Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<AudioBuffers> b)
129 if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
130 AudioMapping mapping = _film->audio_mapping ();
131 if (!_sndfile_buffers) {
132 _sndfile_buffers.reset (new AudioBuffers (mapping.dcp_channels(), b->frames ()));
133 _sndfile_buffers->make_silent ();
136 for (int i = 0; i < b->channels(); ++i) {
137 list<libdcp::Channel> dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i));
138 for (list<libdcp::Channel>::iterator j = dcp.begin(); j != dcp.end(); ++j) {
139 _sndfile_buffers->accumulate (b, i, static_cast<int> (*j));
148 /** @return true on error */
150 Player::seek (double t)
152 if (!_have_valid_decoders) {
154 _have_valid_decoders = true;
157 /* Find the decoder that contains this position */
158 _video_decoder = _video_decoders.begin ();
159 while (_video_decoder != _video_decoders.end ()) {
160 double const this_length = double ((*_video_decoder)->video_length()) / _film->video_frame_rate ();
161 if (t < this_length) {
168 if (_video_decoder != _video_decoders.end()) {
169 (*_video_decoder)->seek (t);
174 /* XXX: don't seek audio because we don't need to... */
180 Player::setup_decoders ()
182 _video_decoders.clear ();
183 _video_decoder = _video_decoders.end ();
184 _sndfile_decoders.clear ();
187 list<shared_ptr<const VideoContent> > vc = _playlist->video ();
188 for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
190 shared_ptr<VideoDecoder> d;
192 /* XXX: into content? */
194 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
196 shared_ptr<FFmpegDecoder> fd (
199 _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
205 if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
206 fd->Audio.connect (bind (&Player::process_audio, this, fc, _1));
212 shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
214 d.reset (new ImageMagickDecoder (_film, ic));
217 d->connect_video (shared_from_this ());
218 _video_decoders.push_back (d);
221 _video_decoder = _video_decoders.begin ();
224 if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
225 list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
226 for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
227 shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
228 _sndfile_decoders.push_back (d);
229 d->Audio.connect (bind (&Player::process_audio, this, *i, _1));
235 Player::disable_video_sync ()
241 Player::last_video_time () const
244 for (list<shared_ptr<VideoDecoder> >::const_iterator i = _video_decoders.begin(); i != _video_decoder; ++i) {
245 t += (*i)->video_length() / (*i)->video_frame_rate ();
248 return t + (*_video_decoder)->last_content_time ();
252 Player::content_changed (weak_ptr<Content> w, int p)
254 shared_ptr<Content> c = w.lock ();
259 if (p == VideoContentProperty::VIDEO_LENGTH) {
260 if (dynamic_pointer_cast<FFmpegContent> (c)) {
261 /* FFmpeg content length changes are serious; we need new decoders */
262 _have_valid_decoders = false;
268 Player::playlist_changed ()
270 _have_valid_decoders = false;