Improve audio mapping handling a bit.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <libcxml/cxml.h>
23 #include "ffmpeg_content.h"
24 #include "ffmpeg_decoder.h"
25 #include "compose.hpp"
26 #include "job.h"
27 #include "util.h"
28 #include "filter.h"
29 #include "log.h"
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::stringstream;
35 using std::vector;
36 using std::list;
37 using std::cout;
38 using boost::shared_ptr;
39 using boost::lexical_cast;
40
41 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
42 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
43 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
44 int const FFmpegContentProperty::AUDIO_STREAM = 103;
45 int const FFmpegContentProperty::FILTERS = 104;
46
47 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
48         : Content (f, p)
49         , VideoContent (f, p)
50         , AudioContent (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 {
60         list<shared_ptr<cxml::Node> > c = node->node_children ("SubtitleStream");
61         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
62                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
63                 if ((*i)->optional_number_child<int> ("Selected")) {
64                         _subtitle_stream = _subtitle_streams.back ();
65                 }
66         }
67
68         c = node->node_children ("AudioStream");
69         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
70                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i)));
71                 if ((*i)->optional_number_child<int> ("Selected")) {
72                         _audio_stream = _audio_streams.back ();
73                 }
74         }
75
76         c = node->node_children ("Filter");
77         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
78                 _filters.push_back (Filter::from_id ((*i)->content ()));
79         }
80 }
81
82 FFmpegContent::FFmpegContent (FFmpegContent const & o)
83         : Content (o)
84         , VideoContent (o)
85         , AudioContent (o)
86         , _subtitle_streams (o._subtitle_streams)
87         , _subtitle_stream (o._subtitle_stream)
88         , _audio_streams (o._audio_streams)
89         , _audio_stream (o._audio_stream)
90 {
91
92 }
93
94 void
95 FFmpegContent::as_xml (xmlpp::Node* node) const
96 {
97         node->add_child("Type")->add_child_text ("FFmpeg");
98         Content::as_xml (node);
99         VideoContent::as_xml (node);
100         AudioContent::as_xml (node);
101
102         boost::mutex::scoped_lock lm (_mutex);
103
104         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
105                 xmlpp::Node* t = node->add_child("SubtitleStream");
106                 if (_subtitle_stream && *i == _subtitle_stream) {
107                         t->add_child("Selected")->add_child_text("1");
108                 }
109                 (*i)->as_xml (t);
110         }
111
112         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
113                 xmlpp::Node* t = node->add_child("AudioStream");
114                 if (_audio_stream && *i == _audio_stream) {
115                         t->add_child("Selected")->add_child_text("1");
116                 }
117                 (*i)->as_xml (t);
118         }
119
120         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
121                 node->add_child("Filter")->add_child_text ((*i)->id ());
122         }
123 }
124
125 void
126 FFmpegContent::examine (shared_ptr<Job> job)
127 {
128         job->set_progress_unknown ();
129
130         Content::examine (job);
131
132         shared_ptr<const Film> film = _film.lock ();
133         assert (film);
134
135         shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false));
136
137         ContentVideoFrame video_length = 0;
138         video_length = decoder->video_length ();
139         film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
140
141         {
142                 boost::mutex::scoped_lock lm (_mutex);
143
144                 _video_length = video_length;
145
146                 _subtitle_streams = decoder->subtitle_streams ();
147                 if (!_subtitle_streams.empty ()) {
148                         _subtitle_stream = _subtitle_streams.front ();
149                 }
150                 
151                 _audio_streams = decoder->audio_streams ();
152                 if (!_audio_streams.empty ()) {
153                         _audio_stream = _audio_streams.front ();
154                 }
155         }
156
157         take_from_video_decoder (decoder);
158
159         signal_changed (VideoContentProperty::VIDEO_LENGTH);
160         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
161         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
162         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
163         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
164         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
165 }
166
167 string
168 FFmpegContent::summary () const
169 {
170         return String::compose (_("Movie: %1"), file().filename().string());
171 }
172
173 string
174 FFmpegContent::information () const
175 {
176         if (video_length() == 0 || video_frame_rate() == 0) {
177                 return "";
178         }
179         
180         stringstream s;
181         
182         s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
183         s << VideoContent::information ();
184
185         return s.str ();
186 }
187
188 void
189 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
190 {
191         {
192                 boost::mutex::scoped_lock lm (_mutex);
193                 _subtitle_stream = s;
194         }
195
196         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
197 }
198
199 void
200 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
201 {
202         {
203                 boost::mutex::scoped_lock lm (_mutex);
204                 _audio_stream = s;
205         }
206
207         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
208 }
209
210 ContentAudioFrame
211 FFmpegContent::audio_length () const
212 {
213         int const cafr = content_audio_frame_rate ();
214         int const vfr  = video_frame_rate ();
215         ContentVideoFrame const vl = video_length ();
216
217         boost::mutex::scoped_lock lm (_mutex);
218         if (!_audio_stream) {
219                 return 0;
220         }
221         
222         return video_frames_to_audio_frames (vl, cafr, vfr);
223 }
224
225 int
226 FFmpegContent::audio_channels () const
227 {
228         boost::mutex::scoped_lock lm (_mutex);
229         
230         if (!_audio_stream) {
231                 return 0;
232         }
233
234         return _audio_stream->channels;
235 }
236
237 int
238 FFmpegContent::content_audio_frame_rate () const
239 {
240         boost::mutex::scoped_lock lm (_mutex);
241
242         if (!_audio_stream) {
243                 return 0;
244         }
245
246         return _audio_stream->frame_rate;
247 }
248
249 int
250 FFmpegContent::output_audio_frame_rate () const
251 {
252         shared_ptr<const Film> film = _film.lock ();
253         assert (film);
254         
255         /* Resample to a DCI-approved sample rate */
256         double t = dcp_audio_frame_rate (content_audio_frame_rate ());
257
258         FrameRateConversion frc (video_frame_rate(), film->dcp_video_frame_rate());
259
260         /* Compensate if the DCP is being run at a different frame rate
261            to the source; that is, if the video is run such that it will
262            look different in the DCP compared to the source (slower or faster).
263            skip/repeat doesn't come into effect here.
264         */
265
266         if (frc.change_speed) {
267                 t *= video_frame_rate() * frc.factor() / film->dcp_video_frame_rate();
268         }
269
270         return rint (t);
271 }
272
273 bool
274 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
275 {
276         return a.id == b.id;
277 }
278
279 bool
280 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
281 {
282         return a.id == b.id;
283 }
284
285 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
286 {
287         name = node->string_child ("Name");
288         id = node->number_child<int> ("Id");
289         frame_rate = node->number_child<int> ("FrameRate");
290         channels = node->number_child<int64_t> ("Channels");
291         mapping = AudioMapping (node->node_child ("Mapping"));
292 }
293
294 void
295 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
296 {
297         root->add_child("Name")->add_child_text (name);
298         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
299         root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
300         root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
301         mapping.as_xml (root->add_child("Mapping"));
302 }
303
304 /** Construct a SubtitleStream from a value returned from to_string().
305  *  @param t String returned from to_string().
306  *  @param v State file version.
307  */
308 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
309 {
310         name = node->string_child ("Name");
311         id = node->number_child<int> ("Id");
312 }
313
314 void
315 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
316 {
317         root->add_child("Name")->add_child_text (name);
318         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
319 }
320
321 shared_ptr<Content>
322 FFmpegContent::clone () const
323 {
324         return shared_ptr<Content> (new FFmpegContent (*this));
325 }
326
327 Time
328 FFmpegContent::length () const
329 {
330         shared_ptr<const Film> film = _film.lock ();
331         assert (film);
332         
333         FrameRateConversion frc (video_frame_rate (), film->dcp_video_frame_rate ());
334         return video_length() * frc.factor() * TIME_HZ / film->dcp_video_frame_rate ();
335 }
336
337 AudioMapping
338 FFmpegContent::audio_mapping () const
339 {
340         boost::mutex::scoped_lock lm (_mutex);
341
342         if (!_audio_stream) {
343                 return AudioMapping ();
344         }
345
346         return _audio_stream->mapping;
347 }
348
349 void
350 FFmpegContent::set_filters (vector<Filter const *> const & filters)
351 {
352         {
353                 boost::mutex::scoped_lock lm (_mutex);
354                 _filters = filters;
355         }
356
357         signal_changed (FFmpegContentProperty::FILTERS);
358 }
359
360 void
361 FFmpegContent::set_audio_mapping (AudioMapping m)
362 {
363         audio_stream()->mapping = m;
364         signal_changed (AudioContentProperty::AUDIO_MAPPING);
365 }