Hi, I have problems encoding the raw frames to h264 format using dmai.
I keep seeing black screen with some mosaic pixels on top of it when i display it on the lcd screen. Can anyone help to shed some light to where could the problem be.
#define BUFSIZEALIGN 128
#define NUM_CAPTURE_BUFS 3
#define NUM_DISPLAY_BUFS 2
Int appMain(){
Int32 bufSize, width, height;
Buffer_Handle encBuf = NULL;
Buffer_Attrs encBufAttrs = Buffer_Attrs_DEFAULT;
int encBufSize = 0; // size of encoded buffer
VideoStd_Type videoStd;
BufTab_Handle hCapBufTab = NULL;
Buffer_Handle cBuffer;
Capture_Attrs cAttrs = Capture_Attrs_OMAP3530_DEFAULT;
Capture_Handle hCapture = NULL;
BufTab_Handle hDisBufTab = NULL;
Buffer_Handle dBuffer;
Display_Attrs dAttrs = Display_Attrs_O3530_VID_DEFAULT;
Display_Handle hDisplay = NULL;
Engine_Handle hEngine = NULL;
Venc1_Handle hVe1 = NULL;
VIDENC1_Params params = Venc1_Params_DEFAULT;
VIDENC1_DynamicParams dynParams = Venc1_DynamicParams_DEFAULT;
BufferGfx_Attrs gfxAttrs = BufferGfx_Attrs_DEFAULT;
CERuntime_init();
Dmai_init();
char* engineName = "encode";
hEngine = Engine_open(engineName, NULL, NULL);
if (hEngine == NULL) {
fprintf(stderr,"Failed to open codec engine: \n");
goto cleanup;
}
if (Capture_detectVideoStd(NULL, &videoStd, &cAttrs ) < 0 )
{
printf( "Failed to detect input video standard, input connected?\n" );
goto cleanup;
}
if (BufferGfx_calcDimensions(videoStd, ColorSpace_UYVY, &gfxAttrs.dim) < 0 )
{
printf( "Failed to calculate dimensions for video driver buffers\n" );
goto cleanup;
}
if (VideoStd_getResolution(videoStd, &width, &height) < 0) {
goto cleanup;
}
params.maxBitRate = 4000000;
params.rateControlPreset = IVIDEO_STORAGE;
params.inputChromaFormat = XDM_YUV_422ILE;
params.maxWidth = 640;
params.maxHeight = 480;
params.maxInterFrameInterval = 1;
dynParams.targetBitRate = params.maxBitRate;
dynParams.inputWidth = params.maxWidth;
dynParams.inputHeight = params.maxHeight;
char* videoEncoder = "h264enc";
hVe1 = Venc1_create(hEngine, videoEncoder, ¶ms, &dynParams);
if (hVe1 == NULL) {
fprintf(stderr,"Failed to create video encoder: h264enc\n");
goto cleanup;
}
gfxAttrs.colorSpace = ColorSpace_UYVY;
cAttrs.numBufs = NUM_CAPTURE_BUFS;
cAttrs.colorSpace = ColorSpace_UYVY;
cAttrs.videoStd = videoStd;
cAttrs.videoInput = Capture_Input_COMPOSITE;
bufSize = BufferGfx_calcSize(videoStd, ColorSpace_UYVY );
bufSize = ( bufSize + 4096) & ( ~0xFFF );
hCapBufTab = BufTab_create(NUM_CAPTURE_BUFS, bufSize,
BufferGfx_getBufferAttrs(&gfxAttrs));
if (hCapBufTab == NULL) {
fprintf(stderr,"Failed to allocate contiguous buffers\n");
goto cleanup;
}
hCapture = Capture_create(hCapBufTab, &cAttrs);
if (hCapture == NULL) {
fprintf(stderr,"Failed to create capture device\n");
return 0;
} else {
printf("hCapture created successfully\n");
}
dAttrs.numBufs = NUM_DISPLAY_BUFS;
dAttrs.colorSpace = ColorSpace_UYVY;
dAttrs.videoStd = videoStd;
dAttrs.videoOutput = Display_Output_LCD;
//dAttrs.rotation=270; // pink and green lines appear - BUG
hDisBufTab = BufTab_create(NUM_DISPLAY_BUFS, bufSize, BufferGfx_getBufferAttrs(&gfxAttrs));
if (hDisBufTab == NULL) {
fprintf(stderr,"Failed to allocate contiguous buffers\n");
goto cleanup;
}
hDisplay = Display_create(hDisBufTab, &dAttrs);
if (hDisplay == NULL) {
fprintf(stderr,"Failed to create Display device\n");
return 0;
} else {
printf("hDisplay created successfully\n");
}
encBufSize = bufSize;
encBufAttrs.memParams.align = BUFSIZEALIGN;
encBufAttrs.memParams.type = Memory_CONTIGPOOL;
encBuf = Buffer_create(Dmai_roundUp(encBufSize, BUFSIZEALIGN), &encBufAttrs);
if ( encBuf == NULL )
{
printf( "Failed to allocate video buffer in video_thread_fxn.\n" );
goto cleanup;
}
int count = 0;
while (count < 1000){
/* Get a captured frame from the capture device */
if (Capture_get(hCapture, &cBuffer) < 0) {
fprintf(stderr,"Failed to get capture buffer\n");
return 0;
}
if(Venc1_process(hVe1, cBuffer, encBuf ) != Dmai_EOK )
{
printf( "Encode_video failed in video_thread_fxn\n" );
break;
}
// Get a frame from the display device to fill with data
if (Display_get(hDisplay, &dBuffer) < 0) {
fprintf(stderr,"Failed to get display buffer\n");
return 0;
}
memcpy(Buffer_getUserPtr(dBuffer), Buffer_getUserPtr(encBuf), Buffer_getSize(dBuffer));
// Give the frame from the display device to the capture device
if (Capture_put(hCapture, cBuffer) < 0) {
fprintf(stderr,"Failed to put capture buffer\n");
return 0;
}
// Give the frame from the capture device to the display device
if (Display_put(hDisplay, dBuffer) < 0) {
fprintf(stderr,"Failed to put display buffer\n");
return 0;
}
count++;
}
cleanup:
// Clean up the application
if (hCapture) {
Capture_delete(hCapture);
}
if (hDisplay) {
Display_delete(hDisplay);
}
if (hCapBufTab) {
BufTab_delete(hCapBufTab);
}
if (hDisBufTab) {
BufTab_delete(hDisBufTab);
}
if (hVe1) {
Venc1_delete(hVe1);
}
if (hEngine) {
Engine_close(hEngine);
}
return 1;
}