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>
21 #include <boost/lexical_cast.hpp>
23 #include "sndfile_content.h"
24 #include "sndfile_decoder.h"
25 #include "video_content.h"
26 #include "ffmpeg_decoder.h"
27 #include "ffmpeg_content.h"
28 #include "imagemagick_decoder.h"
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::lexical_cast;
43 : _audio_from (AUDIO_FFMPEG)
49 Playlist::setup (ContentList content)
51 _audio_from = AUDIO_FFMPEG;
56 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
60 _content_connections.clear ();
62 for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
65 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
67 _video.push_back (vc);
70 /* FFmpegContent is audio if we are doing AUDIO_FFMPEG */
71 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
72 if (fc && _audio_from == AUDIO_FFMPEG) {
73 _audio.push_back (fc);
76 /* SndfileContent trumps FFmpegContent for audio */
77 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
79 if (_audio_from == AUDIO_FFMPEG) {
80 /* This is our fist SndfileContent; clear any FFmpegContent and
81 say that we are using Sndfile.
84 _audio_from = AUDIO_SNDFILE;
87 _audio.push_back (sc);
90 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
96 /** @return Length of our audio */
98 Playlist::audio_length () const
100 ContentAudioFrame len = 0;
102 switch (_audio_from) {
104 /* FFmpeg content is sequential */
105 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
106 len += (*i)->audio_length ();
110 /* Sndfile content is simultaneous */
111 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
112 len = max (len, (*i)->audio_length ());
120 /** @return number of audio channels */
122 Playlist::audio_channels () const
126 switch (_audio_from) {
128 /* FFmpeg audio is sequential, so use the maximum channel count */
129 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
130 channels = max (channels, (*i)->audio_channels ());
134 /* Sndfile audio is simultaneous, so it's the sum of the channel counts */
135 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
136 channels += (*i)->audio_channels ();
145 Playlist::audio_frame_rate () const
147 if (_audio.empty ()) {
151 /* XXX: assuming that all content has the same rate */
152 return _audio.front()->audio_frame_rate ();
156 Playlist::video_frame_rate () const
158 if (_video.empty ()) {
162 /* XXX: assuming all the same */
163 return _video.front()->video_frame_rate ();
167 Playlist::video_size () const
169 if (_video.empty ()) {
170 return libdcp::Size ();
173 /* XXX: assuming all the same */
174 return _video.front()->video_size ();
178 Playlist::video_length () const
180 ContentVideoFrame len = 0;
181 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
182 len += (*i)->video_length ();
189 Playlist::has_audio () const
191 return !_audio.empty ();
195 Playlist::content_changed (weak_ptr<Content> c, int p)
197 ContentChanged (c, p);
201 Playlist::default_audio_mapping () const
204 if (_audio.empty ()) {
208 switch (_audio_from) {
211 /* XXX: assumes all the same */
212 if (_audio.front()->audio_channels() == 1) {
213 /* Map mono sources to centre */
214 m.add (AudioMapping::Channel (_audio.front(), 0), libdcp::CENTRE);
216 int const N = min (_audio.front()->audio_channels (), MAX_AUDIO_CHANNELS);
217 /* Otherwise just start with a 1:1 mapping */
218 for (int i = 0; i < N; ++i) {
219 m.add (AudioMapping::Channel (_audio.front(), i), (libdcp::Channel) i);
228 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
229 for (int j = 0; j < (*i)->audio_channels(); ++j) {
230 m.add (AudioMapping::Channel (*i, j), (libdcp::Channel) n);
232 if (n >= MAX_AUDIO_CHANNELS) {
236 if (n >= MAX_AUDIO_CHANNELS) {
248 Playlist::audio_digest () const
252 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
253 t += (*i)->digest ();
255 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
257 t += lexical_cast<string> (fc->audio_stream()->id);
261 return md5_digest (t.c_str(), t.length());
265 Playlist::video_digest () const
269 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
270 t += (*i)->digest ();
271 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
272 if (fc && fc->subtitle_stream()) {
273 t += fc->subtitle_stream()->id;
277 return md5_digest (t.c_str(), t.length());
281 Playlist::content_length () const
283 float const vfr = video_frame_rate() > 0 ? video_frame_rate() : 24;
284 int const afr = audio_frame_rate() > 0 ? audio_frame_rate() : 48000;
288 ContentVideoFrame (audio_length() * vfr / afr)