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:  armVCM4P2_PutVLCBits.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 *
270c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
280c1bc742181ded4930842b46e9507372f0b1b963James Dong * Contains module for VLC put bits to bitstream
290c1bc742181ded4930842b46e9507372f0b1b963James Dong *
300c1bc742181ded4930842b46e9507372f0b1b963James Dong */
310c1bc742181ded4930842b46e9507372f0b1b963James Dong
320c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
330c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
340c1bc742181ded4930842b46e9507372f0b1b963James Dong
350c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM_Bitstream.h"
380c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVCM4P2_ZigZag_Tables.h"
390c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVCM4P2_Huff_Tables_VLC.h"
400c1bc742181ded4930842b46e9507372f0b1b963James Dong
410c1bc742181ded4930842b46e9507372f0b1b963James Dong
420c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
430c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function: armVCM4P2_PutVLCBits
440c1bc742181ded4930842b46e9507372f0b1b963James Dong *
450c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
460c1bc742181ded4930842b46e9507372f0b1b963James Dong * Checks the type of Escape Mode and put encoded bits for
470c1bc742181ded4930842b46e9507372f0b1b963James Dong * quantized DCT coefficients.
480c1bc742181ded4930842b46e9507372f0b1b963James Dong *
490c1bc742181ded4930842b46e9507372f0b1b963James Dong * Remarks:
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *
510c1bc742181ded4930842b46e9507372f0b1b963James Dong * Parameters:
520c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	 ppBitStream      pointer to the pointer to the current byte in
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *						  the bit stream
540c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]	 pBitOffset       pointer to the bit position in the byte pointed
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        by *ppBitStream. Valid within 0 to 7
560c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in] shortVideoHeader binary flag indicating presence of short_video_header; escape modes 0-3 are used if shortVideoHeader==0,
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *                           and escape mode 4 is used when shortVideoHeader==1.
580c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  start            start indicates whether the encoding begins with
590c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        0th element or 1st.
600c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  maxStoreRunL0    Max store possible (considering last and inter/intra)
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        for last = 0
620c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  maxStoreRunL1    Max store possible (considering last and inter/intra)
630c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        for last = 1
640c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  maxRunForMultipleEntriesL0
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        The run value after which level
660c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        will be equal to 1:
670c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        (considering last and inter/intra status) for last = 0
680c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  maxRunForMultipleEntriesL1
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        The run value after which level
700c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        will be equal to 1:
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        (considering last and inter/intra status) for last = 1
720c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pRunIndexTableL0 Run Index table defined in
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        armVCM4P2_Huff_Tables_VLC.c for last == 0
740c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pVlcTableL0      VLC table for last == 0
750c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pRunIndexTableL1 Run Index table defined in
760c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        armVCM4P2_Huff_Tables_VLC.c for last == 1
770c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pVlcTableL1      VLC table for last == 1
780c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pLMAXTableL0     Level MAX table defined in
790c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        armVCM4P2_Huff_Tables_VLC.c for last == 0
800c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pLMAXTableL1     Level MAX table defined in
810c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        armVCM4P2_Huff_Tables_VLC.c for last == 1
820c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pRMAXTableL0     Run MAX table defined in
830c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        armVCM4P2_Huff_Tables_VLC.c for last == 0
840c1bc742181ded4930842b46e9507372f0b1b963James Dong * [in]  pRMAXTableL1     Run MAX table defined in
850c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        armVCM4P2_Huff_Tables_VLC.c for last == 1
860c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] pQDctBlkCoef     pointer to the quantized DCT coefficient
870c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] ppBitStream      *ppBitStream is updated after the block is encoded
880c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        so that it points to the current byte in the bit
890c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        stream buffer.
900c1bc742181ded4930842b46e9507372f0b1b963James Dong * [out] pBitOffset       *pBitOffset is updated so that it points to the
910c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        current bit position in the byte pointed by
920c1bc742181ded4930842b46e9507372f0b1b963James Dong *                        *ppBitStream.
930c1bc742181ded4930842b46e9507372f0b1b963James Dong *
940c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
950c1bc742181ded4930842b46e9507372f0b1b963James Dong * Standard OMXResult result. See enumeration for possible result codes.
960c1bc742181ded4930842b46e9507372f0b1b963James Dong *
970c1bc742181ded4930842b46e9507372f0b1b963James Dong */
980c1bc742181ded4930842b46e9507372f0b1b963James Dong
990c1bc742181ded4930842b46e9507372f0b1b963James Dong
1000c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult armVCM4P2_PutVLCBits (
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_U8 **ppBitStream,
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_INT * pBitOffset,
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_S16 *pQDctBlkCoef,
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_INT shortVideoHeader,
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_U8 start,
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_U8 maxStoreRunL0,
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_U8 maxStoreRunL1,
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_U8  maxRunForMultipleEntriesL0,
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong              OMX_U8  maxRunForMultipleEntriesL1,
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_U8  * pRunIndexTableL0,
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong              const ARM_VLC32 *pVlcTableL0,
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong			  const OMX_U8  * pRunIndexTableL1,
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong              const ARM_VLC32 *pVlcTableL1,
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_U8  * pLMAXTableL0,
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_U8  * pLMAXTableL1,
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_U8  * pRMAXTableL0,
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_U8  * pRMAXTableL1,
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong              const OMX_U8  * pZigzagTable
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong)
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong{
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U32 storeRun = 0, run, storeRunPlus;
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8  last = 0, first = 1, fMode;
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16 level, storeLevel = 0, storeLevelPlus;
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_INT i;
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* RLE encoding and packing the bits into the streams */
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (i = start, run=0; i < 64; i++)
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong            level   = pQDctBlkCoef[pZigzagTable[i]];
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Counting the run */
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (level == 0)
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong                run++;
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* Found a non-zero coeff */
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong            else
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (first == 0)
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong                    last = 0;
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong                    /* Check for a valid entry in the VLC table */
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong                    storeLevelPlus = armSignCheck(storeLevel) *
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong                      (armAbs(storeLevel) - pLMAXTableL0[storeRun]);
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong                    storeRunPlus = storeRun -
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong                                  (pRMAXTableL0[armAbs(storeLevel) - 1] + 1);
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong                    fMode = armVCM4P2_CheckVLCEscapeMode(
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             storeRun,
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             storeRunPlus,
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             storeLevel,
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             storeLevelPlus,
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             maxStoreRunL0,
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             maxRunForMultipleEntriesL0,
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             shortVideoHeader,
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             pRunIndexTableL0);
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong                    armVCM4P2_FillVLCBuffer (
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      ppBitStream,
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pBitOffset,
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      storeRun,
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      storeLevel,
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong									  storeRunPlus,
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      storeLevelPlus,
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      fMode,
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong									  last,
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      maxRunForMultipleEntriesL0,
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pRunIndexTableL0,
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong                                      pVlcTableL0);
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong                storeLevel = level;
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong                storeRun   = run;
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong                first = 0;
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong                run = 0;
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong        } /* end of for loop for 64 elements */
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* writing the last element */
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong        last = 1;
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Check for a valid entry in the VLC table */
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong        storeLevelPlus = armSignCheck(storeLevel) *
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong                        (armAbs(storeLevel) - pLMAXTableL1[run]);
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong        storeRunPlus = storeRun -
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong                      (pRMAXTableL1[armAbs(storeLevel) - 1] + 1);
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong        fMode = armVCM4P2_CheckVLCEscapeMode(
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 storeRun,
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 storeRunPlus,
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 storeLevel,
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 storeLevelPlus,
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 maxStoreRunL1,
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 maxRunForMultipleEntriesL1,
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 shortVideoHeader,
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong                                 pRunIndexTableL1);
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong        armVCM4P2_FillVLCBuffer (
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong                          ppBitStream,
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong                          pBitOffset,
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong                          storeRun,
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong                          storeLevel,
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong						  storeRunPlus,
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong                          storeLevelPlus,
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong                          fMode,
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong						  last,
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong                          maxRunForMultipleEntriesL1,
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong                          pRunIndexTableL1,
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong                          pVlcTableL1);
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong	return OMX_Sts_NoErr;
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong
2150c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of File */
216