2 * HEVC parameter set parsing
4 * This file is part of FFmpeg.
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.
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.
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
21 #ifndef AVCODEC_HEVC_PS_H
22 #define AVCODEC_HEVC_PS_H
26 #include "libavutil/buffer.h"
27 #include "libavutil/pixfmt.h"
28 #include "libavutil/rational.h"
34 typedef struct ShortTermRPS {
35 unsigned int num_negative_pics;
37 int rps_idx_num_delta_pocs;
38 int32_t delta_poc[32];
42 typedef struct LongTermRPS {
48 typedef struct SliceHeader {
51 ///< address (in raster order) of the first block in the current slice segment
52 unsigned int slice_segment_addr;
53 ///< address (in raster order) of the first block in the current slice
54 unsigned int slice_addr;
56 enum HEVCSliceType slice_type;
58 int pic_order_cnt_lsb;
60 uint8_t first_slice_in_pic_flag;
61 uint8_t dependent_slice_segment_flag;
62 uint8_t pic_output_flag;
63 uint8_t colour_plane_id;
65 ///< RPS coded in the slice header itself is stored here
66 int short_term_ref_pic_set_sps_flag;
67 int short_term_ref_pic_set_size;
68 ShortTermRPS slice_rps;
69 const ShortTermRPS *short_term_rps;
70 int long_term_ref_pic_set_size;
71 LongTermRPS long_term_rps;
72 unsigned int list_entry_lx[2][32];
74 uint8_t rpl_modification_flag[2];
75 uint8_t no_output_of_prior_pics_flag;
76 uint8_t slice_temporal_mvp_enabled_flag;
78 unsigned int nb_refs[2];
80 uint8_t slice_sample_adaptive_offset_flag[3];
81 uint8_t mvd_l1_zero_flag;
83 uint8_t cabac_init_flag;
84 uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
85 uint8_t slice_loop_filter_across_slices_enabled_flag;
86 uint8_t collocated_list;
88 unsigned int collocated_ref_idx;
91 int slice_cb_qp_offset;
92 int slice_cr_qp_offset;
94 uint8_t cu_chroma_qp_offset_enabled_flag;
96 int beta_offset; ///< beta_offset_div2 * 2
97 int tc_offset; ///< tc_offset_div2 * 2
99 unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
101 unsigned *entry_point_offset;
104 int num_entry_point_offsets;
108 uint8_t luma_log2_weight_denom;
109 int16_t chroma_log2_weight_denom;
111 int16_t luma_weight_l0[16];
112 int16_t chroma_weight_l0[16][2];
113 int16_t chroma_weight_l1[16][2];
114 int16_t luma_weight_l1[16];
116 int16_t luma_offset_l0[16];
117 int16_t chroma_offset_l0[16][2];
119 int16_t luma_offset_l1[16];
120 int16_t chroma_offset_l1[16][2];
122 int slice_ctb_addr_rs;
125 typedef struct HEVCWindow {
126 unsigned int left_offset;
127 unsigned int right_offset;
128 unsigned int top_offset;
129 unsigned int bottom_offset;
135 int overscan_info_present_flag;
136 int overscan_appropriate_flag;
138 int video_signal_type_present_flag;
140 int video_full_range_flag;
141 int colour_description_present_flag;
142 uint8_t colour_primaries;
143 uint8_t transfer_characteristic;
144 uint8_t matrix_coeffs;
146 int chroma_loc_info_present_flag;
147 int chroma_sample_loc_type_top_field;
148 int chroma_sample_loc_type_bottom_field;
149 int neutra_chroma_indication_flag;
152 int frame_field_info_present_flag;
154 int default_display_window_flag;
155 HEVCWindow def_disp_win;
157 int vui_timing_info_present_flag;
158 uint32_t vui_num_units_in_tick;
159 uint32_t vui_time_scale;
160 int vui_poc_proportional_to_timing_flag;
161 int vui_num_ticks_poc_diff_one_minus1;
162 int vui_hrd_parameters_present_flag;
164 int bitstream_restriction_flag;
165 int tiles_fixed_structure_flag;
166 int motion_vectors_over_pic_boundaries_flag;
167 int restricted_ref_pic_lists_flag;
168 int min_spatial_segmentation_idc;
169 int max_bytes_per_pic_denom;
170 int max_bits_per_min_cu_denom;
171 int log2_max_mv_length_horizontal;
172 int log2_max_mv_length_vertical;
175 typedef struct PTLCommon {
176 uint8_t profile_space;
179 uint8_t profile_compatibility_flag[32];
181 uint8_t progressive_source_flag;
182 uint8_t interlaced_source_flag;
183 uint8_t non_packed_constraint_flag;
184 uint8_t frame_only_constraint_flag;
188 PTLCommon general_ptl;
189 PTLCommon sub_layer_ptl[HEVC_MAX_SUB_LAYERS];
191 uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
192 uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
195 typedef struct HEVCVPS {
196 uint8_t vps_temporal_id_nesting_flag;
198 int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
201 int vps_sub_layer_ordering_info_present_flag;
202 unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS];
203 unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS];
204 unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS];
205 int vps_max_layer_id;
206 int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
207 uint8_t vps_timing_info_present_flag;
208 uint32_t vps_num_units_in_tick;
209 uint32_t vps_time_scale;
210 uint8_t vps_poc_proportional_to_timing_flag;
211 int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
212 int vps_num_hrd_parameters;
218 typedef struct ScalingList {
219 /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
220 * and size ID 3 only has 2 arrays, not 6. */
221 uint8_t sl[4][6][64];
225 typedef struct HEVCSPS {
227 int chroma_format_idc;
228 uint8_t separate_colour_plane_flag;
230 HEVCWindow output_window;
232 HEVCWindow pic_conf_win;
235 int bit_depth_chroma;
237 enum AVPixelFormat pix_fmt;
239 unsigned int log2_max_poc_lsb;
240 int pcm_enabled_flag;
244 int max_dec_pic_buffering;
245 int num_reorder_pics;
246 int max_latency_increase;
247 } temporal_layer[HEVC_MAX_SUB_LAYERS];
248 uint8_t temporal_id_nesting_flag;
253 uint8_t scaling_list_enable_flag;
254 ScalingList scaling_list;
256 unsigned int nb_st_rps;
257 ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
259 uint8_t amp_enabled_flag;
262 uint8_t long_term_ref_pics_present_flag;
263 uint16_t lt_ref_pic_poc_lsb_sps[HEVC_MAX_LONG_TERM_REF_PICS];
264 uint8_t used_by_curr_pic_lt_sps_flag[HEVC_MAX_LONG_TERM_REF_PICS];
265 uint8_t num_long_term_ref_pics_sps;
269 uint8_t bit_depth_chroma;
270 unsigned int log2_min_pcm_cb_size;
271 unsigned int log2_max_pcm_cb_size;
272 uint8_t loop_filter_disable_flag;
274 uint8_t sps_temporal_mvp_enabled_flag;
275 uint8_t sps_strong_intra_smoothing_enable_flag;
277 unsigned int log2_min_cb_size;
278 unsigned int log2_diff_max_min_coding_block_size;
279 unsigned int log2_min_tb_size;
280 unsigned int log2_max_trafo_size;
281 unsigned int log2_ctb_size;
282 unsigned int log2_min_pu_size;
284 int max_transform_hierarchy_depth_inter;
285 int max_transform_hierarchy_depth_intra;
287 int transform_skip_rotation_enabled_flag;
288 int transform_skip_context_enabled_flag;
289 int implicit_rdpcm_enabled_flag;
290 int explicit_rdpcm_enabled_flag;
291 int intra_smoothing_disabled_flag;
292 int high_precision_offsets_enabled_flag;
293 int persistent_rice_adaptation_enabled_flag;
295 ///< coded frame dimension in various units
318 typedef struct HEVCPPS {
319 unsigned int sps_id; ///< seq_parameter_set_id
321 uint8_t sign_data_hiding_flag;
323 uint8_t cabac_init_present_flag;
325 int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
326 int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
327 int pic_init_qp_minus26;
329 uint8_t constrained_intra_pred_flag;
330 uint8_t transform_skip_enabled_flag;
332 uint8_t cu_qp_delta_enabled_flag;
333 int diff_cu_qp_delta_depth;
337 uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
338 uint8_t weighted_pred_flag;
339 uint8_t weighted_bipred_flag;
340 uint8_t output_flag_present_flag;
341 uint8_t transquant_bypass_enable_flag;
343 uint8_t dependent_slice_segments_enabled_flag;
344 uint8_t tiles_enabled_flag;
345 uint8_t entropy_coding_sync_enabled_flag;
347 int num_tile_columns; ///< num_tile_columns_minus1 + 1
348 int num_tile_rows; ///< num_tile_rows_minus1 + 1
349 uint8_t uniform_spacing_flag;
350 uint8_t loop_filter_across_tiles_enabled_flag;
352 uint8_t seq_loop_filter_across_slices_enabled_flag;
354 uint8_t deblocking_filter_control_present_flag;
355 uint8_t deblocking_filter_override_enabled_flag;
357 int beta_offset; ///< beta_offset_div2 * 2
358 int tc_offset; ///< tc_offset_div2 * 2
360 uint8_t scaling_list_data_present_flag;
361 ScalingList scaling_list;
363 uint8_t lists_modification_present_flag;
364 int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
365 int num_extra_slice_header_bits;
366 uint8_t slice_header_extension_present_flag;
367 uint8_t log2_max_transform_skip_block_size;
368 uint8_t cross_component_prediction_enabled_flag;
369 uint8_t chroma_qp_offset_list_enabled_flag;
370 uint8_t diff_cu_chroma_qp_offset_depth;
371 uint8_t chroma_qp_offset_list_len_minus1;
372 int8_t cb_qp_offset_list[6];
373 int8_t cr_qp_offset_list[6];
374 uint8_t log2_sao_offset_scale_luma;
375 uint8_t log2_sao_offset_scale_chroma;
377 // Inferred parameters
378 unsigned int *column_width; ///< ColumnWidth
379 unsigned int *row_height; ///< RowHeight
380 unsigned int *col_bd; ///< ColBd
381 unsigned int *row_bd; ///< RowBd
384 int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
385 int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
386 int *tile_id; ///< TileId
387 int *tile_pos_rs; ///< TilePosRS
388 int *min_tb_addr_zs; ///< MinTbAddrZS
389 int *min_tb_addr_zs_tab;///< MinTbAddrZS
395 typedef struct HEVCParamSets {
396 AVBufferRef *vps_list[HEVC_MAX_VPS_COUNT];
397 AVBufferRef *sps_list[HEVC_MAX_SPS_COUNT];
398 AVBufferRef *pps_list[HEVC_MAX_PPS_COUNT];
400 /* currently active parameter sets */
407 * Parse the SPS from the bitstream into the provided HEVCSPS struct.
409 * @param sps_id the SPS id will be written here
410 * @param apply_defdispwin if set 1, the default display window from the VUI
411 * will be applied to the video dimensions
412 * @param vps_list if non-NULL, this function will validate that the SPS refers
415 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
416 int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx);
418 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
420 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
421 HEVCParamSets *ps, int apply_defdispwin);
422 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
425 void ff_hevc_ps_uninit(HEVCParamSets *ps);
427 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
428 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header);
430 int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
431 uint8_t *buf, int buf_size);
434 * Compute POC of the current frame and return it.
436 int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type);
438 #endif /* AVCODEC_HEVC_PS_H */