⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpeg_bitsbuffer.c

📁 ADI blackfin DSP的基于device friver的jpeg压缩算法
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
Copyright(c) 2005 Analog Devices, Inc.  All Rights Reserved. This software is 
proprietary and confidential to Analog Devices, Inc. and its licensors.
******************************************************************************

$RCSfile: JPEG_BitsBuffer.c,v $
$Revision: 1.1 $
$Date: 2006/07/17 07:44:02 $

Project:	JPEG IMAGE CODEC
Title:		Example BitsBuffer module
Author(s):	D.G, A.S, P.G
Revised by:

Description:
Bitstream Data Buffer Managing Module

References:

******************************************************************************
Tab Setting:			4
Target Processor:		Blackfin
Target Tools Revision:	VDSP++ 3.5
******************************************************************************

Modification History:
====================
$Log: JPEG_BitsBuffer.c,v $
Revision 1.1  2006/07/17 07:44:02  bmk
JPEG-MJPEG User access files


******************************************************************************/

#include <stdlib.h>				// For NULL
#include <stdio.h>              // For fread()
#include <string.h>             // For memset()

#include "IMG_Common.h"             // common JPEG definitions
#include "JPEG_api_common.h"		//
#include "JPEG_memalloc.h"		    // User defined function prototypes
#include "JPEG_bitsbuffer.h"		// User defined typedefs
#define JPEGBUFFERVALIDATED
#define JPEGBUFFER
/* Bits Buffer functions: */
 tBitsBuffer      *JPEG_BitsBuffer_NEW(void);
 int              JPEG_BitsBuffer_CONFIG(tBitsBuffer *handle, eBitsBuf_config item, unsigned int value);
 int              JPEG_BitsBuffer_INIT(tBitsBuffer *handle);
 void             JPEG_BitsBuffer_DELETE(tBitsBuffer *handle);
 int              JPEG_BitsBuffer_RESET(tBitsBuffer *handle);

/* Encoder Bits Buffer functions: */
 unsigned char    *JPEG_BitsBuffer_REQUEST(tBitsBuffer *handle);
 int              JPEG_BitsBuffer_WRITE(tBitsBuffer *handle, int numBytes);
 int              JPEG_BitsBuffer_FLUSH(tBitsBuffer *handle);

/* Decoder Bits Buffer functions: */
 int				JPEG_BitsBuffer_READ (tBitsBuffer *handle);
// extern unsigned char	*JPEG_BitsBuffer_PROCESS (tBitsBuffer *handle);
 unsigned char    *JPEG_BitsBuffer_PROCESS(tBitsBuffer *handle, int *pNumBytes);
 void				JPEG_BitsBuffer_RELEASE (tBitsBuffer *handle);


#if USE_DMA
#include "BitsBufferDMA.h"		// DMA prototypes
#endif // USE_DMA

#define BITSTREAM_FILE_BUFFER_SIZE 1000000 /* approx 1 MB */
#define DECODER_SINGLE_BUFFERED /* This implementation is ONLY single buffered for DECODER */
/* The double buffer mode is currently broken for the decoder */

/****************************************************************************
 *		Local variables													    *
 ****************************************************************************/
/* Bitstream Constants: */

//static const int    BITS_BUF_DESBUF_SIZE = 1024;    /* Size of each buffer */
#define    BITS_BUF_DESBUF_SIZE  1024  
/****************************************************************************
 * 		Function Declarations												*
 ****************************************************************************/


