summaryrefslogtreecommitdiffstats
path: root/chromecast/media/cma/base/decoder_config_adapter.cc
blob: 24e8b41e8fcc11607e5488482c24f5a6feb26387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromecast/media/cma/base/decoder_config_adapter.h"

#include "base/logging.h"
#include "media/base/channel_layout.h"

namespace chromecast {
namespace media {

namespace {

// Converts ::media::AudioCodec to chromecast::media::AudioCodec. Any unknown or
// unsupported codec will be converted to chromecast::media::kCodecUnknown.
AudioCodec ToAudioCodec(const ::media::AudioCodec audio_codec) {
  switch (audio_codec) {
    case ::media::kCodecAAC:
      return kCodecAAC;
    case ::media::kCodecMP3:
      return kCodecMP3;
    case ::media::kCodecPCM:
      return kCodecPCM;
    case ::media::kCodecPCM_S16BE:
      return kCodecPCM_S16BE;
    case ::media::kCodecVorbis:
      return kCodecVorbis;
    case ::media::kCodecOpus:
      return kCodecOpus;
    case ::media::kCodecFLAC:
      return kCodecFLAC;
    case ::media::kCodecEAC3:
      return kCodecEAC3;
    case ::media::kCodecAC3:
      return kCodecAC3;
    default:
      LOG(ERROR) << "Unsupported audio codec " << audio_codec;
  }
  return kAudioCodecUnknown;
}

SampleFormat ToSampleFormat(const ::media::SampleFormat sample_format) {
  switch (sample_format) {
    case ::media::kUnknownSampleFormat:
      return kUnknownSampleFormat;
    case ::media::kSampleFormatU8:
      return kSampleFormatU8;
    case ::media::kSampleFormatS16:
      return kSampleFormatS16;
    case ::media::kSampleFormatS24:
      return kSampleFormatS24;
    case ::media::kSampleFormatS32:
      return kSampleFormatS32;
    case ::media::kSampleFormatF32:
      return kSampleFormatF32;
    case ::media::kSampleFormatPlanarS16:
      return kSampleFormatPlanarS16;
    case ::media::kSampleFormatPlanarF32:
      return kSampleFormatPlanarF32;
    case ::media::kSampleFormatPlanarS32:
      return kSampleFormatPlanarS32;
  }
  NOTREACHED();
  return kUnknownSampleFormat;
}

// Converts ::media::VideoCodec to chromecast::media::VideoCodec. Any unknown or
// unsupported codec will be converted to chromecast::media::kCodecUnknown.
VideoCodec ToVideoCodec(const ::media::VideoCodec video_codec) {
  switch (video_codec) {
    case ::media::kCodecH264:
      return kCodecH264;
    case ::media::kCodecVP8:
      return kCodecVP8;
    case ::media::kCodecVP9:
      return kCodecVP9;
    case ::media::kCodecHEVC:
      return kCodecHEVC;
    default:
      LOG(ERROR) << "Unsupported video codec " << video_codec;
  }
  return kVideoCodecUnknown;
}

// Converts ::media::VideoCodecProfile to chromecast::media::VideoProfile.
VideoProfile ToVideoProfile(const ::media::VideoCodecProfile codec_profile) {
  switch (codec_profile) {
    case ::media::H264PROFILE_BASELINE:
      return kH264Baseline;
    case ::media::H264PROFILE_MAIN:
      return kH264Main;
    case ::media::H264PROFILE_EXTENDED:
      return kH264Extended;
    case ::media::H264PROFILE_HIGH:
      return kH264High;
    case ::media::H264PROFILE_HIGH10PROFILE:
      return kH264High10;
    case ::media::H264PROFILE_HIGH422PROFILE:
      return kH264High422;
    case ::media::H264PROFILE_HIGH444PREDICTIVEPROFILE:
      return kH264High444Predictive;
    case ::media::H264PROFILE_SCALABLEBASELINE:
      return kH264ScalableBaseline;
    case ::media::H264PROFILE_SCALABLEHIGH:
      return kH264ScalableHigh;
    case ::media::H264PROFILE_STEREOHIGH:
      return kH264Stereohigh;
    case ::media::H264PROFILE_MULTIVIEWHIGH:
      return kH264MultiviewHigh;
    case ::media::VP8PROFILE_ANY:
      return kVP8ProfileAny;
    case ::media::VP9PROFILE_ANY:
      return kVP9ProfileAny;
    default:
      LOG(INFO) << "Unsupported video codec profile " << codec_profile;
  }
  return kVideoProfileUnknown;
}

::media::ChannelLayout ToMediaChannelLayout(int channel_number) {
  switch (channel_number) {
    case 1:
      return ::media::ChannelLayout::CHANNEL_LAYOUT_MONO;
    case 2:
      return ::media::ChannelLayout::CHANNEL_LAYOUT_STEREO;
    default:
      return ::media::ChannelLayout::CHANNEL_LAYOUT_UNSUPPORTED;
  }
}

::media::SampleFormat ToMediaSampleFormat(const SampleFormat sample_format) {
  switch (sample_format) {
    case kUnknownSampleFormat:
      return ::media::kUnknownSampleFormat;
    case kSampleFormatU8:
      return ::media::kSampleFormatU8;
    case kSampleFormatS16:
      return ::media::kSampleFormatS16;
    case kSampleFormatS24:
      return ::media::kSampleFormatS24;
    case kSampleFormatS32:
      return ::media::kSampleFormatS32;
    case kSampleFormatF32:
      return ::media::kSampleFormatF32;
    case kSampleFormatPlanarS16:
      return ::media::kSampleFormatPlanarS16;
    case kSampleFormatPlanarF32:
      return ::media::kSampleFormatPlanarF32;
    case kSampleFormatPlanarS32:
      return ::media::kSampleFormatPlanarS32;
    default:
      NOTREACHED();
      return ::media::kUnknownSampleFormat;
  }
}

::media::AudioCodec ToMediaAudioCodec(
    const chromecast::media::AudioCodec codec) {
  switch (codec) {
    case kAudioCodecUnknown:
      return ::media::kUnknownAudioCodec;
    case kCodecAAC:
      return ::media::kCodecAAC;
    case kCodecMP3:
      return ::media::kCodecMP3;
    case kCodecPCM:
      return ::media::kCodecPCM;
    case kCodecPCM_S16BE:
      return ::media::kCodecPCM_S16BE;
    case kCodecVorbis:
      return ::media::kCodecVorbis;
    case kCodecOpus:
      return ::media::kCodecOpus;
    case kCodecFLAC:
      return ::media::kCodecFLAC;
    case kCodecEAC3:
      return ::media::kCodecEAC3;
    case kCodecAC3:
      return ::media::kCodecAC3;
    default:
      return ::media::kUnknownAudioCodec;
  }
}

::media::EncryptionScheme::CipherMode ToMediaCipherMode(
    EncryptionScheme::CipherMode mode) {
  switch (mode) {
    case EncryptionScheme::CIPHER_MODE_UNENCRYPTED:
      return ::media::EncryptionScheme::CIPHER_MODE_UNENCRYPTED;
    case EncryptionScheme::CIPHER_MODE_AES_CTR:
      return ::media::EncryptionScheme::CIPHER_MODE_AES_CTR;
    case EncryptionScheme::CIPHER_MODE_AES_CBC:
      return ::media::EncryptionScheme::CIPHER_MODE_AES_CBC;
    default:
      NOTREACHED();
      return ::media::EncryptionScheme::CIPHER_MODE_UNENCRYPTED;
  }
}

EncryptionScheme::CipherMode ToCipherMode(
    ::media::EncryptionScheme::CipherMode mode) {
  switch (mode) {
    case ::media::EncryptionScheme::CIPHER_MODE_UNENCRYPTED:
      return EncryptionScheme::CIPHER_MODE_UNENCRYPTED;
    case ::media::EncryptionScheme::CIPHER_MODE_AES_CTR:
      return EncryptionScheme::CIPHER_MODE_AES_CTR;
    case ::media::EncryptionScheme::CIPHER_MODE_AES_CBC:
      return EncryptionScheme::CIPHER_MODE_AES_CBC;
    default:
      NOTREACHED();
      return EncryptionScheme::CIPHER_MODE_UNENCRYPTED;
  }
}

EncryptionScheme::Pattern ToPatternSpec(
    const ::media::EncryptionScheme::Pattern& pattern) {
  return EncryptionScheme::Pattern(
      pattern.encrypt_blocks(), pattern.skip_blocks());
}

::media::EncryptionScheme::Pattern ToMediaPatternSpec(
    const EncryptionScheme::Pattern& pattern) {
  return ::media::EncryptionScheme::Pattern(
      pattern.encrypt_blocks, pattern.skip_blocks);
}

EncryptionScheme ToEncryptionScheme(
    const ::media::EncryptionScheme& scheme) {
  return EncryptionScheme(
    ToCipherMode(scheme.mode()),
    ToPatternSpec(scheme.pattern()));
}

::media::EncryptionScheme ToMediaEncryptionScheme(
    const EncryptionScheme& scheme) {
  return ::media::EncryptionScheme(
    ToMediaCipherMode(scheme.mode),
    ToMediaPatternSpec(scheme.pattern));
}

}  // namespace

// static
AudioConfig DecoderConfigAdapter::ToCastAudioConfig(
    StreamId id,
    const ::media::AudioDecoderConfig& config) {
  AudioConfig audio_config;
  if (!config.IsValidConfig())
    return audio_config;

  audio_config.id = id;
  audio_config.codec = ToAudioCodec(config.codec());
  audio_config.sample_format = ToSampleFormat(config.sample_format());
  audio_config.bytes_per_channel = config.bytes_per_channel();
  audio_config.channel_number =
      ::media::ChannelLayoutToChannelCount(config.channel_layout()),
  audio_config.samples_per_second = config.samples_per_second();
  audio_config.extra_data = config.extra_data();
  audio_config.encryption_scheme = ToEncryptionScheme(
      config.encryption_scheme());
  return audio_config;
}

// static
::media::AudioDecoderConfig DecoderConfigAdapter::ToMediaAudioDecoderConfig(
    const AudioConfig& config) {
  return ::media::AudioDecoderConfig(
      ToMediaAudioCodec(config.codec),
      ToMediaSampleFormat(config.sample_format),
      ToMediaChannelLayout(config.channel_number), config.samples_per_second,
      config.extra_data,
      ToMediaEncryptionScheme(config.encryption_scheme));
}

// static
VideoConfig DecoderConfigAdapter::ToCastVideoConfig(
    StreamId id,
    const ::media::VideoDecoderConfig& config) {
  VideoConfig video_config;
  if (!config.IsValidConfig()) {
    return video_config;
  }

  video_config.id = id;
  video_config.codec = ToVideoCodec(config.codec());
  video_config.profile = ToVideoProfile(config.profile());
  video_config.extra_data = config.extra_data();
  video_config.encryption_scheme = ToEncryptionScheme(
      config.encryption_scheme());
  return video_config;
}

}  // namespace media
}  // namespace chromecast