Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.cc

Issue 10899021: Add CDM video decoder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address more comments. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.h"
14
15 // Include FFmpeg header files.
16 extern "C" {
17 // Temporarily disable possible loss of data warning.
18 MSVC_PUSH_DISABLE_WARNING(4244);
19 #include <libavcodec/avcodec.h>
20 MSVC_POP_WARNING();
21 } // extern "C"
22
23 namespace webkit_media {
24
25 static const int kDecodeThreads = 1;
26
27 static cdm::VideoFormat PixelFormatToCdmVideoFormat(PixelFormat pixel_format) {
28 switch (pixel_format) {
29 case PIX_FMT_YUV420P:
30 return cdm::kYv12;
31 default:
32 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format;
33 }
34 return cdm::kUnknownVideoFormat;
35 }
36
37 static PixelFormat CdmVideoFormatToPixelFormat(cdm::VideoFormat video_format) {
38 switch (video_format) {
39 case cdm::kYv12:
40 case cdm::kI420:
41 return PIX_FMT_YUV420P;
42 case cdm::kUnknownVideoFormat:
43 default:
44 DVLOG(1) << "Unsupported cdm::VideoFormat: " << video_format;
45 }
46 return PIX_FMT_NONE;
47 }
48
49 static CodecID CdmVideoCodecToCodecID(
50 cdm::VideoDecoderConfig::VideoCodec video_codec) {
51 switch (video_codec) {
52 case cdm::VideoDecoderConfig::kCodecVP8:
53 return CODEC_ID_VP8;
54 case cdm::kUnknownVideoFormat:
55 default:
56 DVLOG(1) << "Unsupported cdm::VideoCodec: " << video_codec;
57 }
58 NOTREACHED() << "Unsupported video_codec: " << video_codec;
59 return CODEC_ID_NONE;
60 }
61
62 static void CdmVideoDecoderConfigToAVCodecContext(
63 const cdm::VideoDecoderConfig& config,
64 AVCodecContext* codec_context) {
65 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
66 codec_context->codec_id = CdmVideoCodecToCodecID(config.codec);
67 codec_context->profile = FF_PROFILE_UNKNOWN;
68 codec_context->coded_width = config.coded_size.width;
69 codec_context->coded_height = config.coded_size.height;
70 codec_context->pix_fmt = CdmVideoFormatToPixelFormat(config.format);
71
72 if (config.extra_data) {
73 codec_context->extradata_size = config.extra_data_size;
74 codec_context->extradata = reinterpret_cast<uint8_t*>(
75 av_malloc(config.extra_data_size + FF_INPUT_BUFFER_PADDING_SIZE));
76 memcpy(codec_context->extradata, config.extra_data,
77 config.extra_data_size);
78 memset(codec_context->extradata + config.extra_data_size, 0,
79 FF_INPUT_BUFFER_PADDING_SIZE);
80 } else {
81 codec_context->extradata = NULL;
82 codec_context->extradata_size = 0;
83 }
84
85 }
86
87 static cdm::VideoFrame::VideoPlane VideoPlane(int32_t plane_index) {
88 DCHECK(plane_index >= cdm::VideoFrame::kYPlane &&
89 plane_index < cdm::VideoFrame::kMaxPlanes);
90 return static_cast<cdm::VideoFrame::VideoPlane>(plane_index);
91 }
92
93 static uint8_t* PlanePointer(FFmpegCdmVideoFrame* frame, int plane_index) {
94 DCHECK(frame);
95 return frame->frame_buffer()->data() +
96 frame->plane_offset(VideoPlane(plane_index));
97 }
98
99 static int GetCdmVideoBufferImpl(AVCodecContext* context,
100 AVFrame* frame) {
101 DVLOG(3) << "GetCdmVideoBufferImpl()";
102 FFmpegCdmVideoDecoder* video_decoder =
103 static_cast<FFmpegCdmVideoDecoder*>(context->opaque);
104 DCHECK(video_decoder);
105 return video_decoder->GetVideoBuffer(context, frame);
106 }
107
108 static void ReleaseCdmVideoBufferImpl(AVCodecContext* /* context */,
109 AVFrame* frame) {
110 DVLOG(3) << "ReleaseCdmVideoBufferImpl()";
111
112 scoped_ptr<FFmpegCdmVideoFrame> cdm_video_frame(
113 reinterpret_cast<FFmpegCdmVideoFrame*>(frame->opaque));
114
115 // The FFmpeg API expects us to zero the data pointers in this callback.
116 memset(frame->data, 0, sizeof(frame->data));
117 frame->opaque = NULL;
118 }
119
120 FFmpegCdmVideoDecoder::FFmpegCdmVideoDecoder(cdm::Allocator* allocator)
121 : codec_context_(NULL),
122 av_frame_(NULL),
123 is_initialized_(false),
124 allocator_(allocator) {
125 }
126
127 FFmpegCdmVideoDecoder::~FFmpegCdmVideoDecoder() {
128 ReleaseFFmpegResources();
129 }
130
131 bool FFmpegCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) {
132 DVLOG(1) << "Initialize()";
133
134 if (is_initialized_) {
135 DLOG(ERROR) << "Already initialized.";
136 return false;
137 }
138
139 if (!FFmpegCdmVideoFrame::IsValidConfig(config.format, config.coded_size)) {
140 DLOG(ERROR) << "Invalid VideoDecoderConfig.";
141 return false;
142 }
143
144 av_register_all();
145
146 // Release existing resources if necessary.
147 ReleaseFFmpegResources();
148
149 // Initialize AVCodecContext structure.
150 codec_context_ = avcodec_alloc_context3(NULL);
151 CdmVideoDecoderConfigToAVCodecContext(config, codec_context_);
152
153 // Enable motion vector search (potentially slow), strong deblocking filter
154 // for damaged macroblocks, and set our error detection sensitivity.
155 codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
156 codec_context_->err_recognition = AV_EF_CAREFUL;
157 codec_context_->thread_count = kDecodeThreads;
158 codec_context_->opaque = this;
159 codec_context_->flags |= CODEC_FLAG_EMU_EDGE;
160 codec_context_->get_buffer = GetCdmVideoBufferImpl;
161 codec_context_->release_buffer = ReleaseCdmVideoBufferImpl;
162 DCHECK_EQ(CODEC_ID_VP8, codec_context_->codec_id);
163
164 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
165 if (!codec) {
166 LOG(ERROR) << "avcodec_find_decoder failed.";
167 return false;
168 }
169
170 int status;
171 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) {
172 LOG(ERROR) << "avcodec_open2 failed: " << status;
173 return false;
174 }
175
176 av_frame_ = avcodec_alloc_frame();
177 config_ = config;
xhwang 2012/10/17 20:23:10 Copy from previous review: Yes, so after we make t
Tom Finegan 2012/10/17 21:32:22 Right, and done, really this time. I kind of suck
178
179 // TODO(tomfinegan): Do we need to copy the extra data? Zeroing the members
180 // because we don't own the memory.
181 config_.extra_data = NULL;
182 config_.extra_data_size = 0;
183
184 is_initialized_ = true;
185
186 return true;
187 }
188
189 void FFmpegCdmVideoDecoder::Deinitialize() {
190 DVLOG(1) << "Deinitialize()";
191 ReleaseFFmpegResources();
192 is_initialized_ = false;
193 }
194
195 void FFmpegCdmVideoDecoder::Reset() {
196 DVLOG(1) << "Reset()";
197 avcodec_flush_buffers(codec_context_);
198 }
199
200 cdm::Status FFmpegCdmVideoDecoder::DecodeFrame(
201 const uint8_t* compressed_frame,
202 int32_t compressed_frame_size,
203 int64_t timestamp,
204 cdm::VideoFrame* decoded_frame) {
205 DVLOG(1) << "DecodeFrame()";
206 DCHECK(decoded_frame);
207
208 // Create a packet for input data.
209 AVPacket packet;
210 av_init_packet(&packet);
211
212 // The FFmpeg API changes does not allow us to have const read-only pointers.
213 packet.data = const_cast<uint8_t*>(compressed_frame);
214 packet.size = compressed_frame_size;
215
216 // Let FFmpeg handle presentation timestamp reordering.
217 codec_context_->reordered_opaque = timestamp;
218
219 // Reset frame to default values.
220 avcodec_get_frame_defaults(av_frame_);
221
222 // This is for codecs not using get_buffer to initialize
223 // |av_frame_->reordered_opaque|
224 av_frame_->reordered_opaque = codec_context_->reordered_opaque;
225
226 int frame_decoded = 0;
227 int result = avcodec_decode_video2(codec_context_,
228 av_frame_,
229 &frame_decoded,
230 &packet);
231 // Log the problem when we can't decode a video frame and exit early.
232 if (result < 0) {
233 LOG(ERROR) << "DecodeFrame: Error decoding a video frame with timestamp: "
234 << timestamp << " us, packet size: " << packet.size << " bytes";
235 return cdm::kDecodeError;
236 }
237
238 // If no frame was produced then signal that more data is required to
239 // produce more frames. This can happen under two circumstances:
240 // 1) Decoder was recently initialized/flushed
241 // 2) End of stream was reached and all internal frames have been output
242 if (frame_decoded == 0) {
243 // There was an input frame, but FFmpeg did not produce an output frame.
244 // More input data is needed to produce output.
245 if (compressed_frame && compressed_frame_size > 0)
246 return cdm::kNeedMoreData;
247
248 // No output frame was produced by FFmpeg, and there was no input data.
249 // The decoder has been flushed.
250 else
251 return cdm::kSuccess;
252 }
xhwang 2012/10/17 20:23:10 Thanks. We probably can remove kNeedMoreData later
Tom Finegan 2012/10/17 21:32:22 sgtm
253
254 // The decoder is in a bad state and not decoding correctly.
255 // Checking for NULL avoids a crash.
256 if (!av_frame_->data[cdm::VideoFrame::kYPlane] ||
257 !av_frame_->data[cdm::VideoFrame::kUPlane] ||
258 !av_frame_->data[cdm::VideoFrame::kVPlane]) {
259 LOG(ERROR) << "Video frame was produced yet has invalid frame data.";
260 return cdm::kDecodeError;
261 }
262
263 if (!av_frame_->opaque) {
264 LOG(ERROR) << "FFmpegCdmVideoFrame object associated with frame missing.";
265 return cdm::kDecodeError;
266 }
267
268 scoped_ptr<FFmpegCdmVideoFrame> video_frame(
269 static_cast<FFmpegCdmVideoFrame*>(av_frame_->opaque));
270 DCHECK(video_frame->frame_buffer());
271 video_frame->PassVideoFrame(decoded_frame);
272 decoded_frame->set_timestamp(av_frame_->reordered_opaque);
273 return cdm::kSuccess;
274 }
275
276 int FFmpegCdmVideoDecoder::GetVideoBuffer(AVCodecContext* codec_context,
277 AVFrame* av_frame) {
278 DVLOG(3) << "GetVideoBuffer()";
279
280 // Don't use |codec_context_| here! With threaded decoding,
281 // it will contain unsynchronized width/height/pix_fmt values,
282 // whereas |codec_context| contains the current threads's
283 // updated width/height/pix_fmt, which can change for adaptive
284 // content.
285 cdm::VideoFormat format =
286 PixelFormatToCdmVideoFormat(codec_context->pix_fmt);
287 if (format == cdm::kUnknownVideoFormat)
288 return AVERROR(EINVAL);
289 DCHECK(format == cdm::kYv12 || format == cdm::kI420);
290
291 cdm::Size size(codec_context->width, codec_context->height);
292 int ret;
293 if ((ret = av_image_check_size(size.width, size.height, 0, NULL)) < 0)
294 return ret;
295
296 if (!FFmpegCdmVideoFrame::IsValidConfig(format, size))
297 return AVERROR(EINVAL);
298
299 FFmpegCdmVideoFrame* video_frame = FFmpegCdmVideoFrame::Create(allocator_,
300 format,
301 size);
302 if (!video_frame) {
303 LOG(ERROR) << "GetVideoBuffer: VideoFrame Create failed";
304 return AVERROR(ENOMEM);
305 }
306
307 for (int i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) {
308 av_frame->base[i] = PlanePointer(video_frame, i);
309 av_frame->data[i] = PlanePointer(video_frame, i);
310 av_frame->linesize[i] = video_frame->stride(VideoPlane(i));
311 }
312
313 av_frame->opaque = reinterpret_cast<void*>(video_frame);
314 av_frame->type = FF_BUFFER_TYPE_USER;
315 av_frame->pkt_pts = codec_context->pkt ? codec_context->pkt->pts :
316 AV_NOPTS_VALUE;
317 av_frame->width = codec_context->width;
318 av_frame->height = codec_context->height;
319 av_frame->format = codec_context->pix_fmt;
320
321 return 0;
322 }
323
324 void FFmpegCdmVideoDecoder::ReleaseFFmpegResources() {
325 DVLOG(1) << "ReleaseFFmpegResources()";
326
327 if (codec_context_) {
328 av_free(codec_context_->extradata);
329 avcodec_close(codec_context_);
330 av_free(codec_context_);
331 codec_context_ = NULL;
332 }
333 if (av_frame_) {
334 av_free(av_frame_);
335 av_frame_ = NULL;
336 }
337 }
338
339 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698