📄 omxicjp_decodehuffman8x8_direct_s16_c1.c
字号:
/**** * File Name: omxICJP_DecodeHuffman8x8_Direct_S16_C1.c* OpenMAX DL: v1.0.2* Revision: 10586* Date: Wednesday, March 5, 2008* * (c) Copyright 2007-2008 ARM Limited. All Rights Reserved.* * ** Description : Huffman Decoding function.**/#include "omxtypes.h"#include "armOMX.h"#include "omxIC.h"#include "armCOMM.h"#include "armCOMM_Bitstream.h"#include "armICJP_Bitstream.h"#include "armIC.h"OMXResult armICJP_ExtendDiffCategory(OMX_INT *pdiffVal, OMX_U16 diffCategory);/*** Function: omxICJP_DecodeHuffman8x8_Direct_S16_C1 (5.1.3.5.3)** Description:* Implements the JPEG baseline Huffman decoder. Decodes an 8x8 block of * quantized DCT coefficients using the tables referenced by the parameters * pDCHuffTable and pACHuffTable in accordance with the Huffman decoding * procedure defined in [ISO10918-1], Annex F.2.2, Baseline Huffman Decoding * Procedures. If a JPEG marker is detected during decoding, the function * stops decoding and writes the marker to the location indicated by pMarker. * The decoded data is inverse zigzag scanned to produce the raster scanned * output buffer (8x8). The DC coefficient prediction parameter pDCPred should * be set to 0 during initialization and after every restart interval. The * parameter pMarker should set to 0 during initialization or after the found * marker has been processed. The parameter pNumValidPrefetchedBits should be * set to 0 in the following cases: 1) during function initialization, 2) * after each restart interval, and 3) after each found marker has been * processed. The parameter pPrefetchedBits should be set to 0 during * function initialization. ** Input Arguments:* * pSrc - pointer to the to the first byte of the input JPEG bitstream * buffer both upon function entry and upon function return, i.e., * the function does not modify the value of the pointer. The * location of the first available bit in the buffer is indexed by * the parameter pSrcDstBitsLen. For a buffer of non zero length, * the value of the first bit accessed during the Huffman block * deecode operation is given by * (pSrc[currentByte]>>currentBit)&0x1, where * currentByte=(pSrcDstBitsLen 1)>>3, and * currentBit=7-((pSrcDstBitsLen-1)&0x7). Within each byte, bits * are consumed from most significant to least significant. The * buffer contents should be formatted in accordance with CCITT * T.81, and the pointer pSrc must be aligned on a 4-byte boundary. * pDCHuffTable - pointer to the OMXICJPHuffmanDecodeSpec structure * containing the DC Huffman decoding table; must be aligned on a * 4-byte boundary. * pACHuffTable - pointer to the OMXICJPHuffmanDecodeSpec structure * containing the AC Huffman decoding table; must be aligned on a * 4-byte aligned. * pSrcDstBitsLen - pointer to the current bit position indicator for the * input buffer (pSrc). This parameter informs the Huffman decoder * of where to start reading input bits for the current block since * the start of the current block may not necessarily be positioned * at the start of the input buffer. The parameter pSrcDstBitsLen * indicates the offset in terms of bits of the current bit * relative to pSrc. Updated upon return as described below under * Output Arguments . There is no restriction on buffer length. * It is the responsibility of the caller to maintain the Huffman * buffer and limit the buffer length as appropriate for the target * application or environment. The parameter pSrcDstBitsLen must * be aligned on a 4-byte boundary. * pDCPred - pointer to the DC prediction coefficient. Upon input contains * the quantized DC coefficient decoded from the most recent block. * Should be set to 0 upon function initialization and after each * restart interval. Updated upon return as described below under * Output Arguments. * pMarker - pointer to the most recently encountered marker. The caller * should set this parameter to 0 during function initialization * and after a found marker has been processed. Updated upon * return as described below under Output Arguments. * pPrefetchedBits - implementation-specific pre-fetch parameter; should be * set to 0 during function initialization. This parameter shall * not affect the value of the parameter pSrcDstBitsLen, i.e., the * location of the next bit in the stream shall be fully determined * by combining the parameters pSrc and pSrcDstBitsLen as described * above under the description for the parameter pSrc. * pNumValidPrefetchedBits - implementation-specific pre-fetch parameter * that points to the number of valid bits in the pre-fetch buffer; * should be set to 0 upon input under the following conditions: * 1) function initialization, 2) after each restart interval, 3) * after each found marker has been processed. This parameter * shall not affect the value of the parameter pSrcDstBitsLen, * i.e., the location of the next bit in the stream shall be fully * determined by combining the parameters pSrc and pSrcDstBitsLen * as described above under the description for the parameter pSrc. ** Output Arguments:* * pDst - pointer to the output buffer; must be aligned on a 32-byte * boundary. * pSrcDstBitsLen - pointer to the updated value of the bit index for the * input buffer (pSrc); informs the caller of where the Huffman * decoder stopped reading bits for the current block. The value * *pSrcDstBitsLen is modified by the Huffman decoder such that it * indicates upon return the offset in terms of bits of the * current bit relative to pSrc after block decoding has been * completed. Usage guidelines apply as described above under * Input Arguments. * pDCPred - pointer to the DC prediction coefficient. Returns the * quantized value of the DC coefficient from the current block. * pMarker - pointer to the most recently encountered marker. If a marker * is detected during decoding, the function stops decoding and * returns the encountered marker using this parameter; returned * value should be preserved between calls to the decoder or reset * prior to input as described above under Input Arguments. * pPrefetchedBits - implementation-specific pre-fetch parameter; returned * value should be preserved between calls to the decoder or reset * prior to input as described above under Input Arguments. * pNumValidPrefetchedBits - pointer to the number of valid bits in the * pre-fetch buffer; returned value should be preserved between * calls to the decoder or reset prior to input as described above * under Input Arguments. ** Return Value:* * OMX_Sts_NoErr - no error. * OMX_Sts_Err - error, illegal Huffman code encountered in the input * bitstream. * OMX_StsICJP_JPEGMarkerErr - JPEG marker encountered within an * entropy-coded block; Huffman decoding terminated early. * Marker value returned by the parameter *pMarker. * OMX_StsICJP_JPEGMarker - JPEG marker encountered before any Huffman * symbols; Huffman decoding terminated early. Marker value * returned by the parameter *pMarker. * OMX_Sts_BadArgErr - bad arguments; returned under any of the following * conditions: * - a pointer was NULL * - *pSrcBitsLen was less than 0. * - *pNumValidPrefetchedBits was less than 0. * - the start address of pDst was not 32-byte aligned. **/OMXResult omxICJP_DecodeHuffman8x8_Direct_S16_C1( const OMX_U8 *pSrc, OMX_INT *pSrcDstBitsLen, OMX_S16 *pDst, OMX_S16 *pDCPred, OMX_INT *pMarker, OMX_U32 *pPrefetchedBits, OMX_INT *pNumValidPrefetchedBits, const OMXICJPHuffmanDecodeSpec *pDCHuffTable, const OMXICJPHuffmanDecodeSpec *pACHuffTable ){ const OMX_U8 *pBitStream, *pBitStreamRef; const OMX_U8 **ppBitStream = &pBitStream; OMX_INT offset, *pOffset = &offset; OMX_INT numBytesRead, numBitsRead; OMX_INT diffVal, i, zeroRunLen = 0; OMX_INT BitsRead; OMX_U16 diffCategory, huffcodeVal; armICJP_DecHuffmanSpec *pDCHuffSpec = (armICJP_DecHuffmanSpec *)pDCHuffTable; ARM_VLC32 *pDCHuffman = pDCHuffSpec->pHuffStruct; armICJP_DecHuffmanSpec *pACHuffSpec = (armICJP_DecHuffmanSpec *)pACHuffTable; ARM_VLC32 *pACHuffman = pACHuffSpec->pHuffStruct; OMXResult retVal = OMX_Sts_NoErr; armRetArgErrIf(!pSrc, OMX_Sts_BadArgErr); armRetArgErrIf(!pDst, OMX_Sts_BadArgErr); armRetArgErrIf(!pDCPred, OMX_Sts_BadArgErr); armRetArgErrIf(!pMarker, OMX_Sts_BadArgErr); armRetArgErrIf(!pSrcDstBitsLen, OMX_Sts_BadArgErr); armRetArgErrIf(!pDCHuffTable, OMX_Sts_BadArgErr); armRetArgErrIf(!pACHuffTable, OMX_Sts_BadArgErr); armRetArgErrIf(!pPrefetchedBits, OMX_Sts_BadArgErr); armRetArgErrIf(*pSrcDstBitsLen < 0, OMX_Sts_BadArgErr); armRetArgErrIf(!pNumValidPrefetchedBits, OMX_Sts_BadArgErr); armRetArgErrIf(*pNumValidPrefetchedBits < 0, OMX_Sts_BadArgErr); armRetArgErrIf(armNot4ByteAligned(pDst), OMX_Sts_BadArgErr); pBitStream = pSrc + (*pSrcDstBitsLen)/8; pBitStreamRef = pBitStream; offset = *pSrcDstBitsLen % 8; { const OMX_U8 *pBitStream1 = *ppBitStream; int offset1 = *pOffset; int val, bitRem; bitRem = (8 - (*pOffset)) & 7; val = armICJP_ReadValidBits(ppBitStream, pOffset, bitRem,&BitsRead); BitsRead=0; armICJP_ReadValidBits(ppBitStream, pOffset, 8,&BitsRead); if(val == (0xFF >> (8 - bitRem)) && BitsRead < 8) { /* Marker found return marker and update the bitstream pointer */ *pMarker = (*ppBitStream)[1]; numBytesRead = (OMX_INT)(pBitStream - pBitStreamRef); numBitsRead = (numBytesRead * 8) + offset - (*pSrcDstBitsLen % 8); *pSrcDstBitsLen += numBitsRead; return OMX_StsICJP_JPEGMarker; } else { /* No markers found, restore the bitStream */ *ppBitStream = pBitStream1; *pOffset = offset1; } } /* Continue decoding Huffman code */ diffCategory = armICJP_UnPackVLC16(ppBitStream, pOffset, pDCHuffman); if( diffCategory == ARM_JPEGMarkerErr) { return OMX_StsICJP_JPEGMarkerErr; } if(diffCategory == ARM_NO_CODEBOOK_INDEX) { return OMX_Sts_Err; } if(diffCategory == 0) { diffVal = 0; } else { diffVal = armICJP_ReadEntropyBits(ppBitStream, pOffset, diffCategory); if( diffVal == 0xFFFFFFFF) { return OMX_StsICJP_JPEGMarkerErr; } /* Check for marker Error*/ armICJP_ExtendDiffCategory(&diffVal, diffCategory); } /* Set the DC Value */ pDst[0] = *pDCPred + diffVal; *pDCPred = pDst[0]; /* Decode AC Values */ for(i = 1; i < ARM_ICJP_BLOCKSIZE; i++) { pDst[i] = 0; } for(i = 1; i < ARM_ICJP_BLOCKSIZE; i++) { huffcodeVal = armICJP_UnPackVLC16(ppBitStream, pOffset, pACHuffman); if( huffcodeVal == ARM_JPEGMarkerErr) { return OMX_StsICJP_JPEGMarkerErr; } if(huffcodeVal == ARM_NO_CODEBOOK_INDEX) { return OMX_Sts_Err; } diffCategory = huffcodeVal & 0x0f; zeroRunLen = (huffcodeVal >> 4) & 0x0f; if(diffCategory == 0) { if(zeroRunLen != 15) { break; } i += 15; } else { i += zeroRunLen; if(i >= ARM_ICJP_BLOCKSIZE) { break; } diffVal = armICJP_ReadEntropyBits(ppBitStream, pOffset, diffCategory); if( diffVal == 0xFFFFFFFF) { return OMX_StsICJP_JPEGMarkerErr; } /* Check for marker Error*/ armICJP_ExtendDiffCategory(&diffVal, diffCategory); pDst[armICJP_ZigZagTable[i]] = diffVal; } } numBytesRead = (OMX_INT)(pBitStream - pBitStreamRef); numBitsRead = (numBytesRead * 8) + offset - (*pSrcDstBitsLen % 8); *pSrcDstBitsLen += numBitsRead; return retVal;}/*** Function: armICJP_ExtendDiffCategory** Description:* This function takes in the difference category and the additional bits that* are used to completely represent the Huffman encoded value and returns the* actual value that was represented by these two together.** Parameters:* [in] diffCategory The difference category of the Huffman code.* [in] pdiffVal The additional bits as read from the Huffman Stream.* [out] pdiffVal The final value decoded, after sign adjustments.* * Return Value:* Standard OMXResult result. See enumeration for possible result codes.**/OMXResult armICJP_ExtendDiffCategory( OMX_INT *pdiffVal, OMX_U16 diffCategory ){ OMX_INT minPositiveVal = 1 << (diffCategory - 1); /* ----------------------------------------------------------------------- This functionality is described by the JPEG Standard [ISO10918] in the Figure F.12: Extending the sign bit of a decoded value in V. ----------------------------------------------------------------------- */ if(*pdiffVal < minPositiveVal) { *pdiffVal += ((0xffffffff << diffCategory) + 1); } return OMX_Sts_NoErr;}/* End of file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -