codecs: Move bitrate updater codes into proper places.
[L-SMASH.git] / codecs / h264.h
blobdb943396fa363805c6751a6edd38fa34a5a4f602
1 /*****************************************************************************
2 * h264.h:
3 *****************************************************************************
4 * Copyright (C) 2012-2015 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 enum
25 H264_NALU_TYPE_UNSPECIFIED0 = 0, /* Unspecified */
26 H264_NALU_TYPE_SLICE_N_IDR = 1, /* Coded slice of a non-IDR picture */
27 H264_NALU_TYPE_SLICE_DP_A = 2, /* Coded slice data partition A */
28 H264_NALU_TYPE_SLICE_DP_B = 3, /* Coded slice data partition B */
29 H264_NALU_TYPE_SLICE_DP_C = 4, /* Coded slice data partition C */
30 H264_NALU_TYPE_SLICE_IDR = 5, /* Coded slice of an IDR picture */
31 H264_NALU_TYPE_SEI = 6, /* Supplemental Enhancement Information */
32 H264_NALU_TYPE_SPS = 7, /* Sequence Parameter Set */
33 H264_NALU_TYPE_PPS = 8, /* Picture Parameter Set */
34 H264_NALU_TYPE_AUD = 9, /* Access Unit Delimiter */
35 H264_NALU_TYPE_EOS = 10, /* End of Sequence */
36 H264_NALU_TYPE_EOB = 11, /* End of Bitstream */
37 H264_NALU_TYPE_FD = 12, /* Filler Data */
38 H264_NALU_TYPE_SPS_EXT = 13, /* Sequence Parameter Set Extension */
39 H264_NALU_TYPE_PREFIX = 14, /* Prefix NAL unit */
40 H264_NALU_TYPE_SUBSET_SPS = 15, /* Subset Sequence Parameter Set */
41 H264_NALU_TYPE_RSV_NVCL16 = 16, /* Reserved */
42 H264_NALU_TYPE_RSV_NVCL17 = 17, /* Reserved */
43 H264_NALU_TYPE_RSV_NVCL18 = 18, /* Reserved */
44 H264_NALU_TYPE_SLICE_AUX = 19, /* Coded slice of an auxiliary coded picture without partitioning */
45 H264_NALU_TYPE_SLICE_EXT = 20, /* Coded slice extension */
46 H264_NALU_TYPE_SLICE_EXT_DVC = 21, /* Coded slice extension for depth view components */
47 H264_NALU_TYPE_RSV22 = 22, /* Reserved */
48 H264_NALU_TYPE_RSV23 = 23, /* Reserved */
49 H264_NALU_TYPE_UNSPECIFIED24 = 24, /* Unspecified */
50 H264_NALU_TYPE_UNSPECIFIED31 = 31, /* Unspecified */
53 struct lsmash_h264_parameter_sets_tag
55 /* Each list contains entries as isom_dcr_ps_entry_t. */
56 lsmash_entry_list_t sps_list [1];
57 lsmash_entry_list_t pps_list [1];
58 lsmash_entry_list_t spsext_list[1];
61 typedef struct
63 unsigned forbidden_zero_bit : 1;
64 unsigned nal_ref_idc : 2;
65 unsigned nal_unit_type : 5;
66 uint8_t length;
67 } h264_nalu_header_t;
69 typedef struct
71 uint8_t present;
72 uint8_t CpbDpbDelaysPresentFlag;
73 uint8_t cpb_removal_delay_length;
74 uint8_t dpb_output_delay_length;
75 } h264_hrd_t;
77 typedef struct
79 uint16_t sar_width;
80 uint16_t sar_height;
81 uint8_t video_full_range_flag;
82 uint8_t colour_primaries;
83 uint8_t transfer_characteristics;
84 uint8_t matrix_coefficients;
85 uint32_t num_units_in_tick;
86 uint32_t time_scale;
87 uint8_t fixed_frame_rate_flag;
88 uint8_t pic_struct_present_flag;
89 h264_hrd_t hrd;
90 } h264_vui_t;
92 typedef struct
94 uint8_t present;
95 uint8_t profile_idc;
96 uint8_t constraint_set_flags;
97 uint8_t level_idc;
98 uint8_t seq_parameter_set_id;
99 uint8_t chroma_format_idc;
100 uint8_t separate_colour_plane_flag;
101 uint8_t ChromaArrayType;
102 uint8_t bit_depth_luma_minus8;
103 uint8_t bit_depth_chroma_minus8;
104 uint8_t log2_max_frame_num;
105 uint8_t pic_order_cnt_type;
106 uint8_t delta_pic_order_always_zero_flag;
107 uint8_t num_ref_frames_in_pic_order_cnt_cycle;
108 uint8_t frame_mbs_only_flag;
109 int32_t offset_for_non_ref_pic;
110 int32_t offset_for_top_to_bottom_field;
111 int32_t offset_for_ref_frame[255];
112 int64_t ExpectedDeltaPerPicOrderCntCycle;
113 uint32_t max_num_ref_frames;
114 uint32_t MaxFrameNum;
115 uint32_t log2_max_pic_order_cnt_lsb;
116 uint32_t MaxPicOrderCntLsb;
117 uint32_t PicSizeInMapUnits;
118 uint32_t cropped_width;
119 uint32_t cropped_height;
120 h264_vui_t vui;
121 } h264_sps_t;
123 typedef struct
125 uint8_t present;
126 uint8_t pic_parameter_set_id;
127 uint8_t seq_parameter_set_id;
128 uint8_t entropy_coding_mode_flag;
129 uint8_t bottom_field_pic_order_in_frame_present_flag;
130 uint8_t num_slice_groups_minus1;
131 uint8_t slice_group_map_type;
132 uint8_t num_ref_idx_l0_default_active_minus1;
133 uint8_t num_ref_idx_l1_default_active_minus1;
134 uint8_t weighted_pred_flag;
135 uint8_t weighted_bipred_idc;
136 uint8_t deblocking_filter_control_present_flag;
137 uint8_t redundant_pic_cnt_present_flag;
138 uint32_t SliceGroupChangeRate;
139 } h264_pps_t;
141 typedef struct
143 uint8_t present;
144 uint8_t pic_struct;
145 } h264_pic_timing_t;
147 typedef struct
149 uint8_t present;
150 uint8_t random_accessible;
151 uint8_t broken_link_flag;
152 uint32_t recovery_frame_cnt;
153 } h264_recovery_point_t;
155 typedef struct
157 h264_pic_timing_t pic_timing;
158 h264_recovery_point_t recovery_point;
159 } h264_sei_t;
161 typedef struct
163 uint8_t present;
164 uint8_t slice_id; /* only for slice data partition */
165 uint8_t type;
166 uint8_t pic_order_cnt_type;
167 uint8_t nal_ref_idc;
168 uint8_t IdrPicFlag;
169 uint8_t seq_parameter_set_id;
170 uint8_t pic_parameter_set_id;
171 uint8_t field_pic_flag;
172 uint8_t bottom_field_flag;
173 uint8_t has_mmco5;
174 uint8_t has_redundancy;
175 uint16_t idr_pic_id;
176 uint32_t frame_num;
177 int32_t pic_order_cnt_lsb;
178 int32_t delta_pic_order_cnt_bottom;
179 int32_t delta_pic_order_cnt[2];
180 } h264_slice_info_t;
182 typedef enum
184 H264_PICTURE_TYPE_IDR = 0,
185 H264_PICTURE_TYPE_I = 1,
186 H264_PICTURE_TYPE_I_P = 2,
187 H264_PICTURE_TYPE_I_P_B = 3,
188 H264_PICTURE_TYPE_SI = 4,
189 H264_PICTURE_TYPE_SI_SP = 5,
190 H264_PICTURE_TYPE_I_SI = 6,
191 H264_PICTURE_TYPE_I_SI_P_SP = 7,
192 H264_PICTURE_TYPE_I_SI_P_SP_B = 8,
193 H264_PICTURE_TYPE_NONE = 9,
194 } h264_picture_type;
196 typedef struct
198 h264_picture_type type;
199 uint8_t idr;
200 uint8_t random_accessible;
201 uint8_t independent;
202 uint8_t disposable; /* 1: nal_ref_idc == 0, 0: otherwise */
203 uint8_t has_redundancy;
204 uint8_t has_primary;
205 uint8_t pic_parameter_set_id;
206 uint8_t field_pic_flag;
207 uint8_t bottom_field_flag;
208 uint8_t delta;
209 uint8_t broken_link_flag;
210 /* POC */
211 uint8_t has_mmco5;
212 uint8_t ref_pic_has_mmco5;
213 uint8_t ref_pic_bottom_field_flag;
214 int32_t ref_pic_TopFieldOrderCnt;
215 int32_t ref_pic_PicOrderCntMsb;
216 int32_t ref_pic_PicOrderCntLsb;
217 int32_t pic_order_cnt_lsb;
218 int32_t delta_pic_order_cnt_bottom;
219 int32_t delta_pic_order_cnt[2];
220 int32_t PicOrderCnt;
221 uint32_t FrameNumOffset;
222 /* */
223 uint32_t recovery_frame_cnt;
224 uint32_t frame_num;
225 } h264_picture_info_t;
227 typedef struct
229 uint8_t *data;
230 uint8_t *incomplete_data;
231 uint32_t length;
232 uint32_t incomplete_length;
233 uint32_t number;
234 h264_picture_info_t picture;
235 } h264_access_unit_t;
237 typedef struct h264_info_tag h264_info_t;
239 typedef struct
241 lsmash_multiple_buffers_t *bank;
242 uint8_t *rbsp;
243 } h264_stream_buffer_t;
245 struct h264_info_tag
247 lsmash_h264_specific_parameters_t avcC_param;
248 lsmash_h264_specific_parameters_t avcC_param_next;
249 lsmash_entry_list_t sps_list [1]; /* contains entries as h264_sps_t */
250 lsmash_entry_list_t pps_list [1]; /* contains entries as h264_pps_t */
251 lsmash_entry_list_t slice_list[1]; /* for slice data partition */
252 h264_sps_t sps; /* active SPS */
253 h264_pps_t pps; /* active PPS */
254 h264_sei_t sei; /* active SEI */
255 h264_slice_info_t slice; /* active slice */
256 h264_access_unit_t au;
257 uint8_t prev_nalu_type;
258 uint8_t avcC_pending;
259 lsmash_bits_t *bits;
260 h264_stream_buffer_t buffer;
263 int h264_setup_parser
265 h264_info_t *info,
266 int parse_only
269 void h264_cleanup_parser
271 h264_info_t *info
274 uint64_t h264_find_next_start_code
276 lsmash_bs_t *bs,
277 h264_nalu_header_t *nuh,
278 uint64_t *start_code_length,
279 uint64_t *trailing_zero_bytes
282 int h264_calculate_poc
284 h264_info_t *info,
285 h264_picture_info_t *picture,
286 h264_picture_info_t *prev_picture
289 void h264_update_picture_info_for_slice
291 h264_info_t *info,
292 h264_picture_info_t *picture,
293 h264_slice_info_t *slice
296 void h264_update_picture_info
298 h264_info_t *info,
299 h264_picture_info_t *picture,
300 h264_slice_info_t *slice,
301 h264_sei_t *sei
304 int h264_find_au_delimit_by_slice_info
306 h264_slice_info_t *slice,
307 h264_slice_info_t *prev_slice
310 int h264_find_au_delimit_by_nalu_type
312 uint8_t nalu_type,
313 uint8_t prev_nalu_type
316 int h264_supplement_buffer
318 h264_stream_buffer_t *buffer,
319 h264_access_unit_t *au,
320 uint32_t size
323 int h264_parse_sps
325 h264_info_t *info,
326 uint8_t *rbsp_buffer,
327 uint8_t *ebsp,
328 uint64_t ebsp_size
331 int h264_parse_pps
333 h264_info_t *info,
334 uint8_t *rbsp_buffer,
335 uint8_t *ebsp,
336 uint64_t ebsp_size
339 int h264_parse_sei
341 lsmash_bits_t *bits,
342 h264_sps_t *sps,
343 h264_sei_t *sei,
344 uint8_t *rbsp_buffer,
345 uint8_t *ebsp,
346 uint64_t ebsp_size
349 int h264_parse_slice
351 h264_info_t *info,
352 h264_nalu_header_t *nuh,
353 uint8_t *rbsp_buffer,
354 uint8_t *ebsp,
355 uint64_t ebsp_size
358 int h264_try_to_append_parameter_set
360 h264_info_t *info,
361 lsmash_h264_parameter_set_type ps_type,
362 void *ps_data,
363 uint32_t ps_length
366 int h264_move_pending_avcC_param
368 h264_info_t *info