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

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 namespace {
26
27 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 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 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 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 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 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 } // namespace
100
101 int GetCdmVideoBufferImpl(AVCodecContext* s, AVFrame* frame) {
ddorwin 2012/10/17 03:18:20 "s" is a bad name. Is that consistent with somethi
Tom Finegan 2012/10/17 04:25:29 From media. Renamed args.
102 DVLOG(3) << "GetCdmVideoBufferImpl()";
103 FFmpegCdmVideoDecoder* vd = static_cast<FFmpegCdmVideoDecoder*>(s->opaque);
104 return vd->GetVideoBuffer(s, frame);
105 }
106
107 static void ReleaseCdmVideoBufferImpl(AVCodecContext* s, AVFrame* frame) {
108 DVLOG(3) << "ReleaseCdmVideoBufferImpl()";
109
110 scoped_ptr<FFmpegCdmVideoFrame> cdm_video_frame(
111 reinterpret_cast<FFmpegCdmVideoFrame*>(frame->opaque));
112
113 // The FFmpeg API expects us to zero the data pointers in this callback.
114 memset(frame->data, 0, sizeof(frame->data));
115 frame->opaque = NULL;
116 }
117
118 FFmpegCdmVideoDecoder::FFmpegCdmVideoDecoder(cdm::Allocator* allocator)
119 : codec_context_(NULL),
120 av_frame_(NULL),
121 is_initialized_(false),
122 allocator_(allocator) {
123 }
124
125 FFmpegCdmVideoDecoder::~FFmpegCdmVideoDecoder() {
126 ReleaseFFmpegResources();
127 }
128
129 bool FFmpegCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) {
130 DVLOG(1) << "Initialize()";
131
132 if (is_initialized_) {
133 DLOG(ERROR) << "Already initialized.";
134 return false;
135 }
136
137 if (!FFmpegCdmVideoFrame::IsValidConfig(config.format, config.coded_size)) {
138 DLOG(ERROR) << "Invalid VideoDecoderConfig.";
139 return false;
140 }
141
142 av_register_all();
143
144 // Release existing resources if necessary.
145 ReleaseFFmpegResources();
146
147 // Initialize AVCodecContext structure.
148 codec_context_ = avcodec_alloc_context3(NULL);
149 CdmVideoDecoderConfigToAVCodecContext(config, codec_context_);
150
151 // Enable motion vector search (potentially slow), strong deblocking filter
152 // for damaged macroblocks, and set our error detection sensitivity.
153 codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
154 codec_context_->err_recognition = AV_EF_CAREFUL;
155 codec_context_->thread_count = kDecodeThreads;
156 codec_context_->opaque = this;
157 codec_context_->flags |= CODEC_FLAG_EMU_EDGE;
158 codec_context_->get_buffer = GetCdmVideoBufferImpl;
159 codec_context_->release_buffer = ReleaseCdmVideoBufferImpl;
160 DCHECK_EQ(CODEC_ID_VP8, codec_context_->codec_id);
161
162 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
163 if (!codec) {
164 LOG(ERROR) << "avcodec_find_decoder failed.";
165 return false;
166 }
167
168 int status;
169 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) {
170 LOG(ERROR) << "avcodec_open2 failed: " << status;
171 return false;
172 }
173
174 av_frame_ = avcodec_alloc_frame();
175 config_ = config;
176
177 // TODO(tomfinegan): Do we need to copy the extra data? Zeroing the members
178 // because we don't own the memory.
179 config_.extra_data = NULL;
180 config_.extra_data_size = 0;
181
182 is_initialized_ = true;
183
184 return true;
185 }
186
187 void FFmpegCdmVideoDecoder::Deinitialize() {
188 DVLOG(1) << "Deinitialize()";
189 ReleaseFFmpegResources();
190 }
191
192 void FFmpegCdmVideoDecoder::Reset() {
193 DVLOG(1) << "Reset()";
194 avcodec_flush_buffers(codec_context_);
195 }
196
197 cdm::Status FFmpegCdmVideoDecoder::DecodeFrame(
198 const uint8_t* compressed_frame,
199 int32_t compressed_frame_size,
200 int64_t timestamp,
201 cdm::VideoFrame* decoded_frame) {
202 DVLOG(1) << "DecodeFrame()";
203 DCHECK(decoded_frame);
204
205 // Create a packet for input data.
206 AVPacket packet;
207 av_init_packet(&packet);
208
209 // The FFmpeg API changes does not allow us to have const read-only pointers.
210 packet.data = const_cast<uint8_t*>(compressed_frame);
211 packet.size = compressed_frame_size;
212
213 // Let FFmpeg handle presentation timestamp reordering.
214 codec_context_->reordered_opaque = timestamp;
215
216 // Reset frame to default values.
217 avcodec_get_frame_defaults(av_frame_);
218
219 // This is for codecs not using get_buffer to initialize
220 // |av_frame_->reordered_opaque|
221 av_frame_->reordered_opaque = codec_context_->reordered_opaque;
222
223 int frame_decoded = 0;
224 int result = avcodec_decode_video2(codec_context_,
225 av_frame_,
226 &frame_decoded,
227 &packet);
228 // Log the problem when we can't decode a video frame and exit early.
229 if (result < 0) {
230 LOG(ERROR) << "DecodeFrame: Error decoding a video frame with timestamp: "
231 << timestamp << " us, packet size: " << packet.size << " bytes";
232 return cdm::kDecodeError;
233 }
234
235 // If no frame was produced then signal that more data is required to
236 // produce more frames. This can happen under two circumstances:
237 // 1) Decoder was recently initialized/flushed
238 // 2) End of stream was reached and all internal frames have been output
239 if (frame_decoded == 0)
240 return cdm::kNeedMoreData;
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) << "Video frame was produced yet has invalid frame data.";
248 return cdm::kDecodeError;
249 }
250
251 if (!av_frame_->opaque) {
252 LOG(ERROR) << "FFmpegCdmVideoFrame object associated with frame missing.";
253 return cdm::kDecodeError;
254 }
255
256 scoped_ptr<FFmpegCdmVideoFrame> video_frame(
257 static_cast<FFmpegCdmVideoFrame*>(av_frame_->opaque));
258 DCHECK(video_frame->frame_buffer());
259 video_frame->MoveVideoFrame(decoded_frame);
260 return cdm::kSuccess;
261 }
262
263 int FFmpegCdmVideoDecoder::GetVideoBuffer(AVCodecContext* codec_context,
264 AVFrame* av_frame) {
265 DVLOG(3) << "GetVideoBuffer()";
266
267 // Don't use |codec_context_| here! With threaded decoding,
268 // it will contain unsynchronized width/height/pix_fmt values,
269 // whereas |codec_context| contains the current threads's
270 // updated width/height/pix_fmt, which can change for adaptive
271 // content.
272 cdm::VideoFormat format =
273 PixelFormatToCdmVideoFormat(codec_context->pix_fmt);
274 if (format == cdm::kUnknownVideoFormat)
275 return AVERROR(EINVAL);
276 DCHECK(format == cdm::kYv12 || format == cdm::kI420);
277
278 cdm::Size size(codec_context->width, codec_context->height);
279 int ret;
280 if ((ret = av_image_check_size(size.width, size.height, 0, NULL)) < 0)
281 return ret;
282
283 if (!FFmpegCdmVideoFrame::IsValidConfig(format, size))
284 return AVERROR(EINVAL);
285
286 FFmpegCdmVideoFrame* video_frame = FFmpegCdmVideoFrame::Create(allocator_,
287 format,
288 size);
289 if (!video_frame) {
290 LOG(ERROR) << "GetVideoBuffer: VideoFrame Create failed";
291 return AVERROR(ENOMEM);
292 }
293
294 for (int i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) {
295 av_frame->base[i] = PlanePointer(video_frame, i);
296 av_frame->data[i] = PlanePointer(video_frame, i);
297 av_frame->linesize[i] = video_frame->stride(VideoPlane(i));
298 }
299
300 av_frame->opaque = reinterpret_cast<void*>(video_frame);
301 av_frame->type = FF_BUFFER_TYPE_USER;
302 av_frame->pkt_pts = codec_context->pkt ? codec_context->pkt->pts :
303 AV_NOPTS_VALUE;
304 av_frame->width = codec_context->width;
305 av_frame->height = codec_context->height;
306 av_frame->format = codec_context->pix_fmt;
307
308 return 0;
309 }
310
311 void FFmpegCdmVideoDecoder::ReleaseFFmpegResources() {
312 DVLOG(1) << "ReleaseFFmpegResources()";
313
314 if (codec_context_) {
315 av_free(codec_context_->extradata);
316 avcodec_close(codec_context_);
317 av_free(codec_context_);
318 codec_context_ = NULL;
319 }
320 if (av_frame_) {
321 av_free(av_frame_);
322 av_frame_ = NULL;
323 }
324
325 is_initialized_ = false;
326 }
327
328 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698