xref: /utopia/UTPA2-700.0.x/modules/mfe/api/mfe_ex/madp_mfe.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 // Copyright (c) 2006-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // (uMStar Confidential Informationv) by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 #ifndef __UBOOT__
96 #include <stdio.h>
97 #include <string.h>
98 #include <fcntl.h>
99 #endif
100 
101 #include "MFE_chip.h"
102 #if defined(_MFE_UTOPIA_)
103 #include "MsCommon.h"
104 #include "MsIRQ.h"
105 #include "MsOS.h"
106 #include "drvMMIO.h"
107 #include "drvMIU.h"
108 #endif
109 #include "mfe_type.h"
110 #include "mfe_common.h"
111 #include "madp_ms_dprintf.h"
112 
113 #include "mdrv_mfe_io.h"
114 #include "drv_mfe_st.h"
115 #include "madp_mfe.h"
116 #include "apiVOIPMFE.h"
117 #if defined(_SUPPORT_JPE_)
118 #include "Exif.h"
119 #include "jcmarker.h"
120 #endif
121 
122 #if defined(__MFE_G2__)
123 #include "riumap.h"
124 #endif
125 
126 #include "madp_mfe_int.h"
127 
128 #include "apiMFE_priv.h"
129 
130 
131 
132 #define CEILING_ALIGN(value, align) (((MS_U32)(value)+((align)-1UL)) & ~((align)-1UL))
133 
134 #if defined(_MFE_UTOPIA_)
135 extern void MDrv_MFE_ISR(InterruptNum eIntNum);
136 #endif
137 
138 #define ALIGN_TO_256(f)           (((f) + 255UL) & ~255UL)
139 #define mfe_fd 0UL
140 
141 PVR_Info* appPVR_Info;
142 
MAdp_MFE_Change_State(MFE_STATE * mfe_state,MFE_STATE in_state)143 static MS_BOOL MAdp_MFE_Change_State(MFE_STATE* mfe_state,MFE_STATE in_state)
144 {
145 
146     *mfe_state = in_state;
147 
148     return TRUE;
149 }
150 
my_malloc(MS_VIRT * ppVAStart,MS_U32 size,const MS_S8 * msg)151 MS_U8 *my_malloc(MS_VIRT* ppVAStart,MS_U32 size,const MS_S8* msg)
152 {
153     MS_U8 *ret = (MS_U8 *)*ppVAStart;
154     ms_dprintf(ADP_L2, "my_malloc[%s]: VA_START: %p, SIZE: 0x%x\n", msg, ret, (unsigned int)size);
155 
156 #ifdef MEM_CHECK
157             ms_dprintf(ADP_L2,"my_malloc:Check mem set.\n");
158             memset(ret, 0xFF, size);
159             sleep(100000);
160 #endif
161 
162     *ppVAStart += size;
163     return ret;
164 }
165 
166 
MAdp_MFE_Proc_nonThread(MEMMAP_CUR_t input_frame)167 static MS_BOOL MAdp_MFE_Proc_nonThread(MEMMAP_CUR_t input_frame)
168 {
169 
170     MS_U32 nDispOrder=0;    //     Driver is responsible for frame buffer re-ordering
171     MS_VIRT Vaddr;
172     MS_S32 Vsize=0;
173     BITSFRAME_INFO bitsinfo;
174 #if defined(_SUPPORT_JPE_)
175     MS_S32 fdc_size = 0;
176 #endif
177     memset(&bitsinfo,0x0,sizeof(BITSFRAME_INFO));
178 #ifdef _MFE_M1_
179     //get last frame DropFlag.
180     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_DROPFLAG,  (MS_VIRT)(&appPVR_Info->nDropFlag));
181 
182     //get current frame SkipFlag
183     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_SKIPFLAG,  (MS_VIRT)(&appPVR_Info->nSkipFlag));
184 #endif
185     //if(appPVR_Info->nDropFlag==0)
186     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_DISPORDER, (MS_VIRT)&nDispOrder);
187 #if defined(_MFE_UTOPIA_)
188     if(appPVR_Info->enableISR) {
189         mfe_open_int();
190         MsOS_EnableInterrupt((InterruptNum)E_INT_IRQ_MFE);
191     }
192 #endif
193     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_ENC_ONEFRAME, (MS_VIRT)&input_frame);
194 
195     if(appPVR_Info->nSkipFlag==1) {
196         ms_dprintf(ADP_L3,"SkipFrame\n");
197 #ifndef _GenSkipHeader_
198         return TRUE;
199 #endif
200     }
201     Vsize = 0;
202 #if defined(_SUPPORT_JPE_)
203     if(appPVR_Info->nCodecType == JPEG_ENC)  {//jpeg
204         //FDC_t fdc;
205         MS_S32 q_table[128];
206         mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_QTABLE, (MS_VIRT)&q_table);
207 //        set_fdc(appPVR_Info->nDispWidth,appPVR_Info->nDispHeight);
208         get_fdc(appPVR_Info->BitstreamOutBuffer.miuPointer,&fdc_size,appPVR_Info->nDispWidth,appPVR_Info->nDispHeight,
209             &q_table[0],&q_table[64],appPVR_Info->exif_enable);
210 
211         ms_dprintf(ADP_L3,"header size = 0x%x\n",fdc_size);
212         //memcpy(appPVR_Info->BitstreamOutBuffer.miuPointer,&fdc,fdc_size);
213         Vsize = fdc_size;
214     }
215 #endif
216 #ifdef _SW_BUF_MODE_
217     if(appPVR_Info->m_bFrameMode == 0) {
218          ms_dprintf(ADP_L3,"fake rowdone\n");
219         mfe_fake_row_mode(input_frame,appPVR_Info->nBufWidth,appPVR_Info->nBufHeight);
220     }
221 #endif
222 //    MsOS_DelayTask(20); //delay 2 ms
223     //get bits
224 
225     while(1) {
226         MFE_CONFIG* pConfig = (MFE_CONFIG *)appPVR_Info->mfe_drvHandle;
227         OutStream* pStream = &(pConfig->m_OutStream);
228 
229         if (pConfig->encodeDummyFrame)
230         {
231             bitsinfo.size = pStream->m_nByteCount;
232             bitsinfo.status = BITS_INFO_FRAMEDONE;
233             bitsinfo.voptype = P_VOP;
234             Vaddr = (MS_VIRT)pStream->m_pbFrameBuffer;
235         }
236         else
237         {
238 #ifdef _GenSkipHeader_
239             if (appPVR_Info->nSkipFlag == 0)
240 #endif
241             {
242                 mfe_wait_int(appPVR_Info->enableISR);
243             }
244 
245             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GETBITS, (MS_VIRT)(&bitsinfo));
246             MS_ASSERT(bitsinfo.size >= 0);
247             if(appPVR_Info->enableSecurityMode){
248                 Vsize = bitsinfo.size;
249                 goto NONCOPY;
250             }
251             if (appPVR_Info->reserved0) // cached memory
252                 Vaddr  = (MS_VIRT) MAdp_MPool_PA2KSEG0(bitsinfo.miuAddress+appPVR_Info->MfeApiInfo.miu_offset);
253             else // non-cached memory
254                 Vaddr  = (MS_VIRT) MAdp_MPool_PA2KSEG1(bitsinfo.miuAddress+appPVR_Info->MfeApiInfo.miu_offset);
255         }
256 
257         ms_dprintf(ADP_L3, "bitsinfo.miuAddress = 0x%x, Vaddr = 0x"MPRI_VIRT", size = 0x%x\n", (unsigned int)bitsinfo.miuAddress, (ptrdiff_t)Vaddr, (unsigned int)bitsinfo.size);
258         if((appPVR_Info->BitstreamOutBufferSize-Vsize) < bitsinfo.size) {
259             ms_dprintf(ADP_L0,"BitstreamOutBufferSize is too small\n");
260             return FALSE;
261         }
262         if (appPVR_Info->reserved0) { // cached memory
263            MsOS_Dcache_Flush(Vaddr, CEILING_ALIGN(bitsinfo.size, 16UL));
264         }
265         memcpy(appPVR_Info->BitstreamOutBuffer.miuPointer +Vsize, (MS_U8 *)Vaddr, bitsinfo.size);
266         Vsize += bitsinfo.size;
267 
268         ms_dprintf(ADP_L3, "memcpy 0x%x, total 0x%x\n", (unsigned int)bitsinfo.size, (unsigned int)Vsize);
269         if(bitsinfo.status == BITS_INFO_FRAMEDONE) {//frame done
270             if(appPVR_Info->enableCABAC) {
271                 //CABAC stuffing byte
272                 if(bitsinfo.stuffing_bytes!=0) {
273                     MS_S32 i;
274                     MS_U8 *obufp = appPVR_Info->BitstreamOutBuffer.miuPointer + Vsize;
275 
276                     ms_dprintf(ADP_L3, "CABAC stuffing byte 0x%x\n", (unsigned int)bitsinfo.stuffing_bytes);
277                     for(i=0;i<bitsinfo.stuffing_bytes/3;i++) {
278                         *obufp++ = 0x0;
279                         *obufp++ = 0x0;
280                         *obufp++ = 0x3;
281                     }
282                     for(i=0;i<bitsinfo.stuffing_bytes%3;i++) {
283                         *obufp++ = 0x0;
284                     }
285 
286                 }
287                 Vsize += bitsinfo.stuffing_bytes;
288             }
289             ms_dprintf(ADP_L3,"frame done\n");
290             break;
291         }
292         else if(bitsinfo.status == BITS_INFO_BUFFULL) {
293             ms_dprintf(ADP_L3,"frame full\n");
294             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_CLEAR_BITSBUF, (MS_VIRT)1);
295         }
296         else if (bitsinfo.status == BITS_INFO_IMG_BUFFULL){
297             ms_dprintf(ADP_L0,"input buf full\n");
298             return FALSE;
299         } else {
300             ms_dprintf(ADP_L0,"[MFE]unknown state\n");
301             return FALSE;
302         }
303     }
304 
305 NONCOPY:
306     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_CLEAR_BITSBUF, (MS_VIRT)1);
307     appPVR_Info->BitstreamOutBuffer.size = Vsize;
308     if(Vsize)
309         appPVR_Info->g_StreamCallBackFunc(appPVR_Info->g_pCustomCtx, (MS_S32)bitsinfo.voptype,
310             appPVR_Info->BitstreamOutBuffer.miuPointer, Vsize, 0x3);
311 
312     appPVR_Info->g_CleanCallBackFunc(1, (MS_VIRT)&input_frame);
313 
314     ms_dprintf(ADP_L2, "frame size = 0x%x\n", (unsigned int)appPVR_Info->BitstreamOutBuffer.size);
315 
316 #if (DEBUG_LEVEL & ADP_L3)
317     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_DROPFLAG,  (MS_VIRT)(&appPVR_Info->nDropFlag));
318     if(appPVR_Info->nDropFlag)
319         ms_dprintf(ADP_L3,"Dropframe.");
320 #endif
321     return TRUE;
322 }
323 
MFE_SetRIU_Base(void)324 MS_BOOL MFE_SetRIU_Base(void)
325 {
326 
327 #ifdef __MFE_G2__
328     MS_VIRT u32RiuBaseAdd=0;
329     MsIoMapInit();
330     u32RiuBaseAdd = MsIoMapGetRiuBase(RIUBASE_MFE0);
331 
332     ms_dprintf(ADP_L2,"MFE Init u32RiuBaseAdd = 0x%X\n", (MS_U32)u32RiuBaseAdd);
333 #elif defined(WIN32)
334     MS_VIRT u32RiuBaseAdd=0xa80;
335     MS_PHY u32NonPMBankSize=0;
336 #else
337     MS_VIRT u32RiuBaseAdd=0;
338     MS_PHY u32NonPMBankSize=0;
339 
340     if( !MDrv_MMIO_GetBASE( &u32RiuBaseAdd, &u32NonPMBankSize, MS_MODULE_HW)){
341         ms_dprintf(ADP_L0,"MFE Init Err: MMIO_GetBASE failure\n");
342         return FALSE;
343     }
344 
345     ms_dprintf(ADP_L2, "MFE Init u32RiuBaseAdd = 0x%tx, u32NonPMBankSize = 0x%tx\n",
346         (ptrdiff_t)u32RiuBaseAdd, (ptrdiff_t)u32NonPMBankSize);
347     //MDrv_MFE_SetOSRegBase(u32RiuBaseAdd);
348 #endif
349      mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_RIU_BASE, u32RiuBaseAdd);
350 
351     return TRUE;
352 
353 }
354 
355 
MApi_MFE_CompressOnePicture_ex(MEMMAP_CUR_t YUVPlane,MS_BOOL bForceIframe)356 MS_BOOL MApi_MFE_CompressOnePicture_ex(MEMMAP_CUR_t YUVPlane, MS_BOOL bForceIframe)
357 {
358     return _MApi_MFE_CompressOnePicture(appPVR_Info, YUVPlane, bForceIframe);
359 }
360 
MApi_MFE_Encode(void)361 MS_BOOL MApi_MFE_Encode(void)
362 {
363     return _MApi_MFE_Encode(appPVR_Info);
364 }
365 
MApi_MFE_DeInitialize(void)366 MS_BOOL MApi_MFE_DeInitialize(void)
367 {
368 
369     // old api didnot support multi instance, so always power off mfe when deInit.
370     POWERON_t power_Info;
371     power_Info.is_off = 1;
372     power_Info.clock_level = MFE_CLK_FAST;
373     mfe_ioctl((MS_VIRT)NULL, MFE_IOC_POWEROFF, (MS_VIRT)&power_Info);
374     return _MApi_MFE_DeInit(appPVR_Info);
375 }
376 
377 
MApi_MFE_GetState(void)378 MFE_STATE MApi_MFE_GetState(void)
379 {
380     return appPVR_Info->mfe_state;
381 }
MApi_MFE_GetVOL_ex(void * header_info)382 MS_BOOL MApi_MFE_GetVOL_ex(void *header_info)
383 {
384     return _MApi_MFE_GetVOL(appPVR_Info,header_info);
385 }
386 
MApi_MFE_Initialize_ex(PVR_Info * in_Info,StreamCallback out_cb,CleanCallback mfe_CleanBufStatus,void * ctx)387 MS_BOOL MApi_MFE_Initialize_ex(PVR_Info* in_Info,StreamCallback out_cb,CleanCallback
388 mfe_CleanBufStatus, void *ctx)
389 {
390 
391     MS_BOOL ret;
392     ret = _MApi_MFE_Init(in_Info);
393     if(ret == FALSE)
394         return FALSE;
395     appPVR_Info->g_pCustomCtx = ctx;
396     appPVR_Info->g_StreamCallBackFunc = out_cb;
397     appPVR_Info->g_CleanCallBackFunc = mfe_CleanBufStatus;
398 
399     return ret;
400 
401 }
402 
403 
MApi_MFE_SetConfig(MFE_ENC_CONF conf_type,void * value)404 MS_BOOL MApi_MFE_SetConfig(MFE_ENC_CONF conf_type, void *value)
405 {
406 
407     MS_S32 set_value;
408     if(appPVR_Info->mfe_state != MFE_Idle) {
409         ms_dprintf(ADP_L0,"state check error.[%d]\n",appPVR_Info->mfe_state);
410         return FALSE;
411     }
412 
413     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Busy);
414 
415     switch (conf_type) {
416         case MFE_ENC_SETCONF_FRAME_TYPE:
417             set_value = (*((MS_S32*) value));
418             ms_dprintf(ADP_L1, "MApi_MFE_SetConfig %d\n", (int)set_value);
419             if(set_value == I_VOP)
420                 mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_FORCEDI, (MS_VIRT)NULL);
421             break;
422         case MFE_ENC_SETCONF_CHANGE_FRAME_RATE:
423             set_value = (*((MS_S32*) value));
424             if(set_value == 0) {
425                 ms_dprintf(ADP_L0,"framerate cannot be 0.\n");
426                 MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
427                 return FALSE;
428             }
429             appPVR_Info->FrameRatex100 = set_value;
430             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_RESET_FRAMERATE,  (MS_VIRT)(appPVR_Info));
431             break;
432         case MFE_ENC_SETCONF_CHANGE_BIT_RATE:
433             set_value = (*((MS_S32*) value));
434             if(set_value == 0) {
435                 ms_dprintf(ADP_L0,"bitrate cannot be 0.\n");
436                 MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
437                 return FALSE;
438             }
439             appPVR_Info->nBitrate =  set_value;
440             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_RESET_BITRATE,  (MS_VIRT)(appPVR_Info));
441             break;
442         case MFE_ENC_SETCONF_CHANGE_BIT_RATE_AND_FRAME_RATE:
443             {
444                 PVR_Info* in_Info = (PVR_Info*)value;
445                 appPVR_Info->nBitrate =  in_Info->nBitrate;
446                 appPVR_Info->FrameRatex100 =  in_Info->FrameRatex100;
447                 if (_MApi_MFE_SetBitrateFramerate(appPVR_Info) == FALSE) {
448                     MAdp_MFE_Change_State(&appPVR_Info->mfe_state, MFE_Idle);
449                     return FALSE;
450                 }
451 #if 0
452                 if(in_Info->nBitrate== 0 ) {
453                     ms_dprintf(ADP_L0,"bitrate cannot be 0.\n");
454                     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
455                     return FALSE;
456                 }
457                 if(in_Info->FrameRatex100 == 0) {
458                     ms_dprintf(ADP_L0,"framerate cannot be 0.\n");
459                     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
460                     return FALSE;
461                 }
462                 appPVR_Info->nBitrate =  in_Info->nBitrate;
463                 appPVR_Info->FrameRatex100 =  in_Info->FrameRatex100;
464                 mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_RESET_BITRATE_AND_FRAMERATE,  (MS_VIRT)(appPVR_Info));
465 #endif
466             }
467             break;
468         case MFE_ENC_SETCONF_FRAME_TAG:
469             appPVR_Info->out_frametag_top = (*((MS_S32*) value));
470             break;
471         case MFE_ENC_SETCONF_RESET_SPSPPS:
472             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_RESET_SPSPPS, (MS_VIRT)NULL);
473             break;
474         case MFE_ENC_SETCONF_OUT_BSP_BUFFER:
475         {
476             MEMMAP_t* outbuf = (MEMMAP_t *)value;
477             if(outbuf->miuPointer == NULL || outbuf->size == 0) {
478                 ms_dprintf(ADP_L0,"outbuf->miuPointer == NULL || outbuf->size == 0\n");
479                 MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
480                 return FALSE;
481             }
482             appPVR_Info->BitstreamOutBuffer.miuPointer = outbuf->miuPointer;
483             appPVR_Info->BitstreamOutBuffer.miuAddress = outbuf->miuAddress;
484             appPVR_Info->BitstreamOutBuffer.size = 0;
485             appPVR_Info->BitstreamOutBufferSize = outbuf->size;
486             break;
487         }
488         case MFE_ENC_SETCONF_JPE_QUALITY:
489             set_value = (*((MS_S32*) value));
490             if(set_value <0 || set_value > 100) {
491                 ms_dprintf(ADP_L0, "quality does not support %d\n", (int)set_value);
492                 MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
493                 return FALSE;
494             }
495             appPVR_Info->quality = (MS_S32)set_value;
496             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_QUALITY,  (MS_VIRT)(set_value));
497             break;
498         case MFE_ENC_SETCONF_SET_COLORFORMAT:
499 #if defined(_MFE_MUJI_) || defined(_MFE_MONET_) || defined(_MFE_MESSI_) || defined(_MFE_MANHATTAN_) || defined(_MFE_MASERATI_) || defined(_MFE_MAXIM_) || defined(_MFE_KANO_) || defined(_MFE_K6_)
500             set_value = (*((MS_S32*) value));
501             appPVR_Info->bColorFormat =  (MFE_COLOR_FORMAT_INFO)set_value;
502             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_COLOR_FORMAT,  (MS_VIRT)(appPVR_Info));
503             break;
504 #else
505             ms_dprintf(ADP_L0,"Not support MFE_ENC_SETCONF_SET_COLORFORMAT on this chip\n");
506             MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
507             return FALSE;
508 #endif
509         default:
510             ms_dprintf(ADP_L0,"conf_type error\n");
511             MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
512             return FALSE;
513     }
514 
515     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
516 
517     return TRUE;
518 }
519 
MApi_MFE_GetConfig(MFE_ENC_CONF conf_type,void * value)520 MS_BOOL MApi_MFE_GetConfig(MFE_ENC_CONF conf_type, void *value)
521 {
522     if (MFE_ENC_GETCONF_CONFIG == conf_type) {
523         MS_U32 u32ShmId = 0;
524         MS_U32 u32BufSize = 0;
525         MS_VIRT VAddr = 0;
526         PVR_Info* pShmConfig = NULL;
527         PVR_Info* pDstConfig = (PVR_Info*)value;
528 
529         if (!pDstConfig) {
530             ms_dprintf(ADP_L0, "Null pointer in get config.\n");
531             return FALSE;
532         }
533 
534         if (FALSE == MsOS_SHM_GetId((MS_U8*)("MFE driver"), sizeof(PVR_Info), &u32ShmId, &VAddr, &u32BufSize, MSOS_SHM_QUERY))  // Query if there is mfe driver exist in share memory
535         {
536             ms_dprintf(ADP_L0, "Failed to get share mem.\n");
537             return FALSE;
538         }
539 
540         pShmConfig = (PVR_Info*)VAddr;
541         if (MFE_ReInit != pShmConfig->mfe_state) {
542             ms_dprintf(ADP_L0, "MFE not initialized.\n");
543             return FALSE;
544         }
545 
546         memcpy(pDstConfig, pShmConfig, sizeof(PVR_Info));
547         return TRUE;
548     }
549 
550     if(appPVR_Info->mfe_state != MFE_Idle) {
551         ms_dprintf(ADP_L0,"state check error.[%d]\n",appPVR_Info->mfe_state);
552         return FALSE;
553     }
554 
555     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Busy);
556 
557     switch (conf_type) {
558         case MFE_ENC_GETCONF_FRAME_TAG:
559             (*((MS_U32*) value)) = appPVR_Info->out_frametag_top;
560             break;
561         case MFE_ENC_GETCONF_OUT_BSP_BUFFER:
562         {
563             //memcpy(value,&appPVR_Info->BitstreamOutBuffer,sizeof(MEMMAP_t));
564             _MApi_MFE_GetOBuf(appPVR_Info,(MEMMAP_t*)value);
565             ms_dprintf(ADP_L2, "appPVR_Info->BitstreamOutBuffer.size = 0x%x\n", (unsigned int)appPVR_Info->BitstreamOutBuffer.size);
566             break;
567         }
568         case MFE_ENC_GETCONF_JPE_QUALITY:
569             (*((MS_S32*) value)) = appPVR_Info->quality;
570             break;
571         case MFE_ENC_GETCONF_DROP:
572             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_DROPFLAG,  (MS_VIRT)(&appPVR_Info->nDropFlag));
573             *((MS_U32 *)value) = appPVR_Info->nDropFlag;
574             break;
575         default:
576             ms_dprintf(ADP_L0,"conf_type error\n");
577             MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
578             return FALSE;
579     }
580 
581     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
582 
583     return TRUE;
584 }
585 
MApi_MFE_GetHWCap(MFE_HwCapV0 * pDrv,MS_U32 * len)586 MS_BOOL MApi_MFE_GetHWCap(MFE_HwCapV0 *pDrv, MS_U32 *len)
587 {
588     return _MApi_MFE_GetHWCap(appPVR_Info, pDrv, len);
589 }
590 
_MApi_MFE_Init(PVR_Info * mfe_Info)591 MS_BOOL _MApi_MFE_Init(PVR_Info* mfe_Info)
592 {
593     ms_dprintf(ADP_L1,"_MApi_MFE_Init\n");
594     MS_U32 nFrameSize;
595     MS_U32 MallocToDrvSize ;
596     MS_U32 MallocToDrvFrame;
597     appPVR_Info = mfe_Info;
598 
599     if (appPVR_Info->mfe_state != MFE_Invalid
600         && appPVR_Info->mfe_state != MFE_DeInit
601         && appPVR_Info->mfe_state != MFE_ReInit) {
602         ms_dprintf(ADP_L0, "Incorrect state for initialization.\n");
603         return FALSE;
604     }
605 
606     //parameter check
607     if((appPVR_Info->nCodecType != MPEG4_ENC) && (appPVR_Info->nCodecType != H263_ENC)
608         && (appPVR_Info->nCodecType != H264_ENC) && (appPVR_Info->nCodecType != JPEG_ENC )){
609         ms_dprintf(ADP_L0,"nCodecType error\n");
610         return FALSE;
611     }
612 
613     //video parameter
614     if(appPVR_Info->nCodecType != JPEG_ENC){
615         if(appPVR_Info->FrameRatex100/100 <= 0) {
616                 ms_dprintf(ADP_L0, "FrameRatex100 cannot be %d\n", (int)appPVR_Info->FrameRatex100);
617                 return FALSE;
618         }
619         if(appPVR_Info->nBbetweenP != 0) {
620                 ms_dprintf(ADP_L0, "nBbetweenP cannot be %d\n", (int)appPVR_Info->nBbetweenP);
621                 return FALSE;
622         }
623         //if(appPVR_Info->nBitrate <= 0) {
624         //    ms_dprintf(ADP_L0,"nBitrate cant be %d\n",appPVR_Info->nBitrate);
625         //    return FALSE;
626         //}
627         if(appPVR_Info->width==0 ||appPVR_Info->height == 0) {
628             ms_dprintf(ADP_L0,"appPVR_Info->width==0 ||appPVR_Info->height == 0\n");
629             return FALSE;
630         }
631 #if defined(MFE_SUPPORT_1080P)
632         if(appPVR_Info->height > 1088) {
633             ms_dprintf(ADP_L0,"appPVR_Info->height > 1088\n");
634             return FALSE;
635         }
636         if(appPVR_Info->width > 1920) {
637             ms_dprintf(ADP_L0,"appPVR_Info->width > 1920\n");
638             return FALSE;
639         }
640 #else
641         if(appPVR_Info->height > 720) {
642             ms_dprintf(ADP_L0,"appPVR_Info->height > 720\n");
643             return FALSE;
644         }
645         if(appPVR_Info->width > 1280) {
646             ms_dprintf(ADP_L0,"appPVR_Info->width > 1280\n");
647             return FALSE;
648         }
649 #endif
650         if(appPVR_Info->nCodecType == MPEG4_ENC){
651             if(appPVR_Info->TimeIncreamentRes==0 || appPVR_Info->VopTimeIncreament == 0)
652                 return FALSE;
653         }
654         if(appPVR_Info->m_bFrameMode != 1) {
655             ms_dprintf(ADP_L0,"appPVR_Info->m_bFrameMode != 1\n");
656              return FALSE;
657         }
658     }
659     else{ //jpeg
660         if((appPVR_Info->quality < 1) || (appPVR_Info->quality > 100)) {
661             ms_dprintf(ADP_L0, "quality cannot be %d\n", (int)appPVR_Info->quality);
662             return FALSE;
663         }
664         if(appPVR_Info->height > 2592) {
665             ms_dprintf(ADP_L0, "height cannot be %d\n", (int)appPVR_Info->height);
666             return FALSE;
667         }
668         if(appPVR_Info->width > 2960) {
669             ms_dprintf(ADP_L0, "width cannot be %d\n", (int)appPVR_Info->width);
670             return FALSE;
671         }
672     }
673 
674     if(!appPVR_Info->enableSecurityMode && appPVR_Info->BitstreamOutBuffer.miuPointer == NULL) {
675         ms_dprintf(ADP_L0,"appPVR_Info->BitstreamOutBuffer.miuPointer == NULL\n");
676         return FALSE;
677     }
678     if(appPVR_Info->enableSecurityMode) {
679         ms_dprintf(ADP_L1,"enableSecurityMode\n");
680     }
681     if((appPVR_Info->MfeAdvInfo.input_imi_en!=0) && (appPVR_Info->MfeAdvInfo.low_bandwidth_en!=0)) {
682         ms_dprintf(ADP_L0, "MfeAdvInfo.input_imi_en = %u, MfeAdvInfo.low_bandwidth_en = %u\n",
683             (unsigned int)appPVR_Info->MfeAdvInfo.input_imi_en, (unsigned int)appPVR_Info->MfeAdvInfo.low_bandwidth_en);
684         return FALSE; //cannot enable at the same time.
685     }
686     if(appPVR_Info->bInterlace != PROGRESSIVE) {
687         if(appPVR_Info->nBufHeight % 32 != 0){
688             ms_dprintf(ADP_L0,"Height must %% 32 for interlace mode\n");
689             return FALSE;
690         }
691     }
692 
693 #if defined(_MFE_EDISON_) //eagle only support MSTTILE
694     if(appPVR_Info->bColorFormat == YUVTILE) {
695         ms_dprintf(ADP_L4,"Only support MSTTILE, force to MSTTILE.\n");
696         appPVR_Info->bColorFormat = MSTTILE;
697     }
698 #endif
699 
700 #if defined(MFE_YUV_LOADER)
701     if (appPVR_Info->bColorFormat > MFE_NV21)
702 #else
703     if (appPVR_Info->bColorFormat > MFE_VYUY)
704 #endif
705     {
706         ms_dprintf(ADP_L0, "Invalid input format.\n");
707         return FALSE;
708     }
709 
710     ms_dprintf(ADP_L2,"video parameter check done\n");
711     //set RIU BASE
712     if(MFE_SetRIU_Base() == FALSE) {
713         ms_dprintf(ADP_L0,"MFE_SetRIU_Base() == FALSE\n");
714         return FALSE;
715     }
716 
717     appPVR_Info->nDropFlag = 0;
718 
719     appPVR_Info->nOutBuffer = OBUF_NUM;
720     //appPVR_Info->OutBufferSize = OBUF_SIZE;
721     appPVR_Info->OutBufferSize = appPVR_Info->BitstreamOutBufferSize;
722 
723     if((appPVR_Info->nBufHeight*appPVR_Info->nBufWidth) >= (1280*720))
724         MallocToDrvFrame = MAX_BUFPOOL_COUNT -1; // Only 1 ref.
725     else
726         MallocToDrvFrame = MAX_BUFPOOL_COUNT;
727 
728     if(appPVR_Info->nCodecType == H264_ENC) {
729         MS_U32 out_buf_size = appPVR_Info->OutBufferSize*appPVR_Info->nOutBuffer;
730         if(appPVR_Info->bColorFormat == YUVTILE) {
731             nFrameSize = appPVR_Info->nBufHeight*appPVR_Info->nBufWidth*3/2;
732             if(appPVR_Info->bInterlace != PROGRESSIVE)
733                 nFrameSize = (appPVR_Info->nBufHeight>>1)*appPVR_Info->nBufWidth*3/2;
734         } else { // MSTTILE or YUV422
735             MS_S32 DstSizeY = CEILING_ALIGN(appPVR_Info->nBufHeight,32)*appPVR_Info->nBufWidth;
736             MS_S32 DstSizeUV = CEILING_ALIGN(appPVR_Info->nBufHeight,64)*appPVR_Info->nBufWidth/2;
737             nFrameSize = DstSizeY + DstSizeUV;
738             if(appPVR_Info->bInterlace != PROGRESSIVE) {
739                 MS_S32 DstSizeY = CEILING_ALIGN(appPVR_Info->nBufHeight/2,32)*appPVR_Info->nBufWidth;
740                 MS_S32 DstSizeUV = CEILING_ALIGN(appPVR_Info->nBufHeight/2,64)*appPVR_Info->nBufWidth/2;
741                 nFrameSize = DstSizeY + DstSizeUV;
742             }
743         }
744 #if defined(_MFE_MUJI_) || defined(_MFE_MONET_) || defined(_MFE_MESSI_) || defined(_MFE_MANHATTAN_) || defined(_MFE_MASERATI_) || defined(_MFE_MAXIM_) || defined(_MFE_KANO_) || defined(_MFE_K6_)
745         MallocToDrvSize = nFrameSize * MallocToDrvFrame
746                         + out_buf_size
747                         + (appPVR_Info->nBufWidth >> 4) * 128 + 10 * 1024;
748 #else
749         MallocToDrvSize = nFrameSize * MallocToDrvFrame
750                         + out_buf_size
751                         + (appPVR_Info->nBufWidth >> 4) * 64 + 10 * 1024;
752 #endif
753     }
754     else if(appPVR_Info->nCodecType == H263_ENC || appPVR_Info->nCodecType == MPEG4_ENC) {
755         nFrameSize = appPVR_Info->nBufHeight*appPVR_Info->nBufWidth*3/2;
756         MallocToDrvSize = nFrameSize*MallocToDrvFrame+appPVR_Info->OutBufferSize*appPVR_Info->nOutBuffer +
757              (appPVR_Info->nBufWidth>>4)*64 +
758              appPVR_Info->nBufHeight*appPVR_Info->nBufWidth/256*16 + 10*1024;
759     }
760     else {
761         appPVR_Info->MfeApiInfo.mfe_phy_buf_size = appPVR_Info->MfeApiInfo.mfe_phy_buf_size -
762             (ALIGN_TO_256(appPVR_Info->MfeApiInfo.mfe_phy_buf_addr) -appPVR_Info->MfeApiInfo.mfe_phy_buf_addr);
763         appPVR_Info->MfeApiInfo.mfe_phy_buf_addr = ALIGN_TO_256(appPVR_Info->MfeApiInfo.mfe_phy_buf_addr);
764 
765         appPVR_Info->nOutBuffer = OBUF_NUM;
766         appPVR_Info->OutBufferSize = MallocToDrvSize = appPVR_Info->MfeApiInfo.mfe_phy_buf_size;
767     }
768 
769     if(appPVR_Info->enableSecurityMode) {
770         MallocToDrvSize = MallocToDrvSize - (appPVR_Info->OutBufferSize*appPVR_Info->nOutBuffer);
771     }
772     ms_dprintf(ADP_L2, "mfe_phy_buf_size = 0x%x, MallocToDrvSize = 0x%x\n",
773         (unsigned int)appPVR_Info->MfeApiInfo.mfe_phy_buf_size, (unsigned int)MallocToDrvSize);
774     if(appPVR_Info->MfeApiInfo.mfe_phy_buf_size < MallocToDrvSize) {
775         ms_dprintf(ADP_L0,"appPVR_Info->MfeApiInfo.mfe_phy_buf_size < MallocToDrvSize\n");
776         return FALSE;
777     }
778     // malloc memory for DRV level
779     my_malloc(&appPVR_Info->MfeApiInfo.mfe_vir_buf_ptr, MallocToDrvSize, (const MS_S8*)("pretend to allocate for MFE driver"));
780 
781     //pretend to allocate for MFE
782     appPVR_Info->BitsBuffStart = appPVR_Info->MfeApiInfo.mfe_phy_buf_addr;
783     appPVR_Info->BitsBuffSize = MallocToDrvSize;
784     appPVR_Info->BitsBuffStart -= (appPVR_Info->BitsBuffStart & appPVR_Info->MfeApiInfo.miu_offset);
785 
786     appPVR_Info->MfeApiInfo.Encode_stop = 0;
787     appPVR_Info->mfe_drvHandle = (void*)MfeDrvMemMalloc(sizeof(MFE_CONFIG), (const MS_S8*)"mfe_drvHandle");
788     if ( mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_INIT, (MS_VIRT)(appPVR_Info)) != 0)
789         return FALSE;
790 
791     if(!appPVR_Info->enableSecurityMode)
792         mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_MIU_OFFSET, (MS_VIRT)appPVR_Info->MfeApiInfo.miu_offset);
793 
794 #if 0 // Not needed here. Open interrupt at CompressOnePicture instead.
795     if(appPVR_Info->enableISR) {
796         ms_dprintf(ADP_L1,"MFE open interrupt\n");
797         mfe_open_int();
798     }
799 #endif
800 #if defined(_SUPPORT_JPE_)
801     if(appPVR_Info->nCodecType == JPEG_ENC) {
802         init_fdc();
803         set_fdc(appPVR_Info->nDispWidth,appPVR_Info->nDispHeight);
804     }
805 #endif
806 
807     // Keep a copy of init parameters in share mem
808     do {
809         MS_U32 u32ShmId = 0;
810         MS_U32 u32BufSize = 0;
811         MS_VIRT VAddr = 0;
812         PVR_Info* pShmConfig = NULL;
813         void* tmpHandle = appPVR_Info->mfe_drvHandle;
814 
815         if (FALSE == MsOS_SHM_GetId((MS_U8*)("MFE driver"), sizeof(PVR_Info), &u32ShmId, &VAddr, &u32BufSize, MSOS_SHM_QUERY))  // Query if there is mfe driver exist in share memory
816         {
817             if (FALSE == MsOS_SHM_GetId((MS_U8*)("MFE driver"), sizeof(PVR_Info), &u32ShmId, &VAddr, &u32BufSize, MSOS_SHM_CREATE)) // Create mfe driver in share memory
818             {
819                 ms_dprintf(ADP_L0, "Failed to create share mem.\n");
820                 break;
821             }
822         }
823 
824         pShmConfig = (PVR_Info*)VAddr;
825         appPVR_Info->mfe_drvHandle = NULL;   // Do not put internal pointer in share mem
826         appPVR_Info->mfe_state = MFE_ReInit; // share mem copy of configuration is for re-init
827         memcpy(pShmConfig, appPVR_Info, sizeof(PVR_Info));
828 
829         appPVR_Info->mfe_drvHandle = tmpHandle;
830     } while (0);
831 
832     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Init);
833 
834     return TRUE;
835 
836 }
_MApi_MFE_Encode(PVR_Info * mfe_Info)837 MS_BOOL _MApi_MFE_Encode(PVR_Info* mfe_Info)
838 {
839     appPVR_Info = mfe_Info;
840     ms_dprintf(ADP_L1,"_MApi_MFE_Encode\n");
841     if(appPVR_Info->mfe_state == MFE_Init)
842         MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Busy);
843     else {
844         ms_dprintf(ADP_L0,"state check error.[%d]\n",appPVR_Info->mfe_state);
845         return FALSE;
846     }
847     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
848 
849     return TRUE;
850 }
_MApi_MFE_GetVOL(PVR_Info * mfe_Info,void * header_info)851 MS_BOOL _MApi_MFE_GetVOL(PVR_Info* mfe_Info, void *header_info)
852 {
853     ms_dprintf(ADP_L1,"_MApi_MFE_GetVOL\n");
854     appPVR_Info = mfe_Info;
855     if(appPVR_Info->mfe_state != MFE_Idle) {
856         ms_dprintf(ADP_L0,"state check error.[%d]\n",appPVR_Info->mfe_state);
857         return FALSE;
858     }
859 
860 
861     if(header_info == NULL) {
862         ms_dprintf(ADP_L0,"header_info == NULL\n");
863         return FALSE;
864     }
865 
866 
867     //MPEG4
868     if(appPVR_Info->nCodecType == MPEG4_ENC) {
869         VOL_INFO_t *vol_info = header_info;
870         vol_info->pHeader = appPVR_Info->BitstreamOutBuffer.miuPointer;
871         mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_VOL, (MS_VIRT)vol_info);
872     }
873     else if(appPVR_Info->nCodecType == H264_ENC) { // H264
874         SPSPPS_INFO_t *spspps_info = header_info;
875         spspps_info->pHeaderSPS = appPVR_Info->BitstreamOutBuffer.miuPointer;
876         mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_VOL, (MS_VIRT)spspps_info);
877         appPVR_Info->BitstreamOutBuffer.size = spspps_info->PPSLen + spspps_info->SPSLen;
878     }
879     else {
880         ms_dprintf(ADP_L0, "non-support codec type %d\n", (int)appPVR_Info->nCodecType);
881         return FALSE;
882     }
883 
884     return TRUE;
885 }
886 
887 
mfe_check_input_alignment(MEMMAP_CUR_t YUVPlane)888 static MS_BOOL mfe_check_input_alignment(MEMMAP_CUR_t YUVPlane)
889 {
890     MS_PHY align = 0;
891     switch (appPVR_Info->bColorFormat)
892     {
893         case MSTTILE:
894 #if defined(MFE_YUV_LOADER)
895             align = 0x1FF; // 512-byte alignment
896 #else
897             align = 0xFF;  // 256-byte alignment
898 #endif
899             break;
900 
901         case MFE_YUYV:
902         case MFE_YVYU:
903         case MFE_UYVY:
904         case MFE_VYUY:
905             align = 0x7;   // 8-byte alignment
906             break;
907 
908 #if defined(MFE_YUV_LOADER)
909         case MFE_EVDTILE:
910         case MFE_32x32TILE:
911         case MFE_NV12:
912         case MFE_NV21:
913             align = 0x3FF; // 1024-byte alignment
914             break;
915 #endif
916 
917         default:
918             ms_dprintf(ADP_L0, "error: unsupported input file format\n");
919             return FALSE;
920     }
921 
922     if ((YUVPlane.Cur_Y0.miuAddress & align) != 0 ||
923         (YUVPlane.Cur_C0.miuAddress & align) != 0)
924     {
925         return FALSE;
926     }
927 
928     if (appPVR_Info->m_bFrameMode == 0)
929     {
930         if ((YUVPlane.Cur_Y1.miuAddress & align) != 0 ||
931             (YUVPlane.Cur_C1.miuAddress & align) != 0)
932         {
933             return FALSE;
934         }
935     }
936 
937     return TRUE;
938 }
939 
940 
_MApi_MFE_CompressOnePicture(PVR_Info * mfe_Info,MEMMAP_CUR_t YUVPlane,MS_BOOL bForceIframe)941 MS_BOOL _MApi_MFE_CompressOnePicture(PVR_Info* mfe_Info, MEMMAP_CUR_t YUVPlane, MS_BOOL bForceIframe)
942 {
943     MS_BOOL ret = FALSE;
944     appPVR_Info = mfe_Info;
945     if(appPVR_Info->mfe_state != MFE_Idle) {
946         ms_dprintf(ADP_L0,"state check error.[%d]\n",appPVR_Info->mfe_state);
947         return FALSE;
948     }
949 
950     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Busy);
951 
952     ms_dprintf(ADP_L2, "input Y0: VA %p, PA 0x%x\n", YUVPlane.Cur_Y0.miuPointer, (unsigned int)YUVPlane.Cur_Y0.miuAddress);
953     ms_dprintf(ADP_L2, "input C0: VA %p, PA 0x%x\n", YUVPlane.Cur_C0.miuPointer, (unsigned int)YUVPlane.Cur_C0.miuAddress);
954 
955     if (TRUE == mfe_check_input_alignment(YUVPlane)) {
956         if (bForceIframe) {
957             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_FORCEDI, (MS_VIRT)NULL);
958         }
959         //sem_post(&madp_sem_input);
960         ret = MAdp_MFE_Proc_nonThread(YUVPlane);
961     }
962     else {
963         ms_dprintf(ADP_L0, "error: incorrect input address ailgnment\n");
964     }
965 
966     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
967     return ret;
968 }
969 
970 
_MApi_MFE_DeInit(PVR_Info * mfe_Info)971 MS_BOOL _MApi_MFE_DeInit(PVR_Info* mfe_Info)
972 {
973 
974     MS_S32 err = 0;
975     MS_U32 temp=0;
976     appPVR_Info = mfe_Info;
977     if(appPVR_Info->mfe_state != MFE_Idle) {
978         ms_dprintf(ADP_L4,"MApi_MFE_DeInitialize state check error.[%d] \
979             Still Force deinitialize the mfe..\n",appPVR_Info->mfe_state);
980     }
981 
982     MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_DeInit);
983 
984     appPVR_Info->MfeApiInfo.Encode_stop = 1;
985 
986     if (appPVR_Info->mfe_drvHandle != NULL) //mfe_drvHandle = NULL, not init or init fail
987     {
988         err = mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_FINISH, (MS_VIRT)temp);
989         MfeDrvMemFree(&appPVR_Info->mfe_drvHandle, (const MS_S8*)("mfe_drvHandle"));
990     }
991 
992     if(appPVR_Info->enableISR) {
993         ms_dprintf(ADP_L1,"MFE close interrupt\n");
994         mfe_close_int();
995     }
996 #if defined(_SUPPORT_JPE_)
997     if(appPVR_Info->nCodecType == JPEG_ENC)
998         deinit_fdc();
999 #endif
1000     appPVR_Info = NULL;
1001     //MfeDrvMemFree(appPVR_Info, (const MS_S8*)("appPVR_Info"));
1002     //MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Invalid);
1003 
1004 
1005     return (err == 0);
1006 
1007 
1008 }
1009 
_MApi_MFE_GetOBuf(PVR_Info * mfe_Info,MEMMAP_t * obuf)1010 MS_BOOL _MApi_MFE_GetOBuf(PVR_Info* mfe_Info,MEMMAP_t* obuf)
1011 {
1012     appPVR_Info = mfe_Info;
1013     memcpy(obuf,&appPVR_Info->BitstreamOutBuffer,sizeof(MEMMAP_t));
1014 
1015     return TRUE;
1016 }
1017 
_MApi_MFE_SetBitrateFramerate(PVR_Info * mfe_Info)1018 MS_BOOL _MApi_MFE_SetBitrateFramerate(PVR_Info* mfe_Info)
1019 {
1020 
1021     appPVR_Info = mfe_Info;
1022     if(appPVR_Info->nBitrate== 0 ) {
1023         ms_dprintf(ADP_L0,"bitrate cannot be 0.\n");
1024         MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
1025         return FALSE;
1026     }
1027     if(appPVR_Info->FrameRatex100 == 0) {
1028         ms_dprintf(ADP_L0,"framerate cannot be 0.\n");
1029         MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
1030         return FALSE;
1031     }
1032     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_RESET_BITRATE_AND_FRAMERATE,  (MS_VIRT)(appPVR_Info));
1033 
1034     return TRUE;
1035 }
1036 
_MApi_MFE_PowerOff(PVR_Info * mfe_Info)1037 MS_BOOL _MApi_MFE_PowerOff(PVR_Info* mfe_Info)
1038 {
1039 
1040     POWERON_t power_Info;
1041     appPVR_Info = mfe_Info;
1042     power_Info.is_off = 1;
1043     power_Info.clock_level = MFE_CLK_FAST;
1044     mfe_ioctl((MS_VIRT)NULL, MFE_IOC_POWEROFF, (MS_VIRT)&power_Info);
1045 
1046     return TRUE;
1047 }
1048 
_MApi_MFE_ISREnable(PVR_Info * mfe_Info)1049 MS_BOOL _MApi_MFE_ISREnable(PVR_Info* mfe_Info)
1050 {
1051 
1052     appPVR_Info = mfe_Info;
1053     if(mfe_Info->enableISR) {
1054         // Not needed here. Open interrupt at CompressOnePicture instead.
1055         //mfe_openInt();
1056     } else {
1057         mfe_close_int();
1058     }
1059     mfe_ioctl((MS_VIRT)NULL, MFE_IOC_SET_ISR, (MS_VIRT)(appPVR_Info));
1060     return TRUE;
1061 }
1062 
_MApi_MFE_SetColorFormat(PVR_Info * mfe_Info)1063 MS_BOOL _MApi_MFE_SetColorFormat(PVR_Info* mfe_Info)
1064 {
1065 
1066     appPVR_Info = mfe_Info;
1067 #if defined(_MFE_MUJI_) || defined(_MFE_MONET_) || defined(_MFE_MESSI_) || defined(_MFE_MANHATTAN_) || defined(_MFE_MASERATI_) || defined(_MFE_MAXIM_) || defined(_MFE_KANO_) || defined(_MFE_K6_)
1068             mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_COLOR_FORMAT,  (MS_VIRT)(appPVR_Info));
1069 #else
1070             ms_dprintf(ADP_L0,"Not support MFE_ENC_SETCONF_SET_COLORFORMAT on this chip\n");
1071             MAdp_MFE_Change_State(&appPVR_Info->mfe_state,MFE_Idle);
1072             return FALSE;
1073 #endif
1074 
1075     return TRUE;
1076 }
1077 
_MApi_MFE_ResetSPSPPS(PVR_Info * mfe_Info)1078 MS_BOOL _MApi_MFE_ResetSPSPPS(PVR_Info* mfe_Info)
1079 {
1080     appPVR_Info = mfe_Info;
1081     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_RESET_SPSPPS, (MS_VIRT)NULL);
1082 
1083     return TRUE;
1084 }
1085 
_MApi_MFE_SetFrameType(PVR_Info * mfe_Info,MS_S32 frametype)1086 MS_BOOL _MApi_MFE_SetFrameType(PVR_Info* mfe_Info, MS_S32 frametype)
1087 {
1088     appPVR_Info = mfe_Info;
1089     if(frametype == I_VOP)
1090         mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_FORCEDI, (MS_VIRT)NULL);
1091 
1092     return TRUE;
1093 }
1094 
_MApi_MFE_GetHWCap(PVR_Info * mfe_Info,MFE_HwCapV0 * pDrv,MS_U32 * len)1095 MS_BOOL _MApi_MFE_GetHWCap(PVR_Info* mfe_Info, MFE_HwCapV0 *pDrv, MS_U32 *len)
1096 {
1097 
1098     MS_U16 u16CopiedLength = sizeof(MFE_HwCapV0);
1099     MFE_HwCapV0 DrvHW;
1100     appPVR_Info = mfe_Info;
1101     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_HWCAP,  (MS_VIRT)(&DrvHW));
1102 
1103     if(pDrv->u16ApiHW_Length == 0){
1104         ms_dprintf(ADP_L0, "Null paramter or Wrong u16ApiHW_Length!!\n");
1105         return 0;
1106     }
1107 
1108     if(pDrv->u32ApiHW_Version < 1){
1109         ms_dprintf(ADP_L0, "Wrong u32ApiHW_Version please check!!\n");
1110         return 0;
1111     }
1112 
1113     //new AP + old lib, only the length corresponding to old lib has meaning.
1114     if(pDrv->u32ApiHW_Version > API_HWCAP_VERSION0){
1115         ms_dprintf(ADP_L2, "old lib version has only length: %u\n", (unsigned int)sizeof(MFE_HwCapV0));
1116         ms_dprintf(ADP_L2, "we still copy the min size of both structure, but AP should take care of it.\n");
1117         u16CopiedLength = sizeof(MFE_HwCapV0);
1118     }
1119 
1120     //old AP + new lib, driver shouldn't access to the space which doesn't exist in old structure
1121     if((pDrv->u32ApiHW_Version < API_HWCAP_VERSION0) || (pDrv->u16ApiHW_Length < sizeof(MFE_HwCapV0))){
1122         ms_dprintf(ADP_L2, "new lib version access by old structure!!\n");
1123         u16CopiedLength = pDrv->u16ApiHW_Length;
1124     }
1125 
1126     DrvHW.u32ApiHW_Version = API_HWCAP_VERSION0;
1127     DrvHW.u16ApiHW_Length = u16CopiedLength;
1128     DrvHW.bIsSupportEncoder = TRUE;
1129     DrvHW.u8MaxEncodeFrame = 0;
1130     DrvHW.bSupportSubVidWin = FALSE;
1131     DrvHW.bSupportMpeg4 = FALSE;
1132     DrvHW.bSupportH264 = TRUE;
1133     memcpy(pDrv, &DrvHW, u16CopiedLength);
1134     *len = u16CopiedLength;
1135     return TRUE;
1136 }
1137 
_MApi_MFE_GetOutputInfo(PVR_Info * mfe_Info,MS_S32 * frametype,MS_S32 * size)1138 MS_BOOL _MApi_MFE_GetOutputInfo(PVR_Info *mfe_Info, MS_S32 *frametype, MS_S32 *size)
1139 {
1140     appPVR_Info = mfe_Info;
1141     if (appPVR_Info->mfe_state != MFE_Idle) {
1142         ms_dprintf(ADP_L0, "state check error.[%d]\n", appPVR_Info->mfe_state);
1143         return FALSE;
1144     }
1145     *size = appPVR_Info->BitstreamOutBuffer.size;
1146     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_GET_FRAME_TYPE, (MS_VIRT)(frametype));
1147 
1148     return TRUE;
1149 }
1150 
_MApi_MFE_SetVUI(PVR_Info * mfe_Info,VUI_Info * vui_info)1151 MS_BOOL _MApi_MFE_SetVUI(PVR_Info *mfe_Info, VUI_Info* vui_info)
1152 {
1153     appPVR_Info = mfe_Info;
1154     if (appPVR_Info->mfe_state != MFE_Idle && appPVR_Info->mfe_state != MFE_Init) {
1155         ms_dprintf(ADP_L0, "state check error.[%d]\n", appPVR_Info->mfe_state);
1156         return FALSE;
1157     }
1158     mfe_ioctl((MS_VIRT)appPVR_Info->mfe_drvHandle, MFE_IOC_SET_VUI, (MS_VIRT)(vui_info));
1159 
1160     return TRUE;
1161 }
1162