📄 spcaview.c
字号:
framecount = (int) AVI_video_frames (out_fd); image_width = (int) AVI_video_width (out_fd); image_height = (int) AVI_video_height (out_fd); if(!owidth || !oheight){ owidth = image_width; oheight = image_height; } printf ("frame: %d width: %d height: %d \n", framecount, image_width, image_height); framerate = AVI_frame_rate (out_fd); printf (" framerate : %f \n", framerate); audiobits = AVI_audio_bits (out_fd); audiorate = AVI_audio_rate (out_fd); audioformat = AVI_audio_format (out_fd); printf ("audio rate: %d audio bits: %d audio format: %d\n", audiorate, audiobits, audioformat); compressor = AVI_video_compressor (out_fd); printf (" codec: %s \n", compressor); if (strncmp (compressor, "RGB4", 4) == 0) { format = VIDEO_PALETTE_RGB32; bpp = 4; } else if (strncmp (compressor, "RGB3", 4) == 0) { format = VIDEO_PALETTE_RGB24; bpp = 3; } else if (strncmp (compressor, "RGB2", 4) == 0) { format = VIDEO_PALETTE_RGB565; bpp = 2; } else if (strncmp (compressor, "I420", 4) == 0) { format = VIDEO_PALETTE_YUV420P; bpp = 3; } else if (strncmp (compressor, "MJPG", 4) == 0) { format = VIDEO_PALETTE_JPEG; bpp = 3; } else if (strncmp (compressor, "DPSH", 4) == 0) { format = VIDEO_PALETTE_YUV420P; videocomp = 1; frame_size = (image_width*image_height*3) >> 1; bpp = 3; } else { printf ("Cannot play this file try Mplayer or Xine \n"); AVI_close (out_fd); exit (1); } /* Init the player */ printf ("bpp %d format %d\n", bpp, format); //if (format == VIDEO_PALETTE_RAW_JPEG) // init_libjpeg_decoder (image_width, image_height); //pscreen = //SDL_SetVideoMode (image_width, image_height, bpp * 8, // // SDL_DOUBLEBUF | SDL_SWSURFACE);/* set the player screen always in 24 bits mode */ pscreen = SDL_SetVideoMode (owidth, oheight, testbpp, SDL_DOUBLEBUF | SDL_SWSURFACE); if (pscreen == NULL) { printf ("Couldn't set %d*%dx%d video mode: %s\n", image_width, image_height, bpp * 8, SDL_GetError ()); exit (1); } /* No Cursor in Player */ SDL_ShowCursor (0); SDL_WM_SetCaption ("Spcaview Player ", NULL); p = pscreen->pixels; synctime = 1000 / framerate; printf ("Frame time %d ms\n", synctime); if (AVI_audio_channels (out_fd) == 1) { expect.format=AUDIO_FORMAT; expect.freq=audiorate; expect.callback=callback_play; expect.samples=AUDIO_SAMPLES; expect.channels=CHANNELS; expect.userdata=(void*)&RingBuffer; if(SDL_OpenAudio(&expect,NULL)<0) { fprintf(stderr,"Couldn't open audio output device: %s\n",SDL_GetError()); SDL_CloseAudio(); SDL_Quit(); } /* Init the sound player */ bytes_per_read = 2 * audiorate / framerate; maxsound = audiolength = AVI_audio_bytes (out_fd); //printf ("bytes to read %d \n",bytes_per_read); initAudio =1; /* mute if not a raw PCM file */ if (audioformat == 1) audioout = 1; } i = 0; do { if (run) { intime = SDL_GetTicks (); /* read frame read video and audio */ err =readFrame (out_fd, i,&jpegData,&jpegSize,&RingBuffer, maxsound, &audiolength, &bytes_per_read, audioout, 1,videocomp); //printf("readframe %d audiolength %d position errors %d \n",i,audiolength,err); if (initAudio ){ initAudio = 0; SDL_PauseAudio(0); // start play } refresh_screen ( jpegData, p, format, image_width,image_height,owidth,oheight,jpegSize,0); SDL_Flip (pscreen); //switch the buffer and update screen pictime = SDL_GetTicks () - intime; //printf(" get pictimes %d expected %d \n",pictime,synctime); if (pictime < synctime) SDL_Delay (synctime - pictime); i++; } fflush (stdout); switch (run){ case 1: if (SDL_PollEvent (&sdlevent) == 1) { switch (sdlevent.type) { case SDL_KEYDOWN: switch (sdlevent.key.keysym.sym) { case SDLK_DOWN: SDL_PauseAudio(1); // stop play run = 0; break; case SDLK_s: if (run == 0) { getJpegPicture(jpegData,image_width,image_height, format,jpegSize,PICTURE,NULL); } break; case SDLK_SPACE: if (fullsize == 0) { pscreen = SDL_SetVideoMode (owidth, oheight, testbpp, SDL_DOUBLEBUF | SDL_FULLSCREEN | SDL_SWSURFACE); fullsize = 1; } else { pscreen = SDL_SetVideoMode (owidth, oheight, testbpp, SDL_DOUBLEBUF | SDL_SWSURFACE); fullsize = 0; } if (pscreen == NULL) { printf ("Couldn't set %d*%dx%d video mode: %s\n", owidth, oheight, testbpp, SDL_GetError ()); exit (1); } SDL_ShowCursor (0); break; default: printf ("\nStop asked\n"); quit = 0; break; } break; case SDL_QUIT: quit = 0; break; } } //end if poll break; case 0: if (SDL_WaitEvent (&sdlevent) == 1) { switch (sdlevent.type) { case SDL_KEYDOWN: switch (sdlevent.key.keysym.sym) { case SDLK_UP: initAudio = 1; run = 1; break; case SDLK_RIGHT: if (i < (framecount - 1)) { i++; err = readFrame (out_fd, i, &jpegData,&jpegSize, &RingBuffer, maxsound, &audiolength, &bytes_per_read, audioout, 1,videocomp); printf ("scanning up frame %d\n", i); refresh_screen (jpegData, p, format, image_width, image_height, owidth, oheight, jpegSize,0); SDL_Flip (pscreen); //switch the buffer and update screen } break; case SDLK_LEFT: if (i > 1) { i--; err = readFrame (out_fd, i, &jpegData,&jpegSize, &RingBuffer, maxsound, &audiolength, &bytes_per_read, audioout, 0,videocomp); printf ("scanning down frame %d\n", i); refresh_screen (jpegData, p, format, image_width, image_height, owidth, oheight, jpegSize,0); SDL_Flip (pscreen); //switch the buffer and update screen } break; case SDLK_s: if (run == 0) { getJpegPicture(jpegData,image_width,image_height, format,jpegSize,PICTURE,NULL); } break; case SDLK_SPACE: if (fullsize == 0) { pscreen = SDL_SetVideoMode (owidth, oheight, testbpp, SDL_DOUBLEBUF | SDL_FULLSCREEN | SDL_SWSURFACE); fullsize = 1; } else { pscreen = SDL_SetVideoMode (owidth, oheight, testbpp, SDL_DOUBLEBUF | SDL_SWSURFACE); fullsize = 0; } if (pscreen == NULL) { printf ("Couldn't set %d*%dx%d video mode: %s\n", owidth, oheight, testbpp, SDL_GetError ()); exit (1); } SDL_ShowCursor(0); refresh_screen(jpegData, p, format, image_width, image_height, owidth, oheight, jpegSize,0); SDL_Flip (pscreen); //switch the buffer and update screen break; default: printf ("\nStop asked\n"); quit = 0; break; } break; case SDL_QUIT: quit = 0; break; } } //end if wait break; } //end switch run } while (i < framecount && quit == 1); free (jpegData); //close_libjpeg_decoder (); if (audioout) { while ((RingBuffer.ptread != RingBuffer.ptwrite) && retry--){ SDL_Delay(10); //fprintf(stderr,"Waiting .. stop the player %d\n",retry); } SDL_CloseAudio(); // stop play audioout = 0; } AVI_close (out_fd); SDL_Quit ();return 0;} static void spcaPrintParam (int fd,struct video_param *videoparam);static void spcaSetAutoExpo(int fd, struct video_param * videoparam){ videoparam->chg_para = CHGABRIGHT; videoparam->autobright = !videoparam->autobright; if(ioctl(fd,SPCASVIDIOPARAM, videoparam) == -1){ printf ("autobright error !!\n"); } else spcaPrintParam (fd,videoparam); }static void spcaSetLightFrequency(int fd, struct video_param * videoparam, int light_freq){ videoparam->chg_para = CHGLIGHTFREQ; videoparam->light_freq = light_freq; if(ioctl(fd,SPCASVIDIOPARAM, videoparam) == -1){ printf ("light freqency error !!\n"); } else spcaPrintParam (fd,videoparam);}static int spcaSwitchLightFrequency(int fd, struct video_param * videoparam){ int light_freq; if(ioctl(fd,SPCAGVIDIOPARAM, videoparam) == -1){ printf ("wrong spca5xx device\n"); } else { light_freq = videoparam->light_freq; if(light_freq == 50) light_freq +=10; else if(light_freq == 60) light_freq = 0; else if(light_freq == 0) light_freq = 50; if(light_freq) printf ("Current light frequency filter: %d Hz\n", light_freq); else printf ("Current light frequency filter: NoFliker\n"); spcaSetLightFrequency(fd,videoparam,light_freq); } return light_freq;}static void spcaSetTimeInterval(int fd, struct video_param *videoparam, unsigned short time){ if (time < 1000) { videoparam->chg_para = CHGTINTER; videoparam->time_interval = time; if(ioctl(fd,SPCASVIDIOPARAM, videoparam) == -1){ printf ("frame_times error !!\n"); } else spcaPrintParam (fd,videoparam); }}static void spcaSetQuality(int fd, struct video_param *videoparam, unsigned char index){ if (index < 6) { videoparam->chg_para = CHGQUALITY; videoparam->quality = index; if(ioctl(fd,SPCASVIDIOPARAM, videoparam) == -1){ printf ("quality error !!\n"); } else spcaPrintParam (fd,videoparam); }}static void spcaPrintParam (int fd, struct video_param *videoparam){ if(ioctl(fd,SPCAGVIDIOPARAM, videoparam) == -1){ printf ("wrong spca5xx device\n"); } else printf("quality %d autoexpo %d Timeframe %d lightfreq %d\n", videoparam->quality,videoparam->autobright,videoparam->time_interval, videoparam->light_freq);}static void qualityUp(int fd,struct video_param *videoparam){ unsigned char index = videoparam->quality; index+=1; spcaSetQuality(fd,videoparam,index);}static void qualityDown(int fd,struct video_param *videoparam){ unsigned char index = videoparam->quality; if(index > 0) index--; spcaSetQuality(fd,videoparam,index);}static void timeUp(int fd, struct video_param *videoparam){ unsigned short index = videoparam->time_interval; index+=10; spcaSetTimeInterval(fd,videoparam,index);}static void timeDown(int fd, struct video_param *videoparam){ unsigned short index = videoparam->time_interval; if(index > 0) index -=10; spcaSetTimeInterval(fd,videoparam,index);}int spcaGrab (char *outputfile,char fourcc[4] , const char *videodevice, int image_width,int image_height, int format, int owidth, int oheight, int grabMethod,int videoOn,int audioout,int videocomp,int autobright,int statOn,int decodeOn) { /* Timing value used for statistic */ Uint32 total_decode_time = 0; Uint32 time = 0; Uint32 prevtime = 0; Uint32 synctime = 0; Uint32 decodetime = 0; double average_decode_time = 0; Uint32 intime = 0; Uint32 pictime = 0; Uint32 delaytime = 0; Uint32 compresstime = 0; /**********************************/ /* video structures */ int fd; /* default mmap */ int i, j, k, nframes = 2000, f, status; struct video_mmap vmmap; struct video_capability videocap; int mmapsize; struct video_mbuf videombuf; struct video_picture videopict; struct video_window videowin; struct video_channel videochan; struct video_param videoparam; int norme = 0; int channel = 0; /******** output screen pointer ***/ SDL_Surface *pscreen; SDL_Event sdlevent; unsigned char *p = NULL; unsigned char *pp = NULL; /**********************************/ /* avi parametres */ avi_t *out_fd; unsigned char *pFramebuffer; unsigned char *tmp = NULL; // don't forget the Till lesson unsigned char *dpshDest = NULL; u_int8_t *jpegData = NULL; // Till lesson a good pointer is a pointer NULL int jpegSize; int ff; int framecount = 0; /*********************************/ /* Starting Flags */ int run = 1; int quit = 1; int initAudio = 0; //flag start audio int method = 1; int streamid ; int isVideoChannel = 1; int frame_size = 0; int len = 0; /*********************************/ /* data for SDL_audioin && SDL_audio */ SDL_AudioSpec spec, result; SDL_AudioSpec expect; struct Rbuffer RingBuffer; int retry = 100; int ptread ; int ptwrite ; int err = 0; int bytes_per_read =0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -