aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--TODO15
-rw-r--r--lib/libv4lconvert/Makefile4
-rw-r--r--lib/libv4lconvert/jpeg.c253
-rw-r--r--lib/libv4lconvert/jpeg_memsrcdest.c305
-rw-r--r--lib/libv4lconvert/jpeg_memsrcdest.h9
-rw-r--r--lib/libv4lconvert/libv4lconvert-priv.h9
-rw-r--r--lib/libv4lconvert/libv4lconvert.c22
7 files changed, 613 insertions, 4 deletions
diff --git a/TODO b/TODO
index f44a11e7..21f7fa25 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,14 @@
libv4l todo:
------------
+-don't automatically enable the software effects (awb, gamma) on cameras
+ which need flipping, most of these only produce yuv data and software
+ effects are very expensive on yuv data
+
+-move pixart rotate 90 hack to v4lconvert_decode_jpeg_tinyjpeg, since it
+ is only needed on select pixart cameras, which use this function for
+ decoding, this will nicely cleanup the main conversion routine
+
-libv4lconvert: v4lconvert_do_try_format should always prefer smaller then
requested resolutions over bigger then requested ones
@@ -15,6 +23,13 @@ libv4l todo:
-get standardized CID for AUTOGAIN_TARGET upstream and switch to that
+Nice to have:
+
+-support packed yuv as output format so that we aren't forced to do
+ conversion on upside down uvc cams
+
+-add a software rotation control (0 / 90 / 180 / 270 degrees) for tablets.
+
utils todo:
-----------
diff --git a/lib/libv4lconvert/Makefile b/lib/libv4lconvert/Makefile
index 20f4c0c5..a9791f78 100644
--- a/lib/libv4lconvert/Makefile
+++ b/lib/libv4lconvert/Makefile
@@ -1,6 +1,6 @@
override CPPFLAGS += -I../include -fvisibility=hidden
-LIBS_libv4lconvert = -lrt -lm
+LIBS_libv4lconvert = -lrt -lm -ljpeg
ifeq ($(LINKTYPE),static)
CONVERT_LIB = libv4lconvert.a
@@ -12,7 +12,7 @@ endif
CONVERT_OBJS = libv4lconvert.o tinyjpeg.o sn9c10x.o sn9c20x.o pac207.o \
mr97310a.o flip.o crop.o jidctflt.o spca561-decompress.o \
rgbyuv.o sn9c2028-decomp.o spca501.o sq905c.o bayer.o hm12.o \
- stv0680.o cpia1.o jpgl.o jpeg.o \
+ stv0680.o cpia1.o jpgl.o jpeg.o jpeg_memsrcdest.o \
control/libv4lcontrol.o processing/libv4lprocessing.o \
processing/whitebalance.o processing/autogain.o \
processing/gamma.o helper.o
diff --git a/lib/libv4lconvert/jpeg.c b/lib/libv4lconvert/jpeg.c
index c268e169..ff5fd3c7 100644
--- a/lib/libv4lconvert/jpeg.c
+++ b/lib/libv4lconvert/jpeg.c
@@ -17,7 +17,9 @@
*/
#include <errno.h>
+#include <stdlib.h>
#include "libv4lconvert-priv.h"
+#include "jpeg_memsrcdest.h"
int v4lconvert_decode_jpeg_tinyjpeg(struct v4lconvert_data *data,
unsigned char *src, int src_size, unsigned char *dest,
@@ -104,3 +106,254 @@ int v4lconvert_decode_jpeg_tinyjpeg(struct v4lconvert_data *data,
}
return 0;
}
+
+static void init_libjpeg_cinfo(struct v4lconvert_data *data)
+{
+ struct jpeg_compress_struct cinfo;
+ unsigned char *jpeg_header = NULL;
+ unsigned long jpeg_header_size = 0;
+
+ if (data->cinfo_initialized)
+ return;
+
+ /* Create a jpeg compression object with default params and write
+ default jpeg headers to a mem buffer, so that we can use them to
+ pre-fill a jpeg_decompress_struct with default quant and huffman
+ tables, so that libjpeg can be used to parse [m]jpg-s with
+ incomplete headers */
+ cinfo.err = jpeg_std_error(&data->jerr);
+ jpeg_create_compress(&cinfo);
+ jpeg_mem_dest(&cinfo, &jpeg_header, &jpeg_header_size);
+ cinfo.input_components = 3;
+ cinfo.in_color_space = JCS_RGB;
+ jpeg_set_defaults(&cinfo);
+ jpeg_write_tables(&cinfo);
+ jpeg_destroy_compress(&cinfo);
+
+ /* Init the jpeg_decompress_struct */
+ data->cinfo.err = jpeg_std_error(&data->jerr);
+ jpeg_create_decompress(&data->cinfo);
+ jpeg_mem_src(&data->cinfo, jpeg_header, jpeg_header_size);
+ jpeg_read_header(&data->cinfo, FALSE);
+
+ free(jpeg_header);
+ data->cinfo_initialized = 1;
+}
+
+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;
+
+ cinfo->raw_data_out = TRUE;
+ jpeg_start_decompress(cinfo);
+ while (cinfo->output_scanline < cinfo->image_height) {
+ for (y = 0; y < 8 * v_samp; y++) {
+ y_rows[y] = ydest;
+ ydest += cinfo->image_width;
+ }
+ jpeg_read_raw_data(cinfo, rows, 8 * v_samp);
+
+ /* 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;
+ }
+ jpeg_finish_decompress(cinfo);
+ return 0;
+}
+
+static int decode_libjpeg_h_samp2(struct v4lconvert_data *data,
+ unsigned char *ydest, unsigned char *udest, unsigned char *vdest,
+ int v_samp)
+{
+ struct jpeg_decompress_struct *cinfo = &data->cinfo;
+ int y;
+ 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 };
+
+ cinfo->raw_data_out = TRUE;
+ jpeg_start_decompress(cinfo);
+ while (cinfo->output_scanline < cinfo->image_height) {
+ for (y = 0; y < 8 * v_samp; y++) {
+ y_rows[y] = ydest;
+ ydest += width;
+ }
+ for (y = 0; y < 8; y++) {
+ u_rows[y] = udest;
+ v_rows[y] = vdest;
+ udest += width / 2;
+ vdest += width / 2;
+ }
+ jpeg_read_raw_data(cinfo, rows, 8 * v_samp);
+ /* For v_samp == 1 were going to get another set of uv values,
+ but we need only 1 set since our output has v_samp == 2, so
+ rewind u and vdest and overwrite the previous set. */
+ if (cinfo->output_scanline % 16) {
+ udest -= width * 8 / 2;
+ vdest -= width * 8 / 2;
+ }
+ }
+ jpeg_finish_decompress(cinfo);
+ return 0;
+}
+
+int v4lconvert_decode_jpeg_libjpeg(struct v4lconvert_data *data,
+ unsigned char *src, int src_size, unsigned char *dest,
+ struct v4l2_format *fmt, unsigned int dest_pix_fmt)
+{
+ unsigned int width = fmt->fmt.pix.width;
+ unsigned int height = fmt->fmt.pix.height;
+ int result = 0;
+
+ init_libjpeg_cinfo(data);
+
+ jpeg_mem_src(&data->cinfo, src, src_size);
+ jpeg_read_header(&data->cinfo, TRUE);
+
+ if (data->cinfo.image_width != width ||
+ data->cinfo.image_height != height) {
+ V4LCONVERT_ERR("unexpected width / height in JPEG header"
+ "expected: %ux%u, header: %ux%u\n", width,
+ height, data->cinfo.image_width,
+ data->cinfo.image_height);
+ errno = EIO;
+ return -1;
+ }
+
+ if (data->cinfo.num_components != 3) {
+ V4LCONVERT_ERR("unexpected no components in JPEG: %d\n",
+ data->cinfo.num_components);
+ errno = EIO;
+ return -1;
+ }
+
+ if (dest_pix_fmt == V4L2_PIX_FMT_RGB24 ||
+ dest_pix_fmt == V4L2_PIX_FMT_BGR24) {
+ JSAMPROW row_pointer[1];
+
+#ifdef JCS_EXTENSIONS
+ if (dest_pix_fmt == V4L2_PIX_FMT_BGR24)
+ data->cinfo.out_color_space = JCS_EXT_BGR;
+#endif
+ row_pointer[0] = dest;
+ jpeg_start_decompress(&data->cinfo);
+ while (data->cinfo.output_scanline < height) {
+ jpeg_read_scanlines(&data->cinfo, row_pointer, 1);
+ row_pointer[0] += 3 * width;
+ }
+ jpeg_finish_decompress(&data->cinfo);
+#ifndef JCS_EXTENSIONS
+ if (dest_pix_fmt == V4L2_PIX_FMT_BGR24)
+ v4lconvert_swap_rgb(dest, dest, width, height);
+#endif
+ } else {
+ int h_samp, v_samp;
+ unsigned char *udest, *vdest;
+
+ if (data->cinfo.max_h_samp_factor == 2 &&
+ data->cinfo.cur_comp_info[0]->h_samp_factor == 2 &&
+ data->cinfo.cur_comp_info[1]->h_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[2]->h_samp_factor == 1) {
+ h_samp = 2;
+#if 0 /* HDG: untested, disable for now */
+ } else if (data->cinfo.max_h_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[0]->h_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[1]->h_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[2]->h_samp_factor == 1) {
+ h_samp = 1;
+#endif
+ } else {
+ fprintf(stderr,
+ "libv4lconvert: unsupported jpeg h-sampling "
+ "factors %d:%d:%d, please report this to "
+ "hdegoede@redhat.com\n",
+ data->cinfo.cur_comp_info[0]->h_samp_factor,
+ data->cinfo.cur_comp_info[1]->h_samp_factor,
+ data->cinfo.cur_comp_info[2]->h_samp_factor);
+ errno = EOPNOTSUPP;
+ return -1;
+ }
+
+ if (data->cinfo.max_v_samp_factor == 2 &&
+ data->cinfo.cur_comp_info[0]->v_samp_factor == 2 &&
+ data->cinfo.cur_comp_info[1]->v_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[2]->v_samp_factor == 1) {
+ v_samp = 2;
+ } else if (data->cinfo.max_v_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[0]->v_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[1]->v_samp_factor == 1 &&
+ data->cinfo.cur_comp_info[2]->v_samp_factor == 1) {
+ v_samp = 1;
+ } else {
+ fprintf(stderr,
+ "libv4lconvert: unsupported jpeg v-sampling "
+ "factors %d:%d:%d, please report this to "
+ "hdegoede@redhat.com\n",
+ data->cinfo.cur_comp_info[0]->v_samp_factor,
+ data->cinfo.cur_comp_info[1]->v_samp_factor,
+ data->cinfo.cur_comp_info[2]->v_samp_factor);
+ errno = EOPNOTSUPP;
+ return -1;
+ }
+
+ /* We don't want any padding as that may overflow our dest */
+ if (width % (8 * h_samp) || height % (8 * v_samp)) {
+ V4LCONVERT_ERR(
+ "resolution is not a multiple of dctsize");
+ errno = EIO;
+ return -1;
+ }
+
+ if (dest_pix_fmt == V4L2_PIX_FMT_YVU420) {
+ vdest = dest + width * height;
+ udest = vdest + (width * height) / 4;
+ } else {
+ udest = dest + width * height;
+ vdest = udest + (width * height) / 4;
+ }
+
+ if (h_samp == 1) {
+ result = decode_libjpeg_h_samp1(data, dest, udest,
+ vdest, v_samp);
+ } else {
+ result = decode_libjpeg_h_samp2(data, dest, udest,
+ vdest, v_samp);
+ }
+ }
+
+ return result;
+}
diff --git a/lib/libv4lconvert/jpeg_memsrcdest.c b/lib/libv4lconvert/jpeg_memsrcdest.c
new file mode 100644
index 00000000..d934afa3
--- /dev/null
+++ b/lib/libv4lconvert/jpeg_memsrcdest.c
@@ -0,0 +1,305 @@
+/*
+* memsrc.c
+*
+* Copyright (C) 1994-1996, Thomas G. Lane.
+* This file is part of the Independent JPEG Group's software.
+* For conditions of distribution and use, see the accompanying README file.
+*
+* This file contains decompression data source routines for the case of
+* reading JPEG data from a memory buffer that is preloaded with the entire
+* JPEG file. This would not seem especially useful at first sight, but
+* a number of people have asked for it.
+* This is really just a stripped-down version of jdatasrc.c. Comparison
+* of this code with jdatasrc.c may be helpful in seeing how to make
+* custom source managers for other purposes.
+*/
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <jpeglib.h>
+#include <jerror.h>
+#include "jpeg_memsrcdest.h"
+
+/* libjpeg8 and later come with their own (API compatible) memory source
+ and dest */
+#if JPEG_LIB_VERSION < 80
+
+/* Expanded data source object for memory input */
+
+typedef struct {
+ struct jpeg_source_mgr pub; /* public fields */
+
+ JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
+} my_source_mgr;
+
+typedef my_source_mgr * my_src_ptr;
+
+
+/*
+* Initialize source --- called by jpeg_read_header
+* before any data is actually read.
+*/
+
+METHODDEF(void)
+init_source (j_decompress_ptr cinfo)
+{
+ /* No work, since jpeg_mem_src set up the buffer pointer and count.
+ * Indeed, if we want to read multiple JPEG images from one buffer,
+ * this *must* not do anything to the pointer.
+ */
+}
+
+
+/*
+* Fill the input buffer --- called whenever buffer is emptied.
+*
+* In this application, this routine should never be called; if it is called,
+* the decompressor has overrun the end of the input buffer, implying we
+* supplied an incomplete or corrupt JPEG datastream. A simple error exit
+* might be the most appropriate response.
+*
+* But what we choose to do in this code is to supply dummy EOI markers
+* in order to force the decompressor to finish processing and supply
+* some sort of output image, no matter how corrupted.
+*/
+
+METHODDEF(boolean)
+fill_input_buffer (j_decompress_ptr cinfo)
+{
+ my_src_ptr src = (my_src_ptr) cinfo->src;
+
+ WARNMS(cinfo, JWRN_JPEG_EOF);
+
+ /* Create a fake EOI marker */
+ src->eoi_buffer[0] = (JOCTET) 0xFF;
+ src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
+ src->pub.next_input_byte = src->eoi_buffer;
+ src->pub.bytes_in_buffer = 2;
+
+ return TRUE;
+}
+
+
+/*
+* Skip data --- used to skip over a potentially large amount of
+* uninteresting data (such as an APPn marker).
+*
+* If we overrun the end of the buffer, we let fill_input_buffer deal with
+* it. An extremely large skip could cause some time-wasting here, but
+* it really isn't supposed to happen ... and the decompressor will never
+* skip more than 64K anyway.
+*/
+
+METHODDEF(void)
+skip_input_data (j_decompress_ptr cinfo, long num_bytes)
+{
+ my_src_ptr src = (my_src_ptr) cinfo->src;
+
+ if (num_bytes > 0) {
+ while (num_bytes > (long) src->pub.bytes_in_buffer) {
+ num_bytes -= (long) src->pub.bytes_in_buffer;
+ (void) fill_input_buffer(cinfo);
+ /* note we assume that fill_input_buffer will never
+ * return FALSE, so suspension need not be handled.
+ */
+ }
+ src->pub.next_input_byte += (size_t) num_bytes;
+ src->pub.bytes_in_buffer -= (size_t) num_bytes;
+ }
+}
+
+
+/*
+* An additional method that can be provided by data source modules is the
+* resync_to_restart method for error recovery in the presence of RST markers.
+* For the moment, this source module just uses the default resync method
+* provided by the JPEG library. That method assumes that no backtracking
+* is possible.
+*/
+
+
+/*
+* Terminate source --- called by jpeg_finish_decompress
+* after all data has been read. Often a no-op.
+*
+* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
+* application must deal with any cleanup that should happen even
+* for error exit.
+*/
+
+METHODDEF(void)
+term_source (j_decompress_ptr cinfo)
+{
+ /* no work necessary here */
+}
+
+
+/*
+* Prepare for input from a memory buffer.
+*/
+
+GLOBAL(void)
+jpeg_mem_src (j_decompress_ptr cinfo, unsigned char * buffer,
+ unsigned long bufsize)
+{
+ my_src_ptr src;
+
+ /* The source object is made permanent so that a series of JPEG images
+ * can be read from a single buffer by calling jpeg_mem_src
+ * only before the first one.
+ * This makes it unsafe to use this manager and a different source
+ * manager serially with the same JPEG object. Caveat programmer.
+ */
+ if (cinfo->src == NULL) { /* first time for this JPEG object? */
+ cinfo->src = (struct jpeg_source_mgr *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
+ JPOOL_PERMANENT,
+ sizeof(my_source_mgr));
+ }
+
+ src = (my_src_ptr) cinfo->src;
+ src->pub.init_source = init_source;
+ src->pub.fill_input_buffer = fill_input_buffer;
+ src->pub.skip_input_data = skip_input_data;
+ src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
+ src->pub.term_source = term_source;
+
+ src->pub.next_input_byte = buffer;
+ src->pub.bytes_in_buffer = bufsize;
+}
+
+
+
+/* Memory destination source modelled after Thomas G. Lane's memory source
+ support and jdatadst.c
+
+ Copyright (C) 2010, Hans de Goede
+
+ This code may be used under the same conditions as Thomas G. Lane's memory
+ source (see the copyright header at the top of this file).
+ */
+
+typedef struct {
+ struct jpeg_destination_mgr pub; /* public fields */
+
+ JOCTET **buffer; /* start of buffer */
+ unsigned long buf_size, *outsize;
+} my_destination_mgr;
+
+typedef my_destination_mgr * my_dest_ptr;
+
+#define OUTPUT_BUF_SIZE 32768 /* choose an efficiently fwrite'able size */
+
+
+/*
+ * Initialize destination --- called by jpeg_start_compress
+ * before any data is actually written.
+ */
+
+METHODDEF(void)
+init_destination (j_compress_ptr cinfo)
+{
+ /* No work, since jpeg_mem_dest set up the buffer pointer and count.
+ * Indeed, if we want to write multiple JPEG images to one buffer,
+ * this *must* not do anything to the pointer.
+ */
+}
+
+/*
+ * Empty the output buffer --- called whenever buffer fills up.
+ *
+ * In typical applications, this should write the entire output buffer
+ * (ignoring the current state of next_output_byte & free_in_buffer),
+ * reset the pointer & count to the start of the buffer, and return TRUE
+ * indicating that the buffer has been dumped.
+ *
+ * In applications that need to be able to suspend compression due to output
+ * overrun, a FALSE return indicates that the buffer cannot be emptied now.
+ * In this situation, the compressor will return to its caller (possibly with
+ * an indication that it has not accepted all the supplied scanlines). The
+ * application should resume compression after it has made more room in the
+ * output buffer. Note that there are substantial restrictions on the use of
+ * suspension --- see the documentation.
+ *
+ * When suspending, the compressor will back up to a convenient restart point
+ * (typically the start of the current MCU). next_output_byte & free_in_buffer
+ * indicate where the restart point will be if the current call returns FALSE.
+ * Data beyond this point will be regenerated after resumption, so do not
+ * write it out when emptying the buffer externally.
+ */
+
+METHODDEF(boolean)
+empty_output_buffer (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+
+ *dest->buffer = realloc (*dest->buffer, dest->buf_size + OUTPUT_BUF_SIZE);
+ if (!*dest->buffer)
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
+
+ dest->pub.next_output_byte = *dest->buffer + dest->buf_size;
+ dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
+ dest->buf_size += OUTPUT_BUF_SIZE;
+
+ return TRUE;
+}
+
+/*
+ * Terminate destination --- called by jpeg_finish_compress
+ * after all data has been written. Usually needs to flush buffer.
+ *
+ * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
+ * application must deal with any cleanup that should happen even
+ * for error exit.
+ */
+
+METHODDEF(void)
+term_destination (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+
+ *dest->outsize = dest->buf_size - dest->pub.free_in_buffer;
+}
+
+GLOBAL(void)
+jpeg_mem_dest (j_compress_ptr cinfo, unsigned char ** outbuffer,
+ unsigned long * outsize)
+{
+ my_dest_ptr dest;
+
+ /* The destination object is made permanent so that multiple JPEG images
+ * can be written to the same file without re-executing jpeg_stdio_dest.
+ * This makes it dangerous to use this manager and a different destination
+ * manager serially with the same JPEG object, because their private object
+ * sizes may be different. Caveat programmer.
+ */
+ if (cinfo->dest == NULL) { /* first time for this JPEG object? */
+ cinfo->dest = (struct jpeg_destination_mgr *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
+ JPOOL_PERMANENT,
+ sizeof(my_destination_mgr));
+ }
+
+ dest = (my_dest_ptr) cinfo->dest;
+ dest->pub.init_destination = init_destination;
+ dest->pub.empty_output_buffer = empty_output_buffer;
+ dest->pub.term_destination = term_destination;
+ dest->buffer = outbuffer;
+ dest->buf_size = *outsize;
+ dest->outsize = outsize;
+
+ if (*dest->buffer == NULL || dest->buf_size == 0) {
+ /* Allocate initial buffer */
+ *dest->buffer = malloc(OUTPUT_BUF_SIZE);
+ if (*dest->buffer == NULL)
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
+ dest->buf_size = OUTPUT_BUF_SIZE;
+ }
+
+ dest->pub.next_output_byte = *dest->buffer;
+ dest->pub.free_in_buffer = dest->buf_size;
+}
+
+#endif
diff --git a/lib/libv4lconvert/jpeg_memsrcdest.h b/lib/libv4lconvert/jpeg_memsrcdest.h
new file mode 100644
index 00000000..e9711824
--- /dev/null
+++ b/lib/libv4lconvert/jpeg_memsrcdest.h
@@ -0,0 +1,9 @@
+#include <jpeglib.h>
+
+void
+jpeg_mem_src (j_decompress_ptr cinfo, unsigned char * buffer,
+ unsigned long bufsize);
+
+void
+jpeg_mem_dest (j_compress_ptr cinfo, unsigned char ** outbuffer,
+ unsigned long * outsize);
diff --git a/lib/libv4lconvert/libv4lconvert-priv.h b/lib/libv4lconvert/libv4lconvert-priv.h
index 67084b24..272dda72 100644
--- a/lib/libv4lconvert/libv4lconvert-priv.h
+++ b/lib/libv4lconvert/libv4lconvert-priv.h
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
+#include <jpeglib.h>
#include "libv4lconvert.h"
#include "control/libv4lcontrol.h"
#include "processing/libv4lprocessing.h"
@@ -38,6 +39,7 @@
/* Card flags */
#define V4LCONVERT_IS_UVC 0x01
+#define V4LCONVERT_USE_TINYJPEG 0x02
struct v4lconvert_data {
int fd;
@@ -47,6 +49,9 @@ struct v4lconvert_data {
int64_t supported_src_formats; /* bitfield */
char error_msg[V4LCONVERT_ERROR_MSG_SIZE];
struct jdec_private *tinyjpeg;
+ struct jpeg_error_mgr jerr;
+ struct jpeg_decompress_struct cinfo;
+ int cinfo_initialized;
struct v4l2_frmsizeenum framesizes[V4LCONVERT_MAX_FRAMESIZES];
unsigned int no_framesizes;
int bandwidth;
@@ -177,6 +182,10 @@ 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 v4lconvert_decode_jpeg_libjpeg(struct v4lconvert_data *data,
+ unsigned char *src, int src_size, unsigned char *dest,
+ struct v4l2_format *fmt, unsigned int dest_pix_fmt);
+
void v4lconvert_decode_spca561(const unsigned char *src, unsigned char *dst,
int width, int height);
diff --git a/lib/libv4lconvert/libv4lconvert.c b/lib/libv4lconvert/libv4lconvert.c
index f7a86c71..bbb93127 100644
--- a/lib/libv4lconvert/libv4lconvert.c
+++ b/lib/libv4lconvert/libv4lconvert.c
@@ -180,6 +180,8 @@ void v4lconvert_destroy(struct v4lconvert_data *data)
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);
@@ -625,8 +627,24 @@ static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
/* JPG and variants */
case V4L2_PIX_FMT_MJPEG:
case V4L2_PIX_FMT_JPEG:
- result = v4lconvert_decode_jpeg_tinyjpeg(data, src, src_size,
- dest, fmt, dest_pix_fmt, 0);
+ 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,

Privacy Policy