📄 spcaview.c
字号:
} //bitperpix = GetVideoBpp(); /* Clean up on exit */ atexit (SDL_Quit); printf ("SDL initialized.\n"); printf ("Using %s avifile \n", inputfile); if ((out_fd = AVI_open_input_file (inputfile, 1)) == NULL) { printf ("cannot open input file ? \n"); exit (1); } /* get the parameters from the file */ 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;} 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; 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; int testbpp=16; /*********************************/ pictstruct mypict ; pthread_t waitandshoot_id; int wstatus ; /*********************************/ RingBuffer.ptread =0; RingBuffer.ptwrite =0; /* spcaview grabber */ printf ("Initializing SDL.\n"); /* Initialize defaults, Video and Audio */ if ((SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)) { printf ("Could not initialize SDL: %s.\n", SDL_GetError ()); exit (-1); } /* Clean up on exit */ atexit (SDL_Quit); if(!owidth || !oheight){ owidth = image_width; oheight = image_height; } printf ("SDL initialized.\n"); /* validate parameters */ printf ("bpp %d format %d\n", bpp, format); if(!videoOn) { /* acquire raw data force palette to raw */ printf ("VideoOn = 0\n"); snprintf (fourcc, 5, "RAWD"); format = VIDEO_PALETTE_RAW_JPEG; } if(videocomp) { /* acquire yuv420p and compress diff pixels static huffman */ snprintf (fourcc, 5, "DPSH"); format = VIDEO_PALETTE_YUV420P; } if (videodevice == NULL || *videodevice == 0) { videodevice = "/dev/video0"; } printf ("Using video device %s.\n", videodevice); printf ("Initializing v4l.\n"); //v4l init if ((fd = open (videodevice, O_RDWR)) == -1) { perror ("ERROR opening V4L interface \n"); exit (1); } printf("**************** PROBING CAMERA *********************\n"); if (ioctl (fd, VIDIOCGCAP, &videocap) == -1) { printf ("wrong device\n"); exit (1); } printf("Camera found: %s \n",videocap.name); if (ioctl (fd, VIDIOCGCHAN, &videochan) == -1) { printf ("Hmm did not support Video_channel\n"); isVideoChannel = 0; } if (isVideoChannel){ videochan.norm = norme; videochan.channel = channel; if (ioctl (fd, VIDIOCSCHAN, &videochan) == -1) { printf ("ERROR setting channel and norme \n"); exit (1); } /************ just to be sure *************/ if (ioctl (fd, VIDIOCGCHAN, &videochan) == -1) { printf ("wrong device\n"); exit (1); } printf("Bridge found: %s \n",videochan.name); streamid = getStreamId (videochan.name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -