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