Hi everyone,

I have done many works for the jpeg encoder on dm355 and refferenced some 
message on the mailing-list, and made a big prograss on it these days. But, I 
stiil have doubt about it, actually, I made a jpeg encoder demo based dvsdk 
1.3.00 to save jpeg files by capturing the dynamic YUV422 video, sometimes it 
worked ok, while sometimes it worked wrong. The pictures in the following (
(1)http://linux.omap.com/pipermail/davinci-linux-open-source/attachments/20090525/f55c210e/image_0.jpg
(2)http://linux.omap.com/pipermail/davinci-linux-open-source/attachments/20090525/f55c210e/image_1.jpg
(3)http://linux.omap.com/pipermail/davinci-linux-open-source/attachments/20090525/f55c210e/image_2.jpg
)
shows the problem I met.

I'm so vexed about the problem, so I put my code below. May someone help me, 
and I'm appreciating your help vey much.

/*
 *  ======== jpegenc.c ========
 *
 *  A simple example of encoding an image using the JPEG encoder.
 */
//codec engine headers
#include <xdc/std.h>
#include <ti/sdo/ce/Engine.h>
#include <ti/sdo/ce/osal/Memory.h>
#include <ti/sdo/ce/image1/imgenc1.h>
#include <ti/sdo/ce/trace/gt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <asm/types.h>
#include <linux/videodev.h>
#include <media/davinci_vpfe.h>
/* Demo headers */
#include <rendezvous.h>
#include <fifoutil.h>
#include <pause.h>
#include "encode.h"
#include "video.h"
#include "capture.h"
#include "jpegwriter.h"
#include "jpegenc.h"
/*
 *  The parameters for the encoding process. These can be changed to try
 *  different image sizes and JPEG features.
 */
