h264_vaapi_video_encoder_delegate.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef MEDIA_GPU_VAAPI_H264_VAAPI_VIDEO_ENCODER_DELEGATE_H_
  5. #define MEDIA_GPU_VAAPI_H264_VAAPI_VIDEO_ENCODER_DELEGATE_H_
  6. #include <stddef.h>
  7. #include "base/containers/circular_deque.h"
  8. #include "media/filters/h264_bitstream_buffer.h"
  9. #include "media/gpu/h264_dpb.h"
  10. #include "media/gpu/vaapi/vaapi_video_encoder_delegate.h"
  11. namespace media {
  12. class VaapiWrapper;
  13. // This class provides an H264 encoder functionality, generating stream headers,
  14. // managing encoder state, reference frames, and other codec parameters, while
  15. // requiring support from an Accelerator to encode frame data based on these
  16. // parameters.
  17. //
  18. // This class must be created, called and destroyed on a single sequence.
  19. //
  20. // Names used in documentation of this class refer directly to naming used
  21. // in the H.264 specification (http://www.itu.int/rec/T-REC-H.264).
  22. class H264VaapiVideoEncoderDelegate : public VaapiVideoEncoderDelegate {
  23. public:
  24. struct EncodeParams {
  25. EncodeParams();
  26. VideoBitrateAllocation bitrate_allocation;
  27. // Framerate in FPS.
  28. uint32_t framerate;
  29. // Bitrate window size in ms.
  30. uint32_t cpb_window_size_ms;
  31. // Bitrate window size in bits.
  32. unsigned int cpb_size_bits;
  33. // Quantization parameter. Their ranges are 0-51.
  34. uint8_t initial_qp;
  35. uint8_t min_qp;
  36. uint8_t max_qp;
  37. // Maxium Number of Reference frames.
  38. size_t max_num_ref_frames;
  39. // Maximum size of reference picture list 0.
  40. size_t max_ref_pic_list0_size;
  41. };
  42. H264VaapiVideoEncoderDelegate(scoped_refptr<VaapiWrapper> vaapi_wrapper,
  43. base::RepeatingClosure error_cb);
  44. H264VaapiVideoEncoderDelegate(const H264VaapiVideoEncoderDelegate&) = delete;
  45. H264VaapiVideoEncoderDelegate& operator=(
  46. const H264VaapiVideoEncoderDelegate&) = delete;
  47. ~H264VaapiVideoEncoderDelegate() override;
  48. // VaapiVideoEncoderDelegate implementation.
  49. bool Initialize(const VideoEncodeAccelerator::Config& config,
  50. const VaapiVideoEncoderDelegate::Config& ave_config) override;
  51. bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
  52. uint32_t framerate) override;
  53. gfx::Size GetCodedSize() const override;
  54. size_t GetMaxNumOfRefFrames() const override;
  55. std::vector<gfx::Size> GetSVCLayerResolutions() override;
  56. private:
  57. class TemporalLayers;
  58. friend class H264VaapiVideoEncoderDelegateTest;
  59. bool PrepareEncodeJob(EncodeJob& encode_job) override;
  60. BitstreamBufferMetadata GetMetadata(const EncodeJob& encode_job,
  61. size_t payload_size) override;
  62. // Fill current_sps_ and current_pps_ with current encoding state parameters.
  63. void UpdateSPS();
  64. void UpdatePPS();
  65. // Generate packed SPS and PPS in packed_sps_ and packed_pps_, using values
  66. // in current_sps_ and current_pps_.
  67. void GeneratePackedSPS();
  68. void GeneratePackedPPS();
  69. // Generate packed slice header from |pic_param|, |slice_param| and |pic|.
  70. scoped_refptr<H264BitstreamBuffer> GeneratePackedSliceHeader(
  71. const VAEncPictureParameterBufferH264& pic_param,
  72. const VAEncSliceParameterBufferH264& sliice_param,
  73. const H264Picture& pic);
  74. // Check if |bitrate| and |framerate| and current coded size are supported by
  75. // current profile and level.
  76. bool CheckConfigValidity(uint32_t bitrate, uint32_t framerate);
  77. bool SubmitPackedHeaders(const H264BitstreamBuffer& packed_sps,
  78. const H264BitstreamBuffer& packed_pps);
  79. bool SubmitFrameParameters(
  80. EncodeJob& job,
  81. const H264VaapiVideoEncoderDelegate::EncodeParams& encode_params,
  82. const H264SPS& sps,
  83. const H264PPS& pps,
  84. scoped_refptr<H264Picture> pic,
  85. const base::circular_deque<scoped_refptr<H264Picture>>& ref_pic_list0,
  86. const absl::optional<size_t>& ref_frame_index);
  87. // Current SPS, PPS and their packed versions. Packed versions are NALUs
  88. // in AnnexB format *without* emulation prevention three-byte sequences
  89. // (those are expected to be added by the client as needed).
  90. H264SPS current_sps_;
  91. scoped_refptr<H264BitstreamBuffer> packed_sps_;
  92. H264PPS current_pps_;
  93. scoped_refptr<H264BitstreamBuffer> packed_pps_;
  94. bool submit_packed_headers_;
  95. // Current encoding parameters being used.
  96. EncodeParams curr_params_;
  97. // H264 profile currently used.
  98. VideoCodecProfile profile_ = VIDEO_CODEC_PROFILE_UNKNOWN;
  99. // H264 level currently used.
  100. uint8_t level_ = 0;
  101. // Current visible and coded sizes in pixels.
  102. gfx::Size visible_size_;
  103. gfx::Size coded_size_;
  104. // Width/height in macroblocks.
  105. unsigned int mb_width_ = 0;
  106. unsigned int mb_height_ = 0;
  107. // The number of encoded frames. Resets to 0 on IDR frame.
  108. unsigned int num_encoded_frames_ = 0;
  109. // frame_num (spec section 7.4.3).
  110. unsigned int frame_num_ = 0;
  111. // idr_pic_id (spec section 7.4.3) to be used for the next frame.
  112. unsigned int idr_pic_id_ = 0;
  113. // True if encoding parameters have changed that affect decoder process, then
  114. // we need to submit a keyframe with updated parameters.
  115. bool encoding_parameters_changed_ = false;
  116. // Currently active reference frames.
  117. // RefPicList0 per spec (spec section 8.2.4.2).
  118. base::circular_deque<scoped_refptr<H264Picture>> ref_pic_list0_;
  119. uint8_t num_temporal_layers_ = 1;
  120. };
  121. } // namespace media
  122. #endif // MEDIA_GPU_VAAPI_H264_VAAPI_VIDEO_ENCODER_DELEGATE_H_