/*
********************************************************************************
** Function:		JPEG_BitsBuffer_NEW
**
** Description:		Creates a new instance of Bit Stream buffer object.
**                  Used in JPEG ENCODER & DECODER.
**
** Arguments:		None
**
** Outputs:
**
** Return value:	Handle to new Bit Stream buffer instance
********************************************************************************
*/
tBitsBuffer *JPEG_BitsBuffer_NEW(void)
{
	tBitsBuffer		*bitsbuf = NULL;
	MemObjHandle    *MemObj;

    // Note: previous code had the TempBuffers (PROGRESSIVE) being 1024 large, and the
    // LOSSLESS buffers being dependent on the following formula:
    //	lTemp = (int32)(imageEncParam->maxXDimension * 1.5);
    //	if(lTemp < 2048)
    //		lTemp = 2048;

	MemObj = JPEG_MemAlloc_NEW(1, sizeof(tBitsBuffer), MEM_TYPE_OBJECT);
	if (MemObj==NULL)
		return NULL;

	bitsbuf = (tBitsBuffer *)JPEG_MemAlloc_ADDRESS(MemObj);

	// Reset bitsbuffer to zero - good for second and subsequent calls
	memset (bitsbuf,  0, sizeof(tBitsBuffer));

    // Save pointer to the tBitsBuffer's own memory allocation object
	bitsbuf->ThisMemObj = MemObj;

	MemObj = NULL;
	bitsbuf->desBuffer1 = NULL;

	MemObj = JPEG_MemAlloc_NEW(1, BITS_BUF_DESBUF_SIZE * sizeof(uint8), MEM_TYPE_DATA);
	if (MemObj==NULL)
	{
    // Deallocate previously allocated memory
		JPEG_MemAlloc_DELETE(bitsbuf->ThisMemObj);
		return NULL;
	}

	bitsbuf->desBuffer1 = (uint8 *)JPEG_MemAlloc_ADDRESS(MemObj);

    // Save pointer to desBuffer1's memory allocation object
	bitsbuf->BufferMemObjs[0] = MemObj;

	MemObj = NULL;
	bitsbuf->desBuffer2 = NULL;

	MemObj = JPEG_MemAlloc_NEW(1, BITS_BUF_DESBUF_SIZE * sizeof(uint8), MEM_TYPE_DATA);
	if (MemObj==NULL)
	{
    // Deallocate previously allocated memory
		JPEG_MemAlloc_DELETE(bitsbuf->BufferMemObjs[0]);
		JPEG_MemAlloc_DELETE(bitsbuf->ThisMemObj);
		return NULL;
	}

	bitsbuf->desBuffer2 = (uint8 *)JPEG_MemAlloc_ADDRESS(MemObj);

    // Save pointer to desBuffer1's memory allocation object
	bitsbuf->BufferMemObjs[1] = MemObj;


	return bitsbuf;
}

/*
********************************************************************************
** Function:		JPEG_BitsBuffer_DELETE
**
** Description:		This function deletes the Bit Stream buffer object.
**                  Used in JPEG ENCODER & DECODER.
**
** Arguments:
**	bitsbuf [IN]	Handle to the Bit Stream data access instance.
**
** Outputs:
**
** Return value:	None
********************************************************************************
*/
void JPEG_BitsBuffer_DELETE (tBitsBuffer *bitsbuf)
{
	// Deallocate buffer memory objects
	JPEG_MemAlloc_DELETE(bitsbuf->BufferMemObjs[1]);
	JPEG_MemAlloc_DELETE(bitsbuf->BufferMemObjs[0]);

	// Deallocate Bit Stream Buffer memory object
//	if(!bitsbuf->is_encoder) /* only for decoder */
//    {
//     	JPEG_MemAlloc_DELETE(bitsbuf->DecodeBitStreamMemObj);
//    }

	// Deallocate tBitsBuffer memory object last
	JPEG_MemAlloc_DELETE(bitsbuf->ThisMemObj);
}

/*
********************************************************************************
** Function:		JPEG_BitsBuffer_CONFIG
**
** Description:		This function configures the instance of a Bit Stream
**					buffer object with the given configuration value.
**                  Called in both JPEG ENCODER & DECODER, but only has
**                  functionality in Encoder at this stage.
**
** Arguments:
**	bitsbuf [IN]	Handle to the Bit Stream data access instance.
**	item [IN]		item to configure.
**	value[IN]		Configuration value.
**
** Outputs:
**
** Return value:	1 if successful; 0 otherwise
********************************************************************************
*/
int JPEG_BitsBuffer_CONFIG(tBitsBuffer *bitsbuf, eBitsBuf_config item, unsigned int value)
{
	switch(item)
	{
		case BITSBUF_ENCODER:
			bitsbuf->is_encoder = value;
			break;

		case BITSBUF_POINTER:
		    /* Encoder instance uses this: */
			bitsbuf->externalBuffer = (unsigned char *)value;

			/* Decoder instance uses this: */
			bitsbuf->ptrFile = (tIMG_BufferInfo *)value;
			break;

		default:
			return 0;
	}

	return 1;
}

