1 /*****************************************************************
2 |
3 | AP4 - AVC Parser
4 |
5 | Copyright 2002-2008 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 "Ap4AvcParser.h"
33 #include "Ap4Utils.h"
34
35 /*----------------------------------------------------------------------
36 | debugging
37 +---------------------------------------------------------------------*/
38 //#define AP4_AVC_PARSER_ENABLE_DEBUG
39 #if defined(AP4_AVC_PARSER_ENABLE_DEBUG)
40 #define DBG_PRINTF_0(_x0) printf(_x0)
41 #define DBG_PRINTF_1(_x0, _x1) printf(_x0, _x1)
42 #define DBG_PRINTF_2(_x0, _x1, _x2) printf(_x0, _x1, _x2)
43 #define DBG_PRINTF_3(_x0, _x1, _x2, _x3) printf(_x0, _x1, _x2, _x3)
44 #define DBG_PRINTF_4(_x0, _x1, _x2, _x3, _x4) printf(_x0, _x1, _x2, _x3, _x4)
45 #define DBG_PRINTF_5(_x0, _x1, _x2, _x3, _x4, _x5) printf(_x0, _x1, _x2, _x3, _x4, _x5)
46 #else
47 #define DBG_PRINTF_0(_x0)
48 #define DBG_PRINTF_1(_x0, _x1)
49 #define DBG_PRINTF_2(_x0, _x1, _x2)
50 #define DBG_PRINTF_3(_x0, _x1, _x2, _x3)
51 #define DBG_PRINTF_4(_x0, _x1, _x2, _x3, _x4)
52 #define DBG_PRINTF_5(_x0, _x1, _x2, _x3, _x4, _x5)
53 #endif
54
55 /*----------------------------------------------------------------------
56 | AP4_AvcNalParser::NaluTypeName
57 +---------------------------------------------------------------------*/
58 const char*
NaluTypeName(unsigned int nalu_type)59 AP4_AvcNalParser::NaluTypeName(unsigned int nalu_type)
60 {
61 switch (nalu_type) {
62 case 0: return "Unspecified";
63 case 1: return "Coded slice of a non-IDR picture";
64 case 2: return "Coded slice data partition A";
65 case 3: return "Coded slice data partition B";
66 case 4: return "Coded slice data partition C";
67 case 5: return "Coded slice of an IDR picture";
68 case 6: return "Supplemental enhancement information (SEI)";
69 case 7: return "Sequence parameter set";
70 case 8: return "Picture parameter set";
71 case 9: return "Access unit delimiter";
72 case 10: return "End of sequence";
73 case 11: return "End of stream";
74 case 12: return "Filler data";
75 case 13: return "Sequence parameter set extension";
76 case 14: return "Prefix NAL unit in scalable extension";
77 case 15: return "Subset sequence parameter set";
78 case 19: return "Coded slice of an auxiliary coded picture without partitioning";
79 case 20: return "Coded slice in scalable extension";
80 case 28: return "Dolby Vision RPU NAL units";
81 case 30: return "Dolby Vision EL NAL units";
82 default: return NULL;
83 }
84 }
85
86 /*----------------------------------------------------------------------
87 | AP4_AvcNalParser::PrimaryPicTypeName
88 +---------------------------------------------------------------------*/
89 const char*
PrimaryPicTypeName(unsigned int primary_pic_type)90 AP4_AvcNalParser::PrimaryPicTypeName(unsigned int primary_pic_type)
91 {
92 switch (primary_pic_type) {
93 case 0: return "I";
94 case 1: return "I, P";
95 case 2: return "I, P, B";
96 case 3: return "SI";
97 case 4: return "SI, SP";
98 case 5: return "I, SI";
99 case 6: return "I, SI, P, SP";
100 case 7: return "I, SI, P, SP, B";
101 default: return NULL;
102 }
103 }
104
105 /*----------------------------------------------------------------------
106 | AP4_AvcNalParser::SliceTypeName
107 +---------------------------------------------------------------------*/
108 const char*
SliceTypeName(unsigned int slice_type)109 AP4_AvcNalParser::SliceTypeName(unsigned int slice_type)
110 {
111 switch (slice_type) {
112 case 0: return "P";
113 case 1: return "B";
114 case 2: return "I";
115 case 3: return "SP";
116 case 4: return "SI";
117 case 5: return "P";
118 case 6: return "B";
119 case 7: return "I";
120 case 8: return "SP";
121 case 9: return "SI";
122 default: return NULL;
123 }
124 }
125
126 /*----------------------------------------------------------------------
127 | AP4_AvcNalParser::AP4_AvcNalParser
128 +---------------------------------------------------------------------*/
AP4_AvcNalParser()129 AP4_AvcNalParser::AP4_AvcNalParser() :
130 AP4_NalParser()
131 {
132 }
133
134 /*----------------------------------------------------------------------
135 | AP4_AvcFrameParser::AP4_AvcFrameParser
136 +---------------------------------------------------------------------*/
AP4_AvcFrameParser()137 AP4_AvcFrameParser::AP4_AvcFrameParser() :
138 m_NalUnitType(0),
139 m_NalRefIdc(0),
140 m_SliceHeader(NULL),
141 m_AccessUnitVclNalUnitCount(0),
142 m_TotalNalUnitCount(0),
143 m_TotalAccessUnitCount(0),
144 m_PrevFrameNum(0),
145 m_PrevFrameNumOffset(0),
146 m_PrevPicOrderCntMsb(0),
147 m_PrevPicOrderCntLsb(0)
148 {
149 for (unsigned int i=0; i<256; i++) {
150 m_PPS[i] = NULL;
151 m_SPS[i] = NULL;
152 }
153 }
154
155 /*----------------------------------------------------------------------
156 | AP4_AvcFrameParser::~AP4_AvcFrameParser
157 +---------------------------------------------------------------------*/
~AP4_AvcFrameParser()158 AP4_AvcFrameParser::~AP4_AvcFrameParser()
159 {
160 for (unsigned int i=0; i<256; i++) {
161 delete m_PPS[i];
162 delete m_SPS[i];
163 }
164
165 delete m_SliceHeader;
166
167 // cleanup any un-transfered buffers
168 for (unsigned int i=0; i<m_AccessUnitData.ItemCount(); i++) {
169 delete m_AccessUnitData[i];
170 }
171 }
172
173 /*----------------------------------------------------------------------
174 | ReadGolomb
175 +---------------------------------------------------------------------*/
176 static unsigned int
ReadGolomb(AP4_BitReader & bits)177 ReadGolomb(AP4_BitReader& bits)
178 {
179 unsigned int leading_zeros = 0;
180 while (bits.ReadBit() == 0) {
181 leading_zeros++;
182 if (leading_zeros > 32) return 0; // safeguard
183 }
184 if (leading_zeros) {
185 return (1<<leading_zeros)-1+bits.ReadBits(leading_zeros);
186 } else {
187 return 0;
188 }
189 }
190
191 /*----------------------------------------------------------------------
192 | SignedGolomb
193 +---------------------------------------------------------------------*/
194 static int
SignedGolomb(unsigned int code_num)195 SignedGolomb(unsigned int code_num)
196 {
197 if (code_num % 2) {
198 return (code_num+1)/2;
199 } else {
200 return -((int)code_num/2);
201 }
202 }
203
204 /*----------------------------------------------------------------------
205 | AP4_AvcSequenceParameterSet::AP4_AvcSequenceParameterSet
206 +---------------------------------------------------------------------*/
AP4_AvcSequenceParameterSet()207 AP4_AvcSequenceParameterSet::AP4_AvcSequenceParameterSet() :
208 profile_idc(0),
209 constraint_set0_flag(0),
210 constraint_set1_flag(0),
211 constraint_set2_flag(0),
212 constraint_set3_flag(0),
213 level_idc(0),
214 seq_parameter_set_id(0),
215 chroma_format_idc(1),
216 separate_colour_plane_flag(0),
217 bit_depth_luma_minus8(0),
218 bit_depth_chroma_minus8(0),
219 qpprime_y_zero_transform_bypass_flag(0),
220 seq_scaling_matrix_present_flag(0),
221 log2_max_frame_num_minus4(0),
222 pic_order_cnt_type(0),
223 log2_max_pic_order_cnt_lsb_minus4(0),
224 delta_pic_order_always_zero_flags(0),
225 offset_for_non_ref_pic(0),
226 offset_for_top_to_bottom_field(0),
227 num_ref_frames_in_pic_order_cnt_cycle(0),
228 num_ref_frames(0),
229 gaps_in_frame_num_value_allowed_flag(0),
230 pic_width_in_mbs_minus1(0),
231 pic_height_in_map_units_minus1(0),
232 frame_mbs_only_flag(0),
233 mb_adaptive_frame_field_flag(0),
234 direct_8x8_inference_flag(0),
235 frame_cropping_flag(0),
236 frame_crop_left_offset(0),
237 frame_crop_right_offset(0),
238 frame_crop_top_offset(0),
239 frame_crop_bottom_offset(0)
240 {
241 AP4_SetMemory(scaling_list_4x4, 0, sizeof(scaling_list_4x4));
242 AP4_SetMemory(use_default_scaling_matrix_4x4, 0, sizeof(use_default_scaling_matrix_4x4));
243 AP4_SetMemory(scaling_list_8x8, 0, sizeof(scaling_list_8x8));
244 AP4_SetMemory(use_default_scaling_matrix_8x8, 0, sizeof(use_default_scaling_matrix_8x8));
245 AP4_SetMemory(offset_for_ref_frame, 0, sizeof(offset_for_ref_frame));
246 }
247
248 /*----------------------------------------------------------------------
249 | AP4_AvcSequenceParameterSet::GetInfo
250 +---------------------------------------------------------------------*/
251 void
GetInfo(unsigned int & width,unsigned int & height)252 AP4_AvcSequenceParameterSet::GetInfo(unsigned int& width, unsigned int& height)
253 {
254 width = (pic_width_in_mbs_minus1+1) * 16;
255 height = (2-frame_mbs_only_flag) * (pic_height_in_map_units_minus1+1) * 16;
256
257 if (frame_cropping_flag) {
258 unsigned int crop_h = 2*(frame_crop_left_offset+frame_crop_right_offset);
259 unsigned int crop_v = 2*(frame_crop_top_offset+frame_crop_bottom_offset)*(2-frame_mbs_only_flag);
260 if (crop_h < width) width -= crop_h;
261 if (crop_v < height) height -= crop_v;
262 }
263 }
264
265 /*----------------------------------------------------------------------
266 | AP4_AvcFrameParser::ParseSPS
267 +---------------------------------------------------------------------*/
268 AP4_Result
ParseSPS(const unsigned char * data,unsigned int data_size,AP4_AvcSequenceParameterSet & sps)269 AP4_AvcFrameParser::ParseSPS(const unsigned char* data,
270 unsigned int data_size,
271 AP4_AvcSequenceParameterSet& sps)
272 {
273 sps.raw_bytes.SetData(data, data_size);
274 AP4_DataBuffer unescaped(data, data_size);
275 AP4_NalParser::Unescape(unescaped);
276 AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
277
278 bits.SkipBits(8); // NAL Unit Type
279
280 sps.profile_idc = bits.ReadBits(8);
281 sps.constraint_set0_flag = bits.ReadBit();
282 sps.constraint_set1_flag = bits.ReadBit();
283 sps.constraint_set2_flag = bits.ReadBit();
284 sps.constraint_set3_flag = bits.ReadBit();
285 bits.SkipBits(4);
286 sps.level_idc = bits.ReadBits(8);
287 sps.seq_parameter_set_id = ReadGolomb(bits);
288 if (sps.seq_parameter_set_id > AP4_AVC_SPS_MAX_ID) {
289 return AP4_ERROR_INVALID_FORMAT;
290 }
291 if (sps.profile_idc == 100 ||
292 sps.profile_idc == 110 ||
293 sps.profile_idc == 122 ||
294 sps.profile_idc == 244 ||
295 sps.profile_idc == 44 ||
296 sps.profile_idc == 83 ||
297 sps.profile_idc == 86) {
298 sps.chroma_format_idc = ReadGolomb(bits);
299 sps.separate_colour_plane_flag = 0;
300 if (sps.chroma_format_idc == 3) {
301 sps.separate_colour_plane_flag = bits.ReadBit();
302 }
303 sps.bit_depth_luma_minus8 = ReadGolomb(bits);
304 sps.bit_depth_chroma_minus8 = ReadGolomb(bits);
305 sps.qpprime_y_zero_transform_bypass_flag = bits.ReadBit();
306 sps.seq_scaling_matrix_present_flag = bits.ReadBit();
307 if (sps.seq_scaling_matrix_present_flag) {
308 for (int i=0; i<(sps.chroma_format_idc != 3 ? 8 : 12); i++) {
309 unsigned int seq_scaling_list_present_flag = bits.ReadBit();
310 if (seq_scaling_list_present_flag) {
311 if (i<6) {
312 int last_scale = 8;
313 int next_scale = 8;
314 for (unsigned int j=0; j<16; j++) {
315 if (next_scale) {
316 int delta_scale = SignedGolomb(ReadGolomb(bits));
317 next_scale = (last_scale + delta_scale + 256) % 256;
318 sps.use_default_scaling_matrix_4x4[i] = (j == 0 && next_scale == 0);
319 }
320 sps.scaling_list_4x4[i].scale[j] = (next_scale == 0 ? last_scale : next_scale);
321 last_scale = sps.scaling_list_4x4[i].scale[j];
322 }
323 } else {
324 int last_scale = 8;
325 int next_scale = 8;
326 for (unsigned int j=0; j<64; j++) {
327 if (next_scale) {
328 int delta_scale = SignedGolomb(ReadGolomb(bits));
329 next_scale = (last_scale + delta_scale + 256) % 256;
330 sps.use_default_scaling_matrix_8x8[i-6] = (j == 0 && next_scale == 0);
331 }
332 sps.scaling_list_8x8[i-6].scale[j] = (next_scale == 0 ? last_scale : next_scale);
333 last_scale = sps.scaling_list_8x8[i-6].scale[j];
334 }
335 }
336 }
337 }
338 }
339 }
340 sps.log2_max_frame_num_minus4 = ReadGolomb(bits);
341 sps.pic_order_cnt_type = ReadGolomb(bits);
342 if (sps.pic_order_cnt_type > 2) {
343 return AP4_ERROR_INVALID_FORMAT;
344 }
345 if (sps.pic_order_cnt_type == 0) {
346 sps.log2_max_pic_order_cnt_lsb_minus4 = ReadGolomb(bits);
347 } else if (sps.pic_order_cnt_type == 1) {
348 sps.delta_pic_order_always_zero_flags = bits.ReadBit();
349 sps.offset_for_non_ref_pic = SignedGolomb(ReadGolomb(bits));
350 sps.offset_for_top_to_bottom_field = SignedGolomb(ReadGolomb(bits));
351 sps.num_ref_frames_in_pic_order_cnt_cycle = ReadGolomb(bits);
352 if (sps.num_ref_frames_in_pic_order_cnt_cycle > AP4_AVC_SPS_MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE) {
353 return AP4_ERROR_INVALID_FORMAT;
354 }
355 for (unsigned int i=0; i<sps.num_ref_frames_in_pic_order_cnt_cycle; i++) {
356 sps.offset_for_ref_frame[i] = SignedGolomb(ReadGolomb(bits));
357 }
358 }
359 sps.num_ref_frames = ReadGolomb(bits);
360 sps.gaps_in_frame_num_value_allowed_flag = bits.ReadBit();
361 sps.pic_width_in_mbs_minus1 = ReadGolomb(bits);
362 sps.pic_height_in_map_units_minus1 = ReadGolomb(bits);
363 sps.frame_mbs_only_flag = bits.ReadBit();
364 if (!sps.frame_mbs_only_flag) {
365 sps.mb_adaptive_frame_field_flag = bits.ReadBit();
366 }
367 sps.direct_8x8_inference_flag = bits.ReadBit();
368 sps.frame_cropping_flag = bits.ReadBit();
369 if (sps.frame_cropping_flag) {
370 sps.frame_crop_left_offset = ReadGolomb(bits);
371 sps.frame_crop_right_offset = ReadGolomb(bits);
372 sps.frame_crop_top_offset = ReadGolomb(bits);
373 sps.frame_crop_bottom_offset = ReadGolomb(bits);
374 }
375
376 return AP4_SUCCESS;
377 }
378
379 /*----------------------------------------------------------------------
380 | AP4_AvcPictureParameterSet::AP4_PictureParameterSet
381 +---------------------------------------------------------------------*/
AP4_AvcPictureParameterSet()382 AP4_AvcPictureParameterSet::AP4_AvcPictureParameterSet() :
383 pic_parameter_set_id(0),
384 seq_parameter_set_id(0),
385 entropy_coding_mode_flag(0),
386 pic_order_present_flag(0),
387 num_slice_groups_minus1(0),
388 slice_group_map_type(0),
389 slice_group_change_direction_flag(0),
390 slice_group_change_rate_minus1(0),
391 pic_size_in_map_units_minus1(0),
392 num_ref_idx_10_active_minus1(0),
393 num_ref_idx_11_active_minus1(0),
394 weighted_pred_flag(0),
395 weighted_bipred_idc(0),
396 pic_init_qp_minus26(0),
397 pic_init_qs_minus26(0),
398 chroma_qp_index_offset(0),
399 deblocking_filter_control_present_flag(0),
400 constrained_intra_pred_flag(0),
401 redundant_pic_cnt_present_flag(0)
402 {
403 AP4_SetMemory(run_length_minus1, 0, sizeof(run_length_minus1));
404 AP4_SetMemory(top_left, 0, sizeof(top_left));
405 AP4_SetMemory(bottom_right, 0, sizeof(bottom_right));
406 }
407
408 /*----------------------------------------------------------------------
409 | AP4_AvcFrameParser::ParsePPS
410 +---------------------------------------------------------------------*/
411 AP4_Result
ParsePPS(const unsigned char * data,unsigned int data_size,AP4_AvcPictureParameterSet & pps)412 AP4_AvcFrameParser::ParsePPS(const unsigned char* data,
413 unsigned int data_size,
414 AP4_AvcPictureParameterSet& pps)
415 {
416 pps.raw_bytes.SetData(data, data_size);
417 AP4_DataBuffer unescaped(data, data_size);
418 AP4_NalParser::Unescape(unescaped);
419 AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
420
421 bits.SkipBits(8); // NAL Unit Type
422
423 pps.pic_parameter_set_id = ReadGolomb(bits);
424 if (pps.pic_parameter_set_id > AP4_AVC_PPS_MAX_ID) {
425 return AP4_ERROR_INVALID_FORMAT;
426 }
427 pps.seq_parameter_set_id = ReadGolomb(bits);
428 if (pps.seq_parameter_set_id > AP4_AVC_SPS_MAX_ID) {
429 return AP4_ERROR_INVALID_FORMAT;
430 }
431 pps.entropy_coding_mode_flag = bits.ReadBit();
432 pps.pic_order_present_flag = bits.ReadBit();
433 pps.num_slice_groups_minus1 = ReadGolomb(bits);
434 if (pps.num_slice_groups_minus1 >= AP4_AVC_PPS_MAX_SLICE_GROUPS) {
435 return AP4_ERROR_INVALID_FORMAT;
436 }
437 if (pps.num_slice_groups_minus1 > 0) {
438 pps.slice_group_map_type = ReadGolomb(bits);
439 if (pps.slice_group_map_type == 0) {
440 for (unsigned int i=0; i<=pps.num_slice_groups_minus1; i++) {
441 pps.run_length_minus1[i] = ReadGolomb(bits);
442 }
443 } else if (pps.slice_group_map_type == 2) {
444 for (unsigned int i=0; i<pps.num_slice_groups_minus1; i++) {
445 pps.top_left[i] = ReadGolomb(bits);
446 pps.bottom_right[i] = ReadGolomb(bits);
447 }
448 } else if (pps.slice_group_map_type == 3 ||
449 pps.slice_group_map_type == 4 ||
450 pps.slice_group_map_type == 5) {
451 pps.slice_group_change_direction_flag = bits.ReadBit();
452 pps.slice_group_change_rate_minus1 = ReadGolomb(bits);
453 } else if (pps.slice_group_map_type == 6) {
454 pps.pic_size_in_map_units_minus1 = ReadGolomb(bits);
455 if (pps.pic_size_in_map_units_minus1 >= AP4_AVC_PPS_MAX_PIC_SIZE_IN_MAP_UNITS) {
456 return AP4_ERROR_INVALID_FORMAT;
457 }
458 unsigned int num_bits_per_slice_group_id;
459 if (pps.num_slice_groups_minus1 + 1 > 4) {
460 num_bits_per_slice_group_id = 3;
461 } else if (pps.num_slice_groups_minus1 + 1 > 2) {
462 num_bits_per_slice_group_id = 2;
463 } else {
464 num_bits_per_slice_group_id = 1;
465 }
466 for (unsigned int i=0; i<=pps.pic_size_in_map_units_minus1; i++) {
467 /*pps.slice_group_id[i] =*/ bits.ReadBits(num_bits_per_slice_group_id);
468 }
469 }
470 }
471 pps.num_ref_idx_10_active_minus1 = ReadGolomb(bits);
472 pps.num_ref_idx_11_active_minus1 = ReadGolomb(bits);
473 pps.weighted_pred_flag = bits.ReadBit();
474 pps.weighted_bipred_idc = bits.ReadBits(2);
475 pps.pic_init_qp_minus26 = SignedGolomb(ReadGolomb(bits));
476 pps.pic_init_qs_minus26 = SignedGolomb(ReadGolomb(bits));
477 pps.chroma_qp_index_offset = SignedGolomb(ReadGolomb(bits));
478 pps.deblocking_filter_control_present_flag = bits.ReadBit();
479 pps.constrained_intra_pred_flag = bits.ReadBit();
480 pps.redundant_pic_cnt_present_flag = bits.ReadBit();
481
482 return AP4_SUCCESS;
483 }
484
485 /*----------------------------------------------------------------------
486 | AP4_AvcSliceHeader::AP4_AvcSliceHeader
487 +---------------------------------------------------------------------*/
AP4_AvcSliceHeader()488 AP4_AvcSliceHeader::AP4_AvcSliceHeader() :
489 size(0),
490 first_mb_in_slice(0),
491 slice_type(0),
492 pic_parameter_set_id(0),
493 colour_plane_id(0),
494 frame_num(0),
495 field_pic_flag(0),
496 bottom_field_flag(0),
497 idr_pic_id(0),
498 pic_order_cnt_lsb(0),
499 redundant_pic_cnt(0),
500 direct_spatial_mv_pred_flag(0),
501 num_ref_idx_active_override_flag(0),
502 num_ref_idx_l0_active_minus1(0),
503 num_ref_idx_l1_active_minus1(0),
504 ref_pic_list_reordering_flag_l0(0),
505 reordering_of_pic_nums_idc(0),
506 abs_diff_pic_num_minus1(0),
507 long_term_pic_num(0),
508 ref_pic_list_reordering_flag_l1(0),
509 luma_log2_weight_denom(0),
510 chroma_log2_weight_denom(0),
511 cabac_init_idc(0),
512 slice_qp_delta(0),
513 sp_for_switch_flag(0),
514 slice_qs_delta(0),
515 disable_deblocking_filter_idc(0),
516 slice_alpha_c0_offset_div2(0),
517 slice_beta_offset_div2(0),
518 slice_group_change_cycle(0),
519 no_output_of_prior_pics_flag(0),
520 long_term_reference_flag(0),
521 difference_of_pic_nums_minus1(0),
522 long_term_frame_idx(0),
523 max_long_term_frame_idx_plus1(0)
524 {
525 delta_pic_order_cnt[0] = delta_pic_order_cnt[1] = 0;
526 }
527
528 /*----------------------------------------------------------------------
529 | AP4_AvcFrameParser::ParseSliceHeader
530 +---------------------------------------------------------------------*/
531 AP4_Result
ParseSliceHeader(const AP4_UI08 * data,unsigned int data_size,unsigned int nal_unit_type,unsigned int nal_ref_idc,AP4_AvcSliceHeader & slice_header)532 AP4_AvcFrameParser::ParseSliceHeader(const AP4_UI08* data,
533 unsigned int data_size,
534 unsigned int nal_unit_type,
535 unsigned int nal_ref_idc,
536 AP4_AvcSliceHeader& slice_header)
537 {
538 AP4_DataBuffer unescaped(data, data_size);
539 AP4_NalParser::Unescape(unescaped);
540 AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());
541
542 // init the computer fields
543 slice_header.size = 0;
544
545 slice_header.first_mb_in_slice = ReadGolomb(bits);
546 slice_header.slice_type = ReadGolomb(bits);
547 slice_header.pic_parameter_set_id = ReadGolomb(bits);
548 if (slice_header.pic_parameter_set_id > AP4_AVC_PPS_MAX_ID) {
549 return AP4_ERROR_INVALID_FORMAT;
550 }
551 const AP4_AvcPictureParameterSet* pps = m_PPS[slice_header.pic_parameter_set_id];
552 if (pps == NULL) {
553 return AP4_ERROR_INVALID_FORMAT;
554 }
555 const AP4_AvcSequenceParameterSet* sps = m_SPS[pps->seq_parameter_set_id];
556 if (sps == NULL) {
557 return AP4_ERROR_INVALID_FORMAT;
558 }
559 if (sps->separate_colour_plane_flag) {
560 slice_header.colour_plane_id = bits.ReadBits(2);
561 }
562 slice_header.frame_num = bits.ReadBits(sps->log2_max_frame_num_minus4 + 4);
563 if (!sps->frame_mbs_only_flag) {
564 slice_header.field_pic_flag = bits.ReadBit();
565 if (slice_header.field_pic_flag) {
566 slice_header.bottom_field_flag = bits.ReadBit();
567 }
568 }
569 if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) {
570 slice_header.idr_pic_id = ReadGolomb(bits);
571 }
572 if (sps->pic_order_cnt_type == 0) {
573 slice_header.pic_order_cnt_lsb = bits.ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
574 if (pps->pic_order_present_flag && !slice_header.field_pic_flag) {
575 slice_header.delta_pic_order_cnt[0] = SignedGolomb(ReadGolomb(bits));
576 }
577 }
578 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flags) {
579 slice_header.delta_pic_order_cnt[0] = SignedGolomb(ReadGolomb(bits));
580 if (pps->pic_order_present_flag && !slice_header.field_pic_flag) {
581 slice_header.delta_pic_order_cnt[1] = SignedGolomb(ReadGolomb(bits));
582 }
583 }
584 if (pps->redundant_pic_cnt_present_flag) {
585 slice_header.redundant_pic_cnt = ReadGolomb(bits);
586 }
587
588 unsigned int slice_type = slice_header.slice_type % 5; // this seems to be implicit in the spec
589
590 if (slice_type == AP4_AVC_SLICE_TYPE_B) {
591 slice_header.direct_spatial_mv_pred_flag = bits.ReadBit();
592 }
593
594 if (slice_type == AP4_AVC_SLICE_TYPE_P ||
595 slice_type == AP4_AVC_SLICE_TYPE_SP ||
596 slice_type == AP4_AVC_SLICE_TYPE_B) {
597 slice_header.num_ref_idx_active_override_flag = bits.ReadBit();
598
599 if (slice_header.num_ref_idx_active_override_flag) {
600 slice_header.num_ref_idx_l0_active_minus1 = ReadGolomb(bits);
601 if ((slice_header.slice_type % 5) == AP4_AVC_SLICE_TYPE_B) {
602 slice_header.num_ref_idx_l1_active_minus1 = ReadGolomb(bits);
603 }
604 } else {
605 slice_header.num_ref_idx_l0_active_minus1 = pps->num_ref_idx_10_active_minus1;
606 slice_header.num_ref_idx_l1_active_minus1 = pps->num_ref_idx_11_active_minus1;
607 }
608 }
609
610 // ref_pic_list_reordering
611 if ((slice_header.slice_type % 5) != 2 && (slice_header.slice_type % 5) != 4) {
612 slice_header.ref_pic_list_reordering_flag_l0 = bits.ReadBit();
613 if (slice_header.ref_pic_list_reordering_flag_l0) {
614 do {
615 slice_header.reordering_of_pic_nums_idc = ReadGolomb(bits);
616 if (slice_header.reordering_of_pic_nums_idc == 0 ||
617 slice_header.reordering_of_pic_nums_idc == 1) {
618 slice_header.abs_diff_pic_num_minus1 = ReadGolomb(bits);
619 } else if (slice_header.reordering_of_pic_nums_idc == 2) {
620 slice_header.long_term_pic_num = ReadGolomb(bits);
621 }
622 } while (slice_header.reordering_of_pic_nums_idc != 3);
623 }
624 }
625 if ((slice_header.slice_type % 5) == 1) {
626 slice_header.ref_pic_list_reordering_flag_l1 = bits.ReadBit();
627 if (slice_header.ref_pic_list_reordering_flag_l1) {
628 do {
629 slice_header.reordering_of_pic_nums_idc = ReadGolomb(bits);
630 if (slice_header.reordering_of_pic_nums_idc == 0 ||
631 slice_header.reordering_of_pic_nums_idc == 1) {
632 slice_header.abs_diff_pic_num_minus1 = ReadGolomb(bits);
633 } else if (slice_header.reordering_of_pic_nums_idc == 2) {
634 slice_header.long_term_pic_num = ReadGolomb(bits);
635 }
636 } while (slice_header.reordering_of_pic_nums_idc != 3);
637 }
638 }
639
640 if ((pps->weighted_pred_flag &&
641 (slice_type == AP4_AVC_SLICE_TYPE_P || slice_type == AP4_AVC_SLICE_TYPE_SP)) ||
642 (pps->weighted_bipred_idc == 1 && slice_type == AP4_AVC_SLICE_TYPE_B)) {
643 // pred_weight_table
644 slice_header.luma_log2_weight_denom = ReadGolomb(bits);
645
646 if (sps->chroma_format_idc != 0) {
647 slice_header.chroma_log2_weight_denom = ReadGolomb(bits);
648 }
649
650 for (unsigned int i=0; i<=slice_header.num_ref_idx_l0_active_minus1; i++) {
651 unsigned int luma_weight_l0_flag = bits.ReadBit();
652 if (luma_weight_l0_flag) {
653 /* slice_header.luma_weight_l0[i] = SignedGolomb( */ ReadGolomb(bits);
654 /* slice_header.luma_offset_l0[i] = SignedGolomb( */ ReadGolomb(bits);
655 }
656 if (sps->chroma_format_idc != 0) {
657 unsigned int chroma_weight_l0_flag = bits.ReadBit();
658 if (chroma_weight_l0_flag) {
659 for (unsigned int j=0; j<2; j++) {
660 /* slice_header.chroma_weight_l0[i][j] = SignedGolomb( */ ReadGolomb(bits);
661 /* slice_header.chroma_offset_l0[i][j] = SignedGolomb( */ ReadGolomb(bits);
662 }
663 }
664 }
665 }
666 if ((slice_header.slice_type % 5) == 1) {
667 for (unsigned int i=0; i<=slice_header.num_ref_idx_l1_active_minus1; i++) {
668 unsigned int luma_weight_l1_flag = bits.ReadBit();
669 if (luma_weight_l1_flag) {
670 /* slice_header.luma_weight_l1[i] = SignedGolomb( */ ReadGolomb(bits);
671 /* slice_header.luma_offset_l1[i] = SignedGolomb( */ ReadGolomb(bits);
672 }
673 if (sps->chroma_format_idc != 0) {
674 unsigned int chroma_weight_l1_flag = bits.ReadBit();
675 if (chroma_weight_l1_flag) {
676 for (unsigned int j=0; j<2; j++) {
677 /* slice_header.chroma_weight_l1[i][j] = SignedGolomb( */ ReadGolomb(bits);
678 /* slice_header.chroma_offset_l1[i][j] = SignedGolomb( */ ReadGolomb(bits);
679 }
680 }
681 }
682 }
683 }
684 }
685
686 if (nal_ref_idc != 0) {
687 // dec_ref_pic_marking
688 if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) {
689 slice_header.no_output_of_prior_pics_flag = bits.ReadBit();
690 slice_header.long_term_reference_flag = bits.ReadBit();
691 } else {
692 unsigned int adaptive_ref_pic_marking_mode_flag = bits.ReadBit();
693 if (adaptive_ref_pic_marking_mode_flag) {
694 unsigned int memory_management_control_operation = 0;
695 do {
696 memory_management_control_operation = ReadGolomb(bits);
697 if (memory_management_control_operation == 1 || memory_management_control_operation == 3) {
698 slice_header.difference_of_pic_nums_minus1 = ReadGolomb(bits);
699 }
700 if (memory_management_control_operation == 2) {
701 slice_header.long_term_pic_num = ReadGolomb(bits);
702 }
703 if (memory_management_control_operation == 3 || memory_management_control_operation == 6) {
704 slice_header.long_term_frame_idx = ReadGolomb(bits);
705 }
706 if (memory_management_control_operation == 4) {
707 slice_header.max_long_term_frame_idx_plus1 = ReadGolomb(bits);
708 }
709 } while (memory_management_control_operation != 0);
710 }
711 }
712 }
713 if (pps->entropy_coding_mode_flag && slice_type != AP4_AVC_SLICE_TYPE_I && slice_type != AP4_AVC_SLICE_TYPE_SI) {
714 slice_header.cabac_init_idc = ReadGolomb(bits);
715 }
716 slice_header.slice_qp_delta = ReadGolomb(bits);
717 if (slice_type == AP4_AVC_SLICE_TYPE_SP || slice_type == AP4_AVC_SLICE_TYPE_SI) {
718 if (slice_type == AP4_AVC_SLICE_TYPE_SP) {
719 slice_header.sp_for_switch_flag = bits.ReadBit();
720 }
721 slice_header.slice_qs_delta = SignedGolomb(ReadGolomb(bits));
722 }
723 if (pps->deblocking_filter_control_present_flag) {
724 slice_header.disable_deblocking_filter_idc = ReadGolomb(bits);
725 if (slice_header.disable_deblocking_filter_idc != 1) {
726 slice_header.slice_alpha_c0_offset_div2 = SignedGolomb(ReadGolomb(bits));
727 slice_header.slice_beta_offset_div2 = SignedGolomb(ReadGolomb(bits));
728 }
729 }
730 if (pps->num_slice_groups_minus1 > 0 &&
731 pps->slice_group_map_type >= 3 &&
732 pps->slice_group_map_type <= 5) {
733 slice_header.slice_group_change_cycle = ReadGolomb(bits);
734 }
735
736 /* compute the size */
737 slice_header.size = bits.GetBitsRead();
738
739 return AP4_SUCCESS;
740 }
741
742 /*----------------------------------------------------------------------
743 | AP4_AvcFrameParser::GetSliceSPS
744 +---------------------------------------------------------------------*/
745 AP4_AvcSequenceParameterSet*
GetSliceSPS(AP4_AvcSliceHeader & sh)746 AP4_AvcFrameParser::GetSliceSPS(AP4_AvcSliceHeader& sh)
747 {
748 // lookup the SPS
749 AP4_AvcPictureParameterSet* pps = m_PPS[sh.pic_parameter_set_id];
750 if (pps == NULL) return NULL;
751 return m_SPS[pps->seq_parameter_set_id];
752 }
753
754 /*----------------------------------------------------------------------
755 | AP4_AvcFrameParser::SameFrame
756 | 14496-10 7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded picture
757 +---------------------------------------------------------------------*/
758 bool
SameFrame(unsigned int nal_unit_type_1,unsigned int nal_ref_idc_1,AP4_AvcSliceHeader & sh1,unsigned int nal_unit_type_2,unsigned int nal_ref_idc_2,AP4_AvcSliceHeader & sh2)759 AP4_AvcFrameParser::SameFrame(unsigned int nal_unit_type_1, unsigned int nal_ref_idc_1, AP4_AvcSliceHeader& sh1,
760 unsigned int nal_unit_type_2, unsigned int nal_ref_idc_2, AP4_AvcSliceHeader& sh2)
761 {
762 if (sh1.frame_num != sh2.frame_num) {
763 return false;
764 }
765
766 if (sh1.pic_parameter_set_id != sh2.pic_parameter_set_id) {
767 return false;
768 }
769
770 if (sh1.field_pic_flag != sh2.field_pic_flag) {
771 return false;
772 }
773
774 if (sh1.field_pic_flag) {
775 if (sh1.bottom_field_flag != sh2.bottom_field_flag) {
776 return false;
777 }
778 }
779
780 if ((nal_ref_idc_1 == 0 || nal_ref_idc_2 == 0) && (nal_ref_idc_1 != nal_ref_idc_2)) {
781 return false;
782 }
783
784 // lookup the SPS
785 AP4_AvcSequenceParameterSet* sps = GetSliceSPS(sh1);
786 if (sps == NULL) return false;
787
788 if (sps->pic_order_cnt_type == 0) {
789 if (sh1.pic_order_cnt_lsb != sh2.pic_order_cnt_lsb ||
790 sh1.delta_pic_order_cnt[0] != sh2.delta_pic_order_cnt[0]) {
791 return false;
792 }
793 }
794
795 if (sps->pic_order_cnt_type == 1) {
796 if (sh1.delta_pic_order_cnt[0] != sh2.delta_pic_order_cnt[0] ||
797 sh1.delta_pic_order_cnt[1] != sh2.delta_pic_order_cnt[1]) {
798 return false;
799 }
800 }
801
802 if (nal_unit_type_1 == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE ||
803 nal_unit_type_2 == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) {
804 if (nal_unit_type_1 != nal_unit_type_2) {
805 return false;
806 }
807 }
808
809 if (nal_unit_type_1 == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE &&
810 nal_unit_type_2 == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) {
811 if (sh1.idr_pic_id != sh2.idr_pic_id) {
812 return false;
813 }
814 }
815
816 return true;
817 }
818
819 /*----------------------------------------------------------------------
820 | AP4_AvcFrameParser::CheckIfAccessUnitIsCompleted
821 +---------------------------------------------------------------------*/
822 void
CheckIfAccessUnitIsCompleted(AccessUnitInfo & access_unit_info)823 AP4_AvcFrameParser::CheckIfAccessUnitIsCompleted(AccessUnitInfo& access_unit_info)
824 {
825 if (m_SliceHeader == NULL) {
826 return;
827 }
828 if (!m_AccessUnitVclNalUnitCount) {
829 return;
830 }
831 DBG_PRINTF_0(">>>>>>> New Access Unit\n");
832 m_AccessUnitVclNalUnitCount = 0;
833
834 AP4_AvcSequenceParameterSet* sps = GetSliceSPS(*m_SliceHeader);
835 if (sps == NULL) return;
836 int max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
837
838 // compute the picture type
839 enum {
840 AP4_AVC_PIC_TYPE_FRAME,
841 AP4_AVC_PIC_TYPE_TOP_FIELD,
842 AP4_AVC_PIC_TYPE_BOTTOM_FIELD
843 } pic_type;
844 if (sps->frame_mbs_only_flag || !m_SliceHeader->field_pic_flag) {
845 pic_type = AP4_AVC_PIC_TYPE_FRAME;
846 } else if (m_SliceHeader->bottom_field_flag) {
847 pic_type = AP4_AVC_PIC_TYPE_BOTTOM_FIELD;
848 } else {
849 pic_type = AP4_AVC_PIC_TYPE_TOP_FIELD;
850 }
851
852 // prepare to compute the picture order count
853 int top_field_pic_order_cnt = 0;
854 int bottom_field_pic_order_cnt = 0;
855 unsigned int frame_num_offset = 0;
856 unsigned int frame_num = m_SliceHeader->frame_num;
857 if (m_NalUnitType == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) {
858 m_PrevPicOrderCntMsb = 0;
859 m_PrevPicOrderCntLsb = 0;
860 } else {
861 if (frame_num < m_PrevFrameNum) {
862 // wrap around
863 frame_num_offset = m_PrevFrameNumOffset + max_frame_num;
864 } else {
865 frame_num_offset = m_PrevFrameNumOffset;
866 }
867 }
868
869 // compute the picture order count
870 int pic_order_cnt_msb = 0;
871 if (sps->pic_order_cnt_type == 0) {
872 unsigned int max_pic_order_cnt_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
873 if (m_SliceHeader->pic_order_cnt_lsb < m_PrevPicOrderCntLsb &&
874 m_PrevPicOrderCntLsb - m_SliceHeader->pic_order_cnt_lsb >= max_pic_order_cnt_lsb/2) {
875 pic_order_cnt_msb = m_PrevPicOrderCntMsb + max_pic_order_cnt_lsb;
876 } else if (m_SliceHeader->pic_order_cnt_lsb > m_PrevPicOrderCntLsb &&
877 m_SliceHeader->pic_order_cnt_lsb - m_PrevPicOrderCntLsb > max_pic_order_cnt_lsb/2) {
878 pic_order_cnt_msb = m_PrevPicOrderCntMsb - max_pic_order_cnt_lsb;
879 } else {
880 pic_order_cnt_msb = m_PrevPicOrderCntMsb;
881 }
882
883 if (pic_type != AP4_AVC_PIC_TYPE_BOTTOM_FIELD) {
884 top_field_pic_order_cnt = pic_order_cnt_msb+m_SliceHeader->pic_order_cnt_lsb;
885 }
886 if (pic_type != AP4_AVC_PIC_TYPE_TOP_FIELD) {
887 if (!m_SliceHeader->field_pic_flag) {
888 bottom_field_pic_order_cnt = top_field_pic_order_cnt + m_SliceHeader->delta_pic_order_cnt[0];
889 } else {
890 bottom_field_pic_order_cnt = pic_order_cnt_msb + m_SliceHeader->pic_order_cnt_lsb;
891 }
892 }
893 } else if (sps->pic_order_cnt_type == 1) {
894 unsigned int abs_frame_num = 0;
895 if (sps->num_ref_frames_in_pic_order_cnt_cycle) {
896 abs_frame_num = frame_num_offset + frame_num;
897 }
898 if (m_NalRefIdc == 0 && abs_frame_num > 0) {
899 --abs_frame_num;
900 }
901
902 int expected_pic_order_cnt = 0;
903 if (abs_frame_num >0) {
904 unsigned int pic_order_cnt_cycle_cnt = (abs_frame_num-1)/sps->num_ref_frames_in_pic_order_cnt_cycle;
905 unsigned int frame_num_in_pic_order_cnt_cycle = (abs_frame_num-1)%sps->num_ref_frames_in_pic_order_cnt_cycle;
906
907 int expected_delta_per_pic_order_cnt_cycle = 0;
908 for (unsigned int i=0; i<sps->num_ref_frames_in_pic_order_cnt_cycle; i++) {
909 expected_delta_per_pic_order_cnt_cycle += sps->offset_for_ref_frame[i];
910 }
911 expected_pic_order_cnt = pic_order_cnt_cycle_cnt * expected_delta_per_pic_order_cnt_cycle;
912 for (unsigned int i=0; i<frame_num_in_pic_order_cnt_cycle; i++) {
913 expected_pic_order_cnt += sps->offset_for_ref_frame[i];
914 }
915 }
916 if (m_NalRefIdc == 0) {
917 expected_pic_order_cnt += sps->offset_for_non_ref_pic;
918 }
919
920 if (!m_SliceHeader->field_pic_flag) {
921 top_field_pic_order_cnt = expected_pic_order_cnt + m_SliceHeader->delta_pic_order_cnt[0];
922 bottom_field_pic_order_cnt = top_field_pic_order_cnt + sps->offset_for_top_to_bottom_field + m_SliceHeader->delta_pic_order_cnt[1];
923 } else if (!m_SliceHeader->bottom_field_flag) {
924 top_field_pic_order_cnt = expected_pic_order_cnt + m_SliceHeader->delta_pic_order_cnt[0];
925 } else {
926 bottom_field_pic_order_cnt = expected_pic_order_cnt + sps->offset_for_top_to_bottom_field + m_SliceHeader->delta_pic_order_cnt[0];
927 }
928 } else if (sps->pic_order_cnt_type == 2) {
929 int pic_order_cnt;
930 if (m_NalUnitType == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE) {
931 pic_order_cnt = 0;
932 } else if (m_NalRefIdc == 0) {
933 pic_order_cnt = 2*(frame_num_offset+frame_num)-1;
934 } else {
935 pic_order_cnt = 2*(frame_num_offset+frame_num);
936 }
937
938 if (!m_SliceHeader->field_pic_flag) {
939 top_field_pic_order_cnt = pic_order_cnt;
940 bottom_field_pic_order_cnt = pic_order_cnt;
941 } else if (m_SliceHeader->bottom_field_flag) {
942 bottom_field_pic_order_cnt = pic_order_cnt;
943 } else {
944 top_field_pic_order_cnt = pic_order_cnt;
945 }
946 }
947
948 unsigned int pic_order_cnt;
949 if (pic_type == AP4_AVC_PIC_TYPE_FRAME) {
950 pic_order_cnt = top_field_pic_order_cnt < bottom_field_pic_order_cnt ? top_field_pic_order_cnt : bottom_field_pic_order_cnt;
951 } else if (pic_type == AP4_AVC_PIC_TYPE_TOP_FIELD) {
952 pic_order_cnt = top_field_pic_order_cnt;
953 } else {
954 pic_order_cnt = bottom_field_pic_order_cnt;
955 }
956
957 // emit the access unit (transfer ownership)
958 access_unit_info.nal_units = m_AccessUnitData;
959 access_unit_info.is_idr = (m_NalUnitType == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE);
960 access_unit_info.decode_order = m_TotalAccessUnitCount;
961 access_unit_info.display_order = pic_order_cnt;
962 m_AccessUnitData.Clear();
963 ++m_TotalAccessUnitCount;
964
965 // update state
966 m_PrevFrameNum = frame_num;
967 m_PrevFrameNumOffset = frame_num_offset;
968 if (m_NalRefIdc) {
969 m_PrevPicOrderCntMsb = pic_order_cnt_msb;
970 m_PrevPicOrderCntLsb = m_SliceHeader->pic_order_cnt_lsb;
971 }
972 }
973
974 /*----------------------------------------------------------------------
975 | AP4_AvcFrameParser::AppendNalUnitData
976 +---------------------------------------------------------------------*/
977 void
AppendNalUnitData(const unsigned char * data,unsigned int data_size)978 AP4_AvcFrameParser::AppendNalUnitData(const unsigned char* data, unsigned int data_size)
979 {
980 m_AccessUnitData.Append(new AP4_DataBuffer(data, data_size));
981 }
982
983 /*----------------------------------------------------------------------
984 | AP4_AvcFrameParser::Feed
985 +---------------------------------------------------------------------*/
986 AP4_Result
Feed(const void * data,AP4_Size data_size,AP4_Size & bytes_consumed,AccessUnitInfo & access_unit_info,bool eos)987 AP4_AvcFrameParser::Feed(const void* data,
988 AP4_Size data_size,
989 AP4_Size& bytes_consumed,
990 AccessUnitInfo& access_unit_info,
991 bool eos)
992 {
993 const AP4_DataBuffer* nal_unit = NULL;
994
995 // feed the NAL unit parser
996 AP4_Result result = m_NalParser.Feed(data, data_size, bytes_consumed, nal_unit, eos);
997 if (AP4_FAILED(result)) {
998 return result;
999 }
1000
1001 if (bytes_consumed < data_size) {
1002 // there will be more to parse
1003 eos = false;
1004 }
1005
1006 return Feed(nal_unit ? nal_unit->GetData() : NULL,
1007 nal_unit ? nal_unit->GetDataSize() : 0,
1008 access_unit_info,
1009 eos);
1010 }
1011
1012 /*----------------------------------------------------------------------
1013 | AP4_AvcFrameParser::Feed
1014 +---------------------------------------------------------------------*/
1015 AP4_Result
Feed(const AP4_UI08 * nal_unit,AP4_Size nal_unit_size,AccessUnitInfo & access_unit_info,bool last_unit)1016 AP4_AvcFrameParser::Feed(const AP4_UI08* nal_unit,
1017 AP4_Size nal_unit_size,
1018 AccessUnitInfo& access_unit_info,
1019 bool last_unit)
1020 {
1021 AP4_Result result;
1022
1023 // default return values
1024 access_unit_info.Reset();
1025
1026 if (nal_unit && nal_unit_size) {
1027 unsigned int nal_unit_type = nal_unit[0]&0x1F;
1028 const char* nal_unit_type_name = AP4_AvcNalParser::NaluTypeName(nal_unit_type);
1029 unsigned int nal_ref_idc = (nal_unit[0]>>5)&3;
1030 if (nal_unit_type_name == NULL) nal_unit_type_name = "UNKNOWN";
1031 DBG_PRINTF_5("NALU %5d: ref=%d, size=%5d, type=%02d (%s) ",
1032 m_TotalNalUnitCount,
1033 nal_ref_idc,
1034 nal_unit_size,
1035 nal_unit_type,
1036 nal_unit_type_name);
1037 if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_ACCESS_UNIT_DELIMITER) {
1038 unsigned int primary_pic_type = (nal_unit[1]>>5);
1039 const char* primary_pic_type_name = AP4_AvcNalParser::PrimaryPicTypeName(primary_pic_type);
1040 if (primary_pic_type_name == NULL) primary_pic_type_name = "UNKNOWN";
1041 DBG_PRINTF_2("[%d:%s]\n", primary_pic_type, primary_pic_type_name);
1042
1043 CheckIfAccessUnitIsCompleted(access_unit_info);
1044 } else if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_NON_IDR_PICTURE ||
1045 nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE ||
1046 nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A) {
1047 AP4_AvcSliceHeader* slice_header = new AP4_AvcSliceHeader;
1048 result = ParseSliceHeader(nal_unit+1,
1049 nal_unit_size-1,
1050 nal_unit_type,
1051 nal_ref_idc,
1052 *slice_header);
1053 if (AP4_FAILED(result)) {
1054 return AP4_ERROR_INVALID_FORMAT;
1055 }
1056
1057 const char* slice_type_name = AP4_AvcNalParser::SliceTypeName(slice_header->slice_type);
1058 if (slice_type_name == NULL) slice_type_name = "?";
1059 DBG_PRINTF_5(" pps_id=%d, header_size=%d, frame_num=%d, slice_type=%d (%s), ",
1060 slice_header->pic_parameter_set_id,
1061 slice_header->size,
1062 slice_header->frame_num,
1063 slice_header->slice_type,
1064 slice_type_name);
1065 DBG_PRINTF_0("\n");
1066 if (slice_header) {
1067 if (m_SliceHeader &&
1068 !SameFrame(m_NalUnitType, m_NalRefIdc, *m_SliceHeader,
1069 nal_unit_type, nal_ref_idc, *slice_header)) {
1070 CheckIfAccessUnitIsCompleted(access_unit_info);
1071 m_AccessUnitVclNalUnitCount = 1;
1072 } else {
1073 // continuation of an access unit
1074 ++m_AccessUnitVclNalUnitCount;
1075 }
1076 }
1077
1078 // buffer this NAL unit
1079 AppendNalUnitData(nal_unit, nal_unit_size);
1080 delete m_SliceHeader;
1081 m_SliceHeader = slice_header;
1082 m_NalUnitType = nal_unit_type;
1083 m_NalRefIdc = nal_ref_idc;
1084 } else if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_PPS) {
1085 AP4_AvcPictureParameterSet* pps = new AP4_AvcPictureParameterSet;
1086 result = ParsePPS(nal_unit, nal_unit_size, *pps);
1087 if (AP4_FAILED(result)) {
1088 DBG_PRINTF_0("PPS ERROR!!!\n");
1089 delete pps;
1090 } else {
1091 delete m_PPS[pps->pic_parameter_set_id];
1092 m_PPS[pps->pic_parameter_set_id] = pps;
1093 DBG_PRINTF_2("PPS sps_id=%d, pps_id=%d\n", pps->seq_parameter_set_id, pps->pic_parameter_set_id);
1094
1095 // keep the PPS with the NAL unit (this is optional)
1096 AppendNalUnitData(nal_unit, nal_unit_size);
1097 CheckIfAccessUnitIsCompleted(access_unit_info);
1098 }
1099 } else if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_SPS) {
1100 AP4_AvcSequenceParameterSet* sps = new AP4_AvcSequenceParameterSet;
1101 result = ParseSPS(nal_unit, nal_unit_size, *sps);
1102 if (AP4_FAILED(result)) {
1103 DBG_PRINTF_0("SPS ERROR!!!\n");
1104 delete sps;
1105 } else {
1106 delete m_SPS[sps->seq_parameter_set_id];
1107 m_SPS[sps->seq_parameter_set_id] = sps;
1108 DBG_PRINTF_1("SPS sps_id=%d\n", sps->seq_parameter_set_id);
1109 CheckIfAccessUnitIsCompleted(access_unit_info);
1110 }
1111 } else if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_SEI) {
1112 AppendNalUnitData(nal_unit, nal_unit_size);
1113 CheckIfAccessUnitIsCompleted(access_unit_info);
1114 DBG_PRINTF_0("\n");
1115 } else if (nal_unit_type >= 14 && nal_unit_type <= 18) {
1116 CheckIfAccessUnitIsCompleted(access_unit_info);
1117 DBG_PRINTF_0("\n");
1118 } else if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_UNSPECIFIED28) {
1119 AppendNalUnitData(nal_unit, nal_unit_size);
1120 CheckIfAccessUnitIsCompleted(access_unit_info);
1121 DBG_PRINTF_0("\n");
1122 } else if (nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_UNSPECIFIED30) {
1123 AppendNalUnitData(nal_unit, nal_unit_size);
1124 CheckIfAccessUnitIsCompleted(access_unit_info);
1125 DBG_PRINTF_0("\n");
1126 } else {
1127 DBG_PRINTF_0("\n");
1128 }
1129 m_TotalNalUnitCount++;
1130 }
1131
1132 // flush if needed
1133 if (last_unit && access_unit_info.nal_units.ItemCount() == 0) {
1134 DBG_PRINTF_0("------ last unit\n");
1135 CheckIfAccessUnitIsCompleted(access_unit_info);
1136 }
1137
1138 return AP4_SUCCESS;
1139 }
1140
1141 /*----------------------------------------------------------------------
1142 | AP4_AvcFrameParser::AccessUnitInfo::Reset
1143 +---------------------------------------------------------------------*/
1144 void
Reset()1145 AP4_AvcFrameParser::AccessUnitInfo::Reset()
1146 {
1147 for (unsigned int i=0; i<nal_units.ItemCount(); i++) {
1148 delete nal_units[i];
1149 }
1150 nal_units.Clear();
1151 is_idr = false;
1152 decode_order = 0;
1153 display_order = 0;
1154 }
1155