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