5bff1cecc0768eecdb0f44d5c9c022b0828b16fd
[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_decoder.h"
23 #include "compose.hpp"
24 #include "job.h"
25 #include "util.h"
26 #include "log.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::vector;
32 using std::list;
33 using boost::shared_ptr;
34 using boost::lexical_cast;
35
36 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
37 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
38 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
39 int const FFmpegContentProperty::AUDIO_STREAM = 103;
40
41 FFmpegContent::FFmpegContent (boost::filesystem::path f)
42         : Content (f)
43         , VideoContent (f)
44         , AudioContent (f)
45 {
46
47 }
48
49 FFmpegContent::FFmpegContent (shared_ptr<const cxml::Node> node)
50         : Content (node)
51         , VideoContent (node)
52         , AudioContent (node)
53 {
54         list<shared_ptr<cxml::Node> > c = node->node_children ("SubtitleStream");
55         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
56                 _subtitle_streams.push_back (FFmpegSubtitleStream (*i));
57                 if ((*i)->optional_number_child<int> ("Selected")) {
58                         _subtitle_stream = _subtitle_streams.back ();
59                 }
60         }
61
62         c = node->node_children ("AudioStream");
63         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
64                 _audio_streams.push_back (FFmpegAudioStream (*i));
65                 if ((*i)->optional_number_child<int> ("Selected")) {
66                         _audio_stream = _audio_streams.back ();
67                 }
68         }
69 }
70
71 FFmpegContent::FFmpegContent (FFmpegContent const & o)
72         : Content (o)
73         , VideoContent (o)
74         , AudioContent (o)
75         , boost::enable_shared_from_this<FFmpegContent> (o)
76         , _subtitle_streams (o._subtitle_streams)
77         , _subtitle_stream (o._subtitle_stream)
78         , _audio_streams (o._audio_streams)
79         , _audio_stream (o._audio_stream)
80 {
81
82 }
83
84 void
85 FFmpegContent::as_xml (xmlpp::Node* node) const
86 {
87         node->add_child("Type")->add_child_text ("FFmpeg");
88         Content::as_xml (node);
89         VideoContent::as_xml (node);
90
91         boost::mutex::scoped_lock lm (_mutex);
92
93         for (vector<FFmpegSubtitleStream>::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
94                 xmlpp::Node* t = node->add_child("SubtitleStream");
95                 if (_subtitle_stream && *i == _subtitle_stream.get()) {
96                         t->add_child("Selected")->add_child_text("1");
97                 }
98                 i->as_xml (t);
99         }
100
101         for (vector<FFmpegAudioStream>::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
102                 xmlpp::Node* t = node->add_child("AudioStream");
103                 if (_audio_stream && *i == _audio_stream.get()) {
104                         t->add_child("Selected")->add_child_text("1");
105                 }
106                 i->as_xml (t);
107         }
108 }
109
110 void
111 FFmpegContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
112 {
113         job->descend (0.5);
114         Content::examine (film, job, quick);
115         job->ascend ();
116
117         job->set_progress_unknown ();
118
119         shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false, true));
120
121         ContentVideoFrame video_length = 0;
122         if (quick) {
123                 video_length = decoder->video_length ();
124                 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
125         } else {
126                 while (!decoder->pass ()) {
127                         /* keep going */
128                 }
129
130                 video_length = decoder->video_frame ();
131                 film->log()->log (String::compose ("Video length examined as %1 frames", decoder->video_frame ()));
132         }
133
134         {
135                 boost::mutex::scoped_lock lm (_mutex);
136
137                 _video_length = video_length;
138
139                 _subtitle_streams = decoder->subtitle_streams ();
140                 if (!_subtitle_streams.empty ()) {
141                         _subtitle_stream = _subtitle_streams.front ();
142                 }
143                 
144                 _audio_streams = decoder->audio_streams ();
145                 if (!_audio_streams.empty ()) {
146                         _audio_stream = _audio_streams.front ();
147                 }
148         }
149
150         take_from_video_decoder (decoder);
151
152         Changed (VideoContentProperty::VIDEO_LENGTH);
153         Changed (FFmpegContentProperty::SUBTITLE_STREAMS);
154         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
155         Changed (FFmpegContentProperty::AUDIO_STREAMS);
156         Changed (FFmpegContentProperty::AUDIO_STREAM);
157 }
158
159 string
160 FFmpegContent::summary () const
161 {
162         return String::compose (_("Movie: %1"), file().filename ());
163 }
164
165 void
166 FFmpegContent::set_subtitle_stream (FFmpegSubtitleStream s)
167 {
168         {
169                 boost::mutex::scoped_lock lm (_mutex);
170                 _subtitle_stream = s;
171         }
172
173         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
174 }
175
176 void
177 FFmpegContent::set_audio_stream (FFmpegAudioStream s)
178 {
179         {
180                 boost::mutex::scoped_lock lm (_mutex);
181                 _audio_stream = s;
182         }
183
184         Changed (FFmpegContentProperty::AUDIO_STREAM);
185 }
186
187 ContentAudioFrame
188 FFmpegContent::audio_length () const
189 {
190         if (!_audio_stream) {
191                 return 0;
192         }
193         
194         return video_frames_to_audio_frames (_video_length, audio_frame_rate(), video_frame_rate());
195 }
196
197 int
198 FFmpegContent::audio_channels () const
199 {
200         if (!_audio_stream) {
201                 return 0;
202         }
203
204         return _audio_stream->channels ();
205 }
206
207 int
208 FFmpegContent::audio_frame_rate () const
209 {
210         if (!_audio_stream) {
211                 return 0;
212         }
213
214         return _audio_stream->frame_rate;
215 }
216
217 int64_t
218 FFmpegContent::audio_channel_layout () const
219 {
220         if (!_audio_stream) {
221                 return 0;
222         }
223
224         return _audio_stream->channel_layout;
225 }
226         
227 bool
228 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
229 {
230         return a.id == b.id;
231 }
232
233 bool
234 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
235 {
236         return a.id == b.id;
237 }
238
239 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
240 {
241         name = node->string_child ("Name");
242         id = node->number_child<int> ("Id");
243         frame_rate = node->number_child<int> ("FrameRate");
244         channel_layout = node->number_child<int64_t> ("ChannelLayout");
245 }
246
247 void
248 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
249 {
250         root->add_child("Name")->add_child_text (name);
251         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
252         root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
253         root->add_child("ChannelLayout")->add_child_text (lexical_cast<string> (channel_layout));
254 }
255
256 /** Construct a SubtitleStream from a value returned from to_string().
257  *  @param t String returned from to_string().
258  *  @param v State file version.
259  */
260 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
261 {
262         name = node->string_child ("Name");
263         id = node->number_child<int> ("Id");
264 }
265
266 void
267 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
268 {
269         root->add_child("Name")->add_child_text (name);
270         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
271 }
272
273 shared_ptr<Content>
274 FFmpegContent::clone () const
275 {
276         return shared_ptr<Content> (new FFmpegContent (*this));
277 }