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

📄 spcatools.c

📁 用于LINUX摄像头的相关程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************#	 	spcatools:  Set of tools to decode, extract video stream    ## frame by frame get with spcaview -v .The RAW DATA are decoded with        ## spcadecoder in userland  instead of kernel decoding                       ## 		Copyright (C) 2004 Michel Xhaard                            ##                                                                           ## This program is free software; you can redistribute it and/or modify      ## it under the terms of the GNU General Public License as published by      ## the Free Software Foundation; either version 2 of the License, or         ## (at your option) any later version.                                       ##                                                                           ## This program is distributed in the hope that it will be useful,           ## but WITHOUT ANY WARRANTY; without even the implied warranty of            ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             ## GNU General Public License for more details.                              ##                                                                           ## You should have received a copy of the GNU General Public License         ## along with this program; if not, write to the Free Software               ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA ##                                                                           #****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/file.h>#include <string.h>#include <SDL/SDL.h>#include "SDL_image.h"#include <sys/ioctl.h>#include <sys/mman.h>#include <errno.h>#include <fcntl.h>#include "spca50x.h"#include "avilib.h"#include "spcadecoder.h"#include "SFont.h"enum	{	RED,	GREEN,	BLUE,	MAXCOLOR,};	typedef struct pictfield {	int x;	int y;	char Channel[6];	char sgamma[6];	char sOffset[3];	char sGain [6];	unsigned char curv[256];	} pictfield;	static voidYUV420toRGB (unsigned char *src, unsigned char *dst, int width, int height,	     int flipUV, int ColSpace);static voidgetPictureName (char *Picture, char *videoName, char *plan, int frame,		char *ext){       	char tamp[80];	char myPicture[80];	int namesize;		memset (myPicture,'\0',sizeof(myPicture));	memset (tamp,'\0',sizeof(tamp));			namesize = strlen (videoName); 	snprintf (tamp, namesize - 3, "%s", videoName);		snprintf (myPicture, 80, "%s%s%03d.%s", tamp, plan, frame, ext);	memcpy (Picture, myPicture, strlen (myPicture));}static swapRB (char* Buffer ,int size){ char temp; int i; for (i = 0 ; i < size ; i+=3){ 	temp = Buffer[i];  	Buffer[i] =Buffer[i+2]; 	Buffer[i+2] = temp; }}static processFilter(unsigned char *inbuff, unsigned char *outbuff,int size){ 	char val;	int i,p,k,lastp;	int frameseq = 0x80;	for (i = 0, p=1 ,lastp=0,k=0;i < size; i ++){	if (inbuff[p] == 0xFF && inbuff[p+1]== 0xFF){		printf ("found Packet End at 0x%04X size 0x%04X isoseq: 0x%03X frameseq: 0x%03X \n",p,p-lastp,inbuff[p-1],frameseq++);		lastp=p;				p += 2; /* advance 2 Bytes so skip the header of 2 iso desc  Frame 0xFF 0xFF Nbyte*/		k -= 8; /* rewind 8 bytes remove end marquer */		}	outbuff[k++] = inbuff[p++];		}}static void editinfo( SDL_Surface *Escreen, SFont_Font *Font,  struct pictfield *PictChannel,		struct pictparam *Pictparam,struct spca50x_frame *frame, int numb, int mode){			char field[20];	char *gamma[] = {"2.200","1.700","1.450","1.000","0.689","0.588","0.454"};		PictChannel[RED].x =0;		PictChannel[GREEN].x = frame->width / 3;		PictChannel[BLUE].x = (frame->width / 3) << 1;		PictChannel[RED].y = frame->height - 72; // 3x24		PictChannel[GREEN].y = frame->height - 72;		PictChannel[BLUE].y = frame->height - 72;			memset (field,'\0',sizeof(field));	SFont_Write(Escreen, Font,0,		0, "Spcatools Edit" );	if (mode){		snprintf( field,30,"Frames: %03d",numb);	} else {	snprintf( field,30,"Pictures: %03d",numb);	}				SFont_Write(Escreen, Font,0,		12, field);	if (PictChannel[GREEN].x > 75) {	SFont_Write(Escreen, Font,PictChannel[RED].x ,		frame->height - 4*SFont_TextHeight(Font),PictChannel[RED].Channel  );	SFont_Write(Escreen, Font,PictChannel[GREEN].x ,		frame->height - 4*SFont_TextHeight(Font),PictChannel[GREEN].Channel  );	SFont_Write(Escreen, Font,PictChannel[BLUE].x ,		frame->height - 4*SFont_TextHeight(Font),PictChannel[BLUE].Channel  );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Offset: %03d",Pictparam->OffRed);		SFont_Write(Escreen, Font,PictChannel[RED].x ,		frame->height - 3*SFont_TextHeight(Font),field  );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Offset: %03d",Pictparam->OffGreen);		SFont_Write(Escreen, Font,PictChannel[GREEN].x ,		frame->height - 3*SFont_TextHeight(Font),field );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Offset: %03d",Pictparam->OffBlue);		SFont_Write(Escreen, Font,PictChannel[BLUE].x ,		frame->height - 3*SFont_TextHeight(Font),field  );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Gain: %1.3f",(float)(Pictparam->GRed)/256);			SFont_Write(Escreen, Font,PictChannel[RED].x ,		frame->height - 2*SFont_TextHeight(Font),field  );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Gain: %1.3f",(float)(Pictparam->GGreen)/256);		SFont_Write(Escreen, Font,PictChannel[GREEN].x ,		frame->height - 2*SFont_TextHeight(Font),field );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Gain: %1.3f",(float)(Pictparam->GBlue)/256);		SFont_Write(Escreen, Font,PictChannel[BLUE].x ,		frame->height - 2*SFont_TextHeight(Font),field  );	/* in case we make gamma for each channel */		memset (field,'\0',sizeof(field));		snprintf( field,20,"Gam: %s",gamma[Pictparam->gamma]);		SFont_Write(Escreen, Font,PictChannel[RED].x ,		frame->height - SFont_TextHeight(Font),field  );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Gam: %s",gamma[Pictparam->gamma]);	SFont_Write(Escreen, Font,PictChannel[GREEN].x ,		frame->height - SFont_TextHeight(Font),field );		memset (field,'\0',sizeof(field));		snprintf( field,20,"Gam: %s",gamma[Pictparam->gamma]);	SFont_Write(Escreen, Font,PictChannel[BLUE].x ,		frame->height - SFont_TextHeight(Font),field  );	} else {	printf ("Sorry Screen Width seem too small \n");	}}intmain (int argc, char *argv[]){	char *inputfile = NULL;	static char Picture[80];	FILE *OUTpict;	struct spca50x_frame *myframe;	int size = 0;	int sizein = 0;	avi_t *aviinput ;	char *compressor = NULL;	char *mode = NULL;	char *stream = NULL;	double framerate;	int frames = 0;	int nframes = 0;	int bpp = 0;		SDL_Surface *pscreen;	unsigned char *pixels = NULL;	SDL_Event sdlevent;	SDL_Event sdledit;	SFont_Font* Fontsmall;		int i;	int quitter = 1;	int quit1 = 1;	int run = 1;	int force_rgb = 0;	int extract = 0;	char *sizestring = NULL;	char *separateur;	int width = 0;	int height = 0;	int crop;		int *Offtemp = NULL ;	int *Gtemp = NULL ;	int change = 0;	int j = 0;	struct pictparam mypict;	struct pictparam *ptmypict;	struct pictfield mypictChannel[MAXCOLOR];		/* Initialize struct default picture */	mypict.change = 01;	mypict.gamma = 3;	mypict.OffRed = 0;	mypict.OffBlue = 0;	 	mypict.OffGreen = 0;	mypict.GRed = 256;	mypict.GBlue = 256;	mypict.GGreen = 256;		ptmypict = &mypict;	/* Initialize each channel */	strcpy(mypictChannel[RED].Channel,"Red");	strcpy(mypictChannel[GREEN].Channel,"Green");	strcpy(mypictChannel[BLUE].Channel,"Blue");		printf("spcatools version 0.1 28/02/2004 (c) mxhaard@magic.fr \n");	for (i = 1; i < argc; i++) {		/* skip bad arguments */		if (argv[i] == NULL || *argv[i] == 0 || *argv[i] != '-') {			continue;		}				if (strcmp (argv[i], "-i") == 0) {			if (i + 1 >= argc) {				printf ("No parameter specified with -i, aborting.\n");				exit (1);			}			inputfile = strdup (argv[i + 1]);		}		if (strcmp (argv[i], "-f") == 0) {			if (i + 1 >= argc) {				printf ("No parameter specified with -f, aborting.\n");				exit (1);			}			stream = strdup (argv[i + 1]);		}		if (strcmp (argv[i], "-p") == 0) {			if (i + 1 >= argc) {				printf ("No parameter specified with -p, aborting.\n");				exit (1);			}			mode = strdup (argv[i + 1]);		}		if (strcmp (argv[i], "-r") == 0) {				printf ("bgr instead rgb asked, great about standard !!\n");			force_rgb = 1 ;		}		if (strcmp (argv[i], "-e") == 0) {				printf ("extract raw frames from current avi \n");			extract = 1 ;		}		if (strcmp (argv[i], "-s") == 0) {			if (i + 1 >= argc) {				printf ("No parameter specified with -s, aborting.\n");				exit (1);			}			sizestring = strdup (argv[i + 1]);			width = strtoul (sizestring, &separateur, 10);			if (*separateur != 'x') {				printf ("Error in size use -s widthxheight \n");				exit (1);			} else {				++separateur;				height =					strtoul (separateur, &separateur, 10);				if (*separateur != 0)					printf ("hmm.. dont like that!! trying this height \n");				printf (" size width: %d height: %d \n",					width, height);			}		}		if (strcmp (argv[i], "-n") == 0) {			if (i + 1 >= argc) {				printf ("No parameter specified with -n, aborting.\n");				exit (1);			}			nframes = atoi (argv[i + 1]);		}	}		Fontsmall = SFont_InitFont(IMG_Load("/usr/local/share/spcatools/RedFont.png"));	if(!Fontsmall) {		fprintf(stderr, "An error occured while loading the font.");		exit(1);	}	if ((aviinput = AVI_open_input_file (inputfile, 1)) == NULL) {		printf ("Can't read avi file \n");		exit(1);	}	myframe = (struct spca50x_frame *)malloc((size_t) sizeof (struct spca50x_frame));	myframe->tmpbuffer = NULL;	myframe->data = NULL;	frames = AVI_video_frames (aviinput);	myframe->hdrwidth = AVI_video_width (aviinput);	myframe->hdrheight = AVI_video_height (aviinput);	compressor = AVI_video_compressor (aviinput);	framerate = AVI_frame_rate (aviinput);	/*stream input type */		if (strncmp (stream, "yyuv", 4) == 0) {			myframe->cameratype =YYUV;			} else if (strncmp (stream, "yuyv", 4) == 0) {			myframe->cameratype =YUYV;			} else if (strncmp (stream, "yuvy", 4) == 0) {			myframe->cameratype =YUVY;			} else if (strncmp (stream, "jpeg", 4) == 0) {			myframe->cameratype =JPEG;				} else if (strncmp (stream, "gbrg", 4) == 0) {			myframe->cameratype =GBRG;			} else if (strncmp (stream, "grey", 4) == 0) {			myframe->cameratype =GREY;				}			else {				printf ("No expected camera type -f omitted !!! , aborting.\n");				free(myframe);				exit(1);			}	if ( (width != 0) && (height != 0) &&(width < myframe->hdrwidth) && (height < myframe->hdrheight)){				myframe->width = width;		myframe->height = height;		myframe->method= 1;	} else {		myframe->width= myframe->hdrwidth;		myframe->height=myframe->hdrheight;		myframe->method= 0;	}	if (!myframe->method) {		/* nothing todo hardware found stream */		myframe->cropx1 = myframe->cropx2 = myframe->cropy1 = myframe->cropy2 = 0;	}	if (myframe->method & 0x01) {		/* cropping method */		if (myframe->hdrwidth > myframe->width) {			crop = (myframe->hdrwidth - myframe->width);			if(myframe->cameratype == JPEG) crop = crop >> 4;			myframe->cropx1 = (crop) >> 1;			myframe->cropx2 = myframe->cropx1 + ((crop)% 2);		} else {			myframe->cropx1 = myframe->cropx2 = 0;		}		if (myframe->hdrheight > myframe->height) {			crop = (myframe->hdrheight - myframe->height);			if (myframe->cameratype == JPEG) crop = crop >>4;			myframe->cropy1 = (crop) >> 1;			myframe->cropy2 = myframe->cropy1 + ((crop) % 2);		} else {			myframe->cropy1 = myframe->cropy2 = 0;		}		printf("Cropx1 %d Cropx2 %d Cropy1 %d Cropy2 %d \n",myframe->cropx1,myframe->cropx2,			myframe->cropy1,myframe->cropy2);	}	if (strncasecmp (compressor, "RAWD", 4) != 0) {		printf ("Not a spcaview raw file !!! , aborting.\n");		free(myframe);		exit(1);	}		/*output size */		if (strncmp (mode, "r32", 3) == 0) {			size = (myframe->width * myframe->height * 4) ;			myframe->format = VIDEO_PALETTE_RGB32;			bpp=4;			} else if (strncmp (mode, "r24", 3) == 0) {			size = (myframe->width * myframe->height * 3) ;			myframe->format = VIDEO_PALETTE_RGB24;				bpp=3;			} else if (strncmp (mode, "r16", 3) == 0) {			size = (myframe->width * myframe->height * 2) ;			myframe->format = VIDEO_PALETTE_RGB565;				bpp=2;			} else if (strncmp (mode, "yuv", 3) == 0) {			size = (myframe->width * myframe->height * 3) >> 1;			myframe->format = VIDEO_PALETTE_YUV420P;			bpp=3;			} else if (strncmp (mode, "jpg", 3) == 0) {			size = ( myframe->width * myframe->height );			myframe->format = VIDEO_PALETTE_RGB24;				bpp=3;			} else if (strncmp (mode, "byr", 3) == 0) {			size = ( myframe->width * myframe->height );			myframe->format = VIDEO_PALETTE_RGB24;			bpp=3;				}			else {				printf ("No expected size found -p omitted !!! , aborting.\n");				free(myframe);				exit(1);						}			if (myframe->cameratype == JPEG) 			init_jpeg_decoder();		/* alloc the output buffer */		myframe->data =(unsigned char*) realloc( myframe->data, (size_t) size);		printf("bpp %d mode %s stream %s \n",bpp,mode,stream);		if( bpp != 0){		pscreen = SDL_SetVideoMode (myframe->width, myframe->height, bpp * 8,					  SDL_DOUBLEBUF | SDL_SWSURFACE);		if (pscreen == NULL) {			printf ("Couldn't set %d*%dx%d video mode: %s\n",				myframe->width, myframe->height, bpp * 8,				SDL_GetError ());			exit (1);		}		} else {			printf ("No expected size found -p omitted !!! , aborting.\n");				free(myframe);			exit(1);		}	pixels= pscreen->pixels;	SDL_WM_SetCaption ("Spca-Tools-Box  M.Xhaard (@)2004 GPL", NULL);	if (extract) {		for (i=0;i < frames; i++){			sizein = AVI_frame_size (aviinput, i);			myframe->tmpbuffer = (unsigned char*) realloc( myframe->tmpbuffer, (size_t) sizein);			if (AVI_read_frame			    (aviinput, (unsigned char *) myframe->tmpbuffer) < 0)				printf ("Read error frames %d \n", i);			getPictureName (Picture, inputfile,"-E-", i, stream);			OUTpict = fopen (Picture,"wb");				fwrite (myframe->tmpbuffer , sizeof(char),

⌨️ 快捷键说明

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