10c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
20c1bc742181ded4930842b46e9507372f0b1b963James Dong *
30c1bc742181ded4930842b46e9507372f0b1b963James Dong * File Name:  omxVCM4P2_EncodeVLCZigzag_IntraDCVLC.c
40c1bc742181ded4930842b46e9507372f0b1b963James Dong * OpenMAX DL: v1.0.2
50c1bc742181ded4930842b46e9507372f0b1b963James Dong * Revision:   9641
60c1bc742181ded4930842b46e9507372f0b1b963James Dong * Date:       Thursday, February 7, 2008
70c1bc742181ded4930842b46e9507372f0b1b963James Dong *
80c1bc742181ded4930842b46e9507372f0b1b963James Dong * (c) Copyright 2007-2008 ARM Limited. All Rights Reserved.
90c1bc742181ded4930842b46e9507372f0b1b963James Dong *
100c1bc742181ded4930842b46e9507372f0b1b963James Dong *
110c1bc742181ded4930842b46e9507372f0b1b963James Dong *
120c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
130c1bc742181ded4930842b46e9507372f0b1b963James Dong * Contains modules for zigzag scanning and VLC encoding
140c1bc742181ded4930842b46e9507372f0b1b963James Dong * for intra block.
150c1bc742181ded4930842b46e9507372f0b1b963James Dong *
160c1bc742181ded4930842b46e9507372f0b1b963James Dong */
170c1bc742181ded4930842b46e9507372f0b1b963James Dong
180c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxtypes.h"
190c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armOMX.h"
200c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "omxVC.h"
210c1bc742181ded4930842b46e9507372f0b1b963James Dong
220c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVC.h"
230c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM_Bitstream.h"
240c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armCOMM.h"
250c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVCM4P2_Huff_Tables_VLC.h"
260c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "armVCM4P2_ZigZag_Tables.h"
270c1bc742181ded4930842b46e9507372f0b1b963James Dong
280c1bc742181ded4930842b46e9507372f0b1b963James Dong
290c1bc742181ded4930842b46e9507372f0b1b963James Dong
300c1bc742181ded4930842b46e9507372f0b1b963James Dong/**
310c1bc742181ded4930842b46e9507372f0b1b963James Dong * Function:  omxVCM4P2_EncodeVLCZigzag_IntraDCVLC   (6.2.4.5.2)
320c1bc742181ded4930842b46e9507372f0b1b963James Dong *
330c1bc742181ded4930842b46e9507372f0b1b963James Dong * Description:
340c1bc742181ded4930842b46e9507372f0b1b963James Dong * Performs zigzag scan and VLC encoding of AC and DC coefficients for one
350c1bc742181ded4930842b46e9507372f0b1b963James Dong * intra block.  Two versions of the function (DCVLC and ACVLC) are provided
360c1bc742181ded4930842b46e9507372f0b1b963James Dong * in order to support the two different methods of processing DC
370c1bc742181ded4930842b46e9507372f0b1b963James Dong * coefficients, as described in [ISO14496-2], subclause 7.4.1.4, "Intra DC
380c1bc742181ded4930842b46e9507372f0b1b963James Dong * Coefficient Decoding for the Case of Switched VLC Encoding".
390c1bc742181ded4930842b46e9507372f0b1b963James Dong *
400c1bc742181ded4930842b46e9507372f0b1b963James Dong * Input Arguments:
410c1bc742181ded4930842b46e9507372f0b1b963James Dong *
420c1bc742181ded4930842b46e9507372f0b1b963James Dong *   ppBitStream - double pointer to the current byte in the bitstream
430c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pBitOffset - pointer to the bit position in the byte pointed by
440c1bc742181ded4930842b46e9507372f0b1b963James Dong *            *ppBitStream. Valid within 0 to 7.
450c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pQDctBlkCoef - pointer to the quantized DCT coefficient
460c1bc742181ded4930842b46e9507372f0b1b963James Dong *   predDir - AC prediction direction, which is used to decide the zigzag
470c1bc742181ded4930842b46e9507372f0b1b963James Dong *            scan pattern; takes one of the following values:
480c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -  OMX_VC_NONE - AC prediction not used.
490c1bc742181ded4930842b46e9507372f0b1b963James Dong *                             Performs classical zigzag scan.
500c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -  OMX_VC_HORIZONTAL - Horizontal prediction.
510c1bc742181ded4930842b46e9507372f0b1b963James Dong *                             Performs alternate-vertical zigzag scan.
520c1bc742181ded4930842b46e9507372f0b1b963James Dong *            -  OMX_VC_VERTICAL - Vertical prediction.
530c1bc742181ded4930842b46e9507372f0b1b963James Dong *                             Performs alternate-horizontal zigzag scan.
540c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pattern - block pattern which is used to decide whether this block is
550c1bc742181ded4930842b46e9507372f0b1b963James Dong *            encoded
560c1bc742181ded4930842b46e9507372f0b1b963James Dong *   shortVideoHeader - binary flag indicating presence of
570c1bc742181ded4930842b46e9507372f0b1b963James Dong *            short_video_header; escape modes 0-3 are used if
580c1bc742181ded4930842b46e9507372f0b1b963James Dong *            shortVideoHeader==0, and escape mode 4 is used when
590c1bc742181ded4930842b46e9507372f0b1b963James Dong *            shortVideoHeader==1.
600c1bc742181ded4930842b46e9507372f0b1b963James Dong *   videoComp - video component type (luminance, chrominance) of the current
610c1bc742181ded4930842b46e9507372f0b1b963James Dong *            block
620c1bc742181ded4930842b46e9507372f0b1b963James Dong *
630c1bc742181ded4930842b46e9507372f0b1b963James Dong * Output Arguments:
640c1bc742181ded4930842b46e9507372f0b1b963James Dong *
650c1bc742181ded4930842b46e9507372f0b1b963James Dong *   ppBitStream - *ppBitStream is updated after the block is encoded, so
660c1bc742181ded4930842b46e9507372f0b1b963James Dong *            that it points to the current byte in the bit stream buffer.
670c1bc742181ded4930842b46e9507372f0b1b963James Dong *   pBitOffset - *pBitOffset is updated so that it points to the current bit
680c1bc742181ded4930842b46e9507372f0b1b963James Dong *            position in the byte pointed by *ppBitStream.
690c1bc742181ded4930842b46e9507372f0b1b963James Dong *
700c1bc742181ded4930842b46e9507372f0b1b963James Dong * Return Value:
710c1bc742181ded4930842b46e9507372f0b1b963James Dong *
720c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_NoErr - no error
730c1bc742181ded4930842b46e9507372f0b1b963James Dong *    OMX_Sts_BadArgErr - Bad arguments:
740c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    At least one of the following pointers is NULL: ppBitStream,
750c1bc742181ded4930842b46e9507372f0b1b963James Dong *              *ppBitStream, pBitOffset, pQDctBlkCoef.
760c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -   *pBitOffset < 0, or *pBitOffset >7.
770c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    PredDir is not one of: OMX_VC_NONE, OMX_VC_HORIZONTAL, or
780c1bc742181ded4930842b46e9507372f0b1b963James Dong *         OMX_VC_VERTICAL.
790c1bc742181ded4930842b46e9507372f0b1b963James Dong *    -    VideoComp is not one component of enum OMXVCM4P2VideoComponent.
800c1bc742181ded4930842b46e9507372f0b1b963James Dong *
810c1bc742181ded4930842b46e9507372f0b1b963James Dong */
820c1bc742181ded4930842b46e9507372f0b1b963James Dong
830c1bc742181ded4930842b46e9507372f0b1b963James DongOMXResult omxVCM4P2_EncodeVLCZigzag_IntraDCVLC(
840c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_U8 **ppBitStream,
850c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT *pBitOffset,
860c1bc742181ded4930842b46e9507372f0b1b963James Dong     const OMX_S16 *pQDctBlkCoef,
870c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_U8 predDir,
880c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_U8 pattern,
890c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMX_INT shortVideoHeader,
900c1bc742181ded4930842b46e9507372f0b1b963James Dong     OMXVCM4P2VideoComponent videoComp
910c1bc742181ded4930842b46e9507372f0b1b963James Dong)
920c1bc742181ded4930842b46e9507372f0b1b963James Dong{
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_S16 dcValue, powOfSize;
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U8  DCValueSize, start = 1;
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    OMX_U16 absDCValue;
960c1bc742181ded4930842b46e9507372f0b1b963James Dong
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Argument error checks */
980c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(ppBitStream == NULL, OMX_Sts_BadArgErr);
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(*ppBitStream == NULL, OMX_Sts_BadArgErr);
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pBitOffset == NULL, OMX_Sts_BadArgErr);
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf(pQDctBlkCoef == NULL, OMX_Sts_BadArgErr);
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    armRetArgErrIf((*pBitOffset < 0) || (*pBitOffset >7), OMX_Sts_BadArgErr);
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf((videoComp != OMX_VC_LUMINANCE) && (videoComp != OMX_VC_CHROMINANCE), OMX_Sts_BadArgErr);
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong	armRetArgErrIf((predDir != OMX_VC_NONE) && (predDir != OMX_VC_HORIZONTAL) && (predDir != OMX_VC_VERTICAL) , OMX_Sts_BadArgErr);
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pattern)
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong        dcValue = pQDctBlkCoef[0];
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong        absDCValue = armAbs(dcValue);
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Find the size */
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong        DCValueSize = armLogSize (absDCValue);
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong        absDCValue = armAbs(dcValue);
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Insert the code into the bitstream */
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (videoComp == OMX_VC_LUMINANCE)
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong            armPackVLC32 (ppBitStream, pBitOffset,
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong                          armVCM4P2_aIntraDCLumaIndex[DCValueSize]);
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong        else if (videoComp == OMX_VC_CHROMINANCE)
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong            armPackVLC32 (ppBitStream, pBitOffset,
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong                          armVCM4P2_aIntraDCChromaIndex[DCValueSize]);
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Additional code generation in case of negative
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong           dc value the additional */
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (DCValueSize > 0)
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (dcValue < 0)
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong                /* calulate 2 pow */
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong                powOfSize = (1 << DCValueSize);
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong                absDCValue =  absDCValue ^ (powOfSize - 1);
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong            armPackBits(ppBitStream, pBitOffset, (OMX_U32)absDCValue, \
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong                        DCValueSize);
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (DCValueSize > 8)
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong                armPackBits(ppBitStream, pBitOffset, 1, 1);
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong    return armVCM4P2_EncodeVLCZigzag_Intra(
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong                ppBitStream,
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong                pBitOffset,
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong                pQDctBlkCoef,
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong                predDir,
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong                pattern,
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong                shortVideoHeader,
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong                start);
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong/* End of file */
161