unsigned char *XVIDVideoCodec::compressFrame(VideoFrame *input, int *len) { int m4v_size; int key; int stats_type; int stats_quant; int stats_length; int sse[3]; IplImage *original = input->getImage(); IplImage *resized = cvCreateImage(cvSize(160, 120), IPL_DEPTH_8U, 3); cvResize(original, resized); memcpy(in_buffer, resized->imageData, resized->imageSize); m4v_size = enc_main(in_buffer, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, sse); cvReleaseImage(&resized); if (m4v_size <= 0) { wxMessageBox("Could not compress with XVid CODEC", _T("Error"), wxICON_ERROR | wxOK); return NULL; } unsigned char *retBuffer = new unsigned char[m4v_size]; memcpy(retBuffer, mp4_buffer, m4v_size); *len = m4v_size; return retBuffer; }
// Function name : PASCAL VideoCallbackProc // Description : Encode the captured frame // Return type : LRESULT FAR // Argument : HWND hWnd // Argument : LPVIDEOHDR lpVHdr LRESULT FAR PASCAL VideoCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr) { unsigned char *bufi, *buf; int type=0; int quant=0; int declen=0; int enclen=0; bufi = new unsigned char[lpVHdr->dwBytesUsed+40]; //original image buf = new unsigned char[lpVHdr->dwBytesUsed]; //coded stream memcpy((void *)(bufi), lpVHdr->lpData, lpVHdr->dwBytesUsed); unsigned char *buf1; buf1 = buf; if (m_vfwState==ENCDEC) { //Encode buf1 = (unsigned char*)ICSeqCompressFrame(&pc,0,bufi, &IsKeyFrame,&FrameSize); //enc_main(bufi, buf, (int *)&FrameSize, &IsKeyFrame, -1); //////////////////////////////// if (bSaveAVI){ AVIStreamSetFormat(pMainFrame->ps,pMainFrame->m_Frame++,lpbiTmp,sizeof(BITMAPINFO)); AVIStreamWrite(pMainFrame->ps,pMainFrame->m_Frame, 1, (LPBYTE)buf1, lpbiTmp->bmiHeader.biSizeImage,AVIIF_KEYFRAME,NULL,NULL); } //////////////////////////////// //Decode ICDecompress(hic2,0,&lpbiTmp->bmiHeader,buf1,&lpbiOut->bmiHeader,&bufo[40]); } else { enc_main(bufi,buf, &IsKeyFrame,&type,&quant,&enclen); declen = dec_main(buf, bufi, enclen,lpbiIn->bmiHeader.biWidth); pMainFrame->conv.YV12_to_RGB24(bufi, bufi+(lpbiIn->bmiHeader.biWidth*lpbiIn->bmiHeader.biHeight), bufi+(lpbiIn->bmiHeader.biWidth*lpbiIn->bmiHeader.biHeight*5/4), &bufo[40], lpbiIn->bmiHeader.biWidth, lpbiIn->bmiHeader.biHeight); } pMainFrame->GetActiveView()->InvalidateRect(NULL,FALSE); delete bufi; delete buf; return (LRESULT) TRUE; }
int main(void) { unsigned char *mp4_buffer = NULL; unsigned char *in_buffer = NULL; unsigned char *out_buffer = NULL; int status; long frame_type; long m4v_size; /***************************************************************************** * Arguments checking ****************************************************************************/ if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { my_printf("Trying to retreive width and height from PGM header\n"); } my_malloc_init(); // initial memory allocation. by gary input_file(); // it give input address,or open input file. by gary output_file(); // it give output address, or open output file . by gary /* now we know the sizes, so allocate memory */ in_buffer = (unsigned char *) my_malloc(IMAGE_SIZE(XDIM,YDIM)); if (!in_buffer) goto free_all_memory; /* this should really be enough memory ! */ mp4_buffer = (unsigned char *) my_malloc(IMAGE_SIZE(XDIM,YDIM)*2); if (!mp4_buffer) goto free_all_memory; clock_gettime(CLOCK_REALTIME, &start_time); /***************************************************************************** * XviD PART Start ****************************************************************************/ status = enc_init(); if (status) { my_printf("Encore INIT problem, return value %d\n", status); goto release_all; } /***************************************************************************** * Encoding loop ****************************************************************************/ do { /* read raw data (YUV-format) */ my_read(in_buffer, 1, IMAGE_SIZE(XDIM, YDIM), input_add); input_add = input_add + IMAGE_SIZE(XDIM, YDIM); /***************************************************************************** * Encode and decode this frame ****************************************************************************/ status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type); output_size += m4v_size; my_printf("Frame %5d: intra %1d, size=%6dbytes\n", (int)filenr, (int)frame_type, (int)m4v_size); /***************************************************************************** * Save stream to file ****************************************************************************/ /* Write mp4 data */ my_write(mp4_buffer, m4v_size, 1, output_add_temp); output_add_temp = output_add_temp + m4v_size; filenr++; } while ( filenr < ARG_FRAMENO ); /***************************************************************************** * Calculate totals and averages for output, print results ****************************************************************************/ clock_gettime(CLOCK_REALTIME, &end_time); printf("sizeof(start_time.tv_sec):%d, sizeof(start_time.tv_nsec):%d\n", sizeof(start_time.tv_sec), sizeof(start_time.tv_nsec)); printf("s_time.tv_sec:%d, s_time.tv_nsec:%d\n", start_time.tv_sec, start_time.tv_nsec); printf("e_time.tv_sec:%d, e_time.tv_nsec:%d\n", end_time.tv_sec, end_time.tv_nsec); double execution_time = (double)end_time.tv_sec + (double)end_time.tv_nsec/1000000000.0 - (double)start_time.tv_sec - (double)start_time.tv_nsec/1000000000.0; printf("diff_time:%.4f(s)\n", execution_time); my_memcpy(output_add,&output_size,4); finish_file(); output_size /= filenr; my_printf("Avg: filesize %7d bytes\n",(int)output_size); /***************************************************************************** * XviD PART Stop ****************************************************************************/ release_all: if (enc_handle) { status = enc_stop(); if (status) my_printf("Encore RELEASE problem return value %d\n", status); } free_all_memory: my_free(mp4_buffer); my_free(in_buffer); return 0; }