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 "media/filters/vpx_video_decoder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/command_line.h" | |
10 #include "base/debug/trace_event.h" | |
11 #include "base/location.h" | |
12 #include "base/logging.h" | |
13 #include "base/message_loop_proxy.h" | |
14 #include "base/string_number_conversions.h" | |
15 #include "media/base/bind_to_loop.h" | |
16 #include "media/base/decoder_buffer.h" | |
17 #include "media/base/decryptor.h" | |
18 #include "media/base/demuxer_stream.h" | |
19 #include "media/base/media_switches.h" | |
20 #include "media/base/pipeline.h" | |
21 #include "media/base/video_decoder_config.h" | |
22 #include "media/base/video_frame.h" | |
23 #include "media/base/video_util.h" | |
24 | |
25 // Include libvpx header files. | |
26 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide | |
27 // backwards compatibility for legacy applications using the library. | |
28 #define VPX_CODEC_DISABLE_COMPAT 1 | |
29 extern "C" { | |
30 #include "third_party/libvpx/libvpx.h" | |
31 } | |
32 | |
33 namespace media { | |
34 | |
35 #define BIND_TO_LOOP(function) \ | |
scherkus (not reviewing)
2012/12/14 21:32:41
use BindToCurrentLoop() instead of this macro
Tom Finegan
2012/12/15 06:13:30
Done.
| |
36 media::BindToLoop(message_loop_, base::Bind(function, this)) | |
37 | |
38 // Always try to use three threads for video decoding. There is little reason | |
39 // not to since current day CPUs tend to be multi-core and we measured | |
40 // performance benefits on older machines such as P4s with hyperthreading. | |
41 static const int kDecodeThreads = 2; | |
42 static const int kMaxDecodeThreads = 16; | |
43 | |
44 // Returns the number of threads. | |
45 static int GetThreadCount() { | |
scherkus (not reviewing)
2012/12/14 21:32:41
you don't use this function
Tom Finegan
2012/12/15 06:13:30
Thanks, wasn't intentional. :)
| |
46 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. | |
47 int decode_threads = kDecodeThreads; | |
48 | |
49 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
50 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); | |
51 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) | |
52 return decode_threads; | |
53 | |
54 decode_threads = std::max(decode_threads, 0); | |
55 decode_threads = std::min(decode_threads, kMaxDecodeThreads); | |
56 return decode_threads; | |
57 } | |
58 | |
59 VpxVideoDecoder::VpxVideoDecoder( | |
60 const scoped_refptr<base::MessageLoopProxy>& message_loop) | |
61 : message_loop_(message_loop), | |
62 state_(kUninitialized), | |
63 vpx_codec_(NULL) { | |
64 } | |
65 | |
66 VpxVideoDecoder::~VpxVideoDecoder() { | |
67 DCHECK_EQ(kUninitialized, state_); | |
68 CloseDecoder(); | |
69 } | |
70 | |
71 void VpxVideoDecoder::Initialize( | |
72 const scoped_refptr<DemuxerStream>& stream, | |
73 const PipelineStatusCB& status_cb, | |
74 const StatisticsCB& statistics_cb) { | |
75 if (!message_loop_->BelongsToCurrentThread()) { | |
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread
Tom Finegan
2012/12/15 06:13:30
Done.
| |
76 message_loop_->PostTask(FROM_HERE, base::Bind( | |
77 &VpxVideoDecoder::Initialize, this, | |
78 stream, status_cb, statistics_cb)); | |
79 return; | |
80 } | |
81 | |
82 DCHECK(!demuxer_stream_) << "Already initialized."; | |
83 | |
84 if (!stream) { | |
85 status_cb.Run(PIPELINE_ERROR_DECODE); | |
86 return; | |
87 } | |
88 | |
89 demuxer_stream_ = stream; | |
90 statistics_cb_ = statistics_cb; | |
91 | |
92 if (!ConfigureDecoder()) { | |
93 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); | |
94 return; | |
95 } | |
96 | |
97 // Success! | |
98 state_ = kNormal; | |
99 status_cb.Run(PIPELINE_OK); | |
100 } | |
101 | |
102 bool VpxVideoDecoder::ConfigureDecoder() { | |
103 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); | |
104 if (!config.IsValidConfig()) { | |
105 DLOG(ERROR) << "Invalid video stream config: " | |
106 << config.AsHumanReadableString(); | |
107 return false; | |
108 } | |
109 | |
110 if (config.codec() != kCodecVP9) | |
111 return false; | |
112 | |
113 CloseDecoder(); | |
114 | |
115 vpx_codec_ = new vpx_codec_ctx(); | |
116 vpx_codec_dec_cfg_t vpx_config = {0}; | |
117 vpx_config.w = config.coded_size().width(); | |
118 vpx_config.h = config.coded_size().height(); | |
119 vpx_config.threads = kDecodeThreads; | |
120 | |
121 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, | |
122 vpx_codec_vp8_dx(), | |
123 &vpx_config, | |
124 0); | |
125 if (status != VPX_CODEC_OK) { | |
126 LOG(ERROR) << "ConfigureDecoder(): vpx_codec_dec_init failed, status=" | |
127 << status; | |
128 delete vpx_codec_; | |
129 vpx_codec_ = NULL; | |
130 return false; | |
131 } | |
132 | |
133 return true; | |
134 } | |
135 | |
136 void VpxVideoDecoder::CloseDecoder() { | |
137 if (vpx_codec_) { | |
138 vpx_codec_destroy(vpx_codec_); | |
139 delete vpx_codec_; | |
140 vpx_codec_ = NULL; | |
141 } | |
142 } | |
143 | |
144 void VpxVideoDecoder::Read(const ReadCB& read_cb) { | |
145 // Complete operation asynchronously on different stack of execution as per | |
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread an
Tom Finegan
2012/12/15 06:13:30
Done.
| |
146 // the API contract of VideoDecoder::Read() | |
147 message_loop_->PostTask(FROM_HERE, base::Bind( | |
148 &VpxVideoDecoder::DoRead, this, read_cb)); | |
149 } | |
150 | |
151 void VpxVideoDecoder::Reset(const base::Closure& closure) { | |
152 if (!message_loop_->BelongsToCurrentThread()) { | |
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread
Tom Finegan
2012/12/15 06:13:30
Done.
| |
153 message_loop_->PostTask(FROM_HERE, base::Bind( | |
154 &VpxVideoDecoder::Reset, this, closure)); | |
155 return; | |
156 } | |
157 | |
158 DCHECK(reset_cb_.is_null()); | |
159 reset_cb_ = closure; | |
scherkus (not reviewing)
2012/12/14 21:32:41
use BindToCurrentLoop() on this closure
see ffmpe
Tom Finegan
2012/12/15 06:13:30
Done.
| |
160 | |
161 // Defer the reset if a read is pending. | |
162 if (!read_cb_.is_null()) | |
163 return; | |
164 | |
165 DoReset(); | |
166 } | |
167 | |
168 void VpxVideoDecoder::Stop(const base::Closure& closure) { | |
169 if (!message_loop_->BelongsToCurrentThread()) { | |
scherkus (not reviewing)
2012/12/14 21:32:41
replace this with a DCHECK() for current thread
Tom Finegan
2012/12/15 06:13:30
Done.
| |
170 message_loop_->PostTask(FROM_HERE, base::Bind( | |
171 &VpxVideoDecoder::Stop, this, closure)); | |
172 return; | |
173 } | |
174 | |
175 if (state_ == kUninitialized) { | |
176 closure.Run(); | |
177 return; | |
178 } | |
179 | |
180 if (!read_cb_.is_null()) | |
181 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
182 | |
183 state_ = kUninitialized; | |
184 closure.Run(); | |
185 } | |
186 | |
187 void VpxVideoDecoder::DoRead(const ReadCB& read_cb) { | |
188 DCHECK(message_loop_->BelongsToCurrentThread()); | |
189 DCHECK(!read_cb.is_null()); | |
190 CHECK_NE(state_, kUninitialized); | |
191 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | |
192 | |
193 // Return empty frames if decoding has finished. | |
194 if (state_ == kDecodeFinished) { | |
195 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); | |
196 return; | |
197 } | |
198 | |
199 read_cb_ = read_cb; | |
200 ReadFromDemuxerStream(); | |
201 } | |
202 | |
203 void VpxVideoDecoder::ReadFromDemuxerStream() { | |
204 DCHECK_NE(state_, kUninitialized); | |
205 DCHECK_NE(state_, kDecodeFinished); | |
206 DCHECK(!read_cb_.is_null()); | |
207 | |
208 demuxer_stream_->Read(base::Bind( | |
209 &VpxVideoDecoder::DoDecryptOrDecodeBuffer, this)); | |
210 } | |
211 | |
212 void VpxVideoDecoder::DoDecryptOrDecodeBuffer( | |
213 DemuxerStream::Status status, | |
214 const scoped_refptr<DecoderBuffer>& buffer) { | |
215 if (!message_loop_->BelongsToCurrentThread()) { | |
scherkus (not reviewing)
2012/12/14 21:32:41
replace with check for correct thread
Tom Finegan
2012/12/15 06:13:30
Done.
| |
216 message_loop_->PostTask(FROM_HERE, base::Bind( | |
217 &VpxVideoDecoder::DoDecryptOrDecodeBuffer, this, status, buffer)); | |
218 return; | |
219 } | |
220 | |
221 DCHECK_NE(state_, kDecodeFinished); | |
222 DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status; | |
223 | |
224 if (state_ == kUninitialized) | |
225 return; | |
226 | |
227 DCHECK(!read_cb_.is_null()); | |
228 | |
229 if (!reset_cb_.is_null()) { | |
230 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
231 DoReset(); | |
232 return; | |
233 } | |
234 | |
235 if (status == DemuxerStream::kAborted) { | |
236 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
237 return; | |
238 } | |
239 | |
240 if (status == DemuxerStream::kConfigChanged) { | |
241 if (!ConfigureDecoder()) { | |
242 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
243 return; | |
244 } | |
245 | |
246 ReadFromDemuxerStream(); | |
247 return; | |
248 } | |
249 | |
250 DCHECK_EQ(status, DemuxerStream::kOk); | |
251 DecodeBuffer(buffer); | |
252 } | |
253 | |
254 void VpxVideoDecoder::DecodeBuffer( | |
255 const scoped_refptr<DecoderBuffer>& buffer) { | |
256 DCHECK(message_loop_->BelongsToCurrentThread()); | |
257 DCHECK_NE(state_, kUninitialized); | |
258 DCHECK_NE(state_, kDecodeFinished); | |
259 DCHECK(reset_cb_.is_null()); | |
260 DCHECK(!read_cb_.is_null()); | |
261 DCHECK(buffer); | |
262 | |
263 // During decode, because reads are issued asynchronously, it is possible to | |
scherkus (not reviewing)
2012/12/14 21:32:41
this comment is copied from FFmpegVideoDecoder
ho
Tom Finegan
2012/12/15 06:13:30
It doesn't. Removed the comment.
| |
264 // receive multiple end of stream buffers since each read is acked. When the | |
265 // first end of stream buffer is read, libvpx may still have frames queued | |
266 // up in the decoder so we need to go through the decode loop until it stops | |
267 // giving sensible data. After that, the decoder should output empty | |
268 // frames. There are three states the decoder can be in: | |
269 // | |
270 // kNormal: This is the starting state. Buffers are decoded. Decode errors | |
271 // are discarded. | |
272 // kFlushCodec: There isn't any more input data. Call |vpx_codec_decode()| | |
273 // until no more data is returned to flush out any remaining | |
274 // frames. The input buffer is ignored at this point. | |
275 // kDecodeFinished: All calls return empty frames. | |
276 // | |
277 // These are the possible state transitions. | |
278 // | |
279 // kNormal -> kFlushCodec: | |
280 // When buffer->IsEndOfStream() is first true. | |
281 // kNormal -> kDecodeFinished: | |
282 // A decoding error occurs and decoding needs to stop. | |
283 // kFlushCodec -> kDecodeFinished: | |
284 // When |vpx_codec_decode()| returns no data or errors out. | |
285 // (any state) -> kNormal: | |
286 // Any time Reset() is called. | |
287 | |
288 // Transition to kFlushCodec on the first end of stream buffer. | |
289 if (state_ == kNormal && buffer->IsEndOfStream()) { | |
290 state_ = kFlushCodec; | |
291 } | |
292 | |
293 scoped_refptr<VideoFrame> video_frame; | |
294 if (!Decode(buffer, &video_frame)) { | |
295 state_ = kDecodeFinished; | |
296 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
297 return; | |
298 } | |
299 | |
300 // Any successful decode counts! | |
301 if (buffer->GetDataSize()) { | |
302 PipelineStatistics statistics; | |
303 statistics.video_bytes_decoded = buffer->GetDataSize(); | |
304 statistics_cb_.Run(statistics); | |
305 } | |
306 | |
307 // If we didn't get a frame then we've either completely finished decoding or | |
308 // we need more data. | |
309 if (!video_frame) { | |
310 if (state_ == kFlushCodec) { | |
scherkus (not reviewing)
2012/12/14 21:32:41
does vpx need to be flushed to get the last frames
Tom Finegan
2012/12/15 06:13:30
No. Removed this... I think I'm handling things co
| |
311 state_ = kDecodeFinished; | |
312 base::ResetAndReturn(&read_cb_).Run(kOk, VideoFrame::CreateEmptyFrame()); | |
313 return; | |
314 } | |
315 | |
316 ReadFromDemuxerStream(); | |
317 return; | |
318 } | |
319 | |
320 base::ResetAndReturn(&read_cb_).Run(kOk, video_frame); | |
321 } | |
322 | |
323 bool VpxVideoDecoder::Decode( | |
324 const scoped_refptr<DecoderBuffer>& buffer, | |
325 scoped_refptr<VideoFrame>* video_frame) { | |
326 DCHECK(video_frame); | |
327 | |
328 // Pass |buffer| to libvpx. | |
329 int64 timestamp = buffer->GetTimestamp().InMicroseconds(); | |
330 void* user_priv = reinterpret_cast<void*>(×tamp); | |
331 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_, | |
332 buffer->GetData(), | |
333 buffer->GetDataSize(), | |
334 user_priv, | |
335 0); | |
336 if (status != VPX_CODEC_OK) { | |
337 LOG(ERROR) << "Decode(): vpx_codec_decode failed, status=" << status; | |
scherkus (not reviewing)
2012/12/14 21:32:41
remove Decode() from LOG() here + below
Tom Finegan
2012/12/15 06:13:30
Done.
| |
338 return false; | |
339 } | |
340 | |
341 // Gets pointer to decoded data. | |
342 vpx_codec_iter_t iter = NULL; | |
343 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter); | |
344 if (!vpx_image) { | |
345 *video_frame = NULL; | |
346 return true; | |
347 } | |
348 | |
349 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { | |
350 LOG(ERROR) << "Decode(): invalid output timestamp."; | |
351 return false; | |
352 } | |
353 | |
354 if (!CopyVpxImageTo(vpx_image, video_frame)) { | |
355 LOG(ERROR) << "Decode(): could not copy vpx image to output buffer."; | |
356 return false; | |
357 } | |
358 | |
359 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); | |
360 | |
361 return true; | |
362 } | |
363 | |
364 void VpxVideoDecoder::DoReset() { | |
365 DCHECK(read_cb_.is_null()); | |
366 | |
367 state_ = kNormal; | |
368 reset_cb_.Run(); | |
369 reset_cb_.Reset(); | |
370 } | |
371 | |
372 bool VpxVideoDecoder::CopyVpxImageTo( | |
373 const vpx_image* vpx_image, | |
374 scoped_refptr<VideoFrame>* video_frame) { | |
375 DCHECK(vpx_image); | |
scherkus (not reviewing)
2012/12/14 21:32:41
don't dcheck then handle it gracefully
if it's no
Tom Finegan
2012/12/15 06:13:30
Made method void, removed graceful error handling,
| |
376 if (!vpx_image) | |
377 return false; | |
378 | |
379 DCHECK_EQ(vpx_image->d_w % 2, 0U); | |
scherkus (not reviewing)
2012/12/14 21:32:41
if these DCHECKs fail in release mode, will we ope
Tom Finegan
2012/12/15 06:13:30
Used CHECKs.
| |
380 DCHECK_EQ(vpx_image->d_h % 2, 0U); | |
381 DCHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || | |
382 vpx_image->fmt == VPX_IMG_FMT_YV12); | |
383 | |
384 gfx::Size size(vpx_image->d_w, vpx_image->d_h); | |
385 gfx::Size natural_size = | |
386 demuxer_stream_->video_decoder_config().natural_size(); | |
387 | |
388 const VideoFrame::Format format = VideoFrame::YV12; | |
389 if (!VideoFrame::IsValidConfig(format, | |
scherkus (not reviewing)
2012/12/14 21:32:41
is it actually valid for this to fail?
wouldn't i
Tom Finegan
2012/12/15 06:13:30
Yeah, I think you're right. Removed.
| |
390 size, | |
391 gfx::Rect(size), | |
392 natural_size)) { | |
393 LOG(ERROR) << "CopyVpxImageTo(): invalid video frame."; | |
394 return false; | |
395 } | |
396 | |
397 *video_frame = VideoFrame::CreateFrame(format, | |
scherkus (not reviewing)
2012/12/14 21:32:41
OOC is there a way to use custom allocators with l
Tom Finegan
2012/12/15 06:13:30
No, not at present. :(
| |
398 size, | |
399 gfx::Rect(size), | |
400 natural_size, | |
401 kNoTimestamp()); | |
402 if (!video_frame) { | |
scherkus (not reviewing)
2012/12/14 21:32:41
this always returns true -- you do'nt need this ch
Tom Finegan
2012/12/15 06:13:30
Done.
| |
403 LOG(ERROR) << "CopyVpxImageTo(): cannot create output video frame."; | |
404 return false; | |
405 } | |
406 | |
407 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], | |
408 vpx_image->stride[VPX_PLANE_Y], | |
409 vpx_image->d_h, | |
410 *video_frame); | |
411 CopyUPlane(vpx_image->planes[VPX_PLANE_U], | |
412 vpx_image->stride[VPX_PLANE_U], | |
413 vpx_image->d_h / 2, | |
414 *video_frame); | |
415 CopyVPlane(vpx_image->planes[VPX_PLANE_V], | |
416 vpx_image->stride[VPX_PLANE_V], | |
417 vpx_image->d_h / 2, | |
418 *video_frame); | |
419 | |
420 return true; | |
421 } | |
422 | |
423 } // namespace media | |
OLD | NEW |