| 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 = |
| 265 av_samples_get_buffer_size(NULL, |
| 266 codec_context_->channels, |
| 267 av_frame_->nb_samples, |
| 268 codec_context_->sample_fmt, |
| 269 1); |
| 270 } |
| 271 |
| 272 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { |
| 273 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); |
| 274 decoded_audio_data += dropped_size; |
| 275 decoded_audio_size -= dropped_size; |
| 276 output_bytes_to_drop_ -= dropped_size; |
| 277 } |
| 278 |
| 279 if (decoded_audio_size > 0) { |
| 280 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
| 281 << "Decoder didn't output full frames"; |
| 282 |
| 283 base::TimeDelta output_timestamp = GetNextOutputTimestamp(); |
| 284 total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; |
| 285 |
| 286 // Serialize the audio samples into |serialized_audio_frames_|. |
| 287 SerializeInt64(output_timestamp.InMicroseconds()); |
| 288 SerializeInt64(decoded_audio_size); |
| 289 serialized_audio_frames_.insert(serialized_audio_frames_.end(), |
| 290 decoded_audio_data, |
| 291 decoded_audio_data + decoded_audio_size); |
| 292 } else if (IsEndOfOutputStream(result, |
| 293 decoded_audio_size, |
| 294 is_end_of_stream)) { |
| 295 DCHECK_EQ(packet.size, 0); |
| 296 return cdm::kNeedMoreData; |
| 297 } |
| 298 } while (packet.size > 0); |
| 299 |
| 300 if (serialized_audio_frames_.size() > 0) { |
| 301 decoded_frames->set_buffer( |
| 302 allocator_->Allocate(serialized_audio_frames_.size())); |
| 303 if (!decoded_frames->buffer()) { |
| 304 LOG(ERROR) << "DecodeBuffer() cdm::Allocator::Allocate failed."; |
| 305 return cdm::kDecodeError; |
| 306 } |
| 307 memcpy(decoded_frames->buffer()->data(), |
| 308 &serialized_audio_frames_[0], |
| 309 serialized_audio_frames_.size()); |
| 310 serialized_audio_frames_.clear(); |
| 311 } |
| 312 |
| 313 return cdm::kSuccess; |
| 314 } |
| 315 |
| 316 void FFmpegCdmAudioDecoder::ResetAudioTimingData() { |
| 317 output_timestamp_base_ = media::kNoTimestamp(); |
| 318 total_frames_decoded_ = 0; |
| 319 last_input_timestamp_ = media::kNoTimestamp(); |
| 320 output_bytes_to_drop_ = 0; |
| 321 } |
| 322 |
| 323 void FFmpegCdmAudioDecoder::ReleaseFFmpegResources() { |
| 324 DVLOG(1) << "ReleaseFFmpegResources()"; |
| 325 |
| 326 if (codec_context_) { |
| 327 av_free(codec_context_->extradata); |
| 328 avcodec_close(codec_context_); |
| 329 av_free(codec_context_); |
| 330 codec_context_ = NULL; |
| 331 } |
| 332 if (av_frame_) { |
| 333 av_free(av_frame_); |
| 334 av_frame_ = NULL; |
| 335 } |
| 336 } |
| 337 |
| 338 base::TimeDelta FFmpegCdmAudioDecoder::GetNextOutputTimestamp() const { |
| 339 DCHECK(output_timestamp_base_ != media::kNoTimestamp()); |
| 340 const double total_frames_decoded = total_frames_decoded_; |
| 341 const double decoded_us = (total_frames_decoded / samples_per_second_) * |
| 342 base::Time::kMicrosecondsPerSecond; |
| 343 return output_timestamp_base_ + |
| 344 base::TimeDelta::FromMicroseconds(decoded_us); |
| 345 } |
| 346 |
| 347 void FFmpegCdmAudioDecoder::SerializeInt64(int64 value) { |
| 348 const uint8_t* ptr = reinterpret_cast<uint8_t*>(&value); |
| 349 serialized_audio_frames_.insert(serialized_audio_frames_.end(), |
| 350 ptr, ptr + sizeof(value)); |
| 351 } |
| 352 |
| 353 } // namespace webkit_media |
| OLD | NEW |