视频采集.docx

上传人:b****6 文档编号:4375607 上传时间:2022-12-01 格式:DOCX 页数:15 大小:19.02KB
下载 相关 举报
视频采集.docx_第1页
第1页 / 共15页
视频采集.docx_第2页
第2页 / 共15页
视频采集.docx_第3页
第3页 / 共15页
视频采集.docx_第4页
第4页 / 共15页
视频采集.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

视频采集.docx

《视频采集.docx》由会员分享,可在线阅读,更多相关《视频采集.docx(15页珍藏版)》请在冰豆网上搜索。

视频采集.docx

视频采集

*****************************************************************************************************

linux_m4v.c

gcc-olinux_m4vlinux_m4v.c-lc-lm-lxvidcore

*******************************************************************************************************/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include"xvid_encode.h"

void*enc_handle=NULL;

intdone=0;

int

enc_main(unsignedchar*image,

  unsignedchar*bitstream,

  int*key,

  int*stats_type,

  int*stats_quant,

  int*stats_length,

  intsse[3]);

int

enc_stop();

int

enc_init(intuse_assembler);

double

msecond();

#defineVIDEO_PALETTE_JPEG 21

structvdIn{

 intfd;

 char*videodevice;

 structvideo_capabilityvideocap;

 structvideo_picturevideopict;

 structvideo_windowvideowin;

 intframesizeIn;

 intbppIn;

 int hdrwidth;

 int hdrheight;

 int formatIn;

};

structvdInvideoIn;

intinit_videoIn(structvdIn*vd,char*device,intwidth,intheight,intformat);

staticintGetDepth(intformat);

staticintGetVideoPict(structvdIn*vd);

staticintSetVideoPict(structvdIn*vd);

intmain(intargc,char*argv[])

