]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs_h264.h
lavc: Add coded bitstream read/write support for AV1
[ffmpeg] / libavcodec / cbs_h264.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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_H264_H
20 #define AVCODEC_CBS_H264_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "cbs.h"
26 #include "cbs_h2645.h"
27 #include "h264.h"
28
29
30 enum {
31     // This limit is arbitrary - it is sufficient for one message of each
32     // type plus some repeats, and will therefore easily cover all sane
33     // streams.  However, it is possible to make technically-valid streams
34     // for which it will fail (for example, by including a large number of
35     // user-data-unregistered messages).
36     H264_MAX_SEI_PAYLOADS = 64,
37 };
38
39
40 typedef struct H264RawNALUnitHeader {
41     uint8_t forbidden_zero_bit;
42     uint8_t nal_ref_idc;
43     uint8_t nal_unit_type;
44
45     uint8_t svc_extension_flag;
46     uint8_t avc_3d_extension_flag;
47 } H264RawNALUnitHeader;
48
49 typedef struct H264RawScalingList {
50     int8_t delta_scale[64];
51 } H264RawScalingList;
52
53 typedef struct H264RawHRD {
54     uint8_t cpb_cnt_minus1;
55     uint8_t bit_rate_scale;
56     uint8_t cpb_size_scale;
57
58     uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT];
59     uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT];
60     uint8_t cbr_flag[H264_MAX_CPB_CNT];
61
62     uint8_t initial_cpb_removal_delay_length_minus1;
63     uint8_t cpb_removal_delay_length_minus1;
64     uint8_t dpb_output_delay_length_minus1;
65     uint8_t time_offset_length;
66 } H264RawHRD;
67
68 typedef struct H264RawVUI {
69     uint8_t aspect_ratio_info_present_flag;
70     uint8_t aspect_ratio_idc;
71     uint16_t sar_width;
72     uint16_t sar_height;
73
74     uint8_t overscan_info_present_flag;
75     uint8_t overscan_appropriate_flag;
76
77     uint8_t video_signal_type_present_flag;
78     uint8_t video_format;
79     uint8_t video_full_range_flag;
80     uint8_t colour_description_present_flag;
81     uint8_t colour_primaries;
82     uint8_t transfer_characteristics;
83     uint8_t matrix_coefficients;
84
85     uint8_t chroma_loc_info_present_flag;
86     uint8_t chroma_sample_loc_type_top_field;
87     uint8_t chroma_sample_loc_type_bottom_field;
88
89     uint8_t timing_info_present_flag;
90     uint32_t num_units_in_tick;
91     uint32_t time_scale;
92     uint8_t fixed_frame_rate_flag;
93
94     uint8_t nal_hrd_parameters_present_flag;
95     H264RawHRD nal_hrd_parameters;
96     uint8_t vcl_hrd_parameters_present_flag;
97     H264RawHRD vcl_hrd_parameters;
98     uint8_t low_delay_hrd_flag;
99
100     uint8_t pic_struct_present_flag;
101
102     uint8_t bitstream_restriction_flag;
103     uint8_t motion_vectors_over_pic_boundaries_flag;
104     uint8_t max_bytes_per_pic_denom;
105     uint8_t max_bits_per_mb_denom;
106     uint8_t log2_max_mv_length_horizontal;
107     uint8_t log2_max_mv_length_vertical;
108     uint8_t max_num_reorder_frames;
109     uint8_t max_dec_frame_buffering;
110 } H264RawVUI;
111
112 typedef struct H264RawSPS {
113     H264RawNALUnitHeader nal_unit_header;
114
115     uint8_t profile_idc;
116     uint8_t constraint_set0_flag;
117     uint8_t constraint_set1_flag;
118     uint8_t constraint_set2_flag;
119     uint8_t constraint_set3_flag;
120     uint8_t constraint_set4_flag;
121     uint8_t constraint_set5_flag;
122     uint8_t reserved_zero_2bits;
123     uint8_t level_idc;
124
125     uint8_t seq_parameter_set_id;
126
127     uint8_t chroma_format_idc;
128     uint8_t separate_colour_plane_flag;
129     uint8_t bit_depth_luma_minus8;
130     uint8_t bit_depth_chroma_minus8;
131     uint8_t qpprime_y_zero_transform_bypass_flag;
132
133     uint8_t seq_scaling_matrix_present_flag;
134     uint8_t seq_scaling_list_present_flag[12];
135     H264RawScalingList scaling_list_4x4[6];
136     H264RawScalingList scaling_list_8x8[6];
137
138     uint8_t log2_max_frame_num_minus4;
139     uint8_t pic_order_cnt_type;
140     uint8_t log2_max_pic_order_cnt_lsb_minus4;
141     uint8_t delta_pic_order_always_zero_flag;
142     int32_t offset_for_non_ref_pic;
143     int32_t offset_for_top_to_bottom_field;
144     uint8_t num_ref_frames_in_pic_order_cnt_cycle;
145     int32_t offset_for_ref_frame[256];
146
147     uint8_t max_num_ref_frames;
148     uint8_t gaps_in_frame_num_allowed_flag;
149
150     uint16_t pic_width_in_mbs_minus1;
151     uint16_t pic_height_in_map_units_minus1;
152
153     uint8_t frame_mbs_only_flag;
154     uint8_t mb_adaptive_frame_field_flag;
155     uint8_t direct_8x8_inference_flag;
156
157     uint8_t frame_cropping_flag;
158     uint16_t frame_crop_left_offset;
159     uint16_t frame_crop_right_offset;
160     uint16_t frame_crop_top_offset;
161     uint16_t frame_crop_bottom_offset;
162
163     uint8_t vui_parameters_present_flag;
164     H264RawVUI vui;
165 } H264RawSPS;
166
167 typedef struct H264RawSPSExtension {
168     H264RawNALUnitHeader nal_unit_header;
169
170     uint8_t seq_parameter_set_id;
171
172     uint8_t aux_format_idc;
173     uint8_t bit_depth_aux_minus8;
174     uint8_t alpha_incr_flag;
175     uint16_t alpha_opaque_value;
176     uint16_t alpha_transparent_value;
177
178     uint8_t additional_extension_flag;
179 } H264RawSPSExtension;
180
181 typedef struct H264RawPPS {
182     H264RawNALUnitHeader nal_unit_header;
183
184     uint8_t pic_parameter_set_id;
185     uint8_t seq_parameter_set_id;
186
187     uint8_t entropy_coding_mode_flag;
188     uint8_t bottom_field_pic_order_in_frame_present_flag;
189
190     uint8_t num_slice_groups_minus1;
191     uint8_t slice_group_map_type;
192     uint16_t run_length_minus1[H264_MAX_SLICE_GROUPS];
193     uint16_t top_left[H264_MAX_SLICE_GROUPS];
194     uint16_t bottom_right[H264_MAX_SLICE_GROUPS];
195     uint8_t slice_group_change_direction_flag;
196     uint16_t slice_group_change_rate_minus1;
197     uint16_t pic_size_in_map_units_minus1;
198
199     uint8_t *slice_group_id;
200     AVBufferRef *slice_group_id_ref;
201
202     uint8_t num_ref_idx_l0_default_active_minus1;
203     uint8_t num_ref_idx_l1_default_active_minus1;
204
205     uint8_t weighted_pred_flag;
206     uint8_t weighted_bipred_idc;
207
208     int8_t pic_init_qp_minus26;
209     int8_t pic_init_qs_minus26;
210     int8_t chroma_qp_index_offset;
211
212     uint8_t deblocking_filter_control_present_flag;
213     uint8_t constrained_intra_pred_flag;
214
215     uint8_t more_rbsp_data;
216
217     uint8_t redundant_pic_cnt_present_flag;
218     uint8_t transform_8x8_mode_flag;
219
220     uint8_t pic_scaling_matrix_present_flag;
221     uint8_t pic_scaling_list_present_flag[12];
222     H264RawScalingList scaling_list_4x4[6];
223     H264RawScalingList scaling_list_8x8[6];
224
225     int8_t second_chroma_qp_index_offset;
226 } H264RawPPS;
227
228 typedef struct H264RawAUD {
229     H264RawNALUnitHeader nal_unit_header;
230
231     uint8_t primary_pic_type;
232 } H264RawAUD;
233
234 typedef struct H264RawSEIBufferingPeriod {
235     uint8_t seq_parameter_set_id;
236     struct {
237         uint32_t initial_cpb_removal_delay[H264_MAX_CPB_CNT];
238         uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT];
239     } nal, vcl;
240 } H264RawSEIBufferingPeriod;
241
242 typedef struct H264RawSEIPicTimestamp {
243     uint8_t ct_type;
244     uint8_t nuit_field_based_flag;
245     uint8_t counting_type;
246     uint8_t full_timestamp_flag;
247     uint8_t discontinuity_flag;
248     uint8_t cnt_dropped_flag;
249     uint8_t n_frames;
250     uint8_t seconds_flag;
251     uint8_t seconds_value;
252     uint8_t minutes_flag;
253     uint8_t minutes_value;
254     uint8_t hours_flag;
255     uint8_t hours_value;
256     uint32_t time_offset;
257 } H264RawSEIPicTimestamp;
258
259 typedef struct H264RawSEIPicTiming {
260     uint32_t cpb_removal_delay;
261     uint32_t dpb_output_delay;
262     uint8_t pic_struct;
263     uint8_t clock_timestamp_flag[3];
264     H264RawSEIPicTimestamp timestamp[3];
265 } H264RawSEIPicTiming;
266
267 typedef struct H264RawSEIPanScanRect {
268     uint32_t pan_scan_rect_id;
269     uint8_t  pan_scan_rect_cancel_flag;
270     uint8_t  pan_scan_cnt_minus1;
271     int32_t  pan_scan_rect_left_offset[3];
272     int32_t  pan_scan_rect_right_offset[3];
273     int32_t  pan_scan_rect_top_offset[3];
274     int32_t  pan_scan_rect_bottom_offset[3];
275     uint16_t pan_scan_rect_repetition_period;
276 } H264RawSEIPanScanRect;
277
278 typedef struct H264RawSEIUserDataRegistered {
279     uint8_t itu_t_t35_country_code;
280     uint8_t itu_t_t35_country_code_extension_byte;
281     uint8_t *data;
282     size_t data_length;
283     AVBufferRef *data_ref;
284 } H264RawSEIUserDataRegistered;
285
286 typedef struct H264RawSEIUserDataUnregistered {
287     uint8_t uuid_iso_iec_11578[16];
288     uint8_t *data;
289     size_t data_length;
290     AVBufferRef *data_ref;
291 } H264RawSEIUserDataUnregistered;
292
293 typedef struct H264RawSEIRecoveryPoint {
294     uint16_t recovery_frame_cnt;
295     uint8_t exact_match_flag;
296     uint8_t broken_link_flag;
297     uint8_t changing_slice_group_idc;
298 } H264RawSEIRecoveryPoint;
299
300 typedef struct H264RawSEIDisplayOrientation {
301     uint8_t display_orientation_cancel_flag;
302     uint8_t hor_flip;
303     uint8_t ver_flip;
304     uint16_t anticlockwise_rotation;
305     uint16_t display_orientation_repetition_period;
306     uint8_t display_orientation_extension_flag;
307 } H264RawSEIDisplayOrientation;
308
309 typedef struct H264RawSEIMasteringDisplayColourVolume {
310     uint16_t display_primaries_x[3];
311     uint16_t display_primaries_y[3];
312     uint16_t white_point_x;
313     uint16_t white_point_y;
314     uint32_t max_display_mastering_luminance;
315     uint32_t min_display_mastering_luminance;
316 } H264RawSEIMasteringDisplayColourVolume;
317
318 typedef struct H264RawSEIPayload {
319     uint32_t payload_type;
320     uint32_t payload_size;
321     union {
322         H264RawSEIBufferingPeriod buffering_period;
323         H264RawSEIPicTiming pic_timing;
324         H264RawSEIPanScanRect pan_scan_rect;
325         // H264RawSEIFiller filler -> no fields.
326         H264RawSEIUserDataRegistered user_data_registered;
327         H264RawSEIUserDataUnregistered user_data_unregistered;
328         H264RawSEIRecoveryPoint recovery_point;
329         H264RawSEIDisplayOrientation display_orientation;
330         H264RawSEIMasteringDisplayColourVolume mastering_display_colour_volume;
331         struct {
332             uint8_t *data;
333             size_t data_length;
334             AVBufferRef *data_ref;
335         } other;
336     } payload;
337 } H264RawSEIPayload;
338
339 typedef struct H264RawSEI {
340     H264RawNALUnitHeader nal_unit_header;
341
342     H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS];
343     uint8_t payload_count;
344 } H264RawSEI;
345
346 typedef struct H264RawSliceHeader {
347     H264RawNALUnitHeader nal_unit_header;
348
349     uint32_t first_mb_in_slice;
350     uint8_t slice_type;
351
352     uint8_t pic_parameter_set_id;
353
354     uint8_t colour_plane_id;
355
356     uint16_t frame_num;
357     uint8_t field_pic_flag;
358     uint8_t bottom_field_flag;
359
360     uint16_t idr_pic_id;
361
362     uint16_t pic_order_cnt_lsb;
363     int32_t delta_pic_order_cnt_bottom;
364     int32_t delta_pic_order_cnt[2];
365
366     uint8_t redundant_pic_cnt;
367     uint8_t direct_spatial_mv_pred_flag;
368
369     uint8_t num_ref_idx_active_override_flag;
370     uint8_t num_ref_idx_l0_active_minus1;
371     uint8_t num_ref_idx_l1_active_minus1;
372
373     uint8_t ref_pic_list_modification_flag_l0;
374     uint8_t ref_pic_list_modification_flag_l1;
375     struct {
376         uint8_t modification_of_pic_nums_idc;
377         int32_t abs_diff_pic_num_minus1;
378         uint8_t long_term_pic_num;
379     } rplm_l0[H264_MAX_RPLM_COUNT], rplm_l1[H264_MAX_RPLM_COUNT];
380
381     uint8_t luma_log2_weight_denom;
382     uint8_t chroma_log2_weight_denom;
383
384     uint8_t luma_weight_l0_flag[H264_MAX_REFS];
385     int8_t luma_weight_l0[H264_MAX_REFS];
386     int8_t luma_offset_l0[H264_MAX_REFS];
387     uint8_t chroma_weight_l0_flag[H264_MAX_REFS];
388     int8_t chroma_weight_l0[H264_MAX_REFS][2];
389     int8_t chroma_offset_l0[H264_MAX_REFS][2];
390
391     uint8_t luma_weight_l1_flag[H264_MAX_REFS];
392     int8_t luma_weight_l1[H264_MAX_REFS];
393     int8_t luma_offset_l1[H264_MAX_REFS];
394     uint8_t chroma_weight_l1_flag[H264_MAX_REFS];
395     int8_t chroma_weight_l1[H264_MAX_REFS][2];
396     int8_t chroma_offset_l1[H264_MAX_REFS][2];
397
398     uint8_t no_output_of_prior_pics_flag;
399     uint8_t long_term_reference_flag;
400
401     uint8_t adaptive_ref_pic_marking_mode_flag;
402     struct {
403         uint8_t memory_management_control_operation;
404         int32_t difference_of_pic_nums_minus1;
405         uint8_t long_term_pic_num;
406         uint8_t long_term_frame_idx;
407         uint8_t max_long_term_frame_idx_plus1;
408     } mmco[H264_MAX_MMCO_COUNT];
409
410     uint8_t cabac_init_idc;
411
412     int8_t slice_qp_delta;
413
414     uint8_t sp_for_switch_flag;
415     int8_t slice_qs_delta;
416
417     uint8_t disable_deblocking_filter_idc;
418     int8_t slice_alpha_c0_offset_div2;
419     int8_t slice_beta_offset_div2;
420
421     uint16_t slice_group_change_cycle;
422 } H264RawSliceHeader;
423
424 typedef struct H264RawSlice {
425     H264RawSliceHeader header;
426
427     uint8_t *data;
428     size_t   data_size;
429     int      data_bit_start;
430     AVBufferRef *data_ref;
431 } H264RawSlice;
432
433 typedef struct H264RawFiller {
434     H264RawNALUnitHeader nal_unit_header;
435
436     uint32_t filler_size;
437 } H264RawFiller;
438
439
440 typedef struct CodedBitstreamH264Context {
441     // Reader/writer context in common with the H.265 implementation.
442     CodedBitstreamH2645Context common;
443
444     // All currently available parameter sets.  These are updated when
445     // any parameter set NAL unit is read/written with this context.
446     AVBufferRef *sps_ref[H264_MAX_SPS_COUNT];
447     AVBufferRef *pps_ref[H264_MAX_PPS_COUNT];
448     H264RawSPS *sps[H264_MAX_SPS_COUNT];
449     H264RawPPS *pps[H264_MAX_PPS_COUNT];
450
451     // The currently active parameter sets.  These are updated when any
452     // NAL unit refers to the relevant parameter set.  These pointers
453     // must also be present in the arrays above.
454     const H264RawSPS *active_sps;
455     const H264RawPPS *active_pps;
456
457     // The NAL unit type of the most recent normal slice.  This is required
458     // to be able to read/write auxiliary slices, because IdrPicFlag is
459     // otherwise unknown.
460     uint8_t last_slice_nal_unit_type;
461 } CodedBitstreamH264Context;
462
463
464 /**
465  * Add an SEI message to an access unit.
466  */
467 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
468                                 CodedBitstreamFragment *access_unit,
469                                 const H264RawSEIPayload *payload);
470
471 /**
472  * Delete an SEI message from an access unit.
473  *
474  * Deletes from nal_unit, which must be an SEI NAL unit.  If this is the
475  * last message in nal_unit, also deletes it from access_unit.
476  */
477 int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
478                                    CodedBitstreamFragment *access_unit,
479                                    CodedBitstreamUnit *nal_unit,
480                                    int position);
481
482 #endif /* AVCODEC_CBS_H264_H */