Chromium Code Reviews| 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" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 53 decode_threads = std::max(decode_threads, 0); | 53 decode_threads = std::max(decode_threads, 0); |
| 54 decode_threads = std::min(decode_threads, kMaxDecodeThreads); | 54 decode_threads = std::min(decode_threads, kMaxDecodeThreads); |
| 55 return decode_threads; | 55 return decode_threads; |
| 56 } | 56 } |
| 57 | 57 |
| 58 VpxVideoDecoder::VpxVideoDecoder( | 58 VpxVideoDecoder::VpxVideoDecoder( |
| 59 const scoped_refptr<base::MessageLoopProxy>& message_loop) | 59 const scoped_refptr<base::MessageLoopProxy>& message_loop) |
| 60 : message_loop_(message_loop), | 60 : message_loop_(message_loop), |
| 61 state_(kUninitialized), | 61 state_(kUninitialized), |
| 62 vpx_codec_(NULL) { | 62 vpx_codec_(NULL), |
| 63 vpx_codec_alpha_(NULL) { | |
| 63 } | 64 } |
| 64 | 65 |
| 65 VpxVideoDecoder::~VpxVideoDecoder() { | 66 VpxVideoDecoder::~VpxVideoDecoder() { |
| 66 DCHECK_EQ(kUninitialized, state_); | 67 DCHECK_EQ(kUninitialized, state_); |
| 67 CloseDecoder(); | 68 CloseDecoder(); |
| 68 } | 69 } |
| 69 | 70 |
| 70 void VpxVideoDecoder::Initialize( | 71 void VpxVideoDecoder::Initialize( |
| 71 const scoped_refptr<DemuxerStream>& stream, | 72 const scoped_refptr<DemuxerStream>& stream, |
| 72 const PipelineStatusCB& status_cb, | 73 const PipelineStatusCB& status_cb, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 93 } | 94 } |
| 94 | 95 |
| 95 bool VpxVideoDecoder::ConfigureDecoder() { | 96 bool VpxVideoDecoder::ConfigureDecoder() { |
| 96 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); | 97 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); |
| 97 if (!config.IsValidConfig()) { | 98 if (!config.IsValidConfig()) { |
| 98 DLOG(ERROR) << "Invalid video stream config: " | 99 DLOG(ERROR) << "Invalid video stream config: " |
| 99 << config.AsHumanReadableString(); | 100 << config.AsHumanReadableString(); |
| 100 return false; | 101 return false; |
| 101 } | 102 } |
| 102 | 103 |
| 103 if (config.codec() != kCodecVP9) | 104 format_ = config.format(); |
| 104 return false; | 105 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 106 bool can_handle = false; | |
| 107 if(cmd_line->HasSwitch(switches::kEnableVp9Playback) && | |
| 108 config.codec() == kCodecVP9) { | |
|
fgalligan1
2013/02/12 01:20:58
Line up.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 109 can_handle = true; | |
| 110 } | |
| 111 if(cmd_line->HasSwitch(switches::kEnableVp8AlphaPlayback) && | |
| 112 config.codec() == kCodecVP8 && format_ == VideoFrame::YV12A) { | |
|
fgalligan1
2013/02/12 01:20:58
Line up.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 113 can_handle = true; | |
| 114 } | |
| 115 if(!can_handle) | |
| 116 return false; | |
| 105 | 117 |
| 106 CloseDecoder(); | 118 CloseDecoder(); |
| 107 | 119 |
| 108 vpx_codec_ = new vpx_codec_ctx(); | 120 vpx_codec_ = new vpx_codec_ctx(); |
| 109 vpx_codec_dec_cfg_t vpx_config = {0}; | 121 vpx_codec_dec_cfg_t vpx_config = {0}; |
| 122 | |
|
fgalligan1
2013/02/12 01:20:58
nit: Remove line
vigneshv
2013/02/14 19:06:14
Done.
| |
| 110 vpx_config.w = config.coded_size().width(); | 123 vpx_config.w = config.coded_size().width(); |
| 111 vpx_config.h = config.coded_size().height(); | 124 vpx_config.h = config.coded_size().height(); |
| 112 vpx_config.threads = GetThreadCount(); | 125 vpx_config.threads = GetThreadCount(); |
| 113 | 126 |
| 114 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, | 127 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, |
| 115 vpx_codec_vp9_dx(), | 128 config.codec() == kCodecVP9 ? |
| 129 vpx_codec_vp9_dx() : | |
|
fgalligan1
2013/02/12 01:20:58
nit: 4 space indent.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 130 vpx_codec_vp8_dx(), | |
| 116 &vpx_config, | 131 &vpx_config, |
| 117 0); | 132 0); |
| 118 if (status != VPX_CODEC_OK) { | 133 if (status != VPX_CODEC_OK) { |
| 119 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; | 134 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; |
| 120 delete vpx_codec_; | 135 delete vpx_codec_; |
| 121 vpx_codec_ = NULL; | 136 vpx_codec_ = NULL; |
| 122 return false; | 137 return false; |
| 123 } | 138 } |
| 124 | 139 |
| 140 if(format_ == VideoFrame::YV12A) { | |
| 141 vpx_codec_alpha_ = new vpx_codec_ctx(); | |
| 142 vpx_codec_dec_cfg_t vpx_config_alpha = {0}; | |
| 143 | |
| 144 vpx_config_alpha.w = config.coded_size().width(); | |
| 145 vpx_config_alpha.h = config.coded_size().height(); | |
| 146 vpx_config_alpha.threads = GetThreadCount(); | |
| 147 | |
| 148 status = vpx_codec_dec_init(vpx_codec_alpha_, | |
| 149 vpx_codec_vp8_dx(), | |
| 150 &vpx_config_alpha, | |
| 151 0); | |
| 152 if (status != VPX_CODEC_OK) { | |
| 153 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; | |
| 154 delete vpx_codec_alpha_; | |
| 155 vpx_codec_alpha_ = NULL; | |
| 156 return false; | |
| 157 } | |
| 158 } | |
| 159 | |
| 125 return true; | 160 return true; |
| 126 } | 161 } |
| 127 | 162 |
| 128 void VpxVideoDecoder::CloseDecoder() { | 163 void VpxVideoDecoder::CloseDecoder() { |
| 129 if (vpx_codec_) { | 164 if (vpx_codec_) { |
| 130 vpx_codec_destroy(vpx_codec_); | 165 vpx_codec_destroy(vpx_codec_); |
| 131 delete vpx_codec_; | 166 delete vpx_codec_; |
| 132 vpx_codec_ = NULL; | 167 vpx_codec_ = NULL; |
| 133 } | 168 } |
| 169 if (vpx_codec_alpha_) { | |
| 170 vpx_codec_destroy(vpx_codec_alpha_); | |
| 171 delete vpx_codec_alpha_; | |
| 172 vpx_codec_alpha_ = NULL; | |
| 173 } | |
| 134 } | 174 } |
| 135 | 175 |
| 136 void VpxVideoDecoder::Read(const ReadCB& read_cb) { | 176 void VpxVideoDecoder::Read(const ReadCB& read_cb) { |
| 137 DCHECK(message_loop_->BelongsToCurrentThread()); | 177 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 138 DCHECK(!read_cb.is_null()); | 178 DCHECK(!read_cb.is_null()); |
| 139 CHECK_NE(state_, kUninitialized); | 179 CHECK_NE(state_, kUninitialized); |
| 140 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 180 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
| 141 read_cb_ = BindToCurrentLoop(read_cb); | 181 read_cb_ = BindToCurrentLoop(read_cb); |
| 142 | 182 |
| 143 // Return empty frames if decoding has finished. | 183 // Return empty frames if decoding has finished. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 } | 279 } |
| 240 | 280 |
| 241 scoped_refptr<VideoFrame> video_frame; | 281 scoped_refptr<VideoFrame> video_frame; |
| 242 if (!Decode(buffer, &video_frame)) { | 282 if (!Decode(buffer, &video_frame)) { |
| 243 state_ = kDecodeFinished; | 283 state_ = kDecodeFinished; |
| 244 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | 284 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| 245 return; | 285 return; |
| 246 } | 286 } |
| 247 | 287 |
| 248 // Any successful decode counts! | 288 // Any successful decode counts! |
| 249 if (buffer->GetDataSize()) { | 289 if (buffer->GetDataSize() && buffer->GetSideDataSize()) { |
| 250 PipelineStatistics statistics; | 290 PipelineStatistics statistics; |
| 251 statistics.video_bytes_decoded = buffer->GetDataSize(); | 291 statistics.video_bytes_decoded = buffer->GetDataSize(); |
| 252 statistics_cb_.Run(statistics); | 292 statistics_cb_.Run(statistics); |
| 253 } | 293 } |
| 254 | 294 |
| 255 // If we didn't get a frame we need more data. | 295 // If we didn't get a frame we need more data. |
| 256 if (!video_frame) { | 296 if (!video_frame) { |
| 257 ReadFromDemuxerStream(); | 297 ReadFromDemuxerStream(); |
| 258 return; | 298 return; |
| 259 } | 299 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 286 if (!vpx_image) { | 326 if (!vpx_image) { |
| 287 *video_frame = NULL; | 327 *video_frame = NULL; |
| 288 return true; | 328 return true; |
| 289 } | 329 } |
| 290 | 330 |
| 291 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { | 331 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { |
| 292 LOG(ERROR) << "Invalid output timestamp."; | 332 LOG(ERROR) << "Invalid output timestamp."; |
| 293 return false; | 333 return false; |
| 294 } | 334 } |
| 295 | 335 |
| 296 CopyVpxImageTo(vpx_image, video_frame); | 336 const vpx_image_t* vpx_image_alpha = NULL; |
| 337 if(format_ == VideoFrame::YV12A) { | |
| 338 | |
| 339 // Pass alpha data to libvpx. | |
| 340 int64 timestamp_alpha = buffer->GetTimestamp().InMicroseconds(); | |
| 341 void *user_priv_alpha = reinterpret_cast<void*>(×tamp_alpha); | |
|
fgalligan1
2013/02/12 01:20:58
nit: asterisk with type.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 342 uint64_t side_data_id = *((uint64_t*)buffer->GetSideData()); | |
|
fgalligan1
2013/02/12 01:20:58
Use c++ cast.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 343 if(side_data_id == 1) { | |
|
fgalligan1
2013/02/12 01:20:58
Space between if and (
vigneshv
2013/02/14 19:06:14
Done.
| |
| 344 status = vpx_codec_decode(vpx_codec_alpha_, | |
| 345 buffer->GetSideData() + sizeof(uint64_t), | |
| 346 buffer->GetSideDataSize() - sizeof(uint64_t), | |
| 347 user_priv_alpha, | |
| 348 0); | |
| 349 | |
| 350 if (status != VPX_CODEC_OK) { | |
| 351 LOG(ERROR) << "vpx_codec_decode() failed on alpha, status=" << status; | |
| 352 return false; | |
| 353 } | |
| 354 | |
| 355 // Gets pointer to decoded data. | |
| 356 vpx_codec_iter_t iter_alpha = NULL; | |
| 357 vpx_image_alpha = vpx_codec_get_frame(vpx_codec_alpha_, &iter_alpha); | |
| 358 if (!vpx_image_alpha) { | |
| 359 *video_frame = NULL; | |
| 360 return true; | |
| 361 } | |
| 362 | |
| 363 if (vpx_image_alpha->user_priv != | |
| 364 reinterpret_cast<void*>(×tamp_alpha)) { | |
|
fgalligan1
2013/02/12 01:20:58
Line up.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 365 LOG(ERROR) << "Invalid output timestamp on alpha."; | |
| 366 return false; | |
| 367 } | |
| 368 } | |
| 369 } | |
| 370 | |
| 371 CopyVpxImageTo(vpx_image, vpx_image_alpha, video_frame); | |
| 297 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); | 372 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); |
| 298 return true; | 373 return true; |
| 299 } | 374 } |
| 300 | 375 |
| 301 void VpxVideoDecoder::DoReset() { | 376 void VpxVideoDecoder::DoReset() { |
| 302 DCHECK(read_cb_.is_null()); | 377 DCHECK(read_cb_.is_null()); |
| 303 | 378 |
| 304 state_ = kNormal; | 379 state_ = kNormal; |
| 305 reset_cb_.Run(); | 380 reset_cb_.Run(); |
| 306 reset_cb_.Reset(); | 381 reset_cb_.Reset(); |
| 307 } | 382 } |
| 308 | 383 |
| 309 void VpxVideoDecoder::CopyVpxImageTo( | 384 void VpxVideoDecoder::CopyVpxImageTo( |
| 310 const vpx_image* vpx_image, | 385 const struct vpx_image* vpx_image, |
| 386 const struct vpx_image* vpx_image_alpha, | |
| 311 scoped_refptr<VideoFrame>* video_frame) { | 387 scoped_refptr<VideoFrame>* video_frame) { |
| 312 CHECK(vpx_image); | 388 CHECK(vpx_image); |
| 313 CHECK_EQ(vpx_image->d_w % 2, 0U); | 389 CHECK_EQ(vpx_image->d_w % 2, 0U); |
| 314 CHECK_EQ(vpx_image->d_h % 2, 0U); | 390 CHECK_EQ(vpx_image->d_h % 2, 0U); |
| 315 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || | 391 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || |
| 316 vpx_image->fmt == VPX_IMG_FMT_YV12); | 392 vpx_image->fmt == VPX_IMG_FMT_YV12); |
| 317 | 393 |
| 318 gfx::Size size(vpx_image->d_w, vpx_image->d_h); | 394 gfx::Size size(vpx_image->d_w, vpx_image->d_h); |
| 319 gfx::Size natural_size = | 395 gfx::Size natural_size = |
| 320 demuxer_stream_->video_decoder_config().natural_size(); | 396 demuxer_stream_->video_decoder_config().natural_size(); |
| 321 | 397 |
| 322 *video_frame = VideoFrame::CreateFrame(VideoFrame::YV12, | 398 *video_frame = VideoFrame::CreateFrame(format_, |
| 323 size, | 399 size, |
| 324 gfx::Rect(size), | 400 gfx::Rect(size), |
| 325 natural_size, | 401 natural_size, |
| 326 kNoTimestamp()); | 402 kNoTimestamp()); |
| 403 | |
| 327 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], | 404 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], |
| 328 vpx_image->stride[VPX_PLANE_Y], | 405 vpx_image->stride[VPX_PLANE_Y], |
| 329 vpx_image->d_h, | 406 vpx_image->d_h, |
| 330 *video_frame); | 407 *video_frame); |
| 331 CopyUPlane(vpx_image->planes[VPX_PLANE_U], | 408 CopyUPlane(vpx_image->planes[VPX_PLANE_U], |
| 332 vpx_image->stride[VPX_PLANE_U], | 409 vpx_image->stride[VPX_PLANE_U], |
| 333 vpx_image->d_h / 2, | 410 vpx_image->d_h / 2, |
| 334 *video_frame); | 411 *video_frame); |
| 335 CopyVPlane(vpx_image->planes[VPX_PLANE_V], | 412 CopyVPlane(vpx_image->planes[VPX_PLANE_V], |
| 336 vpx_image->stride[VPX_PLANE_V], | 413 vpx_image->stride[VPX_PLANE_V], |
| 337 vpx_image->d_h / 2, | 414 vpx_image->d_h / 2, |
| 338 *video_frame); | 415 *video_frame); |
| 416 if(format_ == VideoFrame::YV12A) { | |
| 417 if(vpx_image_alpha) { | |
| 418 CopyAPlane(vpx_image_alpha->planes[VPX_PLANE_Y], | |
| 419 vpx_image->stride[VPX_PLANE_Y], | |
| 420 vpx_image->d_h, | |
| 421 *video_frame); | |
| 422 } else { | |
| 423 /* there is no alpha. set it to opaque */ | |
| 424 uint64_t alpha_plane_size = vpx_image->stride[VPX_PLANE_Y] | |
| 425 * vpx_image->d_h; | |
|
fgalligan1
2013/02/12 01:20:58
I would put vpx_image->stride on the next line ind
vigneshv
2013/02/14 19:06:14
Done.
| |
| 426 unsigned char *alpha_plane = (unsigned char*)malloc(alpha_plane_size); | |
|
fgalligan1
2013/02/12 01:20:58
nit: asterisk with type. Use C++ cast.
vigneshv
2013/02/14 19:06:14
Done.
| |
| 427 memset(alpha_plane, 255, alpha_plane_size); | |
| 428 CopyAPlane(alpha_plane, | |
| 429 vpx_image->stride[VPX_PLANE_Y], | |
| 430 vpx_image->d_h, | |
| 431 *video_frame); | |
| 432 free(alpha_plane); | |
| 433 } | |
| 434 } | |
| 339 } | 435 } |
| 340 | 436 |
| 341 } // namespace media | 437 } // namespace media |
| OLD | NEW |