Example #1
0
/*  It should set the capture resolution
    Cheated from the openCV cap_libv4l.cpp the method is the following:
    Turn off the stream (video_disable)
    Unmap buffers
    Close the filedescriptor
    Initialize the camera again with the new resolution
*/
int setResolution(struct vdIn *vd, int width, int height)
{
    int ret;
    DBG("setResolution(%d, %d)\n", width, height);

    vd->streamingState = STREAMING_PAUSED;
    if(video_disable(vd, STREAMING_PAUSED) == 0) {  // do streamoff
        DBG("Unmap buffers\n");
        int i;
        for(i = 0; i < NB_BUFFER; i++)
            munmap(vd->mem[i], vd->buf.length);

        if(CLOSE_VIDEO(vd->fd) == 0) {
            DBG("Device closed successfully\n");
        }

        vd->width = width;
        vd->height = height;
        if(init_v4l2(vd) < 0) {
            fprintf(stderr, " Init v4L2 failed !! exit fatal \n");
            return -1;
        } else {
            DBG("reinit done\n");
            video_enable(vd);
            return 0;
        }
    } else {
        DBG("Unable to disable streaming\n");
        return -1;
    }
    return ret;
}
Example #2
0
int
init_videoIn (struct vdIn *vd, char *device, int width, int height,
	      int format, int grabmethod)
{

  if (vd == NULL || device == NULL)
    return -1;
  if (width == 0 || height == 0)
    return -1;
  if (grabmethod < 0 || grabmethod > 1)
    grabmethod = 1;		//mmap by default;
  vd->videodevice = NULL;
  vd->status = NULL;
  vd->pictName = NULL;
  vd->videodevice = (char *) calloc (1, 16 * sizeof (char));
  vd->status = (char *) calloc (1, 100 * sizeof (char));
  vd->pictName = (char *) calloc (1, 80 * sizeof (char));
  snprintf (vd->videodevice, 12, "%s", device);
  vd->toggleAvi = 0;
  vd->getPict = 0;
  vd->signalquit = 1;
  vd->width = width;
  vd->height = height;
  vd->formatIn = format;
  vd->grabmethod = grabmethod;
  if (init_v4l2 (vd) < 0) {
    fprintf (stderr, " Init v4L2 failed !! exit fatal \n");
    goto error;;
  }
  /* alloc a temp buffer to reconstruct the pict */
  vd->framesizeIn = (vd->width * vd->height << 1);
  switch (vd->formatIn) {
  case V4L2_PIX_FMT_MJPEG:
    vd->tmpbuffer = (unsigned char *) calloc (1, (size_t) vd->framesizeIn);
    if (!vd->tmpbuffer)
      goto error;
    vd->framebuffer =
      (unsigned char *) calloc (1, (size_t) vd->width * (vd->height + 8) * 2);
    break;
  case V4L2_PIX_FMT_YUYV:
    vd->framebuffer = (unsigned char *) calloc (1, (size_t) vd->framesizeIn);
    break;
  default:
    fprintf (stderr, " should never arrive exit fatal !!\n");
    goto error;
    break;
  }
  if (!vd->framebuffer)
    goto error;
  return 0;
error:
  free (vd->videodevice);
  free (vd->status);
  free (vd->pictName);
  close (vd->fd);
  return -1;
}
Example #3
0
DialogCt::DialogCt(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogCt)
{
    ui->setupUi(this);
    if(init_v4l2() == FALSE)
    {
        printf("init error\n");
    }
    v4l2_grab();
    grabAndShow();
}
Example #4
0
int CameraManager2::init_videoIn(char *device, int width, int height, int format, int grabmethod)
{
    
    if (vd == NULL || device == NULL)
        return -1;
    if (width == 0 || height == 0)
        return -1;
    if (grabmethod < 0 || grabmethod > 1)
        grabmethod = 1;		//mmap by default;
    vd->videodevice = NULL;
    vd->status = NULL;
    vd->pictName = NULL;
    vd->videodevice = (char *) calloc (1, 16 * sizeof (char));
    vd->status = (char *) calloc (1, 100 * sizeof (char));
    vd->pictName = (char *) calloc (1, 80 * sizeof (char));
    snprintf (vd->videodevice, 12, "%s", device);
    vd->toggleAvi = 0;
    vd->getPict = 0;
    vd->signalquit = 1;
    vd->width = width;
    vd->height = height;
    vd->formatIn = format;
    vd->grabmethod = grabmethod;
    vd->isstreaming = 0;
    if (init_v4l2 () < 0) {
        fprintf (stderr, " Init v4L2 failed !! exit fatal \n");
        goto error;;
    }
    /* alloc a temp buffer to reconstruct the pict */
    vd->framesizeIn = (vd->width * vd->height << 1);

    vd->tmpbuffer = (unsigned char *) calloc (1, (size_t) vd->framesizeIn);
    if (!vd->tmpbuffer)
        goto error;
    vd->framebuffer = (unsigned char *) calloc (1, (size_t) vd->width * (vd->height + 8) * 2);

    if (!vd->framebuffer)
        goto error;
    return 0;
    
error:
    free (vd->videodevice);
    free (vd->status);
    free (vd->pictName);
    close (vd->fd);
    return -1;
}
Example #5
0
int main(int argc,char **argv)
{
	rfbScreenInfoPtr s;
	double t1, t2;
	double timeout;
	int nread;

	if (argc == 2)
		v4l2str = argv[1];
	printf("Using dev %s\n", v4l2str);
	if (argc == 3)
		v4l2fps = atoi(argv[2]);
	printf("Using fps %d\n", v4l2fps);

	if (open_v4l2() != 0)
		exit(EXIT_FAILURE);
	if (init_v4l2() != 0)
		exit(EXIT_FAILURE);
	if (alloc_mem() != 0)
		exit(EXIT_FAILURE);

	s = rfbGetScreen(&argc, argv, v4l2width, v4l2height, 8, 3, v4l2Bpp);
	s->desktopName = "v4l2tovnc";
	s->frameBuffer = (char *) v4l2buf;
	s->serverFormat.blueShift = 0;
	s->serverFormat.greenShift = 8;
	s->serverFormat.redShift = 16;

	timeout = 1.0 / v4l2fps;
	rfbInitServer(s);
	t1 = timestamp();
	while (rfbIsActive(s)) {
		t2 = timestamp();
		if ((t2 - t1) >= timeout) {
			nread = read_frame();
			if (nread < 0)
				break;
			rfbMarkRectAsModified(s, 0, 0, v4l2width, v4l2height);
			t1 = timestamp();
		}
		rfbProcessEvents(s, -1);
	}
	close_v4l2();
	return 0;
}
Example #6
0
int  init_videoIn(struct vdIn *vd, char *device, int width, int height, int fps, int format, int grabmethod, char *avifilename)
{
    int ret = -1;
    int i;
    if (vd == NULL || device == NULL)
        return -1;
    if (width == 0 || height == 0)
        return -1;
    if (grabmethod < 0 || grabmethod > 1)
        grabmethod = 1;		//mmap by default;

    vd->videodevice = NULL;
    vd->status = NULL;
    vd->pictName = NULL;
    vd->videodevice = (char *) calloc(1, 16 * sizeof(char));
    vd->status = (char *) calloc(1, 100 * sizeof(char));
    vd->pictName = (char *) calloc(1, 80 * sizeof(char));
    snprintf(vd->videodevice, 12, "%s", device);
    printf("video %s \n", vd->videodevice);
    vd->toggleAvi = 0;
   // vd->toggleAvi = 1;

    vd->avifile = NULL;
    vd->avifilename = avifilename;
    vd->recordtime = 0;
    vd->framecount = 0;
    vd->recordstart = 0;
  // vd->getPict = 0;
    vd->getPict = 1;


    vd->signalquit = 1;
    vd->width = width;
    vd->height = height;
    vd->fps = fps;
    vd->formatIn = format;
    vd->grabmethod = grabmethod;
    vd->fileCounter = 0;
    vd->rawFrameCapture = 0;
    vd->rfsBytesWritten = 0;
    vd->rfsFramesWritten = 0;
    vd->captureFile = NULL;
    vd->bytesWritten = 0;
    vd->framesWritten = 0;

    if (init_v4l2(vd) < 0) {
        printf(" Init v4L2 failed !! exit fatal \n");
        goto error;;
    }

    /* alloc a temp buffer to reconstruct the pict */
    vd->framesizeIn = (vd->width * vd->height << 1);

    switch (vd->formatIn) {
    case V4L2_PIX_FMT_MJPEG:
        vd->tmpbuffer =
                (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
        if (!vd->tmpbuffer)
            goto error;
        vd->framebuffer =(unsigned char *) calloc(1,(size_t) vd->width * (vd->height +8) * 2);
        break;

    case V4L2_PIX_FMT_YUYV:
        vd->framebuffer =(unsigned char *) calloc(1, (size_t) vd->framesizeIn);
        break;

    default:
        printf(" should never arrive exit fatal !!\n");
        goto error;
        break;
    }
    if (!vd->framebuffer)
        goto error;
    return 0;
error:
    free(vd->videodevice);
    free(vd->status);
    free(vd->pictName);
    close(vd->fd);
    return -1;
}
Example #7
0
int init_videoIn(struct vdIn *vd, char *device, int width,
                 int height, int fps, int format, int grabmethod, globals *pglobal, int id)
{
    if(vd == NULL || device == NULL)
        return -1;
    if(width == 0 || height == 0)
        return -1;
    if(grabmethod < 0 || grabmethod > 1)
        grabmethod = 1;     //mmap by default;
    vd->videodevice = NULL;
    vd->status = NULL;
    vd->pictName = NULL;
    vd->videodevice = (char *) calloc(1, 16 * sizeof(char));
    vd->status = (char *) calloc(1, 100 * sizeof(char));
    vd->pictName = (char *) calloc(1, 80 * sizeof(char));
    snprintf(vd->videodevice, 12, "%s", device);
    vd->toggleAvi = 0;
    vd->getPict = 0;
    vd->signalquit = 1;
    vd->width = width;
    vd->height = height;
    vd->fps = fps;
    vd->formatIn = format;
    vd->grabmethod = grabmethod;
    if(init_v4l2(vd) < 0) {
        fprintf(stderr, " Init v4L2 failed !! exit fatal \n");
        goto error;;
    }


    // enumerating formats
    int currentWidth, currentHeight = 0;
    struct v4l2_format currentFormat;
    currentFormat.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if(xioctl(vd->fd, VIDIOC_G_FMT, &currentFormat) == 0) {
        currentWidth = currentFormat.fmt.pix.width;
        currentHeight = currentFormat.fmt.pix.height;
        DBG("Current size: %dx%d\n", currentWidth, currentHeight);
    }

    pglobal->in[id].in_formats = NULL;
    for(pglobal->in[id].formatCount = 0; 1; pglobal->in[id].formatCount++) {
        struct v4l2_fmtdesc fmtdesc;
        fmtdesc.index = pglobal->in[id].formatCount;
        fmtdesc.type  = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if(xioctl(vd->fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) {
            break;
        }

        if (pglobal->in[id].in_formats == NULL) {
            pglobal->in[id].in_formats = (input_format*)calloc(1, sizeof(input_format));
        } else {
            pglobal->in[id].in_formats = (input_format*)realloc(pglobal->in[id].in_formats, (pglobal->in[id].formatCount + 1) * sizeof(input_format));
        }

        if (pglobal->in[id].in_formats == NULL) {
            DBG("Calloc/realloc failed: %s\n", strerror(errno));
            return -1;
        }


        memcpy(&pglobal->in[id].in_formats[pglobal->in[id].formatCount], &fmtdesc, sizeof(input_format));

        if(fmtdesc.pixelformat == format)
            pglobal->in[id].currentFormat = pglobal->in[id].formatCount;

        DBG("Supported format: %s\n", fmtdesc.description);
        struct v4l2_frmsizeenum fsenum;
        fsenum.index = pglobal->in[id].formatCount;
        fsenum.pixel_format = fmtdesc.pixelformat;
        int j = 0;
        pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions = NULL;
        pglobal->in[id].in_formats[pglobal->in[id].formatCount].resolutionCount = 0;
        pglobal->in[id].in_formats[pglobal->in[id].formatCount].currentResolution = -1;
        while(1) {
            fsenum.index = j;
            j++;
            if(xioctl(vd->fd, VIDIOC_ENUM_FRAMESIZES, &fsenum) == 0) {
                pglobal->in[id].in_formats[pglobal->in[id].formatCount].resolutionCount++;
                if (pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions == NULL) {
                    pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions =
                        (input_resolution*)calloc(1, sizeof(input_resolution));
                } else {
                    pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions =
                        (input_resolution*)realloc(pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions, j * sizeof(input_resolution));
                }

                if (pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions == NULL) {
                    DBG("Calloc/realloc failed\n");
                    return -1;
                }

                pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions[j-1].width = fsenum.discrete.width;
                pglobal->in[id].in_formats[pglobal->in[id].formatCount].supportedResolutions[j-1].height = fsenum.discrete.height;
                if(format == fmtdesc.pixelformat) {
                    pglobal->in[id].in_formats[pglobal->in[id].formatCount].currentResolution = (j - 1);
                    DBG("\tSupported size with the current format: %dx%d\n", fsenum.discrete.width, fsenum.discrete.height);
                } else {
                    DBG("\tSupported size: %dx%d\n", fsenum.discrete.width, fsenum.discrete.height);
                }
            } else {
                break;
            }
        }
    }

    /* alloc a temp buffer to reconstruct the pict */
    vd->framesizeIn = (vd->width * vd->height << 1);
    switch(vd->formatIn) {
    case V4L2_PIX_FMT_MJPEG:
        vd->tmpbuffer = (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
        if(!vd->tmpbuffer)
            goto error;
        vd->framebuffer =
            (unsigned char *) calloc(1, (size_t) vd->width * (vd->height + 8) * 2);
        break;
    case V4L2_PIX_FMT_YUYV:
        vd->framebuffer =
            (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
        break;
    default:
        fprintf(stderr, " should never arrive exit fatal !!\n");
        goto error;
        break;

    }

    if(!vd->framebuffer)
        goto error;
    return 0;
error:
    free(pglobal->in[id].in_parameters);
    free(vd->videodevice);
    free(vd->status);
    free(vd->pictName);
    CLOSE_VIDEO(vd->fd);
    return -1;
}
Example #8
0
int
init_videoIn(struct vdIn *vd, char *device, int width, int height,
	     int format, int grabmethod, int fps)
{
    //~ int ret = -1;
    //~ int i;
    if (vd == NULL || device == NULL)
	return -1;
    if (width == 0 || height == 0)
	return -1;
    if (grabmethod < 0 || grabmethod > 1)
	grabmethod = 1;		//mmap by default;
    vd->videodevice = NULL;
    vd->status = NULL;
    vd->pictName = NULL;
    if((vd->videodevice = (char *) calloc(1, 16 * sizeof(char)))==NULL){
		printf("couldn't calloc memory for:vd->videodevice\n");
		goto error1;
	}
    if((vd->status = (char *) calloc(1, 100 * sizeof(char)))==NULL){
		printf("couldn't calloc memory for:vd->status\n");
		goto error2;
	}
    if((vd->pictName = (char *) calloc(1, 80 * sizeof(char)))==NULL){
		printf("couldn't calloc memory for:vd->pictName\n");
		goto error3;
	}
    snprintf(vd->videodevice, 12, "%s", device);
    printf("video %s \n", vd->videodevice);
    vd->capAVI = FALSE;
    vd->AVIFName = DEFAULT_AVI_FNAME;
    vd->fps = fps;
    vd->getPict = 0;
    vd->signalquit = 1;
    vd->width = width;
    vd->height = height;
    vd->formatIn = format;
    vd->grabmethod = grabmethod;
    vd->fileCounter = 0;
    vd->rawFrameCapture = 0;
    vd->rfsBytesWritten = 0;
    vd->rfsFramesWritten = 0;
    vd->captureFile = NULL;
    vd->bytesWritten = 0;
    vd->framesWritten = 0;
	vd->signalquit=1;
	vd->capImage=FALSE;
	vd->ImageFName=DEFAULT_IMAGE_FNAME;
	vd->timecode.type = V4L2_TC_TYPE_25FPS;
	vd->timecode.flags = V4L2_TC_FLAG_DROPFRAME;
	
    if (init_v4l2(vd) < 0) {
	printf(" Init v4L2 failed !! exit fatal \n");
	goto error3;
    }
    /* alloc a temp buffer to reconstruct the pict (MJPEG)*/
    vd->framesizeIn = (vd->width * vd->height << 1);
    switch (vd->formatIn) {
    case V4L2_PIX_FMT_MJPEG:
	vd->tmpbuffer =
	    (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
	if (!vd->tmpbuffer) {
	   printf("couldn't calloc memory for:vd->tmpbuffer\n");
		goto error5;
	}
	vd->framebuffer =
	    (unsigned char *) calloc(1,
				     (size_t) vd->width * (vd->height +
							   8) * 2);
	break;
    case V4L2_PIX_FMT_YUYV:/*YUYV doesn't need a temp buffer*/
	vd->framebuffer =
	    (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
	break;
    default:
	printf(" should never arrive exit fatal !!\n");
	goto error5;
	break;
    }
    if (!vd->framebuffer) {
	printf("couldn't calloc memory for:vd->framebuffer\n");	
	goto error6;
	}
    return 0;
	/*error: clean up allocs*/
  error6:
	free(vd->framebuffer);
  error5:
    free(vd->tmpbuffer);
  error4:
	close(vd->fd);
  error3:
    free(vd->pictName);
  error2:
    free(vd->status);
  error1:
    free(vd->videodevice);
    return -1;
}
Example #9
0
int init_videoIn(struct vdIn *vd, char *device, int width,
                 int height, int fps, int format, int grabmethod, globals *pglobal)
{
    if (vd == NULL || device == NULL)
        return -1;
    if (width == 0 || height == 0)
        return -1;
    if (grabmethod < 0 || grabmethod > 1)
        grabmethod = 1;		//mmap by default;
    vd->videodevice = NULL;
    vd->status = NULL;
    vd->pictName = NULL;
    vd->videodevice = (char *) calloc (1, 16 * sizeof (char));
    vd->status = (char *) calloc (1, 100 * sizeof (char));
    vd->pictName = (char *) calloc (1, 80 * sizeof (char));
    snprintf (vd->videodevice, 16-1, "%s", device);
    vd->toggleAvi = 0;
    vd->getPict = 0;
    vd->signalquit = 1;
    vd->width = width;
    vd->height = height;
    vd->fps = fps;
    vd->formatIn = format;
    vd->grabmethod = grabmethod;
    if (init_v4l2 (vd) < 0) {
        fprintf (stderr, " Init v4L2 failed !! exit fatal \n");
        goto error;;
    }

    // enumerating v4l2 controls
    struct v4l2_queryctrl ctrl;
    pglobal->in.parametercount = 0;
    pglobal->in.in_parameters = malloc(0 * sizeof(input_control));
    /* Enumerate the v4l2 controls
     Try the extended control API first */
    #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
    ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
    if(0 == IOCTL_VIDEO(vd->fd, VIDIOC_QUERYCTRL, &ctrl)) {
        do {
            control_readed(vd, &ctrl, pglobal);
            ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
        } while(0 == ioctl (vd->fd, VIDIOC_QUERYCTRL, &ctrl));
    } else
    #endif
    {
        /* Fall back on the standard API */
        /* Check all the standard controls */
        int i;
        for(i = V4L2_CID_BASE; i<V4L2_CID_LASTP1; i++) {
            ctrl.id = i;
            if(IOCTL_VIDEO(vd->fd, VIDIOC_QUERYCTRL, &ctrl) == 0) {
                control_readed(vd, &ctrl, pglobal);
            }
        }

        /* Check any custom controls */
        for(i=V4L2_CID_PRIVATE_BASE; ; i++) {
            ctrl.id = i;
            if(IOCTL_VIDEO(vd->fd, VIDIOC_QUERYCTRL, &ctrl) == 0) {
                control_readed(vd, &ctrl, pglobal);
            } else {
                break;
            }
        }
    }

    // enumerating formats
    int currentWidth, currentHeight = 0;
    struct v4l2_format currentFormat;
    currentFormat.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if (IOCTL_VIDEO(vd->fd, VIDIOC_G_FMT, &currentFormat) == 0) {
        currentWidth = currentFormat.fmt.pix.width;
        currentHeight = currentFormat.fmt.pix.height;
        DBG("%Current size: dx%d\n", currentWidth, currentHeight);
    }

    pglobal->in.in_formats = malloc(0 * sizeof(struct v4l2_fmtdesc));
    for (pglobal->in.formatCount = 0; 1; pglobal->in.formatCount++) {
         struct v4l2_fmtdesc fmtdesc;
         fmtdesc.index = pglobal->in.formatCount;
         fmtdesc.type  = V4L2_BUF_TYPE_VIDEO_CAPTURE;
         if (IOCTL_VIDEO(vd->fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) {
             break;
         }
         pglobal->in.in_formats =
            (input_format*)realloc(pglobal->in.in_formats, (pglobal->in.formatCount + 1) * sizeof(input_format));
        memcpy(&pglobal->in.in_formats[pglobal->in.formatCount], &fmtdesc, sizeof(input_format));

        if (fmtdesc.pixelformat == format)
            pglobal->in.currentFormat = pglobal->in.formatCount;

        DBG("Supported format: %s\n", fmtdesc.description);
        struct v4l2_frmsizeenum fsenum;
        fsenum.index = pglobal->in.formatCount;
        fsenum.pixel_format = fmtdesc.pixelformat;
        int j = 0;
        pglobal->in.in_formats[pglobal->in.formatCount].supportedResolutions = malloc(0);
        pglobal->in.in_formats[pglobal->in.formatCount].resolutionCount = 0;
        pglobal->in.in_formats[pglobal->in.formatCount].currentResolution = -1;
        while (1) {
            fsenum.index = j;
            j++;
            if(IOCTL_VIDEO(vd->fd, VIDIOC_ENUM_FRAMESIZES, &fsenum) == 0) {
                pglobal->in.in_formats[pglobal->in.formatCount].resolutionCount++;
                pglobal->in.in_formats[pglobal->in.formatCount].supportedResolutions = (input_resolution*)
                realloc(pglobal->in.in_formats[pglobal->in.formatCount].supportedResolutions, j * sizeof(input_resolution));
                pglobal->in.in_formats[pglobal->in.formatCount].supportedResolutions[j-1].width = fsenum.discrete.width;
                pglobal->in.in_formats[pglobal->in.formatCount].supportedResolutions[j-1].height = fsenum.discrete.height;
                if (format == fmtdesc.pixelformat) {
                    pglobal->in.in_formats[pglobal->in.formatCount].currentResolution = (j-1);
                    DBG("\tSupported size with the current format: %dx%d\n", fsenum.discrete.width, fsenum.discrete.height);
                } else {
                    DBG("\tSupported size: %dx%d\n", fsenum.discrete.width, fsenum.discrete.height);
                }
            } else {
                break;
            }
        }
     }

  /* alloc a temp buffer to reconstruct the pict */
  vd->framesizeIn = (vd->width * vd->height << 1);
  switch (vd->formatIn) {
  case V4L2_PIX_FMT_MJPEG:
    vd->tmpbuffer = (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
    if (!vd->tmpbuffer)
      goto error;
    vd->framebuffer =
        (unsigned char *) calloc(1, (size_t) vd->width * (vd->height + 8) * 2);
    break;
  case V4L2_PIX_FMT_YUYV:
    vd->framebuffer =
        (unsigned char *) calloc(1, (size_t) vd->framesizeIn);
    break;
  default:
    fprintf(stderr, " should never arrive exit fatal !!\n");
    goto error;
    break;

  }

  if (!vd->framebuffer)
    goto error;
  return 0;
error:
  free(pglobal->in.in_parameters);
  free(vd->videodevice);
  free(vd->status);
  free(vd->pictName);
  close(vd->fd);
  return -1;
}
Example #10
0
struct video_device *create_video_device(char *device, int width, int height, int fps, int format, int jpeg_quality) {
    struct video_device *vd;
    struct v4l2_fmtdesc fmtdesc;
    int current_width, current_height = 0;
    struct v4l2_format current_format;
    struct v4l2_frmsizeenum fsenum;
    int j;

    vd = malloc(sizeof(struct video_device));

    if (device == NULL)
        return NULL;

    if (width == 0 || height == 0)
        return NULL;

    vd->device_filename = (char *) calloc(MAX_DEVICE_FILENAME, sizeof(char));
    snprintf(vd->device_filename, MAX_DEVICE_FILENAME, "%s", device);

    vd->width = width;
    vd->height = height;
    vd->fps = fps;
    vd->format_in = format;
    vd->use_streaming = 1; // Use mmap
    vd->jpeg_quality = jpeg_quality;

    vd->format_count = 0;
    vd->formats = NULL;

    vd->resolution_count = 0;
    vd->resolutions = NULL;

    if (init_v4l2(vd) < 0) {
        user_panic("Init V4L2 failed on device %s.", vd->device_filename);
    }

    // enumerating formats
    memset(&current_format, 0, sizeof(struct v4l2_format));
    current_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if (xioctl(vd->fd, VIDIOC_G_FMT, &current_format) == 0) {
        current_width = current_format.fmt.pix.width;
        current_height = current_format.fmt.pix.height;
        DBG("Current resolution is %dx%d on device %s.", current_width, current_height, vd->device_filename);
    }


    while (1) {
        memset(&fmtdesc, 0, sizeof(struct v4l2_fmtdesc));
        fmtdesc.index = vd->format_count;
        fmtdesc.type  = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (xioctl(vd->fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) {
            break;
        }

        if (vd->formats == NULL) {
            vd->formats = (struct v4l2_fmtdesc*) malloc(sizeof(struct v4l2_fmtdesc));
        } else {
            vd->formats = (struct v4l2_fmtdesc*) realloc(vd->formats, (vd->format_count + 1)  *sizeof(struct v4l2_fmtdesc));
        }

        memcpy(&vd->formats[vd->format_count], &fmtdesc, sizeof(struct v4l2_fmtdesc));

        if (fmtdesc.pixelformat == format) {
            vd->current_format_index = vd->format_count;
        }

        DBG("%s: Supported format: %s", vd->device_filename, fmtdesc.description);

        memset(&fsenum, 0, sizeof(struct v4l2_frmsizeenum));
        for (j = 0; ; j++) {
            fsenum.pixel_format = fmtdesc.pixelformat;
            fsenum.index = j;

            if (xioctl(vd->fd, VIDIOC_ENUM_FRAMESIZES, &fsenum) != 0) {
                break; // Stop enumeration
            }

            if (vd->resolutions == NULL) {
                vd->resolutions = malloc(sizeof(struct resolution));
            }
            else {
                vd->resolutions = realloc(vd->resolutions, (vd->resolution_count + 1)  *sizeof(struct resolution));
            }

            vd->resolutions[vd->resolution_count].width = fsenum.discrete.width;
            vd->resolutions[vd->resolution_count].height = fsenum.discrete.height;
            vd->resolutions[vd->resolution_count].pixelformat = fmtdesc.pixelformat;

            if (format == fmtdesc.pixelformat) {
                if (fsenum.discrete.width == width && fsenum.discrete.height == height) {
                    vd->current_resolution_index = vd->resolution_count;
                }
            }

            DBG("%s: supported size: %dx%d", vd->device_filename, fsenum.discrete.width, fsenum.discrete.height);

            vd->resolution_count += 1;
        }

        vd->format_count++;
    }

    switch(vd->format_in) {
        case V4L2_PIX_FMT_MJPEG:
            vd->framebuffer_size = vd->width * (vd->height + 8) * 2;
            vd->framebuffer = (unsigned char *) malloc(vd->framebuffer_size);
            break;
        case V4L2_PIX_FMT_YUYV:
            vd->framebuffer_size = vd->width * vd->height * 2;
            vd->framebuffer = (unsigned char *) malloc(vd->framebuffer_size);
            break;
        default:
            user_panic("init_video_in: Unsupported format.");
            break;
    }

    return vd;
}