#define SCANS 4
#define QVALUE 73
#define MAXCHNUM 4
/* this example only supports YUV422 interleaved */
#define CHROMAFORMAT   XDM_YUV_422ILE
//#define CHROMAFORMAT   XDM_YUV_422P
#define JPEGENGINENAME "image"
#define MOD_NAME "jpegenc"
/* Not a cached system, no buffer alignment constraints */
#define BUFALIGN Memory_DEFAULTALIGNMENT
static String progName     = "app";
static String encoderName  = "jpegenc";
static String image_base_name = "image";
static char *jpegenc_name[MAXCHNUM] = {"jpegenc", "jpegenc:1", "jpegenc:2", 
"jpegenc:3"};
GT_Mask curMask = {0,0};
void GTtrace()
{
/* init trace */
GT_init(); 
/* create a mask to allow a trace-print welcome message below */
GT_create(&curMask, MOD_NAME); 
/* Enable all trace for this module */
GT_set(MOD_NAME "=01234567"); 
GT_0trace(curMask, GT_2CLASS, "main> " MOD_NAME "\n");
GT_0trace(curMask, GT_1CLASS, "Application started.\n");
}
int jpegEncodeAlgCreate(Engine_Handle hEngine, IMGENC1_Handle *hEncodePtr, 
                        int width, int height, int num, XDAS_Int32 
*imageBufSize)
{
IMGENC1_Params image_encParams;
IMGENC1_DynamicParams image_dynParams;
IMGENC1_Status image_imgStatus;
IMGENC1_Handle hEncode;
image_encParams.size = sizeof(IMGENC1_Params);
image_encParams.maxWidth = width;
image_encParams.maxHeight = height;
image_encParams.maxScans = SCANS;
image_encParams.dataEndianness = XDM_DEFAULT;
image_encParams.forceChromaFormat = XDM_YUV_422ILE;
hEncode = IMGENC1_create(hEngine, jpegenc_name[num], &image_encParams);
if (NULL == hEncode)
{
fprintf(stderr, "error: can't open codec jpegenc\n");
return FAILURE;
}
/* set the parameters for encoding */
image_dynParams.size = sizeof(IMGENC1_DynamicParams);
image_dynParams.numAU = XDM_DEFAULT;
image_dynParams.inputChromaFormat = XDM_YUV_422ILE;
image_dynParams.inputWidth = width;
image_dynParams.inputHeight = height;
image_dynParams.captureWidth = 0;
image_dynParams.generateHeader = XDM_ENCODE_AU;
image_dynParams.qValue = QVALUE;
image_imgStatus.size = sizeof(image_imgStatus);
if (IMGENC1_control(hEncode, XDM_SETPARAMS, &image_dynParams, &image_imgStatus) 
==
XDM_EFAIL) {
fprintf(stderr, "error: could not set PARAMS: 0x%x\n", XDM_SETPARAMS);
return FAILURE;
}
/* ask the codec for buffer sizes - these are typically conservative */
if (IMGENC1_control(hEncode, XDM_GETBUFINFO, &image_dynParams, 
&image_imgStatus) ==
XDM_EFAIL) {
fprintf(stderr, "error: could not get BUFINFO: 0x%x\n", XDM_GETBUFINFO);
return FAILURE;
}
*imageBufSize = image_imgStatus.bufInfo.minOutBufSize[0];
*hEncodePtr = hEncode;
return SUCCESS;
}
void jpegEncode(IMGENC1_Handle enc, unsigned char *inBuf, unsigned int 
inBufSize,
                        unsigned char *encodedBuf, unsigned int *encodedBufSize)
{
Int32 status;
IMGENC1_InArgs encInArgs;
IMGENC1_OutArgs encOutArgs;
XDM1_BufDesc inBufDesc;
XDM1_BufDesc encodedBufDesc;
//XDAS_Int32                  numRead;
/* initialize buffer descriptors that hold input and output buffers */
inBufDesc.numBufs = 1;
inBufDesc.descs[0].bufSize = inBufSize;
inBufDesc.descs[0].buf = inBuf;
encodedBufDesc.numBufs = 1;
encodedBufDesc.descs[0].bufSize = *encodedBufSize;
encodedBufDesc.descs[0].buf = encodedBuf;
/* the size field of Args and Params structs must be initialized */
encInArgs.size    = sizeof(encInArgs);
encOutArgs.size   = sizeof(encOutArgs);
/* perform the image (JPEG) encoding */
status = IMGENC1_process(enc, &inBufDesc, &encodedBufDesc, &encInArgs,
&encOutArgs);
if (status == IMGENC1_EOK)
{
*encodedBufSize = encOutArgs.bytesGenerated;
}
else
{
printf( "Encoder frame processing FAILED, status = 0x%lx, "
"extendedError = 0x%lx\n", status,
encOutArgs.extendedError);
}
}
void SaveJpegFile( void * data, int len, char * basename, char * ext )
{
char name[80];
FILE * out;
int ret;
sprintf( name, "%s.%s", basename, ext );
if ((out = fopen(name, "wb")) == NULL) 
{
fprintf(stderr, "ERROR: can't write to file %s\n", name);
} 
else 
{
ret = fwrite(data, 1, len, out);
if ( ret != len ) 
{
fprintf( stderr
,"ERROR: '%s' only wrote %d bytes, expected %d bytes\n"
,name
,ret
,len
);
return FAILURE;
} 
else
{
fclose( out );
}
}
}
void *jpegencThrFxn(void *arg)
{
Engine_Handle  hEngine  = NULL;
IMGENC1_Handle  hJpegenc = NULL;
IMGENC1_Params  encParams;
IMGENC1_DynamicParams  dynParams;
IMGENC1_Status  imgStatus;
JpegencEnv *envp = (JpegencEnv *) arg;
void *status = THREAD_SUCCESS;
CaptureBufferElement ce;
int imageSize;
int imgWidth;
int imgHeight;
int ch;
int bufIdx;
char *captureBuffers[2];
char outFile[80];
char *image_encodedBuf[2];
XDAS_Int32 image_encodedBufSize = 0;
XDAS_Int32 image_BufSize[MAXCHNUM];
JPEGWriterBufferElement JpegwFlush            = { JPEGWRITER_FLUSH };
JPEGWriterBufferElement Jpegwe;
GTtrace();
CLEAR(image_encodedBuf);
CLEAR(captureBuffers);
if (envp->captureOp == CAPOP_COPY)
{
imageSize = envp->imageWidth * envp->imageHeight * SCREEN_BPP / 8;
imgWidth = envp->imageWidth;
imgHeight = envp->imageHeight;
} 
else
{
imageSize = (envp->imageWidth/2) *( envp->imageHeight/2) * SCREEN_BPP / 8;
imgWidth = envp->imageWidth / 2;
imgHeight = envp->imageHeight / 2;
}
/* open the Codec Engine */
if ((hEngine = Engine_open(ENGINE_NAME, NULL, NULL)) == NULL) 
{
fprintf(stderr, "ERROR: can't open engine %s\n", ENGINE_NAME);
cleanup(THREAD_FAILURE);
}
if(jpegEncodeAlgCreate(hEngine, &hJpegenc, imgWidth, imgHeight, 0, 
&image_encodedBufSize) == FAILURE)
{
cleanup(THREAD_FAILURE);
}
printf("image_encodedBufSize: %lu\n", image_encodedBufSize);
if ((image_encodedBuf[0] = Memory_contigAlloc(image_encodedBufSize, 
Memory_DEFAULTALIGNMENT)) == NULL) 
{
fprintf(stderr, "image_encodedBuf[0] alloc failed!\n");
cleanup(THREAD_FAILURE);
}
/* Allocate buffers for interacting with the capture thread */
for (bufIdx=0; bufIdx < 2; bufIdx++) 
{
captureBuffers[bufIdx] = Memory_contigAlloc(imageSize,
Memory_DEFAULTALIGNMENT);
if (captureBuffers[bufIdx] == NULL)
{
ERR("Failed to allocate contiguous memory block.\n");
cleanup(THREAD_FAILURE);
}
DBG("Capture buffer allocated at %#lx physical address %#lx\n", 
(unsigned long) captureBuffers[bufIdx], 
Memory_getPhysicalAddress(captureBuffers[bufIdx]));
ce.id      = 0;
ce.virtBuf = captureBuffers[bufIdx];
ce.physBuf = Memory_getPhysicalAddress(ce.virtBuf);
ce.bufSize = imageSize;
if (FifoUtil_put(envp->hCaptureInFifo, &ce) == FIFOUTIL_FAILURE) {
ERR("JPEG: 1 Failed to put buffer in output fifo\n");
cleanup(THREAD_FAILURE);
}
}
int savetime = 0;
int savenumber = 0;
/* Signal that initialization is done and wait for other threads */
Rendezvous_meet(envp->hRendezvousInit);
DBG("Entering video main loop.\n");
while (!gblGetQuit())
{ 
/* Pause processing? */
Pause_test(envp->hPause);
/* Get a buffer from the capture thread */
if (FifoUtil_get(envp->hCaptureOutFifo, &ce) == FIFOUTIL_FAILURE)
{
ERR("JPEG: 2 Failed to put buffer in output fifo\n");
breakLoop(THREAD_FAILURE);
}
/* Is the capture thread flushing the pipe? */
if (ce.id == CAPTURE_FLUSH)
{
breakLoop(THREAD_SUCCESS);
}
image_BufSize[0] = image_encodedBufSize;
jpegEncode(hJpegenc, ce.virtBuf, ce.bufSize, image_encodedBuf[0], (unsigned int 
*)&image_BufSize[0]);
if(savetime == 50)
{
sprintf( outFile, "%s_%d", image_base_name,savenumber, 0 );
printf("Images Saving....\n");
SaveJpegFile( image_encodedBuf[0], image_BufSize[0], outFile, "jpg" );
printf("image_BufSize[0] saved is %d....\n", image_BufSize[0]);
savetime = 0;
savenumber ++;
if(savenumber > 20)
savenumber = 0;
printf("Images Saved \n");
}
savetime++;
/* Send the buffer back to the capture thread */
if (FifoUtil_put(envp->hCaptureInFifo, &ce) == FIFOUTIL_FAILURE)
{
ERR("JPEG: 3 Failed to put buffer in output fifo\n");
breakLoop(THREAD_FAILURE);
}
}
cleanup:
/* free buffers */
if (image_encodedBuf[0]) 
{
Memory_contigFree(image_encodedBuf[0], image_encodedBufSize);
}
/* teardown the codec */
if (hJpegenc)
{
IMGENC1_delete(hJpegenc);
}
/* close the engine */
if (hEngine)
{
Engine_close(hEngine);
}
return status;
}


2009-05-26 



Jammy Dane 
_______________________________________________
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source

Reply via email to