50a69ae7b29c51e24561938b526fc3b528ddb3d2
[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 void
72 FFmpegContent::as_xml (xmlpp::Node* node) const
73 {
74         node->add_child("Type")->add_child_text ("FFmpeg");
75         Content::as_xml (node);
76         VideoContent::as_xml (node);
77
78         boost::mutex::scoped_lock lm (_mutex);
79
80         for (vector<FFmpegSubtitleStream>::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
81                 xmlpp::Node* t = node->add_child("SubtitleStream");
82                 if (_subtitle_stream && *i == _subtitle_stream.get()) {
83                         t->add_child("Selected")->add_child_text("1");
84                 }
85                 i->as_xml (t);
86         }
87
88         for (vector<FFmpegAudioStream>::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
89                 xmlpp::Node* t = node->add_child("AudioStream");
90                 if (_audio_stream && *i == _audio_stream.get()) {
91                         t->add_child("Selected")->add_child_text("1");
92                 }
93                 i->as_xml (t);
94         }
95 }
96
97 void
98 FFmpegContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
99 {
100         job->descend (0.5);
101         Content::examine (film, job, quick);
102         job->ascend ();
103
104         job->set_progress_unknown ();
105
106         shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false, true));
107
108         ContentVideoFrame video_length = 0;
109         if (quick) {
110                 video_length = decoder->video_length ();
111                 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
112         } else {
113                 while (!decoder->pass ()) {
114                         /* keep going */
115                 }
116
117                 video_length = decoder->video_frame ();
118                 film->log()->log (String::compose ("Video length examined as %1 frames", decoder->video_frame ()));
119         }
120
121         {
122                 boost::mutex::scoped_lock lm (_mutex);
123
124                 _video_length = video_length;
125
126                 _subtitle_streams = decoder->subtitle_streams ();
127                 if (!_subtitle_streams.empty ()) {
128                         _subtitle_stream = _subtitle_streams.front ();
129                 }
130                 
131                 _audio_streams = decoder->audio_streams ();
132                 if (!_audio_streams.empty ()) {
133                         _audio_stream = _audio_streams.front ();
134                 }
135         }
136
137         take_from_video_decoder (decoder);
138
139         Changed (VideoContentProperty::VIDEO_LENGTH);
140         Changed (FFmpegContentProperty::SUBTITLE_STREAMS);
141         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
142         Changed (FFmpegContentProperty::AUDIO_STREAMS);
143         Changed (FFmpegContentProperty::AUDIO_STREAM);
144 }
145
146 string
147 FFmpegContent::summary () const
148 {
149         return String::compose (_("Movie: %1"), file().filename ());
150 }
151
152 void
153 FFmpegContent::set_subtitle_stream (FFmpegSubtitleStream s)
154 {
155         {
156                 boost::mutex::scoped_lock lm (_mutex);
157                 _subtitle_stream = s;
158         }
159
160         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
161 }
162
163 void
164 FFmpegContent::set_audio_stream (FFmpegAudioStream s)
165 {
166         {
167                 boost::mutex::scoped_lock lm (_mutex);
168                 _audio_stream = s;
169         }
170
171         Changed (FFmpegContentProperty::AUDIO_STREAM);
172 }
173
174 ContentAudioFrame
175 FFmpegContent::audio_length () const
176 {
177         if (!_audio_stream) {
178                 return 0;
179         }
180         
181         return video_frames_to_audio_frames (_video_length, audio_frame_rate(), video_frame_rate());
182 }
183
184 int
185 FFmpegContent::audio_channels () const
186 {
187         if (!_audio_stream) {
188                 return 0;
189         }
190
191         return _audio_stream->channels ();
192 }
193
194 int
195 FFmpegContent::audio_frame_rate () const
196 {
197         if (!_audio_stream) {
198                 return 0;
199         }
200
201         return _audio_stream->frame_rate;
202 }
203
204 int64_t
205 FFmpegContent::audio_channel_layout () const
206 {
207         if (!_audio_stream) {
208                 return 0;
209         }
210
211         return _audio_stream->channel_layout;
212 }
213         
214 bool
215 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
216 {
217         return a.id == b.id;
218 }
219
220 bool
221 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
222 {
223         return a.id == b.id;
224 }
225
226 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
227 {
228         name = node->string_child ("Name");
229         id = node->number_child<int> ("Id");
230         frame_rate = node->number_child<int> ("FrameRate");
231         channel_layout = node->number_child<int64_t> ("ChannelLayout");
232 }
233
234 void
235 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
236 {
237         root->add_child("Name")->add_child_text (name);
238         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
239         root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
240         root->add_child("ChannelLayout")->add_child_text (lexical_cast<string> (channel_layout));
241 }
242
243 /** Construct a SubtitleStream from a value returned from to_string().
244  *  @param t String returned from to_string().
245  *  @param v State file version.
246  */
247 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
248 {
249         name = node->string_child ("Name");
250         id = node->number_child<int> ("Id");
251 }
252
253 void
254 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
255 {
256         root->add_child("Name")->add_child_text (name);
257         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
258 }