void v4lconvert_destroy(struct v4lconvert_data *data) { if (data->jdec) { unsigned char *comps[3] = { NULL, NULL, NULL }; tinyjpeg_set_components(data->jdec, comps, 3); tinyjpeg_free(data->jdec); } free(data->convert_buf); free(data->rotate_buf); free(data->convert_pixfmt_buf); free(data); }
void v4lconvert_destroy(struct v4lconvert_data *data) { v4lprocessing_destroy(data->processing); v4lcontrol_destroy(data->control); if (data->jdec) { unsigned char *comps[3] = { NULL, NULL, NULL }; tinyjpeg_set_components(data->jdec, comps, 3); tinyjpeg_free(data->jdec); } v4lconvert_helper_cleanup(data); free(data->convert1_buf); free(data->convert2_buf); free(data->rotate90_buf); free(data->flip_buf); free(data->convert_pixfmt_buf); free(data); }
void v4lconvert_destroy(struct v4lconvert_data *data) { v4lprocessing_destroy(data->processing); v4lcontrol_destroy(data->control); if (data->tinyjpeg) { unsigned char *comps[3] = { NULL, NULL, NULL }; tinyjpeg_set_components(data->tinyjpeg, comps, 3); tinyjpeg_free(data->tinyjpeg); } if (data->cinfo_initialized) jpeg_destroy_decompress(&data->cinfo); v4lconvert_helper_cleanup(data); free(data->convert1_buf); free(data->convert2_buf); free(data->rotate90_buf); free(data->flip_buf); free(data->convert_pixfmt_buf); free(data->previous_frame); free(data); }
int v4lconvert_decode_jpeg_tinyjpeg(struct v4lconvert_data *data, unsigned char *src, int src_size, unsigned char *dest, struct v4l2_format *fmt, unsigned int dest_pix_fmt, int flags) { int result = 0; unsigned char *components[3]; unsigned int header_width, header_height; unsigned int width = fmt->fmt.pix.width; unsigned int height = fmt->fmt.pix.height; if (!data->tinyjpeg) { data->tinyjpeg = tinyjpeg_init(); if (!data->tinyjpeg) return v4lconvert_oom_error(data); } flags |= TINYJPEG_FLAGS_MJPEG_TABLE; tinyjpeg_set_flags(data->tinyjpeg, flags); if (tinyjpeg_parse_header(data->tinyjpeg, src, src_size)) { V4LCONVERT_ERR("parsing JPEG header: %s", tinyjpeg_get_errorstring(data->tinyjpeg)); errno = EAGAIN; return -1; } tinyjpeg_get_size(data->tinyjpeg, &header_width, &header_height); if (data->control_flags & V4LCONTROL_ROTATED_90_JPEG) { unsigned int tmp = width; width = height; height = tmp; } if (header_width != width || header_height != height) { 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; } fmt->fmt.pix.width = header_width; fmt->fmt.pix.height = header_height; components[0] = dest; switch (dest_pix_fmt) { case V4L2_PIX_FMT_RGB24: tinyjpeg_set_components(data->tinyjpeg, components, 1); result = tinyjpeg_decode(data->tinyjpeg, TINYJPEG_FMT_RGB24); break; case V4L2_PIX_FMT_BGR24: tinyjpeg_set_components(data->tinyjpeg, components, 1); result = tinyjpeg_decode(data->tinyjpeg, 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->tinyjpeg, components, 3); result = tinyjpeg_decode(data->tinyjpeg, 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->tinyjpeg, components, 3); result = tinyjpeg_decode(data->tinyjpeg, 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->tinyjpeg)); errno = EPIPE; return -1; } return 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; }
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; }
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; }