178e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar/*
278e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * Copyright (C) 2007-2008 ARM Limited
378e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
478e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * Licensed under the Apache License, Version 2.0 (the "License");
578e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * you may not use this file except in compliance with the License.
678e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * You may obtain a copy of the License at
778e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
878e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *      http://www.apache.org/licenses/LICENSE-2.0
978e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
1078e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * Unless required by applicable law or agreed to in writing, software
1178e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * distributed under the License is distributed on an "AS IS" BASIS,
1278e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1378e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * See the License for the specific language governing permissions and
1478e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar * limitations under the License.
1578e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar *
1678e52bfac041d71ce53b5b13c2abf78af742b09dLajos Molnar */
170c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
180c1bc742181ded4930842b46e9507372f0b1b963James Dong *
190c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  omxVCM4P10_GetVLCInfo.c
200c1bc742181ded4930842b46e9507372f0b1b963James Dong * OpenMAX DL: v1.0.2
210c1bc742181ded4930842b46e9507372f0b1b963James Dong * Revision:   9641
220c1bc742181ded4930842b46e9507372f0b1b963James Dong * Date:       Thursday, February 7, 2008
230c1bc742181ded4930842b46e9507372f0b1b963James Dong *
240c1bc742181ded4930842b46e9507372f0b1b963James Dong *
250c1bc742181ded4930842b46e9507372f0b1b963James Dong *
260c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
270c1bc742181ded4930842b46e9507372f0b1b963James Dong *
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * This function extracts run-length encoding (RLE) information
290c1bc742181ded4930842b46e9507372f0b1b963James Dong */
300c1bc742181ded4930842b46e9507372f0b1b963James Dong
310c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
320c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
330c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxVC.h"
340c1bc742181ded4930842b46e9507372f0b1b963James Dong
350c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong
380c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
390c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P10_GetVLCInfo   (6.3.5.9.1)
400c1bc742181ded4930842b46e9507372f0b1b963James Dong *
410c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
420c1bc742181ded4930842b46e9507372f0b1b963James Dong * This function extracts run-length encoding (RLE) information from the
430c1bc742181ded4930842b46e9507372f0b1b963James Dong * coefficient matrix.  The results are returned in an OMXVCM4P10VLCInfo
440c1bc742181ded4930842b46e9507372f0b1b963James Dong * structure.
450c1bc742181ded4930842b46e9507372f0b1b963James Dong *
460c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
470c1bc742181ded4930842b46e9507372f0b1b963James Dong *
480c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pSrcCoeff - pointer to the transform coefficient matrix.  8-byte
490c1bc742181ded4930842b46e9507372f0b1b963James Dong *            alignment required.
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pScanMatrix - pointer to the scan order definition matrix.  For a luma
510c1bc742181ded4930842b46e9507372f0b1b963James Dong *            block the scan matrix should follow [ISO14496-10] section 8.5.4,
520c1bc742181ded4930842b46e9507372f0b1b963James Dong *            and should contain the values 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13,
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *            10, 7, 11, 14, 15.  For a chroma block, the scan matrix should
540c1bc742181ded4930842b46e9507372f0b1b963James Dong *            contain the values 0, 1, 2, 3.
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *   bAC - indicates presence of a DC coefficient; 0 = DC coefficient
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *            present, 1= DC coefficient absent.
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *   MaxNumCoef - specifies the number of coefficients contained in the
580c1bc742181ded4930842b46e9507372f0b1b963James Dong *            transform coefficient matrix, pSrcCoeff. The value should be 16
590c1bc742181ded4930842b46e9507372f0b1b963James Dong *            for blocks of type LUMADC, LUMAAC, LUMALEVEL, and CHROMAAC. The
600c1bc742181ded4930842b46e9507372f0b1b963James Dong *            value should be 4 for blocks of type CHROMADC.
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *
620c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
630c1bc742181ded4930842b46e9507372f0b1b963James Dong *
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pDstVLCInfo - pointer to structure that stores information for
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *            run-length coding.
660c1bc742181ded4930842b46e9507372f0b1b963James Dong *
670c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
680c1bc742181ded4930842b46e9507372f0b1b963James Dong *
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr - no error
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - bad arguments; returned if any of the following
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *              conditions are true:
720c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    at least one of the following pointers is NULL:
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *            pSrcCoeff, pScanMatrix, pDstVLCInfo
740c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    pSrcCoeff is not aligned on an 8-byte boundary
750c1bc742181ded4930842b46e9507372f0b1b963James Dong *
760c1bc742181ded4930842b46e9507372f0b1b963James Dong */
770c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult omxVCM4P10_GetVLCInfo (
780c1bc742181ded4930842b46e9507372f0b1b963James Dong	const OMX_S16*		    pSrcCoeff,
790c1bc742181ded4930842b46e9507372f0b1b963James Dong	const OMX_U8*			    pScanMatrix,
800c1bc742181ded4930842b46e9507372f0b1b963James Dong	OMX_U8			    bAC,
810c1bc742181ded4930842b46e9507372f0b1b963James Dong	OMX_U32			    MaxNumCoef,
820c1bc742181ded4930842b46e9507372f0b1b963James Dong	OMXVCM4P10VLCInfo*	pDstVLCInfo
830c1bc742181ded4930842b46e9507372f0b1b963James Dong)
840c1bc742181ded4930842b46e9507372f0b1b963James Dong{
850c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT     i, MinIndex;
860c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S32     Value;
870c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U32     Mask = 4, RunBefore;
880c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16     *pLevel;
890c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8      *pRun;
900c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16     Buf [16];
910c1bc742181ded4930842b46e9507372f0b1b963James Dong
920c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* check for argument error */
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pSrcCoeff == NULL, OMX_Sts_BadArgErr)
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(armNot8ByteAligned(pSrcCoeff), OMX_Sts_BadArgErr)
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pScanMatrix == NULL, OMX_Sts_BadArgErr)
960c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pDstVLCInfo == NULL, OMX_Sts_BadArgErr)
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(bAC > 1, OMX_Sts_BadArgErr)
980c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(MaxNumCoef > 16, OMX_Sts_BadArgErr)
990c1bc742181ded4930842b46e9507372f0b1b963James Dong
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Initialize RLE Info structure */
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstVLCInfo->uTrailing_Ones = 0;
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstVLCInfo->uTrailing_One_Signs = 0;
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstVLCInfo->uNumCoeffs = 0;
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstVLCInfo->uTotalZeros = 0;
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (i = 0; i < 16; i++)
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstVLCInfo->iLevels [i] = 0;
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstVLCInfo->uRuns [i] = 0;
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong    MinIndex = (bAC == 0 && MaxNumCoef == 15) ? 1 : 0;
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (i = MinIndex; i < (MaxNumCoef + MinIndex); i++)
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Scan */
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong        Buf [i - MinIndex] = pSrcCoeff [pScanMatrix [i]];
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* skip zeros at the end */
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong    i = MaxNumCoef - 1;
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong    while (!Buf [i] && i >= 0)
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong        i--;
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (i < 0)
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong        return OMX_Sts_NoErr;
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Fill RLE Info structure */
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong    pLevel = pDstVLCInfo->iLevels;
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong    pRun = pDstVLCInfo->uRuns;
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong    RunBefore = 0;
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Handle first non zero separate */
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDstVLCInfo->uNumCoeffs++;
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong    Value = Buf [i];
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (Value == 1 || Value == -1)
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstVLCInfo->uTrailing_Ones++;
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong        pDstVLCInfo->uTrailing_One_Signs |=
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong            Value == -1 ? Mask : 0;
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong        Mask >>= 1;
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong    else
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong        Value -= (Value > 0 ? 1 : -1);
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pLevel++ = Value;
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong        Mask = 0;
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Remaining non zero */
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong    while (--i >= 0)
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong        Value = Buf [i];
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (Value)
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDstVLCInfo->uNumCoeffs++;
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Mask becomes zero after entering */
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (Mask &&
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong                (Value == 1 ||
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong                 Value == -1))
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong                pDstVLCInfo->uTrailing_Ones++;
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong                pDstVLCInfo->uTrailing_One_Signs |=
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong                    Value == -1 ? Mask : 0;
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong                Mask >>= 1;
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong                *pRun++ = RunBefore;
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong                RunBefore = 0;
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong            else
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong                /* If 3 trailing ones are not completed */
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (Mask)
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong                    Mask = 0;
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong                    Value -= (Value > 0 ? 1 : -1);
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong                *pLevel++ = Value;
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong                *pRun++ = RunBefore;
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong                RunBefore = 0;
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDstVLCInfo->uTotalZeros++;
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong            RunBefore++;
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Update last run */
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (RunBefore)
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pRun++ = RunBefore;
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong    return OMX_Sts_NoErr;
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong/*****************************************************************************
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong *                              END OF FILE
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong *****************************************************************************/
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong
208