1 /*****************************************************************************
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. */
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
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
];
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;
84 /* Profile, Tier and Level */
87 uint8_t profile_space
;
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
;
101 hevc_ptl_common_t general
;
102 hevc_ptl_common_t sub_layer
[6];
105 /* HRD (Hypothetical Reference Decoder) */
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];
120 /* VPS (Video Parameter Set) */
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
;
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
;
148 uint16_t min_spatial_segmentation_idc
;
149 lsmash_crop_t def_disp_win_offset
;
153 /* Short term reference picture sets */
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];
165 /* SPS (Sequence Parameter Set) */
169 uint8_t video_parameter_set_id
;
170 uint8_t max_sub_layers_minus1
;
171 uint8_t temporal_id_nesting_flag
;
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];
192 /* PPS (Picture Parameter Set) */
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
;
214 /* SEI (Supplemental Enhancement Information) */
224 uint8_t broken_link_flag
;
225 int32_t recovery_poc_cnt
;
226 } hevc_recovery_point_t
;
230 hevc_pic_timing_t pic_timing
;
231 hevc_recovery_point_t recovery_point
;
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
;
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,
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 */
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. */
282 int32_t tid0_poc_msb
;
283 int32_t tid0_poc_lsb
;
285 int32_t recovery_poc_cnt
;
286 } hevc_picture_info_t
;
292 uint8_t *incomplete_data
;
294 uint32_t incomplete_length
;
297 hevc_picture_info_t picture
;
298 } hevc_access_unit_t
;
300 typedef struct hevc_info_tag hevc_info_t
;
304 lsmash_multiple_buffers_t
*bank
;
306 } hevc_stream_buffer_t
;
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
;
327 hevc_stream_buffer_t buffer
;
330 int hevc_setup_parser
336 void hevc_cleanup_parser
341 uint64_t hevc_find_next_start_code
344 hevc_nalu_header_t
*nuh
,
345 uint64_t *start_code_length
,
346 uint64_t *trailing_zero_bytes
349 int hevc_calculate_poc
352 hevc_picture_info_t
*picture
,
353 hevc_picture_info_t
*prev_picture
356 void hevc_update_picture_info_for_slice
359 hevc_picture_info_t
*picture
,
360 hevc_slice_info_t
*slice
363 void hevc_update_picture_info
366 hevc_picture_info_t
*picture
,
367 hevc_slice_info_t
*slice
,
372 int hevc_find_au_delimit_by_slice_info
375 hevc_slice_info_t
*slice
,
376 hevc_slice_info_t
*prev_slice
379 int hevc_find_au_delimit_by_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
,
395 uint8_t *rbsp_buffer
,
403 uint8_t *rbsp_buffer
,
411 uint8_t *rbsp_buffer
,
422 hevc_nalu_header_t
*nuh
,
423 uint8_t *rbsp_buffer
,
427 int hevc_parse_slice_segment_header
430 hevc_nalu_header_t
*nuh
,
431 uint8_t *rbsp_buffer
,
436 int hevc_try_to_append_dcr_nalu
439 lsmash_hevc_dcr_nalu_type ps_type
,
444 int hevc_move_pending_hvcC_param