OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/vpx_video_decoder.h" | 5 #include "media/filters/vpx_video_decoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "media/base/bind_to_loop.h" | 14 #include "media/base/bind_to_loop.h" |
15 #include "media/base/decoder_buffer.h" | 15 #include "media/base/decoder_buffer.h" |
16 #include "media/base/demuxer_stream.h" | 16 #include "media/base/demuxer_stream.h" |
17 #include "media/base/media_switches.h" | 17 #include "media/base/media_switches.h" |
18 #include "media/base/pipeline.h" | 18 #include "media/base/pipeline.h" |
19 #include "media/base/video_decoder_config.h" | 19 #include "media/base/video_decoder_config.h" |
20 #include "media/base/video_frame.h" | 20 #include "media/base/video_frame.h" |
21 #include "media/base/video_util.h" | 21 #include "media/base/video_util.h" |
22 | 22 |
23 // Include libvpx header files. | |
24 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide | |
25 // backwards compatibility for legacy applications using the library. | |
26 #define VPX_CODEC_DISABLE_COMPAT 1 | |
27 extern "C" { | |
28 #include "third_party/libvpx/source/libvpx/vpx/vpx_decoder.h" | |
29 #include "third_party/libvpx/source/libvpx/vpx/vp8dx.h" | |
30 } | |
31 | |
32 namespace media { | 23 namespace media { |
33 | 24 |
34 // Always try to use three threads for video decoding. There is little reason | 25 // Always try to use three threads for video decoding. There is little reason |
35 // not to since current day CPUs tend to be multi-core and we measured | 26 // not to since current day CPUs tend to be multi-core and we measured |
36 // performance benefits on older machines such as P4s with hyperthreading. | 27 // performance benefits on older machines such as P4s with hyperthreading. |
37 static const int kDecodeThreads = 2; | 28 static const int kDecodeThreads = 2; |
38 static const int kMaxDecodeThreads = 16; | 29 static const int kMaxDecodeThreads = 16; |
39 | 30 |
40 // Returns the number of threads. | 31 // Returns the number of threads. |
41 static int GetThreadCount() { | 32 static int GetThreadCount() { |
42 // TODO(scherkus): De-duplicate this function and the one used by | 33 // TODO(scherkus): De-duplicate this function and the one used by |
43 // FFmpegVideoDecoder. | 34 // FFmpegVideoDecoder. |
44 | 35 |
45 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. | 36 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. |
46 int decode_threads = kDecodeThreads; | 37 int decode_threads = kDecodeThreads; |
47 | 38 |
48 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 39 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
49 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); | 40 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); |
50 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) | 41 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) |
51 return decode_threads; | 42 return decode_threads; |
52 | 43 |
53 decode_threads = std::max(decode_threads, 0); | 44 decode_threads = std::max(decode_threads, 0); |
54 decode_threads = std::min(decode_threads, kMaxDecodeThreads); | 45 decode_threads = std::min(decode_threads, kMaxDecodeThreads); |
55 return decode_threads; | 46 return decode_threads; |
56 } | 47 } |
57 | 48 |
58 VpxVideoDecoder::VpxVideoDecoder( | 49 VpxVideoDecoder::VpxVideoDecoder( |
59 const scoped_refptr<base::MessageLoopProxy>& message_loop) | 50 const scoped_refptr<base::MessageLoopProxy>& message_loop) |
60 : message_loop_(message_loop), | 51 : message_loop_(message_loop), |
61 state_(kUninitialized), | 52 state_(kUninitialized) { |
62 vpx_codec_(NULL) { | |
63 } | 53 } |
64 | 54 |
65 VpxVideoDecoder::~VpxVideoDecoder() { | 55 VpxVideoDecoder::~VpxVideoDecoder() { |
66 DCHECK_EQ(kUninitialized, state_); | 56 DCHECK_EQ(kUninitialized, state_); |
67 CloseDecoder(); | |
68 } | 57 } |
69 | 58 |
70 void VpxVideoDecoder::Initialize( | 59 void VpxVideoDecoder::Initialize( |
71 const scoped_refptr<DemuxerStream>& stream, | 60 const scoped_refptr<DemuxerStream>& stream, |
72 const PipelineStatusCB& status_cb, | 61 const PipelineStatusCB& status_cb, |
73 const StatisticsCB& statistics_cb) { | 62 const StatisticsCB& statistics_cb) { |
74 DCHECK(message_loop_->BelongsToCurrentThread()); | 63 DCHECK(message_loop_->BelongsToCurrentThread()); |
75 DCHECK(!demuxer_stream_) << "Already initialized."; | 64 DCHECK(!demuxer_stream_) << "Already initialized."; |
76 | 65 |
77 if (!stream) { | 66 if (!stream) { |
(...skipping 15 matching lines...) Expand all Loading... | |
93 } | 82 } |
94 | 83 |
95 bool VpxVideoDecoder::ConfigureDecoder() { | 84 bool VpxVideoDecoder::ConfigureDecoder() { |
96 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); | 85 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); |
97 if (!config.IsValidConfig()) { | 86 if (!config.IsValidConfig()) { |
98 DLOG(ERROR) << "Invalid video stream config: " | 87 DLOG(ERROR) << "Invalid video stream config: " |
99 << config.AsHumanReadableString(); | 88 << config.AsHumanReadableString(); |
100 return false; | 89 return false; |
101 } | 90 } |
102 | 91 |
103 if (config.codec() != kCodecVP9) | 92 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
93 bool can_handle = false; | |
94 if (cmd_line->HasSwitch(switches::kEnableVp9Playback) && | |
95 config.codec() == kCodecVP9) { | |
96 can_handle = true; | |
97 } | |
98 if (cmd_line->HasSwitch(switches::kEnableVp8AlphaPlayback) && | |
99 config.codec() == kCodecVP8 && config.format() == VideoFrame::YV12A) { | |
100 can_handle = true; | |
101 } | |
102 if (!can_handle) | |
104 return false; | 103 return false; |
105 | 104 |
106 CloseDecoder(); | 105 vpx_codec_.reset(new vpx_codec_ctx()); |
107 | |
108 vpx_codec_ = new vpx_codec_ctx(); | |
109 vpx_codec_dec_cfg_t vpx_config = {0}; | 106 vpx_codec_dec_cfg_t vpx_config = {0}; |
scherkus (not reviewing)
2013/04/04 00:36:17
seems like this code is duplicated
how about refa
vignesh
2013/04/04 18:17:52
Done.
| |
110 vpx_config.w = config.coded_size().width(); | 107 vpx_config.w = config.coded_size().width(); |
111 vpx_config.h = config.coded_size().height(); | 108 vpx_config.h = config.coded_size().height(); |
112 vpx_config.threads = GetThreadCount(); | 109 vpx_config.threads = GetThreadCount(); |
113 | 110 |
114 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, | 111 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_.get(), |
scherkus (not reviewing)
2013/04/04 00:36:17
nit: AFAIK you don't need all the .get()s for scop
vignesh
2013/04/04 18:17:52
Nope. As it turns out, the .get()s are required. :
| |
115 vpx_codec_vp9_dx(), | 112 config.codec() == kCodecVP9 ? |
113 vpx_codec_vp9_dx() : | |
114 vpx_codec_vp8_dx(), | |
116 &vpx_config, | 115 &vpx_config, |
117 0); | 116 0); |
118 if (status != VPX_CODEC_OK) { | 117 if (status != VPX_CODEC_OK) { |
119 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; | 118 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; |
120 delete vpx_codec_; | |
121 vpx_codec_ = NULL; | |
122 return false; | 119 return false; |
123 } | 120 } |
124 | 121 |
122 if (config.format() == VideoFrame::YV12A) { | |
123 vpx_codec_alpha_.reset(new vpx_codec_ctx()); | |
124 vpx_codec_dec_cfg_t vpx_config_alpha = {0}; | |
125 | |
126 vpx_config_alpha.w = config.coded_size().width(); | |
127 vpx_config_alpha.h = config.coded_size().height(); | |
128 vpx_config_alpha.threads = GetThreadCount(); | |
129 | |
130 status = vpx_codec_dec_init(vpx_codec_alpha_.get(), | |
131 vpx_codec_vp8_dx(), | |
132 &vpx_config_alpha, | |
133 0); | |
134 if (status != VPX_CODEC_OK) { | |
135 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; | |
136 return false; | |
137 } | |
138 } | |
139 | |
125 return true; | 140 return true; |
126 } | 141 } |
127 | 142 |
128 void VpxVideoDecoder::CloseDecoder() { | |
129 if (vpx_codec_) { | |
130 vpx_codec_destroy(vpx_codec_); | |
131 delete vpx_codec_; | |
132 vpx_codec_ = NULL; | |
133 } | |
134 } | |
135 | |
136 void VpxVideoDecoder::Read(const ReadCB& read_cb) { | 143 void VpxVideoDecoder::Read(const ReadCB& read_cb) { |
137 DCHECK(message_loop_->BelongsToCurrentThread()); | 144 DCHECK(message_loop_->BelongsToCurrentThread()); |
138 DCHECK(!read_cb.is_null()); | 145 DCHECK(!read_cb.is_null()); |
139 CHECK_NE(state_, kUninitialized); | 146 CHECK_NE(state_, kUninitialized); |
140 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 147 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
141 read_cb_ = BindToCurrentLoop(read_cb); | 148 read_cb_ = BindToCurrentLoop(read_cb); |
142 | 149 |
143 // Return empty frames if decoding has finished. | 150 // Return empty frames if decoding has finished. |
144 if (state_ == kDecodeFinished) { | 151 if (state_ == kDecodeFinished) { |
145 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); | 152 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 } | 246 } |
240 | 247 |
241 scoped_refptr<VideoFrame> video_frame; | 248 scoped_refptr<VideoFrame> video_frame; |
242 if (!Decode(buffer, &video_frame)) { | 249 if (!Decode(buffer, &video_frame)) { |
243 state_ = kDecodeFinished; | 250 state_ = kDecodeFinished; |
244 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | 251 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
245 return; | 252 return; |
246 } | 253 } |
247 | 254 |
248 // Any successful decode counts! | 255 // Any successful decode counts! |
249 if (buffer->GetDataSize()) { | 256 if (buffer->GetDataSize() && buffer->GetSideDataSize()) { |
250 PipelineStatistics statistics; | 257 PipelineStatistics statistics; |
251 statistics.video_bytes_decoded = buffer->GetDataSize(); | 258 statistics.video_bytes_decoded = buffer->GetDataSize(); |
252 statistics_cb_.Run(statistics); | 259 statistics_cb_.Run(statistics); |
253 } | 260 } |
254 | 261 |
255 // If we didn't get a frame we need more data. | 262 // If we didn't get a frame we need more data. |
256 if (!video_frame) { | 263 if (!video_frame) { |
257 ReadFromDemuxerStream(); | 264 ReadFromDemuxerStream(); |
258 return; | 265 return; |
259 } | 266 } |
260 | 267 |
261 base::ResetAndReturn(&read_cb_).Run(kOk, video_frame); | 268 base::ResetAndReturn(&read_cb_).Run(kOk, video_frame); |
262 } | 269 } |
263 | 270 |
264 bool VpxVideoDecoder::Decode( | 271 bool VpxVideoDecoder::Decode( |
265 const scoped_refptr<DecoderBuffer>& buffer, | 272 const scoped_refptr<DecoderBuffer>& buffer, |
266 scoped_refptr<VideoFrame>* video_frame) { | 273 scoped_refptr<VideoFrame>* video_frame) { |
267 DCHECK(video_frame); | 274 DCHECK(video_frame); |
268 DCHECK(!buffer->IsEndOfStream()); | 275 DCHECK(!buffer->IsEndOfStream()); |
269 | 276 |
270 // Pass |buffer| to libvpx. | 277 // Pass |buffer| to libvpx. |
271 int64 timestamp = buffer->GetTimestamp().InMicroseconds(); | 278 int64 timestamp = buffer->GetTimestamp().InMicroseconds(); |
272 void* user_priv = reinterpret_cast<void*>(×tamp); | 279 void* user_priv = reinterpret_cast<void*>(×tamp); |
273 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_, | 280 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_.get(), |
274 buffer->GetData(), | 281 buffer->GetData(), |
275 buffer->GetDataSize(), | 282 buffer->GetDataSize(), |
276 user_priv, | 283 user_priv, |
277 0); | 284 0); |
278 if (status != VPX_CODEC_OK) { | 285 if (status != VPX_CODEC_OK) { |
279 LOG(ERROR) << "vpx_codec_decode() failed, status=" << status; | 286 LOG(ERROR) << "vpx_codec_decode() failed, status=" << status; |
280 return false; | 287 return false; |
281 } | 288 } |
282 | 289 |
283 // Gets pointer to decoded data. | 290 // Gets pointer to decoded data. |
284 vpx_codec_iter_t iter = NULL; | 291 vpx_codec_iter_t iter = NULL; |
285 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter); | 292 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_.get(), &iter); |
286 if (!vpx_image) { | 293 if (!vpx_image) { |
287 *video_frame = NULL; | 294 *video_frame = NULL; |
288 return true; | 295 return true; |
289 } | 296 } |
290 | 297 |
291 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { | 298 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { |
292 LOG(ERROR) << "Invalid output timestamp."; | 299 LOG(ERROR) << "Invalid output timestamp."; |
293 return false; | 300 return false; |
294 } | 301 } |
295 | 302 |
296 CopyVpxImageTo(vpx_image, video_frame); | 303 const vpx_image_t* vpx_image_alpha = NULL; |
304 if (vpx_codec_alpha_.get()) { | |
305 // Pass alpha data to libvpx. | |
306 int64 timestamp_alpha = buffer->GetTimestamp().InMicroseconds(); | |
307 void* user_priv_alpha = reinterpret_cast<void*>(×tamp_alpha); | |
308 const uint8* side_data_id_buf = buffer->GetSideData(); | |
309 // First 8 bytes of side data is side_data_id in big endian. | |
scherkus (not reviewing)
2013/04/04 00:36:17
nit: blank lines before //
vignesh
2013/04/04 18:17:52
Done.
| |
310 uint64_t side_data_id = side_data_id_buf[0]; | |
311 for (size_t i = 1; i < 8; ++i) { | |
312 side_data_id <<= 8; | |
313 side_data_id |= side_data_id_buf[i]; | |
scherkus (not reviewing)
2013/04/04 00:36:17
this assumes side data is at least 8 bytes
you'll
vignesh
2013/04/04 18:17:52
not sure what checks you want me to add, could you
| |
314 } | |
315 if (side_data_id == 1) { | |
316 status = vpx_codec_decode(vpx_codec_alpha_.get(), | |
317 buffer->GetSideData() + 8, | |
318 buffer->GetSideDataSize() - 8, | |
319 user_priv_alpha, | |
320 0); | |
321 | |
322 if (status != VPX_CODEC_OK) { | |
323 LOG(ERROR) << "vpx_codec_decode() failed on alpha, status=" << status; | |
324 return false; | |
325 } | |
326 | |
327 // Gets pointer to decoded data. | |
328 vpx_codec_iter_t iter_alpha = NULL; | |
329 vpx_image_alpha = vpx_codec_get_frame(vpx_codec_alpha_.get(), | |
330 &iter_alpha); | |
331 if (!vpx_image_alpha) { | |
332 *video_frame = NULL; | |
333 return true; | |
334 } | |
335 | |
336 if (vpx_image_alpha->user_priv != | |
337 reinterpret_cast<void*>(×tamp_alpha)) { | |
338 LOG(ERROR) << "Invalid output timestamp on alpha."; | |
339 return false; | |
340 } | |
341 } | |
342 } | |
343 | |
344 CopyVpxImageTo(vpx_image, vpx_image_alpha, video_frame); | |
297 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); | 345 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); |
298 return true; | 346 return true; |
299 } | 347 } |
300 | 348 |
301 void VpxVideoDecoder::DoReset() { | 349 void VpxVideoDecoder::DoReset() { |
302 DCHECK(read_cb_.is_null()); | 350 DCHECK(read_cb_.is_null()); |
303 | 351 |
304 state_ = kNormal; | 352 state_ = kNormal; |
305 reset_cb_.Run(); | 353 reset_cb_.Run(); |
306 reset_cb_.Reset(); | 354 reset_cb_.Reset(); |
307 } | 355 } |
308 | 356 |
309 void VpxVideoDecoder::CopyVpxImageTo( | 357 void VpxVideoDecoder::CopyVpxImageTo( |
310 const vpx_image* vpx_image, | 358 const struct vpx_image* vpx_image, |
359 const struct vpx_image* vpx_image_alpha, | |
311 scoped_refptr<VideoFrame>* video_frame) { | 360 scoped_refptr<VideoFrame>* video_frame) { |
312 CHECK(vpx_image); | 361 CHECK(vpx_image); |
313 CHECK_EQ(vpx_image->d_w % 2, 0U); | 362 CHECK_EQ(vpx_image->d_w % 2, 0U); |
314 CHECK_EQ(vpx_image->d_h % 2, 0U); | 363 CHECK_EQ(vpx_image->d_h % 2, 0U); |
315 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || | 364 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || |
316 vpx_image->fmt == VPX_IMG_FMT_YV12); | 365 vpx_image->fmt == VPX_IMG_FMT_YV12); |
317 | 366 |
318 gfx::Size size(vpx_image->d_w, vpx_image->d_h); | 367 gfx::Size size(vpx_image->d_w, vpx_image->d_h); |
319 gfx::Size natural_size = | 368 gfx::Size natural_size = |
320 demuxer_stream_->video_decoder_config().natural_size(); | 369 demuxer_stream_->video_decoder_config().natural_size(); |
321 | 370 |
322 *video_frame = VideoFrame::CreateFrame(VideoFrame::YV12, | 371 *video_frame = VideoFrame::CreateFrame(vpx_codec_alpha_.get() ? |
372 VideoFrame::YV12A : | |
373 VideoFrame::YV12, | |
323 size, | 374 size, |
324 gfx::Rect(size), | 375 gfx::Rect(size), |
325 natural_size, | 376 natural_size, |
326 kNoTimestamp()); | 377 kNoTimestamp()); |
378 | |
327 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], | 379 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], |
328 vpx_image->stride[VPX_PLANE_Y], | 380 vpx_image->stride[VPX_PLANE_Y], |
329 vpx_image->d_h, | 381 vpx_image->d_h, |
330 *video_frame); | 382 *video_frame); |
331 CopyUPlane(vpx_image->planes[VPX_PLANE_U], | 383 CopyUPlane(vpx_image->planes[VPX_PLANE_U], |
332 vpx_image->stride[VPX_PLANE_U], | 384 vpx_image->stride[VPX_PLANE_U], |
333 vpx_image->d_h / 2, | 385 vpx_image->d_h / 2, |
334 *video_frame); | 386 *video_frame); |
335 CopyVPlane(vpx_image->planes[VPX_PLANE_V], | 387 CopyVPlane(vpx_image->planes[VPX_PLANE_V], |
336 vpx_image->stride[VPX_PLANE_V], | 388 vpx_image->stride[VPX_PLANE_V], |
337 vpx_image->d_h / 2, | 389 vpx_image->d_h / 2, |
338 *video_frame); | 390 *video_frame); |
391 if (vpx_codec_alpha_.get()) { | |
scherkus (not reviewing)
2013/04/04 00:36:17
nit: I like to have early-returns when possible
i
vignesh
2013/04/04 18:17:52
Done.
| |
392 if (vpx_image_alpha) { | |
393 CopyAPlane(vpx_image_alpha->planes[VPX_PLANE_Y], | |
394 vpx_image->stride[VPX_PLANE_Y], | |
395 vpx_image->d_h, | |
396 *video_frame); | |
397 } else { | |
398 MakeOpaqueAPlane(vpx_image->stride[VPX_PLANE_Y], vpx_image->d_h, | |
399 *video_frame); | |
400 } | |
401 } | |
339 } | 402 } |
340 | 403 |
341 } // namespace media | 404 } // namespace media |
OLD | NEW |