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