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