lavc: Add coded bitstream read/write support for H.265
[libav.git] / libavcodec / cbs_h265.h
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #ifndef AVCODEC_CBS_H265_H
20 #define AVCODEC_CBS_H265_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "cbs_h2645.h"
26 #include "hevc.h"
27
28
29 typedef struct H265RawNALUnitHeader {
30 uint8_t forbidden_zero_bit;
31 uint8_t nal_unit_type;
32 uint8_t nuh_layer_id;
33 uint8_t nuh_temporal_id_plus1;
34 } H265RawNALUnitHeader;
35
36 typedef struct H265RawProfileTierLevel {
37 uint8_t general_profile_space;
38 uint8_t general_tier_flag;
39 uint8_t general_profile_idc;
40
41 uint8_t general_profile_compatibility_flag[32];
42
43 uint8_t general_progressive_source_flag;
44 uint8_t general_interlaced_source_flag;
45 uint8_t general_non_packed_constraint_flag;
46 uint8_t general_frame_only_constraint_flag;
47
48 uint8_t general_max_12bit_constraint_flag;
49 uint8_t general_max_10bit_constraint_flag;
50 uint8_t general_max_8bit_constraint_flag;
51 uint8_t general_max_422chroma_constraint_flag;
52 uint8_t general_max_420chroma_constraint_flag;
53 uint8_t general_max_monochrome_constraint_flag;
54 uint8_t general_intra_constraint_flag;
55 uint8_t general_one_picture_only_constraint_flag;
56 uint8_t general_lower_bit_rate_constraint_flag;
57 uint8_t general_max_14bit_constraint_flag;
58
59 uint8_t general_inbld_flag;
60
61 uint8_t general_level_idc;
62
63 uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
64 uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
65
66 // TODO: much of that again for each sub-layer.
67 } H265RawProfileTierLevel;
68
69 typedef struct H265RawSubLayerHRDParameters {
70 uint32_t bit_rate_value_minus1[HEVC_MAX_CPB_CNT];
71 uint32_t cpb_size_value_minus1[HEVC_MAX_CPB_CNT];
72 uint32_t cpb_size_du_value_minus1[HEVC_MAX_CPB_CNT];
73 uint32_t bit_rate_du_value_minus1[HEVC_MAX_CPB_CNT];
74 uint8_t cbr_flag[HEVC_MAX_CPB_CNT];
75 } H265RawSubLayerHRDParameters;
76
77 typedef struct H265RawHRDParameters {
78 uint8_t nal_hrd_parameters_present_flag;
79 uint8_t vcl_hrd_parameters_present_flag;
80
81 uint8_t sub_pic_hrd_params_present_flag;
82 uint8_t tick_divisor_minus2;
83 uint8_t du_cpb_removal_delay_increment_length_minus1;
84 uint8_t sub_pic_cpb_params_in_pic_timing_sei_flag;
85 uint8_t dpb_output_delay_du_length_minus1;
86
87 uint8_t bit_rate_scale;
88 uint8_t cpb_size_scale;
89 uint8_t cpb_size_du_scale;
90
91 uint8_t initial_cpb_removal_delay_length_minus1;
92 uint8_t au_cpb_removal_delay_length_minus1;
93 uint8_t dpb_output_delay_length_minus1;
94
95 uint8_t fixed_pic_rate_general_flag[HEVC_MAX_SUB_LAYERS];
96 uint8_t fixed_pic_rate_within_cvs_flag[HEVC_MAX_SUB_LAYERS];
97 uint16_t elemental_duration_in_tc_minus1[HEVC_MAX_SUB_LAYERS];
98 uint8_t low_delay_hrd_flag[HEVC_MAX_SUB_LAYERS];
99 uint8_t cpb_cnt_minus1[HEVC_MAX_SUB_LAYERS];
100 H265RawSubLayerHRDParameters nal_sub_layer_hrd_parameters[HEVC_MAX_SUB_LAYERS];
101 H265RawSubLayerHRDParameters vcl_sub_layer_hrd_parameters[HEVC_MAX_SUB_LAYERS];
102 } H265RawHRDParameters;
103
104 typedef struct H265RawVUI {
105 uint8_t aspect_ratio_info_present_flag;
106 uint8_t aspect_ratio_idc;
107 uint16_t sar_width;
108 uint16_t sar_height;
109
110 uint8_t overscan_info_present_flag;
111 uint8_t overscan_appropriate_flag;
112
113 uint8_t video_signal_type_present_flag;
114 uint8_t video_format;
115 uint8_t video_full_range_flag;
116 uint8_t colour_description_present_flag;
117 uint8_t colour_primaries;
118 uint8_t transfer_characteristics;
119 uint8_t matrix_coefficients;
120
121 uint8_t chroma_loc_info_present_flag;
122 uint8_t chroma_sample_loc_type_top_field;
123 uint8_t chroma_sample_loc_type_bottom_field;
124
125 uint8_t neutral_chroma_indication_flag;
126 uint8_t field_seq_flag;
127 uint8_t frame_field_info_present_flag;
128
129 uint8_t default_display_window_flag;
130 uint16_t def_disp_win_left_offset;
131 uint16_t def_disp_win_right_offset;
132 uint16_t def_disp_win_top_offset;
133 uint16_t def_disp_win_bottom_offset;
134
135 uint8_t vui_timing_info_present_flag;
136 uint32_t vui_num_units_in_tick;
137 uint32_t vui_time_scale;
138 uint8_t vui_poc_proportional_to_timing_flag;
139 uint32_t vui_num_ticks_poc_diff_one_minus1;
140 uint8_t vui_hrd_parameters_present_flag;
141 H265RawHRDParameters hrd_parameters;
142
143 uint8_t bitstream_restriction_flag;
144 uint8_t tiles_fixed_structure_flag;
145 uint8_t motion_vectors_over_pic_boundaries_flag;
146 uint8_t restricted_ref_pic_lists_flag;
147 uint16_t min_spatial_segmentation_idc;
148 uint8_t max_bytes_per_pic_denom;
149 uint8_t max_bits_per_min_cu_denom;
150 uint8_t log2_max_mv_length_horizontal;
151 uint8_t log2_max_mv_length_vertical;
152 } H265RawVUI;
153
154 typedef struct H265RawPSExtensionData {
155 uint8_t *data;
156 size_t bit_length;
157 } H265RawPSExtensionData;
158
159 typedef struct H265RawVPS {
160 H265RawNALUnitHeader nal_unit_header;
161
162 uint8_t vps_video_parameter_set_id;
163
164 uint8_t vps_base_layer_internal_flag;
165 uint8_t vps_base_layer_available_flag;
166 uint8_t vps_max_layers_minus1;
167 uint8_t vps_max_sub_layers_minus1;
168 uint8_t vps_temporal_id_nesting_flag;
169
170 H265RawProfileTierLevel profile_tier_level;
171
172 uint8_t vps_sub_layer_ordering_info_present_flag;
173 uint8_t vps_max_dec_pic_buffering_minus1[HEVC_MAX_SUB_LAYERS];
174 uint8_t vps_max_num_reorder_pics[HEVC_MAX_SUB_LAYERS];
175 uint32_t vps_max_latency_increase_plus1[HEVC_MAX_SUB_LAYERS];
176
177 uint8_t vps_max_layer_id;
178 uint16_t vps_num_layer_sets_minus1;
179 uint8_t layer_id_included_flag[HEVC_MAX_LAYER_SETS][HEVC_MAX_LAYERS];
180
181 uint8_t vps_timing_info_present_flag;
182 uint32_t vps_num_units_in_tick;
183 uint32_t vps_time_scale;
184 uint8_t vps_poc_proportional_to_timing_flag;
185 uint32_t vps_num_ticks_poc_diff_one_minus1;
186 uint16_t vps_num_hrd_parameters;
187 uint16_t hrd_layer_set_idx[HEVC_MAX_LAYER_SETS];
188 uint8_t cprms_present_flag[HEVC_MAX_LAYER_SETS];
189 H265RawHRDParameters hrd_parameters[HEVC_MAX_LAYER_SETS];
190
191 uint8_t vps_extension_flag;
192 H265RawPSExtensionData extension_data;
193 } H265RawVPS;
194
195 typedef struct H265RawSTRefPicSet {
196 uint8_t inter_ref_pic_set_prediction_flag;
197
198 uint8_t delta_idx_minus1;
199 uint8_t delta_rps_sign;
200 uint16_t abs_delta_rps_minus1;
201
202 uint8_t used_by_curr_pic_flag[HEVC_MAX_REFS];
203 uint8_t use_delta_flag[HEVC_MAX_REFS];
204
205 uint8_t num_negative_pics;
206 uint8_t num_positive_pics;
207 uint16_t delta_poc_s0_minus1[HEVC_MAX_REFS];
208 uint8_t used_by_curr_pic_s0_flag[HEVC_MAX_REFS];
209 uint16_t delta_poc_s1_minus1[HEVC_MAX_REFS];
210 uint8_t used_by_curr_pic_s1_flag[HEVC_MAX_REFS];
211 } H265RawSTRefPicSet;
212
213 typedef struct H265RawScalingList {
214 uint8_t scaling_list_pred_mode_flag[4][6];
215 uint8_t scaling_list_pred_matrix_id_delta[4][6];
216 int16_t scaling_list_dc_coef_minus8[4][6];
217 int8_t scaling_list_delta_coeff[4][6][64];
218 } H265RawScalingList;
219
220 typedef struct H265RawSPS {
221 H265RawNALUnitHeader nal_unit_header;
222
223 uint8_t sps_video_parameter_set_id;
224
225 uint8_t sps_max_sub_layers_minus1;
226 uint8_t sps_temporal_id_nesting_flag;
227
228 H265RawProfileTierLevel profile_tier_level;
229
230 uint8_t sps_seq_parameter_set_id;
231
232 uint8_t chroma_format_idc;
233 uint8_t separate_colour_plane_flag;
234
235 uint16_t pic_width_in_luma_samples;
236 uint16_t pic_height_in_luma_samples;
237
238 uint8_t conformance_window_flag;
239 uint16_t conf_win_left_offset;
240 uint16_t conf_win_right_offset;
241 uint16_t conf_win_top_offset;
242 uint16_t conf_win_bottom_offset;
243
244 uint8_t bit_depth_luma_minus8;
245 uint8_t bit_depth_chroma_minus8;
246
247 uint8_t log2_max_pic_order_cnt_lsb_minus4;
248
249 uint8_t sps_sub_layer_ordering_info_present_flag;
250 uint8_t sps_max_dec_pic_buffering_minus1[HEVC_MAX_SUB_LAYERS];
251 uint8_t sps_max_num_reorder_pics[HEVC_MAX_SUB_LAYERS];
252 uint32_t sps_max_latency_increase_plus1[HEVC_MAX_SUB_LAYERS];
253
254 uint8_t log2_min_luma_coding_block_size_minus3;
255 uint8_t log2_diff_max_min_luma_coding_block_size;
256 uint8_t log2_min_luma_transform_block_size_minus2;
257 uint8_t log2_diff_max_min_luma_transform_block_size;
258 uint8_t max_transform_hierarchy_depth_inter;
259 uint8_t max_transform_hierarchy_depth_intra;
260
261 uint8_t scaling_list_enabled_flag;
262 uint8_t sps_scaling_list_data_present_flag;
263 H265RawScalingList scaling_list;
264
265 uint8_t amp_enabled_flag;
266 uint8_t sample_adaptive_offset_enabled_flag;
267
268 uint8_t pcm_enabled_flag;
269 uint8_t pcm_sample_bit_depth_luma_minus1;
270 uint8_t pcm_sample_bit_depth_chroma_minus1;
271 uint8_t log2_min_pcm_luma_coding_block_size_minus3;
272 uint8_t log2_diff_max_min_pcm_luma_coding_block_size;
273 uint8_t pcm_loop_filter_disabled_flag;
274
275 uint8_t num_short_term_ref_pic_sets;
276 H265RawSTRefPicSet st_ref_pic_set[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
277
278 uint8_t long_term_ref_pics_present_flag;
279 uint8_t num_long_term_ref_pics_sps;
280 uint16_t lt_ref_pic_poc_lsb_sps[HEVC_MAX_LONG_TERM_REF_PICS];
281 uint8_t used_by_curr_pic_lt_sps_flag[HEVC_MAX_LONG_TERM_REF_PICS];
282
283 uint8_t sps_temporal_mvp_enabled_flag;
284 uint8_t strong_intra_smoothing_enabled_flag;
285
286 uint8_t vui_parameters_present_flag;
287 H265RawVUI vui;
288
289 uint8_t sps_extension_present_flag;
290 uint8_t sps_range_extension_flag;
291 uint8_t sps_multilayer_extension_flag;
292 uint8_t sps_3d_extension_flag;
293 uint8_t sps_scc_extension_flag;
294 uint8_t sps_extension_4bits;
295
296 H265RawPSExtensionData extension_data;
297
298 // Range extension.
299 uint8_t transform_skip_rotation_enabled_flag;
300 uint8_t transform_skip_context_enabled_flag;
301 uint8_t implicit_rdpcm_enabled_flag;
302 uint8_t explicit_rdpcm_enabled_flag;
303 uint8_t extended_precision_processing_flag;
304 uint8_t intra_smoothing_disabled_flag;
305 uint8_t high_precision_offsets_enabled_flag;
306 uint8_t persistent_rice_adaptation_enabled_flag;
307 uint8_t cabac_bypass_alignment_enabled_flag;
308
309 // Screen content coding extension.
310 uint8_t sps_curr_pic_ref_enabled_flag;
311 uint8_t palette_mode_enabled_flag;
312 uint8_t palette_max_size;
313 uint8_t delta_palette_max_predictor_size;
314 uint8_t sps_palette_predictor_initializer_present_flag;
315 uint8_t sps_num_palette_predictor_initializer_minus1;
316 uint16_t sps_palette_predictor_initializers[3][128];
317
318 uint8_t motion_vector_resolution_control_idc;
319 uint8_t intra_boundary_filtering_disable_flag;
320 } H265RawSPS;
321
322 typedef struct H265RawPPS {
323 H265RawNALUnitHeader nal_unit_header;
324
325 uint8_t pps_pic_parameter_set_id;
326 uint8_t pps_seq_parameter_set_id;
327
328 uint8_t dependent_slice_segments_enabled_flag;
329 uint8_t output_flag_present_flag;
330 uint8_t num_extra_slice_header_bits;
331 uint8_t sign_data_hiding_enabled_flag;
332 uint8_t cabac_init_present_flag;
333
334 uint8_t num_ref_idx_l0_default_active_minus1;
335 uint8_t num_ref_idx_l1_default_active_minus1;
336
337 int8_t init_qp_minus26;
338
339 uint8_t constrained_intra_pred_flag;
340 uint8_t transform_skip_enabled_flag;
341 uint8_t cu_qp_delta_enabled_flag;
342 uint8_t diff_cu_qp_delta_depth;
343
344 int8_t pps_cb_qp_offset;
345 int8_t pps_cr_qp_offset;
346 uint8_t pps_slice_chroma_qp_offsets_present_flag;
347
348 uint8_t weighted_pred_flag;
349 uint8_t weighted_bipred_flag;
350
351 uint8_t transquant_bypass_enabled_flag;
352 uint8_t tiles_enabled_flag;
353 uint8_t entropy_coding_sync_enabled_flag;
354
355 uint8_t num_tile_columns_minus1;
356 uint8_t num_tile_rows_minus1;
357 uint8_t uniform_spacing_flag;
358 uint16_t column_width_minus1[HEVC_MAX_TILE_COLUMNS];
359 uint16_t row_height_minus1[HEVC_MAX_TILE_ROWS];
360 uint8_t loop_filter_across_tiles_enabled_flag;
361
362 uint8_t pps_loop_filter_across_slices_enabled_flag;
363 uint8_t deblocking_filter_control_present_flag;
364 uint8_t deblocking_filter_override_enabled_flag;
365 uint8_t pps_deblocking_filter_disabled_flag;
366 int8_t pps_beta_offset_div2;
367 int8_t pps_tc_offset_div2;
368
369 uint8_t pps_scaling_list_data_present_flag;
370 H265RawScalingList scaling_list;
371
372 uint8_t lists_modification_present_flag;
373 uint8_t log2_parallel_merge_level_minus2;
374
375 uint8_t slice_segment_header_extension_present_flag;
376
377 uint8_t pps_extension_present_flag;
378 uint8_t pps_range_extension_flag;
379 uint8_t pps_multilayer_extension_flag;
380 uint8_t pps_3d_extension_flag;
381 uint8_t pps_scc_extension_flag;
382 uint8_t pps_extension_4bits;
383
384 H265RawPSExtensionData extension_data;
385
386 // Range extension.
387 uint8_t log2_max_transform_skip_block_size_minus2;
388 uint8_t cross_component_prediction_enabled_flag;
389 uint8_t chroma_qp_offset_list_enabled_flag;
390 uint8_t diff_cu_chroma_qp_offset_depth;
391 uint8_t chroma_qp_offset_list_len_minus1;
392 int8_t cb_qp_offset_list[6];
393 int8_t cr_qp_offset_list[6];
394 uint8_t log2_sao_offset_scale_luma;
395 uint8_t log2_sao_offset_scale_chroma;
396
397 // Screen content coding extension.
398 uint8_t pps_curr_pic_ref_enabled_flag;
399 uint8_t residual_adaptive_colour_transform_enabled_flag;
400 uint8_t pps_slice_act_qp_offsets_present_flag;
401 int8_t pps_act_y_qp_offset_plus5;
402 int8_t pps_act_cb_qp_offset_plus5;
403 int8_t pps_act_cr_qp_offset_plus3;
404
405 uint8_t pps_palette_predictor_initializer_present_flag;
406 uint8_t pps_num_palette_predictor_initializer;
407 uint8_t monochrome_palette_flag;
408 uint8_t luma_bit_depth_entry_minus8;
409 uint8_t chroma_bit_depth_entry_minus8;
410 uint16_t pps_palette_predictor_initializers[3][128];
411 } H265RawPPS;
412
413 typedef struct H265RawAUD {
414 H265RawNALUnitHeader nal_unit_header;
415
416 uint8_t pic_type;
417 } H265RawAUD;
418
419 typedef struct H265RawSliceHeader {
420 H265RawNALUnitHeader nal_unit_header;
421
422 uint8_t first_slice_segment_in_pic_flag;
423 uint8_t no_output_of_prior_pics_flag;
424 uint8_t slice_pic_parameter_set_id;
425
426 uint8_t dependent_slice_segment_flag;
427 uint16_t slice_segment_address;
428
429 uint8_t slice_reserved_flag[8];
430 uint8_t slice_type;
431
432 uint8_t pic_output_flag;
433 uint8_t colour_plane_id;
434
435 uint16_t slice_pic_order_cnt_lsb;
436
437 uint8_t short_term_ref_pic_set_sps_flag;
438 H265RawSTRefPicSet short_term_ref_pic_set;
439 uint8_t short_term_ref_pic_set_idx;
440
441 uint8_t num_long_term_sps;
442 uint8_t num_long_term_pics;
443 uint8_t lt_idx_sps[HEVC_MAX_REFS];
444 uint8_t poc_lsb_lt[HEVC_MAX_REFS];
445 uint8_t used_by_curr_pic_lt_flag[HEVC_MAX_REFS];
446 uint8_t delta_poc_msb_present_flag[HEVC_MAX_REFS];
447 uint32_t delta_poc_msb_cycle_lt[HEVC_MAX_REFS];
448
449 uint8_t slice_temporal_mvp_enabled_flag;
450
451 uint8_t slice_sao_luma_flag;
452 uint8_t slice_sao_chroma_flag;
453
454 uint8_t num_ref_idx_active_override_flag;
455 uint8_t num_ref_idx_l0_active_minus1;
456 uint8_t num_ref_idx_l1_active_minus1;
457
458 uint8_t ref_pic_list_modification_flag_l0;
459 uint8_t list_entry_l0[HEVC_MAX_REFS];
460 uint8_t ref_pic_list_modification_flag_l1;
461 uint8_t list_entry_l1[HEVC_MAX_REFS];
462
463 uint8_t mvd_l1_zero_flag;
464 uint8_t cabac_init_flag;
465 uint8_t collocated_from_l0_flag;
466 uint8_t collocated_ref_idx;
467
468 uint8_t luma_log2_weight_denom;
469 int8_t delta_chroma_log2_weight_denom;
470 uint8_t luma_weight_l0_flag[HEVC_MAX_REFS];
471 uint8_t chroma_weight_l0_flag[HEVC_MAX_REFS];
472 int8_t delta_luma_weight_l0[HEVC_MAX_REFS];
473 int16_t luma_offset_l0[HEVC_MAX_REFS];
474 int8_t delta_chroma_weight_l0[HEVC_MAX_REFS][2];
475 int16_t chroma_offset_l0[HEVC_MAX_REFS][2];
476 uint8_t luma_weight_l1_flag[HEVC_MAX_REFS];
477 uint8_t chroma_weight_l1_flag[HEVC_MAX_REFS];
478 int8_t delta_luma_weight_l1[HEVC_MAX_REFS];
479 int16_t luma_offset_l1[HEVC_MAX_REFS];
480 int8_t delta_chroma_weight_l1[HEVC_MAX_REFS][2];
481 int16_t chroma_offset_l1[HEVC_MAX_REFS][2];
482
483 uint8_t five_minus_max_num_merge_cand;
484 uint8_t use_integer_mv_flag;
485
486 int8_t slice_qp_delta;
487 int8_t slice_cb_qp_offset;
488 int8_t slice_cr_qp_offset;
489 int8_t slice_act_y_qp_offset;
490 int8_t slice_act_cb_qp_offset;
491 int8_t slice_act_cr_qp_offset;
492 uint8_t cu_chroma_qp_offset_enabled_flag;
493
494 uint8_t deblocking_filter_override_flag;
495 uint8_t slice_deblocking_filter_disabled_flag;
496 int8_t slice_beta_offset_div2;
497 int8_t slice_tc_offset_div2;
498 uint8_t slice_loop_filter_across_slices_enabled_flag;
499
500 uint16_t num_entry_point_offsets;
501 uint8_t offset_len_minus1;
502 uint32_t entry_point_offset_minus1[HEVC_MAX_ENTRY_POINT_OFFSETS];
503
504 uint16_t slice_segment_header_extension_length;
505 uint8_t slice_segment_header_extension_data_byte[256];
506 } H265RawSliceHeader;
507
508
509 typedef struct H265RawSlice {
510 H265RawSliceHeader header;
511
512 uint8_t *data;
513 size_t data_size;
514 int data_bit_start;
515 } H265RawSlice;
516
517
518 typedef struct CodedBitstreamH265Context {
519 // Reader/writer context in common with the H.264 implementation.
520 CodedBitstreamH2645Context common;
521
522 // All currently available parameter sets. These are updated when
523 // any parameter set NAL unit is read/written with this context.
524 H265RawVPS *vps[HEVC_MAX_VPS_COUNT];
525 H265RawSPS *sps[HEVC_MAX_SPS_COUNT];
526 H265RawPPS *pps[HEVC_MAX_PPS_COUNT];
527
528 // The currently active parameter sets. These are updated when any
529 // NAL unit refers to the relevant parameter set. These pointers
530 // must also be present in the arrays above.
531 const H265RawVPS *active_vps;
532 const H265RawSPS *active_sps;
533 const H265RawPPS *active_pps;
534 } CodedBitstreamH265Context;
535
536
537 #endif /* AVCODEC_CBS_H265_H */