xref: /utopia/UTPA2-700.0.x/modules/mfe/drv/mfe/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_common.h"
83 #include "mdrv_mfe_math.h"
84 #include "ms_dprintf.h"
85 
86 
87 
88 #include "m4v_header.h"
89 #include "msRateCtrl.h"
90 
91 
92 
93 #define MARKER_BIT						1
94 
95 #define START_CODE_PREFIX				1
96 #define NUMBITS_START_CODE_PREFIX		24
97 //	Added for data partitioning mode By Toshiba(1998-1-16:DP+RVLC)
98 #define NUMBITS_START_CODE_SUFFIX		8
99 //	End Toshiba(1998-1-16:DP+RVLC)
100 
101 // session overhead information
102 #define USER_DATA_START_CODE			0xB2
103 #define VSS_START_CODE					0xB0	// 8-bit
104 #define VSS_END_CODE					0xB1	// 8-bit
105 #define NUMBITS_VSS_PROFILE				8
106 #define VSO_START_CODE					0xB5	// 8-bit
107 #define VSO_VERID						1
108 #define VSO_TYPE						1
109 #define NUMBITS_VSO_VERID				4
110 #define NUMBITS_VSO_PRIORITY			3
111 #define NUMBITS_VSO_TYPE				4
112 
113 // VO overhead information
114 #define NUMBITS_VO_START_CODE			3
115 #define VO_START_CODE					0
116 #define NUMBITS_VO_ID					5
117 
118 #define NUMBITS_NEWPRED_ENABLE						1
119 
120 // VOL overhead information
121 #define NUMBITS_SHORT_HEADER_START_CODE 22 // Added by KPN for short headers (1998-02-07, DS)
122 #define SHORT_VIDEO_START_MARKER        32 // Added by KPN for short headers (1998-02-07, DS)
123 #define NUMBITS_VOL_START_CODE			4
124 #define VOL_START_CODE					2
125 #define NUMBITS_VOL_ID					4
126 #define NUMBITS_VOL_SHAPE				2
127 #define NUMBITS_TIME_RESOLUTION			16
128 #define NUMBITS_VOL_FCODE				3
129 #define NUMBITS_SEP_MOTION_TEXTURE		1
130 #define NUMBITS_QMATRIX					8
131 
132 // VOP overhead information
133 #define VOP_START_CODE					0xB6
134 #define NUMBITS_VOP_START_CODE			8
135 #define NUMBITS_VOP_TIMEINCR			10
136 #define NUMBITS_VOP_HORIZONTAL_SPA_REF	13
137 #define NUMBITS_VOP_VERTICAL_SPA_REF	13
138 #define NUMBITS_VOP_WIDTH				13
139 #define NUMBITS_VOP_HEIGHT				13
140 #define NUMBITS_VOP_PRED_TYPE			2
141 #define NUMBITS_VOP_QUANTIZER			5
142 #define NUMBITS_VOP_ALPHA_QUANTIZER		6
143 #define NUMBITS_VOP_FCODE				3
144 
145 
146 
codeSequenceHead(MFE_CONFIG * pConfig,OutStream * pStream)147 void codeSequenceHead (MFE_CONFIG *pConfig, OutStream* pStream)
148 {
149 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, "VSS_Start_Code");
150 	osPutBits(pStream, VSS_START_CODE, NUMBITS_START_CODE_SUFFIX, "VSS_Start_Code");
151 	osPutBits(pStream, 0xF5/*m_volmd.uiProfileAndLevel*/, NUMBITS_VSS_PROFILE, "VSS_Profile_Level");
152 
153 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, "VSO_Start_Code");
154 	osPutBits(pStream, VSO_START_CODE, NUMBITS_START_CODE_SUFFIX, "VSO_Start_Code");
155 	osPutBits(pStream, 0, 1, "VSO_IsVisualObjectIdentifier");
156 	osPutBits(pStream, VSO_TYPE, NUMBITS_VSO_TYPE, "VSO_Type");
157 	osPutBits(pStream, 0, 1, "VSO_VideoSignalType");
158 
159 // 	UInt uiNumBits = NUMBITS_START_CODE_PREFIX + NUMBITS_START_CODE_SUFFIX + NUMBITS_VSS_PROFILE
160 // 		+ NUMBITS_START_CODE_PREFIX + 1 + NUMBITS_VSO_TYPE + 1;
161 //	m_statsVOL.nBitsHead += uiNumBits;
162 
163 //	m_statsVOL.nBitsStuffing += m_pbitstrmOut->flush ();
164 	osNextStartCode(pStream);
165 }
166 
codeVOHead(MFE_CONFIG * pConfig,OutStream * pStream)167 void codeVOHead (MFE_CONFIG *pConfig, OutStream* pStream)
168 {
169 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, "VO_Start_Code");
170 	osPutBits(pStream, VO_START_CODE, NUMBITS_VO_START_CODE, "VO_Start_Code");		//plus 3 bits
171 	osPutBits(pStream, /*m_uiVOId*/1, NUMBITS_VO_ID, "VO_Id");
172 // 	U32 uiNumBits = NUMBITS_START_CODE_PREFIX + NUMBITS_VO_START_CODE + NUMBITS_VO_ID;
173 // 	m_statsVOL.nBitsHead += uiNumBits;
174 }
175 
codeVOLHead(MFE_CONFIG * pConfig,OutStream * pStream)176 void codeVOLHead (MFE_CONFIG *pConfig, OutStream* pStream)
177 {
178 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
179 	CVBRRateControl* rcCtx = &pConfig->ctxRateControl;
180 
181 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, "VOL_Start_Code_Prefix");
182 	osPutBits(pStream, VOL_START_CODE, NUMBITS_VOL_START_CODE, "VOL_Start_Code");		//plus 4 bits
183 	osPutBits(pStream, 0, NUMBITS_VOL_ID, "VOL_Id"); // by katata
184 //	m_statsVOL.nBitsHead+=NUMBITS_START_CODE_PREFIX+NUMBITS_VOL_START_CODE+NUMBITS_VOL_ID;
185 
186 	osPutBits(pStream, (int) 0, 1, "VOL_Random_Access");		//isn't this a system level flg?
187 //	m_statsVOL.nBitsHead++;
188 
189 	osPutBits(pStream, (int) 1, 8, "VOL_Type_Indicator"); // Set to indicate SIMPLE profile.
190 //	m_statsVOL.nBitsHead+=8;
191 
192 	osPutBits(pStream, (int) 1, 1, "VOL_Is_Object_Layer_Identifier");
193 	osPutBits(pStream, (int) 2/*m_volmd.uiVerID*/, 4, "VOL_Verid");
194 	osPutBits(pStream, (int) 1, 3, "VOL_Priority");
195 //	m_statsVOL.nBitsHead+=8;
196 
197 	osPutBits(pStream, (int) 1, 4, "aspect_ratio_info"); // square pix
198 //	m_statsVOL.nBitsHead+=4;
199 
200 	osPutBits(pStream, /*m_volmd.uiVolControlParameters*/0, 1, "VOL_Control_Parameters"); //useless flag for now
201 //	m_statsVOL.nBitsHead++;
202 
203 	{
204 		osPutBits(pStream, /*iAUsage*/0, NUMBITS_VOL_SHAPE, "VOL_Shape_Type");
205 		osPutBits(pStream, 1, 1, "Marker");
206 //		m_statsVOL.nBitsStuffing ++;
207 
208 		osPutBits(pStream, pInfo->nTimeResolution/*m_volmd.iClockRate*/, NUMBITS_TIME_RESOLUTION, "VOL_Time_Increment_Resolution");
209 		osPutBits(pStream, 1, 1, "Marker");
210 //		m_statsVOL.nBitsStuffing ++;
211 
212 		osPutBits(pStream, 0, 1, "VOL_Fixed_Vop_Rate");
213 //		m_statsVOL.nBitsHead += NUMBITS_VOL_SHAPE + NUMBITS_TIME_RESOLUTION + 1;
214 
215 		osPutBits(pStream, MARKER_BIT, 1, "Marker_Bit");
216 //		m_statsVOL.nBitsStuffing ++;
217 		osPutBits(pStream, pConfig->nBufWidth, NUMBITS_VOP_WIDTH, "VOL_Width");
218 		osPutBits(pStream, MARKER_BIT, 1, "Marker_Bit");
219 //		m_statsVOL.nBitsStuffing ++;
220 		osPutBits(pStream, pConfig->nBufHeight, NUMBITS_VOP_HEIGHT, "VOL_Height");
221 		osPutBits(pStream, MARKER_BIT, 1, "Marker_Bit");
222 //		m_statsVOL.nBitsStuffing ++;
223 //		m_statsVOL.nBitsHead += NUMBITS_VOP_WIDTH + NUMBITS_VOP_HEIGHT;
224 	}
225 	osPutBits(pStream, pInfo->bInterlace, 1, "VOL_interlace");
226 //	m_statsVOL.nBitsHead++;
227 	osPutBits(pStream, 1/*m_volmd.bAdvPredDisable*/, 1, "VOL_OBMC_Disable");
228 //	m_statsVOL.nBitsHead++;
229 
230 	// code sprite info
231 	osPutBits(pStream, 0/*m_uiSprite*/, 2, "VOL_Sprite_Usage");
232 //	m_statsVOL.nBitsHead += 2;
233 
234 	// NBIT
235 	osPutBits(pStream, (int) 0/*m_volmd.bNot8Bit*/, 1, "VOL_NOT_8_BIT_VIDEO");
236 //	m_statsVOL.nBitsHead++;
237 
238 	osPutBits(pStream, (int) pInfo->bQuantizerType/*m_volmd.fQuantizer*/, 1, "VOL_Quant_Type");
239 //	m_statsVOL.nBitsHead++;
240 	if (pInfo->bQuantizerType/*m_volmd.fQuantizer*/==1) {
241 		osPutBits(pStream, 0/*m_volmd.bLoadIntraMatrix*/, 1, "VOL_Load_Q_Matrix (intra)");
242 //		m_statsVOL.nBitsHead++;
243 		osPutBits(pStream, 0/*m_volmd.bLoadInterMatrix*/, 1, "VOL_Load_Q_Matrix (inter)");
244 //		m_statsVOL.nBitsHead++;
245 	}
246 
247 	osPutBits(pStream, 0/*m_volmd.bQuarterSample*/, 1, "VOL_Quarter_Sample");
248 //	m_statsVOL.nBitsHead += 1;
249 
250 	// START: Complexity Estimation syntax support - Marc Mongenet (EPFL) - 16 Jun 1998
251 	osPutBits(pStream, 1/*m_volmd.bComplexityEstimationDisable*/, 1, "complexity_estimation_disable");
252 //	m_statsVOL.nBitsHead += 1;
253 
254 	osPutBits(pStream, (rcCtx->m_nVPMbRow<=0&&rcCtx->m_nVPSize<=0)?1:0/*m_volmd.bResyncMarkerDisable*/ , 1, "VOL_resync_marker_disable");
255 //	m_statsVOL.nBitsHead ++;
256 
257 	osPutBits(pStream, 0/*m_volmd.bDataPartitioning*/, 1, "VOL_data_partitioning");
258 // 	m_statsVOL.nBitsHead ++;
259 // 	if( m_volmd.bDataPartitioning )
260 // 	{
261 // 		osPutBits(pStream, m_volmd.bReversibleVlc, 1, "VOL_reversible_vlc");
262 // 		m_statsVOL.nBitsHead ++;
263 // 	}
264 
265 	osPutBits(pStream, 0/*m_volmd.bNewpredEnable*/, NUMBITS_NEWPRED_ENABLE, "*VOL_newpred_enable");
266 //	m_statsVOL.nBitsHead += NUMBITS_NEWPRED_ENABLE;
267 	osPutBits(pStream, 0/*m_volmd.breduced_resolution_vop_enable*/, 1, "reduced_resolution_vop_enable");
268 //	m_statsVOL.nBitsHead ++;
269 
270 	osPutBits(pStream, 0/*m_volmd.volType == ENHN_LAYER*/, 1, "VOL_Scalability");
271 //	m_statsVOL.nBitsHead++;
272 
273 	osNextStartCode(pStream);
274 }
275 
codeVOPHeadInitial(MFE_CONFIG * pConfig,OutStream * pStream)276 static void codeVOPHeadInitial(MFE_CONFIG *pConfig, OutStream* pStream)
277 {
278 	int tCurrSec;
279 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
280 
281 	// Start code
282 	osPutBits(pStream, START_CODE_PREFIX, NUMBITS_START_CODE_PREFIX, "VOP_Start_Code");
283 	osPutBits(pStream, VOP_START_CODE, NUMBITS_VOP_START_CODE, "VOP_Start_Code");
284 //	m_statsVOP.nBitsHead += NUMBITS_START_CODE_PREFIX + NUMBITS_VOP_START_CODE;
285 
286 	// prediction type
287 	osPutBits(pStream, pConfig->vopPredType==I_VOP?0:(pConfig->vopPredType==P_VOP?1:2), NUMBITS_VOP_PRED_TYPE, "VOP_Pred_Type");
288 //	m_statsVOP.nBitsHead += NUMBITS_VOP_PRED_TYPE;
289 
290 	// Time reference
291 	tCurrSec = (int)MFE_DIV_S64((MFE_S64)pInfo->m_t * pInfo->nFixedIncrement, pInfo->nTimeResolution) ;
292 	//tCurrSec = (int)((INT64)pInfo->m_t * pInfo->nFixedIncrement / pInfo->nTimeResolution);// current time in seconds
293 
294 //      printk("\nm_t = %d,nFixedIncrement = %d nTimeResolution=%d\n",pInfo->m_t ,pInfo->nFixedIncrement,pInfo->nTimeResolution);
295 //      printk("\n tCurrSec = %d,pInfo->m_tModuloBaseDecd = %d\n",tCurrSec,pInfo->m_tModuloBaseDecd);
296 
297 	pInfo->m_nBitsModuloBase = tCurrSec - ((pConfig->vopPredType != B_VOP) ?
298 			pInfo->m_tModuloBaseDecd : pInfo->m_tModuloBaseDisp);
299 	MFE_ASSERT(pInfo->m_nBitsModuloBase<=31);
300 
301 	osPutBits(pStream, (int) 0xFFFFFFFE & (0xFFFFFFFF>>(32-(pInfo->m_nBitsModuloBase+1))), pInfo->m_nBitsModuloBase + 1, "VOP_Modulo_Time_Base");
302 //	m_statsVOP.nBitsHead += m_nBitsModuloBase + 1;
303 
304 	pInfo->m_iVopTimeIncr = (int)(MFE_DIV_S64((MFE_S64)pInfo->m_t * pInfo->iClockRate * pInfo->nFixedIncrement, pInfo->nTimeResolution)
305 	       - (MFE_S64)tCurrSec * pInfo->iClockRate) ;
306 	//pInfo->m_iVopTimeIncr = (int)((INT64)pInfo->m_t * pInfo->iClockRate * pInfo->nFixedIncrement / pInfo->nTimeResolution- (INT64)tCurrSec * pInfo->iClockRate);
307 
308 	//printf("codeVOPHeadInitial: m_t=%d, iClockRate=%d, nFixedIncrement=%d, nTimeResolution=%d, tCurrSec=%d\n",
309 	//	pInfo->m_t, pInfo->iClockRate, pInfo->nFixedIncrement, pInfo->nTimeResolution, tCurrSec);
310 
311 	osPutBits(pStream, (int) 1, 1, "Marker"); // marker bit	Added for error resilient mode by Toshiba(1997-11-14)
312 //	m_statsVOP.nBitsStuffing ++;
313 
314 	if(pInfo->nNumBitsTimeIncr!=0)
315 		osPutBits(pStream, pInfo->m_iVopTimeIncr, pInfo->nNumBitsTimeIncr, "VOP_Time_Incr");
316 //	m_statsVOP.nBitsHead += m_iNumBitsTimeIncr;// Modified for error resilient mode by Toshiba(1997-11-14)
317 
318 	osPutBits(pStream, (int) 1, 1, "Marker"); // marker bit
319 //	m_statsVOP.nBitsStuffing ++;
320 
321 	if ( pConfig->vopPredType != B_VOP)
322 	{	//update modulo time base
323 		pInfo->m_tModuloBaseDisp = pInfo->m_tModuloBaseDecd;			//of the most recently displayed I/Pvop
324 		pInfo->m_tModuloBaseDecd = tCurrSec;							//of the most recently decoded I/Pvop
325 	}
326 
327 // 	if (m_tFrameInterval == 0 && pInfo->bInterlace && pInfo->vopPredType == B_VOP) {
328 // 		m_tFrameInterval = pInfo->m_t - pInfo->m_tPastRef;
329 // 	}
330 }
331 
codeNonCodedVOPHead(MFE_CONFIG * pConfig,OutStream * pStream)332 void codeNonCodedVOPHead (MFE_CONFIG *pConfig, OutStream* pStream)
333 {
334 	codeVOPHeadInitial(pConfig, pStream);
335 
336 	osPutBits(pStream, 0, 1, "VOP_Coded");
337 //	m_statsVOP.nBitsHead++;
338 }
339 
340 
codeVOPHead(MFE_CONFIG * pConfig,OutStream * pStream)341 void codeVOPHead (MFE_CONFIG *pConfig, OutStream* pStream)
342 {
343 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
344 
345 	codeVOPHeadInitial(pConfig, pStream);
346 
347 	osPutBits(pStream, 1, 1, "VOP_Coded");
348 //	m_statsVOP.nBitsHead++;
349 
350 	if (pConfig->vopPredType == P_VOP) {
351 		osPutBits(pStream, pInfo->iRoundingControl, 1, "VOP_Rounding_Type");
352 //		m_statsVOP.nBitsHead++;
353 	}
354 
355 	osPutBits(pStream, 0/*m_vopmd.iIntraDcSwitchThr*/, 3, "IntraDCSwitchThr");
356 //	m_statsVOP.nBitsHead+=3;
357 
358 // INTERLACE_
359 	if (pInfo->bInterlace == 1) {
360 		osPutBits(pStream, 1/*m_vopmd.bTopFieldFirst*/, 1, "Top_Field_First");
361 		osPutBits(pStream, 0/*m_vopmd.bAlternateScan*/, 1, "Alternate_Scan");
362 //		m_statsVOP.nBitsHead += 2;
363 	}
364 
365 // INTERLACE
366 
367 	if (pConfig->vopPredType == I_VOP) {
368 		osPutBits(pStream, pInfo->intStep, 5/*m_volmd.uiQuantPrecision*/, "VOP_QUANT");
369 //		m_statsVOP.nBitsHead += m_volmd.uiQuantPrecision;
370 	}
371 	else if (pConfig->vopPredType == P_VOP) {
372 		osPutBits(pStream, pInfo->intStep, 5/*m_volmd.uiQuantPrecision*/, "VOP_QUANT");
373 //		m_statsVOP.nBitsHead += m_volmd.uiQuantPrecision;
374 		osPutBits(pStream, pInfo->iFCode/*m_vopmd.mvInfoForward.uiFCode*/, NUMBITS_VOP_FCODE, "VOP_Fcode_Forward");
375 //		m_statsVOP.nBitsHead += NUMBITS_VOP_FCODE;
376 	}
377 	else if (pConfig->vopPredType == B_VOP) {
378 		osPutBits(pStream, pInfo->intStep, 5/*m_volmd.uiQuantPrecision*/, "VOP_QUANT");
379 //		m_statsVOP.nBitsHead += m_volmd.uiQuantPrecision;
380 		osPutBits(pStream, pInfo->iFCode/*m_vopmd.mvInfoForward.uiFCode*/, NUMBITS_VOP_FCODE, "VOP_Fcode_Forward");
381 //		m_statsVOP.nBitsHead += NUMBITS_VOP_FCODE;
382 		osPutBits(pStream, pInfo->iFCode/*m_vopmd.mvInfoBackward.uiFCode*/, NUMBITS_VOP_FCODE, "VOP_Fcode_Backward");
383 //		m_statsVOP.nBitsHead += NUMBITS_VOP_FCODE;
384 	}
385 }
386 
387 //#define FORCE_30_CLOCK
codeVOPShortHead(MFE_CONFIG * pConfig,OutStream * pStream)388 void codeVOPShortHead(MFE_CONFIG *pConfig, OutStream* pStream)
389 {
390 	int nSourceFormat;
391 	int iTemporalRef;
392 	M4VEINFO *pInfo = &pConfig->ctxM4veInfo;
393 
394     osPutBits(pStream, SHORT_VIDEO_START_MARKER, NUMBITS_SHORT_HEADER_START_CODE, "VOP_SH_Start_Code");
395 #ifdef FORCE_30_CLOCK
396 	{
397 		int tmp=1;
398 		if(m_t<=1)
399 			tmp=0;
400 		iTemporalRef = (int)MFE_DIV_S64((MFE_S64)pInfo->m_t *(15+tmp) *  pInfo->nFixedIncrement, pInfo->nTimeResolution) -tmp;
401 		//iTemporalRef = (int)((INT64)pInfo->m_t * (15+tmp) *  pInfo->nFixedIncrement / pInfo->nTimeResolution) -tmp;
402 	}
403 #else
404 		iTemporalRef = (int)MFE_DIV_S64((MFE_S64)pInfo->m_t * pInfo->iClockRate *  pInfo->nFixedIncrement, pInfo->nTimeResolution);
405 		//iTemporalRef = (int)((INT64)pInfo->m_t * pInfo->iClockRate *  pInfo->nFixedIncrement / pInfo->nTimeResolution);
406 
407 #endif
408     osPutBits(pStream, iTemporalRef&0xff, 8, "temporal_reference");
409     osPutBits(pStream, 1, 1, "marker_bit");
410     osPutBits(pStream, 0, 1, "zero_bit");
411     osPutBits(pStream, 0, 1, "split_screen_indicator");
412     osPutBits(pStream, 0, 1, "document_camera_indicator");
413     osPutBits(pStream, 0, 1, "full_picture_freeze_release");
414 //    m_statsVOP.nBitsHead += NUMBITS_SHORT_HEADER_START_CODE+8+5;
415 
416     if (pConfig->nBufWidth == 128 && pConfig->nBufHeight == 96) {
417 		nSourceFormat = 1;
418     } else if (pConfig->nBufWidth == 176 && pConfig->nBufHeight == 144) {
419 		nSourceFormat = 2;
420     } else if (pConfig->nBufWidth == 352 && pConfig->nBufHeight == 288) {
421 		nSourceFormat = 3;
422     } else if (pConfig->nBufWidth == 704 && pConfig->nBufHeight == 576) {
423 		nSourceFormat = 4;
424     } else if (pConfig->nBufWidth == 1408 && pConfig->nBufHeight == 1152) {
425 		nSourceFormat = 5;
426     } else {
427 		nSourceFormat = 7;	// 111: Extended PTYPE
428     }
429 	osPutBits(pStream, nSourceFormat, 3, "source_format");
430 //    m_statsVOP.nBitsHead += 3;
431 
432 	if (nSourceFormat<=5) {
433 		osPutBits(pStream, pConfig->vopPredType==P_VOP, 1, "picture_coding_type");
434 		osPutBits(pStream, 0, 4, "four_reserved_zero_bits");
435 //		m_statsVOP.nBitsHead += 5;
436 	}
437 	else {	// PLUSTYPE
438 		int UFEP, OPPTYPE, MPPTYPE, CPFMT;
439 		// Update Full Extended PTYPE (UFEP) (3 bits)
440 		if (pConfig->vopPredType==I_VOP)
441 			UFEP = 1;	// 001
442 		else
443 			UFEP = 0;	// 000
444 		osPutBits(pStream, UFEP, 3, "UFEP");
445 //		m_statsVOP.nBitsHead += 3;
446 
447 		// The Optional Part of PLUSPTYPE (OPPTYPE) (18 bits)
448 		if (UFEP==1) {
449 			OPPTYPE = (6<<15)	// Source Format: "110" custom source format
450 				| (0 <<14)	// Optional Custom PCF
451 				| (0 <<13)	// Optional Unrestricted Motion Vector (UMV) mode (see Annex D)
452 				| (0 <<12)	// Optional Syntax-based Arithmetic Coding (SAC) mode (see Annex E)
453 				| (0 <<11)	// Optional Advanced Prediction (AP) mode (see Annex F)
454 				| (0 <<10)	// Optional Advanced INTRA Coding (AIC) mode (see Annex I)
455 				| (0 << 9)	// Optional Deblocking Filter (DF) mode (see Annex J)
456 				| (0 << 8)	// Optional Slice Structured (SS) mode (see Annex K)
457 				| (0 << 7)	// Optional Reference Picture Selection (RPS) mode (see Annex N)
458 				| (0 << 6)	// Optional Independent Segment Decoding (ISD) mode (see Annex R)
459 				| (0 << 5)	// Optional Alternative INTER VLC (AIV) mode (see Annex S)
460 				| (0 << 4)	// Optional Modified Quantization (MQ) mode (see Annex T)
461 				| (1 << 3);	// Equal to "1" to prevent start code emulation
462 			osPutBits(pStream, OPPTYPE, 18, "OPPTYPE");
463 //			m_statsVOP.nBitsHead += 18;
464 		}
465 
466 		// The mandatory part of PLUSPTYPE when PLUSPTYPE present (MPPTYPE) (9 bits)
467 		MPPTYPE = ((pConfig->vopPredType==I_VOP?0:1)<<6)
468 			| (0 << 5)		// Optional Reference Picture Resampling (RPR) mode (see Annex P)
469 			| (0 << 4)		// Optional Reduced-Resolution Update (RRU) mode (see Annex Q)
470 			| (0 << 3)		// Rounding Type (RTYPE)
471 			| (1     );		// Equal to "1" to prevent start code emulation
472 		osPutBits(pStream, MPPTYPE, 9, "MPPTYPE");
473 //		m_statsVOP.nBitsHead += 9;
474 
475 		// CPM=0, PSBI=N/A
476 		osPutBits(pStream, 0, 1, "CPM=0, PSBI=N/A");
477 //		m_statsVOP.nBitsHead += 1;
478 
479 		if (UFEP==1) {
480 			// Custom Picture Format (CPFMT) (23 bits)
481 			CPFMT = (2 << 19)	// Pixel Aspect Ratio Code. 2: 12:11 (CIF for 4:3 picture)
482 				| ((pConfig->nBufWidth/4-1) << 10)	// Picture Width Indication: Range [0, ... , 511]; Number of pixels per line = (PWI + 1) * 4
483 				| (1 << 9)					// Equal to "1" to prevent start code emulation
484 				| (pConfig->nBufHeight/4);			// Picture Height Indication: Range [1, ... , 288]; Number of lines = PHI * 4
485 			osPutBits(pStream, CPFMT, 23, "CPFMT");
486 //			m_statsVOP.nBitsHead += 23;
487 		}
488 	}
489 
490     osPutBits(pStream, pInfo->intStep, 5, "vop_quant");
491 //    m_statsVOP.nBitsHead += 5;
492 	if (nSourceFormat<=5) {
493 		osPutBits(pStream, 0, 1, "zero_bit");
494 		osPutBits(pStream, 0, 1, "pei");
495 //		m_statsVOP.nBitsHead += 2;
496 	}
497 	else {
498 		osPutBits(pStream, 0, 1, "pei");
499 //		m_statsVOP.nBitsHead += 1;
500 	}
501 
502 	// Set GOB info
503 	pInfo->m_iGobFrameId = (pConfig->vopPredType==P_VOP ? 1 : 0);//dec_count&0x3;
504 }
505 
codeNonCodedVOPShortHead(MFE_CONFIG * pConfig,OutStream * pStream)506 void codeNonCodedVOPShortHead(MFE_CONFIG *pConfig, OutStream* pStream)
507 {
508 	int i, nTotalMBs;
509 
510 	codeVOPShortHead(pConfig, pStream);
511 
512 	// Every MB is skipped MB
513 	nTotalMBs = (pConfig->nBufWidth*pConfig->nBufHeight)>>8;
514 	for (i=0; i<nTotalMBs; i++)
515 		osPutBits(pStream, 1, 1, "not_coded");
516 }
517