{

 char*device;

 

 intformat=VIDEO_PALETTE_YUV420P;

 intwidth=352;

  intheight=288;

 inti;

 unsignedcharr_buffer[304128];

 unsignedchar*mp4_buffer;

 doubleenctime;

 doubletotalenctime=0.;

 floattotalPSNR[3]={0.,0.,0.};

 device="/dev/video0";

 ARG_OUTPUTFILE="test.m4v";

 intuse_assembler=1;

 intresult;

 inttotalsize;

 intm4v_size;

 intkey;

 intstats_type;

 intstats_quant;

 intstats_length;

 intinput_num;

 intoutput_num;

 charfilename[256];

 FILE*out_file=NULL;

 memset(&videoIn,0,sizeof(structvdIn));

  if(init_videoIn(&videoIn,device,width,height,format)!

=0)

      printf("damnedencorerate!

!

\n");

 /*xvidinit*/

 ARG_SAVEMPEGSTREAM=1;

 ARG_SAVEINDIVIDUAL=0;

 

 XDIM=width;

 YDIM=height;

 mp4_buffer=(unsignedchar*)malloc(IMAGE_SIZE(XDIM,YDIM)*2);

 

 totalsize=0;

 result=0;

 result=enc_init(0);

 if(result!

=0){

 fprintf(stderr,"EncoreINITproblem,returnvalue%d\n",result);

 gotorelease_all;

 }

/*i=read(videoIn.fd,r_buffer,304128);

 printf("iread:

%d\n",i);

 if(i<0){

 printf("errorread!

\n");

 close(videoIn.fd);

 return-1;

 }

*/

 input_num=0;   /*inputframecounter*/

 output_num=0;

 

 if(ARG_SAVEMPEGSTREAM&&ARG_OUTPUTFILE){

 if((out_file=fopen(ARG_OUTPUTFILE,"w+b"))==NULL){

  fprintf(stderr,"Erroropeningoutputfile%s\n",ARG_OUTPUTFILE);

  gotorelease_all;

 }

 }else{

 out_file=NULL;

 }

 

 

 /*Xvidencode*/

 do{

 char*type;

 intsse[3];

 i=read(videoIn.fd,r_buffer,304128);

 printf("iread:

%d\n",i);

 if(i<0){

  printf("errorread!

\n");

  close(videoIn.fd);

  return-1;

 }

 if(input_num>=ARG_MAXFRAMENR){

  //result=1;

  done=1;

 }

 enctime=msecond();

 m4v_size=enc_main(!

result?

(unsignedchar*)r_buffer:

0,mp4_buffer,&key,&stats_type,&stats_quant,&stats_length,sse);

 enctime=msecond()-enctime;

 printf("m4v_sizeis%d\n",m4v_size);

 if(m4v_size<0){

  printf("erroinencode....\n");

 }

 /*Updateencodingtimestats*/

 totalenctime+=enctime;

 totalsize+=m4v_size;

 if(m4v_size>0&&ARG_SAVEMPEGSTREAM){

  /*Savesinglefiles*/

  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++;

  }

  /*SaveESstream*/

  if(ARG_OUTPUTFILE&&out_file)

   fwrite(mp4_buffer,1,m4v_size,out_file);

 }

 input_num++;

 }while(!

done);

 

 if(m4v_size<0){

 printf("errorwrite_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,"EncoreRELEASEproblemreturnvalue%d\n",

    result);

 }

 

 fclose(out_file);

 close(videoIn.fd);

 return0;

}

intinit_videoIn(structvdIn*vd,char*device,intwidth,intheight,

      intformat)

{

 interreur;

 vd->hdrwidth=width;

  vd->hdrheight=height;

  vd->formatIn=format;

  vd->bppIn=GetDepth(vd->formatIn);

 

 if((vd->fd=open(device,O_RDWR))==-1){

     printf("ERRORopeningV4Linterface\n");

 close(vd->fd);

 return-1;

 }

 if(ioctl(vd->fd,VIDIOCGCAP,&(vd->videocap))==-1){

     printf("ERRORopeningvideo_capabilityinterface\n");

 close(vd->fd);

 return-1;

 }

  printf("Camerafound:

%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("ERRORsetvideo_pictureinterface\n");

 close(vd->fd);

 return-1;

 }

  if(erreur<0){

     printf("ERRORsetpalette\n");

 close(vd->fd);

 return-1;

 }

 if(ioctl(vd->fd,VIDIOCGWIN,&(vd->videowin))<0)

 perror("VIDIOCGWINfailed\n");

      vd->videowin.height=vd->hdrheight;

     vd->videowin.width=vd->hdrwidth;

     if(ioctl(vd->fd,VIDIOCSWIN,&(vd->videowin))<0)

 perror("VIDIOCSWINfailed\n");

 if(ioctl(vd->fd,VIDIOCGWIN,&(vd->videowin))<0)

 perror("VIDIOCGWINfailed\n");

     printf("VIDIOCSWINheight%d width%d\n",

      vd->videowin.height,vd->videowin.width);

 return0;

}

staticint

GetDepth(intformat)

{

 intdepth;

 switch(format)

   {

  

  caseVIDEO_PALETTE_JPEG:

     {

 depth=8; 

     }

     break;

   caseVIDEO_PALETTE_RAW:

     {

 depth=8; 

     }

     break;

   caseVIDEO_PALETTE_YUV420P:

     {

 //depth=(8*3)>>1;

 depth=24;

     }

     break;

   caseVIDEO_PALETTE_RGB565:

     depth=16;

     break;

   caseVIDEO_PALETTE_RGB24:

     depth=24;

     break;

   caseVIDEO_PALETTE_RGB32:

     {

 depth=32;

     }

     break;

   default:

     depth=-1;

     break;

   }

 returndepth;

}

staticint

GetVideoPict(structvdIn*vd)

{

 if(ioctl(vd->fd,VIDIOCGPICT,&vd->videopict)<0){

     printf("ERRORopeningvideo_capabilityinterface\n");

 close(vd->fd);

 return-1;

 }

 printf("VIDIOCGPICTbrightnes=%dhue=%dcolor=%dcontrast=%dwhiteness=%d"

  "depth=%dpalette=%d\n",vd->videopict.brightness,

  vd->videopict.hue,vd->videopict.colour,vd->videopict.contrast,

  vd->videopict.whiteness,vd->videopict.depth,

  vd->videopict.palette);

 return0;

}

staticint

SetVideoPict(structvdIn*vd)

{

 if(ioctl(vd->fd,VIDIOCSPICT,&vd->videopict)<0){

     printf("ERRORsetvideo_capabilityinterface\n");

 close(vd->fd);

 return-1;

 }

 printf("VIDIOCSPICTbrightnes=%dhue=%dcolor=%dcontrast=%dwhiteness=%d"

  "depth=%dpalette=%d\n",vd->videopict.brightness,

  vd->videopict.hue,vd->videopict.colour,vd->videopict.contrast,

  vd->videopict.whiteness,vd->videopict.depth,

  vd->videopict.palette);

 return0;

}

double

msecond()

{

 structtimevaltv;

 gettimeofday(&tv,0);

 return(tv.tv_sec*1.0e3+tv.tv_usec*1.0e-3);

}

#defineFRAMERATE_INCR1001

int

enc_init(intuse_assembler)

{

 intxerr;

 //xvid_plugin_cbr_tcbr;

    xvid_plugin_single_tsingle;

 xvid_plugin_2pass1_trc2pass1;

 xvid_plugin_2pass2_trc2pass2;

 //xvid_plugin_fixed_trcfixed;

 xvid_enc_plugin_tplugins[7];

 xvid_gbl_init_txvid_gbl_init;

 xvid_enc_create_txvid_enc_create;

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

 *XviDcoreinitialization

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

 /*Setversion--versioncheckingwilldonebyxvidcore*/

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

 xvid_gbl_init.version=XVID_VERSION;

    xvid_gbl_init.debug=0;

 /*DowehavetoenableASMoptimizations?

*/

 if(use_assembler){

 xvid_gbl_init.cpu_flags=0;

 }

 /*InitializeXviDcore--Shouldbedoneonceper__process__*/

 xvid_global(NULL,XVID_GBL_INIT,&xvid_gbl_init,NULL);

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

 *XviDencoderinitialization

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

 /*Versionagain*/

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

 xvid_enc_create.version=XVID_VERSION;

 /*WidthandHeightofinputframes*/

 xvid_enc_create.width=XDIM;

 xvid_enc_create.height=YDIM;

 xvid_enc_create.profile=XVID_PROFILE_S_L3;

 /*initplugins */

    xvid_enc_create.zones=NULL;

    xvid_enc_create.num_zones=0;

 xvid_enc_create.plugins=NULL;

 xvid_enc_create.num_plugins=0;

 /*Nofancythreadtests*/

 xvid_enc_create.num_threads=0;

 /*Framerate-Dosomequickfloatfps=fincr/fbasehack*/

 if((ARG_FRAMERATE-(int)ARG_FRAMERATE)

 xvid_enc_create.fincr=1;

 xvid_enc_create.fbase=(int)ARG_FRAMERATE;

 }else{

 xvid_enc_create.fincr=FRAMERATE_INCR;

 xvid_enc_create.fbase=(int)(FRAMERATE_INCR*ARG_FRAMERATE);

 }

 /*Maximumkeyframeinterval*/

    if(ARG_MAXKEYINTERVAL>0){

        xvid_enc_create.max_key_interval=ARG_MAXKEYINTERVAL;

    }else{

     xvid_enc_create.max_key_interval=(int)ARG_FRAMERATE*10;

    }

 /*Bframessettings*/

 xvid_enc_create.max_bframes=0;

 xvid_enc_c

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1