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/gpu_video_decoder.h"
10 #include "base/callback_helpers.h"
11 #include "base/command_line.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/stl_util.h"
16 #include "base/task_runner_util.h"
17 #include "gpu/command_buffer/common/mailbox_holder.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/decoder_buffer.h"
20 #include "media/base/media_switches.h"
21 #include "media/base/pipeline.h"
22 #include "media/base/video_decoder_config.h"
23 #include "media/renderers/gpu_video_accelerator_factories.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
28 const char GpuVideoDecoder::kDecoderName
[] = "GpuVideoDecoder";
30 // Maximum number of concurrent VDA::Decode() operations GVD will maintain.
31 // Higher values allow better pipelining in the GPU, but also require more
33 enum { kMaxInFlightDecodes
= 4 };
35 // Size of shared-memory segments we allocate. Since we reuse them we let them
36 // be on the beefy side.
37 static const size_t kSharedMemorySegmentBytes
= 100 << 10;
39 GpuVideoDecoder::SHMBuffer::SHMBuffer(scoped_ptr
<base::SharedMemory
> m
,
41 : shm(m
.Pass()), size(s
) {
44 GpuVideoDecoder::SHMBuffer::~SHMBuffer() {}
46 GpuVideoDecoder::PendingDecoderBuffer::PendingDecoderBuffer(
48 const scoped_refptr
<DecoderBuffer
>& b
,
49 const DecodeCB
& done_cb
)
50 : shm_buffer(s
), buffer(b
), done_cb(done_cb
) {
53 GpuVideoDecoder::PendingDecoderBuffer::~PendingDecoderBuffer() {}
55 GpuVideoDecoder::BufferData::BufferData(
56 int32 bbid
, base::TimeDelta ts
, const gfx::Rect
& vr
, const gfx::Size
& ns
)
57 : bitstream_buffer_id(bbid
), timestamp(ts
), visible_rect(vr
),
61 GpuVideoDecoder::BufferData::~BufferData() {}
63 GpuVideoDecoder::GpuVideoDecoder(
64 const scoped_refptr
<GpuVideoAcceleratorFactories
>& factories
)
65 : needs_bitstream_conversion_(false),
66 factories_(factories
),
68 decoder_texture_target_(0),
69 next_picture_buffer_id_(0),
70 next_bitstream_buffer_id_(0),
71 available_pictures_(0),
73 DCHECK(factories_
.get());
76 void GpuVideoDecoder::Reset(const base::Closure
& closure
) {
77 DVLOG(3) << "Reset()";
78 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
80 if (state_
== kDrainingDecoder
) {
81 base::MessageLoop::current()->PostTask(
84 &GpuVideoDecoder::Reset
, weak_factory_
.GetWeakPtr(), closure
));
89 base::MessageLoop::current()->PostTask(FROM_HERE
, closure
);
93 DCHECK(pending_reset_cb_
.is_null());
94 pending_reset_cb_
= BindToCurrentLoop(closure
);
99 static bool IsCodedSizeSupported(const gfx::Size
& coded_size
,
100 const gfx::Size
& min_resolution
,
101 const gfx::Size
& max_resolution
) {
102 return (coded_size
.width() <= max_resolution
.width() &&
103 coded_size
.height() <= max_resolution
.height() &&
104 coded_size
.width() >= min_resolution
.width() &&
105 coded_size
.height() >= min_resolution
.height());
108 // Report |success| to UMA and run |cb| with it. This is super-specific to the
109 // UMA stat reported because the UMA_HISTOGRAM_ENUMERATION API requires a
110 // callsite to always be called with the same stat name (can't parameterize it).
111 static void ReportGpuVideoDecoderInitializeStatusToUMAAndRunCB(
112 const VideoDecoder::InitCB
& cb
,
114 // TODO(xhwang): Report |success| directly.
115 PipelineStatus status
= success
? PIPELINE_OK
: DECODER_ERROR_NOT_SUPPORTED
;
116 UMA_HISTOGRAM_ENUMERATION(
117 "Media.GpuVideoDecoderInitializeStatus", status
, PIPELINE_STATUS_MAX
+ 1);
121 std::string
GpuVideoDecoder::GetDisplayName() const {
125 void GpuVideoDecoder::Initialize(const VideoDecoderConfig
& config
,
126 bool /* low_delay */,
127 const InitCB
& init_cb
,
128 const OutputCB
& output_cb
) {
129 DVLOG(3) << "Initialize()";
130 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
131 DCHECK(config
.IsValidConfig());
132 DCHECK(!config
.is_encrypted());
134 InitCB bound_init_cb
=
135 base::Bind(&ReportGpuVideoDecoderInitializeStatusToUMAAndRunCB
,
136 BindToCurrentLoop(init_cb
));
138 bool previously_initialized
= config_
.IsValidConfig();
139 DVLOG(1) << "(Re)initializing GVD with config: "
140 << config
.AsHumanReadableString();
142 // TODO(posciak): destroy and create a new VDA on codec/profile change
143 // (http://crbug.com/260224).
144 if (previously_initialized
&& (config_
.profile() != config
.profile())) {
145 DVLOG(1) << "Codec or profile changed, cannot reinitialize.";
146 bound_init_cb
.Run(false);
150 if (!IsProfileSupported(config
.profile(), config
.coded_size())) {
151 bound_init_cb
.Run(false);
156 needs_bitstream_conversion_
= (config
.codec() == kCodecH264
);
157 output_cb_
= BindToCurrentLoop(output_cb
);
159 if (previously_initialized
) {
160 // Reinitialization with a different config (but same codec and profile).
161 // VDA should handle it by detecting this in-stream by itself,
162 // no need to notify it.
163 bound_init_cb
.Run(true);
167 vda_
= factories_
->CreateVideoDecodeAccelerator().Pass();
168 if (!vda_
|| !vda_
->Initialize(config
.profile(), this)) {
169 bound_init_cb
.Run(false);
173 DVLOG(3) << "GpuVideoDecoder::Initialize() succeeded.";
174 bound_init_cb
.Run(true);
177 void GpuVideoDecoder::DestroyPictureBuffers(PictureBufferMap
* buffers
) {
178 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
179 for (PictureBufferMap::iterator it
= buffers
->begin(); it
!= buffers
->end();
181 factories_
->DeleteTexture(it
->second
.texture_id());
187 void GpuVideoDecoder::DestroyVDA() {
188 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
192 // Not destroying PictureBuffers in |picture_buffers_at_display_| yet, since
193 // their textures may still be in use by the user of this GpuVideoDecoder.
194 for (PictureBufferTextureMap::iterator it
=
195 picture_buffers_at_display_
.begin();
196 it
!= picture_buffers_at_display_
.end();
198 assigned_picture_buffers_
.erase(it
->first
);
200 DestroyPictureBuffers(&assigned_picture_buffers_
);
203 void GpuVideoDecoder::Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
204 const DecodeCB
& decode_cb
) {
205 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
206 DCHECK(pending_reset_cb_
.is_null());
208 DecodeCB bound_decode_cb
= BindToCurrentLoop(decode_cb
);
210 if (state_
== kError
|| !vda_
) {
211 bound_decode_cb
.Run(kDecodeError
);
216 case kDecoderDrained
:
221 case kDrainingDecoder
:
227 DCHECK_EQ(state_
, kNormal
);
229 if (buffer
->end_of_stream()) {
230 state_
= kDrainingDecoder
;
231 eos_decode_cb_
= bound_decode_cb
;
236 size_t size
= buffer
->data_size();
237 scoped_ptr
<SHMBuffer
> shm_buffer
= GetSHM(size
);
239 bound_decode_cb
.Run(kDecodeError
);
243 memcpy(shm_buffer
->shm
->memory(), buffer
->data(), size
);
244 // AndroidVideoDecodeAccelerator needs the timestamp to output frames in
245 // presentation order.
246 BitstreamBuffer
bitstream_buffer(next_bitstream_buffer_id_
,
247 shm_buffer
->shm
->handle(), size
,
248 buffer
->timestamp());
249 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
250 next_bitstream_buffer_id_
= (next_bitstream_buffer_id_
+ 1) & 0x3FFFFFFF;
251 DCHECK(!ContainsKey(bitstream_buffers_in_decoder_
, bitstream_buffer
.id()));
252 bitstream_buffers_in_decoder_
.insert(std::make_pair(
253 bitstream_buffer
.id(),
254 PendingDecoderBuffer(shm_buffer
.release(), buffer
, decode_cb
)));
255 DCHECK_LE(static_cast<int>(bitstream_buffers_in_decoder_
.size()),
256 kMaxInFlightDecodes
);
257 RecordBufferData(bitstream_buffer
, *buffer
.get());
259 vda_
->Decode(bitstream_buffer
);
262 void GpuVideoDecoder::RecordBufferData(const BitstreamBuffer
& bitstream_buffer
,
263 const DecoderBuffer
& buffer
) {
264 input_buffer_data_
.push_front(BufferData(bitstream_buffer
.id(),
266 config_
.visible_rect(),
267 config_
.natural_size()));
268 // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but
269 // that's too small for some pathological B-frame test videos. The cost of
270 // using too-high a value is low (192 bits per extra slot).
271 static const size_t kMaxInputBufferDataSize
= 128;
272 // Pop from the back of the list, because that's the oldest and least likely
273 // to be useful in the future data.
274 if (input_buffer_data_
.size() > kMaxInputBufferDataSize
)
275 input_buffer_data_
.pop_back();
278 void GpuVideoDecoder::GetBufferData(int32 id
, base::TimeDelta
* timestamp
,
279 gfx::Rect
* visible_rect
,
280 gfx::Size
* natural_size
) {
281 for (std::list
<BufferData
>::const_iterator it
=
282 input_buffer_data_
.begin(); it
!= input_buffer_data_
.end();
284 if (it
->bitstream_buffer_id
!= id
)
286 *timestamp
= it
->timestamp
;
287 *visible_rect
= it
->visible_rect
;
288 *natural_size
= it
->natural_size
;
291 NOTREACHED() << "Missing bitstreambuffer id: " << id
;
294 bool GpuVideoDecoder::NeedsBitstreamConversion() const {
295 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
296 return needs_bitstream_conversion_
;
299 bool GpuVideoDecoder::CanReadWithoutStalling() const {
300 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
302 next_picture_buffer_id_
== 0 || // Decode() will ProvidePictureBuffers().
303 available_pictures_
> 0;
306 int GpuVideoDecoder::GetMaxDecodeRequests() const {
307 return kMaxInFlightDecodes
;
310 void GpuVideoDecoder::ProvidePictureBuffers(uint32 count
,
311 const gfx::Size
& size
,
312 uint32 texture_target
) {
313 DVLOG(3) << "ProvidePictureBuffers(" << count
<< ", "
314 << size
.width() << "x" << size
.height() << ")";
315 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
317 std::vector
<uint32
> texture_ids
;
318 std::vector
<gpu::Mailbox
> texture_mailboxes
;
319 decoder_texture_target_
= texture_target
;
320 if (!factories_
->CreateTextures(count
,
324 decoder_texture_target_
)) {
325 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
328 DCHECK_EQ(count
, texture_ids
.size());
329 DCHECK_EQ(count
, texture_mailboxes
.size());
334 std::vector
<PictureBuffer
> picture_buffers
;
335 for (size_t i
= 0; i
< texture_ids
.size(); ++i
) {
336 picture_buffers
.push_back(PictureBuffer(
337 next_picture_buffer_id_
++, size
, texture_ids
[i
], texture_mailboxes
[i
]));
338 bool inserted
= assigned_picture_buffers_
.insert(std::make_pair(
339 picture_buffers
.back().id(), picture_buffers
.back())).second
;
343 available_pictures_
+= count
;
345 vda_
->AssignPictureBuffers(picture_buffers
);
348 void GpuVideoDecoder::DismissPictureBuffer(int32 id
) {
349 DVLOG(3) << "DismissPictureBuffer(" << id
<< ")";
350 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
352 PictureBufferMap::iterator it
= assigned_picture_buffers_
.find(id
);
353 if (it
== assigned_picture_buffers_
.end()) {
354 NOTREACHED() << "Missing picture buffer: " << id
;
358 PictureBuffer buffer_to_dismiss
= it
->second
;
359 assigned_picture_buffers_
.erase(it
);
361 if (!picture_buffers_at_display_
.count(id
)) {
362 // We can delete the texture immediately as it's not being displayed.
363 factories_
->DeleteTexture(buffer_to_dismiss
.texture_id());
364 CHECK_GT(available_pictures_
, 0);
365 --available_pictures_
;
367 // Not destroying a texture in display in |picture_buffers_at_display_|.
368 // Postpone deletion until after it's returned to us.
371 void GpuVideoDecoder::PictureReady(const media::Picture
& picture
) {
372 DVLOG(3) << "PictureReady()";
373 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
375 PictureBufferMap::iterator it
=
376 assigned_picture_buffers_
.find(picture
.picture_buffer_id());
377 if (it
== assigned_picture_buffers_
.end()) {
378 DLOG(ERROR
) << "Missing picture buffer: " << picture
.picture_buffer_id();
379 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
382 const PictureBuffer
& pb
= it
->second
;
384 // Update frame's timestamp.
385 base::TimeDelta timestamp
;
386 // Some of the VDAs like DXVA, AVDA, and VTVDA don't support and thus don't
387 // provide us with visible size in picture.size, passing (0, 0) instead, so
388 // for those cases drop it and use config information instead.
389 gfx::Rect visible_rect
;
390 gfx::Size natural_size
;
391 GetBufferData(picture
.bitstream_buffer_id(), ×tamp
, &visible_rect
,
394 if (!picture
.visible_rect().IsEmpty()) {
395 visible_rect
= picture
.visible_rect();
397 if (!gfx::Rect(pb
.size()).Contains(visible_rect
)) {
398 LOG(WARNING
) << "Visible size " << visible_rect
.ToString()
399 << " is larger than coded size " << pb
.size().ToString();
400 visible_rect
= gfx::Rect(pb
.size());
403 DCHECK(decoder_texture_target_
);
405 scoped_refptr
<VideoFrame
> frame(VideoFrame::WrapNativeTexture(
407 gpu::MailboxHolder(pb
.texture_mailbox(), decoder_texture_target_
,
409 BindToCurrentLoop(base::Bind(
410 &GpuVideoDecoder::ReleaseMailbox
, weak_factory_
.GetWeakPtr(),
411 factories_
, picture
.picture_buffer_id(), pb
.texture_id())),
412 pb
.size(), visible_rect
, natural_size
, timestamp
));
414 DLOG(ERROR
) << "Create frame failed for: " << picture
.picture_buffer_id();
415 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
418 if (picture
.allow_overlay())
419 frame
->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY
, true);
420 CHECK_GT(available_pictures_
, 0);
421 --available_pictures_
;
423 picture_buffers_at_display_
.insert(std::make_pair(
424 picture
.picture_buffer_id(),
425 pb
.texture_id())).second
;
431 void GpuVideoDecoder::DeliverFrame(
432 const scoped_refptr
<VideoFrame
>& frame
) {
433 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
435 // During a pending vda->Reset(), we don't accumulate frames. Drop it on the
437 if (!pending_reset_cb_
.is_null())
440 output_cb_
.Run(frame
);
444 void GpuVideoDecoder::ReleaseMailbox(
445 base::WeakPtr
<GpuVideoDecoder
> decoder
,
446 const scoped_refptr
<media::GpuVideoAcceleratorFactories
>& factories
,
447 int64 picture_buffer_id
,
449 uint32 release_sync_point
) {
450 DCHECK(factories
->GetTaskRunner()->BelongsToCurrentThread());
451 factories
->WaitSyncPoint(release_sync_point
);
454 decoder
->ReusePictureBuffer(picture_buffer_id
);
457 // It's the last chance to delete the texture after display,
458 // because GpuVideoDecoder was destructed.
459 factories
->DeleteTexture(texture_id
);
462 void GpuVideoDecoder::ReusePictureBuffer(int64 picture_buffer_id
) {
463 DVLOG(3) << "ReusePictureBuffer(" << picture_buffer_id
<< ")";
464 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
466 DCHECK(!picture_buffers_at_display_
.empty());
467 PictureBufferTextureMap::iterator display_iterator
=
468 picture_buffers_at_display_
.find(picture_buffer_id
);
469 uint32 texture_id
= display_iterator
->second
;
470 DCHECK(display_iterator
!= picture_buffers_at_display_
.end());
471 picture_buffers_at_display_
.erase(display_iterator
);
473 if (!assigned_picture_buffers_
.count(picture_buffer_id
)) {
474 // This picture was dismissed while in display, so we postponed deletion.
475 factories_
->DeleteTexture(texture_id
);
479 ++available_pictures_
;
481 // DestroyVDA() might already have been called.
483 vda_
->ReusePictureBuffer(picture_buffer_id
);
486 scoped_ptr
<GpuVideoDecoder::SHMBuffer
> GpuVideoDecoder::GetSHM(
488 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
489 if (available_shm_segments_
.empty() ||
490 available_shm_segments_
.back()->size
< min_size
) {
491 size_t size_to_allocate
= std::max(min_size
, kSharedMemorySegmentBytes
);
492 scoped_ptr
<base::SharedMemory
> shm
=
493 factories_
->CreateSharedMemory(size_to_allocate
);
494 // CreateSharedMemory() can return NULL during Shutdown.
497 return make_scoped_ptr(new SHMBuffer(shm
.Pass(), size_to_allocate
));
499 scoped_ptr
<SHMBuffer
> ret(available_shm_segments_
.back());
500 available_shm_segments_
.pop_back();
504 void GpuVideoDecoder::PutSHM(scoped_ptr
<SHMBuffer
> shm_buffer
) {
505 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
506 available_shm_segments_
.push_back(shm_buffer
.release());
509 void GpuVideoDecoder::NotifyEndOfBitstreamBuffer(int32 id
) {
510 DVLOG(3) << "NotifyEndOfBitstreamBuffer(" << id
<< ")";
511 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
513 std::map
<int32
, PendingDecoderBuffer
>::iterator it
=
514 bitstream_buffers_in_decoder_
.find(id
);
515 if (it
== bitstream_buffers_in_decoder_
.end()) {
516 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
517 NOTREACHED() << "Missing bitstream buffer: " << id
;
521 PutSHM(make_scoped_ptr(it
->second
.shm_buffer
));
522 it
->second
.done_cb
.Run(state_
== kError
? kDecodeError
: kOk
);
523 bitstream_buffers_in_decoder_
.erase(it
);
526 GpuVideoDecoder::~GpuVideoDecoder() {
527 DVLOG(3) << __FUNCTION__
;
528 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
532 DCHECK(assigned_picture_buffers_
.empty());
534 for (size_t i
= 0; i
< available_shm_segments_
.size(); ++i
) {
535 delete available_shm_segments_
[i
];
537 available_shm_segments_
.clear();
539 for (std::map
<int32
, PendingDecoderBuffer
>::iterator it
=
540 bitstream_buffers_in_decoder_
.begin();
541 it
!= bitstream_buffers_in_decoder_
.end(); ++it
) {
542 delete it
->second
.shm_buffer
;
543 it
->second
.done_cb
.Run(kAborted
);
545 bitstream_buffers_in_decoder_
.clear();
547 if (!pending_reset_cb_
.is_null())
548 base::ResetAndReturn(&pending_reset_cb_
).Run();
551 void GpuVideoDecoder::NotifyFlushDone() {
552 DVLOG(3) << "NotifyFlushDone()";
553 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
554 DCHECK_EQ(state_
, kDrainingDecoder
);
555 state_
= kDecoderDrained
;
556 base::ResetAndReturn(&eos_decode_cb_
).Run(kOk
);
559 void GpuVideoDecoder::NotifyResetDone() {
560 DVLOG(3) << "NotifyResetDone()";
561 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
562 DCHECK(bitstream_buffers_in_decoder_
.empty());
564 // This needs to happen after the Reset() on vda_ is done to ensure pictures
565 // delivered during the reset can find their time data.
566 input_buffer_data_
.clear();
568 if (!pending_reset_cb_
.is_null())
569 base::ResetAndReturn(&pending_reset_cb_
).Run();
572 void GpuVideoDecoder::NotifyError(media::VideoDecodeAccelerator::Error error
) {
573 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
579 DLOG(ERROR
) << "VDA Error: " << error
;
583 bool GpuVideoDecoder::IsProfileSupported(VideoCodecProfile profile
,
584 const gfx::Size
& coded_size
) {
585 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
586 VideoDecodeAccelerator::SupportedProfiles supported_profiles
=
587 factories_
->GetVideoDecodeAcceleratorSupportedProfiles();
588 for (const auto& supported_profile
: supported_profiles
) {
589 if (profile
== supported_profile
.profile
) {
590 return IsCodedSizeSupported(coded_size
,
591 supported_profile
.min_resolution
,
592 supported_profile
.max_resolution
);
598 void GpuVideoDecoder::DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent()
600 DCHECK(factories_
->GetTaskRunner()->BelongsToCurrentThread());