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