📄 decode.c
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// 2002/12/17
// Konagayoshi Susumu
// The Addition of New Function
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include "sysLCDC.h"
extern unsigned long EncodedSize;
extern unsigned char * pBuffer;
/////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
/////////////////////////////////////////////////////////////////////////////////////////////////
#define RESIZE_MAX_XPOSITION 0x0400
#define RESIZE_MAX_YPOSITION 0x0200
#define VIEW_CIRCUIT 0
//unsigned long JPGFileSize;
unsigned char *pJPEGData; // pointer to JPEG data
unsigned long JPEGDataSize; // JPEG data size
unsigned long YRCOutputDispAddr; // data from YRC is written to FrameBuffer starting from OutputDispAddr;
//YUV/RGB converter setting
unsigned long YUVWrtStartAddr;// YUV Image Input Write Start Adress
unsigned short DesiredDispX;
unsigned short DesiredDispY;
unsigned short ActualDispX;
unsigned short ActualDispY;
//the following PIP (x,y,xSize,ySize) is from application programmer's point of view
unsigned short PIPWidth; // Width (in pixels).
unsigned short PIPHeight; // Height (in pixels).
unsigned short PIPBpp; // Bits per pixel.
unsigned short PIPx=0; // PIP left position (unaware of any h/w rotation)
unsigned short PIPy=0; // PIP top position (unaware of any h/w rotation)
unsigned short PIPxSize; // PIP horizontal size (unaware of any h/w rotation)
unsigned short PIPySize; // PIP vertical size (unaware of any h/w rotation)
unsigned short PIPStride; // Number of bytes from one line to the next.
/*********************************************************
Descriptions:
Returns:
Notes:
**********************************************************/
void CopyFromPIPToScreen(unsigned short DispX,
unsigned short DispY,
unsigned short DispWidth,
unsigned short DispHeight )
{
volatile unsigned int i, length, stride, linebytes;
volatile unsigned short Bpp;
volatile unsigned short SrcWidth, SrcHeight;
volatile unsigned char *SrcBuf, *pipBuffer;
// data validity check
SrcWidth = GetDisplayWidth(cl_MAIN_WINDOW);
SrcHeight = GetDisplayHeight(cl_MAIN_WINDOW);
if( DispX >= SrcWidth || DispY >= SrcHeight
|| DispX + DispWidth >= SrcWidth
|| DispY + DispHeight >= SrcHeight )
return;
pipBuffer = ( unsigned char *)(PIP_WINDOW_ADDRESS+HalInfo.dwMemoryAddress);
stride = GetStride( cl_MAIN_WINDOW );
SrcBuf = ( unsigned char *)(MAIN_WINDOW_ADDRESS+HalInfo.dwMemoryAddress);
Bpp = GetBitsPerPixel(cl_PIP_WINDOW);
SrcBuf += DispY * stride + DispX * Bpp/8;
linebytes = DispWidth * Bpp/8;
for( i = 0; i < DispHeight; i ++ )
{
memcpy( SrcBuf, pipBuffer, linebytes );
SrcBuf += stride;
pipBuffer += linebytes;
}
return;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////////////////
BOOL Decode(unsigned short dispwidth,
unsigned short dispheight,
unsigned char *Buffer,
unsigned long encodesize,
unsigned char mode) // PIPWINDOW_SHOW / PIPWINDOW_HIDE
{
// check data validity
if( (dispwidth > (halReadReg16(0x0042) + 1) * 2)
||( dispheight > halReadReg16(0x004C) + 1) )
return FALSE;
//
JPEGDataSize = encodesize;
pBuffer = Buffer;
DesiredDispX = dispwidth;
DesiredDispY = dispheight;
//
YUVWrtStartAddr=PIP_WINDOW_ADDRESS;
YRCOutputDispAddr = YUVWrtStartAddr;
// 2003/3/12 Konagayoshi
// Desired Display Max Size set Main Window
DesiredDispX = dispwidth ; //Main Window Width;
DesiredDispY = dispheight ; //Main Window Height;
if ( JpegDecode() == FALSE ) // JPEG decode Process!
return FALSE;
// if ( (ActualDispX!=DesiredDispX) || (ActualDispY!=DesiredDispY) ) // display mode set
{
PIPWidth = ActualDispX;
PIPHeight = ActualDispY;
// 2003/4/14 Konagayoshi
// Resizer Constraction
PIPStride = ((halReadReg16(0x0948) - halReadReg16(0x0944) + 1) / halReadReg16(0x094C)) * 2; //ActualDispX * 16 / 8;
PIPxSize = PIPWidth;
PIPySize = PIPHeight;
//change PIP window stride and width, Height
SetStride( cl_PIP_WINDOW, PIPStride );
SetPipSizeCenter(PIPx,PIPy,PIPxSize,PIPySize);
//SetStride( cl_PIP_WINDOW, 176*2 );
//SetPipSize(0,40,176,176);
if( mode == PIPWINDOW_SHOW )
// Main and PIP mode
halWriteReg16(0x0200, (unsigned short)(halReadReg16( 0x0200 ) | 0x0100 ));
else
// Main Windows Only
halWriteReg16(0x0200, (unsigned short)(halReadReg16( 0x0200 ) & 0x00FF ));
}
// halWriteReg16(0x0200, 0x0330);
return TRUE;
}
//------------------------------------------------------------------------------
// jpegDecode - placed .JPG (encoded) data into FIFO for decoding
// Input: JDECODEINFO *pJdecode
// pJdecode->pJEGData should point to a memory buffer, the size of which is multiple of 4 bytes.
// Changed: reg[240h] reg[242h] reg[244h]
//
// It assumes that PIP Window is already set up properly before this routine is called:
//
// Return:
// TRUE successful, FALSE if error.
//
//------------------------------------------------------------------------------
BOOL JpegDecode(void)
{
unsigned short *pWriteData;
unsigned short Status;
unsigned long FileFIFOSize,i; //,JPEGDataSize;
unsigned short XSize, YSize;
long Er = 0;
// 2003/3/11 Konagayoshi
//set YRC output start Address
halWriteReg16(0x0242, halReadReg16(0x0218));
halWriteReg16(0x0244, halReadReg16(0x021A));
// Camera Clock Disable
//halWriteReg16(0x0114, 0x0001);
// YRC Setting
halWriteReg16(0x0240, 0x00C5);
//Sleep(100);
for(i=0;i<100;i++);
halWriteReg16(0x0240, 0x0045);
//start actual init JPEG decode
halWriteReg16(0x0980, 0x0001); //enable JPEG Codec module
halWriteReg16(0x0980, 0x0081); //Reset JPEG Codec module
halWriteReg16(0x1000, 0x0004); //set to Decode mode
halWriteReg16(0x1002, 0x0080); //do JPEG Codec s/w reset
halWriteReg16(0x101C, 0x0001);
// FIFO Size Already Set in VB Debugger
// 2003/3/11 Konagayoshi
// for debug
// halWriteReg16(0x09A4, 0x0007);
halWriteReg16(0x09B8, (unsigned short)JPEGDataSize); //Set JPEG file size
halWriteReg16(0x09BA, (unsigned short)(JPEGDataSize>>16)); //Set JPEG file size
halWriteReg16(0x09A0, 0x0004); //Clear FIFO
// halReadReg16(0x09A6); // Dummy Read
// halReadReg16(0x09A6);
//start JPEG decode process
halWriteReg16(0x0982, 0xFFFF); //clear all status
halWriteReg16(0x0986, 0x0132); //set interrupt enable
halWriteReg16(0x1002, 0x0001); //enable JPEG operation
FileFIFOSize = (halReadReg16(0x09A4) + 1) * 4096;
pWriteData = (unsigned short *)(pBuffer);
while ( 1 )
{
Status = halReadReg16( 0x0982 );
// Decode Marker Read
if(Status & 0x0010)
{
XSize = halReadReg16( 0x1012 ) << 8;
XSize += halReadReg16( 0x1014 );
YSize = halReadReg16( 0x100E ) << 8;
YSize += halReadReg16( 0x1010 );
if ( JPEGSetSizeRegistersDecode(0, 0, XSize, YSize) == FALSE )
return FALSE;
OrBitOn( 0x0982, 0x0010 ); // JPEG DECODE MARKER READ
AndBitOff( 0x0986, 0x0010 );
}
// File Size Mismatch
if(Status & 0x0008)
{
// File Size Mismatch
break;
}
// JPEG CODEC(Decode) Ended
if(Status & 0x0020)
{
break;
}
// FIFO Buffer Empty
if( (Status & 0x0100) && JPEGDataSize>0 )
{
while ( halReadReg16( 0x09A8 ) ); //confirming FIFO valid data size is 0
if(JPEGDataSize < FileFIFOSize)
FileFIFOSize = JPEGDataSize;
for(i = 0; i < FileFIFOSize; i += 4)
{
halWriteReg16( 0x09A6, *pWriteData++ );
halWriteReg16( 0x09A6, *pWriteData++ );
}
JPEGDataSize -= FileFIFOSize;
}
// Error Time Out
// Escape
Er++;
if ( Er == 10000 )
{
return FALSE;
}
}
//exit JPEG decode cleanly
halWriteReg16( 0x0986, 0x0000 ); // Interrupt Disable
halWriteReg16( 0x0982, 0xFFFF ); // All Status Clear
halReadReg16( 0x1004 ); // read reg[1004h]
halWriteReg16( 0x0980, 0x0081 );
halWriteReg16( 0x0980, 0x0000 );
if ( Status & 0x0020 )
return TRUE;
else
return FALSE;
}
//-----------------------------------------------------------------------------
// JPEGSetSizeRegistersDecode - sets Decode size registers
// When the image size exceeds the PIP window size, the display size is reduced
// by the resize.
//
// Input: ResizeXOffset
// ResizeYOffset
// XSize //image size X
// YSize //image size Y
// pJdecode
//
// Output: TRUE if valid XSize and YSize
// FALSE if NOT valid XSize and YSize
//-----------------------------------------------------------------------------
static BOOL JPEGSetSizeRegistersDecode(
unsigned short ResizeXOffset,
unsigned short ResizeYOffset,
unsigned short XSize,
unsigned short YSize)
{
unsigned short Scale;
unsigned short ScaledXsize, ScaledYsize;
Scale = 1;
halWriteReg16( 0x0940, 0 ); //disable view resize
halWriteReg16( 0x0960, 0 ); //disable capture resize
while( XSize/Scale > DesiredDispX )
{
Scale++;
if(Scale > 8) // Resize Scale Error!
return FALSE;
}
while( YSize/Scale > DesiredDispY )
{
Scale++;
if(Scale > 8) // Resize Scale Error!
return FALSE;
}
ScaledXsize = XSize/Scale;
ScaledYsize = YSize/Scale;
halWriteReg16( 0x094C, Scale );
switch(Scale)
{
case 1:
halWriteReg16( 0x094E, 0);
break;
case 3: case 5: case 6: case 7:
halWriteReg16( 0x094E, 1);
break;
default:
halWriteReg16( 0x094E, 2);
break;
}
// For Example, when the reduction rate is adjusted to 1/5, cutting size should be set to become the multiple of five.
if ( ResizeSetCuttingAddress(
VIEW_CIRCUIT,
ResizeXOffset,
ResizeYOffset,
(unsigned short)(ResizeXOffset + ScaledXsize * Scale-1),
(unsigned short)(ResizeYOffset + ScaledYsize * Scale-1)) == FALSE )
return FALSE;
//set the actual Disp X & Y for caller to set stride properly to display JPEG image.
ActualDispX = ScaledXsize;
ActualDispY = ScaledYsize;
// halWriteReg16( 0x0930, 0x0000 ); //data come from view resizer
halWriteReg16( 0x0940, 0x0081 ); //reset Resizer circuit & enable Resizer circuit
halWriteReg16( 0x0940, 0x0001 );
return TRUE;
}
//-----------------------------------------------------------------------------
// Set Resize image cutting start and end address
//-----------------------------------------------------------------------------
static BOOL ResizeSetCuttingAddress (
unsigned char ResizeCircuit, // 0-For View, 1-For Capture
unsigned short XStart,
unsigned short YStart,
unsigned short XEnd,
unsigned short YEnd )
{
if (XStart >= RESIZE_MAX_XPOSITION || YStart >= RESIZE_MAX_YPOSITION || // Error Resize Cutting Size
XEnd >= RESIZE_MAX_XPOSITION || YEnd >= RESIZE_MAX_YPOSITION)
return FALSE;
if (((XEnd - XStart + 1 ) % 2) == 1)
{
halWriteReg16( 0x0944, XStart );
halWriteReg16( 0x0946, YStart );
halWriteReg16( 0x0948, (unsigned short)(XEnd + 1));
halWriteReg16( 0x094A, YEnd );
}
else
{
halWriteReg16( 0x0944, XStart );
halWriteReg16( 0x0946, YStart );
halWriteReg16( 0x0948, XEnd );
halWriteReg16( 0x094A, YEnd );
}
return TRUE;
}
//-----------------------------------------------------------------------------
// OrBitOn - forces bit pattern on
//
// Input: index index of hardware register
// bit bit pattern to force on
//
// Output: n/a
//
//-----------------------------------------------------------------------------
static void OrBitOn( unsigned short index, unsigned short bit )
{
unsigned short value = halReadReg16( index );
value |= bit;
halWriteReg16( index, value );
}
//-----------------------------------------------------------------------------
// AndBitOff - forces bit pattern off
//
// Input: index index of hardware register
// bit bit pattern to force off
//
// Output: n/a
//
//-----------------------------------------------------------------------------
static void AndBitOff( unsigned short index, unsigned short bit )
{
unsigned short value = halReadReg16( index );
value &= ~bit;
halWriteReg16( index, value );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -