vp8_vaapi_video_decoder_delegate.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2018 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.
  4. #include "media/gpu/vaapi/vp8_vaapi_video_decoder_delegate.h"
  5. #include "base/callback_helpers.h"
  6. #include "base/trace_event/trace_event.h"
  7. #include "media/gpu/decode_surface_handler.h"
  8. #include "media/gpu/vaapi/va_surface.h"
  9. #include "media/gpu/vaapi/vaapi_common.h"
  10. #include "media/gpu/vaapi/vaapi_utils.h"
  11. #include "media/gpu/vaapi/vaapi_wrapper.h"
  12. namespace media {
  13. VP8VaapiVideoDecoderDelegate::VP8VaapiVideoDecoderDelegate(
  14. DecodeSurfaceHandler<VASurface>* const vaapi_dec,
  15. scoped_refptr<VaapiWrapper> vaapi_wrapper)
  16. : VaapiVideoDecoderDelegate(vaapi_dec,
  17. std::move(vaapi_wrapper),
  18. base::DoNothing(),
  19. nullptr) {}
  20. VP8VaapiVideoDecoderDelegate::~VP8VaapiVideoDecoderDelegate() {
  21. DCHECK(!iq_matrix_);
  22. DCHECK(!prob_buffer_);
  23. DCHECK(!picture_params_);
  24. DCHECK(!slice_params_);
  25. }
  26. scoped_refptr<VP8Picture> VP8VaapiVideoDecoderDelegate::CreateVP8Picture() {
  27. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  28. const auto va_surface = vaapi_dec_->CreateSurface();
  29. if (!va_surface)
  30. return nullptr;
  31. return new VaapiVP8Picture(std::move(va_surface));
  32. }
  33. bool VP8VaapiVideoDecoderDelegate::SubmitDecode(
  34. scoped_refptr<VP8Picture> pic,
  35. const Vp8ReferenceFrameVector& reference_frames) {
  36. TRACE_EVENT0("media,gpu", "VP8VaapiVideoDecoderDelegate::SubmitDecode");
  37. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  38. const auto va_surface_id = pic->AsVaapiVP8Picture()->va_surface()->id();
  39. DCHECK_NE(va_surface_id, VA_INVALID_SURFACE);
  40. VAIQMatrixBufferVP8 iq_matrix_buf{};
  41. VAProbabilityDataBufferVP8 prob_buf{};
  42. VAPictureParameterBufferVP8 pic_param{};
  43. VASliceParameterBufferVP8 slice_param{};
  44. const Vp8FrameHeader* const header = pic->frame_hdr.get();
  45. DCHECK(header);
  46. FillVP8DataStructures(*header, reference_frames, &iq_matrix_buf, &prob_buf,
  47. &pic_param, &slice_param);
  48. if (!iq_matrix_) {
  49. iq_matrix_ = vaapi_wrapper_->CreateVABuffer(VAIQMatrixBufferType,
  50. sizeof(iq_matrix_buf));
  51. if (!iq_matrix_)
  52. return false;
  53. }
  54. if (!prob_buffer_) {
  55. prob_buffer_ = vaapi_wrapper_->CreateVABuffer(VAProbabilityBufferType,
  56. sizeof(prob_buf));
  57. if (!prob_buffer_)
  58. return false;
  59. }
  60. if (!picture_params_) {
  61. picture_params_ = vaapi_wrapper_->CreateVABuffer(
  62. VAPictureParameterBufferType, sizeof(pic_param));
  63. if (!picture_params_)
  64. return false;
  65. }
  66. if (!slice_params_) {
  67. slice_params_ = vaapi_wrapper_->CreateVABuffer(VASliceParameterBufferType,
  68. sizeof(slice_param));
  69. if (!slice_params_)
  70. return false;
  71. }
  72. // Create VASliceData buffer |encoded_data| every frame so that decoding can
  73. // be more asynchronous than reusing the buffer.
  74. std::unique_ptr<ScopedVABuffer> encoded_data =
  75. vaapi_wrapper_->CreateVABuffer(VASliceDataBufferType, header->frame_size);
  76. if (!encoded_data)
  77. return false;
  78. return vaapi_wrapper_->MapAndCopyAndExecute(
  79. va_surface_id,
  80. {{iq_matrix_->id(),
  81. {iq_matrix_->type(), iq_matrix_->size(), &iq_matrix_buf}},
  82. {prob_buffer_->id(),
  83. {prob_buffer_->type(), prob_buffer_->size(), &prob_buf}},
  84. {picture_params_->id(),
  85. {picture_params_->type(), picture_params_->size(), &pic_param}},
  86. {slice_params_->id(),
  87. {slice_params_->type(), slice_params_->size(), &slice_param}},
  88. {encoded_data->id(),
  89. {encoded_data->type(), header->frame_size, header->data}}});
  90. }
  91. bool VP8VaapiVideoDecoderDelegate::OutputPicture(
  92. scoped_refptr<VP8Picture> pic) {
  93. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  94. const VaapiVP8Picture* vaapi_pic = pic->AsVaapiVP8Picture();
  95. vaapi_dec_->SurfaceReady(vaapi_pic->va_surface(), vaapi_pic->bitstream_id(),
  96. vaapi_pic->visible_rect(),
  97. vaapi_pic->get_colorspace());
  98. return true;
  99. }
  100. void VP8VaapiVideoDecoderDelegate::OnVAContextDestructionSoon() {
  101. // Destroy the member ScopedVABuffers below since they refer to a VAContextID
  102. // that will be destroyed soon.
  103. iq_matrix_.reset();
  104. prob_buffer_.reset();
  105. picture_params_.reset();
  106. slice_params_.reset();
  107. }
  108. } // namespace media