More noncopyable.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013 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 <libcxml/cxml.h>
21 #include "ffmpeg_content.h"
22 #include "ffmpeg_examiner.h"
23 #include "compose.hpp"
24 #include "job.h"
25 #include "util.h"
26 #include "filter.h"
27 #include "film.h"
28 #include "log.h"
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::stringstream;
34 using std::vector;
35 using std::list;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::lexical_cast;
39
40 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
41 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
42 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
43 int const FFmpegContentProperty::AUDIO_STREAM = 103;
44 int const FFmpegContentProperty::FILTERS = 104;
45
46 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
47         : Content (f, p)
48         , VideoContent (f, p)
49         , AudioContent (f, p)
50         , SubtitleContent (f, p)
51 {
52
53 }
54
55 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
56         : Content (f, node)
57         , VideoContent (f, node)
58         , AudioContent (f, node)
59         , SubtitleContent (f, node)
60 {
61         list<shared_ptr<cxml::Node> > c = node->node_children ("SubtitleStream");
62         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
63                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
64                 if ((*i)->optional_number_child<int> ("Selected")) {
65                         _subtitle_stream = _subtitle_streams.back ();
66                 }
67         }
68
69         c = node->node_children ("AudioStream");
70         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
71                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i)));
72                 if ((*i)->optional_number_child<int> ("Selected")) {
73                         _audio_stream = _audio_streams.back ();
74                 }
75         }
76
77         c = node->node_children ("Filter");
78         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
79                 _filters.push_back (Filter::from_id ((*i)->content ()));
80         }
81
82         _first_video = node->optional_number_child<double> ("FirstVideo");
83 }
84
85 void
86 FFmpegContent::as_xml (xmlpp::Node* node) const
87 {
88         node->add_child("Type")->add_child_text ("FFmpeg");
89         Content::as_xml (node);
90         VideoContent::as_xml (node);
91         AudioContent::as_xml (node);
92         SubtitleContent::as_xml (node);
93
94         boost::mutex::scoped_lock lm (_mutex);
95
96         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
97                 xmlpp::Node* t = node->add_child("SubtitleStream");
98                 if (_subtitle_stream && *i == _subtitle_stream) {
99                         t->add_child("Selected")->add_child_text("1");
100                 }
101                 (*i)->as_xml (t);
102         }
103
104         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
105                 xmlpp::Node* t = node->add_child("AudioStream");
106                 if (_audio_stream && *i == _audio_stream) {
107                         t->add_child("Selected")->add_child_text("1");
108                 }
109                 (*i)->as_xml (t);
110         }
111
112         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
113                 node->add_child("Filter")->add_child_text ((*i)->id ());
114         }
115
116         if (_first_video) {
117                 node->add_child("FirstVideo")->add_child_text (lexical_cast<string> (_first_video.get ()));
118         }
119 }
120
121 void
122 FFmpegContent::examine (shared_ptr<Job> job)
123 {
124         job->set_progress_unknown ();
125
126         Content::examine (job);
127
128         shared_ptr<const Film> film = _film.lock ();
129         assert (film);
130
131         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
132
133         VideoContent::Frame video_length = 0;
134         video_length = examiner->video_length ();
135         film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length));
136
137         {
138                 boost::mutex::scoped_lock lm (_mutex);
139
140                 _video_length = video_length;
141
142                 _subtitle_streams = examiner->subtitle_streams ();
143                 if (!_subtitle_streams.empty ()) {
144                         _subtitle_stream = _subtitle_streams.front ();
145                 }
146                 
147                 _audio_streams = examiner->audio_streams ();
148                 if (!_audio_streams.empty ()) {
149                         _audio_stream = _audio_streams.front ();
150                 }
151
152                 _first_video = examiner->first_video ();
153         }
154
155         take_from_video_examiner (examiner);
156
157         signal_changed (ContentProperty::LENGTH);
158         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
159         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
160         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
161         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
162         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
163 }
164
165 string
166 FFmpegContent::summary () const
167 {
168         return String::compose (_("Movie: %1"), file().filename().string());
169 }
170
171 string
172 FFmpegContent::information () const
173 {
174         if (video_length() == 0 || video_frame_rate() == 0) {
175                 return "";
176         }
177         
178         stringstream s;
179         
180         s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
181         s << VideoContent::information ();
182
183         return s.str ();
184 }
185
186 void
187 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
188 {
189         {
190                 boost::mutex::scoped_lock lm (_mutex);
191                 _subtitle_stream = s;
192         }
193
194         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
195 }
196
197 void
198 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
199 {
200         {
201                 boost::mutex::scoped_lock lm (_mutex);
202                 _audio_stream = s;
203         }
204
205         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
206 }
207
208 AudioContent::Frame
209 FFmpegContent::audio_length () const
210 {
211         int const cafr = content_audio_frame_rate ();
212         int const vfr  = video_frame_rate ();
213         VideoContent::Frame const vl = video_length ();
214
215         boost::mutex::scoped_lock lm (_mutex);
216         if (!_audio_stream) {
217                 return 0;
218         }
219         
220         return video_frames_to_audio_frames (vl, cafr, vfr);
221 }
222
223 int
224 FFmpegContent::audio_channels () const
225 {
226         boost::mutex::scoped_lock lm (_mutex);
227         
228         if (!_audio_stream) {
229                 return 0;
230         }
231
232         return _audio_stream->channels;
233 }
234
235 int
236 FFmpegContent::content_audio_frame_rate () const
237 {
238         boost::mutex::scoped_lock lm (_mutex);
239
240         if (!_audio_stream) {
241                 return 0;
242         }
243
244         return _audio_stream->frame_rate;
245 }
246
247 int
248 FFmpegContent::output_audio_frame_rate () const
249 {
250         shared_ptr<const Film> film = _film.lock ();
251         assert (film);
252         
253         /* Resample to a DCI-approved sample rate */
254         double t = dcp_audio_frame_rate (content_audio_frame_rate ());
255
256         FrameRateConversion frc (video_frame_rate(), film->dcp_video_frame_rate());
257
258         /* Compensate if the DCP is being run at a different frame rate
259            to the source; that is, if the video is run such that it will
260            look different in the DCP compared to the source (slower or faster).
261            skip/repeat doesn't come into effect here.
262         */
263
264         if (frc.change_speed) {
265                 t *= video_frame_rate() * frc.factor() / film->dcp_video_frame_rate();
266         }
267
268         return rint (t);
269 }
270
271 bool
272 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
273 {
274         return a.id == b.id;
275 }
276
277 bool
278 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
279 {
280         return a.id == b.id;
281 }
282
283 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
284 {
285         name = node->string_child ("Name");
286         id = node->number_child<int> ("Id");
287         frame_rate = node->number_child<int> ("FrameRate");
288         channels = node->number_child<int64_t> ("Channels");
289         mapping = AudioMapping (node->node_child ("Mapping"));
290         first_audio = node->optional_number_child<double> ("FirstAudio");
291 }
292
293 void
294 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
295 {
296         root->add_child("Name")->add_child_text (name);
297         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
298         root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
299         root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
300         if (first_audio) {
301                 root->add_child("FirstAudio")->add_child_text (lexical_cast<string> (first_audio.get ()));
302         }
303         mapping.as_xml (root->add_child("Mapping"));
304 }
305
306 /** Construct a SubtitleStream from a value returned from to_string().
307  *  @param t String returned from to_string().
308  *  @param v State file version.
309  */
310 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
311 {
312         name = node->string_child ("Name");
313         id = node->number_child<int> ("Id");
314 }
315
316 void
317 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
318 {
319         root->add_child("Name")->add_child_text (name);
320         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
321 }
322
323 Time
324 FFmpegContent::length () const
325 {
326         shared_ptr<const Film> film = _film.lock ();
327         assert (film);
328         
329         FrameRateConversion frc (video_frame_rate (), film->dcp_video_frame_rate ());
330         return video_length() * frc.factor() * TIME_HZ / film->dcp_video_frame_rate ();
331 }
332
333 AudioMapping
334 FFmpegContent::audio_mapping () const
335 {
336         boost::mutex::scoped_lock lm (_mutex);
337
338         if (!_audio_stream) {
339                 return AudioMapping ();
340         }
341
342         return _audio_stream->mapping;
343 }
344
345 void
346 FFmpegContent::set_filters (vector<Filter const *> const & filters)
347 {
348         {
349                 boost::mutex::scoped_lock lm (_mutex);
350                 _filters = filters;
351         }
352
353         signal_changed (FFmpegContentProperty::FILTERS);
354 }
355
356 void
357 FFmpegContent::set_audio_mapping (AudioMapping m)
358 {
359         audio_stream()->mapping = m;
360         signal_changed (AudioContentProperty::AUDIO_MAPPING);
361 }
362
363 string
364 FFmpegContent::identifier () const
365 {
366         stringstream s;
367
368         s << VideoContent::identifier();
369
370         boost::mutex::scoped_lock lm (_mutex);
371
372         if (_subtitle_stream) {
373                 s << "_" << _subtitle_stream->id;
374         }
375
376         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
377                 s << "_" << (*i)->id ();
378         }
379
380         return s.str ();
381 }