timeline: Utilize lsmash_freep().
[L-SMASH.git] / codecs / hevc.h
blob6432f357437c0c4fc09ce3f343e237dd19fcf234
1 /*****************************************************************************
2 * hevc.h:
3 *****************************************************************************
4 * Copyright (C) 2013-2014 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 HEVC_NALU_TYPE_TRAIL_N = 0,
26 HEVC_NALU_TYPE_TRAIL_R = 1,
27 HEVC_NALU_TYPE_TSA_N = 2,
28 HEVC_NALU_TYPE_TSA_R = 3,
29 HEVC_NALU_TYPE_STSA_N = 4,
30 HEVC_NALU_TYPE_STSA_R = 5,
31 HEVC_NALU_TYPE_RADL_N = 6,
32 HEVC_NALU_TYPE_RADL_R = 7,
33 HEVC_NALU_TYPE_RASL_N = 8,
34 HEVC_NALU_TYPE_RASL_R = 9,
35 HEVC_NALU_TYPE_RSV_VCL_R15 = 15,
36 HEVC_NALU_TYPE_BLA_W_LP = 16,
37 HEVC_NALU_TYPE_BLA_W_RADL = 17,
38 HEVC_NALU_TYPE_BLA_N_LP = 18,
39 HEVC_NALU_TYPE_IDR_W_RADL = 19,
40 HEVC_NALU_TYPE_IDR_N_LP = 20,
41 HEVC_NALU_TYPE_CRA = 21,
42 HEVC_NALU_TYPE_RSV_IRAP_VCL22 = 22,
43 HEVC_NALU_TYPE_RSV_IRAP_VCL23 = 23,
44 HEVC_NALU_TYPE_RSV_VCL31 = 31,
45 HEVC_NALU_TYPE_VPS = 32,
46 HEVC_NALU_TYPE_SPS = 33,
47 HEVC_NALU_TYPE_PPS = 34,
48 HEVC_NALU_TYPE_AUD = 35,
49 HEVC_NALU_TYPE_EOS = 36,
50 HEVC_NALU_TYPE_EOB = 37,
51 HEVC_NALU_TYPE_FD = 38,
52 HEVC_NALU_TYPE_PREFIX_SEI = 39,
53 HEVC_NALU_TYPE_SUFFIX_SEI = 40,
54 HEVC_NALU_TYPE_RSV_NVCL41 = 41,
55 HEVC_NALU_TYPE_RSV_NVCL44 = 44,
56 HEVC_NALU_TYPE_RSV_NVCL47 = 47,
57 HEVC_NALU_TYPE_UNSPEC48 = 48,
58 HEVC_NALU_TYPE_UNSPEC55 = 55,
59 HEVC_NALU_TYPE_UNSPEC63 = 63,
60 HEVC_NALU_TYPE_UNKNOWN = 64
63 typedef struct
65 uint8_t array_completeness;
66 uint8_t NAL_unit_type;
67 lsmash_entry_list_t list[1];
68 } hevc_parameter_array_t;
70 struct lsmash_hevc_parameter_arrays_tag
72 hevc_parameter_array_t ps_array[HEVC_DCR_NALU_TYPE_NUM];
75 typedef struct
77 unsigned forbidden_zero_bit : 1;
78 unsigned nal_unit_type : 7; /* +1 bit for HEVC_NALU_TYPE_UNKNOWN */
79 unsigned nuh_layer_id : 6;
80 unsigned TemporalId : 3;
81 unsigned length : 15;
82 } hevc_nalu_header_t;
84 /* Profile, Tier and Level */
85 typedef struct
87 uint8_t profile_space;
88 uint8_t tier_flag;
89 uint8_t profile_idc;
90 uint32_t profile_compatibility_flags;
91 uint8_t progressive_source_flag;
92 uint8_t interlaced_source_flag;
93 uint8_t non_packed_constraint_flag;
94 uint8_t frame_only_constraint_flag;
95 uint64_t reserved_zero_44bits;
96 uint8_t level_idc;
97 } hevc_ptl_common_t;
99 typedef struct
101 hevc_ptl_common_t general;
102 hevc_ptl_common_t sub_layer[6];
103 } hevc_ptl_t;
105 /* HRD (Hypothetical Reference Decoder) */
106 typedef struct
108 uint8_t present;
109 uint8_t CpbDpbDelaysPresentFlag;
110 uint8_t sub_pic_hrd_params_present_flag;
111 uint8_t du_cpb_removal_delay_increment_length;
112 uint8_t sub_pic_cpb_params_in_pic_timing_sei_flag;
113 uint8_t dpb_output_delay_du_length;
114 uint8_t au_cpb_removal_delay_length;
115 uint8_t dpb_output_delay_length;
116 uint8_t fixed_pic_rate_general_flag[7];
117 uint16_t elemental_duration_in_tc [7];
118 } hevc_hrd_t;
120 /* VPS (Video Parameter Set) */
121 typedef struct
123 uint8_t present;
124 uint8_t video_parameter_set_id;
125 uint8_t max_sub_layers_minus1;
126 uint8_t temporal_id_nesting_flag;
127 uint8_t timing_info_present_flag;
128 uint8_t frame_field_info_present_flag;
129 uint16_t num_hrd_parameters;
130 hevc_ptl_t ptl;
131 hevc_hrd_t hrd[2];
132 } hevc_vps_t;
134 typedef struct
136 uint8_t present;
137 uint16_t sar_width;
138 uint16_t sar_height;
139 uint8_t video_full_range_flag;
140 uint8_t colour_description_present_flag;
141 uint8_t colour_primaries;
142 uint8_t transfer_characteristics;
143 uint8_t matrix_coeffs;
144 uint8_t field_seq_flag;
145 uint8_t frame_field_info_present_flag;
146 uint32_t num_units_in_tick;
147 uint32_t time_scale;
148 uint16_t min_spatial_segmentation_idc;
149 lsmash_crop_t def_disp_win_offset;
150 hevc_hrd_t hrd;
151 } hevc_vui_t;
153 /* Short term reference picture sets */
154 typedef struct
156 uint8_t NumNegativePics;
157 uint8_t NumPositivePics;
158 uint8_t NumDeltaPocs;
159 uint8_t UsedByCurrPicS0[16];
160 uint8_t UsedByCurrPicS1[16];
161 int32_t DeltaPocS0[16];
162 int32_t DeltaPocS1[16];
163 } hevc_st_rps_t;
165 /* SPS (Sequence Parameter Set) */
166 typedef struct
168 uint8_t present;
169 uint8_t video_parameter_set_id;
170 uint8_t max_sub_layers_minus1;
171 uint8_t temporal_id_nesting_flag;
172 hevc_ptl_t ptl;
173 uint8_t seq_parameter_set_id;
174 uint8_t chroma_format_idc;
175 uint8_t separate_colour_plane_flag;
176 uint8_t bit_depth_luma_minus8;
177 uint8_t bit_depth_chroma_minus8;
178 uint8_t log2_max_pic_order_cnt_lsb;
179 uint8_t num_short_term_ref_pic_sets;
180 uint8_t long_term_ref_pics_present_flag;
181 uint8_t num_long_term_ref_pics_sps;
182 uint8_t temporal_mvp_enabled_flag;
183 uint32_t cropped_width;
184 uint32_t cropped_height;
185 uint32_t PicWidthInCtbsY;
186 uint32_t PicHeightInCtbsY;
187 uint64_t PicSizeInCtbsY;
188 hevc_st_rps_t st_rps[65];
189 hevc_vui_t vui;
190 } hevc_sps_t;
192 /* PPS (Picture Parameter Set) */
193 typedef struct
195 uint8_t present;
196 uint8_t pic_parameter_set_id;
197 uint8_t seq_parameter_set_id;
198 uint8_t dependent_slice_segments_enabled_flag;
199 uint8_t output_flag_present_flag;
200 uint8_t num_extra_slice_header_bits;
201 uint8_t tiles_enabled_flag;
202 uint8_t entropy_coding_sync_enabled_flag;
203 uint32_t num_tile_columns_minus1;
204 uint32_t num_tile_rows_minus1;
205 #define SIZEOF_PPS_EXCLUDING_HEAP (sizeof(hevc_pps_t) - offsetof( hevc_pps_t, col_alloc_size ))
206 size_t col_alloc_size;
207 size_t row_alloc_size;
208 uint32_t *colWidth;
209 uint32_t *colBd;
210 uint32_t *rowHeight;
211 uint32_t *rowBd;
212 } hevc_pps_t;
214 /* SEI (Supplemental Enhancement Information) */
215 typedef struct
217 uint8_t present;
218 uint8_t pic_struct;
219 } hevc_pic_timing_t;
221 typedef struct
223 uint8_t present;
224 uint8_t broken_link_flag;
225 int32_t recovery_poc_cnt;
226 } hevc_recovery_point_t;
228 typedef struct
230 hevc_pic_timing_t pic_timing;
231 hevc_recovery_point_t recovery_point;
232 } hevc_sei_t;
234 /* Slice segment */
235 typedef struct
237 uint8_t present;
238 uint8_t nalu_type;
239 uint8_t TemporalId;
240 uint8_t type;
241 uint8_t video_parameter_set_id;
242 uint8_t seq_parameter_set_id;
243 uint8_t pic_parameter_set_id;
244 uint8_t first_slice_segment_in_pic_flag;
245 uint8_t dependent_slice_segment_flag;
246 uint64_t segment_address;
247 int32_t pic_order_cnt_lsb;
248 } hevc_slice_info_t;
250 /* Picture */
251 typedef enum
253 HEVC_PICTURE_TYPE_I = 0,
254 HEVC_PICTURE_TYPE_I_P = 1,
255 HEVC_PICTURE_TYPE_I_P_B = 2,
256 HEVC_PICTURE_TYPE_IDR = 3,
257 HEVC_PICTURE_TYPE_CRA = 4,
258 HEVC_PICTURE_TYPE_BLA = 5,
259 HEVC_PICTURE_TYPE_NONE = 6,
260 } hevc_picture_type;
262 typedef struct
264 hevc_picture_type type;
265 uint8_t irap; /* 1: IDR, CRA or BLA picture */
266 uint8_t idr; /* 1: IDR picture */
267 uint8_t broken_link; /* 1: BLA picture or picture with broken link flag */
268 uint8_t radl; /* 1: RADL picture */
269 uint8_t rasl; /* 1: RASL picture */
270 uint8_t sublayer_nonref; /* 1: sub-layer non-reference picture */
271 uint8_t closed_rap; /* 1: no undecodable leading picture in CVS */
272 uint8_t random_accessible;/* 1: RAP or starting point of GDR */
273 uint8_t TemporalId;
274 uint8_t independent; /* 1: intra coded picture */
275 uint8_t field_coded; /* 1: field coded picture */
276 uint8_t pic_parameter_set_id;
277 uint8_t has_primary; /* 1: an independent slice segment is present. */
278 uint8_t delta;
279 /* POC */
280 uint16_t poc_lsb;
281 int32_t poc;
282 int32_t tid0_poc_msb;
283 int32_t tid0_poc_lsb;
284 /* */
285 int32_t recovery_poc_cnt;
286 } hevc_picture_info_t;
288 /* Access unit */
289 typedef struct
291 uint8_t *data;
292 uint8_t *incomplete_data;
293 uint32_t length;
294 uint32_t incomplete_length;
295 uint32_t number;
296 uint8_t TemporalId;
297 hevc_picture_info_t picture;
298 } hevc_access_unit_t;
300 typedef struct hevc_info_tag hevc_info_t;
302 typedef struct
304 lsmash_multiple_buffers_t *bank;
305 uint8_t *rbsp;
306 } hevc_stream_buffer_t;
308 struct hevc_info_tag
310 lsmash_hevc_specific_parameters_t hvcC_param;
311 lsmash_hevc_specific_parameters_t hvcC_param_next;
312 hevc_nalu_header_t nuh;
313 lsmash_entry_list_t vps_list[1];
314 lsmash_entry_list_t sps_list[1];
315 lsmash_entry_list_t pps_list[1];
316 hevc_vps_t vps; /* active VPS */
317 hevc_sps_t sps; /* active SPS */
318 hevc_pps_t pps; /* active PPS */
319 hevc_sei_t sei; /* active SEI */
320 hevc_slice_info_t slice; /* active slice */
321 hevc_access_unit_t au;
322 uint8_t prev_nalu_type;
323 uint8_t hvcC_pending;
324 uint8_t eos; /* end of sequence */
325 uint64_t ebsp_head_pos;
326 lsmash_bits_t *bits;
327 hevc_stream_buffer_t buffer;
330 int hevc_setup_parser
332 hevc_info_t *info,
333 int parse_only
336 void hevc_cleanup_parser
338 hevc_info_t *info
341 uint64_t hevc_find_next_start_code
343 lsmash_bs_t *bs,
344 hevc_nalu_header_t *nuh,
345 uint64_t *start_code_length,
346 uint64_t *trailing_zero_bytes
349 int hevc_calculate_poc
351 hevc_info_t *info,
352 hevc_picture_info_t *picture,
353 hevc_picture_info_t *prev_picture
356 void hevc_update_picture_info_for_slice
358 hevc_info_t *info,
359 hevc_picture_info_t *picture,
360 hevc_slice_info_t *slice
363 void hevc_update_picture_info
365 hevc_info_t *info,
366 hevc_picture_info_t *picture,
367 hevc_slice_info_t *slice,
368 hevc_sps_t *sps,
369 hevc_sei_t *sei
372 int hevc_find_au_delimit_by_slice_info
374 hevc_info_t *info,
375 hevc_slice_info_t *slice,
376 hevc_slice_info_t *prev_slice
379 int hevc_find_au_delimit_by_nalu_type
381 uint8_t nalu_type,
382 uint8_t prev_nalu_type
385 int hevc_supplement_buffer
387 hevc_stream_buffer_t *hb,
388 hevc_access_unit_t *au,
389 uint32_t size
392 int hevc_parse_vps
394 hevc_info_t *info,
395 uint8_t *rbsp_buffer,
396 uint8_t *ebsp,
397 uint64_t ebsp_size
400 int hevc_parse_sps
402 hevc_info_t *info,
403 uint8_t *rbsp_buffer,
404 uint8_t *ebsp,
405 uint64_t ebsp_size
408 int hevc_parse_pps
410 hevc_info_t *info,
411 uint8_t *rbsp_buffer,
412 uint8_t *ebsp,
413 uint64_t ebsp_size
416 int hevc_parse_sei
418 lsmash_bits_t *bits,
419 hevc_vps_t *vps,
420 hevc_sps_t *sps,
421 hevc_sei_t *sei,
422 hevc_nalu_header_t *nuh,
423 uint8_t *rbsp_buffer,
424 uint8_t *ebsp,
425 uint64_t ebsp_size
427 int hevc_parse_slice_segment_header
429 hevc_info_t *info,
430 hevc_nalu_header_t *nuh,
431 uint8_t *rbsp_buffer,
432 uint8_t *ebsp,
433 uint64_t ebsp_size
436 int hevc_try_to_append_dcr_nalu
438 hevc_info_t *info,
439 lsmash_hevc_dcr_nalu_type ps_type,
440 void *ps_data,
441 uint32_t ps_length
444 int hevc_move_pending_hvcC_param
446 hevc_info_t *info