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