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_video_decoder.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "media/base/buffers.h" |
| 10 #include "media/base/limits.h" |
| 11 #include "media/ffmpeg/ffmpeg_common.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 static const int kDecodeThreads = 1; |
| 25 |
| 26 static cdm::VideoFormat PixelFormatToCdmVideoFormat(PixelFormat pixel_format) { |
| 27 switch (pixel_format) { |
| 28 case PIX_FMT_YUV420P: |
| 29 return cdm::kYv12; |
| 30 default: |
| 31 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format; |
| 32 } |
| 33 return cdm::kUnknownVideoFormat; |
| 34 } |
| 35 |
| 36 static PixelFormat CdmVideoFormatToPixelFormat(cdm::VideoFormat video_format) { |
| 37 switch (video_format) { |
| 38 case cdm::kYv12: |
| 39 case cdm::kI420: |
| 40 return PIX_FMT_YUV420P; |
| 41 case cdm::kUnknownVideoFormat: |
| 42 default: |
| 43 DVLOG(1) << "Unsupported cdm::VideoFormat: " << video_format; |
| 44 } |
| 45 return PIX_FMT_NONE; |
| 46 } |
| 47 |
| 48 static CodecID CdmVideoCodecToCodecID( |
| 49 cdm::VideoDecoderConfig::VideoCodec video_codec) { |
| 50 switch (video_codec) { |
| 51 case cdm::VideoDecoderConfig::kCodecVP8: |
| 52 return CODEC_ID_VP8; |
| 53 default: |
| 54 NOTREACHED() << "Unsupported cdm::VideoCodec: " << video_codec; |
| 55 } |
| 56 return CODEC_ID_NONE; |
| 57 } |
| 58 |
| 59 static void CdmVideoDecoderConfigToAVCodecContext( |
| 60 const cdm::VideoDecoderConfig& config, |
| 61 AVCodecContext* codec_context) { |
| 62 codec_context->codec_type = AVMEDIA_TYPE_VIDEO; |
| 63 codec_context->codec_id = CdmVideoCodecToCodecID(config.codec); |
| 64 codec_context->profile = FF_PROFILE_UNKNOWN; |
| 65 codec_context->coded_width = config.coded_size.width; |
| 66 codec_context->coded_height = config.coded_size.height; |
| 67 codec_context->pix_fmt = CdmVideoFormatToPixelFormat(config.format); |
| 68 |
| 69 if (config.extra_data) { |
| 70 codec_context->extradata_size = config.extra_data_size; |
| 71 codec_context->extradata = reinterpret_cast<uint8_t*>( |
| 72 av_malloc(config.extra_data_size + FF_INPUT_BUFFER_PADDING_SIZE)); |
| 73 memcpy(codec_context->extradata, config.extra_data, |
| 74 config.extra_data_size); |
| 75 memset(codec_context->extradata + config.extra_data_size, 0, |
| 76 FF_INPUT_BUFFER_PADDING_SIZE); |
| 77 } else { |
| 78 codec_context->extradata = NULL; |
| 79 codec_context->extradata_size = 0; |
| 80 } |
| 81 |
| 82 } |
| 83 |
| 84 static void CopyPlane(const uint8_t* source, |
| 85 int32_t source_stride, |
| 86 int32_t target_stride, |
| 87 int32_t rows, |
| 88 int32_t copy_bytes_per_row, |
| 89 uint8_t* target) { |
| 90 DCHECK(source); |
| 91 DCHECK(target); |
| 92 DCHECK_LE(copy_bytes_per_row, source_stride); |
| 93 DCHECK_LE(copy_bytes_per_row, target_stride); |
| 94 |
| 95 for (int i = 0; i < rows; ++i) { |
| 96 const int source_offset = i * source_stride; |
| 97 const int target_offset = i * target_stride; |
| 98 memcpy(target + target_offset, |
| 99 source + source_offset, |
| 100 copy_bytes_per_row); |
| 101 } |
| 102 } |
| 103 |
| 104 FFmpegCdmVideoDecoder::FFmpegCdmVideoDecoder(cdm::Allocator* allocator) |
| 105 : codec_context_(NULL), |
| 106 av_frame_(NULL), |
| 107 is_initialized_(false), |
| 108 allocator_(allocator) { |
| 109 } |
| 110 |
| 111 FFmpegCdmVideoDecoder::~FFmpegCdmVideoDecoder() { |
| 112 ReleaseFFmpegResources(); |
| 113 } |
| 114 |
| 115 bool FFmpegCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) { |
| 116 DVLOG(1) << "Initialize()"; |
| 117 |
| 118 if (!IsValidOutputConfig(config.format, config.coded_size)) { |
| 119 LOG(ERROR) << "Initialize(): invalid video decoder configuration."; |
| 120 return false; |
| 121 } |
| 122 |
| 123 if (is_initialized_) { |
| 124 LOG(ERROR) << "Initialize(): Already initialized."; |
| 125 return false; |
| 126 } |
| 127 |
| 128 av_register_all(); |
| 129 |
| 130 // Release existing resources if necessary. |
| 131 ReleaseFFmpegResources(); |
| 132 |
| 133 // Initialize AVCodecContext structure. |
| 134 codec_context_ = avcodec_alloc_context3(NULL); |
| 135 CdmVideoDecoderConfigToAVCodecContext(config, codec_context_); |
| 136 |
| 137 // Enable motion vector search (potentially slow), strong deblocking filter |
| 138 // for damaged macroblocks, and set our error detection sensitivity. |
| 139 codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK; |
| 140 codec_context_->err_recognition = AV_EF_CAREFUL; |
| 141 codec_context_->thread_count = kDecodeThreads; |
| 142 codec_context_->opaque = this; |
| 143 codec_context_->flags |= CODEC_FLAG_EMU_EDGE; |
| 144 DCHECK_EQ(CODEC_ID_VP8, codec_context_->codec_id); |
| 145 |
| 146 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| 147 if (!codec) { |
| 148 LOG(ERROR) << "Initialize(): avcodec_find_decoder failed."; |
| 149 return false; |
| 150 } |
| 151 |
| 152 int status; |
| 153 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { |
| 154 LOG(ERROR) << "Initialize(): avcodec_open2 failed: " << status; |
| 155 return false; |
| 156 } |
| 157 |
| 158 av_frame_ = avcodec_alloc_frame(); |
| 159 is_initialized_ = true; |
| 160 |
| 161 return true; |
| 162 } |
| 163 |
| 164 void FFmpegCdmVideoDecoder::Deinitialize() { |
| 165 DVLOG(1) << "Deinitialize()"; |
| 166 ReleaseFFmpegResources(); |
| 167 is_initialized_ = false; |
| 168 } |
| 169 |
| 170 void FFmpegCdmVideoDecoder::Reset() { |
| 171 DVLOG(1) << "Reset()"; |
| 172 avcodec_flush_buffers(codec_context_); |
| 173 } |
| 174 |
| 175 // static |
| 176 bool FFmpegCdmVideoDecoder::IsValidOutputConfig(cdm::VideoFormat format, |
| 177 const cdm::Size& data_size) { |
| 178 return ((format == cdm::kYv12 || format == cdm::kI420) && |
| 179 (data_size.width % 2) == 0 && (data_size.height % 2) == 0 && |
| 180 data_size.width > 0 && data_size.height > 0 && |
| 181 data_size.width <= media::limits::kMaxDimension && |
| 182 data_size.height <= media::limits::kMaxDimension && |
| 183 data_size.width * data_size.height <= media::limits::kMaxCanvas); |
| 184 } |
| 185 |
| 186 cdm::Status FFmpegCdmVideoDecoder::DecodeFrame( |
| 187 const uint8_t* compressed_frame, |
| 188 int32_t compressed_frame_size, |
| 189 int64_t timestamp, |
| 190 cdm::VideoFrame* decoded_frame) { |
| 191 DVLOG(1) << "DecodeFrame()"; |
| 192 DCHECK(decoded_frame); |
| 193 |
| 194 // Create a packet for input data. |
| 195 AVPacket packet; |
| 196 av_init_packet(&packet); |
| 197 |
| 198 // The FFmpeg API does not allow us to have const read-only pointers. |
| 199 packet.data = const_cast<uint8_t*>(compressed_frame); |
| 200 packet.size = compressed_frame_size; |
| 201 |
| 202 // Let FFmpeg handle presentation timestamp reordering. |
| 203 codec_context_->reordered_opaque = timestamp; |
| 204 |
| 205 // Reset frame to default values. |
| 206 avcodec_get_frame_defaults(av_frame_); |
| 207 |
| 208 // This is for codecs not using get_buffer to initialize |
| 209 // |av_frame_->reordered_opaque| |
| 210 av_frame_->reordered_opaque = codec_context_->reordered_opaque; |
| 211 |
| 212 int frame_decoded = 0; |
| 213 int result = avcodec_decode_video2(codec_context_, |
| 214 av_frame_, |
| 215 &frame_decoded, |
| 216 &packet); |
| 217 // Log the problem when we can't decode a video frame and exit early. |
| 218 if (result < 0) { |
| 219 LOG(ERROR) << "DecodeFrame(): Error decoding video frame with timestamp: " |
| 220 << timestamp << " us, packet size: " << packet.size << " bytes"; |
| 221 return cdm::kDecodeError; |
| 222 } |
| 223 |
| 224 // If no frame was produced then signal that more data is required to |
| 225 // produce more frames. This can happen under two circumstances: |
| 226 // 1) Decoder was recently initialized/flushed |
| 227 // 2) End of stream was reached and all internal frames have been output |
| 228 if (frame_decoded == 0) { |
| 229 // There was an input frame, but FFmpeg did not produce an output frame. |
| 230 // More input data is needed to produce output. |
| 231 if (compressed_frame && compressed_frame_size > 0) |
| 232 return cdm::kNeedMoreData; |
| 233 |
| 234 // No output frame was produced by FFmpeg, and there was no input data. |
| 235 // The decoder has been flushed. |
| 236 else |
| 237 return cdm::kSuccess; |
| 238 } |
| 239 |
| 240 // The decoder is in a bad state and not decoding correctly. |
| 241 // Checking for NULL avoids a crash. |
| 242 if (!av_frame_->data[cdm::VideoFrame::kYPlane] || |
| 243 !av_frame_->data[cdm::VideoFrame::kUPlane] || |
| 244 !av_frame_->data[cdm::VideoFrame::kVPlane]) { |
| 245 LOG(ERROR) << "DecodeFrame(): Video frame has invalid frame data."; |
| 246 return cdm::kDecodeError; |
| 247 } |
| 248 |
| 249 if (!CopyAvFrameTo(decoded_frame)) { |
| 250 LOG(ERROR) << "DecodeFrame() could not copy video frame to output buffer."; |
| 251 return cdm::kDecodeError; |
| 252 } |
| 253 |
| 254 return cdm::kSuccess; |
| 255 } |
| 256 |
| 257 bool FFmpegCdmVideoDecoder::CopyAvFrameTo(cdm::VideoFrame* cdm_video_frame) { |
| 258 DCHECK(cdm_video_frame); |
| 259 DCHECK_EQ(av_frame_->format, PIX_FMT_YUV420P); |
| 260 DCHECK_EQ(av_frame_->width % 2, 0); |
| 261 DCHECK_EQ(av_frame_->height % 2, 0); |
| 262 |
| 263 const int y_size = av_frame_->width * av_frame_->height; |
| 264 const int uv_size = y_size / 2; |
| 265 const int space_required = y_size + (uv_size * 2); |
| 266 |
| 267 DCHECK(!cdm_video_frame->frame_buffer()); |
| 268 cdm_video_frame->set_frame_buffer(allocator_->Allocate(space_required)); |
| 269 if (!cdm_video_frame->frame_buffer()) { |
| 270 LOG(ERROR) << "CopyAvFrameTo() cdm::Allocator::Allocate failed."; |
| 271 return false; |
| 272 } |
| 273 |
| 274 CopyPlane(av_frame_->base[cdm::VideoFrame::kYPlane], |
| 275 av_frame_->linesize[cdm::VideoFrame::kYPlane], |
| 276 av_frame_->width, |
| 277 av_frame_->height, |
| 278 av_frame_->width, |
| 279 cdm_video_frame->frame_buffer()->data()); |
| 280 |
| 281 const int uv_stride = av_frame_->width / 2; |
| 282 const int uv_rows = av_frame_->height / 2; |
| 283 CopyPlane(av_frame_->base[cdm::VideoFrame::kUPlane], |
| 284 av_frame_->linesize[cdm::VideoFrame::kUPlane], |
| 285 uv_stride, |
| 286 uv_rows, |
| 287 uv_stride, |
| 288 cdm_video_frame->frame_buffer()->data() + y_size); |
| 289 |
| 290 CopyPlane(av_frame_->base[cdm::VideoFrame::kVPlane], |
| 291 av_frame_->linesize[cdm::VideoFrame::kVPlane], |
| 292 uv_stride, |
| 293 uv_rows, |
| 294 uv_stride, |
| 295 cdm_video_frame->frame_buffer()->data() + y_size + uv_size); |
| 296 |
| 297 PixelFormat format = static_cast<PixelFormat>(av_frame_->format); |
| 298 cdm_video_frame->set_format(PixelFormatToCdmVideoFormat(format)); |
| 299 |
| 300 cdm::Size video_frame_size; |
| 301 video_frame_size.width = av_frame_->width; |
| 302 video_frame_size.height = av_frame_->height; |
| 303 cdm_video_frame->set_size(video_frame_size); |
| 304 |
| 305 cdm_video_frame->set_plane_offset(cdm::VideoFrame::kYPlane, 0); |
| 306 cdm_video_frame->set_plane_offset(cdm::VideoFrame::kUPlane, y_size); |
| 307 cdm_video_frame->set_plane_offset(cdm::VideoFrame::kVPlane, |
| 308 y_size + uv_size); |
| 309 |
| 310 cdm_video_frame->set_stride(cdm::VideoFrame::kYPlane, av_frame_->width); |
| 311 cdm_video_frame->set_stride(cdm::VideoFrame::kUPlane, uv_stride); |
| 312 cdm_video_frame->set_stride(cdm::VideoFrame::kVPlane, uv_stride); |
| 313 |
| 314 return true; |
| 315 } |
| 316 |
| 317 void FFmpegCdmVideoDecoder::ReleaseFFmpegResources() { |
| 318 DVLOG(1) << "ReleaseFFmpegResources()"; |
| 319 |
| 320 if (codec_context_) { |
| 321 av_free(codec_context_->extradata); |
| 322 avcodec_close(codec_context_); |
| 323 av_free(codec_context_); |
| 324 codec_context_ = NULL; |
| 325 } |
| 326 if (av_frame_) { |
| 327 av_free(av_frame_); |
| 328 av_frame_ = NULL; |
| 329 } |
| 330 } |
| 331 |
| 332 } // namespace webkit_media |
OLD | NEW |