📄 input_plugin.c~
字号:
/* ============================================================================ Name : input_plugin.c Author : Zhang Yi Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <linux/videodev.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <signal.h>#include "input_plugin.h"#include "webserver.h"void *enc_handle= NULL;struct vdIn videoIn;extern globals pglobal;static int XDIM = 0;static int YDIM = 0;int init_videoIn(struct vdIn *vd, char *device, int width, int height, int format) { vd->width = width; vd->height = height; vd->formatIn = format; vd->buf=(unsigned char*)malloc(width*height*3); if ((vd->fd = open(device, O_RDWR)) == -1) { perror("ERROR opening camera device"); return -1; } if (ioctl(vd->fd, VIDIOCGCAP, &(vd->videocap)) == -1) { perror("ERROR opening video_capability"); return -1; } printf("Find camera: %s\n", vd->videocap.name); if (ioctl(vd ->fd, VIDIOCGPICT, &(vd->picture)) < 0) { perror("v4l_get_picture:"); return -1; } printf("colour:%d brightness:%d palette:%d depth:%d\n", vd->picture.colour, vd->picture.brightness, vd->picture.palette, vd->picture.depth); vd->picture.colour = 55535; //vd->picture.palette=21;//TODO vd->picture.brightness=6000; if (ioctl(vd->fd, VIDIOCSPICT, &(vd->picture)) < 0) { perror("ERROR setting video_picture"); return -1; } if (ioctl(vd->fd, VIDIOCGWIN, &(vd->videowin)) < 0) perror("get video_window failed"); vd->videowin.height = vd->height; vd->videowin.width = vd->width; if (ioctl(vd->fd, VIDIOCSWIN, &(vd->videowin)) < 0) perror("ERROR setting video_window"); if (ioctl(vd->fd, VIDIOCGWIN, &(vd->videowin)) < 0) perror("get video_window failed (after set)"); printf("VIDIOCSWIN height %d width %d \n", vd->videowin.height, vd->videowin.width); return 0;}int enc_init() { int xerr; xvid_gbl_init_t xvid_gbl_init; xvid_enc_create_t xvid_enc_create; /*------------------------------------------------------------------------ * XviD core initialization *----------------------------------------------------------------------*/ memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); xvid_gbl_init.version = XVID_VERSION; xvid_gbl_init.debug = 0; xvid_gbl_init.cpu_flags = 0; /* Initialize XviD core -- Should be done once per __process__ */ xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); /*------------------------------------------------------------------------ * XviD encoder initialization *----------------------------------------------------------------------*/ memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); xvid_enc_create.version = XVID_VERSION; /* Width and Height of input frames */ xvid_enc_create.width = XDIM; xvid_enc_create.height = YDIM; xvid_enc_create.profile = XVID_PROFILE_S_L0; xvid_enc_create.fincr = 1; xvid_enc_create.fbase = 3; /* Maximum key frame interval */ xvid_enc_create.max_key_interval = 0; /* Bframes settings */ xvid_enc_create.max_bframes = 0; xvid_enc_create.bquant_ratio = 0; xvid_enc_create.bquant_offset = 0; xvid_enc_create.frame_drop_ratio = 0; /* Global encoder options */ xvid_enc_create.global = XVID_GLOBAL_PACKED; xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); /* Retrieve the encoder instance from the structure */ enc_handle = xvid_enc_create.handle; return (xerr);}int enc_stop() { int xerr; /* Destroy the encoder instance */ xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); return (xerr);}int enc_main(unsigned char *image, unsigned char *bitstream) { int ret; xvid_enc_frame_t xvid_enc_frame; xvid_enc_stats_t xvid_enc_stats; /* Version for the frame and the stats */ memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); xvid_enc_frame.version = XVID_VERSION; memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); xvid_enc_stats.version = XVID_VERSION; /* Bind output buffer */ xvid_enc_frame.bitstream = bitstream; xvid_enc_frame.length = -1; /* Initialize input image fields */ if (image) { xvid_enc_frame.input.plane[0] = image; xvid_enc_frame.input.csp = XVID_CSP_BGR;//TODO xvid_enc_frame.input.stride[0] = XDIM*3; } else { xvid_enc_frame.input.csp = XVID_CSP_NULL; } /* Set up core's general features */ xvid_enc_frame.vol_flags = XVID_VOL_MPEGQUANT|XVID_VOL_QUARTERPEL|XVID_VOL_GMC; /* Frame type -- let core decide for us */ xvid_enc_frame.type = XVID_TYPE_AUTO; /* Force the right quantizer -- It is internally managed by RC plugins */ xvid_enc_frame.quant = 0; /* Set up motion estimation flags */ xvid_enc_frame.motion = XVID_ME_HALFPELREFINE16 | XVID_ME_CHROMA_PVOP | XVID_ME_ADVANCEDDIAMOND16; //xvid_enc_frame.quant= 4; /* Encode the frame */ ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); return (ret);}double msecond() { struct timeval tv; gettimeofday(&tv, 0); return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);}void *input_run() { char *device= "/dev/video0"; int format= VIDEO_PALETTE_JPEG; int width=614, height=384; memset(&videoIn, 0, sizeof(struct vdIn)); if (init_videoIn(&videoIn, device, width, height, format) != 0) { exit(-1); } /* xvid init */ XDIM = width; YDIM = height; pglobal.buf = (unsigned char *) malloc(XDIM*YDIM*2); if (enc_init()) { perror("Encore INIT problem"); exit(-1); } /* Xvid encode */ pglobal.stop=0; while (!pglobal.stop) { int size=read(videoIn.fd, videoIn.buf, XDIM*YDIM*3); if (size<0) { perror("error read!\n"); continue; } pthread_mutex_lock( &pglobal.db); pglobal.size =enc_main(videoIn.buf, pglobal.buf); printf("enc_main\n"); if (pglobal.size < 0) { printf("erro in encode....\n"); } /* signal fresh_frame */ pthread_cond_broadcast(&pglobal.db_update); pthread_mutex_unlock( &pglobal.db); } if (enc_handle) if (enc_stop()) perror("Encore RELEASE problem\n"); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -