1 /* 2 * Copyright © Microsoft Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #ifndef D3D12_VIDEO_ENC_BITSTREAM_BUILDER_HEVC_H 25 #define D3D12_VIDEO_ENC_BITSTREAM_BUILDER_HEVC_H 26 27 #include "d3d12_video_encoder_nalu_writer_hevc.h" 28 #include "d3d12_video_encoder_bitstream_builder.h" 29 30 class d3d12_video_bitstream_builder_hevc : public d3d12_video_bitstream_builder_interface 31 { 32 33 public: d3d12_video_bitstream_builder_hevc()34 d3d12_video_bitstream_builder_hevc() {}; ~d3d12_video_bitstream_builder_hevc()35 ~d3d12_video_bitstream_builder_hevc() {}; 36 37 void build_vps(const D3D12_VIDEO_ENCODER_PROFILE_HEVC& profile, 38 const D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC& level, 39 const DXGI_FORMAT inputFmt, 40 uint8_t maxRefFrames, 41 bool gopHasBFrames, 42 uint8_t vps_video_parameter_set_id, 43 std::vector<BYTE> &headerBitstream, 44 std::vector<BYTE>::iterator placingPositionStart, 45 size_t &writtenBytes, 46 HevcVideoParameterSet* pVPSStruct = nullptr); 47 48 void build_sps(const HevcVideoParameterSet& parentVPS, 49 const struct pipe_h265_enc_seq_param & seqData, 50 uint8_t seq_parameter_set_id, 51 const D3D12_VIDEO_ENCODER_PICTURE_RESOLUTION_DESC& encodeResolution, 52 const D3D12_BOX& crop_window_upper_layer, 53 const UINT picDimensionMultipleRequirement, 54 const DXGI_FORMAT& inputFmt, 55 const D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC& codecConfig, 56 const D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE_HEVC& hevcGOP, 57 std::vector<BYTE> &headerBitstream, 58 std::vector<BYTE>::iterator placingPositionStart, 59 size_t &writtenBytes, 60 HevcSeqParameterSet* outputSPS = nullptr); 61 62 void build_pps(const HevcSeqParameterSet& parentSPS, 63 uint8_t pic_parameter_set_id, 64 const D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC& codecConfig, 65 const D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_HEVC& pictureControl, 66 std::vector<BYTE> &headerBitstream, 67 std::vector<BYTE>::iterator placingPositionStart, 68 size_t &writtenBytes, 69 HevcPicParameterSet* outputPPS = nullptr); 70 71 void write_end_of_stream_nalu(std::vector<uint8_t> & headerBitstream, 72 std::vector<uint8_t>::iterator placingPositionStart, 73 size_t & writtenBytes); 74 void write_end_of_sequence_nalu(std::vector<uint8_t> & headerBitstream, 75 std::vector<uint8_t>::iterator placingPositionStart, 76 size_t & writtenBytes); 77 78 void print_vps(const HevcVideoParameterSet& vps); 79 void print_sps(const HevcSeqParameterSet& sps); 80 void print_pps(const HevcPicParameterSet& pps); 81 void print_rps(const HevcSeqParameterSet* sps, UINT stRpsIdx); 82 get_active_pps()83 std::vector<uint8_t>& get_active_pps() 84 { 85 return m_activePPS; 86 }; 87 get_latest_vps()88 HevcVideoParameterSet get_latest_vps() 89 { 90 return m_latest_vps; 91 } 92 get_latest_sps()93 HevcSeqParameterSet get_latest_sps() 94 { 95 return m_latest_sps; 96 } 97 get_latest_pps()98 HevcPicParameterSet get_latest_pps() 99 { 100 return m_latest_pps; 101 } 102 get_active_vps_id()103 uint32_t get_active_vps_id() 104 { 105 return m_activeVPSIndex; 106 }; get_active_sps_id()107 uint32_t get_active_sps_id() 108 { 109 return m_activeSPSIndex; 110 }; get_active_pps_id()111 uint32_t get_active_pps_id() 112 { 113 return m_activePPSIndex; 114 }; 115 set_active_vps_id(uint32_t active_vps_id)116 void set_active_vps_id(uint32_t active_vps_id) 117 { 118 m_activeVPSIndex = active_vps_id; 119 debug_printf("[d3d12_video_bitstream_builder_hevc] Setting new active VPS ID: %d ", m_activeVPSIndex); 120 }; 121 set_active_sps_id(uint32_t active_sps_id)122 void set_active_sps_id(uint32_t active_sps_id) 123 { 124 m_activeSPSIndex = active_sps_id; 125 debug_printf("[d3d12_video_bitstream_builder_hevc] Setting new active SPS ID: %d ", m_activeSPSIndex); 126 }; set_active_pps_id(uint32_t active_pps_id)127 void set_active_pps_id(uint32_t active_pps_id) 128 { 129 m_activePPSIndex = active_pps_id; 130 debug_printf("[d3d12_video_bitstream_builder_hevc] Setting new active PPS ID: %d ", m_activePPSIndex); 131 }; 132 133 private: 134 d3d12_video_nalu_writer_hevc m_hevcEncoder; 135 uint32_t m_activeVPSIndex = 0; 136 uint32_t m_activeSPSIndex = 0; 137 uint32_t m_activePPSIndex = 0; 138 HevcVideoParameterSet m_latest_vps = {}; 139 HevcSeqParameterSet m_latest_sps = {}; 140 HevcPicParameterSet m_latest_pps = {}; 141 std::vector<uint8_t> m_activePPS; 142 143 void init_profile_tier_level(HEVCProfileTierLevel *ptl, uint8_t HEVCProfileIdc, uint8_t HEVCLevelIdc, bool isHighTier); 144 }; 145 146 #endif 147