📄 encode.c~
字号:
/*************************************************************************//** This file and the information contain herein is provided "AS IS" **//** without warranty of any kind, either expressed or implied, **//** including but not limited to the implied warranties of **//** merchantability and/or fitness for a particular purpose. **//** **//** Copyright (C) 2005 QPixel Technology Inc **//** Changed from testapp3.c for MemCam only/** All rights reserved. **//*************************************************************************/#include <stdio.h>#include <unistd.h>#include <sys/mman.h>#include <fcntl.h>#include <termios.h>#include <unistd.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <sys/ioctl.h>#include <sys/time.h>#include <time.h>#include <mntent.h>#include <signal.h>#include <asm/types.h>#define __user#include "../common/videodev2.h"#include <sys/types.h>#include <errno.h> /* perror() */#include <arpa/inet.h> /* inet_addr() */#include <sys/utsname.h> /* uname() */#include <errno.h>#ifdef JPEG_ENC#include "./qljpeg.h"#endif //JPEG_ENC#include "../common/qlioctl.h"#include "../common/qltypes.h"#include "qlstream.h"#include "state.h"//#include "mct.h"#include "panel.h"#include "voosd.h"#include "menu.h"#define DEC_TRICK_MODE#define MICHAEL_DEBUG#ifndef ENCODE_INTERVAL#define ENCODE_INTERVAL (1*60) #endif#ifdef MICHAEL_DEBUG#define DPrintf(fmt,parm...) printf(fmt,##parm)#else#define DPrintf(fmt,parm...)#endif#ifdef TEST_OSDextern void test_osd_start(DWORD hDev);extern void test_osd_stop(void);extern void test_osd_update(void);#endifunsigned long VerbosePrint= 1;volatile unsigned int bCtrlCReceived = FALSE;#ifdef FULL_DUPLEXvolatile unsigned int bCtrlCReceivedDec = FALSE; // Andy 2007.06.26: for full duplex#endifchar *currcmd= 0;unsigned long qldevoffset= 0;unsigned long swapbuff[1024];char *QlDevEncName[] = { "/dev/video0", "/dev/video1", "/dev/video2", "/dev/video3" };char *QlDevDecName[] = { "/dev/video16", "/dev/video17", "/dev/video18", "/dev/video19" };extern int iStateNow, iStateNext;struct ql201_app_v4l_buf_info { char *addr; unsigned long size;};//Add by Michael 2007-10-29 18:16:41extern void QL_clear_PbModeOSD();extern int PBTrickModeProcess(unsigned long hDecDev, int key);extern FILE *fdr_clip;extern unsigned long read_more_data(char *pbuf, unsigned long size);void ql201_endianswap(unsigned long *psrc, unsigned long size);#ifdef JPEG_ENC#define MAX_HEIGHT 576#define IMAGE_WIDTH 720#define IMAGE_HEIGHT 480typedef struct encode_task_paramEncodeJPEGThreadProceters{ unsigned long hEncDev; unsigned long dwQuality; unsigned long dwWidth; unsigned long dwHeight; unsigned long period; char *nameBase; int sub_en;} TEncodeTaskParameters;void EncodeJPEGThreadProc(unsigned long parameters){ FILE *output_file; TEncodeTaskParameters *ptEncodeParam = (TEncodeTaskParameters *)parameters; unsigned long dwImageWidth = ptEncodeParam->dwWidth; unsigned long dwImageHeight = ptEncodeParam->dwHeight; TQLJPEGInitOptions tInitOptions; unsigned char abyOut[20]; unsigned char *pbyJPEG; HANDLE hJPEGEnc; unsigned long dwFrameCount = 0; int size; pbyJPEG = malloc(dwImageWidth*dwImageHeight*3>>3); //assume one JPEG frame would not larger than this size if (pbyJPEG==NULL) { DPrintf("JPEG bitstream buffer allocate failed!\n"); pthread_exit(NULL); } DPrintf("Linux multi-thread encoding process start...\n"); tInitOptions.dwVersion = QLJPEG_VERSION; tInitOptions.dwWidth = dwImageWidth; tInitOptions.dwHeight = dwImageHeight; tInitOptions.bSubEn = ptEncodeParam->sub_en; tInitOptions.dwQuality = ptEncodeParam->dwQuality; tInitOptions.hEncDev = ptEncodeParam->hEncDev; if (QLJPEG_Initial(&hJPEGEnc, &tInitOptions) != 0) { DPrintf("JPEG encoder initialization failed!\n"); pthread_exit(NULL); } DPrintf("JPEG encoder instance: 0x%08x\n", hJPEGEnc); nice(18); while (1) { unsigned long now; if (bCtrlCReceived == TRUE) { break; } DPrintf("JPEG: Encoding frame %d...\n", dwFrameCount); now = *(unsigned long *)(0x8000001C); if (QLJPEG_Encode(hJPEGEnc, pbyJPEG, (DWORD *)&size) != 0) { DPrintf("Error encoding jpeg (%d)\n", dwFrameCount); break; } DPrintf("diff: %lu\n", *(unsigned long *)(0x8000001C) - now); if (strcmp(ptEncodeParam->nameBase, "0") !=0 ) { snprintf(abyOut, 20, "%s_%d.jpg", ptEncodeParam->nameBase, dwFrameCount); if ((output_file = fopen(abyOut, "wb")) == NULL) { DPrintf("open frame buffer output file failed\n"); pthread_exit(NULL); } fwrite(pbyJPEG, size, 1, output_file); abyOut[4] += 1; fclose(output_file); } DPrintf("finish compress (%d) , size=%d\n", dwFrameCount, size); dwFrameCount++; //sched_yield(); if (ptEncodeParam->period == 0) // encode only 1 frame break; usleep(ptEncodeParam->period << 3); } DPrintf("Got %d frames\n", dwFrameCount); QLJPEG_Release(&hJPEGEnc); if (pbyJPEG) free(pbyJPEG); DPrintf("Encode thread exiting...\n"); pthread_exit(NULL);}#endif //JPEG_ENCvoid *SigIntHanlder(int Signal) { signal(SIGALRM, (void*)Signal); bCtrlCReceived = TRUE; DPrintf("\nReceived CTRL+C ...\n"); return (0);}#ifdef FULL_DUPLEX// Andy 2007.06.26: for full duplexvoid *SigIntHanlderDec(int Signal) { signal(SIGALRM, (void*)Signal); bCtrlCReceivedDec = TRUE; DPrintf("\nReceived CTRL+C for Decoder...\n"); return (0);}#endifunsigned long cnt= 1;unsigned long hDev= 0;unsigned long hEncDev= 0;unsigned long hDecDev= 0;unsigned long hYuvInDev= 0;char *StreamingBuff= 0;//#define MAX_CHUNK_XFER_SIZE 16*1024#define QLES_BUFFER_NUM 4#define RETRY_SLEEP_TIME 20 //in milli secs#define MAX_RETRY_COUNT 3#if 0long EncodeAndStreamv4(char *FName, unsigned int capturetimeout, int doJpeg) { int strmnum=QLSTRM_ENC; long Err= 0; char FNameIdx[256]="Disabled"; char FNameVbi[256]="Disabled"; FILE *fd= 0, *fdIdx= 0, *fdVbi; unsigned long nCount= 0, ulTotolCaptured= 0; int bStopped= FALSE; struct utsname name; unsigned long ulActuTotalXfer= 0, ulActuCurrXfer= 0, ulMTU= 1316, pktcnt= 0; STREAMING_INFO StrmInfo= { 0 }; time_t tStartTime, tElapsedTime; unsigned int framescnt= 0; unsigned int lastindexreadtime= 0, indexsize= 0; unsigned int lastvbireadtime= 0, vbisize= 0; int retrycnt= 0, capturingmp4= 0; char *pDevName; char *outbuff; char *vbioutbuff; QL201_INEX_DATA UserIdxData; QL201_INEX_DATA UserVbiData; QP_VBI_DATAFORMAT vbi_format; char *pethernet_intf_nam= 0; char *udp_unicast_ipaddr= 0; int udp_unicast_portnum= 0; struct v4l2_buffer v4lbuf; int type;#ifdef JPEG_ENC TEncodeTaskParameters tEncodeParam; pthread_t pidEncodeThreadID; int iPolicy;#endif //JPEG_ENC#ifdef FRANK_TEST char file_name[32]; int times = 0;#endif int mode; unsigned long loop_forever= 0; int stop_mode = 0; bCtrlCReceived = FALSE; vbi_format.Enable= 0; signal(SIGINT, (void*)SigIntHanlder); if (!doJpeg) { fd = fopen( FName, "w+" ); if( fd == NULL ) { DPrintf("Unable to open file %s \n", FName); exit(1); } DPrintf("Opened file %s (handle=%x) \n", FName, fd); }start_again: DPrintf("Starting a new capture session \n"); //Now setup capture... time(&tStartTime); pDevName= QlDevEncName[qldevoffset]; hEncDev = open(pDevName, O_RDWR); if(hEncDev < 0) { DPrintf("Unable to Open (%s) hEncDev = (0x%08x)\r\n", pDevName, hEncDev); return (2); } if(!doJpeg && (strmnum==QLSTRM_ENC)) { enum ql201_v4l2_mpeg_streamtype strmtype= 0xFF; Err= ioctl(hEncDev, QL201_ENC_G_STRMTYPE, &strmtype); if ((strmtype & 0xFFFF) == STREAM_TYPE_MPEG4_STREAM) { capturingmp4= 1; //qt_init(0, fd); } } if(!doJpeg && (strmnum != QLSTRM_YUVOUT) && FName) {#ifndef NO_INDX_FILE if (1 || capturingmp4) { strcpy(FNameIdx, FName); strcat(FNameIdx, ".idx"); fdIdx = fopen( FNameIdx, "w+" ); if( fdIdx == NULL ) { fclose(fd); DPrintf("Unable to open file %s \n", FNameIdx); exit(1); } outbuff= malloc(36*8); UserIdxData.addr= (unsigned long) outbuff; UserIdxData.buffsize= (36*8); UserIdxData.allornone= 1; if(!outbuff) { DPrintf("Unable to allocate memory for index data\n"); fclose(fd); fclose(fdIdx); return (2); } }#endif DPrintf("Clip=%s, Index=%s, Vbi=%s \n", FName, FNameIdx, FNameVbi); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; mode = 0; Err = ioctl(hEncDev, QL201_ENC_S_AVSEP, &mode); //DPrintf("ioctl 0x%08x QL201_ENC_S_AVSEP return %d\n", hEncDev, Err);/* Added by James for test */#ifdef JPEG_ENC if (doJpeg) { DPrintf("Create jpeg thread...\n"); tEncodeParam.hEncDev = hEncDev; tEncodeParam.dwQuality = 80; ioctl(hEncDev, QL201_ENC_G_ENC_WIDTH, &tEncodeParam.dwWidth); ioctl(hEncDev, QL201_ENC_G_ENC_HEIGHT, &tEncodeParam.dwHeight); tEncodeParam.dwWidth = tEncodeParam.dwWidth & 0xFFF0; tEncodeParam.dwHeight = tEncodeParam.dwHeight & 0xFFF0; tEncodeParam.dwHeight = (tEncodeParam.dwHeight<144) ? 144 : tEncodeParam.dwHeight; tEncodeParam.sub_en = 0; tEncodeParam.nameBase = argv[cnt++]; tEncodeParam.period = strtoul(argv[cnt++], 0, 10);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -