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/ref_counted.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "media/base/buffers.h" | |
11 #include "media/base/limits.h" | |
12 #include "media/ffmpeg/ffmpeg_common.h" | |
13 #include "webkit/media/crypto/ppapi/content_decryption_module.h" | |
14 #include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.h" | |
15 | |
16 // Include FFmpeg header files. | |
17 extern "C" { | |
18 // Temporarily disable possible loss of data warning. | |
19 MSVC_PUSH_DISABLE_WARNING(4244); | |
20 #include <libavcodec/avcodec.h> | |
21 MSVC_POP_WARNING(); | |
22 } // extern "C" | |
23 | |
24 namespace webkit_media { | |
25 | |
26 namespace { | |
27 | |
28 cdm::VideoFormat PixelFormatToCdmVideoFormat(PixelFormat pixel_format) { | |
29 switch (pixel_format) { | |
30 case PIX_FMT_YUV420P: | |
31 return cdm::kYv12; | |
32 default: | |
33 DVLOG(1) << "Unsupported PixelFormat: " << pixel_format; | |
34 } | |
35 return cdm::kUnknownVideoFormat; | |
36 } | |
37 | |
38 PixelFormat CdmVideoFormatToPixelFormat(cdm::VideoFormat video_format) { | |
39 switch (video_format) { | |
40 case cdm::kYv12: | |
41 case cdm::kI420: | |
42 return PIX_FMT_YUV420P; | |
43 case cdm::kUnknownVideoFormat: | |
44 default: | |
45 DVLOG(1) << "Unsupported cdm::VideoFormat: " << video_format; | |
46 } | |
47 return PIX_FMT_NONE; | |
48 } | |
49 | |
50 CodecID CdmVideoCodecToCodecID( | |
51 cdm::VideoDecoderConfig::VideoCodec video_codec) { | |
52 switch (video_codec) { | |
53 case cdm::VideoDecoderConfig::kCodecVP8: | |
54 return CODEC_ID_VP8; | |
55 case cdm::kUnknownVideoFormat: | |
56 default: | |
57 DVLOG(1) << "Unsupported cdm::VideoCodec: " << video_codec; | |
58 } | |
59 NOTREACHED() << "Unsupported video_codec: " << video_codec; | |
60 return CODEC_ID_NONE; | |
61 } | |
62 | |
63 void CdmVideoDecoderConfigToAVCodecContext( | |
64 const cdm::VideoDecoderConfig& config, | |
65 AVCodecContext* codec_context) { | |
66 codec_context->codec_type = AVMEDIA_TYPE_VIDEO; | |
67 codec_context->codec_id = CdmVideoCodecToCodecID(config.codec); | |
68 codec_context->profile = FF_PROFILE_UNKNOWN; | |
69 codec_context->coded_width = config.coded_size.width; | |
70 codec_context->coded_height = config.coded_size.height; | |
71 codec_context->pix_fmt = CdmVideoFormatToPixelFormat(config.format); | |
72 codec_context->extradata = config.extra_data; | |
73 codec_context->extradata_size = config.extra_data_size; | |
74 } | |
75 | |
76 cdm::VideoFrame::VideoPlane VideoPlane(int32_t plane_index) { | |
77 DCHECK(plane_index >= cdm::VideoFrame::kYPlane && | |
78 plane_index < cdm::VideoFrame::kMaxPlanes); | |
79 return static_cast<cdm::VideoFrame::VideoPlane>(plane_index); | |
80 } | |
81 | |
82 uint8_t* PlanePointer(FFmpegCdmVideoFrame* frame, int plane_index) { | |
83 DCHECK(frame); | |
84 return frame->frame_buffer()->data() + | |
85 frame->plane_offset(VideoPlane(plane_index)); | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 bool IsValidConfig(cdm::VideoFormat format, const cdm::Size& data_size) { | |
91 return ((format == cdm::kYv12 || format == cdm::kI420) && | |
92 data_size.width > 0 && data_size.height > 0 && | |
93 data_size.width <= media::limits::kMaxDimension && | |
94 data_size.height <= media::limits::kMaxDimension && | |
95 data_size.width * data_size.height <= media::limits::kMaxCanvas); | |
96 } | |
97 | |
98 static int GetCdmVideoBufferImpl(AVCodecContext* s, AVFrame* frame) { | |
xhwang
2012/10/16 21:59:53
We are mixing anonymous namespace and static funct
Tom Finegan
2012/10/17 02:39:10
GetCdmVideoBufferImpl needs to be non-static to be
Tom Finegan
2012/10/17 04:25:29
Back to static and a public GetVideoBuffer().
| |
99 FFmpegCdmVideoDecoder* vd = static_cast<FFmpegCdmVideoDecoder*>(s->opaque); | |
100 return vd->GetVideoBuffer(s, frame); | |
101 } | |
102 | |
103 static void ReleaseCdmVideoBufferImpl(AVCodecContext* s, AVFrame* frame) { | |
104 scoped_ptr<FFmpegCdmVideoFrame> cdm_video_frame( | |
105 reinterpret_cast<FFmpegCdmVideoFrame*>(frame->opaque)); | |
106 | |
107 // The FFmpeg API expects us to zero the data pointers in | |
108 // this callback | |
109 memset(frame->data, 0, sizeof(frame->data)); | |
110 frame->opaque = NULL; | |
111 } | |
112 | |
113 FFmpegCdmVideoDecoder::FFmpegCdmVideoDecoder(cdm::Allocator* allocator) | |
114 : codec_context_(NULL), | |
115 av_frame_(NULL), | |
116 allocator_(allocator) { | |
117 } | |
118 | |
119 FFmpegCdmVideoDecoder::~FFmpegCdmVideoDecoder() { | |
120 ReleaseFFmpegResources(); | |
121 } | |
122 | |
123 bool FFmpegCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) { | |
124 if (!IsValidConfig(config.format, config.coded_size)) { | |
125 DLOG(ERROR) << "Invalid VideoDecoderConfig."; | |
126 return false; | |
127 } | |
128 | |
129 av_register_all(); | |
130 | |
131 // Release existing resources if necessary. | |
132 ReleaseFFmpegResources(); | |
133 | |
134 // Initialize AVCodecContext structure. | |
135 codec_context_ = avcodec_alloc_context3(NULL); | |
136 CdmVideoDecoderConfigToAVCodecContext(config, codec_context_); | |
137 | |
138 // Enable motion vector search (potentially slow), strong deblocking filter | |
139 // for damaged macroblocks, and set our error detection sensitivity. | |
140 codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK; | |
141 codec_context_->err_recognition = AV_EF_CAREFUL; | |
142 codec_context_->thread_count = kDecodeThreads; | |
143 codec_context_->opaque = this; | |
144 codec_context_->flags |= CODEC_FLAG_EMU_EDGE; | |
145 codec_context_->get_buffer = GetCdmVideoBufferImpl; | |
146 codec_context_->release_buffer = ReleaseCdmVideoBufferImpl; | |
147 DCHECK_EQ(CODEC_ID_VP8, codec_context_->codec_id); | |
148 | |
149 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | |
150 if (!codec) { | |
151 LOG(ERROR) << "avcodec_find_decoder failed."; | |
152 return false; | |
153 } | |
154 | |
155 int status; | |
156 if ((status = avcodec_open2(codec_context_, codec, NULL)) < 0) { | |
157 LOG(ERROR) << "avcodec_open2 failed: " << status; | |
158 return false; | |
159 } | |
160 | |
161 av_frame_ = avcodec_alloc_frame(); | |
162 config_ = config; | |
163 | |
164 // TODO(tomfinegan): Do we need to copy the extra data? Zeroing the members | |
xhwang
2012/10/16 21:59:53
We don't. extra_data is only useful for initializa
Tom Finegan
2012/10/17 02:39:10
We don't need our own copy, but we do need to make
xhwang
2012/10/17 18:43:00
Yes, so after we make the copy we don't need to sa
| |
165 // because we don't own the memory. | |
166 config_.extra_data = NULL; | |
167 config_.extra_data_size = 0; | |
168 | |
169 return true; | |
170 } | |
171 | |
172 void FFmpegCdmVideoDecoder::Deinitialize() { | |
173 ReleaseFFmpegResources(); | |
174 } | |
175 | |
176 void FFmpegCdmVideoDecoder::Reset() { | |
177 avcodec_flush_buffers(codec_context_); | |
xhwang
2012/10/16 21:59:53
Do we want to have a flag or state to remember thi
Tom Finegan
2012/10/17 02:39:10
Discussed offline.
| |
178 } | |
179 | |
180 void MoveVideoFrame(FFmpegCdmVideoFrame* source, cdm::VideoFrame* target) { | |
ddorwin
2012/10/16 18:04:30
This should be above with the other static/anonymo
Tom Finegan
2012/10/16 21:44:44
Agreed and done. Not sure if MoveVideoFrame() is s
| |
181 DCHECK(source && source->frame_buffer()); | |
182 DCHECK(target); | |
183 target->set_format(source->format()); | |
184 target->set_frame_buffer(source->frame_buffer()); | |
185 | |
186 typedef cdm::VideoFrame::VideoPlane VideoPlane; | |
187 const int kMaxPlanes = cdm::VideoFrame::kMaxPlanes; | |
188 for (int i = 0; static_cast<VideoPlane>(i) < kMaxPlanes; ++i) { | |
xhwang
2012/10/16 21:59:53
kMaxPlanes is an int. Why do we need the static_ca
Tom Finegan
2012/10/17 02:39:10
kMaxPlanes was an int, it's part of VideoPlanes no
Tom Finegan
2012/10/17 04:25:29
Totally misunderstood this the first time. Fixed i
| |
189 cdm::VideoFrame::VideoPlane plane = static_cast<VideoPlane>(i); | |
190 target->set_plane_offset(plane, source->plane_offset(plane)); | |
191 target->set_stride(plane, source->stride(plane)); | |
192 } | |
193 | |
194 source->ReleaseCdmBuffer(); | |
195 } | |
196 | |
197 bool FFmpegCdmVideoDecoder::DecodeFrame(const uint8_t* compressed_frame, | |
198 int32_t compressed_frame_size, | |
199 int64_t timestamp, | |
200 cdm::VideoFrame* decoded_frame) { | |
201 DCHECK(decoded_frame); | |
202 | |
203 // Create a packet for input data. | |
204 // Due to FFmpeg API changes we no longer have const read-only pointers. | |
xhwang
2012/10/16 21:59:53
put this above line 207?
Tom Finegan
2012/10/17 02:39:10
Done, and reworded it.
| |
205 AVPacket packet; | |
206 av_init_packet(&packet); | |
207 packet.data = const_cast<uint8_t*>(compressed_frame); | |
208 packet.size = compressed_frame_size; | |
209 | |
210 // Let FFmpeg handle presentation timestamp reordering. | |
211 codec_context_->reordered_opaque = timestamp; | |
212 | |
213 // Reset frame to default values. | |
214 avcodec_get_frame_defaults(av_frame_); | |
215 | |
216 // This is for codecs not using get_buffer to initialize | |
217 // |av_frame_->reordered_opaque| | |
218 av_frame_->reordered_opaque = codec_context_->reordered_opaque; | |
219 | |
220 int frame_decoded = 0; | |
221 int result = avcodec_decode_video2(codec_context_, | |
222 av_frame_, | |
223 &frame_decoded, | |
224 &packet); | |
225 // Log the problem when we can't decode a video frame and exit early. | |
226 if (result < 0) { | |
227 LOG(ERROR) << "DecodeFrame: Error decoding a video frame with timestamp: " | |
228 << timestamp << " us, packet size: " << packet.size << " bytes"; | |
229 return false; | |
230 } | |
231 | |
232 // If no frame was produced then signal that more data is required to | |
233 // produce more frames. This can happen under two circumstances: | |
234 // 1) Decoder was recently initialized/flushed | |
235 // 2) End of stream was reached and all internal frames have been output | |
236 if (frame_decoded == 0) | |
237 return true; | |
238 | |
239 // The decoder is in a bad state and not decoding correctly. | |
240 // Checking for NULL avoids a crash. | |
241 if (!av_frame_->data[cdm::VideoFrame::kYPlane] || | |
242 !av_frame_->data[cdm::VideoFrame::kUPlane] || | |
243 !av_frame_->data[cdm::VideoFrame::kVPlane]) { | |
244 LOG(ERROR) << "Video frame was produced yet has invalid frame data."; | |
245 return false; | |
246 } | |
247 | |
248 if (!av_frame_->opaque) { | |
249 LOG(ERROR) << "VideoFrame object associated with frame data not set."; | |
250 return false; | |
251 } | |
252 | |
253 scoped_ptr<FFmpegCdmVideoFrame> video_frame( | |
254 static_cast<FFmpegCdmVideoFrame*>(av_frame_->opaque)); | |
255 DCHECK(video_frame->frame_buffer()); | |
256 MoveVideoFrame(video_frame.get(), decoded_frame); | |
257 | |
258 return true; | |
259 } | |
260 | |
261 int FFmpegCdmVideoDecoder::GetVideoBuffer(AVCodecContext* codec_context, | |
262 AVFrame* av_frame) { | |
263 // Don't use |codec_context_| here! With threaded decoding, | |
264 // it will contain unsynchronized width/height/pix_fmt values, | |
265 // whereas |codec_context| contains the current threads's | |
266 // updated width/height/pix_fmt, which can change for adaptive | |
267 // content. | |
268 cdm::VideoFormat format = | |
269 PixelFormatToCdmVideoFormat(codec_context->pix_fmt); | |
270 if (format == cdm::kUnknownVideoFormat) | |
271 return AVERROR(EINVAL); | |
272 DCHECK(format == cdm::kYv12 || format == cdm::kI420); | |
273 | |
274 cdm::Size size(codec_context->width, codec_context->height); | |
275 int ret; | |
276 if ((ret = av_image_check_size(size.width, size.height, 0, NULL)) < 0) | |
277 return ret; | |
278 | |
279 if (!IsValidConfig(format, size)) | |
280 return AVERROR(EINVAL); | |
281 | |
282 FFmpegCdmVideoFrame* video_frame = FFmpegCdmVideoFrame::Create(allocator_, | |
283 format, | |
284 size); | |
285 if (!video_frame) { | |
286 LOG(ERROR) << "GetVideoBuffer: VideoFrame Create failed"; | |
287 return AVERROR(ENOMEM); | |
288 } | |
289 | |
290 for (int i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) { | |
291 av_frame->base[i] = PlanePointer(video_frame, i); | |
292 av_frame->data[i] = PlanePointer(video_frame, i); | |
293 av_frame->linesize[i] = video_frame->stride(VideoPlane(i)); | |
294 } | |
295 | |
296 av_frame->opaque = reinterpret_cast<void*>(video_frame); | |
297 av_frame->type = FF_BUFFER_TYPE_USER; | |
298 av_frame->pkt_pts = codec_context->pkt ? codec_context->pkt->pts : | |
299 AV_NOPTS_VALUE; | |
300 av_frame->width = codec_context->width; | |
301 av_frame->height = codec_context->height; | |
302 av_frame->format = codec_context->pix_fmt; | |
303 | |
304 return 0; | |
305 } | |
306 | |
307 void FFmpegCdmVideoDecoder::ReleaseFFmpegResources() { | |
308 if (codec_context_) { | |
309 av_free(codec_context_->extradata); | |
310 avcodec_close(codec_context_); | |
311 av_free(codec_context_); | |
312 codec_context_ = NULL; | |
313 } | |
314 if (av_frame_) { | |
315 av_free(av_frame_); | |
316 av_frame_ = NULL; | |
317 } | |
318 } | |
319 | |
320 } // namespace webkit_media | |
OLD | NEW |