tskProcess.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:15k
- #include <std.h>
- #include <tsk.h>
- #include <stdio.h>
- #include <time.h>
- #include <csl.h>
- #include <csl_cache.h>
- #include <csl_dat.h>
- #include <chan.h>
- #include <scom.h>
- #include <utl.h>
- #include <ialg.h>
- #include "fvid.h"
- #include "celljpegenc_ti.h"
- #include "celljpegdec_ti.h"
- #include "appmain.h"
- #include "appThreads.h"
- #include "tskProcess.h"
- #include "appBiosObjects.h"
- #include "netmain.h" /*change*/
- //#include "cap_dis_size.h"
- IJPEGENC_Params jpegencParams;
- IJPEGDEC_Params jpegdecParams;
- #pragma DATA_SECTION(jpg_img, ".user_data_ext")
- #pragma DATA_ALIGN( jpg_img, 128)
- unsigned char jpg_img[128 * 2000];
- #pragma DATA_ALIGN(dec_out_y, 128);
- #pragma DATA_ALIGN(dec_out_u, 128);
- #pragma DATA_ALIGN(dec_out_v, 128);
- unsigned char dec_out_y[720 * 480];
- unsigned char dec_out_u[360 * 240];
- unsigned char dec_out_v[360 * 240];
- static Void checkMsg();
- /*
- // Simple Motion Detection
- //
- // This code puts "sensors" on the screen and checks them
- // for change in intensity. A sensor is "tripped" when
- // there is a change in intensity beyond a set threshold.
- //
- */
- #define SPACING 40 /* Pixel spacing of "sensor" */
- #define YDELTATHRESH 12 /* Max delta Y the will not trip "sensor" */
- #define PIXPERSAMPLE 2 /* Number of pixels per sample location */
- #define SENSBUFSIZE 128*4 /* Must be multiple of 128 & large enough */
- #define XPIX (720/SPACING)
- #define YPIX (480/SPACING)
- #define PIXOFF(n) ((n*SPACING)+(SPACING/2))
- #define SAMPLES (XPIX*YPIX)
- #if (SAMPLES*PIXPERSAMPLE) > SENSBUFSIZE
- #error "BUFFER SIZE"
- #endif
- #pragma DATA_SECTION(img_sum1, ".user_data_ext")
- #pragma DATA_ALIGN( img_sum1, 128)
- unsigned char img_sum1[SENSBUFSIZE];
- #pragma DATA_SECTION(img_sum2, ".user_data_ext")
- #pragma DATA_ALIGN( img_sum2, 128)
- unsigned char img_sum2[SENSBUFSIZE];
- unsigned char *img_sum[2] = { img_sum1, img_sum2 };
- #pragma DATA_SECTION(dec_data, ".user_data_ext")
- #pragma DATA_ALIGN( dec_data, 128)
- unsigned char dec_data[SENSBUFSIZE];
- #pragma DATA_SECTION(tag_data, ".user_data_ext")
- #pragma DATA_ALIGN( tag_data, 128)
- unsigned char tag_data[384 * 32];
- #define TAGPITCH 336
- extern unsigned char charset;
- static void drawchar( unsigned char *ps, unsigned char c )
- {
- int y,i;
- unsigned char cdata;
- unsigned char *pc = &charset + ((c-32)*16);
- for( y=0; y<16; y++ )
- {
- cdata = *pc++;
- for(i=0; i<8; i++)
- {
- if( cdata & 1 )
- {
- *(ps) = 0xFF;
- *(ps+1) = 0xFF;
- *(ps+TAGPITCH) = 0xFF;
- *(ps+TAGPITCH+1) = 0xFF;
- }
- cdata >>= 1;
- ps += 2;
- }
- ps += TAGPITCH*2 - 16;
- }
- }
- static void drawstring( unsigned char *pBuf, char *pString )
- {
- unsigned char c;
- while( c = *pString++ )
- {
- drawchar( pBuf, c );
- pBuf += 16;
- }
- }
- static void tagScreen( unsigned char *pY )
- {
- int y;
- uint ms;
- struct tm *ptm;
- time_t secs;
- char str[32];
- secs = llTimerGetTime(&ms) + netcmdArgs[CMD_SETCLOCK];
- ptm = localtime(&secs);
- if( ptm->tm_year >= 30 )
- ptm->tm_year -= 30;
- else
- ptm->tm_year += 70;
- sprintf( str, "%02d/%02d/%02d %02d:%02d:%02d:%02d",
- ptm->tm_mon+1, ptm->tm_mday, ptm->tm_year,
- ptm->tm_hour, ptm->tm_min, ptm->tm_sec, ms/10 );
- memset( tag_data, 0, 384 * 32);
- drawstring( tag_data+8, str );
- OEMCacheClean( tag_data, TAGPITCH*32 );
- OEMCacheCleanSynch();
- for( y=0; y<32; y++ )
- DAT_copy( tag_data+(TAGPITCH*y), pY+(720*424)+(720*y)+32, TAGPITCH );
- }
- static int checkMotion( unsigned char *pY )
- {
- static int idx = 0;
- int x,y,i;
- idx ^= 1;
- OEMCacheClean( img_sum[idx], SAMPLES*PIXPERSAMPLE );
- OEMCacheCleanSynch();
- i = 0;
- for( y=0; y<YPIX; y++ )
- for( x=0; x<XPIX; x++ )
- {
- DAT_copy( pY + PIXOFF(x) + (PIXOFF(y)*720),
- img_sum[idx] + i, PIXPERSAMPLE );
- i += PIXPERSAMPLE;
- }
- x = 0;
- if( !netcmdArgs[CMD_SHOWDOTS] )
- {
- for( i=0; i<SAMPLES * PIXPERSAMPLE; i++ )
- {
- y = (int)img_sum1[i] - (int)img_sum2[i];
- if( y < -YDELTATHRESH || y > YDELTATHRESH )
- x++;
- }
- }
- else
- {
- for( i=0; i<SAMPLES * PIXPERSAMPLE; i++ )
- {
- y = (int)img_sum1[i] - (int)img_sum2[i];
- if( y < -YDELTATHRESH || y > YDELTATHRESH )
- {
- x++;
- dec_data[i] = 0xFF;
- }
- else
- {
- dec_data[i] = 0x0;
- }
- }
- }
- // Return change in "tenths of percent"
- return( (x*1000)/(SAMPLES*PIXPERSAMPLE) );
- }
- static void showMotion( unsigned char *pY )
- {
- int x,y,i;
- OEMCacheClean( dec_data, SAMPLES*PIXPERSAMPLE );
- OEMCacheCleanSynch();
- i = 0;
- for( y=0; y<YPIX; y++ )
- for( x=0; x<XPIX; x++ )
- {
- DAT_copy( dec_data + i, pY + PIXOFF(x) + (PIXOFF(y)*720),
- PIXPERSAMPLE );
- i += PIXPERSAMPLE;
- }
- }
- ThrProcess thrProcess;
- void tskProcessInit()
- {
- int chanNum;
- ICELL_Obj *cell;
- ICC_Handle inputIcc;
- ICC_Handle outputIcc;
- /*----------------------------------------------------*/
- /* Call JPEG specific user initialization if any. */
- /*----------------------------------------------------*/
- JPEGENC_TI_init();
- JPEGDEC_TI_init();
- /*----------------------------------------------------*/
- /* Set up params for all XDAIS algorithms. */
- /*----------------------------------------------------*/
- jpegencParams = IJPEGENC_PARAMS;
- jpegdecParams = IJPEGDEC_PARAMS;
- for (chanNum = 0; chanNum < PROCESSNUMCHANNELS ; chanNum++)
- {
- /*
- * JPEGENC: create an input and output linear ICC:
- * The address to the ICC's will be set in the thrProcessRun()
- * function via the ICC_setBuf().
- */
- ICELL_Obj defaultCell = ICELL_DEFAULT;
- cell = &thrProcess.cellListEncode[(chanNum*PROCESSNUMCELLS) + 0];
- *cell = defaultCell;
- cell->name = "JPEGENC";
- cell->cellFxns = &JPEGENC_CELLFXNS;
- cell->algFxns = (IALG_Fxns *)&JPEGENC_IJPEGENC;
- cell->algParams = (IALG_Params *)&IJPEGENC_PARAMS;
- cell->scrBucketIndex = THRIOSSCRBUCKET;
- inputIcc = (ICC_Handle)ICC_linearCreate(NULL, 0);
- UTL_assert( inputIcc != NULL);
- outputIcc = (ICC_Handle)ICC_linearCreate(NULL, 0);
- UTL_assert( outputIcc != NULL);
- // Only one input and one output ICC are needed.
- CHAN_regCell( cell, &inputIcc, 1, &outputIcc, 1 );
- // Setup Encode Parameters
- thrProcess.cellListEncode[(chanNum*PROCESSNUMCELLS) + 0].algParams =
- (IALG_Params *)&IJPEGENC_PARAMS;
- UTL_logDebug1("JPEGEncoder registerd Channel Number: %d", chanNum);
- /*
- * JPEGDEC: create an input and output linear ICC:
- * The address to the ICC's will be set in the thrProcessRun()
- * function via the ICC_setBuf().
- */
- cell = &thrProcess.cellListDecode[(chanNum*PROCESSNUMCHANNELS) + 0];
- *cell = defaultCell;
- cell->name = "JPEGDEC";
- cell->cellFxns = &JPEGDEC_CELLFXNS;
- cell->algFxns = (IALG_Fxns *)&JPEGDEC_IJPEGDEC;
- cell->algParams = (IALG_Params *)&IJPEGDEC_PARAMS;
- cell->scrBucketIndex = THRIOSSCRBUCKET;
- inputIcc = (ICC_Handle)ICC_linearCreate(NULL, 0);
- UTL_assert( inputIcc != NULL);
- outputIcc = (ICC_Handle)ICC_linearCreate(NULL, 0);
- UTL_assert( outputIcc != NULL);
- // Only one input and one output ICC are needed.
- CHAN_regCell( cell, &inputIcc, 1, &outputIcc, 1 );
- // Setup Decode Parameters
- thrProcess.cellListDecode[(chanNum*PROCESSNUMCELLS) + 0].algParams =
- (IALG_Params *)&IJPEGDEC_PARAMS;
- UTL_logDebug1("JPEGDecoder registerd Channel Number: %d", chanNum);
- }
- memset(dec_out_y, 0x0, sizeof(dec_out_y));
- memset(dec_out_u, 0x80, sizeof(dec_out_u));
- memset(dec_out_v, 0x80, sizeof(dec_out_v));
- CACHE_clean(CACHE_L2ALL, 0, 0);
- CACHE_clean(CACHE_L2ALL, 0, 0);
- }
- void tskProcessStart()
- {
- int chanNum;
- for( chanNum=0; chanNum < PROCESSNUMCHANNELS; chanNum++ )
- {
- // open the encode channel: this causes the algorithms to be created
- CHAN_open( &thrProcess.chanListEncode[chanNum],
- &thrProcess.cellListEncode[(chanNum*PROCESSNUMCELLS)],
- PROCESSNUMCELLS, NULL );
- // open the decode channel: this causes the algorithms to be created
- CHAN_open( &thrProcess.chanListDecode[chanNum],
- &thrProcess.cellListDecode[(chanNum*PROCESSNUMCELLS)],
- PROCESSNUMCELLS, NULL );
- }
- }
- static Void checkMsg()
- {
- CtrlMsg rxMsg;
- Int index;
- Int quality;
- ICELL_Handle handle;
- IJPEGENC_Params jpegencParams;
- int cell_no;
- // check message in "mbxProc"
- while( MBX_pend( &mbxProcess, &rxMsg, 0) )
- {
- switch (rxMsg.cmd)
- {
- case MSGFRAMECHANGE: // frame ratio value changed
- {
- index = rxMsg.arg1; // get the index number
- UTL_assert( (index >= 0) && (index < PROCESSNUMCHANNELS));
- // update the local value
- if (index < PROCESSNUMCHANNELS)
- thrProcess.frameRateControl[index] = rxMsg.arg2;
- break;
- }
- case MSGQUALCHANGE: //quality rate value changed
- {
- index = rxMsg.arg1; // get the index number
- UTL_assert( (index >= 0) && (index < PROCESSNUMCHANNELS));
- if (index < PROCESSNUMCHANNELS)
- {
- jpegencParams = IJPEGENC_PARAMS;
- cell_no = rxMsg.arg2;
- UTL_assert( (cell_no >= 0) && (cell_no < PROCESSNUMCELLS));
- quality = rxMsg.arg3;
- UTL_assert( (quality >= 0) &&
- (quality <= 100));
- jpegencParams.quality = quality;
- if ((quality > 0) && (quality <= 100))
- {
- handle = &(thrProcess.cellListEncode[index]);
- thrProcess.cellListEncode[cell_no].cellFxns->cellControl
- (
- handle,
- (IALG_Cmd) (IJPEG_SETSTATUS),
- (IALG_Status*)(&(jpegencParams))
- );
- }
- }
- break;
- }
- default:
- break;
- }
- }
- }
- extern int SystemReady;
- void tskProcess()
- {
- int i;
- ScomMessage *pMsgBuf;
- ScomMessage scomMsg;
- void *inBuf[3];
- void *outBuf[3];
- int jpg_size;
- int framenum=0;
- CHAN_Handle chanHandle;
- SCOM_Handle fromInputtoProc,fromProctoInput;
- SCOM_Handle fromOuttoProc,fromProctoOut;
- SCOM_Handle fromNettoProc,fromProctoNet;
- fromInputtoProc = SCOM_open("INTOPROC");
- fromProctoInput = SCOM_open("PROCTOIN");
- fromProctoOut = SCOM_open("PROCTOOUT");
- fromOuttoProc = SCOM_open("OUTTOPROC");
- fromProctoNet = SCOM_open("PROCTONET");
- fromNettoProc = SCOM_open("NETTOPROC");
- while(1)
- {
- checkMsg();
- framenum++;
- for(i=0; i<PROCESSNUMCHANNELS; i++)
- {
- // Get Input Buffer
- pMsgBuf = SCOM_getMsg(fromInputtoProc, SYS_FOREVER);
- // If we're skipping this frame, just give the SCOM msg
- // back to the input function and continue the for loop.
- if( thrProcess.frameRateControl[i] != 0 &&
- (framenum % thrProcess.frameRateControl[i]) )
- {
- // Tell the capture routine we're done
- SCOM_putMsg(fromProctoInput,pMsgBuf);
- continue;
- }
- if( netcmdArgs[CMD_SHOWCLOCK] )
- tagScreen( pMsgBuf->bufY );
- //
- // Handle Encode Channel
- //
- chanHandle = &thrProcess.chanListEncode[i];
- chanHandle->state = CHAN_ACTIVE;
- // Channel Input
- inBuf[0] = pMsgBuf->bufY;
- inBuf[1] = pMsgBuf->bufU;
- inBuf[2] = pMsgBuf->bufV;
- ICC_setBuf( chanHandle->cellSet[0].inputIcc[0],
- inBuf, sizeof(void *) * 3 );
- // Channel Output
- outBuf[0] = &jpg_size;
- outBuf[1] = jpg_img;
- ICC_setBuf( chanHandle->cellSet[0].outputIcc[0],
- outBuf, sizeof(void *) * 2 );
- // Execute Channel
- CHAN_execute( chanHandle, framenum );
- //
- // Pass JPG to network task
- //
- scomMsg.sizeLinear = jpg_size;
- scomMsg.motion = checkMotion( pMsgBuf->bufY );
- scomMsg.bufLinear = jpg_img;
- // Tell the capture routine we're done with its buffer
- SCOM_putMsg( fromProctoInput, pMsgBuf );
- // Give the JPG to networking
- SCOM_putMsg( fromProctoNet, &scomMsg );
- // Get our message buffer back from networking
- pMsgBuf = SCOM_getMsg( fromNettoProc, SYS_FOREVER );
- // JPG size may have changed
- jpg_size = pMsgBuf->sizeLinear;
- if( !jpg_size )
- continue;
- //
- // Handle Decode Channel
- //
- chanHandle = &thrProcess.chanListDecode[i];
- chanHandle->state = CHAN_ACTIVE;
- inBuf[0] = &jpg_size;
- inBuf[1] = jpg_img;
- ICC_setBuf( chanHandle->cellSet[0].inputIcc[0],
- inBuf, sizeof(void *) * 2 );
- outBuf[0] = (void *)dec_out_y;
- outBuf[1] = (void *)dec_out_u;
- outBuf[2] = (void *)dec_out_v;
- ICC_setBuf( chanHandle->cellSet[0].outputIcc[0],
- outBuf, sizeof(void *) * 3 );
- // Execute Channel
- CHAN_execute( chanHandle, framenum );
- if( netcmdArgs[CMD_SHOWDOTS] )
- showMotion( dec_out_y );
- // Send the buffer to the display task
- scomMsg.bufY = (void *)dec_out_y;
- scomMsg.bufU = (void *)dec_out_u;
- scomMsg.bufV = (void *)dec_out_v;
- SCOM_putMsg( fromProctoOut, &scomMsg );
- // Get our message buffer back from display
- SCOM_getMsg(fromOuttoProc, SYS_FOREVER);
- }
- }
- }