1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115

media / gpu / vaapi / av1_vaapi_video_encoder_delegate.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_GPU_VAAPI_AV1_VAAPI_VIDEO_ENCODER_DELEGATE_H_
#define MEDIA_GPU_VAAPI_AV1_VAAPI_VIDEO_ENCODER_DELEGATE_H_

#include <stdint.h>

#include <vector>

#include "media/base/video_bitrate_allocation.h"
#include "media/gpu/av1_builder.h"
#include "media/gpu/av1_picture.h"
#include "media/gpu/vaapi/vaapi_video_encoder_delegate.h"

namespace aom {
class AV1RateControlRTC;
}  // namespace aom

namespace media {
class SVCLayers;

class AV1VaapiVideoEncoderDelegate : public VaapiVideoEncoderDelegate {
 public:
  struct EncodeParams {
    EncodeParams();

    size_t intra_period = 0;  // Period between keyframes
    VideoBitrateAllocation bitrate_allocation;
    uint32_t framerate = 0;
    uint8_t min_qp = 0;
    uint8_t max_qp = 0;

    // The rate controller drop frame threshold. 0-100 as this is percentage.
    uint8_t drop_frame_thresh = 0;

    // The encoding content is a screen content.
    bool is_screen = false;

    // Sensible default values for CDEF taken from
    // https://github.com/intel/libva-utils/blob/master/encode/av1encode.c
    // TODO: we may want to tune these parameters.
    uint8_t cdef_y_pri_strength[8] = {9, 12, 0, 6, 2, 4, 1, 2};
    uint8_t cdef_y_sec_strength[8] = {0, 2, 0, 0, 0, 1, 0, 1};
    uint8_t cdef_uv_pri_strength[8] = {9, 12, 0, 6, 2, 4, 1, 2};
    uint8_t cdef_uv_sec_strength[8] = {0, 2, 0, 0, 0, 1, 0, 1};
  };

  AV1VaapiVideoEncoderDelegate(scoped_refptr<VaapiWrapper> vaapi_wrapper,
                               base::RepeatingClosure error_cb);

  AV1VaapiVideoEncoderDelegate(const AV1VaapiVideoEncoderDelegate&) = delete;
  AV1VaapiVideoEncoderDelegate& operator=(const AV1VaapiVideoEncoderDelegate&) =
      delete;

  ~AV1VaapiVideoEncoderDelegate() override;

  // VaapiVideoEncoderDelegate implementation
  bool Initialize(const VideoEncodeAccelerator::Config& config,
                  const VaapiVideoEncoderDelegate::Config& ave_config) override;
  bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
                   uint32_t framerate) override;
  gfx::Size GetCodedSize() const override;
  size_t GetMaxNumOfRefFrames() const override;
  std::vector<gfx::Size> GetSVCLayerResolutions() override;

 private:
  BitstreamBufferMetadata GetMetadata(const EncodeJob& encode_job,
                                      size_t payload_size) override;
  PrepareEncodeJobResult PrepareEncodeJob(EncodeJob& encode_job) override;
  void BitrateControlUpdate(const BitstreamBufferMetadata& metadata) override;

  bool SubmitTemporalDelimiter(size_t& temporal_delimiter_obu_size,
                               std::optional<uint8_t> temporal_idx);
  bool SubmitSequenceHeader(size_t& sequence_header_obu_size);
  bool SubmitSequenceParam();
  bool SubmitSequenceHeaderOBU(size_t& sequence_header_obu_size);
  bool SubmitFrame(const EncodeJob& job,
                   size_t frame_header_obu_offset,
                   std::optional<uint8_t> temporal_idx);
  void UpdateReferenceFrames(scoped_refptr<AV1Picture> picture);
  bool FillPictureParam(VAEncPictureParameterBufferAV1& pic_param,
                        VAEncSegMapBufferAV1& segment_map_param,
                        const EncodeJob& job,
                        const AV1Picture& pic);
  bool SubmitFrameOBU(const VAEncPictureParameterBufferAV1& pic_param,
                      size_t& frame_header_obu_size_offset,
                      std::optional<uint8_t> temporal_idx);
  bool SubmitPictureParam(const VAEncPictureParameterBufferAV1& pic_param);
  bool SubmitSegmentMap(const VAEncSegMapBufferAV1& segment_map_param);
  bool SubmitTileGroup();
  bool SubmitPackedData(const std::vector<uint8_t>& data);

  int level_idx_;
  uint64_t frame_num_ = 0;
  EncodeParams current_params_;
  gfx::Size visible_size_;
  gfx::Size coded_size_;
  // TODO(b:274756117): In tuning this encoder, we may decide we want multiple
  // reference frames, not just the most recent.
  AV1BitstreamBuilder::SequenceHeader sequence_header_;
  std::unique_ptr<aom::AV1RateControlRTC> rate_ctrl_;
  std::vector<uint8_t> segmentation_map_{};
  uint32_t seg_size_;

  std::array<scoped_refptr<AV1Picture>, libgav1::kNumReferenceFrameTypes>
      ref_frames_;
  std::unique_ptr<SVCLayers> svc_layers_;
  uint8_t num_temporal_layers_ = 1;
};

}  // namespace media

#endif  // MEDIA_GPU_VAAPI_AV1_VAAPI_VIDEO_ENCODER_DELEGATE_H_