• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cbs_sei.h"
28 #include "h264.h"
29 
30 
31 typedef struct H264RawNALUnitHeader {
32     uint8_t nal_ref_idc;
33     uint8_t nal_unit_type;
34 
35     uint8_t svc_extension_flag;
36     uint8_t avc_3d_extension_flag;
37 } H264RawNALUnitHeader;
38 
39 typedef struct H264RawScalingList {
40     int8_t delta_scale[64];
41 } H264RawScalingList;
42 
43 typedef struct H264RawHRD {
44     uint8_t cpb_cnt_minus1;
45     uint8_t bit_rate_scale;
46     uint8_t cpb_size_scale;
47 
48     uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT];
49     uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT];
50     uint8_t cbr_flag[H264_MAX_CPB_CNT];
51 
52     uint8_t initial_cpb_removal_delay_length_minus1;
53     uint8_t cpb_removal_delay_length_minus1;
54     uint8_t dpb_output_delay_length_minus1;
55     uint8_t time_offset_length;
56 } H264RawHRD;
57 
58 typedef struct H264RawVUI {
59     uint8_t aspect_ratio_info_present_flag;
60     uint8_t aspect_ratio_idc;
61     uint16_t sar_width;
62     uint16_t sar_height;
63 
64     uint8_t overscan_info_present_flag;
65     uint8_t overscan_appropriate_flag;
66 
67     uint8_t video_signal_type_present_flag;
68     uint8_t video_format;
69     uint8_t video_full_range_flag;
70     uint8_t colour_description_present_flag;
71     uint8_t colour_primaries;
72     uint8_t transfer_characteristics;
73     uint8_t matrix_coefficients;
74 
75     uint8_t chroma_loc_info_present_flag;
76     uint8_t chroma_sample_loc_type_top_field;
77     uint8_t chroma_sample_loc_type_bottom_field;
78 
79     uint8_t timing_info_present_flag;
80     uint32_t num_units_in_tick;
81     uint32_t time_scale;
82     uint8_t fixed_frame_rate_flag;
83 
84     uint8_t nal_hrd_parameters_present_flag;
85     H264RawHRD nal_hrd_parameters;
86     uint8_t vcl_hrd_parameters_present_flag;
87     H264RawHRD vcl_hrd_parameters;
88     uint8_t low_delay_hrd_flag;
89 
90     uint8_t pic_struct_present_flag;
91 
92     uint8_t bitstream_restriction_flag;
93     uint8_t motion_vectors_over_pic_boundaries_flag;
94     uint8_t max_bytes_per_pic_denom;
95     uint8_t max_bits_per_mb_denom;
96     uint8_t log2_max_mv_length_horizontal;
97     uint8_t log2_max_mv_length_vertical;
98     uint8_t max_num_reorder_frames;
99     uint8_t max_dec_frame_buffering;
100 } H264RawVUI;
101 
102 typedef struct H264RawSPS {
103     H264RawNALUnitHeader nal_unit_header;
104 
105     uint8_t profile_idc;
106     uint8_t constraint_set0_flag;
107     uint8_t constraint_set1_flag;
108     uint8_t constraint_set2_flag;
109     uint8_t constraint_set3_flag;
110     uint8_t constraint_set4_flag;
111     uint8_t constraint_set5_flag;
112     uint8_t reserved_zero_2bits;
113     uint8_t level_idc;
114 
115     uint8_t seq_parameter_set_id;
116 
117     uint8_t chroma_format_idc;
118     uint8_t separate_colour_plane_flag;
119     uint8_t bit_depth_luma_minus8;
120     uint8_t bit_depth_chroma_minus8;
121     uint8_t qpprime_y_zero_transform_bypass_flag;
122 
123     uint8_t seq_scaling_matrix_present_flag;
124     uint8_t seq_scaling_list_present_flag[12];
125     H264RawScalingList scaling_list_4x4[6];
126     H264RawScalingList scaling_list_8x8[6];
127 
128     uint8_t log2_max_frame_num_minus4;
129     uint8_t pic_order_cnt_type;
130     uint8_t log2_max_pic_order_cnt_lsb_minus4;
131     uint8_t delta_pic_order_always_zero_flag;
132     int32_t offset_for_non_ref_pic;
133     int32_t offset_for_top_to_bottom_field;
134     uint8_t num_ref_frames_in_pic_order_cnt_cycle;
135     int32_t offset_for_ref_frame[256];
136 
137     uint8_t max_num_ref_frames;
138     uint8_t gaps_in_frame_num_allowed_flag;
139 
140     uint16_t pic_width_in_mbs_minus1;
141     uint16_t pic_height_in_map_units_minus1;
142 
143     uint8_t frame_mbs_only_flag;
144     uint8_t mb_adaptive_frame_field_flag;
145     uint8_t direct_8x8_inference_flag;
146 
147     uint8_t frame_cropping_flag;
148     uint16_t frame_crop_left_offset;
149     uint16_t frame_crop_right_offset;
150     uint16_t frame_crop_top_offset;
151     uint16_t frame_crop_bottom_offset;
152 
153     uint8_t vui_parameters_present_flag;
154     H264RawVUI vui;
155 } H264RawSPS;
156 
157 typedef struct H264RawSPSExtension {
158     H264RawNALUnitHeader nal_unit_header;
159 
160     uint8_t seq_parameter_set_id;
161 
162     uint8_t aux_format_idc;
163     uint8_t bit_depth_aux_minus8;
164     uint8_t alpha_incr_flag;
165     uint16_t alpha_opaque_value;
166     uint16_t alpha_transparent_value;
167 
168     uint8_t additional_extension_flag;
169 } H264RawSPSExtension;
170 
171 typedef struct H264RawPPS {
172     H264RawNALUnitHeader nal_unit_header;
173 
174     uint8_t pic_parameter_set_id;
175     uint8_t seq_parameter_set_id;
176 
177     uint8_t entropy_coding_mode_flag;
178     uint8_t bottom_field_pic_order_in_frame_present_flag;
179 
180     uint8_t num_slice_groups_minus1;
181     uint8_t slice_group_map_type;
182     uint16_t run_length_minus1[H264_MAX_SLICE_GROUPS];
183     uint16_t top_left[H264_MAX_SLICE_GROUPS];
184     uint16_t bottom_right[H264_MAX_SLICE_GROUPS];
185     uint8_t slice_group_change_direction_flag;
186     uint16_t slice_group_change_rate_minus1;
187     uint16_t pic_size_in_map_units_minus1;
188 
189     uint8_t *slice_group_id;
190     AVBufferRef *slice_group_id_ref;
191 
192     uint8_t num_ref_idx_l0_default_active_minus1;
193     uint8_t num_ref_idx_l1_default_active_minus1;
194 
195     uint8_t weighted_pred_flag;
196     uint8_t weighted_bipred_idc;
197 
198     int8_t pic_init_qp_minus26;
199     int8_t pic_init_qs_minus26;
200     int8_t chroma_qp_index_offset;
201 
202     uint8_t deblocking_filter_control_present_flag;
203     uint8_t constrained_intra_pred_flag;
204 
205     uint8_t more_rbsp_data;
206 
207     uint8_t redundant_pic_cnt_present_flag;
208     uint8_t transform_8x8_mode_flag;
209 
210     uint8_t pic_scaling_matrix_present_flag;
211     uint8_t pic_scaling_list_present_flag[12];
212     H264RawScalingList scaling_list_4x4[6];
213     H264RawScalingList scaling_list_8x8[6];
214 
215     int8_t second_chroma_qp_index_offset;
216 } H264RawPPS;
217 
218 typedef struct H264RawAUD {
219     H264RawNALUnitHeader nal_unit_header;
220 
221     uint8_t primary_pic_type;
222 } H264RawAUD;
223 
224 typedef struct H264RawSEIBufferingPeriod {
225     uint8_t seq_parameter_set_id;
226     struct {
227         uint32_t initial_cpb_removal_delay[H264_MAX_CPB_CNT];
228         uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT];
229     } nal, vcl;
230 } H264RawSEIBufferingPeriod;
231 
232 typedef struct H264RawSEIPicTimestamp {
233     uint8_t ct_type;
234     uint8_t nuit_field_based_flag;
235     uint8_t counting_type;
236     uint8_t full_timestamp_flag;
237     uint8_t discontinuity_flag;
238     uint8_t cnt_dropped_flag;
239     uint8_t n_frames;
240     uint8_t seconds_flag;
241     uint8_t seconds_value;
242     uint8_t minutes_flag;
243     uint8_t minutes_value;
244     uint8_t hours_flag;
245     uint8_t hours_value;
246     int32_t time_offset;
247 } H264RawSEIPicTimestamp;
248 
249 typedef struct H264RawSEIPicTiming {
250     uint32_t cpb_removal_delay;
251     uint32_t dpb_output_delay;
252     uint8_t pic_struct;
253     uint8_t clock_timestamp_flag[3];
254     H264RawSEIPicTimestamp timestamp[3];
255 } H264RawSEIPicTiming;
256 
257 typedef struct H264RawSEIPanScanRect {
258     uint32_t pan_scan_rect_id;
259     uint8_t  pan_scan_rect_cancel_flag;
260     uint8_t  pan_scan_cnt_minus1;
261     int32_t  pan_scan_rect_left_offset[3];
262     int32_t  pan_scan_rect_right_offset[3];
263     int32_t  pan_scan_rect_top_offset[3];
264     int32_t  pan_scan_rect_bottom_offset[3];
265     uint16_t pan_scan_rect_repetition_period;
266 } H264RawSEIPanScanRect;
267 
268 typedef struct H264RawSEIRecoveryPoint {
269     uint16_t recovery_frame_cnt;
270     uint8_t exact_match_flag;
271     uint8_t broken_link_flag;
272     uint8_t changing_slice_group_idc;
273 } H264RawSEIRecoveryPoint;
274 
275 typedef struct H264RawSEIDisplayOrientation {
276     uint8_t display_orientation_cancel_flag;
277     uint8_t hor_flip;
278     uint8_t ver_flip;
279     uint16_t anticlockwise_rotation;
280     uint16_t display_orientation_repetition_period;
281     uint8_t display_orientation_extension_flag;
282 } H264RawSEIDisplayOrientation;
283 
284 typedef struct H264RawSEI {
285     H264RawNALUnitHeader nal_unit_header;
286     SEIRawMessageList    message_list;
287 } H264RawSEI;
288 
289 typedef struct H264RawSliceHeader {
290     H264RawNALUnitHeader nal_unit_header;
291 
292     uint32_t first_mb_in_slice;
293     uint8_t slice_type;
294 
295     uint8_t pic_parameter_set_id;
296 
297     uint8_t colour_plane_id;
298 
299     uint16_t frame_num;
300     uint8_t field_pic_flag;
301     uint8_t bottom_field_flag;
302 
303     uint16_t idr_pic_id;
304 
305     uint16_t pic_order_cnt_lsb;
306     int32_t delta_pic_order_cnt_bottom;
307     int32_t delta_pic_order_cnt[2];
308 
309     uint8_t redundant_pic_cnt;
310     uint8_t direct_spatial_mv_pred_flag;
311 
312     uint8_t num_ref_idx_active_override_flag;
313     uint8_t num_ref_idx_l0_active_minus1;
314     uint8_t num_ref_idx_l1_active_minus1;
315 
316     uint8_t ref_pic_list_modification_flag_l0;
317     uint8_t ref_pic_list_modification_flag_l1;
318     struct {
319         uint8_t modification_of_pic_nums_idc;
320         int32_t abs_diff_pic_num_minus1;
321         uint8_t long_term_pic_num;
322     } rplm_l0[H264_MAX_RPLM_COUNT], rplm_l1[H264_MAX_RPLM_COUNT];
323 
324     uint8_t luma_log2_weight_denom;
325     uint8_t chroma_log2_weight_denom;
326 
327     uint8_t luma_weight_l0_flag[H264_MAX_REFS];
328     int8_t luma_weight_l0[H264_MAX_REFS];
329     int8_t luma_offset_l0[H264_MAX_REFS];
330     uint8_t chroma_weight_l0_flag[H264_MAX_REFS];
331     int8_t chroma_weight_l0[H264_MAX_REFS][2];
332     int8_t chroma_offset_l0[H264_MAX_REFS][2];
333 
334     uint8_t luma_weight_l1_flag[H264_MAX_REFS];
335     int8_t luma_weight_l1[H264_MAX_REFS];
336     int8_t luma_offset_l1[H264_MAX_REFS];
337     uint8_t chroma_weight_l1_flag[H264_MAX_REFS];
338     int8_t chroma_weight_l1[H264_MAX_REFS][2];
339     int8_t chroma_offset_l1[H264_MAX_REFS][2];
340 
341     uint8_t no_output_of_prior_pics_flag;
342     uint8_t long_term_reference_flag;
343 
344     uint8_t adaptive_ref_pic_marking_mode_flag;
345     struct {
346         uint8_t memory_management_control_operation;
347         int32_t difference_of_pic_nums_minus1;
348         uint8_t long_term_pic_num;
349         uint8_t long_term_frame_idx;
350         uint8_t max_long_term_frame_idx_plus1;
351     } mmco[H264_MAX_MMCO_COUNT];
352 
353     uint8_t cabac_init_idc;
354 
355     int8_t slice_qp_delta;
356 
357     uint8_t sp_for_switch_flag;
358     int8_t slice_qs_delta;
359 
360     uint8_t disable_deblocking_filter_idc;
361     int8_t slice_alpha_c0_offset_div2;
362     int8_t slice_beta_offset_div2;
363 
364     uint16_t slice_group_change_cycle;
365 } H264RawSliceHeader;
366 
367 typedef struct H264RawSlice {
368     H264RawSliceHeader header;
369 
370     uint8_t     *data;
371     AVBufferRef *data_ref;
372     size_t       data_size;
373     int          data_bit_start;
374 } H264RawSlice;
375 
376 typedef struct H264RawFiller {
377     H264RawNALUnitHeader nal_unit_header;
378 
379     uint32_t filler_size;
380 } H264RawFiller;
381 
382 
383 typedef struct CodedBitstreamH264Context {
384     // Reader/writer context in common with the H.265 implementation.
385     CodedBitstreamH2645Context common;
386 
387     // All currently available parameter sets.  These are updated when
388     // any parameter set NAL unit is read/written with this context.
389     AVBufferRef *sps_ref[H264_MAX_SPS_COUNT];
390     AVBufferRef *pps_ref[H264_MAX_PPS_COUNT];
391     H264RawSPS *sps[H264_MAX_SPS_COUNT];
392     H264RawPPS *pps[H264_MAX_PPS_COUNT];
393 
394     // The currently active parameter sets.  These are updated when any
395     // NAL unit refers to the relevant parameter set.  These pointers
396     // must also be present in the arrays above.
397     const H264RawSPS *active_sps;
398     const H264RawPPS *active_pps;
399 
400     // The NAL unit type of the most recent normal slice.  This is required
401     // to be able to read/write auxiliary slices, because IdrPicFlag is
402     // otherwise unknown.
403     uint8_t last_slice_nal_unit_type;
404 } CodedBitstreamH264Context;
405 
406 #endif /* AVCODEC_CBS_H264_H */
407