Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: media/filters/vpx_video_decoder.cc

Issue 12263013: media: Add support for playback of VP8 Alpha video streams (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressing comments on patchset 5 Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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) {
78 status_cb.Run(PIPELINE_ERROR_DECODE); 67 status_cb.Run(PIPELINE_ERROR_DECODE);
79 return; 68 return;
80 } 69 }
81 70
82 demuxer_stream_ = stream; 71 demuxer_stream_ = stream;
83 statistics_cb_ = statistics_cb; 72 statistics_cb_ = statistics_cb;
84 73
85 if (!ConfigureDecoder()) { 74 if (!ConfigureDecoder()) {
86 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); 75 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
87 return; 76 return;
88 } 77 }
89 78
90 // Success! 79 // Success!
91 state_ = kNormal; 80 state_ = kNormal;
92 status_cb.Run(PIPELINE_OK); 81 status_cb.Run(PIPELINE_OK);
93 } 82 }
94 83
84 scoped_ptr<vpx_codec_ctx, VpxDeleter> InitializeVpxContext(
scherkus (not reviewing) 2013/04/04 22:54:00 should be static
vignesh 2013/04/05 19:06:58 Done.
85 scoped_ptr<vpx_codec_ctx, VpxDeleter> context,
86 const VideoDecoderConfig& config,
87 vpx_codec_err_t* status) {
scherkus (not reviewing) 2013/04/04 22:54:00 how about LOG(ERROR) the status code inside this f
vignesh 2013/04/05 19:06:58 Done.
88 context.reset(new vpx_codec_ctx());
89 vpx_codec_dec_cfg_t vpx_config = {0};
90 vpx_config.w = config.coded_size().width();
91 vpx_config.h = config.coded_size().height();
92 vpx_config.threads = GetThreadCount();
93
94 *status = vpx_codec_dec_init(context.get(),
95 config.codec() == kCodecVP9 ?
96 vpx_codec_vp9_dx() :
97 vpx_codec_vp8_dx(),
98 &vpx_config,
99 0);
100 return context.Pass();
101 }
102
95 bool VpxVideoDecoder::ConfigureDecoder() { 103 bool VpxVideoDecoder::ConfigureDecoder() {
96 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); 104 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config();
97 if (!config.IsValidConfig()) { 105 if (!config.IsValidConfig()) {
98 DLOG(ERROR) << "Invalid video stream config: " 106 DLOG(ERROR) << "Invalid video stream config: "
99 << config.AsHumanReadableString(); 107 << config.AsHumanReadableString();
100 return false; 108 return false;
101 } 109 }
102 110
103 if (config.codec() != kCodecVP9) 111 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
112 bool can_handle = false;
113 if (cmd_line->HasSwitch(switches::kEnableVp9Playback) &&
114 config.codec() == kCodecVP9) {
115 can_handle = true;
116 }
117 if (cmd_line->HasSwitch(switches::kEnableVp8AlphaPlayback) &&
118 config.codec() == kCodecVP8 && config.format() == VideoFrame::YV12A) {
119 can_handle = true;
120 }
121 if (!can_handle)
104 return false; 122 return false;
105 123
106 CloseDecoder(); 124 vpx_codec_err_t status;
107 125 vpx_codec_ = InitializeVpxContext(vpx_codec_.Pass(), config, &status);
108 vpx_codec_ = new vpx_codec_ctx();
109 vpx_codec_dec_cfg_t vpx_config = {0};
110 vpx_config.w = config.coded_size().width();
111 vpx_config.h = config.coded_size().height();
112 vpx_config.threads = GetThreadCount();
113
114 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_,
115 vpx_codec_vp9_dx(),
116 &vpx_config,
117 0);
118 if (status != VPX_CODEC_OK) { 126 if (status != VPX_CODEC_OK) {
119 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; 127 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status;
120 delete vpx_codec_;
121 vpx_codec_ = NULL;
122 return false; 128 return false;
123 } 129 }
124 130
131 if (config.format() == VideoFrame::YV12A) {
132 vpx_codec_alpha_ = InitializeVpxContext(vpx_codec_alpha_.Pass(), config,
133 &status);
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
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*>(&timestamp); 279 void* user_priv = reinterpret_cast<void*>(&timestamp);
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*>(&timestamp)) { 298 if (vpx_image->user_priv != reinterpret_cast<void*>(&timestamp)) {
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*>(&timestamp_alpha);
308 const uint8* side_data_id_buf = buffer->GetSideData();
309
310 // First 8 bytes of side data is side_data_id in big endian.
scherkus (not reviewing) 2013/04/04 22:54:00 I think you can use base/sys_byteorder.h uint64 s
vignesh 2013/04/05 19:06:58 Thanks :) Done.
311 uint64_t side_data_id = side_data_id_buf[0];
scherkus (not reviewing) 2013/04/04 22:54:00 nit: drop the _t suffix -- chromium code uses uint
vignesh 2013/04/05 19:06:58 Done.
312 for (size_t i = 1; i < 8; ++i) {
313 side_data_id <<= 8;
314 side_data_id |= side_data_id_buf[i];
scherkus (not reviewing) 2013/04/04 22:54:00 right now the code always assumes GetData() is non
vignesh 2013/04/05 19:06:58 Done.
315 }
316 if (side_data_id == 1) {
317 status = vpx_codec_decode(vpx_codec_alpha_.get(),
318 buffer->GetSideData() + 8,
319 buffer->GetSideDataSize() - 8,
320 user_priv_alpha,
321 0);
322
323 if (status != VPX_CODEC_OK) {
324 LOG(ERROR) << "vpx_codec_decode() failed on alpha, status=" << status;
325 return false;
326 }
327
328 // Gets pointer to decoded data.
329 vpx_codec_iter_t iter_alpha = NULL;
330 vpx_image_alpha = vpx_codec_get_frame(vpx_codec_alpha_.get(),
331 &iter_alpha);
332 if (!vpx_image_alpha) {
333 *video_frame = NULL;
334 return true;
335 }
336
337 if (vpx_image_alpha->user_priv !=
338 reinterpret_cast<void*>(&timestamp_alpha)) {
339 LOG(ERROR) << "Invalid output timestamp on alpha.";
340 return false;
341 }
342 }
343 }
344
345 CopyVpxImageTo(vpx_image, vpx_image_alpha, video_frame);
297 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); 346 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp));
298 return true; 347 return true;
299 } 348 }
300 349
301 void VpxVideoDecoder::DoReset() { 350 void VpxVideoDecoder::DoReset() {
302 DCHECK(read_cb_.is_null()); 351 DCHECK(read_cb_.is_null());
303 352
304 state_ = kNormal; 353 state_ = kNormal;
305 reset_cb_.Run(); 354 reset_cb_.Run();
306 reset_cb_.Reset(); 355 reset_cb_.Reset();
307 } 356 }
308 357
309 void VpxVideoDecoder::CopyVpxImageTo( 358 void VpxVideoDecoder::CopyVpxImageTo(
310 const vpx_image* vpx_image, 359 const struct vpx_image* vpx_image,
360 const struct vpx_image* vpx_image_alpha,
311 scoped_refptr<VideoFrame>* video_frame) { 361 scoped_refptr<VideoFrame>* video_frame) {
312 CHECK(vpx_image); 362 CHECK(vpx_image);
313 CHECK_EQ(vpx_image->d_w % 2, 0U); 363 CHECK_EQ(vpx_image->d_w % 2, 0U);
314 CHECK_EQ(vpx_image->d_h % 2, 0U); 364 CHECK_EQ(vpx_image->d_h % 2, 0U);
315 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || 365 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 ||
316 vpx_image->fmt == VPX_IMG_FMT_YV12); 366 vpx_image->fmt == VPX_IMG_FMT_YV12);
317 367
318 gfx::Size size(vpx_image->d_w, vpx_image->d_h); 368 gfx::Size size(vpx_image->d_w, vpx_image->d_h);
319 gfx::Size natural_size = 369 gfx::Size natural_size =
320 demuxer_stream_->video_decoder_config().natural_size(); 370 demuxer_stream_->video_decoder_config().natural_size();
321 371
322 *video_frame = VideoFrame::CreateFrame(VideoFrame::YV12, 372 *video_frame = VideoFrame::CreateFrame(vpx_codec_alpha_.get() ?
373 VideoFrame::YV12A :
374 VideoFrame::YV12,
323 size, 375 size,
324 gfx::Rect(size), 376 gfx::Rect(size),
325 natural_size, 377 natural_size,
326 kNoTimestamp()); 378 kNoTimestamp());
379
327 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], 380 CopyYPlane(vpx_image->planes[VPX_PLANE_Y],
328 vpx_image->stride[VPX_PLANE_Y], 381 vpx_image->stride[VPX_PLANE_Y],
329 vpx_image->d_h, 382 vpx_image->d_h,
330 *video_frame); 383 *video_frame);
331 CopyUPlane(vpx_image->planes[VPX_PLANE_U], 384 CopyUPlane(vpx_image->planes[VPX_PLANE_U],
332 vpx_image->stride[VPX_PLANE_U], 385 vpx_image->stride[VPX_PLANE_U],
333 vpx_image->d_h / 2, 386 vpx_image->d_h / 2,
334 *video_frame); 387 *video_frame);
335 CopyVPlane(vpx_image->planes[VPX_PLANE_V], 388 CopyVPlane(vpx_image->planes[VPX_PLANE_V],
336 vpx_image->stride[VPX_PLANE_V], 389 vpx_image->stride[VPX_PLANE_V],
337 vpx_image->d_h / 2, 390 vpx_image->d_h / 2,
338 *video_frame); 391 *video_frame);
392 if (!vpx_codec_alpha_.get())
393 return;
394 if (!vpx_image_alpha) {
395 MakeOpaqueAPlane(vpx_image->stride[VPX_PLANE_Y], vpx_image->d_h,
396 *video_frame);
397 return;
398 }
399 CopyAPlane(vpx_image_alpha->planes[VPX_PLANE_Y],
400 vpx_image->stride[VPX_PLANE_Y],
401 vpx_image->d_h,
402 *video_frame);
339 } 403 }
340 404
341 } // namespace media 405 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698