Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.cc

Issue 11778079: Encrypted Media: Enforcing the CDM to decode audio into S16 integers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.h ('k') | webkit/media/webkit_media.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.h" 5 #include "webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "media/base/audio_bus.h"
11 #include "media/base/audio_timestamp_helper.h"
10 #include "media/base/buffers.h" 12 #include "media/base/buffers.h"
13 #include "media/base/data_buffer.h"
11 #include "media/base/limits.h" 14 #include "media/base/limits.h"
12 #include "webkit/media/crypto/ppapi/cdm/content_decryption_module.h" 15 #include "webkit/media/crypto/ppapi/cdm/content_decryption_module.h"
13 16
14 // Include FFmpeg header files. 17 // Include FFmpeg header files.
15 extern "C" { 18 extern "C" {
16 // Temporarily disable possible loss of data warning. 19 // Temporarily disable possible loss of data warning.
17 MSVC_PUSH_DISABLE_WARNING(4244); 20 MSVC_PUSH_DISABLE_WARNING(4244);
18 #include <libavcodec/avcodec.h> 21 #include <libavcodec/avcodec.h>
19 MSVC_POP_WARNING(); 22 MSVC_POP_WARNING();
20 } // extern "C" 23 } // extern "C"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 } 81 }
79 82
80 FFmpegCdmAudioDecoder::FFmpegCdmAudioDecoder(cdm::Allocator* allocator) 83 FFmpegCdmAudioDecoder::FFmpegCdmAudioDecoder(cdm::Allocator* allocator)
81 : is_initialized_(false), 84 : is_initialized_(false),
82 allocator_(allocator), 85 allocator_(allocator),
83 codec_context_(NULL), 86 codec_context_(NULL),
84 av_frame_(NULL), 87 av_frame_(NULL),
85 bits_per_channel_(0), 88 bits_per_channel_(0),
86 samples_per_second_(0), 89 samples_per_second_(0),
87 bytes_per_frame_(0), 90 bytes_per_frame_(0),
88 output_timestamp_base_(media::kNoTimestamp()),
89 total_frames_decoded_(0),
90 last_input_timestamp_(media::kNoTimestamp()), 91 last_input_timestamp_(media::kNoTimestamp()),
91 output_bytes_to_drop_(0) { 92 output_bytes_to_drop_(0) {
92 } 93 }
93 94
94 FFmpegCdmAudioDecoder::~FFmpegCdmAudioDecoder() { 95 FFmpegCdmAudioDecoder::~FFmpegCdmAudioDecoder() {
95 ReleaseFFmpegResources(); 96 ReleaseFFmpegResources();
96 } 97 }
97 98
98 bool FFmpegCdmAudioDecoder::Initialize(const cdm::AudioDecoderConfig& config) { 99 bool FFmpegCdmAudioDecoder::Initialize(const cdm::AudioDecoderConfig& config) {
99 DVLOG(1) << "Initialize()"; 100 DVLOG(1) << "Initialize()";
100 101
101 if (!IsValidConfig(config)) { 102 if (!IsValidConfig(config)) {
102 LOG(ERROR) << "Initialize(): invalid audio decoder configuration."; 103 LOG(ERROR) << "Initialize(): invalid audio decoder configuration.";
103 return false; 104 return false;
104 } 105 }
105 106
106 if (is_initialized_) { 107 if (is_initialized_) {
107 LOG(ERROR) << "Initialize(): Already initialized."; 108 LOG(ERROR) << "Initialize(): Already initialized.";
108 return false; 109 return false;
109 } 110 }
110 111
111 // Initialize AVCodecContext structure. 112 // Initialize AVCodecContext structure.
112 codec_context_ = avcodec_alloc_context3(NULL); 113 codec_context_ = avcodec_alloc_context3(NULL);
113 CdmAudioDecoderConfigToAVCodecContext(config, codec_context_); 114 CdmAudioDecoderConfigToAVCodecContext(config, codec_context_);
114 115
116 // MP3 decodes to S16P which we don't support, tell it to use S16 instead.
117 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P)
118 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16;
119
115 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); 120 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
116 if (!codec) { 121 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) {
117 LOG(ERROR) << "Initialize(): avcodec_find_decoder failed."; 122 DLOG(ERROR) << "Could not initialize audio decoder: "
123 << codec_context_->codec_id;
118 return false; 124 return false;
119 } 125 }
120 126
121 int status; 127 // Ensure avcodec_open2() respected our format request.
122 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { 128 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) {
123 LOG(ERROR) << "Initialize(): avcodec_open2 failed: " << status; 129 DLOG(ERROR) << "Unable to configure a supported sample format: "
130 << codec_context_->sample_fmt;
124 return false; 131 return false;
125 } 132 }
126 133
134 // Some codecs will only output float data, so we need to convert to integer
135 // before returning the decoded buffer.
136 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP ||
137 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) {
138 // Preallocate the AudioBus for float conversions. We can treat interleaved
139 // float data as a single planar channel since our output is expected in an
140 // interleaved format anyways.
141 int channels = codec_context_->channels;
142 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT)
143 channels = 1;
144 converter_bus_ = media::AudioBus::CreateWrapper(channels);
145 }
146
147 // Success!
127 av_frame_ = avcodec_alloc_frame(); 148 av_frame_ = avcodec_alloc_frame();
128 bits_per_channel_ = config.bits_per_channel; 149 bits_per_channel_ = config.bits_per_channel;
129 samples_per_second_ = config.samples_per_second; 150 samples_per_second_ = config.samples_per_second;
130 bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8; 151 bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8;
152 output_timestamp_helper_.reset(new media::AudioTimestampHelper(
153 bytes_per_frame_, config.samples_per_second));
131 serialized_audio_frames_.reserve(bytes_per_frame_ * samples_per_second_); 154 serialized_audio_frames_.reserve(bytes_per_frame_ * samples_per_second_);
132 is_initialized_ = true; 155 is_initialized_ = true;
133 156
134 return true; 157 return true;
135 } 158 }
136 159
137 void FFmpegCdmAudioDecoder::Deinitialize() { 160 void FFmpegCdmAudioDecoder::Deinitialize() {
138 DVLOG(1) << "Deinitialize()"; 161 DVLOG(1) << "Deinitialize()";
139 ReleaseFFmpegResources(); 162 ReleaseFFmpegResources();
140 is_initialized_ = false; 163 is_initialized_ = false;
141 ResetAudioTimingData(); 164 ResetTimestampState();
142 } 165 }
143 166
144 void FFmpegCdmAudioDecoder::Reset() { 167 void FFmpegCdmAudioDecoder::Reset() {
145 DVLOG(1) << "Reset()"; 168 DVLOG(1) << "Reset()";
146 avcodec_flush_buffers(codec_context_); 169 avcodec_flush_buffers(codec_context_);
147 ResetAudioTimingData(); 170 ResetTimestampState();
148 } 171 }
149 172
150 // static 173 // static
151 bool FFmpegCdmAudioDecoder::IsValidConfig( 174 bool FFmpegCdmAudioDecoder::IsValidConfig(
152 const cdm::AudioDecoderConfig& config) { 175 const cdm::AudioDecoderConfig& config) {
153 return config.codec != cdm::AudioDecoderConfig::kUnknownAudioCodec && 176 return config.codec != cdm::AudioDecoderConfig::kUnknownAudioCodec &&
154 config.channel_count > 0 && 177 config.channel_count > 0 &&
155 config.channel_count <= kMaxChannels && 178 config.channel_count <= kMaxChannels &&
156 config.bits_per_channel > 0 && 179 config.bits_per_channel > 0 &&
157 config.bits_per_channel <= media::limits::kMaxBitsPerSample && 180 config.bits_per_channel <= media::limits::kMaxBitsPerSample &&
158 config.samples_per_second > 0 && 181 config.samples_per_second > 0 &&
159 config.samples_per_second <= media::limits::kMaxSampleRate; 182 config.samples_per_second <= media::limits::kMaxSampleRate;
160 } 183 }
161 184
162 cdm::Status FFmpegCdmAudioDecoder::DecodeBuffer( 185 cdm::Status FFmpegCdmAudioDecoder::DecodeBuffer(
163 const uint8_t* compressed_buffer, 186 const uint8_t* compressed_buffer,
164 int32_t compressed_buffer_size, 187 int32_t compressed_buffer_size,
165 int64_t input_timestamp, 188 int64_t input_timestamp,
166 cdm::AudioFrames* decoded_frames) { 189 cdm::AudioFrames* decoded_frames) {
167 DVLOG(1) << "DecodeBuffer()"; 190 DVLOG(1) << "DecodeBuffer()";
168 const bool is_end_of_stream = compressed_buffer_size == 0; 191 const bool is_end_of_stream = compressed_buffer_size == 0;
169 base::TimeDelta timestamp = 192 base::TimeDelta timestamp =
170 base::TimeDelta::FromMicroseconds(input_timestamp); 193 base::TimeDelta::FromMicroseconds(input_timestamp);
194
195 bool is_vorbis = codec_context_->codec_id == CODEC_ID_VORBIS;
171 if (!is_end_of_stream) { 196 if (!is_end_of_stream) {
172 if (last_input_timestamp_ == media::kNoTimestamp()) { 197 if (last_input_timestamp_ == media::kNoTimestamp()) {
173 if (codec_context_->codec_id == CODEC_ID_VORBIS && 198 if (is_vorbis && timestamp < base::TimeDelta()) {
174 timestamp < base::TimeDelta()) {
175 // Dropping frames for negative timestamps as outlined in section A.2 199 // 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 200 // in the Vorbis spec. http://xiph.org/vorbis/doc/Vorbis_I_spec.html
177 int frames_to_drop = floor( 201 int frames_to_drop = floor(
178 0.5 + -timestamp.InSecondsF() * samples_per_second_); 202 0.5 + -timestamp.InSecondsF() * samples_per_second_);
179 output_bytes_to_drop_ = bytes_per_frame_ * frames_to_drop; 203 output_bytes_to_drop_ = bytes_per_frame_ * frames_to_drop;
180 } else { 204 } else {
181 last_input_timestamp_ = timestamp; 205 last_input_timestamp_ = timestamp;
182 } 206 }
183 } else if (timestamp != media::kNoTimestamp()) { 207 } else if (timestamp != media::kNoTimestamp()) {
184 if (timestamp < last_input_timestamp_) { 208 if (timestamp < last_input_timestamp_) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 << compressed_buffer_size << " bytes"; 247 << compressed_buffer_size << " bytes";
224 248
225 return cdm::kDecodeError; 249 return cdm::kDecodeError;
226 } 250 }
227 251
228 // Update packet size and data pointer in case we need to call the decoder 252 // Update packet size and data pointer in case we need to call the decoder
229 // with the remaining bytes from this packet. 253 // with the remaining bytes from this packet.
230 packet.size -= result; 254 packet.size -= result;
231 packet.data += result; 255 packet.data += result;
232 256
233 if (output_timestamp_base_ == media::kNoTimestamp() && !is_end_of_stream) { 257 if (output_timestamp_helper_->base_timestamp() == media::kNoTimestamp() &&
258 !is_end_of_stream) {
234 DCHECK(timestamp != media::kNoTimestamp()); 259 DCHECK(timestamp != media::kNoTimestamp());
235 if (output_bytes_to_drop_ > 0) { 260 if (output_bytes_to_drop_ > 0) {
261 // Currently Vorbis is the only codec that causes us to drop samples.
236 // If we have to drop samples it always means the timeline starts at 0. 262 // If we have to drop samples it always means the timeline starts at 0.
237 output_timestamp_base_ = base::TimeDelta(); 263 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS);
264 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta());
238 } else { 265 } else {
239 output_timestamp_base_ = timestamp; 266 output_timestamp_helper_->SetBaseTimestamp(timestamp);
240 } 267 }
241 } 268 }
242 269
243 const uint8_t* decoded_audio_data = NULL;
244 int decoded_audio_size = 0; 270 int decoded_audio_size = 0;
245 if (frame_decoded) { 271 if (frame_decoded) {
246 int output_sample_rate = av_frame_->sample_rate; 272 int output_sample_rate = av_frame_->sample_rate;
247 if (output_sample_rate != samples_per_second_) { 273 if (output_sample_rate != samples_per_second_) {
248 DLOG(ERROR) << "Output sample rate (" << output_sample_rate 274 DLOG(ERROR) << "Output sample rate (" << output_sample_rate
249 << ") doesn't match expected rate " << samples_per_second_; 275 << ") doesn't match expected rate " << samples_per_second_;
250 return cdm::kDecodeError; 276 return cdm::kDecodeError;
251 } 277 }
252 278
253 decoded_audio_data = av_frame_->data[0]; 279 decoded_audio_size = av_samples_get_buffer_size(
254 decoded_audio_size = 280 NULL, codec_context_->channels, av_frame_->nb_samples,
255 av_samples_get_buffer_size(NULL, 281 codec_context_->sample_fmt, 1);
256 codec_context_->channels, 282 // If we're decoding into float, adjust audio size.
257 av_frame_->nb_samples, 283 if (converter_bus_ && bits_per_channel_ / 8 != sizeof(float)) {
258 codec_context_->sample_fmt, 284 DCHECK(codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT ||
259 1); 285 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP);
286 decoded_audio_size *=
287 static_cast<float>(bits_per_channel_ / 8) / sizeof(float);
288 }
260 } 289 }
261 290
291 int start_sample = 0;
262 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { 292 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) {
293 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0)
294 << "Decoder didn't output full frames";
295
263 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); 296 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_);
264 decoded_audio_data += dropped_size; 297 start_sample = dropped_size / bytes_per_frame_;
265 decoded_audio_size -= dropped_size; 298 decoded_audio_size -= dropped_size;
266 output_bytes_to_drop_ -= dropped_size; 299 output_bytes_to_drop_ -= dropped_size;
267 } 300 }
268 301
302 scoped_refptr<media::DataBuffer> output;
269 if (decoded_audio_size > 0) { 303 if (decoded_audio_size > 0) {
270 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) 304 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0)
271 << "Decoder didn't output full frames"; 305 << "Decoder didn't output full frames";
272 306
273 base::TimeDelta output_timestamp = GetNextOutputTimestamp(); 307 // Convert float data using an AudioBus.
274 total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; 308 if (converter_bus_) {
309 // Setup the AudioBus as a wrapper of the AVFrame data and then use
310 // AudioBus::ToInterleaved() to convert the data as necessary.
311 int skip_frames = start_sample;
312 int total_frames = av_frame_->nb_samples - start_sample;
313 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) {
314 DCHECK_EQ(converter_bus_->channels(), 1);
315 total_frames *= codec_context_->channels;
316 skip_frames *= codec_context_->channels;
317 }
318 converter_bus_->set_frames(total_frames);
319 DCHECK_EQ(decoded_audio_size,
320 converter_bus_->frames() * bytes_per_frame_);
321
322 for (int i = 0; i < converter_bus_->channels(); ++i) {
323 converter_bus_->SetChannelData(i, reinterpret_cast<float*>(
324 av_frame_->extended_data[i]) + skip_frames);
325 }
326
327 output = new media::DataBuffer(decoded_audio_size);
328 output->SetDataSize(decoded_audio_size);
329 converter_bus_->ToInterleaved(
330 converter_bus_->frames(), bits_per_channel_ / 8,
331 output->GetWritableData());
332 } else {
333 output = new media::DataBuffer(
334 av_frame_->extended_data[0] + start_sample * bytes_per_frame_,
335 decoded_audio_size);
336 }
337
338 base::TimeDelta output_timestamp =
339 output_timestamp_helper_->GetTimestamp();
340 output_timestamp_helper_->AddBytes(decoded_audio_size);
275 341
276 // Serialize the audio samples into |serialized_audio_frames_|. 342 // Serialize the audio samples into |serialized_audio_frames_|.
277 SerializeInt64(output_timestamp.InMicroseconds()); 343 SerializeInt64(output_timestamp.InMicroseconds());
278 SerializeInt64(decoded_audio_size); 344 SerializeInt64(output->GetDataSize());
279 serialized_audio_frames_.insert(serialized_audio_frames_.end(), 345 serialized_audio_frames_.insert(
280 decoded_audio_data, 346 serialized_audio_frames_.end(),
281 decoded_audio_data + decoded_audio_size); 347 output->GetData(),
348 output->GetData() + output->GetDataSize());
282 } 349 }
283 } while (packet.size > 0); 350 } while (packet.size > 0);
284 351
285 if (!serialized_audio_frames_.empty()) { 352 if (!serialized_audio_frames_.empty()) {
286 decoded_frames->SetFrameBuffer( 353 decoded_frames->SetFrameBuffer(
287 allocator_->Allocate(serialized_audio_frames_.size())); 354 allocator_->Allocate(serialized_audio_frames_.size()));
288 if (!decoded_frames->FrameBuffer()) { 355 if (!decoded_frames->FrameBuffer()) {
289 LOG(ERROR) << "DecodeBuffer() cdm::Allocator::Allocate failed."; 356 LOG(ERROR) << "DecodeBuffer() cdm::Allocator::Allocate failed.";
290 return cdm::kDecodeError; 357 return cdm::kDecodeError;
291 } 358 }
292 memcpy(decoded_frames->FrameBuffer()->Data(), 359 memcpy(decoded_frames->FrameBuffer()->Data(),
293 &serialized_audio_frames_[0], 360 &serialized_audio_frames_[0],
294 serialized_audio_frames_.size()); 361 serialized_audio_frames_.size());
295 decoded_frames->FrameBuffer()->SetSize(serialized_audio_frames_.size()); 362 decoded_frames->FrameBuffer()->SetSize(serialized_audio_frames_.size());
296 serialized_audio_frames_.clear(); 363 serialized_audio_frames_.clear();
297 364
298 return cdm::kSuccess; 365 return cdm::kSuccess;
299 } 366 }
300 367
301 return cdm::kNeedMoreData; 368 return cdm::kNeedMoreData;
302 } 369 }
303 370
304 void FFmpegCdmAudioDecoder::ResetAudioTimingData() { 371 void FFmpegCdmAudioDecoder::ResetTimestampState() {
305 output_timestamp_base_ = media::kNoTimestamp(); 372 output_timestamp_helper_->SetBaseTimestamp(media::kNoTimestamp());
306 total_frames_decoded_ = 0;
307 last_input_timestamp_ = media::kNoTimestamp(); 373 last_input_timestamp_ = media::kNoTimestamp();
308 output_bytes_to_drop_ = 0; 374 output_bytes_to_drop_ = 0;
309 } 375 }
310 376
311 void FFmpegCdmAudioDecoder::ReleaseFFmpegResources() { 377 void FFmpegCdmAudioDecoder::ReleaseFFmpegResources() {
312 DVLOG(1) << "ReleaseFFmpegResources()"; 378 DVLOG(1) << "ReleaseFFmpegResources()";
313 379
314 if (codec_context_) { 380 if (codec_context_) {
315 av_free(codec_context_->extradata); 381 av_free(codec_context_->extradata);
316 avcodec_close(codec_context_); 382 avcodec_close(codec_context_);
317 av_free(codec_context_); 383 av_free(codec_context_);
318 codec_context_ = NULL; 384 codec_context_ = NULL;
319 } 385 }
320 if (av_frame_) { 386 if (av_frame_) {
321 av_free(av_frame_); 387 av_free(av_frame_);
322 av_frame_ = NULL; 388 av_frame_ = NULL;
323 } 389 }
324 } 390 }
325 391
326 base::TimeDelta FFmpegCdmAudioDecoder::GetNextOutputTimestamp() const {
327 DCHECK(output_timestamp_base_ != media::kNoTimestamp());
328 const double total_frames_decoded = total_frames_decoded_;
329 const double decoded_us = (total_frames_decoded / samples_per_second_) *
330 base::Time::kMicrosecondsPerSecond;
331 return output_timestamp_base_ +
332 base::TimeDelta::FromMicroseconds(decoded_us);
333 }
334
335 void FFmpegCdmAudioDecoder::SerializeInt64(int64 value) { 392 void FFmpegCdmAudioDecoder::SerializeInt64(int64 value) {
336 int previous_size = serialized_audio_frames_.size(); 393 int previous_size = serialized_audio_frames_.size();
337 serialized_audio_frames_.resize(previous_size + sizeof(value)); 394 serialized_audio_frames_.resize(previous_size + sizeof(value));
338 memcpy(&serialized_audio_frames_[0] + previous_size, &value, sizeof(value)); 395 memcpy(&serialized_audio_frames_[0] + previous_size, &value, sizeof(value));
339 } 396 }
340 397
341 } // namespace webkit_media 398 } // namespace webkit_media
OLDNEW
« no previous file with comments | « webkit/media/crypto/ppapi/ffmpeg_cdm_audio_decoder.h ('k') | webkit/media/webkit_media.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698