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"
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 "media/base/bind_to_current_loop.h"
21 #include "media/base/decoder_buffer.h"
22 #include "media/base/demuxer_stream.h"
23 #include "media/base/limits.h"
24 #include "media/base/media_switches.h"
25 #include "media/base/pipeline.h"
26 #include "media/base/video_decoder_config.h"
27 #include "media/base/video_frame.h"
28 #include "media/base/video_util.h"
30 // Include libvpx header files.
31 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide
32 // backwards compatibility for legacy applications using the library.
33 #define VPX_CODEC_DISABLE_COMPAT 1
35 #include "third_party/libvpx/source/libvpx/vpx/vpx_decoder.h"
36 #include "third_party/libvpx/source/libvpx/vpx/vpx_frame_buffer.h"
37 #include "third_party/libvpx/source/libvpx/vpx/vp8dx.h"
42 // Always try to use three threads for video decoding. There is little reason
43 // not to since current day CPUs tend to be multi-core and we measured
44 // performance benefits on older machines such as P4s with hyperthreading.
45 static const int kDecodeThreads
= 2;
46 static const int kMaxDecodeThreads
= 16;
48 // Returns the number of threads.
49 static int GetThreadCount(const VideoDecoderConfig
& config
) {
50 // Refer to http://crbug.com/93932 for tsan suppressions on decoding.
51 int decode_threads
= kDecodeThreads
;
53 const CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
54 std::string
threads(cmd_line
->GetSwitchValueASCII(switches::kVideoThreads
));
55 if (threads
.empty() || !base::StringToInt(threads
, &decode_threads
)) {
56 if (config
.codec() == kCodecVP9
) {
57 // For VP9 decode when using the default thread count, increase the number
58 // of decode threads to equal the maximum number of tiles possible for
59 // higher resolution streams.
60 if (config
.coded_size().width() >= 2048)
62 else if (config
.coded_size().width() >= 1024)
66 return decode_threads
;
69 decode_threads
= std::max(decode_threads
, 0);
70 decode_threads
= std::min(decode_threads
, kMaxDecodeThreads
);
71 return decode_threads
;
74 // Maximum number of frame buffers that can be used (by both chromium and libvpx
75 // combined) for VP9 Decoding.
76 // TODO(vigneshv): Investigate if this can be relaxed to a higher number.
77 static const uint32 kVP9MaxFrameBuffers
= VP9_MAXIMUM_REF_BUFFERS
+
78 VPX_MAXIMUM_WORK_BUFFERS
+
79 limits::kMaxVideoFrames
;
81 class VpxVideoDecoder::MemoryPool
82 : public base::RefCountedThreadSafe
<VpxVideoDecoder::MemoryPool
> {
86 // Callback that will be called by libvpx when it needs a frame buffer.
88 // |user_priv| Private data passed to libvpx (pointer to memory pool).
89 // |min_size| Minimum size needed by libvpx to decompress the next frame.
90 // |fb| Pointer to the frame buffer to update.
91 // Returns 0 on success. Returns < 0 on failure.
92 static int32
GetVP9FrameBuffer(void* user_priv
, size_t min_size
,
93 vpx_codec_frame_buffer
* fb
);
95 // Callback that will be called by libvpx when the frame buffer is no longer
96 // being used by libvpx. Parameters:
97 // |user_priv| Private data passed to libvpx (pointer to memory pool).
98 // |fb| Pointer to the frame buffer that's being released.
99 static int32
ReleaseVP9FrameBuffer(void *user_priv
,
100 vpx_codec_frame_buffer
*fb
);
102 // Generates a "no_longer_needed" closure that holds a reference
104 base::Closure
CreateFrameCallback(void* fb_priv_data
);
107 friend class base::RefCountedThreadSafe
<VpxVideoDecoder::MemoryPool
>;
110 // Reference counted frame buffers used for VP9 decoding. Reference counting
111 // is done manually because both chromium and libvpx has to release this
112 // before a buffer can be re-used.
113 struct VP9FrameBuffer
{
114 VP9FrameBuffer() : ref_cnt(0) {}
115 std::vector
<uint8
> data
;
119 // Gets the next available frame buffer for use by libvpx.
120 VP9FrameBuffer
* GetFreeFrameBuffer(size_t min_size
);
122 // Method that gets called when a VideoFrame that references this pool gets
124 void OnVideoFrameDestroyed(VP9FrameBuffer
* frame_buffer
);
126 // Frame buffers to be used by libvpx for VP9 Decoding.
127 std::vector
<VP9FrameBuffer
*> frame_buffers_
;
129 DISALLOW_COPY_AND_ASSIGN(MemoryPool
);
132 VpxVideoDecoder::MemoryPool::MemoryPool() {}
134 VpxVideoDecoder::MemoryPool::~MemoryPool() {
135 STLDeleteElements(&frame_buffers_
);
138 VpxVideoDecoder::MemoryPool::VP9FrameBuffer
*
139 VpxVideoDecoder::MemoryPool::GetFreeFrameBuffer(size_t min_size
) {
140 // Check if a free frame buffer exists.
142 for (; i
< frame_buffers_
.size(); ++i
) {
143 if (frame_buffers_
[i
]->ref_cnt
== 0)
147 if (i
== frame_buffers_
.size()) {
148 // Maximum number of frame buffers reached.
149 if (i
== kVP9MaxFrameBuffers
)
152 // Create a new frame buffer.
153 frame_buffers_
.push_back(new VP9FrameBuffer());
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
];
162 int32
VpxVideoDecoder::MemoryPool::GetVP9FrameBuffer(
163 void* user_priv
, size_t min_size
, vpx_codec_frame_buffer
* fb
) {
167 VpxVideoDecoder::MemoryPool
* memory_pool
=
168 static_cast<VpxVideoDecoder::MemoryPool
*>(user_priv
);
170 VP9FrameBuffer
* fb_to_use
= memory_pool
->GetFreeFrameBuffer(min_size
);
171 if (fb_to_use
== NULL
)
174 fb
->data
= &fb_to_use
->data
[0];
175 fb
->size
= fb_to_use
->data
.size();
176 ++fb_to_use
->ref_cnt
;
178 // Set the frame buffer's private data to point at the external frame buffer.
179 fb
->priv
= static_cast<void*>(fb_to_use
);
183 int32
VpxVideoDecoder::MemoryPool::ReleaseVP9FrameBuffer(
184 void *user_priv
, vpx_codec_frame_buffer
*fb
) {
185 VP9FrameBuffer
* frame_buffer
= static_cast<VP9FrameBuffer
*>(fb
->priv
);
186 --frame_buffer
->ref_cnt
;
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,
199 void VpxVideoDecoder::MemoryPool::OnVideoFrameDestroyed(
200 VP9FrameBuffer
* frame_buffer
) {
201 --frame_buffer
->ref_cnt
;
204 VpxVideoDecoder::VpxVideoDecoder(
205 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
)
206 : task_runner_(task_runner
),
208 state_(kUninitialized
),
210 vpx_codec_alpha_(NULL
) {
213 VpxVideoDecoder::~VpxVideoDecoder() {
214 DCHECK_EQ(kUninitialized
, state_
);
218 void VpxVideoDecoder::Initialize(const VideoDecoderConfig
& config
,
219 const PipelineStatusCB
& status_cb
) {
220 DCHECK(task_runner_
->BelongsToCurrentThread());
221 DCHECK(config
.IsValidConfig());
222 DCHECK(!config
.is_encrypted());
223 DCHECK(decode_cb_
.is_null());
224 DCHECK(reset_cb_
.is_null());
226 weak_this_
= weak_factory_
.GetWeakPtr();
228 if (!ConfigureDecoder(config
)) {
229 status_cb
.Run(DECODER_ERROR_NOT_SUPPORTED
);
236 status_cb
.Run(PIPELINE_OK
);
239 static vpx_codec_ctx
* InitializeVpxContext(vpx_codec_ctx
* context
,
240 const VideoDecoderConfig
& config
) {
241 context
= new vpx_codec_ctx();
242 vpx_codec_dec_cfg_t vpx_config
= {0};
243 vpx_config
.w
= config
.coded_size().width();
244 vpx_config
.h
= config
.coded_size().height();
245 vpx_config
.threads
= GetThreadCount(config
);
247 vpx_codec_err_t status
= vpx_codec_dec_init(context
,
248 config
.codec() == kCodecVP9
?
253 if (status
!= VPX_CODEC_OK
) {
254 LOG(ERROR
) << "vpx_codec_dec_init failed, status=" << status
;
261 bool VpxVideoDecoder::ConfigureDecoder(const VideoDecoderConfig
& config
) {
262 const CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
263 bool can_handle
= false;
264 if (config
.codec() == kCodecVP9
)
266 if (!cmd_line
->HasSwitch(switches::kDisableVp8AlphaPlayback
) &&
267 config
.codec() == kCodecVP8
&& config
.format() == VideoFrame::YV12A
) {
275 vpx_codec_
= InitializeVpxContext(vpx_codec_
, config
);
279 // We use our own buffers for VP9 so that there is no need to copy data after
281 if (config
.codec() == kCodecVP9
) {
282 memory_pool_
= new MemoryPool();
283 if (vpx_codec_set_frame_buffer_functions(vpx_codec_
,
284 &MemoryPool::GetVP9FrameBuffer
,
285 &MemoryPool::ReleaseVP9FrameBuffer
,
287 LOG(ERROR
) << "Failed to configure external buffers.";
292 if (config
.format() == VideoFrame::YV12A
) {
293 vpx_codec_alpha_
= InitializeVpxContext(vpx_codec_alpha_
, config
);
294 if (!vpx_codec_alpha_
)
301 void VpxVideoDecoder::CloseDecoder() {
303 vpx_codec_destroy(vpx_codec_
);
308 if (vpx_codec_alpha_
) {
309 vpx_codec_destroy(vpx_codec_alpha_
);
310 delete vpx_codec_alpha_
;
311 vpx_codec_alpha_
= NULL
;
315 void VpxVideoDecoder::Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
316 const DecodeCB
& decode_cb
) {
317 DCHECK(task_runner_
->BelongsToCurrentThread());
318 DCHECK(!decode_cb
.is_null());
319 CHECK_NE(state_
, kUninitialized
);
320 CHECK(decode_cb_
.is_null()) << "Overlapping decodes are not supported.";
322 decode_cb_
= BindToCurrentLoop(decode_cb
);
324 if (state_
== kError
) {
325 base::ResetAndReturn(&decode_cb_
).Run(kDecodeError
, NULL
);
329 // Return empty frames if decoding has finished.
330 if (state_
== kDecodeFinished
) {
331 base::ResetAndReturn(&decode_cb_
).Run(kOk
, VideoFrame::CreateEOSFrame());
335 DecodeBuffer(buffer
);
338 void VpxVideoDecoder::Reset(const base::Closure
& closure
) {
339 DCHECK(task_runner_
->BelongsToCurrentThread());
340 DCHECK(reset_cb_
.is_null());
341 reset_cb_
= BindToCurrentLoop(closure
);
343 // Defer the reset if a decode is pending.
344 if (!decode_cb_
.is_null())
350 void VpxVideoDecoder::Stop(const base::Closure
& closure
) {
351 DCHECK(task_runner_
->BelongsToCurrentThread());
352 base::ScopedClosureRunner
runner(BindToCurrentLoop(closure
));
354 if (state_
== kUninitialized
)
357 if (!decode_cb_
.is_null()) {
358 base::ResetAndReturn(&decode_cb_
).Run(kAborted
, NULL
);
359 // Reset is pending only when decode is pending.
360 if (!reset_cb_
.is_null())
361 base::ResetAndReturn(&reset_cb_
).Run();
364 state_
= kUninitialized
;
367 bool VpxVideoDecoder::HasAlpha() const {
368 return vpx_codec_alpha_
!= NULL
;
371 void VpxVideoDecoder::DecodeBuffer(const scoped_refptr
<DecoderBuffer
>& buffer
) {
372 DCHECK(task_runner_
->BelongsToCurrentThread());
373 DCHECK_NE(state_
, kUninitialized
);
374 DCHECK_NE(state_
, kDecodeFinished
);
375 DCHECK_NE(state_
, kError
);
376 DCHECK(reset_cb_
.is_null());
377 DCHECK(!decode_cb_
.is_null());
380 // Transition to kDecodeFinished on the first end of stream buffer.
381 if (state_
== kNormal
&& buffer
->end_of_stream()) {
382 state_
= kDecodeFinished
;
383 base::ResetAndReturn(&decode_cb_
).Run(kOk
, VideoFrame::CreateEOSFrame());
387 scoped_refptr
<VideoFrame
> video_frame
;
388 if (!VpxDecode(buffer
, &video_frame
)) {
390 base::ResetAndReturn(&decode_cb_
).Run(kDecodeError
, NULL
);
394 // If we didn't get a frame we need more data.
395 if (!video_frame
.get()) {
396 base::ResetAndReturn(&decode_cb_
).Run(kNotEnoughData
, NULL
);
400 base::ResetAndReturn(&decode_cb_
).Run(kOk
, video_frame
);
403 bool VpxVideoDecoder::VpxDecode(const scoped_refptr
<DecoderBuffer
>& buffer
,
404 scoped_refptr
<VideoFrame
>* video_frame
) {
406 DCHECK(!buffer
->end_of_stream());
408 // Pass |buffer| to libvpx.
409 int64 timestamp
= buffer
->timestamp().InMicroseconds();
410 void* user_priv
= reinterpret_cast<void*>(×tamp
);
411 vpx_codec_err_t status
= vpx_codec_decode(vpx_codec_
,
416 if (status
!= VPX_CODEC_OK
) {
417 LOG(ERROR
) << "vpx_codec_decode() failed, status=" << status
;
421 // Gets pointer to decoded data.
422 vpx_codec_iter_t iter
= NULL
;
423 const vpx_image_t
* vpx_image
= vpx_codec_get_frame(vpx_codec_
, &iter
);
429 if (vpx_image
->user_priv
!= reinterpret_cast<void*>(×tamp
)) {
430 LOG(ERROR
) << "Invalid output timestamp.";
434 const vpx_image_t
* vpx_image_alpha
= NULL
;
435 if (vpx_codec_alpha_
&& buffer
->side_data_size() >= 8) {
436 // Pass alpha data to libvpx.
437 int64 timestamp_alpha
= buffer
->timestamp().InMicroseconds();
438 void* user_priv_alpha
= reinterpret_cast<void*>(×tamp_alpha
);
440 // First 8 bytes of side data is side_data_id in big endian.
441 const uint64 side_data_id
= base::NetToHost64(
442 *(reinterpret_cast<const uint64
*>(buffer
->side_data())));
443 if (side_data_id
== 1) {
444 status
= vpx_codec_decode(vpx_codec_alpha_
,
445 buffer
->side_data() + 8,
446 buffer
->side_data_size() - 8,
450 if (status
!= VPX_CODEC_OK
) {
451 LOG(ERROR
) << "vpx_codec_decode() failed on alpha, status=" << status
;
455 // Gets pointer to decoded data.
456 vpx_codec_iter_t iter_alpha
= NULL
;
457 vpx_image_alpha
= vpx_codec_get_frame(vpx_codec_alpha_
, &iter_alpha
);
458 if (!vpx_image_alpha
) {
463 if (vpx_image_alpha
->user_priv
!=
464 reinterpret_cast<void*>(×tamp_alpha
)) {
465 LOG(ERROR
) << "Invalid output timestamp on alpha.";
471 CopyVpxImageTo(vpx_image
, vpx_image_alpha
, video_frame
);
472 (*video_frame
)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp
));
476 void VpxVideoDecoder::DoReset() {
477 DCHECK(decode_cb_
.is_null());
484 void VpxVideoDecoder::CopyVpxImageTo(const vpx_image
* vpx_image
,
485 const struct vpx_image
* vpx_image_alpha
,
486 scoped_refptr
<VideoFrame
>* video_frame
) {
488 CHECK(vpx_image
->fmt
== VPX_IMG_FMT_I420
||
489 vpx_image
->fmt
== VPX_IMG_FMT_YV12
);
491 gfx::Size
size(vpx_image
->d_w
, vpx_image
->d_h
);
493 if (!vpx_codec_alpha_
&& memory_pool_
) {
494 *video_frame
= VideoFrame::WrapExternalYuvData(
496 size
, gfx::Rect(size
), config_
.natural_size(),
497 vpx_image
->stride
[VPX_PLANE_Y
],
498 vpx_image
->stride
[VPX_PLANE_U
],
499 vpx_image
->stride
[VPX_PLANE_V
],
500 vpx_image
->planes
[VPX_PLANE_Y
],
501 vpx_image
->planes
[VPX_PLANE_U
],
502 vpx_image
->planes
[VPX_PLANE_V
],
504 memory_pool_
->CreateFrameCallback(vpx_image
->fb_priv
));
508 *video_frame
= frame_pool_
.CreateFrame(
509 vpx_codec_alpha_
? VideoFrame::YV12A
: VideoFrame::YV12
,
512 config_
.natural_size(),
515 CopyYPlane(vpx_image
->planes
[VPX_PLANE_Y
],
516 vpx_image
->stride
[VPX_PLANE_Y
],
519 CopyUPlane(vpx_image
->planes
[VPX_PLANE_U
],
520 vpx_image
->stride
[VPX_PLANE_U
],
521 (vpx_image
->d_h
+ 1) / 2,
523 CopyVPlane(vpx_image
->planes
[VPX_PLANE_V
],
524 vpx_image
->stride
[VPX_PLANE_V
],
525 (vpx_image
->d_h
+ 1) / 2,
527 if (!vpx_codec_alpha_
)
529 if (!vpx_image_alpha
) {
531 vpx_image
->stride
[VPX_PLANE_Y
], vpx_image
->d_h
, video_frame
->get());
534 CopyAPlane(vpx_image_alpha
->planes
[VPX_PLANE_Y
],
535 vpx_image
->stride
[VPX_PLANE_Y
],