Accessor for ClosedCaptionsDialog.
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 extern "C" {
22 #include <libavcodec/avcodec.h>
23 #include <libavformat/avformat.h>
24 #include <libavutil/pixfmt.h>
25 #include <libavutil/pixdesc.h>
26 #include <libavutil/eval.h>
27 #include <libavutil/display.h>
28 }
29 #include "ffmpeg_examiner.h"
30 #include "ffmpeg_content.h"
31 #include "job.h"
32 #include "ffmpeg_audio_stream.h"
33 #include "ffmpeg_subtitle_stream.h"
34 #include "util.h"
35 #include <boost/foreach.hpp>
36 #include <iostream>
37
38 #include "i18n.h"
39
40 using std::string;
41 using std::cout;
42 using std::max;
43 using boost::shared_ptr;
44 using boost::optional;
45 using namespace dcpomatic;
46
47 /** @param job job that the examiner is operating in, or 0 */
48 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Job> job)
49         : FFmpeg (c)
50         , _video_length (0)
51         , _need_video_length (false)
52 {
53         /* Find audio and subtitle streams */
54
55         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
56                 AVStream* s = _format_context->streams[i];
57                 if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
58
59                         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
60                            so bodge it here.  No idea why we should have to do this.
61                         */
62
63                         if (s->codec->channel_layout == 0) {
64                                 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
65                         }
66
67                         DCPOMATIC_ASSERT (_format_context->duration != AV_NOPTS_VALUE);
68                         DCPOMATIC_ASSERT (s->codec->codec);
69                         DCPOMATIC_ASSERT (s->codec->codec->name);
70
71                         _audio_streams.push_back (
72                                 shared_ptr<FFmpegAudioStream> (
73                                         new FFmpegAudioStream (
74                                                 stream_name (s),
75                                                 s->codec->codec->name,
76                                                 s->id,
77                                                 s->codec->sample_rate,
78                                                 llrint ((double (_format_context->duration) / AV_TIME_BASE) * s->codec->sample_rate),
79                                                 s->codec->channels
80                                                 )
81                                         )
82                                 );
83
84                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
85                         _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (subtitle_stream_name (s), s->id)));
86                 }
87         }
88
89         if (has_video ()) {
90                 /* See if the header has duration information in it */
91                 _need_video_length = _format_context->duration == AV_NOPTS_VALUE;
92                 if (!_need_video_length) {
93                         _video_length = llrint ((double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate().get());
94                 }
95         }
96
97         if (job && _need_video_length) {
98                 job->sub (_("Finding length"));
99         }
100
101         /* Run through until we find:
102          *   - the first video.
103          *   - the first audio for each stream.
104          */
105
106         int64_t const len = _file_group.length ();
107         while (true) {
108                 int r = av_read_frame (_format_context, &_packet);
109                 if (r < 0) {
110                         break;
111                 }
112
113                 if (job) {
114                         if (len > 0) {
115                                 job->set_progress (float (_format_context->pb->pos) / len);
116                         } else {
117                                 job->set_progress_unknown ();
118                         }
119                 }
120
121                 AVCodecContext* context = _format_context->streams[_packet.stream_index]->codec;
122
123                 if (_video_stream && _packet.stream_index == _video_stream.get()) {
124                         video_packet (context);
125                 }
126
127                 bool got_all_audio = true;
128
129                 for (size_t i = 0; i < _audio_streams.size(); ++i) {
130                         if (_audio_streams[i]->uses_index (_format_context, _packet.stream_index)) {
131                                 audio_packet (context, _audio_streams[i]);
132                         }
133                         if (!_audio_streams[i]->first_audio) {
134                                 got_all_audio = false;
135                         }
136                 }
137
138                 av_packet_unref (&_packet);
139
140                 if (_first_video && got_all_audio) {
141                         /* All done */
142                         break;
143                 }
144         }
145
146         if (_video_stream) {
147                 /* This code taken from get_rotation() in ffmpeg:cmdutils.c */
148                 AVStream* stream = _format_context->streams[*_video_stream];
149                 AVDictionaryEntry* rotate_tag = av_dict_get (stream->metadata, "rotate", 0, 0);
150                 uint8_t* displaymatrix = av_stream_get_side_data (stream, AV_PKT_DATA_DISPLAYMATRIX, 0);
151                 _rotation = 0;
152
153                 if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) {
154                         char *tail;
155                         _rotation = av_strtod (rotate_tag->value, &tail);
156                         if (*tail) {
157                                 _rotation = 0;
158                         }
159                 }
160
161                 if (displaymatrix && !_rotation) {
162                         _rotation = - av_display_rotation_get ((int32_t*) displaymatrix);
163                 }
164
165                 _rotation = *_rotation - 360 * floor (*_rotation / 360 + 0.9 / 360);
166
167                 DCPOMATIC_ASSERT (fabs (*_rotation - 90 * round (*_rotation / 90)) < 2);
168         }
169
170 #ifdef DCPOMATIC_VARIANT_SWAROOP
171         AVDictionaryEntry* e = av_dict_get (_format_context->metadata, SWAROOP_ID_TAG, 0, 0);
172         if (e) {
173                 _id = e->value;
174         }
175 #endif
176 }
177
178 void
179 FFmpegExaminer::video_packet (AVCodecContext* context)
180 {
181         DCPOMATIC_ASSERT (_video_stream);
182
183         if (_first_video && !_need_video_length) {
184                 return;
185         }
186
187         int frame_finished;
188         if (avcodec_decode_video2 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
189                 if (!_first_video) {
190                         _first_video = frame_time (_format_context->streams[_video_stream.get()]);
191                 }
192                 if (_need_video_length) {
193                         _video_length = frame_time (
194                                 _format_context->streams[_video_stream.get()]
195                                 ).get_value_or (ContentTime ()).frames_round (video_frame_rate().get ());
196                 }
197         }
198 }
199
200 void
201 FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStream> stream)
202 {
203         if (stream->first_audio) {
204                 return;
205         }
206
207         int frame_finished;
208         if (avcodec_decode_audio4 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
209                 stream->first_audio = frame_time (stream->stream (_format_context));
210         }
211 }
212
213 optional<ContentTime>
214 FFmpegExaminer::frame_time (AVStream* s) const
215 {
216         optional<ContentTime> t;
217
218         int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
219         if (bet != AV_NOPTS_VALUE) {
220                 t = ContentTime::from_seconds (bet * av_q2d (s->time_base));
221         }
222
223         return t;
224 }
225
226 optional<double>
227 FFmpegExaminer::video_frame_rate () const
228 {
229         DCPOMATIC_ASSERT (_video_stream);
230         return av_q2d(av_guess_frame_rate(_format_context, _format_context->streams[_video_stream.get()], 0));
231 }
232
233 dcp::Size
234 FFmpegExaminer::video_size () const
235 {
236         return dcp::Size (video_codec_context()->width, video_codec_context()->height);
237 }
238
239 /** @return Length according to our content's header */
240 Frame
241 FFmpegExaminer::video_length () const
242 {
243         return max (Frame (1), _video_length);
244 }
245
246 optional<double>
247 FFmpegExaminer::sample_aspect_ratio () const
248 {
249         DCPOMATIC_ASSERT (_video_stream);
250         AVRational sar = av_guess_sample_aspect_ratio (_format_context, _format_context->streams[_video_stream.get()], 0);
251         if (sar.num == 0) {
252                 /* I assume this means that we don't know */
253                 return optional<double> ();
254         }
255         return double (sar.num) / sar.den;
256 }
257
258 string
259 FFmpegExaminer::subtitle_stream_name (AVStream* s) const
260 {
261         string n = stream_name (s);
262
263         if (n.empty()) {
264                 n = _("unknown");
265         }
266
267         return n;
268 }
269
270 string
271 FFmpegExaminer::stream_name (AVStream* s) const
272 {
273         string n;
274
275         if (s->metadata) {
276                 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
277                 if (lang) {
278                         n = lang->value;
279                 }
280
281                 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
282                 if (title) {
283                         if (!n.empty()) {
284                                 n += " ";
285                         }
286                         n += title->value;
287                 }
288         }
289
290         return n;
291 }
292
293 optional<int>
294 FFmpegExaminer::bits_per_pixel () const
295 {
296         if (video_codec_context()->pix_fmt == -1) {
297                 return optional<int>();
298         }
299
300         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get (video_codec_context()->pix_fmt);
301         DCPOMATIC_ASSERT (d);
302         return av_get_bits_per_pixel (d);
303 }
304
305 bool
306 FFmpegExaminer::yuv () const
307 {
308         switch (video_codec_context()->pix_fmt) {
309         case AV_PIX_FMT_YUV420P:
310         case AV_PIX_FMT_YUYV422:
311         case AV_PIX_FMT_YUV422P:
312         case AV_PIX_FMT_YUV444P:
313         case AV_PIX_FMT_YUV410P:
314         case AV_PIX_FMT_YUV411P:
315         case AV_PIX_FMT_YUVJ420P:
316         case AV_PIX_FMT_YUVJ422P:
317         case AV_PIX_FMT_YUVJ444P:
318         case AV_PIX_FMT_UYVY422:
319         case AV_PIX_FMT_UYYVYY411:
320         case AV_PIX_FMT_NV12:
321         case AV_PIX_FMT_NV21:
322         case AV_PIX_FMT_YUV440P:
323         case AV_PIX_FMT_YUVJ440P:
324         case AV_PIX_FMT_YUVA420P:
325         case AV_PIX_FMT_YUV420P16LE:
326         case AV_PIX_FMT_YUV420P16BE:
327         case AV_PIX_FMT_YUV422P16LE:
328         case AV_PIX_FMT_YUV422P16BE:
329         case AV_PIX_FMT_YUV444P16LE:
330         case AV_PIX_FMT_YUV444P16BE:
331         case AV_PIX_FMT_YUV420P9BE:
332         case AV_PIX_FMT_YUV420P9LE:
333         case AV_PIX_FMT_YUV420P10BE:
334         case AV_PIX_FMT_YUV420P10LE:
335         case AV_PIX_FMT_YUV422P10BE:
336         case AV_PIX_FMT_YUV422P10LE:
337         case AV_PIX_FMT_YUV444P9BE:
338         case AV_PIX_FMT_YUV444P9LE:
339         case AV_PIX_FMT_YUV444P10BE:
340         case AV_PIX_FMT_YUV444P10LE:
341         case AV_PIX_FMT_YUV422P9BE:
342         case AV_PIX_FMT_YUV422P9LE:
343         case AV_PIX_FMT_YUVA420P9BE:
344         case AV_PIX_FMT_YUVA420P9LE:
345         case AV_PIX_FMT_YUVA422P9BE:
346         case AV_PIX_FMT_YUVA422P9LE:
347         case AV_PIX_FMT_YUVA444P9BE:
348         case AV_PIX_FMT_YUVA444P9LE:
349         case AV_PIX_FMT_YUVA420P10BE:
350         case AV_PIX_FMT_YUVA420P10LE:
351         case AV_PIX_FMT_YUVA422P10BE:
352         case AV_PIX_FMT_YUVA422P10LE:
353         case AV_PIX_FMT_YUVA444P10BE:
354         case AV_PIX_FMT_YUVA444P10LE:
355         case AV_PIX_FMT_YUVA420P16BE:
356         case AV_PIX_FMT_YUVA420P16LE:
357         case AV_PIX_FMT_YUVA422P16BE:
358         case AV_PIX_FMT_YUVA422P16LE:
359         case AV_PIX_FMT_YUVA444P16BE:
360         case AV_PIX_FMT_YUVA444P16LE:
361         case AV_PIX_FMT_NV16:
362         case AV_PIX_FMT_NV20LE:
363         case AV_PIX_FMT_NV20BE:
364         case AV_PIX_FMT_YVYU422:
365         case AV_PIX_FMT_YUVA444P:
366         case AV_PIX_FMT_YUVA422P:
367         case AV_PIX_FMT_YUV420P12BE:
368         case AV_PIX_FMT_YUV420P12LE:
369         case AV_PIX_FMT_YUV420P14BE:
370         case AV_PIX_FMT_YUV420P14LE:
371         case AV_PIX_FMT_YUV422P12BE:
372         case AV_PIX_FMT_YUV422P12LE:
373         case AV_PIX_FMT_YUV422P14BE:
374         case AV_PIX_FMT_YUV422P14LE:
375         case AV_PIX_FMT_YUV444P12BE:
376         case AV_PIX_FMT_YUV444P12LE:
377         case AV_PIX_FMT_YUV444P14BE:
378         case AV_PIX_FMT_YUV444P14LE:
379         case AV_PIX_FMT_YUVJ411P:
380                 return true;
381         default:
382                 return false;
383         }
384 }
385
386 bool
387 FFmpegExaminer::has_video () const
388 {
389         return static_cast<bool> (_video_stream);
390 }
391
392 VideoRange
393 FFmpegExaminer::range () const
394 {
395         switch (color_range()) {
396         case AVCOL_RANGE_MPEG:
397         case AVCOL_RANGE_UNSPECIFIED:
398                 return VIDEO_RANGE_VIDEO;
399         case AVCOL_RANGE_JPEG:
400         default:
401                 return VIDEO_RANGE_FULL;
402         }
403 }