Update broken references to image assets
[chromium-blink-merge.git] / media / base / video_decoder_config.cc
blob0ad2f621a1c3d9e14f5102e3c4a2876e41dbd2fe
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/base/video_decoder_config.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "media/base/video_frame.h"
11 namespace media {
13 VideoCodec VideoCodecProfileToVideoCodec(VideoCodecProfile profile) {
14 switch (profile) {
15 case VIDEO_CODEC_PROFILE_UNKNOWN:
16 return kUnknownVideoCodec;
17 case H264PROFILE_BASELINE:
18 case H264PROFILE_MAIN:
19 case H264PROFILE_EXTENDED:
20 case H264PROFILE_HIGH:
21 case H264PROFILE_HIGH10PROFILE:
22 case H264PROFILE_HIGH422PROFILE:
23 case H264PROFILE_HIGH444PREDICTIVEPROFILE:
24 case H264PROFILE_SCALABLEBASELINE:
25 case H264PROFILE_SCALABLEHIGH:
26 case H264PROFILE_STEREOHIGH:
27 case H264PROFILE_MULTIVIEWHIGH:
28 return kCodecH264;
29 case VP8PROFILE_ANY:
30 return kCodecVP8;
31 case VP9PROFILE_ANY:
32 return kCodecVP9;
34 NOTREACHED();
35 return kUnknownVideoCodec;
39 VideoDecoderConfig::VideoDecoderConfig()
40 : codec_(kUnknownVideoCodec),
41 profile_(VIDEO_CODEC_PROFILE_UNKNOWN),
42 format_(PIXEL_FORMAT_UNKNOWN),
43 is_encrypted_(false) {
46 VideoDecoderConfig::VideoDecoderConfig(VideoCodec codec,
47 VideoCodecProfile profile,
48 VideoPixelFormat format,
49 ColorSpace color_space,
50 const gfx::Size& coded_size,
51 const gfx::Rect& visible_rect,
52 const gfx::Size& natural_size,
53 const uint8* extra_data,
54 size_t extra_data_size,
55 bool is_encrypted) {
56 Initialize(codec, profile, format, color_space, coded_size, visible_rect,
57 natural_size, extra_data, extra_data_size, is_encrypted, true);
60 VideoDecoderConfig::~VideoDecoderConfig() {}
62 // Some videos just want to watch the world burn, with a height of 0; cap the
63 // "infinite" aspect ratio resulting.
64 static const int kInfiniteRatio = 99999;
66 // Common aspect ratios (multiplied by 100 and truncated) used for histogramming
67 // video sizes. These were taken on 20111103 from
68 // http://wikipedia.org/wiki/Aspect_ratio_(image)#Previous_and_currently_used_aspect_ratios
69 static const int kCommonAspectRatios100[] = {
70 100, 115, 133, 137, 143, 150, 155, 160, 166, 175, 177, 185, 200, 210, 220,
71 221, 235, 237, 240, 255, 259, 266, 276, 293, 400, 1200, kInfiniteRatio,
74 template<class T> // T has int width() & height() methods.
75 static void UmaHistogramAspectRatio(const char* name, const T& size) {
76 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
77 name,
78 // Intentionally use integer division to truncate the result.
79 size.height() ? (size.width() * 100) / size.height() : kInfiniteRatio,
80 base::CustomHistogram::ArrayToCustomRanges(
81 kCommonAspectRatios100, arraysize(kCommonAspectRatios100)));
84 void VideoDecoderConfig::Initialize(VideoCodec codec,
85 VideoCodecProfile profile,
86 VideoPixelFormat format,
87 ColorSpace color_space,
88 const gfx::Size& coded_size,
89 const gfx::Rect& visible_rect,
90 const gfx::Size& natural_size,
91 const uint8* extra_data,
92 size_t extra_data_size,
93 bool is_encrypted,
94 bool record_stats) {
95 CHECK((extra_data_size != 0) == (extra_data != NULL));
97 if (record_stats) {
98 UMA_HISTOGRAM_ENUMERATION("Media.VideoCodec", codec, kVideoCodecMax + 1);
99 // Drop UNKNOWN because U_H_E() uses one bucket for all values less than 1.
100 if (profile >= 0) {
101 UMA_HISTOGRAM_ENUMERATION("Media.VideoCodecProfile", profile,
102 VIDEO_CODEC_PROFILE_MAX + 1);
104 UMA_HISTOGRAM_COUNTS_10000("Media.VideoCodedWidth", coded_size.width());
105 UmaHistogramAspectRatio("Media.VideoCodedAspectRatio", coded_size);
106 UMA_HISTOGRAM_COUNTS_10000("Media.VideoVisibleWidth", visible_rect.width());
107 UmaHistogramAspectRatio("Media.VideoVisibleAspectRatio", visible_rect);
108 UMA_HISTOGRAM_ENUMERATION("Media.VideoFramePixelFormat", format,
109 PIXEL_FORMAT_MAX + 1);
110 UMA_HISTOGRAM_ENUMERATION("Media.VideoFrameColorSpace", color_space,
111 COLOR_SPACE_MAX + 1);
114 codec_ = codec;
115 profile_ = profile;
116 format_ = format;
117 color_space_ = color_space;
118 coded_size_ = coded_size;
119 visible_rect_ = visible_rect;
120 natural_size_ = natural_size;
121 extra_data_.assign(extra_data, extra_data + extra_data_size);
122 is_encrypted_ = is_encrypted;
125 bool VideoDecoderConfig::IsValidConfig() const {
126 return codec_ != kUnknownVideoCodec &&
127 natural_size_.width() > 0 &&
128 natural_size_.height() > 0 &&
129 VideoFrame::IsValidConfig(format_, VideoFrame::STORAGE_UNOWNED_MEMORY,
130 coded_size_, visible_rect_, natural_size_);
133 bool VideoDecoderConfig::Matches(const VideoDecoderConfig& config) const {
134 return ((codec() == config.codec()) &&
135 (format() == config.format()) &&
136 (profile() == config.profile()) &&
137 (coded_size() == config.coded_size()) &&
138 (visible_rect() == config.visible_rect()) &&
139 (natural_size() == config.natural_size()) &&
140 (extra_data_size() == config.extra_data_size()) &&
141 (!extra_data() || !memcmp(extra_data(), config.extra_data(),
142 extra_data_size())) &&
143 (is_encrypted() == config.is_encrypted()));
146 std::string VideoDecoderConfig::AsHumanReadableString() const {
147 std::ostringstream s;
148 s << "codec: " << GetHumanReadableCodecName()
149 << " format: " << format()
150 << " profile: " << profile()
151 << " coded size: [" << coded_size().width()
152 << "," << coded_size().height() << "]"
153 << " visible rect: [" << visible_rect().x()
154 << "," << visible_rect().y()
155 << "," << visible_rect().width()
156 << "," << visible_rect().height() << "]"
157 << " natural size: [" << natural_size().width()
158 << "," << natural_size().height() << "]"
159 << " has extra data? " << (extra_data() ? "true" : "false")
160 << " encrypted? " << (is_encrypted() ? "true" : "false");
161 return s.str();
164 // The names come from src/third_party/ffmpeg/libavcodec/codec_desc.c
165 std::string VideoDecoderConfig::GetHumanReadableCodecName() const {
166 switch (codec()) {
167 case kUnknownVideoCodec:
168 return "unknown";
169 case kCodecH264:
170 return "h264";
171 case kCodecVC1:
172 return "vc1";
173 case kCodecMPEG2:
174 return "mpeg2video";
175 case kCodecMPEG4:
176 return "mpeg4";
177 case kCodecTheora:
178 return "theora";
179 case kCodecVP8:
180 return "vp8";
181 case kCodecVP9:
182 return "vp9";
184 NOTREACHED();
185 return "";
188 const uint8* VideoDecoderConfig::extra_data() const {
189 if (extra_data_.empty())
190 return NULL;
191 return &extra_data_[0];
194 } // namespace media