⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 摄像头驱动采集.txt

📁 摄像头驱动采集
💻 TXT
📖 第 1 页 / 共 2 页
字号:
gcc -o linux_m4v linux_m4v.c -lc -lm -lxvidcore




来源于互联网
*******************************************************************************************************/

#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 "xvid_encode.h"
void *enc_handle = NULL;

int done = 0;
int
enc_main(unsigned char *image,
   unsigned char *bitstream,
   int *key,
   int *stats_type,
   int *stats_quant,
   int *stats_length,
   int sse[3]);
int
enc_stop();
int

enc_init(int use_assembler);
double
msecond();



#define VIDEO_PALETTE_JPEG 21

struct vdIn {
int fd;
char *videodevice ;
struct video_capability videocap;
struct video_picture videopict;
struct video_window videowin;
int framesizeIn ;
int bppIn;
int hdrwidth;
int hdrheight;
int formatIn;
};

struct vdIn videoIn;
int init_videoIn (struct vdIn *vd, char *device, int width, int height,int format);
static int GetDepth (int format);
static int GetVideoPict (struct vdIn *vd);
static int SetVideoPict (struct vdIn *vd);


int main(int argc,char *argv[])
{
char *device;

int format = VIDEO_PALETTE_YUV420P;
int width = 352;
int height = 288;
int i;
unsigned char r_buffer[304128];
unsigned char* mp4_buffer;

double enctime;
double totalenctime = 0.;
float totalPSNR[3] = {0., 0., 0.};
device = "/dev/video0";
ARG_OUTPUTFILE = "test.m4v";
int use_assembler = 1;
int result;
int totalsize;
int m4v_size;
int key;
int stats_type;
int stats_quant;
int stats_length;
int input_num;
int output_num;

char filename[256];
FILE *out_file = NULL;

memset (&videoIn, 0, sizeof (struct vdIn));
if (init_videoIn(&videoIn, device, width, height,format) != 0)
        printf (" damned encore rate !!\n");
/* xvid init */
ARG_SAVEMPEGSTREAM = 1;
ARG_SAVEINDIVIDUAL = 0;

XDIM = width;
YDIM = height;
mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2);

totalsize = 0;

result = 0;
result = enc_init(0);
if (result != 0) {
   fprintf(stderr, "Encore INIT problem, return value %d\n",result);
   goto release_all;
}

/* i=read(videoIn.fd,r_buffer,304128);
printf("i read : %d\n",i);
if(i<0){
   printf("error read!\n");
   close(videoIn.fd);
   return -1;
   }
*/
input_num = 0;     /* input frame counter */
output_num = 0;



if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) {

   if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {
    fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);
    goto release_all;
   }

} else {
   out_file = NULL;
}



/* Xvid encode */
do {

   char *type;
   int sse[3];
   i=read(videoIn.fd,r_buffer,304128);
   printf("i read : %d\n",i);
   if(i<0){
    printf("error read!\n");
    close(videoIn.fd);
    return -1;
   }

   if (input_num >= ARG_MAXFRAMENR) {
    //result = 1;
    done = 1;
   }
   enctime = msecond();
   m4v_size =enc_main(!result ? (unsigned char*)r_buffer:0,mp4_buffer, &key, &stats_type,&stats_quant, &stats_length, sse);
   enctime = msecond() - enctime;
   printf("m4v_size is %d \n",m4v_size);
if (m4v_size < 0) {
    printf("erro in encode....\n");
   }

   /* Update encoding time stats */
   totalenctime += enctime;
   totalsize += m4v_size;
if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {

    /* Save single files */
    if (ARG_SAVEINDIVIDUAL) {
     FILE *out;
     sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
     out = fopen(filename, "w+b");
     fwrite(mp4_buffer, m4v_size, 1, out);
     fclose(out);
     output_num++;
    }
    /* Save ES stream */
    if (ARG_OUTPUTFILE && out_file)
     fwrite(mp4_buffer, 1, m4v_size, out_file);
   }

   input_num++;


} while (!done);



if(m4v_size<0){
   printf("error write_jpeg!\n");
   close(videoIn.fd);
   fclose(out_file);
   return -1;
   }
printf("OK.\n");

release_all:

if (enc_handle) {
   result = enc_stop();
   if (result)
    fprintf(stderr, "Encore RELEASE problem return value %d\n",
      result);
}


fclose(out_file);
close(videoIn.fd);
return 0;
}

