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.
4
5#include "media/base/video_decoder_config.h"
6
7#include "base/logging.h"
8#include "base/metrics/histogram.h"
9
10namespace media {
11
12VideoDecoderConfig::VideoDecoderConfig()
13    : codec_(kUnknownVideoCodec),
14      profile_(VIDEO_CODEC_PROFILE_UNKNOWN),
15      format_(VideoFrame::UNKNOWN),
16      is_encrypted_(false) {
17}
18
19VideoDecoderConfig::VideoDecoderConfig(VideoCodec codec,
20                                       VideoCodecProfile profile,
21                                       VideoFrame::Format format,
22                                       const gfx::Size& coded_size,
23                                       const gfx::Rect& visible_rect,
24                                       const gfx::Size& natural_size,
25                                       const uint8* extra_data,
26                                       size_t extra_data_size,
27                                       bool is_encrypted) {
28  Initialize(codec, profile, format, coded_size, visible_rect, natural_size,
29             extra_data, extra_data_size, is_encrypted, true);
30}
31
32VideoDecoderConfig::~VideoDecoderConfig() {}
33
34// Some videos just want to watch the world burn, with a height of 0; cap the
35// "infinite" aspect ratio resulting.
36static const int kInfiniteRatio = 99999;
37
38// Common aspect ratios (multiplied by 100 and truncated) used for histogramming
39// video sizes.  These were taken on 20111103 from
40// http://wikipedia.org/wiki/Aspect_ratio_(image)#Previous_and_currently_used_aspect_ratios
41static const int kCommonAspectRatios100[] = {
42  100, 115, 133, 137, 143, 150, 155, 160, 166, 175, 177, 185, 200, 210, 220,
43  221, 235, 237, 240, 255, 259, 266, 276, 293, 400, 1200, kInfiniteRatio,
44};
45
46template<class T>  // T has int width() & height() methods.
47static void UmaHistogramAspectRatio(const char* name, const T& size) {
48  UMA_HISTOGRAM_CUSTOM_ENUMERATION(
49      name,
50      // Intentionally use integer division to truncate the result.
51      size.height() ? (size.width() * 100) / size.height() : kInfiniteRatio,
52      base::CustomHistogram::ArrayToCustomRanges(
53          kCommonAspectRatios100, arraysize(kCommonAspectRatios100)));
54}
55
56void VideoDecoderConfig::Initialize(VideoCodec codec,
57                                    VideoCodecProfile profile,
58                                    VideoFrame::Format format,
59                                    const gfx::Size& coded_size,
60                                    const gfx::Rect& visible_rect,
61                                    const gfx::Size& natural_size,
62                                    const uint8* extra_data,
63                                    size_t extra_data_size,
64                                    bool is_encrypted,
65                                    bool record_stats) {
66  CHECK((extra_data_size != 0) == (extra_data != NULL));
67
68  if (record_stats) {
69    UMA_HISTOGRAM_ENUMERATION("Media.VideoCodec", codec, kVideoCodecMax + 1);
70    // Drop UNKNOWN because U_H_E() uses one bucket for all values less than 1.
71    if (profile >= 0) {
72      UMA_HISTOGRAM_ENUMERATION("Media.VideoCodecProfile", profile,
73                                VIDEO_CODEC_PROFILE_MAX + 1);
74    }
75    UMA_HISTOGRAM_COUNTS_10000("Media.VideoCodedWidth", coded_size.width());
76    UmaHistogramAspectRatio("Media.VideoCodedAspectRatio", coded_size);
77    UMA_HISTOGRAM_COUNTS_10000("Media.VideoVisibleWidth", visible_rect.width());
78    UmaHistogramAspectRatio("Media.VideoVisibleAspectRatio", visible_rect);
79    UMA_HISTOGRAM_ENUMERATION(
80        "Media.VideoPixelFormat", format, VideoFrame::FORMAT_MAX + 1);
81  }
82
83  codec_ = codec;
84  profile_ = profile;
85  format_ = format;
86  coded_size_ = coded_size;
87  visible_rect_ = visible_rect;
88  natural_size_ = natural_size;
89  extra_data_.assign(extra_data, extra_data + extra_data_size);
90  is_encrypted_ = is_encrypted;
91}
92
93bool VideoDecoderConfig::IsValidConfig() const {
94  return codec_ != kUnknownVideoCodec &&
95      natural_size_.width() > 0 &&
96      natural_size_.height() > 0 &&
97      VideoFrame::IsValidConfig(format_, coded_size_, visible_rect_,
98          natural_size_);
99}
100
101bool VideoDecoderConfig::Matches(const VideoDecoderConfig& config) const {
102  return ((codec() == config.codec()) &&
103          (format() == config.format()) &&
104          (profile() == config.profile()) &&
105          (coded_size() == config.coded_size()) &&
106          (visible_rect() == config.visible_rect()) &&
107          (natural_size() == config.natural_size()) &&
108          (extra_data_size() == config.extra_data_size()) &&
109          (!extra_data() || !memcmp(extra_data(), config.extra_data(),
110                                    extra_data_size())) &&
111          (is_encrypted() == config.is_encrypted()));
112}
113
114std::string VideoDecoderConfig::AsHumanReadableString() const {
115  std::ostringstream s;
116  s << "codec: " << codec()
117    << " format: " << format()
118    << " profile: " << profile()
119    << " coded size: [" << coded_size().width()
120    << "," << coded_size().height() << "]"
121    << " visible rect: [" << visible_rect().x()
122    << "," << visible_rect().y()
123    << "," << visible_rect().width()
124    << "," << visible_rect().height() << "]"
125    << " natural size: [" << natural_size().width()
126    << "," << natural_size().height() << "]"
127    << " has extra data? " << (extra_data() ? "true" : "false")
128    << " encrypted? " << (is_encrypted() ? "true" : "false");
129  return s.str();
130}
131
132VideoCodec VideoDecoderConfig::codec() const {
133  return codec_;
134}
135
136VideoCodecProfile VideoDecoderConfig::profile() const {
137  return profile_;
138}
139
140VideoFrame::Format VideoDecoderConfig::format() const {
141  return format_;
142}
143
144gfx::Size VideoDecoderConfig::coded_size() const {
145  return coded_size_;
146}
147
148gfx::Rect VideoDecoderConfig::visible_rect() const {
149  return visible_rect_;
150}
151
152gfx::Size VideoDecoderConfig::natural_size() const {
153  return natural_size_;
154}
155
156const uint8* VideoDecoderConfig::extra_data() const {
157  if (extra_data_.empty())
158    return NULL;
159  return &extra_data_[0];
160}
161
162size_t VideoDecoderConfig::extra_data_size() const {
163  return extra_data_.size();
164}
165
166bool VideoDecoderConfig::is_encrypted() const {
167  return is_encrypted_;
168}
169
170}  // namespace media
171