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:    MPEG-4 header.
10//
11*/
12
13
14#ifndef _H264_H_
15#define _H264_H_
16
17#ifdef HOST_ONLY
18#include <stdio.h>
19#include <stdlib.h>
20#include <memory.h>
21#endif
22
23#include "stdint.h"
24#include "viddec_debug.h"
25
26#include "viddec_fw_workload.h"
27#include "h264parse_sei.h"
28
29
30#ifdef WIN32
31#define mfd_printf OS_INFO
32#endif
33
34#ifdef H264_VERBOSE
35#define PRINTF(format, args...) OS_INFO("%s:  %s[%d]:: " format, __FILE__, __FUNCTION__ , __LINE__ ,  ## args )
36#else
37//#define PRINTF(args...)
38#endif
39
40//#pragma warning(disable : 4710) // function not inlined
41//#pragma warning(disable : 4514) // unreferenced inline function has been removed CL
42//#pragma warning(disable : 4100) // unreferenced formal parameter CL
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48#define MAX_INT32_VALUE 	0x7fffffff
49
50#define MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE 256
51#define MAX_CPB_CNT	32
52#define MAX_NUM_SLICE_GRPS 	1				//As per Annex A for high profile, the num_slice_groups_minus1 is 0
53#define MAX_PIC_LIST_NUM	8
54
55//#define MAX_PIC_SIZE_IN_MAP_UNITS	1024 //0 ???????? Henry
56#define MAX_NUM_REF_IDX_L0_ACTIVE	32
57//#define STARTCODE_BUF_SIZE 			2048+1024
58
59#define NUM_MMCO_OPERATIONS         17
60
61// Used to check whether the SEI RP is the only way for recovery (cisco contents)
62// This threshold will decide the interval of recovery even no error detected if no IDR during this time
63#define SEI_REC_CHECK_TH				8
64
65//SPS
66#define MAX_NUM_SPS			32
67#define SCL_DEFAULT 		1
68
69//PPS
70#define MAX_PIC_PARAMS		255
71#define MAX_NUM_REF_FRAMES	32
72#define MAX_QP				51
73#define MAX_NUM_PPS			256
74
75#define PUT_FS_IDC_BITS(w)                                (w&0x1F)
76#define PUT_LIST_INDEX_FIELD_BIT(w)                       ((w&0x1)<<5)
77#define PUT_LIST_LONG_TERM_BITS(w)                        ((w&0x1)<<6)
78#define PUT_LIST_PTR_LIST_ID_BIT(id)                      (id<<5)
79
80
81// DPB
82#define FRAME_FLAG_DANGLING_TOP_FIELD        ( 0x1 << 3  )
83#define FRAME_FLAG_DANGLING_BOTTOM_FIELD     ( 0x1 << 4  )
84
85#define MPD_DPB_FS_NULL_IDC			31            // May need to be changed if we alter gaps_in_frame_num to use
86
87#define MFD_H264_MAX_FRAME_BUFFERS  17
88#define NUM_DPB_FRAME_STORES        (MFD_H264_MAX_FRAME_BUFFERS + 1)  // 1 extra for storign non-existent pictures.
89
90//Scalling Matrix Type
91#define PPS_QM                  0
92#define SPS_QM                  1
93#define FB_QM                   2
94#define DEFAULT_QM              3
95
96//Frame Type
97#define FRAME_TYPE_IDR        0x00
98#define FRAME_TYPE_I          0x01
99#define FRAME_TYPE_P          0x02
100#define FRAME_TYPE_B          0x03
101#define FRAME_TYPE_INVALID    0x04
102
103
104#define FRAME_TYPE_FRAME_OFFSET     3
105#define FRAME_TYPE_TOP_OFFSET       3
106#define FRAME_TYPE_BOTTOM_OFFSET    0
107#define FRAME_TYPE_STRUCTRUE_OFFSET 6
108
109//// Error handling
110#define FIELD_ERR_OFFSET		17			//offset for Field error flag ----refer to the structure definition viddec_fw_workload_error_codes in viddec_fw_common_defs.h
111
112////Bits Handling
113#define h264_bitfields_extract(x_32, start, mask)     (((x_32) >> (start)) & (mask) )
114#define h264_bitfields_insert(x_32, val_32, start, mask) ((x_32) = (((x_32) & ~( (mask) << (start))) | (((val_32) & (mask)) << (start))))
115
116
117//// PIP
118typedef enum _pip_setting_t
119{
120  PIP_SCALER_DISABLED,
121  PIP_SCALE_FACTOR_1_BY_4,
122  PIP_SCALE_FACTOR_1_BY_2,
123  PIP_SCALER_INVALID,
124
125} pip_setting_t;
126
127
128#ifdef VERBOSE
129#define DEBUGGETBITS(args...)  OS_INFO( args )
130#else
131//#define DEBUGGETBITS(args...)
132#endif
133
134/* status codes */
135typedef enum _h264_Status
136{
137    H264_STATUS_EOF          =  1,   // end of file
138    H264_STATUS_OK           =  0,   // no error
139    H264_STATUS_NO_MEM       =  2,   // out of memory
140    H264_STATUS_FILE_ERROR   =  3,   // file error
141    H264_STATUS_NOTSUPPORT   =  4,   // not supported mode
142    H264_STATUS_PARSE_ERROR  =  5,   // fail in parse MPEG-4 stream
143    H264_STATUS_ERROR        =  6,   // unknown/unspecified error
144    H264_NAL_ERROR,
145	H264_SPS_INVALID_PROFILE,
146	H264_SPS_INVALID_LEVEL,
147	H264_SPS_INVALID_SEQ_PARAM_ID,
148	H264_SPS_ERROR,
149	H264_PPS_INVALID_PIC_ID,
150	H264_PPS_INVALID_SEQ_ID,
151	H264_PPS_ERROR,
152	H264_SliceHeader_INVALID_MB,
153	H264_SliceHeader_ERROR,
154	H264_FRAME_DONE,
155	H264_SLICE_DONE,
156	H264_STATUS_POLL_ONCE_ERROR,
157	H264_STATUS_DEC_MEMINIT_ERROR,
158	H264_STATUS_NAL_UNIT_TYPE_ERROR,
159	H264_STATUS_SEI_ERROR,
160	H264_STATUS_SEI_DONE,
161} h264_Status;
162
163
164
165typedef enum _picture_structure_t
166{
167   TOP_FIELD		= 1,
168   BOTTOM_FIELD		= 2,
169   FRAME			= 3,
170   INVALID			= 4
171} picture_structure_t;
172
173///// Chorma format
174
175typedef enum _h264_chroma_format_t
176{
177	H264_CHROMA_MONOCHROME,
178	H264_CHROMA_420,
179	H264_CHROMA_422,
180	H264_CHROMA_444,
181}h264_chroma_format_t;
182
183/* H264 start code values */
184typedef enum _h264_nal_unit_type
185{
186  	h264_NAL_UNIT_TYPE_unspecified = 0,
187	h264_NAL_UNIT_TYPE_SLICE,
188	h264_NAL_UNIT_TYPE_DPA,
189	h264_NAL_UNIT_TYPE_DPB,
190	h264_NAL_UNIT_TYPE_DPC,
191	h264_NAL_UNIT_TYPE_IDR,
192	h264_NAL_UNIT_TYPE_SEI,
193	h264_NAL_UNIT_TYPE_SPS,
194	h264_NAL_UNIT_TYPE_PPS,
195	h264_NAL_UNIT_TYPE_Acc_unit_delimiter,
196	h264_NAL_UNIT_TYPE_EOSeq,
197	h264_NAL_UNIT_TYPE_EOstream,
198	h264_NAL_UNIT_TYPE_filler_data,
199	h264_NAL_UNIT_TYPE_SPS_extension,
200	h264_NAL_UNIT_TYPE_Reserved1			=14,		/*14-18*/
201	h264_NAL_UNIT_TYPE_Reserved2			=15,		/*14-18*/
202	h264_NAL_UNIT_TYPE_Reserved3			=16,		/*14-18*/
203	h264_NAL_UNIT_TYPE_Reserved4			=17,		/*14-18*/
204	h264_NAL_UNIT_TYPE_Reserved5			=18,		/*14-18*/
205	h264_NAL_UNIT_TYPE_ACP				=19,
206	h264_NAL_UNIT_TYPE_Reserved6			=20,		/*20-23*/
207	h264_NAL_UNIT_TYPE_unspecified2		=24,		/*24-31*/
208} h264_nal_unit_type;
209
210#define h264_NAL_PRIORITY_HIGHEST     3
211#define h264_NAL_PRIORITY_HIGH        2
212#define h264_NAL_PRIRITY_LOW          1
213#define h264_NAL_PRIORITY_DISPOSABLE  0
214
215
216typedef enum _h264_Profile
217{
218    h264_ProfileBaseline = 66,  	/** Baseline profile */
219    h264_ProfileMain = 77,        	/** Main profile */
220    h264_ProfileExtended = 88,    	/** Extended profile */
221    h264_ProfileHigh = 100 ,     		/** High profile */
222    h264_ProfileHigh10 = 110,			/** High 10 profile */
223    h264_ProfileHigh422 = 122,		/** High profile 4:2:2 */
224    h264_ProfileHigh444 = 144,		/** High profile 4:4:4 */
225} h264_Profile;
226
227
228typedef enum _h264_Level
229{
230    h264_Level1b	= 9,		    /** Level 1b */
231    h264_Level1		= 10,			/** Level 1 */
232    h264_Level11	= 11, 		    /** Level 1.1 */
233	h264_Level12	= 12, 		    /** Level 1.2 */
234	h264_Level13	= 13, 		    /** Level 1.3 */
235	h264_Level2		= 20,			/** Level 2 */
236    h264_Level21 	= 21, 		    /** Level 2.1 */
237	h264_Level22	= 22, 		    /** Level 2.2 */
238	h264_Level3		= 30, 		    /** Level 3 */
239	h264_Level31	= 31, 		    /** Level 3.1 */
240	h264_Level32	= 32, 		    /** Level 3.2 */
241	h264_Level4		= 40, 		    /** Level 4 */
242	h264_Level41	= 41, 		    /** Level 4.1 */
243	h264_Level42	= 42, 		    /** Level 4.2 */
244	h264_Level5		= 50, 		    /** Level 5 */
245	h264_Level51	= 51, 		    /** Level 5.1 */
246	h264_LevelReserved = 255  /** Unknown profile */
247} h264_Level;
248
249
250typedef enum _h264_video_format
251{
252	h264_Component	=0,
253	h264_PAL,
254	h264_NTSC,
255	h264_SECAM,
256	h264_MAC,
257	h264_unspecified,
258	h264_Reserved6,
259	h264_Reserved7
260}h264_video_format;
261
262
263typedef enum _h264_fcm
264{
265    h264_ProgressiveFrame = 0,
266    h264_InterlacedFrame  = 1,
267    h264_InterlacedField  = 3,
268    h264_PictureFormatNone
269} h264_fcm;
270
271
272///// Define the picture types []
273typedef enum _h264_ptype_t
274{
275    h264_PtypeP = 0,
276    h264_PtypeB = 1,
277    h264_PtypeI = 2,
278    h264_PtypeSP = 3,
279    h264_PtypeSI = 4,
280    h264_Ptype_unspecified,
281} h264_ptype_t;
282
283
284///// Aspect ratio
285typedef enum _h264_aspect_ratio
286{
287	h264_AR_Unspecified = 0,
288	h264_AR_1_1 = 1,
289	h264_AR_12_11 = 2,
290	h264_AR_10_11 = 3,
291	h264_AR_16_11 = 4,
292	h264_AR_40_33 = 5,
293	h264_AR_24_11 = 6,
294	h264_AR_20_11 = 7,
295	h264_AR_32_11 = 8,
296	h264_AR_80_33 = 9,
297	h264_AR_18_11 = 10,
298	h264_AR_15_11 = 11,
299	h264_AR_64_33 = 12,
300	h264_AR_160_99 = 13,
301	h264_AR_4_3 = 14,
302	h264_AR_3_2 = 15,
303	h264_AR_2_1 = 16,
304	h264_AR_RESERVED = 17,
305	h264_AR_Extended_SAR = 255,
306}h264_aspect_ratio;
307
308
309//////////////////////////////////////////////
310
311//////////////////////////////////////////////
312// storable_picture
313
314/* Structure details
315   If all members remain ints
316   Size = 11 ints, i.e. 44 bytes
317*/
318
319typedef struct
320{
321  int32_t	poc;
322  int32_t	pic_num;
323
324  int32_t	long_term_pic_num;
325
326  uint8_t	long_term_frame_idx;
327  uint8_t	is_long_term;
328  uint8_t	used_for_reference;
329  uint8_t	pad_flag;  		// Used to indicate the status
330
331} storable_picture, *storable_picture_ptr;
332
333//////////////////////////////////////////////
334// frame store
335
336/* Structure details
337   If all members remain ints
338   Size = 46 ints, i.e. 184 bytes
339*/
340
341typedef struct _frame_store
342{
343	storable_picture frame;
344	storable_picture top_field;
345	storable_picture bottom_field;
346
347	int32_t	frame_num;
348
349	int32_t	frame_num_wrap;
350
351
352	uint8_t	fs_idc;
353	uint8_t	pic_type;            //bit7 structure: 1 frame , 0 field;
354	                              //bit4,5,6 top field (frame) pic type,  00 IDR 01 I 10 P 11 B 100 INVALID
355	                              //bit1,2,3 bottom pic type,  00 IDR 01 I 10 P 11 B 100 INVALID
356   uint8_t	long_term_frame_idx; // No two frame stores may have the same long-term frame index
357
358   #define viddec_h264_get_dec_structure(x)         h264_bitfields_extract( (x)->fs_flag_1, 0, 0x03)
359   #define viddec_h264_set_dec_structure(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 0, 0x03)
360   #define viddec_h264_get_is_used(x)         h264_bitfields_extract( (x)->fs_flag_1, 2, 0x03)
361   #define viddec_h264_set_is_frame_used(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 2, 0x03)
362   #define viddec_h264_set_is_top_used(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 2, 0x01)
363   #define viddec_h264_set_is_bottom_used(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 3, 0x01)
364   #define viddec_h264_get_is_skipped(x)         h264_bitfields_extract( (x)->fs_flag_1, 4, 0x03)
365   #define viddec_h264_set_is_frame_skipped(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 4, 0x03)
366   #define viddec_h264_set_is_top_skipped(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 4, 0x01)
367   #define viddec_h264_set_is_bottom_skipped(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 5, 0x01)
368   #define viddec_h264_get_is_long_term(x)         h264_bitfields_extract( (x)->fs_flag_1, 6, 0x03)
369   #define viddec_h264_set_is_frame_long_term(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 6, 0x03)
370   #define viddec_h264_set_is_top_long_term(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 6, 0x01)
371   #define viddec_h264_set_is_bottom_long_term(x, val)    h264_bitfields_insert ( (x)->fs_flag_1, (val), 7, 0x01)
372   uint8_t  fs_flag_1;
373
374
375   #define viddec_h264_get_is_non_existent(x)            h264_bitfields_extract( (x)->fs_flag_2, 0, 0x01)
376   #define viddec_h264_set_is_non_existent(x, val)       h264_bitfields_insert ( (x)->fs_flag_2, (val), 0, 0x01)
377   #define viddec_h264_get_is_output(x)                  h264_bitfields_extract( (x)->fs_flag_2, 1, 0x01)
378   #define viddec_h264_set_is_output(x, val)             h264_bitfields_insert ( (x)->fs_flag_2, (val), 1, 0x01)
379   #define viddec_h264_get_is_dangling(x)                h264_bitfields_extract( (x)->fs_flag_2, 2, 0x01)
380   #define viddec_h264_set_is_dangling(x, val)           h264_bitfields_insert ( (x)->fs_flag_2, (val), 2, 0x01)
381   #define viddec_h264_get_recovery_pt_picture(x)        h264_bitfields_extract( (x)->fs_flag_2, 3, 0x01)
382   #define viddec_h264_set_recovery_pt_picture(x, val)   h264_bitfields_insert ( (x)->fs_flag_2, (val), 3, 0x01)
383   #define viddec_h264_get_broken_link_picture(x)        h264_bitfields_extract( (x)->fs_flag_2, 4, 0x01)
384   #define viddec_h264_set_broken_link_picture(x, val)   h264_bitfields_insert ( (x)->fs_flag_2, (val), 4, 0x01)
385   #define viddec_h264_get_open_gop_entry(x)             h264_bitfields_extract( (x)->fs_flag_2, 5, 0x01)
386   #define viddec_h264_set_open_gop_entry(x, val)        h264_bitfields_insert ( (x)->fs_flag_2, (val), 5, 0x01)
387   #define viddec_h264_get_first_field_intra(x)          h264_bitfields_extract( (x)->fs_flag_2, 6, 0x01)
388   #define viddec_h264_set_first_field_intra(x, val)     h264_bitfields_insert ( (x)->fs_flag_2, (val), 6, 0x01)
389	uint8_t  fs_flag_2;
390
391   uint8_t  fs_flag_reserve_1;
392   uint8_t  fs_flag_reserve_2;
393   uint8_t  fs_flag_reserve_3;
394
395	// If non-reference, may have skipped pixel decode
396	//uint8_t	non_ref_skipped;
397} frame_store, *frame_param_ptr;
398
399//! Decoded Picture Buffer
400typedef struct _h264_decoded_picture_buffer
401{
402	///
403	int32_t     last_output_poc;
404	int32_t     max_long_term_pic_idx;
405
406	//// Resolutions
407	int32_t		PicWidthInMbs;
408	int32_t		FrameHeightInMbs;
409
410	frame_store	fs[NUM_DPB_FRAME_STORES];
411
412	uint8_t		fs_ref_idc[16];
413	uint8_t		fs_ltref_idc[16];
414
415	uint8_t		fs_dpb_idc[NUM_DPB_FRAME_STORES+2];
416
417	uint8_t		listX_0[33+3];  // [bit5}:field_flag:0 for top, 1 for bottom, [bit4~0]:fs_idc
418	uint8_t		listX_1[33+3];
419
420	uint8_t		listXsize[2]; // 1 to 32
421	uint8_t		nInitListSize[2];
422
423	//uint32_t	size;
424	uint8_t		fs_dec_idc;
425	uint8_t		fs_non_exist_idc;
426	uint8_t		BumpLevel;
427	uint8_t		used_size;
428
429	uint8_t		OutputLevel;
430	uint8_t		OutputLevelValid;
431	uint8_t		OutputCtrl;
432	uint8_t     num_ref_frames;
433
434	uint8_t		ref_frames_in_buffer;
435	uint8_t		ltref_frames_in_buffer;
436	uint8_t		SuspendOutput;
437	uint8_t		WaitSeiRecovery;
438
439
440   uint8_t		frame_numbers_need_to_be_allocated;
441   uint8_t		frame_id_need_to_be_allocated;
442
443	//// frame list to release from dpb, need be displayed
444   uint8_t		frame_numbers_need_to_be_removed;
445   uint8_t		frame_id_need_to_be_removed[17];
446
447	//// frame list to removed from dpb but not display
448   uint8_t		frame_numbers_need_to_be_dropped;
449   uint8_t		frame_id_need_to_be_dropped[17];
450
451	//// frame list to display (in display order)
452   uint8_t		frame_numbers_need_to_be_displayed;
453   uint8_t		frame_id_need_to_be_displayed[17];
454
455
456} h264_DecodedPictureBuffer;
457
458
459//////////////////////////////////////////////
460// qm_matrix_set
461typedef struct _qm_matrix_set
462{
463 // uint8_t scaling_default_vector;
464  uint8_t scaling_list[56];            // 0 to 23 for qm 0 to 5 (4x4), 24 to 55 for qm 6 & 7 (8x8)
465
466} qm_matrix_set, *qm_matrix_set_ptr;
467
468/*
469///////// Currently not enabled in parser fw///////////////////
470typedef struct _h264_SPS_Extension_RBSP {
471	int32_t 			seq_parameter_set_id;					//UE
472	int32_t				aux_format_idc;							//UE
473	int32_t				bit_depth_aux_minus8;					//UE
474	int32_t				alpha_incr_flag;
475	int32_t				alpha_opaque_value;
476	int32_t				alpha_transparent_value;
477	int32_t				additional_extension_flag;
478//	h264_rbsp_trail_set* rbsp_trail_ptr;
479}h264_SPS_Extension_RBSP_t;
480*/
481
482typedef struct _h264_hrd_param_set {
483	int32_t				bit_rate_value_minus1[MAX_CPB_CNT];			// ue(v), 0 to (2^32)-2
484	int32_t				cpb_size_value_minus1[MAX_CPB_CNT];			// ue(v), 0 to (2^32)-2
485
486	uint8_t				cbr_flag[MAX_CPB_CNT];							// u(1) * 32
487
488} h264_hrd_param_set, *h264_hrd_param_set_ptr;
489
490typedef struct _vui_seq_parameters_t_used
491{
492	uint32_t	num_units_in_tick;                             // u(32)
493	uint32_t	time_scale;                                    // u(32)
494
495	int32_t  num_reorder_frames;                               // ue(v), 0 to max_dec_frame_buffering
496	int32_t	max_dec_frame_buffering;                          // ue(v), 0 to MaxDpbSize, specified in subclause A.3
497
498	uint16_t	 sar_width;                                       // u(16)
499	uint16_t	 sar_height;                                      // u(16)
500
501	uint8_t   aspect_ratio_info_present_flag;                  // u(1)
502	uint8_t   aspect_ratio_idc;                                // u(8)
503	uint8_t   video_signal_type_present_flag;                  // u(1)
504	uint8_t   video_format;                                    // u(3)
505
506	uint8_t   colour_description_present_flag;                 // u(1)
507	uint8_t   colour_primaries;                                // u(8)
508	uint8_t   transfer_characteristics;                        // u(8)
509	uint8_t   timing_info_present_flag;                        // u(1)
510
511	uint8_t   fixed_frame_rate_flag;                           // u(1)
512	uint8_t   low_delay_hrd_flag;                              // u(1)
513	uint8_t   bitstream_restriction_flag;                      // u(1)
514	uint8_t   pic_struct_present_flag;
515
516	uint8_t   nal_hrd_parameters_present_flag;                 // u(1)
517	uint8_t 	 nal_hrd_cpb_removal_delay_length_minus1;				// u(5)
518	uint8_t   nal_hrd_dpb_output_delay_length_minus1;				// u(5)
519	uint8_t   nal_hrd_time_offset_length;								// u(5)
520
521	uint8_t   nal_hrd_cpb_cnt_minus1;									// ue(v), 0 to 31
522	uint8_t   nal_hrd_initial_cpb_removal_delay_length_minus1;	// u(5)
523   uint8_t   vcl_hrd_parameters_present_flag;                 // u(1)
524	uint8_t 	 vcl_hrd_cpb_removal_delay_length_minus1;				// u(5)
525
526	uint8_t   vcl_hrd_dpb_output_delay_length_minus1;				// u(5)
527	uint8_t   vcl_hrd_time_offset_length;								// u(5)
528	uint8_t   vcl_hrd_cpb_cnt_minus1;									// ue(v), 0 to 31
529	uint8_t   vcl_hrd_initial_cpb_removal_delay_length_minus1;	// u(5)
530
531	/////// Here should be kept as 32-bits aligned for next structures
532	/// 2 structures for NAL&VCL HRD
533
534
535} vui_seq_parameters_t_used;
536
537
538typedef struct _vui_seq_parameters_t_not_used
539{
540	int16_t  chroma_sample_loc_type_top_field;                // ue(v)
541	int16_t  chroma_sample_loc_type_bottom_field;             // ue(v)
542
543	uint8_t   overscan_info_present_flag;                      // u(1)
544	uint8_t   overscan_appropriate_flag;                       // u(1)
545
546   uint8_t   video_full_range_flag;                           // u(1)
547	uint8_t   matrix_coefficients;                             // u(8)
548
549   uint8_t   chroma_location_info_present_flag;               // u(1)
550	uint8_t   max_bytes_per_pic_denom;                          // ue(v), 0 to 16
551	uint8_t   max_bits_per_mb_denom;                            // ue(v), 0 to 16
552	uint8_t   log2_max_mv_length_vertical;                      // ue(v), 0 to 16, default to 16
553	uint8_t   log2_max_mv_length_horizontal;                    // ue(v), 0 to 16, default to 16
554
555	uint8_t   motion_vectors_over_pic_boundaries_flag;          // u(1)
556
557   uint8_t   nal_hrd_bit_rate_scale;									// u(4)
558	uint8_t   nal_hrd_cpb_size_scale;									// u(4)
559
560   uint8_t   vcl_hrd_bit_rate_scale;									// u(4)
561	uint8_t   vcl_hrd_cpb_size_scale;									// u(4)
562
563   h264_hrd_param_set nal_hrd_parameters;
564   h264_hrd_param_set vcl_hrd_parameters;
565
566
567} vui_seq_parameters_t_not_used, *vui_seq_parameters_t_not_used_ptr;
568
569
570//////////////////////////////////////////////
571// picture parameter set
572
573typedef struct _PPS_PAR
574{
575	//int32_t DOUBLE_ALIGN valid;                          // indicates the parameter set is valid
576
577	int32_t pic_init_qp_minus26;                             // se(v), -26 to +25
578	int32_t pic_init_qs_minus26;                             // se(v), -26 to +25
579	int32_t chroma_qp_index_offset;                          // se(v), -12 to +12
580	int32_t second_chroma_qp_index_offset;
581
582	uint8_t pic_parameter_set_id;                            // ue(v), 0 to 255, restricted to 0 to 127 by MPD_CTRL_MAXPPS = 128
583	uint8_t seq_parameter_set_id;                            // ue(v), 0 to 31
584	uint8_t entropy_coding_mode_flag;                        // u(1)
585	uint8_t pic_order_present_flag;                          // u(1)
586
587	uint8_t num_slice_groups_minus1;                         // ue(v), shall be 0 for MP
588	// Below are not relevant for main profile...
589	uint8_t slice_group_map_type;                            // ue(v), 0 to 6
590	uint8_t num_ref_idx_l0_active;							// ue(v), 0 to 31
591	uint8_t num_ref_idx_l1_active;							// ue(v), 0 to 31
592
593	uint8_t weighted_pred_flag;                              // u(1)
594	uint8_t weighted_bipred_idc;                             // u(2)
595	uint8_t deblocking_filter_control_present_flag;          // u(1)
596	uint8_t constrained_intra_pred_flag;                     // u(1)
597
598	uint8_t redundant_pic_cnt_present_flag;                  // u(1)
599	uint8_t transform_8x8_mode_flag;
600	uint8_t pic_scaling_matrix_present_flag;
601	uint8_t pps_status_flag;
602
603	//// Keep here with 32-bits aligned
604	uint8_t	pic_scaling_list_present_flag[MAX_PIC_LIST_NUM];
605
606	qm_matrix_set	pps_qm;
607
608	uint8_t 		ScalingList4x4[6][16];
609	uint8_t 		ScalingList8x8[2][64];
610	uint8_t   	UseDefaultScalingMatrix4x4Flag[6+2];
611	uint8_t		UseDefaultScalingMatrix8x8Flag[6+2];
612
613} pic_param_set, *pic_param_set_ptr, h264_PicParameterSet_t;
614
615typedef union _list_reordering_num_t
616{
617	int32_t abs_diff_pic_num_minus1;
618	int32_t long_term_pic_num;
619} list_reordering_num_t;
620
621typedef struct _h264_Ref_Pic_List_Reordering				////size = 8*33+ 1 + 33
622{
623	list_reordering_num_t list_reordering_num[MAX_NUM_REF_FRAMES+1];
624
625	uint8_t			ref_pic_list_reordering_flag;
626	uint8_t			reordering_of_pic_nums_idc[MAX_NUM_REF_FRAMES+1];							//UE
627
628}h264_Ref_Pic_List_Reordering_t;
629
630typedef enum _H264_DANGLING_TYPE
631{
632	DANGLING_TYPE_LAST_FIELD,
633	DANGLING_TYPE_DPB_RESET,
634	DANGLING_TYPE_FIELD,
635	DANGLING_TYPE_FRAME,
636	DANGLING_TYPE_GAP_IN_FRAME
637
638} H264_DANGLING_TYPE;
639
640
641typedef struct _h264_Dec_Ref_Pic_Marking			//size = 17*4*2 + 17*3 + 4 + 1
642{
643	int32_t		difference_of_pic_num_minus1[NUM_MMCO_OPERATIONS];
644	int32_t		long_term_pic_num[NUM_MMCO_OPERATIONS];
645
646	/// MMCO
647	uint8_t		memory_management_control_operation[NUM_MMCO_OPERATIONS];
648	uint8_t		max_long_term_frame_idx_plus1[NUM_MMCO_OPERATIONS];
649	uint8_t		long_term_frame_idx[NUM_MMCO_OPERATIONS];
650	uint8_t		long_term_reference_flag;
651
652	uint8_t		adaptive_ref_pic_marking_mode_flag;
653	uint8_t		dec_ref_pic_marking_count;
654	uint8_t		no_output_of_prior_pics_flag;
655
656	uint8_t		pad;
657}h264_Dec_Ref_Pic_Marking_t;
658
659
660
661typedef struct old_slice_par
662{
663	int32_t		frame_num;
664	int32_t		pic_order_cnt_lsb;
665	int32_t		delta_pic_order_cnt_bottom;
666	int32_t		delta_pic_order_cnt[2];
667
668	uint8_t		field_pic_flag;
669	uint8_t		bottom_field_flag;
670	uint8_t		nal_ref_idc;
671	uint8_t		structure;
672
673	uint8_t		idr_flag;
674	uint8_t		idr_pic_id;
675	uint8_t		pic_parameter_id;
676	uint8_t		status;
677} OldSliceParams;
678
679#ifdef VBP
680typedef struct _h264__pred_weight_table
681{
682  uint8_t luma_log2_weight_denom;
683  uint8_t chroma_log2_weight_denom;
684  uint8_t luma_weight_l0_flag;
685  int16_t luma_weight_l0[32];
686  int8_t luma_offset_l0[32];
687  uint8_t chroma_weight_l0_flag;
688  int16_t chroma_weight_l0[32][2];
689  int8_t chroma_offset_l0[32][2];
690
691  uint8_t luma_weight_l1_flag;
692  int16_t luma_weight_l1[32];
693  int8_t luma_offset_l1[32];
694  uint8_t chroma_weight_l1_flag;
695  int16_t chroma_weight_l1[32][2];
696  int8_t chroma_offset_l1[32][2];
697} h264_pred_weight_table;
698#endif
699
700typedef struct _h264_Slice_Header
701{
702	int32_t 		first_mb_in_slice;								//UE
703	int32_t		frame_num;											//UV
704	int32_t		pic_order_cnt_lsb;								//UV
705	int32_t		delta_pic_order_cnt_bottom;					//SE
706	int32_t		delta_pic_order_cnt[2];								//SE
707	int32_t		redundant_pic_cnt;									//UE
708
709	uint32_t		num_ref_idx_l0_active;								//UE
710	uint32_t		num_ref_idx_l1_active;								//UE
711
712	int32_t		slice_qp_delta;										//SE
713	int32_t		slice_qs_delta;										//SE
714	int32_t		slice_alpha_c0_offset_div2;						//SE
715	int32_t		slice_beta_offset_div2;								//SE
716	int32_t		slice_group_change_cycle;							//UV
717
718#ifdef VBP
719  h264_pred_weight_table  sh_predwttbl;
720#endif
721
722	///// Flags or IDs
723	//h264_ptype_t	slice_type;											//UE
724	uint8_t			slice_type;
725	uint8_t 			nal_ref_idc;
726	uint8_t			structure;
727	uint8_t 			pic_parameter_id;									//UE
728
729	uint8_t			field_pic_flag;
730	uint8_t			bottom_field_flag;
731	uint8_t			idr_flag;											//UE
732	uint8_t			idr_pic_id;											//UE
733
734	uint8_t 			sh_error;
735	uint8_t			cabac_init_idc;										//UE
736	uint8_t			sp_for_switch_flag;
737	uint8_t			disable_deblocking_filter_idc;						//UE
738
739	uint8_t			direct_spatial_mv_pred_flag;
740	uint8_t			num_ref_idx_active_override_flag;
741	int16_t			current_slice_nr;
742
743	//// For Ref list reordering
744	h264_Dec_Ref_Pic_Marking_t sh_dec_refpic;
745	h264_Ref_Pic_List_Reordering_t sh_refpic_l0;
746	h264_Ref_Pic_List_Reordering_t sh_refpic_l1;
747
748} h264_Slice_Header_t;
749
750
751#define   MAX_USER_DATA_SIZE              1024
752typedef struct _h264_user_data_t
753{
754	h264_sei_payloadtype    user_data_type;
755
756  	int32_t    user_data_id;
757  	int32_t    dsn;
758  	int32_t    user_data_size;
759  	int32_t    user_data[MAX_USER_DATA_SIZE>>2];
760} h264_user_data_t;
761
762// SPS DISPLAY parameters: seq_param_set_disp, *seq_param_set_disp_ptr;
763typedef struct _SPS_DISP
764{
765	///// VUI info
766	vui_seq_parameters_t_used vui_seq_parameters;    //size =
767
768	///// Resolution
769	int16_t pic_width_in_mbs_minus1;
770	int16_t pic_height_in_map_units_minus1;
771
772	///// Cropping
773	int16_t frame_crop_rect_left_offset;
774	int16_t frame_crop_rect_right_offset;
775
776	int16_t frame_crop_rect_top_offset;
777	int16_t frame_crop_rect_bottom_offset;
778
779	///// Flags
780	uint8_t frame_mbs_only_flag;
781	uint8_t mb_adaptive_frame_field_flag;
782	uint8_t direct_8x8_inference_flag;
783	uint8_t frame_cropping_flag;
784
785	uint16_t vui_parameters_present_flag;
786	uint16_t chroma_format_idc;
787} seq_param_set_disp, *seq_param_set_disp_ptr;
788
789
790////SPS: seq_param_set, *seq_param_set_ptr;
791
792typedef struct _SPS_PAR_USED
793{
794   uint32_t    is_updated;
795
796	/////////// Required for display section //////////////////////////
797	seq_param_set_disp sps_disp;
798
799	int32_t		expectedDeltaPerPOCCycle;
800	int32_t 		offset_for_non_ref_pic;                           // se(v), -2^31 to (2^31)-1, 32-bit integer
801	int32_t 		offset_for_top_to_bottom_field;                   // se(v), -2^31 to (2^31)-1, 32-bit integer
802
803	/////////// IDC
804	uint8_t 		profile_idc;                                      // u(8), 0x77 for MP
805	uint8_t 		constraint_set_flags;                             // bit 0 to 3 for set0 to set3
806	uint8_t 		level_idc;                                        // u(8)
807	uint8_t 		seq_parameter_set_id;                             // ue(v), 0 to 31
808
809
810	uint8_t 		pic_order_cnt_type;                               // ue(v), 0 to 2
811	uint8_t 		log2_max_frame_num_minus4;                        // ue(v), 0 to 12
812	uint8_t 		log2_max_pic_order_cnt_lsb_minus4;                // ue(v), 0 to 12
813	uint8_t 		num_ref_frames_in_pic_order_cnt_cycle;            // ue(v), 0 to 255
814
815	//int32_t offset_for_ref_frame[MAX_NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE];   // se(v), -2^31 to (2^31)-1, 32-bit integer
816	uint8_t 		num_ref_frames;                                   // ue(v), 0 to 16,
817	uint8_t 		gaps_in_frame_num_value_allowed_flag;             // u(1)
818	// This is my addition, we should calculate this once and leave it with the sps
819	// as opposed to calculating it each time in h264_hdr_decoding_POC()
820
821	uint8_t 		delta_pic_order_always_zero_flag;                 // u(1)
822	uint8_t		residual_colour_transform_flag;
823
824	uint8_t		bit_depth_luma_minus8;
825	uint8_t		bit_depth_chroma_minus8;
826	uint8_t		lossless_qpprime_y_zero_flag;
827	uint8_t		seq_scaling_matrix_present_flag;
828
829	uint8_t		seq_scaling_list_present_flag[MAX_PIC_LIST_NUM];			//0-7
830
831	//// Combine the scaling matrix to word ( 24 + 32)
832	uint8_t 		ScalingList4x4[6][16];
833	uint8_t 		ScalingList8x8[2][64];
834	uint8_t		UseDefaultScalingMatrix4x4Flag[6];
835	uint8_t		UseDefaultScalingMatrix8x8Flag[6];
836
837} seq_param_set_used, *seq_param_set_used_ptr;
838
839
840typedef struct _SPS_PAR_ALL
841{
842
843    seq_param_set_used  sps_par_used;
844    vui_seq_parameters_t_not_used sps_vui_par_not_used;
845
846}seq_param_set_all, *seq_param_set_all_ptr;
847
848
849///// Image control parameter////////////
850typedef struct _h264_img_par
851{
852	int32_t frame_num;				// decoding num of current frame
853	int32_t frame_count;				// count of decoded frames
854	int32_t current_slice_num;
855	int32_t gaps_in_frame_num;
856
857	// POC decoding
858	int32_t num_ref_frames_in_pic_order_cnt_cycle;
859	int32_t delta_pic_order_always_zero_flag;
860	int32_t offset_for_non_ref_pic;
861	int32_t offset_for_top_to_bottom_field;
862
863	int32_t pic_order_cnt_lsb;
864	int32_t pic_order_cnt_msb;
865	int32_t delta_pic_order_cnt_bottom;
866	int32_t delta_pic_order_cnt[2];
867
868	int32_t PicOrderCntMsb;
869	int32_t CurrPicOrderCntMsb;
870	int32_t PrevPicOrderCntLsb;
871
872	int32_t FrameNumOffset;
873
874	int32_t PreviousFrameNum;
875	int32_t PreviousFrameNumOffset;
876
877	int32_t toppoc;
878	int32_t bottompoc;
879	int32_t framepoc;
880	int32_t ThisPOC;
881
882	//int32_t sei_freeze_this_image;
883
884	///////////////////// Resolutions
885	int32_t PicWidthInMbs;
886	int32_t FrameHeightInMbs;
887
888	///////////////////// MMCO
889	uint8_t last_has_mmco_5;
890	uint8_t curr_has_mmco_5;
891
892	/////////////////// Flags
893	uint8_t g_new_frame;
894	uint8_t g_new_pic;
895
896	uint8_t structure;
897	uint8_t second_field;           // Set to one if this is the second field of a set of paired fields...
898	uint8_t field_pic_flag;
899	uint8_t last_pic_bottom_field;
900
901	uint8_t bottom_field_flag;
902	uint8_t MbaffFrameFlag;
903	uint8_t no_output_of_prior_pics_flag;
904	uint8_t long_term_reference_flag;
905
906	uint8_t skip_this_pic;
907	uint8_t pic_order_cnt_type;
908	// Recovery
909	uint8_t recovery_point_found;
910	uint8_t used_for_reference;
911} h264_img_par;
912
913
914typedef struct  _h264_slice_reg_data
915{
916	uint32_t h264_bsd_slice_p1;      // 0x150
917	//uint32_t h264_mpr_list0[8];       // from 0x380 to 0x3BC
918	uint32_t h264_bsd_slice_p2;      // 0x154
919	uint32_t h264_bsd_slice_start;   // 0x158
920
921} h264_slice_data;
922
923
924typedef struct  _h264_pic_data
925{
926	uint32_t h264_dpb_init;          // 0x40
927	//info For current pic
928	uint32_t h264_cur_bsd_img_init;      // 0x140
929	uint32_t h264_cur_mpr_tf_poc;        // 0x300
930	uint32_t h264_cur_mpr_bf_poc;        // 0x304
931
932	//info For framess in DPB
933	//uint32_t h264_dpb_bsd_img_init[16];      //0x140
934	//uint32_t h264_dpb_mpr_tf_poc[16];        // 0x300
935	//uint32_t h264_dpb_mpr_bf_poc[16];        // 0x304
936} h264_pic_data;
937
938enum h264_workload_item_type
939{
940   VIDDEC_WORKLOAD_H264_SLICE_REG = VIDDEC_WORKLOAD_DECODER_SPECIFIC,
941   VIDDEC_WORKLOAD_H264_PIC_REG,
942   VIDDEC_WORKLOAD_H264_DPB_FRAME_POC,
943   VIDDEC_WORKLOAD_H264_SH_BITS_OFFSET,
944   VIDDEC_WORKLOAD_H264_PWT_BITS_OFFSET,
945   VIDDEC_WORKLOAD_H264_PWT_ES_BYTES,
946   VIDDEC_WORKLOAD_H264_SCALING_MATRIX,
947   VIDDEC_WORKLOAD_H264_DEBUG
948};
949
950
951
952////////////////////////////////////////////
953/* Full Info set*/
954////////////////////////////////////////////
955typedef struct _h264_Info
956{
957
958   h264_DecodedPictureBuffer           dpb;
959
960   //// Structures
961   //// need to gurantee active_SPS and active_PPS start from 4-bytes alignment address
962   seq_param_set_used	active_SPS;
963   pic_param_set			active_PPS;
964
965
966   h264_Slice_Header_t  SliceHeader;
967   OldSliceParams       old_slice;
968   sei_info             sei_information;
969
970   h264_img_par      img;
971
972   uint32_t          SPS_PADDR_GL;
973   uint32_t          PPS_PADDR_GL;
974   uint32_t          OFFSET_REF_FRAME_PADDR_GL;
975	uint32_t				TMP_OFFSET_REFFRM_PADDR_GL;
976
977   uint32_t          h264_list_replacement;
978
979   uint32_t          h264_pwt_start_byte_offset;
980   uint32_t          h264_pwt_start_bit_offset;
981   uint32_t          h264_pwt_end_byte_offset;
982   uint32_t          h264_pwt_end_bit_offset;
983   uint32_t          h264_pwt_enabled;
984
985	uint32_t          sps_valid;
986
987   uint8_t           slice_ref_list0[32];
988   uint8_t           slice_ref_list1[32];
989
990
991   uint8_t           qm_present_list;
992   //h264_NAL_Unit_t
993   uint8_t           nal_unit_type;
994   uint8_t           old_nal_unit_type;
995   uint8_t    			got_start;
996
997   //workload
998   uint8_t           push_to_cur;
999   uint8_t           Is_first_frame_in_stream;
1000   uint8_t           Is_SPS_updated;
1001   uint8_t           number_of_first_au_info_nal_before_first_slice;
1002
1003   uint8_t           is_frame_boundary_detected_by_non_slice_nal;
1004   uint8_t           is_frame_boundary_detected_by_slice_nal;
1005   uint8_t           is_current_workload_done;
1006   uint8_t			 primary_pic_type_plus_one;	  //AUD---[0,7]
1007
1008	//Error handling
1009	uint8_t			sei_rp_received;
1010	uint8_t			last_I_frame_idc;
1011	uint8_t			sei_b_state_ready;
1012	uint8_t			gop_err_flag;
1013
1014
1015	uint32_t		wl_err_curr;
1016	uint32_t		wl_err_next;
1017
1018} h264_Info;
1019
1020
1021
1022struct h264_viddec_parser
1023{
1024   uint32_t     sps_pps_ddr_paddr;
1025   h264_Info    info;
1026};
1027
1028
1029
1030
1031
1032#endif  //_H264_H_
1033
1034
1035