]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_ps.h
mimic: Convert to the new bitstream reader
[ffmpeg] / libavcodec / hevc_ps.h
1 /*
2  * HEVC parameter set parsing
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 } HEVCVPS;
131
132 typedef struct ScalingList {
133     /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
134      * and size ID 3 only has 2 arrays, not 6. */
135     uint8_t sl[4][6][64];
136     uint8_t sl_dc[2][6];
137 } ScalingList;
138
139 typedef struct HEVCSPS {
140     int vps_id;
141     int chroma_format_idc;
142     uint8_t separate_colour_plane_flag;
143
144     ///< output (i.e. cropped) values
145     int output_width, output_height;
146     HEVCWindow output_window;
147
148     HEVCWindow pic_conf_win;
149
150     int bit_depth;
151     int pixel_shift;
152     enum AVPixelFormat pix_fmt;
153
154     unsigned int log2_max_poc_lsb;
155     int pcm_enabled_flag;
156
157     int max_sub_layers;
158     struct {
159         int max_dec_pic_buffering;
160         int num_reorder_pics;
161         int max_latency_increase;
162     } temporal_layer[HEVC_MAX_SUB_LAYERS];
163
164     VUI vui;
165     PTL ptl;
166
167     uint8_t scaling_list_enable_flag;
168     ScalingList scaling_list;
169
170     unsigned int nb_st_rps;
171     ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_RPS_COUNT];
172
173     uint8_t amp_enabled_flag;
174     uint8_t sao_enabled;
175
176     uint8_t long_term_ref_pics_present_flag;
177     uint16_t lt_ref_pic_poc_lsb_sps[32];
178     uint8_t used_by_curr_pic_lt_sps_flag[32];
179     uint8_t num_long_term_ref_pics_sps;
180
181     struct {
182         uint8_t bit_depth;
183         uint8_t bit_depth_chroma;
184         unsigned int log2_min_pcm_cb_size;
185         unsigned int log2_max_pcm_cb_size;
186         uint8_t loop_filter_disable_flag;
187     } pcm;
188     uint8_t sps_temporal_mvp_enabled_flag;
189     uint8_t sps_strong_intra_smoothing_enable_flag;
190
191     unsigned int log2_min_cb_size;
192     unsigned int log2_diff_max_min_coding_block_size;
193     unsigned int log2_min_tb_size;
194     unsigned int log2_max_trafo_size;
195     unsigned int log2_ctb_size;
196     unsigned int log2_min_pu_size;
197
198     int max_transform_hierarchy_depth_inter;
199     int max_transform_hierarchy_depth_intra;
200
201     ///< coded frame dimension in various units
202     int width;
203     int height;
204     int ctb_width;
205     int ctb_height;
206     int ctb_size;
207     int min_cb_width;
208     int min_cb_height;
209     int min_tb_width;
210     int min_tb_height;
211     int min_pu_width;
212     int min_pu_height;
213
214     int hshift[3];
215     int vshift[3];
216
217     int qp_bd_offset;
218 } HEVCSPS;
219
220 typedef struct HEVCPPS {
221     unsigned int sps_id; ///< seq_parameter_set_id
222
223     uint8_t sign_data_hiding_flag;
224
225     uint8_t cabac_init_present_flag;
226
227     int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
228     int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
229     int pic_init_qp_minus26;
230
231     uint8_t constrained_intra_pred_flag;
232     uint8_t transform_skip_enabled_flag;
233
234     uint8_t cu_qp_delta_enabled_flag;
235     int diff_cu_qp_delta_depth;
236
237     int cb_qp_offset;
238     int cr_qp_offset;
239     uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
240     uint8_t weighted_pred_flag;
241     uint8_t weighted_bipred_flag;
242     uint8_t output_flag_present_flag;
243     uint8_t transquant_bypass_enable_flag;
244
245     uint8_t dependent_slice_segments_enabled_flag;
246     uint8_t tiles_enabled_flag;
247     uint8_t entropy_coding_sync_enabled_flag;
248
249     int num_tile_columns;   ///< num_tile_columns_minus1 + 1
250     int num_tile_rows;      ///< num_tile_rows_minus1 + 1
251     uint8_t uniform_spacing_flag;
252     uint8_t loop_filter_across_tiles_enabled_flag;
253
254     uint8_t seq_loop_filter_across_slices_enabled_flag;
255
256     uint8_t deblocking_filter_control_present_flag;
257     uint8_t deblocking_filter_override_enabled_flag;
258     uint8_t disable_dbf;
259     int beta_offset;    ///< beta_offset_div2 * 2
260     int tc_offset;      ///< tc_offset_div2 * 2
261
262     uint8_t scaling_list_data_present_flag;
263     ScalingList scaling_list;
264
265     uint8_t lists_modification_present_flag;
266     int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
267     int num_extra_slice_header_bits;
268     uint8_t slice_header_extension_present_flag;
269
270     // Inferred parameters
271     unsigned int *column_width;  ///< ColumnWidth
272     unsigned int *row_height;    ///< RowHeight
273     unsigned int *col_bd;        ///< ColBd
274     unsigned int *row_bd;        ///< RowBd
275     int *col_idxX;
276
277     int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
278     int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
279     int *tile_id;           ///< TileId
280     int *tile_pos_rs;       ///< TilePosRS
281     int *min_tb_addr_zs;    ///< MinTbAddrZS
282 } HEVCPPS;
283
284 typedef struct HEVCParamSets {
285     AVBufferRef *vps_list[HEVC_MAX_VPS_COUNT];
286     AVBufferRef *sps_list[HEVC_MAX_SPS_COUNT];
287     AVBufferRef *pps_list[HEVC_MAX_PPS_COUNT];
288
289     /* currently active parameter sets */
290     const HEVCVPS *vps;
291     const HEVCSPS *sps;
292     const HEVCPPS *pps;
293 } HEVCParamSets;
294
295 /**
296  * Parse the SPS from the bitstream into the provided HEVCSPS struct.
297  *
298  * @param sps_id the SPS id will be written here
299  * @param apply_defdispwin if set 1, the default display window from the VUI
300  *                         will be applied to the video dimensions
301  * @param vps_list if non-NULL, this function will validate that the SPS refers
302  *                 to an existing VPS
303  */
304 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
305                       int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx);
306
307 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
308                            HEVCParamSets *ps);
309 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
310                            HEVCParamSets *ps, int apply_defdispwin);
311 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
312                            HEVCParamSets *ps);
313
314 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
315                                   ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header);
316
317 int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
318                            uint8_t *buf, int buf_size);
319
320 #endif /* AVCODEC_HEVC_PS_H */