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

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

Issue 10899021: Add CDM video decoder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Maybe sorta could work... 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 "base/logging.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "media/base/buffers.h"
9 #include "media/base/limits.h"
10 #include "media/ffmpeg/ffmpeg_common.h"
11 #include "webkit/media/crypto/ppapi/content_decryption_module.h"
12 #include "webkit/media/crypto/ppapi/ffmpeg_video_decoder.h"
13 #include "webkit/media/crypto/ppapi/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 {
24
25 cdm::VideoFormat PixelFormatToVideoFormat(PixelFormat pixel_format) {
26 switch (pixel_format) {
27 case PIX_FMT_YUV420P:
28 return cdm::kYv12;
29 default:
30 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format;
31 }
32 return cdm::kUnknownVideoFormat;
33 }
34
35 PixelFormat VideoFormatToPixelFormat(cdm::VideoFormat video_format) {
36 switch (video_format) {
37 case cdm::kYv12:
38 case cdm::kI420:
39 return PIX_FMT_YUV420P;
40 case cdm::kUnknownVideoFormat:
41 default:
42 DVLOG(1) << "Unsupported cdm::VideoFormat: " << video_format;
43 }
44 return PIX_FMT_NONE;
45 }
46
47 CodecID VideoCodecToCodecID(cdm::VideoDecoderConfig::VideoCodec video_codec) {
48 switch (video_codec) {
49 case cdm::VideoDecoderConfig::kCodecVP8:
50 return CODEC_ID_VP8;
51 case cdm::kUnknownVideoFormat:
52 default:
53 DVLOG(1) << "Unsupported cdm::VideoCodec: " << video_codec;
54 }
55 NOTREACHED() << "Unsupported video_codec: " << video_codec;
56 return CODEC_ID_NONE;
57 }
58
59 void VideoDecoderConfigToAVCodecContext(
60 const cdm::VideoDecoderConfig& config,
61 AVCodecContext* codec_context) {
62 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
63 codec_context->codec_id = VideoCodecToCodecID(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 = VideoFormatToPixelFormat(config.format);
68 }
69
70 void VideoFrameToVideoDecoderConfig(const cdm::VideoFrame& frame,
71 cdm::VideoDecoderConfig* config) {
72 DCHECK(config);
ddorwin 2012/10/13 03:54:42 NOTIMPLEMENTED()?
Tom Finegan 2012/10/13 23:47:26 Forgot to remove this... something I thought I nee
73 }
74
75 cdm::VideoFrame::VideoPlane VideoPlane(int32_t plane_index) {
76 DCHECK(plane_index >= cdm::VideoFrame::kYPlane &&
77 plane_index < cdm::VideoFrame::kMaxPlanes);
78 return static_cast<cdm::VideoFrame::VideoPlane>(plane_index);
79 }
80
81 uint8_t* PlanePointer(cdm::VideoFrame* frame, int plane_index) {
82 DCHECK(frame);
83 return frame->frame_buffer()->data() +
84 frame->plane_offset(VideoPlane(plane_index));
85
86 }
87
88 } // anonymous name space
ddorwin 2012/10/13 03:54:42 // namespace
Tom Finegan 2012/10/13 23:47:26 Done.
89
90 namespace webkit_media {
91
92 bool IsValidConfig(cdm::VideoFormat format, const cdm::Size& data_size) {
93 return ((format == cdm::kYv12 || format == cdm::kI420) &&
94 data_size.width > 0 && data_size.height > 0 &&
95 data_size.width <= media::limits::kMaxDimension &&
96 data_size.height <= media::limits::kMaxDimension &&
97 data_size.width * data_size.height <= media::limits::kMaxCanvas);
98 }
99
100 static int GetVideoBufferImpl(AVCodecContext* s, AVFrame* frame) {
101 FFmpegVideoDecoder* vd = static_cast<FFmpegVideoDecoder*>(s->opaque);
102 return vd->GetVideoBuffer(s, frame);
103 }
104
105 static void ReleaseVideoBufferImpl(AVCodecContext* s, AVFrame* frame) {
106 // The FFmpeg API expects us to zero the data pointers in
107 // this callback
108 memset(frame->data, 0, sizeof(frame->data));
109 frame->opaque = NULL;
110 }
111
112 FFmpegVideoDecoder::FFmpegVideoDecoder(cdm::Allocator* allocator)
113 : codec_context_(NULL),
114 av_frame_(NULL),
115 allocator_(allocator) {
116 }
117
118 FFmpegVideoDecoder::~FFmpegVideoDecoder() {
119 ReleaseFFmpegResources();
120 }
121
122 bool FFmpegVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) {
123 if (!IsValidConfig(config.format, config.coded_size)) {
124 DLOG(ERROR) << "Invalid VideoDecoderConfig.";
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 VideoDecoderConfigToAVCodecContext(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 codec_context_->get_buffer = GetVideoBufferImpl;
145 codec_context_->release_buffer = ReleaseVideoBufferImpl;
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) << "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) << "avcodec_open2 failed: " << status;
157 return false;
158 }
159
160 av_frame_ = avcodec_alloc_frame();
161 config_ = config;
162 // TODO(tomfinegan): Do we need to copy the extra data? Zeroing the members
163 // because we don't own this memory.
164 config_.extra_data = NULL;
165 config_.extra_data_size = 0;
166 return true;
167 }
168
169 void FFmpegVideoDecoder::Deinitialize() {
170 ReleaseFFmpegResources();
171 }
172
173 void FFmpegVideoDecoder::Reset() {
174 avcodec_flush_buffers(codec_context_);
175 }
176
177 void CopyCdmVideoFrame(cdm::VideoFrame* source, cdm::VideoFrame* target) {
ddorwin 2012/10/13 03:54:42 More of a Move. Maybe we should clear source as we
Tom Finegan 2012/10/13 23:47:26 Renamed to MoveCdmVideoFrame(), changed arg 1 to w
178 DCHECK(source);
179 DCHECK(target);
180 target->set_format(source->format());
181 target->set_frame_buffer(source->frame_buffer());
182
183 typedef cdm::VideoFrame::VideoPlane VideoPlane;
184 const int kMaxPlanes = cdm::VideoFrame::kMaxPlanes;
185 for (int i = 0; static_cast<VideoPlane>(i) < kMaxPlanes; ++i) {
186 cdm::VideoFrame::VideoPlane plane = static_cast<VideoPlane>(i);
187 target->set_plane_offset(plane, source->plane_offset(plane));
188 target->set_stride(plane, source->stride(plane));
189 }
190
191 target->set_timestamp(source->timestamp());
192 }
193
194 bool FFmpegVideoDecoder::DecodeFrame(const uint8_t* compressed_frame,
195 int32_t frame_length,
196 int64_t timestamp,
197 cdm::VideoFrame* decoded_frame) {
198 DCHECK(decoded_frame);
199
200 // Create a packet for input data.
201 // Due to FFmpeg API changes we no longer have const read-only pointers.
202 AVPacket packet;
203 av_init_packet(&packet);
204 packet.data = const_cast<uint8_t*>(compressed_frame);
205 packet.size = frame_length;
206
207 // Let FFmpeg handle presentation timestamp reordering.
208 codec_context_->reordered_opaque = timestamp;
209
210 // Reset frame to default values.
211 avcodec_get_frame_defaults(av_frame_);
212
213 // This is for codecs not using get_buffer to initialize
214 // |av_frame_->reordered_opaque|
215 av_frame_->reordered_opaque = codec_context_->reordered_opaque;
216
217 int frame_decoded = 0;
218 int result = avcodec_decode_video2(codec_context_,
219 av_frame_,
220 &frame_decoded,
221 &packet);
222 // Log the problem when we can't decode a video frame and exit early.
223 if (result < 0) {
224 LOG(ERROR) << "Error decoding a video frame with timestamp: " << timestamp
225 << " us, packet size: " << frame_length << " bytes";
226 return false;
227 }
228
229 // If no frame was produced then signal that more data is required to
230 // produce more frames. This can happen under two circumstances:
231 // 1) Decoder was recently initialized/flushed
232 // 2) End of stream was reached and all internal frames have been output
233 if (frame_decoded == 0)
234 return true;
235
236 // The decoder is in a bad state and not decoding correctly.
237 // Checking for NULL avoids a crash.
238 if (!av_frame_->data[cdm::VideoFrame::kYPlane] ||
239 !av_frame_->data[cdm::VideoFrame::kUPlane] ||
240 !av_frame_->data[cdm::VideoFrame::kVPlane]) {
241 LOG(ERROR) << "Video frame was produced yet has invalid frame data.";
242 return false;
243 }
244
245 if (!av_frame_->opaque) {
246 LOG(ERROR) << "VideoFrame object associated with frame data not set.";
247 return false;
248 }
249
250 scoped_ptr<VideoFrame> video_frame(
251 static_cast<VideoFrame*>(av_frame_->opaque));
252 DCHECK(video_frame->frame());
253 CopyCdmVideoFrame(video_frame->frame(), decoded_frame);
254 video_frame->ReleaseCdmVideoFrame();
255 return true;
256 }
257
258 int FFmpegVideoDecoder::GetVideoBuffer(AVCodecContext* codec_context,
259 AVFrame* av_frame) {
260 // Don't use |codec_context_| here! With threaded decoding,
261 // it will contain unsynchronized width/height/pix_fmt values,
262 // whereas |codec_context| contains the current threads's
263 // updated width/height/pix_fmt, which can change for adaptive
264 // content.
265 cdm::VideoFormat format =
266 PixelFormatToVideoFormat(codec_context->pix_fmt);
267 if (format == cdm::kUnknownVideoFormat)
268 return AVERROR(EINVAL);
269 DCHECK(format == cdm::kYv12 || format == cdm::kI420);
270
271 cdm::Size size(codec_context->width, codec_context->height);
272 int ret;
273 if ((ret = av_image_check_size(size.width, size.height, 0, NULL)) < 0)
274 return ret;
275
276 if (!IsValidConfig(format, size))
277 return AVERROR(EINVAL);
278
279 VideoFrame* video_frame = VideoFrame::Create(allocator_, format, size, 0);
280 if (!video_frame) {
281 LOG(ERROR) << "FFmpegVideoDecoder::GetVideoBuffer VideoFrame Create failed";
282 return AVERROR(ENOMEM);
283 }
284
285 for (int i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) {
286 av_frame->base[i] = PlanePointer(video_frame->frame(), i);
287 av_frame->data[i] = PlanePointer(video_frame->frame(), i);
288 av_frame->linesize[i] = video_frame->frame()->stride(VideoPlane(i));
289 }
290
291 av_frame->opaque = reinterpret_cast<void*>(video_frame);
292 av_frame->type = FF_BUFFER_TYPE_USER;
293 av_frame->pkt_pts = codec_context->pkt ? codec_context->pkt->pts :
294 AV_NOPTS_VALUE;
295 av_frame->width = codec_context->width;
296 av_frame->height = codec_context->height;
297 av_frame->format = codec_context->pix_fmt;
298
299 return 0;
300 }
301
302 void FFmpegVideoDecoder::ReleaseFFmpegResources() {
303 if (codec_context_) {
304 av_free(codec_context_->extradata);
305 avcodec_close(codec_context_);
306 av_free(codec_context_);
307 codec_context_ = NULL;
308 }
309 if (av_frame_) {
310 av_free(av_frame_);
311 av_frame_ = NULL;
312 }
313 }
314
315 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698