Merge master; fix destruction of Server; some test cleanups.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013-2014 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 extern "C" {
21 #include <libavformat/avformat.h>
22 }
23 #include <libcxml/cxml.h>
24 #include <dcp/raw_convert.h>
25 #include "ffmpeg_content.h"
26 #include "ffmpeg_examiner.h"
27 #include "ffmpeg_subtitle_stream.h"
28 #include "ffmpeg_audio_stream.h"
29 #include "compose.hpp"
30 #include "job.h"
31 #include "util.h"
32 #include "filter.h"
33 #include "film.h"
34 #include "log.h"
35 #include "exceptions.h"
36 #include "frame_rate_change.h"
37
38 #include "i18n.h"
39
40 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
41
42 using std::string;
43 using std::stringstream;
44 using std::vector;
45 using std::list;
46 using std::cout;
47 using std::pair;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50 using dcp::raw_convert;
51
52 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
53 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
54 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
55 int const FFmpegContentProperty::AUDIO_STREAM = 103;
56 int const FFmpegContentProperty::FILTERS = 104;
57
58 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
59         : Content (f, p)
60         , VideoContent (f, p)
61         , AudioContent (f, p)
62         , SubtitleContent (f, p)
63 {
64
65 }
66
67 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version, list<string>& notes)
68         : Content (f, node)
69         , VideoContent (f, node, version)
70         , AudioContent (f, node)
71         , SubtitleContent (f, node, version)
72 {
73         list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
74         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
75                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
76                 if ((*i)->optional_number_child<int> ("Selected")) {
77                         _subtitle_stream = _subtitle_streams.back ();
78                 }
79         }
80
81         c = node->node_children ("AudioStream");
82         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
83                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
84                 if ((*i)->optional_number_child<int> ("Selected")) {
85                         _audio_stream = _audio_streams.back ();
86                 }
87         }
88
89         c = node->node_children ("Filter");
90         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
91                 Filter const * f = Filter::from_id ((*i)->content ());
92                 if (f) {
93                         _filters.push_back (f);
94                 } else {
95                         notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
96                 }
97         }
98
99         _first_video = node->optional_number_child<double> ("FirstVideo");
100 }
101
102 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
103         : Content (f, c)
104         , VideoContent (f, c)
105         , AudioContent (f, c)
106         , SubtitleContent (f, c)
107 {
108         shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
109         assert (ref);
110
111         for (size_t i = 0; i < c.size(); ++i) {
112                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
113                 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
114                         throw JoinError (_("Content to be joined must use the same subtitle stream."));
115                 }
116
117                 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
118                         throw JoinError (_("Content to be joined must use the same audio stream."));
119                 }
120         }
121
122         _subtitle_streams = ref->subtitle_streams ();
123         _subtitle_stream = ref->subtitle_stream ();
124         _audio_streams = ref->audio_streams ();
125         _audio_stream = ref->audio_stream ();
126         _first_video = ref->_first_video;
127 }
128
129 void
130 FFmpegContent::as_xml (xmlpp::Node* node) const
131 {
132         node->add_child("Type")->add_child_text ("FFmpeg");
133         Content::as_xml (node);
134         VideoContent::as_xml (node);
135         AudioContent::as_xml (node);
136         SubtitleContent::as_xml (node);
137
138         boost::mutex::scoped_lock lm (_mutex);
139
140         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
141                 xmlpp::Node* t = node->add_child("SubtitleStream");
142                 if (_subtitle_stream && *i == _subtitle_stream) {
143                         t->add_child("Selected")->add_child_text("1");
144                 }
145                 (*i)->as_xml (t);
146         }
147
148         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
149                 xmlpp::Node* t = node->add_child("AudioStream");
150                 if (_audio_stream && *i == _audio_stream) {
151                         t->add_child("Selected")->add_child_text("1");
152                 }
153                 (*i)->as_xml (t);
154         }
155
156         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
157                 node->add_child("Filter")->add_child_text ((*i)->id ());
158         }
159
160         if (_first_video) {
161                 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
162         }
163 }
164
165 void
166 FFmpegContent::examine (shared_ptr<Job> job)
167 {
168         job->set_progress_unknown ();
169
170         Content::examine (job);
171
172         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
173         take_from_video_examiner (examiner);
174
175         ContentTime video_length = examiner->video_length ();
176
177         shared_ptr<const Film> film = _film.lock ();
178         assert (film);
179         LOG_GENERAL ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ()));
180
181         {
182                 boost::mutex::scoped_lock lm (_mutex);
183
184                 _video_length = video_length;
185
186                 _subtitle_streams = examiner->subtitle_streams ();
187                 if (!_subtitle_streams.empty ()) {
188                         _subtitle_stream = _subtitle_streams.front ();
189                 }
190                 
191                 _audio_streams = examiner->audio_streams ();
192                 if (!_audio_streams.empty ()) {
193                         _audio_stream = _audio_streams.front ();
194                 }
195
196                 _first_video = examiner->first_video ();
197         }
198
199         signal_changed (ContentProperty::LENGTH);
200         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
201         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
202         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
203         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
204         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
205 }
206
207 string
208 FFmpegContent::summary () const
209 {
210         /* Get the string() here so that the name does not have quotes around it */
211         return String::compose (_("%1 [movie]"), path_summary ());
212 }
213
214 string
215 FFmpegContent::technical_summary () const
216 {
217         string as = "none";
218         if (_audio_stream) {
219                 as = _audio_stream->technical_summary ();
220         }
221
222         string ss = "none";
223         if (_subtitle_stream) {
224                 ss = _subtitle_stream->technical_summary ();
225         }
226
227         string filt = Filter::ffmpeg_string (_filters);
228         
229         return Content::technical_summary() + " - "
230                 + VideoContent::technical_summary() + " - "
231                 + AudioContent::technical_summary() + " - "
232                 + String::compose (
233                         "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
234                         );
235 }
236
237 string
238 FFmpegContent::information () const
239 {
240         if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
241                 return "";
242         }
243         
244         stringstream s;
245         
246         s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine().frames (video_frame_rate()), video_frame_rate()) << "\n";
247         s << VideoContent::information ();
248
249         return s.str ();
250 }
251
252 void
253 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
254 {
255         {
256                 boost::mutex::scoped_lock lm (_mutex);
257                 _subtitle_stream = s;
258         }
259
260         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
261 }
262
263 void
264 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
265 {
266         {
267                 boost::mutex::scoped_lock lm (_mutex);
268                 _audio_stream = s;
269         }
270
271         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
272 }
273
274 ContentTime
275 FFmpegContent::audio_length () const
276 {
277         if (!audio_stream ()) {
278                 return ContentTime ();
279         }
280
281         return video_length ();
282 }
283
284 int
285 FFmpegContent::audio_channels () const
286 {
287         boost::mutex::scoped_lock lm (_mutex);
288         
289         if (!_audio_stream) {
290                 return 0;
291         }
292
293         return _audio_stream->channels;
294 }
295
296 int
297 FFmpegContent::audio_frame_rate () const
298 {
299         boost::mutex::scoped_lock lm (_mutex);
300
301         if (!_audio_stream) {
302                 return 0;
303         }
304
305         return _audio_stream->frame_rate;
306 }
307
308 bool
309 operator== (FFmpegStream const & a, FFmpegStream const & b)
310 {
311         return a._id == b._id;
312 }
313
314 bool
315 operator!= (FFmpegStream const & a, FFmpegStream const & b)
316 {
317         return a._id != b._id;
318 }
319
320 DCPTime
321 FFmpegContent::full_length () const
322 {
323         shared_ptr<const Film> film = _film.lock ();
324         assert (film);
325         return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
326 }
327
328 AudioMapping
329 FFmpegContent::audio_mapping () const
330 {
331         boost::mutex::scoped_lock lm (_mutex);
332
333         if (!_audio_stream) {
334                 return AudioMapping ();
335         }
336
337         return _audio_stream->mapping;
338 }
339
340 void
341 FFmpegContent::set_filters (vector<Filter const *> const & filters)
342 {
343         {
344                 boost::mutex::scoped_lock lm (_mutex);
345                 _filters = filters;
346         }
347
348         signal_changed (FFmpegContentProperty::FILTERS);
349 }
350
351 void
352 FFmpegContent::set_audio_mapping (AudioMapping m)
353 {
354         audio_stream()->mapping = m;
355         AudioContent::set_audio_mapping (m);
356 }
357
358 string
359 FFmpegContent::identifier () const
360 {
361         stringstream s;
362
363         s << VideoContent::identifier();
364
365         boost::mutex::scoped_lock lm (_mutex);
366
367         if (_subtitle_stream) {
368                 s << "_" << _subtitle_stream->identifier ();
369         }
370
371         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
372                 s << "_" << (*i)->id ();
373         }
374
375         return s.str ();
376 }
377
378 boost::filesystem::path
379 FFmpegContent::audio_analysis_path () const
380 {
381         shared_ptr<const Film> film = _film.lock ();
382         if (!film) {
383                 return boost::filesystem::path ();
384         }
385
386         /* We need to include the stream ID in this path so that we get different
387            analyses for each stream.
388         */
389
390         boost::filesystem::path p = film->audio_analysis_dir ();
391         string name = digest ();
392         if (audio_stream ()) {
393                 name += "_" + audio_stream()->identifier ();
394         }
395         p /= name;
396         return p;
397 }
398
399 bool
400 FFmpegContent::has_subtitle_during (ContentTimePeriod period) const
401 {
402         shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
403         if (!stream) {
404                 return false;
405         }
406
407         /* XXX: inefficient */
408         for (vector<ContentTimePeriod>::const_iterator i = stream->periods.begin(); i != stream->periods.end(); ++i) {
409                 if (i->from <= period.to && i->to >= period.from) {
410                         return true;
411                 }
412         }
413
414         return false;
415 }