OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/ffmpeg/ffmpeg_common.h" | 5 #include "media/ffmpeg/ffmpeg_common.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "media/base/decoder_buffer.h" | 9 #include "media/base/decoder_buffer.h" |
10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 switch (codec_id) { | 61 switch (codec_id) { |
62 case CODEC_ID_AAC: | 62 case CODEC_ID_AAC: |
63 return kCodecAAC; | 63 return kCodecAAC; |
64 case CODEC_ID_MP3: | 64 case CODEC_ID_MP3: |
65 return kCodecMP3; | 65 return kCodecMP3; |
66 case CODEC_ID_VORBIS: | 66 case CODEC_ID_VORBIS: |
67 return kCodecVorbis; | 67 return kCodecVorbis; |
68 case CODEC_ID_PCM_U8: | 68 case CODEC_ID_PCM_U8: |
69 case CODEC_ID_PCM_S16LE: | 69 case CODEC_ID_PCM_S16LE: |
70 case CODEC_ID_PCM_S24LE: | 70 case CODEC_ID_PCM_S24LE: |
| 71 case CODEC_ID_PCM_F32LE: |
71 return kCodecPCM; | 72 return kCodecPCM; |
72 case CODEC_ID_PCM_S16BE: | 73 case CODEC_ID_PCM_S16BE: |
73 return kCodecPCM_S16BE; | 74 return kCodecPCM_S16BE; |
74 case CODEC_ID_PCM_S24BE: | 75 case CODEC_ID_PCM_S24BE: |
75 return kCodecPCM_S24BE; | 76 return kCodecPCM_S24BE; |
76 case CODEC_ID_FLAC: | 77 case CODEC_ID_FLAC: |
77 return kCodecFLAC; | 78 return kCodecFLAC; |
78 case CODEC_ID_AMR_NB: | 79 case CODEC_ID_AMR_NB: |
79 return kCodecAMR_NB; | 80 return kCodecAMR_NB; |
80 case CODEC_ID_AMR_WB: | 81 case CODEC_ID_AMR_WB: |
81 return kCodecAMR_WB; | 82 return kCodecAMR_WB; |
82 case CODEC_ID_GSM_MS: | 83 case CODEC_ID_GSM_MS: |
83 return kCodecGSM_MS; | 84 return kCodecGSM_MS; |
84 case CODEC_ID_PCM_MULAW: | 85 case CODEC_ID_PCM_MULAW: |
85 return kCodecPCM_MULAW; | 86 return kCodecPCM_MULAW; |
86 default: | 87 default: |
87 DVLOG(1) << "Unknown audio CodecID: " << codec_id; | 88 DVLOG(1) << "Unknown audio CodecID: " << codec_id; |
88 } | 89 } |
89 return kUnknownAudioCodec; | 90 return kUnknownAudioCodec; |
90 } | 91 } |
91 | 92 |
92 static CodecID AudioCodecToCodecID(AudioCodec audio_codec, | 93 static CodecID AudioCodecToCodecID(AudioCodec audio_codec, |
93 int bits_per_channel) { | 94 SampleFormat sample_format) { |
94 switch (audio_codec) { | 95 switch (audio_codec) { |
95 case kCodecAAC: | 96 case kCodecAAC: |
96 return CODEC_ID_AAC; | 97 return CODEC_ID_AAC; |
97 case kCodecMP3: | 98 case kCodecMP3: |
98 return CODEC_ID_MP3; | 99 return CODEC_ID_MP3; |
99 case kCodecPCM: | 100 case kCodecPCM: |
100 switch (bits_per_channel) { | 101 switch (sample_format) { |
101 case 8: | 102 case kSampleFormatU8: |
102 return CODEC_ID_PCM_U8; | 103 return CODEC_ID_PCM_U8; |
103 case 16: | 104 case kSampleFormatS16: |
104 return CODEC_ID_PCM_S16LE; | 105 return CODEC_ID_PCM_S16LE; |
105 case 32: | 106 case kSampleFormatS32: |
106 return CODEC_ID_PCM_S24LE; | 107 return CODEC_ID_PCM_S24LE; |
| 108 case kSampleFormatF32: |
| 109 return CODEC_ID_PCM_F32LE; |
107 default: | 110 default: |
108 DVLOG(1) << "Unsupported bits per channel: " << bits_per_channel; | 111 DVLOG(1) << "Unsupported sample format: " << sample_format; |
109 } | 112 } |
110 break; | 113 break; |
111 case kCodecPCM_S16BE: | 114 case kCodecPCM_S16BE: |
112 return CODEC_ID_PCM_S16BE; | 115 return CODEC_ID_PCM_S16BE; |
113 case kCodecPCM_S24BE: | 116 case kCodecPCM_S24BE: |
114 return CODEC_ID_PCM_S24BE; | 117 return CODEC_ID_PCM_S24BE; |
115 case kCodecVorbis: | 118 case kCodecVorbis: |
116 return CODEC_ID_VORBIS; | 119 return CODEC_ID_VORBIS; |
117 case kCodecFLAC: | 120 case kCodecFLAC: |
118 return CODEC_ID_FLAC; | 121 return CODEC_ID_FLAC; |
119 case kCodecAMR_NB: | 122 case kCodecAMR_NB: |
120 return CODEC_ID_AMR_NB; | 123 return CODEC_ID_AMR_NB; |
121 case kCodecAMR_WB: | 124 case kCodecAMR_WB: |
122 return CODEC_ID_AMR_WB; | 125 return CODEC_ID_AMR_WB; |
123 case kCodecGSM_MS: | 126 case kCodecGSM_MS: |
124 return CODEC_ID_GSM_MS; | 127 return CODEC_ID_GSM_MS; |
125 case kCodecPCM_MULAW: | 128 case kCodecPCM_MULAW: |
126 return CODEC_ID_PCM_MULAW; | 129 return CODEC_ID_PCM_MULAW; |
127 default: | 130 default: |
128 DVLOG(1) << "Unknown AudioCodec: " << audio_codec; | 131 DVLOG(1) << "Unknown AudioCodec: " << audio_codec; |
129 } | 132 } |
130 return CODEC_ID_NONE; | 133 return CODEC_ID_NONE; |
131 } | 134 } |
132 | 135 |
133 VideoCodec CodecIDToVideoCodec(CodecID codec_id) { | 136 VideoCodec CodecIDToVideoCodec(CodecID codec_id) { |
134 switch (codec_id) { | 137 switch (codec_id) { |
135 case CODEC_ID_VC1: | |
136 return kCodecVC1; | |
137 case CODEC_ID_H264: | 138 case CODEC_ID_H264: |
138 return kCodecH264; | 139 return kCodecH264; |
139 case CODEC_ID_THEORA: | 140 case CODEC_ID_THEORA: |
140 return kCodecTheora; | 141 return kCodecTheora; |
141 case CODEC_ID_MPEG2VIDEO: | |
142 return kCodecMPEG2; | |
143 case CODEC_ID_MPEG4: | 142 case CODEC_ID_MPEG4: |
144 return kCodecMPEG4; | 143 return kCodecMPEG4; |
145 case CODEC_ID_VP8: | 144 case CODEC_ID_VP8: |
146 return kCodecVP8; | 145 return kCodecVP8; |
147 default: | 146 default: |
148 DVLOG(1) << "Unknown video CodecID: " << codec_id; | 147 DVLOG(1) << "Unknown video CodecID: " << codec_id; |
149 } | 148 } |
150 return kUnknownVideoCodec; | 149 return kUnknownVideoCodec; |
151 } | 150 } |
152 | 151 |
153 static CodecID VideoCodecToCodecID(VideoCodec video_codec) { | 152 static CodecID VideoCodecToCodecID(VideoCodec video_codec) { |
154 switch (video_codec) { | 153 switch (video_codec) { |
155 case kCodecVC1: | |
156 return CODEC_ID_VC1; | |
157 case kCodecH264: | 154 case kCodecH264: |
158 return CODEC_ID_H264; | 155 return CODEC_ID_H264; |
159 case kCodecTheora: | 156 case kCodecTheora: |
160 return CODEC_ID_THEORA; | 157 return CODEC_ID_THEORA; |
161 case kCodecMPEG2: | |
162 return CODEC_ID_MPEG2VIDEO; | |
163 case kCodecMPEG4: | 158 case kCodecMPEG4: |
164 return CODEC_ID_MPEG4; | 159 return CODEC_ID_MPEG4; |
165 case kCodecVP8: | 160 case kCodecVP8: |
166 return CODEC_ID_VP8; | 161 return CODEC_ID_VP8; |
167 default: | 162 default: |
168 DVLOG(1) << "Unknown VideoCodec: " << video_codec; | 163 DVLOG(1) << "Unknown VideoCodec: " << video_codec; |
169 } | 164 } |
170 return CODEC_ID_NONE; | 165 return CODEC_ID_NONE; |
171 } | 166 } |
172 | 167 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 case H264PROFILE_HIGH422PROFILE: | 206 case H264PROFILE_HIGH422PROFILE: |
212 return FF_PROFILE_H264_HIGH_422; | 207 return FF_PROFILE_H264_HIGH_422; |
213 case H264PROFILE_HIGH444PREDICTIVEPROFILE: | 208 case H264PROFILE_HIGH444PREDICTIVEPROFILE: |
214 return FF_PROFILE_H264_HIGH_444_PREDICTIVE; | 209 return FF_PROFILE_H264_HIGH_444_PREDICTIVE; |
215 default: | 210 default: |
216 DVLOG(1) << "Unknown VideoCodecProfile: " << profile; | 211 DVLOG(1) << "Unknown VideoCodecProfile: " << profile; |
217 } | 212 } |
218 return FF_PROFILE_UNKNOWN; | 213 return FF_PROFILE_UNKNOWN; |
219 } | 214 } |
220 | 215 |
| 216 static SampleFormat AVSampleFormatToSampleFormat(AVSampleFormat sample_format) { |
| 217 switch (sample_format) { |
| 218 case AV_SAMPLE_FMT_U8: |
| 219 return kSampleFormatU8; |
| 220 case AV_SAMPLE_FMT_S16: |
| 221 return kSampleFormatS16; |
| 222 case AV_SAMPLE_FMT_S32: |
| 223 return kSampleFormatS32; |
| 224 case AV_SAMPLE_FMT_FLT: |
| 225 return kSampleFormatF32; |
| 226 case AV_SAMPLE_FMT_FLTP: |
| 227 return kSampleFormatPlanarF32; |
| 228 default: |
| 229 DVLOG(1) << "Unknown AVSampleFormat: " << sample_format; |
| 230 } |
| 231 return kUnknownSampleFormat; |
| 232 } |
| 233 |
| 234 static AVSampleFormat SampleFormatToAVSampleFormat(SampleFormat sample_format) { |
| 235 switch (sample_format) { |
| 236 case kSampleFormatU8: |
| 237 return AV_SAMPLE_FMT_U8; |
| 238 case kSampleFormatS16: |
| 239 return AV_SAMPLE_FMT_S16; |
| 240 case kSampleFormatS32: |
| 241 return AV_SAMPLE_FMT_S32; |
| 242 case kSampleFormatF32: |
| 243 return AV_SAMPLE_FMT_FLT; |
| 244 case kSampleFormatPlanarF32: |
| 245 return AV_SAMPLE_FMT_FLTP; |
| 246 default: |
| 247 DVLOG(1) << "Unknown SampleFormat: " << sample_format; |
| 248 } |
| 249 return AV_SAMPLE_FMT_NONE; |
| 250 } |
| 251 |
221 void AVCodecContextToAudioDecoderConfig( | 252 void AVCodecContextToAudioDecoderConfig( |
222 const AVCodecContext* codec_context, | 253 const AVCodecContext* codec_context, |
223 AudioDecoderConfig* config) { | 254 AudioDecoderConfig* config) { |
224 DCHECK_EQ(codec_context->codec_type, AVMEDIA_TYPE_AUDIO); | 255 DCHECK_EQ(codec_context->codec_type, AVMEDIA_TYPE_AUDIO); |
225 | 256 |
226 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id); | 257 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout( |
227 int bytes_per_channel = av_get_bytes_per_sample(codec_context->sample_fmt); | 258 codec_context->channel_layout, codec_context->channels); |
228 ChannelLayout channel_layout = | 259 config->Initialize(CodecIDToAudioCodec(codec_context->codec_id), |
229 ChannelLayoutToChromeChannelLayout(codec_context->channel_layout, | 260 AVSampleFormatToSampleFormat(codec_context->sample_fmt), |
230 codec_context->channels); | |
231 int samples_per_second = codec_context->sample_rate; | |
232 | |
233 config->Initialize(codec, | |
234 bytes_per_channel << 3, | |
235 channel_layout, | 261 channel_layout, |
236 samples_per_second, | 262 codec_context->sample_rate, |
237 codec_context->extradata, | 263 codec_context->extradata, |
238 codec_context->extradata_size, | 264 codec_context->extradata_size, |
239 false, // Not encrypted. | 265 false, // Not encrypted. |
240 true); | 266 true); |
| 267 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, |
| 268 config->bits_per_channel()); |
241 } | 269 } |
242 | 270 |
243 void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config, | 271 void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config, |
244 AVCodecContext* codec_context) { | 272 AVCodecContext* codec_context) { |
245 codec_context->codec_type = AVMEDIA_TYPE_AUDIO; | 273 codec_context->codec_type = AVMEDIA_TYPE_AUDIO; |
246 codec_context->codec_id = AudioCodecToCodecID(config.codec(), | 274 codec_context->codec_id = AudioCodecToCodecID(config.codec(), |
247 config.bits_per_channel()); | 275 config.sample_format()); |
248 | 276 codec_context->sample_fmt = SampleFormatToAVSampleFormat( |
249 switch (config.bits_per_channel()) { | 277 config.sample_format()); |
250 case 8: | |
251 codec_context->sample_fmt = AV_SAMPLE_FMT_U8; | |
252 break; | |
253 case 16: | |
254 codec_context->sample_fmt = AV_SAMPLE_FMT_S16; | |
255 break; | |
256 case 32: | |
257 codec_context->sample_fmt = AV_SAMPLE_FMT_S32; | |
258 break; | |
259 default: | |
260 DVLOG(1) << "Unsupported bits per channel: " << config.bits_per_channel(); | |
261 codec_context->sample_fmt = AV_SAMPLE_FMT_NONE; | |
262 } | |
263 | 278 |
264 // TODO(scherkus): should we set |channel_layout|? I'm not sure if FFmpeg uses | 279 // TODO(scherkus): should we set |channel_layout|? I'm not sure if FFmpeg uses |
265 // said information to decode. | 280 // said information to decode. |
266 codec_context->channels = | 281 codec_context->channels = |
267 ChannelLayoutToChannelCount(config.channel_layout()); | 282 ChannelLayoutToChannelCount(config.channel_layout()); |
268 codec_context->sample_rate = config.samples_per_second(); | 283 codec_context->sample_rate = config.samples_per_second(); |
269 | 284 |
270 if (config.extra_data()) { | 285 if (config.extra_data()) { |
271 codec_context->extradata_size = config.extra_data_size(); | 286 codec_context->extradata_size = config.extra_data_size(); |
272 codec_context->extradata = reinterpret_cast<uint8_t*>( | 287 codec_context->extradata = reinterpret_cast<uint8_t*>( |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 return PIX_FMT_YUV422P; | 436 return PIX_FMT_YUV422P; |
422 case VideoFrame::YV12: | 437 case VideoFrame::YV12: |
423 return PIX_FMT_YUV420P; | 438 return PIX_FMT_YUV420P; |
424 default: | 439 default: |
425 DVLOG(1) << "Unsupported VideoFrame::Format: " << video_format; | 440 DVLOG(1) << "Unsupported VideoFrame::Format: " << video_format; |
426 } | 441 } |
427 return PIX_FMT_NONE; | 442 return PIX_FMT_NONE; |
428 } | 443 } |
429 | 444 |
430 } // namespace media | 445 } // namespace media |
OLD | NEW |