Пример #1
0
static int decode_libjpeg_h_samp1(struct v4lconvert_data *data,
	unsigned char *ydest, unsigned char *udest, unsigned char *vdest,
	int v_samp)
{
	struct jpeg_decompress_struct *cinfo = &data->cinfo;
	int x, y;
	unsigned char *uv_buf;
	unsigned int width = cinfo->image_width;
	JSAMPROW y_rows[16], u_rows[8], v_rows[8];
	JSAMPARRAY rows[3] = { y_rows, u_rows, v_rows };

	uv_buf = v4lconvert_alloc_buffer(width * 16,
					 &data->convert_pixfmt_buf,
					 &data->convert_pixfmt_buf_size);
	if (!uv_buf)
		return v4lconvert_oom_error(data);

	for (y = 0; y < 8; y++) {
		u_rows[y] = uv_buf;
		uv_buf += width;
		v_rows[y] = uv_buf;
		uv_buf += width;
	}
	uv_buf -= width * 16;

	while (cinfo->output_scanline < cinfo->image_height) {
		for (y = 0; y < 8 * v_samp; y++) {
			y_rows[y] = ydest;
			ydest += cinfo->image_width;
		}
		y = jpeg_read_raw_data(cinfo, rows, 8 * v_samp);
		if (y != 8 * v_samp)
			return -1;

		/* For v_samp == 1 skip copying uv vals every other time */
		if (cinfo->output_scanline % 16)
			continue;

		/* Copy over every other u + v pixel for 8 lines */
		for (y = 0; y < 8; y++) {
			for (x = 0; x < width; x += 2) {
				*udest++ = *uv_buf++;
				uv_buf++;
			}
			for (x = 0; x < width; x += 2) {
				*vdest++ = *uv_buf++;
				uv_buf++;
			}
		}
		uv_buf -= width * 16;
	}
	return 0;
}
Пример #2
0
int v4lconvert_convert(struct v4lconvert_data *data,
  const struct v4l2_format *src_fmt,  /* in */
  const struct v4l2_format *dest_fmt, /* in */
  unsigned char *src, int src_size, unsigned char *dest, int dest_size)
{
  int res, dest_needed, temp_needed, convert = 0, rotate = 0, crop = 0;
  unsigned char *convert_dest = dest, *rotate_src = src, *rotate_dest = dest;
  unsigned char *crop_src = src;
  struct v4l2_format my_src_fmt = *src_fmt;
  struct v4l2_format my_dest_fmt = *dest_fmt;

  /* Special case when no conversion is needed */
  if (!v4lconvert_needs_conversion(data, src_fmt, dest_fmt)) {
    int to_copy = MIN(dest_size, src_size);
    memcpy(dest, src, to_copy);
    return to_copy;
  }

  /* When field is V4L2_FIELD_ALTERNATE, each buffer only contains half the
     lines */
  if (my_src_fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE) {
    my_src_fmt.fmt.pix.height /= 2;
    my_dest_fmt.fmt.pix.height /= 2;
  }

  /* sanity check, is the dest buffer large enough? */
  switch (my_dest_fmt.fmt.pix.pixelformat) {
    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
      dest_needed = my_dest_fmt.fmt.pix.width * my_dest_fmt.fmt.pix.height * 3;
      temp_needed = my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3;
      break;
    case V4L2_PIX_FMT_YUV420:
    case V4L2_PIX_FMT_YVU420:
      dest_needed =
	my_dest_fmt.fmt.pix.width * my_dest_fmt.fmt.pix.height * 3 / 2;
      temp_needed =
	my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3 / 2;
      break;
    default:
      V4LCONVERT_ERR("Unknown dest format in conversion\n");
      errno = EINVAL;
      return -1;
  }

  if (dest_size < dest_needed) {
    V4LCONVERT_ERR("destination buffer too small\n");
    errno = EFAULT;
    return -1;
  }

  if (my_dest_fmt.fmt.pix.pixelformat != my_src_fmt.fmt.pix.pixelformat)
    convert = 1;

  if (data->flags & V4LCONVERT_ROTATE_90)
    rotate += 90;
  if (data->flags & V4LCONVERT_ROTATE_180)
    rotate += 180;

  if (my_dest_fmt.fmt.pix.width != my_src_fmt.fmt.pix.width ||
      my_dest_fmt.fmt.pix.height != my_src_fmt.fmt.pix.height)
    crop = 1;

  /* convert_pixfmt -> rotate -> crop, all steps are optional */
  if (convert && (rotate || crop)) {
    convert_dest = v4lconvert_alloc_buffer(data, temp_needed,
		     &data->convert_buf, &data->convert_buf_size);
    if (!convert_dest)
      return -1;

    rotate_src = crop_src = convert_dest;
  }

  if (rotate && crop) {
    rotate_dest = v4lconvert_alloc_buffer(data, temp_needed,
		    &data->rotate_buf, &data->rotate_buf_size);
    if (!rotate_dest)
      return -1;

    crop_src = rotate_dest;
  }

  if (convert) {
    res = v4lconvert_convert_pixfmt(data, src, src_size, convert_dest,
				    &my_src_fmt,
				    my_dest_fmt.fmt.pix.pixelformat);
    if (res)
      return res;

    my_src_fmt.fmt.pix.pixelformat = my_dest_fmt.fmt.pix.pixelformat;
    v4lconvert_fixup_fmt(&my_src_fmt);
  }

  if (rotate)
    v4lconvert_rotate(rotate_src, rotate_dest,
		      my_src_fmt.fmt.pix.width, my_src_fmt.fmt.pix.height,
		      my_src_fmt.fmt.pix.pixelformat, rotate);

  if (crop)
    v4lconvert_crop(crop_src, dest, &my_src_fmt, &my_dest_fmt);

  return dest_needed;
}
Пример #3
0
static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
  unsigned char *src, int src_size, unsigned char *dest,
  const struct v4l2_format *src_fmt, unsigned int dest_pix_fmt)
{
  unsigned int header_width, header_height;
  int result = 0, jpeg_flags = TINYJPEG_FLAGS_MJPEG_TABLE;
  unsigned char *components[3];
  unsigned int src_pix_fmt = src_fmt->fmt.pix.pixelformat;
  unsigned int width  = src_fmt->fmt.pix.width;
  unsigned int height = src_fmt->fmt.pix.height;

  switch (src_pix_fmt) {
    case V4L2_PIX_FMT_PJPG:
      jpeg_flags |= TINYJPEG_FLAGS_PIXART_JPEG;
      /* Fall through */
    case V4L2_PIX_FMT_MJPEG:
    case V4L2_PIX_FMT_JPEG:
      if (!data->jdec) {
	data->jdec = tinyjpeg_init();
	if (!data->jdec) {
	  V4LCONVERT_ERR("out of memory!\n");
	  errno = ENOMEM;
	  return -1;
	}
      }
      tinyjpeg_set_flags(data->jdec, jpeg_flags);
      if (tinyjpeg_parse_header(data->jdec, src, src_size)) {
	V4LCONVERT_ERR("parsing JPEG header: %s\n",
	  tinyjpeg_get_errorstring(data->jdec));
	errno = EIO;
	return -1;
      }
      tinyjpeg_get_size(data->jdec, &header_width, &header_height);

      if (header_width != width || header_height != height) {
	/* Check for (pixart) rotated JPEG */
	if (header_width == height && header_height == width) {
	  if (!(data->flags & V4LCONVERT_ROTATE_90)) {
	    V4LCONVERT_ERR("JPEG needs 90 degree rotation\n");
	    data->flags |= V4LCONVERT_ROTATE_90;
	    errno = EAGAIN;
	    return -1;
	  }
	} else {
	  V4LCONVERT_ERR("unexpected width / height in JPEG header"
			 "expected: %ux%u, header: %ux%u\n", width, height,
			 header_width, header_height);
	  errno = EIO;
	  return -1;
	}
      }

      components[0] = dest;

      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	tinyjpeg_set_components(data->jdec, components, 1);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_RGB24);
	break;
      case V4L2_PIX_FMT_BGR24:
	tinyjpeg_set_components(data->jdec, components, 1);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_BGR24);
	break;
      case V4L2_PIX_FMT_YUV420:
	components[1] = components[0] + width * height;
	components[2] = components[1] + width * height / 4;
	tinyjpeg_set_components(data->jdec, components, 3);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P);
	break;
      case V4L2_PIX_FMT_YVU420:
	components[2] = components[0] + width * height;
	components[1] = components[2] + width * height / 4;
	tinyjpeg_set_components(data->jdec, components, 3);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P);
	break;
      }

      if (result) {
	/* Pixart webcam's seem to regulary generate corrupt frames, which
	   are best thrown away to avoid flashes in the video stream. Tell
	   the upper layer this is an intermediate fault and it should try
	   again with a new buffer by setting errno to EAGAIN */
	if (src_pix_fmt == V4L2_PIX_FMT_PJPG) {
	  V4LCONVERT_ERR("decompressing JPEG: %s",
	    tinyjpeg_get_errorstring(data->jdec));
	  errno = EAGAIN;
	  return -1;
	} else {
	/* If the JPEG header checked out ok and we get an error during actual
	   decompression, log the error, but don't return an errorcode to the
	   application, so that the user gets what we managed to decompress */
	  fprintf(stderr, "libv4lconvert: Error decompressing JPEG: %s",
	    tinyjpeg_get_errorstring(data->jdec));
	}
      }
      break;

    /* Custom cam specific YUV formats */
    case V4L2_PIX_FMT_SPCA501:
    case V4L2_PIX_FMT_SPCA505:
    case V4L2_PIX_FMT_SPCA508:
    case V4L2_PIX_FMT_SN9C20X_I420:
    {
      unsigned char *d;
      int yvu = 0;

      if (dest_pix_fmt != V4L2_PIX_FMT_YUV420 &&
	  dest_pix_fmt != V4L2_PIX_FMT_YVU420) {
	d = v4lconvert_alloc_buffer(data, width * height * 3 / 2,
	      &data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
	if (!d)
	  return -1;
      } else
	d = dest;

      if (dest_pix_fmt == V4L2_PIX_FMT_YVU420)
	yvu = 1;

      switch (src_pix_fmt) {
	case V4L2_PIX_FMT_SPCA501:
	  v4lconvert_spca501_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_SPCA505:
	  v4lconvert_spca505_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_SPCA508:
	  v4lconvert_spca508_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_SN9C20X_I420:
	  v4lconvert_sn9c20x_to_yuv420(src, d, width, height, yvu);
	  break;
      }

      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuv420_to_rgb24(data->convert_pixfmt_buf, dest, width,
				   height, yvu);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuv420_to_bgr24(data->convert_pixfmt_buf, dest, width,
				   height, yvu);
	break;
      }
      break;
    }

    /* Conexant cx2341x raw video macroblock format */
    case V4L2_PIX_FMT_HM12:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_hm12_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_hm12_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_hm12_to_yuv420(src, dest, width, height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_hm12_to_yuv420(src, dest, width, height, 1);
	break;
      }
      break;

    /* compressed bayer formats */
    case V4L2_PIX_FMT_SPCA561:
    case V4L2_PIX_FMT_SN9C10X:
    case V4L2_PIX_FMT_PAC207:
    case V4L2_PIX_FMT_MR97310A:
    case V4L2_PIX_FMT_SQ905C:
    {
      unsigned char *tmpbuf;

      tmpbuf = v4lconvert_alloc_buffer(data, width * height,
	    &data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
      if (!tmpbuf)
	return -1;

      switch (src_pix_fmt) {
	case V4L2_PIX_FMT_SPCA561:
	  v4lconvert_decode_spca561(src, tmpbuf, width, height);
	  src_pix_fmt = V4L2_PIX_FMT_SGBRG8;
	  break;
	case V4L2_PIX_FMT_SN9C10X:
	  v4lconvert_decode_sn9c10x(src, tmpbuf, width, height);
	  src_pix_fmt = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_PAC207:
	  v4lconvert_decode_pac207(src, tmpbuf, width, height);
	  src_pix_fmt = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_MR97310A:
	  v4lconvert_decode_mr97310a(src, tmpbuf, width, height);
	  src_pix_fmt = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_SQ905C:
	  v4lconvert_decode_sq905c(src, tmpbuf, width, height);
	  src_pix_fmt = V4L2_PIX_FMT_SRGGB8;
	  break;
      }
      src = tmpbuf;
      /* Deliberate fall through to raw bayer fmt code! */
    }

    /* Raw bayer formats */
    case V4L2_PIX_FMT_SBGGR8:
    case V4L2_PIX_FMT_SGBRG8:
    case V4L2_PIX_FMT_SGRBG8:
    case V4L2_PIX_FMT_SRGGB8:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_bayer_to_rgb24(src, dest, width, height, src_pix_fmt);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_bayer_to_bgr24(src, dest, width, height, src_pix_fmt);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_bayer_to_yuv420(src, dest, width, height, src_pix_fmt, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_bayer_to_yuv420(src, dest, width, height, src_pix_fmt, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_RGB24:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_swap_rgb(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_rgb24_to_yuv420(src, dest, src_fmt, 0, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_rgb24_to_yuv420(src, dest, src_fmt, 0, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_BGR24:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_swap_rgb(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_rgb24_to_yuv420(src, dest, src_fmt, 1, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_rgb24_to_yuv420(src, dest, src_fmt, 1, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_YUV420:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuv420_to_rgb24(src, dest, width,
				   height, 0);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuv420_to_bgr24(src, dest, width,
				   height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_swap_uv(src, dest, src_fmt);
	break;
      }
      break;

    case V4L2_PIX_FMT_YVU420:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuv420_to_rgb24(src, dest, width,
				   height, 1);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuv420_to_bgr24(src, dest, width,
				   height, 1);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_swap_uv(src, dest, src_fmt);
	break;
      }
      break;

    case V4L2_PIX_FMT_YUYV:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuyv_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuyv_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_YVYU:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yvyu_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yvyu_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	/* Note we use yuyv_to_yuv420 not v4lconvert_yvyu_to_yuv420,
	   with the last argument reversed to make it have as we want */
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
	break;
      }
      break;

    case V4L2_PIX_FMT_UYVY:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_uyvy_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_uyvy_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_uyvy_to_yuv420(src, dest, width, height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_uyvy_to_yuv420(src, dest, width, height, 1);
	break;
      }
      break;

    default:
      V4LCONVERT_ERR("Unknown src format in conversion\n");
      errno = EINVAL;
      return -1;
  }
  return 0;
}
Пример #4
0
static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
	unsigned char *src, int src_size, unsigned char *dest, int dest_size,
	struct v4l2_format *fmt, unsigned int dest_pix_fmt)
{
	int result = 0;
	unsigned int src_pix_fmt = fmt->fmt.pix.pixelformat;
	unsigned int width  = fmt->fmt.pix.width;
	unsigned int height = fmt->fmt.pix.height;
	unsigned int bytesperline = fmt->fmt.pix.bytesperline;

	switch (src_pix_fmt) {
	/* JPG and variants */
	case V4L2_PIX_FMT_MJPEG:
	case V4L2_PIX_FMT_JPEG:
		if (data->flags & V4LCONVERT_USE_TINYJPEG) {
			result = v4lconvert_decode_jpeg_tinyjpeg(data,
							src, src_size, dest,
							fmt, dest_pix_fmt, 0);
		} else {
			result = v4lconvert_decode_jpeg_libjpeg(data,
							src, src_size, dest,
							fmt, dest_pix_fmt);
			if (result == -1 && errno == EOPNOTSUPP) {
				/* Fall back to tinyjpeg */
				jpeg_destroy_decompress(&data->cinfo);
				data->cinfo_initialized = 0;
				data->flags |= V4LCONVERT_USE_TINYJPEG;
				result = v4lconvert_decode_jpeg_tinyjpeg(data,
							src, src_size, dest,
							fmt, dest_pix_fmt, 0);
			}
		}
		break;
	case V4L2_PIX_FMT_PJPG:
		result = v4lconvert_decode_jpeg_tinyjpeg(data, src, src_size,
				dest, fmt, dest_pix_fmt,
				TINYJPEG_FLAGS_PIXART_JPEG);
		break;
	case V4L2_PIX_FMT_JPGL:
		result = v4lconvert_decode_jpgl(src, src_size, dest_pix_fmt,
						dest, width, height);
		break;

	/* Custom cam specific YUV formats */
	case V4L2_PIX_FMT_SPCA501:
	case V4L2_PIX_FMT_SPCA505:
	case V4L2_PIX_FMT_SPCA508:
	case V4L2_PIX_FMT_CIT_YYVYUY:
	case V4L2_PIX_FMT_KONICA420:
	case V4L2_PIX_FMT_M420:
	case V4L2_PIX_FMT_SN9C20X_I420:
	case V4L2_PIX_FMT_CPIA1:
	case V4L2_PIX_FMT_OV511:
	case V4L2_PIX_FMT_OV518: {
		unsigned char *d;
		int d_size;
		int yvu = 0;

		if (dest_pix_fmt != V4L2_PIX_FMT_YUV420 &&
				dest_pix_fmt != V4L2_PIX_FMT_YVU420) {
			d = v4lconvert_alloc_buffer(width * height * 3 / 2,
					&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
			if (!d)
				return v4lconvert_oom_error(data);
			d_size = width * height * 3 / 2;
		} else {
			d = dest;
			d_size = dest_size;
		}

		if (dest_pix_fmt == V4L2_PIX_FMT_YVU420)
			yvu = 1;

		switch (src_pix_fmt) {
		case V4L2_PIX_FMT_SPCA501:
			v4lconvert_spca501_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_SPCA505:
			v4lconvert_spca505_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_SPCA508:
			v4lconvert_spca508_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_CIT_YYVYUY:
			v4lconvert_cit_yyvyuy_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_KONICA420:
			v4lconvert_konica_yuv420_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_M420:
			v4lconvert_m420_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_SN9C20X_I420:
			v4lconvert_sn9c20x_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_CPIA1:
			if (v4lconvert_cpia1_to_yuv420(data, src, src_size, d,
						width, height, yvu)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			break;
		case V4L2_PIX_FMT_OV511:
			if (v4lconvert_helper_decompress(data, LIBDIR "/" LIBSUBDIR "/ov511-decomp",
						src, src_size, d, d_size, width, height, yvu)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			break;
		case V4L2_PIX_FMT_OV518:
			if (v4lconvert_helper_decompress(data, LIBDIR "/" LIBSUBDIR "/ov518-decomp",
						src, src_size, d, d_size, width, height, yvu)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			break;
		}

		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuv420_to_rgb24(data->convert_pixfmt_buf, dest, width,
					height, yvu);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuv420_to_bgr24(data->convert_pixfmt_buf, dest, width,
					height, yvu);
			break;
		}
		break;
	}

		/* Conexant cx2341x raw video macroblock format */
	case V4L2_PIX_FMT_HM12:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_hm12_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_hm12_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_hm12_to_yuv420(src, dest, width, height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_hm12_to_yuv420(src, dest, width, height, 1);
			break;
		}
		break;

		/* compressed bayer formats */
	case V4L2_PIX_FMT_SPCA561:
	case V4L2_PIX_FMT_SN9C10X:
	case V4L2_PIX_FMT_PAC207:
	case V4L2_PIX_FMT_MR97310A:
	case V4L2_PIX_FMT_JL2005BCD:
	case V4L2_PIX_FMT_SN9C2028:
	case V4L2_PIX_FMT_SQ905C:
	case V4L2_PIX_FMT_STV0680: { /* Not compressed but needs some shuffling */
		unsigned char *tmpbuf;
		struct v4l2_format tmpfmt = *fmt;

		tmpbuf = v4lconvert_alloc_buffer(width * height,
				&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
		if (!tmpbuf)
			return v4lconvert_oom_error(data);

		switch (src_pix_fmt) {
		case V4L2_PIX_FMT_SPCA561:
			v4lconvert_decode_spca561(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SGBRG8;
			break;
		case V4L2_PIX_FMT_SN9C10X:
			v4lconvert_decode_sn9c10x(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_PAC207:
			if (v4lconvert_decode_pac207(data, src, src_size, tmpbuf,
						width, height)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_MR97310A:
			if (v4lconvert_decode_mr97310a(data, src, src_size, tmpbuf,
						width, height)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_JL2005BCD:
			if (v4lconvert_decode_jl2005bcd(data, src, src_size,
							tmpbuf,
							width, height)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
			break;
		case V4L2_PIX_FMT_SN9C2028:
			v4lconvert_decode_sn9c2028(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_SQ905C:
			v4lconvert_decode_sq905c(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
			break;
		case V4L2_PIX_FMT_STV0680:
			v4lconvert_decode_stv0680(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
			break;
		}
		/* Do processing on the tmp buffer, because doing it on bayer data is
		   cheaper, and bayer == rgb and our dest_fmt may be yuv */
		tmpfmt.fmt.pix.bytesperline = width;
		tmpfmt.fmt.pix.sizeimage = width * height;
		v4lprocessing_processing(data->processing, tmpbuf, &tmpfmt);
		/* Deliberate fall through to raw bayer fmt code! */
		src_pix_fmt = tmpfmt.fmt.pix.pixelformat;
		src = tmpbuf;
		src_size = width * height;
		/* fall through */
	}

		/* Raw bayer formats */
	case V4L2_PIX_FMT_SBGGR8:
	case V4L2_PIX_FMT_SGBRG8:
	case V4L2_PIX_FMT_SGRBG8:
	case V4L2_PIX_FMT_SRGGB8:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_bayer_to_rgb24(src, dest, width, height, bytesperline, src_pix_fmt);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_bayer_to_bgr24(src, dest, width, height, bytesperline, src_pix_fmt);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_bayer_to_yuv420(src, dest, width, height, bytesperline, src_pix_fmt, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_bayer_to_yuv420(src, dest, width, height, bytesperline, src_pix_fmt, 1);
			break;
		}
		if (src_size < (width * height)) {
			V4LCONVERT_ERR("short raw bayer data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_SE401: {
		unsigned char *d = NULL;

		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			d = dest;
			break;
		case V4L2_PIX_FMT_BGR24:
		case V4L2_PIX_FMT_YUV420:
		case V4L2_PIX_FMT_YVU420:
			d = v4lconvert_alloc_buffer(width * height * 3,
					&data->convert_pixfmt_buf,
					&data->convert_pixfmt_buf_size);
			if (!d)
				return v4lconvert_oom_error(data);

			fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
			v4lconvert_fixup_fmt(fmt);
			break;
		}

		result = v4lconvert_se401_to_rgb24(data, src, src_size, d,
						   width, height);
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_swap_rgb(d, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 1);
			break;
		}
		break;
	}

	case V4L2_PIX_FMT_GREY:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
	        case V4L2_PIX_FMT_BGR24:
			v4lconvert_grey_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_grey_to_yuv420(src, dest, fmt);
			break;
		}
		if (src_size < (width * height)) {
			V4LCONVERT_ERR("short grey data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_Y10BPACK:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
	        case V4L2_PIX_FMT_BGR24:
			result = v4lconvert_y10b_to_rgb24(data, src, dest,
							  width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
		case V4L2_PIX_FMT_YVU420:
			result = v4lconvert_y10b_to_yuv420(data, src, dest,
							   width, height);
			break;
		}
		if (result == 0 && src_size < (width * height * 10 / 8)) {
			V4LCONVERT_ERR("short y10b data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_RGB565:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_rgb565_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_rgb565_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb565_to_yuv420(src, dest, fmt, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb565_to_yuv420(src, dest, fmt, 1);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short rgb565 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_RGB24:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			memcpy(dest, src, width * height * 3);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_swap_rgb(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1);
			break;
		}
		if (src_size < (width * height * 3)) {
			V4LCONVERT_ERR("short rgb24 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_BGR24:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_swap_rgb(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			memcpy(dest, src, width * height * 3);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1);
			break;
		}
		if (src_size < (width * height * 3)) {
			V4LCONVERT_ERR("short bgr24 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YUV420:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuv420_to_rgb24(src, dest, width,
					height, 0);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuv420_to_bgr24(src, dest, width,
					height, 0);
			break;
		case V4L2_PIX_FMT_YUV420:
			memcpy(dest, src, width * height * 3 / 2);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_swap_uv(src, dest, fmt);
			break;
		}
		if (src_size < (width * height * 3 / 2)) {
			V4LCONVERT_ERR("short yuv420 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YVU420:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuv420_to_rgb24(src, dest, width,
					height, 1);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuv420_to_bgr24(src, dest, width,
					height, 1);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_swap_uv(src, dest, fmt);
			break;
		case V4L2_PIX_FMT_YVU420:
			memcpy(dest, src, width * height * 3 / 2);
			break;
		}
		if (src_size < (width * height * 3 / 2)) {
			V4LCONVERT_ERR("short yvu420 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YUYV:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuyv_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuyv_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short yuyv data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YVYU:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yvyu_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yvyu_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			/* Note we use yuyv_to_yuv420 not v4lconvert_yvyu_to_yuv420,
			   with the last argument reversed to make it have as we want */
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short yvyu data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_UYVY:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_uyvy_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_uyvy_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_uyvy_to_yuv420(src, dest, width, height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_uyvy_to_yuv420(src, dest, width, height, 1);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short uyvy data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	default:
		V4LCONVERT_ERR("Unknown src format in conversion\n");
		errno = EINVAL;
		return -1;
	}

	fmt->fmt.pix.pixelformat = dest_pix_fmt;
	v4lconvert_fixup_fmt(fmt);

	return result;
}
Пример #5
0
int v4lconvert_convert(struct v4lconvert_data *data,
		const struct v4l2_format *src_fmt,  /* in */
		const struct v4l2_format *dest_fmt, /* in */
		unsigned char *src, int src_size, unsigned char *dest, int dest_size)
{
	int res, dest_needed, temp_needed, processing, convert = 0;
	int rotate90, vflip, hflip, crop;
	unsigned char *convert1_dest = dest;
	int convert1_dest_size = dest_size;
	unsigned char *convert2_src = src, *convert2_dest = dest;
	int convert2_dest_size = dest_size;
	unsigned char *rotate90_src = src, *rotate90_dest = dest;
	unsigned char *flip_src = src, *flip_dest = dest;
	unsigned char *crop_src = src;
	struct v4l2_format my_src_fmt = *src_fmt;
	struct v4l2_format my_dest_fmt = *dest_fmt;

	processing = v4lprocessing_pre_processing(data->processing);
	rotate90 = data->control_flags & V4LCONTROL_ROTATED_90_JPEG;
	hflip = v4lcontrol_get_ctrl(data->control, V4LCONTROL_HFLIP);
	vflip = v4lcontrol_get_ctrl(data->control, V4LCONTROL_VFLIP);
	crop = my_dest_fmt.fmt.pix.width != my_src_fmt.fmt.pix.width ||
		my_dest_fmt.fmt.pix.height != my_src_fmt.fmt.pix.height;

	if (/* If no conversion/processing is needed */
			(src_fmt->fmt.pix.pixelformat == dest_fmt->fmt.pix.pixelformat &&
			 !processing && !rotate90 && !hflip && !vflip && !crop) ||
			/* or if we should do processing/rotating/flipping but the app tries to
			   use the native cam format, we just return an unprocessed frame copy */
			!v4lconvert_supported_dst_format(dest_fmt->fmt.pix.pixelformat)) {
		int to_copy = MIN(dest_size, src_size);
		memcpy(dest, src, to_copy);
		return to_copy;
	}

	/* When field is V4L2_FIELD_ALTERNATE, each buffer only contains half the
	   lines */
	if (my_src_fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE) {
		my_src_fmt.fmt.pix.height /= 2;
		my_dest_fmt.fmt.pix.height /= 2;
	}

	/* sanity check, is the dest buffer large enough? */
	switch (my_dest_fmt.fmt.pix.pixelformat) {
	case V4L2_PIX_FMT_RGB24:
	case V4L2_PIX_FMT_BGR24:
		dest_needed = my_dest_fmt.fmt.pix.width * my_dest_fmt.fmt.pix.height * 3;
		temp_needed = my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3;
		break;
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_YVU420:
		dest_needed =
			my_dest_fmt.fmt.pix.width * my_dest_fmt.fmt.pix.height * 3 / 2;
		temp_needed =
			my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3 / 2;
		break;
	default:
		V4LCONVERT_ERR("Unknown dest format in conversion\n");
		errno = EINVAL;
		return -1;
	}

	if (dest_size < dest_needed) {
		V4LCONVERT_ERR("destination buffer too small (%d < %d)\n",
				dest_size, dest_needed);
		errno = EFAULT;
		return -1;
	}


	/* Sometimes we need foo -> rgb -> bar as video processing (whitebalance,
	   etc.) can only be done on rgb data */
	if (processing && v4lconvert_processing_needs_double_conversion(
				my_src_fmt.fmt.pix.pixelformat,
				my_dest_fmt.fmt.pix.pixelformat))
		convert = 2;
	else if (my_dest_fmt.fmt.pix.pixelformat !=
			my_src_fmt.fmt.pix.pixelformat ||
		 /* Special case if we do not need to do conversion, but we
		    are not doing any other step involving copying either,
		    force going through convert_pixfmt to copy the data from
		    source to dest */
		 (!rotate90 && !hflip && !vflip && !crop))
		convert = 1;

	/* convert_pixfmt (only if convert == 2) -> processing -> convert_pixfmt ->
	   rotate -> flip -> crop, all steps are optional */
	if (convert == 2) {
		convert1_dest = v4lconvert_alloc_buffer(
				my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3,
				&data->convert1_buf, &data->convert1_buf_size);
		if (!convert1_dest)
			return v4lconvert_oom_error(data);

		convert1_dest_size =
			my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3;
		convert2_src = convert1_dest;
	}

	if (convert && (rotate90 || hflip || vflip || crop)) {
		convert2_dest = v4lconvert_alloc_buffer(temp_needed,
				&data->convert2_buf, &data->convert2_buf_size);
		if (!convert2_dest)
			return v4lconvert_oom_error(data);

		convert2_dest_size = temp_needed;
		rotate90_src = flip_src = crop_src = convert2_dest;
	}

	if (rotate90 && (hflip || vflip || crop)) {
		rotate90_dest = v4lconvert_alloc_buffer(temp_needed,
				&data->rotate90_buf, &data->rotate90_buf_size);
		if (!rotate90_dest)
			return v4lconvert_oom_error(data);

		flip_src = crop_src = rotate90_dest;
	}

	if ((vflip || hflip) && crop) {
		flip_dest = v4lconvert_alloc_buffer(temp_needed, &data->flip_buf,
				&data->flip_buf_size);
		if (!flip_dest)
			return v4lconvert_oom_error(data);

		crop_src = flip_dest;
	}

	/* Done setting sources / dest and allocating intermediate buffers,
	   real conversion / processing / ... starts here. */
	if (convert == 2) {
		res = v4lconvert_convert_pixfmt(data, src, src_size,
				convert1_dest, convert1_dest_size,
				&my_src_fmt,
				V4L2_PIX_FMT_RGB24);
		if (res)
			return res;

		src_size = my_src_fmt.fmt.pix.sizeimage;
	}

	if (processing)
		v4lprocessing_processing(data->processing, convert2_src, &my_src_fmt);

	if (convert) {
		res = v4lconvert_convert_pixfmt(data, convert2_src, src_size,
				convert2_dest, convert2_dest_size,
				&my_src_fmt,
				my_dest_fmt.fmt.pix.pixelformat);
		if (res)
			return res;

		src_size = my_src_fmt.fmt.pix.sizeimage;

		/* We call processing here again in case the source format was not
		   rgb, but the dest is. v4lprocessing checks it self it only actually
		   does the processing once per frame. */
		if (processing)
			v4lprocessing_processing(data->processing, convert2_dest, &my_src_fmt);
	}

	if (rotate90)
		v4lconvert_rotate90(rotate90_src, rotate90_dest, &my_src_fmt);

	if (hflip || vflip)
		v4lconvert_flip(flip_src, flip_dest, &my_src_fmt, hflip, vflip);

	if (crop)
		v4lconvert_crop(crop_src, dest, &my_src_fmt, &my_dest_fmt);

	return dest_needed;
}
Пример #6
0
static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
  unsigned char *src, int src_size, unsigned char *dest, int dest_size,
  struct v4l2_format *fmt, unsigned int dest_pix_fmt)
{
  unsigned int header_width, header_height;
  int result = 0, jpeg_flags = TINYJPEG_FLAGS_MJPEG_TABLE;
  unsigned char *components[3];
  unsigned int src_pix_fmt = fmt->fmt.pix.pixelformat;
  unsigned int width  = fmt->fmt.pix.width;
  unsigned int height = fmt->fmt.pix.height;

  switch (src_pix_fmt) {
    case V4L2_PIX_FMT_PJPG:
      jpeg_flags |= TINYJPEG_FLAGS_PIXART_JPEG;
      /* Fall through */
    case V4L2_PIX_FMT_MJPEG:
    case V4L2_PIX_FMT_JPEG:
      if (!data->jdec) {
	data->jdec = tinyjpeg_init();
	if (!data->jdec) {
	  V4LCONVERT_ERR("out of memory!\n");
	  errno = ENOMEM;
	  return -1;
	}
      }
      tinyjpeg_set_flags(data->jdec, jpeg_flags);
      if (tinyjpeg_parse_header(data->jdec, src, src_size)) {
	V4LCONVERT_ERR("parsing JPEG header: %s\n",
	  tinyjpeg_get_errorstring(data->jdec));
	errno = EIO;
	return -1;
      }
      tinyjpeg_get_size(data->jdec, &header_width, &header_height);

      if (header_width != width || header_height != height) {
	/* Check for (pixart) rotated JPEG */
	if (header_width == height && header_height == width) {
	  if (!(data->control_flags & V4LCONTROL_ROTATED_90_JPEG)) {
	    V4LCONVERT_ERR("JPEG needs 90° rotation, please report "
			   "this to <*****@*****.**>\n");
	    errno = EIO;
	    return -1;
	  }
	  fmt->fmt.pix.width = header_width;
	  fmt->fmt.pix.height = header_height;
	} else {
	  V4LCONVERT_ERR("unexpected width / height in JPEG header"
			 "expected: %ux%u, header: %ux%u\n", width, height,
			 header_width, header_height);
	  errno = EIO;
	  return -1;
	}
      } else if ((data->control_flags & V4LCONTROL_ROTATED_90_JPEG)) {
	fprintf(stderr, "libv4lconvert: expected 90° rotated JPEG, but got "
		"normal JPEG, please report this to <*****@*****.**>\n");
	V4LCONVERT_ERR("expected 90° rotated JPEG, but got normal JPEG\n");
	errno = EAGAIN;
	data->control_flags &= ~V4LCONTROL_ROTATED_90_JPEG;
	return -1;
      }

      components[0] = dest;

      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	tinyjpeg_set_components(data->jdec, components, 1);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_RGB24);
	break;
      case V4L2_PIX_FMT_BGR24:
	tinyjpeg_set_components(data->jdec, components, 1);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_BGR24);
	break;
      case V4L2_PIX_FMT_YUV420:
	components[1] = components[0] + width * height;
	components[2] = components[1] + width * height / 4;
	tinyjpeg_set_components(data->jdec, components, 3);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P);
	break;
      case V4L2_PIX_FMT_YVU420:
	components[2] = components[0] + width * height;
	components[1] = components[2] + width * height / 4;
	tinyjpeg_set_components(data->jdec, components, 3);
	result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P);
	break;
      }

      if (result) {
	/* Pixart webcam's seem to regulary generate corrupt frames, which
	   are best thrown away to avoid flashes in the video stream. Tell
	   the upper layer this is an intermediate fault and it should try
	   again with a new buffer by setting errno to EAGAIN */
	if (src_pix_fmt == V4L2_PIX_FMT_PJPG ||
	    data->flags & V4LCONVERT_IS_SN9C20X) {
	  V4LCONVERT_ERR("decompressing JPEG: %s",
	    tinyjpeg_get_errorstring(data->jdec));
	  errno = EAGAIN;
	  return -1;
	} else {
	/* If the JPEG header checked out ok and we get an error during actual
	   decompression, log the error, but don't return an errorcode to the
	   application, so that the user gets what we managed to decompress */
	  fprintf(stderr, "libv4lconvert: Error decompressing JPEG: %s",
	    tinyjpeg_get_errorstring(data->jdec));
	}
      }
      break;

    /* Custom cam specific YUV formats */
    case V4L2_PIX_FMT_SPCA501:
    case V4L2_PIX_FMT_SPCA505:
    case V4L2_PIX_FMT_SPCA508:
    case V4L2_PIX_FMT_SN9C20X_I420:
    case V4L2_PIX_FMT_OV511:
    case V4L2_PIX_FMT_OV518:
    {
      unsigned char *d;
      int d_size;
      int yvu = 0;

      if (dest_pix_fmt != V4L2_PIX_FMT_YUV420 &&
	  dest_pix_fmt != V4L2_PIX_FMT_YVU420) {
	d = v4lconvert_alloc_buffer(data, width * height * 3 / 2,
	      &data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
	if (!d)
	  return -1;
	d_size = width * height * 3 / 2;
      } else {
	d = dest;
	d_size = dest_size;
      }

      if (dest_pix_fmt == V4L2_PIX_FMT_YVU420)
	yvu = 1;

      switch (src_pix_fmt) {
	case V4L2_PIX_FMT_SPCA501:
	  v4lconvert_spca501_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_SPCA505:
	  v4lconvert_spca505_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_SPCA508:
	  v4lconvert_spca508_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_SN9C20X_I420:
	  v4lconvert_sn9c20x_to_yuv420(src, d, width, height, yvu);
	  break;
	case V4L2_PIX_FMT_OV511:
	  if (v4lconvert_helper_decompress(data, LIBDIR "/" LIBSUBDIR "/ov511-decomp",
		     src, src_size, d, d_size, width, height, yvu)) {
	    /* Corrupt frame, better get another one */
	    errno = EAGAIN;
	    return -1;
	  }
	  break;
	case V4L2_PIX_FMT_OV518:
	  if (v4lconvert_helper_decompress(data, LIBDIR "/" LIBSUBDIR "/ov518-decomp",
		     src, src_size, d, d_size, width, height, yvu)) {
	    /* Corrupt frame, better get another one */
	    errno = EAGAIN;
	    return -1;
	  }
	  break;
      }

      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuv420_to_rgb24(data->convert_pixfmt_buf, dest, width,
				   height, yvu);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuv420_to_bgr24(data->convert_pixfmt_buf, dest, width,
				   height, yvu);
	break;
      }
      break;
    }

    /* Conexant cx2341x raw video macroblock format */
    case V4L2_PIX_FMT_HM12:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_hm12_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_hm12_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_hm12_to_yuv420(src, dest, width, height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_hm12_to_yuv420(src, dest, width, height, 1);
	break;
      }
      break;

    /* compressed bayer formats */
    case V4L2_PIX_FMT_SPCA561:
    case V4L2_PIX_FMT_SN9C10X:
    case V4L2_PIX_FMT_PAC207:
    case V4L2_PIX_FMT_MR97310A:
    case V4L2_PIX_FMT_SN9C2028:
    case V4L2_PIX_FMT_SQ905C:
    {
      unsigned char *tmpbuf;
      struct v4l2_format tmpfmt = *fmt;

      tmpbuf = v4lconvert_alloc_buffer(data, width * height,
	    &data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
      if (!tmpbuf)
	return -1;

      switch (src_pix_fmt) {
	case V4L2_PIX_FMT_SPCA561:
	  v4lconvert_decode_spca561(src, tmpbuf, width, height);
	  tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SGBRG8;
	  break;
	case V4L2_PIX_FMT_SN9C10X:
	  v4lconvert_decode_sn9c10x(src, tmpbuf, width, height);
	  tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_PAC207:
	  if (v4lconvert_decode_pac207(data, src, src_size, tmpbuf,
				       width, height)) {
	    /* Corrupt frame, better get another one */
	    errno = EAGAIN;
	    return -1;
	  }
	  tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_MR97310A:
	  v4lconvert_decode_mr97310a(src, tmpbuf, width, height);
	  tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_SN9C2028:
	  v4lconvert_decode_sn9c2028(src, tmpbuf, width, height);
	  tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
	  break;
	case V4L2_PIX_FMT_SQ905C:
	  v4lconvert_decode_sq905c(src, tmpbuf, width, height);
	  tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
	  break;
      }
      /* Do processing on the tmp buffer, because doing it on bayer data is
	 cheaper, and bayer == rgb and our dest_fmt may be yuv */
      tmpfmt.fmt.pix.bytesperline = width;
      tmpfmt.fmt.pix.sizeimage = width * height;
      v4lprocessing_processing(data->processing, tmpbuf, &tmpfmt);
      /* Deliberate fall through to raw bayer fmt code! */
      src_pix_fmt = tmpfmt.fmt.pix.pixelformat;
      src = tmpbuf;
    }

    /* Raw bayer formats */
    case V4L2_PIX_FMT_SBGGR8:
    case V4L2_PIX_FMT_SGBRG8:
    case V4L2_PIX_FMT_SGRBG8:
    case V4L2_PIX_FMT_SRGGB8:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_bayer_to_rgb24(src, dest, width, height, src_pix_fmt);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_bayer_to_bgr24(src, dest, width, height, src_pix_fmt);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_bayer_to_yuv420(src, dest, width, height, src_pix_fmt, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_bayer_to_yuv420(src, dest, width, height, src_pix_fmt, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_RGB565:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_rgb565_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_rgb565_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_rgb565_to_yuv420(src, dest, fmt, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_rgb565_to_yuv420(src, dest, fmt, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_RGB24:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_swap_rgb(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_BGR24:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_swap_rgb(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_YUV420:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuv420_to_rgb24(src, dest, width,
				   height, 0);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuv420_to_bgr24(src, dest, width,
				   height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_swap_uv(src, dest, fmt);
	break;
      }
      break;

    case V4L2_PIX_FMT_YVU420:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuv420_to_rgb24(src, dest, width,
				   height, 1);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuv420_to_bgr24(src, dest, width,
				   height, 1);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_swap_uv(src, dest, fmt);
	break;
      }
      break;

    case V4L2_PIX_FMT_YUYV:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yuyv_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yuyv_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
	break;
      }
      break;

    case V4L2_PIX_FMT_YVYU:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_yvyu_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_yvyu_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	/* Note we use yuyv_to_yuv420 not v4lconvert_yvyu_to_yuv420,
	   with the last argument reversed to make it have as we want */
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
	break;
      }
      break;

    case V4L2_PIX_FMT_UYVY:
      switch (dest_pix_fmt) {
      case V4L2_PIX_FMT_RGB24:
	v4lconvert_uyvy_to_rgb24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_BGR24:
	v4lconvert_uyvy_to_bgr24(src, dest, width, height);
	break;
      case V4L2_PIX_FMT_YUV420:
	v4lconvert_uyvy_to_yuv420(src, dest, width, height, 0);
	break;
      case V4L2_PIX_FMT_YVU420:
	v4lconvert_uyvy_to_yuv420(src, dest, width, height, 1);
	break;
      }
      break;

    default:
      V4LCONVERT_ERR("Unknown src format in conversion\n");
      errno = EINVAL;
      return -1;
  }

  fmt->fmt.pix.pixelformat = dest_pix_fmt;
  v4lconvert_fixup_fmt(fmt);

  return 0;
}
Пример #7
0
static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
		unsigned char *src, int src_size, unsigned char *dest, int dest_size,
		struct v4l2_format *fmt, unsigned int dest_pix_fmt)
{
	unsigned int header_width, header_height;
	int result = 0, jpeg_flags = TINYJPEG_FLAGS_MJPEG_TABLE;
	unsigned char *components[3];
	unsigned int src_pix_fmt = fmt->fmt.pix.pixelformat;
	unsigned int width  = fmt->fmt.pix.width;
	unsigned int height = fmt->fmt.pix.height;

	switch (src_pix_fmt) {
	case V4L2_PIX_FMT_PJPG:
		jpeg_flags |= TINYJPEG_FLAGS_PIXART_JPEG;
		/* Fall through */
	case V4L2_PIX_FMT_MJPEG:
	case V4L2_PIX_FMT_JPEG:
		if (!data->jdec) {
			data->jdec = tinyjpeg_init();
			if (!data->jdec)
				return v4lconvert_oom_error(data);
		}
		tinyjpeg_set_flags(data->jdec, jpeg_flags);
		if (tinyjpeg_parse_header(data->jdec, src, src_size)) {
			V4LCONVERT_ERR("parsing JPEG header: %s",
					tinyjpeg_get_errorstring(data->jdec));
			errno = EIO;
			return -1;
		}
		tinyjpeg_get_size(data->jdec, &header_width, &header_height);

		if (header_width != width || header_height != height) {
			/* Check for (pixart) rotated JPEG */
			if (header_width == height && header_height == width) {
				if (!(data->control_flags & V4LCONTROL_ROTATED_90_JPEG)) {
					V4LCONVERT_ERR("JPEG needs 90° rotation, please report "
							"this to <*****@*****.**>\n");
					errno = EIO;
					return -1;
				}
				fmt->fmt.pix.width = header_width;
				fmt->fmt.pix.height = header_height;
			} else {
				V4LCONVERT_ERR("unexpected width / height in JPEG header"
						"expected: %ux%u, header: %ux%u\n", width, height,
						header_width, header_height);
				errno = EIO;
				return -1;
			}
		} else if ((data->control_flags & V4LCONTROL_ROTATED_90_JPEG)) {
			fprintf(stderr, "libv4lconvert: expected 90° rotated JPEG, but got "
					"normal JPEG, please report this to <*****@*****.**>\n");
			V4LCONVERT_ERR("expected 90° rotated JPEG, but got normal JPEG\n");
			errno = EAGAIN;
			data->control_flags &= ~V4LCONTROL_ROTATED_90_JPEG;
			return -1;
		}

		components[0] = dest;

		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			tinyjpeg_set_components(data->jdec, components, 1);
			result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_RGB24);
			break;
		case V4L2_PIX_FMT_BGR24:
			tinyjpeg_set_components(data->jdec, components, 1);
			result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_BGR24);
			break;
		case V4L2_PIX_FMT_YUV420:
			components[1] = components[0] + width * height;
			components[2] = components[1] + width * height / 4;
			tinyjpeg_set_components(data->jdec, components, 3);
			result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P);
			break;
		case V4L2_PIX_FMT_YVU420:
			components[2] = components[0] + width * height;
			components[1] = components[2] + width * height / 4;
			tinyjpeg_set_components(data->jdec, components, 3);
			result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P);
			break;
		}

		if (result) {
			/* The JPEG header checked out ok but we got an error
			   during decompression. Some webcams, esp pixart and
			   sn9c20x based ones regulary generate corrupt frames,
			   which are best thrown away to avoid flashes in the
			   video stream. We use EPIPE to signal the upper layer
			   we have some video data, but it is incomplete.

			   The upper layer (usually libv4l2) should respond to
			   this by trying a number of times to get a new frame
			   and if that fails just passing up whatever we did
			   manage to decompress. */
			V4LCONVERT_ERR("decompressing JPEG: %s",
					tinyjpeg_get_errorstring(data->jdec));
			errno = EPIPE;
			result = -1;
		}
		break;

		/* Custom cam specific YUV formats */
	case V4L2_PIX_FMT_SPCA501:
	case V4L2_PIX_FMT_SPCA505:
	case V4L2_PIX_FMT_SPCA508:
	case V4L2_PIX_FMT_CIT_YYVYUY:
	case V4L2_PIX_FMT_KONICA420:
	case V4L2_PIX_FMT_SN9C20X_I420:
	case V4L2_PIX_FMT_CPIA1:
	case V4L2_PIX_FMT_OV511:
	case V4L2_PIX_FMT_OV518: {
		unsigned char *d;
		int d_size;
		int yvu = 0;

		if (dest_pix_fmt != V4L2_PIX_FMT_YUV420 &&
				dest_pix_fmt != V4L2_PIX_FMT_YVU420) {
			d = v4lconvert_alloc_buffer(width * height * 3 / 2,
					&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
			if (!d)
				return v4lconvert_oom_error(data);
			d_size = width * height * 3 / 2;
		} else {
			d = dest;
			d_size = dest_size;
		}

		if (dest_pix_fmt == V4L2_PIX_FMT_YVU420)
			yvu = 1;

		switch (src_pix_fmt) {
		case V4L2_PIX_FMT_SPCA501:
			v4lconvert_spca501_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_SPCA505:
			v4lconvert_spca505_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_SPCA508:
			v4lconvert_spca508_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_CIT_YYVYUY:
			v4lconvert_cit_yyvyuy_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_KONICA420:
			v4lconvert_konica_yuv420_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_SN9C20X_I420:
			v4lconvert_sn9c20x_to_yuv420(src, d, width, height, yvu);
			break;
		case V4L2_PIX_FMT_CPIA1:
			if (v4lconvert_cpia1_to_yuv420(data, src, src_size, d,
						width, height, yvu)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			break;
		case V4L2_PIX_FMT_OV511:
			if (v4lconvert_helper_decompress(data, LIBDIR "/" LIBSUBDIR "/ov511-decomp",
						src, src_size, d, d_size, width, height, yvu)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			break;
		case V4L2_PIX_FMT_OV518:
			if (v4lconvert_helper_decompress(data, LIBDIR "/" LIBSUBDIR "/ov518-decomp",
						src, src_size, d, d_size, width, height, yvu)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			break;
		}

		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuv420_to_rgb24(data->convert_pixfmt_buf, dest, width,
					height, yvu);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuv420_to_bgr24(data->convert_pixfmt_buf, dest, width,
					height, yvu);
			break;
		}
		break;
	}

		/* Conexant cx2341x raw video macroblock format */
	case V4L2_PIX_FMT_HM12:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_hm12_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_hm12_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_hm12_to_yuv420(src, dest, width, height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_hm12_to_yuv420(src, dest, width, height, 1);
			break;
		}
		break;

		/* compressed bayer formats */
	case V4L2_PIX_FMT_SPCA561:
	case V4L2_PIX_FMT_SN9C10X:
	case V4L2_PIX_FMT_PAC207:
	case V4L2_PIX_FMT_MR97310A:
	case V4L2_PIX_FMT_SN9C2028:
	case V4L2_PIX_FMT_SQ905C:
	case V4L2_PIX_FMT_STV0680: { /* Not compressed but needs some shuffling */
		unsigned char *tmpbuf;
		struct v4l2_format tmpfmt = *fmt;

		tmpbuf = v4lconvert_alloc_buffer(width * height,
				&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
		if (!tmpbuf)
			return v4lconvert_oom_error(data);

		switch (src_pix_fmt) {
		case V4L2_PIX_FMT_SPCA561:
			v4lconvert_decode_spca561(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SGBRG8;
			break;
		case V4L2_PIX_FMT_SN9C10X:
			v4lconvert_decode_sn9c10x(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_PAC207:
			if (v4lconvert_decode_pac207(data, src, src_size, tmpbuf,
						width, height)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_MR97310A:
			if (v4lconvert_decode_mr97310a(data, src, src_size, tmpbuf,
						width, height)) {
				/* Corrupt frame, better get another one */
				errno = EAGAIN;
				return -1;
			}
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_SN9C2028:
			v4lconvert_decode_sn9c2028(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
			break;
		case V4L2_PIX_FMT_SQ905C:
			v4lconvert_decode_sq905c(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
			break;
		case V4L2_PIX_FMT_STV0680:
			v4lconvert_decode_stv0680(src, tmpbuf, width, height);
			tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
			break;
		}
		/* Do processing on the tmp buffer, because doing it on bayer data is
		   cheaper, and bayer == rgb and our dest_fmt may be yuv */
		tmpfmt.fmt.pix.bytesperline = width;
		tmpfmt.fmt.pix.sizeimage = width * height;
		v4lprocessing_processing(data->processing, tmpbuf, &tmpfmt);
		/* Deliberate fall through to raw bayer fmt code! */
		src_pix_fmt = tmpfmt.fmt.pix.pixelformat;
		src = tmpbuf;
		src_size = width * height;
		/* fall through */
	}

		/* Raw bayer formats */
	case V4L2_PIX_FMT_SBGGR8:
	case V4L2_PIX_FMT_SGBRG8:
	case V4L2_PIX_FMT_SGRBG8:
	case V4L2_PIX_FMT_SRGGB8:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_bayer_to_rgb24(src, dest, width, height, src_pix_fmt);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_bayer_to_bgr24(src, dest, width, height, src_pix_fmt);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_bayer_to_yuv420(src, dest, width, height, src_pix_fmt, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_bayer_to_yuv420(src, dest, width, height, src_pix_fmt, 1);
			break;
		}
		if (src_size < (width * height)) {
			V4LCONVERT_ERR("short raw bayer data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_RGB565:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_rgb565_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_rgb565_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb565_to_yuv420(src, dest, fmt, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb565_to_yuv420(src, dest, fmt, 1);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short rgb565 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_RGB24:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_swap_rgb(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1);
			break;
		}
		if (src_size < (width * height * 3)) {
			V4LCONVERT_ERR("short rgb24 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_BGR24:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_swap_rgb(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1);
			break;
		}
		if (src_size < (width * height * 3)) {
			V4LCONVERT_ERR("short bgr24 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YUV420:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuv420_to_rgb24(src, dest, width,
					height, 0);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuv420_to_bgr24(src, dest, width,
					height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_swap_uv(src, dest, fmt);
			break;
		}
		if (src_size < (width * height * 3 / 2)) {
			V4LCONVERT_ERR("short yuv420 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YVU420:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuv420_to_rgb24(src, dest, width,
					height, 1);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuv420_to_bgr24(src, dest, width,
					height, 1);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_swap_uv(src, dest, fmt);
			break;
		}
		if (src_size < (width * height * 3 / 2)) {
			V4LCONVERT_ERR("short yvu420 data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YUYV:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yuyv_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yuyv_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short yuyv data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_YVYU:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_yvyu_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_yvyu_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			/* Note we use yuyv_to_yuv420 not v4lconvert_yvyu_to_yuv420,
			   with the last argument reversed to make it have as we want */
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 1);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_yuyv_to_yuv420(src, dest, width, height, 0);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short yvyu data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	case V4L2_PIX_FMT_UYVY:
		switch (dest_pix_fmt) {
		case V4L2_PIX_FMT_RGB24:
			v4lconvert_uyvy_to_rgb24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_BGR24:
			v4lconvert_uyvy_to_bgr24(src, dest, width, height);
			break;
		case V4L2_PIX_FMT_YUV420:
			v4lconvert_uyvy_to_yuv420(src, dest, width, height, 0);
			break;
		case V4L2_PIX_FMT_YVU420:
			v4lconvert_uyvy_to_yuv420(src, dest, width, height, 1);
			break;
		}
		if (src_size < (width * height * 2)) {
			V4LCONVERT_ERR("short uyvy data frame\n");
			errno = EPIPE;
			result = -1;
		}
		break;

	default:
		V4LCONVERT_ERR("Unknown src format in conversion\n");
		errno = EINVAL;
		return -1;
	}

	fmt->fmt.pix.pixelformat = dest_pix_fmt;
	v4lconvert_fixup_fmt(fmt);

	return result;
}