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"
12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/task_runner_util.h"
15 #include "media/base/bind_to_loop.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/media_log.h"
18 #include "media/base/pipeline.h"
19 #include "media/base/pipeline_status.h"
20 #include "media/base/video_decoder_config.h"
21 #include "media/filters/gpu_video_accelerator_factories.h"
25 // Maximum number of concurrent VDA::Decode() operations GVD will maintain.
26 // Higher values allow better pipelining in the GPU, but also require more
28 enum { kMaxInFlightDecodes
= 4 };
30 // Size of shared-memory segments we allocate. Since we reuse them we let them
31 // be on the beefy side.
32 static const size_t kSharedMemorySegmentBytes
= 100 << 10;
34 GpuVideoDecoder::SHMBuffer::SHMBuffer(base::SharedMemory
* m
, size_t s
)
38 GpuVideoDecoder::SHMBuffer::~SHMBuffer() {}
40 GpuVideoDecoder::BufferPair::BufferPair(
41 SHMBuffer
* s
, const scoped_refptr
<DecoderBuffer
>& b
)
42 : shm_buffer(s
), buffer(b
) {
45 GpuVideoDecoder::BufferPair::~BufferPair() {}
47 GpuVideoDecoder::BufferData::BufferData(
48 int32 bbid
, base::TimeDelta ts
, const gfx::Rect
& vr
, const gfx::Size
& ns
)
49 : bitstream_buffer_id(bbid
), timestamp(ts
), visible_rect(vr
),
53 GpuVideoDecoder::BufferData::~BufferData() {}
55 GpuVideoDecoder::GpuVideoDecoder(
56 const scoped_refptr
<GpuVideoAcceleratorFactories
>& factories
,
57 const scoped_refptr
<MediaLog
>& media_log
)
58 : needs_bitstream_conversion_(false),
59 gvd_loop_proxy_(factories
->GetMessageLoop()),
61 factories_(factories
),
63 media_log_(media_log
),
64 decoder_texture_target_(0),
65 next_picture_buffer_id_(0),
66 next_bitstream_buffer_id_(0),
67 available_pictures_(0) {
68 DCHECK(factories_
.get());
71 void GpuVideoDecoder::Reset(const base::Closure
& closure
) {
72 DVLOG(3) << "Reset()";
73 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
75 if (state_
== kDrainingDecoder
&& !factories_
->IsAborted()) {
76 gvd_loop_proxy_
->PostTask(FROM_HERE
, base::Bind(
77 &GpuVideoDecoder::Reset
, weak_this_
, closure
));
78 // NOTE: if we're deferring Reset() until a Flush() completes, return
79 // queued pictures to the VDA so they can be used to finish that Flush().
80 if (pending_decode_cb_
.is_null())
81 ready_video_frames_
.clear();
85 // Throw away any already-decoded, not-yet-delivered frames.
86 ready_video_frames_
.clear();
89 gvd_loop_proxy_
->PostTask(FROM_HERE
, closure
);
93 if (!pending_decode_cb_
.is_null())
94 EnqueueFrameAndTriggerFrameDelivery(VideoFrame::CreateEmptyFrame());
96 DCHECK(pending_reset_cb_
.is_null());
97 pending_reset_cb_
= BindToCurrentLoop(closure
);
102 void GpuVideoDecoder::Stop(const base::Closure
& closure
) {
103 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
106 if (!pending_decode_cb_
.is_null())
107 EnqueueFrameAndTriggerFrameDelivery(VideoFrame::CreateEmptyFrame());
108 if (!pending_reset_cb_
.is_null())
109 base::ResetAndReturn(&pending_reset_cb_
).Run();
110 BindToCurrentLoop(closure
).Run();
113 static bool IsCodedSizeSupported(const gfx::Size
& coded_size
) {
114 // Only non-Windows, Ivy Bridge+ platforms can support more than 1920x1080.
115 // We test against 1088 to account for 16x16 macroblocks.
116 if (coded_size
.width() <= 1920 && coded_size
.height() <= 1088)
120 bool hw_large_video_support
=
121 (cpu
.vendor_name() == "GenuineIntel") && cpu
.model() >= 58;
122 bool os_large_video_support
= true;
124 os_large_video_support
= false;
126 return os_large_video_support
&& hw_large_video_support
;
129 void GpuVideoDecoder::Initialize(const VideoDecoderConfig
& config
,
130 const PipelineStatusCB
& orig_status_cb
) {
131 DVLOG(3) << "Initialize()";
132 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
133 DCHECK(config
.IsValidConfig());
134 DCHECK(!config
.is_encrypted());
136 weak_this_
= weak_factory_
.GetWeakPtr();
138 PipelineStatusCB status_cb
= CreateUMAReportingPipelineCB(
139 "Media.GpuVideoDecoderInitializeStatus",
140 BindToCurrentLoop(orig_status_cb
));
142 bool previously_initialized
= config_
.IsValidConfig();
143 #if !defined(OS_CHROMEOS) && !defined(OS_WIN)
144 if (previously_initialized
) {
145 // TODO(xhwang): Make GpuVideoDecoder reinitializable.
146 // See http://crbug.com/233608
147 DVLOG(1) << "GpuVideoDecoder reinitialization not supported.";
148 status_cb
.Run(DECODER_ERROR_NOT_SUPPORTED
);
152 DVLOG(1) << "(Re)initializing GVD with config: "
153 << config
.AsHumanReadableString();
155 // TODO(posciak): destroy and create a new VDA on codec/profile change
156 // (http://crbug.com/260224).
157 if (previously_initialized
&& (config_
.profile() != config
.profile())) {
158 DVLOG(1) << "Codec or profile changed, cannot reinitialize.";
159 status_cb
.Run(DECODER_ERROR_NOT_SUPPORTED
);
163 if (!IsCodedSizeSupported(config
.coded_size())) {
164 status_cb
.Run(DECODER_ERROR_NOT_SUPPORTED
);
169 needs_bitstream_conversion_
= (config
.codec() == kCodecH264
);
171 if (previously_initialized
) {
172 // Reinitialization with a different config (but same codec and profile).
173 // VDA should handle it by detecting this in-stream by itself,
174 // no need to notify it.
175 status_cb
.Run(PIPELINE_OK
);
180 factories_
->CreateVideoDecodeAccelerator(config
.profile(), this).Pass();
182 status_cb
.Run(DECODER_ERROR_NOT_SUPPORTED
);
186 DVLOG(3) << "GpuVideoDecoder::Initialize() succeeded.";
187 media_log_
->SetStringProperty("video_decoder", "gpu");
188 status_cb
.Run(PIPELINE_OK
);
191 void GpuVideoDecoder::DestroyTextures() {
192 std::map
<int32
, PictureBuffer
>::iterator it
;
194 for (it
= assigned_picture_buffers_
.begin();
195 it
!= assigned_picture_buffers_
.end(); ++it
) {
196 factories_
->DeleteTexture(it
->second
.texture_id());
198 assigned_picture_buffers_
.clear();
200 for (it
= dismissed_picture_buffers_
.begin();
201 it
!= dismissed_picture_buffers_
.end(); ++it
) {
202 factories_
->DeleteTexture(it
->second
.texture_id());
204 dismissed_picture_buffers_
.clear();
207 void GpuVideoDecoder::DestroyVDA() {
208 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
211 vda_
.release()->Destroy();
216 void GpuVideoDecoder::Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
217 const DecodeCB
& decode_cb
) {
218 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
219 DCHECK(pending_reset_cb_
.is_null());
220 DCHECK(pending_decode_cb_
.is_null());
222 pending_decode_cb_
= BindToCurrentLoop(decode_cb
);
224 if (state_
== kError
|| !vda_
) {
225 base::ResetAndReturn(&pending_decode_cb_
).Run(kDecodeError
, NULL
);
230 case kDecoderDrained
:
231 if (!ready_video_frames_
.empty()) {
232 EnqueueFrameAndTriggerFrameDelivery(NULL
);
239 case kDrainingDecoder
:
240 DCHECK(buffer
->end_of_stream());
241 // Do nothing. Will be satisfied either by a PictureReady or
242 // NotifyFlushDone below.
249 if (buffer
->end_of_stream()) {
250 if (state_
== kNormal
) {
251 state_
= kDrainingDecoder
;
253 // If we have ready frames, go ahead and process them to ensure that the
254 // Flush operation does not block in the VDA due to lack of picture
256 if (!ready_video_frames_
.empty())
257 EnqueueFrameAndTriggerFrameDelivery(NULL
);
262 size_t size
= buffer
->data_size();
263 SHMBuffer
* shm_buffer
= GetSHM(size
);
265 base::ResetAndReturn(&pending_decode_cb_
).Run(kDecodeError
, NULL
);
269 memcpy(shm_buffer
->shm
->memory(), buffer
->data(), size
);
270 BitstreamBuffer
bitstream_buffer(
271 next_bitstream_buffer_id_
, shm_buffer
->shm
->handle(), size
);
272 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
273 next_bitstream_buffer_id_
= (next_bitstream_buffer_id_
+ 1) & 0x3FFFFFFF;
274 bool inserted
= bitstream_buffers_in_decoder_
.insert(std::make_pair(
275 bitstream_buffer
.id(), BufferPair(shm_buffer
, buffer
))).second
;
277 RecordBufferData(bitstream_buffer
, *buffer
.get());
279 vda_
->Decode(bitstream_buffer
);
281 if (!ready_video_frames_
.empty()) {
282 EnqueueFrameAndTriggerFrameDelivery(NULL
);
286 if (CanMoreDecodeWorkBeDone())
287 base::ResetAndReturn(&pending_decode_cb_
).Run(kNotEnoughData
, NULL
);
290 bool GpuVideoDecoder::CanMoreDecodeWorkBeDone() {
291 return bitstream_buffers_in_decoder_
.size() < kMaxInFlightDecodes
;
294 void GpuVideoDecoder::RecordBufferData(const BitstreamBuffer
& bitstream_buffer
,
295 const DecoderBuffer
& buffer
) {
296 input_buffer_data_
.push_front(BufferData(bitstream_buffer
.id(),
298 config_
.visible_rect(),
299 config_
.natural_size()));
300 // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but
301 // that's too small for some pathological B-frame test videos. The cost of
302 // using too-high a value is low (192 bits per extra slot).
303 static const size_t kMaxInputBufferDataSize
= 128;
304 // Pop from the back of the list, because that's the oldest and least likely
305 // to be useful in the future data.
306 if (input_buffer_data_
.size() > kMaxInputBufferDataSize
)
307 input_buffer_data_
.pop_back();
310 void GpuVideoDecoder::GetBufferData(int32 id
, base::TimeDelta
* timestamp
,
311 gfx::Rect
* visible_rect
,
312 gfx::Size
* natural_size
) {
313 for (std::list
<BufferData
>::const_iterator it
=
314 input_buffer_data_
.begin(); it
!= input_buffer_data_
.end();
316 if (it
->bitstream_buffer_id
!= id
)
318 *timestamp
= it
->timestamp
;
319 *visible_rect
= it
->visible_rect
;
320 *natural_size
= it
->natural_size
;
323 NOTREACHED() << "Missing bitstreambuffer id: " << id
;
326 bool GpuVideoDecoder::HasAlpha() const {
327 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
331 bool GpuVideoDecoder::NeedsBitstreamConversion() const {
332 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
333 return needs_bitstream_conversion_
;
336 bool GpuVideoDecoder::CanReadWithoutStalling() const {
337 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
338 return available_pictures_
> 0 || !ready_video_frames_
.empty();
341 void GpuVideoDecoder::NotifyInitializeDone() {
342 NOTREACHED() << "GpuVideoDecodeAcceleratorHost::Initialize is synchronous!";
345 void GpuVideoDecoder::ProvidePictureBuffers(uint32 count
,
346 const gfx::Size
& size
,
347 uint32 texture_target
) {
348 DVLOG(3) << "ProvidePictureBuffers(" << count
<< ", "
349 << size
.width() << "x" << size
.height() << ")";
350 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
352 std::vector
<uint32
> texture_ids
;
353 std::vector
<gpu::Mailbox
> texture_mailboxes
;
354 decoder_texture_target_
= texture_target
;
355 // Discards the sync point returned here since PictureReady will imply that
356 // the produce has already happened, and the texture is ready for use.
357 if (!factories_
->CreateTextures(count
,
361 decoder_texture_target_
)) {
362 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
365 DCHECK_EQ(count
, texture_ids
.size());
366 DCHECK_EQ(count
, texture_mailboxes
.size());
371 std::vector
<PictureBuffer
> picture_buffers
;
372 for (size_t i
= 0; i
< texture_ids
.size(); ++i
) {
373 picture_buffers
.push_back(PictureBuffer(
374 next_picture_buffer_id_
++, size
, texture_ids
[i
], texture_mailboxes
[i
]));
375 bool inserted
= assigned_picture_buffers_
.insert(std::make_pair(
376 picture_buffers
.back().id(), picture_buffers
.back())).second
;
380 available_pictures_
+= count
;
382 vda_
->AssignPictureBuffers(picture_buffers
);
385 void GpuVideoDecoder::DismissPictureBuffer(int32 id
) {
386 DVLOG(3) << "DismissPictureBuffer(" << id
<< ")";
387 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
389 std::map
<int32
, PictureBuffer
>::iterator it
=
390 assigned_picture_buffers_
.find(id
);
391 if (it
== assigned_picture_buffers_
.end()) {
392 NOTREACHED() << "Missing picture buffer: " << id
;
396 PictureBuffer buffer_to_dismiss
= it
->second
;
397 assigned_picture_buffers_
.erase(it
);
399 std::set
<int32
>::iterator at_display_it
=
400 picture_buffers_at_display_
.find(id
);
402 if (at_display_it
== picture_buffers_at_display_
.end()) {
403 // We can delete the texture immediately as it's not being displayed.
404 factories_
->DeleteTexture(buffer_to_dismiss
.texture_id());
405 CHECK_GT(available_pictures_
, 0);
406 --available_pictures_
;
408 // Texture in display. Postpone deletion until after it's returned to us.
409 bool inserted
= dismissed_picture_buffers_
.insert(std::make_pair(
410 id
, buffer_to_dismiss
)).second
;
415 void GpuVideoDecoder::PictureReady(const media::Picture
& picture
) {
416 DVLOG(3) << "PictureReady()";
417 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
419 std::map
<int32
, PictureBuffer
>::iterator it
=
420 assigned_picture_buffers_
.find(picture
.picture_buffer_id());
421 if (it
== assigned_picture_buffers_
.end()) {
422 NOTREACHED() << "Missing picture buffer: " << picture
.picture_buffer_id();
423 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
426 const PictureBuffer
& pb
= it
->second
;
428 // Update frame's timestamp.
429 base::TimeDelta timestamp
;
430 gfx::Rect visible_rect
;
431 gfx::Size natural_size
;
432 GetBufferData(picture
.bitstream_buffer_id(), ×tamp
, &visible_rect
,
434 DCHECK(decoder_texture_target_
);
436 scoped_refptr
<VideoFrame
> frame(VideoFrame::WrapNativeTexture(
437 new VideoFrame::MailboxHolder(
438 pb
.texture_mailbox(),
440 BindToCurrentLoop(base::Bind(&GpuVideoDecoder::ReusePictureBuffer
,
442 picture
.picture_buffer_id()))),
443 decoder_texture_target_
,
448 base::Bind(&GpuVideoAcceleratorFactories::ReadPixels
,
451 decoder_texture_target_
,
452 gfx::Size(visible_rect
.width(), visible_rect
.height())),
454 CHECK_GT(available_pictures_
, 0);
455 --available_pictures_
;
457 picture_buffers_at_display_
.insert(picture
.picture_buffer_id()).second
;
460 EnqueueFrameAndTriggerFrameDelivery(frame
);
463 void GpuVideoDecoder::EnqueueFrameAndTriggerFrameDelivery(
464 const scoped_refptr
<VideoFrame
>& frame
) {
465 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
467 // During a pending vda->Reset(), we don't accumulate frames. Drop it on the
469 if (!pending_reset_cb_
.is_null())
473 ready_video_frames_
.push_back(frame
);
475 DCHECK(!ready_video_frames_
.empty());
477 if (pending_decode_cb_
.is_null())
480 base::ResetAndReturn(&pending_decode_cb_
)
481 .Run(kOk
, ready_video_frames_
.front());
482 ready_video_frames_
.pop_front();
485 void GpuVideoDecoder::ReusePictureBuffer(int64 picture_buffer_id
,
487 DVLOG(3) << "ReusePictureBuffer(" << picture_buffer_id
<< ")";
488 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
493 CHECK(!picture_buffers_at_display_
.empty());
495 size_t num_erased
= picture_buffers_at_display_
.erase(picture_buffer_id
);
498 std::map
<int32
, PictureBuffer
>::iterator it
=
499 assigned_picture_buffers_
.find(picture_buffer_id
);
501 if (it
== assigned_picture_buffers_
.end()) {
502 // This picture was dismissed while in display, so we postponed deletion.
503 it
= dismissed_picture_buffers_
.find(picture_buffer_id
);
504 DCHECK(it
!= dismissed_picture_buffers_
.end());
505 factories_
->DeleteTexture(it
->second
.texture_id());
506 dismissed_picture_buffers_
.erase(it
);
510 factories_
->WaitSyncPoint(sync_point
);
511 ++available_pictures_
;
513 vda_
->ReusePictureBuffer(picture_buffer_id
);
516 GpuVideoDecoder::SHMBuffer
* GpuVideoDecoder::GetSHM(size_t min_size
) {
517 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
518 if (available_shm_segments_
.empty() ||
519 available_shm_segments_
.back()->size
< min_size
) {
520 size_t size_to_allocate
= std::max(min_size
, kSharedMemorySegmentBytes
);
521 base::SharedMemory
* shm
= factories_
->CreateSharedMemory(size_to_allocate
);
522 // CreateSharedMemory() can return NULL during Shutdown.
525 return new SHMBuffer(shm
, size_to_allocate
);
527 SHMBuffer
* ret
= available_shm_segments_
.back();
528 available_shm_segments_
.pop_back();
532 void GpuVideoDecoder::PutSHM(SHMBuffer
* shm_buffer
) {
533 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
534 available_shm_segments_
.push_back(shm_buffer
);
537 void GpuVideoDecoder::NotifyEndOfBitstreamBuffer(int32 id
) {
538 DVLOG(3) << "NotifyEndOfBitstreamBuffer(" << id
<< ")";
539 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
541 std::map
<int32
, BufferPair
>::iterator it
=
542 bitstream_buffers_in_decoder_
.find(id
);
543 if (it
== bitstream_buffers_in_decoder_
.end()) {
544 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE
);
545 NOTREACHED() << "Missing bitstream buffer: " << id
;
549 PutSHM(it
->second
.shm_buffer
);
550 bitstream_buffers_in_decoder_
.erase(it
);
552 if (pending_reset_cb_
.is_null() && state_
!= kDrainingDecoder
&&
553 CanMoreDecodeWorkBeDone() && !pending_decode_cb_
.is_null()) {
554 base::ResetAndReturn(&pending_decode_cb_
).Run(kNotEnoughData
, NULL
);
558 GpuVideoDecoder::~GpuVideoDecoder() {
559 DCHECK(!vda_
.get()); // Stop should have been already called.
560 DCHECK(pending_decode_cb_
.is_null());
561 for (size_t i
= 0; i
< available_shm_segments_
.size(); ++i
) {
562 available_shm_segments_
[i
]->shm
->Close();
563 delete available_shm_segments_
[i
];
565 available_shm_segments_
.clear();
566 for (std::map
<int32
, BufferPair
>::iterator it
=
567 bitstream_buffers_in_decoder_
.begin();
568 it
!= bitstream_buffers_in_decoder_
.end(); ++it
) {
569 it
->second
.shm_buffer
->shm
->Close();
571 bitstream_buffers_in_decoder_
.clear();
576 void GpuVideoDecoder::NotifyFlushDone() {
577 DVLOG(3) << "NotifyFlushDone()";
578 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
579 DCHECK_EQ(state_
, kDrainingDecoder
);
580 state_
= kDecoderDrained
;
581 EnqueueFrameAndTriggerFrameDelivery(VideoFrame::CreateEmptyFrame());
584 void GpuVideoDecoder::NotifyResetDone() {
585 DVLOG(3) << "NotifyResetDone()";
586 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
587 DCHECK(ready_video_frames_
.empty());
589 // This needs to happen after the Reset() on vda_ is done to ensure pictures
590 // delivered during the reset can find their time data.
591 input_buffer_data_
.clear();
593 if (!pending_reset_cb_
.is_null())
594 base::ResetAndReturn(&pending_reset_cb_
).Run();
596 if (!pending_decode_cb_
.is_null())
597 EnqueueFrameAndTriggerFrameDelivery(VideoFrame::CreateEmptyFrame());
600 void GpuVideoDecoder::NotifyError(media::VideoDecodeAccelerator::Error error
) {
601 DCHECK(gvd_loop_proxy_
->BelongsToCurrentThread());
605 DLOG(ERROR
) << "VDA Error: " << error
;
610 if (!pending_decode_cb_
.is_null()) {
611 base::ResetAndReturn(&pending_decode_cb_
).Run(kDecodeError
, NULL
);