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