jencode.c

来自「是一个手机功能的模拟程序」· C语言 代码 · 共 663 行 · 第 1/2 页

C
663
字号
	}
	else
	{
		pJpegInfo->m_pBuf		+= size;
		pJpegInfo->m_BufSize	-= size;
		*pTotalSize				+= size;
	}
	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	prvInitAnyEncode - Initialize JPEG hardware for any JPEG encode capture
//						This normally only needs to happen once for many
//						images of the same size and position.
//
//	Input:	pJpegInfo	pointer to JPEGINFO structure
//
//	Output:	TRUE means success
//			FALSE means NOT success
//
//-----------------------------------------------------------------------------
Boolean prvInitAnyEncode( JPEGINFO* pJpegInfo )
{
	Boolean fSuccess = TRUE;
	UInt16	width;
	UInt16	height;
	UInt8	value;

	if (pJpegInfo->m_fMotionJPEG)
	{
		halWriteReg8( REG0250_MJPEG, 0);	//enable JPEG header for the 1st frame.

		// Even when not in View mode, we need to turn on Repeat Capture Enable (REG[0108h-b2]) and trigger it with Capture Single Frame (REG[0108h-b1]).
		value = halReadReg8( REG0108_CMCONTROL );
		//76543210b
		//|||||||+--CMCLK Output Hold Reset
		//||||||+---Capture Frame
		//|||||+----Repeat Capture Frame
		//||||+-----Camera Capture Stop
		//|||+------Camera Data Type
		//+++-------n/a
		value |= 0x06;
		halWriteReg8( REG0108_CMCONTROL, value );
	}

	// Enable Encode Scaled Overlay
	if ( pJpegInfo->m_fSOE )
		halWriteReg8( REG004E_SCALEUP, (UInt8)(halReadReg8(REG004E_SCALEUP)|0x80) );

	// If encoding from memory, enable Encode from RYC
	if ( pJpegInfo->m_MemEnc || pJpegInfo->m_fSOE )
		halWriteReg8( REG0210_RESIZEOP, (UInt8)(halReadReg8(REG0210_RESIZEOP)|0x10) );

	// 2. Enable the JPEG module
	prvSetJpeg( TRUE, FALSE );

	// 3a. Software reset the JPEG codec
	prvResetJpegCodec();

	// 3b. Select the operation mode for encoding
	halWriteReg8( REG1000_OPMODE, (UInt8)pJpegInfo->m_YUVFmt );
	//76543210b
	//||||||++--YUV Format Select
	//|||||+----JPEG Operation Select
	//||||+-----Marker Insert Enable
	//|||+------Reserved DNL Insert Enable
	//+++-------n/a

	// 3c. Set the desired quantization table number and the huffman table number
	halWriteReg8( REG1006_QUANTNUM, 0x06 );
	//76543210b
	//|||||||+--Color 1 Table Select
	//||||||+---Color 2 Table Select
	//|||||+----Color 3 Table Select
	//+++++-----n/a

	halWriteReg8( REG1008_HUFFMANNUM, 0x3C );
	//76543210b
	//|||||||+--DC Color 1 Table Select
	//||||||+---AC Color 1 Table Select
	//|||||+----DC Color 2 Table Select
	//||||+-----AC Color 2 Table Select
	//|||+------DC Color 3 Table Select
	//||+-------AC Color 3 Table Select
	//++--------n/a

	// 3e. Configure the vertical pixel size and the horizontal pixel size
	width	= pJpegInfo->m_Width;
	height	= pJpegInfo->m_Height;
	if ( pJpegInfo->m_HScaleRate > 1 )
	{
		width /= pJpegInfo->m_HScaleRate;
	}
	if ( pJpegInfo->m_VScaleRate > 1 )
	{
		height /= pJpegInfo->m_VScaleRate;
	}

	prvHalWriteReg16( REG1010_VERTPIXELSIZE1, REG100E_VERTPIXELSIZE0, height );
	prvHalWriteReg16( REG1014_HORZPIXELSIZE1, REG1012_HORZPIXELSIZE0, width );

	// 3g. Initialize Quantization table No. 0 and Quantization Table No. 1
	prvLoadQuantizationTables();

	// 3h. Set DC Huffman Tables and the AC Huffman Tables
	prvLoadHuffmanTables();

	// 4. Set the JPEG module registers
	// 4a. Enable the JPEG module and perform a JPEG software reset
	prvSetJpeg( TRUE, TRUE );

	// 4b. Set the Encode Size Limit (set by Cfg)
	prvHalWriteReg16( REG0820_ENCODELIMIT0, REG0821_ENCODELIMIT1, 0xFFFF );//when camera encode i use 0xffff--norman

	fSuccess = prvInitEncodeResizer( pJpegInfo );

	if ( pJpegInfo->m_MemEnc )
	{
		halWriteReg8( REG0212_MEMHORZRES, (UInt8)(pJpegInfo->m_Width-1) );
		halWriteReg8( REG0214_MEMVERTRES, (UInt8)(pJpegInfo->m_Height-1) );
	}

	// Enable Overlay
	if ( pJpegInfo->m_fSOE )
		halWriteReg8( REG004C_DISPSETTING, (UInt8)(halReadReg8(REG004C_DISPSETTING)|0x40) );

	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	prvAssessStatusError - determines type of status error
//
//	Input:	jpegStatus
//
//	Output:	n/a
//
//-----------------------------------------------------------------------------
void prvAssessStatusError( prvJpegFlag jpegStatus )
{
	jpegError	error;
	Boolean		fSuccess = (jpegStatus & LnBufOverflow) ? FALSE : TRUE;
	//FEDBCA9876543210b
	//|||||||||||||||+--Reserved JPEG Line Buffer Interrupt Flag
	//||||||||||||||+---JPEG Code Interrupt Flag
	//|||||||||||||+----Line Buffer Overflow Flag
	//||||||||||||+-----Reserved Size Mismatch Flag
	//|||||||||||+------Decode Marker Read Flag
	//||||||||||+-------JPEG Decode Complete Flag
	//|||||||||+--------n/a
	//||||||||+---------Reserved
	//|||||||+----------JPEG Encode Overflow Flag
	//|||||++-----------n/a
	//||||+-------------Encode Size Limit Violation Flag
	//||++--------------n/a
	//|+----------------JPEG Codec File Out Status
	//+-----------------Reserved
	if ( !fSuccess )
	{
		error = JPEG_ERR_LINEBUF_OVERFLOW;
	}
	else
	{
		fSuccess	= (jpegStatus & EncOverflow) ? FALSE : TRUE;
		error		= fSuccess ? JPEG_ERR_ENCSIZELIMIT : JPEG_ERR_ENCODE_OVERFLOW;
	}
	jpegSetLastError( error );
}

//------------------------------------------------------------------------------
//	prvInitEachEncode - prepare overall JPEG capture
//
//	Input:	pJpegInfo	pointer to JPEGINFO structure
//
//	Return:	n/a
//
//------------------------------------------------------------------------------
void prvInitEachEncode( JPEGINFO* pJpegInfo )
{

	// 7a. Clear all status bits
	// Disable all interrupts
	prvSetIrqControl( IrqDisable );

	// Reset all status flags
	prvSetJpegStatus( AllStatusClear );

	// 7b. Enable the appropriate interrupts
	prvSetIrqControl( JpegCodec|LnBufOverflow|EncSizeLimit|EncOverflow );
	//FEDBCA9876543210b
	//|||||||||||||||+--Reserved JPEG Line Buffer Interrupt Enable
	//||||||||||||||+---JPEG Code Interrupt Enable
	//|||||||||||||+----Line Buffer Overflow Interrupt Enable
	//||||||||||||+-----n/a
	//|||||||||||+------Decode Marker Read Interrupt Enable
	//||||||||||+-------JPEG Decode Complete Interrupt Enable
	//||||||||++--------n/a
	//|||||||+----------JPEG Encode overflow Interrupt Enable
	//|||||++-----------n/a
	//||||+-------------Encode Size Limit Over Interrupt Enable
	//++++--------------n/a

	if ( pJpegInfo->m_fPause )
	{
		////printf( "Pause before Encode/Capture start.  Press any key to continue..." );
		//getch();
		////printf( "\n" );
	}

	// 7c. Start capturing
	halReadReg8( REG1004_JPEGOP );	// Must be read before writing to REG[0810h].
	halWriteReg8( REG0810_JPEGSTARTSTOP, 0x01 );

	if ( !pJpegInfo->m_MemEnc && !pJpegInfo->m_fMotionJPEG )	//for motionjpeg: may turn on reg[108h] b2b1 only once before the 1st encode?
		halWriteReg8( REG0108_CMCONTROL, (UInt8)(halReadReg8(REG0108_CMCONTROL)|0x02) );
	/*else//norman debug add
	{
		UInt8 reg = halReadReg8(REG003C_LCDXFER);
		if(reg & 0x01)
		{
			halWriteReg8(REG0216_RYC,halReadReg8( REG0038_LCDSTATUS) | 0x80);
			halWriteReg8(REG003C_LCDXFER, 0);
			while ( halReadReg8( REG0038_LCDSTATUS) & 0x0001 );
			//halWriteReg8( REG003A_LCDFRAMEXFER, 0x0001 );
			//halWriteReg8(REG0108_CMCONTROL,0x18);
		}
	}*/

	halDelayUS(4000);
	//if ( pJpegInfo->m_fPause )
	//{
		////printf( "Pause before JPEG Codec start.  Press any key to continue..." );
		//getch();
		////printf( "\n" );
	//}

	// 7d. Start the JPEG operation
	prvStartJpeg();

}

//-----------------------------------------------------------------------------
//	prvInitEncodeResizer - programs resizer for encoding
//
//	Input:	pJpegInfo	pointer to JPEGINFO structure
//
//	Output:	TRUE means success
//			FALSE means NOT success
//
//-----------------------------------------------------------------------------
Boolean prvInitEncodeResizer( JPEGINFO* pJpegInfo )
{
	Boolean fSuccess;

	// 5. Set the resizer registers
	prvSetResize( FALSE, pJpegInfo );

	if ( pJpegInfo->m_RWidth > 0 )
		pJpegInfo->m_XEnd = pJpegInfo->m_XStart + pJpegInfo->m_RWidth - 1;
	if ( pJpegInfo->m_RHeight > 0 )
		pJpegInfo->m_YEnd = pJpegInfo->m_YStart + pJpegInfo->m_RHeight - 1;
	fSuccess = prvSetResizeRect( pJpegInfo->m_XStart, pJpegInfo->m_YStart, pJpegInfo->m_XEnd, pJpegInfo->m_YEnd );
	if ( fSuccess )
	{
		fSuccess = prvSetResizeIndScaleRate( pJpegInfo->m_fIndScale, pJpegInfo );
		if ( fSuccess )
		{
			fSuccess = prvSetResizeHScaleRate( pJpegInfo->m_HScaleRate, pJpegInfo );
			if ( fSuccess )
			{
				UInt8 value;

				fSuccess = prvSetResizeVScaleRate( pJpegInfo->m_VScaleRate, pJpegInfo );
				prvSetResizeScaleMode( pJpegInfo->m_ScaleMode, pJpegInfo );

				value = halReadReg8( REG0202_RESIZE );
				halWriteReg8( REG0202_RESIZE, (UInt8)(value|0x81) );		// Reset and Enable Resizer
				halWriteReg8( REG0202_RESIZE, (UInt8)(value|0x01) );		// Reset off (sticky reset bit)

				// 6. Set the Image Input Write Start Address (set by Cfg)
				if ( !prvGetCamClk() && !pJpegInfo->m_MemEnc )
				{
					prvSetCamClk( TRUE );
					halDelayUS( 750 * 1000 );
				}
			}
		}
	}

	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	prvInitCamera - programs camera register 100-100a  for memory encoding
//
//	Input:	pJpegInfo	pointer to JPEGINFO structure
//
//	Output:	n/a
//
//-----------------------------------------------------------------------------
void prvInitCamera(JPEGINFO* pJpegInfo)
{
	UInt8 reg;
	halDelayUS(10000);

	reg=halReadReg8(REG0108_CMCONTROL);	
	
	if((pJpegInfo->m_MemEnc) && (!(reg & 0x08)))
	{
		halWriteReg8(REG003C_LCDXFER,0x00);

		//Enable and reset resizer
		halWriteReg8(REG0202_RESIZE, 0x81);
		halWriteReg8(REG0202_RESIZE, 0x01);
		halDelayUS(100000);
	
		halWriteReg8(REG0108_CMCONTROL,reg | 0x08);
		while(halReadReg8(REG010A_CMSTATUS) & 0x01)
			{
			continue;
			}
		clStartOneFrameTransfer();
		//halWriteReg8(REG022A_YOFFSET0, 16);

		halDelayUS(30000);
		halWriteReg8(REG0108_CMCONTROL,reg & 0xF7);
		//halWriteReg8(REG0108_CMCONTROL,reg & 0xFB);		
	}

}

⌨️ 快捷键说明

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