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_AV1_H 25 #define D3D12_VIDEO_ENC_BITSTREAM_BUILDER_AV1_H 26 27 #include "d3d12_video_encoder_bitstream_builder.h" 28 #include "d3d12_video_encoder_bitstream.h" 29 30 typedef struct av1_tile_group_t 31 { 32 uint8_t tg_start; 33 uint8_t tg_end; 34 } av1_tile_group_t; 35 36 typedef enum av1_obutype_t 37 { 38 OBU_SEQUENCE_HEADER = 1, 39 OBU_TEMPORAL_DELIMITER = 2, 40 OBU_FRAME_HEADER = 3, 41 OBU_TILE_GROUP = 4, 42 OBU_METADATA = 5, 43 OBU_FRAME = 6, 44 OBU_REDUNDANT_FRAME_HEADER = 7, 45 OBU_PADDING = 15, 46 } av1_obutype_t; 47 48 typedef struct av1_pic_tile_info_t 49 { 50 uint32_t uniform_tile_spacing_flag; 51 D3D12_VIDEO_ENCODER_AV1_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_TILES tile_partition; 52 D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE tile_mode; 53 D3D12_VIDEO_ENCODER_AV1_FRAME_SUBREGION_LAYOUT_CONFIG_SUPPORT tile_support_caps; 54 } av1_pic_tile_info_t; 55 56 typedef struct av1_pic_lr_params_t 57 { 58 D3D12_VIDEO_ENCODER_AV1_RESTORATION_TYPE lr_type[3]; 59 uint32_t lr_unit_shift; 60 uint32_t lr_uv_shift; 61 uint32_t lr_unit_extra_shift; 62 } av1_pic_lr_params_t; 63 64 typedef struct av1_seq_color_config_t 65 { 66 DXGI_FORMAT bit_depth; 67 // uint32_t mono_chrome; // coded in bitstream by default as 0 68 uint32_t color_primaries; 69 uint32_t transfer_characteristics; 70 uint32_t matrix_coefficients; 71 uint32_t color_description_present_flag; 72 uint32_t color_range; 73 uint32_t chroma_sample_position; 74 uint32_t subsampling_x; 75 uint32_t subsampling_y; 76 uint32_t separate_uv_delta_q; 77 } av1_seq_color_config_t; 78 79 typedef struct av1_pic_header_t 80 { 81 uint32_t show_existing_frame; 82 uint32_t frame_to_show_map_idx; 83 // uint32_t display_frame_id; // frame_id_numbers_present_flag coded in bitstream by default as 0 84 D3D12_VIDEO_ENCODER_AV1_FRAME_TYPE frame_type; 85 uint32_t show_frame; 86 uint32_t showable_frame; 87 uint32_t error_resilient_mode; 88 uint32_t disable_cdf_update; 89 uint32_t allow_screen_content_tools; 90 uint32_t force_integer_mv; 91 uint32_t frame_size_override_flag; 92 uint32_t order_hint; 93 uint32_t ref_order_hint[8]; 94 uint32_t primary_ref_frame; 95 uint8_t refresh_frame_flags; 96 uint32_t FrameWidth; 97 uint32_t FrameHeight; 98 uint32_t frame_width_sb; 99 uint32_t frame_height_sb; 100 uint32_t use_superres; 101 uint32_t SuperresDenom; 102 uint32_t UpscaledWidth; 103 uint32_t RenderWidth; 104 uint32_t RenderHeight; 105 uint32_t allow_intrabc; 106 int32_t ref_frame_idx[7]; 107 D3D12_VIDEO_ENCODER_AV1_REFERENCE_PICTURE_WARPED_MOTION_INFO ref_global_motion_info[7]; 108 uint32_t allow_high_precision_mv; 109 D3D12_VIDEO_ENCODER_AV1_INTERPOLATION_FILTERS interpolation_filter; 110 uint32_t is_motion_mode_switchable; 111 uint32_t use_ref_frame_mvs; 112 uint32_t disable_frame_end_update_cdf; 113 av1_pic_tile_info_t tile_info; 114 D3D12_VIDEO_ENCODER_CODEC_AV1_QUANTIZATION_CONFIG quantization_params; 115 D3D12_VIDEO_ENCODER_CODEC_AV1_LOOP_FILTER_DELTA_CONFIG delta_lf_params; 116 D3D12_VIDEO_ENCODER_CODEC_AV1_QUANTIZATION_DELTA_CONFIG delta_q_params; 117 // uint32_t CodedLossless; // coded in bitstream by default as 0 118 uint32_t AllLossless; // coded in bitstream by default as 0 119 D3D12_VIDEO_ENCODER_CODEC_AV1_LOOP_FILTER_CONFIG loop_filter_params; 120 D3D12_VIDEO_ENCODER_AV1_CDEF_CONFIG cdef_params; 121 av1_pic_lr_params_t lr_params; 122 D3D12_VIDEO_ENCODER_AV1_TX_MODE TxMode; 123 uint32_t reference_select; 124 uint32_t skip_mode_present; 125 uint32_t allow_warped_motion; 126 uint32_t reduced_tx_set; 127 uint32_t segmentation_enabled; 128 D3D12_VIDEO_ENCODER_AV1_SEGMENTATION_CONFIG segmentation_config; 129 // uint32_t frame_refs_short_signaling; // coded in bitstream by default as 0 130 } av1_pic_header_t; 131 132 typedef struct av1_seq_header_t 133 { 134 uint32_t seq_profile; 135 // uint32_t still_picture; // coded in bitstream by default as 0 136 // uint32_t reduced_still_picture_header; // coded in bitstream by default as 0 137 // uint32_t timing_info_present_flag; // coded in bitstream by default as 0 138 // uint32_t decoder_model_info_present_flag; // coded in bitstream by default as 0 139 uint32_t operating_points_cnt_minus_1; 140 uint32_t operating_point_idc[32]; 141 uint32_t seq_level_idx[32]; 142 uint32_t seq_tier[32]; 143 uint32_t decoder_model_present_for_this_op[32]; 144 // uint32_t initial_display_delay_present_flag; // coded in bitstream by default as 0 145 // uint32_t initial_display_delay_minus_1[32]; 146 // uint32_t initial_display_delay_present_for_this_op[32] 147 uint32_t max_frame_width; 148 uint32_t max_frame_height; 149 // uint32_t frame_id_numbers_present_flag; // coded in bitstream by default as 0 150 uint32_t use_128x128_superblock; 151 uint32_t enable_filter_intra; 152 uint32_t enable_intra_edge_filter; 153 uint32_t enable_interintra_compound; 154 uint32_t enable_masked_compound; 155 uint32_t enable_warped_motion; 156 uint32_t enable_dual_filter; 157 uint32_t enable_order_hint; 158 uint32_t enable_jnt_comp; 159 uint32_t enable_ref_frame_mvs; 160 uint32_t seq_choose_screen_content_tools; 161 uint32_t seq_force_screen_content_tools; 162 uint32_t seq_choose_integer_mv; 163 uint32_t seq_force_integer_mv; 164 uint32_t order_hint_bits_minus1; 165 uint32_t enable_superres; 166 uint32_t enable_cdef; 167 uint32_t enable_restoration; 168 av1_seq_color_config_t color_config; 169 // uint32_t film_grain_params_present; // coded in bitstream by default as 0 170 } av1_seq_header_t; 171 172 class d3d12_video_bitstream_builder_av1 : public d3d12_video_bitstream_builder_interface 173 { 174 public: d3d12_video_bitstream_builder_av1()175 d3d12_video_bitstream_builder_av1() 176 { } ~d3d12_video_bitstream_builder_av1()177 ~d3d12_video_bitstream_builder_av1() 178 { } 179 180 void write_temporal_delimiter_obu(std::vector<uint8_t> &headerBitstream, 181 std::vector<uint8_t>::iterator placingPositionStart, 182 size_t &writtenBytes); 183 184 void write_sequence_header(const av1_seq_header_t *pSeqHdr, 185 std::vector<uint8_t> &headerBitstream, 186 std::vector<uint8_t>::iterator placingPositionStart, 187 size_t &writtenBytes); 188 189 190 void write_frame_header(const av1_seq_header_t *pSeqHdr, 191 const av1_pic_header_t *pPicHdr, 192 av1_obutype_t frame_pack_type, 193 size_t extra_obu_size_bytes, 194 std::vector<uint8_t> &headerBitstream, 195 std::vector<uint8_t>::iterator placingPositionStart, 196 size_t &writtenBytes); 197 198 void calculate_tile_group_obu_size( 199 const D3D12_VIDEO_ENCODER_OUTPUT_METADATA *pParsedMetadata, 200 const D3D12_VIDEO_ENCODER_FRAME_SUBREGION_METADATA *pFrameSubregionMetadata, 201 size_t TileSizeBytes, // Pass already +1'd from TileSizeBytesMinus1 202 const D3D12_VIDEO_ENCODER_AV1_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_TILES &TilesPartition, 203 const av1_tile_group_t &tileGroup, 204 size_t &tile_group_obu_total_size, 205 size_t &decode_tile_elements_size); 206 207 void write_obu_tile_group_header(size_t tile_group_obu_size, 208 std::vector<uint8_t> &headerBitstream, 209 std::vector<uint8_t>::iterator placingPositionStart, 210 size_t &writtenBytes); 211 212 private: 213 // static void write_delta_q_value(d3d12_video_encoder_bitstream& bitstream, 214 // int32_t delta_q_val); 215 const size_t c_DefaultBitstreamBufSize = 1024; 216 217 void write_obu_header(d3d12_video_encoder_bitstream *pBit, 218 av1_obutype_t obu_type, 219 uint32_t obu_extension_flag, 220 uint32_t temporal_id, 221 uint32_t spatial_id); 222 223 void pack_obu_header_size(d3d12_video_encoder_bitstream *pBit, uint64_t val); 224 225 void write_seq_data(d3d12_video_encoder_bitstream *pBit, const av1_seq_header_t *pSeqHdr); 226 227 void write_pic_data(d3d12_video_encoder_bitstream *pBit, 228 const av1_seq_header_t *pSeqHdr, 229 const av1_pic_header_t *pPicHdr); 230 231 void write_delta_q_value(d3d12_video_encoder_bitstream *pBit, int32_t delta_q_val); 232 233 void write_frame_size(d3d12_video_encoder_bitstream *pBit, 234 const av1_seq_header_t *pSeqHdr, 235 const av1_pic_header_t *pPicHdr); 236 237 void write_frame_size_with_refs(d3d12_video_encoder_bitstream *pBit, 238 const av1_seq_header_t *pSeqHdr, 239 const av1_pic_header_t *pPicHdr); 240 241 void write_render_size(d3d12_video_encoder_bitstream *pBit, const av1_pic_header_t *pPicHdr); 242 243 void write_superres_params(d3d12_video_encoder_bitstream *pBit, 244 const av1_seq_header_t *pSeqHdr, 245 const av1_pic_header_t *pPicHdr); 246 247 // Constants 248 static const uint32_t frame_width_bits_minus_1 = 15; 249 static const uint32_t frame_height_bits_minus_1 = 15; 250 }; 251 252 #endif // D3D12_VIDEO_ENC_BITSTREAM_BUILDER_AV1_H 253