Chromium Code Reviews| 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/libvpx_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) \ | |
| 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 // | |
| 42 // Handling decoding on separate threads also frees up the pipeline thread to | |
| 43 // continue processing. Although it'd be nice to have the option of a single | |
| 44 // decoding thread, FFmpeg treats having one thread the same as having zero | |
|
fgalligan1
2012/12/10 07:47:12
FFmpeg? :)
Tom Finegan
2012/12/13 09:57:26
Done.
| |
| 45 // threads (i.e., avcodec_decode_video() will execute on the calling thread). | |
| 46 // Yet another reason for having two threads :) | |
| 47 static const int kDecodeThreads = 2; | |
| 48 static const int kMaxDecodeThreads = 16; | |
| 49 | |
| 50 // Returns the number of threads given the FFmpeg CodecID. Also inspects the | |
| 51 // command line for a valid --video-threads flag. | |
| 52 static int GetThreadCount() { | |
| 53 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. | |
| 54 int decode_threads = kDecodeThreads; | |
| 55 | |
| 56 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
| 57 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); | |
| 58 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) | |
| 59 return decode_threads; | |
| 60 | |
| 61 decode_threads = std::max(decode_threads, 0); | |
| 62 decode_threads = std::min(decode_threads, kMaxDecodeThreads); | |
| 63 return decode_threads; | |
| 64 } | |
| 65 | |
| 66 LibvpxVideoDecoder::LibvpxVideoDecoder( | |
| 67 const scoped_refptr<base::MessageLoopProxy>& message_loop) | |
| 68 : message_loop_(message_loop), | |
| 69 state_(kUninitialized), | |
| 70 frame_num_(0), | |
| 71 vpx_codec_(NULL) { | |
| 72 } | |
| 73 | |
| 74 LibvpxVideoDecoder::~LibvpxVideoDecoder() { | |
| 75 DCHECK_EQ(kUninitialized, state_); | |
| 76 CloseDecoder(); | |
| 77 } | |
| 78 | |
| 79 void LibvpxVideoDecoder::Initialize( | |
| 80 const scoped_refptr<DemuxerStream>& stream, | |
| 81 const PipelineStatusCB& status_cb, | |
| 82 const StatisticsCB& statistics_cb) { | |
| 83 if (!message_loop_->BelongsToCurrentThread()) { | |
| 84 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 85 &LibvpxVideoDecoder::Initialize, this, | |
| 86 stream, status_cb, statistics_cb)); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 DCHECK(!demuxer_stream_) << "Already initialized."; | |
| 91 | |
| 92 if (!stream) { | |
| 93 status_cb.Run(PIPELINE_ERROR_DECODE); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 demuxer_stream_ = stream; | |
| 98 statistics_cb_ = statistics_cb; | |
| 99 | |
| 100 if (!ConfigureDecoder()) { | |
| 101 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 // Success! | |
| 106 state_ = kNormal; | |
| 107 status_cb.Run(PIPELINE_OK); | |
| 108 } | |
| 109 | |
| 110 bool LibvpxVideoDecoder::ConfigureDecoder() { | |
| 111 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); | |
| 112 if (!config.IsValidConfig()) { | |
| 113 DLOG(ERROR) << "Invalid video stream config: " | |
| 114 << config.AsHumanReadableString(); | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 if (config.codec() != kCodecVP8) | |
|
fgalligan1
2012/12/10 07:47:12
Maybe add a check for VP9 and then comment this co
Tom Finegan
2012/12/13 09:57:26
Changed it to VP9.
| |
| 119 return false; | |
| 120 | |
| 121 CloseDecoder(); | |
| 122 | |
| 123 vpx_codec_ = new vpx_codec_ctx(); | |
| 124 vpx_codec_dec_cfg_t vpx_config = {0}; | |
| 125 vpx_config.w = config.coded_size().width(); | |
| 126 vpx_config.h = config.coded_size().height(); | |
| 127 vpx_config.threads = kDecodeThreads; | |
| 128 | |
| 129 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, | |
| 130 vpx_codec_vp8_dx(), | |
| 131 &vpx_config, | |
| 132 0); | |
| 133 if (status != VPX_CODEC_OK) { | |
| 134 LOG(ERROR) << "ConfigureDecoder(): vpx_codec_dec_init failed, status=" | |
| 135 << status; | |
| 136 delete vpx_codec_; | |
| 137 vpx_codec_ = NULL; | |
|
xhwang
2012/12/10 01:41:11
return false?
Tom Finegan
2012/12/13 09:57:26
Done.
| |
| 138 } | |
| 139 | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 void LibvpxVideoDecoder::CloseDecoder() { | |
| 144 if (vpx_codec_) { | |
| 145 vpx_codec_destroy(vpx_codec_); | |
| 146 delete vpx_codec_; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void LibvpxVideoDecoder::Read(const ReadCB& read_cb) { | |
| 151 // Complete operation asynchronously on different stack of execution as per | |
| 152 // the API contract of VideoDecoder::Read() | |
| 153 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 154 &LibvpxVideoDecoder::DoRead, this, read_cb)); | |
| 155 } | |
| 156 | |
| 157 void LibvpxVideoDecoder::Reset(const base::Closure& closure) { | |
| 158 if (!message_loop_->BelongsToCurrentThread()) { | |
| 159 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 160 &LibvpxVideoDecoder::Reset, this, closure)); | |
| 161 return; | |
| 162 } | |
| 163 | |
| 164 DCHECK(reset_cb_.is_null()); | |
| 165 reset_cb_ = closure; | |
| 166 | |
| 167 //if (decryptor_) | |
| 168 // decryptor_->CancelDecrypt(Decryptor::kVideo); | |
| 169 | |
| 170 // Defer the reset if a read is pending. | |
| 171 if (!read_cb_.is_null()) | |
| 172 return; | |
| 173 | |
| 174 DoReset(); | |
| 175 | |
| 176 | |
| 177 if (!message_loop_->BelongsToCurrentThread()) { | |
| 178 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 179 &LibvpxVideoDecoder::Reset, this, closure)); | |
| 180 return; | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void LibvpxVideoDecoder::Stop(const base::Closure& closure) { | |
| 185 if (!message_loop_->BelongsToCurrentThread()) { | |
| 186 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 187 &LibvpxVideoDecoder::Stop, this, closure)); | |
| 188 return; | |
| 189 } | |
| 190 | |
| 191 if (state_ == kUninitialized) { | |
| 192 closure.Run(); | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 //if (decryptor_) | |
| 197 // decryptor_->CancelDecrypt(Decryptor::kVideo); | |
| 198 | |
| 199 if (!read_cb_.is_null()) | |
| 200 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
| 201 | |
| 202 //ReleaseFFmpegResources(); | |
| 203 state_ = kUninitialized; | |
| 204 closure.Run(); | |
| 205 } | |
| 206 | |
| 207 void LibvpxVideoDecoder::DoRead(const ReadCB& read_cb) { | |
| 208 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 209 DCHECK(!read_cb.is_null()); | |
| 210 CHECK_NE(state_, kUninitialized); | |
| 211 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | |
| 212 | |
| 213 // Return empty frames if decoding has finished. | |
| 214 if (state_ == kDecodeFinished) { | |
| 215 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 read_cb_ = read_cb; | |
| 220 ReadFromDemuxerStream(); | |
| 221 } | |
| 222 | |
| 223 void LibvpxVideoDecoder::ReadFromDemuxerStream() { | |
| 224 DCHECK_NE(state_, kUninitialized); | |
| 225 DCHECK_NE(state_, kDecodeFinished); | |
| 226 DCHECK(!read_cb_.is_null()); | |
| 227 | |
| 228 demuxer_stream_->Read(base::Bind( | |
| 229 &LibvpxVideoDecoder::DoDecryptOrDecodeBuffer, this)); | |
| 230 } | |
| 231 | |
| 232 void LibvpxVideoDecoder::DoDecryptOrDecodeBuffer( | |
| 233 DemuxerStream::Status status, | |
| 234 const scoped_refptr<DecoderBuffer>& buffer) { | |
| 235 if (!message_loop_->BelongsToCurrentThread()) { | |
| 236 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 237 &LibvpxVideoDecoder::DoDecryptOrDecodeBuffer, this, status, buffer)); | |
| 238 return; | |
| 239 } | |
| 240 | |
| 241 DCHECK_NE(state_, kDecodeFinished); | |
| 242 DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status; | |
| 243 | |
| 244 if (state_ == kUninitialized) | |
| 245 return; | |
| 246 | |
| 247 DCHECK(!read_cb_.is_null()); | |
| 248 | |
| 249 if (!reset_cb_.is_null()) { | |
| 250 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
| 251 DoReset(); | |
| 252 return; | |
| 253 } | |
| 254 | |
| 255 if (status == DemuxerStream::kAborted) { | |
| 256 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
| 257 return; | |
| 258 } | |
| 259 | |
| 260 if (status == DemuxerStream::kConfigChanged) { | |
| 261 if (!ConfigureDecoder()) { | |
| 262 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
| 263 return; | |
| 264 } | |
| 265 | |
| 266 ReadFromDemuxerStream(); | |
| 267 return; | |
| 268 } | |
| 269 | |
| 270 DCHECK_EQ(status, DemuxerStream::kOk); | |
| 271 | |
| 272 /* | |
| 273 if (buffer->GetDecryptConfig() && buffer->GetDataSize()) { | |
| 274 decryptor_->Decrypt(Decryptor::kVideo, | |
| 275 buffer, | |
| 276 base::Bind(&FFmpegVideoDecoder::BufferDecrypted, this)); | |
| 277 return; | |
| 278 } | |
| 279 */ | |
| 280 | |
| 281 DecodeBuffer(buffer); | |
| 282 } | |
| 283 | |
| 284 void LibvpxVideoDecoder::DecodeBuffer( | |
| 285 const scoped_refptr<DecoderBuffer>& buffer) { | |
| 286 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 287 DCHECK_NE(state_, kUninitialized); | |
| 288 DCHECK_NE(state_, kDecodeFinished); | |
| 289 DCHECK(reset_cb_.is_null()); | |
| 290 DCHECK(!read_cb_.is_null()); | |
| 291 DCHECK(buffer); | |
| 292 | |
| 293 // During decode, because reads are issued asynchronously, it is possible to | |
| 294 // receive multiple end of stream buffers since each read is acked. When the | |
| 295 // first end of stream buffer is read, FFmpeg may still have frames queued | |
| 296 // up in the decoder so we need to go through the decode loop until it stops | |
| 297 // giving sensible data. After that, the decoder should output empty | |
| 298 // frames. There are three states the decoder can be in: | |
| 299 // | |
| 300 // kNormal: This is the starting state. Buffers are decoded. Decode errors | |
| 301 // are discarded. | |
| 302 // kFlushCodec: There isn't any more input data. Call avcodec_decode_video2 | |
| 303 // until no more data is returned to flush out remaining | |
| 304 // frames. The input buffer is ignored at this point. | |
| 305 // kDecodeFinished: All calls return empty frames. | |
| 306 // | |
| 307 // These are the possible state transitions. | |
| 308 // | |
| 309 // kNormal -> kFlushCodec: | |
| 310 // When buffer->IsEndOfStream() is first true. | |
| 311 // kNormal -> kDecodeFinished: | |
| 312 // A decoding error occurs and decoding needs to stop. | |
| 313 // kFlushCodec -> kDecodeFinished: | |
| 314 // When avcodec_decode_video2() returns 0 data or errors out. | |
| 315 // (any state) -> kNormal: | |
| 316 // Any time Reset() is called. | |
| 317 | |
| 318 // Transition to kFlushCodec on the first end of stream buffer. | |
| 319 if (state_ == kNormal && buffer->IsEndOfStream()) { | |
| 320 state_ = kFlushCodec; | |
| 321 } | |
| 322 | |
| 323 scoped_refptr<VideoFrame> video_frame; | |
| 324 if (!Decode(buffer, &video_frame)) { | |
| 325 state_ = kDecodeFinished; | |
| 326 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
| 327 return; | |
| 328 } | |
| 329 | |
| 330 // Any successful decode counts! | |
| 331 if (buffer->GetDataSize()) { | |
| 332 PipelineStatistics statistics; | |
| 333 statistics.video_bytes_decoded = buffer->GetDataSize(); | |
| 334 statistics_cb_.Run(statistics); | |
| 335 } | |
| 336 | |
| 337 // If we didn't get a frame then we've either completely finished decoding or | |
| 338 // we need more data. | |
| 339 if (!video_frame) { | |
| 340 if (state_ == kFlushCodec) { | |
| 341 state_ = kDecodeFinished; | |
| 342 base::ResetAndReturn(&read_cb_).Run(kOk, VideoFrame::CreateEmptyFrame()); | |
| 343 return; | |
| 344 } | |
| 345 | |
| 346 ReadFromDemuxerStream(); | |
| 347 return; | |
| 348 } | |
| 349 | |
| 350 base::ResetAndReturn(&read_cb_).Run(kOk, video_frame); | |
| 351 } | |
| 352 | |
| 353 bool LibvpxVideoDecoder::Decode( | |
| 354 const scoped_refptr<DecoderBuffer>& buffer, | |
| 355 scoped_refptr<VideoFrame>* video_frame) { | |
| 356 DCHECK(video_frame); | |
| 357 | |
| 358 // Pass |buffer| to libvpx. | |
| 359 int64 timestamp = buffer->GetTimestamp().InMicroseconds(); | |
| 360 void* user_priv = reinterpret_cast<void*>(×tamp); | |
| 361 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_, | |
| 362 buffer->GetData(), | |
| 363 buffer->GetDataSize(), | |
| 364 user_priv, | |
| 365 0); | |
| 366 if (status != VPX_CODEC_OK) { | |
| 367 LOG(ERROR) << "Decode(): vpx_codec_decode failed, status=" << status; | |
| 368 return false; | |
| 369 } | |
| 370 | |
| 371 // Gets pointer to decoded data. | |
| 372 vpx_codec_iter_t iter = NULL; | |
| 373 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter); | |
| 374 if (!vpx_image) { | |
| 375 *video_frame = NULL; | |
| 376 return true; | |
| 377 } | |
| 378 | |
| 379 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { | |
| 380 LOG(ERROR) << "Decode(): invalid output timestamp."; | |
| 381 return false; | |
| 382 } | |
| 383 | |
| 384 if (!CopyVpxImageTo(vpx_image, video_frame)) { | |
| 385 LOG(ERROR) << "Decode(): could not copy vpx image to output buffer."; | |
| 386 return false; | |
| 387 } | |
| 388 | |
| 389 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); | |
| 390 | |
| 391 return true; | |
| 392 } | |
| 393 | |
| 394 void LibvpxVideoDecoder::DoReset() { | |
| 395 DCHECK(read_cb_.is_null()); | |
| 396 | |
| 397 //avcodec_flush_buffers(codec_context_); | |
| 398 state_ = kNormal; | |
| 399 reset_cb_.Run(); | |
| 400 reset_cb_.Reset(); | |
| 401 } | |
| 402 | |
| 403 bool LibvpxVideoDecoder::CopyVpxImageTo( | |
| 404 const vpx_image* vpx_image, | |
| 405 scoped_refptr<VideoFrame>* video_frame) { | |
| 406 DCHECK(vpx_image); | |
| 407 if (!vpx_image) | |
| 408 return false; | |
| 409 | |
| 410 DCHECK_EQ(vpx_image->d_w % 2, 0U); | |
| 411 DCHECK_EQ(vpx_image->d_h % 2, 0U); | |
| 412 DCHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || | |
| 413 vpx_image->fmt == VPX_IMG_FMT_YV12); | |
| 414 | |
| 415 gfx::Size size(vpx_image->d_w, vpx_image->d_h); | |
| 416 gfx::Size natural_size = | |
| 417 demuxer_stream_->video_decoder_config().natural_size(); | |
| 418 | |
| 419 const VideoFrame::Format format = VideoFrame::YV12; | |
| 420 if (!VideoFrame::IsValidConfig(format, | |
| 421 size, | |
| 422 gfx::Rect(size), | |
| 423 natural_size)) { | |
| 424 LOG(ERROR) << "CopyVpxImageTo(): invalid video frame."; | |
| 425 return false; | |
| 426 } | |
| 427 | |
| 428 *video_frame = VideoFrame::CreateFrame(format, | |
| 429 size, | |
| 430 gfx::Rect(size), | |
| 431 natural_size, | |
| 432 kNoTimestamp()); | |
| 433 if (!video_frame) { | |
| 434 LOG(ERROR) << "CopyVpxImageTo(): cannot create output video frame."; | |
| 435 return false; | |
| 436 } | |
| 437 | |
| 438 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], | |
| 439 vpx_image->stride[VPX_PLANE_Y], | |
| 440 vpx_image->d_h, | |
| 441 *video_frame); | |
| 442 CopyUPlane(vpx_image->planes[VPX_PLANE_U], | |
| 443 vpx_image->stride[VPX_PLANE_U], | |
| 444 vpx_image->d_h / 2, | |
| 445 *video_frame); | |
| 446 CopyVPlane(vpx_image->planes[VPX_PLANE_V], | |
| 447 vpx_image->stride[VPX_PLANE_V], | |
| 448 vpx_image->d_h / 2, | |
| 449 *video_frame); | |
| 450 | |
| 451 return true; | |
| 452 } | |
| 453 | |
| 454 } // namespace media | |
| OLD | NEW |