/*
********************************************************************************
** Function:		JPEG_BitsBuffer_INIT
**
** Description:		This function initialises the instance of an bit stream data
**					access object, allocating any memory. JPEG_BitsBuffer_CONFIG
**					must have been called before calling this function.
**                  Used in JPEG ENCODER & DECODER.
**
** Arguments:
**	bitsbuf [IN]	Handle to the Bit Stream data access instance.
**
** Outputs:
**
** Return value:	1 if successful; 0 otherwise
********************************************************************************
*/
int JPEG_BitsBuffer_INIT(tBitsBuffer *bitsbuf)
{
	MemObjHandle    *MemObj;
	int     error_code=0;

	if(bitsbuf->is_encoder) /* Encoder specific initialisation */
	{
    	bitsbuf->numberOfBuffers = 2; /* double buffering scheme */

    	error_code=1;
    }
    else /* Decoder specific initialisation */
	{
    	bitsbuf->numberOfBuffers = 1; /* single buffering scheme */

//    	MemObj = JPEG_MemAlloc_NEW(1, (BITSTREAM_FILE_BUFFER_SIZE+4) * sizeof(uint8), MEM_TYPE_DATA);
//    	if (MemObj!=NULL)
//    	{
//
//        	bitsbuf->InputBitstreamFile = (uint8 *)JPEG_MemAlloc_ADDRESS(MemObj);
//
//            // Save pointer to desBuffer1's memory allocation object
//        	bitsbuf->DecodeBitStreamMemObj = MemObj;
//
//        	bitsbuf->BitStreamFileSize = fread(bitsbuf->InputBitstreamFile, 1, BITSTREAM_FILE_BUFFER_SIZE, bitsbuf->ptrFile);
//
//            if (!feof(bitsbuf->ptrFile))
//            {
//                printf("Input file is too big!\n");
//            }
//            else
//                 error_code=1;
//        }
        bitsbuf->BitStreamFileSize = bitsbuf->ptrFile->Length;
        bitsbuf->InputBitstreamFile = bitsbuf->ptrFile->Pointer;
        error_code=1;
    }

	return error_code;
}

/*
********************************************************************************
** Function:		JPEG_BitsBuffer_RESET
**
** Description:		This function resets the instance of a bit stream data access
**					object, setting all fields to default starting values.
**                  Used in JPEG ENCODER & DECODER.
** Arguments:
**	bitsbuf [IN]	Handle to the Bit Stream data access instance.
**
** Outputs:
**
** Return value:	1 if successful; 0 otherwise
********************************************************************************
*/
int JPEG_BitsBuffer_RESET(tBitsBuffer *bitsbuf)
{
	/* Encoder-only: */
	bitsbuf->dmaInitiateThreshold = 256;
	bitsbuf->externalBufferIndex = 0;

	bitsbuf->numberOfEmptyBuffers = bitsbuf->numberOfBuffers;
	bitsbuf->writeBufferCount = 0;
	bitsbuf->nextBuffer = 1;			// to select a new buffer
	bitsbuf->firstOrsecondBuffer = 1;	// first new buffer will be 0.

	bitsbuf->numBytes = 0;

	/* Decoder-only: */
	bitsbuf->end_of_file = 0;

	bitsbuf->numBytesBuf_1 = 0;
	bitsbuf->numBytesBuf_2 = 0;

    bitsbuf->InputFilePosition = 0;

	return 1;
}

