Esempio n. 1
0
static
void errno_exit(const char *s)
{
    fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
    fprintf(stderr, "%s\n", v4lconvert_get_error_message(v4lconvert_data));
    exit(EXIT_FAILURE);
}
Esempio n. 2
0
static void errno_exit(const char *s)
{
	fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
#ifdef WITH_V4L2_LIB
	fprintf(stderr, "%s\n",
			v4lconvert_get_error_message(v4lconvert_data));
#endif
	exit(EXIT_FAILURE);
}
Esempio n. 3
0
static int tc_v4l2_video_setup_image_format(V4L2Source *vs, int width, int height)
{
    int err = 0;

    vs->width  = width;
    vs->height = height;

    vs->v4l_convert = v4lconvert_create(vs->video_fd);
    if (!vs->v4l_convert) {
        return TC_ERROR;
    }

    memset(&(vs->v4l_dst_fmt), 0, sizeof(vs->v4l_dst_fmt));
    vs->v4l_dst_fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    vs->v4l_dst_fmt.fmt.pix.width       = width;
    vs->v4l_dst_fmt.fmt.pix.height      = height;
    vs->v4l_dst_fmt.fmt.pix.pixelformat = vs->v4l_dst_csp;
	
    err = v4lconvert_try_format(vs->v4l_convert,
                                &(vs->v4l_dst_fmt), &(vs->v4l_src_fmt));
    if (err) {
        tc_log_error(MOD_NAME, "unable to match formats: %s",
                     v4lconvert_get_error_message(vs->v4l_convert));
        return TC_ERROR;
    }

    err = v4l2_ioctl(vs->video_fd, VIDIOC_S_FMT, &(vs->v4l_src_fmt));
    if (err < 0) {
        tc_log_error(MOD_NAME, "error while setting the cam image format");
        return TC_ERROR;            
    }

    if (!v4lconvert_needs_conversion(vs->v4l_convert,
                                    &(vs->v4l_src_fmt),
                                    &(vs->v4l_dst_fmt))) {
        tc_log_info(MOD_NAME, "fetch frames directly");
        vs->fetch_data = tc_v4l2_fetch_data_memcpy;
        /* Into the near future we should aim for zero-copy. -- FR */
    } else {
        char src_fcc[5] = { '\0' };
        char dst_fcc[5] = { '\0' };

        pixfmt_to_fourcc(vs->v4l_src_fmt.fmt.pix.pixelformat, src_fcc);
        pixfmt_to_fourcc(vs->v4l_dst_fmt.fmt.pix.pixelformat, dst_fcc);

        tc_log_info(MOD_NAME, "fetch frames using libv4lconvert "
                              "[%s] -> [%s]",
                              src_fcc, dst_fcc);
        vs->fetch_data = tc_v4l2_fetch_data_v4lconv;
    }

    return TC_OK;
}
Esempio n. 4
0
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;
	}
Esempio n. 5
0
static int v4l2_read_and_convert(int index, unsigned char *dest, int dest_size)
{
	const int max_tries = 10;
	int result, buf_size, tries = max_tries;

	buf_size = devices[index].dest_fmt.fmt.pix.sizeimage;

	if (devices[index].readbuf_size < buf_size) {
		unsigned char *new_buf;

		new_buf = realloc(devices[index].readbuf, buf_size);
		if (!new_buf)
			return -1;

		devices[index].readbuf = new_buf;
		devices[index].readbuf_size = buf_size;
	}

	do {
		result = SYS_READ(devices[index].fd, devices[index].readbuf, buf_size);
		if (result <= 0) {
			if (result && errno != EAGAIN) {
				int saved_err = errno;

				V4L2_LOG_ERR("reading: %s\n", strerror(errno));
				errno = saved_err;
			}
			return result;
		}

		result = v4lconvert_convert(devices[index].convert,
				&devices[index].src_fmt, &devices[index].dest_fmt,
				devices[index].readbuf, result, dest, dest_size);

		if (devices[index].first_frame) {
			/* Always treat convert errors as EAGAIN during the first few frames, as
			   some cams produce bad frames at the start of the stream
			   (hsync and vsync still syncing ??). */
			if (result < 0)
				errno = EAGAIN;
			devices[index].first_frame--;
		}

		if (result < 0) {
			int saved_err = errno;

			if (errno == EAGAIN)
				V4L2_LOG("warning error while converting frame data: %s",
						v4lconvert_get_error_message(devices[index].convert));
			else
				V4L2_LOG_ERR("converting / decoding frame data: %s",
						v4lconvert_get_error_message(devices[index].convert));

			errno = saved_err;
		}
		tries--;
	} while (result < 0 && errno == EAGAIN && tries);

	if (result < 0 && errno == EAGAIN) {
		V4L2_LOG_ERR("got %d consecutive frame decode errors, last error: %s",
				max_tries, v4lconvert_get_error_message(devices[index].convert));
	}

	return result;
}
Esempio n. 6
0
static int v4l2_dequeue_and_convert(int index, struct v4l2_buffer *buf,
		unsigned char *dest, int dest_size)
{
	const int max_tries = 10;
	int result, tries = max_tries;

	/* Make sure we have the real v4l2 buffers mapped */
	result = v4l2_map_buffers(index);
	if (result)
		return result;

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

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

		devices[index].frame_queued &= ~(1 << buf->index);

		result = v4lconvert_convert(devices[index].convert,
				&devices[index].src_fmt, &devices[index].dest_fmt,
				devices[index].frame_pointers[buf->index],
				buf->bytesused, dest ? dest : (devices[index].convert_mmap_buf +
					buf->index * V4L2_FRAME_BUF_SIZE), dest_size);

		if (devices[index].first_frame) {
			/* Always treat convert errors as EAGAIN during the first few frames, as
			   some cams produce bad frames at the start of the stream
			   (hsync and vsync still syncing ??). */
			if (result < 0)
				errno = EAGAIN;
			devices[index].first_frame--;
		}

		if (result < 0) {
			int saved_err = errno;

			if (errno == EAGAIN)
				V4L2_LOG("warning error while converting frame data: %s",
						v4lconvert_get_error_message(devices[index].convert));
			else
				V4L2_LOG_ERR("converting / decoding frame data: %s",
						v4lconvert_get_error_message(devices[index].convert));

			v4l2_queue_read_buffer(index, buf->index);
			errno = saved_err;
		}
		tries--;
	} while (result < 0 && errno == EAGAIN && tries);

	if (result < 0 && errno == EAGAIN) {
		V4L2_LOG_ERR("got %d consecutive frame decode errors, last error: %s",
				max_tries, v4lconvert_get_error_message(devices[index].convert));
		errno = EAGAIN;
	}

	return result;
}