示例#1
0
文件: libv4l2.c 项目: zccrs/opencv
static int v4l2_deactivate_read_stream(int index)
{
	int result;

	result = v4l2_streamoff(index);
	if (result)
		return result;

	/* No need to unqueue our buffers, streamoff does that for us */

	v4l2_unmap_buffers(index);

	v4l2_unrequest_read_buffers(index);

	devices[index].flags &= ~V4L2_STREAM_CONTROLLED_BY_READ;

	return 0;
}
示例#2
0
文件: libv4l2.c 项目: zccrs/opencv
int v4l2_ioctl(int fd, unsigned long int request, ...)
{
	void *arg;
	va_list ap;
	int result, index, saved_err;
	int is_capture_request = 0, stream_needs_locking = 0;

	va_start(ap, request);
	arg = va_arg(ap, void *);
	va_end(ap);

	index = v4l2_get_index(fd);
	if (index == -1)
		return SYS_IOCTL(fd, request, arg);

	/* Appearantly the kernel and / or glibc ignore the 32 most significant bits
	   when long = 64 bits, and some applications pass an int holding the req to
	   ioctl, causing it to get sign extended, depending upon this behavior */
	request = (unsigned int)request;

	/* Is this a capture request and do we need to take the stream lock? */
	switch (request) {
	case VIDIOC_QUERYCTRL:
	case VIDIOC_G_CTRL:
	case VIDIOC_S_CTRL:
		if (!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_QUERYCAP:
		is_capture_request = 1;
		break;
	case VIDIOC_ENUM_FMT:
		if (((struct v4l2_fmtdesc *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
				!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_ENUM_FRAMESIZES:
	case VIDIOC_ENUM_FRAMEINTERVALS:
		if (!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_TRY_FMT:
		if (((struct v4l2_format *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
				!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_S_FMT:
	case VIDIOC_G_FMT:
		if (((struct v4l2_format *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
		break;
	case VIDIOC_REQBUFS:
		if (((struct v4l2_requestbuffers *)arg)->type ==
				V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
		break;
	case VIDIOC_QUERYBUF:
	case VIDIOC_QBUF:
	case VIDIOC_DQBUF:
		if (((struct v4l2_buffer *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
		break;
	case VIDIOC_STREAMON:
	case VIDIOC_STREAMOFF:
		if (*((enum v4l2_buf_type *)arg) == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
	}

	if (!is_capture_request) {
		result = SYS_IOCTL(fd, request, arg);
		saved_err = errno;
		v4l2_log_ioctl(request, arg, result);
		errno = saved_err;
		return result;
	}


	if (stream_needs_locking) {
		pthread_mutex_lock(&devices[index].stream_lock);
		/* If this is the first stream related ioctl, and we should only allow
		   libv4lconvert supported destination formats (so that it can do flipping,
		   processing, etc.) and the current destination format is not supported,
		   try setting the format to RGB24 (which is a supported dest. format). */
		if (!(devices[index].flags & V4L2_STREAM_TOUCHED) &&
				!(devices[index].flags & V4L2_DISABLE_CONVERSION) &&
				v4lconvert_supported_dst_fmt_only(devices[index].convert) &&
				!v4lconvert_supported_dst_format(
					devices[index].dest_fmt.fmt.pix.pixelformat)) {
			struct v4l2_format fmt = devices[index].dest_fmt;

			V4L2_LOG("Setting pixelformat to RGB24 (supported_dst_fmt_only)");
			devices[index].flags |= V4L2_STREAM_TOUCHED;
			fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
			pthread_mutex_unlock(&devices[index].stream_lock);
			v4l2_ioctl(fd, VIDIOC_S_FMT, &fmt);
			pthread_mutex_lock(&devices[index].stream_lock);
			V4L2_LOG("Done setting pixelformat (supported_dst_fmt_only)");
		}
		devices[index].flags |= V4L2_STREAM_TOUCHED;
	}

	switch (request) {
	case VIDIOC_QUERYCTRL:
		result = v4lconvert_vidioc_queryctrl(devices[index].convert, arg);
		break;

	case VIDIOC_G_CTRL:
		result = v4lconvert_vidioc_g_ctrl(devices[index].convert, arg);
		break;

	case VIDIOC_S_CTRL:
		result = v4lconvert_vidioc_s_ctrl(devices[index].convert, arg);
		break;

	case VIDIOC_QUERYCAP: {
		struct v4l2_capability *cap = arg;

		result = SYS_IOCTL(devices[index].fd, VIDIOC_QUERYCAP, cap);
		if (result == 0)
			/* We always support read() as we fake it using mmap mode */
			cap->capabilities |= V4L2_CAP_READWRITE;
		break;
	}

	case VIDIOC_ENUM_FMT:
		result = v4lconvert_enum_fmt(devices[index].convert, arg);
		break;

	case VIDIOC_ENUM_FRAMESIZES:
		result = v4lconvert_enum_framesizes(devices[index].convert, arg);
		break;

	case VIDIOC_ENUM_FRAMEINTERVALS:
		result = v4lconvert_enum_frameintervals(devices[index].convert, arg);
		if (result)
			V4L2_LOG("ENUM_FRAMEINTERVALS Error: %s",
					v4lconvert_get_error_message(devices[index].convert));
		break;

	case VIDIOC_TRY_FMT:
		result = v4lconvert_try_format(devices[index].convert, arg, NULL);
		break;

	case VIDIOC_S_FMT: {
		struct v4l2_format src_fmt, *dest_fmt = arg;
		struct v4l2_pix_format req_pix_fmt;

		/* Don't be lazy on uvc cams, as this triggers a bug in the uvcvideo
		   driver in kernel <= 2.6.28 (with certain cams) */
		if (!(devices[index].flags & V4L2_IS_UVC) &&
				v4l2_pix_fmt_identical(&devices[index].dest_fmt, dest_fmt)) {
			*dest_fmt = devices[index].dest_fmt;
			result = 0;
			break;
		}

		if (v4l2_log_file) {
			int pixfmt = dest_fmt->fmt.pix.pixelformat;

			fprintf(v4l2_log_file, "VIDIOC_S_FMT app requesting: %c%c%c%c\n",
					pixfmt & 0xff,
					(pixfmt >> 8) & 0xff,
					(pixfmt >> 16) & 0xff,
					pixfmt >> 24);
		}

		if (devices[index].flags & V4L2_DISABLE_CONVERSION) {
			result = SYS_IOCTL(devices[index].fd, VIDIOC_TRY_FMT,
					dest_fmt);
			src_fmt = *dest_fmt;
		} else {
			result = v4lconvert_try_format(devices[index].convert, dest_fmt,
					&src_fmt);
		}

		if (result) {
			saved_err = errno;
			V4L2_LOG("S_FMT error trying format: %s\n", strerror(errno));
			errno = saved_err;
			break;
		}

		if (src_fmt.fmt.pix.pixelformat != dest_fmt->fmt.pix.pixelformat &&
				v4l2_log_file) {
			int pixfmt = src_fmt.fmt.pix.pixelformat;

			fprintf(v4l2_log_file, "VIDIOC_S_FMT converting from: %c%c%c%c\n",
					pixfmt & 0xff,
					(pixfmt >> 8) & 0xff,
					(pixfmt >> 16) & 0xff,
					pixfmt >> 24);
		}

		/* Maybe after try format has adjusted width/height etc, to whats
		   available nothing has changed (on the cam side) ? */
		if (!(devices[index].flags & V4L2_IS_UVC) &&
				v4l2_pix_fmt_identical(&devices[index].src_fmt, &src_fmt)) {
			v4l2_set_src_and_dest_format(index, &devices[index].src_fmt,
					dest_fmt);
			result = 0;
			break;
		}

		result = v4l2_check_buffer_change_ok(index);
		if (result)
			break;

		req_pix_fmt = src_fmt.fmt.pix;
		result = SYS_IOCTL(devices[index].fd, VIDIOC_S_FMT, &src_fmt);
		if (result) {
			saved_err = errno;
			V4L2_LOG_ERR("setting pixformat: %s\n", strerror(errno));
			/* Report to the app dest_fmt has not changed */
			*dest_fmt = devices[index].dest_fmt;
			errno = saved_err;
			break;
		}
		/* See if we've gotten what try_fmt promised us
		   (this check should never fail) */
		if (src_fmt.fmt.pix.width != req_pix_fmt.width ||
				src_fmt.fmt.pix.height != req_pix_fmt.height ||
				src_fmt.fmt.pix.pixelformat != req_pix_fmt.pixelformat) {
			V4L2_LOG_ERR("set_fmt gave us a different result then try_fmt!\n");
			/* Not what we expected / wanted, disable conversion */
			*dest_fmt = src_fmt;
		}

		v4l2_set_src_and_dest_format(index, &src_fmt, dest_fmt);
		break;
	}

	case VIDIOC_G_FMT: {
		struct v4l2_format *fmt = arg;

		*fmt = devices[index].dest_fmt;
		result = 0;
		break;
	}

	case VIDIOC_REQBUFS: {
		struct v4l2_requestbuffers *req = arg;

		/* IMPROVEME (maybe?) add support for userptr's? */
		if (req->memory != V4L2_MEMORY_MMAP) {
			errno = EINVAL;
			result = -1;
			break;
		}

		result = v4l2_check_buffer_change_ok(index);
		if (result)
			break;

		/* No more buffers then we can manage please */
		if (req->count > V4L2_MAX_NO_FRAMES)
			req->count = V4L2_MAX_NO_FRAMES;

		result = SYS_IOCTL(devices[index].fd, VIDIOC_REQBUFS, req);
		if (result < 0)
			break;
		result = 0; /* some drivers return the number of buffers on success */

		devices[index].no_frames = MIN(req->count, V4L2_MAX_NO_FRAMES);
		devices[index].flags &= ~V4L2_BUFFERS_REQUESTED_BY_READ;
		break;
	}

	case VIDIOC_QUERYBUF: {
		struct v4l2_buffer *buf = arg;

		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		/* Do a real query even when converting to let the driver fill in
		   things like buf->field */
		result = SYS_IOCTL(devices[index].fd, VIDIOC_QUERYBUF, buf);
		if (result || !v4l2_needs_conversion(index))
			break;

		buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index;
		buf->length = V4L2_FRAME_BUF_SIZE;
		if (devices[index].frame_map_count[buf->index])
			buf->flags |= V4L2_BUF_FLAG_MAPPED;
		else
			buf->flags &= ~V4L2_BUF_FLAG_MAPPED;
		break;
	}

	case VIDIOC_QBUF:
		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		/* With some drivers the buffers must be mapped before queuing */
		if (v4l2_needs_conversion(index)) {
			result = v4l2_map_buffers(index);
			if (result)
				break;
		}

		result = SYS_IOCTL(devices[index].fd, VIDIOC_QBUF, arg);
		break;

	case VIDIOC_DQBUF: {
		struct v4l2_buffer *buf = arg;

		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		if (!v4l2_needs_conversion(index)) {
			result = SYS_IOCTL(devices[index].fd, VIDIOC_DQBUF, buf);
			if (result) {
				int saved_err = errno;

				V4L2_LOG_ERR("dequeuing buf: %s\n", strerror(errno));
				errno = saved_err;
			}
			break;
		}

		/* An application can do a DQBUF before mmap-ing in the buffer,
		   but we need the buffer _now_ to write our converted data
		   to it! */
		if (devices[index].convert_mmap_buf == MAP_FAILED) {
			devices[index].convert_mmap_buf = (void *)SYS_MMAP(NULL,
				(size_t)(devices[index].no_frames * V4L2_FRAME_BUF_SIZE),
				PROT_READ | PROT_WRITE,
				MAP_ANONYMOUS | MAP_PRIVATE,
				-1, 0);
			if (devices[index].convert_mmap_buf == MAP_FAILED) {
				saved_err = errno;
				V4L2_LOG_ERR("allocating conversion buffer\n");
				errno = saved_err;
				result = -1;
				break;
			}
		}

		result = v4l2_dequeue_and_convert(index, buf, 0, V4L2_FRAME_BUF_SIZE);
		if (result < 0)
			break;

		buf->bytesused = result;
		buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index;
		buf->length = V4L2_FRAME_BUF_SIZE;
		if (devices[index].frame_map_count[buf->index])
			buf->flags |= V4L2_BUF_FLAG_MAPPED;
		else
			buf->flags &= ~V4L2_BUF_FLAG_MAPPED;

		result = 0;
		break;
	}

	case VIDIOC_STREAMON:
	case VIDIOC_STREAMOFF:
		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		if (request == VIDIOC_STREAMON)
			result = v4l2_streamon(index);
		else
			result = v4l2_streamoff(index);
		break;

	default:
		result = SYS_IOCTL(fd, request, arg);
		break;
	}
示例#3
0
void *
capture_loop(void *arg)
{
	struct thr_data *data = (struct thr_data *)arg;
	struct display *disp = data->disp;
	struct v4l2 *v4l2 = data->v4l2;
	uint32_t fourcc = data->fourcc;
	uint32_t width = data->width, height = data->height;

	struct buffer **buffers, *capt, *alg, **alg_bufs;
	int ret, i;
	int *src, *dst;
	int w, h;

	buffers = disp_get_vid_buffers(disp, NBUF, fourcc, width, height);
	if (!buffers) {
		return NULL;
	}

	alg_bufs = disp_get_vid_buffers(disp, 1, fourcc, width, height);
	alg = alg_bufs[0];

	ret = v4l2_reqbufs(v4l2, buffers, NBUF);
	if (ret) {
		return NULL;
	}

	for (i = 0; i < NBUF; i++) {
		v4l2_qbuf(v4l2, buffers[i]);
	}

	ret = v4l2_streamon(v4l2);
	if (ret) {
		return NULL;
	}

	for (i = 1; i < CNT; i++) {

		capt = v4l2_dqbuf(v4l2);

src = omap_bo_map(capt->bo[0]);
dst = omap_bo_map(alg->bo[0]);

//printf("debug: %p %p\n", src, dst);
omap_bo_cpu_prep(capt->bo[0], OMAP_GEM_READ);
omap_bo_cpu_prep(alg->bo[0], OMAP_GEM_WRITE);
//printf("bufers prepared\n");

/*
for (h=0; h<720; h++)
	for(w=0;w<1280 / 2;w++)
		dst[h * 1280 / 2 + w] = src[h * 1280 / 2 + 1280 / 2 - w];


*/

printf("Calling get Image");
//getImage(dst);




//printf("Mirrrored\n");
omap_bo_cpu_fini(capt->bo[0], OMAP_GEM_READ);
omap_bo_cpu_fini(alg->bo[0], OMAP_GEM_WRITE);

ret = disp_post_vid_buffer(disp, alg,
		0, 0, width, height);

//printf("Alg buffer posted\n");
		ret = disp_post_vid_buffer(disp, capt,
			0, 0, width, height);
		if (ret) {
			ERROR("Post buffer failed");
			return NULL;
		}
		v4l2_qbuf(v4l2, capt);
	}
	v4l2_streamoff(v4l2);

	MSG("Ok!");
	return disp;
}