/*
********************************************************************************
** Function:		JPEG_BitsBuffer_REQUEST
**
** Description:		This function is used to obtain the next available Bit
**					Stream buffer about to be filled by the processed output
**					data.
**                  Used in JPEG ENCODER only.
** Arguments:
**	bitsbuf [IN]	Handle to the Bit Stream data access instance.
**
** Outputs:
**
** Return value:	Pointer to a empty data buffer ready for storing the
**					processing output.
********************************************************************************
*/
#if USE_DMA
unsigned char *JPEG_BitsBuffer_REQUEST (tBitsBuffer *bitsbuf)
{
	unsigned char	*lPointer;

	if(bitsbuf->nextBuffer == 1)
	{


			bitsbuf->firstOrsecondBuffer++;
			if(bitsbuf->firstOrsecondBuffer >= bitsbuf->numberOfBuffers)
			{
				bitsbuf->firstOrsecondBuffer = 0;
			}

			bitsbuf->nextBuffer = 0;


// wait until buffer is ready...
// only need to check once 'numberOfBuffers' (ie 2) have been read.
// the first buffers are assumed to be "empty".
			if (bitsbuf->writeBufferCount >= bitsbuf->numberOfBuffers)
				dma_done(bitsbuf->dmaIDs[bitsbuf->firstOrsecondBuffer]);

	}

	if(bitsbuf->firstOrsecondBuffer == 0)
	{
		lPointer = bitsbuf->desBuffer1;
	}
	else
	{
		lPointer = bitsbuf->desBuffer2;
	}

	lPointer = lPointer + bitsbuf->numBytes;

	return(lPointer);
}
#else
#ifdef JPEGBUFFERVALIDATED
asm (".section program;\n"
".align 8;\n"
"_JPEG_BitsBuffer_REQUEST:\n"
"		P0 = R0; NOP; NOP; R3 = 1; \n"
"		R0 = 0; R2 = [P0 + 20];\n"
"		CC = R2 == R3;\n"
"		IF !CC JUMP BBRQ1;\n"
"		R1 = [P0 + 28];\n"
"		CC = R1 <= 0;\n"
"		IF CC JUMP  BBRQ7;\n"
"		R1 = R1 - R2 (NS) || R3 = [P0 + 16];\n"
"		R3 = R3 + R2 (NS) || R0 = [P0 + 24];\n"
"		CC = R0 <= R3;\n"
"		R0= R0 -|- R0 || [P0 + 28] = R1;\n"
"		[P0 + 20] = R0;\n"
"		IF !CC R0 = R3;\n"
"		[P0 + 16] = R0;\n"
"BBRQ1: R0 = [P0 + 16];\n"
"		CC = R0;\n"
"		R1 = [P0 + 8];\n"
"		R2 = [P0 + 4];\n"
"		R0 = [P0 + 36];\n"
"		IF CC R2 = R1;\n"
"		R0 = R2 + R0;\n"
"BBRQ7:	RTS;\n"
".global _JPEG_BitsBuffer_REQUEST;\n");
#else
unsigned char *JPEG_BitsBuffer_REQUEST (tBitsBuffer *bitsbuf) {	
	unsigned char	*lPointer;
	if(bitsbuf->nextBuffer == 1) {
		if(bitsbuf->numberOfEmptyBuffers > 0) {
			bitsbuf->numberOfEmptyBuffers--;
			bitsbuf->firstOrsecondBuffer++;
			if(bitsbuf->firstOrsecondBuffer >= bitsbuf->numberOfBuffers)			
				bitsbuf->firstOrsecondBuffer = 0;			
			bitsbuf->nextBuffer = 0;
		}	else	return (NULL);		
	}
	if(bitsbuf->firstOrsecondBuffer == 0)		lPointer = bitsbuf->desBuffer1;	
	else		lPointer = bitsbuf->desBuffer2;
	lPointer = lPointer + bitsbuf->numBytes;
	return(lPointer);
}
#endif
#endif
/*
********************************************************************************
** Function:		JPEG_BitsBuffer_WRITE
**
** Description:		This function is used to empty a data buffer after
**					processing.
**                  Used in JPEG ENCODER only.
**
** Arguments:
**	bitsbuf [IN]	Handle to the Bit Stream data access instance.
**	numBytes [IN]	Number of Bytes written into the buffer.
**
** Outputs:
**
** Return value:	E_TRUE or E_FALSE
********************************************************************************
*/
#if USE_DMA
int JPEG_BitsBuffer_WRITE (tBitsBuffer *bitsbuf, int numBytes)
{
	int		lCount;

	bitsbuf->numBytes += numBytes;

	if(bitsbuf->numBytes > bitsbuf->dmaInitiateThreshold)
	{
		if(bitsbuf->firstOrsecondBuffer == 0)
		{
			bitsbuf->dmaIDs[bitsbuf->firstOrsecondBuffer] = DMA_BitsBuffer(&bitsbuf->externalBuffer[bitsbuf->externalBufferIndex],
				bitsbuf->desBuffer1, bitsbuf->numBytes);
			bitsbuf->externalBufferIndex += bitsbuf->numBytes;
		}
		else
		{
			bitsbuf->dmaIDs[bitsbuf->firstOrsecondBuffer] = DMA_BitsBuffer(&bitsbuf->externalBuffer[bitsbuf->externalBufferIndex],
				bitsbuf->desBuffer2, bitsbuf->numBytes);
			bitsbuf->externalBufferIndex += bitsbuf->numBytes;
		}
		bitsbuf->numBytes = 0;
		bitsbuf->nextBuffer = 1;
		bitsbuf->writeBufferCount++;

		bitsbuf->numberOfEmptyBuffers++;
	}

	return (E_TRUE);
}
#else
#ifdef JPEGBUFFERVALIDATED
asm(".section program;\n"
".align 8;\n"
"_JPEG_BitsBuffer_WRITE:\n"
"		P0 = R0;\n"
"		R0 = 1; NOP;\n"
"		[--SP] = (P5:4);\n"
"		R2 = [P0 + 36];\n"
"		R2 = R2 + R1 (NS) || R1 = [P0 + 40];\n"
"		[P0 + 36] = R2;\n"
"		CC = R1 < R2;\n"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -