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 // Returns true when the decode result was end of stream. | |
| 80 static inline bool IsEndOfOutputStream(int result, | |
| 81 int decoded_size, | |
| 82 bool is_end_of_input_stream) { | |
| 83 // Three conditions to meet to declare end of stream for this decoder: | |
| 84 // 1. FFmpeg didn't read anything. | |
| 85 // 2. FFmpeg didn't output anything. | |
| 86 // 3. An end of stream buffer is received. | |
| 87 return result == 0 && decoded_size == 0 && is_end_of_input_stream; | |
| 88 } | |
| 89 | |
| 90 FFmpegCdmAudioDecoder::FFmpegCdmAudioDecoder(cdm::Allocator* allocator) | |
| 91 : is_initialized_(false), | |
| 92 allocator_(allocator), | |
| 93 codec_context_(NULL), | |
| 94 av_frame_(NULL), | |
| 95 bits_per_channel_(0), | |
| 96 samples_per_second_(0), | |
| 97 bytes_per_frame_(0), | |
| 98 output_timestamp_base_(media::kNoTimestamp()), | |
| 99 total_frames_decoded_(0), | |
| 100 last_input_timestamp_(media::kNoTimestamp()), | |
| 101 output_bytes_to_drop_(0) { | |
| 102 } | |
| 103 | |
| 104 FFmpegCdmAudioDecoder::~FFmpegCdmAudioDecoder() { | |
| 105 ReleaseFFmpegResources(); | |
| 106 } | |
| 107 | |
| 108 bool FFmpegCdmAudioDecoder::Initialize(const cdm::AudioDecoderConfig& config) { | |
| 109 DVLOG(1) << "Initialize()"; | |
| 110 | |
| 111 if (!IsValidConfig(config)) { | |
| 112 LOG(ERROR) << "Initialize(): invalid audio decoder configuration."; | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 if (is_initialized_) { | |
| 117 LOG(ERROR) << "Initialize(): Already initialized."; | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 // Initialize AVCodecContext structure. | |
| 122 codec_context_ = avcodec_alloc_context3(NULL); | |
| 123 CdmAudioDecoderConfigToAVCodecContext(config, codec_context_); | |
| 124 DCHECK_EQ(CODEC_ID_VORBIS, codec_context_->codec_id); | |
| 125 | |
| 126 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | |
| 127 if (!codec) { | |
| 128 LOG(ERROR) << "Initialize(): avcodec_find_decoder failed."; | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 int status; | |
| 133 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { | |
| 134 LOG(ERROR) << "Initialize(): avcodec_open2 failed: " << status; | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 av_frame_ = avcodec_alloc_frame(); | |
| 139 bits_per_channel_ = config.bits_per_channel; | |
| 140 samples_per_second_ = config.samples_per_second; | |
| 141 bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8; | |
| 142 serialized_audio_frames_.reserve(bytes_per_frame_ * samples_per_second_); | |
| 143 is_initialized_ = true; | |
| 144 | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 void FFmpegCdmAudioDecoder::Deinitialize() { | |
| 149 DVLOG(1) << "Deinitialize()"; | |
| 150 ReleaseFFmpegResources(); | |
| 151 is_initialized_ = false; | |
| 152 ResetAudioTimingData(); | |
| 153 } | |
| 154 | |
| 155 void FFmpegCdmAudioDecoder::Reset() { | |
| 156 DVLOG(1) << "Reset()"; | |
| 157 avcodec_flush_buffers(codec_context_); | |
| 158 ResetAudioTimingData(); | |
| 159 } | |
| 160 | |
| 161 // static | |
| 162 bool FFmpegCdmAudioDecoder::IsValidConfig( | |
| 163 const cdm::AudioDecoderConfig& config) { | |
| 164 return config.codec == cdm::AudioDecoderConfig::kCodecVorbis && | |
| 165 config.channel_count > 0 && | |
| 166 config.channel_count <= kMaxVorbisChannels && | |
| 167 config.bits_per_channel > 0 && | |
| 168 config.bits_per_channel <= media::limits::kMaxBitsPerSample && | |
| 169 config.samples_per_second > 0 && | |
| 170 config.samples_per_second <= media::limits::kMaxSampleRate; | |
| 171 } | |
| 172 | |
| 173 cdm::Status FFmpegCdmAudioDecoder::DecodeBuffer( | |
| 174 const uint8_t* compressed_buffer, | |
| 175 int32_t compressed_buffer_size, | |
| 176 int64_t input_timestamp, | |
| 177 cdm::AudioFrames* decoded_frames) { | |
| 178 const bool is_end_of_stream = compressed_buffer_size == 0; | |
| 179 base::TimeDelta timestamp = | |
| 180 base::TimeDelta::FromMicroseconds(input_timestamp); | |
| 181 if (!is_end_of_stream) { | |
| 182 if (last_input_timestamp_ == media::kNoTimestamp()) { | |
| 183 if (codec_context_->codec_id == CODEC_ID_VORBIS && | |
| 184 timestamp < base::TimeDelta()) { | |
| 185 // Dropping frames for negative timestamps as outlined in section A.2 | |
| 186 // in the Vorbis spec. http://xiph.org/vorbis/doc/Vorbis_I_spec.html | |
| 187 int frames_to_drop = floor( | |
| 188 0.5 + -timestamp.InSecondsF() * samples_per_second_); | |
| 189 output_bytes_to_drop_ = bytes_per_frame_ * frames_to_drop; | |
| 190 } else { | |
| 191 last_input_timestamp_ = timestamp; | |
| 192 } | |
| 193 } else if (timestamp != media::kNoTimestamp()) { | |
| 194 if (timestamp < last_input_timestamp_) { | |
| 195 base::TimeDelta diff = timestamp - last_input_timestamp_; | |
| 196 DVLOG(1) << "Input timestamps are not monotonically increasing! " | |
| 197 << " ts " << timestamp.InMicroseconds() << " us" | |
| 198 << " diff " << diff.InMicroseconds() << " us"; | |
| 199 return cdm::kDecodeError; | |
| 200 } | |
| 201 | |
| 202 last_input_timestamp_ = timestamp; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 AVPacket packet; | |
| 207 av_init_packet(&packet); | |
| 208 packet.data = const_cast<uint8_t*>(compressed_buffer); | |
| 209 packet.size = compressed_buffer_size; | |
| 210 | |
| 211 // Each audio packet may contain several frames, so we must call the decoder | |
| 212 // until we've exhausted the packet. Regardless of the packet size we always | |
| 213 // want to hand it to the decoder at least once, otherwise we would end up | |
| 214 // skipping end of stream packets since they have a size of zero. | |
| 215 do { | |
| 216 // Reset frame to default values. | |
| 217 avcodec_get_frame_defaults(av_frame_); | |
| 218 | |
| 219 int frame_decoded = 0; | |
| 220 int result = avcodec_decode_audio4( | |
| 221 codec_context_, av_frame_, &frame_decoded, &packet); | |
| 222 | |
| 223 if (result < 0) { | |
| 224 DCHECK(!is_end_of_stream) | |
| 225 << "End of stream buffer produced an error! " | |
| 226 << "This is quite possibly a bug in the audio decoder not handling " | |
| 227 << "end of stream AVPackets correctly."; | |
| 228 | |
| 229 DLOG(ERROR) | |
| 230 << "Error decoding an audio frame with timestamp: " | |
| 231 << timestamp.InMicroseconds() << " us, duration: " | |
| 232 << timestamp.InMicroseconds() << " us, packet size: " | |
| 233 << compressed_buffer_size << " bytes"; | |
| 234 | |
| 235 return cdm::kDecodeError; | |
| 236 } | |
| 237 | |
| 238 // Update packet size and data pointer in case we need to call the decoder | |
| 239 // with the remaining bytes from this packet. | |
| 240 packet.size -= result; | |
| 241 packet.data += result; | |
| 242 | |
| 243 if (output_timestamp_base_ == media::kNoTimestamp() && !is_end_of_stream) { | |
| 244 DCHECK(timestamp != media::kNoTimestamp()); | |
| 245 if (output_bytes_to_drop_ > 0) { | |
| 246 // If we have to drop samples it always means the timeline starts at 0. | |
| 247 output_timestamp_base_ = base::TimeDelta(); | |
| 248 } else { | |
| 249 output_timestamp_base_ = timestamp; | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 const uint8_t* decoded_audio_data = NULL; | |
| 254 int decoded_audio_size = 0; | |
| 255 if (frame_decoded) { | |
| 256 int output_sample_rate = av_frame_->sample_rate; | |
| 257 if (output_sample_rate != samples_per_second_) { | |
| 258 DLOG(ERROR) << "Output sample rate (" << output_sample_rate | |
| 259 << ") doesn't match expected rate " << samples_per_second_; | |
| 260 return cdm::kDecodeError; | |
| 261 } | |
| 262 | |
| 263 decoded_audio_data = av_frame_->data[0]; | |
| 264 decoded_audio_size = av_samples_get_buffer_size( | |
| 265 NULL, codec_context_->channels, av_frame_->nb_samples, | |
| 266 codec_context_->sample_fmt, 1); | |
| 267 } | |
| 268 | |
| 269 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
| 270 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); | |
| 271 decoded_audio_data += dropped_size; | |
| 272 decoded_audio_size -= dropped_size; | |
| 273 output_bytes_to_drop_ -= dropped_size; | |
| 274 } | |
| 275 | |
| 276 if (decoded_audio_size > 0) { | |
| 277 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) | |
| 278 << "Decoder didn't output full frames"; | |
| 279 | |
| 280 base::TimeDelta output_timestamp = GetNextOutputTimestamp(); | |
| 281 total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; | |
| 282 | |
| 283 // Serialize the audio samples into |serialized_audio_frames_|. | |
| 284 SerializeInt64(output_timestamp.InMicroseconds()); | |
| 285 SerializeInt64(decoded_audio_size); | |
| 286 serialized_audio_frames_.insert( | |
| 287 serialized_audio_frames_.end(), | |
| 288 decoded_audio_data, | |
| 289 decoded_audio_data + decoded_audio_size); | |
| 290 } else if (IsEndOfOutputStream(result, | |
| 291 decoded_audio_size, | |
| 292 is_end_of_stream)) { | |
| 293 DCHECK_EQ(packet.size, 0); | |
| 294 return cdm::kNeedMoreData; | |
| 295 } | |
| 296 } while (packet.size > 0); | |
| 297 | |
| 298 if (serialized_audio_frames_.size() > 0) { | |
| 299 decoded_frames->set_buffer( | |
| 300 allocator_->Allocate(serialized_audio_frames_.size())); | |
| 301 if (!decoded_frames->buffer()) { | |
| 302 LOG(ERROR) << "DecodeBuffer() cdm::Allocator::Allocate failed."; | |
| 303 return cdm::kDecodeError; | |
| 304 } | |
| 305 memcpy(decoded_frames->buffer()->data(), | |
| 306 &serialized_audio_frames_[0], | |
| 307 serialized_audio_frames_.size()); | |
| 308 serialized_audio_frames_.clear(); | |
| 309 } | |
| 310 | |
| 311 return cdm::kSuccess; | |
| 312 } | |
| 313 | |
| 314 void FFmpegCdmAudioDecoder::ResetAudioTimingData() { | |
| 315 output_timestamp_base_ = media::kNoTimestamp(); | |
| 316 total_frames_decoded_ = 0; | |
| 317 last_input_timestamp_ = media::kNoTimestamp(); | |
| 318 output_bytes_to_drop_ = 0; | |
| 319 } | |
| 320 | |
| 321 void FFmpegCdmAudioDecoder::ReleaseFFmpegResources() { | |
| 322 DVLOG(1) << "ReleaseFFmpegResources()"; | |
| 323 | |
| 324 if (codec_context_) { | |
| 325 av_free(codec_context_->extradata); | |
| 326 avcodec_close(codec_context_); | |
| 327 av_free(codec_context_); | |
| 328 codec_context_ = NULL; | |
| 329 } | |
| 330 if (av_frame_) { | |
| 331 av_free(av_frame_); | |
| 332 av_frame_ = NULL; | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 base::TimeDelta FFmpegCdmAudioDecoder::GetNextOutputTimestamp() const { | |
| 337 DCHECK(output_timestamp_base_ != media::kNoTimestamp()); | |
| 338 double decoded_us = (total_frames_decoded_ / samples_per_second_) * | |
|
xhwang
2012/10/24 22:24:19
samples_per_second can be large, e.g. 44100. We co
Tom Finegan
2012/10/24 23:37:52
Done. Are the consts OK?
| |
| 339 base::Time::kMicrosecondsPerSecond; | |
| 340 return output_timestamp_base_ + | |
| 341 base::TimeDelta::FromMicroseconds(decoded_us); | |
| 342 } | |
| 343 | |
| 344 void FFmpegCdmAudioDecoder::SerializeInt64(int64 value) { | |
| 345 const uint8_t* ptr = reinterpret_cast<uint8_t*>(&value); | |
| 346 serialized_audio_frames_.insert(serialized_audio_frames_.end(), | |
| 347 ptr, ptr + sizeof(value)); | |
| 348 } | |
| 349 | |
| 350 } // namespace webkit_media | |
| OLD | NEW |