1 /* ///////////////////////////////////////////////////////////////////////
2 //
3 //               INTEL CORPORATION PROPRIETARY INFORMATION
4 //  This software is supplied under the terms of a license agreement or
5 //  nondisclosure agreement with Intel Corporation and may not be copied
6 //  or disclosed except in accordance with the terms of that agreement.
7 //        Copyright (c) 2001-2006 Intel Corporation. All Rights Reserved.
8 //
9 //  Description:    h264 parser
10 //
11 ///////////////////////////////////////////////////////////////////////*/
12 
13 
14 #include "h264.h"
15 #include "h264parse.h"
16 #include "h264parse_dpb.h"
17 
18 
19 /* ------------------------------------------------------------------------------------------ */
20 /* ------------------------------------------------------------------------------------------ */
21 /* ------------------------------------------------------------------------------------------ */
22 
23 
24 
h264_Scaling_List(void * parent,uint8_t * scalingList,int32_t sizeOfScalingList,uint8_t * UseDefaultScalingMatrix,h264_Info * pInfo)25 h264_Status h264_Scaling_List(void *parent, uint8_t *scalingList, int32_t sizeOfScalingList, uint8_t *UseDefaultScalingMatrix, h264_Info* pInfo)
26 {
27 	int32_t j, scanj;
28 	int32_t delta_scale, lastScale, nextScale;
29 
30 #if 0
31 	const uint8_t ZZ_SCAN[16]  =
32 	{  0,  1,  4,  8,  5,  2,  3,  6,  9, 12, 13, 10,  7, 11, 14, 15
33 	};
34 
35 	const uint8_t ZZ_SCAN8[64] =
36 	{  0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
37 		12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
38 		35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
39 		58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
40 	};
41 #endif
42 
43 	lastScale      = 8;
44 	nextScale      = 8;
45    scanj = 0;
46 
47 	for(j=0; j<sizeOfScalingList; j++)
48 	{
49 		//scanj = (sizeOfScalingList==16)?ZZ_SCAN[j]:ZZ_SCAN8[j];
50 
51 		if(nextScale!=0)
52 		{
53 		  delta_scale = h264_GetVLCElement(parent, pInfo, true);
54 		  nextScale = (lastScale + delta_scale + 256) % 256;
55 		  *UseDefaultScalingMatrix = (uint8_t) (scanj==0 && nextScale==0);
56 		}
57 
58 		scalingList[scanj] = (nextScale==0) ? lastScale:nextScale;
59 		lastScale = scalingList[scanj];
60       scanj ++;
61 	}
62 
63 	return H264_STATUS_OK;
64 }
65 
66 /* ------------------------------------------------------------------------------------------ */
67 /* ------------------------------------------------------------------------------------------ */
68 /* ------------------------------------------------------------------------------------------ */
69 
h264_active_par_set(h264_Info * pInfo,h264_Slice_Header_t * SliceHeader)70 h264_Status h264_active_par_set(h264_Info*pInfo,h264_Slice_Header_t* SliceHeader)
71 {
72 	//h264_Slice_Header_t* SliceHeader = &pInfo->SliceHeader;
73 
74 	///////////////////////////////////////////////////
75 	// Reload SPS/PPS while
76 	// 1) Start of Frame (in case of context switch)
77 	// 2) PPS id changed
78 	///////////////////////////////////////////////////
79 	if((SliceHeader->first_mb_in_slice == 0) || (SliceHeader->pic_parameter_id != pInfo->active_PPS.pic_parameter_set_id))
80 	{
81 #ifndef WIN32
82 		h264_Parse_Copy_Pps_From_DDR(pInfo, &pInfo->active_PPS, SliceHeader->pic_parameter_id);
83 
84 		if(pInfo->active_PPS.seq_parameter_set_id >= MAX_NUM_SPS)
85 		{
86 			return H264_PPS_INVALID_PIC_ID;			/// Invalid PPS detected
87 		}
88 
89       if(pInfo->active_PPS.seq_parameter_set_id != pInfo->active_SPS.seq_parameter_set_id)
90       {
91          pInfo->Is_SPS_updated =1;
92 		 h264_Parse_Copy_Sps_From_DDR(pInfo, &pInfo->active_SPS, pInfo->active_PPS.seq_parameter_set_id);
93 		 h264_Parse_Clear_Sps_Updated_Flag(pInfo, pInfo->active_PPS.seq_parameter_set_id);
94       }
95 	  else
96 	  {
97 	     if(h264_Parse_Check_Sps_Updated_Flag(pInfo, pInfo->active_PPS.seq_parameter_set_id))
98 	     {
99 	        pInfo->Is_SPS_updated =1;
100 		    h264_Parse_Copy_Sps_From_DDR(pInfo, &pInfo->active_SPS, pInfo->active_PPS.seq_parameter_set_id);
101 			h264_Parse_Clear_Sps_Updated_Flag(pInfo, pInfo->active_PPS.seq_parameter_set_id);
102 	     }
103 	  }
104 
105 #else
106 		pInfo->active_PPS = PPS_GL[SliceHeader->pic_parameter_id];
107 		pInfo->active_SPS = SPS_GL[pInfo->active_PPS.seq_parameter_set_id];
108 #endif
109 
110 		if(pInfo->active_SPS.seq_parameter_set_id >= MAX_NUM_SPS)
111 		{
112 			return H264_PPS_INVALID_PIC_ID;			//// Invalid SPS detected
113 		}
114 	}
115 	else {
116 		if((pInfo->active_PPS.seq_parameter_set_id >= MAX_NUM_SPS)  || (pInfo->active_SPS.seq_parameter_set_id >= MAX_NUM_SPS))
117 		{
118 			return H264_PPS_INVALID_PIC_ID;			/// Invalid PPS detected
119 		}
120 	}
121 
122 
123 	pInfo->img.PicWidthInMbs    = (pInfo->active_SPS.sps_disp.pic_width_in_mbs_minus1 + 1);
124 	//pInfo->img.PicHeightInMapUnits = (pInfo->active_SPS.sps_disp.pic_height_in_map_units_minus1 + 1);
125 	pInfo->img.FrameHeightInMbs = pInfo->active_SPS.sps_disp.frame_mbs_only_flag?				\
126 								(pInfo->active_SPS.sps_disp.pic_height_in_map_units_minus1 + 1):	\
127 								((pInfo->active_SPS.sps_disp.pic_height_in_map_units_minus1 + 1)<<1);
128 
129 
130 	return H264_STATUS_OK;
131 };   //// End of h264_active_par_set
132 
133 /* ------------------------------------------------------------------------------------------ */
134 /* ------------------------------------------------------------------------------------------ */
135 /* ------------------------------------------------------------------------------------------ */
136 
137 //////////////////////////////////////////////////
138 // Parse slice header info
139 //////////////////////////////////////////////////
h264_Parse_Slice_Layer_Without_Partitioning_RBSP(void * parent,h264_Info * pInfo,h264_Slice_Header_t * SliceHeader)140 h264_Status h264_Parse_Slice_Layer_Without_Partitioning_RBSP(void *parent, h264_Info* pInfo, h264_Slice_Header_t *SliceHeader)
141 {
142 	h264_Status retStatus = H264_STATUS_ERROR;
143 
144 	////////////////////////////////////////////////////
145 	//// Parse slice header info
146 	//// Part1: not depend on the active PPS/SPS
147 	//// Part2/3: depend on the active parset
148 	//////////////////////////////////////////////////
149 
150 	//retStatus = h264_Parse_Slice_Header_1(pInfo);
151 
152 	SliceHeader->sh_error = 0;
153 
154 	if(h264_Parse_Slice_Header_1(parent, pInfo, SliceHeader) == H264_STATUS_OK)
155 	{
156 		//////////////////////////////////////////
157 		//// Active parameter set for this slice
158 		//////////////////////////////////////////
159 		retStatus = h264_active_par_set(pInfo, SliceHeader);
160 	}
161 
162 	if(retStatus == H264_STATUS_OK) {
163 		switch(pInfo->active_SPS.profile_idc)
164 		{
165 			case h264_ProfileBaseline:
166 			case h264_ProfileMain:
167 			case h264_ProfileExtended:
168 				pInfo->active_PPS.transform_8x8_mode_flag=0;
169 				pInfo->active_PPS.pic_scaling_matrix_present_flag =0;
170 				pInfo->active_PPS.second_chroma_qp_index_offset = pInfo->active_PPS.chroma_qp_index_offset;
171 
172 			default:
173 				break;
174 		}
175 
176 		if( h264_Parse_Slice_Header_2(parent, pInfo, SliceHeader) != H264_STATUS_OK)
177 		{
178 			SliceHeader->sh_error |= 2;
179 		}
180 		else	if( h264_Parse_Slice_Header_3(parent, pInfo, SliceHeader) != H264_STATUS_OK)
181 		{
182 			SliceHeader->sh_error |= 4;
183 		}
184 
185 	} else 	{
186 		SliceHeader->sh_error |= 1;
187 	}
188 
189 
190 	//if(SliceHeader->sh_error) {
191 		//pInfo->wl_err_flag |= VIDDEC_FW_WORKLOAD_ERR_NOTDECODABLE;
192 	//}
193 
194 
195 
196 	//////////////////////////////////
197 	//// Parse slice data (MB loop)
198 	//////////////////////////////////
199 	//retStatus = h264_Parse_Slice_Data(pInfo);
200 	{
201 		//uint32_t data = 0;
202 		//if( viddec_pm_peek_bits(parent, &data, 32) == -1)
203 			//retStatus = H264_STATUS_ERROR;
204 	}
205 	//h264_Parse_rbsp_trailing_bits(pInfo);
206 
207 	return retStatus;
208 }
209 
210 
211 
212 /* ------------------------------------------------------------------------------------------ */
213 /* ------------------------------------------------------------------------------------------ */
214 /* ------------------------------------------------------------------------------------------ */
215 
h264_Parse_NAL_Unit(void * parent,h264_Info * pInfo,uint8_t * nal_ref_idc)216 h264_Status h264_Parse_NAL_Unit(void *parent, h264_Info* pInfo, uint8_t *nal_ref_idc)
217 {
218 	h264_Status ret = H264_STATUS_ERROR;
219 
220 	//h264_NAL_Unit_t* NAL = &pInfo->NAL;
221 	uint32_t code;
222 #if 0
223 	viddec_pm_get_bits(void * parent,uint32_t * data,uint32_t num_bits)(parent, &code, 24);
224 	viddec_pm_get_bits(parent, &code, 1);   //forbidden_zero_bit
225 
226 	viddec_pm_get_bits(parent, &code, 2);
227 	SliceHeader->nal_ref_idc = (uint8_t)code;
228 
229 	viddec_pm_get_bits(parent, &code, 5);
230 	pInfo->nal_unit_type = (uint8_t)code;
231 #else
232 #ifdef VBP
233 	if( viddec_pm_get_bits(parent, &code, 8) != -1)
234 #else
235 	//// 24bit SC, 1 bit: forbidden_zero_bit, 2 bitrs: nal_ref_idc, 5 bits: nal_unit_type
236 	if( viddec_pm_get_bits(parent, &code, 32) != -1)
237 #endif
238 	{
239 		*nal_ref_idc = (uint8_t)((code>>5)&0x3);
240 		pInfo->nal_unit_type = (uint8_t)((code>>0)&0x1f);
241 		ret = H264_STATUS_OK;
242 	}
243 #endif
244 
245 	return ret;
246 }
247 
248 
249 /* ------------------------------------------------------------------------------------------ */
250 /* ------------------------------------------------------------------------------------------ */
251 /* ------------------------------------------------------------------------------------------ */
252 
253 /*!
254  ************************************************************************
255  * \brief
256  *    set defaults for old_slice
257  *    NAL unit of a picture"
258  ************************************************************************
259  */
260 #ifndef INT_MAX
261 #define INT_MAX 0xFFFFFFFF
262 #endif
263 
264 #ifndef UINT_MAX
265 #define UINT_MAX 0x7FFFFFFF
266 #endif
267 
h264_init_old_slice(h264_Info * pInfo)268 void h264_init_old_slice(h264_Info* pInfo)
269 {
270 	pInfo->SliceHeader.field_pic_flag = 0;
271 
272 	pInfo->SliceHeader.pic_parameter_id = 0xFF;
273 
274 	pInfo->SliceHeader.frame_num = INT_MAX;
275 
276 	pInfo->SliceHeader.nal_ref_idc = 0xFF;
277 
278 	pInfo->SliceHeader.idr_flag = 0;
279 
280 	pInfo->SliceHeader.pic_order_cnt_lsb          = UINT_MAX;
281 	pInfo->SliceHeader.delta_pic_order_cnt_bottom = INT_MAX;
282 
283 	pInfo->SliceHeader.delta_pic_order_cnt[0] = INT_MAX;
284 	pInfo->SliceHeader.delta_pic_order_cnt[1] = INT_MAX;
285 
286 	return;
287 }
288 
289 
h264_init_img(h264_Info * pInfo)290 void h264_init_img(h264_Info* pInfo)
291 {
292    h264_memset(&(pInfo->img), 0x0, sizeof(h264_img_par) );
293 
294 
295    return;
296 }
297 
298 
h264_init_sps_pps(struct h264_viddec_parser * parser,uint32_t * persist_mem)299 void h264_init_sps_pps(struct h264_viddec_parser* parser, uint32_t *persist_mem)
300 {
301     int32_t i;
302 
303     h264_Info * pInfo = &(parser->info);
304 
305     parser->sps_pps_ddr_paddr = (uint32_t)persist_mem;
306 
307     pInfo->SPS_PADDR_GL = parser->sps_pps_ddr_paddr;
308     pInfo->PPS_PADDR_GL = pInfo->SPS_PADDR_GL + MAX_NUM_SPS * sizeof(seq_param_set_all);
309     pInfo->OFFSET_REF_FRAME_PADDR_GL = pInfo->PPS_PADDR_GL + MAX_NUM_PPS * sizeof(pic_param_set);
310     pInfo->TMP_OFFSET_REFFRM_PADDR_GL = pInfo->OFFSET_REF_FRAME_PADDR_GL +
311         MAX_NUM_SPS * sizeof(int32_t) * MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE;
312 
313     h264_memset( &(pInfo->active_SPS), 0x0, sizeof(seq_param_set_used) );
314     h264_memset( &(pInfo->active_PPS), 0x0, sizeof(pic_param_set) );
315 
316     /* Global for SPS   & PPS */
317     for(i=0;i<MAX_NUM_SPS;i++)
318     {
319         pInfo->active_SPS.seq_parameter_set_id = 0xff;
320         h264_Parse_Copy_Sps_To_DDR (pInfo, &(pInfo->active_SPS), i);
321     }
322     for(i=0;i<MAX_NUM_PPS;i++)
323     {
324         pInfo->active_PPS.seq_parameter_set_id = 0xff;
325         h264_Parse_Copy_Pps_To_DDR (pInfo, &(pInfo->active_PPS), i);
326     }
327 
328     pInfo->active_SPS.seq_parameter_set_id = 0xff;
329     pInfo->sps_valid = 0;
330     pInfo->got_start = 0;
331 
332     return;
333 }
334 
335 
h264_init_Info_under_sps_pps_level(h264_Info * pInfo)336 void h264_init_Info_under_sps_pps_level(h264_Info* pInfo)
337 {
338     int32_t i=0;
339 
340     h264_memset( &(pInfo->dpb), 0x0, sizeof(h264_DecodedPictureBuffer) );
341     h264_memset( &(pInfo->SliceHeader), 0x0, sizeof(h264_Slice_Header_t) );
342     h264_memset( &(pInfo->old_slice), 0x0, sizeof(OldSliceParams) );
343     h264_memset( &(pInfo->sei_information), 0x0, sizeof(sei_info) );
344     h264_memset( &(pInfo->img), 0x0, sizeof(h264_img_par) );
345 
346     pInfo->h264_list_replacement = 0;
347 
348     pInfo->h264_pwt_start_byte_offset = 0;
349     pInfo->h264_pwt_start_bit_offset = 0;
350     pInfo->h264_pwt_end_byte_offset = 0;
351     pInfo->h264_pwt_end_bit_offset = 0;
352     pInfo->h264_pwt_enabled = 0;
353 
354     for(i=0;i<32;i++)
355     {
356         pInfo->slice_ref_list0[i] = 0;
357         pInfo->slice_ref_list1[i] = 0;
358     }
359 
360     pInfo->qm_present_list = 0;
361 
362     pInfo->nal_unit_type = 0;
363     pInfo->old_nal_unit_type = 0xff;
364 
365     pInfo->push_to_cur = 0;
366     pInfo->Is_first_frame_in_stream = 1;
367     pInfo->Is_SPS_updated = 0;
368     pInfo->number_of_first_au_info_nal_before_first_slice = 0;
369 
370     pInfo->is_frame_boundary_detected_by_non_slice_nal = 0;
371     pInfo->is_frame_boundary_detected_by_slice_nal = 0;
372     pInfo->is_current_workload_done = 0;
373 
374     pInfo->sei_rp_received = 0;
375     pInfo->last_I_frame_idc = 255;
376     pInfo->wl_err_curr = 0;
377     pInfo->wl_err_next = 0;
378 
379     pInfo->primary_pic_type_plus_one = 0;
380 	pInfo->sei_b_state_ready = 0;
381 
382     /* Init old slice structure  */
383     h264_init_old_slice(pInfo);
384 
385     /* init_dpb */
386     h264_init_dpb(&(pInfo->dpb));
387 
388     /* init_sei	*/
389     h264_sei_stream_initialise(pInfo);
390 
391 }
392 
h264_init_Info(h264_Info * pInfo)393 void h264_init_Info(h264_Info* pInfo)
394 {
395     h264_memset(pInfo, 0x0, sizeof(h264_Info));
396 
397     pInfo->old_nal_unit_type = 0xff;
398 
399     pInfo->Is_first_frame_in_stream =1;
400     pInfo->img.frame_count = 0;
401     pInfo->last_I_frame_idc = 255;
402 
403     return;
404 }
405 
406  /* ------------------------------------------------------------------------------------------ */
407 /* ------------------------------------------------------------------------------------------ */
408 /* ------------------------------------------------------------------------------------------ */
409 
410 /////////////////////////////////////////////////////
411 //
412 // Judge whether it is the first VCL of a new picture
413 //
414 /////////////////////////////////////////////////////
h264_is_second_field(h264_Info * pInfo)415  int32_t h264_is_second_field(h264_Info * pInfo)
416  {
417     h264_Slice_Header_t cur_slice = pInfo->SliceHeader;
418     OldSliceParams old_slice = pInfo->old_slice;
419 
420     int result = 0;
421 
422     //pInfo->img.second_field = 0;
423 
424     /// is it second field?
425 
426     //OS_INFO( "xxx is_used = %d\n", pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].is_used);
427 
428     if (cur_slice.structure != FRAME)
429     {
430        if( ( MPD_DPB_FS_NULL_IDC != pInfo->dpb.fs_dec_idc)&&(3 != viddec_h264_get_is_used(&(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc])) )
431          &&(0 != viddec_h264_get_is_used(&(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc])) ))
432        {
433           if ((cur_slice.frame_num == old_slice.frame_num)||(cur_slice.idr_flag))
434           {
435 
436              if(old_slice.structure != cur_slice.structure)
437              {
438 
439                 if (((cur_slice.structure == TOP_FIELD &&old_slice.structure == BOTTOM_FIELD) || // Condition 1:
440                       (old_slice.structure == TOP_FIELD && cur_slice.structure == BOTTOM_FIELD)) &&    \
441                    ((old_slice.nal_ref_idc ==0 && cur_slice.nal_ref_idc == 0)              || // Condition 2:
442                       (old_slice.nal_ref_idc !=0 &&cur_slice.nal_ref_idc != 0)))
443                 {
444                    //pInfo->img.second_field = 1;
445                    result = 1;
446                 }
447              }
448           }
449 
450 
451        }
452 
453 
454     }
455 
456 
457 
458     return result;
459 
460  } //// End of h264_is_second_field
461 
462 
463 
464 /* ------------------------------------------------------------------------------------------ */
465 /* ------------------------------------------------------------------------------------------ */
466 /* ------------------------------------------------------------------------------------------ */
467 
h264_is_new_picture_start(h264_Info * pInfo,h264_Slice_Header_t cur_slice,h264_Slice_Header_t old_slice)468 int32_t h264_is_new_picture_start(h264_Info * pInfo, h264_Slice_Header_t cur_slice, h264_Slice_Header_t old_slice)
469 {
470 	int result = 0;
471 
472     if(pInfo->number_of_first_au_info_nal_before_first_slice)
473     {
474        pInfo->number_of_first_au_info_nal_before_first_slice = 0;
475        return 1;
476     }
477 
478 
479 
480 	result |= (old_slice.pic_parameter_id != cur_slice.pic_parameter_id);
481 	result |= (old_slice.frame_num != cur_slice.frame_num);
482 	result |= (old_slice.field_pic_flag != cur_slice.field_pic_flag);
483 	if(cur_slice.field_pic_flag && old_slice.field_pic_flag)
484 	{
485 		result |= (old_slice.bottom_field_flag != cur_slice.bottom_field_flag);
486 	}
487 
488 	result |= (old_slice.nal_ref_idc != cur_slice.nal_ref_idc) && \
489 			 ((old_slice.nal_ref_idc == 0) || (cur_slice.nal_ref_idc == 0));
490 	result |= ( old_slice.idr_flag != cur_slice.idr_flag);
491 
492 	if (cur_slice.idr_flag && old_slice.idr_flag)
493 	{
494 		result |= (old_slice.idr_pic_id != cur_slice.idr_pic_id);
495 	}
496 
497 	if (pInfo->active_SPS.pic_order_cnt_type == 0)
498 	{
499 		result |=  (old_slice.pic_order_cnt_lsb          != cur_slice.pic_order_cnt_lsb);
500 		result |=  (old_slice.delta_pic_order_cnt_bottom != cur_slice.delta_pic_order_cnt_bottom);
501 	}
502 
503 	if (pInfo->active_SPS.pic_order_cnt_type == 1)
504 	{
505 		result |= (old_slice.delta_pic_order_cnt[0] != cur_slice.delta_pic_order_cnt[0]);
506 		result |= (old_slice.delta_pic_order_cnt[1] != cur_slice.delta_pic_order_cnt[1]);
507 	}
508 
509    return result;
510 }
511 
512 
h264_check_previous_frame_end(h264_Info * pInfo)513 int32_t h264_check_previous_frame_end(h264_Info * pInfo)
514 {
515 	int result = 0;
516 
517 	if( (h264_NAL_UNIT_TYPE_SLICE==pInfo->old_nal_unit_type)||(h264_NAL_UNIT_TYPE_IDR==pInfo->old_nal_unit_type) )
518 	{
519 
520 		switch ( pInfo->nal_unit_type )
521 		{
522 			case h264_NAL_UNIT_TYPE_Acc_unit_delimiter:
523 			case h264_NAL_UNIT_TYPE_SPS:
524 			case h264_NAL_UNIT_TYPE_PPS:
525 			case h264_NAL_UNIT_TYPE_SEI:
526 			case h264_NAL_UNIT_TYPE_EOSeq:
527 			case h264_NAL_UNIT_TYPE_EOstream:
528 			case h264_NAL_UNIT_TYPE_Reserved1:
529 			case h264_NAL_UNIT_TYPE_Reserved2:
530 			case h264_NAL_UNIT_TYPE_Reserved3:
531 			case h264_NAL_UNIT_TYPE_Reserved4:
532 			case h264_NAL_UNIT_TYPE_Reserved5:
533 			{
534 				pInfo->img.current_slice_num = 0;
535 
536 				if((pInfo->img.structure == FRAME) || (pInfo->img.second_field)) {
537 					pInfo->is_frame_boundary_detected_by_non_slice_nal =1;
538 					pInfo->is_current_workload_done=1;
539 					result=1;
540 				}
541 				break;
542 			}
543 			default:
544 				break;
545 		}
546 
547 	}
548 
549 	return result;
550 
551 }
552 
553 
554 
555 
556 /* ------------------------------------------------------------------------------------------ */
557 /* ------------------------------------------------------------------------------------------ */
558 /* ------------------------------------------------------------------------------------------ */
559 
560 //////////////////////////////////////////////////////////////
561 // 1) Update old slice structure for frame boundary detection
562 //////////////////////////////////////////////////////////////
h264_update_old_slice(h264_Info * pInfo,h264_Slice_Header_t next_SliceHeader)563 void h264_update_old_slice(h264_Info * pInfo,h264_Slice_Header_t next_SliceHeader)
564 {
565 	pInfo->old_slice.pic_parameter_id = pInfo->SliceHeader.pic_parameter_id;
566 
567 	pInfo->old_slice.frame_num = pInfo->SliceHeader.frame_num;
568 
569 	pInfo->old_slice.field_pic_flag = pInfo->SliceHeader.field_pic_flag;
570 
571 	if(pInfo->SliceHeader.field_pic_flag)
572 	{
573 		pInfo->old_slice.bottom_field_flag = pInfo->SliceHeader.bottom_field_flag;
574 	}
575 
576 	pInfo->old_slice.nal_ref_idc   = pInfo->SliceHeader.nal_ref_idc;
577 
578 	pInfo->old_slice.structure = pInfo->SliceHeader.structure;
579 
580 	pInfo->old_slice.idr_flag = pInfo->SliceHeader.idr_flag;
581 	if (pInfo->SliceHeader.idr_flag)
582 	{
583 		pInfo->old_slice.idr_pic_id = pInfo->SliceHeader.idr_pic_id;
584 	}
585 
586 	if (pInfo->active_SPS.pic_order_cnt_type == 0)
587 	{
588 		pInfo->old_slice.pic_order_cnt_lsb          = pInfo->SliceHeader.pic_order_cnt_lsb;
589 		pInfo->old_slice.delta_pic_order_cnt_bottom = pInfo->SliceHeader.delta_pic_order_cnt_bottom;
590 	}
591 
592 	if (pInfo->active_SPS.pic_order_cnt_type == 1)
593 	{
594 		pInfo->old_slice.delta_pic_order_cnt[0] = pInfo->SliceHeader.delta_pic_order_cnt[0];
595 		pInfo->old_slice.delta_pic_order_cnt[1] = pInfo->SliceHeader.delta_pic_order_cnt[1];
596 	}
597 
598 	////////////////////////////// Next to current
599 	memcpy(&pInfo->SliceHeader, &next_SliceHeader, sizeof(h264_Slice_Header_t));
600 
601 	return;
602 }
603 
604 /* ------------------------------------------------------------------------------------------ */
605 /* ------------------------------------------------------------------------------------------ */
606 /* ------------------------------------------------------------------------------------------ */
607 
608 //////////////////////////////////////////////////////////////////////////////
609 // Initialization for new picture
610 //////////////////////////////////////////////////////////////////////////////
h264_update_img_info(h264_Info * pInfo)611 void h264_update_img_info(h264_Info * pInfo )
612 {
613 	h264_DecodedPictureBuffer *p_dpb = &pInfo->dpb;
614 
615 	pInfo->img.frame_num = pInfo->SliceHeader.frame_num;
616 	pInfo->img.structure = pInfo->SliceHeader.structure;
617 
618 	pInfo->img.field_pic_flag = pInfo->SliceHeader.field_pic_flag;
619 	pInfo->img.bottom_field_flag = pInfo->SliceHeader.bottom_field_flag;
620 
621 	pInfo->img.MbaffFrameFlag  = pInfo->active_SPS.sps_disp.mb_adaptive_frame_field_flag & (!(pInfo->SliceHeader.field_pic_flag));
622 	pInfo->img.pic_order_cnt_type = pInfo->active_SPS.pic_order_cnt_type;
623 
624 	if(pInfo->img.pic_order_cnt_type == 1) {
625 		pInfo->img.num_ref_frames_in_pic_order_cnt_cycle = pInfo->active_SPS.num_ref_frames_in_pic_order_cnt_cycle;
626 		pInfo->img.delta_pic_order_always_zero_flag = pInfo->active_SPS.delta_pic_order_always_zero_flag;
627 		pInfo->img.offset_for_non_ref_pic = pInfo->active_SPS.offset_for_non_ref_pic;
628 		pInfo->img.offset_for_top_to_bottom_field = pInfo->active_SPS.offset_for_top_to_bottom_field;
629 	}
630 
631 	pInfo->img.pic_order_cnt_lsb = pInfo->SliceHeader.pic_order_cnt_lsb;
632 	//pInfo->img.pic_order_cnt_msb = pInfo->SliceHeader.pic_order_cnt_msb;
633 	pInfo->img.delta_pic_order_cnt_bottom = pInfo->SliceHeader.delta_pic_order_cnt_bottom;
634 	pInfo->img.delta_pic_order_cnt[0] = pInfo->SliceHeader.delta_pic_order_cnt[0];
635 	pInfo->img.delta_pic_order_cnt[1] = pInfo->SliceHeader.delta_pic_order_cnt[1];
636 
637 
638 	pInfo->img.PreviousFrameNum = pInfo->old_slice.frame_num;
639 
640 	pInfo->img.no_output_of_prior_pics_flag = pInfo->SliceHeader.sh_dec_refpic.no_output_of_prior_pics_flag;
641 
642 	////////////////////////////////////////////////// Check SEI recovery point
643 	if (pInfo->sei_information.recovery_point) {
644 		int32_t MaxFrameNum = 1 << (pInfo->active_SPS.log2_max_frame_num_minus4 + 4);
645 		pInfo->sei_information.recovery_frame_num = (pInfo->img.frame_num + pInfo->sei_information.recovery_frame_cnt) % MaxFrameNum;
646 	}
647 
648 	if (pInfo->SliceHeader.idr_flag)
649 		pInfo->sei_information.recovery_frame_num = pInfo->img.frame_num;
650 
651 
652 
653 	/////////////////////////////////////////////////Resolution Change
654 	pInfo->img.curr_has_mmco_5 = 0;
655 
656 	if ( (pInfo->img.PicWidthInMbs != p_dpb->PicWidthInMbs)||
657 	   (pInfo->img.FrameHeightInMbs != p_dpb->FrameHeightInMbs) )
658 	{
659 		int32_t no_output_old_pics = (pInfo->SliceHeader.idr_flag)? pInfo->img.no_output_of_prior_pics_flag : 0;
660 
661 		// If resolution changed, reset the soft DPB here
662 		h264_dpb_reset_dpb(pInfo, pInfo->img.PicWidthInMbs, pInfo->img.FrameHeightInMbs, 1, no_output_old_pics);
663 	}
664 
665 	return;
666 
667 } ///// End of init new frame
668 
669 
h264_update_frame_type(h264_Info * pInfo)670 void h264_update_frame_type(h264_Info * pInfo )
671 {
672 
673 //update frame type
674    if(pInfo->img.structure == FRAME)
675    {
676       if(pInfo->nal_unit_type == h264_NAL_UNIT_TYPE_IDR)
677       {
678          pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (0x1 << FRAME_TYPE_STRUCTRUE_OFFSET)|(FRAME_TYPE_IDR << FRAME_TYPE_FRAME_OFFSET);
679          //pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = 0xff;
680          //pInfo->dpb.fs[0].pic_type = pInfo->dpb.fs_dec_idc;
681 
682       }
683       else
684       {
685       #if 1
686          switch(pInfo->SliceHeader.slice_type)
687          {
688             case h264_PtypeB:
689                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (0x1 << FRAME_TYPE_STRUCTRUE_OFFSET)|(FRAME_TYPE_B << FRAME_TYPE_FRAME_OFFSET);
690             	break;
691             case h264_PtypeSP:
692             case h264_PtypeP:
693 	            if( ((pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type) & (0x7 << FRAME_TYPE_FRAME_OFFSET))>>FRAME_TYPE_FRAME_OFFSET != FRAME_TYPE_B)
694 	               pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (0x1 << FRAME_TYPE_STRUCTRUE_OFFSET)|(FRAME_TYPE_P << FRAME_TYPE_FRAME_OFFSET);
695 	            break;
696             case h264_PtypeI:
697             case h264_PtypeSI:
698 	            if( ((pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type) & (0x7 << FRAME_TYPE_FRAME_OFFSET))>>FRAME_TYPE_FRAME_OFFSET == FRAME_TYPE_INVALID)
699 	            {
700 	               pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (0x1 << FRAME_TYPE_STRUCTRUE_OFFSET)|(FRAME_TYPE_I << FRAME_TYPE_FRAME_OFFSET);
701 	            }
702 				pInfo->last_I_frame_idc = pInfo->dpb.fs_dec_idc;
703 
704 	            break;
705             default:
706             	break;
707 
708          }
709       #endif
710 
711       }
712 
713    }
714    else if(pInfo->img.structure == TOP_FIELD)
715    {
716       if(pInfo->nal_unit_type == h264_NAL_UNIT_TYPE_IDR)
717       {
718          pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_IDR << FRAME_TYPE_TOP_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_BOTTOM_OFFSET));;
719       }
720       else
721       {
722          switch(pInfo->SliceHeader.slice_type)
723          {
724             case h264_PtypeB:
725                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_B << FRAME_TYPE_TOP_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_BOTTOM_OFFSET));
726             break;
727             case h264_PtypeSP:
728             case h264_PtypeP:
729             if( ((pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type) & (0x7 << FRAME_TYPE_TOP_OFFSET))>>FRAME_TYPE_TOP_OFFSET != FRAME_TYPE_B)
730                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_P << FRAME_TYPE_TOP_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_BOTTOM_OFFSET));
731             break;
732             case h264_PtypeI:
733             case h264_PtypeSI:
734             if( ((pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type) & (0x7 << FRAME_TYPE_TOP_OFFSET))>>FRAME_TYPE_TOP_OFFSET == FRAME_TYPE_INVALID)
735             {
736                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_I << FRAME_TYPE_TOP_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_BOTTOM_OFFSET));
737             }
738             break;
739             default:
740             break;
741 
742          }
743 
744       }
745 
746 
747    }else if(pInfo->img.structure == BOTTOM_FIELD)
748    {
749       if(pInfo->nal_unit_type == h264_NAL_UNIT_TYPE_IDR)
750       {
751          pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_IDR << FRAME_TYPE_BOTTOM_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_TOP_OFFSET));;
752       }
753       else
754       {
755          switch(pInfo->SliceHeader.slice_type)
756          {
757             case h264_PtypeB:
758                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_B << FRAME_TYPE_BOTTOM_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_TOP_OFFSET));
759             break;
760             case h264_PtypeSP:
761             case h264_PtypeP:
762             if( ((pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type) & (0x7 << FRAME_TYPE_BOTTOM_OFFSET))>>FRAME_TYPE_BOTTOM_OFFSET != FRAME_TYPE_B)
763                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_P << FRAME_TYPE_BOTTOM_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_TOP_OFFSET));
764             break;
765             case h264_PtypeI:
766             case h264_PtypeSI:
767             if( ((pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type) & (0x7 << FRAME_TYPE_BOTTOM_OFFSET))>>FRAME_TYPE_BOTTOM_OFFSET == FRAME_TYPE_INVALID)
768             {
769                pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type = (FRAME_TYPE_I << FRAME_TYPE_BOTTOM_OFFSET)|(pInfo->dpb.fs[pInfo->dpb.fs_dec_idc].pic_type & (0x7 << FRAME_TYPE_TOP_OFFSET));
770             }
771             break;
772             default:
773             break;
774 
775          }
776 
777       }
778 
779    }
780    return;
781 
782 }
783 
784 
785 //////#endif ///////////// IFDEF H264_PARSE_C///////////////////
786 
787