Handle multiple audio streams in a single piece of content
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "ffmpeg_content.h"
21 #include "ffmpeg_examiner.h"
22 #include "ffmpeg_subtitle_stream.h"
23 #include "ffmpeg_audio_stream.h"
24 #include "compose.hpp"
25 #include "job.h"
26 #include "util.h"
27 #include "filter.h"
28 #include "film.h"
29 #include "log.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "safe_stringstream.h"
33 #include "raw_convert.h"
34 #include <libcxml/cxml.h>
35 extern "C" {
36 #include <libavformat/avformat.h>
37 }
38 #include <boost/foreach.hpp>
39
40 #include "i18n.h"
41
42 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
43
44 using std::string;
45 using std::vector;
46 using std::list;
47 using std::cout;
48 using std::pair;
49 using boost::shared_ptr;
50 using boost::dynamic_pointer_cast;
51
52 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
53 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
54 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
55 int const FFmpegContentProperty::FILTERS = 103;
56
57 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
58         : Content (f, p)
59         , VideoContent (f, p)
60         , AudioContent (f, p)
61         , SubtitleContent (f, p)
62 {
63
64 }
65
66 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version, list<string>& notes)
67         : Content (f, node)
68         , VideoContent (f, node, version)
69         , AudioContent (f, node)
70         , SubtitleContent (f, node, version)
71 {
72         list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
73         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
74                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
75                 if ((*i)->optional_number_child<int> ("Selected")) {
76                         _subtitle_stream = _subtitle_streams.back ();
77                 }
78         }
79
80         c = node->node_children ("AudioStream");
81         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
82                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
83         }
84
85         c = node->node_children ("Filter");
86         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
87                 Filter const * f = Filter::from_id ((*i)->content ());
88                 if (f) {
89                         _filters.push_back (f);
90                 } else {
91                         notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
92                 }
93         }
94
95         _first_video = node->optional_number_child<double> ("FirstVideo");
96 }
97
98 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
99         : Content (f, c)
100         , VideoContent (f, c)
101         , AudioContent (f, c)
102         , SubtitleContent (f, c)
103 {
104         shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
105         DCPOMATIC_ASSERT (ref);
106
107         for (size_t i = 0; i < c.size(); ++i) {
108                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
109                 if (fc->use_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
110                         throw JoinError (_("Content to be joined must use the same subtitle stream."));
111                 }
112         }
113
114         _subtitle_streams = ref->subtitle_streams ();
115         _subtitle_stream = ref->subtitle_stream ();
116         _audio_streams = ref->ffmpeg_audio_streams ();
117         _first_video = ref->_first_video;
118 }
119
120 void
121 FFmpegContent::as_xml (xmlpp::Node* node) const
122 {
123         node->add_child("Type")->add_child_text ("FFmpeg");
124         Content::as_xml (node);
125         VideoContent::as_xml (node);
126         AudioContent::as_xml (node);
127         SubtitleContent::as_xml (node);
128
129         boost::mutex::scoped_lock lm (_mutex);
130
131         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
132                 xmlpp::Node* t = node->add_child("SubtitleStream");
133                 if (_subtitle_stream && *i == _subtitle_stream) {
134                         t->add_child("Selected")->add_child_text("1");
135                 }
136                 (*i)->as_xml (t);
137         }
138
139         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
140                 (*i)->as_xml (node->add_child("AudioStream"));
141         }
142
143         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
144                 node->add_child("Filter")->add_child_text ((*i)->id ());
145         }
146
147         if (_first_video) {
148                 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
149         }
150 }
151
152 void
153 FFmpegContent::examine (shared_ptr<Job> job)
154 {
155         job->set_progress_unknown ();
156
157         Content::examine (job);
158
159         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
160         take_from_video_examiner (examiner);
161
162         shared_ptr<const Film> film = _film.lock ();
163         DCPOMATIC_ASSERT (film);
164
165         {
166                 boost::mutex::scoped_lock lm (_mutex);
167
168                 _subtitle_streams = examiner->subtitle_streams ();
169                 if (!_subtitle_streams.empty ()) {
170                         _subtitle_stream = _subtitle_streams.front ();
171                 }
172                 
173                 _audio_streams = examiner->audio_streams ();
174
175                 if (!_audio_streams.empty ()) {
176                         _audio_streams.front()->mapping().make_default ();
177                 }
178
179                 _first_video = examiner->first_video ();
180         }
181
182         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
183         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
184         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
185 }
186
187 string
188 FFmpegContent::summary () const
189 {
190         /* Get the string() here so that the name does not have quotes around it */
191         return String::compose (_("%1 [movie]"), path_summary ());
192 }
193
194 string
195 FFmpegContent::technical_summary () const
196 {
197         string as = "";
198         BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, ffmpeg_audio_streams ()) {
199                 as += i->technical_summary () + " " ;
200         }
201
202         if (as.empty ()) {
203                 as = "none";
204         }
205
206         string ss = "none";
207         if (_subtitle_stream) {
208                 ss = _subtitle_stream->technical_summary ();
209         }
210
211         string filt = Filter::ffmpeg_string (_filters);
212         
213         return Content::technical_summary() + " - "
214                 + VideoContent::technical_summary() + " - "
215                 + AudioContent::technical_summary() + " - "
216                 + String::compose (
217                         "ffmpeg: audio %1 subtitle %2 filters %3", as, ss, filt
218                         );
219 }
220
221 void
222 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
223 {
224         {
225                 boost::mutex::scoped_lock lm (_mutex);
226                 _subtitle_stream = s;
227         }
228
229         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
230 }
231
232 bool
233 operator== (FFmpegStream const & a, FFmpegStream const & b)
234 {
235         return a._id == b._id;
236 }
237
238 bool
239 operator!= (FFmpegStream const & a, FFmpegStream const & b)
240 {
241         return a._id != b._id;
242 }
243
244 DCPTime
245 FFmpegContent::full_length () const
246 {
247         shared_ptr<const Film> film = _film.lock ();
248         DCPOMATIC_ASSERT (film);
249         FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
250         return DCPTime::from_frames (rint (video_length_after_3d_combine() * frc.factor()), film->video_frame_rate());
251 }
252
253 void
254 FFmpegContent::set_filters (vector<Filter const *> const & filters)
255 {
256         {
257                 boost::mutex::scoped_lock lm (_mutex);
258                 _filters = filters;
259         }
260
261         signal_changed (FFmpegContentProperty::FILTERS);
262 }
263
264 string
265 FFmpegContent::identifier () const
266 {
267         SafeStringStream s;
268
269         s << VideoContent::identifier();
270
271         boost::mutex::scoped_lock lm (_mutex);
272
273         if (_subtitle_stream) {
274                 s << "_" << _subtitle_stream->identifier ();
275         }
276
277         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
278                 s << "_" << (*i)->id ();
279         }
280
281         return s.str ();
282 }
283
284 list<ContentTimePeriod>
285 FFmpegContent::subtitles_during (ContentTimePeriod period, bool starting) const
286 {
287         shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
288         if (!stream) {
289                 return list<ContentTimePeriod> ();
290         }
291
292         return stream->subtitles_during (period, starting);
293 }
294
295 bool
296 FFmpegContent::has_subtitles () const
297 {
298         return !subtitle_streams().empty ();
299 }
300
301 void
302 FFmpegContent::set_default_colour_conversion ()
303 {
304         dcp::Size const s = video_size ();
305
306         boost::mutex::scoped_lock lm (_mutex);
307
308         if (s.width < 1080) {
309                 _colour_conversion = PresetColourConversion::from_id ("rec601").conversion;
310         } else {
311                 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
312         }
313 }
314
315 vector<AudioStreamPtr>
316 FFmpegContent::audio_streams () const
317 {
318         boost::mutex::scoped_lock lm (_mutex);
319         
320         vector<AudioStreamPtr> s;
321         copy (_audio_streams.begin(), _audio_streams.end(), back_inserter (s));
322         return s;
323 }