📄 camera.cpp
字号:
exit_fatal ("could't set video palette Abort !");
if (vd->grabMethod)
{
if(debug)
printf (" grabbing method default MMAP asked \n");
// MMAP VIDEO acquisition
memset (&(vd->videombuf), 0, sizeof (vd->videombuf));
if (ioctl (vd->fd, VIDIOCGMBUF, &(vd->videombuf)) < 0)
{
perror (" init VIDIOCGMBUF FAILED\n");
}
if(debug)
printf ("VIDIOCGMBUF size %d frames %d offets[0]=%d offsets[1]=%d\n",
vd->videombuf.size, vd->videombuf.frames,
vd->videombuf.offsets[0], vd->videombuf.offsets[1]);
vd->pFramebuffer = (unsigned char *) mmap (0, vd->videombuf.size, PROT_READ | PROT_WRITE, MAP_SHARED, vd->fd, 0);
vd->mmapsize = vd->videombuf.size;
vd->vmmap.height = vd->hdrheight;
vd->vmmap.width = vd->hdrwidth;
vd->vmmap.format = vd->formatIn;
for (f = 0; f < vd->videombuf.frames; f++)
{
vd->vmmap.frame = f;
if (ioctl (vd->fd, VIDIOCMCAPTURE, &(vd->vmmap)))
{
perror ("cmcapture");
}
}
vd->vmmap.frame = 0;
}
else
{
/* read method */
/* allocate the read buffer */
vd->pFramebuffer = (unsigned char *) realloc (vd->pFramebuffer, (size_t) vd->framesizeIn);
if(debug)
printf (" grabbing method READ asked \n");
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(debug)
printf ("VIDIOCSWIN height %d width %d \n", vd->videowin.height, vd->videowin.width);
}
vd->frame_cour = 0;
return erreur;
}
void Camera::exit_fatal(char *messages)
{
printf("%s \n",messages);
exit(1);
}
int Camera::GetStreamId (const char *BridgeName)
{
int i = -1;
int match = -1;
/* return Stream_id otherwhise -1 */
if ((match = isSpcaChip (BridgeName)) < 0)
{
if(debug) printf ("Not an Spca5xx Camera !!\n");
return match;
}
switch (match)
{
case BRIDGE_SPCA505:
case BRIDGE_SPCA506:
{
i = YYUV;
break;
}
case BRIDGE_SPCA501:
{
i = YUYV;
break;
}
case BRIDGE_SPCA508:
{
i = YUVY;
break;
}
case BRIDGE_SPCA536:
case BRIDGE_SPCA504:
case BRIDGE_SPCA500:
case BRIDGE_SPCA504B:
case BRIDGE_SPCA533:
case BRIDGE_SPCA504C:
case BRIDGE_ZR364XX:
case BRIDGE_ZC3XX:
case BRIDGE_CX11646:
case BRIDGE_SN9CXXX:
case BRIDGE_MR97311:
{
i = JPEG;
break;
}
case BRIDGE_ETOMS:
case BRIDGE_SONIX:
case BRIDGE_SPCA561:
case BRIDGE_TV8532:
{
i = GBRG;
break;
}
default:
{
i = UNOW; // -1;
if(debug)
printf ("Unable to find a StreamId !!\n");
break;
}
}
return i;
}
int Camera::isSpcaChip (const char *BridgeName)
{
int i = -1;
int find = -1;
int size = 0;
/* Spca506 return more with channel video, cut it */
/* return Bridge otherwhise -1 */
for (i = 0; i < MAX_BRIDGE -1; i++)
{
size = strlen(Blist[i].name) ;
if(debug)
printf ("is_spca %s \n",Blist[i].name);
if (strncmp (BridgeName, Blist[i].name, size) == 0)
{
find = i;
break;
}
}
return find;
}
void Camera::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 \n",
videoparam->quality,videoparam->autobright,videoparam->time_interval);
}
int Camera::SetVideoPict (struct vdIn *vd)
{
if (ioctl (vd->fd, VIDIOCSPICT, &vd->videopict) < 0)
{
printf("vd->fd = %d\n", vd->fd);
exit_fatal ("Couldnt set videopict params with VIDIOCSPICT");
}
if(debug)
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;
}
int Camera::GetVideoPict (struct vdIn *vd)
{
if (ioctl (vd->fd, VIDIOCGPICT, &vd->videopict) < 0)
exit_fatal ("Couldnt get videopict params with VIDIOCGPICT");
if(debug)
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;
}
void* Camera::grab(void* arg)
{
int err = 0;
struct vdIn videoIn = *(struct vdIn *)arg;
videoIn.signalquit = 1;
for (;;)
{
//if(debug) printf("I am the GRABBER !!!!! \n");
err = Camera::v4lGrab(&videoIn);
if (!videoIn.signalquit || (err < 0))
{
if(debug)
printf("GRABBER going out !!!!! \n");
printf("err=%d\n", err);
printf("videoIn.signalquit =%d\n", videoIn.signalquit);
break;
}
}
return 0;
}
int Camera::v4lGrab (struct vdIn *vd )
{
struct frame_t *headerframe;
double timecourant =0;
static int frame = 0;
double temps = 0;
int jpegsize = 0;
int erreur = 0;
int size;
int len;
timecourant = ms_time();
if (vd->grabMethod == methodMmap)
{
vd->vmmap.height = vd->hdrheight;
vd->vmmap.width = vd->hdrwidth;
vd->vmmap.format = vd->formatIn;
if (ioctl (vd->fd, VIDIOCSYNC, &vd->vmmap.frame) < 0)
{
perror ("cvsync err\n");
erreur = -1;
}
/* Is there someone using the frame */
while((vd->framelock[vd->frame_cour] != 0) && vd->signalquit)
usleep(1000);
pthread_mutex_lock (&vd->grabmutex);
temps = ms_time();
jpegsize= convertframe(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),
vd->pFramebuffer + vd->videombuf.offsets[vd->vmmap.frame],
vd->hdrwidth,vd->hdrheight,vd->formatIn,vd->framesizeIn);
headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
snprintf(headerframe->header,5,"%s","SPCA");
headerframe->seqtimes = ms_time();
headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant);
headerframe->w = vd->hdrwidth;
headerframe->h = vd->hdrheight;
headerframe->size = (( jpegsize < 0)?0:jpegsize);
headerframe->format = vd->formatIn;
headerframe->nbframe = frame++;
pthread_mutex_unlock (&vd->grabmutex);
/************************************/
if ((ioctl (vd->fd, VIDIOCMCAPTURE, &(vd->vmmap))) < 0)
{
perror ("cmcapture");
if(debug)
printf (">>cmcapture err \n");
erreur = -1;
}
vd->vmmap.frame = (vd->vmmap.frame + 1) % vd->videombuf.frames;
vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
}
else if(vd->grabMethod == methodRead)
{
/* read method */
size = vd->framesizeIn;
len = read (vd->fd, vd->pFramebuffer, size);
if (len < 0 )
{
if(debug)
printf ("v4l read error\n");
if(debug)
printf ("len %d asked %d \n", len, size);
return 0;
}
/* Is there someone using the frame */
while((vd->framelock[vd->frame_cour] != 0)&& vd->signalquit)
usleep(1000);
pthread_mutex_lock (&vd->grabmutex);
temps = ms_time();
jpegsize= convertframe(vd->ptframe[vd->frame_cour]+ sizeof(struct frame_t),
vd->pFramebuffer ,
vd->hdrwidth,vd->hdrheight,vd->formatIn,vd->framesizeIn);
headerframe=(struct frame_t*)vd->ptframe[vd->frame_cour];
snprintf(headerframe->header,5,"%s","SPCA");
headerframe->seqtimes = ms_time();
headerframe->deltatimes=(int)(headerframe->seqtimes-timecourant);
headerframe->w = vd->hdrwidth;
headerframe->h = vd->hdrheight;
headerframe->size = (( jpegsize < 0)?0:jpegsize);
headerframe->format = vd->formatIn;
headerframe->nbframe = frame++;
vd->frame_cour = (vd->frame_cour +1) % OUTFRMNUMB;
pthread_mutex_unlock (&vd->grabmutex);
}
return erreur;
}
double Camera::ms_time (void)
{
static struct timeval tod;
gettimeofday(&tod, NULL);
return ((double) tod.tv_sec * 1000.0 + (double) tod.tv_usec / 1000.0);
}
int Camera::convertframe(unsigned char *dst,unsigned char *src, int width,int height, int formatIn, int size)
{
int jpegsize =0;
switch (formatIn)
{
case VIDEO_PALETTE_JPEG:
{
jpegsize = get_jpegsize(src, size);
if (jpegsize < 0)
break;
memcpy(dst,src,jpegsize);
break;
}
default:
break;
}
return jpegsize;
}
int Camera::get_jpegsize (unsigned char *buf, int insize)
{
int i;
for ( i= 1024 ; i< insize; i++)
{
if ((buf[i] == 0xFF) && (buf[i+1] == 0xD9))
return i+10;
}
return -1;
}
int Camera::close_v4l (struct vdIn *vd)
{
int i;
if (vd->grabMethod)
{
if(debug)
printf ("unmapping frame buffer\n");
munmap (vd->pFramebuffer, vd->mmapsize);
}
else
{
free(vd->pFramebuffer);
vd->pFramebuffer = NULL;
}
if(debug)
printf ("close video_device\n");
close (vd->fd);
/* dealloc the whole buffers */
if (vd->videodevice)
{
free (vd->videodevice);
vd->videodevice = NULL;
}
if (vd->cameraname)
{
free (vd->cameraname);
vd->cameraname = NULL;
}
for (i = 0; i < OUTFRMNUMB; i++)
{
if (vd->ptframe[i])
{
free (vd->ptframe[i]);
vd->ptframe[i] = NULL;
vd->framelock[i] = 0;
if(debug)
printf ("freeing output buffer %d\n",i);
}
}
pthread_mutex_destroy (&vd->grabmutex);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -