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