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"
13 VideoDecoderConfig::VideoDecoderConfig()
14 : codec_(kUnknownVideoCodec
),
15 profile_(VIDEO_CODEC_PROFILE_UNKNOWN
),
16 format_(PIXEL_FORMAT_UNKNOWN
),
17 is_encrypted_(false) {
20 VideoDecoderConfig::VideoDecoderConfig(VideoCodec codec
,
21 VideoCodecProfile profile
,
22 VideoPixelFormat format
,
23 ColorSpace color_space
,
24 const gfx::Size
& coded_size
,
25 const gfx::Rect
& visible_rect
,
26 const gfx::Size
& natural_size
,
27 const uint8
* extra_data
,
28 size_t extra_data_size
,
30 Initialize(codec
, profile
, format
, color_space
, coded_size
, visible_rect
,
31 natural_size
, extra_data
, extra_data_size
, is_encrypted
, true);
34 VideoDecoderConfig::~VideoDecoderConfig() {}
36 // Some videos just want to watch the world burn, with a height of 0; cap the
37 // "infinite" aspect ratio resulting.
38 static const int kInfiniteRatio
= 99999;
40 // Common aspect ratios (multiplied by 100 and truncated) used for histogramming
41 // video sizes. These were taken on 20111103 from
42 // http://wikipedia.org/wiki/Aspect_ratio_(image)#Previous_and_currently_used_aspect_ratios
43 static const int kCommonAspectRatios100
[] = {
44 100, 115, 133, 137, 143, 150, 155, 160, 166, 175, 177, 185, 200, 210, 220,
45 221, 235, 237, 240, 255, 259, 266, 276, 293, 400, 1200, kInfiniteRatio
,
48 template<class T
> // T has int width() & height() methods.
49 static void UmaHistogramAspectRatio(const char* name
, const T
& size
) {
50 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
52 // Intentionally use integer division to truncate the result.
53 size
.height() ? (size
.width() * 100) / size
.height() : kInfiniteRatio
,
54 base::CustomHistogram::ArrayToCustomRanges(
55 kCommonAspectRatios100
, arraysize(kCommonAspectRatios100
)));
58 void VideoDecoderConfig::Initialize(VideoCodec codec
,
59 VideoCodecProfile profile
,
60 VideoPixelFormat format
,
61 ColorSpace color_space
,
62 const gfx::Size
& coded_size
,
63 const gfx::Rect
& visible_rect
,
64 const gfx::Size
& natural_size
,
65 const uint8
* extra_data
,
66 size_t extra_data_size
,
69 CHECK((extra_data_size
!= 0) == (extra_data
!= NULL
));
72 UMA_HISTOGRAM_ENUMERATION("Media.VideoCodec", codec
, kVideoCodecMax
+ 1);
73 // Drop UNKNOWN because U_H_E() uses one bucket for all values less than 1.
75 UMA_HISTOGRAM_ENUMERATION("Media.VideoCodecProfile", profile
,
76 VIDEO_CODEC_PROFILE_MAX
+ 1);
78 UMA_HISTOGRAM_COUNTS_10000("Media.VideoCodedWidth", coded_size
.width());
79 UmaHistogramAspectRatio("Media.VideoCodedAspectRatio", coded_size
);
80 UMA_HISTOGRAM_COUNTS_10000("Media.VideoVisibleWidth", visible_rect
.width());
81 UmaHistogramAspectRatio("Media.VideoVisibleAspectRatio", visible_rect
);
82 UMA_HISTOGRAM_ENUMERATION("Media.VideoFramePixelFormat", format
,
83 PIXEL_FORMAT_MAX
+ 1);
84 UMA_HISTOGRAM_ENUMERATION("Media.VideoFrameColorSpace", color_space
,
91 color_space_
= color_space
;
92 coded_size_
= coded_size
;
93 visible_rect_
= visible_rect
;
94 natural_size_
= natural_size
;
95 extra_data_
.assign(extra_data
, extra_data
+ extra_data_size
);
96 is_encrypted_
= is_encrypted
;
99 bool VideoDecoderConfig::IsValidConfig() const {
100 return codec_
!= kUnknownVideoCodec
&&
101 natural_size_
.width() > 0 &&
102 natural_size_
.height() > 0 &&
103 VideoFrame::IsValidConfig(format_
, VideoFrame::STORAGE_UNOWNED_MEMORY
,
104 coded_size_
, visible_rect_
, natural_size_
);
107 bool VideoDecoderConfig::Matches(const VideoDecoderConfig
& config
) const {
108 return ((codec() == config
.codec()) &&
109 (format() == config
.format()) &&
110 (profile() == config
.profile()) &&
111 (coded_size() == config
.coded_size()) &&
112 (visible_rect() == config
.visible_rect()) &&
113 (natural_size() == config
.natural_size()) &&
114 (extra_data_size() == config
.extra_data_size()) &&
115 (!extra_data() || !memcmp(extra_data(), config
.extra_data(),
116 extra_data_size())) &&
117 (is_encrypted() == config
.is_encrypted()));
120 std::string
VideoDecoderConfig::AsHumanReadableString() const {
121 std::ostringstream s
;
122 s
<< "codec: " << GetHumanReadableCodecName()
123 << " format: " << format()
124 << " profile: " << profile()
125 << " coded size: [" << coded_size().width()
126 << "," << coded_size().height() << "]"
127 << " visible rect: [" << visible_rect().x()
128 << "," << visible_rect().y()
129 << "," << visible_rect().width()
130 << "," << visible_rect().height() << "]"
131 << " natural size: [" << natural_size().width()
132 << "," << natural_size().height() << "]"
133 << " has extra data? " << (extra_data() ? "true" : "false")
134 << " encrypted? " << (is_encrypted() ? "true" : "false");
138 // The names come from src/third_party/ffmpeg/libavcodec/codec_desc.c
139 std::string
VideoDecoderConfig::GetHumanReadableCodecName() const {
141 case kUnknownVideoCodec
:
162 const uint8
* VideoDecoderConfig::extra_data() const {
163 if (extra_data_
.empty())
165 return &extra_data_
[0];