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/vpx_video_decoder.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/location.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/string_number_conversions.h" |
| 14 #include "media/base/bind_to_loop.h" |
| 15 #include "media/base/decoder_buffer.h" |
| 16 #include "media/base/demuxer_stream.h" |
| 17 #include "media/base/media_switches.h" |
| 18 #include "media/base/pipeline.h" |
| 19 #include "media/base/video_decoder_config.h" |
| 20 #include "media/base/video_frame.h" |
| 21 #include "media/base/video_util.h" |
| 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 { |
| 33 |
| 34 // 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 |
| 36 // performance benefits on older machines such as P4s with hyperthreading. |
| 37 static const int kDecodeThreads = 2; |
| 38 static const int kMaxDecodeThreads = 16; |
| 39 |
| 40 // Returns the number of threads. |
| 41 static int GetThreadCount() { |
| 42 // TODO(scherkus): De-duplicate this function and the one used by |
| 43 // FFmpegVideoDecoder. |
| 44 |
| 45 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. |
| 46 int decode_threads = kDecodeThreads; |
| 47 |
| 48 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 49 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); |
| 50 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) |
| 51 return decode_threads; |
| 52 |
| 53 decode_threads = std::max(decode_threads, 0); |
| 54 decode_threads = std::min(decode_threads, kMaxDecodeThreads); |
| 55 return decode_threads; |
| 56 } |
| 57 |
| 58 VpxVideoDecoder::VpxVideoDecoder( |
| 59 const scoped_refptr<base::MessageLoopProxy>& message_loop) |
| 60 : message_loop_(message_loop), |
| 61 state_(kUninitialized), |
| 62 vpx_codec_(NULL) { |
| 63 } |
| 64 |
| 65 VpxVideoDecoder::~VpxVideoDecoder() { |
| 66 DCHECK_EQ(kUninitialized, state_); |
| 67 CloseDecoder(); |
| 68 } |
| 69 |
| 70 void VpxVideoDecoder::Initialize( |
| 71 const scoped_refptr<DemuxerStream>& stream, |
| 72 const PipelineStatusCB& status_cb, |
| 73 const StatisticsCB& statistics_cb) { |
| 74 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 75 DCHECK(!demuxer_stream_) << "Already initialized."; |
| 76 |
| 77 if (!stream) { |
| 78 status_cb.Run(PIPELINE_ERROR_DECODE); |
| 79 return; |
| 80 } |
| 81 |
| 82 demuxer_stream_ = stream; |
| 83 statistics_cb_ = statistics_cb; |
| 84 |
| 85 if (!ConfigureDecoder()) { |
| 86 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); |
| 87 return; |
| 88 } |
| 89 |
| 90 // Success! |
| 91 state_ = kNormal; |
| 92 status_cb.Run(PIPELINE_OK); |
| 93 } |
| 94 |
| 95 bool VpxVideoDecoder::ConfigureDecoder() { |
| 96 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config(); |
| 97 if (!config.IsValidConfig()) { |
| 98 DLOG(ERROR) << "Invalid video stream config: " |
| 99 << config.AsHumanReadableString(); |
| 100 return false; |
| 101 } |
| 102 |
| 103 if (config.codec() != kCodecVP9) |
| 104 return false; |
| 105 |
| 106 CloseDecoder(); |
| 107 |
| 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) { |
| 119 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status; |
| 120 delete vpx_codec_; |
| 121 vpx_codec_ = NULL; |
| 122 return false; |
| 123 } |
| 124 |
| 125 return true; |
| 126 } |
| 127 |
| 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) { |
| 137 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 138 DCHECK(!read_cb.is_null()); |
| 139 CHECK_NE(state_, kUninitialized); |
| 140 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
| 141 read_cb_ = BindToCurrentLoop(read_cb); |
| 142 |
| 143 // Return empty frames if decoding has finished. |
| 144 if (state_ == kDecodeFinished) { |
| 145 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); |
| 146 return; |
| 147 } |
| 148 |
| 149 ReadFromDemuxerStream(); |
| 150 } |
| 151 |
| 152 void VpxVideoDecoder::Reset(const base::Closure& closure) { |
| 153 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 154 DCHECK(reset_cb_.is_null()); |
| 155 reset_cb_ = BindToCurrentLoop(closure); |
| 156 |
| 157 // Defer the reset if a read is pending. |
| 158 if (!read_cb_.is_null()) |
| 159 return; |
| 160 |
| 161 DoReset(); |
| 162 } |
| 163 |
| 164 void VpxVideoDecoder::Stop(const base::Closure& closure) { |
| 165 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 166 |
| 167 if (state_ == kUninitialized) { |
| 168 closure.Run(); |
| 169 return; |
| 170 } |
| 171 |
| 172 if (!read_cb_.is_null()) |
| 173 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); |
| 174 |
| 175 state_ = kUninitialized; |
| 176 closure.Run(); |
| 177 } |
| 178 |
| 179 void VpxVideoDecoder::ReadFromDemuxerStream() { |
| 180 DCHECK_NE(state_, kUninitialized); |
| 181 DCHECK_NE(state_, kDecodeFinished); |
| 182 DCHECK(!read_cb_.is_null()); |
| 183 |
| 184 demuxer_stream_->Read(base::Bind( |
| 185 &VpxVideoDecoder::DoDecryptOrDecodeBuffer, this)); |
| 186 } |
| 187 |
| 188 void VpxVideoDecoder::DoDecryptOrDecodeBuffer( |
| 189 DemuxerStream::Status status, |
| 190 const scoped_refptr<DecoderBuffer>& buffer) { |
| 191 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 192 DCHECK_NE(state_, kDecodeFinished); |
| 193 DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status; |
| 194 |
| 195 if (state_ == kUninitialized) |
| 196 return; |
| 197 |
| 198 DCHECK(!read_cb_.is_null()); |
| 199 |
| 200 if (!reset_cb_.is_null()) { |
| 201 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); |
| 202 DoReset(); |
| 203 return; |
| 204 } |
| 205 |
| 206 if (status == DemuxerStream::kAborted) { |
| 207 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); |
| 208 return; |
| 209 } |
| 210 |
| 211 if (status == DemuxerStream::kConfigChanged) { |
| 212 if (!ConfigureDecoder()) { |
| 213 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| 214 return; |
| 215 } |
| 216 |
| 217 ReadFromDemuxerStream(); |
| 218 return; |
| 219 } |
| 220 |
| 221 DCHECK_EQ(status, DemuxerStream::kOk); |
| 222 DecodeBuffer(buffer); |
| 223 } |
| 224 |
| 225 void VpxVideoDecoder::DecodeBuffer( |
| 226 const scoped_refptr<DecoderBuffer>& buffer) { |
| 227 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 228 DCHECK_NE(state_, kUninitialized); |
| 229 DCHECK_NE(state_, kDecodeFinished); |
| 230 DCHECK(reset_cb_.is_null()); |
| 231 DCHECK(!read_cb_.is_null()); |
| 232 DCHECK(buffer); |
| 233 |
| 234 // Transition to kDecodeFinished on the first end of stream buffer. |
| 235 if (state_ == kNormal && buffer->IsEndOfStream()) { |
| 236 state_ = kDecodeFinished; |
| 237 base::ResetAndReturn(&read_cb_).Run(kOk, VideoFrame::CreateEmptyFrame()); |
| 238 return; |
| 239 } |
| 240 |
| 241 scoped_refptr<VideoFrame> video_frame; |
| 242 if (!Decode(buffer, &video_frame)) { |
| 243 state_ = kDecodeFinished; |
| 244 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); |
| 245 return; |
| 246 } |
| 247 |
| 248 // Any successful decode counts! |
| 249 if (buffer->GetDataSize()) { |
| 250 PipelineStatistics statistics; |
| 251 statistics.video_bytes_decoded = buffer->GetDataSize(); |
| 252 statistics_cb_.Run(statistics); |
| 253 } |
| 254 |
| 255 // If we didn't get a frame we need more data. |
| 256 if (!video_frame) { |
| 257 ReadFromDemuxerStream(); |
| 258 return; |
| 259 } |
| 260 |
| 261 base::ResetAndReturn(&read_cb_).Run(kOk, video_frame); |
| 262 } |
| 263 |
| 264 bool VpxVideoDecoder::Decode( |
| 265 const scoped_refptr<DecoderBuffer>& buffer, |
| 266 scoped_refptr<VideoFrame>* video_frame) { |
| 267 DCHECK(video_frame); |
| 268 DCHECK(!buffer->IsEndOfStream()); |
| 269 |
| 270 // Pass |buffer| to libvpx. |
| 271 int64 timestamp = buffer->GetTimestamp().InMicroseconds(); |
| 272 void* user_priv = reinterpret_cast<void*>(×tamp); |
| 273 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_, |
| 274 buffer->GetData(), |
| 275 buffer->GetDataSize(), |
| 276 user_priv, |
| 277 0); |
| 278 if (status != VPX_CODEC_OK) { |
| 279 LOG(ERROR) << "vpx_codec_decode() failed, status=" << status; |
| 280 return false; |
| 281 } |
| 282 |
| 283 // Gets pointer to decoded data. |
| 284 vpx_codec_iter_t iter = NULL; |
| 285 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter); |
| 286 if (!vpx_image) { |
| 287 *video_frame = NULL; |
| 288 return true; |
| 289 } |
| 290 |
| 291 if (vpx_image->user_priv != reinterpret_cast<void*>(×tamp)) { |
| 292 LOG(ERROR) << "Invalid output timestamp."; |
| 293 return false; |
| 294 } |
| 295 |
| 296 CopyVpxImageTo(vpx_image, video_frame); |
| 297 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp)); |
| 298 return true; |
| 299 } |
| 300 |
| 301 void VpxVideoDecoder::DoReset() { |
| 302 DCHECK(read_cb_.is_null()); |
| 303 |
| 304 state_ = kNormal; |
| 305 reset_cb_.Run(); |
| 306 reset_cb_.Reset(); |
| 307 } |
| 308 |
| 309 void VpxVideoDecoder::CopyVpxImageTo( |
| 310 const vpx_image* vpx_image, |
| 311 scoped_refptr<VideoFrame>* video_frame) { |
| 312 CHECK(vpx_image); |
| 313 CHECK_EQ(vpx_image->d_w % 2, 0U); |
| 314 CHECK_EQ(vpx_image->d_h % 2, 0U); |
| 315 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || |
| 316 vpx_image->fmt == VPX_IMG_FMT_YV12); |
| 317 |
| 318 gfx::Size size(vpx_image->d_w, vpx_image->d_h); |
| 319 gfx::Size natural_size = |
| 320 demuxer_stream_->video_decoder_config().natural_size(); |
| 321 |
| 322 *video_frame = VideoFrame::CreateFrame(VideoFrame::YV12, |
| 323 size, |
| 324 gfx::Rect(size), |
| 325 natural_size, |
| 326 kNoTimestamp()); |
| 327 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], |
| 328 vpx_image->stride[VPX_PLANE_Y], |
| 329 vpx_image->d_h, |
| 330 *video_frame); |
| 331 CopyUPlane(vpx_image->planes[VPX_PLANE_U], |
| 332 vpx_image->stride[VPX_PLANE_U], |
| 333 vpx_image->d_h / 2, |
| 334 *video_frame); |
| 335 CopyVPlane(vpx_image->planes[VPX_PLANE_V], |
| 336 vpx_image->stride[VPX_PLANE_V], |
| 337 vpx_image->d_h / 2, |
| 338 *video_frame); |
| 339 } |
| 340 |
| 341 } // namespace media |
OLD | NEW |