Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromecast / media / cma / base / decoder_config_adapter.cc
blobb75d5f2be86ae56fbb8b8bd5b84e9da03768189a
1 // Copyright 2015 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 "chromecast/media/cma/base/decoder_config_adapter.h"
7 #include "base/logging.h"
8 #include "media/base/channel_layout.h"
10 namespace chromecast {
11 namespace media {
13 namespace {
15 // Converts ::media::AudioCodec to chromecast::media::AudioCodec. Any unknown or
16 // unsupported codec will be converted to chromecast::media::kCodecUnknown.
17 AudioCodec ToAudioCodec(const ::media::AudioCodec audio_codec) {
18 switch (audio_codec) {
19 case ::media::kCodecAAC:
20 return kCodecAAC;
21 case ::media::kCodecMP3:
22 return kCodecMP3;
23 case ::media::kCodecPCM:
24 return kCodecPCM;
25 case ::media::kCodecPCM_S16BE:
26 return kCodecPCM_S16BE;
27 case ::media::kCodecVorbis:
28 return kCodecVorbis;
29 default:
30 LOG(ERROR) << "Unsupported audio codec " << audio_codec;
32 return kAudioCodecUnknown;
35 SampleFormat ToSampleFormat(const ::media::SampleFormat sample_format) {
36 switch (sample_format) {
37 case ::media::kUnknownSampleFormat:
38 return kUnknownSampleFormat;
39 case ::media::kSampleFormatU8:
40 return kSampleFormatU8;
41 case ::media::kSampleFormatS16:
42 return kSampleFormatS16;
43 case ::media::kSampleFormatS32:
44 return kSampleFormatS32;
45 case ::media::kSampleFormatF32:
46 return kSampleFormatF32;
47 case ::media::kSampleFormatPlanarS16:
48 return kSampleFormatPlanarS16;
49 case ::media::kSampleFormatPlanarF32:
50 return kSampleFormatPlanarF32;
51 case ::media::kSampleFormatPlanarS32:
52 return kSampleFormatPlanarS32;
54 NOTREACHED();
55 return kUnknownSampleFormat;
58 // Converts ::media::VideoCodec to chromecast::media::VideoCodec. Any unknown or
59 // unsupported codec will be converted to chromecast::media::kCodecUnknown.
60 VideoCodec ToVideoCodec(const ::media::VideoCodec video_codec) {
61 switch (video_codec) {
62 case ::media::kCodecH264:
63 return kCodecH264;
64 case ::media::kCodecVP8:
65 return kCodecVP8;
66 case ::media::kCodecVP9:
67 return kCodecVP9;
68 default:
69 LOG(ERROR) << "Unsupported video codec " << video_codec;
71 return kVideoCodecUnknown;
74 // Converts ::media::VideoCodecProfile to chromecast::media::VideoProfile.
75 VideoProfile ToVideoProfile(const ::media::VideoCodecProfile codec_profile) {
76 switch(codec_profile) {
77 case ::media::H264PROFILE_BASELINE:
78 return kH264Baseline;
79 case ::media::H264PROFILE_MAIN:
80 return kH264Main;
81 case ::media::H264PROFILE_EXTENDED:
82 return kH264Extended;
83 case ::media::H264PROFILE_HIGH:
84 return kH264High;
85 case ::media::H264PROFILE_HIGH10PROFILE:
86 return kH264High10;
87 case ::media::H264PROFILE_HIGH422PROFILE:
88 return kH264High422;
89 case ::media::H264PROFILE_HIGH444PREDICTIVEPROFILE:
90 return kH264High444Predictive;
91 case ::media::H264PROFILE_SCALABLEBASELINE:
92 return kH264ScalableBaseline;
93 case ::media::H264PROFILE_SCALABLEHIGH:
94 return kH264ScalableHigh;
95 case ::media::H264PROFILE_STEREOHIGH:
96 return kH264Stereohigh;
97 case ::media::H264PROFILE_MULTIVIEWHIGH:
98 return kH264MultiviewHigh;
99 case ::media::VP8PROFILE_ANY:
100 return kVP8ProfileAny;
101 case ::media::VP9PROFILE_ANY:
102 return kVP9ProfileAny;
103 default:
104 LOG(INFO) << "Unsupported video codec profile " << codec_profile;
106 return kVideoProfileUnknown;
109 ::media::ChannelLayout ToMediaChannelLayout(int channel_number) {
110 switch (channel_number) {
111 case 1:
112 return ::media::ChannelLayout::CHANNEL_LAYOUT_MONO;
113 case 2:
114 return ::media::ChannelLayout::CHANNEL_LAYOUT_STEREO;
115 default:
116 return ::media::ChannelLayout::CHANNEL_LAYOUT_UNSUPPORTED;
120 ::media::SampleFormat ToMediaSampleFormat(const SampleFormat sample_format) {
121 switch (sample_format) {
122 case kUnknownSampleFormat:
123 return ::media::kUnknownSampleFormat;
124 case kSampleFormatU8:
125 return ::media::kSampleFormatU8;
126 case kSampleFormatS16:
127 return ::media::kSampleFormatS16;
128 case kSampleFormatS32:
129 return ::media::kSampleFormatS32;
130 case kSampleFormatF32:
131 return ::media::kSampleFormatF32;
132 case kSampleFormatPlanarS16:
133 return ::media::kSampleFormatPlanarS16;
134 case kSampleFormatPlanarF32:
135 return ::media::kSampleFormatPlanarF32;
136 case kSampleFormatPlanarS32:
137 return ::media::kSampleFormatPlanarS32;
138 default:
139 NOTREACHED();
140 return ::media::kUnknownSampleFormat;
144 ::media::AudioCodec ToMediaAudioCodec(
145 const chromecast::media::AudioCodec codec) {
146 switch (codec) {
147 case kAudioCodecUnknown:
148 return ::media::kUnknownAudioCodec;
149 case kCodecAAC:
150 return ::media::kCodecAAC;
151 case kCodecMP3:
152 return ::media::kCodecMP3;
153 case kCodecPCM:
154 return ::media::kCodecPCM;
155 case kCodecPCM_S16BE:
156 return ::media::kCodecPCM_S16BE;
157 case kCodecVorbis:
158 return ::media::kCodecVorbis;
159 case kCodecOpus:
160 return ::media::kCodecOpus;
161 default:
162 return ::media::kUnknownAudioCodec;
166 } // namespace
168 // static
169 AudioConfig DecoderConfigAdapter::ToCastAudioConfig(
170 StreamId id,
171 const ::media::AudioDecoderConfig& config) {
172 AudioConfig audio_config;
173 if (!config.IsValidConfig()) {
174 return audio_config;
177 audio_config.id = id;
178 audio_config.codec = ToAudioCodec(config.codec());
179 audio_config.sample_format = ToSampleFormat(config.sample_format());
180 audio_config.bytes_per_channel = config.bytes_per_channel();
181 audio_config.channel_number =
182 ::media::ChannelLayoutToChannelCount(config.channel_layout()),
183 audio_config.samples_per_second = config.samples_per_second();
184 audio_config.extra_data =
185 (config.extra_data_size() > 0) ? config.extra_data() : nullptr;
186 audio_config.extra_data_size = config.extra_data_size();
187 audio_config.is_encrypted = config.is_encrypted();
188 return audio_config;
191 // static
192 ::media::AudioDecoderConfig DecoderConfigAdapter::ToMediaAudioDecoderConfig(
193 const AudioConfig& config) {
194 return ::media::AudioDecoderConfig(
195 ToMediaAudioCodec(config.codec),
196 ToMediaSampleFormat(config.sample_format),
197 ToMediaChannelLayout(config.channel_number), config.samples_per_second,
198 config.extra_data, config.extra_data_size, config.is_encrypted);
201 // static
202 VideoConfig DecoderConfigAdapter::ToCastVideoConfig(
203 StreamId id,
204 const ::media::VideoDecoderConfig& config) {
205 VideoConfig video_config;
206 if (!config.IsValidConfig()) {
207 return video_config;
210 video_config.id = id;
211 video_config.codec = ToVideoCodec(config.codec());
212 video_config.profile = ToVideoProfile(config.profile());
213 video_config.extra_data = (config.extra_data_size() > 0) ?
214 config.extra_data() : nullptr;
215 video_config.extra_data_size = config.extra_data_size();
216 video_config.is_encrypted = config.is_encrypted();
217 return video_config;
220 } // namespace media
221 } // namespace chromecast