📄 encode.c
字号:
}
if (varInfo.xres != D1_WIDTH ||
varInfo.yres != D1_HEIGHT ||
varInfo.bits_per_pixel != SCREEN_BPP) {
ERR("Failed to get the requested screen size: %dx%d at %d bpp\n",
D1_WIDTH, D1_HEIGHT, SCREEN_BPP);
return FAILURE;
}
displays[0] = (char *) mmap (NULL,
D1_FRAME_SIZE * NUM_BUFS,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd, 0);
/* Map the video buffer to user space */
if (displays[0] == MAP_FAILED) {
ERR("Failed mmap on %s (%s)\n", FBVID_DEVICE, strerror(errno));
return FAILURE;
}
for (i=0; i<NUM_BUFS-1; i++) {
displays[i+1] = displays[i] + D1_FRAME_SIZE;
}
/* Clear the video buffers */
buf = (unsigned int *) displays[0];
for (i=0; i<D1_FRAME_SIZE * NUM_BUFS / sizeof(unsigned int); i++) {
buf[i] = UYVY_BLACK;
}
/* Set the display zoom factor */
if (resolution == ZOOM) {
zoom.WindowID = VID1_INDEX;
zoom.Zoom_H = ZOOM_2X;
zoom.Zoom_V = ZOOM_2X;
if (ioctl(fd, FBIO_SETZOOM, &zoom)) {
ERR("Failed setting zoom parameters\n");
return FAILURE;
}
printf("Display set to %dx%d and 2x zoom\n",varInfo.xres, varInfo.yres);
}
else {
zoom.WindowID = VID1_INDEX;
zoom.Zoom_H = ZOOM_1X;
zoom.Zoom_V = ZOOM_1X;
if (ioctl(fd, FBIO_SETZOOM, &zoom)) {
ERR("Failed setting zoom parameters\n");
return FAILURE;
}
printf("Display set to %dx%d and no zoom\n",varInfo.xres, varInfo.yres);
}
return fd;
}
/******************************************************************************
* cleanupDisplayDevice
******************************************************************************/
static void cleanupDisplayDevice(int fd, char *displays[])
{
struct Zoom_Params zoom;
zoom.WindowID = VID1_INDEX;
zoom.Zoom_H = ZOOM_1X;
zoom.Zoom_V = ZOOM_1X;
if (ioctl(fd, FBIO_SETZOOM, &zoom)) {
ERR("Failed setting zoom parameters\n");
}
munmap(displays[0], D1_FRAME_SIZE * NUM_BUFS);
close(fd);
}
/******************************************************************************
* waitForFrame
******************************************************************************/
static inline int waitForFrame(int fd)
{
struct timeval tv;
fd_set fds;
int ret;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
ret = select(fd + 1, &fds, NULL, NULL, &tv);
if (ret == -1) {
ERR("Select failed capture device (%s)\n", strerror(errno));
return FAILURE;
}
if (ret == 0) {
ERR("Select timed out\n");
return FAILURE;
}
return SUCCESS;
}
/******************************************************************************
* encodeThrFxn
******************************************************************************/
void *encodeThrFxn(void *arg)
{
enum InitLevels initLevel = 0;
DemoEnv *envp = (DemoEnv *) arg;
void *status = THREAD_SUCCESS;
char *cifBuf = NULL;
int displayIdx = 0;
int workingIdx = 1;
int framesDropped;
int encBufSize;
int captureFd;
int captureWidth=720;
int captureHeight=480;
char *displays[NUM_BUFS];
unsigned int numVidBufs;
VideoBuffer *vidBufs;
VIDDEC_Handle hDecode;
VIDENC_Handle hEncode;
Engine_Handle hEngine;
struct v4l2_buffer v4l2buf;
int frameSize;
int captureSize;
char *encBuf;
int fbFd;
char *dst;
char *src;
int y;
int fd;
struct one_block block;
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connect's address infordation
printf("Test Point 1-------------\n");
envp->address = "192.168.136.142";
if((he=gethostbyname(envp->address)) == NULL){ // eer the host info
herror("gethostbyname") ;
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socker");
exit(1);
}
printf("client:sockfd=%d\n",sockfd);
their_addr.sin_family = AF_INET; // host byte older
their_addr.sin_port = htons(5010); // sholl, netrork byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zerc therest of the struct
initLevel = DISPLAYDEVICEINITIALIZED;
captureFd = initCaptureDevice(envp->resolution, &vidBufs, &numVidBufs,
&captureWidth, &captureHeight);
if (captureFd == FAILURE) {
DBG("init capture errorr\n");
CLEANUP(THREAD_FAILURE);
}
captureSize = captureWidth * captureHeight * SCREEN_BPP / 8;
initLevel = CAPTUREDEVICEINITIALIZED;
/* Reset, load, and start DSP Engine */
if ((hEngine = Engine_open(ENGINE_NAME, NULL, NULL)) == NULL) {
ERR("Failed to open codec engine %s\n", ENGINE_NAME);
CLEANUP(THREAD_FAILURE);
}
initLevel = ENGINEOPENED;
/* Allocate and initialize video encoder on the engine */
if (videoEncodeAlgCreate(envp->bitrate,hEngine, &hEncode,
captureWidth, captureHeight) == FAILURE) {
CLEANUP(THREAD_FAILURE);
}
initLevel = VIDEOENCODERCREATED;
initLevel = VIDEODECODERCREATED;
if(envp->resolution==D1)
encBufSize=200000;
else
encBufSize=10000;
/* Allocate intermediate buffer (for encoded data) */
encBuf = (char *) Memory_contigAlloc(encBufSize,
Memory_DEFAULTALIGNMENT);
if (encBuf == NULL) {
ERR("ENCODE:Failed to allocate contiguous memory block.\n");
CLEANUP(THREAD_FAILURE);
}
DBG("encode:Contiguous buffer allocated at phyical address 0x%lx encBufSize=%d)\n",
Memory_getPhysicalAddress(encBuf),encBufSize);
initLevel = ENCODEDBUFFERALLOCATED;
initLevel = CIFBUFFERALLOCATED;
if((fd = open("encbuf",O_RDWR|O_CREAT|O_TRUNC)) == -1)
{
fprintf(stderr,"Open %s Error:%s\n","encbuf",strerror(errno));
exit(1);
}
/* Signal main thread that video initialization is done */
MET_CONDITION(encodeMutex, encodeInit);
/* Wait until all the other threads have done their initialization */
WAIT_CONDITION(allMutex, allInit);
while (!getQuit()) {
if (waitForFrame(captureFd) == FAILURE) {
BREAK_LOOP(THREAD_FAILURE);
}
/* Don't play if the demo is paused */
if (!getPlay()) {
usleep(100);
continue;
}
CLEAR(v4l2buf);
v4l2buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v4l2buf.memory = V4L2_MEMORY_MMAP;
/* Dequeue a frame buffer from the capture device driver */
if (ioctl(captureFd, VIDIOC_DQBUF, &v4l2buf) == -1) {
if (errno == EAGAIN) {
continue;
}
ERR("VIDIOC_DQBUF failed (%s)\n", strerror(errno));
BREAK_LOOP(THREAD_FAILURE);
}
frameSize = encBufSize;
/* Encode the buffer using H.264 */
if (encodeVideoBuffer(hEncode, vidBufs[v4l2buf.index].start,
captureSize, encBuf, &frameSize) == FAILURE) {
BREAK_LOOP(THREAD_FAILURE);
}
/* Issue capture buffer back to capture device driver */
if (ioctl(captureFd, VIDIOC_QBUF, &v4l2buf) == -1) {
ERR("VIDIOC_QBUF failed (%s)\n", strerror(errno));
BREAK_LOOP(THREAD_FAILURE);
}
if((numbytes = sendto(sockfd, encBuf, frameSize, 0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr_in))) == -1) {
perror("sendto");
exit(1);
}
////////////////DBG("send data numbytes=%d\n",numbytes);
incFrames();
incVideoBytesEncoded(frameSize);
printf("frames encoded is %d and Bytes encoded is %d\n", gbl.frames, gbl.videoBytesEncoded);
//usleep(10000);
}
cleanup:
close(fd);
if (initLevel >= CIFBUFFERALLOCATED && envp->resolution != D1) {
Memory_contigFree(cifBuf, CIF_FRAME_SIZE);
}
if (initLevel >= ENCODEDBUFFERALLOCATED) {
Memory_contigFree(encBuf, encBufSize);
}
if (initLevel >= VIDEODECODERCREATED) {
VIDDEC_delete(hDecode);
}
if (initLevel >= VIDEOENCODERCREATED) {
VIDENC_delete(hEncode);
}
if (initLevel >= ENGINEOPENED) {
dspTraceDump(hEngine);
Engine_close(hEngine);
}
if (initLevel >= CAPTUREDEVICEINITIALIZED) {
cleanupCaptureDevice(captureFd, vidBufs, numVidBufs);
}
if (initLevel >= DISPLAYDEVICEINITIALIZED) {
cleanupDisplayDevice(fbFd, displays);
}
return status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -