2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include <libavcodec/avcodec.h>
23 #include <libavformat/avformat.h>
24 #include <libavutil/pixfmt.h>
25 #include <libavutil/pixdesc.h>
27 #include "ffmpeg_examiner.h"
28 #include "ffmpeg_content.h"
30 #include "ffmpeg_audio_stream.h"
31 #include "ffmpeg_subtitle_stream.h"
33 #include <boost/foreach.hpp>
41 using boost::shared_ptr;
42 using boost::optional;
44 /** @param job job that the examiner is operating in, or 0 */
45 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Job> job)
48 , _need_video_length (false)
50 /* Find audio and subtitle streams */
52 for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
53 AVStream* s = _format_context->streams[i];
54 if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
56 /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
57 so bodge it here. No idea why we should have to do this.
60 if (s->codec->channel_layout == 0) {
61 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
64 DCPOMATIC_ASSERT (_format_context->duration != AV_NOPTS_VALUE);
65 DCPOMATIC_ASSERT (s->codec->codec);
66 DCPOMATIC_ASSERT (s->codec->codec->name);
68 _audio_streams.push_back (
69 shared_ptr<FFmpegAudioStream> (
70 new FFmpegAudioStream (
72 s->codec->codec->name,
74 s->codec->sample_rate,
75 (double (_format_context->duration) / AV_TIME_BASE) * s->codec->sample_rate,
81 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
82 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (subtitle_stream_name (s), s->id)));
87 /* See if the header has duration information in it */
88 _need_video_length = _format_context->duration == AV_NOPTS_VALUE;
89 if (!_need_video_length) {
90 _video_length = llrint ((double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate().get());
94 if (job && _need_video_length) {
95 job->sub (_("Finding length"));
98 /* Run through until we find:
100 * - the first audio for each stream.
103 int64_t const len = _file_group.length ();
105 int r = av_read_frame (_format_context, &_packet);
112 job->set_progress (float (_format_context->pb->pos) / len);
114 job->set_progress_unknown ();
118 AVCodecContext* context = _format_context->streams[_packet.stream_index]->codec;
120 if (_video_stream && _packet.stream_index == _video_stream.get()) {
121 video_packet (context);
124 bool got_all_audio = true;
126 for (size_t i = 0; i < _audio_streams.size(); ++i) {
127 if (_audio_streams[i]->uses_index (_format_context, _packet.stream_index)) {
128 audio_packet (context, _audio_streams[i]);
130 if (!_audio_streams[i]->first_audio) {
131 got_all_audio = false;
135 av_packet_unref (&_packet);
137 if (_first_video && got_all_audio) {
145 FFmpegExaminer::video_packet (AVCodecContext* context)
147 DCPOMATIC_ASSERT (_video_stream);
149 if (_first_video && !_need_video_length) {
154 if (avcodec_decode_video2 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
156 _first_video = frame_time (_format_context->streams[_video_stream.get()]);
158 if (_need_video_length) {
159 _video_length = frame_time (
160 _format_context->streams[_video_stream.get()]
161 ).get_value_or (ContentTime ()).frames_round (video_frame_rate().get ());
167 FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStream> stream)
169 if (stream->first_audio) {
174 if (avcodec_decode_audio4 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
175 stream->first_audio = frame_time (stream->stream (_format_context));
179 optional<ContentTime>
180 FFmpegExaminer::frame_time (AVStream* s) const
182 optional<ContentTime> t;
184 int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
185 if (bet != AV_NOPTS_VALUE) {
186 t = ContentTime::from_seconds (bet * av_q2d (s->time_base));
193 FFmpegExaminer::video_frame_rate () const
195 DCPOMATIC_ASSERT (_video_stream);
196 /* This use of r_frame_rate is debateable; there's a few different
197 * frame rates in the format context, but this one seems to be the most
200 return av_q2d (av_stream_get_r_frame_rate (_format_context->streams[_video_stream.get()]));
204 FFmpegExaminer::video_size () const
206 return dcp::Size (video_codec_context()->width, video_codec_context()->height);
209 /** @return Length according to our content's header */
211 FFmpegExaminer::video_length () const
213 return max (Frame (1), _video_length);
217 FFmpegExaminer::sample_aspect_ratio () const
219 DCPOMATIC_ASSERT (_video_stream);
220 AVRational sar = av_guess_sample_aspect_ratio (_format_context, _format_context->streams[_video_stream.get()], 0);
222 /* I assume this means that we don't know */
223 return optional<double> ();
225 return double (sar.num) / sar.den;
229 FFmpegExaminer::subtitle_stream_name (AVStream* s) const
231 string n = stream_name (s);
241 FFmpegExaminer::stream_name (AVStream* s) const
246 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
251 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
264 FFmpegExaminer::bits_per_pixel () const
266 if (video_codec_context()->pix_fmt == -1) {
267 throw DecodeError (_("Could not find pixel format for video."));
270 AVPixFmtDescriptor const * d = av_pix_fmt_desc_get (video_codec_context()->pix_fmt);
271 DCPOMATIC_ASSERT (d);
272 return av_get_bits_per_pixel (d);
276 FFmpegExaminer::yuv () const
278 switch (video_codec_context()->pix_fmt) {
279 case AV_PIX_FMT_YUV420P:
280 case AV_PIX_FMT_YUYV422:
281 case AV_PIX_FMT_YUV422P:
282 case AV_PIX_FMT_YUV444P:
283 case AV_PIX_FMT_YUV410P:
284 case AV_PIX_FMT_YUV411P:
285 case AV_PIX_FMT_YUVJ420P:
286 case AV_PIX_FMT_YUVJ422P:
287 case AV_PIX_FMT_YUVJ444P:
288 case AV_PIX_FMT_UYVY422:
289 case AV_PIX_FMT_UYYVYY411:
290 case AV_PIX_FMT_NV12:
291 case AV_PIX_FMT_NV21:
292 case AV_PIX_FMT_YUV440P:
293 case AV_PIX_FMT_YUVJ440P:
294 case AV_PIX_FMT_YUVA420P:
295 case AV_PIX_FMT_YUV420P16LE:
296 case AV_PIX_FMT_YUV420P16BE:
297 case AV_PIX_FMT_YUV422P16LE:
298 case AV_PIX_FMT_YUV422P16BE:
299 case AV_PIX_FMT_YUV444P16LE:
300 case AV_PIX_FMT_YUV444P16BE:
301 case AV_PIX_FMT_YUV420P9BE:
302 case AV_PIX_FMT_YUV420P9LE:
303 case AV_PIX_FMT_YUV420P10BE:
304 case AV_PIX_FMT_YUV420P10LE:
305 case AV_PIX_FMT_YUV422P10BE:
306 case AV_PIX_FMT_YUV422P10LE:
307 case AV_PIX_FMT_YUV444P9BE:
308 case AV_PIX_FMT_YUV444P9LE:
309 case AV_PIX_FMT_YUV444P10BE:
310 case AV_PIX_FMT_YUV444P10LE:
311 case AV_PIX_FMT_YUV422P9BE:
312 case AV_PIX_FMT_YUV422P9LE:
313 case AV_PIX_FMT_YUVA420P9BE:
314 case AV_PIX_FMT_YUVA420P9LE:
315 case AV_PIX_FMT_YUVA422P9BE:
316 case AV_PIX_FMT_YUVA422P9LE:
317 case AV_PIX_FMT_YUVA444P9BE:
318 case AV_PIX_FMT_YUVA444P9LE:
319 case AV_PIX_FMT_YUVA420P10BE:
320 case AV_PIX_FMT_YUVA420P10LE:
321 case AV_PIX_FMT_YUVA422P10BE:
322 case AV_PIX_FMT_YUVA422P10LE:
323 case AV_PIX_FMT_YUVA444P10BE:
324 case AV_PIX_FMT_YUVA444P10LE:
325 case AV_PIX_FMT_YUVA420P16BE:
326 case AV_PIX_FMT_YUVA420P16LE:
327 case AV_PIX_FMT_YUVA422P16BE:
328 case AV_PIX_FMT_YUVA422P16LE:
329 case AV_PIX_FMT_YUVA444P16BE:
330 case AV_PIX_FMT_YUVA444P16LE:
331 case AV_PIX_FMT_NV16:
332 case AV_PIX_FMT_NV20LE:
333 case AV_PIX_FMT_NV20BE:
334 case AV_PIX_FMT_YVYU422:
335 case AV_PIX_FMT_YUVA444P:
336 case AV_PIX_FMT_YUVA422P:
337 case AV_PIX_FMT_YUV420P12BE:
338 case AV_PIX_FMT_YUV420P12LE:
339 case AV_PIX_FMT_YUV420P14BE:
340 case AV_PIX_FMT_YUV420P14LE:
341 case AV_PIX_FMT_YUV422P12BE:
342 case AV_PIX_FMT_YUV422P12LE:
343 case AV_PIX_FMT_YUV422P14BE:
344 case AV_PIX_FMT_YUV422P14LE:
345 case AV_PIX_FMT_YUV444P12BE:
346 case AV_PIX_FMT_YUV444P12LE:
347 case AV_PIX_FMT_YUV444P14BE:
348 case AV_PIX_FMT_YUV444P14LE:
349 case AV_PIX_FMT_YUVJ411P:
357 FFmpegExaminer::has_video () const
359 return static_cast<bool> (_video_stream);