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