Report errors from ChromiumEnv::GetChildren in Posix.
[chromium-blink-merge.git] / media / ffmpeg / ffmpeg_common.cc
blob72b31252f8508df48cc10c7d2d832264525233cd
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/ffmpeg/ffmpeg_common.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "media/base/decoder_buffer.h"
10 #include "media/base/video_frame.h"
11 #include "media/base/video_util.h"
13 namespace media {
15 // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are
16 // padded. Check here to ensure FFmpeg only receives data padded to its
17 // specifications.
18 COMPILE_ASSERT(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE,
19 decoder_buffer_padding_size_does_not_fit_ffmpeg_requirement);
21 // Alignment requirement by FFmpeg for input and output buffers. This need to
22 // be updated to match FFmpeg when it changes.
23 #if defined(ARCH_CPU_ARM_FAMILY)
24 static const int kFFmpegBufferAddressAlignment = 16;
25 #else
26 static const int kFFmpegBufferAddressAlignment = 32;
27 #endif
29 // Check here to ensure FFmpeg only receives data aligned to its specifications.
30 COMPILE_ASSERT(
31 DecoderBuffer::kAlignmentSize >= kFFmpegBufferAddressAlignment &&
32 DecoderBuffer::kAlignmentSize % kFFmpegBufferAddressAlignment == 0,
33 decoder_buffer_alignment_size_does_not_fit_ffmpeg_requirement);
35 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally.
36 // See video_get_buffer() in libavcodec/utils.c.
37 static const int kFFmpegOutputBufferPaddingSize = 16;
39 COMPILE_ASSERT(VideoFrame::kFrameSizePadding >= kFFmpegOutputBufferPaddingSize,
40 video_frame_padding_size_does_not_fit_ffmpeg_requirement);
42 COMPILE_ASSERT(
43 VideoFrame::kFrameAddressAlignment >= kFFmpegBufferAddressAlignment &&
44 VideoFrame::kFrameAddressAlignment % kFFmpegBufferAddressAlignment == 0,
45 video_frame_address_alignment_does_not_fit_ffmpeg_requirement);
47 static const AVRational kMicrosBase = { 1, base::Time::kMicrosecondsPerSecond };
49 base::TimeDelta ConvertFromTimeBase(const AVRational& time_base,
50 int64 timestamp) {
51 int64 microseconds = av_rescale_q(timestamp, time_base, kMicrosBase);
52 return base::TimeDelta::FromMicroseconds(microseconds);
55 int64 ConvertToTimeBase(const AVRational& time_base,
56 const base::TimeDelta& timestamp) {
57 return av_rescale_q(timestamp.InMicroseconds(), kMicrosBase, time_base);
60 // Converts an FFmpeg audio codec ID into its corresponding supported codec id.
61 static AudioCodec CodecIDToAudioCodec(AVCodecID codec_id) {
62 switch (codec_id) {
63 case AV_CODEC_ID_AAC:
64 return kCodecAAC;
65 case AV_CODEC_ID_MP3:
66 return kCodecMP3;
67 case AV_CODEC_ID_VORBIS:
68 return kCodecVorbis;
69 case AV_CODEC_ID_PCM_U8:
70 case AV_CODEC_ID_PCM_S16LE:
71 case AV_CODEC_ID_PCM_S24LE:
72 case AV_CODEC_ID_PCM_F32LE:
73 return kCodecPCM;
74 case AV_CODEC_ID_PCM_S16BE:
75 return kCodecPCM_S16BE;
76 case AV_CODEC_ID_PCM_S24BE:
77 return kCodecPCM_S24BE;
78 case AV_CODEC_ID_FLAC:
79 return kCodecFLAC;
80 case AV_CODEC_ID_AMR_NB:
81 return kCodecAMR_NB;
82 case AV_CODEC_ID_AMR_WB:
83 return kCodecAMR_WB;
84 case AV_CODEC_ID_GSM_MS:
85 return kCodecGSM_MS;
86 case AV_CODEC_ID_PCM_MULAW:
87 return kCodecPCM_MULAW;
88 case AV_CODEC_ID_OPUS:
89 return kCodecOpus;
90 default:
91 DVLOG(1) << "Unknown audio CodecID: " << codec_id;
93 return kUnknownAudioCodec;
96 static AVCodecID AudioCodecToCodecID(AudioCodec audio_codec,
97 SampleFormat sample_format) {
98 switch (audio_codec) {
99 case kCodecAAC:
100 return AV_CODEC_ID_AAC;
101 case kCodecMP3:
102 return AV_CODEC_ID_MP3;
103 case kCodecPCM:
104 switch (sample_format) {
105 case kSampleFormatU8:
106 return AV_CODEC_ID_PCM_U8;
107 case kSampleFormatS16:
108 return AV_CODEC_ID_PCM_S16LE;
109 case kSampleFormatS32:
110 return AV_CODEC_ID_PCM_S24LE;
111 case kSampleFormatF32:
112 return AV_CODEC_ID_PCM_F32LE;
113 default:
114 DVLOG(1) << "Unsupported sample format: " << sample_format;
116 break;
117 case kCodecPCM_S16BE:
118 return AV_CODEC_ID_PCM_S16BE;
119 case kCodecPCM_S24BE:
120 return AV_CODEC_ID_PCM_S24BE;
121 case kCodecVorbis:
122 return AV_CODEC_ID_VORBIS;
123 case kCodecFLAC:
124 return AV_CODEC_ID_FLAC;
125 case kCodecAMR_NB:
126 return AV_CODEC_ID_AMR_NB;
127 case kCodecAMR_WB:
128 return AV_CODEC_ID_AMR_WB;
129 case kCodecGSM_MS:
130 return AV_CODEC_ID_GSM_MS;
131 case kCodecPCM_MULAW:
132 return AV_CODEC_ID_PCM_MULAW;
133 case kCodecOpus:
134 return AV_CODEC_ID_OPUS;
135 default:
136 DVLOG(1) << "Unknown AudioCodec: " << audio_codec;
138 return AV_CODEC_ID_NONE;
141 // Converts an FFmpeg video codec ID into its corresponding supported codec id.
142 static VideoCodec CodecIDToVideoCodec(AVCodecID codec_id) {
143 switch (codec_id) {
144 case AV_CODEC_ID_H264:
145 return kCodecH264;
146 case AV_CODEC_ID_THEORA:
147 return kCodecTheora;
148 case AV_CODEC_ID_MPEG4:
149 return kCodecMPEG4;
150 case AV_CODEC_ID_VP8:
151 return kCodecVP8;
152 case AV_CODEC_ID_VP9:
153 return kCodecVP9;
154 default:
155 DVLOG(1) << "Unknown video CodecID: " << codec_id;
157 return kUnknownVideoCodec;
160 static AVCodecID VideoCodecToCodecID(VideoCodec video_codec) {
161 switch (video_codec) {
162 case kCodecH264:
163 return AV_CODEC_ID_H264;
164 case kCodecTheora:
165 return AV_CODEC_ID_THEORA;
166 case kCodecMPEG4:
167 return AV_CODEC_ID_MPEG4;
168 case kCodecVP8:
169 return AV_CODEC_ID_VP8;
170 case kCodecVP9:
171 return AV_CODEC_ID_VP9;
172 default:
173 DVLOG(1) << "Unknown VideoCodec: " << video_codec;
175 return AV_CODEC_ID_NONE;
178 static VideoCodecProfile ProfileIDToVideoCodecProfile(int profile) {
179 // Clear out the CONSTRAINED & INTRA flags which are strict subsets of the
180 // corresponding profiles with which they're used.
181 profile &= ~FF_PROFILE_H264_CONSTRAINED;
182 profile &= ~FF_PROFILE_H264_INTRA;
183 switch (profile) {
184 case FF_PROFILE_H264_BASELINE:
185 return H264PROFILE_BASELINE;
186 case FF_PROFILE_H264_MAIN:
187 return H264PROFILE_MAIN;
188 case FF_PROFILE_H264_EXTENDED:
189 return H264PROFILE_EXTENDED;
190 case FF_PROFILE_H264_HIGH:
191 return H264PROFILE_HIGH;
192 case FF_PROFILE_H264_HIGH_10:
193 return H264PROFILE_HIGH10PROFILE;
194 case FF_PROFILE_H264_HIGH_422:
195 return H264PROFILE_HIGH422PROFILE;
196 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
197 return H264PROFILE_HIGH444PREDICTIVEPROFILE;
198 default:
199 DVLOG(1) << "Unknown profile id: " << profile;
201 return VIDEO_CODEC_PROFILE_UNKNOWN;
204 static int VideoCodecProfileToProfileID(VideoCodecProfile profile) {
205 switch (profile) {
206 case H264PROFILE_BASELINE:
207 return FF_PROFILE_H264_BASELINE;
208 case H264PROFILE_MAIN:
209 return FF_PROFILE_H264_MAIN;
210 case H264PROFILE_EXTENDED:
211 return FF_PROFILE_H264_EXTENDED;
212 case H264PROFILE_HIGH:
213 return FF_PROFILE_H264_HIGH;
214 case H264PROFILE_HIGH10PROFILE:
215 return FF_PROFILE_H264_HIGH_10;
216 case H264PROFILE_HIGH422PROFILE:
217 return FF_PROFILE_H264_HIGH_422;
218 case H264PROFILE_HIGH444PREDICTIVEPROFILE:
219 return FF_PROFILE_H264_HIGH_444_PREDICTIVE;
220 default:
221 DVLOG(1) << "Unknown VideoCodecProfile: " << profile;
223 return FF_PROFILE_UNKNOWN;
226 SampleFormat AVSampleFormatToSampleFormat(AVSampleFormat sample_format) {
227 switch (sample_format) {
228 case AV_SAMPLE_FMT_U8:
229 return kSampleFormatU8;
230 case AV_SAMPLE_FMT_S16:
231 return kSampleFormatS16;
232 case AV_SAMPLE_FMT_S32:
233 return kSampleFormatS32;
234 case AV_SAMPLE_FMT_FLT:
235 return kSampleFormatF32;
236 case AV_SAMPLE_FMT_S16P:
237 return kSampleFormatPlanarS16;
238 case AV_SAMPLE_FMT_FLTP:
239 return kSampleFormatPlanarF32;
240 default:
241 DVLOG(1) << "Unknown AVSampleFormat: " << sample_format;
243 return kUnknownSampleFormat;
246 static AVSampleFormat SampleFormatToAVSampleFormat(SampleFormat sample_format) {
247 switch (sample_format) {
248 case kSampleFormatU8:
249 return AV_SAMPLE_FMT_U8;
250 case kSampleFormatS16:
251 return AV_SAMPLE_FMT_S16;
252 case kSampleFormatS32:
253 return AV_SAMPLE_FMT_S32;
254 case kSampleFormatF32:
255 return AV_SAMPLE_FMT_FLT;
256 case kSampleFormatPlanarS16:
257 return AV_SAMPLE_FMT_S16P;
258 case kSampleFormatPlanarF32:
259 return AV_SAMPLE_FMT_FLTP;
260 default:
261 DVLOG(1) << "Unknown SampleFormat: " << sample_format;
263 return AV_SAMPLE_FMT_NONE;
266 static void AVCodecContextToAudioDecoderConfig(
267 const AVCodecContext* codec_context,
268 bool is_encrypted,
269 AudioDecoderConfig* config,
270 bool record_stats) {
271 DCHECK_EQ(codec_context->codec_type, AVMEDIA_TYPE_AUDIO);
273 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id);
275 SampleFormat sample_format =
276 AVSampleFormatToSampleFormat(codec_context->sample_fmt);
278 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout(
279 codec_context->channel_layout, codec_context->channels);
281 if (codec == kCodecOpus) {
282 // |codec_context->sample_fmt| is not set by FFmpeg because Opus decoding is
283 // not enabled in FFmpeg, so we need to manually set the sample format.
284 sample_format = kSampleFormatS16;
287 config->Initialize(codec,
288 sample_format,
289 channel_layout,
290 codec_context->sample_rate,
291 codec_context->extradata,
292 codec_context->extradata_size,
293 is_encrypted,
294 record_stats,
295 base::TimeDelta(),
296 base::TimeDelta());
297 if (codec != kCodecOpus) {
298 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8,
299 config->bits_per_channel());
303 void AVStreamToAudioDecoderConfig(
304 const AVStream* stream,
305 AudioDecoderConfig* config,
306 bool record_stats) {
307 bool is_encrypted = false;
308 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL, 0);
309 if (key)
310 is_encrypted = true;
311 return AVCodecContextToAudioDecoderConfig(
312 stream->codec, is_encrypted, config, record_stats);
315 void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config,
316 AVCodecContext* codec_context) {
317 codec_context->codec_type = AVMEDIA_TYPE_AUDIO;
318 codec_context->codec_id = AudioCodecToCodecID(config.codec(),
319 config.sample_format());
320 codec_context->sample_fmt = SampleFormatToAVSampleFormat(
321 config.sample_format());
323 // TODO(scherkus): should we set |channel_layout|? I'm not sure if FFmpeg uses
324 // said information to decode.
325 codec_context->channels =
326 ChannelLayoutToChannelCount(config.channel_layout());
327 codec_context->sample_rate = config.samples_per_second();
329 if (config.extra_data()) {
330 codec_context->extradata_size = config.extra_data_size();
331 codec_context->extradata = reinterpret_cast<uint8_t*>(
332 av_malloc(config.extra_data_size() + FF_INPUT_BUFFER_PADDING_SIZE));
333 memcpy(codec_context->extradata, config.extra_data(),
334 config.extra_data_size());
335 memset(codec_context->extradata + config.extra_data_size(), '\0',
336 FF_INPUT_BUFFER_PADDING_SIZE);
337 } else {
338 codec_context->extradata = NULL;
339 codec_context->extradata_size = 0;
343 void AVStreamToVideoDecoderConfig(
344 const AVStream* stream,
345 VideoDecoderConfig* config,
346 bool record_stats) {
347 gfx::Size coded_size(stream->codec->coded_width, stream->codec->coded_height);
349 // TODO(vrk): This assumes decoded frame data starts at (0, 0), which is true
350 // for now, but may not always be true forever. Fix this in the future.
351 gfx::Rect visible_rect(stream->codec->width, stream->codec->height);
353 AVRational aspect_ratio = { 1, 1 };
354 if (stream->sample_aspect_ratio.num)
355 aspect_ratio = stream->sample_aspect_ratio;
356 else if (stream->codec->sample_aspect_ratio.num)
357 aspect_ratio = stream->codec->sample_aspect_ratio;
359 VideoCodec codec = CodecIDToVideoCodec(stream->codec->codec_id);
361 VideoCodecProfile profile = VIDEO_CODEC_PROFILE_UNKNOWN;
362 if (codec == kCodecVP8)
363 profile = VP8PROFILE_MAIN;
364 else if (codec == kCodecVP9)
365 profile = VP9PROFILE_MAIN;
366 else
367 profile = ProfileIDToVideoCodecProfile(stream->codec->profile);
369 gfx::Size natural_size = GetNaturalSize(
370 visible_rect.size(), aspect_ratio.num, aspect_ratio.den);
372 VideoFrame::Format format = PixelFormatToVideoFormat(stream->codec->pix_fmt);
373 if (codec == kCodecVP9) {
374 // TODO(tomfinegan): libavcodec doesn't know about VP9.
375 format = VideoFrame::YV12;
376 coded_size = natural_size;
379 bool is_encrypted = false;
380 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL, 0);
381 if (key)
382 is_encrypted = true;
384 AVDictionaryEntry* webm_alpha =
385 av_dict_get(stream->metadata, "alpha_mode", NULL, 0);
386 if (webm_alpha && !strcmp(webm_alpha->value, "1")) {
387 format = VideoFrame::YV12A;
390 config->Initialize(codec,
391 profile,
392 format,
393 coded_size, visible_rect, natural_size,
394 stream->codec->extradata, stream->codec->extradata_size,
395 is_encrypted,
396 record_stats);
399 void VideoDecoderConfigToAVCodecContext(
400 const VideoDecoderConfig& config,
401 AVCodecContext* codec_context) {
402 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
403 codec_context->codec_id = VideoCodecToCodecID(config.codec());
404 codec_context->profile = VideoCodecProfileToProfileID(config.profile());
405 codec_context->coded_width = config.coded_size().width();
406 codec_context->coded_height = config.coded_size().height();
407 codec_context->pix_fmt = VideoFormatToPixelFormat(config.format());
409 if (config.extra_data()) {
410 codec_context->extradata_size = config.extra_data_size();
411 codec_context->extradata = reinterpret_cast<uint8_t*>(
412 av_malloc(config.extra_data_size() + FF_INPUT_BUFFER_PADDING_SIZE));
413 memcpy(codec_context->extradata, config.extra_data(),
414 config.extra_data_size());
415 memset(codec_context->extradata + config.extra_data_size(), '\0',
416 FF_INPUT_BUFFER_PADDING_SIZE);
417 } else {
418 codec_context->extradata = NULL;
419 codec_context->extradata_size = 0;
423 ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout, int channels) {
424 switch (layout) {
425 case AV_CH_LAYOUT_MONO:
426 return CHANNEL_LAYOUT_MONO;
427 case AV_CH_LAYOUT_STEREO:
428 return CHANNEL_LAYOUT_STEREO;
429 case AV_CH_LAYOUT_2_1:
430 return CHANNEL_LAYOUT_2_1;
431 case AV_CH_LAYOUT_SURROUND:
432 return CHANNEL_LAYOUT_SURROUND;
433 case AV_CH_LAYOUT_4POINT0:
434 return CHANNEL_LAYOUT_4_0;
435 case AV_CH_LAYOUT_2_2:
436 return CHANNEL_LAYOUT_2_2;
437 case AV_CH_LAYOUT_QUAD:
438 return CHANNEL_LAYOUT_QUAD;
439 case AV_CH_LAYOUT_5POINT0:
440 return CHANNEL_LAYOUT_5_0;
441 case AV_CH_LAYOUT_5POINT1:
442 return CHANNEL_LAYOUT_5_1;
443 case AV_CH_LAYOUT_5POINT0_BACK:
444 return CHANNEL_LAYOUT_5_0_BACK;
445 case AV_CH_LAYOUT_5POINT1_BACK:
446 return CHANNEL_LAYOUT_5_1_BACK;
447 case AV_CH_LAYOUT_7POINT0:
448 return CHANNEL_LAYOUT_7_0;
449 case AV_CH_LAYOUT_7POINT1:
450 return CHANNEL_LAYOUT_7_1;
451 case AV_CH_LAYOUT_7POINT1_WIDE:
452 return CHANNEL_LAYOUT_7_1_WIDE;
453 case AV_CH_LAYOUT_STEREO_DOWNMIX:
454 return CHANNEL_LAYOUT_STEREO_DOWNMIX;
455 case AV_CH_LAYOUT_2POINT1:
456 return CHANNEL_LAYOUT_2POINT1;
457 case AV_CH_LAYOUT_3POINT1:
458 return CHANNEL_LAYOUT_3_1;
459 case AV_CH_LAYOUT_4POINT1:
460 return CHANNEL_LAYOUT_4_1;
461 case AV_CH_LAYOUT_6POINT0:
462 return CHANNEL_LAYOUT_6_0;
463 case AV_CH_LAYOUT_6POINT0_FRONT:
464 return CHANNEL_LAYOUT_6_0_FRONT;
465 case AV_CH_LAYOUT_HEXAGONAL:
466 return CHANNEL_LAYOUT_HEXAGONAL;
467 case AV_CH_LAYOUT_6POINT1:
468 return CHANNEL_LAYOUT_6_1;
469 case AV_CH_LAYOUT_6POINT1_BACK:
470 return CHANNEL_LAYOUT_6_1_BACK;
471 case AV_CH_LAYOUT_6POINT1_FRONT:
472 return CHANNEL_LAYOUT_6_1_FRONT;
473 case AV_CH_LAYOUT_7POINT0_FRONT:
474 return CHANNEL_LAYOUT_7_0_FRONT;
475 #ifdef AV_CH_LAYOUT_7POINT1_WIDE_BACK
476 case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
477 return CHANNEL_LAYOUT_7_1_WIDE_BACK;
478 #endif
479 case AV_CH_LAYOUT_OCTAGONAL:
480 return CHANNEL_LAYOUT_OCTAGONAL;
481 default:
482 // FFmpeg channel_layout is 0 for .wav and .mp3. Attempt to guess layout
483 // based on the channel count.
484 return GuessChannelLayout(channels);
488 VideoFrame::Format PixelFormatToVideoFormat(PixelFormat pixel_format) {
489 switch (pixel_format) {
490 case PIX_FMT_YUV422P:
491 return VideoFrame::YV16;
492 // TODO(scherkus): We should be paying attention to the color range of each
493 // format and scaling as appropriate when rendering. Regular YUV has a range
494 // of 16-239 where as YUVJ has a range of 0-255.
495 case PIX_FMT_YUV420P:
496 case PIX_FMT_YUVJ420P:
497 return VideoFrame::YV12;
498 case PIX_FMT_YUVA420P:
499 return VideoFrame::YV12A;
500 default:
501 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format;
503 return VideoFrame::INVALID;
506 PixelFormat VideoFormatToPixelFormat(VideoFrame::Format video_format) {
507 switch (video_format) {
508 case VideoFrame::YV16:
509 return PIX_FMT_YUV422P;
510 case VideoFrame::YV12:
511 return PIX_FMT_YUV420P;
512 case VideoFrame::YV12A:
513 return PIX_FMT_YUVA420P;
514 default:
515 DVLOG(1) << "Unsupported VideoFrame::Format: " << video_format;
517 return PIX_FMT_NONE;
520 } // namespace media