int init_videoIn (struct vdIn *vd, char *device, int width, int height,
       int format)
{
int erreur;
vd->hdrwidth = width;
vd->hdrheight = height;
vd->formatIn = format; 
vd->bppIn = GetDepth (vd->formatIn);

if ((vd->fd = open (device, O_RDWR)) == -1){
       printf("ERROR opening V4L interface\n");
   close(vd->fd);
   return -1;
   }
if (ioctl (vd->fd, VIDIOCGCAP, &(vd->videocap)) == -1){
       printf("ERROR opening video_capability interface\n");
   close(vd->fd);
   return -1;
   }
printf ("Camera found: %s \n", vd->videocap.name);

erreur = GetVideoPict (vd);
vd->videopict.palette = vd->formatIn;
vd->videopict.depth = GetDepth (vd->formatIn);
vd->bppIn = GetDepth (vd->formatIn);

   vd->framesizeIn = (vd->hdrwidth * vd->hdrheight * vd->bppIn) >> 3;
   
erreur = SetVideoPict (vd);
erreur = GetVideoPict (vd);
if (vd->formatIn != vd->videopict.palette ||
         vd->bppIn != vd->videopict.depth){
       printf("ERROR set video_picture interface\n");
   close(vd->fd);
   return -1;
   }
if (erreur < 0){
       printf("ERROR set palette \n");
   close(vd->fd);
   return -1;
   }
if (ioctl (vd->fd, VIDIOCGWIN, &(vd->videowin)) < 0)
   perror ("VIDIOCGWIN failed \n");
      vd->videowin.height = vd->hdrheight;
      vd->videowin.width = vd->hdrwidth;
      if (ioctl (vd->fd, VIDIOCSWIN, &(vd->videowin)) < 0)
   perror ("VIDIOCSWIN failed \n");
if (ioctl (vd->fd, VIDIOCGWIN, &(vd->videowin)) < 0)
   perror ("VIDIOCGWIN failed \n");
      printf ("VIDIOCSWIN height %d width %d \n",
       vd->videowin.height, vd->videowin.width);
return 0;
}

static int
GetDepth (int format)
{
int depth;
switch (format)
    {
   
   case VIDEO_PALETTE_JPEG:
      {
depth = 8;  
      }
      break;
    case VIDEO_PALETTE_RAW:
      {
depth = 8;  
      }
      break;
    case VIDEO_PALETTE_YUV420P:
      {
//depth = (8 * 3) >> 1;
depth = 24;
      }
      break;
    case VIDEO_PALETTE_RGB565:
      depth = 16;
      break;
    case VIDEO_PALETTE_RGB24:
      depth = 24;
      break;
    case VIDEO_PALETTE_RGB32:
      {
depth = 32;
      }
      break;
    default:
      depth = -1;
      break;
    }
return depth;
}

static int
GetVideoPict (struct vdIn *vd)
{
if (ioctl (vd->fd, VIDIOCGPICT, &vd->videopict) < 0){
       printf("ERROR opening video_capability interface\n");
   close(vd->fd);
   return -1;
   }


printf ("VIDIOCGPICT brightnes=%d hue=%d color=%d contrast=%d whiteness=%d"
   "depth=%d palette=%d\n", vd->videopict.brightness,
   vd->videopict.hue, vd->videopict.colour, vd->videopict.contrast,
   vd->videopict.whiteness, vd->videopict.depth,
   vd->videopict.palette);

return 0;
}

static int
SetVideoPict (struct vdIn *vd)
{
if (ioctl (vd->fd, VIDIOCSPICT, &vd->videopict) < 0){
       printf("ERROR set video_capability interface\n");
   close(vd->fd);
   return -1;
   }

printf ("VIDIOCSPICT brightnes=%d hue=%d color=%d contrast=%d whiteness=%d"
   "depth=%d palette=%d\n", vd->videopict.brightness,
   vd->videopict.hue, vd->videopict.colour, vd->videopict.contrast,
   vd->videopict.whiteness, vd->videopict.depth,
   vd->videopict.palette);

return 0;
}

double
msecond()
{

struct timeval tv;

gettimeofday(&tv, 0);
return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);

}

#define FRAMERATE_INCR 1001

int

enc_init(int use_assembler)

{

int xerr;

//xvid_plugin_cbr_t cbr;

    xvid_plugin_single_t single;

xvid_plugin_2pass1_t rc2pass1;

xvid_plugin_2pass2_t rc2pass2;

//xvid_plugin_fixed_t rcfixed;

xvid_enc_plugin_t plugins[7];

xvid_gbl_init_t xvid_gbl_init;

xvid_enc_create_t xvid_enc_create;


/*------------------------------------------------------------------------

* XviD core initialization

*----------------------------------------------------------------------*/


/* Set version -- version checking will done by xvidcore */

memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));

xvid_gbl_init.version = XVID_VERSION;

    xvid_gbl_init.debug = 0;



/* Do we have to enable ASM optimizations ? */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -