Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / ffmpeg / ffmpeg_common.cc
blob5b6ef7fe3c19c3df7b31ffb194240198bc3e6f81
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 // The format and coded size may be unknown if FFmpeg is compiled without
412 // video decoders.
413 #if defined(DISABLE_FFMPEG_VIDEO_DECODERS)
414 if (format == PIXEL_FORMAT_UNKNOWN)
415 format = PIXEL_FORMAT_YV12;
416 if (coded_size == gfx::Size(0, 0))
417 coded_size = visible_rect.size();
418 #endif
420 if (codec == kCodecVP9) {
421 // TODO(tomfinegan): libavcodec doesn't know about VP9.
422 format = PIXEL_FORMAT_YV12;
423 coded_size = visible_rect.size();
426 // Pad out |coded_size| for subsampled YUV formats.
427 if (format != PIXEL_FORMAT_YV24) {
428 coded_size.set_width((coded_size.width() + 1) / 2 * 2);
429 if (format != PIXEL_FORMAT_YV16)
430 coded_size.set_height((coded_size.height() + 1) / 2 * 2);
433 bool is_encrypted = false;
434 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL, 0);
435 if (key)
436 is_encrypted = true;
438 AVDictionaryEntry* webm_alpha =
439 av_dict_get(stream->metadata, "alpha_mode", NULL, 0);
440 if (webm_alpha && !strcmp(webm_alpha->value, "1")) {
441 format = PIXEL_FORMAT_YV12A;
444 // Prefer the color space found by libavcodec if available.
445 ColorSpace color_space = AVColorSpaceToColorSpace(stream->codec->colorspace,
446 stream->codec->color_range);
447 if (color_space == COLOR_SPACE_UNSPECIFIED) {
448 // Otherwise, assume that SD video is usually Rec.601, and HD is usually
449 // Rec.709.
450 color_space = (natural_size.height() < 720) ? COLOR_SPACE_SD_REC601
451 : COLOR_SPACE_HD_REC709;
454 config->Initialize(codec, profile, format, color_space, coded_size,
455 visible_rect, natural_size, stream->codec->extradata,
456 stream->codec->extradata_size, is_encrypted, record_stats);
459 void VideoDecoderConfigToAVCodecContext(
460 const VideoDecoderConfig& config,
461 AVCodecContext* codec_context) {
462 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
463 codec_context->codec_id = VideoCodecToCodecID(config.codec());
464 codec_context->profile = VideoCodecProfileToProfileID(config.profile());
465 codec_context->coded_width = config.coded_size().width();
466 codec_context->coded_height = config.coded_size().height();
467 codec_context->pix_fmt = VideoPixelFormatToPixelFormat(config.format());
468 if (config.color_space() == COLOR_SPACE_JPEG)
469 codec_context->color_range = AVCOL_RANGE_JPEG;
471 if (config.extra_data()) {
472 codec_context->extradata_size = config.extra_data_size();
473 codec_context->extradata = reinterpret_cast<uint8_t*>(
474 av_malloc(config.extra_data_size() + FF_INPUT_BUFFER_PADDING_SIZE));
475 memcpy(codec_context->extradata, config.extra_data(),
476 config.extra_data_size());
477 memset(codec_context->extradata + config.extra_data_size(), '\0',
478 FF_INPUT_BUFFER_PADDING_SIZE);
479 } else {
480 codec_context->extradata = NULL;
481 codec_context->extradata_size = 0;
485 ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout, int channels) {
486 switch (layout) {
487 case AV_CH_LAYOUT_MONO:
488 return CHANNEL_LAYOUT_MONO;
489 case AV_CH_LAYOUT_STEREO:
490 return CHANNEL_LAYOUT_STEREO;
491 case AV_CH_LAYOUT_2_1:
492 return CHANNEL_LAYOUT_2_1;
493 case AV_CH_LAYOUT_SURROUND:
494 return CHANNEL_LAYOUT_SURROUND;
495 case AV_CH_LAYOUT_4POINT0:
496 return CHANNEL_LAYOUT_4_0;
497 case AV_CH_LAYOUT_2_2:
498 return CHANNEL_LAYOUT_2_2;
499 case AV_CH_LAYOUT_QUAD:
500 return CHANNEL_LAYOUT_QUAD;
501 case AV_CH_LAYOUT_5POINT0:
502 return CHANNEL_LAYOUT_5_0;
503 case AV_CH_LAYOUT_5POINT1:
504 return CHANNEL_LAYOUT_5_1;
505 case AV_CH_LAYOUT_5POINT0_BACK:
506 return CHANNEL_LAYOUT_5_0_BACK;
507 case AV_CH_LAYOUT_5POINT1_BACK:
508 return CHANNEL_LAYOUT_5_1_BACK;
509 case AV_CH_LAYOUT_7POINT0:
510 return CHANNEL_LAYOUT_7_0;
511 case AV_CH_LAYOUT_7POINT1:
512 return CHANNEL_LAYOUT_7_1;
513 case AV_CH_LAYOUT_7POINT1_WIDE:
514 return CHANNEL_LAYOUT_7_1_WIDE;
515 case AV_CH_LAYOUT_STEREO_DOWNMIX:
516 return CHANNEL_LAYOUT_STEREO_DOWNMIX;
517 case AV_CH_LAYOUT_2POINT1:
518 return CHANNEL_LAYOUT_2POINT1;
519 case AV_CH_LAYOUT_3POINT1:
520 return CHANNEL_LAYOUT_3_1;
521 case AV_CH_LAYOUT_4POINT1:
522 return CHANNEL_LAYOUT_4_1;
523 case AV_CH_LAYOUT_6POINT0:
524 return CHANNEL_LAYOUT_6_0;
525 case AV_CH_LAYOUT_6POINT0_FRONT:
526 return CHANNEL_LAYOUT_6_0_FRONT;
527 case AV_CH_LAYOUT_HEXAGONAL:
528 return CHANNEL_LAYOUT_HEXAGONAL;
529 case AV_CH_LAYOUT_6POINT1:
530 return CHANNEL_LAYOUT_6_1;
531 case AV_CH_LAYOUT_6POINT1_BACK:
532 return CHANNEL_LAYOUT_6_1_BACK;
533 case AV_CH_LAYOUT_6POINT1_FRONT:
534 return CHANNEL_LAYOUT_6_1_FRONT;
535 case AV_CH_LAYOUT_7POINT0_FRONT:
536 return CHANNEL_LAYOUT_7_0_FRONT;
537 #ifdef AV_CH_LAYOUT_7POINT1_WIDE_BACK
538 case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
539 return CHANNEL_LAYOUT_7_1_WIDE_BACK;
540 #endif
541 case AV_CH_LAYOUT_OCTAGONAL:
542 return CHANNEL_LAYOUT_OCTAGONAL;
543 default:
544 // FFmpeg channel_layout is 0 for .wav and .mp3. Attempt to guess layout
545 // based on the channel count.
546 return GuessChannelLayout(channels);
550 VideoPixelFormat PixelFormatToVideoPixelFormat(PixelFormat pixel_format) {
551 // The YUVJ alternatives are FFmpeg's (deprecated, but still in use) way to
552 // specify a pixel format and full range color combination.
553 switch (pixel_format) {
554 case PIX_FMT_YUV422P:
555 case PIX_FMT_YUVJ422P:
556 return PIXEL_FORMAT_YV16;
557 case PIX_FMT_YUV444P:
558 case PIX_FMT_YUVJ444P:
559 return PIXEL_FORMAT_YV24;
560 case PIX_FMT_YUV420P:
561 case PIX_FMT_YUVJ420P:
562 return PIXEL_FORMAT_YV12;
563 case PIX_FMT_YUVA420P:
564 return PIXEL_FORMAT_YV12A;
565 default:
566 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format;
568 return PIXEL_FORMAT_UNKNOWN;
571 PixelFormat VideoPixelFormatToPixelFormat(VideoPixelFormat video_format) {
572 switch (video_format) {
573 case PIXEL_FORMAT_YV16:
574 return PIX_FMT_YUV422P;
575 case PIXEL_FORMAT_YV12:
576 return PIX_FMT_YUV420P;
577 case PIXEL_FORMAT_YV12A:
578 return PIX_FMT_YUVA420P;
579 case PIXEL_FORMAT_YV24:
580 return PIX_FMT_YUV444P;
581 default:
582 DVLOG(1) << "Unsupported Format: " << video_format;
584 return PIX_FMT_NONE;
587 ColorSpace AVColorSpaceToColorSpace(AVColorSpace color_space,
588 AVColorRange color_range) {
589 if (color_range == AVCOL_RANGE_JPEG)
590 return COLOR_SPACE_JPEG;
592 switch (color_space) {
593 case AVCOL_SPC_UNSPECIFIED:
594 break;
595 case AVCOL_SPC_BT709:
596 return COLOR_SPACE_HD_REC709;
597 case AVCOL_SPC_SMPTE170M:
598 case AVCOL_SPC_BT470BG:
599 return COLOR_SPACE_SD_REC601;
600 default:
601 DVLOG(1) << "Unknown AVColorSpace: " << color_space;
603 return COLOR_SPACE_UNSPECIFIED;
606 bool FFmpegUTCDateToTime(const char* date_utc, base::Time* out) {
607 DCHECK(date_utc);
608 DCHECK(out);
610 std::vector<base::StringPiece> fields = base::SplitStringPiece(
611 date_utc, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
612 if (fields.size() != 2)
613 return false;
615 std::vector<base::StringPiece> date_fields = base::SplitStringPiece(
616 fields[0], "-", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
617 if (date_fields.size() != 3)
618 return false;
620 // TODO(acolwell): Update this parsing code when FFmpeg returns sub-second
621 // information.
622 std::vector<base::StringPiece> time_fields = base::SplitStringPiece(
623 fields[1], ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
624 if (time_fields.size() != 3)
625 return false;
627 base::Time::Exploded exploded;
628 exploded.millisecond = 0;
629 if (base::StringToInt(date_fields[0], &exploded.year) &&
630 base::StringToInt(date_fields[1], &exploded.month) &&
631 base::StringToInt(date_fields[2], &exploded.day_of_month) &&
632 base::StringToInt(time_fields[0], &exploded.hour) &&
633 base::StringToInt(time_fields[1], &exploded.minute) &&
634 base::StringToInt(time_fields[2], &exploded.second)) {
635 base::Time parsed_time = base::Time::FromUTCExploded(exploded);
636 if (parsed_time.is_null())
637 return false;
639 *out = parsed_time;
640 return true;
643 return false;
646 } // namespace media