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
{
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
:
21 case ::media::kCodecMP3
:
23 case ::media::kCodecPCM
:
25 case ::media::kCodecPCM_S16BE
:
26 return kCodecPCM_S16BE
;
27 case ::media::kCodecVorbis
:
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
;
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
:
64 case ::media::kCodecVP8
:
66 case ::media::kCodecVP9
:
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
:
79 case ::media::H264PROFILE_MAIN
:
81 case ::media::H264PROFILE_EXTENDED
:
83 case ::media::H264PROFILE_HIGH
:
85 case ::media::H264PROFILE_HIGH10PROFILE
:
87 case ::media::H264PROFILE_HIGH422PROFILE
:
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
;
104 LOG(INFO
) << "Unsupported video codec profile " << codec_profile
;
106 return kVideoProfileUnknown
;
109 ::media::ChannelLayout
ToMediaChannelLayout(int channel_number
) {
110 switch (channel_number
) {
112 return ::media::ChannelLayout::CHANNEL_LAYOUT_MONO
;
114 return ::media::ChannelLayout::CHANNEL_LAYOUT_STEREO
;
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
;
140 return ::media::kUnknownSampleFormat
;
144 ::media::AudioCodec
ToMediaAudioCodec(
145 const chromecast::media::AudioCodec codec
) {
147 case kAudioCodecUnknown
:
148 return ::media::kUnknownAudioCodec
;
150 return ::media::kCodecAAC
;
152 return ::media::kCodecMP3
;
154 return ::media::kCodecPCM
;
155 case kCodecPCM_S16BE
:
156 return ::media::kCodecPCM_S16BE
;
158 return ::media::kCodecVorbis
;
160 return ::media::kCodecOpus
;
162 return ::media::kUnknownAudioCodec
;
169 AudioConfig
DecoderConfigAdapter::ToCastAudioConfig(
171 const ::media::AudioDecoderConfig
& config
) {
172 AudioConfig audio_config
;
173 if (!config
.IsValidConfig()) {
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();
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
);
202 VideoConfig
DecoderConfigAdapter::ToCastVideoConfig(
204 const ::media::VideoDecoderConfig
& config
) {
205 VideoConfig video_config
;
206 if (!config
.IsValidConfig()) {
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();
221 } // namespace chromecast