Move UpdateManifest test from unit_tests into extensions_unittests.
[chromium-blink-merge.git] / media / ffmpeg / ffmpeg_common.cc
blobe2e9892f50fa238595218ae1e190ad49933bdd62
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 gfx::Size natural_size = GetNaturalSize(
398 visible_rect.size(), aspect_ratio.num, aspect_ratio.den);
400 if (record_stats) {
401 // Note the PRESUBMIT_IGNORE_UMA_MAX below, this silences the PRESUBMIT.py
402 // check for uma enum max usage, since we're abusing
403 // UMA_HISTOGRAM_ENUMERATION to report a discrete value.
404 UMA_HISTOGRAM_ENUMERATION("Media.VideoColorRange",
405 stream->codec->color_range,
406 AVCOL_RANGE_NB); // PRESUBMIT_IGNORE_UMA_MAX
409 VideoPixelFormat format =
410 PixelFormatToVideoPixelFormat(stream->codec->pix_fmt);
411 if (codec == kCodecVP9) {
412 // TODO(tomfinegan): libavcodec doesn't know about VP9.
413 format = PIXEL_FORMAT_YV12;
414 coded_size = visible_rect.size();
417 // Pad out |coded_size| for subsampled YUV formats.
418 if (format != PIXEL_FORMAT_YV24) {
419 coded_size.set_width((coded_size.width() + 1) / 2 * 2);
420 if (format != PIXEL_FORMAT_YV16)
421 coded_size.set_height((coded_size.height() + 1) / 2 * 2);
424 bool is_encrypted = false;
425 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL, 0);
426 if (key)
427 is_encrypted = true;
429 AVDictionaryEntry* webm_alpha =
430 av_dict_get(stream->metadata, "alpha_mode", NULL, 0);
431 if (webm_alpha && !strcmp(webm_alpha->value, "1")) {
432 format = PIXEL_FORMAT_YV12A;
435 // Prefer the color space found by libavcodec if available.
436 ColorSpace color_space = AVColorSpaceToColorSpace(stream->codec->colorspace);
437 if (color_space == COLOR_SPACE_UNSPECIFIED) {
438 // Otherwise, assume that SD video is usually Rec.601, and HD is usually
439 // Rec.709.
440 color_space = (natural_size.height() < 720) ? COLOR_SPACE_SD_REC601
441 : COLOR_SPACE_HD_REC709;
444 config->Initialize(codec, profile, format, color_space, coded_size,
445 visible_rect, natural_size, stream->codec->extradata,
446 stream->codec->extradata_size, is_encrypted, record_stats);
449 void VideoDecoderConfigToAVCodecContext(
450 const VideoDecoderConfig& config,
451 AVCodecContext* codec_context) {
452 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
453 codec_context->codec_id = VideoCodecToCodecID(config.codec());
454 codec_context->profile = VideoCodecProfileToProfileID(config.profile());
455 codec_context->coded_width = config.coded_size().width();
456 codec_context->coded_height = config.coded_size().height();
457 codec_context->pix_fmt = VideoPixelFormatToPixelFormat(config.format());
459 if (config.extra_data()) {
460 codec_context->extradata_size = config.extra_data_size();
461 codec_context->extradata = reinterpret_cast<uint8_t*>(
462 av_malloc(config.extra_data_size() + FF_INPUT_BUFFER_PADDING_SIZE));
463 memcpy(codec_context->extradata, config.extra_data(),
464 config.extra_data_size());
465 memset(codec_context->extradata + config.extra_data_size(), '\0',
466 FF_INPUT_BUFFER_PADDING_SIZE);
467 } else {
468 codec_context->extradata = NULL;
469 codec_context->extradata_size = 0;
473 ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout, int channels) {
474 switch (layout) {
475 case AV_CH_LAYOUT_MONO:
476 return CHANNEL_LAYOUT_MONO;
477 case AV_CH_LAYOUT_STEREO:
478 return CHANNEL_LAYOUT_STEREO;
479 case AV_CH_LAYOUT_2_1:
480 return CHANNEL_LAYOUT_2_1;
481 case AV_CH_LAYOUT_SURROUND:
482 return CHANNEL_LAYOUT_SURROUND;
483 case AV_CH_LAYOUT_4POINT0:
484 return CHANNEL_LAYOUT_4_0;
485 case AV_CH_LAYOUT_2_2:
486 return CHANNEL_LAYOUT_2_2;
487 case AV_CH_LAYOUT_QUAD:
488 return CHANNEL_LAYOUT_QUAD;
489 case AV_CH_LAYOUT_5POINT0:
490 return CHANNEL_LAYOUT_5_0;
491 case AV_CH_LAYOUT_5POINT1:
492 return CHANNEL_LAYOUT_5_1;
493 case AV_CH_LAYOUT_5POINT0_BACK:
494 return CHANNEL_LAYOUT_5_0_BACK;
495 case AV_CH_LAYOUT_5POINT1_BACK:
496 return CHANNEL_LAYOUT_5_1_BACK;
497 case AV_CH_LAYOUT_7POINT0:
498 return CHANNEL_LAYOUT_7_0;
499 case AV_CH_LAYOUT_7POINT1:
500 return CHANNEL_LAYOUT_7_1;
501 case AV_CH_LAYOUT_7POINT1_WIDE:
502 return CHANNEL_LAYOUT_7_1_WIDE;
503 case AV_CH_LAYOUT_STEREO_DOWNMIX:
504 return CHANNEL_LAYOUT_STEREO_DOWNMIX;
505 case AV_CH_LAYOUT_2POINT1:
506 return CHANNEL_LAYOUT_2POINT1;
507 case AV_CH_LAYOUT_3POINT1:
508 return CHANNEL_LAYOUT_3_1;
509 case AV_CH_LAYOUT_4POINT1:
510 return CHANNEL_LAYOUT_4_1;
511 case AV_CH_LAYOUT_6POINT0:
512 return CHANNEL_LAYOUT_6_0;
513 case AV_CH_LAYOUT_6POINT0_FRONT:
514 return CHANNEL_LAYOUT_6_0_FRONT;
515 case AV_CH_LAYOUT_HEXAGONAL:
516 return CHANNEL_LAYOUT_HEXAGONAL;
517 case AV_CH_LAYOUT_6POINT1:
518 return CHANNEL_LAYOUT_6_1;
519 case AV_CH_LAYOUT_6POINT1_BACK:
520 return CHANNEL_LAYOUT_6_1_BACK;
521 case AV_CH_LAYOUT_6POINT1_FRONT:
522 return CHANNEL_LAYOUT_6_1_FRONT;
523 case AV_CH_LAYOUT_7POINT0_FRONT:
524 return CHANNEL_LAYOUT_7_0_FRONT;
525 #ifdef AV_CH_LAYOUT_7POINT1_WIDE_BACK
526 case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
527 return CHANNEL_LAYOUT_7_1_WIDE_BACK;
528 #endif
529 case AV_CH_LAYOUT_OCTAGONAL:
530 return CHANNEL_LAYOUT_OCTAGONAL;
531 default:
532 // FFmpeg channel_layout is 0 for .wav and .mp3. Attempt to guess layout
533 // based on the channel count.
534 return GuessChannelLayout(channels);
538 VideoPixelFormat PixelFormatToVideoPixelFormat(PixelFormat pixel_format) {
539 switch (pixel_format) {
540 case PIX_FMT_YUV422P:
541 return PIXEL_FORMAT_YV16;
542 case PIX_FMT_YUV444P:
543 return PIXEL_FORMAT_YV24;
544 case PIX_FMT_YUV420P:
545 return PIXEL_FORMAT_YV12;
546 case PIX_FMT_YUVA420P:
547 return PIXEL_FORMAT_YV12A;
548 default:
549 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format;
551 return PIXEL_FORMAT_UNKNOWN;
554 PixelFormat VideoPixelFormatToPixelFormat(VideoPixelFormat video_format) {
555 switch (video_format) {
556 case PIXEL_FORMAT_YV16:
557 return PIX_FMT_YUV422P;
558 case PIXEL_FORMAT_YV12:
559 return PIX_FMT_YUV420P;
560 case PIXEL_FORMAT_YV12A:
561 return PIX_FMT_YUVA420P;
562 case PIXEL_FORMAT_YV24:
563 return PIX_FMT_YUV444P;
564 default:
565 DVLOG(1) << "Unsupported Format: " << video_format;
567 return PIX_FMT_NONE;
570 ColorSpace AVColorSpaceToColorSpace(
571 AVColorSpace color_space) {
572 switch (color_space) {
573 case AVCOL_SPC_UNSPECIFIED:
574 break;
575 case AVCOL_SPC_BT709:
576 return COLOR_SPACE_HD_REC709;
577 case AVCOL_SPC_SMPTE170M:
578 case AVCOL_SPC_BT470BG:
579 return COLOR_SPACE_SD_REC601;
580 default:
581 DVLOG(1) << "Unknown AVColorSpace: " << color_space;
583 return COLOR_SPACE_UNSPECIFIED;
586 bool FFmpegUTCDateToTime(const char* date_utc, base::Time* out) {
587 DCHECK(date_utc);
588 DCHECK(out);
590 std::vector<base::StringPiece> fields = base::SplitStringPiece(
591 date_utc, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
592 if (fields.size() != 2)
593 return false;
595 std::vector<base::StringPiece> date_fields = base::SplitStringPiece(
596 fields[0], "-", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
597 if (date_fields.size() != 3)
598 return false;
600 // TODO(acolwell): Update this parsing code when FFmpeg returns sub-second
601 // information.
602 std::vector<base::StringPiece> time_fields = base::SplitStringPiece(
603 fields[1], ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
604 if (time_fields.size() != 3)
605 return false;
607 base::Time::Exploded exploded;
608 exploded.millisecond = 0;
609 if (base::StringToInt(date_fields[0], &exploded.year) &&
610 base::StringToInt(date_fields[1], &exploded.month) &&
611 base::StringToInt(date_fields[2], &exploded.day_of_month) &&
612 base::StringToInt(time_fields[0], &exploded.hour) &&
613 base::StringToInt(time_fields[1], &exploded.minute) &&
614 base::StringToInt(time_fields[2], &exploded.second)) {
615 base::Time parsed_time = base::Time::FromUTCExploded(exploded);
616 if (parsed_time.is_null())
617 return false;
619 *out = parsed_time;
620 return true;
623 return false;
626 } // namespace media