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