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