blob: b0a17bc8f0c15fed72e4a07128a3291fd5ebc1ac
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 level_idc; |
98 | uint8_t progressive_source_flag; |
99 | uint8_t interlaced_source_flag; |
100 | uint8_t non_packed_constraint_flag; |
101 | uint8_t frame_only_constraint_flag; |
102 | } PTLCommon; |
103 | |
104 | typedef struct PTL { |
105 | PTLCommon general_ptl; |
106 | PTLCommon sub_layer_ptl[HEVC_MAX_SUB_LAYERS]; |
107 | |
108 | uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS]; |
109 | uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS]; |
110 | } PTL; |
111 | |
112 | typedef struct HEVCVPS { |
113 | uint8_t vps_temporal_id_nesting_flag; |
114 | int vps_max_layers; |
115 | int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1 |
116 | |
117 | PTL ptl; |
118 | int vps_sub_layer_ordering_info_present_flag; |
119 | unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS]; |
120 | unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS]; |
121 | unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS]; |
122 | int vps_max_layer_id; |
123 | int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1 |
124 | uint8_t vps_timing_info_present_flag; |
125 | uint32_t vps_num_units_in_tick; |
126 | uint32_t vps_time_scale; |
127 | uint8_t vps_poc_proportional_to_timing_flag; |
128 | int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1 |
129 | int vps_num_hrd_parameters; |
130 | |
131 | uint8_t data[4096]; |
132 | int data_size; |
133 | } HEVCVPS; |
134 | |
135 | typedef struct ScalingList { |
136 | /* This is a little wasteful, since sizeID 0 only needs 8 coeffs, |
137 | * and size ID 3 only has 2 arrays, not 6. */ |
138 | uint8_t sl[4][6][64]; |
139 | uint8_t sl_dc[2][6]; |
140 | } ScalingList; |
141 | |
142 | typedef struct HEVCSPS { |
143 | unsigned vps_id; |
144 | int chroma_format_idc; |
145 | uint8_t separate_colour_plane_flag; |
146 | |
147 | ///< output (i.e. cropped) values |
148 | int output_width, output_height; |
149 | HEVCWindow output_window; |
150 | |
151 | HEVCWindow pic_conf_win; |
152 | |
153 | int bit_depth; |
154 | int pixel_shift; |
155 | enum AVPixelFormat pix_fmt; |
156 | |
157 | unsigned int log2_max_poc_lsb; |
158 | int pcm_enabled_flag; |
159 | |
160 | int max_sub_layers; |
161 | struct { |
162 | int max_dec_pic_buffering; |
163 | int num_reorder_pics; |
164 | int max_latency_increase; |
165 | } temporal_layer[HEVC_MAX_SUB_LAYERS]; |
166 | |
167 | VUI vui; |
168 | PTL ptl; |
169 | |
170 | uint8_t scaling_list_enable_flag; |
171 | ScalingList scaling_list; |
172 | |
173 | unsigned int nb_st_rps; |
174 | ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_RPS_COUNT]; |
175 | |
176 | uint8_t amp_enabled_flag; |
177 | uint8_t sao_enabled; |
178 | |
179 | uint8_t long_term_ref_pics_present_flag; |
180 | uint16_t lt_ref_pic_poc_lsb_sps[32]; |
181 | uint8_t used_by_curr_pic_lt_sps_flag[32]; |
182 | uint8_t num_long_term_ref_pics_sps; |
183 | |
184 | struct { |
185 | uint8_t bit_depth; |
186 | uint8_t bit_depth_chroma; |
187 | unsigned int log2_min_pcm_cb_size; |
188 | unsigned int log2_max_pcm_cb_size; |
189 | uint8_t loop_filter_disable_flag; |
190 | } pcm; |
191 | uint8_t sps_temporal_mvp_enabled_flag; |
192 | uint8_t sps_strong_intra_smoothing_enable_flag; |
193 | |
194 | unsigned int log2_min_cb_size; |
195 | unsigned int log2_diff_max_min_coding_block_size; |
196 | unsigned int log2_min_tb_size; |
197 | unsigned int log2_max_trafo_size; |
198 | unsigned int log2_ctb_size; |
199 | unsigned int log2_min_pu_size; |
200 | |
201 | int max_transform_hierarchy_depth_inter; |
202 | int max_transform_hierarchy_depth_intra; |
203 | |
204 | int transform_skip_rotation_enabled_flag; |
205 | int transform_skip_context_enabled_flag; |
206 | int implicit_rdpcm_enabled_flag; |
207 | int explicit_rdpcm_enabled_flag; |
208 | int intra_smoothing_disabled_flag; |
209 | int persistent_rice_adaptation_enabled_flag; |
210 | |
211 | ///< coded frame dimension in various units |
212 | int width; |
213 | int height; |
214 | int ctb_width; |
215 | int ctb_height; |
216 | int ctb_size; |
217 | int min_cb_width; |
218 | int min_cb_height; |
219 | int min_tb_width; |
220 | int min_tb_height; |
221 | int min_pu_width; |
222 | int min_pu_height; |
223 | int tb_mask; |
224 | |
225 | int hshift[3]; |
226 | int vshift[3]; |
227 | |
228 | int qp_bd_offset; |
229 | |
230 | uint8_t data[4096]; |
231 | int data_size; |
232 | } HEVCSPS; |
233 | |
234 | typedef struct HEVCPPS { |
235 | unsigned int sps_id; ///< seq_parameter_set_id |
236 | |
237 | uint8_t sign_data_hiding_flag; |
238 | |
239 | uint8_t cabac_init_present_flag; |
240 | |
241 | int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1 |
242 | int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1 |
243 | int pic_init_qp_minus26; |
244 | |
245 | uint8_t constrained_intra_pred_flag; |
246 | uint8_t transform_skip_enabled_flag; |
247 | |
248 | uint8_t cu_qp_delta_enabled_flag; |
249 | int diff_cu_qp_delta_depth; |
250 | |
251 | int cb_qp_offset; |
252 | int cr_qp_offset; |
253 | uint8_t pic_slice_level_chroma_qp_offsets_present_flag; |
254 | uint8_t weighted_pred_flag; |
255 | uint8_t weighted_bipred_flag; |
256 | uint8_t output_flag_present_flag; |
257 | uint8_t transquant_bypass_enable_flag; |
258 | |
259 | uint8_t dependent_slice_segments_enabled_flag; |
260 | uint8_t tiles_enabled_flag; |
261 | uint8_t entropy_coding_sync_enabled_flag; |
262 | |
263 | int num_tile_columns; ///< num_tile_columns_minus1 + 1 |
264 | int num_tile_rows; ///< num_tile_rows_minus1 + 1 |
265 | uint8_t uniform_spacing_flag; |
266 | uint8_t loop_filter_across_tiles_enabled_flag; |
267 | |
268 | uint8_t seq_loop_filter_across_slices_enabled_flag; |
269 | |
270 | uint8_t deblocking_filter_control_present_flag; |
271 | uint8_t deblocking_filter_override_enabled_flag; |
272 | uint8_t disable_dbf; |
273 | int beta_offset; ///< beta_offset_div2 * 2 |
274 | int tc_offset; ///< tc_offset_div2 * 2 |
275 | |
276 | uint8_t scaling_list_data_present_flag; |
277 | ScalingList scaling_list; |
278 | |
279 | uint8_t lists_modification_present_flag; |
280 | int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2 |
281 | int num_extra_slice_header_bits; |
282 | uint8_t slice_header_extension_present_flag; |
283 | uint8_t log2_max_transform_skip_block_size; |
284 | uint8_t cross_component_prediction_enabled_flag; |
285 | uint8_t chroma_qp_offset_list_enabled_flag; |
286 | uint8_t diff_cu_chroma_qp_offset_depth; |
287 | uint8_t chroma_qp_offset_list_len_minus1; |
288 | int8_t cb_qp_offset_list[5]; |
289 | int8_t cr_qp_offset_list[5]; |
290 | uint8_t log2_sao_offset_scale_luma; |
291 | uint8_t log2_sao_offset_scale_chroma; |
292 | |
293 | // Inferred parameters |
294 | unsigned int *column_width; ///< ColumnWidth |
295 | unsigned int *row_height; ///< RowHeight |
296 | unsigned int *col_bd; ///< ColBd |
297 | unsigned int *row_bd; ///< RowBd |
298 | int *col_idxX; |
299 | |
300 | int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS |
301 | int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS |
302 | int *tile_id; ///< TileId |
303 | int *tile_pos_rs; ///< TilePosRS |
304 | int *min_tb_addr_zs; ///< MinTbAddrZS |
305 | int *min_tb_addr_zs_tab;///< MinTbAddrZS |
306 | |
307 | uint8_t data[4096]; |
308 | int data_size; |
309 | } HEVCPPS; |
310 | |
311 | typedef struct HEVCParamSets { |
312 | AVBufferRef *vps_list[HEVC_MAX_VPS_COUNT]; |
313 | AVBufferRef *sps_list[HEVC_MAX_SPS_COUNT]; |
314 | AVBufferRef *pps_list[HEVC_MAX_PPS_COUNT]; |
315 | |
316 | /* currently active parameter sets */ |
317 | const HEVCVPS *vps; |
318 | const HEVCSPS *sps; |
319 | const HEVCPPS *pps; |
320 | } HEVCParamSets; |
321 | |
322 | /** |
323 | * Parse the SPS from the bitstream into the provided HEVCSPS struct. |
324 | * |
325 | * @param sps_id the SPS id will be written here |
326 | * @param apply_defdispwin if set 1, the default display window from the VUI |
327 | * will be applied to the video dimensions |
328 | * @param vps_list if non-NULL, this function will validate that the SPS refers |
329 | * to an existing VPS |
330 | */ |
331 | int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id, |
332 | int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx); |
333 | |
334 | int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, |
335 | HEVCParamSets *ps); |
336 | int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, |
337 | HEVCParamSets *ps, int apply_defdispwin); |
338 | int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, |
339 | HEVCParamSets *ps); |
340 | |
341 | int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, |
342 | ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header); |
343 | |
344 | int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id, |
345 | uint8_t *buf, int buf_size); |
346 | |
347 | #endif /* AVCODEC_HEVC_PS_H */ |
348 |