Add ICU message format support
[chromium-blink-merge.git] / media / filters / vpx_video_decoder.cc
blob6c467db427ec9ca4ec309f335c74d0a25bd73108
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.
5 #include "media/filters/vpx_video_decoder.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/command_line.h"
14 #include "base/location.h"
15 #include "base/logging.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/sys_byteorder.h"
20 #include "base/sys_info.h"
21 #include "base/trace_event/memory_allocator_dump.h"
22 #include "base/trace_event/memory_dump_manager.h"
23 #include "base/trace_event/memory_dump_provider.h"
24 #include "base/trace_event/process_memory_dump.h"
25 #include "base/trace_event/trace_event.h"
26 #include "media/base/bind_to_current_loop.h"
27 #include "media/base/decoder_buffer.h"
28 #include "media/base/limits.h"
29 #include "media/base/media_switches.h"
30 #include "media/base/pipeline.h"
31 #include "media/base/video_util.h"
33 // Include libvpx header files.
34 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide
35 // backwards compatibility for legacy applications using the library.
36 #define VPX_CODEC_DISABLE_COMPAT 1
37 extern "C" {
38 #include "third_party/libvpx/source/libvpx/vpx/vpx_decoder.h"
39 #include "third_party/libvpx/source/libvpx/vpx/vpx_frame_buffer.h"
40 #include "third_party/libvpx/source/libvpx/vpx/vp8dx.h"
43 namespace media {
45 // Always try to use three threads for video decoding. There is little reason
46 // not to since current day CPUs tend to be multi-core and we measured
47 // performance benefits on older machines such as P4s with hyperthreading.
48 static const int kDecodeThreads = 2;
49 static const int kMaxDecodeThreads = 16;
51 // Returns the number of threads.
52 static int GetThreadCount(const VideoDecoderConfig& config) {
53 // Refer to http://crbug.com/93932 for tsan suppressions on decoding.
54 int decode_threads = kDecodeThreads;
56 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
57 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads));
58 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) {
59 if (config.codec() == kCodecVP9) {
60 // For VP9 decode when using the default thread count, increase the number
61 // of decode threads to equal the maximum number of tiles possible for
62 // higher resolution streams.
63 if (config.coded_size().width() >= 2048)
64 decode_threads = 8;
65 else if (config.coded_size().width() >= 1024)
66 decode_threads = 4;
69 decode_threads = std::min(decode_threads,
70 base::SysInfo::NumberOfProcessors());
71 return decode_threads;
74 decode_threads = std::max(decode_threads, 0);
75 decode_threads = std::min(decode_threads, kMaxDecodeThreads);
76 return decode_threads;
79 class VpxVideoDecoder::MemoryPool
80 : public base::RefCountedThreadSafe<VpxVideoDecoder::MemoryPool>,
81 public base::trace_event::MemoryDumpProvider {
82 public:
83 MemoryPool();
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);
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);
101 // Generates a "no_longer_needed" closure that holds a reference
102 // to this pool.
103 base::Closure CreateFrameCallback(void* fb_priv_data);
105 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
106 base::trace_event::ProcessMemoryDump* pmd) override;
108 private:
109 friend class base::RefCountedThreadSafe<VpxVideoDecoder::MemoryPool>;
110 ~MemoryPool() override;
112 // Reference counted frame buffers used for VP9 decoding. Reference counting
113 // is done manually because both chromium and libvpx has to release this
114 // before a buffer can be re-used.
115 struct VP9FrameBuffer {
116 VP9FrameBuffer() : ref_cnt(0) {}
117 std::vector<uint8> data;
118 uint32 ref_cnt;
121 // Gets the next available frame buffer for use by libvpx.
122 VP9FrameBuffer* GetFreeFrameBuffer(size_t min_size);
124 // Method that gets called when a VideoFrame that references this pool gets
125 // destroyed.
126 void OnVideoFrameDestroyed(VP9FrameBuffer* frame_buffer);
128 // Frame buffers to be used by libvpx for VP9 Decoding.
129 std::vector<VP9FrameBuffer*> frame_buffers_;
131 DISALLOW_COPY_AND_ASSIGN(MemoryPool);
134 VpxVideoDecoder::MemoryPool::MemoryPool() {}
136 VpxVideoDecoder::MemoryPool::~MemoryPool() {
137 STLDeleteElements(&frame_buffers_);
140 VpxVideoDecoder::MemoryPool::VP9FrameBuffer*
141 VpxVideoDecoder::MemoryPool::GetFreeFrameBuffer(size_t min_size) {
142 // Check if a free frame buffer exists.
143 size_t i = 0;
144 for (; i < frame_buffers_.size(); ++i) {
145 if (frame_buffers_[i]->ref_cnt == 0)
146 break;
149 if (i == frame_buffers_.size()) {
150 // Create a new frame buffer.
151 frame_buffers_.push_back(new VP9FrameBuffer());
154 // Resize the frame buffer if necessary.
155 if (frame_buffers_[i]->data.size() < min_size)
156 frame_buffers_[i]->data.resize(min_size);
157 return frame_buffers_[i];
160 int32 VpxVideoDecoder::MemoryPool::GetVP9FrameBuffer(
161 void* user_priv, size_t min_size, vpx_codec_frame_buffer* fb) {
162 DCHECK(user_priv);
163 DCHECK(fb);
165 VpxVideoDecoder::MemoryPool* memory_pool =
166 static_cast<VpxVideoDecoder::MemoryPool*>(user_priv);
168 VP9FrameBuffer* fb_to_use = memory_pool->GetFreeFrameBuffer(min_size);
169 if (fb_to_use == NULL)
170 return -1;
172 fb->data = &fb_to_use->data[0];
173 fb->size = fb_to_use->data.size();
174 ++fb_to_use->ref_cnt;
176 // Set the frame buffer's private data to point at the external frame buffer.
177 fb->priv = static_cast<void*>(fb_to_use);
178 return 0;
181 int32 VpxVideoDecoder::MemoryPool::ReleaseVP9FrameBuffer(
182 void *user_priv, vpx_codec_frame_buffer *fb) {
183 VP9FrameBuffer* frame_buffer = static_cast<VP9FrameBuffer*>(fb->priv);
184 --frame_buffer->ref_cnt;
185 return 0;
188 base::Closure VpxVideoDecoder::MemoryPool::CreateFrameCallback(
189 void* fb_priv_data) {
190 VP9FrameBuffer* frame_buffer = static_cast<VP9FrameBuffer*>(fb_priv_data);
191 ++frame_buffer->ref_cnt;
192 return BindToCurrentLoop(
193 base::Bind(&MemoryPool::OnVideoFrameDestroyed, this,
194 frame_buffer));
197 bool VpxVideoDecoder::MemoryPool::OnMemoryDump(
198 const base::trace_event::MemoryDumpArgs& args,
199 base::trace_event::ProcessMemoryDump* pmd) {
200 base::trace_event::MemoryAllocatorDump* memory_dump =
201 pmd->CreateAllocatorDump("media/vpx/memory_pool");
202 base::trace_event::MemoryAllocatorDump* used_memory_dump =
203 pmd->CreateAllocatorDump("media/vpx/memory_pool/used");
205 pmd->AddSuballocation(memory_dump->guid(),
206 base::trace_event::MemoryDumpManager::GetInstance()
207 ->system_allocator_pool_name());
208 size_t bytes_used = 0;
209 size_t bytes_reserved = 0;
210 for (const VP9FrameBuffer* frame_buffer : frame_buffers_) {
211 if (frame_buffer->ref_cnt) {
212 bytes_used += frame_buffer->data.size();
214 bytes_reserved += frame_buffer->data.size();
217 memory_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
218 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
219 bytes_reserved);
220 used_memory_dump->AddScalar(
221 base::trace_event::MemoryAllocatorDump::kNameSize,
222 base::trace_event::MemoryAllocatorDump::kUnitsBytes, bytes_used);
224 return true;
227 void VpxVideoDecoder::MemoryPool::OnVideoFrameDestroyed(
228 VP9FrameBuffer* frame_buffer) {
229 --frame_buffer->ref_cnt;
232 VpxVideoDecoder::VpxVideoDecoder(
233 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
234 : task_runner_(task_runner),
235 state_(kUninitialized),
236 vpx_codec_(NULL),
237 vpx_codec_alpha_(NULL) {}
239 VpxVideoDecoder::~VpxVideoDecoder() {
240 DCHECK(task_runner_->BelongsToCurrentThread());
241 CloseDecoder();
244 std::string VpxVideoDecoder::GetDisplayName() const {
245 return "VpxVideoDecoder";
248 void VpxVideoDecoder::Initialize(const VideoDecoderConfig& config,
249 bool low_delay,
250 const InitCB& init_cb,
251 const OutputCB& output_cb) {
252 DCHECK(task_runner_->BelongsToCurrentThread());
253 DCHECK(config.IsValidConfig());
254 DCHECK(!config.is_encrypted());
255 DCHECK(decode_cb_.is_null());
257 InitCB bound_init_cb = BindToCurrentLoop(init_cb);
259 if (!ConfigureDecoder(config)) {
260 bound_init_cb.Run(false);
261 return;
264 // Success!
265 config_ = config;
266 state_ = kNormal;
267 output_cb_ = BindToCurrentLoop(output_cb);
268 bound_init_cb.Run(true);
271 static vpx_codec_ctx* InitializeVpxContext(vpx_codec_ctx* context,
272 const VideoDecoderConfig& config) {
273 context = new vpx_codec_ctx();
274 vpx_codec_dec_cfg_t vpx_config = {0};
275 vpx_config.w = config.coded_size().width();
276 vpx_config.h = config.coded_size().height();
277 vpx_config.threads = GetThreadCount(config);
279 vpx_codec_err_t status = vpx_codec_dec_init(context,
280 config.codec() == kCodecVP9 ?
281 vpx_codec_vp9_dx() :
282 vpx_codec_vp8_dx(),
283 &vpx_config,
285 if (status != VPX_CODEC_OK) {
286 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status;
287 delete context;
288 return NULL;
290 return context;
293 bool VpxVideoDecoder::ConfigureDecoder(const VideoDecoderConfig& config) {
294 if (config.codec() != kCodecVP8 && config.codec() != kCodecVP9)
295 return false;
297 // In VP8 videos, only those with alpha are handled by VpxVideoDecoder. All
298 // other VP8 videos go to FFmpegVideoDecoder.
299 if (config.codec() == kCodecVP8 && config.format() != PIXEL_FORMAT_YV12A)
300 return false;
302 CloseDecoder();
304 vpx_codec_ = InitializeVpxContext(vpx_codec_, config);
305 if (!vpx_codec_)
306 return false;
308 // We use our own buffers for VP9 so that there is no need to copy data after
309 // decoding.
310 if (config.codec() == kCodecVP9) {
311 memory_pool_ = new MemoryPool();
312 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
313 memory_pool_.get(), task_runner_);
314 if (vpx_codec_set_frame_buffer_functions(vpx_codec_,
315 &MemoryPool::GetVP9FrameBuffer,
316 &MemoryPool::ReleaseVP9FrameBuffer,
317 memory_pool_.get())) {
318 LOG(ERROR) << "Failed to configure external buffers.";
319 return false;
323 if (config.format() == PIXEL_FORMAT_YV12A) {
324 vpx_codec_alpha_ = InitializeVpxContext(vpx_codec_alpha_, config);
325 if (!vpx_codec_alpha_)
326 return false;
329 return true;
332 void VpxVideoDecoder::CloseDecoder() {
333 if (vpx_codec_) {
334 vpx_codec_destroy(vpx_codec_);
335 delete vpx_codec_;
336 vpx_codec_ = NULL;
337 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
338 memory_pool_.get());
339 memory_pool_ = NULL;
341 if (vpx_codec_alpha_) {
342 vpx_codec_destroy(vpx_codec_alpha_);
343 delete vpx_codec_alpha_;
344 vpx_codec_alpha_ = NULL;
348 void VpxVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
349 const DecodeCB& decode_cb) {
350 DCHECK(task_runner_->BelongsToCurrentThread());
351 DCHECK(!decode_cb.is_null());
352 CHECK_NE(state_, kUninitialized);
353 CHECK(decode_cb_.is_null()) << "Overlapping decodes are not supported.";
355 decode_cb_ = BindToCurrentLoop(decode_cb);
357 if (state_ == kError) {
358 base::ResetAndReturn(&decode_cb_).Run(kDecodeError);
359 return;
362 // Return empty frames if decoding has finished.
363 if (state_ == kDecodeFinished) {
364 base::ResetAndReturn(&decode_cb_).Run(kOk);
365 return;
368 DecodeBuffer(buffer);
371 void VpxVideoDecoder::Reset(const base::Closure& closure) {
372 DCHECK(task_runner_->BelongsToCurrentThread());
373 DCHECK(decode_cb_.is_null());
375 state_ = kNormal;
376 task_runner_->PostTask(FROM_HERE, closure);
379 void VpxVideoDecoder::DecodeBuffer(const scoped_refptr<DecoderBuffer>& buffer) {
380 DCHECK(task_runner_->BelongsToCurrentThread());
381 DCHECK_NE(state_, kUninitialized);
382 DCHECK_NE(state_, kDecodeFinished);
383 DCHECK_NE(state_, kError);
384 DCHECK(!decode_cb_.is_null());
385 DCHECK(buffer.get());
387 // Transition to kDecodeFinished on the first end of stream buffer.
388 if (state_ == kNormal && buffer->end_of_stream()) {
389 state_ = kDecodeFinished;
390 base::ResetAndReturn(&decode_cb_).Run(kOk);
391 return;
394 scoped_refptr<VideoFrame> video_frame;
395 if (!VpxDecode(buffer, &video_frame)) {
396 state_ = kError;
397 base::ResetAndReturn(&decode_cb_).Run(kDecodeError);
398 return;
401 if (video_frame.get())
402 output_cb_.Run(video_frame);
404 // VideoDecoderShim expects that |decode_cb| is called only after
405 // |output_cb_|.
406 base::ResetAndReturn(&decode_cb_).Run(kOk);
409 bool VpxVideoDecoder::VpxDecode(const scoped_refptr<DecoderBuffer>& buffer,
410 scoped_refptr<VideoFrame>* video_frame) {
411 DCHECK(video_frame);
412 DCHECK(!buffer->end_of_stream());
414 // Pass |buffer| to libvpx.
415 int64 timestamp = buffer->timestamp().InMicroseconds();
416 void* user_priv = reinterpret_cast<void*>(&timestamp);
419 TRACE_EVENT1("video", "vpx_codec_decode", "timestamp", timestamp);
420 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_,
421 buffer->data(),
422 buffer->data_size(),
423 user_priv,
425 if (status != VPX_CODEC_OK) {
426 LOG(ERROR) << "vpx_codec_decode() failed, status=" << status;
427 return false;
431 // Gets pointer to decoded data.
432 vpx_codec_iter_t iter = NULL;
433 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter);
434 if (!vpx_image) {
435 *video_frame = NULL;
436 return true;
439 if (vpx_image->user_priv != reinterpret_cast<void*>(&timestamp)) {
440 LOG(ERROR) << "Invalid output timestamp.";
441 return false;
444 const vpx_image_t* vpx_image_alpha = NULL;
445 if (vpx_codec_alpha_ && buffer->side_data_size() >= 8) {
446 // Pass alpha data to libvpx.
447 int64 timestamp_alpha = buffer->timestamp().InMicroseconds();
448 void* user_priv_alpha = reinterpret_cast<void*>(&timestamp_alpha);
450 // First 8 bytes of side data is side_data_id in big endian.
451 const uint64 side_data_id = base::NetToHost64(
452 *(reinterpret_cast<const uint64*>(buffer->side_data())));
453 if (side_data_id == 1) {
455 TRACE_EVENT1("video", "vpx_codec_decode_alpha",
456 "timestamp_alpha", timestamp_alpha);
457 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_alpha_,
458 buffer->side_data() + 8,
459 buffer->side_data_size() - 8,
460 user_priv_alpha,
462 if (status != VPX_CODEC_OK) {
463 LOG(ERROR) << "vpx_codec_decode() failed on alpha, status=" << status;
464 return false;
468 // Gets pointer to decoded data.
469 vpx_codec_iter_t iter_alpha = NULL;
470 vpx_image_alpha = vpx_codec_get_frame(vpx_codec_alpha_, &iter_alpha);
471 if (!vpx_image_alpha) {
472 *video_frame = NULL;
473 return true;
476 if (vpx_image_alpha->user_priv !=
477 reinterpret_cast<void*>(&timestamp_alpha)) {
478 LOG(ERROR) << "Invalid output timestamp on alpha.";
479 return false;
482 if (vpx_image_alpha->d_h != vpx_image->d_h ||
483 vpx_image_alpha->d_w != vpx_image->d_w) {
484 LOG(ERROR) << "The alpha plane dimensions are not the same as the "
485 "image dimensions.";
486 return false;
491 CopyVpxImageTo(vpx_image, vpx_image_alpha, video_frame);
492 (*video_frame)->set_timestamp(base::TimeDelta::FromMicroseconds(timestamp));
493 return true;
496 void VpxVideoDecoder::CopyVpxImageTo(const vpx_image* vpx_image,
497 const struct vpx_image* vpx_image_alpha,
498 scoped_refptr<VideoFrame>* video_frame) {
499 CHECK(vpx_image);
500 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 ||
501 vpx_image->fmt == VPX_IMG_FMT_YV12 ||
502 vpx_image->fmt == VPX_IMG_FMT_I444);
504 VideoPixelFormat codec_format = PIXEL_FORMAT_YV12;
505 int uv_rows = (vpx_image->d_h + 1) / 2;
507 if (vpx_image->fmt == VPX_IMG_FMT_I444) {
508 CHECK(!vpx_codec_alpha_);
509 codec_format = PIXEL_FORMAT_YV24;
510 uv_rows = vpx_image->d_h;
511 } else if (vpx_codec_alpha_) {
512 codec_format = PIXEL_FORMAT_YV12A;
515 // Default to the color space from the config, but if the bistream specifies
516 // one, prefer that instead.
517 ColorSpace color_space = config_.color_space();
518 if (vpx_image->cs == VPX_CS_BT_709)
519 color_space = COLOR_SPACE_HD_REC709;
520 else if (vpx_image->cs == VPX_CS_BT_601)
521 color_space = COLOR_SPACE_SD_REC601;
523 // The mixed |w|/|d_h| in |coded_size| is intentional. Setting the correct
524 // coded width is necessary to allow coalesced memory access, which may avoid
525 // frame copies. Setting the correct coded height however does not have any
526 // benefit, and only risk copying too much data.
527 const gfx::Size coded_size(vpx_image->w, vpx_image->d_h);
528 const gfx::Size visible_size(vpx_image->d_w, vpx_image->d_h);
530 if (!vpx_codec_alpha_ && memory_pool_.get()) {
531 *video_frame = VideoFrame::WrapExternalYuvData(
532 codec_format,
533 coded_size, gfx::Rect(visible_size), config_.natural_size(),
534 vpx_image->stride[VPX_PLANE_Y],
535 vpx_image->stride[VPX_PLANE_U],
536 vpx_image->stride[VPX_PLANE_V],
537 vpx_image->planes[VPX_PLANE_Y],
538 vpx_image->planes[VPX_PLANE_U],
539 vpx_image->planes[VPX_PLANE_V],
540 kNoTimestamp());
541 video_frame->get()->AddDestructionObserver(
542 memory_pool_->CreateFrameCallback(vpx_image->fb_priv));
543 video_frame->get()->metadata()->SetInteger(VideoFrameMetadata::COLOR_SPACE,
544 color_space);
545 return;
548 *video_frame = frame_pool_.CreateFrame(
549 codec_format,
550 visible_size,
551 gfx::Rect(visible_size),
552 config_.natural_size(),
553 kNoTimestamp());
554 video_frame->get()->metadata()->SetInteger(VideoFrameMetadata::COLOR_SPACE,
555 color_space);
557 CopyYPlane(vpx_image->planes[VPX_PLANE_Y],
558 vpx_image->stride[VPX_PLANE_Y],
559 vpx_image->d_h,
560 video_frame->get());
561 CopyUPlane(vpx_image->planes[VPX_PLANE_U],
562 vpx_image->stride[VPX_PLANE_U],
563 uv_rows,
564 video_frame->get());
565 CopyVPlane(vpx_image->planes[VPX_PLANE_V],
566 vpx_image->stride[VPX_PLANE_V],
567 uv_rows,
568 video_frame->get());
569 if (!vpx_codec_alpha_)
570 return;
571 if (!vpx_image_alpha) {
572 MakeOpaqueAPlane(
573 vpx_image->stride[VPX_PLANE_Y], vpx_image->d_h, video_frame->get());
574 return;
576 CopyAPlane(vpx_image_alpha->planes[VPX_PLANE_Y],
577 vpx_image_alpha->stride[VPX_PLANE_Y],
578 vpx_image_alpha->d_h,
579 video_frame->get());
582 } // namespace media