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