diff options
author | tomfinegan@chromium.org <tomfinegan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-23 04:31:43 +0000 |
---|---|---|
committer | tomfinegan@chromium.org <tomfinegan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-23 04:31:43 +0000 |
commit | bf5f16885c4ab420986fcdf3f9364b9f714e1e4e (patch) | |
tree | 18625398b0ca9ed40da98192c22c99e1db42548a /webkit/media/crypto/ppapi | |
parent | 8ccddf61312b0f0c514be035cd7754b3474c6aef (diff) | |
download | chromium_src-bf5f16885c4ab420986fcdf3f9364b9f714e1e4e.zip chromium_src-bf5f16885c4ab420986fcdf3f9364b9f714e1e4e.tar.gz chromium_src-bf5f16885c4ab420986fcdf3f9364b9f714e1e4e.tar.bz2 |
Add CDM video decoder.
Add FFmpeg CDM video decoder, and fix bugs with passing emtpy resources through DecryptAndDecode, DeliverFrame, and Deliversamples.
TBR=brettw,viettrungluu
BUG=141780
TEST=*ExternalClearKey* browser tests pass
Review URL: https://chromiumcodereview.appspot.com/10899021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163501 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/media/crypto/ppapi')
-rw-r--r-- | webkit/media/crypto/ppapi/cdm_wrapper.cc | 26 | ||||
-rw-r--r-- | webkit/media/crypto/ppapi/clear_key_cdm.cc | 84 | ||||
-rw-r--r-- | webkit/media/crypto/ppapi/clear_key_cdm.h | 11 | ||||
-rw-r--r-- | webkit/media/crypto/ppapi/content_decryption_module.h | 2 | ||||
-rw-r--r-- | webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc | 332 | ||||
-rw-r--r-- | webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h | 59 |
6 files changed, 492 insertions, 22 deletions
diff --git a/webkit/media/crypto/ppapi/cdm_wrapper.cc b/webkit/media/crypto/ppapi/cdm_wrapper.cc index 2180c34..0596643 100644 --- a/webkit/media/crypto/ppapi/cdm_wrapper.cc +++ b/webkit/media/crypto/ppapi/cdm_wrapper.cc @@ -23,8 +23,8 @@ #include "ppapi/cpp/dev/buffer_dev.h" #include "ppapi/cpp/private/content_decryptor_private.h" #include "ppapi/utility/completion_callback_factory.h" -#include "webkit/media/crypto/ppapi/linked_ptr.h" #include "webkit/media/crypto/ppapi/content_decryption_module.h" +#include "webkit/media/crypto/ppapi/linked_ptr.h" namespace { @@ -136,6 +136,19 @@ cdm::VideoFormat PpDecryptedFrameFormatToCdmVideoFormat( return cdm::kUnknownVideoFormat; } +cdm::StreamType PpDecryptorStreamTypeToCdmStreamType( + PP_DecryptorStreamType stream_type) { + switch (stream_type) { + case PP_DECRYPTORSTREAMTYPE_AUDIO: + return cdm::kStreamTypeAudio; + case PP_DECRYPTORSTREAMTYPE_VIDEO: + return cdm::kStreamTypeVideo; + } + + PP_NOTREACHED(); + return cdm::kStreamTypeVideo; +} + } // namespace namespace webkit_media { @@ -495,6 +508,7 @@ PpbBufferAllocator::~PpbBufferAllocator() { cdm::Buffer* PpbBufferAllocator::Allocate(int32_t size) { PP_DCHECK(size > 0); + PP_DCHECK(IsMainThread()); pp::Buffer_Dev buffer(instance_, size); if (buffer.is_null()) @@ -657,8 +671,7 @@ void CdmWrapper::InitializeVideoDecoder( void CdmWrapper::DeinitializeDecoder(PP_DecryptorStreamType decoder_type, uint32_t request_id) { - // TODO(tomfinegan): Implement DeinitializeDecoder in clear key CDM, and call - // it here. + cdm_->DeinitializeDecoder(PpDecryptorStreamTypeToCdmStreamType(decoder_type)); CallOnMain(callback_factory_.NewCallback( &CdmWrapper::DecoderDeinitializeDone, decoder_type, @@ -667,8 +680,7 @@ void CdmWrapper::DeinitializeDecoder(PP_DecryptorStreamType decoder_type, void CdmWrapper::ResetDecoder(PP_DecryptorStreamType decoder_type, uint32_t request_id) { - // TODO(tomfinegan): Implement ResetDecoder in clear key CDM, and call it - // here. + cdm_->ResetDecoder(PpDecryptorStreamTypeToCdmStreamType(decoder_type)); CallOnMain(callback_factory_.NewCallback(&CdmWrapper::DecoderResetDone, decoder_type, request_id)); @@ -852,6 +864,10 @@ void CdmWrapper::DeliverFrame( decrypted_frame_info.strides[PP_DECRYPTEDFRAMEPLANES_V] = video_frame->stride(cdm::VideoFrame::kVPlane); break; + case cdm::kNeedMoreData: + decrypted_frame_info.result = PP_DECRYPTRESULT_SUCCESS; + decrypted_frame_info.format = PP_DECRYPTEDFRAMEFORMAT_EMPTY; + break; case cdm::kNoKey: decrypted_frame_info.result = PP_DECRYPTRESULT_DECRYPT_NOKEY; break; diff --git a/webkit/media/crypto/ppapi/clear_key_cdm.cc b/webkit/media/crypto/ppapi/clear_key_cdm.cc index 6f7ea092..53ba112 100644 --- a/webkit/media/crypto/ppapi/clear_key_cdm.cc +++ b/webkit/media/crypto/ppapi/clear_key_cdm.cc @@ -11,6 +11,34 @@ #include "base/time.h" #include "media/base/decoder_buffer.h" +#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) +#include "base/at_exit.h" +#include "base/file_path.h" +#include "base/path_service.h" +#include "media/base/media.h" +#include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h" + +// TODO(tomfinegan): When COMPONENT_BUILD is not defined an AtExitManager must +// exist before the call to InitializeFFmpegLibraries(). This should no longer +// be required after http://crbug.com/91970 because we'll be able to get rid of +// InitializeFFmpegLibraries(). +#if !defined COMPONENT_BUILD +static base::AtExitManager g_at_exit_manager; +#endif + +// TODO(tomfinegan): InitializeFFmpegLibraries() and |g_cdm_module_initialized| +// are required for running in the sandbox, and should no longer be required +// after http://crbug.com/91970 is fixed. +static bool InitializeFFmpegLibraries() { + FilePath file_path; + CHECK(PathService::Get(base::DIR_EXE, &file_path)); + CHECK(media::InitializeMediaLibrary(file_path)); + return true; +} + +static bool g_cdm_module_initialized = InitializeFFmpegLibraries(); +#endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER + static const char kClearKeyCdmVersion[] = "0.1.0.0"; static scoped_refptr<media::DecoderBuffer> CopyDecoderBufferFrom( @@ -65,10 +93,12 @@ static Type* AllocateAndCopy(const Type* data, int size) { cdm::ContentDecryptionModule* CreateCdmInstance( cdm::Allocator* allocator, cdm::CdmHost* host) { + DVLOG(1) << "CreateCdmInstance()"; return new webkit_media::ClearKeyCdm(allocator, host); } void DestroyCdmInstance(cdm::ContentDecryptionModule* instance) { + DVLOG(1) << "DestroyCdmInstance()"; delete instance; } @@ -244,28 +274,43 @@ cdm::Status ClearKeyCdm::InitializeAudioDecoder( cdm::Status ClearKeyCdm::InitializeVideoDecoder( const cdm::VideoDecoderConfig& video_decoder_config) { -#if !defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) - NOTIMPLEMENTED(); - return cdm::kSessionError; -#else +#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) + if (!video_decoder_) + video_decoder_.reset(new webkit_media::FFmpegCdmVideoDecoder(allocator_)); + + if (!video_decoder_->Initialize(video_decoder_config)) + return cdm::kSessionError; + + return cdm::kSuccess; +#elif defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) video_size_ = video_decoder_config.coded_size; return cdm::kSuccess; -#endif // CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER +#else + NOTIMPLEMENTED(); + return cdm::kSessionError; +#endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER + } -void ClearKeyCdm::ResetDecoder(cdm::StreamType) { - NOTIMPLEMENTED(); +void ClearKeyCdm::ResetDecoder(cdm::StreamType decoder_type) { +#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) + DCHECK(decoder_type == cdm::kStreamTypeVideo); + video_decoder_->Reset(); +#endif } -void ClearKeyCdm::DeinitializeDecoder(cdm::StreamType) { - NOTIMPLEMENTED(); +void ClearKeyCdm::DeinitializeDecoder(cdm::StreamType decoder_type) { +#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) + DCHECK(decoder_type == cdm::kStreamTypeVideo); + video_decoder_->Deinitialize(); +#endif } cdm::Status ClearKeyCdm::DecryptAndDecodeFrame( const cdm::InputBuffer& encrypted_buffer, - cdm::VideoFrame* video_frame) { + cdm::VideoFrame* decoded_frame) { if (!encrypted_buffer.data) { - video_frame->set_format(cdm::kEmptyVideoFrame); + decoded_frame->set_format(cdm::kEmptyVideoFrame); return cdm::kSuccess; } @@ -285,13 +330,20 @@ cdm::Status ClearKeyCdm::DecryptAndDecodeFrame( if (status == media::Decryptor::kNoKey) return cdm::kNoKey; -#if !defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) +#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) + DCHECK(status == media::Decryptor::kSuccess); + DCHECK(buffer); + return video_decoder_->DecodeFrame(buffer.get()->GetData(), + buffer->GetDataSize(), + encrypted_buffer.timestamp, + decoded_frame); +#elif defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) + GenerateFakeVideoFrame(decoder_buffer->GetTimestamp(), decoded_frame); + return cdm::kSuccess; +#else NOTIMPLEMENTED(); return cdm::kDecodeError; -#else - GenerateFakeVideoFrame(decoder_buffer->GetTimestamp(), video_frame); - return cdm::kSuccess; -#endif // CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER +#endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER } #if defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) diff --git a/webkit/media/crypto/ppapi/clear_key_cdm.h b/webkit/media/crypto/ppapi/clear_key_cdm.h index 8d185ff..e5a6cc7 100644 --- a/webkit/media/crypto/ppapi/clear_key_cdm.h +++ b/webkit/media/crypto/ppapi/clear_key_cdm.h @@ -10,6 +10,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" #include "base/synchronization/lock.h" #include "media/base/decryptor_client.h" #include "media/crypto/aes_decryptor.h" @@ -18,12 +19,18 @@ // Enable this to use the fake decoder for testing. // #define CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER +#if defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) +#undef CLEAR_KEY_CDM_USE_FFMPEG_DECODER +#endif + namespace media { class DecoderBuffer; } namespace webkit_media { +class FFmpegCdmVideoDecoder; + // Clear key implementation of the cdm::ContentDecryptionModule interface. class ClearKeyCdm : public cdm::ContentDecryptionModule { public: @@ -120,6 +127,10 @@ class ClearKeyCdm : public cdm::ContentDecryptionModule { cdm::Allocator* const allocator_; +#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) + scoped_ptr<FFmpegCdmVideoDecoder> video_decoder_; +#endif + #if defined(CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER) cdm::Size video_size_; #endif // CLEAR_KEY_CDM_USE_FAKE_VIDEO_DECODER diff --git a/webkit/media/crypto/ppapi/content_decryption_module.h b/webkit/media/crypto/ppapi/content_decryption_module.h index 4fd2ed9..7cfb5b2 100644 --- a/webkit/media/crypto/ppapi/content_decryption_module.h +++ b/webkit/media/crypto/ppapi/content_decryption_module.h @@ -166,7 +166,7 @@ struct VideoDecoderConfig { profile(kUnknownVideoCodecProfile), format(kUnknownVideoFormat), extra_data(NULL), - extra_data_size() {} + extra_data_size(0) {} VideoCodec codec; VideoCodecProfile profile; diff --git a/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc new file mode 100644 index 0000000..94d2af5 --- /dev/null +++ b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc @@ -0,0 +1,332 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h" + +#include "base/logging.h" +#include "base/memory/scoped_ptr.h" +#include "media/base/buffers.h" +#include "media/base/limits.h" +#include "media/ffmpeg/ffmpeg_common.h" +#include "webkit/media/crypto/ppapi/content_decryption_module.h" + +// Include FFmpeg header files. +extern "C" { +// Temporarily disable possible loss of data warning. +MSVC_PUSH_DISABLE_WARNING(4244); +#include <libavcodec/avcodec.h> +MSVC_POP_WARNING(); +} // extern "C" + +namespace webkit_media { + +static const int kDecodeThreads = 1; + +static cdm::VideoFormat PixelFormatToCdmVideoFormat(PixelFormat pixel_format) { + switch (pixel_format) { + case PIX_FMT_YUV420P: + return cdm::kYv12; + default: + DVLOG(1) << "Unsupported PixelFormat: " << pixel_format; + } + return cdm::kUnknownVideoFormat; +} + +static PixelFormat CdmVideoFormatToPixelFormat(cdm::VideoFormat video_format) { + switch (video_format) { + case cdm::kYv12: + case cdm::kI420: + return PIX_FMT_YUV420P; + case cdm::kUnknownVideoFormat: + default: + DVLOG(1) << "Unsupported cdm::VideoFormat: " << video_format; + } + return PIX_FMT_NONE; +} + +static CodecID CdmVideoCodecToCodecID( + cdm::VideoDecoderConfig::VideoCodec video_codec) { + switch (video_codec) { + case cdm::VideoDecoderConfig::kCodecVP8: + return CODEC_ID_VP8; + default: + NOTREACHED() << "Unsupported cdm::VideoCodec: " << video_codec; + } + return CODEC_ID_NONE; +} + +static void CdmVideoDecoderConfigToAVCodecContext( + const cdm::VideoDecoderConfig& config, + AVCodecContext* codec_context) { + codec_context->codec_type = AVMEDIA_TYPE_VIDEO; + codec_context->codec_id = CdmVideoCodecToCodecID(config.codec); + codec_context->profile = FF_PROFILE_UNKNOWN; + codec_context->coded_width = config.coded_size.width; + codec_context->coded_height = config.coded_size.height; + codec_context->pix_fmt = CdmVideoFormatToPixelFormat(config.format); + + if (config.extra_data) { + codec_context->extradata_size = config.extra_data_size; + codec_context->extradata = reinterpret_cast<uint8_t*>( + av_malloc(config.extra_data_size + FF_INPUT_BUFFER_PADDING_SIZE)); + memcpy(codec_context->extradata, config.extra_data, + config.extra_data_size); + memset(codec_context->extradata + config.extra_data_size, 0, + FF_INPUT_BUFFER_PADDING_SIZE); + } else { + codec_context->extradata = NULL; + codec_context->extradata_size = 0; + } + +} + +static void CopyPlane(const uint8_t* source, + int32_t source_stride, + int32_t target_stride, + int32_t rows, + int32_t copy_bytes_per_row, + uint8_t* target) { + DCHECK(source); + DCHECK(target); + DCHECK_LE(copy_bytes_per_row, source_stride); + DCHECK_LE(copy_bytes_per_row, target_stride); + + for (int i = 0; i < rows; ++i) { + const int source_offset = i * source_stride; + const int target_offset = i * target_stride; + memcpy(target + target_offset, + source + source_offset, + copy_bytes_per_row); + } +} + +FFmpegCdmVideoDecoder::FFmpegCdmVideoDecoder(cdm::Allocator* allocator) + : codec_context_(NULL), + av_frame_(NULL), + is_initialized_(false), + allocator_(allocator) { +} + +FFmpegCdmVideoDecoder::~FFmpegCdmVideoDecoder() { + ReleaseFFmpegResources(); +} + +bool FFmpegCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) { + DVLOG(1) << "Initialize()"; + + if (!IsValidOutputConfig(config.format, config.coded_size)) { + LOG(ERROR) << "Initialize(): invalid video decoder configuration."; + return false; + } + + if (is_initialized_) { + LOG(ERROR) << "Initialize(): Already initialized."; + return false; + } + + av_register_all(); + + // Release existing resources if necessary. + ReleaseFFmpegResources(); + + // Initialize AVCodecContext structure. + codec_context_ = avcodec_alloc_context3(NULL); + CdmVideoDecoderConfigToAVCodecContext(config, codec_context_); + + // Enable motion vector search (potentially slow), strong deblocking filter + // for damaged macroblocks, and set our error detection sensitivity. + codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK; + codec_context_->err_recognition = AV_EF_CAREFUL; + codec_context_->thread_count = kDecodeThreads; + codec_context_->opaque = this; + codec_context_->flags |= CODEC_FLAG_EMU_EDGE; + DCHECK_EQ(CODEC_ID_VP8, codec_context_->codec_id); + + AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); + if (!codec) { + LOG(ERROR) << "Initialize(): avcodec_find_decoder failed."; + return false; + } + + int status; + if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { + LOG(ERROR) << "Initialize(): avcodec_open2 failed: " << status; + return false; + } + + av_frame_ = avcodec_alloc_frame(); + is_initialized_ = true; + + return true; +} + +void FFmpegCdmVideoDecoder::Deinitialize() { + DVLOG(1) << "Deinitialize()"; + ReleaseFFmpegResources(); + is_initialized_ = false; +} + +void FFmpegCdmVideoDecoder::Reset() { + DVLOG(1) << "Reset()"; + avcodec_flush_buffers(codec_context_); +} + +// static +bool FFmpegCdmVideoDecoder::IsValidOutputConfig(cdm::VideoFormat format, + const cdm::Size& data_size) { + return ((format == cdm::kYv12 || format == cdm::kI420) && + (data_size.width % 2) == 0 && (data_size.height % 2) == 0 && + data_size.width > 0 && data_size.height > 0 && + data_size.width <= media::limits::kMaxDimension && + data_size.height <= media::limits::kMaxDimension && + data_size.width * data_size.height <= media::limits::kMaxCanvas); +} + +cdm::Status FFmpegCdmVideoDecoder::DecodeFrame( + const uint8_t* compressed_frame, + int32_t compressed_frame_size, + int64_t timestamp, + cdm::VideoFrame* decoded_frame) { + DVLOG(1) << "DecodeFrame()"; + DCHECK(decoded_frame); + + // Create a packet for input data. + AVPacket packet; + av_init_packet(&packet); + + // The FFmpeg API does not allow us to have const read-only pointers. + packet.data = const_cast<uint8_t*>(compressed_frame); + packet.size = compressed_frame_size; + + // Let FFmpeg handle presentation timestamp reordering. + codec_context_->reordered_opaque = timestamp; + + // Reset frame to default values. + avcodec_get_frame_defaults(av_frame_); + + // This is for codecs not using get_buffer to initialize + // |av_frame_->reordered_opaque| + av_frame_->reordered_opaque = codec_context_->reordered_opaque; + + int frame_decoded = 0; + int result = avcodec_decode_video2(codec_context_, + av_frame_, + &frame_decoded, + &packet); + // Log the problem when we can't decode a video frame and exit early. + if (result < 0) { + LOG(ERROR) << "DecodeFrame(): Error decoding video frame with timestamp: " + << timestamp << " us, packet size: " << packet.size << " bytes"; + return cdm::kDecodeError; + } + + // If no frame was produced then signal that more data is required to + // produce more frames. This can happen under two circumstances: + // 1) Decoder was recently initialized/flushed + // 2) End of stream was reached and all internal frames have been output + if (frame_decoded == 0) { + // There was an input frame, but FFmpeg did not produce an output frame. + // More input data is needed to produce output. + if (compressed_frame && compressed_frame_size > 0) + return cdm::kNeedMoreData; + + // No output frame was produced by FFmpeg, and there was no input data. + // The decoder has been flushed. + else + return cdm::kSuccess; + } + + // The decoder is in a bad state and not decoding correctly. + // Checking for NULL avoids a crash. + if (!av_frame_->data[cdm::VideoFrame::kYPlane] || + !av_frame_->data[cdm::VideoFrame::kUPlane] || + !av_frame_->data[cdm::VideoFrame::kVPlane]) { + LOG(ERROR) << "DecodeFrame(): Video frame has invalid frame data."; + return cdm::kDecodeError; + } + + if (!CopyAvFrameTo(decoded_frame)) { + LOG(ERROR) << "DecodeFrame() could not copy video frame to output buffer."; + return cdm::kDecodeError; + } + + return cdm::kSuccess; +} + +bool FFmpegCdmVideoDecoder::CopyAvFrameTo(cdm::VideoFrame* cdm_video_frame) { + DCHECK(cdm_video_frame); + DCHECK_EQ(av_frame_->format, PIX_FMT_YUV420P); + DCHECK_EQ(av_frame_->width % 2, 0); + DCHECK_EQ(av_frame_->height % 2, 0); + + const int y_size = av_frame_->width * av_frame_->height; + const int uv_size = y_size / 2; + const int space_required = y_size + (uv_size * 2); + + DCHECK(!cdm_video_frame->frame_buffer()); + cdm_video_frame->set_frame_buffer(allocator_->Allocate(space_required)); + if (!cdm_video_frame->frame_buffer()) { + LOG(ERROR) << "CopyAvFrameTo() cdm::Allocator::Allocate failed."; + return false; + } + + CopyPlane(av_frame_->base[cdm::VideoFrame::kYPlane], + av_frame_->linesize[cdm::VideoFrame::kYPlane], + av_frame_->width, + av_frame_->height, + av_frame_->width, + cdm_video_frame->frame_buffer()->data()); + + const int uv_stride = av_frame_->width / 2; + const int uv_rows = av_frame_->height / 2; + CopyPlane(av_frame_->base[cdm::VideoFrame::kUPlane], + av_frame_->linesize[cdm::VideoFrame::kUPlane], + uv_stride, + uv_rows, + uv_stride, + cdm_video_frame->frame_buffer()->data() + y_size); + + CopyPlane(av_frame_->base[cdm::VideoFrame::kVPlane], + av_frame_->linesize[cdm::VideoFrame::kVPlane], + uv_stride, + uv_rows, + uv_stride, + cdm_video_frame->frame_buffer()->data() + y_size + uv_size); + + PixelFormat format = static_cast<PixelFormat>(av_frame_->format); + cdm_video_frame->set_format(PixelFormatToCdmVideoFormat(format)); + + cdm::Size video_frame_size; + video_frame_size.width = av_frame_->width; + video_frame_size.height = av_frame_->height; + cdm_video_frame->set_size(video_frame_size); + + cdm_video_frame->set_plane_offset(cdm::VideoFrame::kYPlane, 0); + cdm_video_frame->set_plane_offset(cdm::VideoFrame::kUPlane, y_size); + cdm_video_frame->set_plane_offset(cdm::VideoFrame::kVPlane, + y_size + uv_size); + + cdm_video_frame->set_stride(cdm::VideoFrame::kYPlane, av_frame_->width); + cdm_video_frame->set_stride(cdm::VideoFrame::kUPlane, uv_stride); + cdm_video_frame->set_stride(cdm::VideoFrame::kVPlane, uv_stride); + + return true; +} + +void FFmpegCdmVideoDecoder::ReleaseFFmpegResources() { + DVLOG(1) << "ReleaseFFmpegResources()"; + + if (codec_context_) { + av_free(codec_context_->extradata); + avcodec_close(codec_context_); + av_free(codec_context_); + codec_context_ = NULL; + } + if (av_frame_) { + av_free(av_frame_); + av_frame_ = NULL; + } +} + +} // namespace webkit_media diff --git a/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h new file mode 100644 index 0000000..7fcfcb5 --- /dev/null +++ b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h @@ -0,0 +1,59 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef WEBKIT_MEDIA_CRYPTO_PPAPI_FFMPEG_CDM_VIDEO_DECODER_H_ +#define WEBKIT_MEDIA_CRYPTO_PPAPI_FFMPEG_CDM_VIDEO_DECODER_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "webkit/media/crypto/ppapi/content_decryption_module.h" + +struct AVCodecContext; +struct AVFrame; + +namespace webkit_media { + +class FFmpegCdmVideoDecoder { + public: + FFmpegCdmVideoDecoder(cdm::Allocator* allocator); + ~FFmpegCdmVideoDecoder(); + bool Initialize(const cdm::VideoDecoderConfig& config); + void Deinitialize(); + void Reset(); + + // Returns true when |format| and |data_size| specify a supported video + // output configuration. + static bool IsValidOutputConfig(cdm::VideoFormat format, + const cdm::Size& data_size); + + // Decodes |compressed_frame|. Stores output frame in |decoded_frame| and + // returns |cdm::kSuccess| when an output frame is available. Returns + // |cdm::kNeedMoreData| when |compressed_frame| does not produce an output + // frame. Returns |cdm::kDecodeError| when decoding fails. + cdm::Status DecodeFrame(const uint8_t* compressed_frame, + int32_t compressed_frame_size, + int64_t timestamp, + cdm::VideoFrame* decoded_frame); + + private: + // Allocates storage, then copies video frame stored in |av_frame_| to + // |cdm_video_frame|. Returns true when allocation and copy succeed. + bool CopyAvFrameTo(cdm::VideoFrame* cdm_video_frame); + + void ReleaseFFmpegResources(); + + // FFmpeg structures owned by this object. + AVCodecContext* codec_context_; + AVFrame* av_frame_; + + bool is_initialized_; + + cdm::Allocator* const allocator_; + + DISALLOW_COPY_AND_ASSIGN(FFmpegCdmVideoDecoder); +}; + +} // namespace webkit_media + +#endif // WEBKIT_MEDIA_CRYPTO_PPAPI_FFMPEG_CDM_VIDEO_DECODER_H_ |