All audio content should resample if the output frame rate and content
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013-2014 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 extern "C" {
21 #include <libavformat/avformat.h>
22 }
23 #include <libcxml/cxml.h>
24 #include <libdcp/raw_convert.h>
25 #include "ffmpeg_content.h"
26 #include "ffmpeg_examiner.h"
27 #include "compose.hpp"
28 #include "job.h"
29 #include "util.h"
30 #include "filter.h"
31 #include "film.h"
32 #include "log.h"
33 #include "exceptions.h"
34
35 #include "i18n.h"
36
37 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
38
39 using std::string;
40 using std::stringstream;
41 using std::vector;
42 using std::list;
43 using std::cout;
44 using std::pair;
45 using boost::shared_ptr;
46 using boost::dynamic_pointer_cast;
47 using libdcp::raw_convert;
48
49 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
50 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
51 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
52 int const FFmpegContentProperty::AUDIO_STREAM = 103;
53 int const FFmpegContentProperty::FILTERS = 104;
54
55 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
56         : Content (f, p)
57         , VideoContent (f, p)
58         , AudioContent (f, p)
59         , SubtitleContent (f, p)
60 {
61
62 }
63
64 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
65         : Content (f, node)
66         , VideoContent (f, node, version)
67         , AudioContent (f, node)
68         , SubtitleContent (f, node, version)
69 {
70         list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
71         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
72                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
73                 if ((*i)->optional_number_child<int> ("Selected")) {
74                         _subtitle_stream = _subtitle_streams.back ();
75                 }
76         }
77
78         c = node->node_children ("AudioStream");
79         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
80                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
81                 if ((*i)->optional_number_child<int> ("Selected")) {
82                         _audio_stream = _audio_streams.back ();
83                 }
84         }
85
86         c = node->node_children ("Filter");
87         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
88                 Filter const * f = Filter::from_id ((*i)->content ());
89                 if (f) {
90                         _filters.push_back (f);
91                 } else {
92                         notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
93                 }
94         }
95
96         _first_video = node->optional_number_child<double> ("FirstVideo");
97 }
98
99 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
100         : Content (f, c)
101         , VideoContent (f, c)
102         , AudioContent (f, c)
103         , SubtitleContent (f, c)
104 {
105         shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
106         assert (ref);
107
108         for (size_t i = 0; i < c.size(); ++i) {
109                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
110                 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
111                         throw JoinError (_("Content to be joined must use the same subtitle stream."));
112                 }
113
114                 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
115                         throw JoinError (_("Content to be joined must use the same audio stream."));
116                 }
117         }
118
119         _subtitle_streams = ref->subtitle_streams ();
120         _subtitle_stream = ref->subtitle_stream ();
121         _audio_streams = ref->audio_streams ();
122         _audio_stream = ref->audio_stream ();
123         _first_video = ref->_first_video;
124 }
125
126 void
127 FFmpegContent::as_xml (xmlpp::Node* node) const
128 {
129         node->add_child("Type")->add_child_text ("FFmpeg");
130         Content::as_xml (node);
131         VideoContent::as_xml (node);
132         AudioContent::as_xml (node);
133         SubtitleContent::as_xml (node);
134
135         boost::mutex::scoped_lock lm (_mutex);
136
137         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
138                 xmlpp::Node* t = node->add_child("SubtitleStream");
139                 if (_subtitle_stream && *i == _subtitle_stream) {
140                         t->add_child("Selected")->add_child_text("1");
141                 }
142                 (*i)->as_xml (t);
143         }
144
145         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
146                 xmlpp::Node* t = node->add_child("AudioStream");
147                 if (_audio_stream && *i == _audio_stream) {
148                         t->add_child("Selected")->add_child_text("1");
149                 }
150                 (*i)->as_xml (t);
151         }
152
153         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
154                 node->add_child("Filter")->add_child_text ((*i)->id ());
155         }
156
157         if (_first_video) {
158                 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get ()));
159         }
160 }
161
162 void
163 FFmpegContent::examine (shared_ptr<Job> job)
164 {
165         job->set_progress_unknown ();
166
167         Content::examine (job);
168
169         shared_ptr<const Film> film = _film.lock ();
170         assert (film);
171
172         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
173
174         VideoContent::Frame video_length = 0;
175         video_length = examiner->video_length ();
176         LOG_GENERAL ("Video length obtained from header as %1 frames", video_length);
177
178         {
179                 boost::mutex::scoped_lock lm (_mutex);
180
181                 _video_length = video_length;
182
183                 _subtitle_streams = examiner->subtitle_streams ();
184                 if (!_subtitle_streams.empty ()) {
185                         _subtitle_stream = _subtitle_streams.front ();
186                 }
187                 
188                 _audio_streams = examiner->audio_streams ();
189                 if (!_audio_streams.empty ()) {
190                         _audio_stream = _audio_streams.front ();
191                 }
192
193                 _first_video = examiner->first_video ();
194         }
195
196         take_from_video_examiner (examiner);
197
198         signal_changed (ContentProperty::LENGTH);
199         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
200         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
201         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
202         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
203         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
204 }
205
206 string
207 FFmpegContent::summary () const
208 {
209         /* Get the string() here so that the name does not have quotes around it */
210         return String::compose (_("%1 [movie]"), path_summary ());
211 }
212
213 string
214 FFmpegContent::technical_summary () const
215 {
216         string as = "none";
217         if (_audio_stream) {
218                 as = _audio_stream->technical_summary ();
219         }
220
221         string ss = "none";
222         if (_subtitle_stream) {
223                 ss = _subtitle_stream->technical_summary ();
224         }
225
226         string filt = Filter::ffmpeg_string (_filters);
227         
228         return Content::technical_summary() + " - "
229                 + VideoContent::technical_summary() + " - "
230                 + AudioContent::technical_summary() + " - "
231                 + String::compose (
232                         "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
233                         );
234 }
235
236 string
237 FFmpegContent::information () const
238 {
239         if (video_length() == 0 || video_frame_rate() == 0) {
240                 return "";
241         }
242         
243         stringstream s;
244         
245         s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine(), video_frame_rate()) << "\n";
246         s << VideoContent::information ();
247
248         return s.str ();
249 }
250
251 void
252 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
253 {
254         {
255                 boost::mutex::scoped_lock lm (_mutex);
256                 _subtitle_stream = s;
257         }
258
259         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
260 }
261
262 void
263 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
264 {
265         {
266                 boost::mutex::scoped_lock lm (_mutex);
267                 _audio_stream = s;
268         }
269
270         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
271 }
272
273 AudioContent::Frame
274 FFmpegContent::audio_length () const
275 {
276         int const cafr = content_audio_frame_rate ();
277         float const vfr = video_frame_rate ();
278         VideoContent::Frame const vl = video_length_after_3d_combine ();
279
280         boost::mutex::scoped_lock lm (_mutex);
281         if (!_audio_stream) {
282                 return 0;
283         }
284         
285         return video_frames_to_audio_frames (vl, cafr, vfr);
286 }
287
288 int
289 FFmpegContent::audio_channels () const
290 {
291         boost::mutex::scoped_lock lm (_mutex);
292         
293         if (!_audio_stream) {
294                 return 0;
295         }
296
297         return _audio_stream->channels;
298 }
299
300 int
301 FFmpegContent::content_audio_frame_rate () const
302 {
303         boost::mutex::scoped_lock lm (_mutex);
304
305         if (!_audio_stream) {
306                 return 0;
307         }
308
309         return _audio_stream->frame_rate;
310 }
311
312 bool
313 operator== (FFmpegStream const & a, FFmpegStream const & b)
314 {
315         return a._id == b._id;
316 }
317
318 bool
319 operator!= (FFmpegStream const & a, FFmpegStream const & b)
320 {
321         return a._id != b._id;
322 }
323
324 FFmpegStream::FFmpegStream (shared_ptr<const cxml::Node> node)
325         : name (node->string_child ("Name"))
326         , _id (node->number_child<int> ("Id"))
327 {
328
329 }
330
331 void
332 FFmpegStream::as_xml (xmlpp::Node* root) const
333 {
334         root->add_child("Name")->add_child_text (name);
335         root->add_child("Id")->add_child_text (raw_convert<string> (_id));
336 }
337
338 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node, int version)
339         : FFmpegStream (node)
340         , mapping (node->node_child ("Mapping"), version)
341 {
342         frame_rate = node->number_child<int> ("FrameRate");
343         channels = node->number_child<int64_t> ("Channels");
344         first_audio = node->optional_number_child<double> ("FirstAudio");
345 }
346
347 void
348 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
349 {
350         FFmpegStream::as_xml (root);
351         root->add_child("FrameRate")->add_child_text (raw_convert<string> (frame_rate));
352         root->add_child("Channels")->add_child_text (raw_convert<string> (channels));
353         if (first_audio) {
354                 root->add_child("FirstAudio")->add_child_text (raw_convert<string> (first_audio.get ()));
355         }
356         mapping.as_xml (root->add_child("Mapping"));
357 }
358
359 bool
360 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
361 {
362         size_t i = 0;
363         while (i < fc->nb_streams) {
364                 if (fc->streams[i]->id == _id) {
365                         return int (i) == index;
366                 }
367                 ++i;
368         }
369
370         return false;
371 }
372
373 AVStream *
374 FFmpegStream::stream (AVFormatContext const * fc) const
375 {
376         size_t i = 0;
377         while (i < fc->nb_streams) {
378                 if (fc->streams[i]->id == _id) {
379                         return fc->streams[i];
380                 }
381                 ++i;
382         }
383
384         assert (false);
385         return 0;
386 }
387
388 /** Construct a SubtitleStream from a value returned from to_string().
389  *  @param t String returned from to_string().
390  *  @param v State file version.
391  */
392 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
393         : FFmpegStream (node)
394 {
395         
396 }
397
398 void
399 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
400 {
401         FFmpegStream::as_xml (root);
402 }
403
404 Time
405 FFmpegContent::full_length () const
406 {
407         shared_ptr<const Film> film = _film.lock ();
408         assert (film);
409         
410         FrameRateConversion frc (video_frame_rate (), film->video_frame_rate ());
411         return video_length_after_3d_combine() * frc.factor() * TIME_HZ / film->video_frame_rate ();
412 }
413
414 AudioMapping
415 FFmpegContent::audio_mapping () const
416 {
417         boost::mutex::scoped_lock lm (_mutex);
418
419         if (!_audio_stream) {
420                 return AudioMapping ();
421         }
422
423         return _audio_stream->mapping;
424 }
425
426 void
427 FFmpegContent::set_filters (vector<Filter const *> const & filters)
428 {
429         {
430                 boost::mutex::scoped_lock lm (_mutex);
431                 _filters = filters;
432         }
433
434         signal_changed (FFmpegContentProperty::FILTERS);
435 }
436
437 void
438 FFmpegContent::set_audio_mapping (AudioMapping m)
439 {
440         audio_stream()->mapping = m;
441         signal_changed (AudioContentProperty::AUDIO_MAPPING);
442 }
443
444 string
445 FFmpegContent::identifier () const
446 {
447         stringstream s;
448
449         s << VideoContent::identifier();
450
451         boost::mutex::scoped_lock lm (_mutex);
452
453         if (_subtitle_stream) {
454                 s << "_" << _subtitle_stream->identifier ();
455         }
456
457         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
458                 s << "_" << (*i)->id ();
459         }
460
461         return s.str ();
462 }
463
464 boost::filesystem::path
465 FFmpegContent::audio_analysis_path () const
466 {
467         shared_ptr<const Film> film = _film.lock ();
468         if (!film) {
469                 return boost::filesystem::path ();
470         }
471
472         /* We need to include the stream ID in this path so that we get different
473            analyses for each stream.
474         */
475
476         boost::filesystem::path p = film->audio_analysis_dir ();
477         string name = digest ();
478         if (audio_stream ()) {
479                 name += "_" + audio_stream()->identifier ();
480         }
481         p /= name;
482         return p;
483 }