static int v4l2_set_pix_format(struct context *cnt, src_v4l2_t * s, int *width, int *height)
{
	struct v4l2_fmtdesc fmt;
	short int v4l2_pal;

	static const u32 supported_formats[] = {	/* higher index means better chance to be used */
		V4L2_PIX_FMT_SN9C10X,
		V4L2_PIX_FMT_SBGGR8,
		V4L2_PIX_FMT_MJPEG,
		V4L2_PIX_FMT_JPEG,
		V4L2_PIX_FMT_RGB24,
		V4L2_PIX_FMT_UYVY,
		V4L2_PIX_FMT_YUYV,
		V4L2_PIX_FMT_YUV422P,
		V4L2_PIX_FMT_YUV420,
		0
	};

	short int index_format = -1;

	memset(&fmt, 0, sizeof(struct v4l2_fmtdesc));
	fmt.index = v4l2_pal = 0;
	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

	motion_log(LOG_INFO, 0, "Supported palettes:");
	while (xioctl(s->fd, VIDIOC_ENUM_FMT, &fmt) != -1) {
		short int i;

		motion_log(LOG_INFO, 0, "%i: %c%c%c%c (%s)", v4l2_pal,
			   fmt.pixelformat >> 0, fmt.pixelformat >> 8,
			   fmt.pixelformat >> 16, fmt.pixelformat >> 24, fmt.description);

		for (i = 0; supported_formats[i]; i++)
			if (supported_formats[i] == fmt.pixelformat && i > index_format) {
				if (cnt->conf.v4l2_palette == i) {
					index_format = cnt->conf.v4l2_palette;
					motion_log(LOG_INFO, 0, "Selected palette %c%c%c%c", fmt.pixelformat >> 0, 
								fmt.pixelformat >> 8, fmt.pixelformat >> 16, fmt.pixelformat >> 24);
					i=10;
					break;
				}
				index_format = i;
			}

		memset(&fmt, 0, sizeof(struct v4l2_fmtdesc));
		fmt.index = ++v4l2_pal;
		fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	}

	if (index_format >= 0) {
