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) {
63 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
65 _video.push_back (vc);
68 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
70 _sndfile.push_back (sc);
71 _audio_from = AUDIO_SNDFILE;
74 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
81 Playlist::audio_length () const
83 ContentAudioFrame len = 0;
85 switch (_audio_from) {
87 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
88 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
90 len += fc->audio_length ();
95 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
96 len += (*i)->audio_length ();
105 Playlist::audio_channels () const
109 switch (_audio_from) {
111 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
112 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
114 channels = max (channels, fc->audio_channels ());
119 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
120 channels += (*i)->audio_channels ();
129 Playlist::audio_frame_rate () const
131 /* XXX: assuming that all content has the same rate */
133 switch (_audio_from) {
136 shared_ptr<const FFmpegContent> fc = first_ffmpeg ();
138 return fc->audio_frame_rate ();
143 return _sndfile.front()->audio_frame_rate ();
150 Playlist::video_frame_rate () const
152 if (_video.empty ()) {
156 /* XXX: assuming all the same */
157 return _video.front()->video_frame_rate ();
161 Playlist::video_size () const
163 if (_video.empty ()) {
164 return libdcp::Size ();
167 /* XXX: assuming all the same */
168 return _video.front()->video_size ();
172 Playlist::video_length () const
174 ContentVideoFrame len = 0;
175 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
176 len += (*i)->video_length ();
183 Playlist::has_audio () const
190 Playlist::content_changed (weak_ptr<Content> c, int p)
192 ContentChanged (c, p);
195 shared_ptr<const FFmpegContent>
196 Playlist::first_ffmpeg () const
198 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
199 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
205 return shared_ptr<const FFmpegContent> ();
210 Playlist::default_audio_mapping () const
214 switch (_audio_from) {
217 shared_ptr<const FFmpegContent> fc = first_ffmpeg ();
222 /* XXX: assumes all the same */
223 if (fc->audio_channels() == 1) {
224 /* Map mono sources to centre */
225 m.add (AudioMapping::Channel (fc, 0), libdcp::CENTRE);
227 int const N = min (fc->audio_channels (), MAX_AUDIO_CHANNELS);
228 /* Otherwise just start with a 1:1 mapping */
229 for (int i = 0; i < N; ++i) {
230 m.add (AudioMapping::Channel (fc, i), (libdcp::Channel) i);
239 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
240 for (int j = 0; j < (*i)->audio_channels(); ++j) {
241 m.add (AudioMapping::Channel (*i, j), (libdcp::Channel) n);
243 if (n >= MAX_AUDIO_CHANNELS) {
247 if (n >= MAX_AUDIO_CHANNELS) {
259 Playlist::audio_digest () const
263 switch (_audio_from) {
265 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
266 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
268 t += (*i)->digest ();
269 t += lexical_cast<string> (fc->audio_stream()->id);
274 for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
275 t += (*i)->digest ();
280 return md5_digest (t.c_str(), t.length());
284 Playlist::video_digest () const
288 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
289 t += (*i)->digest ();
290 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
292 t += fc->subtitle_stream()->id;
296 return md5_digest (t.c_str(), t.length());