1 /*****************************************************************
2 |
3 |    AP4 - HEVC Parser
4 |
5 |    Copyright 2002-2014 Axiomatic Systems, LLC
6 |
7 |
8 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 |    Unless you have obtained Bento4 under a difference license,
11 |    this version of Bento4 is Bento4|GPL.
12 |    Bento4|GPL is free software; you can redistribute it and/or modify
13 |    it under the terms of the GNU General Public License as published by
14 |    the Free Software Foundation; either version 2, or (at your option)
15 |    any later version.
16 |
17 |    Bento4|GPL is distributed in the hope that it will be useful,
18 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 |    GNU General Public License for more details.
21 |
22 |    You should have received a copy of the GNU General Public License
23 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 |    02111-1307, USA.
26 |
27 ****************************************************************/
28 
29 /*----------------------------------------------------------------------
30 |   includes
31 +---------------------------------------------------------------------*/
32 #include "Ap4HevcParser.h"
33 #include "Ap4Utils.h"
34 
35 /*----------------------------------------------------------------------
36 |   debugging
37 +---------------------------------------------------------------------*/
38 #define AP4_HEVC_PARSER_ENABLE_DEBUG 0
39 
40 #if AP4_HEVC_PARSER_ENABLE_DEBUG
41 #define DBG_PRINTF_0(_x0) printf(_x0)
42 #define DBG_PRINTF_1(_x0, _x1) printf(_x0, _x1)
43 #define DBG_PRINTF_2(_x0, _x1, _x2) printf(_x0, _x1, _x2)
44 #define DBG_PRINTF_3(_x0, _x1, _x2, _x3) printf(_x0, _x1, _x2, _x3)
45 #define DBG_PRINTF_4(_x0, _x1, _x2, _x3, _x4) printf(_x0, _x1, _x2, _x3, _x4)
46 #define DBG_PRINTF_5(_x0, _x1, _x2, _x3, _x4, _x5) printf(_x0, _x1, _x2, _x3, _x4, _x5)
47 #define DBG_PRINTF_6(_x0, _x1, _x2, _x3, _x4, _x5, _x6) printf(_x0, _x1, _x2, _x3, _x4, _x5, _x6)
48 #define DBG_PRINTF_7(_x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7) printf(_x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7)
49 #else
50 #define DBG_PRINTF_0(_x0)
51 #define DBG_PRINTF_1(_x0, _x1)
52 #define DBG_PRINTF_2(_x0, _x1, _x2)
53 #define DBG_PRINTF_3(_x0, _x1, _x2, _x3)
54 #define DBG_PRINTF_4(_x0, _x1, _x2, _x3, _x4)
55 #define DBG_PRINTF_5(_x0, _x1, _x2, _x3, _x4, _x5)
56 #define DBG_PRINTF_6(_x0, _x1, _x2, _x3, _x4, _x5, _x6)
57 #define DBG_PRINTF_7(_x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7)
58 #endif
59 
60 /*----------------------------------------------------------------------
61 |   AP4_HevcNalParser::NaluTypeName
62 +---------------------------------------------------------------------*/
63 const char*
NaluTypeName(unsigned int nalu_type)64 AP4_HevcNalParser::NaluTypeName(unsigned int nalu_type)
65 {
66     switch (nalu_type) {
67         case  0: return "TRAIL_N - Coded slice segment of a non-TSA, non-STSA trailing picture";
68         case  1: return "TRAIL_R - Coded slice segment of a non-TSA, non-STSA trailing picture";
69         case  2: return "TSA_N - Coded slice segment of a TSA picture";
70         case  3: return "TSA_R - Coded slice segment of a TSA picture";
71         case  4: return "STSA_N - Coded slice segment of an STSA picture";
72         case  5: return "STSA_R - Coded slice segment of an STSA picture";
73         case  6: return "RADL_N - Coded slice segment of a RADL picture";
74         case  7: return "RADL_R - Coded slice segment of a RADL picture";
75         case  8: return "RASL_N - Coded slice segment of a RASL picture";
76         case  9: return "RASL_R - Coded slice segment of a RASL picture";
77         case 10: return "RSV_VCL_N10 - Reserved non-IRAP sub-layer non-reference";
78         case 12: return "RSV_VCL_N12 - Reserved non-IRAP sub-layer non-reference";
79         case 14: return "RSV_VCL_N14 - Reserved non-IRAP sub-layer non-reference";
80         case 11: return "RSV_VCL_R11 - Reserved non-IRAP sub-layer reference";
81         case 13: return "RSV_VCL_R13 - Reserved non-IRAP sub-layer reference";
82         case 15: return "RSV_VCL_R15 - Reserved non-IRAP sub-layer reference";
83         case 16: return "BLA_W_LP - Coded slice segment of a BLA picture";
84         case 17: return "BLA_W_RADL - Coded slice segment of a BLA picture";
85         case 18: return "BLA_N_LP - Coded slice segment of a BLA picture";
86         case 19: return "IDR_W_RADL - Coded slice segment of an IDR picture";
87         case 20: return "IDR_N_LP - Coded slice segment of an IDR picture";
88         case 21: return "CRA_NUT - Coded slice segment of a CRA picture";
89         case 22: return "RSV_IRAP_VCL22 - Reserved IRAP";
90         case 23: return "RSV_IRAP_VCL23 - Reserved IRAP";
91         case 24: return "RSV_VCL24 - Reserved non-IRAP";
92         case 25: return "RSV_VCL25 - Reserved non-IRAP";
93         case 26: return "RSV_VCL26 - Reserved non-IRAP";
94         case 27: return "RSV_VCL27 - Reserved non-IRAP";
95         case 28: return "RSV_VCL28 - Reserved non-IRAP";
96         case 29: return "RSV_VCL29 - Reserved non-IRAP";
97         case 30: return "RSV_VCL30 - Reserved non-IRAP";
98         case 31: return "RSV_VCL31 - Reserved non-IRAP";
99         case 32: return "VPS_NUT - Video parameter set";
100         case 33: return "SPS_NUT - Sequence parameter set";
101         case 34: return "PPS_NUT - Picture parameter set";
102         case 35: return "AUD_NUT - Access unit delimiter";
103         case 36: return "EOS_NUT - End of sequence";
104         case 37: return "EOB_NUT - End of bitstream";
105         case 38: return "FD_NUT - Filler data";
106         case 39: return "PREFIX_SEI_NUT - Supplemental enhancement information";
107         case 40: return "SUFFIX_SEI_NUT - Supplemental enhancement information";
108         case 62: return "Dolby Vision RPU NAL units";
109         case 63: return "Dolby Vision EL NAL units";
110         default: return NULL;
111     }
112 }
113 
114 /*----------------------------------------------------------------------
115 |   AP4_HevcNalParser::PicTypeName
116 +---------------------------------------------------------------------*/
117 const char*
PicTypeName(unsigned int primary_pic_type)118 AP4_HevcNalParser::PicTypeName(unsigned int primary_pic_type)
119 {
120 	switch (primary_pic_type) {
121         case 0: return "I";
122         case 1: return "I, P";
123         case 2: return "I, P, B";
124         default: return NULL;
125     }
126 }
127 
128 /*----------------------------------------------------------------------
129 |   AP4_HevcNalParser::SliceTypeName
130 +---------------------------------------------------------------------*/
131 const char*
SliceTypeName(unsigned int slice_type)132 AP4_HevcNalParser::SliceTypeName(unsigned int slice_type)
133 {
134 	switch (slice_type) {
135         case AP4_HEVC_SLICE_TYPE_B: return "B";
136         case AP4_HEVC_SLICE_TYPE_P: return "P";
137         case AP4_HEVC_SLICE_TYPE_I: return "I";
138         default: return NULL;
139     }
140 }
141 
142 /*----------------------------------------------------------------------
143 |   ReadGolomb
144 +---------------------------------------------------------------------*/
145 static unsigned int
ReadGolomb(AP4_BitReader & bits)146 ReadGolomb(AP4_BitReader& bits)
147 {
148     unsigned int leading_zeros = 0;
149     while (bits.ReadBit() == 0) {
150         leading_zeros++;
151         if (leading_zeros > 32) return 0; // safeguard
152     }
153     if (leading_zeros) {
154         return (1<<leading_zeros)-1+bits.ReadBits(leading_zeros);
155     } else {
156         return 0;
157     }
158 }
159 
160 /*----------------------------------------------------------------------
161 |   SignedGolomb
162 +---------------------------------------------------------------------*/
163 static int
SignedGolomb(unsigned int code_num)164 SignedGolomb(unsigned int code_num)
165 {
166     if (code_num % 2) {
167         return (code_num+1)/2;
168     } else {
169         return -((int)code_num/2);
170     }
171 }
172 
173 /*----------------------------------------------------------------------
174 |   BitsNeeded
175 +---------------------------------------------------------------------*/
176 static unsigned int
BitsNeeded(unsigned int num_values)177 BitsNeeded(unsigned int num_values)
178 {
179     unsigned int bits_needed = 1;
180     while (num_values > (unsigned int)(1 << bits_needed)) {
181         ++bits_needed;
182     }
183 
184     return bits_needed;
185 }
186 
187 /*----------------------------------------------------------------------
188 |   AP4_HevcNalParser::AP4_HevcNalParser
189 +---------------------------------------------------------------------*/
AP4_HevcNalParser()190 AP4_HevcNalParser::AP4_HevcNalParser() :
191     AP4_NalParser()
192 {
193 }
194 
195 /*----------------------------------------------------------------------
196 |   scaling_list_data
197 +---------------------------------------------------------------------*/
198 static void
scaling_list_data(AP4_BitReader & bits)199 scaling_list_data(AP4_BitReader& bits)
200 {
201     for (unsigned int sizeId = 0; sizeId < 4; sizeId++) {
202         for (unsigned int matrixId = 0; matrixId < (unsigned int)((sizeId == 3)?2:6); matrixId++) {
203             unsigned int flag = bits.ReadBit(); // scaling_list_pred_mode_flag[ sizeId ][ matrixId ]
204             if (!flag) {
205                 ReadGolomb(bits); // scaling_list_pred_matrix_id_delta[ sizeId ][ matrixId ]
206             } else {
207                 // nextCoef = 8;
208                 unsigned int coefNum = (1 << (4+(sizeId << 1)));
209                 if (coefNum > 64) coefNum = 64;
210                 if (sizeId > 1) {
211                     ReadGolomb(bits); // scaling_list_dc_coef_minus8[ sizeId − 2 ][ matrixId ]
212                     // nextCoef = scaling_list_dc_coef_minus8[ sizeId − 2 ][ matrixId ] + 8
213                 }
214                 for (unsigned i = 0; i < coefNum; i++) {
215                     ReadGolomb(bits); // scaling_list_delta_coef
216                     // nextCoef = ( nextCoef + scaling_list_delta_coef + 256 ) % 256
217                     // ScalingList[ sizeId ][ matrixId ][ i ] = nextCoef
218                 }
219             }
220         }
221     }
222 }
223 
224 /*----------------------------------------------------------------------
225 |   st_ref_pic_set
226 +---------------------------------------------------------------------*/
227 static AP4_Result
parse_st_ref_pic_set(AP4_HevcShortTermRefPicSet * rps,const AP4_HevcSequenceParameterSet * sps,unsigned int stRpsIdx,unsigned int num_short_term_ref_pic_sets,AP4_BitReader & bits)228 parse_st_ref_pic_set(AP4_HevcShortTermRefPicSet*         rps,
229                      const AP4_HevcSequenceParameterSet* sps,
230                      unsigned int                        stRpsIdx,
231                      unsigned int                        num_short_term_ref_pic_sets,
232                      AP4_BitReader&                      bits) {
233     AP4_SetMemory(rps, 0, sizeof(*rps));
234 
235     unsigned int inter_ref_pic_set_prediction_flag = 0;
236     if (stRpsIdx != 0) {
237         inter_ref_pic_set_prediction_flag = bits.ReadBit();
238     }
239     if (inter_ref_pic_set_prediction_flag) {
240         unsigned int delta_idx_minus1 = 0;
241         if (stRpsIdx == num_short_term_ref_pic_sets) {
242             delta_idx_minus1 = ReadGolomb(bits);
243         }
244         /* delta_rps_sign = */ bits.ReadBit();
245         /* abs_delta_rps_minus1 = */ ReadGolomb(bits);
246         if (delta_idx_minus1+1 > stRpsIdx) return AP4_ERROR_INVALID_FORMAT; // should not happen
247         unsigned int RefRpsIdx = stRpsIdx - (delta_idx_minus1 + 1);
248         unsigned int NumDeltaPocs = sps->short_term_ref_pic_sets[RefRpsIdx].num_delta_pocs;
249         for (unsigned j=0; j<=NumDeltaPocs; j++) {
250             unsigned int used_by_curr_pic_flag /*[j]*/ = bits.ReadBit();
251             unsigned int use_delta_flag /*[j]*/ = 1;
252             if (!used_by_curr_pic_flag /*[j]*/) {
253                 use_delta_flag /*[j]*/ = bits.ReadBit();
254             }
255             if (used_by_curr_pic_flag /*[j]*/ || use_delta_flag /*[j]*/) {
256                 rps->num_delta_pocs++;
257             }
258         }
259     } else {
260         rps->num_negative_pics = ReadGolomb(bits);
261         rps->num_positive_pics = ReadGolomb(bits);
262         if (rps->num_negative_pics > 16 || rps->num_positive_pics > 16) {
263             return AP4_ERROR_INVALID_FORMAT;
264         }
265         rps->num_delta_pocs = rps->num_negative_pics + rps->num_positive_pics;
266         for (unsigned int i=0; i<rps->num_negative_pics; i++) {
267             rps->delta_poc_s0_minus1[i] = ReadGolomb(bits);
268             rps->used_by_curr_pic_s0_flag[i] = bits.ReadBit();
269         }
270         for (unsigned i=0; i<rps->num_positive_pics; i++) {
271             rps->delta_poc_s1_minus1[i] = ReadGolomb(bits);
272             rps->used_by_curr_pic_s1_flag[i] = bits.ReadBit();
273         }
274     }
275 
276     return AP4_SUCCESS;
277 }
278 
279 /*----------------------------------------------------------------------
280 |   NumPicTotalCurr
281 +---------------------------------------------------------------------*/
282 static unsigned int
NumPicTotalCurr(const AP4_HevcShortTermRefPicSet * rps,const AP4_HevcSliceSegmentHeader * slice_segment_header)283 NumPicTotalCurr(const AP4_HevcShortTermRefPicSet* rps,
284                 const AP4_HevcSliceSegmentHeader* slice_segment_header)
285 {
286     // compute NumPicTotalCurr
287     unsigned int nptc = 0;
288     if (rps) {
289         for (unsigned int i=0; i<rps->num_negative_pics; i++) {
290             if (rps->used_by_curr_pic_s0_flag[i]) {
291                 ++nptc;
292             }
293         }
294         for (unsigned int i=0; i<rps->num_positive_pics; i++) {
295             if (rps->used_by_curr_pic_s1_flag[i]) {
296                 ++nptc;
297             }
298         }
299     }
300     for (unsigned int i=0; i<slice_segment_header->num_long_term_sps + slice_segment_header->num_long_term_pics; i++) {
301         if (slice_segment_header->used_by_curr_pic_lt_flag[i]) {
302             ++nptc;
303         }
304     }
305     // TODO: for now we assume pps_curr_pic_ref_enabled is 0
306     //if (pps_curr_pic_ref_enabled) {
307     //    ++nptc;
308     //}
309 
310     return nptc;
311 }
312 
313 /*----------------------------------------------------------------------
314 |   AP4_HevcSliceSegmentHeader::Parse
315 +---------------------------------------------------------------------*/
316 AP4_Result
Parse(const AP4_UI08 * data,unsigned int data_size,unsigned int nal_unit_type,AP4_HevcPictureParameterSet ** picture_parameter_sets,AP4_HevcSequenceParameterSet ** sequence_parameter_sets)317 AP4_HevcSliceSegmentHeader::Parse(const AP4_UI08*                data,
318                                   unsigned int                   data_size,
319                                   unsigned int                   nal_unit_type,
320                                   AP4_HevcPictureParameterSet**  picture_parameter_sets,
321                                   AP4_HevcSequenceParameterSet** sequence_parameter_sets) {
322     // initialize all members to 0
323     AP4_SetMemory(this, 0, sizeof(*this));
324 
325     // some fields default to 1
326     pic_output_flag = 1;
327 
328     // start the parser
329     AP4_DataBuffer unescaped(data, data_size);
330     AP4_NalParser::Unescape(unescaped);
331     AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
332 
333     first_slice_segment_in_pic_flag = bits.ReadBit();
334     if (nal_unit_type >= AP4_HEVC_NALU_TYPE_BLA_W_LP && nal_unit_type <= AP4_HEVC_NALU_TYPE_RSV_IRAP_VCL23) {
335         no_output_of_prior_pics_flag = bits.ReadBit();
336     }
337     slice_pic_parameter_set_id = ReadGolomb(bits);
338     if (slice_pic_parameter_set_id > AP4_HEVC_PPS_MAX_ID) {
339         return AP4_ERROR_INVALID_FORMAT;
340     }
341     const AP4_HevcPictureParameterSet* pps = picture_parameter_sets[slice_pic_parameter_set_id];
342     if (pps == NULL) {
343         return AP4_ERROR_INVALID_FORMAT;
344     }
345     const AP4_HevcSequenceParameterSet* sps = sequence_parameter_sets[pps->pps_seq_parameter_set_id];
346     if (sps == NULL) {
347         return AP4_ERROR_INVALID_FORMAT;
348     }
349 
350     // PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY
351     // PicWidthInCtbsY = Ceil( pic_width_in_luma_samples / CtbSizeY )
352     // PicHeightInCtbsY = Ceil( pic_height_in_luma_samples / CtbSizeY )
353     // CtbSizeY = 1 << CtbLog2SizeY
354     // CtbLog2SizeY = MinCbLog2SizeY + log2_diff_max_min_luma_coding_block_size
355     // MinCbLog2SizeY = log2_min_luma_coding_block_size_minus3 + 3
356     unsigned int MinCbLog2SizeY   = sps->log2_min_luma_coding_block_size_minus3 + 3;
357     unsigned int CtbLog2SizeY     = MinCbLog2SizeY + sps->log2_diff_max_min_luma_coding_block_size;
358     unsigned int CtbSizeY         = 1 << CtbLog2SizeY;
359     unsigned int PicWidthInCtbsY  = (sps->pic_width_in_luma_samples + CtbSizeY - 1) / CtbSizeY;
360     unsigned int PicHeightInCtbsY = (sps->pic_height_in_luma_samples + CtbSizeY - 1) / CtbSizeY;
361     unsigned int PicSizeInCtbsY   = PicWidthInCtbsY * PicHeightInCtbsY;
362 
363     if (!first_slice_segment_in_pic_flag) {
364         if (pps->dependent_slice_segments_enabled_flag) {
365             dependent_slice_segment_flag = bits.ReadBit();
366         }
367 
368         unsigned int bits_needed = BitsNeeded(PicSizeInCtbsY);
369         if (bits_needed) {
370             slice_segment_address = bits.ReadBits(bits_needed);
371         }
372     }
373 
374     if (!dependent_slice_segment_flag) {
375         if (pps->num_extra_slice_header_bits) {
376             bits.ReadBits(pps->num_extra_slice_header_bits); // slice_reserved_flag[...]
377         }
378 
379         slice_type = ReadGolomb(bits);
380         if (slice_type != AP4_HEVC_SLICE_TYPE_B && slice_type != AP4_HEVC_SLICE_TYPE_P && slice_type != AP4_HEVC_SLICE_TYPE_I) {
381             return AP4_ERROR_INVALID_FORMAT;
382         }
383         if (pps->output_flag_present_flag) {
384             pic_output_flag = bits.ReadBit();
385         }
386         if (sps->separate_colour_plane_flag) {
387             colour_plane_id = bits.ReadBits(2);
388         }
389         unsigned int slice_sao_luma_flag = 0;
390         unsigned int slice_sao_chroma_flag = 0;
391         unsigned int slice_deblocking_filter_disabled_flag = 0;
392         unsigned int slice_temporal_mvp_enabled_flag = 0;
393         const AP4_HevcShortTermRefPicSet* rps = NULL;
394         if (nal_unit_type != AP4_HEVC_NALU_TYPE_IDR_W_RADL && nal_unit_type != AP4_HEVC_NALU_TYPE_IDR_N_LP) {
395             slice_pic_order_cnt_lsb = bits.ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4+4);
396             short_term_ref_pic_set_sps_flag = bits.ReadBit();
397             if (!short_term_ref_pic_set_sps_flag) {
398                 AP4_Result result = parse_st_ref_pic_set(&short_term_ref_pic_set,
399                                                          sps,
400                                                          sps->num_short_term_ref_pic_sets,
401                                                          sps->num_short_term_ref_pic_sets,
402                                                          bits);
403                 if (AP4_FAILED(result)) return result;
404                 rps = &short_term_ref_pic_set;
405             } else if (sps->num_short_term_ref_pic_sets > 1) {
406                 short_term_ref_pic_set_idx = bits.ReadBits(BitsNeeded(sps->num_short_term_ref_pic_sets));
407                 rps = &sps->short_term_ref_pic_sets[short_term_ref_pic_set_idx];
408             }
409 
410             if (sps->long_term_ref_pics_present_flag) {
411                 if (sps->num_long_term_ref_pics_sps > 0) {
412                     num_long_term_sps = ReadGolomb(bits);
413                 }
414                 num_long_term_pics = ReadGolomb(bits);
415 
416                 if (num_long_term_sps > sps->num_long_term_ref_pics_sps) {
417                     return AP4_ERROR_INVALID_FORMAT;
418                 }
419                 if (num_long_term_sps + num_long_term_pics > AP4_HEVC_MAX_LT_REFS) {
420                     return AP4_ERROR_INVALID_FORMAT;
421                 }
422                 for (unsigned int i=0; i<num_long_term_sps + num_long_term_pics; i++) {
423                     if (i < num_long_term_sps) {
424                         if (sps->num_long_term_ref_pics_sps > 1) {
425                             /* lt_idx_sps[i] = */ bits.ReadBits(BitsNeeded(sps->num_long_term_ref_pics_sps));
426                         }
427                     } else {
428                         /* poc_lsb_lt[i] = */ bits.ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4+4);
429                         used_by_curr_pic_lt_flag[i] = bits.ReadBit();
430                     }
431                     unsigned int delta_poc_msb_present_flag /*[i]*/ = bits.ReadBit();
432                     if (delta_poc_msb_present_flag /*[i]*/) {
433                         /* delta_poc_msb_cycle_lt[i] = */ ReadGolomb(bits);
434                     }
435                 }
436             }
437             if (sps->sps_temporal_mvp_enabled_flag) {
438                 slice_temporal_mvp_enabled_flag = bits.ReadBit();
439             }
440         }
441         if (sps->sample_adaptive_offset_enabled_flag) {
442             slice_sao_luma_flag   = bits.ReadBit();
443             unsigned int ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
444             if (ChromaArrayType) {
445                 slice_sao_chroma_flag = bits.ReadBit();
446             }
447         }
448         if (slice_type == AP4_HEVC_SLICE_TYPE_P || slice_type == AP4_HEVC_SLICE_TYPE_B) {
449             unsigned int num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
450             unsigned int num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
451             unsigned int num_ref_idx_active_override_flag = bits.ReadBit();
452             if (num_ref_idx_active_override_flag) {
453                 num_ref_idx_l0_active_minus1 = ReadGolomb(bits);
454                 if (slice_type == AP4_HEVC_SLICE_TYPE_B) {
455                     num_ref_idx_l1_active_minus1 = ReadGolomb(bits);
456                 }
457             }
458             if (num_ref_idx_l0_active_minus1 > 14 || num_ref_idx_l1_active_minus1 > 14) {
459                 return AP4_ERROR_INVALID_FORMAT;
460             }
461             unsigned int nptc = NumPicTotalCurr(rps, this);
462             if (pps->lists_modification_present_flag && nptc > 1) {
463                 // ref_pic_lists_modification
464                 unsigned int ref_pic_list_modification_flag_l0 = bits.ReadBit();
465                 if (ref_pic_list_modification_flag_l0) {
466                     for (unsigned int i=0; i<=num_ref_idx_l0_active_minus1; i++) {
467                         /* list_entry_l0[i]; */ bits.ReadBits(BitsNeeded(nptc));
468                     }
469                 }
470                 if (slice_type == AP4_HEVC_SLICE_TYPE_B) {
471                     unsigned int ref_pic_list_modification_flag_l1 = bits.ReadBit();
472                     if (ref_pic_list_modification_flag_l1) {
473                         for (unsigned int i=0; i<=num_ref_idx_l1_active_minus1; i++) {
474                             /* list_entry_l1[i]; */ bits.ReadBits(BitsNeeded(nptc));
475                         }
476                     }
477                 }
478             }
479             if (slice_type == AP4_HEVC_SLICE_TYPE_B) {
480                 /* mvd_l1_zero_flag = */ bits.ReadBit();
481             }
482             if (pps->cabac_init_present_flag) {
483                 /* cabac_init_flag = */ bits.ReadBit();
484             }
485             if (slice_temporal_mvp_enabled_flag) {
486                 unsigned int collocated_from_l0_flag = 1;
487                 if (slice_type == AP4_HEVC_SLICE_TYPE_B) {
488                     collocated_from_l0_flag = bits.ReadBit();
489                 }
490                 if (( collocated_from_l0_flag && num_ref_idx_l0_active_minus1 > 0) ||
491                     (!collocated_from_l0_flag && num_ref_idx_l1_active_minus1 > 0)) {
492                     /* collocated_ref_idx = */ ReadGolomb(bits);
493                 }
494             }
495             if ((pps->weighted_pred_flag   && slice_type == AP4_HEVC_SLICE_TYPE_P) ||
496                 (pps->weighted_bipred_flag && slice_type == AP4_HEVC_SLICE_TYPE_B)) {
497                 // +++ pred_weight_table()
498                 /* luma_log2_weight_denom = */ ReadGolomb(bits);
499                 if (sps->chroma_format_idc != 0) {
500                     /* delta_chroma_log2_weight_denom = */ /* SignedGolomb( */ ReadGolomb(bits) /*)*/;
501                 }
502                 unsigned int luma_weight_l0_flag[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
503                 for (unsigned int i=0; i<=num_ref_idx_l0_active_minus1; i++) {
504                     luma_weight_l0_flag[i] = bits.ReadBit();
505                 }
506                 unsigned int chroma_weight_l0_flag[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
507                 if (sps->chroma_format_idc != 0) {
508                     for (unsigned int i=0; i<=num_ref_idx_l0_active_minus1; i++) {
509                         chroma_weight_l0_flag[i] = bits.ReadBit();
510                     }
511                 }
512                 for (unsigned int i=0; i<=num_ref_idx_l0_active_minus1; i++) {
513                     if (luma_weight_l0_flag[i]) {
514                         /* delta_luma_weight_l0[i] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
515                         /* luma_offset_l0[i] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
516                     }
517                     if (chroma_weight_l0_flag[i]) {
518                         for (unsigned int j=0; j<2; j++) {
519                             /* delta_chroma_weight_l0[i][j] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
520                             /* delta_chroma_offset_l0[i][j] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
521                         }
522                     }
523                 }
524                 if (slice_type == AP4_HEVC_SLICE_TYPE_B) {
525                     unsigned int luma_weight_l1_flag[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
526                     for (unsigned int i=0; i<=num_ref_idx_l1_active_minus1; i++) {
527                         luma_weight_l1_flag[i] = bits.ReadBit();
528                     }
529                     unsigned int chroma_weight_l1_flag[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
530                     if (sps->chroma_format_idc != 0) {
531                         for (unsigned int i=0; i<=num_ref_idx_l1_active_minus1; i++) {
532                             chroma_weight_l1_flag[i] = bits.ReadBit();
533                         }
534                     }
535                     for (unsigned int i=0; i<=num_ref_idx_l1_active_minus1; i++) {
536                         if (luma_weight_l1_flag[i]) {
537                             /* delta_luma_weight_l1[i] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
538                             /* luma_offset_l1[i] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
539                         }
540                         if (chroma_weight_l1_flag[i]) {
541                             for (unsigned int j=0; j<2; j++) {
542                                 /* delta_chroma_weight_l1[i][j] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
543                                 /* delta_chroma_offset_l1[i][j] = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
544                             }
545                         }
546                     }
547                 }
548                 // --- pred_weight_table()
549             }
550             /* five_minus_max_num_merge_cand = */ ReadGolomb(bits);
551         }
552         /* slice_qp_delta = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
553         if (pps->pps_slice_chroma_qp_offsets_present_flag) {
554             /* slice_cb_qp_offset = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
555             /* slice_cr_qp_offset = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
556         }
557         unsigned int deblocking_filter_override_flag = 0;
558         if (pps->deblocking_filter_override_enabled_flag) {
559             deblocking_filter_override_flag = bits.ReadBit();
560         }
561         if (deblocking_filter_override_flag) {
562             slice_deblocking_filter_disabled_flag = bits.ReadBit();
563             if (!slice_deblocking_filter_disabled_flag) {
564                 /* slice_beta_offset_div2 = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
565                 /* slice_tc_offset_div2   = */ /*SignedGolomb(*/ ReadGolomb(bits) /*)*/;
566             }
567         }
568         if (pps->pps_loop_filter_across_slices_enabled_flag &&
569             (slice_sao_luma_flag || slice_sao_chroma_flag || !slice_deblocking_filter_disabled_flag)) {
570             /* slice_loop_filter_across_slices_enabled_flag = */ bits.ReadBit();
571         }
572     }
573 
574     if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
575         num_entry_point_offsets = ReadGolomb(bits);
576         if (num_entry_point_offsets > 0 ) {
577             offset_len_minus1 = ReadGolomb(bits);
578             if (offset_len_minus1 > 31) {
579                 return AP4_ERROR_INVALID_FORMAT;
580             }
581             for (unsigned int i=0; i<num_entry_point_offsets; i++) {
582                 bits.ReadBits(offset_len_minus1+1);
583             }
584         }
585     }
586 
587     if (pps->slice_segment_header_extension_present_flag) {
588         unsigned int slice_segment_header_extension_length = ReadGolomb(bits);
589         for (unsigned int i=0; i<slice_segment_header_extension_length; i++) {
590             bits.ReadBits(8); // slice_segment_header_extension_data_byte[i]
591         }
592     }
593 
594     // byte_alignment()
595     bits.ReadBit(); // alignment_bit_equal_to_one
596     unsigned int bits_read = bits.GetBitsRead();
597     if (bits_read % 8) {
598         bits.ReadBits(8-(bits_read%8));
599     }
600 
601     /* compute the size */
602     size = bits.GetBitsRead();
603     DBG_PRINTF_2("*** slice segment header size=%d bits (%d bytes)\n", size, size/8);
604 
605     return AP4_SUCCESS;
606 }
607 
608 /*----------------------------------------------------------------------
609 |   AP4_HevcProfileTierLevel::AP4_HevcProfileTierLevel
610 +---------------------------------------------------------------------*/
AP4_HevcProfileTierLevel()611 AP4_HevcProfileTierLevel::AP4_HevcProfileTierLevel() :
612     general_profile_space(0),
613     general_tier_flag(0),
614     general_profile_idc(0),
615     general_profile_compatibility_flags(0),
616     general_constraint_indicator_flags(0),
617     general_level_idc(0)
618 {
619     AP4_SetMemory(&sub_layer_info[0], 0, sizeof(sub_layer_info));
620 }
621 
622 /*----------------------------------------------------------------------
623 |   AP4_HevcProfileTierLevel::Parse
624 +---------------------------------------------------------------------*/
625 AP4_Result
Parse(AP4_BitReader & bits,unsigned int max_num_sub_layers_minus_1)626 AP4_HevcProfileTierLevel::Parse(AP4_BitReader& bits, unsigned int max_num_sub_layers_minus_1)
627 {
628     // profile_tier_level
629     general_profile_space               = bits.ReadBits(2);
630     general_tier_flag                   = bits.ReadBit();
631     general_profile_idc                 = bits.ReadBits(5);
632     general_profile_compatibility_flags = bits.ReadBits(32);
633 
634     general_constraint_indicator_flags  = ((AP4_UI64)bits.ReadBits(16)) << 32;
635     general_constraint_indicator_flags |= bits.ReadBits(32);
636 
637     general_level_idc                   = bits.ReadBits(8);
638     for (unsigned int i = 0; i < max_num_sub_layers_minus_1; i++) {
639         sub_layer_info[i].sub_layer_profile_present_flag = bits.ReadBit();
640         sub_layer_info[i].sub_layer_level_present_flag   = bits.ReadBit();
641     }
642     if (max_num_sub_layers_minus_1) {
643         for (unsigned int i = max_num_sub_layers_minus_1; i < 8; i++) {
644             bits.ReadBits(2); // reserved_zero_2bits[i]
645         }
646     }
647     for (unsigned int i = 0; i < max_num_sub_layers_minus_1; i++) {
648         if (sub_layer_info[i].sub_layer_profile_present_flag) {
649             sub_layer_info[i].sub_layer_profile_space               = bits.ReadBits(2);
650             sub_layer_info[i].sub_layer_tier_flag                   = bits.ReadBit();
651             sub_layer_info[i].sub_layer_profile_idc                 = bits.ReadBits(5);
652             sub_layer_info[i].sub_layer_profile_compatibility_flags = bits.ReadBits(32);
653             sub_layer_info[i].sub_layer_progressive_source_flag     = bits.ReadBit();
654             sub_layer_info[i].sub_layer_interlaced_source_flag      = bits.ReadBit();
655             sub_layer_info[i].sub_layer_non_packed_constraint_flag  = bits.ReadBit();
656             sub_layer_info[i].sub_layer_frame_only_constraint_flag  = bits.ReadBit();
657             bits.ReadBits(32); bits.ReadBits(12); // sub_layer_reserved_zero_44bits
658         }
659         if (sub_layer_info[i].sub_layer_level_present_flag) {
660             sub_layer_info[i].sub_layer_level_idc = bits.ReadBits(8);
661         }
662     }
663 
664     return AP4_SUCCESS;
665 }
666 
667 /*----------------------------------------------------------------------
668 |   AP4_HevcPictureParameterSet::AP4_HevcPictureParameterSet
669 +---------------------------------------------------------------------*/
AP4_HevcPictureParameterSet()670 AP4_HevcPictureParameterSet::AP4_HevcPictureParameterSet() :
671     pps_pic_parameter_set_id(0),
672     pps_seq_parameter_set_id(0),
673     dependent_slice_segments_enabled_flag(0),
674     output_flag_present_flag(0),
675     num_extra_slice_header_bits(0),
676     sign_data_hiding_enabled_flag(0),
677     cabac_init_present_flag(0),
678     num_ref_idx_l0_default_active_minus1(0),
679     num_ref_idx_l1_default_active_minus1(0),
680     init_qp_minus26(0),
681     constrained_intra_pred_flag(0),
682     transform_skip_enabled_flag(0),
683     cu_qp_delta_enabled_flag(0),
684     diff_cu_qp_delta_depth(0),
685     pps_cb_qp_offset(0),
686     pps_cr_qp_offset(0),
687     pps_slice_chroma_qp_offsets_present_flag(0),
688     weighted_pred_flag(0),
689     weighted_bipred_flag(0),
690     transquant_bypass_enabled_flag(0),
691     tiles_enabled_flag(0),
692     entropy_coding_sync_enabled_flag(0),
693     num_tile_columns_minus1(0),
694     num_tile_rows_minus1(0),
695     uniform_spacing_flag(1),
696     loop_filter_across_tiles_enabled_flag(1),
697     pps_loop_filter_across_slices_enabled_flag(0),
698     deblocking_filter_control_present_flag(0),
699     deblocking_filter_override_enabled_flag(0),
700     pps_deblocking_filter_disabled_flag(0),
701     pps_beta_offset_div2(0),
702     pps_tc_offset_div2(0),
703     pps_scaling_list_data_present_flag(0),
704     lists_modification_present_flag(0),
705     log2_parallel_merge_level_minus2(0),
706     slice_segment_header_extension_present_flag(0)
707 {
708 }
709 
710 /*----------------------------------------------------------------------
711 |   AP4_HevcPictureParameterSet::Parse
712 +---------------------------------------------------------------------*/
713 AP4_Result
Parse(const unsigned char * data,unsigned int data_size)714 AP4_HevcPictureParameterSet::Parse(const unsigned char* data, unsigned int data_size)
715 {
716     raw_bytes.SetData(data, data_size);
717 
718     AP4_DataBuffer unescaped(data, data_size);
719     AP4_NalParser::Unescape(unescaped);
720     AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
721 
722     bits.SkipBits(16); // NAL Unit Header
723 
724     pps_pic_parameter_set_id = ReadGolomb(bits);
725     if (pps_pic_parameter_set_id > AP4_HEVC_PPS_MAX_ID) {
726         return AP4_ERROR_INVALID_FORMAT;
727     }
728     pps_seq_parameter_set_id = ReadGolomb(bits);
729     if (pps_seq_parameter_set_id > AP4_HEVC_SPS_MAX_ID) {
730         return AP4_ERROR_INVALID_FORMAT;
731     }
732     dependent_slice_segments_enabled_flag    = bits.ReadBit();
733     output_flag_present_flag                 = bits.ReadBit();
734     num_extra_slice_header_bits              = bits.ReadBits(3);
735     sign_data_hiding_enabled_flag            = bits.ReadBit();
736     cabac_init_present_flag                  = bits.ReadBit();
737     num_ref_idx_l0_default_active_minus1     = ReadGolomb(bits);
738     num_ref_idx_l1_default_active_minus1     = ReadGolomb(bits);
739     init_qp_minus26                          = SignedGolomb(ReadGolomb(bits));
740     constrained_intra_pred_flag              = bits.ReadBit();
741     transform_skip_enabled_flag              = bits.ReadBit();
742     cu_qp_delta_enabled_flag                 = bits.ReadBit();
743     if (cu_qp_delta_enabled_flag) {
744         diff_cu_qp_delta_depth = ReadGolomb(bits);
745     }
746     pps_cb_qp_offset                         = SignedGolomb(ReadGolomb(bits));
747     pps_cr_qp_offset                         = SignedGolomb(ReadGolomb(bits));
748     pps_slice_chroma_qp_offsets_present_flag = bits.ReadBit();
749     weighted_pred_flag                       = bits.ReadBit();
750     weighted_bipred_flag                     = bits.ReadBit();
751     transquant_bypass_enabled_flag           = bits.ReadBit();
752     tiles_enabled_flag                       = bits.ReadBit();
753     entropy_coding_sync_enabled_flag         = bits.ReadBit();
754     if (tiles_enabled_flag) {
755         num_tile_columns_minus1 = ReadGolomb(bits);
756         num_tile_rows_minus1    = ReadGolomb(bits);
757         uniform_spacing_flag    = bits.ReadBit();
758         if (!uniform_spacing_flag) {
759             for (unsigned int i=0; i<num_tile_columns_minus1; i++) {
760                 ReadGolomb(bits); // column_width_minus1[i]
761             }
762             for (unsigned int i = 0; i < num_tile_rows_minus1; i++) {
763                 ReadGolomb(bits); // row_height_minus1[i]
764             }
765         }
766         loop_filter_across_tiles_enabled_flag = bits.ReadBit();
767     }
768     pps_loop_filter_across_slices_enabled_flag = bits.ReadBit();
769     deblocking_filter_control_present_flag     = bits.ReadBit();
770     if (deblocking_filter_control_present_flag) {
771         deblocking_filter_override_enabled_flag = bits.ReadBit();
772         pps_deblocking_filter_disabled_flag     = bits.ReadBit();
773         if (!pps_deblocking_filter_disabled_flag) {
774             pps_beta_offset_div2 = SignedGolomb(ReadGolomb(bits));
775             pps_tc_offset_div2   = SignedGolomb(ReadGolomb(bits));
776         }
777     }
778     pps_scaling_list_data_present_flag = bits.ReadBit();
779     if (pps_scaling_list_data_present_flag) {
780         scaling_list_data(bits);
781     }
782     lists_modification_present_flag = bits.ReadBit();
783     log2_parallel_merge_level_minus2 = ReadGolomb(bits);
784     slice_segment_header_extension_present_flag = bits.ReadBit();
785 
786     return AP4_SUCCESS;
787 }
788 
789 /*----------------------------------------------------------------------
790 |   AP4_HevcSequenceParameterSet::AP4_HevcSequenceParameterSet
791 +---------------------------------------------------------------------*/
AP4_HevcSequenceParameterSet()792 AP4_HevcSequenceParameterSet::AP4_HevcSequenceParameterSet() :
793     sps_video_parameter_set_id(0),
794     sps_max_sub_layers_minus1(0),
795     sps_temporal_id_nesting_flag(0),
796     sps_seq_parameter_set_id(0),
797     chroma_format_idc(0),
798     separate_colour_plane_flag(0),
799     pic_width_in_luma_samples(0),
800     pic_height_in_luma_samples(0),
801     conformance_window_flag(0),
802     conf_win_left_offset(0),
803     conf_win_right_offset(0),
804     conf_win_top_offset(0),
805     conf_win_bottom_offset(0),
806     bit_depth_luma_minus8(0),
807     bit_depth_chroma_minus8(0),
808     log2_max_pic_order_cnt_lsb_minus4(0),
809     sps_sub_layer_ordering_info_present_flag(0),
810     log2_min_luma_coding_block_size_minus3(0),
811     log2_diff_max_min_luma_coding_block_size(0),
812     log2_min_transform_block_size_minus2(0),
813     log2_diff_max_min_transform_block_size(0),
814     max_transform_hierarchy_depth_inter(0),
815     max_transform_hierarchy_depth_intra(0),
816     scaling_list_enabled_flag(0),
817     sps_scaling_list_data_present_flag(0),
818     amp_enabled_flag(0),
819     sample_adaptive_offset_enabled_flag(0),
820     pcm_enabled_flag(0),
821     pcm_sample_bit_depth_luma_minus1(0),
822     pcm_sample_bit_depth_chroma_minus1(0),
823     log2_min_pcm_luma_coding_block_size_minus3(0),
824     log2_diff_max_min_pcm_luma_coding_block_size(0),
825     pcm_loop_filter_disabled_flag(0),
826     num_short_term_ref_pic_sets(0),
827     long_term_ref_pics_present_flag(0),
828     num_long_term_ref_pics_sps(0),
829     sps_temporal_mvp_enabled_flag(0),
830     strong_intra_smoothing_enabled_flag(0)
831 {
832     AP4_SetMemory(&profile_tier_level, 0, sizeof(profile_tier_level));
833     for (unsigned int i=0; i<8; i++) {
834         sps_max_dec_pic_buffering_minus1[i] = 0;
835         sps_max_num_reorder_pics[i]         = 0;
836         sps_max_latency_increase_plus1[i]   = 0;
837     }
838     AP4_SetMemory(short_term_ref_pic_sets, 0, sizeof(short_term_ref_pic_sets));
839 }
840 
841 /*----------------------------------------------------------------------
842 |   AP4_HevcSequenceParameterSet::Parse
843 +---------------------------------------------------------------------*/
844 AP4_Result
Parse(const unsigned char * data,unsigned int data_size)845 AP4_HevcSequenceParameterSet::Parse(const unsigned char* data, unsigned int data_size)
846 {
847     raw_bytes.SetData(data, data_size);
848 
849     AP4_DataBuffer unescaped(data, data_size);
850     AP4_NalParser::Unescape(unescaped);
851     AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
852 
853     bits.SkipBits(16); // NAL Unit Header
854 
855     sps_video_parameter_set_id   = bits.ReadBits(4);
856     sps_max_sub_layers_minus1    = bits.ReadBits(3);
857     sps_temporal_id_nesting_flag = bits.ReadBit();
858 
859     AP4_Result result = profile_tier_level.Parse(bits, sps_max_sub_layers_minus1);
860     if (AP4_FAILED(result)) {
861         return result;
862     }
863 
864     sps_seq_parameter_set_id = ReadGolomb(bits);
865     if (sps_seq_parameter_set_id > AP4_HEVC_SPS_MAX_ID) {
866         return AP4_ERROR_INVALID_FORMAT;
867     }
868 
869     chroma_format_idc = ReadGolomb(bits);
870     if (chroma_format_idc == 3) {
871         separate_colour_plane_flag = bits.ReadBit();
872     }
873     pic_width_in_luma_samples  = ReadGolomb(bits);
874     pic_height_in_luma_samples = ReadGolomb(bits);
875     conformance_window_flag    = bits.ReadBit();
876 
877     if (conformance_window_flag) {
878         conf_win_left_offset    = ReadGolomb(bits);
879         conf_win_right_offset   = ReadGolomb(bits);
880         conf_win_top_offset     = ReadGolomb(bits);
881         conf_win_bottom_offset  = ReadGolomb(bits);
882     }
883     bit_depth_luma_minus8                    = ReadGolomb(bits);
884     bit_depth_chroma_minus8                  = ReadGolomb(bits);
885     log2_max_pic_order_cnt_lsb_minus4        = ReadGolomb(bits);
886     if (log2_max_pic_order_cnt_lsb_minus4 > 16) {
887         return AP4_ERROR_INVALID_FORMAT;
888     }
889     sps_sub_layer_ordering_info_present_flag = bits.ReadBit();
890     for (unsigned int i = (sps_sub_layer_ordering_info_present_flag ? 0 : sps_max_sub_layers_minus1);
891                       i <= sps_max_sub_layers_minus1;
892                       i++) {
893         sps_max_dec_pic_buffering_minus1[i] = ReadGolomb(bits);
894         sps_max_num_reorder_pics[i]         = ReadGolomb(bits);
895         sps_max_latency_increase_plus1[i]   = ReadGolomb(bits);
896     }
897     log2_min_luma_coding_block_size_minus3   = ReadGolomb(bits);
898     log2_diff_max_min_luma_coding_block_size = ReadGolomb(bits);
899     log2_min_transform_block_size_minus2     = ReadGolomb(bits);
900     log2_diff_max_min_transform_block_size   = ReadGolomb(bits);
901     max_transform_hierarchy_depth_inter      = ReadGolomb(bits);
902     max_transform_hierarchy_depth_intra      = ReadGolomb(bits);
903     scaling_list_enabled_flag                = bits.ReadBit();
904     if (scaling_list_enabled_flag) {
905         sps_scaling_list_data_present_flag = bits.ReadBit();
906         if (sps_scaling_list_data_present_flag) {
907             scaling_list_data(bits);
908         }
909     }
910     amp_enabled_flag = bits.ReadBit();
911     sample_adaptive_offset_enabled_flag = bits.ReadBit();
912     pcm_enabled_flag = bits.ReadBit();
913     if (pcm_enabled_flag) {
914         pcm_sample_bit_depth_luma_minus1 = bits.ReadBits(4);
915         pcm_sample_bit_depth_chroma_minus1 = bits.ReadBits(4);
916         log2_min_pcm_luma_coding_block_size_minus3 = ReadGolomb(bits);
917         log2_diff_max_min_pcm_luma_coding_block_size = ReadGolomb(bits);
918         pcm_loop_filter_disabled_flag = bits.ReadBit();
919     }
920     num_short_term_ref_pic_sets = ReadGolomb(bits);
921     if (num_short_term_ref_pic_sets > AP4_HEVC_SPS_MAX_RPS) {
922         return AP4_ERROR_INVALID_FORMAT;
923     }
924     for (unsigned int i=0; i<num_short_term_ref_pic_sets; i++) {
925         result = parse_st_ref_pic_set(&short_term_ref_pic_sets[i], this, i, num_short_term_ref_pic_sets, bits);
926         if (AP4_FAILED(result)) return result;
927     }
928     long_term_ref_pics_present_flag = bits.ReadBit();
929     if (long_term_ref_pics_present_flag) {
930         num_long_term_ref_pics_sps = ReadGolomb(bits);
931         for (unsigned int i=0; i<num_long_term_ref_pics_sps; i++) {
932             /* lt_ref_pic_poc_lsb_sps[i] = */ bits.ReadBits(log2_max_pic_order_cnt_lsb_minus4 + 4);
933             /* used_by_curr_pic_lt_sps_flag[i] = */ bits.ReadBit();
934         }
935     }
936     sps_temporal_mvp_enabled_flag  = bits.ReadBit();
937     strong_intra_smoothing_enabled_flag = bits.ReadBit();
938 
939     return AP4_SUCCESS;
940 }
941 
942 /*----------------------------------------------------------------------
943 |   AP4_HevcSequenceParameterSet::GetInfo
944 +---------------------------------------------------------------------*/
945 void
GetInfo(unsigned int & width,unsigned int & height)946 AP4_HevcSequenceParameterSet::GetInfo(unsigned int& width, unsigned int& height)
947 {
948     width  = pic_width_in_luma_samples;
949     height = pic_height_in_luma_samples;
950 }
951 
952 /*----------------------------------------------------------------------
953 |   AP4_HevcVideoParameterSet::AP4_HevcVideoParameterSet
954 +---------------------------------------------------------------------*/
AP4_HevcVideoParameterSet()955 AP4_HevcVideoParameterSet::AP4_HevcVideoParameterSet() :
956     vps_video_parameter_set_id(0),
957     vps_max_layers_minus1(0),
958     vps_max_sub_layers_minus1(0),
959     vps_temporal_id_nesting_flag(0),
960     vps_sub_layer_ordering_info_present_flag(0),
961     vps_max_layer_id(0),
962     vps_num_layer_sets_minus1(0),
963     vps_timing_info_present_flag(0),
964     vps_num_units_in_tick(0),
965     vps_time_scale(0),
966     vps_poc_proportional_to_timing_flag(0),
967     vps_num_ticks_poc_diff_one_minus1(0)
968 {
969     AP4_SetMemory(&profile_tier_level, 0, sizeof(profile_tier_level));
970     for (unsigned int i=0; i<8; i++) {
971         vps_max_dec_pic_buffering_minus1[i] = 0;
972         vps_max_num_reorder_pics[i]         = 0;
973         vps_max_latency_increase_plus1[i]   = 0;
974     }
975 }
976 
977 /*----------------------------------------------------------------------
978 |   AP4_HevcVideoParameterSet::GetInfo
979 +---------------------------------------------------------------------*/
980 void
GetInfo(unsigned int & time_scale,unsigned int & num_units)981 AP4_HevcVideoParameterSet::GetInfo(unsigned int& time_scale, unsigned int& num_units)
982 {
983     time_scale = vps_time_scale;
984     num_units  = vps_num_units_in_tick;
985 }
986 
987 /*----------------------------------------------------------------------
988 |   AP4_HevcVideoParameterSet::Parse
989 +---------------------------------------------------------------------*/
990 AP4_Result
Parse(const unsigned char * data,unsigned int data_size)991 AP4_HevcVideoParameterSet::Parse(const unsigned char* data, unsigned int data_size)
992 {
993     raw_bytes.SetData(data, data_size);
994 
995     AP4_DataBuffer unescaped(data, data_size);
996     AP4_NalParser::Unescape(unescaped);
997     AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
998 
999     bits.SkipBits(16); // NAL Unit Header
1000 
1001     vps_video_parameter_set_id     = bits.ReadBits(4);
1002     /* vps_reserved_three_2bits */   bits.ReadBits(2);
1003     vps_max_layers_minus1          = bits.ReadBits(6);
1004     vps_max_sub_layers_minus1      = bits.ReadBits(3);
1005     vps_temporal_id_nesting_flag   = bits.ReadBit();
1006     /* vps_reserved_0xffff_16bits */ bits.ReadBits(16);
1007     profile_tier_level.Parse(bits, vps_max_sub_layers_minus1);
1008     vps_sub_layer_ordering_info_present_flag = bits.ReadBit();
1009     for (unsigned int i = (vps_sub_layer_ordering_info_present_flag ? 0 : vps_max_sub_layers_minus1);
1010                       i <= vps_max_sub_layers_minus1;
1011                       i++) {
1012         vps_max_dec_pic_buffering_minus1[i] = ReadGolomb(bits);
1013         vps_max_num_reorder_pics[i]         = ReadGolomb(bits);
1014         vps_max_latency_increase_plus1[i]   = ReadGolomb(bits);
1015     }
1016     vps_max_layer_id          = bits.ReadBits(6);
1017     vps_num_layer_sets_minus1 = ReadGolomb(bits);
1018     for (unsigned int i = 1; i <= vps_num_layer_sets_minus1; i++) {
1019         for (unsigned int j = 0; j <= vps_max_layer_id; j++) {
1020             bits.ReadBit();
1021         }
1022     }
1023     vps_timing_info_present_flag = bits.ReadBit();
1024     if (vps_timing_info_present_flag) {
1025         vps_num_units_in_tick               = bits.ReadBits(32);
1026         vps_time_scale                      = bits.ReadBits(32);
1027         vps_poc_proportional_to_timing_flag = bits.ReadBit();
1028         if (vps_poc_proportional_to_timing_flag) {
1029             vps_num_ticks_poc_diff_one_minus1 = ReadGolomb(bits);
1030         }
1031     }
1032 
1033     return AP4_SUCCESS;
1034 }
1035 
1036 /*----------------------------------------------------------------------
1037 |   AP4_HevcFrameParser::AP4_HevcFrameParser
1038 +---------------------------------------------------------------------*/
AP4_HevcFrameParser()1039 AP4_HevcFrameParser::AP4_HevcFrameParser() :
1040     m_CurrentSlice(NULL),
1041     m_CurrentNalUnitType(0),
1042     m_CurrentTemporalId(0),
1043     m_TotalNalUnitCount(0),
1044     m_TotalAccessUnitCount(0),
1045     m_AccessUnitFlags(0),
1046     m_VclNalUnitsInAccessUnit(0),
1047     m_PrevTid0Pic_PicOrderCntMsb(0),
1048     m_PrevTid0Pic_PicOrderCntLsb(0)
1049 {
1050     for (unsigned int i=0; i<=AP4_HEVC_PPS_MAX_ID; i++) {
1051         m_PPS[i] = NULL;
1052     }
1053     for (unsigned int i=0; i<=AP4_HEVC_SPS_MAX_ID; i++) {
1054         m_SPS[i] = NULL;
1055     }
1056     for (unsigned int i=0; i<=AP4_HEVC_VPS_MAX_ID; i++) {
1057         m_VPS[i] = NULL;
1058     }
1059 }
1060 
1061 /*----------------------------------------------------------------------
1062 |   AP4_HevcFrameParser::~AP4_HevcFrameParser
1063 +---------------------------------------------------------------------*/
~AP4_HevcFrameParser()1064 AP4_HevcFrameParser::~AP4_HevcFrameParser()
1065 {
1066     delete m_CurrentSlice;
1067 
1068     for (unsigned int i=0; i<=AP4_HEVC_PPS_MAX_ID; i++) {
1069         delete m_PPS[i];
1070     }
1071     for (unsigned int i=0; i<=AP4_HEVC_SPS_MAX_ID; i++) {
1072         delete m_SPS[i];
1073     }
1074     for (unsigned int i=0; i<=AP4_HEVC_VPS_MAX_ID; i++) {
1075         delete m_VPS[i];
1076     }
1077 
1078     // cleanup any un-transfered buffers
1079     for (unsigned int i=0; i<m_AccessUnitData.ItemCount(); i++) {
1080         delete m_AccessUnitData[i];
1081     }
1082 }
1083 
1084 /*----------------------------------------------------------------------
1085 |   AP4_HevcFrameParser::AppendNalUnitData
1086 +---------------------------------------------------------------------*/
1087 void
AppendNalUnitData(const unsigned char * data,unsigned int data_size)1088 AP4_HevcFrameParser::AppendNalUnitData(const unsigned char* data, unsigned int data_size)
1089 {
1090     m_AccessUnitData.Append(new AP4_DataBuffer(data, data_size));
1091 }
1092 
1093 /*----------------------------------------------------------------------
1094 |   AP4_HevcFrameParser::CheckIfAccessUnitIsCompleted
1095 +---------------------------------------------------------------------*/
1096 void
CheckIfAccessUnitIsCompleted(AccessUnitInfo & access_unit_info)1097 AP4_HevcFrameParser::CheckIfAccessUnitIsCompleted(AccessUnitInfo& access_unit_info)
1098 {
1099     if (!m_VclNalUnitsInAccessUnit || !m_CurrentSlice) {
1100         return;
1101     }
1102     DBG_PRINTF_0("\n>>>>>>> New Access Unit\n");
1103 
1104     AP4_HevcSequenceParameterSet* sps = m_SPS[m_CurrentSlice->slice_pic_parameter_set_id];
1105     if (sps == NULL) return;
1106 
1107     unsigned int MaxPicOrderCntLsb = (1 << (sps->log2_max_pic_order_cnt_lsb_minus4+4));
1108     bool NoRaslOutputFlag = false;
1109     if (m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_IRAP) {
1110         if ((m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_IDR) ||
1111             (m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_BLA)
1112             /* TODO: check for end-of-sequence */) {
1113             NoRaslOutputFlag = true;
1114         }
1115     }
1116     unsigned int PrevPicOrderCntLsb = 0;
1117     unsigned int PrevPicOrderCntMsb = 0;
1118     if (!((m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_IRAP) && NoRaslOutputFlag)) {
1119         PrevPicOrderCntLsb = m_PrevTid0Pic_PicOrderCntLsb;
1120         PrevPicOrderCntMsb = m_PrevTid0Pic_PicOrderCntMsb;
1121     }
1122     unsigned int PicOrderCntMsb = 0;
1123     if (m_CurrentSlice->slice_pic_order_cnt_lsb < PrevPicOrderCntLsb &&
1124         (PrevPicOrderCntLsb - m_CurrentSlice->slice_pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)) {
1125         PicOrderCntMsb = PrevPicOrderCntMsb + MaxPicOrderCntLsb;
1126     } else if (m_CurrentSlice->slice_pic_order_cnt_lsb > PrevPicOrderCntLsb &&
1127                (m_CurrentSlice->slice_pic_order_cnt_lsb - PrevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)) {
1128         PicOrderCntMsb = PrevPicOrderCntMsb - MaxPicOrderCntLsb;
1129     } else {
1130         PicOrderCntMsb = PrevPicOrderCntMsb;
1131     }
1132 
1133     if (m_CurrentNalUnitType == AP4_HEVC_NALU_TYPE_BLA_N_LP ||
1134         m_CurrentNalUnitType == AP4_HEVC_NALU_TYPE_BLA_W_LP ||
1135         m_CurrentNalUnitType == AP4_HEVC_NALU_TYPE_BLA_W_RADL) {
1136         PicOrderCntMsb = 0;
1137     }
1138     unsigned int PicOrderCntVal = PicOrderCntMsb + m_CurrentSlice->slice_pic_order_cnt_lsb;
1139 
1140     if (m_CurrentTemporalId == 0 && (
1141         !(m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_RADL) ||
1142         !(m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_RASL) ||
1143         !(m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_SUBLAYER_NON_REF))) {
1144         m_PrevTid0Pic_PicOrderCntLsb = m_CurrentSlice->slice_pic_order_cnt_lsb;
1145         m_PrevTid0Pic_PicOrderCntMsb = PicOrderCntMsb;
1146     }
1147 
1148     // emit the access unit (transfer ownership)
1149     access_unit_info.nal_units        = m_AccessUnitData;
1150     access_unit_info.decode_order     = m_TotalAccessUnitCount;
1151     access_unit_info.is_random_access = (m_AccessUnitFlags & AP4_HEVC_ACCESS_UNIT_FLAG_IS_IRAP) ? true : false;
1152     access_unit_info.display_order    = PicOrderCntVal;
1153     m_AccessUnitData.Clear();
1154     m_VclNalUnitsInAccessUnit  = 0;
1155     m_AccessUnitFlags          = 0;
1156     delete m_CurrentSlice;
1157     m_CurrentSlice = NULL;
1158     ++m_TotalAccessUnitCount;
1159 }
1160 
1161 /*----------------------------------------------------------------------
1162 |   AP4_HevcFrameParser::Feed
1163 +---------------------------------------------------------------------*/
1164 AP4_Result
Feed(const void * data,AP4_Size data_size,AP4_Size & bytes_consumed,AccessUnitInfo & access_unit_info,bool eos)1165 AP4_HevcFrameParser::Feed(const void*     data,
1166                           AP4_Size        data_size,
1167                           AP4_Size&       bytes_consumed,
1168                           AccessUnitInfo& access_unit_info,
1169                           bool            eos)
1170 {
1171     const AP4_DataBuffer* nal_unit = NULL;
1172 
1173     // feed the NAL unit parser
1174     AP4_Result result = m_NalParser.Feed(data, data_size, bytes_consumed, nal_unit, eos);
1175     if (AP4_FAILED(result)) {
1176         return result;
1177     }
1178 
1179     if (bytes_consumed < data_size) {
1180         // there will be more to parse
1181         eos = false;
1182     }
1183 
1184     return Feed(nal_unit ? nal_unit->GetData() : NULL,
1185                 nal_unit ? nal_unit->GetDataSize() : 0,
1186                 access_unit_info,
1187                 eos);
1188 }
1189 
1190 /*----------------------------------------------------------------------
1191 |   AP4_HevcFrameParser::Feed
1192 +---------------------------------------------------------------------*/
1193 AP4_Result
Feed(const AP4_UI08 * nal_unit,AP4_Size nal_unit_size,AccessUnitInfo & access_unit_info,bool last_unit)1194 AP4_HevcFrameParser::Feed(const AP4_UI08* nal_unit,
1195                           AP4_Size        nal_unit_size,
1196                           AccessUnitInfo& access_unit_info,
1197                           bool            last_unit)
1198 {
1199     AP4_Result result;
1200 
1201     // default return values
1202     access_unit_info.Reset();
1203 
1204     if (nal_unit && nal_unit_size >= 2) {
1205         unsigned int nal_unit_type    = (nal_unit[0] >> 1) & 0x3F;
1206         unsigned int nuh_layer_id     = (((nal_unit[0] & 1) << 5) | (nal_unit[1] >> 3));
1207         unsigned int nuh_temporal_id  = nal_unit[1] & 0x7;
1208         (void)nuh_layer_id;
1209 
1210         if (nuh_temporal_id-- == 0) {
1211             // illegal value, ignore this NAL unit
1212             return AP4_SUCCESS;
1213         }
1214 
1215         m_CurrentNalUnitType = nal_unit_type;
1216         m_CurrentTemporalId  = nuh_temporal_id;
1217         const char* nal_unit_type_name = AP4_HevcNalParser::NaluTypeName(nal_unit_type);
1218         if (nal_unit_type_name == NULL) nal_unit_type_name = "UNKNOWN";
1219         DBG_PRINTF_6("NALU %5d: layer_id=%d, temporal_id=%d, size=%5d, type=%02d (%s) ",
1220                m_TotalNalUnitCount,
1221                nuh_layer_id,
1222                nuh_temporal_id,
1223                nal_unit_size,
1224                nal_unit_type,
1225                nal_unit_type_name);
1226 
1227         // parse the NAL unit details and react accordingly
1228         if (nal_unit_type < AP4_HEVC_NALU_TYPE_VPS_NUT) {
1229             // this is a VCL NAL Unit
1230             AP4_HevcSliceSegmentHeader* slice_header = new AP4_HevcSliceSegmentHeader;
1231             result = slice_header->Parse(nal_unit+2, nal_unit_size-2, nal_unit_type, &m_PPS[0], &m_SPS[0]);
1232             if (AP4_FAILED(result)) {
1233                 DBG_PRINTF_1("VCL parsing failed (%d)", result);
1234                 return AP4_ERROR_INVALID_FORMAT;
1235             }
1236 
1237 #if defined(AP4_HEVC_PARSER_ENABLE_DEBUG)
1238             const char* slice_type_name = AP4_HevcNalParser::SliceTypeName(slice_header->slice_type);
1239             if (slice_type_name == NULL) slice_type_name = "?";
1240             DBG_PRINTF_5(" pps_id=%d, first=%s, slice_type=%d (%s), size=%d, ",
1241                 slice_header->slice_pic_parameter_set_id,
1242                 slice_header->first_slice_segment_in_pic_flag?"YES":"NO",
1243                 slice_header->slice_type,
1244                 slice_type_name,
1245                 slice_header->size);
1246 #endif
1247             if (slice_header->first_slice_segment_in_pic_flag) {
1248                 CheckIfAccessUnitIsCompleted(access_unit_info);
1249             }
1250 
1251             // compute access unit flags
1252             m_AccessUnitFlags = 0;
1253             if (nal_unit_type >= AP4_HEVC_NALU_TYPE_BLA_W_LP && nal_unit_type <= AP4_HEVC_NALU_TYPE_RSV_IRAP_VCL23) {
1254                 m_AccessUnitFlags |= AP4_HEVC_ACCESS_UNIT_FLAG_IS_IRAP;
1255             }
1256             if (nal_unit_type == AP4_HEVC_NALU_TYPE_IDR_W_RADL || nal_unit_type == AP4_HEVC_NALU_TYPE_IDR_N_LP) {
1257                 m_AccessUnitFlags |= AP4_HEVC_ACCESS_UNIT_FLAG_IS_IDR;
1258             }
1259             if (nal_unit_type >= AP4_HEVC_NALU_TYPE_BLA_W_LP && nal_unit_type <= AP4_HEVC_NALU_TYPE_BLA_N_LP) {
1260                 m_AccessUnitFlags |= AP4_HEVC_ACCESS_UNIT_FLAG_IS_BLA;
1261             }
1262             if (nal_unit_type == AP4_HEVC_NALU_TYPE_RADL_N || nal_unit_type == AP4_HEVC_NALU_TYPE_RADL_R) {
1263                 m_AccessUnitFlags |= AP4_HEVC_ACCESS_UNIT_FLAG_IS_RADL;
1264             }
1265             if (nal_unit_type == AP4_HEVC_NALU_TYPE_RASL_N || nal_unit_type == AP4_HEVC_NALU_TYPE_RASL_R) {
1266                 m_AccessUnitFlags |= AP4_HEVC_ACCESS_UNIT_FLAG_IS_RASL;
1267             }
1268             if (nal_unit_type <= AP4_HEVC_NALU_TYPE_RSV_VCL_R15 && ((nal_unit_type & 1) == 0)) {
1269                 m_AccessUnitFlags |= AP4_HEVC_ACCESS_UNIT_FLAG_IS_SUBLAYER_NON_REF;
1270             }
1271 
1272             // make this the current slice if this is the first slice in the access unit
1273             if (m_CurrentSlice == NULL) {
1274                 m_CurrentSlice = slice_header;
1275             }
1276 
1277             // buffer this NAL unit
1278             AppendNalUnitData(nal_unit, nal_unit_size);
1279             ++m_VclNalUnitsInAccessUnit;
1280         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_AUD_NUT) {
1281             unsigned int pic_type = (nal_unit[1]>>5);
1282             const char*  pic_type_name = AP4_HevcNalParser::PicTypeName(pic_type);
1283             if (pic_type_name == NULL) pic_type_name = "UNKNOWN";
1284             DBG_PRINTF_2("[%d:%s]\n", pic_type, pic_type_name);
1285 
1286             CheckIfAccessUnitIsCompleted(access_unit_info);
1287         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_PPS_NUT) {
1288             AP4_HevcPictureParameterSet* pps = new AP4_HevcPictureParameterSet;
1289             result = pps->Parse(nal_unit, nal_unit_size);
1290             if (AP4_FAILED(result)) {
1291                 DBG_PRINTF_0("PPS ERROR!!!");
1292                 delete pps;
1293                 return AP4_ERROR_INVALID_FORMAT;
1294             }
1295             delete m_PPS[pps->pps_pic_parameter_set_id];
1296             m_PPS[pps->pps_pic_parameter_set_id] = pps;
1297             DBG_PRINTF_2("PPS pps_id=%d, sps_id=%d", pps->pps_pic_parameter_set_id, pps->pps_seq_parameter_set_id);
1298 
1299             // keep the PPS with the NAL unit (this is optional)
1300             AppendNalUnitData(nal_unit, nal_unit_size);
1301             CheckIfAccessUnitIsCompleted(access_unit_info);
1302         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_SPS_NUT) {
1303             AP4_HevcSequenceParameterSet* sps = new AP4_HevcSequenceParameterSet;
1304             result = sps->Parse(nal_unit, nal_unit_size);
1305             if (AP4_FAILED(result)) {
1306                 DBG_PRINTF_0("SPS ERROR!!!\n");
1307                 delete sps;
1308                 return AP4_ERROR_INVALID_FORMAT;
1309             }
1310             delete m_SPS[sps->sps_seq_parameter_set_id];
1311             m_SPS[sps->sps_seq_parameter_set_id] = sps;
1312             DBG_PRINTF_2("SPS sps_id=%d, vps_id=%d", sps->sps_seq_parameter_set_id, sps->sps_video_parameter_set_id);
1313 
1314             // keep the SPS with the NAL unit (this is optional)
1315             AppendNalUnitData(nal_unit, nal_unit_size);
1316             CheckIfAccessUnitIsCompleted(access_unit_info);
1317         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_VPS_NUT) {
1318             AP4_HevcVideoParameterSet* vps = new AP4_HevcVideoParameterSet;
1319             result = vps->Parse(nal_unit, nal_unit_size);
1320             if (AP4_FAILED(result)) {
1321                 DBG_PRINTF_0("VPS ERROR!!!\n");
1322                 delete vps;
1323                 return AP4_ERROR_INVALID_FORMAT;
1324             }
1325             delete m_VPS[vps->vps_video_parameter_set_id];
1326             m_VPS[vps->vps_video_parameter_set_id] = vps;
1327             DBG_PRINTF_1("VPS vps_id=%d", vps->vps_video_parameter_set_id);
1328 
1329             // keep the VPS with the NAL unit (this is optional)
1330             AppendNalUnitData(nal_unit, nal_unit_size);
1331             CheckIfAccessUnitIsCompleted(access_unit_info);
1332         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_EOS_NUT ||
1333                    nal_unit_type == AP4_HEVC_NALU_TYPE_EOB_NUT) {
1334             CheckIfAccessUnitIsCompleted(access_unit_info);
1335         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_PREFIX_SEI_NUT) {
1336             CheckIfAccessUnitIsCompleted(access_unit_info);
1337             AppendNalUnitData(nal_unit, nal_unit_size);
1338         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_SUFFIX_SEI_NUT){
1339             AppendNalUnitData(nal_unit, nal_unit_size);
1340         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_UNSPEC62) {
1341             AppendNalUnitData(nal_unit, nal_unit_size);
1342         } else if (nal_unit_type == AP4_HEVC_NALU_TYPE_UNSPEC63) {
1343             AppendNalUnitData(nal_unit, nal_unit_size);
1344         }
1345         DBG_PRINTF_0("\n");
1346         m_TotalNalUnitCount++;
1347     }
1348 
1349     // flush if needed
1350     if (last_unit && access_unit_info.nal_units.ItemCount() == 0) {
1351         DBG_PRINTF_0("------ last unit\n");
1352         CheckIfAccessUnitIsCompleted(access_unit_info);
1353     }
1354 
1355     return AP4_SUCCESS;
1356 }
1357 
1358 /*----------------------------------------------------------------------
1359 |   AP4_HevFrameParser::AccessUnitInfo::Reset
1360 +---------------------------------------------------------------------*/
1361 AP4_Result
ParseSliceSegmentHeader(const AP4_UI08 * data,unsigned int data_size,unsigned int nal_unit_type,AP4_HevcSliceSegmentHeader & slice_header)1362 AP4_HevcFrameParser::ParseSliceSegmentHeader(const AP4_UI08*             data,
1363                                              unsigned int                data_size,
1364                                              unsigned int                nal_unit_type,
1365                                              AP4_HevcSliceSegmentHeader& slice_header)
1366 {
1367     return slice_header.Parse(data, data_size, nal_unit_type, &m_PPS[0], &m_SPS[0]);
1368 }
1369 
1370 /*----------------------------------------------------------------------
1371 |   AP4_HevFrameParser::AccessUnitInfo::Reset
1372 +---------------------------------------------------------------------*/
1373 void
Reset()1374 AP4_HevcFrameParser::AccessUnitInfo::Reset()
1375 {
1376     for (unsigned int i=0; i<nal_units.ItemCount(); i++) {
1377         delete nal_units[i];
1378     }
1379     nal_units.Clear();
1380     is_random_access = false;
1381     decode_order = 0;
1382     display_order = 0;
1383 }
1384