xref: /utopia/UTPA2-700.0.x/modules/vdec_lite/drv/hvd_lite/HVD_EX_Common.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) 2008-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 // ("MStar Confidential Information") 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 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvHVD_EX.c
98 /// @brief  HVD Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 #if (!defined(MSOS_TYPE_NUTTX) && !defined(MSOS_TYPE_OPTEE)) || defined(SUPPORT_X_MODEL_FEATURE)
107 
108 // Common Definition
109 #ifdef MSOS_TYPE_LINUX_KERNEL
110 #include <linux/string.h>
111 #else
112 #include <string.h>
113 #endif
114 
115 #include "drvHVD_Common.h"
116 #include "drvHVD_EX.h"
117 #include "drvHVD_def.h"
118 #include "HVD_EX_Common.h"
119 
120 #include "fwHVD_if.h"
121 #include "halVPU_EX.h"
122 #include "halHVD_EX.h"
123 #include "drvSYS.h"
124 
125 #if HVD_ENABLE_AUTO_SET_REG_BASE
126 #include "drvMMIO.h"
127 #endif
128 
129 //-------------------------------------------------------------------------------------------------
130 //  Driver Compiler Options
131 //-------------------------------------------------------------------------------------------------
132 
133 
134 //-------------------------------------------------------------------------------------------------
135 //  Local Defines
136 //-------------------------------------------------------------------------------------------------
137 
138 
139 //-------------------------------------------------------------------------------------------------
140 //  Local Structures
141 //-------------------------------------------------------------------------------------------------
142 
143 //-------------------------------------------------------------------------------------------------
144 // Local Functions Prototype
145 //-------------------------------------------------------------------------------------------------
146 #ifndef CONFIG_MBOOT  //Add For GPL (content protection)
147 
148 #define IPAUTH_VIDEO_H264           (27)
149 #define IPAUTH_VIDEO_RM             (28)
150 #define IPAUTH_VIDEO_AVS            (33)
151 #define IPAUTH_VIDEO_MVC            (40)
152 #define IPAUTH_VIDEO_VP8            (61)
153 #define IPAUTH_VIDEO_HEVC           (75)
154 #define IPAUTH_VIDEO_VP9            (91)
155 MS_BOOL bModeSelectHD = FALSE;
156 static MS_BOOL bAutoIpcheck = FALSE;
157 #if !defined(SUPPORT_X_MODEL_FEATURE)
158 extern MS_U8 MDrv_AUTH_IPCheck(MS_U8 u8Bit);            //FROM #include "drvIPAUTH.h"
159 #endif
160 #define HVD_CODEC_INFO_MASK 0x0000000f
161 
_MDrv_HVD_AUTH_IPCheck(MS_U32 u32CodecType)162 MS_BOOL  _MDrv_HVD_AUTH_IPCheck(MS_U32 u32CodecType)
163 {
164     MS_BOOL bIPAUTHReady = FALSE;
165     VDEC_EX_CODEC_CAP_INFO pCodecCapInfo;
166 #if !defined(SUPPORT_X_MODEL_FEATURE)
167     u32CodecType = (u32CodecType & HVD_CODEC_INFO_MASK);
168     bAutoIpcheck = FALSE;
169     switch (u32CodecType)
170     {
171         case HVD_INIT_HW_AVC:
172             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_H264) == TRUE)
173             {
174                 bModeSelectHD =TRUE;
175                 bIPAUTHReady = TRUE;
176                 bAutoIpcheck =TRUE;
177             }
178             break;
179         case HVD_INIT_HW_AVS:
180             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_AVS) == TRUE)
181             {
182                 bModeSelectHD =TRUE;
183                 bIPAUTHReady = TRUE;
184                 bAutoIpcheck =TRUE;
185             }
186             break;
187         case HVD_INIT_HW_RM:
188             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_RM) == TRUE)
189             {
190                 bModeSelectHD =TRUE;
191                 bIPAUTHReady =TRUE;
192                 bAutoIpcheck =TRUE;
193             }
194             break;
195         case HVD_INIT_HW_MVC:  /// SUPPORT_MVC
196             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_MVC) == TRUE)
197             {
198                 bModeSelectHD =TRUE;
199                 bIPAUTHReady =TRUE;
200                 bAutoIpcheck =TRUE;
201             }
202             break;
203         case HVD_INIT_HW_VP8:
204             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_VP8) == TRUE)
205             {
206                 bModeSelectHD =TRUE;
207                 bIPAUTHReady =TRUE;
208                 bAutoIpcheck =TRUE;
209             }
210             break;
211         case HVD_INIT_HW_HEVC:
212             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_HEVC) == TRUE)
213             {
214                 bModeSelectHD =TRUE;
215                 bIPAUTHReady =TRUE;
216                 bAutoIpcheck =TRUE;
217             }
218             break;
219          case HVD_INIT_HW_VP9:
220             if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_VP9) == TRUE && HAL_VPU_EX_GetCodecCapInfo( E_HVD_EX_CODEC_TYPE_VP9, &pCodecCapInfo) == TRUE)
221             {
222                 bModeSelectHD =TRUE;
223                 bIPAUTHReady =TRUE;
224                 bAutoIpcheck =TRUE;
225             }
226             break;
227         default:
228             bIPAUTHReady = FALSE;
229             break;
230     }
231 
232     if(bIPAUTHReady == FALSE)
233     {
234         HVD_EX_MSG_ERR("[%s]User using invaild key !!!\n",__FUNCTION__);
235     }
236 #endif
237     return bIPAUTHReady;
238 }
239 #endif
240 
241 #ifdef VDEC3
242 #define HVD_MAX_STREAMS         16
243 #else
244 #define HVD_MAX_STREAMS         2
245 #endif
246 
247 #define VPU_MAIL_BOX_SEND_FAIL 1
248 #define CODEC_KEY_COMPARE_TIMEOUT 2
249 extern MS_U8 u8CodecCompare[HVD_MAX_STREAMS];
250 extern HVD_EX_Result _HVD_EX_Rst(MS_U32 u32Id, MS_BOOL bErrHandle);
251 #define _VDEC_GET_IDX(x)    (MS_U8)((x)>>24)
252 
253 #if defined(CHIP_KANO)
_MDrv_HVDSendVPUCMD(MS_U32 u32Id,MS_U32 u32CMD,MS_U32 u32Arg)254 MS_BOOL _MDrv_HVDSendVPUCMD(MS_U32 u32Id,MS_U32 u32CMD,MS_U32 u32Arg)
255 {
256     MS_BOOL bRet = TRUE;
257     MS_U8  u8Idx = _VDEC_GET_IDX(u32Id);
258 
259     HAL_VPU_EX_Mutex_Lock();
260     if(HAL_HVD_EX_SetCmd(u32Id, u32CMD, u32Arg) != E_HVD_RETURN_SUCCESS)
261     {
262         bRet = FALSE;
263     }
264     HAL_VPU_EX_Mutex_UnLock();
265 
266     if(bRet == FALSE)
267     {
268         HVD_PRINT("[Info]Case1\n");
269         u8CodecCompare[u8Idx] = VPU_MAIL_BOX_SEND_FAIL;
270     }
271 
272     return bRet;
273 }
274 
_MDrv_HVDSendCodecKey(MS_U32 u32Id,MS_U8 u8Decoder,MS_U16 u16CodecKey)275 MS_BOOL _MDrv_HVDSendCodecKey(MS_U32 u32Id,MS_U8 u8Decoder,MS_U16 u16CodecKey)
276 {
277     MS_U8 u8Idx = HAL_VPU_EX_GetOffsetIdx(u32Id);
278     MS_U32 u32Timeout = 0;
279     struct _ctl_info *ctl_ptr = (struct _ctl_info *) MsOS_PA2KSEG1(HAL_VPU_EX_GetFWCodeAddr(u32Id) + CTL_INFO_ADDR);
280 
281     if(_MDrv_HVDSendVPUCMD(u32Id,E_NST_CMD_COMMON_CMD1,(((VDEC_TAG<<24)&0xff000000))|(((u8Decoder<<16)&0xff0000))|(u16CodecKey&0xffff)) == FALSE)
282     {
283         HAL_HVD_EX_DeInit(u32Id);
284         return FALSE;
285     }
286 
287     if(_MDrv_HVDSendVPUCMD(u32Id,E_NST_CMD_COMMON_MASK,0) == FALSE)
288     {
289         HAL_HVD_EX_DeInit(u32Id);
290         return FALSE;
291     }
292 
293     MsOS_ReadMemory();
294     u32Timeout = HVD_GetSysTime_ms() + 500; //timeout 500 ms
295     while (CTL_TASK_CMD != ctl_ptr->task_statue[u8Idx])
296     {
297         if (HVD_GetSysTime_ms() > u32Timeout)
298         {
299             HVD_PRINT("[Info]Case2\n");
300             u8CodecCompare[u8Idx] = CODEC_KEY_COMPARE_TIMEOUT;
301             HAL_HVD_EX_DeInit(u32Id);
302             return FALSE;
303         }
304         MsOS_ReadMemory();
305     }
306 
307     return TRUE;
308 }
309 #endif
310 
_HVD_EX_Init_(MS_U32 u32Id,MS_U32 u32ModeFlag)311 HVD_EX_Result _HVD_EX_Init_(MS_U32 u32Id,MS_U32 u32ModeFlag)
312 {
313 	HVD_EX_Result eRst = E_HVD_EX_FAIL;
314 #ifndef CONFIG_MBOOT  //Add For GPL (content protection)
315 	if(bAutoIpcheck)
316 	{
317 		eRst = _HVD_EX_Rst(u32Id ,FALSE);
318 
319 #if defined(CHIP_KANO)
320         MS_U32 u32CodecType = u32ModeFlag & HVD_CODEC_INFO_MASK;
321         if(eRst == E_HVD_EX_OK)
322         {
323             if(u32CodecType != HVD_INIT_HW_MJPEG)
324             {
325                 if(_MDrv_HVDSendCodecKey(u32Id,HVD_DECODER,u32CodecType) == FALSE)
326                 {
327                     eRst = E_HVD_EX_FAIL;
328                 }
329             }
330         }
331 #endif
332     }
333     else
334 	{
335 		HVD_PRINT("[%s] .. STOP and Contact VDEC team!!!\n",__FUNCTION__);
336 	}
337 #else
338     eRst = _HVD_EX_Rst(u32Id ,FALSE);
339 #endif
340 
341 	return eRst;
342 }
343 
344 #if SUPPORT_EVD
_HVD_EX_DemuxTS_GetBits(const MS_U8 * buffer,MS_U16 totbitoffset,MS_U32 * info,MS_U16 bufferLen,MS_U16 numbits)345 static MS_U16 _HVD_EX_DemuxTS_GetBits(const MS_U8 *buffer, MS_U16 totbitoffset, MS_U32 *info, MS_U16 bufferLen, MS_U16 numbits)
346 {
347     MS_U32 inf;
348     MS_U8 bitoffset  = (totbitoffset & 0x07); // bit from start of byte
349     MS_U16 byteoffset = (totbitoffset >> 3);       // byte from start of buffer
350     MS_U16 bitcounter = numbits;
351     const MS_U8 *curbyte;
352     if ((byteoffset) + ((numbits + bitoffset)>> 3)  > bufferLen)
353         return 0;
354 
355     curbyte = &(buffer[byteoffset]);
356     bitoffset = 7 - bitoffset;
357     inf=0;
358 
359     while (numbits--)
360     {
361         inf <<=1;
362         inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
363         curbyte   += (bitoffset >> 3) & 0x01;
364         //curbyte   -= (bitoffset >> 3);
365         bitoffset &= 0x07;
366         //curbyte   += (bitoffset == 7);
367     }
368 
369     *info = inf;
370     return bitcounter;           // return absolute offset in bit from start of frame
371 
372 }
373 
_HVD_EX_DemuxTS_PeekOneByte(MS_VIRT u32NonCacheAddr)374 static MS_U8 _HVD_EX_DemuxTS_PeekOneByte(MS_VIRT u32NonCacheAddr)
375 {
376    return *((MS_U8 *)(u32NonCacheAddr));
377 }
378 
_HVD_EX_DemuxTS_EBSPtoRBSP(MS_U32 u32SrcPos,MS_U8 * pu8DestBuf,MS_U16 u16end_bytepos,MS_U16 u16begin_bytepos)379 static MS_U16 _HVD_EX_DemuxTS_EBSPtoRBSP(MS_U32 u32SrcPos, MS_U8 *pu8DestBuf, MS_U16 u16end_bytepos, MS_U16 u16begin_bytepos)
380 {
381     MS_U16 i, j, count;
382     MS_VIRT tmpPos;
383     count = 0;
384 
385     if(u16end_bytepos < u16begin_bytepos)
386     {
387         HVD_PRINT("_HVD_EX_DemuxTS_EBSPtoRBSP failed~ u16end_bytepos < u16begin_bytepos\n");
388         return 0;
389     }
390 
391     j = 0;
392     for(i = u16begin_bytepos; i < u16end_bytepos; i++)
393     {
394         tmpPos = i+u32SrcPos;
395         /// Check 0x000000, 0x000001 or 0x000002, those shall not occur at any byte-aligned.
396         /// ISO 14496-10 Page:49 , 7.4.1 emulation_prevention_three_byte information.
397         if((count == 2) && (_HVD_EX_DemuxTS_PeekOneByte(tmpPos) < 0x03))
398             return j;
399         if((count == 2) && (_HVD_EX_DemuxTS_PeekOneByte(tmpPos) == 0x03))
400         {
401             if((i < u16end_bytepos-1) && (_HVD_EX_DemuxTS_PeekOneByte(tmpPos+1) > 0x03))
402                 return j;
403             if(i == u16end_bytepos-1)
404                 return j;
405 
406             i++;
407             tmpPos++;
408             count = 0;
409         }
410         pu8DestBuf[j] = _HVD_EX_DemuxTS_PeekOneByte(tmpPos);
411         if(_HVD_EX_DemuxTS_PeekOneByte(tmpPos) == 0x00)
412             count++;
413         else
414             count = 0;
415         j++;
416 
417         if ( j > H265_RBSP_BUF_LEN )
418         {
419             HVD_PRINT("_HVD_EX_DemuxTS_EBSPtoRBSP failed~ byte is too much than 1024!!\n");
420             return H265_RBSP_BUF_LEN;
421         }
422     }
423     return j;
424 }
425 
_HVD_EX_DemuxTS_ReadSyntaxElement_FLC(MS_U16 u16ReadBits,const MS_U8 * buffer,MS_U16 bufferLen,MS_U16 * u16bitLen,MS_BOOL bUnsige)426 static MS_U32 _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(MS_U16 u16ReadBits, const MS_U8* buffer, MS_U16 bufferLen, MS_U16* u16bitLen, MS_BOOL bUnsige)
427 {
428     MS_U16 u16readbitLen = 0;
429     MS_U32 u32codeword = 0;
430     u16readbitLen = _HVD_EX_DemuxTS_GetBits(buffer, *u16bitLen, &u32codeword, bufferLen, u16ReadBits);
431     HVD_EX_MSG_DBG("BitLen=%d, Readbits=%d, Codeword=%d.\n",*u16bitLen,u16readbitLen,u32codeword);
432     *u16bitLen += u16readbitLen;
433     if(!bUnsige)
434     {
435         /// u32codeword change to s32 format
436     }
437     return u32codeword;
438 }
439 
_HVD_EX_DemuxTS_GetVLCSymbol(MS_U8 * buffer,MS_U16 totbitoffset,MS_U32 * info,MS_U16 bufferLen)440 static MS_U16 _HVD_EX_DemuxTS_GetVLCSymbol(MS_U8 *buffer, MS_U16 totbitoffset, MS_U32 *info, MS_U16 bufferLen)
441 {
442     MS_U32 infw;
443     // byte from start of buffer
444     MS_U16 byte_off = (totbitoffset >> 3);
445     // bit from start of byte
446     MS_U8  bit_off = (MS_U8)(7 - (totbitoffset & 0x07));
447     MS_U16  bit_cnt = 1;
448     MS_U16  length = 0;
449     MS_U8 *current_byte = &(buffer[byte_off]);
450     // control bit for current bit posision
451     MS_U8  control_bit  = ((*current_byte) >> (bit_off)) & 0x01;
452 
453     // Finding leading 1 bit
454     while (control_bit == 0)
455     {
456         bit_off--;
457         bit_off &= 0x07;
458         if (bit_off == 7)
459         {
460             current_byte += 1;
461             byte_off += 1;
462         }
463         control_bit = ((*current_byte) >> (bit_off)) & 0x01;
464         length++;
465         bit_cnt++;
466     }
467 
468     if (byte_off + ((length + 7) >> 3) > bufferLen)
469         return 0;
470 
471     // make infoword
472     // shortest possible code is 1, then info is always 0
473     infw = 0;
474     while (length--)
475     {
476         bit_off--;
477         bit_off &= 0x07;
478         if (bit_off == 7)
479             current_byte += 1;
480         infw <<= 1;
481         infw |= ((*current_byte) >> (bit_off)) & 0x01;
482         bit_cnt++;
483     }
484 
485     *info = infw;
486     // return absolute offset in bit from start of frame
487     return bit_cnt;
488 }
489 
_HVD_EX_DemuxTS_ReadSyntaxElement_VLC(MS_U8 * buffer,MS_U16 bufferLen,MS_U16 * u16bitLen,MS_BOOL bUnsige)490 static MS_U32 _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(MS_U8* buffer, MS_U16 bufferLen, MS_U16* u16bitLen, MS_BOOL bUnsige)
491 {
492     MS_U16 u16readbitLen = 0;
493     MS_U32 u32codeword = 0;
494     u16readbitLen = _HVD_EX_DemuxTS_GetVLCSymbol(buffer, *u16bitLen, &u32codeword, bufferLen);
495     u32codeword = (1 << (u16readbitLen >> 1)) + u32codeword - 1;
496     HVD_EX_MSG_DBG("BitLen=%d, Readbits=%d, Codeword=%d.\n",*u16bitLen,u16readbitLen,u32codeword);
497     *u16bitLen += u16readbitLen;
498     if(!bUnsige)
499     {
500         MS_U32 u32codewordtmp = u32codeword;
501         u32codeword = (u32codewordtmp + 1) >> 1;
502         if((u32codewordtmp & 0x01) == 0)
503             u32codeword = (-1*u32codeword);
504     }
505     return u32codeword;
506 }
507 
_HVD_EX_DemuxTS_ParseHEVCSPS(MS_VIRT u32DataPos,MS_U16 u16DataLen,MS_U32 * pMemUsageSize)508 HVD_EX_Result _HVD_EX_DemuxTS_ParseHEVCSPS(MS_VIRT u32DataPos, MS_U16 u16DataLen, MS_U32 *pMemUsageSize)
509 {
510     MS_U8 pu8tmp[H265_RBSP_BUF_LEN] = {0};
511     MS_U16 u16Width = 0;
512     MS_U16 u16Height = 0;
513 
514 #if 0
515 {
516     MS_U32 i = 0;
517     MS_U8 *__pu8tmp = (MS_U8 *)u32DataPos;
518     HVD_ERR("DemuxTS_ParseHEVCSPS size = %d", u16DataLen);
519     for (i = 0; i < u16DataLen; i++)
520     {
521         if(i%32 == 0)
522             HVD_ERR("\n");
523         HVD_ERR("%02x ", *(__pu8tmp + i));
524     }
525     HVD_ERR("\n");
526 }
527 #endif
528 
529     while (u16DataLen >= 5)
530     {
531         if ( (_HVD_EX_DemuxTS_PeekOneByte(u32DataPos) == 0x0
532            && _HVD_EX_DemuxTS_PeekOneByte(u32DataPos+1) == 0x0
533            && _HVD_EX_DemuxTS_PeekOneByte(u32DataPos+2) == 0x1
534            && ((_HVD_EX_DemuxTS_PeekOneByte(u32DataPos+3)>>1) & 0x3F) == 0x21)
535         && u16DataLen > 16 )
536         {
537             #define MAX_SUB_LAYER_MINUS1 6
538             MS_U32 sub_layer_profile_present_flag[MAX_SUB_LAYER_MINUS1];
539             MS_U32 sub_layer_level_present_flag[MAX_SUB_LAYER_MINUS1];
540             MS_U16 u16BitLen = 0;
541             MS_U32 sps_max_sub_layers_minus1;
542             //MS_U32 general_level_idc;
543             MS_U32 chroma_format_idc;
544             MS_U32 pic_width_in_luma_samples, pic_height_in_luma_samples;
545             MS_U32 conformance_window_flag;
546             MS_U32 bit_depth_luma_minus8, bit_depth_chroma_minus8;
547             MS_U32 i, j;
548             MS_U16 _u16DataLen = 0;
549 
550             /// EBSP to RBSP
551             _u16DataLen = _HVD_EX_DemuxTS_EBSPtoRBSP(u32DataPos+5, pu8tmp, (u16DataLen - 5), 0);
552             HVD_EX_MSG_DBG("[DemuxTS] valid data len = %d\n", _u16DataLen);
553             if (_u16DataLen == 0)
554             {
555                 HVD_PRINT("_HVD_EX_DemuxTS_ParseHEVCSPS wrong position\n");
556                 return E_HVD_EX_FAIL;
557             }
558 #if 0
559 {
560     MS_U32 k = 0;
561     for (k = 0; k < _u16DataLen; k++)
562     {
563         if(k%32 == 0)
564             HVD_PRINT("\n");
565         HVD_PRINT("%02x ", pu8tmp[k]);
566     }
567     HVD_PRINT("\n");
568 }
569 #endif
570             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(4,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
571             sps_max_sub_layers_minus1 = _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(3,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
572             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
573 
574             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(2,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
575             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
576             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(5,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
577             for (i = 0; i < 32; i++)
578             {
579                 _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
580             }
581             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
582             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
583             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
584             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
585             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(44,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
586             _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(8,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
587             for (i = 0; i < sps_max_sub_layers_minus1; i++)
588             {
589                 sub_layer_profile_present_flag[i] = _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
590                 sub_layer_level_present_flag[i] = _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
591             }
592 
593             if (sps_max_sub_layers_minus1 > 0)
594             {
595                 for (i = sps_max_sub_layers_minus1; i < 8; i++)
596                 {
597                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(2,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
598                 }
599             }
600 
601             for (i = 0; i < sps_max_sub_layers_minus1; i++)
602             {
603                 if (sub_layer_profile_present_flag[i])
604                 {
605                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(2,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
606                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
607                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(5,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
608                     for (j = 0; j < 32; j++)
609                     {
610                         _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
611                     }
612                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
613                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
614                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
615                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
616                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(44,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
617                 }
618 
619                 if (sub_layer_level_present_flag[i])
620                 {
621                     _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(8,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
622                 }
623             }
624 
625             _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
626             chroma_format_idc = _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
627 
628             if (chroma_format_idc == 3)
629             {
630                 _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
631             }
632 
633             pic_width_in_luma_samples = _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
634             pic_height_in_luma_samples = _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
635             conformance_window_flag = _HVD_EX_DemuxTS_ReadSyntaxElement_FLC(1,pu8tmp,_u16DataLen,&u16BitLen,TRUE);
636 
637             if (conformance_window_flag)
638             {
639                 _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
640                 _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
641                 _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
642                 _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
643             }
644 
645             bit_depth_luma_minus8 = _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
646             bit_depth_chroma_minus8 = _HVD_EX_DemuxTS_ReadSyntaxElement_VLC(pu8tmp,_u16DataLen,&u16BitLen,TRUE);
647             ///pProgramList->enID = E_VDP_CODEC_ID_HEVC;
648             u16Width = (MS_U16)(MEMALIGN(pic_width_in_luma_samples, 32));
649             u16Height = (MS_U16)(MEMALIGN(pic_height_in_luma_samples, 32));
650             ///pProgramList->u8Level_idc = (MS_U8) (general_level_idc / 30);
651             ///pProgramList->u8Profile_idc = (bit_depth_luma_minus8 || bit_depth_chroma_minus8); // used for 10bit
652             ///pProgramList->u8DecFrameNum = 0; // not used
653             ///pProgramList->u8NumRefFrames = 0; // not used
654             ///pProgramList->u8FrameMbsOnlyFlag = 0; // not used
655             ///pProgramList->u8Direct8x8InferenceFlag = 0; // not used
656 
657             {
658                 #define MaxLumaPS_Level_4x    (0x220000) //level4.0
659                 #define MaxLumaPS_Level_5x    (0x880000) //level5.0
660                 #define maxDpbPicBuf (6)
661                 MS_U32 MaxDpbSize;
662                 //MS_U8 u8Level_idc = (MS_U8) (general_level_idc / 30);
663                 MS_U8 u8Profile_idc = (bit_depth_luma_minus8 || bit_depth_chroma_minus8); // used for 10bit
664                 // MS_U32 MaxLumaPS = (u8Level_idc >= 5 || u8Level_idc == 0) ? MaxLumaPS_Level_5x : MaxLumaPS_Level_4x;
665                 //MS_U32 MaxLumaPS = (u8Level_idc >= 5) ? MaxLumaPS_Level_5x : MaxLumaPS_Level_4x;
666                 MS_U32 MaxLumaPS = ((u16Width > 2048) || (u16Height > 1088)) ? MaxLumaPS_Level_5x : MaxLumaPS_Level_4x;
667                 MS_U32 u32FrameSize = u16Height * u16Width;
668                 MS_U32 PicSizeInSamplesY = u32FrameSize;
669 
670                 if ( PicSizeInSamplesY <= ( MaxLumaPS >> 2 ) )
671                     MaxDpbSize = HVD_MIN( 4 * maxDpbPicBuf, 16 );
672                 else if ( PicSizeInSamplesY <= ( MaxLumaPS >> 1 ) )
673                     MaxDpbSize = HVD_MIN( 2 * maxDpbPicBuf, 16 );
674                 else if ( PicSizeInSamplesY <= ( ( 3 * MaxLumaPS ) >> 2 ) )
675                     MaxDpbSize = HVD_MIN( (4 * maxDpbPicBuf) / 3, 16 );
676                 else
677                     MaxDpbSize = maxDpbPicBuf;
678 
679                 if (u8Profile_idc) // 10bit case
680                     *pMemUsageSize = MaxDpbSize * u32FrameSize * 3 / 2 * 5 / 4; /// 1.5 * 1.25
681                 else
682                     *pMemUsageSize = MaxDpbSize * u32FrameSize * 3 / 2;
683 
684                 //consider hw buffer
685                 *pMemUsageSize += HAL_HVD_EX_GetEVDHWBuffer();
686 
687                 HVD_EX_MSG_INF("[DemuxTS] EVD Memory usage = %d\n",
688                     (MS_U32)*pMemUsageSize);
689                 HVD_EX_MSG_INF("[DemuxTS]Prog#X:HEVC,Width X Height = %dX%d.\n",
690                     u16Width, u16Height);
691             }
692 
693             return E_HVD_EX_OK;
694         }
695         else
696         {
697             u32DataPos++;
698             u16DataLen--;
699         }
700     }
701 
702     return E_HVD_EX_FAIL;
703 }
704 #endif //SUPPORT_EVD
705 
706 #endif
707 
708