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