1 // Copyright (c) 2014 The Intel Corporation. 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 "ozone/media/vaapi_video_decode_accelerator.h"
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "content/common/gpu/gpu_channel.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/video/picture.h"
20 #include "ui/gl/gl_bindings.h"
21 #include "ui/gl/gl_surface_egl.h"
22 #include "ui/gl/scoped_binders.h"
24 static void ReportToUMA(
25 media::VaapiH264Decoder::VAVDAH264DecoderFailure failure) {
26 UMA_HISTOGRAM_ENUMERATION(
27 "Media.VAVDAH264.DecoderFailure",
29 media::VaapiH264Decoder::VAVDA_H264_DECODER_FAILURES_MAX);
34 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret) \
38 NotifyError(error_code); \
43 VaapiVideoDecodeAccelerator::InputBuffer::InputBuffer() : id(0), size(0) {
46 VaapiVideoDecodeAccelerator::InputBuffer::~InputBuffer() {
49 void VaapiVideoDecodeAccelerator::NotifyError(Error error) {
50 if (message_loop_ != base::MessageLoop::current()) {
51 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
52 message_loop_->PostTask(FROM_HERE, base::Bind(
53 &VaapiVideoDecodeAccelerator::NotifyError, weak_this_, error));
57 // Post Cleanup() as a task so we don't recursively acquire lock_.
58 message_loop_->PostTask(FROM_HERE, base::Bind(
59 &VaapiVideoDecodeAccelerator::Cleanup, weak_this_));
61 DVLOG(1) << "Notifying of error " << error;
63 client_->NotifyError(error);
64 client_ptr_factory_.reset();
68 // TFPPicture allocates VAImage and binds them to textures passed
69 // in PictureBuffers from clients to them. TFPPictures are created as
70 // a consequence of receiving a set of PictureBuffers from clients and released
71 // at the end of decode (or when a new set of PictureBuffers is required).
73 // TFPPictures are used for output, contents of VASurfaces passed from decoder
74 // are put into the associated vaimage memory and upload to client.
75 class VaapiVideoDecodeAccelerator::TFPPicture : public base::NonThreadSafe {
79 static linked_ptr<TFPPicture> Create(
80 const base::Callback<bool(void)>& make_context_current, //NOLINT
81 VaapiWrapper* va_wrapper,
82 int32 picture_buffer_id,
86 int32 picture_buffer_id() {
87 return picture_buffer_id_;
95 return va_image_.get();
98 // Upload vaimage data to texture. Needs to be called every frame.
99 bool Upload(VASurfaceID id);
101 // Bind EGL image to texture. Needs to be called every frame.
103 bool UpdateEGLImage(VASurfaceID id);
106 TFPPicture(const base::Callback<bool(void)>& make_context_current, //NOLINT
107 VaapiWrapper* va_wrapper,
108 int32 picture_buffer_id,
114 EGLImageKHR CreateEGLImage(
115 EGLDisplay egl_display, VASurfaceID surface, VAImage* va_image);
116 bool DestroyEGLImage(EGLDisplay egl_display, EGLImageKHR egl_image);
118 base::Callback<bool(void)> make_context_current_; //NOLINT
120 VaapiWrapper* va_wrapper_;
122 // Output id for the client.
123 int32 picture_buffer_id_;
127 scoped_ptr<VAImage> va_image_;
128 EGLImageKHR egl_image_;
129 EGLDisplay egl_display_;
131 DISALLOW_COPY_AND_ASSIGN(TFPPicture);
134 VaapiVideoDecodeAccelerator::TFPPicture::TFPPicture(
135 const base::Callback<bool(void)>& make_context_current, //NOLINT
136 VaapiWrapper* va_wrapper,
137 int32 picture_buffer_id,
140 : make_context_current_(make_context_current),
141 va_wrapper_(va_wrapper),
142 picture_buffer_id_(picture_buffer_id),
143 texture_id_(texture_id),
145 va_image_(new VAImage()),
146 egl_display_(gfx::GLSurfaceEGL::GetHardwareDisplay()) {
147 DCHECK(!make_context_current_.is_null());
151 linked_ptr<VaapiVideoDecodeAccelerator::TFPPicture>
152 VaapiVideoDecodeAccelerator::TFPPicture::Create(
153 const base::Callback<bool(void)>& make_context_current, //NOLINT
154 VaapiWrapper* va_wrapper,
155 int32 picture_buffer_id,
158 linked_ptr<TFPPicture> tfp_picture(
159 new TFPPicture(make_context_current, va_wrapper,
160 picture_buffer_id, texture_id, size));
162 if (!tfp_picture->Initialize())
168 bool VaapiVideoDecodeAccelerator::TFPPicture::Initialize() {
169 DCHECK(CalledOnValidThread());
170 if (!make_context_current_.Run())
173 if (!va_wrapper_->CreateRGBImage(size_, va_image_.get())) {
174 DVLOG(1) << "Failed to create VAImage";
181 VaapiVideoDecodeAccelerator::TFPPicture::~TFPPicture() {
182 DCHECK(CalledOnValidThread());
184 if (egl_image_ != EGL_NO_IMAGE_KHR)
185 DestroyEGLImage(egl_display_, egl_image_);
188 va_wrapper_->DestroyImage(va_image_.get());
192 bool VaapiVideoDecodeAccelerator::TFPPicture::UpdateEGLImage(
193 VASurfaceID surface) {
194 DCHECK(CalledOnValidThread());
196 if (!make_context_current_.Run())
199 if (egl_image_ != EGL_NO_IMAGE_KHR)
200 DestroyEGLImage(egl_display_, egl_image_);
202 egl_image_ = CreateEGLImage(egl_display_, surface, va_image_.get());
203 if (egl_image_ == EGL_NO_IMAGE_KHR) {
204 DVLOG(1) << "Failed to create EGL image";
211 bool VaapiVideoDecodeAccelerator::TFPPicture::Upload(VASurfaceID surface) {
212 DCHECK(CalledOnValidThread());
214 if (!make_context_current_.Run())
217 if (!va_wrapper_->PutSurfaceIntoImage(surface, va_image_.get())) {
218 DVLOG(1) << "Failed to put va surface to image";
223 if (!va_wrapper_->MapImage(va_image_.get(), &buffer)) {
224 DVLOG(1) << "Failed to map VAImage";
228 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id_);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 // See bug https://crosswalk-project.org/jira/browse/XWALK-2265.
233 // The following small piece of code is a workaround for the current VDA
234 // texture output implementation. It can be removed when zero buffer copy
236 unsigned int al = 4 * size_.width();
237 if (al != va_image_->pitches[0]) {
238 // Not aligned phenomenon occurs only in special size video in None-X11.
239 // So re-check RGBA data alignment and realign filled video frame in need.
240 unsigned char* bhandle = static_cast<unsigned char*>(buffer);
241 for (int i = 0; i < size_.height(); i++) {
242 memcpy(bhandle + (i * al), bhandle + (i * (va_image_->pitches[0])), al);
246 glTexImage2D(GL_TEXTURE_2D,
256 va_wrapper_->UnmapImage(va_image_.get());
261 bool VaapiVideoDecodeAccelerator::TFPPicture::Bind() {
262 DCHECK(CalledOnValidThread());
263 if (!make_context_current_.Run())
266 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id_);
267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
269 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image_);
273 EGLImageKHR VaapiVideoDecodeAccelerator::TFPPicture::CreateEGLImage(
274 EGLDisplay egl_display, VASurfaceID va_surface, VAImage* va_image) {
275 DCHECK(CalledOnValidThread());
278 VABufferInfo buffer_info;
279 if (!va_wrapper_->LockBuffer(va_surface, va_image->buf, &buffer_info)) {
280 DVLOG(1) << "Failed to lock Buffer";
281 return EGL_NO_IMAGE_KHR;
287 EGL_DRM_BUFFER_STRIDE_MESA, 0,
288 EGL_DRM_BUFFER_FORMAT_MESA,
289 EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
290 EGL_DRM_BUFFER_USE_MESA,
291 EGL_DRM_BUFFER_USE_SHARE_MESA,
293 attribs[1] = va_image->width;
294 attribs[3] = va_image->height;
295 attribs[5] = va_image->pitches[0] / 4;
297 EGLImageKHR egl_image = eglCreateImageKHR(
301 (EGLClientBuffer) buffer_info.handle,
305 va_wrapper_->UnlockBuffer(va_surface, va_image->buf, &buffer_info);
311 bool VaapiVideoDecodeAccelerator::TFPPicture::DestroyEGLImage(
312 EGLDisplay egl_display, EGLImageKHR egl_image) {
313 return eglDestroyImageKHR(egl_display, egl_image);
316 VaapiVideoDecodeAccelerator::TFPPicture*
317 VaapiVideoDecodeAccelerator::TFPPictureById(int32 picture_buffer_id) {
318 TFPPictures::iterator it = tfp_pictures_.find(picture_buffer_id);
319 if (it == tfp_pictures_.end()) {
320 DVLOG(1) << "Picture id " << picture_buffer_id << " does not exist";
324 return it->second.get();
327 VaapiVideoDecodeAccelerator::VaapiVideoDecodeAccelerator(
328 const base::Callback<bool(void)>& make_context_current) //NOLINT
329 : make_context_current_(make_context_current),
330 state_(kUninitialized),
331 input_ready_(&lock_),
332 surfaces_available_(&lock_),
333 message_loop_(base::MessageLoop::current()),
334 decoder_thread_("VaapiDecoderThread"),
335 num_frames_at_client_(0),
336 num_stream_bufs_at_decoder_(0),
337 finish_flush_pending_(false),
338 awaiting_va_surfaces_recycle_(false),
339 requested_num_pics_(0),
340 weak_this_factory_(this) {
341 weak_this_ = weak_this_factory_.GetWeakPtr();
342 va_surface_release_cb_ = media::BindToCurrentLoop(base::Bind(
343 &VaapiVideoDecodeAccelerator::RecycleVASurfaceID, weak_this_));
346 VaapiVideoDecodeAccelerator::~VaapiVideoDecodeAccelerator() {
347 DCHECK_EQ(message_loop_, base::MessageLoop::current());
350 bool VaapiVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
352 DCHECK_EQ(message_loop_, base::MessageLoop::current());
354 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
355 client_ = client_ptr_factory_->GetWeakPtr();
357 base::AutoLock auto_lock(lock_);
358 DCHECK_EQ(state_, kUninitialized);
359 DVLOG(2) << "Initializing VAVDA, profile: " << profile;
361 if (!make_context_current_.Run())
364 vaapi_wrapper_ = VaapiWrapper::Create(
366 gfx::GLSurfaceEGL::GetNativeDisplay(),
367 base::Bind(&ReportToUMA, VaapiH264Decoder::VAAPI_ERROR));
369 if (!vaapi_wrapper_.get()) {
370 DVLOG(1) << "Failed initializing VAAPI";
374 supports_valockBuffer_apis_ = vaapi_wrapper_->SupportsVaLockBufferApis();
376 supports_valockBuffer_apis_ ? "supports" : "doesn't support";
377 LOG(INFO) << "VAAPI " << query << " vaLockBuffer apis";
380 new VaapiH264Decoder(
381 vaapi_wrapper_.get(),
382 media::BindToCurrentLoop(base::Bind(
383 &VaapiVideoDecodeAccelerator::SurfaceReady, weak_this_)),
384 base::Bind(&ReportToUMA)));
386 CHECK(decoder_thread_.Start());
387 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy();
394 void VaapiVideoDecodeAccelerator::SurfaceReady(
396 const scoped_refptr<VASurface>& va_surface) {
397 DCHECK_EQ(message_loop_, base::MessageLoop::current());
398 DCHECK(!awaiting_va_surfaces_recycle_);
400 // Drop any requests to output if we are resetting or being destroyed.
401 if (state_ == kResetting || state_ == kDestroying)
404 pending_output_cbs_.push(
405 base::Bind(&VaapiVideoDecodeAccelerator::OutputPicture,
406 weak_this_, va_surface, input_id));
411 void VaapiVideoDecodeAccelerator::OutputPicture(
412 const scoped_refptr<VASurface>& va_surface,
414 TFPPicture* tfp_picture) {
415 DCHECK_EQ(message_loop_, base::MessageLoop::current());
417 int32 output_id = tfp_picture->picture_buffer_id();
419 TRACE_EVENT2("Video Decoder", "VAVDA::OutputSurface",
420 "input_id", input_id,
421 "output_id", output_id);
423 DVLOG(3) << "Outputting VASurface " << va_surface->id()
424 << " into texture bound to picture buffer id " << output_id;
426 if (supports_valockBuffer_apis_) {
427 RETURN_AND_NOTIFY_ON_FAILURE(
428 vaapi_wrapper_->PutSurfaceIntoImage(
430 tfp_picture->va_image()),
431 "Failed putting surface into vaimage",
432 PLATFORM_FAILURE, ); //NOLINT
434 RETURN_AND_NOTIFY_ON_FAILURE(
435 tfp_picture->UpdateEGLImage(va_surface->id()),
436 "Failed to update egl image per vaimage info",
437 PLATFORM_FAILURE, ); //NOLINT
439 RETURN_AND_NOTIFY_ON_FAILURE(
441 "Failed to bind egl image to texture",
442 PLATFORM_FAILURE, ); //NOLINT
444 RETURN_AND_NOTIFY_ON_FAILURE(
445 tfp_picture->Upload(va_surface->id()),
446 "Failed to upload VASurface to texture",
447 PLATFORM_FAILURE, ); //NOLINT
450 // Notify the client a picture is ready to be displayed.
451 ++num_frames_at_client_;
452 TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
453 DVLOG(4) << "Notifying output picture id " << output_id
454 << " for input "<< input_id << " is ready";
455 // TODO(posciak): Use visible size from decoder here instead
456 // (crbug.com/402760).
458 client_->PictureReady(
459 media::Picture(output_id, input_id, gfx::Rect(tfp_picture->size())));
462 void VaapiVideoDecodeAccelerator::TryOutputSurface() {
463 DCHECK_EQ(message_loop_, base::MessageLoop::current());
465 // Handle Destroy() arriving while pictures are queued for output.
469 if (pending_output_cbs_.empty() || output_buffers_.empty())
472 OutputCB output_cb = pending_output_cbs_.front();
473 pending_output_cbs_.pop();
475 TFPPicture* tfp_picture = TFPPictureById(output_buffers_.front());
477 output_buffers_.pop();
479 output_cb.Run(tfp_picture);
481 if (finish_flush_pending_ && pending_output_cbs_.empty())
485 void VaapiVideoDecodeAccelerator::MapAndQueueNewInputBuffer(
486 const media::BitstreamBuffer& bitstream_buffer) {
487 DCHECK_EQ(message_loop_, base::MessageLoop::current());
488 TRACE_EVENT1("Video Decoder", "MapAndQueueNewInputBuffer", "input_id",
489 bitstream_buffer.id());
491 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id()
492 << " size: " << static_cast<int>(bitstream_buffer.size());
494 scoped_ptr<base::SharedMemory> shm(
495 new base::SharedMemory(bitstream_buffer.handle(), true));
496 RETURN_AND_NOTIFY_ON_FAILURE(shm->Map(bitstream_buffer.size()),
497 "Failed to map input buffer", UNREADABLE_INPUT,); //NOLINT
499 base::AutoLock auto_lock(lock_);
501 // Set up a new input buffer and queue it for later.
502 linked_ptr<InputBuffer> input_buffer(new InputBuffer());
503 input_buffer->shm.reset(shm.release());
504 input_buffer->id = bitstream_buffer.id();
505 input_buffer->size = bitstream_buffer.size();
507 ++num_stream_bufs_at_decoder_;
508 TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
509 num_stream_bufs_at_decoder_);
511 input_buffers_.push(input_buffer);
512 input_ready_.Signal();
515 bool VaapiVideoDecodeAccelerator::GetInputBuffer_Locked() {
516 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
517 lock_.AssertAcquired();
519 if (curr_input_buffer_.get())
522 // Will only wait if it is expected that in current state new buffers will
523 // be queued from the client via Decode(). The state can change during wait.
524 while (input_buffers_.empty() && (state_ == kDecoding || state_ == kIdle)) {
528 // We could have got woken up in a different state or never got to sleep
529 // due to current state; check for that.
532 // Here we are only interested in finishing up decoding buffers that are
533 // already queued up. Otherwise will stop decoding.
534 if (input_buffers_.empty())
539 DCHECK(!input_buffers_.empty());
541 curr_input_buffer_ = input_buffers_.front();
542 input_buffers_.pop();
544 DVLOG(4) << "New current bitstream buffer, id: "
545 << curr_input_buffer_->id
546 << " size: " << curr_input_buffer_->size;
549 static_cast<uint8*>(curr_input_buffer_->shm->memory()),
550 curr_input_buffer_->size, curr_input_buffer_->id);
554 // We got woken up due to being destroyed/reset, ignore any already
560 void VaapiVideoDecodeAccelerator::ReturnCurrInputBuffer_Locked() {
561 lock_.AssertAcquired();
562 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
563 DCHECK(curr_input_buffer_.get());
565 int32 id = curr_input_buffer_->id;
566 curr_input_buffer_.reset();
567 DVLOG(4) << "End of input buffer " << id;
568 message_loop_->PostTask(FROM_HERE, base::Bind(
569 &Client::NotifyEndOfBitstreamBuffer, client_, id));
571 --num_stream_bufs_at_decoder_;
572 TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
573 num_stream_bufs_at_decoder_);
576 bool VaapiVideoDecodeAccelerator::FeedDecoderWithOutputSurfaces_Locked() {
577 lock_.AssertAcquired();
578 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
580 while (available_va_surfaces_.empty() &&
581 (state_ == kDecoding || state_ == kFlushing || state_ == kIdle)) {
582 surfaces_available_.Wait();
585 if (state_ != kDecoding && state_ != kFlushing && state_ != kIdle)
588 while (!available_va_surfaces_.empty()) {
589 scoped_refptr<VASurface> va_surface(
590 new VASurface(available_va_surfaces_.front(), va_surface_release_cb_));
591 available_va_surfaces_.pop_front();
592 decoder_->ReuseSurface(va_surface);
598 void VaapiVideoDecodeAccelerator::DecodeTask() {
599 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
600 TRACE_EVENT0("Video Decoder", "VAVDA::DecodeTask");
601 base::AutoLock auto_lock(lock_);
603 if (state_ != kDecoding)
607 DVLOG(4) << "Decode task";
609 // Try to decode what stream data is (still) in the decoder until we run out
611 while (GetInputBuffer_Locked()) {
612 DCHECK(curr_input_buffer_.get());
614 VaapiH264Decoder::DecResult res;
616 // We are OK releasing the lock here, as decoder never calls our methods
617 // directly and we will reacquire the lock before looking at state again.
618 // This is the main decode function of the decoder and while keeping
619 // the lock for its duration would be fine, it would defeat the purpose
620 // of having a separate decoder thread.
621 base::AutoUnlock auto_unlock(lock_);
622 res = decoder_->Decode();
626 case VaapiH264Decoder::kAllocateNewSurfaces:
627 DVLOG(1) << "Decoder requesting a new set of surfaces";
628 message_loop_->PostTask(FROM_HERE, base::Bind(
629 &VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange, weak_this_,
630 decoder_->GetRequiredNumOfPictures(),
631 decoder_->GetPicSize()));
632 // We'll get rescheduled once ProvidePictureBuffers() finishes.
635 case VaapiH264Decoder::kRanOutOfStreamData:
636 ReturnCurrInputBuffer_Locked();
639 case VaapiH264Decoder::kRanOutOfSurfaces:
640 // No more output buffers in the decoder, try getting more or go to
641 // sleep waiting for them.
642 if (!FeedDecoderWithOutputSurfaces_Locked())
647 case VaapiH264Decoder::kDecodeError:
648 RETURN_AND_NOTIFY_ON_FAILURE(false, "Error decoding stream",
649 PLATFORM_FAILURE,); //NOLINT
655 void VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange(size_t num_pics,
657 DCHECK_EQ(message_loop_, base::MessageLoop::current());
658 DCHECK(!awaiting_va_surfaces_recycle_);
660 // At this point decoder has stopped running and has already posted onto our
661 // loop any remaining output request callbacks, which executed before we got
662 // here. Some of them might have been pended though, because we might not
663 // have had enough TFPictures to output surfaces to. Initiate a wait cycle,
664 // which will wait for client to return enough PictureBuffers to us, so that
665 // we can finish all pending output callbacks, releasing associated surfaces.
666 DVLOG(1) << "Initiating surface set change";
667 awaiting_va_surfaces_recycle_ = true;
669 requested_num_pics_ = num_pics;
670 requested_pic_size_ = size;
672 TryFinishSurfaceSetChange();
675 void VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange() {
676 DCHECK_EQ(message_loop_, base::MessageLoop::current());
678 if (!awaiting_va_surfaces_recycle_)
681 if (!pending_output_cbs_.empty() ||
682 tfp_pictures_.size() != available_va_surfaces_.size()) {
684 // 1. Not all pending pending output callbacks have been executed yet.
685 // Wait for the client to return enough pictures and retry later.
686 // 2. The above happened and all surface release callbacks have been posted
687 // as the result, but not all have executed yet. Post ourselves after them
688 // to let them release surfaces.
689 DVLOG(2) << "Awaiting pending output/surface release callbacks to finish";
690 message_loop_->PostTask(FROM_HERE, base::Bind(
691 &VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange, weak_this_));
695 // All surfaces released, destroy them and dismiss all PictureBuffers.
696 awaiting_va_surfaces_recycle_ = false;
697 available_va_surfaces_.clear();
698 vaapi_wrapper_->DestroySurfaces();
700 for (TFPPictures::iterator iter = tfp_pictures_.begin();
701 iter != tfp_pictures_.end(); ++iter) {
702 DVLOG(2) << "Dismissing picture id: " << iter->first;
704 client_->DismissPictureBuffer(iter->first);
706 tfp_pictures_.clear();
708 // And ask for a new set as requested.
709 DVLOG(1) << "Requesting " << requested_num_pics_ << " pictures of size: "
710 << requested_pic_size_.ToString();
712 message_loop_->PostTask(FROM_HERE, base::Bind(
713 &Client::ProvidePictureBuffers, client_,
714 requested_num_pics_, requested_pic_size_, GL_TEXTURE_2D));
717 void VaapiVideoDecodeAccelerator::Decode(
718 const media::BitstreamBuffer& bitstream_buffer) {
719 DCHECK_EQ(message_loop_, base::MessageLoop::current());
721 TRACE_EVENT1("Video Decoder", "VAVDA::Decode", "Buffer id",
722 bitstream_buffer.id());
724 // We got a new input buffer from the client, map it and queue for later use.
725 MapAndQueueNewInputBuffer(bitstream_buffer);
727 base::AutoLock auto_lock(lock_);
731 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
732 &VaapiVideoDecodeAccelerator::DecodeTask,
733 base::Unretained(this)));
737 // Decoder already running, fallthrough.
739 // When resetting, allow accumulating bitstream buffers, so that
740 // the client can queue after-seek-buffers while we are finishing with
741 // the before-seek one.
745 RETURN_AND_NOTIFY_ON_FAILURE(false,
746 "Decode request from client in invalid state: " << state_,
747 PLATFORM_FAILURE,); //NOLINT
752 void VaapiVideoDecodeAccelerator::RecycleVASurfaceID(
753 VASurfaceID va_surface_id) {
754 DCHECK_EQ(message_loop_, base::MessageLoop::current());
755 base::AutoLock auto_lock(lock_);
757 available_va_surfaces_.push_back(va_surface_id);
758 surfaces_available_.Signal();
761 void VaapiVideoDecodeAccelerator::AssignPictureBuffers(
762 const std::vector<media::PictureBuffer>& buffers) {
763 DCHECK_EQ(message_loop_, base::MessageLoop::current());
765 base::AutoLock auto_lock(lock_);
766 DCHECK(tfp_pictures_.empty());
768 while (!output_buffers_.empty())
769 output_buffers_.pop();
771 RETURN_AND_NOTIFY_ON_FAILURE(
772 buffers.size() == requested_num_pics_,
773 "Got an invalid buffers. (Got " << buffers.size() << ", requested "
774 << requested_num_pics_ << ")", INVALID_ARGUMENT,); //NOLINT
775 DCHECK(requested_pic_size_ == buffers[0].size());
777 std::vector<VASurfaceID> va_surface_ids;
778 RETURN_AND_NOTIFY_ON_FAILURE(
779 vaapi_wrapper_->CreateSurfaces(requested_pic_size_,
782 "Failed creating VA Surfaces", PLATFORM_FAILURE,); //NOLINT
783 DCHECK_EQ(va_surface_ids.size(), buffers.size());
785 for (size_t i = 0; i < buffers.size(); ++i) {
786 DVLOG(2) << "Assigning picture id: " << buffers[i].id()
787 << " to texture id: " << buffers[i].texture_id()
788 << " VASurfaceID: " << va_surface_ids[i];
790 linked_ptr<TFPPicture> tfp_picture(
791 TFPPicture::Create(make_context_current_,
792 vaapi_wrapper_.get(),
794 buffers[i].texture_id(),
795 requested_pic_size_));
797 RETURN_AND_NOTIFY_ON_FAILURE(
798 tfp_picture.get(), "Failed assigning picture buffer to a texture.",
799 PLATFORM_FAILURE,); //NOLINT
801 bool inserted = tfp_pictures_.insert(std::make_pair(
802 buffers[i].id(), tfp_picture)).second;
805 output_buffers_.push(buffers[i].id());
806 available_va_surfaces_.push_back(va_surface_ids[i]);
807 surfaces_available_.Signal();
811 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
812 &VaapiVideoDecodeAccelerator::DecodeTask, base::Unretained(this)));
815 void VaapiVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) {
816 DCHECK_EQ(message_loop_, base::MessageLoop::current());
817 TRACE_EVENT1("Video Decoder", "VAVDA::ReusePictureBuffer", "Picture id",
820 --num_frames_at_client_;
821 TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
823 output_buffers_.push(picture_buffer_id);
827 void VaapiVideoDecodeAccelerator::FlushTask() {
828 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
829 DVLOG(1) << "Flush task";
831 // First flush all the pictures that haven't been outputted, notifying the
832 // client to output them.
833 bool res = decoder_->Flush();
834 RETURN_AND_NOTIFY_ON_FAILURE(res, "Failed flushing the decoder.",
835 PLATFORM_FAILURE,); //NOLINT
837 // Put the decoder in idle state, ready to resume.
840 message_loop_->PostTask(FROM_HERE, base::Bind(
841 &VaapiVideoDecodeAccelerator::FinishFlush, weak_this_));
844 void VaapiVideoDecodeAccelerator::Flush() {
845 DCHECK_EQ(message_loop_, base::MessageLoop::current());
846 DVLOG(1) << "Got flush request";
848 base::AutoLock auto_lock(lock_);
850 // Queue a flush task after all existing decoding tasks to clean up.
851 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
852 &VaapiVideoDecodeAccelerator::FlushTask, base::Unretained(this)));
854 input_ready_.Signal();
855 surfaces_available_.Signal();
858 void VaapiVideoDecodeAccelerator::FinishFlush() {
859 DCHECK_EQ(message_loop_, base::MessageLoop::current());
861 finish_flush_pending_ = false;
863 base::AutoLock auto_lock(lock_);
864 if (state_ != kFlushing) {
865 DCHECK_EQ(state_, kDestroying);
866 return; // We could've gotten destroyed already.
869 // Still waiting for textures from client to finish outputting all pending
870 // frames. Try again later.
871 if (!pending_output_cbs_.empty()) {
872 finish_flush_pending_ = true;
878 message_loop_->PostTask(FROM_HERE, base::Bind(
879 &Client::NotifyFlushDone, client_));
881 DVLOG(1) << "Flush finished";
884 void VaapiVideoDecodeAccelerator::ResetTask() {
885 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
886 DVLOG(1) << "ResetTask";
888 // All the decoding tasks from before the reset request from client are done
889 // by now, as this task was scheduled after them and client is expected not
890 // to call Decode() after Reset() and before NotifyResetDone.
893 base::AutoLock auto_lock(lock_);
895 // Return current input buffer, if present.
896 if (curr_input_buffer_.get())
897 ReturnCurrInputBuffer_Locked();
899 // And let client know that we are done with reset.
900 message_loop_->PostTask(FROM_HERE, base::Bind(
901 &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
904 void VaapiVideoDecodeAccelerator::Reset() {
905 DCHECK_EQ(message_loop_, base::MessageLoop::current());
906 DVLOG(1) << "Got reset request";
908 // This will make any new decode tasks exit early.
909 base::AutoLock auto_lock(lock_);
911 finish_flush_pending_ = false;
913 // Drop all remaining input buffers, if present.
914 while (!input_buffers_.empty()) {
915 message_loop_->PostTask(FROM_HERE, base::Bind(
916 &Client::NotifyEndOfBitstreamBuffer, client_,
917 input_buffers_.front()->id));
918 input_buffers_.pop();
921 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
922 &VaapiVideoDecodeAccelerator::ResetTask, base::Unretained(this)));
924 input_ready_.Signal();
925 surfaces_available_.Signal();
928 void VaapiVideoDecodeAccelerator::FinishReset() {
929 DCHECK_EQ(message_loop_, base::MessageLoop::current());
930 DVLOG(1) << "FinishReset";
931 base::AutoLock auto_lock(lock_);
933 if (state_ != kResetting) {
934 DCHECK(state_ == kDestroying || state_ == kUninitialized) << state_;
935 return; // We could've gotten destroyed already.
938 // Drop pending outputs.
939 while (!pending_output_cbs_.empty())
940 pending_output_cbs_.pop();
942 if (awaiting_va_surfaces_recycle_) {
943 // Decoder requested a new surface set while we were waiting for it to
944 // finish the last DecodeTask, running at the time of Reset().
945 // Let the surface set change finish first before resetting.
946 message_loop_->PostTask(FROM_HERE, base::Bind(
947 &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
951 num_stream_bufs_at_decoder_ = 0;
954 message_loop_->PostTask(FROM_HERE, base::Bind(
955 &Client::NotifyResetDone, client_));
957 // The client might have given us new buffers via Decode() while we were
958 // resetting and might be waiting for our move, and not call Decode() anymore
959 // until we return something. Post a DecodeTask() so that we won't
960 // sleep forever waiting for Decode() in that case. Having two of them
961 // in the pipe is harmless, the additional one will return as soon as it sees
962 // that we are back in kDecoding state.
963 if (!input_buffers_.empty()) {
965 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
966 &VaapiVideoDecodeAccelerator::DecodeTask,
967 base::Unretained(this)));
970 DVLOG(1) << "Reset finished";
973 void VaapiVideoDecodeAccelerator::Cleanup() {
974 DCHECK_EQ(message_loop_, base::MessageLoop::current());
976 if (state_ == kUninitialized || state_ == kDestroying)
979 DVLOG(1) << "Destroying VAVDA";
980 base::AutoLock auto_lock(lock_);
981 state_ = kDestroying;
983 client_ptr_factory_.reset();
984 weak_this_factory_.InvalidateWeakPtrs();
987 base::AutoUnlock auto_unlock(lock_);
988 // Post a dummy task to the decoder_thread_ to ensure it is drained.
989 base::WaitableEvent waiter(false, false);
990 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
991 &base::WaitableEvent::Signal, base::Unretained(&waiter)));
992 input_ready_.Signal();
993 surfaces_available_.Signal();
995 decoder_thread_.Stop();
998 state_ = kUninitialized;
1001 void VaapiVideoDecodeAccelerator::Destroy() {
1002 DCHECK_EQ(message_loop_, base::MessageLoop::current());
1007 bool VaapiVideoDecodeAccelerator::CanDecodeOnIOThread() {
1011 } // namespace media