Chromium Code Reviews| Index: webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc | 
| 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 0000000000000000000000000000000000000000..419224018bd291433baffeca19948ea3609ecfc5 | 
| --- /dev/null | 
| +++ b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc | 
| @@ -0,0 +1,320 @@ | 
| +// 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/ref_counted.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 "webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.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 { | 
| + | 
| +namespace { | 
| + | 
| +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; | 
| +} | 
| + | 
| +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; | 
| +} | 
| + | 
| +CodecID CdmVideoCodecToCodecID( | 
| + cdm::VideoDecoderConfig::VideoCodec video_codec) { | 
| + switch (video_codec) { | 
| + case cdm::VideoDecoderConfig::kCodecVP8: | 
| + return CODEC_ID_VP8; | 
| + case cdm::kUnknownVideoFormat: | 
| + default: | 
| + DVLOG(1) << "Unsupported cdm::VideoCodec: " << video_codec; | 
| + } | 
| + NOTREACHED() << "Unsupported video_codec: " << video_codec; | 
| + return CODEC_ID_NONE; | 
| +} | 
| + | 
| +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); | 
| + codec_context->extradata = config.extra_data; | 
| + codec_context->extradata_size = config.extra_data_size; | 
| +} | 
| + | 
| +cdm::VideoFrame::VideoPlane VideoPlane(int32_t plane_index) { | 
| + DCHECK(plane_index >= cdm::VideoFrame::kYPlane && | 
| + plane_index < cdm::VideoFrame::kMaxPlanes); | 
| + return static_cast<cdm::VideoFrame::VideoPlane>(plane_index); | 
| +} | 
| + | 
| +uint8_t* PlanePointer(FFmpegCdmVideoFrame* frame, int plane_index) { | 
| + DCHECK(frame); | 
| + return frame->frame_buffer()->data() + | 
| + frame->plane_offset(VideoPlane(plane_index)); | 
| +} | 
| + | 
| +} // namespace | 
| + | 
| +bool IsValidConfig(cdm::VideoFormat format, const cdm::Size& data_size) { | 
| + return ((format == cdm::kYv12 || format == cdm::kI420) && | 
| + 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); | 
| +} | 
| + | 
| +static int GetCdmVideoBufferImpl(AVCodecContext* s, AVFrame* frame) { | 
| 
 
xhwang
2012/10/16 21:59:53
We are mixing anonymous namespace and static funct
 
Tom Finegan
2012/10/17 02:39:10
GetCdmVideoBufferImpl needs to be non-static to be
 
Tom Finegan
2012/10/17 04:25:29
Back to static and a public GetVideoBuffer().
 
 | 
| + FFmpegCdmVideoDecoder* vd = static_cast<FFmpegCdmVideoDecoder*>(s->opaque); | 
| + return vd->GetVideoBuffer(s, frame); | 
| +} | 
| + | 
| +static void ReleaseCdmVideoBufferImpl(AVCodecContext* s, AVFrame* frame) { | 
| + scoped_ptr<FFmpegCdmVideoFrame> cdm_video_frame( | 
| + reinterpret_cast<FFmpegCdmVideoFrame*>(frame->opaque)); | 
| + | 
| + // The FFmpeg API expects us to zero the data pointers in | 
| + // this callback | 
| + memset(frame->data, 0, sizeof(frame->data)); | 
| + frame->opaque = NULL; | 
| +} | 
| + | 
| +FFmpegCdmVideoDecoder::FFmpegCdmVideoDecoder(cdm::Allocator* allocator) | 
| + : codec_context_(NULL), | 
| + av_frame_(NULL), | 
| + allocator_(allocator) { | 
| +} | 
| + | 
| +FFmpegCdmVideoDecoder::~FFmpegCdmVideoDecoder() { | 
| + ReleaseFFmpegResources(); | 
| +} | 
| + | 
| +bool FFmpegCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) { | 
| + if (!IsValidConfig(config.format, config.coded_size)) { | 
| + DLOG(ERROR) << "Invalid VideoDecoderConfig."; | 
| + 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; | 
| + codec_context_->get_buffer = GetCdmVideoBufferImpl; | 
| + codec_context_->release_buffer = ReleaseCdmVideoBufferImpl; | 
| + DCHECK_EQ(CODEC_ID_VP8, codec_context_->codec_id); | 
| + | 
| + AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 
| + if (!codec) { | 
| + LOG(ERROR) << "avcodec_find_decoder failed."; | 
| + return false; | 
| + } | 
| + | 
| + int status; | 
| + if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { | 
| + LOG(ERROR) << "avcodec_open2 failed: " << status; | 
| + return false; | 
| + } | 
| + | 
| + av_frame_ = avcodec_alloc_frame(); | 
| + config_ = config; | 
| + | 
| + // TODO(tomfinegan): Do we need to copy the extra data? Zeroing the members | 
| 
 
xhwang
2012/10/16 21:59:53
We don't. extra_data is only useful for initializa
 
Tom Finegan
2012/10/17 02:39:10
We don't need our own copy, but we do need to make
 
xhwang
2012/10/17 18:43:00
Yes, so after we make the copy we don't need to sa
 
 | 
| + // because we don't own the memory. | 
| + config_.extra_data = NULL; | 
| + config_.extra_data_size = 0; | 
| + | 
| + return true; | 
| +} | 
| + | 
| +void FFmpegCdmVideoDecoder::Deinitialize() { | 
| + ReleaseFFmpegResources(); | 
| +} | 
| + | 
| +void FFmpegCdmVideoDecoder::Reset() { | 
| + avcodec_flush_buffers(codec_context_); | 
| 
 
xhwang
2012/10/16 21:59:53
Do we want to have a flag or state to remember thi
 
Tom Finegan
2012/10/17 02:39:10
Discussed offline.
 
 | 
| +} | 
| + | 
| +void MoveVideoFrame(FFmpegCdmVideoFrame* source, cdm::VideoFrame* target) { | 
| 
 
ddorwin
2012/10/16 18:04:30
This should be above with the other static/anonymo
 
Tom Finegan
2012/10/16 21:44:44
Agreed and done. Not sure if MoveVideoFrame() is s
 
 | 
| + DCHECK(source && source->frame_buffer()); | 
| + DCHECK(target); | 
| + target->set_format(source->format()); | 
| + target->set_frame_buffer(source->frame_buffer()); | 
| + | 
| + typedef cdm::VideoFrame::VideoPlane VideoPlane; | 
| + const int kMaxPlanes = cdm::VideoFrame::kMaxPlanes; | 
| + for (int i = 0; static_cast<VideoPlane>(i) < kMaxPlanes; ++i) { | 
| 
 
xhwang
2012/10/16 21:59:53
kMaxPlanes is an int. Why do we need the static_ca
 
Tom Finegan
2012/10/17 02:39:10
kMaxPlanes was an int, it's part of VideoPlanes no
 
Tom Finegan
2012/10/17 04:25:29
Totally misunderstood this the first time. Fixed i
 
 | 
| + cdm::VideoFrame::VideoPlane plane = static_cast<VideoPlane>(i); | 
| + target->set_plane_offset(plane, source->plane_offset(plane)); | 
| + target->set_stride(plane, source->stride(plane)); | 
| + } | 
| + | 
| + source->ReleaseCdmBuffer(); | 
| +} | 
| + | 
| +bool FFmpegCdmVideoDecoder::DecodeFrame(const uint8_t* compressed_frame, | 
| + int32_t compressed_frame_size, | 
| + int64_t timestamp, | 
| + cdm::VideoFrame* decoded_frame) { | 
| + DCHECK(decoded_frame); | 
| + | 
| + // Create a packet for input data. | 
| + // Due to FFmpeg API changes we no longer have const read-only pointers. | 
| 
 
xhwang
2012/10/16 21:59:53
put this above line 207?
 
Tom Finegan
2012/10/17 02:39:10
Done, and reworded it.
 
 | 
| + AVPacket packet; | 
| + av_init_packet(&packet); | 
| + 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 a video frame with timestamp: " | 
| + << timestamp << " us, packet size: " << packet.size << " bytes"; | 
| + return false; | 
| + } | 
| + | 
| + // 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) | 
| + return true; | 
| + | 
| + // 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) << "Video frame was produced yet has invalid frame data."; | 
| + return false; | 
| + } | 
| + | 
| + if (!av_frame_->opaque) { | 
| + LOG(ERROR) << "VideoFrame object associated with frame data not set."; | 
| + return false; | 
| + } | 
| + | 
| + scoped_ptr<FFmpegCdmVideoFrame> video_frame( | 
| + static_cast<FFmpegCdmVideoFrame*>(av_frame_->opaque)); | 
| + DCHECK(video_frame->frame_buffer()); | 
| + MoveVideoFrame(video_frame.get(), decoded_frame); | 
| + | 
| + return true; | 
| +} | 
| + | 
| +int FFmpegCdmVideoDecoder::GetVideoBuffer(AVCodecContext* codec_context, | 
| + AVFrame* av_frame) { | 
| + // Don't use |codec_context_| here! With threaded decoding, | 
| + // it will contain unsynchronized width/height/pix_fmt values, | 
| + // whereas |codec_context| contains the current threads's | 
| + // updated width/height/pix_fmt, which can change for adaptive | 
| + // content. | 
| + cdm::VideoFormat format = | 
| + PixelFormatToCdmVideoFormat(codec_context->pix_fmt); | 
| + if (format == cdm::kUnknownVideoFormat) | 
| + return AVERROR(EINVAL); | 
| + DCHECK(format == cdm::kYv12 || format == cdm::kI420); | 
| + | 
| + cdm::Size size(codec_context->width, codec_context->height); | 
| + int ret; | 
| + if ((ret = av_image_check_size(size.width, size.height, 0, NULL)) < 0) | 
| + return ret; | 
| + | 
| + if (!IsValidConfig(format, size)) | 
| + return AVERROR(EINVAL); | 
| + | 
| + FFmpegCdmVideoFrame* video_frame = FFmpegCdmVideoFrame::Create(allocator_, | 
| + format, | 
| + size); | 
| + if (!video_frame) { | 
| + LOG(ERROR) << "GetVideoBuffer: VideoFrame Create failed"; | 
| + return AVERROR(ENOMEM); | 
| + } | 
| + | 
| + for (int i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) { | 
| + av_frame->base[i] = PlanePointer(video_frame, i); | 
| + av_frame->data[i] = PlanePointer(video_frame, i); | 
| + av_frame->linesize[i] = video_frame->stride(VideoPlane(i)); | 
| + } | 
| + | 
| + av_frame->opaque = reinterpret_cast<void*>(video_frame); | 
| + av_frame->type = FF_BUFFER_TYPE_USER; | 
| + av_frame->pkt_pts = codec_context->pkt ? codec_context->pkt->pts : | 
| + AV_NOPTS_VALUE; | 
| + av_frame->width = codec_context->width; | 
| + av_frame->height = codec_context->height; | 
| + av_frame->format = codec_context->pix_fmt; | 
| + | 
| + return 0; | 
| +} | 
| + | 
| +void FFmpegCdmVideoDecoder::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 |