xref: /utopia/UTPA2-700.0.x/modules/mfe/drv/mfe_ex/cModel/m4v_header.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 
79 
80 
81 #include "MFE_chip.h"
82 #include "mfe_type.h"
83 #include "mfe_common.h"
84 #include "mdrv_mfe_math.h"
85 #include "ms_dprintf.h"
86 
87 
88 
89 #include "m4v_header.h"
90 #include "msRateCtrl.h"
91 
92 
93 
94 #define MARKER_BIT						1UL
95 
96 #define START_CODE_PREFIX				1UL
97 #define NUMBITS_START_CODE_PREFIX		24UL
98 //	Added for data partitioning mode By Toshiba(1998-1-16:DP+RVLC)
99 #define NUMBITS_START_CODE_SUFFIX		8UL
100 //	End Toshiba(1998-1-16:DP+RVLC)
101 
102 // session overhead information
103 #define USER_DATA_START_CODE			0xB2UL
104 #define VSS_START_CODE					0xB0UL	// 8-bit
105 #define VSS_END_CODE					0xB1UL	// 8-bit
106 #define NUMBITS_VSS_PROFILE				8UL
107 #define VSO_START_CODE					0xB5UL	// 8-bit
108 #define VSO_VERID						1UL
109 #define VSO_TYPE						1UL
110 #define NUMBITS_VSO_VERID				4UL
111 #define NUMBITS_VSO_PRIORITY			3UL
112 #define NUMBITS_VSO_TYPE				4UL
113 
114 // VO overhead information
115 #define NUMBITS_VO_START_CODE			3UL
116 #define VO_START_CODE					0UL
117 #define NUMBITS_VO_ID					5UL
118 
119 #define NUMBITS_NEWPRED_ENABLE			1UL
120 
121 // VOL overhead information
122 #define NUMBITS_SHORT_HEADER_START_CODE 22UL // Added by KPN for short headers (1998-02-07, DS)
123 #define SHORT_VIDEO_START_MARKER        32UL // Added by KPN for short headers (1998-02-07, DS)
124 #define NUMBITS_VOL_START_CODE			4UL
125 #define VOL_START_CODE					2UL
126 #define NUMBITS_VOL_ID					4UL
127 #define NUMBITS_VOL_SHAPE				2UL
128 #define NUMBITS_TIME_RESOLUTION			16UL
129 #define NUMBITS_VOL_FCODE				3UL
130 #define NUMBITS_SEP_MOTION_TEXTURE		1UL
131 #define NUMBITS_QMATRIX					8UL
132 
133 // VOP overhead information
134 #define VOP_START_CODE					0xB6UL
135 #define NUMBITS_VOP_START_CODE			8UL
136 #define NUMBITS_VOP_TIMEINCR			10UL
137 #define NUMBITS_VOP_HORIZONTAL_SPA_REF	13UL
138 #define NUMBITS_VOP_VERTICAL_SPA_REF	13UL
139 #define NUMBITS_VOP_WIDTH				13UL
140 #define NUMBITS_VOP_HEIGHT				13UL
141 #define NUMBITS_VOP_PRED_TYPE			2UL
142 #define NUMBITS_VOP_QUANTIZER			5UL
143 #define NUMBITS_VOP_ALPHA_QUANTIZER		6UL
144 #define NUMBITS_VOP_FCODE				3UL
145 
146 
147 
codeSequenceHead(MFE_CONFIG * pConfig,OutStream * pStream)148 void codeSequenceHead (MFE_CONFIG *pConfig, OutStream* pStream)
149 {
150 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, (MS_S8*)("VSS_Start_Code"));
151 	osPutBits(pStream, VSS_START_CODE, NUMBITS_START_CODE_SUFFIX, (MS_S8*)("VSS_Start_Code"));
152 	osPutBits(pStream, 0xF5/*m_volmd.uiProfileAndLevel*/, NUMBITS_VSS_PROFILE, (MS_S8*)("VSS_Profile_Level"));
153 
154 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, (MS_S8*)("VSO_Start_Code"));
155 	osPutBits(pStream, VSO_START_CODE, NUMBITS_START_CODE_SUFFIX, (MS_S8*)("VSO_Start_Code"));
156 	osPutBits(pStream, 0, 1, (MS_S8*)("VSO_IsVisualObjectIdentifier"));
157 	osPutBits(pStream, VSO_TYPE, NUMBITS_VSO_TYPE, (MS_S8*)("VSO_Type"));
158 	osPutBits(pStream, 0, 1, (MS_S8*)("VSO_VideoSignalType"));
159 
160 // 	UInt uiNumBits = NUMBITS_START_CODE_PREFIX + NUMBITS_START_CODE_SUFFIX + NUMBITS_VSS_PROFILE
161 // 		+ NUMBITS_START_CODE_PREFIX + 1 + NUMBITS_VSO_TYPE + 1;
162 //	m_statsVOL.nBitsHead += uiNumBits;
163 
164 //	m_statsVOL.nBitsStuffing += m_pbitstrmOut->flush ();
165 	osNextStartCode(pStream);
166 }
167 
codeVOHead(MFE_CONFIG * pConfig,OutStream * pStream)168 void codeVOHead (MFE_CONFIG *pConfig, OutStream* pStream)
169 {
170 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, (MS_S8*)("VO_Start_Code"));
171 	osPutBits(pStream, VO_START_CODE, NUMBITS_VO_START_CODE, (MS_S8*)("VO_Start_Code"));		//plus 3 bits
172 	osPutBits(pStream, /*m_uiVOId*/1, NUMBITS_VO_ID, (MS_S8*)("VO_Id"));
173 // 	U32 uiNumBits = NUMBITS_START_CODE_PREFIX + NUMBITS_VO_START_CODE + NUMBITS_VO_ID;
174 // 	m_statsVOL.nBitsHead += uiNumBits;
175 }
176 
codeVOLHead(MFE_CONFIG * pConfig,OutStream * pStream)177 void codeVOLHead (MFE_CONFIG *pConfig, OutStream* pStream)
178 {
179 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
180 	CVBRRateControl* rcCtx = &pConfig->ctxRateControl;
181 
182 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, (MS_S8*)("VOL_Start_Code_Prefix"));
183 	osPutBits(pStream, VOL_START_CODE, NUMBITS_VOL_START_CODE, (MS_S8*)("VOL_Start_Code"));		//plus 4 bits
184 	osPutBits(pStream, 0, NUMBITS_VOL_ID, (MS_S8*)("VOL_Id")); // by katata
185 //	m_statsVOL.nBitsHead+=NUMBITS_START_CODE_PREFIX+NUMBITS_VOL_START_CODE+NUMBITS_VOL_ID;
186 
187 	osPutBits(pStream, (MS_S32) 0, 1, (MS_S8*)("VOL_Random_Access"));		//isn't this a system level flg?
188 //	m_statsVOL.nBitsHead++;
189 
190 	osPutBits(pStream, (MS_S32) 1, 8, (MS_S8*)("VOL_Type_Indicator")); // Set to indicate SIMPLE profile.
191 //	m_statsVOL.nBitsHead+=8;
192 
193 	osPutBits(pStream, (MS_S32) 1, 1, (MS_S8*)("VOL_Is_Object_Layer_Identifier"));
194 	osPutBits(pStream, (MS_S32) 2/*m_volmd.uiVerID*/, 4, (MS_S8*)("VOL_Verid"));
195 	osPutBits(pStream, (MS_S32) 1, 3, (MS_S8*)("VOL_Priority"));
196 //	m_statsVOL.nBitsHead+=8;
197 
198 	osPutBits(pStream, (MS_S32) 1, 4, (MS_S8*)("aspect_ratio_info")); // square pix
199 //	m_statsVOL.nBitsHead+=4;
200 
201 	osPutBits(pStream, /*m_volmd.uiVolControlParameters*/0, 1, (MS_S8*)("VOL_Control_Parameters")); //useless flag for now
202 //	m_statsVOL.nBitsHead++;
203 
204 	{
205 		osPutBits(pStream, /*iAUsage*/0, NUMBITS_VOL_SHAPE, (MS_S8*)("VOL_Shape_Type"));
206 		osPutBits(pStream, 1, 1, (MS_S8*)("Marker"));
207 //		m_statsVOL.nBitsStuffing ++;
208 
209 		osPutBits(pStream, pInfo->nTimeResolution/*m_volmd.iClockRate*/, NUMBITS_TIME_RESOLUTION, (MS_S8*)("VOL_Time_Increment_Resolution"));
210 		osPutBits(pStream, 1, 1, (MS_S8*)("Marker"));
211 //		m_statsVOL.nBitsStuffing ++;
212 
213 		osPutBits(pStream, 0, 1, (MS_S8*)("VOL_Fixed_Vop_Rate"));
214 //		m_statsVOL.nBitsHead += NUMBITS_VOL_SHAPE + NUMBITS_TIME_RESOLUTION + 1;
215 
216 		osPutBits(pStream, MARKER_BIT, 1, (MS_S8*)("Marker_Bit"));
217 //		m_statsVOL.nBitsStuffing ++;
218 		osPutBits(pStream, pConfig->nBufWidth, NUMBITS_VOP_WIDTH, (MS_S8*)("VOL_Width"));
219 		osPutBits(pStream, MARKER_BIT, 1, (MS_S8*)("Marker_Bit"));
220 //		m_statsVOL.nBitsStuffing ++;
221 		osPutBits(pStream, pConfig->nBufHeight, NUMBITS_VOP_HEIGHT, (MS_S8*)("VOL_Height"));
222 		osPutBits(pStream, MARKER_BIT, 1, (MS_S8*)("Marker_Bit"));
223 //		m_statsVOL.nBitsStuffing ++;
224 //		m_statsVOL.nBitsHead += NUMBITS_VOP_WIDTH + NUMBITS_VOP_HEIGHT;
225 	}
226 	osPutBits(pStream, pInfo->bInterlace, 1, (MS_S8*)("VOL_interlace"));
227 //	m_statsVOL.nBitsHead++;
228 	osPutBits(pStream, 1/*m_volmd.bAdvPredDisable*/, 1, (MS_S8*)("VOL_OBMC_Disable"));
229 //	m_statsVOL.nBitsHead++;
230 
231 	// code sprite info
232 	osPutBits(pStream, 0/*m_uiSprite*/, 2, (MS_S8*)("VOL_Sprite_Usage"));
233 //	m_statsVOL.nBitsHead += 2;
234 
235 	// NBIT
236 	osPutBits(pStream, (MS_S32) 0/*m_volmd.bNot8Bit*/, 1, (MS_S8*)("VOL_NOT_8_BIT_VIDEO"));
237 //	m_statsVOL.nBitsHead++;
238 
239 	osPutBits(pStream, (MS_S32) pInfo->bQuantizerType/*m_volmd.fQuantizer*/, 1, (MS_S8*)("VOL_Quant_Type"));
240 //	m_statsVOL.nBitsHead++;
241 	if (pInfo->bQuantizerType/*m_volmd.fQuantizer*/==1) {
242 		osPutBits(pStream, 0/*m_volmd.bLoadIntraMatrix*/, 1, (MS_S8*)("VOL_Load_Q_Matrix (intra)"));
243 //		m_statsVOL.nBitsHead++;
244 		osPutBits(pStream, 0/*m_volmd.bLoadInterMatrix*/, 1, (MS_S8*)("VOL_Load_Q_Matrix (inter)"));
245 //		m_statsVOL.nBitsHead++;
246 	}
247 
248 	osPutBits(pStream, 0/*m_volmd.bQuarterSample*/, 1, (MS_S8*)("VOL_Quarter_Sample"));
249 //	m_statsVOL.nBitsHead += 1;
250 
251 	// START: Complexity Estimation syntax support - Marc Mongenet (EPFL) - 16 Jun 1998
252 	osPutBits(pStream, 1/*m_volmd.bComplexityEstimationDisable*/, 1, (MS_S8*)("complexity_estimation_disable"));
253 //	m_statsVOL.nBitsHead += 1;
254 
255 	osPutBits(pStream, (rcCtx->m_nVPMbRow<=0&&rcCtx->m_nVPSize<=0)?1:0/*m_volmd.bResyncMarkerDisable*/ , 1, (MS_S8*)("VOL_resync_marker_disable"));
256 //	m_statsVOL.nBitsHead ++;
257 
258 	osPutBits(pStream, 0/*m_volmd.bDataPartitioning*/, 1, (MS_S8*)("VOL_data_partitioning"));
259 // 	m_statsVOL.nBitsHead ++;
260 // 	if( m_volmd.bDataPartitioning )
261 // 	{
262 // 		osPutBits(pStream, m_volmd.bReversibleVlc, 1, (MS_S8*)("VOL_reversible_vlc"));
263 // 		m_statsVOL.nBitsHead ++;
264 // 	}
265 
266 	osPutBits(pStream, 0/*m_volmd.bNewpredEnable*/, NUMBITS_NEWPRED_ENABLE, (MS_S8*)("*VOL_newpred_enable"));
267 //	m_statsVOL.nBitsHead += NUMBITS_NEWPRED_ENABLE;
268 	osPutBits(pStream, 0/*m_volmd.breduced_resolution_vop_enable*/, 1, (MS_S8*)("reduced_resolution_vop_enable"));
269 //	m_statsVOL.nBitsHead ++;
270 
271 	osPutBits(pStream, 0/*m_volmd.volType == ENHN_LAYER*/, 1, (MS_S8*)("VOL_Scalability"));
272 //	m_statsVOL.nBitsHead++;
273 
274 	osNextStartCode(pStream);
275 }
276 
codeVOPHeadInitial(MFE_CONFIG * pConfig,OutStream * pStream)277 static void codeVOPHeadInitial(MFE_CONFIG *pConfig, OutStream* pStream)
278 {
279 	MS_S32 tCurrSec;
280 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
281 
282 	// Start code
283 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, (MS_S8*)("VOP_Start_Code"));
284 	osPutBits(pStream, VOP_START_CODE, NUMBITS_VOP_START_CODE, (MS_S8*)("VOP_Start_Code"));
285 //	m_statsVOP.nBitsHead += NUMBITS_START_CODE_PREFIX + NUMBITS_VOP_START_CODE;
286 
287 	// prediction type
288 	osPutBits(pStream, pConfig->vopPredType==I_VOP?0:(pConfig->vopPredType==P_VOP?1:2), NUMBITS_VOP_PRED_TYPE, (MS_S8*)("VOP_Pred_Type"));
289 //	m_statsVOP.nBitsHead += NUMBITS_VOP_PRED_TYPE;
290 
291 	// Time reference
292 	tCurrSec = (MS_S32)MFE_DIV_S64((MS_S64)pInfo->m_t * pInfo->nFixedIncrement, pInfo->nTimeResolution) ;
293 	//tCurrSec = (MS_S32)((INT64)pInfo->m_t * pInfo->nFixedIncrement / pInfo->nTimeResolution);// current time in seconds
294 
295 //      printk("\nm_t = %d,nFixedIncrement = %d nTimeResolution=%d\n",pInfo->m_t ,pInfo->nFixedIncrement,pInfo->nTimeResolution);
296 //      printk("\n tCurrSec = %d,pInfo->m_tModuloBaseDecd = %d\n",tCurrSec,pInfo->m_tModuloBaseDecd);
297 
298 	pInfo->m_nBitsModuloBase = tCurrSec - ((pConfig->vopPredType != B_VOP) ?
299 			pInfo->m_tModuloBaseDecd : pInfo->m_tModuloBaseDisp);
300 	MS_ASSERT(pInfo->m_nBitsModuloBase<=31);
301 
302 	osPutBits(pStream, (MS_S32) 0xFFFFFFFE & (0xFFFFFFFF>>(32-(pInfo->m_nBitsModuloBase+1))), pInfo->m_nBitsModuloBase + 1, (MS_S8*)("VOP_Modulo_Time_Base"));
303 //	m_statsVOP.nBitsHead += m_nBitsModuloBase + 1;
304 
305 	pInfo->m_iVopTimeIncr = (MS_S32)(MFE_DIV_S64((MS_S64)pInfo->m_t * pInfo->iClockRate * pInfo->nFixedIncrement, pInfo->nTimeResolution)
306 	       - (MS_S64)tCurrSec * pInfo->iClockRate) ;
307 	//pInfo->m_iVopTimeIncr = (MS_S32)((INT64)pInfo->m_t * pInfo->iClockRate * pInfo->nFixedIncrement / pInfo->nTimeResolution- (INT64)tCurrSec * pInfo->iClockRate);
308 
309 	//printf("codeVOPHeadInitial: m_t=%d, iClockRate=%d, nFixedIncrement=%d, nTimeResolution=%d, tCurrSec=%d\n",
310 	//	pInfo->m_t, pInfo->iClockRate, pInfo->nFixedIncrement, pInfo->nTimeResolution, tCurrSec);
311 
312 	osPutBits(pStream, (MS_S32) 1, 1, (MS_S8*)("Marker")); // marker bit	Added for error resilient mode by Toshiba(1997-11-14)
313 //	m_statsVOP.nBitsStuffing ++;
314 
315 	if(pInfo->nNumBitsTimeIncr!=0)
316 		osPutBits(pStream, pInfo->m_iVopTimeIncr, pInfo->nNumBitsTimeIncr, (MS_S8*)("VOP_Time_Incr"));
317 //	m_statsVOP.nBitsHead += m_iNumBitsTimeIncr;// Modified for error resilient mode by Toshiba(1997-11-14)
318 
319 	osPutBits(pStream, (MS_S32) 1, 1, (MS_S8*)("Marker")); // marker bit
320 //	m_statsVOP.nBitsStuffing ++;
321 
322 	if ( pConfig->vopPredType != B_VOP)
323 	{	//update modulo time base
324 		pInfo->m_tModuloBaseDisp = pInfo->m_tModuloBaseDecd;			//of the most recently displayed I/Pvop
325 		pInfo->m_tModuloBaseDecd = tCurrSec;							//of the most recently decoded I/Pvop
326 	}
327 
328 // 	if (m_tFrameInterval == 0 && pInfo->bInterlace && pInfo->vopPredType == B_VOP) {
329 // 		m_tFrameInterval = pInfo->m_t - pInfo->m_tPastRef;
330 // 	}
331 }
332 
codeNonCodedVOPHead(MFE_CONFIG * pConfig,OutStream * pStream)333 void codeNonCodedVOPHead (MFE_CONFIG *pConfig, OutStream* pStream)
334 {
335 	codeVOPHeadInitial(pConfig, pStream);
336 
337 	osPutBits(pStream, 0, 1, (MS_S8*)("VOP_Coded"));
338 //	m_statsVOP.nBitsHead++;
339 }
340 
341 
codeVOPHead(MFE_CONFIG * pConfig,OutStream * pStream)342 void codeVOPHead (MFE_CONFIG *pConfig, OutStream* pStream)
343 {
344 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
345 
346 	codeVOPHeadInitial(pConfig, pStream);
347 
348 	osPutBits(pStream, 1, 1, (MS_S8*)("VOP_Coded"));
349 //	m_statsVOP.nBitsHead++;
350 
351 	if (pConfig->vopPredType == P_VOP) {
352 		osPutBits(pStream, pInfo->iRoundingControl, 1, (MS_S8*)("VOP_Rounding_Type"));
353 //		m_statsVOP.nBitsHead++;
354 	}
355 
356 	osPutBits(pStream, 0/*m_vopmd.iIntraDcSwitchThr*/, 3, (MS_S8*)("IntraDCSwitchThr"));
357 //	m_statsVOP.nBitsHead+=3;
358 
359 // INTERLACE_
360 	if (pInfo->bInterlace) {
361 		osPutBits(pStream, 1/*m_vopmd.bTopFieldFirst*/, 1, (MS_S8*)("Top_Field_First"));
362 		osPutBits(pStream, 0/*m_vopmd.bAlternateScan*/, 1, (MS_S8*)("Alternate_Scan"));
363 //		m_statsVOP.nBitsHead += 2;
364 	}
365 
366 // INTERLACE
367 
368 	if (pConfig->vopPredType == I_VOP) {
369 		osPutBits(pStream, pInfo->intQP, 5/*m_volmd.uiQuantPrecision*/, (MS_S8*)("VOP_QUANT"));
370 //		m_statsVOP.nBitsHead += m_volmd.uiQuantPrecision;
371 	}
372 	else if (pConfig->vopPredType == P_VOP) {
373 		osPutBits(pStream, pInfo->intQP, 5/*m_volmd.uiQuantPrecision*/, (MS_S8*)("VOP_QUANT"));
374 //		m_statsVOP.nBitsHead += m_volmd.uiQuantPrecision;
375 		osPutBits(pStream, pInfo->iFCode/*m_vopmd.mvInfoForward.uiFCode*/, NUMBITS_VOP_FCODE, (MS_S8*)("VOP_Fcode_Forward"));
376 //		m_statsVOP.nBitsHead += NUMBITS_VOP_FCODE;
377 	}
378 	else if (pConfig->vopPredType == B_VOP) {
379 		osPutBits(pStream, pInfo->intQP, 5/*m_volmd.uiQuantPrecision*/, (MS_S8*)("VOP_QUANT"));
380 //		m_statsVOP.nBitsHead += m_volmd.uiQuantPrecision;
381 		osPutBits(pStream, pInfo->iFCode/*m_vopmd.mvInfoForward.uiFCode*/, NUMBITS_VOP_FCODE, (MS_S8*)("VOP_Fcode_Forward"));
382 //		m_statsVOP.nBitsHead += NUMBITS_VOP_FCODE;
383 		osPutBits(pStream, pInfo->iFCode/*m_vopmd.mvInfoBackward.uiFCode*/, NUMBITS_VOP_FCODE, (MS_S8*)("VOP_Fcode_Backward"));
384 //		m_statsVOP.nBitsHead += NUMBITS_VOP_FCODE;
385 	}
386 }
387 
388 //#define FORCE_30_CLOCK
codeVOPShortHead(MFE_CONFIG * pConfig,OutStream * pStream)389 void codeVOPShortHead(MFE_CONFIG *pConfig, OutStream* pStream)
390 {
391 	MS_S32 nSourceFormat;
392 	MS_S32 iTemporalRef;
393 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
394 
395     osPutBits(pStream, SHORT_VIDEO_START_MARKER, NUMBITS_SHORT_HEADER_START_CODE, (MS_S8*)("VOP_SH_Start_Code"));
396 #ifdef FORCE_30_CLOCK
397 	{
398 		MS_S32 tmp=1;
399 		if(m_t<=1)
400 			tmp=0;
401 		iTemporalRef = (MS_S32)MFE_DIV_S64((MS_S64)pInfo->m_t *(15+tmp) *  pInfo->nFixedIncrement, pInfo->nTimeResolution) -tmp;
402 		//iTemporalRef = (MS_S32)((INT64)pInfo->m_t * (15+tmp) *  pInfo->nFixedIncrement / pInfo->nTimeResolution) -tmp;
403 	}
404 #else
405 		iTemporalRef = (MS_S32)MFE_DIV_S64((MS_S64)pInfo->m_t * pInfo->iClockRate *  pInfo->nFixedIncrement, pInfo->nTimeResolution);
406 		//iTemporalRef = (MS_S32)((INT64)pInfo->m_t * pInfo->iClockRate *  pInfo->nFixedIncrement / pInfo->nTimeResolution);
407 
408 #endif
409     osPutBits(pStream, iTemporalRef&0xff, 8, (MS_S8*)("temporal_reference"));
410     osPutBits(pStream, 1, 1, (MS_S8*)("marker_bit"));
411     osPutBits(pStream, 0, 1, (MS_S8*)("zero_bit"));
412     osPutBits(pStream, 0, 1, (MS_S8*)("split_screen_indicator"));
413     osPutBits(pStream, 0, 1, (MS_S8*)("document_camera_indicator"));
414     osPutBits(pStream, 0, 1, (MS_S8*)("full_picture_freeze_release"));
415 //    m_statsVOP.nBitsHead += NUMBITS_SHORT_HEADER_START_CODE+8+5;
416 
417     if (pConfig->nBufWidth == 128 && pConfig->nBufHeight == 96) {
418 		nSourceFormat = 1;
419     } else if (pConfig->nBufWidth == 176 && pConfig->nBufHeight == 144) {
420 		nSourceFormat = 2;
421     } else if (pConfig->nBufWidth == 352 && pConfig->nBufHeight == 288) {
422 		nSourceFormat = 3;
423     } else if (pConfig->nBufWidth == 704 && pConfig->nBufHeight == 576) {
424 		nSourceFormat = 4;
425     } else if (pConfig->nBufWidth == 1408 && pConfig->nBufHeight == 1152) {
426 		nSourceFormat = 5;
427     } else {
428 		nSourceFormat = 7;	// 111: Extended PTYPE
429     }
430 	osPutBits(pStream, nSourceFormat, 3, (MS_S8*)("source_format"));
431 //    m_statsVOP.nBitsHead += 3;
432 
433 	if (nSourceFormat<=5) {
434 		osPutBits(pStream, pConfig->vopPredType==P_VOP, 1, (MS_S8*)("picture_coding_type"));
435 		osPutBits(pStream, 0, 4, (MS_S8*)("four_reserved_zero_bits"));
436 //		m_statsVOP.nBitsHead += 5;
437 	}
438 	else {	// PLUSTYPE
439 		MS_S32 UFEP, OPPTYPE, MPPTYPE, CPFMT;
440 		// Update Full Extended PTYPE (UFEP) (3 bits)
441 		if (pConfig->vopPredType==I_VOP)
442 			UFEP = 1;	// 001
443 		else
444 			UFEP = 0;	// 000
445 		osPutBits(pStream, UFEP, 3, (MS_S8*)("UFEP"));
446 //		m_statsVOP.nBitsHead += 3;
447 
448 		// The Optional Part of PLUSPTYPE (OPPTYPE) (18 bits)
449 		if (UFEP==1) {
450 			OPPTYPE = (6<<15)	// Source Format: "110" custom source format
451 				| (0 <<14)	// Optional Custom PCF
452 				| (0 <<13)	// Optional Unrestricted Motion Vector (UMV) mode (see Annex D)
453 				| (0 <<12)	// Optional Syntax-based Arithmetic Coding (SAC) mode (see Annex E)
454 				| (0 <<11)	// Optional Advanced Prediction (AP) mode (see Annex F)
455 				| (0 <<10)	// Optional Advanced INTRA Coding (AIC) mode (see Annex I)
456 				| (0 << 9)	// Optional Deblocking Filter (DF) mode (see Annex J)
457 				| (0 << 8)	// Optional Slice Structured (SS) mode (see Annex K)
458 				| (0 << 7)	// Optional Reference Picture Selection (RPS) mode (see Annex N)
459 				| (0 << 6)	// Optional Independent Segment Decoding (ISD) mode (see Annex R)
460 				| (0 << 5)	// Optional Alternative INTER VLC (AIV) mode (see Annex S)
461 				| (0 << 4)	// Optional Modified Quantization (MQ) mode (see Annex T)
462 				| (1 << 3);	// Equal to "1" to prevent start code emulation
463 			osPutBits(pStream, OPPTYPE, 18, (MS_S8*)("OPPTYPE"));
464 //			m_statsVOP.nBitsHead += 18;
465 		}
466 
467 		// The mandatory part of PLUSPTYPE when PLUSPTYPE present (MPPTYPE) (9 bits)
468 		MPPTYPE = ((pConfig->vopPredType==I_VOP?0:1)<<6)
469 			| (0 << 5)		// Optional Reference Picture Resampling (RPR) mode (see Annex P)
470 			| (0 << 4)		// Optional Reduced-Resolution Update (RRU) mode (see Annex Q)
471 			| (0 << 3)		// Rounding Type (RTYPE)
472 			| (1     );		// Equal to "1" to prevent start code emulation
473 		osPutBits(pStream, MPPTYPE, 9, (MS_S8*)("MPPTYPE"));
474 //		m_statsVOP.nBitsHead += 9;
475 
476 		// CPM=0, PSBI=N/A
477 		osPutBits(pStream, 0, 1, (MS_S8*)("CPM=0, PSBI=N/A"));
478 //		m_statsVOP.nBitsHead += 1;
479 
480 		if (UFEP==1) {
481 			// Custom Picture Format (CPFMT) (23 bits)
482 			CPFMT = (2 << 19)	// Pixel Aspect Ratio Code. 2: 12:11 (CIF for 4:3 picture)
483 				| ((pConfig->nBufWidth/4-1) << 10)	// Picture Width Indication: Range [0, ... , 511]; Number of pixels per line = (PWI + 1) * 4
484 				| (1 << 9)					// Equal to "1" to prevent start code emulation
485 				| (pConfig->nBufHeight/4);			// Picture Height Indication: Range [1, ... , 288]; Number of lines = PHI * 4
486 			osPutBits(pStream, CPFMT, 23, (MS_S8*)("CPFMT"));
487 //			m_statsVOP.nBitsHead += 23;
488 		}
489 	}
490 
491     osPutBits(pStream, pInfo->intQP, 5, (MS_S8*)("vop_quant"));
492 //    m_statsVOP.nBitsHead += 5;
493 	if (nSourceFormat<=5) {
494 		osPutBits(pStream, 0, 1, (MS_S8*)("zero_bit"));
495 		osPutBits(pStream, 0, 1, (MS_S8*)("pei"));
496 //		m_statsVOP.nBitsHead += 2;
497 	}
498 	else {
499 		osPutBits(pStream, 0, 1, (MS_S8*)("pei"));
500 //		m_statsVOP.nBitsHead += 1;
501 	}
502 
503 	// Set GOB info
504 	pInfo->m_iGobFrameId = (pConfig->vopPredType==P_VOP ? 1 : 0);//dec_count&0x3;
505 }
506 
codeNonCodedVOPShortHead(MFE_CONFIG * pConfig,OutStream * pStream)507 void codeNonCodedVOPShortHead(MFE_CONFIG *pConfig, OutStream* pStream)
508 {
509 	MS_S32 i, nTotalMBs;
510 
511 	codeVOPShortHead(pConfig, pStream);
512 
513 	// Every MB is skipped MB
514 	nTotalMBs = (pConfig->nBufWidth*pConfig->nBufHeight)>>8;
515 	for (i=0; i<nTotalMBs; i++)
516 		osPutBits(pStream, 1, 1, (MS_S8*)("not_coded"));
517 }
518