Update broken references to image assets
[chromium-blink-merge.git] / media / ffmpeg / ffmpeg_common.cc
blob8550712bae2921cc52724faec28ad4cb45610747
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 "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "media/base/decoder_buffer.h"
14 #include "media/base/video_util.h"
16 namespace media {
18 // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are
19 // padded. Check here to ensure FFmpeg only receives data padded to its
20 // specifications.
21 static_assert(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE,
22 "DecoderBuffer padding size does not fit ffmpeg requirement");
24 // Alignment requirement by FFmpeg for input and output buffers. This need to
25 // be updated to match FFmpeg when it changes.
26 #if defined(ARCH_CPU_ARM_FAMILY)
27 static const int kFFmpegBufferAddressAlignment = 16;
28 #else
29 static const int kFFmpegBufferAddressAlignment = 32;
30 #endif
32 // Check here to ensure FFmpeg only receives data aligned to its specifications.
33 static_assert(
34 DecoderBuffer::kAlignmentSize >= kFFmpegBufferAddressAlignment &&
35 DecoderBuffer::kAlignmentSize % kFFmpegBufferAddressAlignment == 0,
36 "DecoderBuffer alignment size does not fit ffmpeg requirement");
38 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally.
39 // See video_get_buffer() in libavcodec/utils.c.
40 static const int kFFmpegOutputBufferPaddingSize = 16;
42 static_assert(VideoFrame::kFrameSizePadding >= kFFmpegOutputBufferPaddingSize,
43 "VideoFrame padding size does not fit ffmpeg requirement");
45 static_assert(
46 VideoFrame::kFrameAddressAlignment >= kFFmpegBufferAddressAlignment &&
47 VideoFrame::kFrameAddressAlignment % kFFmpegBufferAddressAlignment == 0,
48 "VideoFrame frame address alignment does not fit ffmpeg requirement");
50 static const AVRational kMicrosBase = { 1, base::Time::kMicrosecondsPerSecond };
52 base::TimeDelta ConvertFromTimeBase(const AVRational& time_base,
53 int64 timestamp) {
54 int64 microseconds = av_rescale_q(timestamp, time_base, kMicrosBase);
55 return base::TimeDelta::FromMicroseconds(microseconds);
58 int64 ConvertToTimeBase(const AVRational& time_base,
59 const base::TimeDelta& timestamp) {
60 return av_rescale_q(timestamp.InMicroseconds(), kMicrosBase, time_base);
63 // Converts an FFmpeg audio codec ID into its corresponding supported codec id.
64 static AudioCodec CodecIDToAudioCodec(AVCodecID codec_id) {
65 switch (codec_id) {
66 case AV_CODEC_ID_AAC:
67 return kCodecAAC;
68 case AV_CODEC_ID_MP3:
69 return kCodecMP3;
70 case AV_CODEC_ID_VORBIS:
71 return kCodecVorbis;
72 case AV_CODEC_ID_PCM_U8:
73 case AV_CODEC_ID_PCM_S16LE:
74 case AV_CODEC_ID_PCM_S24LE:
75 case AV_CODEC_ID_PCM_F32LE:
76 return kCodecPCM;
77 case AV_CODEC_ID_PCM_S16BE:
78 return kCodecPCM_S16BE;
79 case AV_CODEC_ID_PCM_S24BE:
80 return kCodecPCM_S24BE;
81 case AV_CODEC_ID_FLAC:
82 return kCodecFLAC;
83 case AV_CODEC_ID_AMR_NB:
84 return kCodecAMR_NB;
85 case AV_CODEC_ID_AMR_WB:
86 return kCodecAMR_WB;
87 case AV_CODEC_ID_GSM_MS:
88 return kCodecGSM_MS;
89 case AV_CODEC_ID_PCM_ALAW:
90 return kCodecPCM_ALAW;
91 case AV_CODEC_ID_PCM_MULAW:
92 return kCodecPCM_MULAW;
93 case AV_CODEC_ID_OPUS:
94 return kCodecOpus;
95 case AV_CODEC_ID_ALAC:
96 return kCodecALAC;
97 default:
98 DVLOG(1) << "Unknown audio CodecID: " << codec_id;
100 return kUnknownAudioCodec;
103 static AVCodecID AudioCodecToCodecID(AudioCodec audio_codec,
104 SampleFormat sample_format) {
105 switch (audio_codec) {
106 case kCodecAAC:
107 return AV_CODEC_ID_AAC;
108 case kCodecALAC:
109 return AV_CODEC_ID_ALAC;
110 case kCodecMP3:
111 return AV_CODEC_ID_MP3;
112 case kCodecPCM:
113 switch (sample_format) {
114 case kSampleFormatU8:
115 return AV_CODEC_ID_PCM_U8;
116 case kSampleFormatS16:
117 return AV_CODEC_ID_PCM_S16LE;
118 case kSampleFormatS32:
119 return AV_CODEC_ID_PCM_S24LE;
120 case kSampleFormatF32:
121 return AV_CODEC_ID_PCM_F32LE;
122 default:
123 DVLOG(1) << "Unsupported sample format: " << sample_format;
125 break;
126 case kCodecPCM_S16BE:
127 return AV_CODEC_ID_PCM_S16BE;
128 case kCodecPCM_S24BE:
129 return AV_CODEC_ID_PCM_S24BE;
130 case kCodecVorbis:
131 return AV_CODEC_ID_VORBIS;
132 case kCodecFLAC:
133 return AV_CODEC_ID_FLAC;
134 case kCodecAMR_NB:
135 return AV_CODEC_ID_AMR_NB;
136 case kCodecAMR_WB:
137 return AV_CODEC_ID_AMR_WB;
138 case kCodecGSM_MS:
139 return AV_CODEC_ID_GSM_MS;
140 case kCodecPCM_ALAW:
141 return AV_CODEC_ID_PCM_ALAW;
142 case kCodecPCM_MULAW:
143 return AV_CODEC_ID_PCM_MULAW;
144 case kCodecOpus:
145 return AV_CODEC_ID_OPUS;
146 default:
147 DVLOG(1) << "Unknown AudioCodec: " << audio_codec;
149 return AV_CODEC_ID_NONE;
152 // Converts an FFmpeg video codec ID into its corresponding supported codec id.
153 static VideoCodec CodecIDToVideoCodec(AVCodecID codec_id) {
154 switch (codec_id) {
155 case AV_CODEC_ID_H264:
156 return kCodecH264;
157 case AV_CODEC_ID_THEORA:
158 return kCodecTheora;
159 case AV_CODEC_ID_MPEG4:
160 return kCodecMPEG4;
161 case AV_CODEC_ID_VP8:
162 return kCodecVP8;
163 case AV_CODEC_ID_VP9:
164 return kCodecVP9;
165 default:
166 DVLOG(1) << "Unknown video CodecID: " << codec_id;
168 return kUnknownVideoCodec;
171 static AVCodecID VideoCodecToCodecID(VideoCodec video_codec) {
172 switch (video_codec) {
173 case kCodecH264:
174 return AV_CODEC_ID_H264;
175 case kCodecTheora:
176 return AV_CODEC_ID_THEORA;
177 case kCodecMPEG4:
178 return AV_CODEC_ID_MPEG4;
179 case kCodecVP8:
180 return AV_CODEC_ID_VP8;
181 case kCodecVP9:
182 return AV_CODEC_ID_VP9;
183 default:
184 DVLOG(1) << "Unknown VideoCodec: " << video_codec;
186 return AV_CODEC_ID_NONE;
189 static VideoCodecProfile ProfileIDToVideoCodecProfile(int profile) {
190 // Clear out the CONSTRAINED & INTRA flags which are strict subsets of the
191 // corresponding profiles with which they're used.
192 profile &= ~FF_PROFILE_H264_CONSTRAINED;
193 profile &= ~FF_PROFILE_H264_INTRA;
194 switch (profile) {
195 case FF_PROFILE_H264_BASELINE:
196 return H264PROFILE_BASELINE;
197 case FF_PROFILE_H264_MAIN:
198 return H264PROFILE_MAIN;
199 case FF_PROFILE_H264_EXTENDED:
200 return H264PROFILE_EXTENDED;
201 case FF_PROFILE_H264_HIGH:
202 return H264PROFILE_HIGH;
203 case FF_PROFILE_H264_HIGH_10:
204 return H264PROFILE_HIGH10PROFILE;
205 case FF_PROFILE_H264_HIGH_422:
206 return H264PROFILE_HIGH422PROFILE;
207 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
208 return H264PROFILE_HIGH444PREDICTIVEPROFILE;
209 default:
210 DVLOG(1) << "Unknown profile id: " << profile;
212 return VIDEO_CODEC_PROFILE_UNKNOWN;
215 static int VideoCodecProfileToProfileID(VideoCodecProfile profile) {
216 switch (profile) {
217 case H264PROFILE_BASELINE:
218 return FF_PROFILE_H264_BASELINE;
219 case H264PROFILE_MAIN:
220 return FF_PROFILE_H264_MAIN;
221 case H264PROFILE_EXTENDED:
222 return FF_PROFILE_H264_EXTENDED;
223 case H264PROFILE_HIGH:
224 return FF_PROFILE_H264_HIGH;
225 case H264PROFILE_HIGH10PROFILE:
226 return FF_PROFILE_H264_HIGH_10;
227 case H264PROFILE_HIGH422PROFILE:
228 return FF_PROFILE_H264_HIGH_422;
229 case H264PROFILE_HIGH444PREDICTIVEPROFILE:
230 return FF_PROFILE_H264_HIGH_444_PREDICTIVE;
231 default:
232 DVLOG(1) << "Unknown VideoCodecProfile: " << profile;
234 return FF_PROFILE_UNKNOWN;
237 SampleFormat AVSampleFormatToSampleFormat(AVSampleFormat sample_format) {
238 switch (sample_format) {
239 case AV_SAMPLE_FMT_U8:
240 return kSampleFormatU8;
241 case AV_SAMPLE_FMT_S16:
242 return kSampleFormatS16;
243 case AV_SAMPLE_FMT_S32:
244 return kSampleFormatS32;
245 case AV_SAMPLE_FMT_FLT:
246 return kSampleFormatF32;
247 case AV_SAMPLE_FMT_S16P:
248 return kSampleFormatPlanarS16;
249 case AV_SAMPLE_FMT_S32P:
250 return kSampleFormatPlanarS32;
251 case AV_SAMPLE_FMT_FLTP:
252 return kSampleFormatPlanarF32;
253 default:
254 DVLOG(1) << "Unknown AVSampleFormat: " << sample_format;
256 return kUnknownSampleFormat;
259 static AVSampleFormat SampleFormatToAVSampleFormat(SampleFormat sample_format) {
260 switch (sample_format) {
261 case kSampleFormatU8:
262 return AV_SAMPLE_FMT_U8;
263 case kSampleFormatS16:
264 return AV_SAMPLE_FMT_S16;
265 case kSampleFormatS32:
266 return AV_SAMPLE_FMT_S32;
267 case kSampleFormatF32:
268 return AV_SAMPLE_FMT_FLT;
269 case kSampleFormatPlanarS16:
270 return AV_SAMPLE_FMT_S16P;
271 case kSampleFormatPlanarF32:
272 return AV_SAMPLE_FMT_FLTP;
273 default:
274 DVLOG(1) << "Unknown SampleFormat: " << sample_format;
276 return AV_SAMPLE_FMT_NONE;
279 void AVCodecContextToAudioDecoderConfig(
280 const AVCodecContext* codec_context,
281 bool is_encrypted,
282 AudioDecoderConfig* config,
283 bool record_stats) {
284 DCHECK_EQ(codec_context->codec_type, AVMEDIA_TYPE_AUDIO);
286 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id);
288 SampleFormat sample_format =
289 AVSampleFormatToSampleFormat(codec_context->sample_fmt);
291 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout(
292 codec_context->channel_layout, codec_context->channels);
294 int sample_rate = codec_context->sample_rate;
295 if (codec == kCodecOpus) {
296 // |codec_context->sample_fmt| is not set by FFmpeg because Opus decoding is
297 // not enabled in FFmpeg. It doesn't matter what value is set here, so long
298 // as it's valid, the true sample format is selected inside the decoder.
299 sample_format = kSampleFormatF32;
301 // Always use 48kHz for OPUS. Technically we should match to the highest
302 // supported hardware sample rate among [8, 12, 16, 24, 48] kHz, but we
303 // don't know the hardware sample rate at this point and those rates are
304 // rarely used for output. See the "Input Sample Rate" section of the spec:
305 // http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11
306 sample_rate = 48000;
309 base::TimeDelta seek_preroll;
310 if (codec_context->seek_preroll > 0) {
311 seek_preroll = base::TimeDelta::FromMicroseconds(
312 codec_context->seek_preroll * 1000000.0 / codec_context->sample_rate);
315 config->Initialize(codec,
316 sample_format,
317 channel_layout,
318 sample_rate,
319 codec_context->extradata,
320 codec_context->extradata_size,
321 is_encrypted,
322 record_stats,
323 seek_preroll,
324 codec_context->delay);
325 if (codec != kCodecOpus) {
326 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8,
327 config->bits_per_channel());
331 void AVStreamToAudioDecoderConfig(
332 const AVStream* stream,
333 AudioDecoderConfig* config,
334 bool record_stats) {
335 bool is_encrypted = false;
336 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL, 0);
337 if (key)
338 is_encrypted = true;
339 return AVCodecContextToAudioDecoderConfig(
340 stream->codec, is_encrypted, config, record_stats);
343 void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config,
344 AVCodecContext* codec_context) {
345 codec_context->codec_type = AVMEDIA_TYPE_AUDIO;
346 codec_context->codec_id = AudioCodecToCodecID(config.codec(),
347 config.sample_format());
348 codec_context->sample_fmt = SampleFormatToAVSampleFormat(
349 config.sample_format());
351 // TODO(scherkus): should we set |channel_layout|? I'm not sure if FFmpeg uses
352 // said information to decode.
353 codec_context->channels =
354 ChannelLayoutToChannelCount(config.channel_layout());
355 codec_context->sample_rate = config.samples_per_second();
357 if (config.extra_data()) {
358 codec_context->extradata_size = config.extra_data_size();
359 codec_context->extradata = reinterpret_cast<uint8_t*>(
360 av_malloc(config.extra_data_size() + FF_INPUT_BUFFER_PADDING_SIZE));
361 memcpy(codec_context->extradata, config.extra_data(),
362 config.extra_data_size());
363 memset(codec_context->extradata + config.extra_data_size(), '\0',
364 FF_INPUT_BUFFER_PADDING_SIZE);
365 } else {
366 codec_context->extradata = NULL;
367 codec_context->extradata_size = 0;
371 void AVStreamToVideoDecoderConfig(
372 const AVStream* stream,
373 VideoDecoderConfig* config,
374 bool record_stats) {
375 gfx::Size coded_size(stream->codec->coded_width, stream->codec->coded_height);
377 // TODO(vrk): This assumes decoded frame data starts at (0, 0), which is true
378 // for now, but may not always be true forever. Fix this in the future.
379 gfx::Rect visible_rect(stream->codec->width, stream->codec->height);
381 AVRational aspect_ratio = { 1, 1 };
382 if (stream->sample_aspect_ratio.num)
383 aspect_ratio = stream->sample_aspect_ratio;
384 else if (stream->codec->sample_aspect_ratio.num)
385 aspect_ratio = stream->codec->sample_aspect_ratio;
387 VideoCodec codec = CodecIDToVideoCodec(stream->codec->codec_id);
389 VideoCodecProfile profile = VIDEO_CODEC_PROFILE_UNKNOWN;
390 if (codec == kCodecVP8)
391 profile = VP8PROFILE_ANY;
392 else if (codec == kCodecVP9)
393 profile = VP9PROFILE_ANY;
394 else
395 profile = ProfileIDToVideoCodecProfile(stream->codec->profile);
397 // Without the FFmpeg h264 decoder, AVFormat is unable to get the profile, so
398 // default to baseline and let the VDA fail later if it doesn't support the
399 // real profile. This is alright because if the FFmpeg h264 decoder isn't
400 // enabled, there is no fallback if the VDA fails.
401 #if defined(DISABLE_FFMPEG_VIDEO_DECODERS)
402 if (codec == kCodecH264)
403 profile = H264PROFILE_BASELINE;
404 #endif
406 gfx::Size natural_size = GetNaturalSize(
407 visible_rect.size(), aspect_ratio.num, aspect_ratio.den);
409 if (record_stats) {
410 // Note the PRESUBMIT_IGNORE_UMA_MAX below, this silences the PRESUBMIT.py
411 // check for uma enum max usage, since we're abusing
412 // UMA_HISTOGRAM_ENUMERATION to report a discrete value.
413 UMA_HISTOGRAM_ENUMERATION("Media.VideoColorRange",
414 stream->codec->color_range,
415 AVCOL_RANGE_NB); // PRESUBMIT_IGNORE_UMA_MAX
418 VideoPixelFormat format =
419 AVPixelFormatToVideoPixelFormat(stream->codec->pix_fmt);
420 // The format and coded size may be unknown if FFmpeg is compiled without
421 // video decoders.
422 #if defined(DISABLE_FFMPEG_VIDEO_DECODERS)
423 if (format == PIXEL_FORMAT_UNKNOWN)
424 format = PIXEL_FORMAT_YV12;
425 if (coded_size == gfx::Size(0, 0))
426 coded_size = visible_rect.size();
427 #endif
429 if (codec == kCodecVP9) {
430 // TODO(tomfinegan): libavcodec doesn't know about VP9.
431 format = PIXEL_FORMAT_YV12;
432 coded_size = visible_rect.size();
435 // Pad out |coded_size| for subsampled YUV formats.
436 if (format != PIXEL_FORMAT_YV24) {
437 coded_size.set_width((coded_size.width() + 1) / 2 * 2);
438 if (format != PIXEL_FORMAT_YV16)
439 coded_size.set_height((coded_size.height() + 1) / 2 * 2);
442 bool is_encrypted = false;
443 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL, 0);
444 if (key)
445 is_encrypted = true;
447 AVDictionaryEntry* webm_alpha =
448 av_dict_get(stream->metadata, "alpha_mode", NULL, 0);
449 if (webm_alpha && !strcmp(webm_alpha->value, "1")) {
450 format = PIXEL_FORMAT_YV12A;
453 // Prefer the color space found by libavcodec if available.
454 ColorSpace color_space = AVColorSpaceToColorSpace(stream->codec->colorspace,
455 stream->codec->color_range);
456 if (color_space == COLOR_SPACE_UNSPECIFIED) {
457 // Otherwise, assume that SD video is usually Rec.601, and HD is usually
458 // Rec.709.
459 color_space = (natural_size.height() < 720) ? COLOR_SPACE_SD_REC601
460 : COLOR_SPACE_HD_REC709;
463 config->Initialize(codec, profile, format, color_space, coded_size,
464 visible_rect, natural_size, stream->codec->extradata,
465 stream->codec->extradata_size, is_encrypted, record_stats);
468 void VideoDecoderConfigToAVCodecContext(
469 const VideoDecoderConfig& config,
470 AVCodecContext* codec_context) {
471 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
472 codec_context->codec_id = VideoCodecToCodecID(config.codec());
473 codec_context->profile = VideoCodecProfileToProfileID(config.profile());
474 codec_context->coded_width = config.coded_size().width();
475 codec_context->coded_height = config.coded_size().height();
476 codec_context->pix_fmt = VideoPixelFormatToAVPixelFormat(config.format());
477 if (config.color_space() == COLOR_SPACE_JPEG)
478 codec_context->color_range = AVCOL_RANGE_JPEG;
480 if (config.extra_data()) {
481 codec_context->extradata_size = config.extra_data_size();
482 codec_context->extradata = reinterpret_cast<uint8_t*>(
483 av_malloc(config.extra_data_size() + FF_INPUT_BUFFER_PADDING_SIZE));
484 memcpy(codec_context->extradata, config.extra_data(),
485 config.extra_data_size());
486 memset(codec_context->extradata + config.extra_data_size(), '\0',
487 FF_INPUT_BUFFER_PADDING_SIZE);
488 } else {
489 codec_context->extradata = NULL;
490 codec_context->extradata_size = 0;
494 ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout, int channels) {
495 switch (layout) {
496 case AV_CH_LAYOUT_MONO:
497 return CHANNEL_LAYOUT_MONO;
498 case AV_CH_LAYOUT_STEREO:
499 return CHANNEL_LAYOUT_STEREO;
500 case AV_CH_LAYOUT_2_1:
501 return CHANNEL_LAYOUT_2_1;
502 case AV_CH_LAYOUT_SURROUND:
503 return CHANNEL_LAYOUT_SURROUND;
504 case AV_CH_LAYOUT_4POINT0:
505 return CHANNEL_LAYOUT_4_0;
506 case AV_CH_LAYOUT_2_2:
507 return CHANNEL_LAYOUT_2_2;
508 case AV_CH_LAYOUT_QUAD:
509 return CHANNEL_LAYOUT_QUAD;
510 case AV_CH_LAYOUT_5POINT0:
511 return CHANNEL_LAYOUT_5_0;
512 case AV_CH_LAYOUT_5POINT1:
513 return CHANNEL_LAYOUT_5_1;
514 case AV_CH_LAYOUT_5POINT0_BACK:
515 return CHANNEL_LAYOUT_5_0_BACK;
516 case AV_CH_LAYOUT_5POINT1_BACK:
517 return CHANNEL_LAYOUT_5_1_BACK;
518 case AV_CH_LAYOUT_7POINT0:
519 return CHANNEL_LAYOUT_7_0;
520 case AV_CH_LAYOUT_7POINT1:
521 return CHANNEL_LAYOUT_7_1;
522 case AV_CH_LAYOUT_7POINT1_WIDE:
523 return CHANNEL_LAYOUT_7_1_WIDE;
524 case AV_CH_LAYOUT_STEREO_DOWNMIX:
525 return CHANNEL_LAYOUT_STEREO_DOWNMIX;
526 case AV_CH_LAYOUT_2POINT1:
527 return CHANNEL_LAYOUT_2POINT1;
528 case AV_CH_LAYOUT_3POINT1:
529 return CHANNEL_LAYOUT_3_1;
530 case AV_CH_LAYOUT_4POINT1:
531 return CHANNEL_LAYOUT_4_1;
532 case AV_CH_LAYOUT_6POINT0:
533 return CHANNEL_LAYOUT_6_0;
534 case AV_CH_LAYOUT_6POINT0_FRONT:
535 return CHANNEL_LAYOUT_6_0_FRONT;
536 case AV_CH_LAYOUT_HEXAGONAL:
537 return CHANNEL_LAYOUT_HEXAGONAL;
538 case AV_CH_LAYOUT_6POINT1:
539 return CHANNEL_LAYOUT_6_1;
540 case AV_CH_LAYOUT_6POINT1_BACK:
541 return CHANNEL_LAYOUT_6_1_BACK;
542 case AV_CH_LAYOUT_6POINT1_FRONT:
543 return CHANNEL_LAYOUT_6_1_FRONT;
544 case AV_CH_LAYOUT_7POINT0_FRONT:
545 return CHANNEL_LAYOUT_7_0_FRONT;
546 #ifdef AV_CH_LAYOUT_7POINT1_WIDE_BACK
547 case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
548 return CHANNEL_LAYOUT_7_1_WIDE_BACK;
549 #endif
550 case AV_CH_LAYOUT_OCTAGONAL:
551 return CHANNEL_LAYOUT_OCTAGONAL;
552 default:
553 // FFmpeg channel_layout is 0 for .wav and .mp3. Attempt to guess layout
554 // based on the channel count.
555 return GuessChannelLayout(channels);
559 VideoPixelFormat AVPixelFormatToVideoPixelFormat(AVPixelFormat pixel_format) {
560 // The YUVJ alternatives are FFmpeg's (deprecated, but still in use) way to
561 // specify a pixel format and full range color combination.
562 switch (pixel_format) {
563 case AV_PIX_FMT_YUV422P:
564 case AV_PIX_FMT_YUVJ422P:
565 return PIXEL_FORMAT_YV16;
566 case AV_PIX_FMT_YUV444P:
567 case AV_PIX_FMT_YUVJ444P:
568 return PIXEL_FORMAT_YV24;
569 case AV_PIX_FMT_YUV420P:
570 case AV_PIX_FMT_YUVJ420P:
571 return PIXEL_FORMAT_YV12;
572 case AV_PIX_FMT_YUVA420P:
573 return PIXEL_FORMAT_YV12A;
574 default:
575 DVLOG(1) << "Unsupported AVPixelFormat: " << pixel_format;
577 return PIXEL_FORMAT_UNKNOWN;
580 AVPixelFormat VideoPixelFormatToAVPixelFormat(VideoPixelFormat video_format) {
581 switch (video_format) {
582 case PIXEL_FORMAT_YV16:
583 return AV_PIX_FMT_YUV422P;
584 case PIXEL_FORMAT_YV12:
585 return AV_PIX_FMT_YUV420P;
586 case PIXEL_FORMAT_YV12A:
587 return AV_PIX_FMT_YUVA420P;
588 case PIXEL_FORMAT_YV24:
589 return AV_PIX_FMT_YUV444P;
590 default:
591 DVLOG(1) << "Unsupported Format: " << video_format;
593 return AV_PIX_FMT_NONE;
596 ColorSpace AVColorSpaceToColorSpace(AVColorSpace color_space,
597 AVColorRange color_range) {
598 if (color_range == AVCOL_RANGE_JPEG)
599 return COLOR_SPACE_JPEG;
601 switch (color_space) {
602 case AVCOL_SPC_UNSPECIFIED:
603 break;
604 case AVCOL_SPC_BT709:
605 return COLOR_SPACE_HD_REC709;
606 case AVCOL_SPC_SMPTE170M:
607 case AVCOL_SPC_BT470BG:
608 return COLOR_SPACE_SD_REC601;
609 default:
610 DVLOG(1) << "Unknown AVColorSpace: " << color_space;
612 return COLOR_SPACE_UNSPECIFIED;
615 bool FFmpegUTCDateToTime(const char* date_utc, base::Time* out) {
616 DCHECK(date_utc);
617 DCHECK(out);
619 std::vector<base::StringPiece> fields = base::SplitStringPiece(
620 date_utc, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
621 if (fields.size() != 2)
622 return false;
624 std::vector<base::StringPiece> date_fields = base::SplitStringPiece(
625 fields[0], "-", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
626 if (date_fields.size() != 3)
627 return false;
629 // TODO(acolwell): Update this parsing code when FFmpeg returns sub-second
630 // information.
631 std::vector<base::StringPiece> time_fields = base::SplitStringPiece(
632 fields[1], ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
633 if (time_fields.size() != 3)
634 return false;
636 base::Time::Exploded exploded;
637 exploded.millisecond = 0;
638 if (base::StringToInt(date_fields[0], &exploded.year) &&
639 base::StringToInt(date_fields[1], &exploded.month) &&
640 base::StringToInt(date_fields[2], &exploded.day_of_month) &&
641 base::StringToInt(time_fields[0], &exploded.hour) &&
642 base::StringToInt(time_fields[1], &exploded.minute) &&
643 base::StringToInt(time_fields[2], &exploded.second)) {
644 base::Time parsed_time = base::Time::FromUTCExploded(exploded);
645 if (parsed_time.is_null())
646 return false;
648 *out = parsed_time;
649 return true;
652 return false;
655 } // namespace media