Move FFmpegStream classes into their own source files.
[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 <dcp/raw_convert.h>
25 #include "ffmpeg_content.h"
26 #include "ffmpeg_examiner.h"
27 #include "ffmpeg_subtitle_stream.h"
28 #include "ffmpeg_audio_stream.h"
29 #include "compose.hpp"
30 #include "job.h"
31 #include "util.h"
32 #include "filter.h"
33 #include "film.h"
34 #include "log.h"
35 #include "exceptions.h"
36
37 #include "i18n.h"
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 dcp::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, cxml::ConstNodePtr 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().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<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
170         take_from_video_examiner (examiner);
171
172         ContentTime video_length = examiner->video_length ();
173
174         shared_ptr<const Film> film = _film.lock ();
175         assert (film);
176         film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ())));
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         signal_changed (ContentProperty::LENGTH);
197         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
198         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
199         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
200         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
201         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
202 }
203
204 string
205 FFmpegContent::summary () const
206 {
207         /* Get the string() here so that the name does not have quotes around it */
208         return String::compose (_("%1 [movie]"), path_summary ());
209 }
210
211 string
212 FFmpegContent::technical_summary () const
213 {
214         string as = "none";
215         if (_audio_stream) {
216                 as = _audio_stream->technical_summary ();
217         }
218
219         string ss = "none";
220         if (_subtitle_stream) {
221                 ss = _subtitle_stream->technical_summary ();
222         }
223
224         string filt = Filter::ffmpeg_string (_filters);
225         
226         return Content::technical_summary() + " - "
227                 + VideoContent::technical_summary() + " - "
228                 + AudioContent::technical_summary() + " - "
229                 + String::compose (
230                         "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
231                         );
232 }
233
234 string
235 FFmpegContent::information () const
236 {
237         if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
238                 return "";
239         }
240         
241         stringstream s;
242         
243         s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine().frames (video_frame_rate()), video_frame_rate()) << "\n";
244         s << VideoContent::information ();
245
246         return s.str ();
247 }
248
249 void
250 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
251 {
252         {
253                 boost::mutex::scoped_lock lm (_mutex);
254                 _subtitle_stream = s;
255         }
256
257         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
258 }
259
260 void
261 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
262 {
263         {
264                 boost::mutex::scoped_lock lm (_mutex);
265                 _audio_stream = s;
266         }
267
268         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
269 }
270
271 ContentTime
272 FFmpegContent::audio_length () const
273 {
274         if (!audio_stream ()) {
275                 return ContentTime ();
276         }
277
278         return video_length ();
279 }
280
281 int
282 FFmpegContent::audio_channels () const
283 {
284         boost::mutex::scoped_lock lm (_mutex);
285         
286         if (!_audio_stream) {
287                 return 0;
288         }
289
290         return _audio_stream->channels;
291 }
292
293 int
294 FFmpegContent::audio_frame_rate () const
295 {
296         boost::mutex::scoped_lock lm (_mutex);
297
298         if (!_audio_stream) {
299                 return 0;
300         }
301
302         return _audio_stream->frame_rate;
303 }
304
305 bool
306 operator== (FFmpegStream const & a, FFmpegStream const & b)
307 {
308         return a._id == b._id;
309 }
310
311 bool
312 operator!= (FFmpegStream const & a, FFmpegStream const & b)
313 {
314         return a._id != b._id;
315 }
316
317 DCPTime
318 FFmpegContent::full_length () const
319 {
320         shared_ptr<const Film> film = _film.lock ();
321         assert (film);
322         return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
323 }
324
325 AudioMapping
326 FFmpegContent::audio_mapping () const
327 {
328         boost::mutex::scoped_lock lm (_mutex);
329
330         if (!_audio_stream) {
331                 return AudioMapping ();
332         }
333
334         return _audio_stream->mapping;
335 }
336
337 void
338 FFmpegContent::set_filters (vector<Filter const *> const & filters)
339 {
340         {
341                 boost::mutex::scoped_lock lm (_mutex);
342                 _filters = filters;
343         }
344
345         signal_changed (FFmpegContentProperty::FILTERS);
346 }
347
348 void
349 FFmpegContent::set_audio_mapping (AudioMapping m)
350 {
351         audio_stream()->mapping = m;
352         AudioContent::set_audio_mapping (m);
353 }
354
355 string
356 FFmpegContent::identifier () const
357 {
358         stringstream s;
359
360         s << VideoContent::identifier();
361
362         boost::mutex::scoped_lock lm (_mutex);
363
364         if (_subtitle_stream) {
365                 s << "_" << _subtitle_stream->identifier ();
366         }
367
368         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
369                 s << "_" << (*i)->id ();
370         }
371
372         return s.str ();
373 }
374
375 boost::filesystem::path
376 FFmpegContent::audio_analysis_path () const
377 {
378         shared_ptr<const Film> film = _film.lock ();
379         if (!film) {
380                 return boost::filesystem::path ();
381         }
382
383         /* We need to include the stream ID in this path so that we get different
384            analyses for each stream.
385         */
386
387         boost::filesystem::path p = film->audio_analysis_dir ();
388         string name = digest ();
389         if (audio_stream ()) {
390                 name += "_" + audio_stream()->identifier ();
391         }
392         p /= name;
393         return p;
394 }