1 /* 2 * HEVC parameter set parsing 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #ifndef AVCODEC_HEVC_PS_H 22 #define AVCODEC_HEVC_PS_H 23 24 #include <stdint.h> 25 26 #include "libavutil/buffer.h" 27 #include "libavutil/pixfmt.h" 28 #include "libavutil/rational.h" 29 30 #include "avcodec.h" 31 #include "get_bits.h" 32 #include "hevc.h" 33 34 typedef struct ShortTermRPS { 35 unsigned int num_negative_pics; 36 int num_delta_pocs; 37 int rps_idx_num_delta_pocs; 38 int32_t delta_poc[32]; 39 uint8_t used[32]; 40 } ShortTermRPS; 41 42 typedef struct HEVCWindow { 43 unsigned int left_offset; 44 unsigned int right_offset; 45 unsigned int top_offset; 46 unsigned int bottom_offset; 47 } HEVCWindow; 48 49 typedef struct VUI { 50 AVRational sar; 51 52 int overscan_info_present_flag; 53 int overscan_appropriate_flag; 54 55 int video_signal_type_present_flag; 56 int video_format; 57 int video_full_range_flag; 58 int colour_description_present_flag; 59 uint8_t colour_primaries; 60 uint8_t transfer_characteristic; 61 uint8_t matrix_coeffs; 62 63 int chroma_loc_info_present_flag; 64 int chroma_sample_loc_type_top_field; 65 int chroma_sample_loc_type_bottom_field; 66 int neutra_chroma_indication_flag; 67 68 int field_seq_flag; 69 int frame_field_info_present_flag; 70 71 int default_display_window_flag; 72 HEVCWindow def_disp_win; 73 74 int vui_timing_info_present_flag; 75 uint32_t vui_num_units_in_tick; 76 uint32_t vui_time_scale; 77 int vui_poc_proportional_to_timing_flag; 78 int vui_num_ticks_poc_diff_one_minus1; 79 int vui_hrd_parameters_present_flag; 80 81 int bitstream_restriction_flag; 82 int tiles_fixed_structure_flag; 83 int motion_vectors_over_pic_boundaries_flag; 84 int restricted_ref_pic_lists_flag; 85 int min_spatial_segmentation_idc; 86 int max_bytes_per_pic_denom; 87 int max_bits_per_min_cu_denom; 88 int log2_max_mv_length_horizontal; 89 int log2_max_mv_length_vertical; 90 } VUI; 91 92 typedef struct PTLCommon { 93 uint8_t profile_space; 94 uint8_t tier_flag; 95 uint8_t profile_idc; 96 uint8_t profile_compatibility_flag[32]; 97 uint8_t progressive_source_flag; 98 uint8_t interlaced_source_flag; 99 uint8_t non_packed_constraint_flag; 100 uint8_t frame_only_constraint_flag; 101 uint8_t max_12bit_constraint_flag; 102 uint8_t max_10bit_constraint_flag; 103 uint8_t max_8bit_constraint_flag; 104 uint8_t max_422chroma_constraint_flag; 105 uint8_t max_420chroma_constraint_flag; 106 uint8_t max_monochrome_constraint_flag; 107 uint8_t intra_constraint_flag; 108 uint8_t one_picture_only_constraint_flag; 109 uint8_t lower_bit_rate_constraint_flag; 110 uint8_t max_14bit_constraint_flag; 111 uint8_t inbld_flag; 112 uint8_t level_idc; 113 } PTLCommon; 114 115 typedef struct PTL { 116 PTLCommon general_ptl; 117 PTLCommon sub_layer_ptl[HEVC_MAX_SUB_LAYERS]; 118 119 uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS]; 120 uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS]; 121 } PTL; 122 123 typedef struct HEVCVPS { 124 uint8_t vps_temporal_id_nesting_flag; 125 int vps_max_layers; 126 int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1 127 128 PTL ptl; 129 int vps_sub_layer_ordering_info_present_flag; 130 unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS]; 131 unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS]; 132 unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS]; 133 int vps_max_layer_id; 134 int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1 135 uint8_t vps_timing_info_present_flag; 136 uint32_t vps_num_units_in_tick; 137 uint32_t vps_time_scale; 138 uint8_t vps_poc_proportional_to_timing_flag; 139 int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1 140 int vps_num_hrd_parameters; 141 142 uint8_t data[4096]; 143 int data_size; 144 } HEVCVPS; 145 146 typedef struct ScalingList { 147 /* This is a little wasteful, since sizeID 0 only needs 8 coeffs, 148 * and size ID 3 only has 2 arrays, not 6. */ 149 uint8_t sl[4][6][64]; 150 uint8_t sl_dc[2][6]; 151 } ScalingList; 152 153 typedef struct HEVCSPS { 154 unsigned vps_id; 155 int chroma_format_idc; 156 uint8_t separate_colour_plane_flag; 157 158 HEVCWindow output_window; 159 160 HEVCWindow pic_conf_win; 161 162 int bit_depth; 163 int bit_depth_chroma; 164 int pixel_shift; 165 enum AVPixelFormat pix_fmt; 166 167 unsigned int log2_max_poc_lsb; 168 int pcm_enabled_flag; 169 170 int max_sub_layers; 171 struct { 172 int max_dec_pic_buffering; 173 int num_reorder_pics; 174 int max_latency_increase; 175 } temporal_layer[HEVC_MAX_SUB_LAYERS]; 176 uint8_t temporal_id_nesting_flag; 177 178 VUI vui; 179 PTL ptl; 180 181 uint8_t scaling_list_enable_flag; 182 ScalingList scaling_list; 183 184 unsigned int nb_st_rps; 185 ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_REF_PIC_SETS]; 186 187 uint8_t amp_enabled_flag; 188 uint8_t sao_enabled; 189 190 uint8_t long_term_ref_pics_present_flag; 191 uint16_t lt_ref_pic_poc_lsb_sps[HEVC_MAX_LONG_TERM_REF_PICS]; 192 uint8_t used_by_curr_pic_lt_sps_flag[HEVC_MAX_LONG_TERM_REF_PICS]; 193 uint8_t num_long_term_ref_pics_sps; 194 195 struct { 196 uint8_t bit_depth; 197 uint8_t bit_depth_chroma; 198 unsigned int log2_min_pcm_cb_size; 199 unsigned int log2_max_pcm_cb_size; 200 uint8_t loop_filter_disable_flag; 201 } pcm; 202 uint8_t sps_temporal_mvp_enabled_flag; 203 uint8_t sps_strong_intra_smoothing_enable_flag; 204 205 unsigned int log2_min_cb_size; 206 unsigned int log2_diff_max_min_coding_block_size; 207 unsigned int log2_min_tb_size; 208 unsigned int log2_max_trafo_size; 209 unsigned int log2_ctb_size; 210 unsigned int log2_min_pu_size; 211 212 int max_transform_hierarchy_depth_inter; 213 int max_transform_hierarchy_depth_intra; 214 215 int sps_range_extension_flag; 216 int transform_skip_rotation_enabled_flag; 217 int transform_skip_context_enabled_flag; 218 int implicit_rdpcm_enabled_flag; 219 int explicit_rdpcm_enabled_flag; 220 int extended_precision_processing_flag; 221 int intra_smoothing_disabled_flag; 222 int high_precision_offsets_enabled_flag; 223 int persistent_rice_adaptation_enabled_flag; 224 int cabac_bypass_alignment_enabled_flag; 225 226 ///< coded frame dimension in various units 227 int width; 228 int height; 229 int ctb_width; 230 int ctb_height; 231 int ctb_size; 232 int min_cb_width; 233 int min_cb_height; 234 int min_tb_width; 235 int min_tb_height; 236 int min_pu_width; 237 int min_pu_height; 238 int tb_mask; 239 240 int hshift[3]; 241 int vshift[3]; 242 243 int qp_bd_offset; 244 245 uint8_t data[4096]; 246 int data_size; 247 } HEVCSPS; 248 249 typedef struct HEVCPPS { 250 unsigned int sps_id; ///< seq_parameter_set_id 251 252 uint8_t sign_data_hiding_flag; 253 254 uint8_t cabac_init_present_flag; 255 256 int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1 257 int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1 258 int pic_init_qp_minus26; 259 260 uint8_t constrained_intra_pred_flag; 261 uint8_t transform_skip_enabled_flag; 262 263 uint8_t cu_qp_delta_enabled_flag; 264 int diff_cu_qp_delta_depth; 265 266 int cb_qp_offset; 267 int cr_qp_offset; 268 uint8_t pic_slice_level_chroma_qp_offsets_present_flag; 269 uint8_t weighted_pred_flag; 270 uint8_t weighted_bipred_flag; 271 uint8_t output_flag_present_flag; 272 uint8_t transquant_bypass_enable_flag; 273 274 uint8_t dependent_slice_segments_enabled_flag; 275 uint8_t tiles_enabled_flag; 276 uint8_t entropy_coding_sync_enabled_flag; 277 278 uint16_t num_tile_columns; ///< num_tile_columns_minus1 + 1 279 uint16_t num_tile_rows; ///< num_tile_rows_minus1 + 1 280 uint8_t uniform_spacing_flag; 281 uint8_t loop_filter_across_tiles_enabled_flag; 282 283 uint8_t seq_loop_filter_across_slices_enabled_flag; 284 285 uint8_t deblocking_filter_control_present_flag; 286 uint8_t deblocking_filter_override_enabled_flag; 287 uint8_t disable_dbf; 288 int beta_offset; ///< beta_offset_div2 * 2 289 int tc_offset; ///< tc_offset_div2 * 2 290 291 uint8_t scaling_list_data_present_flag; 292 ScalingList scaling_list; 293 294 uint8_t lists_modification_present_flag; 295 int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2 296 int num_extra_slice_header_bits; 297 uint8_t slice_header_extension_present_flag; 298 uint8_t log2_max_transform_skip_block_size; 299 uint8_t pps_range_extensions_flag; 300 uint8_t cross_component_prediction_enabled_flag; 301 uint8_t chroma_qp_offset_list_enabled_flag; 302 uint8_t diff_cu_chroma_qp_offset_depth; 303 uint8_t chroma_qp_offset_list_len_minus1; 304 int8_t cb_qp_offset_list[6]; 305 int8_t cr_qp_offset_list[6]; 306 uint8_t log2_sao_offset_scale_luma; 307 uint8_t log2_sao_offset_scale_chroma; 308 309 // Inferred parameters 310 unsigned int *column_width; ///< ColumnWidth 311 unsigned int *row_height; ///< RowHeight 312 unsigned int *col_bd; ///< ColBd 313 unsigned int *row_bd; ///< RowBd 314 int *col_idxX; 315 316 int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS 317 int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS 318 int *tile_id; ///< TileId 319 int *tile_pos_rs; ///< TilePosRS 320 int *min_tb_addr_zs; ///< MinTbAddrZS 321 int *min_tb_addr_zs_tab;///< MinTbAddrZS 322 323 uint8_t data[4096]; 324 int data_size; 325 } HEVCPPS; 326 327 typedef struct HEVCParamSets { 328 AVBufferRef *vps_list[HEVC_MAX_VPS_COUNT]; 329 AVBufferRef *sps_list[HEVC_MAX_SPS_COUNT]; 330 AVBufferRef *pps_list[HEVC_MAX_PPS_COUNT]; 331 332 /* currently active parameter sets */ 333 const HEVCVPS *vps; 334 const HEVCSPS *sps; 335 const HEVCPPS *pps; 336 } HEVCParamSets; 337 338 /** 339 * Parse the SPS from the bitstream into the provided HEVCSPS struct. 340 * 341 * @param sps_id the SPS id will be written here 342 * @param apply_defdispwin if set 1, the default display window from the VUI 343 * will be applied to the video dimensions 344 * @param vps_list if non-NULL, this function will validate that the SPS refers 345 * to an existing VPS 346 */ 347 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id, 348 int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx); 349 350 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, 351 HEVCParamSets *ps); 352 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, 353 HEVCParamSets *ps, int apply_defdispwin); 354 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, 355 HEVCParamSets *ps); 356 357 void ff_hevc_ps_uninit(HEVCParamSets *ps); 358 359 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, 360 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header); 361 362 int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id, 363 uint8_t *buf, int buf_size); 364 365 /** 366 * Compute POC of the current frame and return it. 367 */ 368 int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type); 369 370 #endif /* AVCODEC_HEVC_PS_H */ 371