- add sources.
[platform/framework/web/crosswalk.git] / src / media / base / video_decoder_config.cc
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
10 namespace media {
11
12 VideoDecoderConfig::VideoDecoderConfig()
13     : codec_(kUnknownVideoCodec),
14       profile_(VIDEO_CODEC_PROFILE_UNKNOWN),
15       format_(VideoFrame::UNKNOWN),
16       is_encrypted_(false) {
17 }
18
19 VideoDecoderConfig::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
32 VideoDecoderConfig::~VideoDecoderConfig() {}
33
34 // Some videos just want to watch the world burn, with a height of 0; cap the
35 // "infinite" aspect ratio resulting.
36 static 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
41 static 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
46 template<class T>  // T has int width() & height() methods.
47 static 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
56 void 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   }
80
81   codec_ = codec;
82   profile_ = profile;
83   format_ = format;
84   coded_size_ = coded_size;
85   visible_rect_ = visible_rect;
86   natural_size_ = natural_size;
87   extra_data_.assign(extra_data, extra_data + extra_data_size);
88   is_encrypted_ = is_encrypted;
89 }
90
91 bool VideoDecoderConfig::IsValidConfig() const {
92   return codec_ != kUnknownVideoCodec &&
93       natural_size_.width() > 0 &&
94       natural_size_.height() > 0 &&
95       VideoFrame::IsValidConfig(format_, coded_size_, visible_rect_,
96           natural_size_);
97 }
98
99 bool VideoDecoderConfig::Matches(const VideoDecoderConfig& config) const {
100   return ((codec() == config.codec()) &&
101           (format() == config.format()) &&
102           (profile() == config.profile()) &&
103           (coded_size() == config.coded_size()) &&
104           (visible_rect() == config.visible_rect()) &&
105           (natural_size() == config.natural_size()) &&
106           (extra_data_size() == config.extra_data_size()) &&
107           (!extra_data() || !memcmp(extra_data(), config.extra_data(),
108                                     extra_data_size())) &&
109           (is_encrypted() == config.is_encrypted()));
110 }
111
112 std::string VideoDecoderConfig::AsHumanReadableString() const {
113   std::ostringstream s;
114   s << "codec: " << codec()
115     << " format: " << format()
116     << " profile: " << profile()
117     << " coded size: [" << coded_size().width()
118     << "," << coded_size().height() << "]"
119     << " visible rect: [" << visible_rect().x()
120     << "," << visible_rect().y()
121     << "," << visible_rect().width()
122     << "," << visible_rect().height() << "]"
123     << " natural size: [" << natural_size().width()
124     << "," << natural_size().height() << "]"
125     << " has extra data? " << (extra_data() ? "true" : "false")
126     << " encrypted? " << (is_encrypted() ? "true" : "false");
127   return s.str();
128 }
129
130 VideoCodec VideoDecoderConfig::codec() const {
131   return codec_;
132 }
133
134 VideoCodecProfile VideoDecoderConfig::profile() const {
135   return profile_;
136 }
137
138 VideoFrame::Format VideoDecoderConfig::format() const {
139   return format_;
140 }
141
142 gfx::Size VideoDecoderConfig::coded_size() const {
143   return coded_size_;
144 }
145
146 gfx::Rect VideoDecoderConfig::visible_rect() const {
147   return visible_rect_;
148 }
149
150 gfx::Size VideoDecoderConfig::natural_size() const {
151   return natural_size_;
152 }
153
154 const uint8* VideoDecoderConfig::extra_data() const {
155   if (extra_data_.empty())
156     return NULL;
157   return &extra_data_[0];
158 }
159
160 size_t VideoDecoderConfig::extra_data_size() const {
161   return extra_data_.size();
162 }
163
164 bool VideoDecoderConfig::is_encrypted() const {
165   return is_encrypted_;
166 }
167
168 }  // namespace media