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