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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/sys_byteorder.h" | 18 #include "base/sys_byteorder.h" |
| 18 #include "media/base/bind_to_current_loop.h" | 19 #include "media/base/bind_to_current_loop.h" |
| 19 #include "media/base/decoder_buffer.h" | 20 #include "media/base/decoder_buffer.h" |
| 20 #include "media/base/demuxer_stream.h" | 21 #include "media/base/demuxer_stream.h" |
| 22 #include "media/base/limits.h" | |
| 21 #include "media/base/media_switches.h" | 23 #include "media/base/media_switches.h" |
| 22 #include "media/base/pipeline.h" | 24 #include "media/base/pipeline.h" |
| 23 #include "media/base/video_decoder_config.h" | 25 #include "media/base/video_decoder_config.h" |
| 24 #include "media/base/video_frame.h" | 26 #include "media/base/video_frame.h" |
| 25 #include "media/base/video_util.h" | 27 #include "media/base/video_util.h" |
| 26 | 28 |
| 27 // Include libvpx header files. | 29 // Include libvpx header files. |
| 28 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide | 30 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide |
| 29 // backwards compatibility for legacy applications using the library. | 31 // backwards compatibility for legacy applications using the library. |
| 30 #define VPX_CODEC_DISABLE_COMPAT 1 | 32 #define VPX_CODEC_DISABLE_COMPAT 1 |
| 31 extern "C" { | 33 extern "C" { |
| 32 #include "third_party/libvpx/source/libvpx/vpx/vpx_decoder.h" | 34 #include "third_party/libvpx/source/libvpx/vpx/vpx_decoder.h" |
| 35 #include "third_party/libvpx/source/libvpx/vpx/vpx_external_frame_buffer.h" | |
| 33 #include "third_party/libvpx/source/libvpx/vpx/vp8dx.h" | 36 #include "third_party/libvpx/source/libvpx/vpx/vp8dx.h" |
| 34 } | 37 } |
| 35 | 38 |
| 36 namespace media { | 39 namespace media { |
| 37 | 40 |
| 38 // Always try to use three threads for video decoding. There is little reason | 41 // 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 | 42 // 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. | 43 // performance benefits on older machines such as P4s with hyperthreading. |
| 41 static const int kDecodeThreads = 2; | 44 static const int kDecodeThreads = 2; |
| 42 static const int kMaxDecodeThreads = 16; | 45 static const int kMaxDecodeThreads = 16; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 60 } | 63 } |
| 61 | 64 |
| 62 return decode_threads; | 65 return decode_threads; |
| 63 } | 66 } |
| 64 | 67 |
| 65 decode_threads = std::max(decode_threads, 0); | 68 decode_threads = std::max(decode_threads, 0); |
| 66 decode_threads = std::min(decode_threads, kMaxDecodeThreads); | 69 decode_threads = std::min(decode_threads, kMaxDecodeThreads); |
| 67 return decode_threads; | 70 return decode_threads; |
| 68 } | 71 } |
| 69 | 72 |
| 73 // Maximum number of frame buffers that can be used (by both chromium and libvpx | |
| 74 // combined) for VP9 Decoding. | |
| 75 // TODO(vigneshv): Investigate if this can be relaxed to a higher number. | |
| 76 static const int kVP9MaxFrameBuffers = VP9_MAXIMUM_REF_BUFFERS + | |
| 77 VPX_MAXIMUM_WORK_BUFFERS + | |
| 78 limits::kMaxVideoFrames; | |
| 79 | |
| 80 class VpxVideoDecoder::MemoryPool | |
| 81 : public base::RefCountedThreadSafe<VpxVideoDecoder::MemoryPool> { | |
| 82 public: | |
| 83 MemoryPool(); | |
| 84 | |
| 85 // Callback that will be called by libvpx when it needs a frame buffer. | |
| 86 // Parameters: | |
| 87 // |user_priv| Private data passed to libvpx (pointer to memory pool). | |
| 88 // |min_size| Minimum size needed by libvpx to decompress the next frame. | |
| 89 // |fb| Pointer to the frame buffer to update. | |
| 90 // Returns 0 on success. Returns < 0 on failure. | |
| 91 static int32 GetVP9FrameBuffer(void* user_priv, size_t min_size, | |
| 92 vpx_codec_frame_buffer* fb); | |
| 93 | |
| 94 // Callback that will be called by libvpx when the frame buffer is no longer | |
| 95 // being used by libvpx. Parameters: | |
| 96 // |user_priv| Private data passed to libvpx (pointer to memory pool). | |
| 97 // |fb| Pointer to the frame buffer that's being released. | |
| 98 static int32 ReleaseVP9FrameBuffer(void *user_priv, | |
| 99 vpx_codec_frame_buffer *fb); | |
| 100 | |
| 101 // Generates a "no_longer_needed" closure that holds a reference | |
| 102 // to this pool. | |
| 103 base::Closure CreateFrameCallback(void* fb_priv_data); | |
| 104 | |
| 105 private: | |
| 106 friend class base::RefCountedThreadSafe<VpxVideoDecoder::MemoryPool>; | |
| 107 ~MemoryPool(); | |
| 108 | |
| 109 // Reference counted frame buffers used for VP9 decoding. Reference counting i s | |
| 110 // done manually because both chromium and libvpx has to release this before a | |
| 111 // buffer can be re-used. | |
| 112 struct VP9FrameBuffer { | |
| 113 std::vector<uint8> data; | |
| 114 uint32 ref_cnt; | |
| 115 }; | |
| 116 | |
| 117 // Gets the next available frame buffer for use by libvpx. | |
| 118 VP9FrameBuffer* GetFreeFrameBuffer(size_t min_size); | |
| 119 | |
| 120 // Method that gets called when a VideoFrame that references this pool gets | |
| 121 // destroyed. | |
| 122 void OnVideoFrameDestroyed(VP9FrameBuffer* frame_buffer); | |
| 123 | |
| 124 // Frame buffers to be used by libvpx for VP9 Decoding. | |
| 125 std::vector<VP9FrameBuffer*> frame_buffers_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(MemoryPool); | |
| 128 }; | |
| 129 | |
| 130 VpxVideoDecoder::MemoryPool::MemoryPool() {} | |
| 131 | |
| 132 VpxVideoDecoder::MemoryPool::~MemoryPool() { | |
| 133 for (size_t i = 0; i < frame_buffers_.size(); ++i) { | |
|
acolwell GONE FROM CHROMIUM
2014/02/04 17:47:50
nit: Use STLDeleteElements() from base/stl_util.h
vignesh
2014/02/04 19:01:09
Done.
| |
| 134 delete frame_buffers_[i]; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 VpxVideoDecoder::MemoryPool::VP9FrameBuffer* | |
| 139 VpxVideoDecoder::MemoryPool::GetFreeFrameBuffer(size_t min_size) { | |
| 140 // Check if a free frame buffer exists. | |
| 141 size_t i = 0; | |
| 142 for (; i < frame_buffers_.size(); ++i) { | |
| 143 if (frame_buffers_[i]->ref_cnt == 0) | |
| 144 break; | |
| 145 } | |
| 146 | |
| 147 if (i == frame_buffers_.size()) { | |
| 148 // Maximum number of frame buffers reached. | |
| 149 if (i == kVP9MaxFrameBuffers) | |
| 150 return NULL; | |
| 151 | |
| 152 // Create a new frame buffer. | |
| 153 frame_buffers_.push_back(new VP9FrameBuffer()); | |
| 154 } | |
| 155 | |
| 156 // Resize the frame buffer if necessary. | |
| 157 if (frame_buffers_[i]->data.size() < min_size) | |
| 158 frame_buffers_[i]->data.resize(min_size); | |
| 159 return frame_buffers_[i]; | |
| 160 } | |
| 161 | |
| 162 int32 VpxVideoDecoder::MemoryPool::GetVP9FrameBuffer( | |
| 163 void* user_priv, size_t min_size, vpx_codec_frame_buffer* fb) { | |
| 164 DCHECK(user_priv != NULL); | |
|
acolwell GONE FROM CHROMIUM
2014/02/04 17:47:50
nit: Remove the != NULL here and below.
vignesh
2014/02/04 19:01:09
Done.
| |
| 165 DCHECK(fb != NULL); | |
| 166 | |
| 167 VpxVideoDecoder::MemoryPool* memory_pool = | |
| 168 static_cast<VpxVideoDecoder::MemoryPool*>(user_priv); | |
| 169 | |
| 170 VP9FrameBuffer* fb_to_use = memory_pool->GetFreeFrameBuffer(min_size); | |
| 171 if (fb_to_use == NULL) | |
| 172 return -1; | |
| 173 | |
| 174 fb->data = &fb_to_use->data[0]; | |
| 175 fb->size = fb_to_use->data.size(); | |
| 176 ++fb_to_use->ref_cnt; | |
| 177 | |
| 178 // Set the frame buffer's private data to point at the external frame buffer. | |
| 179 fb->frame_priv = static_cast<void*>(fb_to_use); | |
| 180 return 0; | |
| 181 } | |
| 182 | |
| 183 int32 VpxVideoDecoder::MemoryPool::ReleaseVP9FrameBuffer( | |
| 184 void *user_priv, vpx_codec_frame_buffer *fb) { | |
| 185 VP9FrameBuffer* frame_buffer = static_cast<VP9FrameBuffer*>(fb->frame_priv); | |
| 186 --frame_buffer->ref_cnt; | |
| 187 return 0; | |
| 188 } | |
| 189 | |
| 190 base::Closure VpxVideoDecoder::MemoryPool::CreateFrameCallback( | |
| 191 void* fb_priv_data) { | |
| 192 VP9FrameBuffer* frame_buffer = static_cast<VP9FrameBuffer*>(fb_priv_data); | |
| 193 ++frame_buffer->ref_cnt; | |
| 194 return BindToCurrentLoop( | |
| 195 base::Bind(&MemoryPool::OnVideoFrameDestroyed, this, | |
| 196 frame_buffer)); | |
| 197 } | |
| 198 | |
| 199 void VpxVideoDecoder::MemoryPool::OnVideoFrameDestroyed( | |
| 200 VP9FrameBuffer* frame_buffer) { | |
| 201 --frame_buffer->ref_cnt; | |
| 202 } | |
| 203 | |
| 70 VpxVideoDecoder::VpxVideoDecoder( | 204 VpxVideoDecoder::VpxVideoDecoder( |
| 71 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 205 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 72 : task_runner_(task_runner), | 206 : task_runner_(task_runner), |
| 73 weak_factory_(this), | 207 weak_factory_(this), |
| 74 state_(kUninitialized), | 208 state_(kUninitialized), |
| 75 vpx_codec_(NULL), | 209 vpx_codec_(NULL), |
| 76 vpx_codec_alpha_(NULL) { | 210 vpx_codec_alpha_(NULL) { |
| 77 } | 211 } |
| 78 | 212 |
| 79 VpxVideoDecoder::~VpxVideoDecoder() { | 213 VpxVideoDecoder::~VpxVideoDecoder() { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 } | 269 } |
| 136 if (!can_handle) | 270 if (!can_handle) |
| 137 return false; | 271 return false; |
| 138 | 272 |
| 139 CloseDecoder(); | 273 CloseDecoder(); |
| 140 | 274 |
| 141 vpx_codec_ = InitializeVpxContext(vpx_codec_, config); | 275 vpx_codec_ = InitializeVpxContext(vpx_codec_, config); |
| 142 if (!vpx_codec_) | 276 if (!vpx_codec_) |
| 143 return false; | 277 return false; |
| 144 | 278 |
| 279 // We use our own buffers for VP9 so that there is no need to copy data after | |
| 280 // decoding. | |
| 281 if (config.codec() == kCodecVP9) { | |
| 282 memory_pool_ = new MemoryPool(); | |
| 283 if (vpx_codec_set_external_frame_buffer_functions( | |
| 284 vpx_codec_, | |
| 285 &MemoryPool::GetVP9FrameBuffer, | |
| 286 &MemoryPool::ReleaseVP9FrameBuffer, | |
| 287 memory_pool_)) { | |
| 288 LOG(ERROR) << "Failed to configure external buffers."; | |
| 289 return false; | |
| 290 } | |
| 291 } | |
| 292 | |
| 145 if (config.format() == VideoFrame::YV12A) { | 293 if (config.format() == VideoFrame::YV12A) { |
| 146 vpx_codec_alpha_ = InitializeVpxContext(vpx_codec_alpha_, config); | 294 vpx_codec_alpha_ = InitializeVpxContext(vpx_codec_alpha_, config); |
| 147 if (!vpx_codec_alpha_) | 295 if (!vpx_codec_alpha_) |
| 148 return false; | 296 return false; |
| 149 } | 297 } |
| 150 | 298 |
| 151 return true; | 299 return true; |
| 152 } | 300 } |
| 153 | 301 |
| 154 void VpxVideoDecoder::CloseDecoder() { | 302 void VpxVideoDecoder::CloseDecoder() { |
| 155 if (vpx_codec_) { | 303 if (vpx_codec_) { |
| 156 vpx_codec_destroy(vpx_codec_); | 304 vpx_codec_destroy(vpx_codec_); |
| 157 delete vpx_codec_; | 305 delete vpx_codec_; |
| 158 vpx_codec_ = NULL; | 306 vpx_codec_ = NULL; |
| 307 if (memory_pool_) | |
|
acolwell GONE FROM CHROMIUM
2014/02/04 17:47:50
nit: remove condition since blindly clearing this
vignesh
2014/02/04 19:01:09
Done.
| |
| 308 memory_pool_ = NULL; | |
| 159 } | 309 } |
| 160 if (vpx_codec_alpha_) { | 310 if (vpx_codec_alpha_) { |
| 161 vpx_codec_destroy(vpx_codec_alpha_); | 311 vpx_codec_destroy(vpx_codec_alpha_); |
| 162 delete vpx_codec_alpha_; | 312 delete vpx_codec_alpha_; |
| 163 vpx_codec_alpha_ = NULL; | 313 vpx_codec_alpha_ = NULL; |
| 164 } | 314 } |
| 165 } | 315 } |
| 166 | 316 |
| 167 void VpxVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 317 void VpxVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
| 168 const DecodeCB& decode_cb) { | 318 const DecodeCB& decode_cb) { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 | 485 |
| 336 void VpxVideoDecoder::CopyVpxImageTo(const vpx_image* vpx_image, | 486 void VpxVideoDecoder::CopyVpxImageTo(const vpx_image* vpx_image, |
| 337 const struct vpx_image* vpx_image_alpha, | 487 const struct vpx_image* vpx_image_alpha, |
| 338 scoped_refptr<VideoFrame>* video_frame) { | 488 scoped_refptr<VideoFrame>* video_frame) { |
| 339 CHECK(vpx_image); | 489 CHECK(vpx_image); |
| 340 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || | 490 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 || |
| 341 vpx_image->fmt == VPX_IMG_FMT_YV12); | 491 vpx_image->fmt == VPX_IMG_FMT_YV12); |
| 342 | 492 |
| 343 gfx::Size size(vpx_image->d_w, vpx_image->d_h); | 493 gfx::Size size(vpx_image->d_w, vpx_image->d_h); |
| 344 | 494 |
| 495 if (!vpx_codec_alpha_ && memory_pool_) { | |
| 496 *video_frame = VideoFrame::WrapExternalYuvData( | |
| 497 VideoFrame::YV12, | |
| 498 size, gfx::Rect(size), config_.natural_size(), | |
| 499 vpx_image->stride[VPX_PLANE_Y], | |
| 500 vpx_image->stride[VPX_PLANE_U], | |
| 501 vpx_image->stride[VPX_PLANE_V], | |
| 502 vpx_image->planes[VPX_PLANE_Y], | |
| 503 vpx_image->planes[VPX_PLANE_U], | |
| 504 vpx_image->planes[VPX_PLANE_V], | |
| 505 kNoTimestamp(), | |
| 506 memory_pool_->CreateFrameCallback( | |
| 507 vpx_image->ext_fb_priv)); | |
| 508 return; | |
| 509 } | |
| 510 | |
| 345 *video_frame = frame_pool_.CreateFrame( | 511 *video_frame = frame_pool_.CreateFrame( |
| 346 vpx_codec_alpha_ ? VideoFrame::YV12A : VideoFrame::YV12, | 512 vpx_codec_alpha_ ? VideoFrame::YV12A : VideoFrame::YV12, |
| 347 size, | 513 size, |
| 348 gfx::Rect(size), | 514 gfx::Rect(size), |
| 349 config_.natural_size(), | 515 config_.natural_size(), |
| 350 kNoTimestamp()); | 516 kNoTimestamp()); |
| 351 | 517 |
| 352 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], | 518 CopyYPlane(vpx_image->planes[VPX_PLANE_Y], |
| 353 vpx_image->stride[VPX_PLANE_Y], | 519 vpx_image->stride[VPX_PLANE_Y], |
| 354 vpx_image->d_h, | 520 vpx_image->d_h, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 368 vpx_image->stride[VPX_PLANE_Y], vpx_image->d_h, video_frame->get()); | 534 vpx_image->stride[VPX_PLANE_Y], vpx_image->d_h, video_frame->get()); |
| 369 return; | 535 return; |
| 370 } | 536 } |
| 371 CopyAPlane(vpx_image_alpha->planes[VPX_PLANE_Y], | 537 CopyAPlane(vpx_image_alpha->planes[VPX_PLANE_Y], |
| 372 vpx_image->stride[VPX_PLANE_Y], | 538 vpx_image->stride[VPX_PLANE_Y], |
| 373 vpx_image->d_h, | 539 vpx_image->d_h, |
| 374 video_frame->get()); | 540 video_frame->get()); |
| 375 } | 541 } |
| 376 | 542 |
| 377 } // namespace media | 543 } // namespace media |
| OLD | NEW |