Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "media/base/buffers.h" | |
| 11 #include "media/base/limits.h" | |
| 12 #include "webkit/media/crypto/ppapi/content_decryption_module.h" | |
| 13 | |
| 14 // Include FFmpeg header files. | |
| 15 extern "C" { | |
| 16 // Temporarily disable possible loss of data warning. | |
| 17 MSVC_PUSH_DISABLE_WARNING(4244); | |
| 18 #include <libavcodec/avcodec.h> | |
| 19 MSVC_POP_WARNING(); | |
| 20 } // extern "C" | |
| 21 | |
| 22 namespace webkit_media { | |
| 23 | |
| 24 // Maximum number of channels with defined order in the Vorbis specification. | |
| 25 // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html | |
| 26 static const int kMaxVorbisChannels = 8; | |
| 27 | |
| 28 static CodecID CdmAudioCodecToCodecID( | |
| 29 cdm::AudioDecoderConfig::AudioCodec audio_codec) { | |
| 30 switch (audio_codec) { | |
| 31 case cdm::AudioDecoderConfig::kCodecVorbis: | |
| 32 return CODEC_ID_VORBIS; | |
| 33 default: | |
| 34 NOTREACHED() << "Unsupported cdm::AudioCodec: " << audio_codec; | |
| 35 } | |
| 36 | |
| 37 return CODEC_ID_NONE; | |
| 38 } | |
| 39 | |
| 40 static void CdmAudioDecoderConfigToAVCodecContext( | |
| 41 const cdm::AudioDecoderConfig& config, | |
| 42 AVCodecContext* codec_context) { | |
| 43 codec_context->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 44 codec_context->codec_id = CdmAudioCodecToCodecID(config.codec); | |
| 45 | |
| 46 switch (config.bits_per_channel) { | |
| 47 case 8: | |
| 48 codec_context->sample_fmt = AV_SAMPLE_FMT_U8; | |
| 49 break; | |
| 50 case 16: | |
| 51 codec_context->sample_fmt = AV_SAMPLE_FMT_S16; | |
| 52 break; | |
| 53 case 32: | |
| 54 codec_context->sample_fmt = AV_SAMPLE_FMT_S32; | |
| 55 break; | |
| 56 default: | |
| 57 DVLOG(1) << "CdmAudioDecoderConfigToAVCodecContext() Unsupported bits " | |
| 58 "per channel: " << config.bits_per_channel; | |
| 59 codec_context->sample_fmt = AV_SAMPLE_FMT_NONE; | |
| 60 } | |
| 61 | |
| 62 codec_context->channels = config.channel_count; | |
| 63 codec_context->sample_rate = config.samples_per_second; | |
| 64 | |
| 65 if (config.extra_data) { | |
| 66 codec_context->extradata_size = config.extra_data_size; | |
| 67 codec_context->extradata = reinterpret_cast<uint8_t*>( | |
| 68 av_malloc(config.extra_data_size + FF_INPUT_BUFFER_PADDING_SIZE)); | |
| 69 memcpy(codec_context->extradata, config.extra_data, | |
| 70 config.extra_data_size); | |
| 71 memset(codec_context->extradata + config.extra_data_size, '\0', | |
| 72 FF_INPUT_BUFFER_PADDING_SIZE); | |
| 73 } else { | |
| 74 codec_context->extradata = NULL; | |
| 75 codec_context->extradata_size = 0; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 FFmpegCdmAudioDecoder::FFmpegCdmAudioDecoder(cdm::Allocator* allocator) | |
| 80 : is_initialized_(false), | |
| 81 allocator_(allocator), | |
| 82 codec_context_(NULL), | |
| 83 av_frame_(NULL), | |
| 84 bits_per_channel_(0), | |
| 85 samples_per_second_(0), | |
| 86 bytes_per_frame_(0), | |
| 87 output_timestamp_base_(media::kNoTimestamp()), | |
| 88 total_frames_decoded_(0), | |
| 89 last_input_timestamp_(media::kNoTimestamp()), | |
| 90 output_bytes_to_drop_(0) { | |
| 91 } | |
| 92 | |
| 93 FFmpegCdmAudioDecoder::~FFmpegCdmAudioDecoder() { | |
| 94 ReleaseFFmpegResources(); | |
| 95 } | |
| 96 | |
| 97 bool FFmpegCdmAudioDecoder::Initialize(const cdm::AudioDecoderConfig& config) { | |
| 98 DVLOG(1) << "Initialize()"; | |
| 99 | |
| 100 if (!IsValidConfig(config)) { | |
| 101 LOG(ERROR) << "Initialize(): invalid audio decoder configuration."; | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 if (is_initialized_) { | |
| 106 LOG(ERROR) << "Initialize(): Already initialized."; | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 // Initialize AVCodecContext structure. | |
| 111 codec_context_ = avcodec_alloc_context3(NULL); | |
| 112 CdmAudioDecoderConfigToAVCodecContext(config, codec_context_); | |
| 113 DCHECK_EQ(CODEC_ID_VORBIS, codec_context_->codec_id); | |
| 114 | |
| 115 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | |
| 116 if (!codec) { | |
| 117 LOG(ERROR) << "Initialize(): avcodec_find_decoder failed."; | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 int status; | |
| 122 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { | |
| 123 LOG(ERROR) << "Initialize(): avcodec_open2 failed: " << status; | |
| 124 return false; | |
| 125 } | |
| 126 | |
| 127 av_frame_ = avcodec_alloc_frame(); | |
| 128 bits_per_channel_ = config.bits_per_channel; | |
| 129 samples_per_second_ = config.samples_per_second; | |
| 130 bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8; | |
| 131 serialized_audio_frames_.reserve(bytes_per_frame_ * samples_per_second_); | |
| 132 is_initialized_ = true; | |
| 133 | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 void FFmpegCdmAudioDecoder::Deinitialize() { | |
| 138 DVLOG(1) << "Deinitialize()"; | |
| 139 ReleaseFFmpegResources(); | |
| 140 is_initialized_ = false; | |
| 141 ResetAudioTimingData(); | |
| 142 } | |
| 143 | |
| 144 void FFmpegCdmAudioDecoder::Reset() { | |
| 145 DVLOG(1) << "Reset()"; | |
| 146 avcodec_flush_buffers(codec_context_); | |
| 147 ResetAudioTimingData(); | |
| 148 } | |
| 149 | |
| 150 // static | |
| 151 bool FFmpegCdmAudioDecoder::IsValidConfig( | |
| 152 const cdm::AudioDecoderConfig& config) { | |
| 153 return config.codec == cdm::AudioDecoderConfig::kCodecVorbis && | |
| 154 config.channel_count > 0 && | |
| 155 config.channel_count <= kMaxVorbisChannels && | |
| 156 config.bits_per_channel > 0 && | |
| 157 config.bits_per_channel <= media::limits::kMaxBitsPerSample && | |
| 158 config.samples_per_second > 0 && | |
| 159 config.samples_per_second <= media::limits::kMaxSampleRate; | |
| 160 } | |
| 161 | |
| 162 cdm::Status FFmpegCdmAudioDecoder::DecodeBuffer( | |
| 163 const uint8_t* compressed_buffer, | |
| 164 int32_t compressed_buffer_size, | |
| 165 int64_t input_timestamp, | |
| 166 cdm::AudioFrames* decoded_frames) { | |
| 167 DVLOG(1) << "DecodeBuffer()"; | |
| 168 const bool is_end_of_stream = compressed_buffer_size == 0; | |
| 169 base::TimeDelta timestamp = | |
| 170 base::TimeDelta::FromMicroseconds(input_timestamp); | |
| 171 if (!is_end_of_stream) { | |
| 172 if (last_input_timestamp_ == media::kNoTimestamp()) { | |
| 173 if (codec_context_->codec_id == CODEC_ID_VORBIS && | |
| 174 timestamp < base::TimeDelta()) { | |
| 175 // Dropping frames for negative timestamps as outlined in section A.2 | |
| 176 // in the Vorbis spec. http://xiph.org/vorbis/doc/Vorbis_I_spec.html | |
| 177 int frames_to_drop = floor( | |
| 178 0.5 + -timestamp.InSecondsF() * samples_per_second_); | |
| 179 output_bytes_to_drop_ = bytes_per_frame_ * frames_to_drop; | |
| 180 } else { | |
| 181 last_input_timestamp_ = timestamp; | |
| 182 } | |
| 183 } else if (timestamp != media::kNoTimestamp()) { | |
| 184 if (timestamp < last_input_timestamp_) { | |
| 185 base::TimeDelta diff = timestamp - last_input_timestamp_; | |
| 186 DVLOG(1) << "Input timestamps are not monotonically increasing! " | |
| 187 << " ts " << timestamp.InMicroseconds() << " us" | |
| 188 << " diff " << diff.InMicroseconds() << " us"; | |
| 189 return cdm::kDecodeError; | |
| 190 } | |
| 191 | |
| 192 last_input_timestamp_ = timestamp; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 AVPacket packet; | |
| 197 av_init_packet(&packet); | |
| 198 packet.data = const_cast<uint8_t*>(compressed_buffer); | |
| 199 packet.size = compressed_buffer_size; | |
| 200 | |
| 201 // Each audio packet may contain several frames, so we must call the decoder | |
| 202 // until we've exhausted the packet. Regardless of the packet size we always | |
| 203 // want to hand it to the decoder at least once, otherwise we would end up | |
| 204 // skipping end of stream packets since they have a size of zero. | |
| 205 do { | |
| 206 // Reset frame to default values. | |
| 207 avcodec_get_frame_defaults(av_frame_); | |
| 208 | |
| 209 int frame_decoded = 0; | |
| 210 int result = avcodec_decode_audio4( | |
| 211 codec_context_, av_frame_, &frame_decoded, &packet); | |
| 212 | |
| 213 if (result < 0) { | |
| 214 DCHECK(!is_end_of_stream) | |
| 215 << "End of stream buffer produced an error! " | |
| 216 << "This is quite possibly a bug in the audio decoder not handling " | |
| 217 << "end of stream AVPackets correctly."; | |
| 218 | |
| 219 DLOG(ERROR) | |
| 220 << "Error decoding an audio frame with timestamp: " | |
| 221 << timestamp.InMicroseconds() << " us, duration: " | |
| 222 << timestamp.InMicroseconds() << " us, packet size: " | |
| 223 << compressed_buffer_size << " bytes"; | |
| 224 | |
| 225 return cdm::kDecodeError; | |
| 226 } | |
| 227 | |
| 228 // Update packet size and data pointer in case we need to call the decoder | |
| 229 // with the remaining bytes from this packet. | |
| 230 packet.size -= result; | |
| 231 packet.data += result; | |
| 232 | |
| 233 if (output_timestamp_base_ == media::kNoTimestamp() && !is_end_of_stream) { | |
| 234 DCHECK(timestamp != media::kNoTimestamp()); | |
| 235 if (output_bytes_to_drop_ > 0) { | |
| 236 // If we have to drop samples it always means the timeline starts at 0. | |
| 237 output_timestamp_base_ = base::TimeDelta(); | |
| 238 } else { | |
| 239 output_timestamp_base_ = timestamp; | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 const uint8_t* decoded_audio_data = NULL; | |
| 244 int decoded_audio_size = 0; | |
| 245 if (frame_decoded) { | |
| 246 int output_sample_rate = av_frame_->sample_rate; | |
| 247 if (output_sample_rate != samples_per_second_) { | |
| 248 DLOG(ERROR) << "Output sample rate (" << output_sample_rate | |
| 249 << ") doesn't match expected rate " << samples_per_second_; | |
| 250 return cdm::kDecodeError; | |
| 251 } | |
| 252 | |
| 253 decoded_audio_data = av_frame_->data[0]; | |
| 254 decoded_audio_size = | |
| 255 av_samples_get_buffer_size(NULL, | |
| 256 codec_context_->channels, | |
| 257 av_frame_->nb_samples, | |
| 258 codec_context_->sample_fmt, | |
| 259 1); | |
| 260 } | |
| 261 | |
| 262 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
| 263 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); | |
| 264 decoded_audio_data += dropped_size; | |
| 265 decoded_audio_size -= dropped_size; | |
| 266 output_bytes_to_drop_ -= dropped_size; | |
| 267 } | |
| 268 | |
| 269 if (decoded_audio_size > 0) { | |
| 270 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) | |
| 271 << "Decoder didn't output full frames"; | |
| 272 | |
| 273 base::TimeDelta output_timestamp = GetNextOutputTimestamp(); | |
| 274 total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; | |
| 275 | |
| 276 // Serialize the audio samples into |serialized_audio_frames_|. | |
| 277 SerializeInt64(output_timestamp.InMicroseconds()); | |
| 278 SerializeInt64(decoded_audio_size); | |
| 279 serialized_audio_frames_.insert(serialized_audio_frames_.end(), | |
| 280 decoded_audio_data, | |
| 281 decoded_audio_data + decoded_audio_size); | |
| 282 } | |
| 283 } while (packet.size > 0); | |
| 284 | |
| 285 if (serialized_audio_frames_.size() > 0) { | |
|
xhwang
2012/10/25 01:24:58
nit: !empty()
Tom Finegan
2012/10/25 01:42:19
Done.
| |
| 286 decoded_frames->set_buffer( | |
| 287 allocator_->Allocate(serialized_audio_frames_.size())); | |
| 288 if (!decoded_frames->buffer()) { | |
| 289 LOG(ERROR) << "DecodeBuffer() cdm::Allocator::Allocate failed."; | |
| 290 return cdm::kDecodeError; | |
| 291 } | |
| 292 memcpy(decoded_frames->buffer()->data(), | |
| 293 &serialized_audio_frames_[0], | |
| 294 serialized_audio_frames_.size()); | |
| 295 serialized_audio_frames_.clear(); | |
| 296 | |
| 297 return cdm::kSuccess; | |
| 298 } | |
| 299 | |
| 300 return cdm::kNeedMoreData; | |
|
xhwang
2012/10/25 01:24:58
I like this!
Tom Finegan
2012/10/25 01:42:19
It definitely seems to make this more readable. :)
| |
| 301 } | |
| 302 | |
| 303 void FFmpegCdmAudioDecoder::ResetAudioTimingData() { | |
| 304 output_timestamp_base_ = media::kNoTimestamp(); | |
| 305 total_frames_decoded_ = 0; | |
| 306 last_input_timestamp_ = media::kNoTimestamp(); | |
| 307 output_bytes_to_drop_ = 0; | |
| 308 } | |
| 309 | |
| 310 void FFmpegCdmAudioDecoder::ReleaseFFmpegResources() { | |
| 311 DVLOG(1) << "ReleaseFFmpegResources()"; | |
| 312 | |
| 313 if (codec_context_) { | |
| 314 av_free(codec_context_->extradata); | |
| 315 avcodec_close(codec_context_); | |
| 316 av_free(codec_context_); | |
| 317 codec_context_ = NULL; | |
| 318 } | |
| 319 if (av_frame_) { | |
| 320 av_free(av_frame_); | |
| 321 av_frame_ = NULL; | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 base::TimeDelta FFmpegCdmAudioDecoder::GetNextOutputTimestamp() const { | |
| 326 DCHECK(output_timestamp_base_ != media::kNoTimestamp()); | |
| 327 const double total_frames_decoded = total_frames_decoded_; | |
| 328 const double decoded_us = (total_frames_decoded / samples_per_second_) * | |
| 329 base::Time::kMicrosecondsPerSecond; | |
| 330 return output_timestamp_base_ + | |
| 331 base::TimeDelta::FromMicroseconds(decoded_us); | |
| 332 } | |
| 333 | |
| 334 void FFmpegCdmAudioDecoder::SerializeInt64(int64 value) { | |
| 335 const uint8_t* ptr = reinterpret_cast<uint8_t*>(&value); | |
| 336 serialized_audio_frames_.insert(serialized_audio_frames_.end(), | |
| 337 ptr, ptr + sizeof(value)); | |
| 338 } | |
| 339 | |
| 340 } // namespace webkit_media | |
| OLD | NEW |