jcamencode.c
来自「是一个手机功能的模拟程序」· C语言 代码 · 共 1,071 行 · 第 1/3 页
C
1,071 行
//printf( "QTable:%u\n", iQTable + 1 );
}
// Get static portion of header
fSuccess = prvCopy2Jpeg( &pDest, &g_JpegHeader[ 0 ], sizeof( g_JpegHeader ), pEnd );
if ( fSuccess )
{
// Now select each QTable
for
(
g_QuantizationHeader.m_Identifier = 0;
(g_QuantizationHeader.m_Identifier < 3) && fSuccess;
++g_QuantizationHeader.m_Identifier
)
{
fSuccess = prvCopy2Jpeg( &pDest, &g_QuantizationHeader, sizeof( g_QuantizationHeader ), pEnd );
if ( fSuccess )
{
fSuccess = prvCopy2Jpeg( &pDest, pQTable, 64, pEnd );
}
}
if ( fSuccess )
{
UInt32 width;
UInt32 height;
fSuccess = AgilentResGet( &width, &height );
if ( !fSuccess )
{
jpegSetLastError( JPEG_ERR_AGILENT_ERROR );
}
else
{
g_RemainderInfo.m_FrameHeader.m_Rows[0] = (UInt8)((height >> 8) & 0xff);
g_RemainderInfo.m_FrameHeader.m_Rows[1] = (UInt8) (height & 0xff);
g_RemainderInfo.m_FrameHeader.m_Columns[0] = (UInt8)((width >> 8) & 0xff);
g_RemainderInfo.m_FrameHeader.m_Columns[1] = (UInt8) (width & 0xff);
fSuccess = prvCopy2Jpeg( &pDest, &g_RemainderInfo, sizeof( g_RemainderInfo ), pEnd );
if ( fSuccess )
{
// Skip over first 4 bytes
fSuccess = prvCopy2Jpeg( &pDest, (pJpegInfo->m_pBuf + 4), pJpegInfo->m_EncSize - 4, pEnd );
if ( fSuccess )
{
memcpy( pJpegInfo->m_pBuf, pStartJpeg, size );
free( pStartJpeg );
pJpegInfo->m_EncSize = size;
fSuccess = jpegHasImageMarkers( pJpegInfo );
}
}
}
}
}
}
}
return ( fSuccess );
}
//------------------------------------------------------------------------------
// prvStartCapture - start JPEG capture in 13711 hardware
//
// Input: pJpegInfo pointer to JPEGINFO object
// pCamEncState pointer to prvCamEncodeState object
// pComplete pointer to complete flag
//
//
// Return: TRUE if success (whether capture complete or not)
// FALSE if NOT success
//
//------------------------------------------------------------------------------
Boolean prvStartCapture( JPEGINFO* pJpegInfo, prvCamEncodeState* pCamEncState, Boolean* pComplete )
{
Boolean fSuccess = TRUE;
// Clear any existing interrupt status
if ( prvHasDataCapInt() )
{
halWriteReg8( REG0300_IRQSTATUS0, 0x10 );
halWriteReg8( REG0300_IRQSTATUS0, 0x00 );
}
// Capture JPEG data from camera to video memory
// Capture Frame but no repeat capture enable
halWriteReg8( REG0108_CMCONTROL, 0x02 );
//76543210b
//|||||||+--CMCLK Output Hold/Reset
//||||||+---Capture Frame
//|||||+----Repeat Capture Enable
//||||+-----Capture Stop
//|||+------Camera Data Type
//+++-------n/a
// Wait till capture complete
while ( !prvHasDataCapInt() && fSuccess )
{
#ifndef MV850E
fSuccess = jpegNoTimeOut( pJpegInfo );
#endif
}
*pComplete = prvIsCaptureComplete();
return( fSuccess );
}
//------------------------------------------------------------------------------
// prvStopCapture - stop current capture in 13711 hardware
//
// Input: n/a
//
// Return: previous CameraControl
//
//------------------------------------------------------------------------------
UInt8 prvStopCapture( void )
{
UInt8 camControl = halReadReg8( REG0108_CMCONTROL );
//76543210b
//|||||||+--CMCLK Output Hold/Reset
//||||||+---Capture Frame
//|||||+----Repeat Capture Enable
//||||+-----Capture Stop
//|||+------Camera Data Type
//+++-------n/a
UInt8 newCamControl = (UInt8)((camControl & ~0x02) | 0x08);
halWriteReg8( REG0108_CMCONTROL, newCamControl );
// Set 13711 to JPEG mode
halWriteReg8( REG0108_CMCONTROL, 0x08 );
// Stop Camera Capture must be set to 0 before starting capture
halWriteReg8( REG0108_CMCONTROL, 0x00 );
return ( camControl );
}
//------------------------------------------------------------------------------
// prvCopy2Jpeg - copies data to JPEG image
//
// Input: ppDest pointer to pointer to destination buffer
// pSource pointer to source data
// size number of bytes of source data to copy
// pEnd end marker
//
// Return: true means success
// false means NO success
//
//------------------------------------------------------------------------------
Boolean prvCopy2Jpeg( UInt8** ppDest, const void* pSource, UInt32 size, UInt8* pEnd )
{
UInt8* pDest = *ppDest;
Boolean fSuccess = (Boolean)(pDest + size <= pEnd);
if ( !fSuccess )
{
jpegSetLastError( JPEG_ERR_ENCSIZELIMIT );
}
else
{
memcpy( pDest, pSource, size );
pDest += size;
*ppDest = pDest;
}
return ( fSuccess );
}
//------------------------------------------------------------------------------
// prvHasDataCapInt - determines if Camera Data Capture Interrupt occurred
//
// Input: n/a
//
// Return: TRUE means camera capture complete
// FALSE means camera capture NOT complete
//
//------------------------------------------------------------------------------
Boolean prvHasDataCapInt( void )
{
UInt8 status = halReadReg8( REG0300_IRQSTATUS0 );
Boolean fIsCapture = (status & 0x10) ? TRUE : FALSE;
return ( fIsCapture );
}
//------------------------------------------------------------------------------
// prvAccessAgilent - Tests to see if Agilent is responding
//
// Input: pJpegInfo pointer to JPEGINFO object
// pCamEncState pointer to prvCamEncodeState object
//
// Return: TRUE means success
// FALSE means NOT successful
//
//------------------------------------------------------------------------------
Boolean prvAccessAgilent( JPEGINFO* pJpegInfo, prvCamEncodeState* pCamEncState )
{
UInt32 loop = 0;
Boolean fSuccess;
do
{
fSuccess = AgilentIsYUV( &pCamEncState->m_fYUV );
if ( !fSuccess )
{
halDelayUS( 200 * 1000 );
++loop;
#ifdef MV850E
pCamEncState->m_fNoTimeOut = (Boolean)(loop <= 100);
#else
pCamEncState->m_fNoTimeOut = jpegNoTimeOut( pJpegInfo );
#endif
if ( pJpegInfo->m_fVerbose )
{
//printf( "Agilent Delay Loop:%u\n", loop );
}
}
}
while ( (!fSuccess) && pCamEncState->m_fNoTimeOut );
if ( !fSuccess )
{
jpegSetLastError( JPEG_ERR_AGILENT_ERROR );
}
return ( fSuccess );
}
//------------------------------------------------------------------------------
// prvIsCaptureComplete - Determines if complete JPEG capture has occured
//
// Input: n/a
//
// Return: TRUE if capture copmlete
// FALSE if capture truncated
//
//------------------------------------------------------------------------------
Boolean prvIsCaptureComplete( void )
{
UInt8 cameraStatus = halReadReg8( REG010A_CMSTATUS );
//76543210b
//|||||||+--Camera Interface Status
//||||||+---JPEG Capture Data Status (RO)
//++++++----n/a
Boolean fComplete = (cameraStatus & 0x02) ? TRUE : FALSE;
return ( fComplete );
}
//------------------------------------------------------------------------------
// prvSetJpegMode - sets camera into JPEG mode
//
// Input: pJpegInfo pointer to JPEGINFO object
// pCamEncState pointer to prvCamEncodeState object
//
// Return: TRUE means JPEG mode set successfully
// FALSE means JPEG mode NOT set successfully
//
//------------------------------------------------------------------------------
Boolean prvSetJpegMode( JPEGINFO* pJpegInfo, prvCamEncodeState* pCamEncState )
{
Boolean fSuccess = prvAccessAgilent( pJpegInfo, pCamEncState );
if ( fSuccess )
{
fSuccess = AgilentJpegTimeOut( pJpegInfo->m_TimeOutTgt, pJpegInfo->m_TimeOutMS );
if ( !fSuccess )
{
jpegSetLastError( JPEG_ERR_AGILENT_ERROR );
}
else
{
fSuccess = prvAccessAgilent( pJpegInfo, pCamEncState );
if ( fSuccess )
{
fSuccess = AgilentResSet( (UInt32)pJpegInfo->m_Width, (UInt32)pJpegInfo->m_Height );
if ( !fSuccess )
{
jpegSetLastError( JPEG_ERR_DIM_RESTRICT );
}
}
}
if ( pJpegInfo->m_fVerbose )
{
AgilentDump( FALSE );
AgilentDump( TRUE );
}
}
return ( fSuccess );
}
//-----------------------------------------------------------------------------
// prvInitNextCapture - program structures for next image
//
// Input: pJpegInfo pointer to JPEGINFO object
// pCamEncState pointer to prvCamEncodeState object
//
// Output: TRUE if valid parameters
// FALSE if NOT valid paramaters
//
//-----------------------------------------------------------------------------
Boolean prvInitNextCapture( JPEGINFO* pJpegInfo, prvCamEncodeState* pCamEncState )
{
Boolean fSuccess = TRUE;
prvAdvanceStartAddr( pJpegInfo, &pCamEncState->m_StartAddr );
if ( pJpegInfo->m_FrameCount > 1 )
{
fSuccess = (pJpegInfo->m_EncSize <= pJpegInfo->m_BufSize) ? TRUE : FALSE;
if ( !fSuccess )
{
jpegSetLastError( JPEG_ERR_ENCSIZELIMIT );
}
else
{
mfInitializeMultiFrame( pCamEncState->m_pMulFrm, pJpegInfo->m_EncSize );
pJpegInfo->m_pBuf += pCamEncState->m_pMulFrm->PaddedSize;
pJpegInfo->m_BufSize -= pCamEncState->m_pMulFrm->PaddedSize;
pCamEncState->m_pMulFrm->FrameNumber = pCamEncState->m_Count;
pCamEncState->m_TotalSize += (sizeof( JPEGMULTIFRAME ) + pCamEncState->m_pMulFrm->PaddedSize);
}
}
else
{
pJpegInfo->m_BufSize -= pJpegInfo->m_EncSize;
pJpegInfo->m_pBuf += pJpegInfo->m_EncSize;
pCamEncState->m_TotalSize += pJpegInfo->m_EncSize;
}
return ( fSuccess );
}
//-----------------------------------------------------------------------------
// prvSelectQTable - Select quality table for Agilent camera
//
// Input: pJpegInfo pointer to JPEGINFO object
//
// Output: TRUE if successful
// FALSE if NOT successful
//
//-----------------------------------------------------------------------------
Boolean prvSelectQTable( JPEGINFO* pJpegInfo )
{
UInt16 mainCfg2;
//Boolean fSuccess = AgilentProcessorRead( apr04_CFG_MAIN2, &mainCfg2 );
//FEDCBA9876543210b
//|||||||||||||||+--Pixel Correction
//||||||||||||||+---Sizer Control
//|||||||||||||+----Demosiac operation control
//||||||||||||+-----JPEG Compression control
//|||||||||||+------Auto Configure Image Sensor
//||||||||||+-------ALQ Auto load quantization table RAM
//|||||||||+--------AQA Auto Q Adjust
//||||||||+---------Auto load gamma correction lookup table RAM
//|||||||+----------Sign of Cr bit - MCU (minimum coded unit)
//||||||+-----------Sign of Cb bit - MCU (minimum coded unit)
//|||||+------------Sign of Y bit (minimum coded unit)
//||||+-------------Compression Control Mode
//|||+--------------Frame Header /trailer disable
//||+---------------Unused
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?