From 096cff3723440c08b1e706007cfdfda445409476 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Tue, 1 Jul 2008 21:05:22 +0200 Subject: v4l2-library: libv4lconvert From: Hans de Goede libv4lconvert: video decompression routines Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/include/libv4lconvert.h | 67 + v4l2-apps/lib/libv4l/libv4lconvert/Makefile | 46 + v4l2-apps/lib/libv4l/libv4lconvert/bayer.c | 597 ++++++ v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c | 286 +++ .../lib/libv4l/libv4lconvert/libv4lconvert-priv.h | 80 + v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c | 347 ++++ v4l2-apps/lib/libv4l/libv4lconvert/rgbyuv.c | 82 + v4l2-apps/lib/libv4l/libv4lconvert/spca501.c | 67 + .../lib/libv4l/libv4lconvert/spca561-decompress.c | 1002 +++++++++ .../lib/libv4l/libv4lconvert/tinyjpeg-internal.h | 121 ++ v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.c | 2164 ++++++++++++++++++++ v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.h | 73 + 12 files changed, 4932 insertions(+) create mode 100644 v4l2-apps/lib/libv4l/include/libv4lconvert.h create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/Makefile create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/bayer.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/rgbyuv.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/spca501.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg-internal.h create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.c create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.h (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/include/libv4lconvert.h b/v4l2-apps/lib/libv4l/include/libv4lconvert.h new file mode 100644 index 000000000..d8446dac8 --- /dev/null +++ b/v4l2-apps/lib/libv4l/include/libv4lconvert.h @@ -0,0 +1,67 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __LIBV4LCONVERT_H +#define __LIBV4LCONVERT_H + +/* These headers are not needed by us, but by linux/videodev2.h, + which is broken on some systems and doesn't include them itself :( */ +#include +#include +#include +/* end broken header workaround includes */ +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct v4lconvert_data; + +struct v4lconvert_data *v4lconvert_create(int fd); +void v4lconvert_destroy(struct v4lconvert_data *data); + +/* With regards to dest_fmt just like VIDIOC_TRY_FMT, except that the try + format will succeed and return the requested V4L2_PIX_FMT_foo in dest_fmt if + the cam has a format from which v4lconvert can convert to dest_fmt. + The real format to which the cam should be set is returned through src_fmt + when not NULL. */ +int v4lconvert_try_format(struct v4lconvert_data *data, + struct v4l2_format *dest_fmt, /* in / out */ + struct v4l2_format *src_fmt /* out */ +); + +/* Just like VIDIOC_ENUM_FMT, except that the emulated formats are added at + the end of the list */ +int v4lconvert_enum_fmt(struct v4lconvert_data *data, struct v4l2_fmtdesc *fmt); + +/* return value of -1 on error, otherwise the amount of bytes written to + dest */ +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); + +/* get a string describing the last error*/ +const char *v4lconvert_get_error_message(struct v4lconvert_data *data); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile new file mode 100644 index 000000000..cbd54f471 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile @@ -0,0 +1,46 @@ +CC = gcc +LD = gcc + +CPPFLAGS = -fPIC -I../include -I../../../../linux/include + +CFLAGS := -g -O1 +CFLAGS += -Wall -W -Wno-unused -Wpointer-arith -Wstrict-prototypes + +LDFLAGS = -shared + +CONVERT_LIB = libv4lconvert.so +CONVERT_OBJS = libv4lconvert.o tinyjpeg.o \ + jidctflt.o spca561-decompress.o rgbyuv.o spca501.o bayer.o +TARGETS = $(CONVERT_LIB) +INCLUDES = ../include/libv4lconvert.h + +ifeq ($(LIB_RELEASE),) +LIB_RELEASE = 0 +endif + +ifeq ($(DESTDIR),) +DESTDIR = /usr/local +endif + +all: $(TARGETS) + +$(CONVERT_LIB): $(CONVERT_OBJS) + +install: all + mkdir -p $(DESTDIR)/include + cp $(INCLUDES) $(DESTDIR)/include + mkdir -p $(DESTDIR)/lib + cp $(CONVERT_LIB).$(LIB_RELEASE) $(DESTDIR)/lib + cd $(DESTDIR)/lib && \ + ln -f -s $(CONVERT_LIB).$(LIB_RELEASE) $(CONVERT_LIB) + +clean:: + rm -f *.so* *.o log *~ + rm -f *.d + +%.o: %.c + $(CC) -c -MMD $(CPPFLAGS) $(CFLAGS) -o $@ $< + +%.so: + $(CC) $(LDFLAGS) -Wl,-soname,$@.$(LIB_RELEASE) -o $@.$(LIB_RELEASE) $^ + ln -f -s $@.$(LIB_RELEASE) $@ diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/bayer.c b/v4l2-apps/lib/libv4l/libv4lconvert/bayer.c new file mode 100644 index 000000000..166c13011 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/bayer.c @@ -0,0 +1,597 @@ +/* + * lib4lconvert, video4linux2 format conversion lib + * (C) 2008 Hans de Goede + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Note: original bayer_to_bgr24 code from : + * 1394-Based Digital Camera Control Library + * + * Bayer pattern decoding functions + * + * Written by Damien Douxchamps and Frederic Devernay + * + * Note that the original bayer.c in libdc1394 supports many different + * bayer decode algorithms, for lib4lconvert the one in this file has been + * chosen (and optimized a bit) and the other algorithm's have been removed, + * see bayer.c from libdc1394 for all supported algorithms + */ + +#include +#include "libv4lconvert-priv.h" + +/************************************************************** + * Color conversion functions for cameras that can * + * output raw-Bayer pattern images, such as some Basler and * + * Point Grey camera. Most of the algos presented here come * + * from http://www-ise.stanford.edu/~tingchen/ and have been * + * converted from Matlab to C and extended to all elementary * + * patterns. * + **************************************************************/ + +/* insprired by OpenCV's Bayer decoding */ +static void v4lconvert_border_bayer_line_to_bgr24( + const unsigned char* bayer, const unsigned char* adjacent_bayer, + unsigned char *bgr, int width, int start_with_green, int blue_line) +{ + int t0, t1; + + if (start_with_green) { + /* First pixel */ + if (blue_line) { + *bgr++ = bayer[1]; + *bgr++ = bayer[0]; + *bgr++ = adjacent_bayer[0]; + } else { + *bgr++ = adjacent_bayer[0]; + *bgr++ = bayer[0]; + *bgr++ = bayer[1]; + } + /* Second pixel */ + t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3; + t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1; + if (blue_line) { + *bgr++ = bayer[1]; + *bgr++ = t0; + *bgr++ = t1; + } else { + *bgr++ = t1; + *bgr++ = t0; + *bgr++ = bayer[1]; + } + bayer++; + adjacent_bayer++; + width -= 2; + } else { + /* First pixel */ + t0 = (bayer[1] + adjacent_bayer[0] + 1) >> 1; + if (blue_line) { + *bgr++ = bayer[0]; + *bgr++ = t0; + *bgr++ = adjacent_bayer[1]; + } else { + *bgr++ = adjacent_bayer[1]; + *bgr++ = t0; + *bgr++ = bayer[0]; + } + width--; + } + + if (blue_line) { + for ( ; width > 2; width -= 2) { + t0 = (bayer[0] + bayer[2] + 1) >> 1; + *bgr++ = t0; + *bgr++ = bayer[1]; + *bgr++ = adjacent_bayer[1]; + bayer++; + adjacent_bayer++; + + t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3; + t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1; + *bgr++ = bayer[1]; + *bgr++ = t0; + *bgr++ = t1; + bayer++; + adjacent_bayer++; + } + } else { + for ( ; width > 2; width -= 2) { + t0 = (bayer[0] + bayer[2] + 1) >> 1; + *bgr++ = adjacent_bayer[1]; + *bgr++ = bayer[1]; + *bgr++ = t0; + bayer++; + adjacent_bayer++; + + t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3; + t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1; + *bgr++ = t1; + *bgr++ = t0; + *bgr++ = bayer[1]; + bayer++; + adjacent_bayer++; + } + } + + if (width == 2) { + /* Second to last pixel */ + t0 = (bayer[0] + bayer[2] + 1) >> 1; + if (blue_line) { + *bgr++ = t0; + *bgr++ = bayer[1]; + *bgr++ = adjacent_bayer[1]; + } else { + *bgr++ = adjacent_bayer[1]; + *bgr++ = bayer[1]; + *bgr++ = t0; + } + /* Last pixel */ + t0 = (bayer[1] + adjacent_bayer[2] + 1) >> 1; + if (blue_line) { + *bgr++ = bayer[2]; + *bgr++ = t0; + *bgr++ = adjacent_bayer[1]; + } else { + *bgr++ = adjacent_bayer[1]; + *bgr++ = t0; + *bgr++ = bayer[2]; + } + } else { + /* Last pixel */ + if (blue_line) { + *bgr++ = bayer[0]; + *bgr++ = bayer[1]; + *bgr++ = adjacent_bayer[1]; + } else { + *bgr++ = adjacent_bayer[1]; + *bgr++ = bayer[1]; + *bgr++ = bayer[0]; + } + } +} + +/* From libdc1394, which on turn was based on OpenCV's Bayer decoding */ +void v4lconvert_bayer_to_bgr24(const unsigned char *bayer, + unsigned char *bgr, int width, int height, unsigned int pixfmt) +{ + int blue_line = pixfmt == V4L2_PIX_FMT_SBGGR8 + || pixfmt == V4L2_PIX_FMT_SGBRG8; + int start_with_green = pixfmt == V4L2_PIX_FMT_SGBRG8 + || pixfmt == V4L2_PIX_FMT_SGRBG8; + + /* render the first line */ + v4lconvert_border_bayer_line_to_bgr24(bayer, bayer + width, bgr, width, + start_with_green, blue_line); + bgr += width * 3; + + /* reduce height by 2 because of the special case top/bottom line */ + for (height -= 2; height; height--) { + int t0, t1; + /* (width - 2) because of the border */ + const unsigned char *bayerEnd = bayer + (width - 2); + + if (start_with_green) { + /* OpenCV has a bug in the next line, which was + t0 = (bayer[0] + bayer[width * 2] + 1) >> 1; */ + t0 = (bayer[1] + bayer[width * 2 + 1] + 1) >> 1; + /* Write first pixel */ + t1 = (bayer[0] + bayer[width * 2] + bayer[width + 1] + 1) / 3; + if (blue_line) { + *bgr++ = t0; + *bgr++ = t1; + *bgr++ = bayer[width]; + } else { + *bgr++ = bayer[width]; + *bgr++ = t1; + *bgr++ = t0; + } + + /* Write second pixel */ + t1 = (bayer[width] + bayer[width + 2] + 1) >> 1; + if (blue_line) { + *bgr++ = t0; + *bgr++ = bayer[width + 1]; + *bgr++ = t1; + } else { + *bgr++ = t1; + *bgr++ = bayer[width + 1]; + *bgr++ = t0; + } + bayer++; + } else { + /* Write first pixel */ + t0 = (bayer[0] + bayer[width * 2] + 1) >> 1; + if (blue_line) { + *bgr++ = t0; + *bgr++ = bayer[width]; + *bgr++ = bayer[width + 1]; + } else { + *bgr++ = bayer[width + 1]; + *bgr++ = bayer[width]; + *bgr++ = t0; + } + } + + if (blue_line) { + for (; bayer <= bayerEnd - 2; bayer += 2) { + t0 = (bayer[0] + bayer[2] + bayer[width * 2] + + bayer[width * 2 + 2] + 2) >> 2; + t1 = (bayer[1] + bayer[width] + + bayer[width + 2] + bayer[width * 2 + 1] + + 2) >> 2; + *bgr++ = t0; + *bgr++ = t1; + *bgr++ = bayer[width + 1]; + + t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1; + t1 = (bayer[width + 1] + bayer[width + 3] + + 1) >> 1; + *bgr++ = t0; + *bgr++ = bayer[width + 2]; + *bgr++ = t1; + } + } else { + for (; bayer <= bayerEnd - 2; bayer += 2) { + t0 = (bayer[0] + bayer[2] + bayer[width * 2] + + bayer[width * 2 + 2] + 2) >> 2; + t1 = (bayer[1] + bayer[width] + + bayer[width + 2] + bayer[width * 2 + 1] + + 2) >> 2; + *bgr++ = bayer[width + 1]; + *bgr++ = t1; + *bgr++ = t0; + + t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1; + t1 = (bayer[width + 1] + bayer[width + 3] + + 1) >> 1; + *bgr++ = t1; + *bgr++ = bayer[width + 2]; + *bgr++ = t0; + } + } + + if (bayer < bayerEnd) { + /* write second to last pixel */ + t0 = (bayer[0] + bayer[2] + bayer[width * 2] + + bayer[width * 2 + 2] + 2) >> 2; + t1 = (bayer[1] + bayer[width] + + bayer[width + 2] + bayer[width * 2 + 1] + + 2) >> 2; + if (blue_line) { + *bgr++ = t0; + *bgr++ = t1; + *bgr++ = bayer[width + 1]; + } else { + *bgr++ = bayer[width + 1]; + *bgr++ = t1; + *bgr++ = t0; + } + /* write last pixel */ + t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1; + if (blue_line) { + *bgr++ = t0; + *bgr++ = bayer[width + 2]; + *bgr++ = bayer[width + 1]; + } else { + *bgr++ = bayer[width + 1]; + *bgr++ = bayer[width + 2]; + *bgr++ = t0; + } + bayer++; + } else { + /* write last pixel */ + t0 = (bayer[0] + bayer[width * 2] + 1) >> 1; + t1 = (bayer[1] + bayer[width * 2 + 1] + bayer[width] + 1) / 3; + if (blue_line) { + *bgr++ = t0; + *bgr++ = t1; + *bgr++ = bayer[width + 1]; + } else { + *bgr++ = bayer[width + 1]; + *bgr++ = t1; + *bgr++ = t0; + } + } + + /* skip 2 border pixels */ + bayer += 2; + + blue_line = !blue_line; + start_with_green = !start_with_green; + } + + /* render the last line */ + v4lconvert_border_bayer_line_to_bgr24(bayer + width, bayer, bgr, width, + !start_with_green, !blue_line); +} + +static void v4lconvert_border_bayer_line_to_y( + const unsigned char* bayer, const unsigned char* adjacent_bayer, + unsigned char *y, int width, int start_with_green, int blue_line) +{ + int t0, t1; + + if (start_with_green) { + /* First pixel */ + if (blue_line) { + *y++ = (8453*adjacent_bayer[0] + 16594*bayer[0] + 3223*bayer[1] + 524288) + >> 15; + } else { + *y++ = (8453*bayer[1] + 16594*bayer[0] + 3223*adjacent_bayer[0] + 524288) + >> 15; + } + /* Second pixel */ + t0 = bayer[0] + bayer[2] + adjacent_bayer[1]; + t1 = adjacent_bayer[0] + adjacent_bayer[2]; + if (blue_line) { + *y++ = (4226*t1 + 5531*t0 + 3223*bayer[1] + 524288) >> 15; + } else { + *y++ = (8453*bayer[1] + 5531*t0 + 1611*t1 + 524288) >> 15; + } + bayer++; + adjacent_bayer++; + width -= 2; + } else { + /* First pixel */ + t0 = bayer[1] + adjacent_bayer[0]; + if (blue_line) { + *y++ = (8453*adjacent_bayer[1] + 8297*t0 + 3223*bayer[0] + 524288) + >> 15; + } else { + *y++ = (8453*bayer[0] + 8297*t0 + 3223*adjacent_bayer[1] + 524288) + >> 15; + } + width--; + } + + if (blue_line) { + for ( ; width > 2; width -= 2) { + t0 = bayer[0] + bayer[2]; + *y++ = (8453*adjacent_bayer[1] + 16594*bayer[1] + 1611*t0 + 524288) + >> 15; + bayer++; + adjacent_bayer++; + + t0 = bayer[0] + bayer[2] + adjacent_bayer[1]; + t1 = adjacent_bayer[0] + adjacent_bayer[2]; + *y++ = (4226*t1 + 5531*t0 + 3223*bayer[1] + 524288) >> 15; + bayer++; + adjacent_bayer++; + } + } else { + for ( ; width > 2; width -= 2) { + t0 = bayer[0] + bayer[2]; + *y++ = (4226*t0 + 16594*bayer[1] + 3223*adjacent_bayer[1] + 524288) + >> 15; + bayer++; + adjacent_bayer++; + + t0 = bayer[0] + bayer[2] + adjacent_bayer[1]; + t1 = adjacent_bayer[0] + adjacent_bayer[2]; + *y++ = (8453*bayer[1] + 5531*t0 + 1611*t1 + 524288) >> 15; + bayer++; + adjacent_bayer++; + } + } + + if (width == 2) { + /* Second to last pixel */ + t0 = bayer[0] + bayer[2]; + if (blue_line) { + *y++ = (8453*adjacent_bayer[1] + 16594*bayer[1] + 1611*t0 + 524288) + >> 15; + } else { + *y++ = (4226*t0 + 16594*bayer[1] + 3223*adjacent_bayer[1] + 524288) + >> 15; + } + /* Last pixel */ + t0 = bayer[1] + adjacent_bayer[2]; + if (blue_line) { + *y++ = (8453*adjacent_bayer[1] + 8297*t0 + 3223*bayer[2] + 524288) + >> 15; + } else { + *y++ = (8453*bayer[2] + 8297*t0 + 3223*adjacent_bayer[1] + 524288) + >> 15; + } + } else { + /* Last pixel */ + if (blue_line) { + *y++ = (8453*adjacent_bayer[1] + 16594*bayer[1] + 3223*bayer[0] + 524288) + >> 15; + } else { + *y++ = (8453*bayer[0] + 16594*bayer[1] + 3223*adjacent_bayer[1] + 524288) + >> 15; + } + } +} + +void v4lconvert_bayer_to_yuv420(const unsigned char *bayer, + unsigned char *yuv, int width, int height, unsigned int pixfmt) +{ + int blue_line = 0, start_with_green = 0, x, y; + unsigned char *ydst = yuv; + unsigned char *udst = yuv + width * height; + unsigned char *vdst = udst + width * height / 4; + + /* First calculate the u and v planes 2x2 pixels at a time */ + switch (pixfmt) { + case V4L2_PIX_FMT_SBGGR8: + for (y = 0; y < height; y += 2) { + for (x = 0; x < width; x += 2) { + int b, g, r; + b = bayer[x]; + g = bayer[x+1]; + g += bayer[x+width]; + r = bayer[x+width+1]; + *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15; + *vdst++ = (14456 * r - 6052 * g - 2351 * b + 4210688) >> 15; + } + bayer += 2 * width; + } + blue_line = 1; + break; + + case V4L2_PIX_FMT_SRGGB8: + for (y = 0; y < height; y += 2) { + for (x = 0; x < width; x += 2) { + int b, g, r; + r = bayer[x]; + g = bayer[x+1]; + g += bayer[x+width]; + b = bayer[x+width+1]; + *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15; + *vdst++ = (14456 * r - 6052 * g - 2351 * b + 4210688) >> 15; + } + bayer += 2 * width; + } + break; + + case V4L2_PIX_FMT_SGBRG8: + for (y = 0; y < height; y += 2) { + for (x = 0; x < width; x += 2) { + int b, g, r; + g = bayer[x]; + b = bayer[x+1]; + r = bayer[x+width]; + g += bayer[x+width+1]; + *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15; + *vdst++ = (14456 * r - 6052 * g - 2351 * b + 4210688) >> 15; + } + bayer += 2 * width; + } + blue_line = 1; + start_with_green = 1; + break; + + case V4L2_PIX_FMT_SGRBG8: + for (y = 0; y < height; y += 2) { + for (x = 0; x < width; x += 2) { + int b, g, r; + g = bayer[x]; + r = bayer[x+1]; + b = bayer[x+width]; + g += bayer[x+width+1]; + *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15; + *vdst++ = (14456 * r - 6052 * g - 2351 * b + 4210688) >> 15; + } + bayer += 2 * width; + } + start_with_green = 1; + break; + } + + bayer -= width * height; + + /* render the first line */ + v4lconvert_border_bayer_line_to_y(bayer, bayer + width, ydst, width, + start_with_green, blue_line); + ydst += width; + + /* reduce height by 2 because of the border */ + for (height -= 2; height; height--) { + int t0, t1; + /* (width - 2) because of the border */ + const unsigned char *bayerEnd = bayer + (width - 2); + + if (start_with_green) { + t0 = bayer[1] + bayer[width * 2 + 1]; + /* Write first pixel */ + t1 = bayer[0] + bayer[width * 2] + bayer[width + 1]; + if (blue_line) + *ydst++ = (8453*bayer[width] + 5516*t1 + 1661*t0 + 524288) >> 15; + else + *ydst++ = (4226*t0 + 5516*t1 + 3223*bayer[width] + 524288) >> 15; + + /* Write second pixel */ + t1 = bayer[width] + bayer[width + 2]; + if (blue_line) + *ydst++ = (4226*t1 + 16594*bayer[width+1] + 1611*t0 + 524288) >> 15; + else + *ydst++ = (4226*t0 + 16594*bayer[width+1] + 1611*t1 + 524288) >> 15; + bayer++; + } else { + /* Write first pixel */ + t0 = bayer[0] + bayer[width * 2]; + if (blue_line) { + *ydst++ = (8453*bayer[width+1] + 16594*bayer[width] + 1661*t0 + + 524288) >> 15; + } else { + *ydst++ = (4226*t0 + 16594*bayer[width] + 3223*bayer[width+1] + + 524288) >> 15; + } + } + + if (blue_line) { + for (; bayer <= bayerEnd - 2; bayer += 2) { + t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2]; + t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1]; + *ydst++ = (8453*bayer[width+1] + 4148*t1 + 806*t0 + 524288) >> 15; + + t0 = bayer[2] + bayer[width * 2 + 2]; + t1 = bayer[width + 1] + bayer[width + 3]; + *ydst++ = (4226*t1 + 16594*bayer[width+2] + 1611*t0 + 524288) >> 15; + } + } else { + for (; bayer <= bayerEnd - 2; bayer += 2) { + t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2]; + t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1]; + *ydst++ = (2113*t0 + 4148*t1 + 3223*bayer[width+1] + 524288) >> 15; + + t0 = bayer[2] + bayer[width * 2 + 2]; + t1 = bayer[width + 1] + bayer[width + 3]; + *ydst++ = (4226*t0 + 16594*bayer[width+2] + 1611*t1 + 524288) >> 15; + } + } + + if (bayer < bayerEnd) { + /* Write second to last pixel */ + t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2]; + t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1]; + if (blue_line) + *ydst++ = (8453*bayer[width+1] + 4148*t1 + 806*t0 + 524288) >> 15; + else + *ydst++ = (2113*t0 + 4148*t1 + 3223*bayer[width+1] + 524288) >> 15; + + /* write last pixel */ + t0 = bayer[2] + bayer[width * 2 + 2]; + if (blue_line) { + *ydst++ = (8453*bayer[width+1] + 16594*bayer[width+2] + 1661*t0 + + 524288) >> 15; + } else { + *ydst++ = (4226*t0 + 16594*bayer[width+2] + 3223*bayer[width+1] + + 524288) >> 15; + } + bayer++; + } else { + /* write last pixel */ + t0 = bayer[0] + bayer[width * 2]; + t1 = bayer[1] + bayer[width * 2 + 1] + bayer[width]; + if (blue_line) + *ydst++ = (8453*bayer[width+1] + 5516*t1 + 1661*t0 + 524288) >> 15; + else + *ydst++ = (4226*t0 + 5516*t1 + 3223*bayer[width+1] + 524288) >> 15; + } + + /* skip 2 border pixels */ + bayer += 2; + + blue_line = !blue_line; + start_with_green = !start_with_green; + } + + /* render the last line */ + v4lconvert_border_bayer_line_to_y(bayer + width, bayer, ydst, width, + !start_with_green, !blue_line); +} diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c b/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c new file mode 100644 index 000000000..4b3ea428c --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c @@ -0,0 +1,286 @@ +/* + * jidctflt.c + * + * Copyright (C) 1994-1998, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * + * The authors make NO WARRANTY or representation, either express or implied, + * with respect to this software, its quality, accuracy, merchantability, or + * fitness for a particular purpose. This software is provided "AS IS", and you, + * its user, assume the entire risk as to its quality and accuracy. + * + * This software is copyright (C) 1991-1998, Thomas G. Lane. + * All Rights Reserved except as specified below. + * + * Permission is hereby granted to use, copy, modify, and distribute this + * software (or portions thereof) for any purpose, without fee, subject to these + * conditions: + * (1) If any part of the source code for this software is distributed, then this + * README file must be included, with this copyright and no-warranty notice + * unaltered; and any additions, deletions, or changes to the original files + * must be clearly indicated in accompanying documentation. + * (2) If only executable code is distributed, then the accompanying + * documentation must state that "this software is based in part on the work of + * the Independent JPEG Group". + * (3) Permission for use of this software is granted only if the user accepts + * full responsibility for any undesirable consequences; the authors accept + * NO LIABILITY for damages of any kind. + * + * These conditions apply to any software derived from or based on the IJG code, + * not just to the unmodified library. If you use our work, you ought to + * acknowledge us. + * + * Permission is NOT granted for the use of any IJG author's name or company name + * in advertising or publicity relating to this software or products derived from + * it. This software may be referred to only as "the Independent JPEG Group's + * software". + * + * We specifically permit and encourage the use of this software as the basis of + * commercial products, provided that all warranty or liability claims are + * assumed by the product vendor. + * + * + * This file contains a floating-point implementation of the + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine + * must also perform dequantization of the input coefficients. + * + * This implementation should be more accurate than either of the integer + * IDCT implementations. However, it may not give the same results on all + * machines because of differences in roundoff behavior. Speed will depend + * on the hardware's floating point capacity. + * + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT + * on each row (or vice versa, but it's more convenient to emit a row at + * a time). Direct algorithms are also available, but they are much more + * complex and seem not to be any faster when reduced to code. + * + * This implementation is based on Arai, Agui, and Nakajima's algorithm for + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in + * Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following code + * is based directly on figure 4-8 in P&M. + * While an 8-point DCT cannot be done in less than 11 multiplies, it is + * possible to arrange the computation so that many of the multiplies are + * simple scalings of the final outputs. These multiplies can then be + * folded into the multiplications or divisions by the JPEG quantization + * table entries. The AA&N method leaves only 5 multiplies and 29 adds + * to be done in the DCT itself. + * The primary disadvantage of this method is that with a fixed-point + * implementation, accuracy is lost due to imprecise representation of the + * scaled quantization values. However, that problem does not arise if + * we use floating point arithmetic. + */ + +#include +#include "tinyjpeg-internal.h" + +#define FAST_FLOAT float +#define DCTSIZE 8 +#define DCTSIZE2 (DCTSIZE*DCTSIZE) + +#define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval)) + +#if defined(__GNUC__) && (defined(__i686__)) // || defined(__x86_64__)) + +static inline unsigned char descale_and_clamp(int x, int shift) +{ + __asm__ ( + "add %3,%1\n" + "\tsar %2,%1\n" + "\tsub $-128,%1\n" + "\tcmovl %5,%1\n" /* Use the sub to compare to 0 */ + "\tcmpl %4,%1\n" + "\tcmovg %4,%1\n" + : "=r"(x) + : "0"(x), "Ir"(shift), "ir"(1UL<<(shift-1)), "r" (0xff), "r" (0) + ); + return x; +} + +#else +static inline unsigned char descale_and_clamp(int x, int shift) +{ + x += (1UL<<(shift-1)); + if (x<0) + x = (x >> shift) | ((~(0UL)) << (32-(shift))); + else + x >>= shift; + x += 128; + if (x>255) + return 255; + else if (x<0) + return 0; + else + return x; +} +#endif + +/* + * Perform dequantization and inverse DCT on one block of coefficients. + */ + +void +tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride) +{ + FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + FAST_FLOAT tmp10, tmp11, tmp12, tmp13; + FAST_FLOAT z5, z10, z11, z12, z13; + int16_t *inptr; + FAST_FLOAT *quantptr; + FAST_FLOAT *wsptr; + uint8_t *outptr; + int ctr; + FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */ + + /* Pass 1: process columns from input, store into work array. */ + + inptr = compptr->DCT; + quantptr = compptr->Q_table; + wsptr = workspace; + for (ctr = DCTSIZE; ctr > 0; ctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any column in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * column DCT calculations can be simplified this way. + */ + + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && + inptr[DCTSIZE*7] == 0) { + /* AC terms all zero */ + FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + + wsptr[DCTSIZE*0] = dcval; + wsptr[DCTSIZE*1] = dcval; + wsptr[DCTSIZE*2] = dcval; + wsptr[DCTSIZE*3] = dcval; + wsptr[DCTSIZE*4] = dcval; + wsptr[DCTSIZE*5] = dcval; + wsptr[DCTSIZE*6] = dcval; + wsptr[DCTSIZE*7] = dcval; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + continue; + } + + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp10 = tmp0 + tmp2; /* phase 3 */ + tmp11 = tmp0 - tmp2; + + tmp13 = tmp1 + tmp3; /* phases 5-3 */ + tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */ + + tmp0 = tmp10 + tmp13; /* phase 2 */ + tmp3 = tmp10 - tmp13; + tmp1 = tmp11 + tmp12; + tmp2 = tmp11 - tmp12; + + /* Odd part */ + + tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + z13 = tmp6 + tmp5; /* phase 6 */ + z10 = tmp6 - tmp5; + z11 = tmp4 + tmp7; + z12 = tmp4 - tmp7; + + tmp7 = z11 + z13; /* phase 5 */ + tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */ + + z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ + tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ + tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ + + tmp6 = tmp12 - tmp7; /* phase 2 */ + tmp5 = tmp11 - tmp6; + tmp4 = tmp10 + tmp5; + + wsptr[DCTSIZE*0] = tmp0 + tmp7; + wsptr[DCTSIZE*7] = tmp0 - tmp7; + wsptr[DCTSIZE*1] = tmp1 + tmp6; + wsptr[DCTSIZE*6] = tmp1 - tmp6; + wsptr[DCTSIZE*2] = tmp2 + tmp5; + wsptr[DCTSIZE*5] = tmp2 - tmp5; + wsptr[DCTSIZE*4] = tmp3 + tmp4; + wsptr[DCTSIZE*3] = tmp3 - tmp4; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + } + + /* Pass 2: process rows from work array, store into output array. */ + /* Note that we must descale the results by a factor of 8 == 2**3. */ + + wsptr = workspace; + outptr = output_buf; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + /* Rows of zeroes can be exploited in the same way as we did with columns. + * However, the column calculation has created many nonzero AC terms, so + * the simplification applies less often (typically 5% to 10% of the time). + * And testing floats for zero is relatively expensive, so we don't bother. + */ + + /* Even part */ + + tmp10 = wsptr[0] + wsptr[4]; + tmp11 = wsptr[0] - wsptr[4]; + + tmp13 = wsptr[2] + wsptr[6]; + tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13; + + tmp0 = tmp10 + tmp13; + tmp3 = tmp10 - tmp13; + tmp1 = tmp11 + tmp12; + tmp2 = tmp11 - tmp12; + + /* Odd part */ + + z13 = wsptr[5] + wsptr[3]; + z10 = wsptr[5] - wsptr[3]; + z11 = wsptr[1] + wsptr[7]; + z12 = wsptr[1] - wsptr[7]; + + tmp7 = z11 + z13; + tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); + + z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ + tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ + tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ + + tmp6 = tmp12 - tmp7; + tmp5 = tmp11 - tmp6; + tmp4 = tmp10 + tmp5; + + /* Final output stage: scale down by a factor of 8 and range-limit */ + + outptr[0] = descale_and_clamp((int)(tmp0 + tmp7), 3); + outptr[7] = descale_and_clamp((int)(tmp0 - tmp7), 3); + outptr[1] = descale_and_clamp((int)(tmp1 + tmp6), 3); + outptr[6] = descale_and_clamp((int)(tmp1 - tmp6), 3); + outptr[2] = descale_and_clamp((int)(tmp2 + tmp5), 3); + outptr[5] = descale_and_clamp((int)(tmp2 - tmp5), 3); + outptr[4] = descale_and_clamp((int)(tmp3 + tmp4), 3); + outptr[3] = descale_and_clamp((int)(tmp3 - tmp4), 3); + + + wsptr += DCTSIZE; /* advance pointer to next row */ + outptr += stride; + } +} + diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h new file mode 100644 index 000000000..ce050119e --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h @@ -0,0 +1,80 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __LIBV4LCONVERT_PRIV_H +#define __LIBV4LCONVERT_PRIV_H + +#include +#include "libv4lconvert.h" +#include "tinyjpeg.h" + +#ifndef V4L2_PIX_FMT_SPCA501 +#define V4L2_PIX_FMT_SPCA501 v4l2_fourcc('S','5','0','1') +#endif + +#ifndef V4L2_PIX_FMT_SPCA561 +#define V4L2_PIX_FMT_SPCA561 v4l2_fourcc('S','5','6','1') +#endif + +#ifndef V4L2_PIX_FMT_SGBRG8 +#define V4L2_PIX_FMT_SGBRG8 v4l2_fourcc('G','B','R','G') +#endif + +#ifndef V4L2_PIX_FMT_SGRBG8 +#define V4L2_PIX_FMT_SGRBG8 v4l2_fourcc('G','R','B','G') +#endif + +#ifndef V4L2_PIX_FMT_SRGGB8 +#define V4L2_PIX_FMT_SRGGB8 v4l2_fourcc('R','G','G','B') +#endif + +#define V4LCONVERT_ERROR_MSG_SIZE 256 + +#define V4LCONVERT_ERR(...) \ + snprintf(data->error_msg, V4LCONVERT_ERROR_MSG_SIZE, \ + "v4l-convert: error " __VA_ARGS__) + + +struct v4lconvert_data { + int fd; + int supported_src_formats; /* bitfield */ + int no_formats; + char error_msg[V4LCONVERT_ERROR_MSG_SIZE]; + struct jdec_private *jdec; +}; + + +void v4lconvert_yuv420_to_bgr24(const unsigned char *src, unsigned char *dst, + int width, int height); + +void v4lconvert_spca501_to_yuv420(const unsigned char *src, unsigned char *dst, + int width, int height); + +void v4lconvert_spca501_to_bgr24(const unsigned char *src, unsigned char *dst, + int width, int height); + +void v4lconvert_decode_spca561(const unsigned char *src, unsigned char *dst, + int width, int height); + +void v4lconvert_bayer_to_bgr24(const unsigned char *bayer, + unsigned char *rgb, int width, int height, unsigned int pixfmt); + +void v4lconvert_bayer_to_yuv420(const unsigned char *bayer, + unsigned char *yuv, int width, int height, unsigned int pixfmt); + +#endif diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c new file mode 100644 index 000000000..ad94c4ef3 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c @@ -0,0 +1,347 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +#include +#include +#include "libv4lconvert.h" +#include "libv4lconvert-priv.h" + +#define MIN(a,b) (((a)<(b))?(a):(b)) + +static const unsigned int supported_src_pixfmts[] = { + V4L2_PIX_FMT_BGR24, + V4L2_PIX_FMT_YUV420, + V4L2_PIX_FMT_MJPEG, + V4L2_PIX_FMT_JPEG, + V4L2_PIX_FMT_SBGGR8, + V4L2_PIX_FMT_SGBRG8, + V4L2_PIX_FMT_SGRBG8, + V4L2_PIX_FMT_SRGGB8, + V4L2_PIX_FMT_SPCA501, + V4L2_PIX_FMT_SPCA561, + -1 +}; + +static const unsigned int supported_dst_pixfmts[] = { + V4L2_PIX_FMT_BGR24, + V4L2_PIX_FMT_YUV420, + -1 +}; + + +struct v4lconvert_data *v4lconvert_create(int fd) +{ + int i, j; + struct v4lconvert_data *data = calloc(1, sizeof(struct v4lconvert_data)); + + if (!data) + return NULL; + + data->fd = fd; + data->jdec = NULL; + + /* Check supported formats */ + for (i = 0; ; i++) { + struct v4l2_fmtdesc fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; + + fmt.index = i; + + if (syscall(SYS_ioctl, fd, VIDIOC_ENUM_FMT, &fmt)) + break; + + for (j = 0; supported_src_pixfmts[j] != -1; j++) + if (fmt.pixelformat == supported_src_pixfmts[j]) { + data->supported_src_formats |= 1 << j; + break; + } + } + + data->no_formats = i; + + return data; +} + +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); +} + +/* See libv4lconvert.h for description of in / out parameters */ +int v4lconvert_enum_fmt(struct v4lconvert_data *data, struct v4l2_fmtdesc *fmt) +{ + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || + fmt->index < data->no_formats || + !data->supported_src_formats) + return syscall(SYS_ioctl, data->fd, VIDIOC_ENUM_FMT, fmt); + + fmt->flags = 0; + fmt->pixelformat = -1; + memset(fmt->reserved, 0, 4); + + /* Note bgr24 and yuv420 are the first 2 in our mask of supported formats */ + switch (fmt->index - data->no_formats) { + case 0: + if (!(data->supported_src_formats & 1)) { + strcpy((char *)fmt->description, "BGR24"); + fmt->pixelformat = V4L2_PIX_FMT_BGR24; + } else if (!(data->supported_src_formats & 2)) { + strcpy((char *)fmt->description, "YUV420"); + fmt->pixelformat = V4L2_PIX_FMT_YUV420; + } + break; + case 1: + if (!(data->supported_src_formats & 3)) { + strcpy((char *)fmt->description, "YUV420"); + fmt->pixelformat = V4L2_PIX_FMT_YUV420; + } + break; + } + if (fmt->pixelformat == -1) { + errno = EINVAL; + return -1; + } + + return 0; +} + +/* See libv4lconvert.h for description of in / out parameters */ +int v4lconvert_try_format(struct v4lconvert_data *data, + struct v4l2_format *dest_fmt, struct v4l2_format *src_fmt) +{ + int i; + unsigned int try_pixfmt, closest_fmt_size_diff = -1; + unsigned int desired_pixfmt = dest_fmt->fmt.pix.pixelformat; + struct v4l2_format try_fmt, closest_fmt = { .type = -1 }; + + for (i = 0; supported_dst_pixfmts[i] != -1; i++) + if (supported_dst_pixfmts[i] == desired_pixfmt) + break; + + /* Can we do conversion to the requested format & type? */ + if (supported_dst_pixfmts[i] == -1 || + dest_fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + int ret = syscall(SYS_ioctl, data->fd, VIDIOC_TRY_FMT, dest_fmt); + if (src_fmt) + *src_fmt = *dest_fmt; + return ret; + } + + for (i = 0; (try_pixfmt = supported_src_pixfmts[i]) != -1; i++) { + /* is this format supported? */ + if (!(data->supported_src_formats & (1 << i))) + continue; + + try_fmt = *dest_fmt; + try_fmt.fmt.pix.pixelformat = try_pixfmt; + + if (!syscall(SYS_ioctl, data->fd, VIDIOC_TRY_FMT, &try_fmt)) + { + if (try_fmt.fmt.pix.pixelformat == try_pixfmt) { + int size_x_diff = abs((int)try_fmt.fmt.pix.width - + (int)dest_fmt->fmt.pix.width); + int size_y_diff = abs((int)try_fmt.fmt.pix.height - + (int)dest_fmt->fmt.pix.height); + unsigned int size_diff = size_x_diff * size_x_diff + + size_y_diff * size_y_diff; + if (size_diff < closest_fmt_size_diff) { + closest_fmt_size_diff = size_diff; + closest_fmt = try_fmt; + } + } + } + } + + if (closest_fmt.type == -1) { + errno = EINVAL; + return -1; + } + + *dest_fmt = closest_fmt; + + /* Are we converting? */ + if (closest_fmt.fmt.pix.pixelformat != desired_pixfmt) { + dest_fmt->fmt.pix.pixelformat = desired_pixfmt; + switch (dest_fmt->fmt.pix.pixelformat) { + case V4L2_PIX_FMT_BGR24: + dest_fmt->fmt.pix.bytesperline = dest_fmt->fmt.pix.width * 3; + dest_fmt->fmt.pix.sizeimage = dest_fmt->fmt.pix.width * + dest_fmt->fmt.pix.height * 3; + break; + case V4L2_PIX_FMT_YUV420: + dest_fmt->fmt.pix.bytesperline = dest_fmt->fmt.pix.width; + dest_fmt->fmt.pix.sizeimage = (dest_fmt->fmt.pix.width * + dest_fmt->fmt.pix.height * 3) / 2; + break; + } + } + + if (src_fmt) + *src_fmt = closest_fmt; + + return 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) +{ + unsigned int header_width, header_height; + int result, needed; + unsigned char *components[3]; + + /* Special case when no conversion is needed */ + if(!memcmp(src_fmt, dest_fmt, sizeof(*src_fmt))) { + int to_copy = MIN(dest_size, src_size); + memcpy(dest, src, to_copy); + return to_copy; + } + + /* sanity check, is the dest buffer large enough? */ + switch (dest_fmt->fmt.pix.pixelformat) { + case V4L2_PIX_FMT_BGR24: + needed = dest_fmt->fmt.pix.width * dest_fmt->fmt.pix.height * 3; + break; + case V4L2_PIX_FMT_YUV420: + needed = (dest_fmt->fmt.pix.width * dest_fmt->fmt.pix.height * 3) / 2; + break; + default: + V4LCONVERT_ERR("Unknown dest format in conversion\n"); + errno = EINVAL; + return -1; + } + + if (dest_size < needed) { + V4LCONVERT_ERR("destination buffer too small\n"); + errno = EFAULT; + return -1; + } + + switch (src_fmt->fmt.pix.pixelformat) { + 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, + (src_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)? + TINYJPEG_FLAGS_MJPEG_TABLE : 0); + 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 != dest_fmt->fmt.pix.width || header_height != dest_fmt->fmt.pix.height) { + V4LCONVERT_ERR("unexpected width / height in JPEG header\n"); + V4LCONVERT_ERR("expected: %dx%d, header: %ux%u\n", + dest_fmt->fmt.pix.width, dest_fmt->fmt.pix.height, + header_width, header_height); + errno = EIO; + return -1; + } + + components[0] = dest; + components[1] = components[0] + dest_fmt->fmt.pix.width * + dest_fmt->fmt.pix.height; + components[2] = components[1] + (dest_fmt->fmt.pix.width * + dest_fmt->fmt.pix.height) / 4; + + if (dest_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24) { + tinyjpeg_set_components(data->jdec, components, 1); + result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_BGR24); + } else { + tinyjpeg_set_components(data->jdec, components, 3); + result = tinyjpeg_decode(data->jdec, TINYJPEG_FMT_YUV420P); + } + + if (result) { + V4LCONVERT_ERR("decompressing JPEG: %s\n", + tinyjpeg_get_errorstring(data->jdec)); + errno = EIO; + return -1; + } + break; + + case V4L2_PIX_FMT_SBGGR8: + case V4L2_PIX_FMT_SGBRG8: + case V4L2_PIX_FMT_SGRBG8: + case V4L2_PIX_FMT_SRGGB8: + if (dest_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24) + v4lconvert_bayer_to_bgr24(src, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height, src_fmt->fmt.pix.pixelformat); + else + v4lconvert_bayer_to_yuv420(src, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height, src_fmt->fmt.pix.pixelformat); + break; + + case V4L2_PIX_FMT_SPCA501: + if (dest_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24) + v4lconvert_spca501_to_bgr24(src, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + else + v4lconvert_spca501_to_yuv420(src, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + break; + + case V4L2_PIX_FMT_SPCA561: + { + static unsigned char tmpbuf[640 * 480]; + v4lconvert_decode_spca561(src, tmpbuf, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + if (dest_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24) + v4lconvert_bayer_to_bgr24(tmpbuf, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height, V4L2_PIX_FMT_SGBRG8); + else + v4lconvert_bayer_to_yuv420(tmpbuf, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height, V4L2_PIX_FMT_SGBRG8); + break; + } + case V4L2_PIX_FMT_BGR24: + /* dest must be V4L2_PIX_FMT_YUV420 then */ + printf("FIXME add bgr24 -> yuv420 conversion\n"); + break; + case V4L2_PIX_FMT_YUV420: + /* dest must be V4L2_PIX_FMT_BGR24 then */ + v4lconvert_yuv420_to_bgr24(src, dest, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + break; + } + + return needed; +} + +const char *v4lconvert_get_error_message(struct v4lconvert_data *data) +{ + return data->error_msg; +} diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/rgbyuv.c b/v4l2-apps/lib/libv4l/libv4lconvert/rgbyuv.c new file mode 100644 index 000000000..79c8ecb35 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/rgbyuv.c @@ -0,0 +1,82 @@ +/* + +# RGB <-> YUV conversion routines + +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#define RGB2YUV(r,g,b,y,u,v) \ + (y) = (( 8453*(r) + 16594*(g) + 3223*(b) + 524288) >> 15); \ + (u) = (( -4878*(r) - 9578*(g) + 14456*(b) + 4210688) >> 15); \ + (v) = (( 14456*(r) - 12105*(g) - 2351*(b) + 4210688) >> 15) + +#define YUV2R(y, u, v) ({ \ + int r = (y) + ((((v)-128)*1436) >> 10); r > 255 ? 255 : r < 0 ? 0 : r; }) +#define YUV2G(y, u, v) ({ \ + int g = (y) - ((((u)-128)*352 + ((v)-128)*731) >> 10); g > 255 ? 255 : g < 0 ? 0 : g; }) +#define YUV2B(y, u, v) ({ \ + int b = (y) + ((((u)-128)*1814) >> 10); b > 255 ? 255 : b < 0 ? 0 : b; }) + +#define CLIP(color) (unsigned char)(((color)>0xFF)?0xff:(((color)<0)?0:(color))) + +void v4lconvert_yuv420_to_bgr24(const unsigned char *src, unsigned char *dest, + int width, int height) +{ + int i,j; + + const unsigned char *ysrc = src; + const unsigned char *usrc = src + width * height; + const unsigned char *vsrc = usrc + (width * height) / 4; + + for (i = 0; i < height; i++) { + for (j = 0; j < width; j += 2) { +#if 1 /* fast slightly less accurate multiplication free code */ + int u1 = (((*usrc - 128) << 7) + (*usrc - 128)) >> 6; + int rg = (((*usrc - 128) << 1) + (*usrc - 128) + + ((*vsrc - 128) << 2) + ((*vsrc - 128) << 1)) >> 3; + int v1 = (((*vsrc - 128) << 1) + (*vsrc - 128)) >> 1; + + *dest++ = CLIP(*ysrc + u1); + *dest++ = CLIP(*ysrc - rg); + *dest++ = CLIP(*ysrc + v1); + ysrc++; + + *dest++ = CLIP(*ysrc + u1); + *dest++ = CLIP(*ysrc - rg); + *dest++ = CLIP(*ysrc + v1); +#else + *dest++ = YUV2B(*ysrc, *usrc, *vsrc); + *dest++ = YUV2G(*ysrc, *usrc, *vsrc); + *dest++ = YUV2R(*ysrc, *usrc, *vsrc); + ysrc++; + + *dest++ = YUV2B(*ysrc, *usrc, *vsrc); + *dest++ = YUV2G(*ysrc, *usrc, *vsrc); + *dest++ = YUV2R(*ysrc, *usrc, *vsrc); +#endif + ysrc++; + usrc++; + vsrc++; + } + /* Rewind u and v for next line */ + if (i&1) { + usrc -= width / 2; + vsrc -= width / 2; + } + } +} diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/spca501.c b/v4l2-apps/lib/libv4l/libv4lconvert/spca501.c new file mode 100644 index 000000000..b0170e7cb --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/spca501.c @@ -0,0 +1,67 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "libv4lconvert-priv.h" + +void v4lconvert_spca501_to_yuv420(const unsigned char *src, unsigned char *dst, + int width, int height) +{ + int i,j; + + for (i = 0; i < height; i += 2) { + unsigned long *lsrc = (unsigned long *)(src + (i / 2) * 3 * width); + + /* -128 - 127 --> 0 - 255 and copy first line Y */ + unsigned long *ldst = (unsigned long *)(dst + i * width); + for (j = 0; j < width; j += sizeof(long)) { + *ldst = *lsrc++; + *ldst++ ^= 0x8080808080808080ULL; + } + + /* -128 - 127 --> 0 - 255 and copy 1 line U */ + ldst = (unsigned long *)(dst + width * height + i * width / 4); + for (j = 0; j < width/2; j += sizeof(long)) { + *ldst = *lsrc++; + *ldst++ ^= 0x8080808080808080ULL; + } + + /* -128 - 127 --> 0 - 255 and copy second line Y */ + ldst = (unsigned long *)(dst + i * width + width); + for (j = 0; j < width; j += sizeof(long)) { + *ldst = *lsrc++; + *ldst++ ^= 0x8080808080808080ULL; + } + + /* -128 - 127 --> 0 - 255 and copy 1 line V */ + ldst = (unsigned long *)(dst + (width * height * 5) / 4 + i * width / 4); + for (j = 0; j < width/2; j += sizeof(long)) { + *ldst = *lsrc++; + *ldst++ ^= 0x8080808080808080ULL; + } + } +} + +/* IMPROVEME (maybe?) make this convert in one go?? */ +void v4lconvert_spca501_to_bgr24(const unsigned char *src, unsigned char *dst, + int width, int height) +{ + unsigned char buf[(width * height * 6) / 4]; + + v4lconvert_spca501_to_yuv420(src, buf, width, height); + v4lconvert_yuv420_to_bgr24(buf, dst, width, height); +} diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c b/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c new file mode 100644 index 000000000..1f7c6dd2f --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c @@ -0,0 +1,1002 @@ +/* + +# Spca561decoder (C) 2005 Andrzej Szombierski [qq@kuku.eu.org] + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Note this code was originally licensed under the GNU GPL instead of the +# GNU LGPL, its license has been changed with permission, see the permission +# mail at the end of this file. + +*/ + +/* + * Decoder for compressed spca561 images + * It was developed for "Labtec WebCam Elch 2(SPCA561A)" (046d:0929) + * but it might work with other spca561 cameras + */ +#include + +/*fixme: not reentrant */ +static unsigned int bit_bucket; +static const unsigned char *input_ptr; + +static inline void refill(int *bitfill) +{ + if (*bitfill < 8) { + bit_bucket = (bit_bucket << 8) | *(input_ptr++); + *bitfill += 8; + } +} + +static inline int nbits(int *bitfill, int n) +{ + bit_bucket = (bit_bucket << 8) | *(input_ptr++); + *bitfill -= n; + return (bit_bucket >> (*bitfill & 0xff)) & ((1 << n) - 1); +} + +static inline int _nbits(int *bitfill, int n) +{ + *bitfill -= n; + return (bit_bucket >> (*bitfill & 0xff)) & ((1 << n) - 1); +} + +static int fun_A(int *bitfill) +{ + int ret; + static int tab[] = { + 12, 13, 14, 15, 16, 17, 18, 19, -12, -13, -14, -15, + -16, -17, -18, -19, -19 + }; + + ret = tab[nbits(bitfill, 4)]; + + refill(bitfill); + return ret; +} +static int fun_B(int *bitfill) +{ + static int tab1[] = + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31, 31, + 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, + 16, 17, + 18, + 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 + }; + static int tab[] = + { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -5, + -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, + -18, -19 + }; + unsigned int tmp; + + tmp = nbits(bitfill, 7) - 68; + refill(bitfill); + if (tmp > 47) + return 0xff; + return tab[tab1[tmp]]; +} +static int fun_C(int *bitfill, int gkw) +{ + static int tab1[] = + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 12, 13, + 14, + 15, 16, 17, 18, 19, 20, 21, 22 + }; + static int tab[] = + { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -9, -10, -11, + -12, -13, -14, -15, -16, -17, -18, -19 + }; + unsigned int tmp; + + if (gkw == 0xfe) { + if (nbits(bitfill, 1) == 0) + return 7; + else + return -8; + } + + if (gkw != 0xff) + return 0xff; + + tmp = nbits(bitfill, 7) - 72; + if (tmp > 43) + return 0xff; + + refill(bitfill); + return tab[tab1[tmp]]; +} +static int fun_D(int *bitfill, int gkw) +{ + if (gkw == 0xfd) { + if (nbits(bitfill, 1) == 0) + return 12; + return -13; + } + + if (gkw == 0xfc) { + if (nbits(bitfill, 1) == 0) + return 13; + return -14; + } + + if (gkw == 0xfe) { + switch (nbits(bitfill, 2)) { + case 0: + return 14; + case 1: + return -15; + case 2: + return 15; + case 3: + return -16; + } + } + + if (gkw == 0xff) { + switch (nbits(bitfill, 3)) { + case 4: + return 16; + case 5: + return -17; + case 6: + return 17; + case 7: + return -18; + case 2: + return _nbits(bitfill, 1) ? 0xed : 0x12; + case 3: + (*bitfill)--; + return 18; + } + return 0xff; + } + return gkw; +} + +static int fun_E(int cur_byte, int *bitfill) +{ + static int tab0[] = { 0, -1, 1, -2, 2, -3, 3, -4 }; + static int tab1[] = { 4, -5, 5, -6, 6, -7, 7, -8 }; + static int tab2[] = { 8, -9, 9, -10, 10, -11, 11, -12 }; + static int tab3[] = { 12, -13, 13, -14, 14, -15, 15, -16 }; + static int tab4[] = { 16, -17, 17, -18, 18, -19, 19, -19 }; + + if ((cur_byte & 0xf0) >= 0x80) { + *bitfill -= 4; + return tab0[(cur_byte >> 4) & 7]; + } + if ((cur_byte & 0xc0) == 0x40) { + *bitfill -= 5; + return tab1[(cur_byte >> 3) & 7]; + + } + if ((cur_byte & 0xe0) == 0x20) { + *bitfill -= 6; + return tab2[(cur_byte >> 2) & 7]; + + } + if ((cur_byte & 0xf0) == 0x10) { + *bitfill -= 7; + return tab3[(cur_byte >> 1) & 7]; + + } + if ((cur_byte & 0xf8) == 8) { + *bitfill -= 8; + return tab4[cur_byte & 7]; + } + return 0xff; +} + +static int fun_F(int cur_byte, int *bitfill) +{ + *bitfill -= 5; + switch (cur_byte & 0xf8) { + case 0x80: + return 0; + case 0x88: + return -1; + case 0x90: + return 1; + case 0x98: + return -2; + case 0xa0: + return 2; + case 0xa8: + return -3; + case 0xb0: + return 3; + case 0xb8: + return -4; + case 0xc0: + return 4; + case 0xc8: + return -5; + case 0xd0: + return 5; + case 0xd8: + return -6; + case 0xe0: + return 6; + case 0xe8: + return -7; + case 0xf0: + return 7; + case 0xf8: + return -8; + } + + *bitfill -= 1; + switch (cur_byte & 0xfc) { + case 0x40: + return 8; + case 0x44: + return -9; + case 0x48: + return 9; + case 0x4c: + return -10; + case 0x50: + return 10; + case 0x54: + return -11; + case 0x58: + return 11; + case 0x5c: + return -12; + case 0x60: + return 12; + case 0x64: + return -13; + case 0x68: + return 13; + case 0x6c: + return -14; + case 0x70: + return 14; + case 0x74: + return -15; + case 0x78: + return 15; + case 0x7c: + return -16; + } + + *bitfill -= 1; + switch (cur_byte & 0xfe) { + case 0x20: + return 16; + case 0x22: + return -17; + case 0x24: + return 17; + case 0x26: + return -18; + case 0x28: + return 18; + case 0x2a: + return -19; + case 0x2c: + return 19; + } + + *bitfill += 7; + return 0xff; +} + +static int internal_spca561_decode(int width, int height, + const unsigned char *inbuf, + unsigned char *outbuf) +{ + /* buffers */ + static int accum[8 * 8 * 8]; + static int i_hits[8 * 8 * 8]; + + const static int nbits_A[] = + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, + 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + }; + const static int tab_A[] = + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 11, -11, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, + 10, 10, + 255, 254, -4, + -4, -5, -5, -6, -6, -7, -7, -8, -8, -9, -9, -10, -10, -1, + -1, -1, + -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, + 3, 3, 3, + 3, 3, 3, + -2, -2, -2, -2, -2, -2, -2, -2, -3, -3, -3, -3, -3, -3, -3, + -3, 1, + 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, + 1 + }; + + const static int nbits_B[] = + { 0, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + }; + const static int tab_B[] = + { 0xff, -4, 3, 3, -3, -3, -3, -3, 2, 2, 2, 2, 2, 2, 2, 2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + 1, 1, + 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, + }; + + const static int nbits_C[] = + { 0, 0, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, + 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + }; + const static int tab_C[] = + { 0xff, 0xfe, 6, -7, 5, 5, -6, -6, 4, 4, 4, 4, -5, -5, -5, -5, + 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4, -4, 2, + 2, 2, 2, + 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, -3, -3, -3, -3, -3, -3, -3, -3, + -3, -3, + -3, -3, -3, + -3, -3, -3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, -2, -2, -2, -2, -2, -2, + -2, -2, + -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, + -2, -2, + -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, + }; + + const static int nbits_D[] = + { 0, 0, 0, 0, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, + 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, + 4, 4, + 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, + 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, + 4, 4, 4, 4, 4, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, + 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 + }; + const static int tab_D[] = + { 0xff, 0xfe, 0xfd, 0xfc, 10, -11, 11, -12, 8, 8, -9, -9, 9, 9, + -10, -10, 6, 6, 6, 6, -7, -7, -7, -7, 7, 7, 7, 7, -8, -8, + -8, -8, + 4, 4, 4, 4, + 4, 4, 4, 4, -5, -5, -5, -5, -5, -5, -5, -5, 5, 5, 5, 5, 5, + 5, 5, 5, + -6, -6, + -6, -6, -6, -6, -6, -6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, + 2, 2, -3, + -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, + 3, 3, + 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4, + -4, -4, + -4, -4, -4, + -4, -4, -4, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, + -1, -1, + -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, + -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, + -2, -2 + }; + + /* a_curve[19 + i] = ... [-19..19] => [-160..160] */ + const static int a_curve[] = + { -160, -144, -128, -112, -98, -88, -80, -72, -64, -56, -48, + -40, -32, -24, -18, -12, -8, -5, -2, 0, 2, 5, 8, 12, 18, + 24, 32, + 40, 48, 56, 64, + 72, 80, 88, 98, 112, 128, 144, 160 + }; + /* clamp0_255[256 + i] = min(max(i,255),0) */ + const static unsigned char clamp0_255[] = + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 2, + 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, + 28, 29, 30, 31, 32, 33, + 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, + 51, 52, 53, 54, 55, 56, + 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, + 72, 73, + 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, + 95, 96, + 97, 98, 99, 100, 101, + 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, + 114, + 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, + 132, + 133, 134, 135, 136, 137, + 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, + 150, + 151, 152, 153, 154, 155, + 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, + 168, + 169, 170, 171, 172, 173, + 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, + 186, + 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, + 204, + 205, 206, 207, 208, 209, + 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, + 222, + 223, 224, 225, 226, 227, + 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, + 241, 242, 243, 244, 245, + 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, + 255 + }; + /* abs_clamp15[19 + i] = min(abs(i), 15) */ + const static int abs_clamp15[] = + { 15, 15, 15, 15, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, + 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 15, 15, + 15 + }; + /* diff_encoding[256 + i] = ... */ + const static int diff_encoding[] = + { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, + 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5, + 5, 5, + 5, 5, 5, 5, 5, 3, 3, + 3, 3, 1, 1, 0, 2, 2, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, + 6, 6, 6, 6, 6, 6 + }; + + int block; + int bitfill = 0; + int xwidth = width + 6; + int off_up_right = 2 - 2 * xwidth; + int off_up_left = -2 - 2 * xwidth; + int pixel_U = 0, saved_pixel_UR = 0; + int pixel_x = 0, pixel_y = 2; + unsigned char *output_ptr = outbuf; + + memset(i_hits, 0, sizeof(i_hits)); + memset(accum, 0, sizeof(accum)); + + memcpy(outbuf + xwidth * 2 + 3, inbuf + 0x14, width); + memcpy(outbuf + xwidth * 3 + 3, inbuf + 0x14 + width, width); + + input_ptr = inbuf + 0x14 + width * 2; + output_ptr = outbuf + (xwidth) * 4 + 3; + + bit_bucket = 0; + + for (block = 0; block < ((height - 2) * width) / 32; ++block) { + int b_it, var_7 = 0; + int cur_byte; + + refill(&bitfill); + + cur_byte = (bit_bucket >> (bitfill & 7)) & 0xff; + + if ((cur_byte & 0x80) == 0) { + var_7 = 0; + bitfill--; + } else if ((cur_byte & 0xC0) == 0x80) { + var_7 = 1; + bitfill -= 2; + } else if ((cur_byte & 0xc0) == 0xc0) { + var_7 = 2; + bitfill -= 2; + } + + for (b_it = 0; b_it < 32; b_it++) { + int index; + int pixel_L, pixel_UR, pixel_UL; + int multiplier; + int dL, dC, dR; + int gkw; /* God knows what */ + + refill(&bitfill); + cur_byte = bit_bucket >> (bitfill & 7) & 0xff; + + pixel_L = output_ptr[-2]; + pixel_UR = output_ptr[off_up_right]; + pixel_UL = output_ptr[off_up_left]; + + dL = diff_encoding[0x100 + pixel_UL - pixel_L]; + dC = diff_encoding[0x100 + pixel_U - pixel_UL]; + dR = diff_encoding[0x100 + pixel_UR - pixel_U]; + + if (pixel_x < 2) { + pixel_L = pixel_UL = pixel_U = + output_ptr[-xwidth * 2]; + pixel_UR = output_ptr[off_up_right]; + dL = dC = 0; + dR = diff_encoding[0x100 + pixel_UR - + pixel_U]; + } else if (pixel_x > width - 3) + dR = 0; + + multiplier = 4; + index = dR + dC * 8 + dL * 64; + + if (pixel_L + pixel_U * 2 <= 144 + && (pixel_y & 1) == 0 + && (b_it & 3) == 0 && (dR < 5) && (dC < 5) + && (dL < 5)) { + multiplier = 1; + } else if (pixel_L <= 48 + && dL <= 4 && dC <= 4 && dL >= 1 + && dC >= 1) { + multiplier = 2; + } else if (var_7 == 1) { + multiplier = 2; + } else if (dC + dL >= 11 || var_7 == 2) { + multiplier = 8; + } + + if (i_hits[index] < 7) { + bitfill -= nbits_A[cur_byte]; + gkw = tab_A[cur_byte]; + if (gkw == 0xfe) + gkw = fun_A(&bitfill); + } else if (i_hits[index] >= accum[index]) { + bitfill -= nbits_B[cur_byte]; + gkw = tab_B[cur_byte]; + if (cur_byte == 0) + gkw = fun_B(&bitfill); + } else if (i_hits[index] * 2 >= accum[index]) { + bitfill -= nbits_C[cur_byte]; + gkw = tab_C[cur_byte]; + if (cur_byte < 2) + gkw = fun_C(&bitfill, gkw); + } else if (i_hits[index] * 4 >= accum[index]) { + bitfill -= nbits_D[cur_byte]; + gkw = tab_D[cur_byte]; + if (cur_byte < 4) + gkw = fun_D(&bitfill, gkw); + } else if (i_hits[index] * 8 >= accum[index]) { + gkw = fun_E(cur_byte, &bitfill); + } else { + gkw = fun_F(cur_byte, &bitfill); + } + + if (gkw == 0xff) + return -3; + + { + int tmp1, tmp2; + + tmp1 = + (pixel_U + pixel_L) * 3 - pixel_UL * 2; + tmp1 += (tmp1 < 0) ? 3 : 0; + tmp2 = a_curve[19 + gkw] * multiplier; + tmp2 += (tmp2 < 0) ? 1 : 0; + + *(output_ptr++) = + clamp0_255[0x100 + (tmp1 >> 2) - + (tmp2 >> 1)]; + } + pixel_U = saved_pixel_UR; + saved_pixel_UR = pixel_UR; + + if (++pixel_x == width) { + output_ptr += 6; + pixel_x = 0; + pixel_y++; + } + + accum[index] += abs_clamp15[19 + gkw]; + + if (i_hits[index]++ == 15) { + i_hits[index] = 8; + accum[index] /= 2; + } + } + } + return 0; +} + +/* FIXME, change internal_spca561_decode not to need the extra border + around its dest buffer */ +void v4lconvert_decode_spca561(const unsigned char *inbuf, + unsigned char *outbuf, int width, int height) +{ + int i; + static unsigned char tmpbuf[650 * 490]; + if (internal_spca561_decode(width, height, inbuf, tmpbuf) != 0) + return; + for (i = 0; i < height; i++) + memcpy(outbuf + i * width, + tmpbuf + (i + 2) * (width + 6) + 3, width); +} + +/*************** License Change Permission Notice *************** + +Return-Path: +Received: from koko.hhs.nl ([145.52.2.16] verified) + by hhs.nl (CommuniGate Pro SMTP 4.3.6) + with ESMTP id 88574071 for j.w.r.degoede@hhs.nl; Mon, 16 Jun 2008 16:36:24 +0200 +Received: from exim (helo=koko) + by koko.hhs.nl with local-smtp (Exim 4.62) + (envelope-from ) + id 1K8Fom-0002iJ-3K + for j.w.r.degoede@hhs.nl; Mon, 16 Jun 2008 16:36:24 +0200 +Received: from [192.87.102.74] (port=41377 helo=filter6-ams.mf.surf.net) + by koko.hhs.nl with esmtp (Exim 4.62) + (envelope-from ) + id 1K8Fol-0002iC-Qo + for j.w.r.degoede@hhs.nl; Mon, 16 Jun 2008 16:36:23 +0200 +Received: from kuku.eu.org (pa90.wielkopole.sdi.tpnet.pl [217.99.123.90]) + by filter6-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5GEa55r001787 + for ; Mon, 16 Jun 2008 16:36:06 +0200 +Received: (qmail 2243 invoked by uid 500); 16 Jun 2008 14:29:37 -0000 +Date: Mon, 16 Jun 2008 16:29:37 +0200 (CEST) +From: Andrzej Szombierski +To: Hans de Goede +Subject: Re: spca561 decoder license question +In-Reply-To: <485673B6.4050003@hhs.nl> +Message-ID: +References: <485673B6.4050003@hhs.nl> +MIME-Version: 1.0 +Content-Type: TEXT/PLAIN; charset=iso-8859-2 +Content-Transfer-Encoding: QUOTED-PRINTABLE +X-Canit-CHI2: 0.00 +X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) +X-Spam-Score: 2.00 (**) [Tag at 6.00] RBL(uceprotect-blacklist.surfnet.nl,2.0) +X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default) +X-Canit-Stats-ID: 85673281 - 37e52c8b07bc +X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.74 +X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 16062008 #776409, status: clean + +On Mon, 16 Jun 2008, Hans de Goede wrote: + +> Hi, +>=20 +> I don't know if you're still subscribed to the spca devel mailing list, s= +o let=20 +> me start with a short intro. +> +> I'm a Linux enthusiast / developer currently helping Jean-Fran=E7ois Moin= +e with=20 +> porting gspca to video4linux2 and cleaning up the code to get it ready fo= +r=20 +> mainline kernel inclusion. +>=20 +> As part of this process the decompression code for all supported cams mus= +t be=20 +> moved to userspace, as doing in kernel decompression is considered unwant= +ed by=20 +> the mainline people (I agree) as it should be done in userspace. +> + +Sounds reasonable. +=20 +> As such I'm working on a library which does decompression of custom cam f= +ormats=20 +> in userspace. +> + +Nice. I hope that the library won't be limited to spca-supported webcams,= +=20 +and as an application developer I would be able to just request RGB data=20 +from any /dev/video*, right? + +> I do not want to license this library as GPL (as the current spca code is= +), as=20 +> it should be usable by as much software as possible. Instead I want to li= +cense=20 +> it under the LGPL version 2.1 or later. + +Also sounds reasonable. + +>=20 +> So my question us my I have your permission to relicense your spca561=20 +> decompression code under the LGPL? +>=20 + +Yes, of course.=20 + +> Thanks & Regards, +>=20 +> Hans +>=20 +> + +--=20 +:: Andrzej Szombierski :: qq@kuku.eu.org :: http://kuku.eu.org :: +:: anszom@bezkitu.com :: radio bez kitu :: http://bezkitu.com :: + +*/ diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg-internal.h b/v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg-internal.h new file mode 100644 index 000000000..26844c28a --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg-internal.h @@ -0,0 +1,121 @@ +/* + * Small jpeg decoder library (Internal header) + * + * Copyright (c) 2006, Luc Saillard + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + + +#ifndef __TINYJPEG_INTERNAL_H_ +#define __TINYJPEG_INTERNAL_H_ + +#include + +#define SANITY_CHECK 1 + +struct jdec_private; + +#define HUFFMAN_HASH_NBITS 9 +#define HUFFMAN_HASH_SIZE (1UL< + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include + +#include "tinyjpeg.h" +#include "tinyjpeg-internal.h" + +enum std_markers { + DQT = 0xDB, /* Define Quantization Table */ + SOF = 0xC0, /* Start of Frame (size information) */ + DHT = 0xC4, /* Huffman Table */ + SOI = 0xD8, /* Start of Image */ + SOS = 0xDA, /* Start of Scan */ + RST = 0xD0, /* Reset Marker d0 -> .. */ + RST7 = 0xD7, /* Reset Marker .. -> d7 */ + EOI = 0xD9, /* End of Image */ + DRI = 0xDD, /* Define Restart Interval */ + APP0 = 0xE0, +}; + +#define cY 0 +#define cCb 1 +#define cCr 2 + +#define BLACK_Y 0 +#define BLACK_U 127 +#define BLACK_V 127 + +#if DEBUG +#if LOG2FILE + +#define trace(fmt, args...) do { \ + FILE *f = fopen("/tmp/jpeg.log", "a"); \ + fprintf(f, fmt, ## args); \ + fflush(f); \ + fclose(f); \ +} while(0) + +#else + +#define trace(fmt, args...) do { \ + fprintf(stderr, fmt, ## args); \ + fflush(stderr); \ +} while(0) +#endif + +#else +#define trace(fmt, args...) do { } while (0) +#endif + +#define error(fmt, args...) do { \ + snprintf(priv->error_string, sizeof(priv->error_string), fmt, ## args); \ + return -1; \ +} while(0) + + +#if 0 +static char *print_bits(unsigned int value, char *bitstr) +{ + int i, j; + i=31; + while (i>0) + { + if (value & (1UL<=0) + { + bitstr[j++] = (value & (1UL< + * Ex: 0000 0000 1010 0000 1111 0000 <== reservoir + * ^ + * bit 1 + * To get two bits from this example + * result = (reservoir >> 15) & 3 + * + */ +#define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ + while (nbits_in_reservoir= priv->stream_end) \ + longjmp(priv->jump_state, -EIO); \ + c = *stream++; \ + reservoir <<= 8; \ + if (c == 0xff && *stream == 0x00) \ + stream++; \ + reservoir |= c; \ + nbits_in_reservoir+=8; \ + } \ +} while(0); + +/* Signed version !!!! */ +#define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \ + fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ + result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \ + nbits_in_reservoir -= (nbits_wanted); \ + reservoir &= ((1U<>(nbits_in_reservoir-(nbits_wanted))); \ +} while(0); + +/* To speed up the decoding, we assume that the reservoir have enough bit + * slow version: + * #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ + * fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ + * nbits_in_reservoir -= (nbits_wanted); \ + * reservoir &= ((1U<reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode); + value = huffman_table->lookup[hcode]; + if (value >= 0) + { + unsigned int code_size = huffman_table->code_size[value]; + skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size); + return value; + } + + /* Decode more bits each time ... */ + for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++) + { + nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits; + + look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode); + slowtable = huffman_table->slowtable[extra_nbits]; + /* Search if the code is in this array */ + while (slowtable[0]) { + if (slowtable[0] == hcode) { + skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits); + return slowtable[1]; + } + slowtable+=2; + } + } + return 0; +} + + + + +/** + * + * Decode a single block that contains the DCT coefficients. + * The table coefficients is already dezigzaged at the end of the operation. + * + */ +static void process_Huffman_data_unit(struct jdec_private *priv, int component) +{ + unsigned char j; + unsigned int huff_code; + unsigned char size_val, count_0; + + struct component *c = &priv->component_infos[component]; + short int DCT[64]; + + /* Initialize the DCT coef table */ + memset(DCT, 0, sizeof(DCT)); + + /* DC coefficient decoding */ + huff_code = get_next_huffman_code(priv, c->DC_table); + if (huff_code) { + get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]); + DCT[0] += c->previous_DC; + c->previous_DC = DCT[0]; + } else { + DCT[0] = c->previous_DC; + } + + + /* AC coefficient decoding */ + j = 1; + while (j<64) + { + huff_code = get_next_huffman_code(priv, c->AC_table); + + size_val = huff_code & 0xF; + count_0 = huff_code >> 4; + + if (size_val == 0) + { /* RLE */ + if (count_0 == 0) + break; /* EOB found, go out */ + else if (count_0 == 0xF) + j += 16; /* skip 16 zeros */ + } + else + { + j += count_0; /* skip count_0 zeroes */ + get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]); + j++; + } + } + + for (j = 0; j < 64; j++) + c->DCT[j] = DCT[zigzag[j]]; +} + +/* + * Takes two array of bits, and build the huffman table for size, and code + * + * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS. + * code_size will be used to known how many bits this symbol is encoded. + * slowtable will be used when the first lookup didn't give the result. + */ +static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table) +{ + unsigned int i, j, code, code_size, val, nbits; + unsigned char huffsize[257], *hz; + unsigned int huffcode[257], *hc; + int next_free_entry; + + /* + * Build a temp array + * huffsize[X] => numbers of bits to write vals[X] + */ + hz = huffsize; + for (i=1; i<=16; i++) + { + for (j=1; j<=bits[i]; j++) + *hz++ = i; + } + *hz = 0; + + memset(table->lookup, 0xff, sizeof(table->lookup)); + for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++) + table->slowtable[i][0] = 0; + + /* Build a temp array + * huffcode[X] => code used to write vals[X] + */ + code = 0; + hc = huffcode; + hz = huffsize; + nbits = *hz; + while (*hz) + { + while (*hz == nbits) { + *hc++ = code++; + hz++; + } + code <<= 1; + nbits++; + } + + /* + * Build the lookup table, and the slowtable if needed. + */ + next_free_entry = -1; + for (i=0; huffsize[i]; i++) + { + val = vals[i]; + code = huffcode[i]; + code_size = huffsize[i]; + + trace("val=%2.2x code=%8.8x codesize=%2.2d\n", i, code, code_size); + + table->code_size[val] = code_size; + if (code_size <= HUFFMAN_HASH_NBITS) + { + /* + * Good: val can be put in the lookup table, so fill all value of this + * column with value val + */ + int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size); + code <<= HUFFMAN_HASH_NBITS - code_size; + while ( repeat-- ) + table->lookup[code++] = val; + + } + else + { + /* Perhaps sorting the array will be an optimization */ + uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1]; + while(slowtable[0]) + slowtable+=2; + slowtable[0] = code; + slowtable[1] = val; + slowtable[2] = 0; + /* TODO: NEED TO CHECK FOR AN OVERFLOW OF THE TABLE */ + } + + } + +} + +static void build_default_huffman_tables(struct jdec_private *priv) +{ + if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE) + && priv->default_huffman_table_initialized) + return; + + build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]); + build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]); + + build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]); + build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]); + + priv->default_huffman_table_initialized = 1; +} + + + +/******************************************************************************* + * + * Colorspace conversion routine + * + * + * Note: + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. + * The conversion equations to be implemented are therefore + * R = Y + 1.40200 * Cr + * G = Y - 0.34414 * Cb - 0.71414 * Cr + * B = Y + 1.77200 * Cb + * + ******************************************************************************/ + +static unsigned char clamp(int i) +{ + if (i<0) + return 0; + else if (i>255) + return 255; + else + return i; +} + + +/** + * YCrCb -> YUV420P (1x1) + * .---. + * | 1 | + * `---' + */ +static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv) +{ + const unsigned char *s, *y; + unsigned char *p; + int i,j; + + p = priv->plane[0]; + y = priv->Y; + for (i=0; i<8; i++) + { + memcpy(p, y, 8); + p+=priv->width; + y+=8; + } + + p = priv->plane[1]; + s = priv->Cb; + for (i=0; i<8; i+=2) + { + for (j=0; j<8; j+=2, s+=2) + *p++ = *s; + s += 8; /* Skip one line */ + p += priv->width/2 - 4; + } + + p = priv->plane[2]; + s = priv->Cr; + for (i=0; i<8; i+=2) + { + for (j=0; j<8; j+=2, s+=2) + *p++ = *s; + s += 8; /* Skip one line */ + p += priv->width/2 - 4; + } +} + +/** + * YCrCb -> YUV420P (2x1) + * .-------. + * | 1 | 2 | + * `-------' + */ +static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv) +{ + unsigned char *p; + const unsigned char *s, *y1; + unsigned int i; + + p = priv->plane[0]; + y1 = priv->Y; + for (i=0; i<8; i++) + { + memcpy(p, y1, 16); + p += priv->width; + y1 += 16; + } + + p = priv->plane[1]; + s = priv->Cb; + for (i=0; i<8; i+=2) + { + memcpy(p, s, 8); + s += 16; /* Skip one line */ + p += priv->width/2; + } + + p = priv->plane[2]; + s = priv->Cr; + for (i=0; i<8; i+=2) + { + memcpy(p, s, 8); + s += 16; /* Skip one line */ + p += priv->width/2; + } +} + + +/** + * YCrCb -> YUV420P (1x2) + * .---. + * | 1 | + * |---| + * | 2 | + * `---' + */ +static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv) +{ + const unsigned char *s, *y; + unsigned char *p; + int i,j; + + p = priv->plane[0]; + y = priv->Y; + for (i=0; i<16; i++) + { + memcpy(p, y, 8); + p+=priv->width; + y+=8; + } + + p = priv->plane[1]; + s = priv->Cb; + for (i=0; i<8; i++) + { + for (j=0; j<8; j+=2, s+=2) + *p++ = *s; + p += priv->width/2 - 4; + } + + p = priv->plane[2]; + s = priv->Cr; + for (i=0; i<8; i++) + { + for (j=0; j<8; j+=2, s+=2) + *p++ = *s; + p += priv->width/2 - 4; + } +} + +/** + * YCrCb -> YUV420P (2x2) + * .-------. + * | 1 | 2 | + * |---+---| + * | 3 | 4 | + * `-------' + */ +static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv) +{ + unsigned char *p; + const unsigned char *s, *y1; + unsigned int i; + + p = priv->plane[0]; + y1 = priv->Y; + for (i=0; i<16; i++) + { + memcpy(p, y1, 16); + p += priv->width; + y1 += 16; + } + + p = priv->plane[1]; + s = priv->Cb; + for (i=0; i<8; i++) + { + memcpy(p, s, 8); + s += 8; + p += priv->width/2; + } + + p = priv->plane[2]; + s = priv->Cr; + for (i=0; i<8; i++) + { + memcpy(p, s, 8); + s += 8; + p += priv->width/2; + } +} + +/** + * YCrCb -> RGB24 (1x1) + * .---. + * | 1 | + * `---' + */ +static void YCrCB_to_RGB24_1x1(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = priv->width*3 - 8*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + y = (*Y++) << SCALEBITS; + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + + } + + p += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + +/** + * YCrCb -> BGR24 (1x1) + * .---. + * | 1 | + * `---' + */ +static void YCrCB_to_BGR24_1x1(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = priv->width*3 - 8*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + y = (*Y++) << SCALEBITS; + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + + } + + p += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + + +/** + * YCrCb -> RGB24 (2x1) + * .-------. + * | 1 | 2 | + * `-------' + */ +static void YCrCB_to_RGB24_2x1(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = priv->width*3 - 16*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + y = (*Y++) << SCALEBITS; + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + + y = (*Y++) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + + } + + p += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + +/* + * YCrCb -> BGR24 (2x1) + * .-------. + * | 1 | 2 | + * `-------' + */ +static void YCrCB_to_BGR24_2x1(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = priv->width*3 - 16*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + y = (*Y++) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + + y = (*Y++) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + + } + + p += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + +/** + * YCrCb -> RGB24 (1x2) + * .---. + * | 1 | + * |---| + * | 2 | + * `---' + */ +static void YCrCB_to_RGB24_1x2(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p, *p2; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + p2 = priv->plane[0] + priv->width*3; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = 2*priv->width*3 - 8*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + y = (*Y++) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + + y = (Y[8-1]) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p2++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p2++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p2++ = clamp(b); + + } + Y += 8; + p += offset_to_next_row; + p2 += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + +/* + * YCrCb -> BGR24 (1x2) + * .---. + * | 1 | + * |---| + * | 2 | + * `---' + */ +static void YCrCB_to_BGR24_1x2(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p, *p2; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + p2 = priv->plane[0] + priv->width*3; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = 2*priv->width*3 - 8*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + y = (*Y++) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + + y = (Y[8-1]) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p2++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p2++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p2++ = clamp(r); + + } + Y += 8; + p += offset_to_next_row; + p2 += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + + +/** + * YCrCb -> RGB24 (2x2) + * .-------. + * | 1 | 2 | + * |---+---| + * | 3 | 4 | + * `-------' + */ +static void YCrCB_to_RGB24_2x2(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p, *p2; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + p2 = priv->plane[0] + priv->width*3; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = (priv->width*3*2) - 16*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + y = (*Y++) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + + y = (*Y++) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + + y = (Y[16-2]) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p2++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p2++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p2++ = clamp(b); + + y = (Y[16-1]) << SCALEBITS; + r = (y + add_r) >> SCALEBITS; + *p2++ = clamp(r); + g = (y + add_g) >> SCALEBITS; + *p2++ = clamp(g); + b = (y + add_b) >> SCALEBITS; + *p2++ = clamp(b); + } + Y += 16; + p += offset_to_next_row; + p2 += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + + +/* + * YCrCb -> BGR24 (2x2) + * .-------. + * | 1 | 2 | + * |---+---| + * | 3 | 4 | + * `-------' + */ +static void YCrCB_to_BGR24_2x2(struct jdec_private *priv) +{ + const unsigned char *Y, *Cb, *Cr; + unsigned char *p, *p2; + int i,j; + int offset_to_next_row; + +#define SCALEBITS 10 +#define ONE_HALF (1UL << (SCALEBITS-1)) +#define FIX(x) ((int)((x) * (1UL<plane[0]; + p2 = priv->plane[0] + priv->width*3; + Y = priv->Y; + Cb = priv->Cb; + Cr = priv->Cr; + offset_to_next_row = (priv->width*3*2) - 16*3; + for (i=0; i<8; i++) { + + for (j=0; j<8; j++) { + + int y, cb, cr; + int add_r, add_g, add_b; + int r, g , b; + + cb = *Cb++ - 128; + cr = *Cr++ - 128; + add_r = FIX(1.40200) * cr + ONE_HALF; + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; + add_b = FIX(1.77200) * cb + ONE_HALF; + + y = (*Y++) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + + y = (*Y++) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p++ = clamp(r); + + y = (Y[16-2]) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p2++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p2++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p2++ = clamp(r); + + y = (Y[16-1]) << SCALEBITS; + b = (y + add_b) >> SCALEBITS; + *p2++ = clamp(b); + g = (y + add_g) >> SCALEBITS; + *p2++ = clamp(g); + r = (y + add_r) >> SCALEBITS; + *p2++ = clamp(r); + } + Y += 16; + p += offset_to_next_row; + p2 += offset_to_next_row; + } + +#undef SCALEBITS +#undef ONE_HALF +#undef FIX + +} + + + +/** + * YCrCb -> Grey (1x1) + * .---. + * | 1 | + * `---' + */ +static void YCrCB_to_Grey_1x1(struct jdec_private *priv) +{ + const unsigned char *y; + unsigned char *p; + unsigned int i; + int offset_to_next_row; + + p = priv->plane[0]; + y = priv->Y; + offset_to_next_row = priv->width; + + for (i=0; i<8; i++) { + memcpy(p, y, 8); + y+=8; + p += offset_to_next_row; + } +} + +/** + * YCrCb -> Grey (2x1) + * .-------. + * | 1 | 2 | + * `-------' + */ +static void YCrCB_to_Grey_2x1(struct jdec_private *priv) +{ + const unsigned char *y; + unsigned char *p; + unsigned int i; + + p = priv->plane[0]; + y = priv->Y; + + for (i=0; i<8; i++) { + memcpy(p, y, 16); + y += 16; + p += priv->width; + } +} + + +/** + * YCrCb -> Grey (1x2) + * .---. + * | 1 | + * |---| + * | 2 | + * `---' + */ +static void YCrCB_to_Grey_1x2(struct jdec_private *priv) +{ + const unsigned char *y; + unsigned char *p; + unsigned int i; + + p = priv->plane[0]; + y = priv->Y; + + for (i=0; i<16; i++) { + memcpy(p, y, 8); + y += 8; + p += priv->width; + } +} + +/** + * YCrCb -> Grey (2x2) + * .-------. + * | 1 | 2 | + * |---+---| + * | 3 | 4 | + * `-------' + */ +static void YCrCB_to_Grey_2x2(struct jdec_private *priv) +{ + const unsigned char *y; + unsigned char *p; + unsigned int i; + + p = priv->plane[0]; + y = priv->Y; + + for (i=0; i<16; i++) { + memcpy(p, y, 16); + y += 16; + p += priv->width; + } +} + + +/* + * Decode all the 3 components for 1x1 + */ +static void decode_MCU_1x1_3planes(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 8); + + // Cb + process_Huffman_data_unit(priv, cCb); + IDCT(&priv->component_infos[cCb], priv->Cb, 8); + + // Cr + process_Huffman_data_unit(priv, cCr); + IDCT(&priv->component_infos[cCr], priv->Cr, 8); +} + +/* + * Decode a 1x1 directly in 1 color + */ +static void decode_MCU_1x1_1plane(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 8); + + // Cb + process_Huffman_data_unit(priv, cCb); + IDCT(&priv->component_infos[cCb], priv->Cb, 8); + + // Cr + process_Huffman_data_unit(priv, cCr); + IDCT(&priv->component_infos[cCr], priv->Cr, 8); +} + + +/* + * Decode a 2x1 + * .-------. + * | 1 | 2 | + * `-------' + */ +static void decode_MCU_2x1_3planes(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+8, 16); + + // Cb + process_Huffman_data_unit(priv, cCb); + IDCT(&priv->component_infos[cCb], priv->Cb, 8); + + // Cr + process_Huffman_data_unit(priv, cCr); + IDCT(&priv->component_infos[cCr], priv->Cr, 8); +} + +/* + * Decode a 2x1 + * .-------. + * | 1 | 2 | + * `-------' + */ +static void decode_MCU_2x1_1plane(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+8, 16); + + // Cb + process_Huffman_data_unit(priv, cCb); + + // Cr + process_Huffman_data_unit(priv, cCr); +} + + +/* + * Decode a 2x2 + * .-------. + * | 1 | 2 | + * |---+---| + * | 3 | 4 | + * `-------' + */ +static void decode_MCU_2x2_3planes(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+8, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+64*2, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16); + + // Cb + process_Huffman_data_unit(priv, cCb); + IDCT(&priv->component_infos[cCb], priv->Cb, 8); + + // Cr + process_Huffman_data_unit(priv, cCr); + IDCT(&priv->component_infos[cCr], priv->Cr, 8); +} + +/* + * Decode a 2x2 directly in GREY format (8bits) + * .-------. + * | 1 | 2 | + * |---+---| + * | 3 | 4 | + * `-------' + */ +static void decode_MCU_2x2_1plane(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+8, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+64*2, 16); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16); + + // Cb + process_Huffman_data_unit(priv, cCb); + + // Cr + process_Huffman_data_unit(priv, cCr); +} + +/* + * Decode a 1x2 mcu + * .---. + * | 1 | + * |---| + * | 2 | + * `---' + */ +static void decode_MCU_1x2_3planes(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 8); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+64, 8); + + // Cb + process_Huffman_data_unit(priv, cCb); + IDCT(&priv->component_infos[cCb], priv->Cb, 8); + + // Cr + process_Huffman_data_unit(priv, cCr); + IDCT(&priv->component_infos[cCr], priv->Cr, 8); +} + +/* + * Decode a 1x2 mcu + * .---. + * | 1 | + * |---| + * | 2 | + * `---' + */ +static void decode_MCU_1x2_1plane(struct jdec_private *priv) +{ + // Y + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y, 8); + process_Huffman_data_unit(priv, cY); + IDCT(&priv->component_infos[cY], priv->Y+64, 8); + + // Cb + process_Huffman_data_unit(priv, cCb); + + // Cr + process_Huffman_data_unit(priv, cCr); +} + +static void print_SOF(const unsigned char *stream) +{ + int width, height, nr_components, precision; +#if DEBUG + const char *nr_components_to_string[] = { + "????", + "Grayscale", + "????", + "YCbCr", + "CYMK" + }; +#endif + + precision = stream[2]; + height = be16_to_cpu(stream+3); + width = be16_to_cpu(stream+5); + nr_components = stream[7]; + + trace("> SOF marker\n"); + trace("Size:%dx%d nr_components:%d (%s) precision:%d\n", + width, height, + nr_components, nr_components_to_string[nr_components], + precision); +} + +/******************************************************************************* + * + * JPEG/JFIF Parsing functions + * + * Note: only a small subset of the jpeg file format is supported. No markers, + * nor progressive stream is supported. + * + ******************************************************************************/ + +static void build_quantization_table(float *qtable, const unsigned char *ref_table) +{ + /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct. + * For float AA&N IDCT method, divisors are equal to quantization + * coefficients scaled by scalefactor[row]*scalefactor[col], where + * scalefactor[0] = 1 + * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 + * We apply a further scale factor of 8. + * What's actually stored is 1/divisor so that the inner loop can + * use a multiplication rather than a division. + */ + int i, j; + static const double aanscalefactor[8] = { + 1.0, 1.387039845, 1.306562965, 1.175875602, + 1.0, 0.785694958, 0.541196100, 0.275899379 + }; + const unsigned char *zz = zigzag; + + for (i=0; i<8; i++) { + for (j=0; j<8; j++) { + *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j]; + } + } + +} + +static int parse_DQT(struct jdec_private *priv, const unsigned char *stream) +{ + int qi; + float *table; + const unsigned char *dqt_block_end; + + trace("> DQT marker\n"); + dqt_block_end = stream + be16_to_cpu(stream); + stream += 2; /* Skip length */ + + while (stream < dqt_block_end) + { + qi = *stream++; +#if SANITY_CHECK + if (qi>>4) + error("16 bits quantization table is not supported\n"); + if (qi>4) + error("No more 4 quantization table is supported (got %d)\n", qi); +#endif + table = priv->Q_tables[qi]; + build_quantization_table(table, stream); + stream += 64; + } + trace("< DQT marker\n"); + return 0; +} + +static int parse_SOF(struct jdec_private *priv, const unsigned char *stream) +{ + int i, width, height, nr_components, cid, sampling_factor; + int Q_table; + struct component *c; + + trace("> SOF marker\n"); + print_SOF(stream); + + height = be16_to_cpu(stream+3); + width = be16_to_cpu(stream+5); + nr_components = stream[7]; +#if SANITY_CHECK + if (stream[2] != 8) + error("Precision other than 8 is not supported\n"); + if (width>JPEG_MAX_WIDTH || height>JPEG_MAX_HEIGHT) + error("Width and Height (%dx%d) seems suspicious\n", width, height); + if (nr_components != 3) + error("We only support YUV images\n"); + if (height%8) + error("Height need to be a multiple of 8 (current height is %d)\n", height); + if (width%16) + error("Width need to be a multiple of 16 (current Width is %d)\n", width); +#endif + stream += 8; + for (i=0; icomponent_infos[i]; +#if SANITY_CHECK + c->cid = cid; +#endif + c->Vfactor = sampling_factor&0xf; + c->Hfactor = sampling_factor>>4; + c->Q_table = priv->Q_tables[Q_table]; + trace("Component:%d factor:%dx%d Quantization table:%d\n", + cid, c->Hfactor, c->Hfactor, Q_table ); + + } + priv->width = width; + priv->height = height; + + trace("< SOF marker\n"); + + return 0; +} + +static int parse_SOS(struct jdec_private *priv, const unsigned char *stream) +{ + unsigned int i, cid, table; + unsigned int nr_components = stream[2]; + + trace("> SOS marker\n"); + +#if SANITY_CHECK + if (nr_components != 3) + error("We only support YCbCr image\n"); +#endif + + stream += 3; + for (i=0;i=4) + error("We do not support more than 2 AC Huffman table\n"); + if ((table>>4)>=4) + error("We do not support more than 2 DC Huffman table\n"); + if (cid != priv->component_infos[i].cid) + error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n", + i, cid, i, priv->component_infos[i].cid); + trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4); +#endif + priv->component_infos[i].AC_table = &priv->HTAC[table&0xf]; + priv->component_infos[i].DC_table = &priv->HTDC[table>>4]; + } + priv->stream = stream+3; + + /* ITU-T T.81 (9/92) chapter E.1.3 clearly states that RSTm is to be set to 0 at the beginning of each scan */ + priv->last_rst_marker_seen = 0; + + trace("< SOS marker\n"); + + return 0; +} + +static int parse_DHT(struct jdec_private *priv, const unsigned char *stream) +{ + unsigned int count, i; + unsigned char huff_bits[17]; + int length, index; + + length = be16_to_cpu(stream) - 2; + stream += 2; /* Skip length */ + + trace("> DHT marker (length=%d)\n", length); + + while (length>0) { + index = *stream++; + + /* We need to calculate the number of bytes 'vals' will takes */ + huff_bits[0] = 0; + count = 0; + for (i=1; i<17; i++) { + huff_bits[i] = *stream++; + count += huff_bits[i]; + } +#if SANITY_CHECK + if (count > 1024) + error("No more than 1024 bytes is allowed to describe a huffman table"); + if ( (index &0xf) >= HUFFMAN_TABLES) + error("No mode than %d Huffman tables is supported\n", HUFFMAN_TABLES); + trace("Huffman table %s n%d\n", (index&0xf0)?"AC":"DC", index&0xf); + trace("Length of the table: %d\n", count); +#endif + + if (index & 0xf0 ) + build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]); + else + build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]); + + length -= 1; + length -= 16; + length -= count; + stream += count; + } + trace("< DHT marker\n"); + return 0; +} + +static int parse_DRI(struct jdec_private *priv, const unsigned char *stream) +{ + unsigned int length; + + trace("> DRI marker\n"); + + length = be16_to_cpu(stream); + +#if SANITY_CHECK + if (length != 4) + error("Length of DRI marker need to be 4\n"); +#endif + + priv->restart_interval = be16_to_cpu(stream+2); + +#if DEBUG + trace("Restart interval = %d\n", priv->restart_interval); +#endif + + trace("< DRI marker\n"); + + return 0; +} + + + +static void resync(struct jdec_private *priv) +{ + int i; + + /* Init DC coefficients */ + for (i=0; icomponent_infos[i].previous_DC = 0; + + priv->reservoir = 0; + priv->nbits_in_reservoir = 0; + if (priv->restart_interval > 0) + priv->restarts_to_go = priv->restart_interval; + else + priv->restarts_to_go = -1; +} + +static int find_next_rst_marker(struct jdec_private *priv) +{ + int rst_marker_found = 0; + int marker; + const unsigned char *stream = priv->stream; + + /* Parse marker */ + while (!rst_marker_found) + { + while (*stream++ != 0xff) + { + if (stream >= priv->stream_end) + error("EOF while search for a RST marker."); + } + /* Skip any padding ff byte (this is normal) */ + while (*stream == 0xff) + stream++; + + marker = *stream++; + if ((RST+priv->last_rst_marker_seen) == marker) + rst_marker_found = 1; + else if (marker >= RST && marker <= RST7) + error("Wrong Reset marker found, abording"); + else if (marker == EOI) + return 0; + } + + priv->stream = stream; + priv->last_rst_marker_seen++; + priv->last_rst_marker_seen &= 7; + + return 0; +} + +static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream) +{ + int chuck_len; + int marker; + int sos_marker_found = 0; + int dht_marker_found = 0; + const unsigned char *next_chunck; + + /* Parse marker */ + while (!sos_marker_found) + { + if (*stream++ != 0xff) + goto bogus_jpeg_format; + /* Skip any padding ff byte (this is normal) */ + while (*stream == 0xff) + stream++; + + marker = *stream++; + chuck_len = be16_to_cpu(stream); + next_chunck = stream + chuck_len; + switch (marker) + { + case SOF: + if (parse_SOF(priv, stream) < 0) + return -1; + break; + case DQT: + if (parse_DQT(priv, stream) < 0) + return -1; + break; + case SOS: + if (parse_SOS(priv, stream) < 0) + return -1; + sos_marker_found = 1; + break; + case DHT: + if (parse_DHT(priv, stream) < 0) + return -1; + dht_marker_found = 1; + break; + case DRI: + if (parse_DRI(priv, stream) < 0) + return -1; + break; + default: + trace("> Unknown marker %2.2x\n", marker); + break; + } + + stream = next_chunck; + } + + if (!dht_marker_found) { + trace("No Huffman table loaded, using the default one\n"); + build_default_huffman_tables(priv); + } + +#ifdef SANITY_CHECK + if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor) + || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor)) + error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n"); + if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor) + || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor)) + error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n"); + if ( (priv->component_infos[cCb].Hfactor!=1) + || (priv->component_infos[cCr].Hfactor!=1) + || (priv->component_infos[cCb].Vfactor!=1) + || (priv->component_infos[cCr].Vfactor!=1)) + error("Sampling other than 1x1 for Cr and Cb is not supported"); +#endif + + return 0; +bogus_jpeg_format: + error("Bogus jpeg format\n"); + return -1; +} + +/******************************************************************************* + * + * Functions exported of the library. + * + * Note: Some applications can access directly to internal pointer of the + * structure. It's is not recommended, but if you have many images to + * uncompress with the same parameters, some functions can be called to speedup + * the decoding. + * + ******************************************************************************/ + +/** + * Allocate a new tinyjpeg decoder object. + * + * Before calling any other functions, an object need to be called. + */ +struct jdec_private *tinyjpeg_init(void) +{ + struct jdec_private *priv; + + priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private)); + if (priv == NULL) + return NULL; + return priv; +} + +/** + * Free a tinyjpeg object. + * + * No others function can be called after this one. + */ +void tinyjpeg_free(struct jdec_private *priv) +{ + int i; + for (i=0; icomponents[i]) + free(priv->components[i]); + priv->components[i] = NULL; + } + free(priv); +} + +/** + * Initialize the tinyjpeg object and prepare the decoding of the stream. + * + * Check if the jpeg can be decoded with this jpeg decoder. + * Fill some table used for preprocessing. + */ +int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size) +{ + int ret; + + /* Identify the file */ + if ((buf[0] != 0xFF) || (buf[1] != SOI)) + error("Not a JPG file ?\n"); + + priv->stream_begin = buf+2; + priv->stream_length = size-2; + priv->stream_end = priv->stream_begin + priv->stream_length; + + ret = parse_JFIF(priv, priv->stream_begin); + + return ret; +} + +static const decode_MCU_fct decode_mcu_3comp_table[4] = { + decode_MCU_1x1_3planes, + decode_MCU_1x2_3planes, + decode_MCU_2x1_3planes, + decode_MCU_2x2_3planes, +}; + +static const decode_MCU_fct decode_mcu_1comp_table[4] = { + decode_MCU_1x1_1plane, + decode_MCU_1x2_1plane, + decode_MCU_2x1_1plane, + decode_MCU_2x2_1plane, +}; + +static const convert_colorspace_fct convert_colorspace_yuv420p[4] = { + YCrCB_to_YUV420P_1x1, + YCrCB_to_YUV420P_1x2, + YCrCB_to_YUV420P_2x1, + YCrCB_to_YUV420P_2x2, +}; + +static const convert_colorspace_fct convert_colorspace_rgb24[4] = { + YCrCB_to_RGB24_1x1, + YCrCB_to_RGB24_1x2, + YCrCB_to_RGB24_2x1, + YCrCB_to_RGB24_2x2, +}; + +static const convert_colorspace_fct convert_colorspace_bgr24[4] = { + YCrCB_to_BGR24_1x1, + YCrCB_to_BGR24_1x2, + YCrCB_to_BGR24_2x1, + YCrCB_to_BGR24_2x2, +}; + +static const convert_colorspace_fct convert_colorspace_grey[4] = { + YCrCB_to_Grey_1x1, + YCrCB_to_Grey_1x2, + YCrCB_to_Grey_2x1, + YCrCB_to_Grey_2x2, +}; + +/** + * Decode and convert the jpeg image into @pixfmt@ image + * + * Note: components will be automaticaly allocated if no memory is attached. + */ +int tinyjpeg_decode(struct jdec_private *priv, int pixfmt) +{ + unsigned int x, y, xstride_by_mcu, ystride_by_mcu; + unsigned int bytes_per_blocklines[3], bytes_per_mcu[3]; + decode_MCU_fct decode_MCU; + const decode_MCU_fct *decode_mcu_table; + const convert_colorspace_fct *colorspace_array_conv; + convert_colorspace_fct convert_to_pixfmt; + + if (setjmp(priv->jump_state)) + return -1; + + /* To keep gcc happy initialize some array */ + bytes_per_mcu[1] = 0; + bytes_per_mcu[2] = 0; + bytes_per_blocklines[1] = 0; + bytes_per_blocklines[2] = 0; + + decode_mcu_table = decode_mcu_3comp_table; + switch (pixfmt) { + case TINYJPEG_FMT_YUV420P: + colorspace_array_conv = convert_colorspace_yuv420p; + if (priv->components[0] == NULL) + priv->components[0] = (uint8_t *)malloc(priv->width * priv->height); + if (priv->components[1] == NULL) + priv->components[1] = (uint8_t *)malloc(priv->width * priv->height/4); + if (priv->components[2] == NULL) + priv->components[2] = (uint8_t *)malloc(priv->width * priv->height/4); + bytes_per_blocklines[0] = priv->width; + bytes_per_blocklines[1] = priv->width/4; + bytes_per_blocklines[2] = priv->width/4; + bytes_per_mcu[0] = 8; + bytes_per_mcu[1] = 4; + bytes_per_mcu[2] = 4; + break; + + case TINYJPEG_FMT_RGB24: + colorspace_array_conv = convert_colorspace_rgb24; + if (priv->components[0] == NULL) + priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3); + bytes_per_blocklines[0] = priv->width * 3; + bytes_per_mcu[0] = 3*8; + break; + + case TINYJPEG_FMT_BGR24: + colorspace_array_conv = convert_colorspace_bgr24; + if (priv->components[0] == NULL) + priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3); + bytes_per_blocklines[0] = priv->width * 3; + bytes_per_mcu[0] = 3*8; + break; + + case TINYJPEG_FMT_GREY: + decode_mcu_table = decode_mcu_1comp_table; + colorspace_array_conv = convert_colorspace_grey; + if (priv->components[0] == NULL) + priv->components[0] = (uint8_t *)malloc(priv->width * priv->height); + bytes_per_blocklines[0] = priv->width; + bytes_per_mcu[0] = 8; + break; + + default: + trace("Bad pixel format\n"); + return -1; + } + + xstride_by_mcu = ystride_by_mcu = 8; + if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) { + decode_MCU = decode_mcu_table[0]; + convert_to_pixfmt = colorspace_array_conv[0]; + trace("Use decode 1x1 sampling\n"); + } else if (priv->component_infos[cY].Hfactor == 1) { + decode_MCU = decode_mcu_table[1]; + convert_to_pixfmt = colorspace_array_conv[1]; + ystride_by_mcu = 16; + trace("Use decode 1x2 sampling (not supported)\n"); + } else if (priv->component_infos[cY].Vfactor == 2) { + decode_MCU = decode_mcu_table[3]; + convert_to_pixfmt = colorspace_array_conv[3]; + xstride_by_mcu = 16; + ystride_by_mcu = 16; + trace("Use decode 2x2 sampling\n"); + } else { + decode_MCU = decode_mcu_table[2]; + convert_to_pixfmt = colorspace_array_conv[2]; + xstride_by_mcu = 16; + trace("Use decode 2x1 sampling\n"); + } + + resync(priv); + + /* Don't forget to that block can be either 8 or 16 lines */ + bytes_per_blocklines[0] *= ystride_by_mcu; + bytes_per_blocklines[1] *= ystride_by_mcu; + bytes_per_blocklines[2] *= ystride_by_mcu; + + bytes_per_mcu[0] *= xstride_by_mcu/8; + bytes_per_mcu[1] *= xstride_by_mcu/8; + bytes_per_mcu[2] *= xstride_by_mcu/8; + + /* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */ + for (y=0; y < priv->height/ystride_by_mcu; y++) + { + //trace("Decoding row %d\n", y); + priv->plane[0] = priv->components[0] + (y * bytes_per_blocklines[0]); + priv->plane[1] = priv->components[1] + (y * bytes_per_blocklines[1]); + priv->plane[2] = priv->components[2] + (y * bytes_per_blocklines[2]); + for (x=0; x < priv->width; x+=xstride_by_mcu) + { + decode_MCU(priv); + convert_to_pixfmt(priv); + priv->plane[0] += bytes_per_mcu[0]; + priv->plane[1] += bytes_per_mcu[1]; + priv->plane[2] += bytes_per_mcu[2]; + if (priv->restarts_to_go>0) + { + priv->restarts_to_go--; + if (priv->restarts_to_go == 0) + { + priv->stream -= (priv->nbits_in_reservoir/8); + resync(priv); + if (find_next_rst_marker(priv) < 0) + return -1; + } + } + } + } + + return 0; +} + +const char *tinyjpeg_get_errorstring(struct jdec_private *priv) +{ + return priv->error_string; +} + +void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height) +{ + *width = priv->width; + *height = priv->height; +} + +int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components) +{ + int i; + for (i=0; priv->components[i] && icomponents[i]; + return 0; +} + +int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents) +{ + unsigned int i; + if (ncomponents > COMPONENTS) + ncomponents = COMPONENTS; + for (i=0; icomponents[i] = components[i]; + return 0; +} + +int tinyjpeg_set_flags(struct jdec_private *priv, int flags) +{ + int oldflags = priv->flags; + priv->flags = flags; + return oldflags; +} + diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.h b/v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.h new file mode 100644 index 000000000..42b60c1ee --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/tinyjpeg.h @@ -0,0 +1,73 @@ +/* + * Small jpeg decoder library (header file) + * + * Copyright (c) 2006, Luc Saillard + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + + +#ifndef __JPEGDEC_H__ +#define __JPEGDEC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct jdec_private; + +/* Flags that can be set by any applications */ +#define TINYJPEG_FLAGS_MJPEG_TABLE (1<<1) + +/* Format accepted in outout */ +enum tinyjpeg_fmt { + TINYJPEG_FMT_GREY = 1, + TINYJPEG_FMT_BGR24, + TINYJPEG_FMT_RGB24, + TINYJPEG_FMT_YUV420P, +}; + +struct jdec_private *tinyjpeg_init(void); +void tinyjpeg_free(struct jdec_private *priv); + +int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size); +int tinyjpeg_decode(struct jdec_private *priv, int pixel_format); +const char *tinyjpeg_get_errorstring(struct jdec_private *priv); +void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height); +int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components); +int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents); +int tinyjpeg_set_flags(struct jdec_private *priv, int flags); + +#ifdef __cplusplus +} +#endif + +#endif + + + -- cgit v1.2.3 From 0d3f75db47da8e0a51eab041eb401bfb6f309174 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Tue, 1 Jul 2008 21:11:29 +0200 Subject: v4l2-library: libv4l2 and v4l2convert From: Hans de Goede libv4l2 offers v4l2_ prefixed versions of open/close/etc. The API is 100% the same as directly opening /dev/videoX using regular open/close/etc, the big difference is that format conversion is done if necessary when capturing. That is if you (try to) set a capture format which is not supported by the cam, but is supported by libv4lconvert, then the try_fmt / set_fmt will succeed as if the cam supports the format and on dqbuf / read the data will be converted for you and returned in the request format. v4l2convert: open/close/ioctl/mmap/munmap library call wrapper doing format conversion for v4l2 applications which want to be able to simply capture bgr24 / yuv420 from v4l2 devices with more exotic frame formats. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/include/libv4l2.h | 104 +++ v4l2-apps/lib/libv4l/libv4l2/Makefile | 52 ++ v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h | 94 +++ v4l2-apps/lib/libv4l/libv4l2/libv4l2.c | 1060 +++++++++++++++++++++++++++ v4l2-apps/lib/libv4l/libv4l2/log.c | 138 ++++ v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c | 121 +++ 6 files changed, 1569 insertions(+) create mode 100644 v4l2-apps/lib/libv4l/include/libv4l2.h create mode 100644 v4l2-apps/lib/libv4l/libv4l2/Makefile create mode 100644 v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h create mode 100644 v4l2-apps/lib/libv4l/libv4l2/libv4l2.c create mode 100644 v4l2-apps/lib/libv4l/libv4l2/log.c create mode 100644 v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/include/libv4l2.h b/v4l2-apps/lib/libv4l/include/libv4l2.h new file mode 100644 index 000000000..0e88039b8 --- /dev/null +++ b/v4l2-apps/lib/libv4l/include/libv4l2.h @@ -0,0 +1,104 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __LIBV4L2_H +#define __LIBV4L2_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* Point this to a FILE opened for writing when you want to log error and + status messages to a file, when NULL errors will get send to stderr */ +extern FILE *v4l2_log_file; + +/* Just like your regular open/close/etc, except that format conversion is + done if necessary when capturing. That is if you (try to) set a capture + format which is not supported by the cam, but is supported by libv4lconvert, + then the try_fmt / set_fmt will succeed as if the cam supports the format + and on dqbuf / read the data will be converted for you and returned in + the request format. + + Another difference is that you can make v4l2_read() calls even on devices + which do not support the regular read() method. + + Note that libv4l2 normally does not interfere with enum_fmt, so enum_fmt + will still return the actual formats the hardware supports, and not any + formats which may be emulated on top of that. If you pass the + V4L2_ENABLE_ENUM_FMT_EMULATION flag to v4l2_fd_open (as the v4l2convert.so + wrapper does) then enum_fmt will also report support for the formats to + which conversion is possible. + + Note the device name passed to v4l2_open must be of a video4linux2 device, + if it is anything else (including a video4linux1 device), v4l2_open will + fail. +*/ + +int v4l2_open (const char *file, int oflag, ...); +int v4l2_close(int fd); +int v4l2_dup(int fd); +int v4l2_ioctl (int fd, unsigned long int request, ...); +ssize_t v4l2_read (int fd, void* buffer, size_t n); +void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, + off_t offset); +int v4l2_munmap(void *_start, size_t length); + + +/* Misc utility functions */ + +/* This function takes a value of 0 - 65535, and then scales that range to + the actual range of the given v4l control id, and then if the cid exists + and is not locked sets the cid to the scaled value. + + Normally returns 0, even if the cid did not exist or was locked, returns + non 0 when an other error occured. */ +int v4l2_set_control(int fd, int cid, int value); + +/* This function returns a value of 0 - 65535, scaled to from the actual range + of the given v4l control id. when the cid does not exist, could not be + accessed for some reason, or some error occured 0 is returned. */ +int v4l2_get_control(int fd, int cid); + + +/* "low level" access functions, these functions allow somewhat lower level + access to libv4l2 (currently there only is v4l2_fd_open here) */ + +/* Flags for v4l2_fd_open's v4l2_flags argument */ + +/* Disable all format conversion done by libv4l2 (reduces libv4l2 functionality + to offering v4l2_read() even on devices which don't implement read()) */ +#define V4L2_DISABLE_CONVERSION 0x01 +/* Report not only real but also emulated formats with the ENUM_FMT ioctl */ +#define V4L2_ENABLE_ENUM_FMT_EMULATION 02 + +/* v4l2_fd_open: open an already opened fd for further use through + v4l2lib and possibly modify libv4l2's default behavior through the + v4l2_flags argument. + + Returns fd on success, -1 if the fd is not suitable for use through libv4l2 + (note the fd is left open in this case). */ +int v4l2_fd_open(int fd, int v4l2_flags); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/v4l2-apps/lib/libv4l/libv4l2/Makefile b/v4l2-apps/lib/libv4l/libv4l2/Makefile new file mode 100644 index 000000000..c5463800b --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l2/Makefile @@ -0,0 +1,52 @@ +CC = gcc +LD = gcc + +CPPFLAGS = -fPIC -I../include -I../../../../linux/include + +CFLAGS := -g -O1 +CFLAGS += -Wall -W -Wno-unused -Wpointer-arith -Wstrict-prototypes + +LDFLAGS = -shared + +V4L2_OBJS = libv4l2.o log.o ../libv4lconvert/libv4lconvert.so +V4L2_LIB = libv4l2.so +V4L2CONVERT = v4l2convert.so +V4L2CONVERT_O = v4l2convert.o libv4l2.so +TARGETS = $(V4L2_LIB) $(V4L2CONVERT) +INCLUDES = ../include/libv4l2.h + +ifeq ($(LIB_RELEASE),) +LIB_RELEASE = 0 +endif + +ifeq ($(DESTDIR),) +DESTDIR = /usr/local +endif + +all: $(TARGETS) + +$(V4L2_LIB): $(V4L2_OBJS) + +$(V4L2CONVERT): $(V4L2CONVERT_O) $(V4L2_LIB) + +install: all + mkdir -p $(DESTDIR)/include + cp $(INCLUDES) $(DESTDIR)/include + mkdir -p $(DESTDIR)/lib + cp $(V4L2_LIB).$(LIB_RELEASE) $(DESTDIR)/lib + cd $(DESTDIR)/lib && \ + ln -f -s $(V4L2_LIB).$(LIB_RELEASE) $(V4L2_LIB) + cp $(V4L2CONVERT).$(LIB_RELEASE) $(DESTDIR)/lib + cd $(DESTDIR)/lib && \ + ln -f -s $(V4L2CONVERT).$(LIB_RELEASE) $(V4L2CONVERT) + +clean:: + rm -f *.so* *.o log *~ + rm -f *.d + +%.o: %.c + $(CC) -c -MMD $(CPPFLAGS) $(CFLAGS) -o $@ $< + +%.so: + $(CC) $(LDFLAGS) -Wl,-soname,$@.$(LIB_RELEASE) -o $@.$(LIB_RELEASE) $^ + ln -f -s $@.$(LIB_RELEASE) $@ diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h b/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h new file mode 100644 index 000000000..bdbd45e5c --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h @@ -0,0 +1,94 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __LIBV4L2_PRIV_H +#define __LIBV4L2_PRIV_H + +#include +#include +#include /* includes videodev2.h for us */ + +#define V4L2_MAX_DEVICES 16 +/* Warning when making this larger the frame_queued and frame_mapped members of + the v4l2_dev_info struct can no longer be a bitfield, so the code needs to + be adjusted! */ +#define V4L2_MAX_NO_FRAMES 32u +#define V4L2_DEFAULT_NREADBUFFERS 4 +#define V4L2_FRAME_BUF_SIZE (4096 * 4096) + +#define V4L2_LOG_ERR(...) \ + do { \ + if (v4l2_log_file) { \ + fprintf(v4l2_log_file, "libv4l2: error " __VA_ARGS__); \ + fflush(v4l2_log_file); \ + } else \ + fprintf(stderr, "libv4l2: error " __VA_ARGS__); \ + } while(0) + +#define V4L2_LOG_WARN(...) \ + do { \ + if (v4l2_log_file) { \ + fprintf(v4l2_log_file, "libv4l2: warning " __VA_ARGS__); \ + fflush(v4l2_log_file); \ + } else \ + fprintf(stderr, "libv4l2: warning " __VA_ARGS__); \ + } while(0) + +#define V4L2_LOG(...) \ + do { \ + if (v4l2_log_file) { \ + fprintf(v4l2_log_file, "libv4l2: " __VA_ARGS__); \ + fflush(v4l2_log_file); \ + } \ + } while(0) + +#define MIN(a,b) (((a)<(b))?(a):(b)) + +enum v4l2_io { v4l2_io_none, v4l2_io_read, v4l2_io_mmap }; + +struct v4l2_dev_info { + int fd; + int flags; + int open_count; + /* actually format of the cam */ + struct v4l2_format src_fmt; + /* fmt as seen by the application (iow after conversion) */ + struct v4l2_format dest_fmt; + pthread_mutex_t stream_lock; + unsigned int no_frames; + unsigned int nreadbuffers; + enum v4l2_io io; + struct v4lconvert_data *convert; + unsigned char *convert_mmap_buf; + /* Frame bookkeeping is only done when in read or mmap-conversion mode */ + unsigned char *frame_pointers[V4L2_MAX_NO_FRAMES]; + int frame_sizes[V4L2_MAX_NO_FRAMES]; + int frame_queued; /* 1 status bit per frame */ + /* mapping tracking of our fake (converting mmap) frame buffers, todo this + perfect we should use a map counter per frame, this is a good + approximation but there are scenarios thinkable where this doesn't work. + However no normal application not even a buggy one is likely to exhibit + the patterns needed to fail this somewhat simplified tracking */ + int frame_mapped; /* 1 status bit per frame */ + int frame_map_count; /* total number of maps of (fake) buffers combined */ +}; + +/* From log.c */ +void v4l2_log_ioctl(unsigned long int request, void *arg, int result); + +#endif diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c new file mode 100644 index 000000000..33d9b2891 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c @@ -0,0 +1,1060 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* MAKING CHANGES TO THIS FILE?? READ THIS FIRST!!! + + This file implements libv4l2, which offers v4l2_ prefixed versions of + open/close/etc. The API is 100% the same as directly opening /dev/videoX + using regular open/close/etc, the big difference is that format conversion + is done if necessary when capturing. That is if you (try to) set a capture + format which is not supported by the cam, but is supported by libv4lconvert, + then the try_fmt / set_fmt will succeed as if the cam supports the format + and on dqbuf / read the data will be converted for you and returned in + the request format. + + Important note to people making changes to this file: All functions + (v4l2_close, v4l2_ioctl, etc.) are designed to function as their regular + counterpart when they get passed a fd that is not "registered" by libv4l2, + there are 2 reasons for this: + 1) This allows us to get completely out of the way when dealing with non + capture devices. + 2) libv4l2 is the base of the v4l2convert.so wrapper lib, which is a .so + which can be LD_PRELOAD-ed and the overrules the libc's open/close/etc, + and when opening /dev/videoX calls v4l2_open. Because we behave as the + regular counterpart when the fd is not known (instead of say throwing + an error), v4l2convert.so can simply call the v4l2_ prefixed function + for all wrapped functions (except for v4l2_open which will fail when not + called on a v4l2 device). This way the wrapper does not have to keep + track of which fd's are being handled by libv4l2, as libv4l2 already + keeps track of this itself. + + This also means that libv4l2 may not use any of the regular functions + it mimics, as for example open could be a symbol in v4l2convert.so, which + in turn will call v4l2_open, so therefor v4l2_open (for example) may not + use the regular open()! + + Another important note: libv4l2 does conversion for capture usage only, if + any calls are made which are passed a v4l2_buffer or v4l2_format with a + v4l2_buf_type which is different from V4L2_BUF_TYPE_VIDEO_CAPTURE, then + the v4l2_ methods behave exactly the same as their regular counterparts. + When modifications are made, one should be carefull that this behavior is + preserved. +*/ + +#define _LARGEFILE64_SOURCE 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libv4l2.h" +#include "libv4l2-priv.h" + +/* Note these flags are stored together with the flags passed to v4l2_fd_open() + in v4l2_dev_info's flags member, so care should be taken that the do not + use the same bits! */ +#define V4L2_STREAMON 0x0100 + +#define V4L2_MMAP_OFFSET_MAGIC 0xABCDEF00u + +static pthread_mutex_t v4l2_open_mutex = PTHREAD_MUTEX_INITIALIZER; +static struct v4l2_dev_info devices[V4L2_MAX_DEVICES] = { {-1}, {-1}, + {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, + {-1}, {-1}}; +static int devices_used = 0; + + +static int v4l2_request_read_buffers(int index) +{ + int result; + struct v4l2_requestbuffers req; + + /* No-op if already done */ + if (devices[index].no_frames) + return 0; + + /* Request buffers */ + req.count = devices[index].nreadbuffers; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + req.memory = V4L2_MEMORY_MMAP; + if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, &req))){ + int saved_err = errno; + V4L2_LOG_ERR("requesting buffers: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + + devices[index].no_frames = MIN(req.count, V4L2_MAX_NO_FRAMES); + return 0; +} + +static int v4l2_unrequest_read_buffers(int index) +{ + int result; + struct v4l2_requestbuffers req; + + /* No-op of already done */ + if (devices[index].no_frames == 0) + return 0; + + /* (Un)Request buffers */ + req.count = 0; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + req.memory = V4L2_MEMORY_MMAP; + if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, &req))) { + int saved_err = errno; + V4L2_LOG_ERR("unrequesting buffers: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + + devices[index].no_frames = MIN(req.count, V4L2_MAX_NO_FRAMES); + if (devices[index].no_frames) { + V4L2_LOG_ERR("number of buffers > 0 after requesting 0 buffers\n"); + errno = EBUSY; + return -1; + } + return 0; +} + +static int v4l2_map_buffers(int index) +{ + int result = 0; + unsigned int i; + struct v4l2_buffer buf; + + for (i = 0; i < devices[index].no_frames; i++) { + if (devices[index].frame_pointers[i] != MAP_FAILED) + continue; + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + buf.index = i; + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QUERYBUF, &buf); + if (result) { + int saved_err = errno; + V4L2_LOG_ERR("querying buffer %u: %s\n", i, strerror(errno)); + errno = saved_err; + break; + } + + devices[index].frame_pointers[i] = mmap64(NULL, buf.length, + PROT_READ, MAP_SHARED, devices[index].fd, buf.m.offset); + if (devices[index].frame_pointers[i] == MAP_FAILED) { + int saved_err = errno; + V4L2_LOG_ERR("mmapping buffer %u: %s\n", i, strerror(errno)); + errno = saved_err; + result = -1; + break; + } + V4L2_LOG("mapped buffer %u at %p\n", i, + devices[index].frame_pointers[i]); + + devices[index].frame_sizes[i] = buf.length; + } + + return result; +} + +static void v4l2_unmap_buffers(int index) +{ + unsigned int i; + + /* unmap the buffers */ + for (i = 0; i < devices[index].no_frames; i++) { + if (devices[index].frame_pointers[i] != MAP_FAILED) { + syscall(SYS_munmap, devices[index].frame_pointers[i], + devices[index].frame_sizes[i]); + devices[index].frame_pointers[i] = MAP_FAILED; + V4L2_LOG("unmapped buffer %u\n", i); + } + } +} + +static int v4l2_streamon(int index) +{ + int result; + enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if (!(devices[index].flags & V4L2_STREAMON)) { + if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_STREAMON, + &type))) { + int saved_err = errno; + V4L2_LOG_ERR("turning on stream: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + devices[index].flags |= V4L2_STREAMON; + } + + return 0; +} + +static int v4l2_streamoff(int index) +{ + int result; + enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if (devices[index].flags & V4L2_STREAMON) { + if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_STREAMOFF, + &type))) { + int saved_err = errno; + V4L2_LOG_ERR("turning off stream: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + devices[index].flags &= ~V4L2_STREAMON; + + /* Stream off also unqueues all our buffers! */ + devices[index].frame_queued = 0; + } + + return 0; +} + +static int v4l2_queue_read_buffer(int index, int buffer_index) +{ + int result; + struct v4l2_buffer buf; + + if (devices[index].frame_queued & (1 << buffer_index)) + return 0; + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + buf.index = buffer_index; + if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QBUF, &buf))) { + int saved_err = errno; + V4L2_LOG_ERR("queuing buf %d: %s\n", buffer_index, strerror(errno)); + errno = saved_err; + return result; + } + + devices[index].frame_queued |= 1 << buffer_index; + return 0; +} + +static int v4l2_dequeue_read_buffer(int index, int *bytesused) +{ + int result; + struct v4l2_buffer buf; + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_DQBUF, &buf))) { + int saved_err = errno; + V4L2_LOG_ERR("dequeuing buf: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + + devices[index].frame_queued &= ~(1 << buf.index); + *bytesused = buf.bytesused; + return buf.index; +} + +static int v4l2_queue_read_buffers(int index) +{ + unsigned int i; + int last_error = EIO, queued = 0; + + for (i = 0; i < devices[index].no_frames; i++) { + /* Don't queue unmapped buffers (should never happen) */ + if (devices[index].frame_pointers[i] != MAP_FAILED) { + if (v4l2_queue_read_buffer(index, i)) { + last_error = errno; + continue; + } + queued++; + } + } + + if (!queued) { + errno = last_error; + return -1; + } + return 0; +} + +static int v4l2_activate_read_stream(int index) +{ + int result; + + if ((result = v4l2_request_read_buffers(index))) + return result; + + if ((result = v4l2_map_buffers(index))) + return result; + + if ((result = v4l2_queue_read_buffers(index))) + return result; + + return result = v4l2_streamon(index); +} + +static int v4l2_deactivate_read_stream(int index) +{ + int result; + + if ((result = v4l2_streamoff(index))) + return result; + + /* No need to unqueue our buffers, streamoff does that for us */ + + v4l2_unmap_buffers(index); + + if ((result = v4l2_unrequest_read_buffers(index))) + return result; + + return 0; +} + + +int v4l2_open (const char *file, int oflag, ...) +{ + int fd; + + /* original open code */ + if (oflag & O_CREAT) + { + va_list ap; + mode_t mode; + + va_start (ap, oflag); + mode = va_arg (ap, mode_t); + + fd = syscall(SYS_open, file, oflag, mode); + + va_end(ap); + } + else + fd = syscall(SYS_open, file, oflag); + /* end of original open code */ + + if (fd == -1) + return fd; + + if (v4l2_fd_open(fd, 0) == -1) { + int saved_err = errno; + syscall(SYS_close, fd); + errno = saved_err; + return -1; + } + + return fd; +} + +int v4l2_fd_open(int fd, int v4l2_flags) +{ + int i, index; + char *lfname; + struct v4l2_capability cap; + struct v4l2_format fmt; + struct v4lconvert_data *convert; + + /* If no log file was set by the app, see if one was specified through the + environment */ + if (!v4l2_log_file && (lfname = getenv("LIBV4L2_LOG_FILENAME"))) + v4l2_log_file = fopen(lfname, "w"); + + /* check that this is an v4l2 device */ + if (syscall(SYS_ioctl, fd, VIDIOC_QUERYCAP, &cap)) { + int saved_err = errno; + V4L2_LOG_ERR("getting capabilities: %s\n", strerror(errno)); + errno = saved_err; + return -1; + } + + /* we only add functionality for video capture devices */ + if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) + return fd; + + /* Get current cam format */ + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (syscall(SYS_ioctl, fd, VIDIOC_G_FMT, &fmt)) { + int saved_err = errno; + V4L2_LOG_ERR("getting pixformat: %s\n", strerror(errno)); + errno = saved_err; + return -1; + } + + /* init libv4lconvert */ + if (!(convert = v4lconvert_create(fd))) + return -1; + + /* So we have a v4l2 capture device, register it in our devices array */ + pthread_mutex_lock(&v4l2_open_mutex); + for (index = 0; index < V4L2_MAX_DEVICES; index++) + if(devices[index].fd == -1) { + devices[index].fd = fd; + break; + } + pthread_mutex_unlock(&v4l2_open_mutex); + + if (index == V4L2_MAX_DEVICES) { + V4L2_LOG_ERR("attempting to open more then %d video devices\n", + V4L2_MAX_DEVICES); + errno = EBUSY; + return -1; + } + + devices[index].flags = v4l2_flags; + devices[index].open_count = 1; + devices[index].src_fmt = fmt; + devices[index].dest_fmt = fmt; + + pthread_mutex_init(&devices[index].stream_lock, NULL); + + devices[index].no_frames = 0; + devices[index].nreadbuffers = V4L2_DEFAULT_NREADBUFFERS; + devices[index].io = v4l2_io_none; + devices[index].convert = convert; + devices[index].convert_mmap_buf = MAP_FAILED; + for (i = 0; i < V4L2_MAX_NO_FRAMES; i++) { + devices[index].frame_pointers[i] = MAP_FAILED; + } + devices[index].frame_queued = 0; + devices[index].frame_mapped = 0; + devices[index].frame_map_count = 0; + + if (index >= devices_used) + devices_used = index + 1; + + V4L2_LOG("open: %d\n", fd); + + return fd; +} + +/* Is this an fd for which we are emulating v4l1 ? */ +static int v4l2_get_index(int fd) +{ + int index; + + /* We never handle fd -1 */ + if (fd == -1) + return -1; + + for (index = 0; index < devices_used; index++) + if (devices[index].fd == fd) + break; + + if (index == devices_used) + return -1; + + return index; +} + + +int v4l2_close(int fd) +{ + int index, result; + + if ((index = v4l2_get_index(fd)) == -1) + return syscall(SYS_close, fd); + + /* Abuse stream_lock to stop 2 closes from racing and trying to free the + resources twice */ + pthread_mutex_lock(&devices[index].stream_lock); + devices[index].open_count--; + result = devices[index].open_count != 0; + pthread_mutex_unlock(&devices[index].stream_lock); + + if (result) + return 0; + + /* Free resources */ + v4l2_unmap_buffers(index); + v4lconvert_destroy(devices[index].convert); + if (devices[index].convert_mmap_buf != MAP_FAILED) { + if (devices[index].frame_mapped || devices[index].frame_map_count) + V4L2_LOG( + "v4l2 mmap buffers still mapped on close(), mask: %08x, count: %d\n", + (unsigned int)devices[index].frame_mapped, + devices[index].frame_map_count); + else + syscall(SYS_munmap, devices[index].convert_mmap_buf, + devices[index].no_frames * V4L2_FRAME_BUF_SIZE); + devices[index].convert_mmap_buf = MAP_FAILED; + } + + /* Remove the fd from our list of managed fds before closing it, because as + soon as we've done the actual close the fd maybe returned by an open in + another thread and we don't want to intercept calls to this new fd. */ + devices[index].fd = -1; + + /* Since we've marked the fd as no longer used, and freed the resources, + redo the close in case it was interrupted */ + do { + result = syscall(SYS_close, fd); + } while (result == -1 && errno == EINTR); + + V4L2_LOG("close: %d\n", fd); + + return result; +} + +int v4l2_dup(int fd) +{ + int index; + + if ((index = v4l2_get_index(fd)) == -1) + return syscall(SYS_dup, fd); + + devices[index].open_count++; + + return fd; +} + +static int v4l2_buf_ioctl_pre_check(int index, unsigned long int request, + struct v4l2_buffer *buf, int *result) +{ + if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + *result = syscall(SYS_ioctl, devices[index].fd, request, buf); + return 1; + } + + /* IMPROVEME (maybe?) add support for userptr's? */ + if (devices[index].io != v4l2_io_mmap || + buf->memory != V4L2_MEMORY_MMAP || + buf->index >= devices[index].no_frames) { + errno = EINVAL; + *result = -1; + return 1; + } + + return 0; +} + +int v4l2_ioctl (int fd, unsigned long int request, ...) +{ + void *arg; + va_list ap; + int result, converting, index, saved_err, stream_locked = 0; + + va_start (ap, request); + arg = va_arg (ap, void *); + va_end (ap); + + if ((index = v4l2_get_index(fd)) == -1) + return syscall(SYS_ioctl, fd, request, arg); + + /* do we need to take the stream lock for this ioctl? */ + switch (request) { + case VIDIOC_S_FMT: + case VIDIOC_G_FMT: + case VIDIOC_REQBUFS: + case VIDIOC_QUERYBUF: + case VIDIOC_QBUF: + case VIDIOC_DQBUF: + case VIDIOC_STREAMON: + case VIDIOC_STREAMOFF: + pthread_mutex_lock(&devices[index].stream_lock); + stream_locked = 1; + } + + converting = devices[index].src_fmt.fmt.pix.pixelformat != + devices[index].dest_fmt.fmt.pix.pixelformat; + + + switch (request) { + case VIDIOC_ENUM_FMT: + { + struct v4l2_fmtdesc *fmtdesc = arg; + + if (fmtdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || + !(devices[index].flags & V4L2_ENABLE_ENUM_FMT_EMULATION)) + result = syscall(SYS_ioctl, devices[index].fd, request, arg); + else + result = v4lconvert_enum_fmt(devices[index].convert, fmtdesc); + } + break; + + case VIDIOC_TRY_FMT: + { + struct v4l2_format *fmt = arg; + + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || + (devices[index].flags & V4L2_DISABLE_CONVERSION)) { + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, fmt); + break; + } + + result = v4lconvert_try_format(devices[index].convert, fmt, NULL); + } + break; + + case VIDIOC_S_FMT: + { + struct v4l2_format src_fmt, *dest_fmt = arg; + + if (dest_fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_S_FMT, + dest_fmt); + break; + } + + if (!memcmp(&devices[index].dest_fmt, dest_fmt, sizeof(*dest_fmt))) { + result = 0; + break; + } + + /* Don't allow changing the format when mmap-ed IO is active, we could + allow this to happen in certain special circumstances, but it is + best to consistently deny this so that application developers do not + go expect this to work, because in their test setup it happens to + work. This also keeps the code much saner. */ + if (devices[index].io == v4l2_io_mmap) { + errno = EBUSY; + result = -1; + break; + } + + if (devices[index].flags & V4L2_DISABLE_CONVERSION) { + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, + dest_fmt); + src_fmt = *dest_fmt; + } else { + result = v4lconvert_try_format(devices[index].convert, dest_fmt, + &src_fmt); + } + + if (result) + break; + + /* Maybe after try format has adjusted width/height etc, to whats + available nothing has changed (on the cam side) ? */ + if (!memcmp(&devices[index].src_fmt, &src_fmt, sizeof(src_fmt))) { + devices[index].dest_fmt = *dest_fmt; + result = 0; + break; + } + + if (devices[index].io == v4l2_io_read) { + V4L2_LOG("deactivating read-stream for format change\n"); + if ((result = v4l2_deactivate_read_stream(index))) { + /* Undo what we've done to leave things in a consisten state */ + if (v4l2_activate_read_stream(index)) + V4L2_LOG_ERR( + "reactivating stream after deactivate failure (AAIIEEEE)\n"); + return result; + } + } + + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_S_FMT, &src_fmt); + if (result) { + saved_err = errno; + V4L2_LOG_ERR("setting pixformat: %s\n", strerror(errno)); + /* Report to the app dest_fmt has not changed */ + *dest_fmt = devices[index].dest_fmt; + errno = saved_err; + break; + } + + devices[index].src_fmt = src_fmt; + devices[index].dest_fmt = *dest_fmt; + } + break; + + case VIDIOC_G_FMT: + { + struct v4l2_format* fmt = arg; + + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_G_FMT, fmt); + break; + } + + *fmt = devices[index].dest_fmt; + result = 0; + } + break; + + case VIDIOC_REQBUFS: + { + struct v4l2_requestbuffers *req = arg; + + if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + result = syscall(SYS_ioctl, devices[index].fd, request, arg); + return 1; + } + + /* Don't allow mixing read / mmap io, either we control the buffers + (read based io), or the app does */ + if (devices[index].io == v4l2_io_read) { + V4L2_LOG_ERR("to change from read io to mmap io open and close the device first!\n"); + errno = EBUSY; + result = -1; + break; + } + + /* Are any of our fake (convert_mmap_buf) buffers still mapped ? */ + if (devices[index].frame_mapped || devices[index].frame_map_count) { + errno = EBUSY; + result = -1; + break; + } + + /* IMPROVEME (maybe?) add support for userptr's? */ + if (req->memory != V4L2_MEMORY_MMAP) { + errno = EINVAL; + result = -1; + break; + } + + /* No more buffers then we can manage please */ + if (req->count > V4L2_MAX_NO_FRAMES) + req->count = V4L2_MAX_NO_FRAMES; + + /* Stop stream and unmap our real mapping of the buffers + (only relevant when we're converting, otherwise a no-op) */ + v4l2_streamoff(index); + v4l2_unmap_buffers(index); + + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, req); + if (result) + break; + + /* If we got more frames then we can handle lie to the app */ + if (req->count > V4L2_MAX_NO_FRAMES) + req->count = V4L2_MAX_NO_FRAMES; + + /* Force reallocation of convert_mmap_buf to fit the new no_frames */ + syscall(SYS_munmap, devices[index].convert_mmap_buf, + devices[index].no_frames * V4L2_FRAME_BUF_SIZE); + devices[index].convert_mmap_buf = MAP_FAILED; + devices[index].no_frames = req->count; + devices[index].io = req->count? v4l2_io_mmap:v4l2_io_none; + } + break; + + case VIDIOC_QUERYBUF: + { + struct v4l2_buffer *buf = arg; + + if (v4l2_buf_ioctl_pre_check(index, request, buf, &result)) + break; + + /* Do a real query even when converting to let the driver fill in + things like buf->field */ + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QUERYBUF, buf); + if (result || !converting) + break; + + buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index; + buf->length = V4L2_FRAME_BUF_SIZE; + } + break; + + case VIDIOC_QBUF: + { + struct v4l2_buffer *buf = arg; + + if (v4l2_buf_ioctl_pre_check(index, request, buf, &result)) + break; + + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QBUF, buf); + } + break; + + case VIDIOC_DQBUF: + { + struct v4l2_buffer *buf = arg; + + if (v4l2_buf_ioctl_pre_check(index, request, buf, &result)) + break; + + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_DQBUF, buf); + if (result || !converting) + break; + + /* An application can do a DQBUF before mmap-ing in the buffer, + but we need the buffer _now_ to write our converted data + to it! */ + if (devices[index].convert_mmap_buf == MAP_FAILED) { + devices[index].convert_mmap_buf = mmap64(NULL, + devices[index].no_frames * + V4L2_FRAME_BUF_SIZE, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + if (devices[index].convert_mmap_buf == MAP_FAILED) { + saved_err = errno; + V4L2_LOG_ERR("allocating conversion buffer\n"); + errno = saved_err; + result = -1; + break; + } + } + + /* Make sure we have the real v4l2 buffers mapped before trying to + read from them */ + if ((result = v4l2_map_buffers(index))) + break; + + result = v4lconvert_convert(devices[index].convert, + &devices[index].src_fmt, &devices[index].dest_fmt, + devices[index].frame_pointers[buf->index], + buf->bytesused, + devices[index].convert_mmap_buf + + buf->index * V4L2_FRAME_BUF_SIZE, + V4L2_FRAME_BUF_SIZE); + if (result < 0) + break; + + buf->bytesused = result; + buf->length = V4L2_FRAME_BUF_SIZE; + + result = 0; + } + break; + + case VIDIOC_STREAMON: + case VIDIOC_STREAMOFF: + { + enum v4l2_buf_type *type = arg; + + if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + result = syscall(SYS_ioctl, devices[index].fd, request, type); + break; + } + + if (devices[index].io != v4l2_io_mmap) { + errno = EINVAL; + result = -1; + break; + } + + if (request == VIDIOC_STREAMON) + result = v4l2_streamon(index); + else + result = v4l2_streamoff(index); + } + break; + + default: + result = syscall(SYS_ioctl, fd, request, arg); + } + + if (stream_locked) + pthread_mutex_unlock(&devices[index].stream_lock); + + saved_err = errno; + v4l2_log_ioctl(request, arg, result); + errno = saved_err; + + return result; +} + + +ssize_t v4l2_read (int fd, void* buffer, size_t n) +{ + ssize_t result; + int index, bytesused, frame_index; + + if ((index = v4l2_get_index(fd)) == -1) + return syscall(SYS_read, fd, buffer, n); + + pthread_mutex_lock(&devices[index].stream_lock); + + if (devices[index].io == v4l2_io_mmap) { + V4L2_LOG_ERR("to change from mmap io to read io first do request_buffers with a count of 0\n"); + errno = EBUSY; + result = -1; + goto leave; + } + devices[index].io = v4l2_io_read; + + if ((result = v4l2_activate_read_stream(index))) + goto leave; + + if ((frame_index = v4l2_dequeue_read_buffer(index, &bytesused)) < 0) { + result = -1; + goto leave; + } + + result = v4lconvert_convert(devices[index].convert, + &devices[index].src_fmt, &devices[index].dest_fmt, + devices[index].frame_pointers[frame_index], bytesused, + buffer, n); + + v4l2_queue_read_buffer(index, frame_index); + +leave: + pthread_mutex_unlock(&devices[index].stream_lock); + + return result; +} + +void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, + off_t offset) +{ + int index; + unsigned int buffer_index; + void *result; + + if ((index = v4l2_get_index(fd)) == -1 || + /* Check if the mmap data matches our answer to QUERY_BUF, if it doesn't + let the kernel handle it (to allow for mmap based non capture use) */ + start || length != V4L2_FRAME_BUF_SIZE || + (offset & ~0xff) != V4L2_MMAP_OFFSET_MAGIC) { + if (index != -1) + V4L2_LOG("Passing mmap(%p, %d, ..., %x, through to the driver\n", + start, (int)length, (int)offset); + return mmap64(start, length, prot, flags, fd, offset); + } + + pthread_mutex_lock(&devices[index].stream_lock); + + buffer_index = offset & 0xff; + if (buffer_index >= devices[index].no_frames || + devices[index].io != v4l2_io_mmap || + /* Got magic offset and not converting ?? */ + devices[index].src_fmt.fmt.pix.pixelformat == + devices[index].dest_fmt.fmt.pix.pixelformat) { + errno = EINVAL; + result = MAP_FAILED; + goto leave; + } + + if (devices[index].convert_mmap_buf == MAP_FAILED) { + devices[index].convert_mmap_buf = mmap64(NULL, + devices[index].no_frames * + V4L2_FRAME_BUF_SIZE, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + if (devices[index].convert_mmap_buf == MAP_FAILED) { + int saved_err = errno; + V4L2_LOG_ERR("allocating conversion buffer\n"); + errno = saved_err; + result = MAP_FAILED; + goto leave; + } + } + + devices[index].frame_mapped |= 1 << buffer_index; + devices[index].frame_map_count++; + + result = devices[index].convert_mmap_buf + + buffer_index * V4L2_FRAME_BUF_SIZE; + + V4L2_LOG("Fake (conversion) mmap buf %u, seen by app at: %p\n", + buffer_index, result); + +leave: + pthread_mutex_unlock(&devices[index].stream_lock); + + return result; +} + +int v4l2_munmap(void *_start, size_t length) +{ + int index; + unsigned int buffer_index; + unsigned char *start = _start; + + /* Is this memory ours? */ + if (start != MAP_FAILED && length == V4L2_FRAME_BUF_SIZE) { + for (index = 0; index < devices_used; index++) + if (devices[index].fd != -1 && + devices[index].convert_mmap_buf != MAP_FAILED && + start >= devices[index].convert_mmap_buf && + (start - devices[index].convert_mmap_buf) % length == 0) + break; + + if (index != devices_used) { + int unmapped = 0; + + pthread_mutex_lock(&devices[index].stream_lock); + + buffer_index = (start - devices[index].convert_mmap_buf) / length; + + /* Redo our checks now that we have the lock, things may have changed */ + if (devices[index].convert_mmap_buf != MAP_FAILED && + start >= devices[index].convert_mmap_buf && + (start - devices[index].convert_mmap_buf) % length == 0 && + buffer_index < devices[index].no_frames) { + devices[index].frame_mapped &= ~(1 << buffer_index); + if (devices[index].frame_map_count > 0) + devices[index].frame_map_count--; + unmapped = 1; + } + + pthread_mutex_unlock(&devices[index].stream_lock); + + if (unmapped) { + V4L2_LOG("v4l2 fake buffer munmap %p, %d\n", start, (int)length); + return 0; + } + } + } + + V4L2_LOG("v4l2 unknown munmap %p, %d\n", start, (int)length); + + return syscall(SYS_munmap, _start, length); +} + +/* Misc utility functions */ +int v4l2_set_control(int fd, int cid, int value) +{ + struct v4l2_queryctrl qctrl = { .id = cid }; + struct v4l2_control ctrl = { .id = cid }; + int result; + + if ((result = syscall(SYS_ioctl, fd, VIDIOC_QUERYCTRL, &qctrl))) + return result; + + if (!(qctrl.flags & V4L2_CTRL_FLAG_DISABLED) && + !(qctrl.flags & V4L2_CTRL_FLAG_GRABBED)) { + if (qctrl.type == V4L2_CTRL_TYPE_BOOLEAN) + ctrl.value = value? 1:0; + else + ctrl.value = (value * (qctrl.maximum - qctrl.minimum) + 32767) / 65535 + + qctrl.minimum; + + result = syscall(SYS_ioctl, fd, VIDIOC_S_CTRL, &ctrl); + } + + return result; +} + +int v4l2_get_control(int fd, int cid) +{ + struct v4l2_queryctrl qctrl = { .id = cid }; + struct v4l2_control ctrl = { .id = cid }; + + if (syscall(SYS_ioctl, fd, VIDIOC_QUERYCTRL, &qctrl)) + return 0; + + if (qctrl.flags & V4L2_CTRL_FLAG_DISABLED) + return 0; + + if (syscall(SYS_ioctl, fd, VIDIOC_G_CTRL, &ctrl)) + return 0; + + return ((ctrl.value - qctrl.minimum) * 65535 + + (qctrl.maximum - qctrl.minimum) / 2) / + (qctrl.maximum - qctrl.minimum); +} diff --git a/v4l2-apps/lib/libv4l/libv4l2/log.c b/v4l2-apps/lib/libv4l/libv4l2/log.c new file mode 100644 index 000000000..982a185c6 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l2/log.c @@ -0,0 +1,138 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +/* These headers are not needed by us, but by linux/videodev2.h, + which is broken on some systems and doesn't include them itself :( */ +#include +#include +/* end broken header workaround includes */ +#include + +#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) + +FILE *v4l2_log_file = NULL; + +static const char *v4l2_ioctls[] = { + /* start v4l2 ioctls */ + [_IOC_NR(VIDIOC_QUERYCAP)] = "VIDIOC_QUERYCAP", + [_IOC_NR(VIDIOC_RESERVED)] = "VIDIOC_RESERVED", + [_IOC_NR(VIDIOC_ENUM_FMT)] = "VIDIOC_ENUM_FMT", + [_IOC_NR(VIDIOC_G_FMT)] = "VIDIOC_G_FMT", + [_IOC_NR(VIDIOC_S_FMT)] = "VIDIOC_S_FMT", + [_IOC_NR(VIDIOC_REQBUFS)] = "VIDIOC_REQBUFS", + [_IOC_NR(VIDIOC_QUERYBUF)] = "VIDIOC_QUERYBUF", + [_IOC_NR(VIDIOC_G_FBUF)] = "VIDIOC_G_FBUF", + [_IOC_NR(VIDIOC_S_FBUF)] = "VIDIOC_S_FBUF", + [_IOC_NR(VIDIOC_OVERLAY)] = "VIDIOC_OVERLAY", + [_IOC_NR(VIDIOC_QBUF)] = "VIDIOC_QBUF", + [_IOC_NR(VIDIOC_DQBUF)] = "VIDIOC_DQBUF", + [_IOC_NR(VIDIOC_STREAMON)] = "VIDIOC_STREAMON", + [_IOC_NR(VIDIOC_STREAMOFF)] = "VIDIOC_STREAMOFF", + [_IOC_NR(VIDIOC_G_PARM)] = "VIDIOC_G_PARM", + [_IOC_NR(VIDIOC_S_PARM)] = "VIDIOC_S_PARM", + [_IOC_NR(VIDIOC_G_STD)] = "VIDIOC_G_STD", + [_IOC_NR(VIDIOC_S_STD)] = "VIDIOC_S_STD", + [_IOC_NR(VIDIOC_ENUMSTD)] = "VIDIOC_ENUMSTD", + [_IOC_NR(VIDIOC_ENUMINPUT)] = "VIDIOC_ENUMINPUT", + [_IOC_NR(VIDIOC_G_CTRL)] = "VIDIOC_G_CTRL", + [_IOC_NR(VIDIOC_S_CTRL)] = "VIDIOC_S_CTRL", + [_IOC_NR(VIDIOC_G_TUNER)] = "VIDIOC_G_TUNER", + [_IOC_NR(VIDIOC_S_TUNER)] = "VIDIOC_S_TUNER", + [_IOC_NR(VIDIOC_G_AUDIO)] = "VIDIOC_G_AUDIO", + [_IOC_NR(VIDIOC_S_AUDIO)] = "VIDIOC_S_AUDIO", + [_IOC_NR(VIDIOC_QUERYCTRL)] = "VIDIOC_QUERYCTRL", + [_IOC_NR(VIDIOC_QUERYMENU)] = "VIDIOC_QUERYMENU", + [_IOC_NR(VIDIOC_G_INPUT)] = "VIDIOC_G_INPUT", + [_IOC_NR(VIDIOC_S_INPUT)] = "VIDIOC_S_INPUT", + [_IOC_NR(VIDIOC_G_OUTPUT)] = "VIDIOC_G_OUTPUT", + [_IOC_NR(VIDIOC_S_OUTPUT)] = "VIDIOC_S_OUTPUT", + [_IOC_NR(VIDIOC_ENUMOUTPUT)] = "VIDIOC_ENUMOUTPUT", + [_IOC_NR(VIDIOC_G_AUDOUT)] = "VIDIOC_G_AUDOUT", + [_IOC_NR(VIDIOC_S_AUDOUT)] = "VIDIOC_S_AUDOUT", + [_IOC_NR(VIDIOC_G_MODULATOR)] = "VIDIOC_G_MODULATOR", + [_IOC_NR(VIDIOC_S_MODULATOR)] = "VIDIOC_S_MODULATOR", + [_IOC_NR(VIDIOC_G_FREQUENCY)] = "VIDIOC_G_FREQUENCY", + [_IOC_NR(VIDIOC_S_FREQUENCY)] = "VIDIOC_S_FREQUENCY", + [_IOC_NR(VIDIOC_CROPCAP)] = "VIDIOC_CROPCAP", + [_IOC_NR(VIDIOC_G_CROP)] = "VIDIOC_G_CROP", + [_IOC_NR(VIDIOC_S_CROP)] = "VIDIOC_S_CROP", + [_IOC_NR(VIDIOC_G_JPEGCOMP)] = "VIDIOC_G_JPEGCOMP", + [_IOC_NR(VIDIOC_S_JPEGCOMP)] = "VIDIOC_S_JPEGCOMP", + [_IOC_NR(VIDIOC_QUERYSTD)] = "VIDIOC_QUERYSTD", + [_IOC_NR(VIDIOC_TRY_FMT)] = "VIDIOC_TRY_FMT", + [_IOC_NR(VIDIOC_ENUMAUDIO)] = "VIDIOC_ENUMAUDIO", + [_IOC_NR(VIDIOC_ENUMAUDOUT)] = "VIDIOC_ENUMAUDOUT", + [_IOC_NR(VIDIOC_G_PRIORITY)] = "VIDIOC_G_PRIORITY", + [_IOC_NR(VIDIOC_S_PRIORITY)] = "VIDIOC_S_PRIORITY", + [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP", + [_IOC_NR(VIDIOC_LOG_STATUS)] = "VIDIOC_LOG_STATUS", + [_IOC_NR(VIDIOC_G_EXT_CTRLS)] = "VIDIOC_G_EXT_CTRLS", + [_IOC_NR(VIDIOC_S_EXT_CTRLS)] = "VIDIOC_S_EXT_CTRLS", + [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)] = "VIDIOC_TRY_EXT_CTRLS", +}; + +void v4l2_log_ioctl(unsigned long int request, void *arg, int result) +{ + const char *ioctl_str; + char buf[40]; + + if (!v4l2_log_file) + return; + + if (_IOC_TYPE(request) == 'V' && _IOC_NR(request) < ARRAY_SIZE(v4l2_ioctls)) + ioctl_str = v4l2_ioctls[_IOC_NR(request)]; + else { + snprintf(buf, sizeof(buf), "unknown request: %c %d\n", + (int)_IOC_TYPE(request), (int)_IOC_NR(request)); + ioctl_str = buf; + } + + fprintf(v4l2_log_file, "request == %s\n", ioctl_str); + + switch (request) { + case VIDIOC_G_FMT: + case VIDIOC_S_FMT: + case VIDIOC_TRY_FMT: + { + struct v4l2_format* fmt = arg; + int pixfmt = fmt->fmt.pix.pixelformat; + + if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + fprintf(v4l2_log_file, " pixelformat: %c%c%c%c %dx%d\n", + pixfmt & 0xff, + (pixfmt >> 8) & 0xff, + (pixfmt >> 16) & 0xff, + pixfmt >> 24, + fmt->fmt.pix.width, + fmt->fmt.pix.height); + fprintf(v4l2_log_file, " field: %d bytesperline: %d imagesize%d\n", + (int)fmt->fmt.pix.field, (int)fmt->fmt.pix.bytesperline, + (int)fmt->fmt.pix.sizeimage); + fprintf(v4l2_log_file, " colorspace: %d, priv: %x\n", + (int)fmt->fmt.pix.colorspace, (int)fmt->fmt.pix.priv); + } + } + break; + } + + fprintf(v4l2_log_file, "result == %d\n", result); + fflush(v4l2_log_file); +} diff --git a/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c b/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c new file mode 100644 index 000000000..bcd11c2cc --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c @@ -0,0 +1,121 @@ +/* +# open/close/ioctl/mmap/munmap library call wrapper doing format conversion +# for v4l2 applications which want to be able to simply capture bgr24 / yuv420 +# from v4l2 devices with more exotic frame formats. + +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +#include +#include +/* These headers are not needed by us, but by linux/videodev2.h, + which is broken on some systems and doesn't include them itself :( */ +#include +#include +#include +/* end broken header workaround includes */ +#include +#include + +/* Check that open/read/mmap is not a define */ +#if defined open || defined read || defined mmap +#error open/read/mmap is a prepocessor macro !! +#endif + +int open (const char *file, int oflag, ...) +{ + int fd; + struct v4l2_capability cap; + + /* original open code */ + if (oflag & O_CREAT) + { + va_list ap; + mode_t mode; + + va_start (ap, oflag); + mode = va_arg (ap, mode_t); + + fd = syscall(SYS_open, file, oflag, mode); + + va_end(ap); + } else + fd = syscall(SYS_open, file, oflag); + /* end of original open code */ + + if (fd == -1) + return fd; + + /* check if we're opening a video4linux2 device */ + if (strncmp(file, "/dev/video", 10)) + return fd; + + /* check that this is an v4l2 device, libv4l2 only supports v4l2 devices */ + if (syscall(SYS_ioctl, fd, VIDIOC_QUERYCAP, &cap)) + return fd; + + /* libv4l2 only adds functionality to capture capable devices */ + if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) + return fd; + + /* Try to Register with libv4l2 (in case of failure pass the fd to the + application as is) */ + v4l2_fd_open(fd, V4L2_ENABLE_ENUM_FMT_EMULATION); + + return fd; +} + +int close(int fd) +{ + return v4l2_close(fd); +} + +int dup(int fd) +{ + return v4l2_dup(fd); +} + +int ioctl (int fd, unsigned long int request, ...) +{ + void *arg; + va_list ap; + + va_start (ap, request); + arg = va_arg (ap, void *); + va_end (ap); + + return v4l2_ioctl (fd, request, arg); +} + +ssize_t read (int fd, void* buffer, size_t n) +{ + return v4l2_read (fd, buffer, n); +} + +void mmap(void *start, size_t length, int prot, int flags, int fd, + off_t offset) +{ + return v4l2_mmap(start, length, prot, flags, fd, offset); +} + +int munmap(void *start, size_t length) +{ + return v4l2_munmap(start, length); +} -- cgit v1.2.3 From 1e8ee38c5ab44a4b296a8d47c22691d82e7515be Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Tue, 1 Jul 2008 21:16:07 +0200 Subject: v4l2-library: libv4l1 and v4l1compat From: Hans de Goede libv4l1 is the base of the v4l1compat.so wrapper lib, which is a .so which can be LD_PRELOAD-ed and the overrules the libc's open/close/etc, and when opening /dev/videoX calls v4l1_open. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/include/libv4l1.h | 67 +++ v4l2-apps/lib/libv4l/libv4l1/Makefile | 53 ++ v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h | 74 +++ v4l2-apps/lib/libv4l/libv4l1/libv4l1.c | 825 ++++++++++++++++++++++++++++ v4l2-apps/lib/libv4l/libv4l1/log.c | 138 +++++ v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c | 88 +++ 6 files changed, 1245 insertions(+) create mode 100644 v4l2-apps/lib/libv4l/include/libv4l1.h create mode 100644 v4l2-apps/lib/libv4l/libv4l1/Makefile create mode 100644 v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h create mode 100644 v4l2-apps/lib/libv4l/libv4l1/libv4l1.c create mode 100644 v4l2-apps/lib/libv4l/libv4l1/log.c create mode 100644 v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/include/libv4l1.h b/v4l2-apps/lib/libv4l/include/libv4l1.h new file mode 100644 index 000000000..fc188c32d --- /dev/null +++ b/v4l2-apps/lib/libv4l/include/libv4l1.h @@ -0,0 +1,67 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __LIBV4L1_H +#define __LIBV4L1_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include +#include + +/* Point this to a FILE opened for writing when you want to log error and + status messages to a file, when NULL errors will get send to stderr */ +extern FILE *v4l1_log_file; + +/* Just like your regular open/close/etc, except that when opening a v4l2 + capture only device, full v4l1 emulation is done including emulating the + often not implemented in v4l2 drivers CGMBUF ioctl and v4l1 style mmap call + in userspace. + + Format conversion is done if necessary when capturing. That is if you + (try to) set a capture format which is not supported by the cam, but is + supported by libv4lconvert then SPICT will succeed and on SYNC / read the + data will be converted for you and returned in the request format. + + Note that currently libv4l1 depends on the kernel v4l1 compatibility layer + for: 1) Devices which are not capture only, 2) Emulation of many basic + v4l1 ioctl's which require no driver specific handling. + + Note that no functionality is added to v4l1 devices, so if for example an + obscure v4l1 device is opened which only supports some weird capture format + then libv4l1 will not be of any help (in this case it would be best to get + the driver converted to v4l2, as v4l2 has been designed to include weird + capture formats, like hw specific bayer compression methods). +*/ + +int v4l1_open (const char *file, int oflag, ...); +int v4l1_close(int fd); +int v4l1_dup(int fd); +int v4l1_ioctl (int fd, unsigned long int request, ...); +ssize_t v4l1_read (int fd, void* buffer, size_t n); +void *v4l1_mmap(void *start, size_t length, int prot, int flags, int fd, + off_t offset); +int v4l1_munmap(void *_start, size_t length); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/v4l2-apps/lib/libv4l/libv4l1/Makefile b/v4l2-apps/lib/libv4l/libv4l1/Makefile new file mode 100644 index 000000000..c679d35d8 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l1/Makefile @@ -0,0 +1,53 @@ +CC = gcc +LD = gcc + +CPPFLAGS = -fPIC -I../include -I../../../../linux/include + +CFLAGS := -g -O1 +CFLAGS += -Wall -W -Wno-unused -Wpointer-arith -Wstrict-prototypes + +LDFLAGS = -shared + +V4L1_OBJS = libv4l1.o log.o ../libv4l2/libv4l2.so +V4L1_LIB = libv4l1.so +V4L1COMPAT = v4l1compat.so +V4L1COMPAT_O = v4l1compat.o libv4l1.so +TARGETS = $(V4L1_LIB) $(V4L1COMPAT) +INCLUDES = ../include/libv4l1.h + +ifeq ($(LIB_RELEASE),) +LIB_RELEASE = 0 +endif + +ifeq ($(DESTDIR),) +DESTDIR = /usr/local +endif + +all: $(TARGETS) + + +$(V4L1_LIB): $(V4L1_OBJS) + +$(V4L1COMPAT): $(V4L1COMPAT_O) $(V4L1_LIB) + +install: all + mkdir -p $(DESTDIR)/include + cp $(INCLUDES) $(DESTDIR)/include + mkdir -p $(DESTDIR)/lib + cp $(V4L1_LIB).$(LIB_RELEASE) $(DESTDIR)/lib + cd $(DESTDIR)/lib && \ + ln -f -s $(V4L1_LIB).$(LIB_RELEASE) $(V4L1_LIB) + cp $(V4L1COMPAT).$(LIB_RELEASE) $(DESTDIR)/lib + cd $(DESTDIR)/lib && \ + ln -f -s $(V4L1COMPAT).$(LIB_RELEASE) $(V4L1COMPAT) + +clean:: + rm -f *.so* *.o log *~ + rm -f *.d + +%.o: %.c + $(CC) -c -MMD $(CPPFLAGS) $(CFLAGS) -o $@ $< + +%.so: + $(CC) $(LDFLAGS) -Wl,-soname,$@.$(LIB_RELEASE) -o $@.$(LIB_RELEASE) $^ + ln -f -s $@.$(LIB_RELEASE) $@ diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h b/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h new file mode 100644 index 000000000..7caff68a9 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h @@ -0,0 +1,74 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __LIBV4L1_PRIV_H +#define __LIBV4L1_PRIV_H + +#include +#include + +#define V4L1_MAX_DEVICES 16 +#define V4L1_NO_FRAMES 4 +#define V4L1_FRAME_BUF_SIZE (4096 * 4096) + +extern FILE *v4l1_log_file; + +#define V4L1_LOG_ERR(...) \ + do { \ + if (v4l1_log_file) { \ + fprintf(v4l1_log_file, "libv4l1: error " __VA_ARGS__); \ + fflush(v4l1_log_file); \ + } else \ + fprintf(stderr, "libv4l1: error " __VA_ARGS__); \ + } while(0) + +#define V4L1_LOG_WARN(...) \ + do { \ + if (v4l1_log_file) { \ + fprintf(v4l1_log_file, "libv4l1: warning " __VA_ARGS__); \ + fflush(v4l1_log_file); \ + } else \ + fprintf(stderr, "libv4l1: warning " __VA_ARGS__); \ + } while(0) + +#define V4L1_LOG(...) \ + do { \ + if (v4l1_log_file) { \ + fprintf(v4l1_log_file, "libv4l1: " __VA_ARGS__); \ + fflush(v4l1_log_file); \ + } \ + } while(0) + +struct v4l1_dev_info { + int fd; + int flags; + int open_count; + int v4l1_frame_buf_map_count; + pthread_mutex_t stream_lock; + unsigned int depth; + unsigned int v4l1_pal; /* VIDEO_PALETTE */ + unsigned int v4l2_pixfmt; /* V4L2_PIX_FMT */ + unsigned int min_width, min_height, max_width, max_height; + int width, height; + unsigned char *v4l1_frame_pointer; +}; + +/* From log.c */ +void v4l1_log_ioctl(unsigned long int request, void *arg, int result); + +#endif diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c new file mode 100644 index 000000000..24feafe4f --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c @@ -0,0 +1,825 @@ +/* +# libv4l1 userspace v4l1 api emulation for v4l2 devices + +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* MAKING CHANGES TO THIS FILE?? READ THIS FIRST!!! + + Important note to people making changes to this file: All functions + (v4l1_close, v4l1_ioctl, etc.) are designed to function as their regular + counterpart when they get passed a fd that is not "registered" by libv4l1, + there are 2 reasons for this: + 1) This allows us to get completely out of the way when dealing with non + capture only devices, or non v4l2 devices. + 2) libv4l1 is the base of the v4l1compat.so wrapper lib, which is a .so + which can be LD_PRELOAD-ed and the overrules the libc's open/close/etc, + and when opening /dev/videoX calls v4l1_open. Because we behave as the + regular counterpart when the fd is not known (instead of say throwing + an error), v4l1compat.so can simply call the v4l1_ prefixed function + for all wrapped functions. This way the wrapper does not have to keep + track of which fd's are being handled by libv4l1, as libv4l1 already + keeps track of this itself. + + This also means that libv4l1 may not use any of the regular functions + it mimics, as for example open could be a symbol in v4l1compat.so, which + in turn will call v4l1_open, so therefor v4l1_open (for example) may not + use the regular open()! +*/ + +#define _LARGEFILE64_SOURCE 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +/* These headers are not needed by us, but by linux/videodev2.h, + which is broken on some systems and doesn't include them itself :( */ +#include +#include +#include +/* end broken header workaround includes */ +#include +#include +#include +#include "libv4l1-priv.h" + +#define V4L1_SUPPORTS_ENUMINPUT 0x01 +#define V4L1_SUPPORTS_ENUMSTD 0x02 +#define V4L1_PIX_FMT_TOUCHED 0x04 +#define V4L1_PIX_SIZE_TOUCHED 0x08 + +static pthread_mutex_t v4l1_open_mutex = PTHREAD_MUTEX_INITIALIZER; +static struct v4l1_dev_info devices[V4L1_MAX_DEVICES] = { {-1}, {-1}, {-1}, + {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, + {-1} }; +static int devices_used = 0; + +static unsigned int palette_to_pixelformat(unsigned int palette) +{ + switch (palette) { + case VIDEO_PALETTE_GREY: + return V4L2_PIX_FMT_GREY; + case VIDEO_PALETTE_RGB555: + return V4L2_PIX_FMT_RGB555; + case VIDEO_PALETTE_RGB565: + return V4L2_PIX_FMT_RGB565; + case VIDEO_PALETTE_RGB24: + return V4L2_PIX_FMT_BGR24; + case VIDEO_PALETTE_RGB32: + return V4L2_PIX_FMT_BGR32; + case VIDEO_PALETTE_YUYV: + return V4L2_PIX_FMT_YUYV; + case VIDEO_PALETTE_YUV422: + return V4L2_PIX_FMT_YUYV; + case VIDEO_PALETTE_UYVY: + return V4L2_PIX_FMT_UYVY; + case VIDEO_PALETTE_YUV410P: + return V4L2_PIX_FMT_YUV410; + case VIDEO_PALETTE_YUV420: + case VIDEO_PALETTE_YUV420P: + return V4L2_PIX_FMT_YUV420; + case VIDEO_PALETTE_YUV411P: + return V4L2_PIX_FMT_YUV411P; + case VIDEO_PALETTE_YUV422P: + return V4L2_PIX_FMT_YUV422P; + } + return 0; +} + +static unsigned int pixelformat_to_palette(unsigned int pixelformat) +{ + switch (pixelformat) { + case V4L2_PIX_FMT_GREY: + return VIDEO_PALETTE_GREY; + case V4L2_PIX_FMT_RGB555: + return VIDEO_PALETTE_RGB555; + case V4L2_PIX_FMT_RGB565: + return VIDEO_PALETTE_RGB565; + case V4L2_PIX_FMT_BGR24: + return VIDEO_PALETTE_RGB24; + case V4L2_PIX_FMT_BGR32: + return VIDEO_PALETTE_RGB32; + case V4L2_PIX_FMT_YUYV: + return VIDEO_PALETTE_YUYV; + case V4L2_PIX_FMT_UYVY: + return VIDEO_PALETTE_UYVY; + case V4L2_PIX_FMT_YUV410: + case V4L2_PIX_FMT_YUV420: + return VIDEO_PALETTE_YUV420P; + case V4L2_PIX_FMT_YUV411P: + return VIDEO_PALETTE_YUV411P; + case V4L2_PIX_FMT_YUV422P: + return VIDEO_PALETTE_YUV422P; + } + return 0; +} + +static int v4l1_set_format(int index, int width, int height, + int v4l1_pal, int width_height_may_differ) +{ + int result; + unsigned int v4l2_pixfmt; + struct v4l2_format fmt2 = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; + + if (v4l1_pal != -1) { + v4l2_pixfmt = palette_to_pixelformat(v4l1_pal); + if (!v4l2_pixfmt) { + V4L1_LOG("Unknown v4l1 palette number: %d\n", v4l1_pal); + errno = EINVAL; + return -1; + } + } else { + v4l2_pixfmt = devices[index].v4l2_pixfmt; + v4l1_pal = devices[index].v4l1_pal; + } + + /* Do we need to change the resolution / format ? */ + if (width == devices[index].width && height == devices[index].height && + v4l2_pixfmt == devices[index].v4l2_pixfmt) + return 0; + + /* Get current settings, apply our changes and try the new setting */ + if ((result = v4l2_ioctl(devices[index].fd, VIDIOC_G_FMT, &fmt2))) { + int saved_err = errno; + V4L1_LOG_ERR("getting pixformat: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + + fmt2.fmt.pix.pixelformat = v4l2_pixfmt; + fmt2.fmt.pix.width = width; + fmt2.fmt.pix.height = height; + if ((result = v4l2_ioctl(devices[index].fd, VIDIOC_TRY_FMT, &fmt2))) + { + int saved_err = errno; + V4L1_LOG("error trying pixformat: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + + /* Check if we get what we asked for */ + if (fmt2.fmt.pix.pixelformat != v4l2_pixfmt || (!width_height_may_differ && + (fmt2.fmt.pix.width != width || fmt2.fmt.pix.height != height))) { + V4L1_LOG("requested fmt, width, height combo not available\n"); + errno = EINVAL; + return -1; + } + + /* Maybe after the TRY_FMT things haven't changed after all ? */ + if (fmt2.fmt.pix.width == devices[index].width && + fmt2.fmt.pix.height == devices[index].height && + fmt2.fmt.pix.pixelformat == devices[index].v4l2_pixfmt) { + devices[index].v4l1_pal = v4l1_pal; + return 0; + } + + if ((result = v4l2_ioctl(devices[index].fd, VIDIOC_S_FMT, &fmt2))) { + int saved_err = errno; + V4L1_LOG_ERR("setting pixformat: %s\n", strerror(errno)); + errno = saved_err; + return result; + } + + devices[index].width = fmt2.fmt.pix.width; + devices[index].height = fmt2.fmt.pix.height; + devices[index].v4l2_pixfmt = v4l2_pixfmt; + devices[index].v4l1_pal = v4l1_pal; + devices[index].depth = ((fmt2.fmt.pix.bytesperline << 3) + + (fmt2.fmt.pix.width - 1)) / fmt2.fmt.pix.width; + + return result; +} + +static void v4l1_find_min_and_max_size(int index, struct v4l2_format *fmt2) +{ + int i; + struct v4l2_fmtdesc fmtdesc2 = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; + + devices[index].min_width = -1; + devices[index].min_height = -1; + devices[index].max_width = 0; + devices[index].max_height = 0; + + for (i = 0; ; i++) { + fmtdesc2.index = i; + + if (syscall(SYS_ioctl, devices[index].fd, VIDIOC_ENUM_FMT, &fmtdesc2)) + break; + + fmt2->fmt.pix.pixelformat = fmtdesc2.pixelformat; + fmt2->fmt.pix.width = 48; + fmt2->fmt.pix.height = 32; + + if (syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, fmt2) == 0) { + if (fmt2->fmt.pix.width < devices[index].min_width) + devices[index].min_width = fmt2->fmt.pix.width; + if (fmt2->fmt.pix.height < devices[index].min_height) + devices[index].min_height = fmt2->fmt.pix.height; + } + + fmt2->fmt.pix.pixelformat = fmtdesc2.pixelformat; + fmt2->fmt.pix.width = 100000; + fmt2->fmt.pix.height = 100000; + + if (syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, fmt2) == 0) { + if (fmt2->fmt.pix.width > devices[index].max_width) + devices[index].max_width = fmt2->fmt.pix.width; + if (fmt2->fmt.pix.height > devices[index].max_height) + devices[index].max_height = fmt2->fmt.pix.height; + } + } +} + + +int v4l1_open (const char *file, int oflag, ...) +{ + int index, fd; + char *lfname; + struct v4l2_capability cap2; + struct v4l2_format fmt2; + struct v4l2_input input2; + struct v4l2_standard standard2; + + /* original open code */ + if (oflag & O_CREAT) + { + va_list ap; + mode_t mode; + + va_start (ap, oflag); + mode = va_arg (ap, mode_t); + + fd = syscall(SYS_open, file, oflag, mode); + + va_end(ap); + } else + fd = syscall(SYS_open, file, oflag); + /* end of original open code */ + + if (fd == -1) + return fd; + + /* check if we're opening a video4linux2 device */ + if (strncmp(file, "/dev/video", 10)) + return fd; + + /* check that this is an v4l2 device, no need to emulate v4l1 on + a v4l1 device */ + if (syscall(SYS_ioctl, fd, VIDIOC_QUERYCAP, &cap2)) + return fd; + + /* IMPROVEME */ + /* we only support simple video capture devices which do not do overlay */ + if ((cap2.capabilities & 0x0F) != V4L2_CAP_VIDEO_CAPTURE) + return fd; + + /* If no log file was set by the app, see if one was specified through the + environment */ + if (!v4l1_log_file && (lfname = getenv("LIBV4L1_LOG_FILENAME"))) + v4l1_log_file = fopen(lfname, "w"); + + /* redirect libv4l2 log messages to our logfile if no libv4l2 logfile is + specified */ + if (!v4l2_log_file) + v4l2_log_file = v4l1_log_file; + + /* Get initial width, height and pixelformat */ + fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (syscall(SYS_ioctl, fd, VIDIOC_G_FMT, &fmt2)) { + int saved_err = errno; + V4L1_LOG_ERR("getting pixformat: %s\n", strerror(errno)); + syscall(SYS_close, fd); + errno = saved_err; + return -1; + } + + /* Register with libv4l2, as we use that todo format conversion and read() + emulation for us */ + if (v4l2_fd_open(fd, V4L2_ENABLE_ENUM_FMT_EMULATION) == -1) { + int saved_err = errno; + syscall(SYS_close, fd); + errno = saved_err; + return -1; + } + + /* So we have a device on which we can (and want to) emulate v4l1, register + it in our devices array */ + pthread_mutex_lock(&v4l1_open_mutex); + for (index = 0; index < V4L1_MAX_DEVICES; index++) + if(devices[index].fd == -1) { + devices[index].fd = fd; + break; + } + pthread_mutex_unlock(&v4l1_open_mutex); + + if (index == V4L1_MAX_DEVICES) { + V4L1_LOG_ERR("attempting to open more then %d video devices\n", + V4L1_MAX_DEVICES); + v4l2_close(fd); + errno = EBUSY; + return -1; + } + + if (index >= devices_used) + devices_used = index + 1; + + devices[index].flags = 0; + devices[index].open_count = 1; + devices[index].v4l1_frame_buf_map_count = 0; + devices[index].v4l1_frame_pointer = MAP_FAILED; + devices[index].width = fmt2.fmt.pix.width; + devices[index].height = fmt2.fmt.pix.height; + devices[index].v4l2_pixfmt = fmt2.fmt.pix.pixelformat; + devices[index].v4l1_pal = pixelformat_to_palette(fmt2.fmt.pix.pixelformat); + devices[index].depth = ((fmt2.fmt.pix.bytesperline << 3) + + (fmt2.fmt.pix.width - 1)) / fmt2.fmt.pix.width; + + v4l1_find_min_and_max_size(index, &fmt2); + + /* Check ENUM_INPUT and ENUM_STD support */ + input2.index = 0; + if (v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2) == 0) + devices[index].flags |= V4L1_SUPPORTS_ENUMINPUT; + + standard2.index = 0; + if (v4l2_ioctl(fd, VIDIOC_ENUMSTD, &input2) == 0) + devices[index].flags |= V4L1_SUPPORTS_ENUMSTD; + + V4L1_LOG("open: %d\n", fd); + + return fd; +} + +/* Is this an fd for which we are emulating v4l1 ? */ +static int v4l1_get_index(int fd) +{ + int index; + + /* We never handle fd -1 */ + if (fd == -1) + return -1; + + for (index = 0; index < devices_used; index++) + if (devices[index].fd == fd) + break; + + if (index == devices_used) + return -1; + + return index; +} + + +int v4l1_close(int fd) { + int index, result; + + if ((index = v4l1_get_index(fd)) == -1) + return syscall(SYS_close, fd); + + /* Abuse stream_lock to stop 2 closes from racing and trying to free the + resources twice */ + pthread_mutex_lock(&devices[index].stream_lock); + devices[index].open_count--; + result = devices[index].open_count != 0; + pthread_mutex_unlock(&devices[index].stream_lock); + + if (result) + return v4l2_close(fd); + + /* Free resources */ + if (devices[index].v4l1_frame_pointer != MAP_FAILED) { + if (devices[index].v4l1_frame_buf_map_count) + V4L1_LOG("v4l1 capture buffer still mapped: %d times on close()\n", + devices[index].v4l1_frame_buf_map_count); + else + syscall(SYS_munmap, devices[index].v4l1_frame_pointer, + V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE); + devices[index].v4l1_frame_pointer = MAP_FAILED; + } + + /* Remove the fd from our list of managed fds before closing it, because as + soon as we've done the actual close the fd maybe returned by an open in + another thread and we don't want to intercept calls to this new fd. */ + devices[index].fd = -1; + + result = v4l2_close(fd); + + V4L1_LOG("close: %d\n", fd); + + return result; +} + +int v4l1_dup(int fd) +{ + int index; + + if ((index = v4l1_get_index(fd)) == -1) + return syscall(SYS_dup, fd); + + devices[index].open_count++; + + return v4l2_dup(fd); +} + + +int v4l1_ioctl (int fd, unsigned long int request, ...) +{ + void *arg; + va_list ap; + int result, index, saved_err, stream_locked = 0; + + va_start (ap, request); + arg = va_arg (ap, void *); + va_end (ap); + + if ((index = v4l1_get_index(fd)) == -1) + return syscall(SYS_ioctl, fd, request, arg); + + /* do we need to take the stream lock for this ioctl? */ + switch (request) { + case VIDIOCSPICT: + case VIDIOCGPICT: + case VIDIOCSWIN: + case VIDIOCGWIN: + case VIDIOCGMBUF: + case VIDIOCMCAPTURE: + case VIDIOCSYNC: + case VIDIOC_S_FMT: + pthread_mutex_lock(&devices[index].stream_lock); + stream_locked = 1; + } + + switch (request) { + + case VIDIOCGCAP: + { + struct video_capability *cap = arg; + + result = syscall(SYS_ioctl, fd, request, arg); + + /* override kernel v4l1 compat min / max size with our own more + accurate values */ + cap->minwidth = devices[index].min_width; + cap->minheight = devices[index].min_height; + cap->maxwidth = devices[index].max_width; + cap->maxheight = devices[index].max_height; + } + break; + + case VIDIOCSPICT: + { + struct video_picture *pic = arg; + + devices[index].flags |= V4L1_PIX_FMT_TOUCHED; + + v4l2_set_control(fd, V4L2_CID_BRIGHTNESS, pic->brightness); + v4l2_set_control(fd, V4L2_CID_HUE, pic->hue); + v4l2_set_control(fd, V4L2_CID_CONTRAST, pic->contrast); + v4l2_set_control(fd, V4L2_CID_SATURATION, pic->colour); + v4l2_set_control(fd, V4L2_CID_WHITENESS, pic->whiteness); + + result = v4l1_set_format(index, devices[index].width, + devices[index].height, pic->palette, 0); + } + break; + + case VIDIOCGPICT: + { + struct video_picture *pic = arg; + + /* If our v4l2 pixformat has no corresponding v4l1 palette, and the + app has not touched the pixformat sofar, try setting a palette which + does (and which we emulate when necessary) so that applications + which just query the current format and then take whatever they get + will work */ + if (!(devices[index].flags & V4L1_PIX_FMT_TOUCHED) && + !pixelformat_to_palette(devices[index].v4l2_pixfmt)) + v4l1_set_format(index, devices[index].width, + devices[index].height, + VIDEO_PALETTE_RGB24, + (devices[index].flags & + V4L1_PIX_SIZE_TOUCHED) ? 0 : 1); + + devices[index].flags |= V4L1_PIX_FMT_TOUCHED; + + pic->depth = devices[index].depth; + pic->palette = devices[index].v4l1_pal; + pic->hue = v4l2_get_control(devices[index].fd, V4L2_CID_HUE); + pic->colour = v4l2_get_control(devices[index].fd, V4L2_CID_SATURATION); + pic->contrast = v4l2_get_control(devices[index].fd, V4L2_CID_CONTRAST); + pic->whiteness = v4l2_get_control(devices[index].fd, + V4L2_CID_WHITENESS); + pic->brightness = v4l2_get_control(devices[index].fd, + V4L2_CID_BRIGHTNESS); + } + break; + + case VIDIOCSWIN: + { + struct video_window *win = arg; + + devices[index].flags |= V4L1_PIX_SIZE_TOUCHED; + + result = v4l1_set_format(index, win->width, win->height, -1, 1); + if (result == 0) { + win->width = devices[index].width; + win->height = devices[index].height; + } + } + break; + + case VIDIOCGWIN: + devices[index].flags |= V4L1_PIX_SIZE_TOUCHED; + result = syscall(SYS_ioctl, fd, request, arg); + break; + + case VIDIOCGCHAN: + { + struct v4l2_input input2; + struct video_channel *chan = arg; + + if ((devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) && + (devices[index].flags & V4L1_SUPPORTS_ENUMSTD)) { + result = syscall(SYS_ioctl, fd, request, arg); + break; + } + + /* Set some defaults */ + chan->tuners = 0; + chan->flags = 0; + chan->type = VIDEO_TYPE_CAMERA; + chan->norm = 0; + + /* In case of no ENUMSTD support, ignore the norm member of the + channel struct */ + if (devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) { + input2.index = chan->channel; + result = v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2); + if (result == 0) { + snprintf(chan->name, sizeof(chan->name), "%s", input2.name); + if (input2.type == V4L2_INPUT_TYPE_TUNER) { + chan->tuners = 1; + chan->type = VIDEO_TYPE_TV; + chan->flags = VIDEO_VC_TUNER; + } + } + break; + } + + /* No ENUMINPUT support, fake it (assume its a Camera in this case) */ + if (chan->channel == 0) { + snprintf(chan->name, sizeof(chan->name), "Camera"); + result = 0; + } else { + errno = EINVAL; + result = -1; + } + } + break; + + case VIDIOCSCHAN: + { + struct video_channel *chan = arg; + if ((devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) && + (devices[index].flags & V4L1_SUPPORTS_ENUMSTD)) { + result = syscall(SYS_ioctl, fd, request, arg); + break; + } + /* In case of no ENUMSTD support, ignore the norm member of the + channel struct */ + if (devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) { + result = v4l2_ioctl(fd, VIDIOC_S_INPUT, &chan->channel); + break; + } + /* No ENUMINPUT support, fake it (assume its a Camera in this case) */ + if (chan->channel == 0) { + result = 0; + } else { + errno = EINVAL; + result = -1; + } + } + break; + + case VIDIOCGMBUF: + /* When VIDIOCGMBUF is done, we don't necessarrily know the format the + application wants yet (with some apps this is passed for the first + time through VIDIOCMCAPTURE), so we just create an anonymous mapping + that should be large enough to hold any sort of frame. Note this only + takes virtual memory, and does not use memory until actually used. */ + { + int i; + struct video_mbuf *mbuf = arg; + + mbuf->size = V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE; + mbuf->frames = V4L1_NO_FRAMES; + for (i = 0; i < mbuf->frames; i++) { + mbuf->offsets[i] = i * V4L1_FRAME_BUF_SIZE; + } + + if (devices[index].v4l1_frame_pointer == MAP_FAILED) { + devices[index].v4l1_frame_pointer = mmap64(NULL, mbuf->size, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (devices[index].v4l1_frame_pointer == MAP_FAILED) { + saved_err = errno; + V4L1_LOG_ERR("allocating v4l1 buffer: %s\n", strerror(errno)); + errno = saved_err; + result = -1; + break; + } + V4L1_LOG("allocated v4l1 buffer @ %p\n", + devices[index].v4l1_frame_pointer); + } + result = 0; + } + break; + + case VIDIOCMCAPTURE: + { + struct video_mmap *map = arg; + + devices[index].flags |= V4L1_PIX_FMT_TOUCHED | + V4L1_PIX_SIZE_TOUCHED; + + result = v4l1_set_format(index, map->width, map->height, + map->format, 0); + } + break; + + case VIDIOCSYNC: + { + int *frame_index = arg; + + if (devices[index].v4l1_frame_pointer == MAP_FAILED || + *frame_index < 0 || *frame_index >= V4L1_NO_FRAMES) { + errno = EINVAL; + result = -1; + break; + } + + result = v4l2_read(devices[index].fd, + devices[index].v4l1_frame_pointer + + *frame_index * V4L1_FRAME_BUF_SIZE, + V4L1_FRAME_BUF_SIZE); + result = (result > 0) ? 0:result; + } + break; + + /* We are passing through v4l2 calls to libv4l2 for applications which are + using v4l2 through libv4l1 (possible with the v4l1compat.so wrapper). + + So the application could be calling VIDIOC_S_FMT, in this case update + our own bookkeeping of the cam's format. Note that this really only is + relevant if an application is mixing and matching v4l1 and v4l2 calls, + which is crazy, but better safe then sorry. */ + case VIDIOC_S_FMT: + { + struct v4l2_format *fmt2 = arg; + + result = v4l2_ioctl(fd, request, arg); + + if (result == 0 && fmt2->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + if (devices[index].v4l2_pixfmt != fmt2->fmt.pix.pixelformat) { + devices[index].v4l2_pixfmt = fmt2->fmt.pix.pixelformat; + devices[index].v4l1_pal = + pixelformat_to_palette(fmt2->fmt.pix.pixelformat); + } + devices[index].width = fmt2->fmt.pix.width; + devices[index].height = fmt2->fmt.pix.height; + } + } + break; + + default: + /* Pass through libv4l2 for applications which are using v4l2 through + libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */ + result = v4l2_ioctl(fd, request, arg); + } + + if (stream_locked) + pthread_mutex_unlock(&devices[index].stream_lock); + + saved_err = errno; + v4l1_log_ioctl(request, arg, result); + errno = saved_err; + + return result; +} + + +ssize_t v4l1_read(int fd, void* buffer, size_t n) +{ + int index; + ssize_t result; + + if ((index = v4l1_get_index(fd)) == -1) + return syscall(SYS_read, fd, buffer, n); + + pthread_mutex_lock(&devices[index].stream_lock); + result = v4l2_read(fd, buffer, n); + pthread_mutex_unlock(&devices[index].stream_lock); + + return result; +} + + +void *v4l1_mmap(void *start, size_t length, int prot, int flags, int fd, + off_t offset) +{ + int index; + void *result; + + /* Check if the mmap data matches our answer to VIDIOCGMBUF, if not + pass through libv4l2 for applications which are using v4l2 through + libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */ + if ((index = v4l1_get_index(fd)) == -1 || start || offset || + length != (V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE)) + return v4l2_mmap(start, length, prot, flags, fd, offset); + + + pthread_mutex_lock(&devices[index].stream_lock); + + /* It could be that we get called with an mmap which seems to match what + we expect, but no VIDIOCGMBUF has been done yet, then it is certainly not + for us so pass it through */ + if (devices[index].v4l1_frame_pointer == MAP_FAILED) { + result = v4l2_mmap(start, length, prot, flags, fd, offset); + goto leave; + } + + devices[index].v4l1_frame_buf_map_count++; + + V4L1_LOG("v4l1 buffer @ %p mapped by application\n", + devices[index].v4l1_frame_pointer); + + result = devices[index].v4l1_frame_pointer; + +leave: + pthread_mutex_unlock(&devices[index].stream_lock); + + return result; +} + +int v4l1_munmap(void *_start, size_t length) +{ + int index; + unsigned char *start = _start; + + /* Is this memory ours? */ + if (start != MAP_FAILED && + length == (V4L1_FRAME_BUF_SIZE * V4L1_NO_FRAMES)) { + for (index = 0; index < devices_used; index++) + if (devices[index].fd != -1 && + start == devices[index].v4l1_frame_pointer) + break; + + if (index != devices_used) { + int unmapped = 0; + + pthread_mutex_lock(&devices[index].stream_lock); + + /* Redo our checks now that we have the lock, things may have changed */ + if (start == devices[index].v4l1_frame_pointer) { + if (devices[index].v4l1_frame_buf_map_count > 0) + devices[index].v4l1_frame_buf_map_count--; + + unmapped = 1; + } + + pthread_mutex_unlock(&devices[index].stream_lock); + + if (unmapped) { + V4L1_LOG("v4l1 buffer munmap %p, %d\n", start, (int)length); + return 0; + } + } + } + + V4L1_LOG("v4l1 unknown munmap %p, %d\n", start, (int)length); + + /* If not pass through libv4l2 for applications which are using v4l2 through + libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */ + return v4l2_munmap(start, length); +} diff --git a/v4l2-apps/lib/libv4l/libv4l1/log.c b/v4l2-apps/lib/libv4l/libv4l1/log.c new file mode 100644 index 000000000..178c6d23c --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l1/log.c @@ -0,0 +1,138 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +/* These headers are not needed by us, but by linux/videodev2.h, + which is broken on some systems and doesn't include them itself :( */ +#include +#include +/* end broken header workaround includes */ +#include + +#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) + +FILE *v4l1_log_file = NULL; + +static const char *v4l1_ioctls[] = { + [_IOC_NR(VIDIOCGCAP)] = "VIDIOCGCAP", + [_IOC_NR(VIDIOCGCHAN)] = "VIDIOCGCHAN", + [_IOC_NR(VIDIOCSCHAN)] = "VIDIOCSCHAN", + [_IOC_NR(VIDIOCGTUNER)] = "VIDIOCGTUNER", + [_IOC_NR(VIDIOCSTUNER)] = "VIDIOCSTUNER", + [_IOC_NR(VIDIOCGPICT)] = "VIDIOCGPICT", + [_IOC_NR(VIDIOCSPICT)] = "VIDIOCSPICT", + [_IOC_NR(VIDIOCCAPTURE)] = "VIDIOCCAPTURE", + [_IOC_NR(VIDIOCGWIN)] = "VIDIOCGWIN", + [_IOC_NR(VIDIOCSWIN)] = "VIDIOCSWIN", + [_IOC_NR(VIDIOCGFBUF)] = "VIDIOCGFBUF", + [_IOC_NR(VIDIOCSFBUF)] = "VIDIOCSFBUF", + [_IOC_NR(VIDIOCKEY)] = "VIDIOCKEY", + [_IOC_NR(VIDIOCGFREQ)] = "VIDIOCGFREQ", + [_IOC_NR(VIDIOCSFREQ)] = "VIDIOCSFREQ", + [_IOC_NR(VIDIOCGAUDIO)] = "VIDIOCGAUDIO", + [_IOC_NR(VIDIOCSAUDIO)] = "VIDIOCSAUDIO", + [_IOC_NR(VIDIOCSYNC)] = "VIDIOCSYNC", + [_IOC_NR(VIDIOCMCAPTURE)] = "VIDIOCMCAPTURE", + [_IOC_NR(VIDIOCGMBUF)] = "VIDIOCGMBUF", + [_IOC_NR(VIDIOCGUNIT)] = "VIDIOCGUNIT", + [_IOC_NR(VIDIOCGCAPTURE)] = "VIDIOCGCAPTURE", + [_IOC_NR(VIDIOCSCAPTURE)] = "VIDIOCSCAPTURE", + [_IOC_NR(VIDIOCSPLAYMODE)] = "VIDIOCSPLAYMODE", + [_IOC_NR(VIDIOCSWRITEMODE)] = "VIDIOCSWRITEMODE", + [_IOC_NR(VIDIOCGPLAYINFO)] = "VIDIOCGPLAYINFO", + [_IOC_NR(VIDIOCSMICROCODE)] = "VIDIOCSMICROCODE", + [_IOC_NR(VIDIOCGVBIFMT)] = "VIDIOCGVBIFMT", + [_IOC_NR(VIDIOCSVBIFMT)] = "VIDIOCSVBIFMT", +}; + +void v4l1_log_ioctl(unsigned long int request, void *arg, int result) +{ + const char *ioctl_str = "unknown"; + + if (!v4l1_log_file) + return; + + /* Don't log v4l2 ioctl's as unknown we pass them to libv4l2 which will + log them for us */ + if (_IOC_TYPE(request) == 'V') + return; + + if (_IOC_TYPE(request) == 'v' && _IOC_NR(request) < ARRAY_SIZE(v4l1_ioctls)) + ioctl_str = v4l1_ioctls[_IOC_NR(request)]; + + fprintf(v4l1_log_file, "request == %s\n", ioctl_str); + + switch(request) + { + case VIDIOCGCAP:fprintf(v4l1_log_file,"name %s\n",( (struct video_capability*)arg)->name ); + fprintf(v4l1_log_file,"type %d\n",( (struct video_capability*)arg)->type ); + fprintf(v4l1_log_file,"channels %d\n",( (struct video_capability*)arg)->channels ); + fprintf(v4l1_log_file,"audios %d\n",( (struct video_capability*)arg)->audios ); + fprintf(v4l1_log_file,"maxwidth %d\n",( (struct video_capability*)arg)->maxwidth ); + fprintf(v4l1_log_file,"maxheight %d\n",( (struct video_capability*)arg)->maxheight ); + fprintf(v4l1_log_file,"minwidth %d\n",( (struct video_capability*)arg)->minwidth ); + fprintf(v4l1_log_file,"minheight %d\n",( (struct video_capability*)arg)->minheight ); + break; + case VIDIOCGWIN: + case VIDIOCSWIN: + fprintf(v4l1_log_file,"width\t%d\n", + ((struct video_window *)arg)->width); + fprintf(v4l1_log_file,"height\t%d\n", + ((struct video_window *)arg)->height); + break; + + case VIDIOCGCHAN: + case VIDIOCSCHAN: + fprintf(v4l1_log_file,"channel %d\n",( (struct video_channel*)arg)->channel ); + fprintf(v4l1_log_file,"name %s\n",( (struct video_channel*)arg)->name ); + break; + + case VIDIOCGPICT: + case VIDIOCSPICT: + fprintf(v4l1_log_file,"brightness %d\n",( (int)((struct video_picture*)arg)->brightness) ); + fprintf(v4l1_log_file,"hue %d\n",( (int)((struct video_picture*)arg)->hue) ); + fprintf(v4l1_log_file,"colour %d\n",( (int)((struct video_picture*)arg)->colour) ); + fprintf(v4l1_log_file,"contrast %d\n",( (int)((struct video_picture*)arg)->contrast) ); + fprintf(v4l1_log_file,"whiteness %d\n",( (int)((struct video_picture*)arg)->whiteness) ); + fprintf(v4l1_log_file,"depth %d\n",( (int)((struct video_picture*)arg)->depth) ); + fprintf(v4l1_log_file,"palette %d\n",( (int)((struct video_picture*)arg)->palette) ); + break; + + case VIDIOCCAPTURE: fprintf(v4l1_log_file,"on/of? %d\n", *((int *)arg) ); + break; + + case VIDIOCSYNC: fprintf(v4l1_log_file,"sync %d\n", *((int *)arg) ); + break; + + case VIDIOCMCAPTURE: + fprintf(v4l1_log_file,"frame %u\n",( (struct video_mmap*)arg)->frame ); + fprintf(v4l1_log_file,"width %d\n",( (struct video_mmap*)arg)->width ); + fprintf(v4l1_log_file,"height %d\n",( (struct video_mmap*)arg)->height ); + fprintf(v4l1_log_file,"format %u\n",( (struct video_mmap*)arg)->format ); + break; + + case VIDIOCGMBUF: + fprintf(v4l1_log_file,"size %d\n",( (struct video_mbuf*)arg)->size ); + fprintf(v4l1_log_file,"frames %d\n",( (struct video_mbuf*)arg)->frames ); + break; + } + fprintf(v4l1_log_file, "result == %d\n", result); + fflush(v4l1_log_file); +} diff --git a/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c b/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c new file mode 100644 index 000000000..3300d56d9 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c @@ -0,0 +1,88 @@ +/* +# open/close/ioctl/mmap/munmap library call wrapper doing v4l1 api emulation +# for v4l2 devices + +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +#include + +/* Check that open/read/mmap is not a define */ +#if defined open || defined read || defined mmap +#error open/read/mmap is a prepocessor macro !! +#endif + +int open (const char *file, int oflag, ...) +{ + int fd; + + if (oflag & O_CREAT) + { + va_list ap; + mode_t mode; + + va_start (ap, oflag); + mode = va_arg (ap, mode_t); + + fd = v4l1_open(file, oflag, mode); + + va_end(ap); + } else + fd = v4l1_open(file, oflag); + + return fd; +} + +int close(int fd) { + return v4l1_close(fd); +} + +int dup(int fd) +{ + return v4l1_dup(fd); +} + +int ioctl (int fd, unsigned long int request, ...) +{ + void *arg; + va_list ap; + + va_start (ap, request); + arg = va_arg (ap, void *); + va_end (ap); + + return v4l1_ioctl (fd, request, arg); +} + +ssize_t read(int fd, void* buffer, size_t n) +{ + return v4l1_read (fd, buffer, n); +} + +void mmap(void *start, size_t length, int prot, int flags, int fd, + off_t offset) +{ + return v4l1_mmap(start, length, prot, flags, fd, offset); +} + +int munmap(void *start, size_t length) +{ + return v4l1_munmap(start, length); +} -- cgit v1.2.3 From 10bc397f22cbd930c1d4d932ec929979d75865b1 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Tue, 1 Jul 2008 21:18:23 +0200 Subject: v4l2-library: global files From: Hans de Goede Global files to make the libv4l compile, install and exported into a tar.gz file Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/ChangeLog | 107 +++++++++++++++++++++++ v4l2-apps/lib/libv4l/INSTALL | 4 + v4l2-apps/lib/libv4l/Makefile | 16 ++++ v4l2-apps/lib/libv4l/README | 129 ++++++++++++++++++++++++++++ v4l2-apps/lib/libv4l/README.multi-threading | 12 +++ v4l2-apps/lib/libv4l/TODO | 26 ++++++ 6 files changed, 294 insertions(+) create mode 100644 v4l2-apps/lib/libv4l/ChangeLog create mode 100644 v4l2-apps/lib/libv4l/INSTALL create mode 100644 v4l2-apps/lib/libv4l/Makefile create mode 100644 v4l2-apps/lib/libv4l/README create mode 100644 v4l2-apps/lib/libv4l/README.multi-threading create mode 100644 v4l2-apps/lib/libv4l/TODO (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/ChangeLog b/v4l2-apps/lib/libv4l/ChangeLog new file mode 100644 index 000000000..e7b899085 --- /dev/null +++ b/v4l2-apps/lib/libv4l/ChangeLog @@ -0,0 +1,107 @@ +libv4l-0.3 +---------- +* add extern "C" magic to public header files for c++ usage (Gregor Jasny) +* Make libv4l1 and libv4l2 multithread use safe, see README.multi-threading +* Add v4lx_dup() calls (and intercept dup() from the wrappers) this fixes + use with gstreamer's v4l2 plugin (tested with cheese) +* Hopefully definitely fix compile errors on systems with a broken videodev2.h + +libv4l-0.2 +---------- +*** API change *** +* Change v4lconvert api so that the v4lconvert struct always gets allocated + by the library, this to make it opaque, so that we can avoid future API + and ABI changes +* Add support for yuv420 -> bgr24 conversion +* When converting from v4l2 pixelformat to v4l12 palette return + VIDEO_PALETTE_YUV420P instead of VIDEO_PALETTE_YUV420 for + V4L2_PIX_FMT_YUV420 as that is what most apps seem to expect +* override kernel v4l1 compat min / max size with our own more accurate values +* fix v4l1 munmap bug where it didn't recognise the buffer being unmapped was + our fake buffer (fixes gstreamer v4l1 support, checked with cheese) +* add support for reporting the emulated pixelformats with ENUM_FMT, this + defaults to off, and can be activated by passing a flag to enable it to + v4l2_fd_open. This gets enabled by default the wrappers. +* v4l2: mmap the real device buffers before doing conversion when DQBUF gets + called before the application has called mmap (avoid crash). + + +libv4l-0.1 +---------- +* major shuffle / rewrite now split into libv4l1, libv4l2, libv4lconvert + and 2 wrappers for binary compatibility +* rewritten LGPL bayer decoding +* many many other changes and fixes + + +v4l1-compat-0.6 (V4L2 apps stay working) +---------------------------------------- +* Do not go into emulation mode of rgb24 immediately, but only after a + GPICT ioctl which has not been preceded by a SPICT ioctl, AKA do not get + in the way of V4L2 read calls by doing conversion on them +* Do not get in the way of mmap calls made by V4L2 applications +* Fix swapping of red and blue in bayer -> bgr24 decode routine +* Remember the v4l1 palette asked for with SPICT and return that, as + otherwise we loose information when going v4l1 -> v4l2 -> v4l1, for example + YUV420P becomes YUV420, which are separate in v4l1. + + +v4l1-compat-0.5 (perfect camorama) +---------------------------------- +* Allow changing of format after the buffers have been mapped, by tearing + down the entire house, changing the fundament and then rebuilding it. + Now changing the capture resolution in camorama works! +* Fix jpeg decoding error reporting +* Allow jpeg's with a height which is a multiple of 8 (was 16) +* Remove a number of pretty new VIDIOCXXX -> string mappings from log.c, + fixing compiling with somewhat older kernels + + +v4l1-compat 0.4 +--------------- +* Do not even try to change the format in v4l1_compat_set_format(), unless + _really_ necessary. +* Cleanup ambigious use of src_format (no functional changes) +* Drop the mmap hack for zerocopy access under certain conditions, one of them + that the cam can deliver the requested format. Although avoiding the + memcpy in this scenarios is a good thing todo, there were several issues + with the 0.3 implementation of this, fixing all these means adding lots of + special cases all over the code. So instead we just drop support and + always do atleast a memcpy (or a conversion). If an application cannot + live with the speed penalty this imposes it should be ported to v4l2. +* Now that we've gotten rid of the zerocopy mmap hack, we can safely allow + mixing read and mmap based IO. +* Explictly include linux/ioctl.h, to fix compile with kernel headers where + linux/videodev.h doesn't. + + +v4l1-compat 0.3 +--------------- +* Don't allow multiple opens, in theory our code can handle it, but not all + v4l2 devices like it (ekiga does it and uvc doesn't like it). + + +v4l1-compat 0.2 +--------------- +* When mmap gets passed an fd of -1 (anonymous map) don't look for it in our + list of managed fds, as we use -1 to mark unused entries (fixes ekiga + crashing). Also check for an fd of -1 in the other calls we intercept. +* In close() start with removing the fd from our list of managed fds, this must + be done first, because as soon as we've done the actual close syscall, the + fd maybe returned by an open in another thread and we don't want to intercept + calls to this new fd. +* Make unknown v4l1 palette types a normal level log messages instead of an + error. +* When an applicaiton changes the width / height through the CMCAPTURE ioctl + remember the new width and height. +* If the devices initial v4l2 pixformat has no corresponding v4l1 palette, try + setting a format which does (and which we emulate when necessary) so that + applicactions which just query the current format (GPICT) and then take + whatever they get will work (partially fixes camorama) +* Implement our own SWIN instead of using kernel compat layer, for more + flexibility and better error checking + + +v4l1-compat 0.1 +--------------- +* Initial public release. diff --git a/v4l2-apps/lib/libv4l/INSTALL b/v4l2-apps/lib/libv4l/INSTALL new file mode 100644 index 000000000..b8eb042ee --- /dev/null +++ b/v4l2-apps/lib/libv4l/INSTALL @@ -0,0 +1,4 @@ +To compile simply type "make" after that you can find the compiled libraries +and wrappers under the lib dir, public headers are under the include dir. + +Sorry no make install target for now. diff --git a/v4l2-apps/lib/libv4l/Makefile b/v4l2-apps/lib/libv4l/Makefile new file mode 100644 index 000000000..78c6893a3 --- /dev/null +++ b/v4l2-apps/lib/libv4l/Makefile @@ -0,0 +1,16 @@ +LIB_RELEASE=0 +V4L2_LIB_VERSION=$(LIB_RELEASE).3 + +all clean install: + $(MAKE) -C libv4lconvert $@ + $(MAKE) -C libv4l2 $@ + $(MAKE) -C libv4l1 $@ + +export: clean + mkdir /tmp/libv4l-$(V4L2_LIB_VERSION) + cp -a . /tmp/libv4l-$(V4L2_LIB_VERSION)/ + cd /tmp/ && \ + tar cvf /tmp/libv4l-$(V4L2_LIB_VERSION).tar\ + libv4l-$(V4L2_LIB_VERSION) + gzip /tmp/libv4l-$(V4L2_LIB_VERSION).tar + rm -rf /tmp/libv4l-$(V4L2_LIB_VERSION) diff --git a/v4l2-apps/lib/libv4l/README b/v4l2-apps/lib/libv4l/README new file mode 100644 index 000000000..c588947b5 --- /dev/null +++ b/v4l2-apps/lib/libv4l/README @@ -0,0 +1,129 @@ +Introduction +------------ + +libv4l is a collection of libraries which adds a thin abstraction layer on +top of video4linux2 devices. The purpose of this (thin) layer is to make it +easy for application writers to support a wide variety of devices without +having to write seperate code for different devices in the same class. + +All libv4l components are licensed under the GNU Library General Publishing +License version 2 or (at your option) any later version. + +libv4l consists of 3 different libraries: + + +libv4lconvert +------------- + +libv4lconvert offers functions to convert from any (known) pixelformat +to V4l2_PIX_FMT_BGR24 or V4l2_PIX_FMT_YUV420. + +Currently the following source formats are supported: +jpeg, mjpeg, bayer (all 4 variants: bggr, rggb, gbrg, grbg), +spca501 (chip specific yuv 420 with interlaced components), +spca561 (chip specific compressed gbrg bayer) +For more details on the v4lconvert_ functions see libv4lconvert.h . + + +libv4l1 +------- + +This offers functions like v4l1_open, v4l1_ioctl, etc. which can by used to +quickly make v4l1 applications work with v4l2 devices. These functions work +exactly like the normal open/close/etc, except that libv4l1 does full emulation +of the v4l1 api on top of v4l2 drivers, in case of v4l1 drivers it will just +pass calls through. For more details on the v4l1_ functions see libv4l1.h . + + +libv4l2 +------- + +This offers functions like v4l2_open, v4l2_ioctl, etc. which can by used to +quickly make v4l2 applications work with v4l2 devices with weird formats. +libv4l2 mostly passes calls directly through to the v4l2 driver. When the +app does a TRY_FMT / S_FMT with a not supported format libv4l2 will get in +the middle and emulate the format (if an app wants to know which formats the +hardware can _really_ do it should use ENUM_FMT, not randomly try a bunch of +S_FMT's). For more details on the v4l2_ functions see libv4l2.h . + + +wrappers +-------- + +The functionality provided by libv4l1 for v4l1 apps and libv4l2 for v4l2 apps +can also be used by existing apps without modifying them. For this purpose +2 wrapper libraries are provided which can be preloaded before starting the +application using the LD_PRELOAD environment variable. These wrappers will +then intercept calls to open/close/ioctl/etc. and if these calls directed +towards a video device the wrapper will redirect the call to the libv4lX +counterparts. + +The preloadable libv4l1 wrapper which adds v4l2 device compatibility to v4l1 +applications is called v4l1compat.so. The preloadable libv4l1 wrapper which +adds v4l2 device compatibility to v4l1 applications is called v4l2convert.so + +Example usage: +$ export LD_LIBRARY_PATH=`pwd`/lib +$ export LD_PRELOAD=`pwd`/lib/v4l1compat.so +$ camorama + + +FAQ +--- + +Q: Why libv4l, whats wrong with directly accessing v4l2 devices ? +Q: Do we really need yet another library ? +A: Current webcam using applications like ekiga contain code to handle many +different specific pixelformats webcam's use, but that code only supports a +small subset of all native webcam (compressed) pixelformats. Other current +v4l2 applications do not support anything but rgb pixelformats (xawtv for +example) and this will not work with most webcams at all. + +With gspca being ported to v4l2 and thus decoding to normal formats being +removed from the device driver as this really belongs in userspace, ekiga +would need to be extended with many more often chip dependent formats, like +the bayer compression used by the spca561 and the (different) compression used +by the pac207 and the (again different) compression used by the sn9c102. Adding +support for all these formats should not be done at the application level, as +then it needs to be written for each application seperately. Licensing issues +with the decompressors will then also become a problem as just cut and pasting +from one application to another is bound to hit license incompatibilities. + +So clearly this belongs in a library, and in a library with a license which +allows this code to be used from as many different applications as possible. +Hence libv4l was born. + +Q: Under which license may I use and distribute libv4l? +A: All libv4l components are licensed under the GNU Library General Publishing +License version 2 or (at your option) any later version. See the included +COPYING.LIB file. + +Q: Okay so I get the use of having a libv4lconvert, but why libv4l1 ? +A: Many v4l2 drivers do not offer full v4l1 compatibility. They often do not +implemented the CGMBUF ioctl and v4l1 style mmap call. Adding support to all +these drivers for this is a lot of work and more importantly unnecessary +adds code to kernel space. + +Also even if the CGMBUF ioctl and v4l1 style mmap are supported, then most +cams still deliver pixelformats which v4l1 applications do not understand. + +This libv4l1 was born as an easy way to get v4l1 applications to work with +v4l2 devices without requiring full v4l1 emulation (including format +conversion) in the kernel, and without requiring major changes to the +applications. + + +Q: Why should I use libv4l2 in my app instead of direct device access +combined with libv4lconvert? + +libv4l2 is mainly meant for quickly and easily adding support for more +pixelformats to existing v4l2 applications. So if you feel better directly +accessing the device in combination with libv4lconvert thats fine too. + +Notice that libv4l2 also does emulation of the read() call on devices which +do not support it in the driver. In the background this uses mmap buffers +(even on devices which do support the read call). This mmap gives libv4lconvert +zero-copy access to the captured frame, and then it can write the converted +data directly to the buffer the application provided to v4l2_read(). Thus +another reason to use liv4l2 is to get the no memcpy advantage of the mmap +capture method combined with the simplicity of making a simple read() call. diff --git a/v4l2-apps/lib/libv4l/README.multi-threading b/v4l2-apps/lib/libv4l/README.multi-threading new file mode 100644 index 000000000..93b393c8c --- /dev/null +++ b/v4l2-apps/lib/libv4l/README.multi-threading @@ -0,0 +1,12 @@ +libv4lconvert is not safe for using one convert instance as returned by +v4lconvert_create from multiple threads, if you want to use one v4lconvert +instance from multiple threads you must provide your own locking and make +sure no simultanious calls are made. + +libv4l1 and libv4l2 are safe for multithread use *under* *the* *following* +*conditions* : + +* when using v4lx_fd_open, do not make any v4lx_ calls to the passed fd until + v4lx_fd_open has completed + +* all v4lx_ calls must be completed before calling v4lx_close diff --git a/v4l2-apps/lib/libv4l/TODO b/v4l2-apps/lib/libv4l/TODO new file mode 100644 index 000000000..0170e6b14 --- /dev/null +++ b/v4l2-apps/lib/libv4l/TODO @@ -0,0 +1,26 @@ +-protect open() against being called from different threads simultaniously, + we are then thread safe except for the jpeg decompression under the following + assumption: + * We assume all device setup (for a single device) is done from a single + thread + * We assume that at the time an videodev fd gets closed all other threads + which may have been using it have stopped using it. + +-add support for setting / getting the number of read buffers + +-add code to v4l2_read to not return frames more then say 5 seconds old + +-add support for libv4l1 for non pure capture (combined capture and overlay) + devices so that atleast CGMBUF emulation (but no conversion, as thats + impossible for overlays) can be done, so that it will no longer be + necessary to implement CGMBUF in the kernel for each driver. + +-add (configurable) support for alsa faking enum_fmt to libv4l2 ? + +-check v4l2_field during conversion + +-add make install target + +-add conversion from bgr24 to yuv420 + +-add v4l2_dup, make re-entrant safe (for gstreamer v4l2 support) -- cgit v1.2.3 From 9530d8bebf9a1259505bbe3b9ff237795bff3bee Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Thu, 3 Jul 2008 20:41:07 +0200 Subject: v4l2-library: libv4l: update mercurial tree to latest 0.3.1 release From: Hans de Goede This patch syncs (updates) the libv4l in mercurial with (to) the latest 0.3.1 release. - Only serialize V4L2_BUF_TYPE_VIDEO_CAPTURE type ioctls - Do not return an uninitialized variable as result code for GPICT (fixes vlc, but see below) - Add an apps-patches directory which includes: * vlc-0.8.6-libv4l1.patch, modify vlc's v4l1 plugin to directly call into libv4l1, in the end we want all apps todo this as its better then LD_PRELOAD tricks, but for vlc this is needed as vlc's plugin system causes LD_PRELOAD to not work on symbols in the plugins * camorama-0.19-fixes.patch, small bugfixes to camorama's v4l1 support, this patch only fixes _real_ bugs in camorama and does not change it to work with v4l1compat. Although it does work better with these bugs fixed :) With this patch and LD_PRELOAD=/v4l1compat.so it works flawless. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/ChangeLog | 17 ++++ v4l2-apps/lib/libv4l/libv4l1/libv4l1.c | 2 + v4l2-apps/lib/libv4l/libv4l2/libv4l2.c | 145 ++++++++++++--------------------- 3 files changed, 71 insertions(+), 93 deletions(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/ChangeLog b/v4l2-apps/lib/libv4l/ChangeLog index e7b899085..0e4a1af33 100644 --- a/v4l2-apps/lib/libv4l/ChangeLog +++ b/v4l2-apps/lib/libv4l/ChangeLog @@ -1,3 +1,20 @@ +libv4l-0.3.1 +------------ +* Only serialize V4L2_BUF_TYPE_VIDEO_CAPTURE type ioctls +* Do not return an uninitialized variable as result code for GPICT + (fixes vlc, but see below) +* Add an apps-patches directory which includes: + * vlc-0.8.6-libv4l1.patch, modify vlc's v4l1 plugin to directly call into + libv4l1, in the end we want all apps todo this as its better then + LD_PRELOAD tricks, but for vlc this is needed as vlc's plugin system + causes LD_PRELOAD to not work on symbols in the plugins + * camorama-0.19-fixes.patch, small bugfixes to camorama's v4l1 support, + this patch only fixes _real_ bugs in camorama and does not change it to + work with v4l1compat. Although it does work better with these bugs fixed + :) With this patch and LD_PRELOAD=/v4l1compat.so it works + flawless. + + libv4l-0.3 ---------- * add extern "C" magic to public header files for c++ usage (Gregor Jasny) diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c index 24feafe4f..f0604e5c9 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c @@ -532,6 +532,8 @@ int v4l1_ioctl (int fd, unsigned long int request, ...) V4L2_CID_WHITENESS); pic->brightness = v4l2_get_control(devices[index].fd, V4L2_CID_BRIGHTNESS); + + result = 0; } break; diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c index 33d9b2891..f97fa09b6 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c @@ -527,31 +527,13 @@ int v4l2_dup(int fd) return fd; } -static int v4l2_buf_ioctl_pre_check(int index, unsigned long int request, - struct v4l2_buffer *buf, int *result) -{ - if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { - *result = syscall(SYS_ioctl, devices[index].fd, request, buf); - return 1; - } - - /* IMPROVEME (maybe?) add support for userptr's? */ - if (devices[index].io != v4l2_io_mmap || - buf->memory != V4L2_MEMORY_MMAP || - buf->index >= devices[index].no_frames) { - errno = EINVAL; - *result = -1; - return 1; - } - - return 0; -} int v4l2_ioctl (int fd, unsigned long int request, ...) { void *arg; va_list ap; - int result, converting, index, saved_err, stream_locked = 0; + int result, converting, index, saved_err; + int is_capture_request = 0, stream_needs_locking = 0; va_start (ap, request); arg = va_arg (ap, void *); @@ -560,61 +542,70 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) if ((index = v4l2_get_index(fd)) == -1) return syscall(SYS_ioctl, fd, request, arg); - /* do we need to take the stream lock for this ioctl? */ + /* Is this a capture request and do we need to take the stream lock? */ switch (request) { + case VIDIOC_ENUM_FMT: + if (((struct v4l2_fmtdesc *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) + is_capture_request = 1; + break; + case VIDIOC_TRY_FMT: + if (((struct v4l2_format *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) + is_capture_request = 1; + break; case VIDIOC_S_FMT: case VIDIOC_G_FMT: + if (((struct v4l2_format *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + is_capture_request = 1; + stream_needs_locking = 1; + } + break; case VIDIOC_REQBUFS: + if (((struct v4l2_requestbuffers *)arg)->type == + V4L2_BUF_TYPE_VIDEO_CAPTURE) { + is_capture_request = 1; + stream_needs_locking = 1; + } + break; case VIDIOC_QUERYBUF: case VIDIOC_QBUF: case VIDIOC_DQBUF: + if (((struct v4l2_buffer *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + is_capture_request = 1; + stream_needs_locking = 1; + } + break; case VIDIOC_STREAMON: case VIDIOC_STREAMOFF: - pthread_mutex_lock(&devices[index].stream_lock); - stream_locked = 1; + if (*((enum v4l2_buf_type *)arg) == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + is_capture_request = 1; + stream_needs_locking = 1; + } } + if (!is_capture_request) + return syscall(SYS_ioctl, fd, request, arg); + + + if (stream_needs_locking) + pthread_mutex_lock(&devices[index].stream_lock); + converting = devices[index].src_fmt.fmt.pix.pixelformat != devices[index].dest_fmt.fmt.pix.pixelformat; switch (request) { case VIDIOC_ENUM_FMT: - { - struct v4l2_fmtdesc *fmtdesc = arg; - - if (fmtdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || - !(devices[index].flags & V4L2_ENABLE_ENUM_FMT_EMULATION)) - result = syscall(SYS_ioctl, devices[index].fd, request, arg); - else - result = v4lconvert_enum_fmt(devices[index].convert, fmtdesc); - } + result = v4lconvert_enum_fmt(devices[index].convert, arg); break; case VIDIOC_TRY_FMT: - { - struct v4l2_format *fmt = arg; - - if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || - (devices[index].flags & V4L2_DISABLE_CONVERSION)) { - result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, fmt); - break; - } - - result = v4lconvert_try_format(devices[index].convert, fmt, NULL); - } + result = v4lconvert_try_format(devices[index].convert, arg, NULL); break; case VIDIOC_S_FMT: { struct v4l2_format src_fmt, *dest_fmt = arg; - if (dest_fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { - result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_S_FMT, - dest_fmt); - break; - } - if (!memcmp(&devices[index].dest_fmt, dest_fmt, sizeof(*dest_fmt))) { result = 0; break; @@ -658,7 +649,7 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) if (v4l2_activate_read_stream(index)) V4L2_LOG_ERR( "reactivating stream after deactivate failure (AAIIEEEE)\n"); - return result; + break; } } @@ -681,11 +672,6 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) { struct v4l2_format* fmt = arg; - if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { - result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_G_FMT, fmt); - break; - } - *fmt = devices[index].dest_fmt; result = 0; } @@ -695,11 +681,6 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) { struct v4l2_requestbuffers *req = arg; - if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { - result = syscall(SYS_ioctl, devices[index].fd, request, arg); - return 1; - } - /* Don't allow mixing read / mmap io, either we control the buffers (read based io), or the app does */ if (devices[index].io == v4l2_io_read) { @@ -753,9 +734,6 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) { struct v4l2_buffer *buf = arg; - if (v4l2_buf_ioctl_pre_check(index, request, buf, &result)) - break; - /* Do a real query even when converting to let the driver fill in things like buf->field */ result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QUERYBUF, buf); @@ -768,23 +746,13 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) break; case VIDIOC_QBUF: - { - struct v4l2_buffer *buf = arg; - - if (v4l2_buf_ioctl_pre_check(index, request, buf, &result)) - break; - - result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QBUF, buf); - } + result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QBUF, arg); break; case VIDIOC_DQBUF: { struct v4l2_buffer *buf = arg; - if (v4l2_buf_ioctl_pre_check(index, request, buf, &result)) - break; - result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_DQBUF, buf); if (result || !converting) break; @@ -832,32 +800,23 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) case VIDIOC_STREAMON: case VIDIOC_STREAMOFF: - { - enum v4l2_buf_type *type = arg; - - if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { - result = syscall(SYS_ioctl, devices[index].fd, request, type); - break; - } - - if (devices[index].io != v4l2_io_mmap) { - errno = EINVAL; - result = -1; - break; - } - - if (request == VIDIOC_STREAMON) - result = v4l2_streamon(index); - else - result = v4l2_streamoff(index); + if (devices[index].io != v4l2_io_mmap) { + errno = EINVAL; + result = -1; + break; } + + if (request == VIDIOC_STREAMON) + result = v4l2_streamon(index); + else + result = v4l2_streamoff(index); break; default: result = syscall(SYS_ioctl, fd, request, arg); } - if (stream_locked) + if (stream_needs_locking) pthread_mutex_unlock(&devices[index].stream_lock); saved_err = errno; -- cgit v1.2.3 From 289901010b302b8f7cf3f1750b38a4ea702bd484 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Thu, 3 Jul 2008 20:47:43 +0200 Subject: v4l2-library: Add support for sonix (sn9c10x) bayer compression to libv4lconvert From: Hans de Goede Add support for sonix (sn9c10x) bayer compression to libv4lconvert Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/libv4lconvert/Makefile | 2 +- .../lib/libv4l/libv4lconvert/libv4lconvert-priv.h | 3 + v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c | 28 ++- v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c | 199 +++++++++++++++++++++ 4 files changed, 226 insertions(+), 6 deletions(-) create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile index cbd54f471..9be0903e3 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile +++ b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile @@ -9,7 +9,7 @@ CFLAGS += -Wall -W -Wno-unused -Wpointer-arith -Wstrict-prototypes LDFLAGS = -shared CONVERT_LIB = libv4lconvert.so -CONVERT_OBJS = libv4lconvert.o tinyjpeg.o \ +CONVERT_OBJS = libv4lconvert.o tinyjpeg.o sn9c10x.o \ jidctflt.o spca561-decompress.o rgbyuv.o spca501.o bayer.o TARGETS = $(CONVERT_LIB) INCLUDES = ../include/libv4lconvert.h diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h index ce050119e..db88bfae2 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h @@ -71,6 +71,9 @@ void v4lconvert_spca501_to_bgr24(const unsigned char *src, unsigned char *dst, void v4lconvert_decode_spca561(const unsigned char *src, unsigned char *dst, int width, int height); +void v4lconvert_decode_sn9c10x(const unsigned char *src, unsigned char *dst, + int width, int height); + void v4lconvert_bayer_to_bgr24(const unsigned char *bayer, unsigned char *rgb, int width, int height, unsigned int pixfmt); diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c index ad94c4ef3..b6d238c66 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c @@ -37,6 +37,7 @@ static const unsigned int supported_src_pixfmts[] = { V4L2_PIX_FMT_SRGGB8, V4L2_PIX_FMT_SPCA501, V4L2_PIX_FMT_SPCA561, + V4L2_PIX_FMT_SN9C10X, -1 }; @@ -314,23 +315,40 @@ int v4lconvert_convert(struct v4lconvert_data *data, dest_fmt->fmt.pix.height); break; + /* compressed bayer formats */ case V4L2_PIX_FMT_SPCA561: + case V4L2_PIX_FMT_SN9C10X: { - static unsigned char tmpbuf[640 * 480]; - v4lconvert_decode_spca561(src, tmpbuf, dest_fmt->fmt.pix.width, - dest_fmt->fmt.pix.height); + unsigned char tmpbuf[dest_fmt->fmt.pix.width*dest_fmt->fmt.pix.height]; + unsigned int bayer_fmt; + + switch (src_fmt->fmt.pix.pixelformat) { + case V4L2_PIX_FMT_SPCA561: + v4lconvert_decode_spca561(src, tmpbuf, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + bayer_fmt = V4L2_PIX_FMT_SGBRG8; + break; + case V4L2_PIX_FMT_SN9C10X: + v4lconvert_decode_sn9c10x(src, tmpbuf, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + bayer_fmt = V4L2_PIX_FMT_SGBRG8; + break; + } + if (dest_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24) v4lconvert_bayer_to_bgr24(tmpbuf, dest, dest_fmt->fmt.pix.width, - dest_fmt->fmt.pix.height, V4L2_PIX_FMT_SGBRG8); + dest_fmt->fmt.pix.height, bayer_fmt); else v4lconvert_bayer_to_yuv420(tmpbuf, dest, dest_fmt->fmt.pix.width, - dest_fmt->fmt.pix.height, V4L2_PIX_FMT_SGBRG8); + dest_fmt->fmt.pix.height, bayer_fmt); break; } + case V4L2_PIX_FMT_BGR24: /* dest must be V4L2_PIX_FMT_YUV420 then */ printf("FIXME add bgr24 -> yuv420 conversion\n"); break; + case V4L2_PIX_FMT_YUV420: /* dest must be V4L2_PIX_FMT_BGR24 then */ v4lconvert_yuv420_to_bgr24(src, dest, dest_fmt->fmt.pix.width, diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c b/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c new file mode 100644 index 000000000..28ec5fa01 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c @@ -0,0 +1,199 @@ +/* +# (C) 2008 Hans de Goede + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "libv4lconvert-priv.h" + +/* FIXME FIXME FIXME add permission notice from Bertrik Sikken to release + this code (which is his) under the LGPL */ + +#define CLAMP(x) ((x)<0?0:((x)>255)?255:(x)) + +typedef struct { + int is_abs; + int len; + int val; + int unk; +} code_table_t; + + +/* local storage */ +/* FIXME not thread safe !! */ +static code_table_t table[256]; +static int init_done = 0; + +/* global variable */ +static int sonix_unknown = 0; + +/* + sonix_decompress_init + ===================== + pre-calculates a locally stored table for efficient huffman-decoding. + + Each entry at index x in the table represents the codeword + present at the MSB of byte x. + +*/ +void sonix_decompress_init(void) +{ + int i; + int is_abs, val, len, unk; + + for (i = 0; i < 256; i++) { + is_abs = 0; + val = 0; + len = 0; + unk = 0; + if ((i & 0x80) == 0) { + /* code 0 */ + val = 0; + len = 1; + } + else if ((i & 0xE0) == 0x80) { + /* code 100 */ + val = +4; + len = 3; + } + else if ((i & 0xE0) == 0xA0) { + /* code 101 */ + val = -4; + len = 3; + } + else if ((i & 0xF0) == 0xD0) { + /* code 1101 */ + val = +11; + len = 4; + } + else if ((i & 0xF0) == 0xF0) { + /* code 1111 */ + val = -11; + len = 4; + } + else if ((i & 0xF8) == 0xC8) { + /* code 11001 */ + val = +20; + len = 5; + } + else if ((i & 0xFC) == 0xC0) { + /* code 110000 */ + val = -20; + len = 6; + } + else if ((i & 0xFC) == 0xC4) { + /* code 110001xx: unknown */ + val = 0; + len = 8; + unk = 1; + } + else if ((i & 0xF0) == 0xE0) { + /* code 1110xxxx */ + is_abs = 1; + val = (i & 0x0F) << 4; + len = 8; + } + table[i].is_abs = is_abs; + table[i].val = val; + table[i].len = len; + table[i].unk = unk; + } + + sonix_unknown = 0; + init_done = 1; +} + + +/* + sonix_decompress + ================ + decompresses an image encoded by a SN9C101 camera controller chip. + + IN width + height + inp pointer to compressed frame (with header already stripped) + OUT outp pointer to decompressed frame + + Returns 0 if the operation was successful. + Returns <0 if operation failed. + +*/ +void v4lconvert_decode_sn9c10x(const unsigned char *inp, unsigned char *outp, + int width, int height) +{ + int row, col; + int val; + int bitpos; + unsigned char code; + unsigned char *addr; + + if (!init_done) + sonix_decompress_init(); + + bitpos = 0; + for (row = 0; row < height; row++) { + + col = 0; + + /* first two pixels in first two rows are stored as raw 8-bit */ + if (row < 2) { + addr = inp + (bitpos >> 3); + code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7))); + bitpos += 8; + *outp++ = code; + + addr = inp + (bitpos >> 3); + code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7))); + bitpos += 8; + *outp++ = code; + + col += 2; + } + + while (col < width) { + /* get bitcode from bitstream */ + addr = inp + (bitpos >> 3); + code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7))); + + /* update bit position */ + bitpos += table[code].len; + + /* update code statistics */ + sonix_unknown += table[code].unk; + + /* calculate pixel value */ + val = table[code].val; + if (!table[code].is_abs) { + /* value is relative to top and left pixel */ + if (col < 2) { + /* left column: relative to top pixel */ + val += outp[-2*width]; + } + else if (row < 2) { + /* top row: relative to left pixel */ + val += outp[-2]; + } + else { + /* main area: average of left pixel and top pixel */ + val += (outp[-2] + outp[-2*width]) / 2; + } + } + + /* store pixel */ + *outp++ = CLAMP(val); + col++; + } + } +} -- cgit v1.2.3 From d51ad054ea07b49a0a44672411ba72f30e83c98f Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Thu, 3 Jul 2008 21:59:12 +0200 Subject: v4l2-library: add support to libv4l for the compressed bayer format emitted by pac207 based cams From: Hans de Goede This patch adds support to libv4l for the compressed bayer format emitted by pac207 based cams. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/libv4lconvert/Makefile | 2 +- .../lib/libv4l/libv4lconvert/libv4lconvert-priv.h | 7 + v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c | 7 + v4l2-apps/lib/libv4l/libv4lconvert/pac207.c | 421 +++++++++++++++++++++ 4 files changed, 436 insertions(+), 1 deletion(-) create mode 100644 v4l2-apps/lib/libv4l/libv4lconvert/pac207.c (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile index 9be0903e3..decec4817 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile +++ b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile @@ -9,7 +9,7 @@ CFLAGS += -Wall -W -Wno-unused -Wpointer-arith -Wstrict-prototypes LDFLAGS = -shared CONVERT_LIB = libv4lconvert.so -CONVERT_OBJS = libv4lconvert.o tinyjpeg.o sn9c10x.o \ +CONVERT_OBJS = libv4lconvert.o tinyjpeg.o sn9c10x.o pac207.o \ jidctflt.o spca561-decompress.o rgbyuv.o spca501.o bayer.o TARGETS = $(CONVERT_LIB) INCLUDES = ../include/libv4lconvert.h diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h index db88bfae2..ed1aa01ac 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h @@ -31,6 +31,10 @@ #define V4L2_PIX_FMT_SPCA561 v4l2_fourcc('S','5','6','1') #endif +#ifndef V4L2_PIX_FMT_PAC207 +#define V4L2_PIX_FMT_PAC207 v4l2_fourcc('P','2','0','7') +#endif + #ifndef V4L2_PIX_FMT_SGBRG8 #define V4L2_PIX_FMT_SGBRG8 v4l2_fourcc('G','B','R','G') #endif @@ -74,6 +78,9 @@ void v4lconvert_decode_spca561(const unsigned char *src, unsigned char *dst, void v4lconvert_decode_sn9c10x(const unsigned char *src, unsigned char *dst, int width, int height); +void v4lconvert_decode_pac207(const unsigned char *src, unsigned char *dst, + int width, int height); + void v4lconvert_bayer_to_bgr24(const unsigned char *bayer, unsigned char *rgb, int width, int height, unsigned int pixfmt); diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c index b6d238c66..7d01425ad 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c @@ -38,6 +38,7 @@ static const unsigned int supported_src_pixfmts[] = { V4L2_PIX_FMT_SPCA501, V4L2_PIX_FMT_SPCA561, V4L2_PIX_FMT_SN9C10X, + V4L2_PIX_FMT_PAC207, -1 }; @@ -318,6 +319,7 @@ int v4lconvert_convert(struct v4lconvert_data *data, /* compressed bayer formats */ case V4L2_PIX_FMT_SPCA561: case V4L2_PIX_FMT_SN9C10X: + case V4L2_PIX_FMT_PAC207: { unsigned char tmpbuf[dest_fmt->fmt.pix.width*dest_fmt->fmt.pix.height]; unsigned int bayer_fmt; @@ -333,6 +335,11 @@ int v4lconvert_convert(struct v4lconvert_data *data, dest_fmt->fmt.pix.height); bayer_fmt = V4L2_PIX_FMT_SGBRG8; break; + case V4L2_PIX_FMT_PAC207: + v4lconvert_decode_pac207(src, tmpbuf, dest_fmt->fmt.pix.width, + dest_fmt->fmt.pix.height); + bayer_fmt = V4L2_PIX_FMT_SBGGR8; + break; } if (dest_fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24) diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c b/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c new file mode 100644 index 000000000..3291a42e1 --- /dev/null +++ b/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c @@ -0,0 +1,421 @@ +/* + +# PAC207 decoder +# Bertrik.Sikken. Thomas Kaiser (C) 2005 +# Copyright (C) 2003 2004 2005 Michel Xhaard + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Note this code was originally licensed under the GNU GPL instead of the +# GNU LGPL, its license has been changed with permission, see the permission +# mails at the end of this file. + +*/ + +#include +#include "libv4lconvert-priv.h" + +#define CLIP(color) (unsigned char)(((color)>0xFF)?0xff:(((color)<0)?0:(color))) + +/* FIXME not threadsafe */ +static int decoder_initialized = 0; + +static struct { + unsigned char is_abs; + unsigned char len; + signed char val; +} table[256]; + +void init_pixart_decoder(void) +{ + int i; + int is_abs, val, len; + for (i = 0; i < 256; i++) { + is_abs = 0; + val = 0; + len = 0; + if ((i & 0xC0) == 0) { + /* code 00 */ + val = 0; + len = 2; + } else if ((i & 0xC0) == 0x40) { + /* code 01 */ + val = -5; + len = 2; + } else if ((i & 0xC0) == 0x80) { + /* code 10 */ + val = +5; + len = 2; + } else if ((i & 0xF0) == 0xC0) { + /* code 1100 */ + val = -10; + len = 4; + } else if ((i & 0xF0) == 0xD0) { + /* code 1101 */ + val = +10; + len = 4; + } else if ((i & 0xF8) == 0xE0) { + /* code 11100 */ + val = -15; + len = 5; + } else if ((i & 0xF8) == 0xE8) { + /* code 11101 */ + val = +15; + len = 5; + } else if ((i & 0xFC) == 0xF0) { + /* code 111100 */ + val = -20; + len = 6; + } else if ((i & 0xFC) == 0xF4) { + /* code 111101 */ + val = +20; + len = 6; + } else if ((i & 0xF8) == 0xF8) { + /* code 11111xxxxxx */ + is_abs = 1; + val = 0; + len = 5; + } + table[i].is_abs = is_abs; + table[i].val = val; + table[i].len = len; + } + decoder_initialized = 1; +} + +static inline unsigned char getByte(const unsigned char *inp, + unsigned int bitpos) +{ + const unsigned char *addr; + addr = inp + (bitpos >> 3); + return (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7))); +} + +static inline unsigned short getShort(const unsigned char *pt) +{ + return ((pt[0] << 8) | pt[1]); +} + +static int +pac_decompress_row(const unsigned char *inp, unsigned char *outp, int width) +{ + int col; + int val; + int bitpos; + unsigned char code; + + if (!decoder_initialized) + init_pixart_decoder(); + + /* first two pixels are stored as raw 8-bit */ + *outp++ = inp[2]; + *outp++ = inp[3]; + bitpos = 32; + + /* main decoding loop */ + for (col = 2; col < width; col++) { + /* get bitcode */ + + code = getByte(inp, bitpos); + bitpos += table[code].len; + + /* calculate pixel value */ + if (table[code].is_abs) { + /* absolute value: get 6 more bits */ + code = getByte(inp, bitpos); + bitpos += 6; + *outp++ = code & 0xFC; + } else { + /* relative to left pixel */ + val = outp[-2] + table[code].val; + *outp++ = CLIP(val); + } + } + + /* return line length, rounded up to next 16-bit word */ + return 2 * ((bitpos + 15) / 16); +} + +void v4lconvert_decode_pac207(const unsigned char *inp, unsigned char *outp, + int width, int height) +{ +/* we should received a whole frame with header and EOL marker +in myframe->data and return a GBRG pattern in frame->tmpbuffer +remove the header then copy line by line EOL is set with 0x0f 0xf0 marker +or 0x1e 0xe1 for compressed line*/ + unsigned short word; + int row; + + /* iterate over all rows */ + for (row = 0; row < height; row++) { + word = getShort(inp); + switch (word) { + case 0x0FF0: + memcpy(outp, inp + 2, width); + inp += (2 + width); + break; + case 0x1EE1: + inp += pac_decompress_row(inp, outp, width); + break; + + default: + /* FIXME add error reporting */ + /* Notice this seems to happen with high framerates (low exposure + setting due to much light and bad compressible images, so most + likely the usb just cannot keep us and we miss parts of some + frames (sometimes of many frames in a row) messing things up + completely. It might be worth to try changing the compression + balance setting to see if that can compensate for this. + However currently I cannot reproduce this (no daylight) */ + return; + } + outp += width; + } + + return; +} + + + + +/* +Return-Path: +Received: from koko.hhs.nl ([145.52.2.16] verified) + by hhs.nl (CommuniGate Pro SMTP 4.3.6) + with ESMTP id 88906346 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 01:17:00 +0200 +Received: from exim (helo=koko) + by koko.hhs.nl with local-smtp (Exim 4.62) + (envelope-from ) + id 1KBeEW-0001qu-H6 + for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 01:17:00 +0200 +Received: from [192.87.102.74] (port=41049 helo=filter6-ams.mf.surf.net) + by koko.hhs.nl with esmtp (Exim 4.62) + (envelope-from ) + id 1KBeEV-0001qn-2T + for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 01:17:00 +0200 +Received: from smtp0.lie-comtel.li (smtp0.lie-comtel.li [217.173.238.80]) + by filter6-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5PNGwSF007539 + for ; Thu, 26 Jun 2008 01:16:58 +0200 +Received: from localhost (localhost.lie-comtel.li [127.0.0.1]) + by smtp0.lie-comtel.li (Postfix) with ESMTP id DDB609FEC1D; + Thu, 26 Jun 2008 00:16:56 +0100 (GMT-1) +X-Virus-Scanned: Virus scanned by amavis at smtp.lie-comtel.li +Received: from [192.168.0.16] (217-173-228-198.cmts.powersurf.li [217.173.228.198]) + by smtp0.lie-comtel.li (Postfix) with ESMTP id 80B589FEC19; + Thu, 26 Jun 2008 00:16:56 +0100 (GMT-1) +Message-ID: <4862D211.3000802@kaiser-linux.li> +Date: Thu, 26 Jun 2008 01:17:37 +0200 +From: Thomas Kaiser +User-Agent: Thunderbird 2.0.0.14 (X11/20080505) +MIME-Version: 1.0 +To: Hans de Goede +CC: Thomas Kaiser , bertrik@zonnet.nl, + mxhaard@magic.fr +Subject: Re: pac207 bayer decompression algorithm license question +References: <4862C0A4.3060003@hhs.nl> +In-Reply-To: <4862C0A4.3060003@hhs.nl> +Content-Type: text/plain; charset=ISO-8859-1; format=flowed +Content-Transfer-Encoding: 7bit +X-Canit-CHI2: 0.00 +X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) +X-Spam-Score: 0.00 () [Tag at 8.00] +X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default) +X-Canit-Stats-ID: 88604132 - 38b3b44cd798 +X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.74 +X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 25062008 #787666, status: clean + +Hello Hans + +Hans de Goede wrote: +> Hi, +> +> As you may have seen on the mailinglist, I've created a userspace +> library to handle cam specific format handling in userspace where it +> belongs, see: +> http://hansdegoede.livejournal.com/ +Yes, I saw it on the mail list and I think it is a good idea :-) +> +> I would like to also add support for decompressing the pac207's +> compressed bayer to this lib (and remove it from the kernel driver) +> for this I need permission to relicense the decompress code under the +> LGPL (version 2 or later). +Actually, this was done by Bertrik Sikken (bertrik@zonnet.nl), Michel +Xhaard (mxhaard@magic.fr) and me. But Bertrik was the one who found out +how to decode the lines :-) +> +> Can you give me permission for this, or if the code is not yours put +> me in contact with someone who can? +For me it's no problem to release it with LGPL. Maybe you have to ask +the other one's also. +> +> Thanks & Regards, +> +> Hans + +Rgeards, Thomas +*/ + +/* +Return-Path: +Received: from koko.hhs.nl ([145.52.2.16] verified) + by hhs.nl (CommuniGate Pro SMTP 4.3.6) + with ESMTP id 88910192 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 09:15:37 +0200 +Received: from exim (helo=koko) + by koko.hhs.nl with local-smtp (Exim 4.62) + (envelope-from ) + id 1KBlhh-0006Fi-Oe + for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 09:15:37 +0200 +Received: from [194.171.167.220] (port=54180 helo=filter4-til.mf.surf.net) + by koko.hhs.nl with esmtp (Exim 4.62) + (envelope-from ) + id 1KBlhh-0006Fd-FY + for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 09:15:37 +0200 +Received: from smtp4-g19.free.fr (smtp4-g19.free.fr [212.27.42.30]) + by filter4-til.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5Q7FY1I006360 + for ; Thu, 26 Jun 2008 09:15:34 +0200 +Received: from smtp4-g19.free.fr (localhost.localdomain [127.0.0.1]) + by smtp4-g19.free.fr (Postfix) with ESMTP id 51C683EA0E7; + Thu, 26 Jun 2008 09:15:34 +0200 (CEST) +Received: from [192.168.1.11] (lns-bzn-54-82-251-105-53.adsl.proxad.net [82.251.105.53]) + by smtp4-g19.free.fr (Postfix) with ESMTP id 1149E3EA0C7; + Thu, 26 Jun 2008 09:15:34 +0200 (CEST) +From: Michel Xhaard +To: Hans de Goede +Subject: Re: pac207 bayer decompression algorithm license question +Date: Thu, 26 Jun 2008 11:15:32 +0200 +User-Agent: KMail/1.9.5 +Cc: bertrik@zonnet.nl, spca5xx@kaiser-linux.li, + "Jean-Francois Moine" +References: <48633F02.3040108@hhs.nl> +In-Reply-To: <48633F02.3040108@hhs.nl> +MIME-Version: 1.0 +Content-Type: text/plain; + charset="iso-8859-1" +Content-Transfer-Encoding: quoted-printable +Content-Disposition: inline +Message-Id: <200806261115.32909.mxhaard@magic.fr> +X-Canit-CHI2: 0.00 +X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) +X-Spam-Score: 0.00 () [Tag at 8.00] +X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default) +X-Canit-Stats-ID: 88656338 - 0dde233cb8b5 +X-Scanned-By: CanIt (www . roaringpenguin . com) on 194.171.167.220 +X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 26062008 #787720, status: clean + +Le jeudi 26 juin 2008 09:02, Hans de Goede a =E9crit=A0: +> Hi, +> +> As you may have seen on the mailinglist, I've created a userspace library +> to handle cam specific format handling in userspace, see: +> http://hansdegoede.livejournal.com/ +> +> I would like to also add support for decompressing the pac207's compressed +> bayer to this lib (and remove it from the kernel driver) and I've heard +> from Thomas Kaiser that you are a co-author of the decompression code. In +> order to add support for decompressing pac207 compressed bayer to libv4l I +> need permission to relicense the decompression code under the LGPL (versi= +on +> 2 or later). +> +> Can you give me permission for this? +> +> Thanks & Regards, +> +> Hans +> +> +> +> p.s. +> +> Thomas has already given permission. + +=46or me it is ok and a good idea for all free world familly ;-). +Bests regards +=2D-=20 +Michel Xhaard +http://mxhaard.free.fr +*/ + +/* +Return-Path: +Received: from koko.hhs.nl ([145.52.2.16] verified) + by hhs.nl (CommuniGate Pro SMTP 4.3.6) + with ESMTP id 88940205 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 22:03:30 +0200 +Received: from exim (helo=koko) + by koko.hhs.nl with local-smtp (Exim 4.62) + (envelope-from ) + id 1KBxgo-0003Dj-ET + for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 22:03:30 +0200 +Received: from [192.87.102.69] (port=51992 helo=filter1-ams.mf.surf.net) + by koko.hhs.nl with esmtp (Exim 4.62) + (envelope-from ) + id 1KBxgo-0003Dd-5i + for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 22:03:30 +0200 +Received: from pelian.kabelfoon.nl (pelian3.kabelfoon.nl [62.45.45.106]) + by filter1-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5QK3ThE007720 + for ; Thu, 26 Jun 2008 22:03:29 +0200 +Received: from [192.168.1.1] (062-015-045-062.dynamic.caiway.nl [62.45.15.62]) + by pelian.kabelfoon.nl (Postfix) with ESMTP id 9239B428100 + for ; Thu, 26 Jun 2008 22:03:29 +0200 (CEST) +Message-ID: <4863F611.80104@sikken.nl> +Date: Thu, 26 Jun 2008 22:03:29 +0200 +From: Bertrik Sikken +User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) +MIME-Version: 1.0 +To: Hans de Goede +Subject: Re: pac207 bayer decompression algorithm license question +References: <48633F02.3040108@hhs.nl> +In-Reply-To: <48633F02.3040108@hhs.nl> +X-Enigmail-Version: 0.95.6 +Content-Type: text/plain; charset=ISO-8859-1; format=flowed +Content-Transfer-Encoding: 7bit +X-Canit-CHI2: 0.00 +X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) +X-Spam-Score: 0.00 () [Tag at 8.00] +X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default) +X-Canit-Stats-ID: 88938005 - ef1f0836ffc7 +X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.69 +X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 26062008 #787877, status: clean + +Hallo Hans, + +Hans de Goede wrote: +> Hi, +> +> As you may have seen on the mailinglist, I've created a userspace +> library to +> handle cam specific format handling in userspace, see: +> http://hansdegoede.livejournal.com/ + +O leuk, zoiets is naar mijn idee precies wat er nodig is voor webcam +support onder linux. Ik ben een jaar of 3 geleden heel actief geweest +met een aantal webcams, maar doe er tegenwoordig helemaal niets meer +aan. + +> I would like to also add support for decompressing the pac207's compressed +> bayer to this lib (and remove it from the kernel driver) and I've heard +> from Thomas Kaiser that you are a co-author of the decompression code. +> In order to add support for decompressing pac207 compressed bayer to +> libv4l I need +> permission to relicense the decompression code under the LGPL (version 2 +> or later). +> +> Can you give me permission for this? + +Ja, vind ik goed. + +Vriendelijke groet, +Bertrik +*/ -- cgit v1.2.3 From 61a18e29300c234b778a7d208016096bb4d5ce07 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 07:21:55 +0200 Subject: v4l2-library: libv4l-sonix-license-permission From: Hans de Goede Add license change (GPL -> LGPL) permission notice, and don't claim copyright over code I didn't write, instead add copyright header of the original author (oops) Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c | 96 ++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 4 deletions(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c b/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c index 28ec5fa01..ba7c8d064 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c @@ -1,5 +1,6 @@ /* -# (C) 2008 Hans de Goede +# sonix decoder +# Bertrik.Sikken. (C) 2005 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by @@ -14,13 +15,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Note this code was originally licensed under the GNU GPL instead of the +# GNU LGPL, its license has been changed with permission, see the permission +# mail at the end of this file. */ #include "libv4lconvert-priv.h" -/* FIXME FIXME FIXME add permission notice from Bertrik Sikken to release - this code (which is his) under the LGPL */ - #define CLAMP(x) ((x)<0?0:((x)>255)?255:(x)) typedef struct { @@ -197,3 +199,89 @@ void v4lconvert_decode_sn9c10x(const unsigned char *inp, unsigned char *outp, } } } + +/* +Return-Path: +Received: from koko.hhs.nl ([145.52.2.16] verified) + by hhs.nl (CommuniGate Pro SMTP 4.3.6) + with ESMTP id 89132066 for j.w.r.degoede@hhs.nl; Thu, 03 Jul 2008 15:19:55 +0200 +Received: from exim (helo=koko) + by koko.hhs.nl with local-smtp (Exim 4.62) + (envelope-from ) + id 1KEOj5-0000nq-KR + for j.w.r.degoede@hhs.nl; Thu, 03 Jul 2008 15:19:55 +0200 +Received: from [192.87.102.69] (port=33783 helo=filter1-ams.mf.surf.net) + by koko.hhs.nl with esmtp (Exim 4.62) + (envelope-from ) + id 1KEOj5-0000nj-7r + for j.w.r.degoede@hhs.nl; Thu, 03 Jul 2008 15:19:55 +0200 +Received: from cardassian.kabelfoon.nl (cardassian3.kabelfoon.nl [62.45.45.105]) + by filter1-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m63DJsKW032598 + for ; Thu, 3 Jul 2008 15:19:54 +0200 +Received: from [192.168.1.1] (044-013-045-062.dynamic.caiway.nl [62.45.13.44]) + by cardassian.kabelfoon.nl (Postfix) with ESMTP id 77761341D9A + for ; Thu, 3 Jul 2008 15:19:54 +0200 (CEST) +Message-ID: <486CD1F9.8000307@sikken.nl> +Date: Thu, 03 Jul 2008 15:19:53 +0200 +From: Bertrik Sikken +User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) +MIME-Version: 1.0 +To: Hans de Goede +Subject: Re: pac207 bayer decompression algorithm license question +References: <48633F02.3040108@hhs.nl> <4863F611.80104@sikken.nl> <486CC6AF.7050509@hhs.nl> +In-Reply-To: <486CC6AF.7050509@hhs.nl> +X-Enigmail-Version: 0.95.6 +Content-Type: text/plain; charset=ISO-8859-1; format=flowed +Content-Transfer-Encoding: 7bit +X-Canit-CHI2: 0.00 +X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) +X-Spam-Score: 0.00 () [Tag at 8.00] +X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default) +X-Canit-Stats-ID: 90943081 - 6a9ff19e8165 +X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.69 +X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 03072008 #811719, status: clean + +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +Hans de Goede wrote: +| Bertrik Sikken wrote: +|> Hallo Hans, +|> +|> Hans de Goede wrote: +|>> I would like to also add support for decompressing the pac207's +|>> compressed +|>> bayer to this lib (and remove it from the kernel driver) and I've +|>> heard from Thomas Kaiser that you are a co-author of the +|>> decompression code. In order to add support for decompressing pac207 +|>> compressed bayer to libv4l I need +|>> permission to relicense the decompression code under the LGPL +|>> (version 2 or later). +|>> +|>> Can you give me permission for this? +|> +|> Ja, vind ik goed. +|> +| +| Thanks! +| +| I'm currently working on adding support for the sn9c10x bayer +| compression to libv4l too, and I noticed this was written by you too. +| +| May I have your permission to relicense the sn9c10x bayer decompression +| code under the LGPL (version 2 or later)? + +I hereby grant you permission to relicense the sn9c10x bayer +decompression code under the LGPL (version 2 or later). + +Kind regards, +Bertrik +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.4.7 (MingW32) +Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org + +iD8DBQFIbNH5ETD6mlrWxPURAipvAJ9sv1ZpHyb81NMFejr6x0wqHX3i7QCfRDoB +jZi2e5lUjEh5KvS0dqXbi9I= +=KQfR +-----END PGP SIGNATURE----- +*/ -- cgit v1.2.3 From 47ca18f618040bcbca758432b8d8a28af5ac3bc8 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 19:23:56 +0200 Subject: v4l2-library: libv4l-pac207-comments.patch From: Hans de Goede Add comments to pac207.c about what todays experiments have teached us about the pac207 compression. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/libv4lconvert/pac207.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c b/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c index 3291a42e1..085d7a772 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/pac207.c @@ -170,15 +170,12 @@ or 0x1e 0xe1 for compressed line*/ inp += pac_decompress_row(inp, outp, width); break; - default: + case 0x2DD2: /* prefix for "stronger" compressed lines, currently the + kernel driver programs the cam so that we should not + get any of these */ + + default: /* corrupt frame */ /* FIXME add error reporting */ - /* Notice this seems to happen with high framerates (low exposure - setting due to much light and bad compressible images, so most - likely the usb just cannot keep us and we miss parts of some - frames (sometimes of many frames in a row) messing things up - completely. It might be worth to try changing the compression - balance setting to see if that can compensate for this. - However currently I cannot reproduce this (no daylight) */ return; } outp += width; -- cgit v1.2.3 From a2ba6df565553cab552748eed038e5ed50a83fe8 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 19:26:40 +0200 Subject: v4l2-library: libv4l-update-documentation.patch From: Hans de Goede Some documentation updates to bring the documentation up2date with the latest changes. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/INSTALL | 4 ---- v4l2-apps/lib/libv4l/README | 14 +++++++++++--- v4l2-apps/lib/libv4l/TODO | 14 -------------- 3 files changed, 11 insertions(+), 21 deletions(-) delete mode 100644 v4l2-apps/lib/libv4l/INSTALL (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/INSTALL b/v4l2-apps/lib/libv4l/INSTALL deleted file mode 100644 index b8eb042ee..000000000 --- a/v4l2-apps/lib/libv4l/INSTALL +++ /dev/null @@ -1,4 +0,0 @@ -To compile simply type "make" after that you can find the compiled libraries -and wrappers under the lib dir, public headers are under the include dir. - -Sorry no make install target for now. diff --git a/v4l2-apps/lib/libv4l/README b/v4l2-apps/lib/libv4l/README index c588947b5..e795e7d81 100644 --- a/v4l2-apps/lib/libv4l/README +++ b/v4l2-apps/lib/libv4l/README @@ -62,12 +62,20 @@ The preloadable libv4l1 wrapper which adds v4l2 device compatibility to v4l1 applications is called v4l1compat.so. The preloadable libv4l1 wrapper which adds v4l2 device compatibility to v4l1 applications is called v4l2convert.so -Example usage: -$ export LD_LIBRARY_PATH=`pwd`/lib -$ export LD_PRELOAD=`pwd`/lib/v4l1compat.so +Example usage (after install in default location): +$ export LD_PRELOAD=/usr/local/lib/v4l1compat.so $ camorama +Installation Instructions +------------------------- + +Simple type the following commands from the libv4l-x.y.z directory +(adjusting DESTDIR as desired): +make +make install DESTDIR=/usr/local + + FAQ --- diff --git a/v4l2-apps/lib/libv4l/TODO b/v4l2-apps/lib/libv4l/TODO index 0170e6b14..f3f9ff527 100644 --- a/v4l2-apps/lib/libv4l/TODO +++ b/v4l2-apps/lib/libv4l/TODO @@ -1,11 +1,3 @@ --protect open() against being called from different threads simultaniously, - we are then thread safe except for the jpeg decompression under the following - assumption: - * We assume all device setup (for a single device) is done from a single - thread - * We assume that at the time an videodev fd gets closed all other threads - which may have been using it have stopped using it. - -add support for setting / getting the number of read buffers -add code to v4l2_read to not return frames more then say 5 seconds old @@ -15,12 +7,6 @@ impossible for overlays) can be done, so that it will no longer be necessary to implement CGMBUF in the kernel for each driver. --add (configurable) support for alsa faking enum_fmt to libv4l2 ? - -check v4l2_field during conversion --add make install target - -add conversion from bgr24 to yuv420 - --add v4l2_dup, make re-entrant safe (for gstreamer v4l2 support) -- cgit v1.2.3 From 40536a32a310338da9cf2c697dd978780aeda18a Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 19:28:49 +0200 Subject: v4l2-library: libv4l-makefile-improvements.patch From: Hans de Goede Makefile improvements: * Split DESTDIR into DESTDIR and PREFIX as used in most makefiles out there * Add LIBDIR variable to allow installation in /lib64 for example * Install the wrappers in /libv4l instead of directly under libdir, as they are not libraries meant for linking * preserve timestamps of header files when installing them Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/README | 14 +++++++++----- v4l2-apps/lib/libv4l/libv4l1/Makefile | 23 +++++++++++++---------- v4l2-apps/lib/libv4l/libv4l2/Makefile | 23 +++++++++++++---------- v4l2-apps/lib/libv4l/libv4lconvert/Makefile | 18 +++++++++++------- 4 files changed, 46 insertions(+), 32 deletions(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/README b/v4l2-apps/lib/libv4l/README index e795e7d81..b9b056f66 100644 --- a/v4l2-apps/lib/libv4l/README +++ b/v4l2-apps/lib/libv4l/README @@ -59,11 +59,12 @@ towards a video device the wrapper will redirect the call to the libv4lX counterparts. The preloadable libv4l1 wrapper which adds v4l2 device compatibility to v4l1 -applications is called v4l1compat.so. The preloadable libv4l1 wrapper which -adds v4l2 device compatibility to v4l1 applications is called v4l2convert.so +applications is called v4l1compat.so. The preloadable libv4l2 wrapper which +adds support for various pixelformats to v4l2 applications is called +v4l2convert.so. Example usage (after install in default location): -$ export LD_PRELOAD=/usr/local/lib/v4l1compat.so +$ export LD_PRELOAD=/usr/local/lib/libv4l/v4l1compat.so $ camorama @@ -71,9 +72,12 @@ Installation Instructions ------------------------- Simple type the following commands from the libv4l-x.y.z directory -(adjusting DESTDIR as desired): +(adjusting PREFIX as desired): make -make install DESTDIR=/usr/local +make install PREFIX=/usr/local + +Note: make install also supports the DESTDIR=... paramter for installation +into chroots. FAQ diff --git a/v4l2-apps/lib/libv4l/libv4l1/Makefile b/v4l2-apps/lib/libv4l/libv4l1/Makefile index c679d35d8..8cb064cd4 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/Makefile +++ b/v4l2-apps/lib/libv4l/libv4l1/Makefile @@ -19,8 +19,12 @@ ifeq ($(LIB_RELEASE),) LIB_RELEASE = 0 endif -ifeq ($(DESTDIR),) -DESTDIR = /usr/local +ifeq ($(PREFIX),) +PREFIX = /usr/local +endif + +ifeq ($(LIBDIR),) +LIBDIR = $(PREFIX)/lib endif all: $(TARGETS) @@ -31,15 +35,14 @@ $(V4L1_LIB): $(V4L1_OBJS) $(V4L1COMPAT): $(V4L1COMPAT_O) $(V4L1_LIB) install: all - mkdir -p $(DESTDIR)/include - cp $(INCLUDES) $(DESTDIR)/include - mkdir -p $(DESTDIR)/lib - cp $(V4L1_LIB).$(LIB_RELEASE) $(DESTDIR)/lib - cd $(DESTDIR)/lib && \ + mkdir -p $(DESTDIR)$(PREFIX)/include + install -p -m 644 $(INCLUDES) $(DESTDIR)$(PREFIX)/include + mkdir -p $(DESTDIR)$(LIBDIR)/libv4l + install -m 755 $(V4L1_LIB).$(LIB_RELEASE) $(DESTDIR)$(LIBDIR) + cd $(DESTDIR)$(LIBDIR) && \ ln -f -s $(V4L1_LIB).$(LIB_RELEASE) $(V4L1_LIB) - cp $(V4L1COMPAT).$(LIB_RELEASE) $(DESTDIR)/lib - cd $(DESTDIR)/lib && \ - ln -f -s $(V4L1COMPAT).$(LIB_RELEASE) $(V4L1COMPAT) + install -m 755 $(V4L1COMPAT).$(LIB_RELEASE) \ + $(DESTDIR)$(LIBDIR)/libv4l/$(V4L1COMPAT) clean:: rm -f *.so* *.o log *~ diff --git a/v4l2-apps/lib/libv4l/libv4l2/Makefile b/v4l2-apps/lib/libv4l/libv4l2/Makefile index c5463800b..1258e379b 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/Makefile +++ b/v4l2-apps/lib/libv4l/libv4l2/Makefile @@ -19,8 +19,12 @@ ifeq ($(LIB_RELEASE),) LIB_RELEASE = 0 endif -ifeq ($(DESTDIR),) -DESTDIR = /usr/local +ifeq ($(PREFIX),) +PREFIX = /usr/local +endif + +ifeq ($(LIBDIR),) +LIBDIR = $(PREFIX)/lib endif all: $(TARGETS) @@ -30,15 +34,14 @@ $(V4L2_LIB): $(V4L2_OBJS) $(V4L2CONVERT): $(V4L2CONVERT_O) $(V4L2_LIB) install: all - mkdir -p $(DESTDIR)/include - cp $(INCLUDES) $(DESTDIR)/include - mkdir -p $(DESTDIR)/lib - cp $(V4L2_LIB).$(LIB_RELEASE) $(DESTDIR)/lib - cd $(DESTDIR)/lib && \ + mkdir -p $(DESTDIR)$(PREFIX)/include + install -p -m 644 $(INCLUDES) $(DESTDIR)$(PREFIX)/include + mkdir -p $(DESTDIR)$(LIBDIR)/libv4l + install -m 755 $(V4L2_LIB).$(LIB_RELEASE) $(DESTDIR)$(LIBDIR) + cd $(DESTDIR)$(LIBDIR) && \ ln -f -s $(V4L2_LIB).$(LIB_RELEASE) $(V4L2_LIB) - cp $(V4L2CONVERT).$(LIB_RELEASE) $(DESTDIR)/lib - cd $(DESTDIR)/lib && \ - ln -f -s $(V4L2CONVERT).$(LIB_RELEASE) $(V4L2CONVERT) + install -m 755 $(V4L2CONVERT).$(LIB_RELEASE) \ + $(DESTDIR)$(LIBDIR)/libv4l/$(V4L2CONVERT) clean:: rm -f *.so* *.o log *~ diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile index decec4817..38071de94 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/Makefile +++ b/v4l2-apps/lib/libv4l/libv4lconvert/Makefile @@ -18,8 +18,12 @@ ifeq ($(LIB_RELEASE),) LIB_RELEASE = 0 endif -ifeq ($(DESTDIR),) -DESTDIR = /usr/local +ifeq ($(PREFIX),) +PREFIX = /usr/local +endif + +ifeq ($(LIBDIR),) +LIBDIR = $(PREFIX)/lib endif all: $(TARGETS) @@ -27,11 +31,11 @@ all: $(TARGETS) $(CONVERT_LIB): $(CONVERT_OBJS) install: all - mkdir -p $(DESTDIR)/include - cp $(INCLUDES) $(DESTDIR)/include - mkdir -p $(DESTDIR)/lib - cp $(CONVERT_LIB).$(LIB_RELEASE) $(DESTDIR)/lib - cd $(DESTDIR)/lib && \ + mkdir -p $(DESTDIR)$(PREFIX)/include + install -p -m 644 $(INCLUDES) $(DESTDIR)$(PREFIX)/include + mkdir -p $(DESTDIR)$(LIBDIR) + install -m 755 $(CONVERT_LIB).$(LIB_RELEASE) $(DESTDIR)$(LIBDIR) + cd $(DESTDIR)$(LIBDIR) && \ ln -f -s $(CONVERT_LIB).$(LIB_RELEASE) $(CONVERT_LIB) clean:: -- cgit v1.2.3 From 63c4e4bef7b1537de4e313ee9d20907a372d8f56 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 19:30:13 +0200 Subject: v4l2-library: libv4l-warnings.patch From: Hans de Goede Fix all compiler warnings in libv4l Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h | 2 +- v4l2-apps/lib/libv4l/libv4l1/libv4l1.c | 13 ++++++----- v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c | 2 +- v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h | 2 +- v4l2-apps/lib/libv4l/libv4l2/libv4l2.c | 13 ++++++----- v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c | 2 +- .../lib/libv4l/libv4lconvert/libv4lconvert-priv.h | 2 +- v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c | 27 +++++++++++----------- v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c | 2 +- .../lib/libv4l/libv4lconvert/spca561-decompress.c | 24 +++++++++---------- 10 files changed, 45 insertions(+), 44 deletions(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h b/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h index 7caff68a9..e107581d1 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1-priv.h @@ -64,7 +64,7 @@ struct v4l1_dev_info { unsigned int v4l1_pal; /* VIDEO_PALETTE */ unsigned int v4l2_pixfmt; /* V4L2_PIX_FMT */ unsigned int min_width, min_height, max_width, max_height; - int width, height; + unsigned int width, height; unsigned char *v4l1_frame_pointer; }; diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c index f0604e5c9..7f5e79e3d 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c @@ -70,9 +70,10 @@ #define V4L1_PIX_SIZE_TOUCHED 0x08 static pthread_mutex_t v4l1_open_mutex = PTHREAD_MUTEX_INITIALIZER; -static struct v4l1_dev_info devices[V4L1_MAX_DEVICES] = { {-1}, {-1}, {-1}, - {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, - {-1} }; +static struct v4l1_dev_info devices[V4L1_MAX_DEVICES] = { { .fd = -1 }, + { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, + { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, + { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }}; static int devices_used = 0; static unsigned int palette_to_pixelformat(unsigned int palette) @@ -135,8 +136,8 @@ static unsigned int pixelformat_to_palette(unsigned int pixelformat) return 0; } -static int v4l1_set_format(int index, int width, int height, - int v4l1_pal, int width_height_may_differ) +static int v4l1_set_format(int index, unsigned int width, + unsigned int height, int v4l1_pal, int width_height_may_differ) { int result; unsigned int v4l2_pixfmt; @@ -748,7 +749,7 @@ ssize_t v4l1_read(int fd, void* buffer, size_t n) void *v4l1_mmap(void *start, size_t length, int prot, int flags, int fd, - off_t offset) + __off_t offset) { int index; void *result; diff --git a/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c b/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c index 3300d56d9..4a4e5c253 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c +++ b/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c @@ -77,7 +77,7 @@ ssize_t read(int fd, void* buffer, size_t n) } void mmap(void *start, size_t length, int prot, int flags, int fd, - off_t offset) + __off_t offset) { return v4l1_mmap(start, length, prot, flags, fd, offset); } diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h b/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h index bdbd45e5c..e4edf91f2 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h @@ -27,7 +27,7 @@ /* Warning when making this larger the frame_queued and frame_mapped members of the v4l2_dev_info struct can no longer be a bitfield, so the code needs to be adjusted! */ -#define V4L2_MAX_NO_FRAMES 32u +#define V4L2_MAX_NO_FRAMES 32 #define V4L2_DEFAULT_NREADBUFFERS 4 #define V4L2_FRAME_BUF_SIZE (4096 * 4096) diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c index f97fa09b6..0ed6ebdc6 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c @@ -80,9 +80,10 @@ #define V4L2_MMAP_OFFSET_MAGIC 0xABCDEF00u static pthread_mutex_t v4l2_open_mutex = PTHREAD_MUTEX_INITIALIZER; -static struct v4l2_dev_info devices[V4L2_MAX_DEVICES] = { {-1}, {-1}, - {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, {-1}, - {-1}, {-1}}; +static struct v4l2_dev_info devices[V4L2_MAX_DEVICES] = { { .fd = -1 }, + { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, + { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, + { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }}; static int devices_used = 0; @@ -830,7 +831,7 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) ssize_t v4l2_read (int fd, void* buffer, size_t n) { ssize_t result; - int index, bytesused, frame_index; + int index, bytesused = 0, frame_index; if ((index = v4l2_get_index(fd)) == -1) return syscall(SYS_read, fd, buffer, n); @@ -867,7 +868,7 @@ leave: } void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, - off_t offset) + __off_t offset) { int index; unsigned int buffer_index; @@ -877,7 +878,7 @@ void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, /* Check if the mmap data matches our answer to QUERY_BUF, if it doesn't let the kernel handle it (to allow for mmap based non capture use) */ start || length != V4L2_FRAME_BUF_SIZE || - (offset & ~0xff) != V4L2_MMAP_OFFSET_MAGIC) { + ((unsigned int)offset & ~0xFFu) != V4L2_MMAP_OFFSET_MAGIC) { if (index != -1) V4L2_LOG("Passing mmap(%p, %d, ..., %x, through to the driver\n", start, (int)length, (int)offset); diff --git a/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c b/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c index bcd11c2cc..67f563602 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c +++ b/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c @@ -110,7 +110,7 @@ ssize_t read (int fd, void* buffer, size_t n) } void mmap(void *start, size_t length, int prot, int flags, int fd, - off_t offset) + __off_t offset) { return v4l2_mmap(start, length, prot, flags, fd, offset); } diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h index ed1aa01ac..badb89d62 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert-priv.h @@ -57,7 +57,7 @@ struct v4lconvert_data { int fd; int supported_src_formats; /* bitfield */ - int no_formats; + unsigned int no_formats; char error_msg[V4LCONVERT_ERROR_MSG_SIZE]; struct jdec_private *jdec; }; diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c index 7d01425ad..35fd52481 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c @@ -25,6 +25,7 @@ #include "libv4lconvert-priv.h" #define MIN(a,b) (((a)<(b))?(a):(b)) +#define ARRAY_SIZE(x) ((int)sizeof(x)/(int)sizeof((x)[0])) static const unsigned int supported_src_pixfmts[] = { V4L2_PIX_FMT_BGR24, @@ -39,13 +40,11 @@ static const unsigned int supported_src_pixfmts[] = { V4L2_PIX_FMT_SPCA561, V4L2_PIX_FMT_SN9C10X, V4L2_PIX_FMT_PAC207, - -1 }; static const unsigned int supported_dst_pixfmts[] = { V4L2_PIX_FMT_BGR24, V4L2_PIX_FMT_YUV420, - -1 }; @@ -69,7 +68,7 @@ struct v4lconvert_data *v4lconvert_create(int fd) if (syscall(SYS_ioctl, fd, VIDIOC_ENUM_FMT, &fmt)) break; - for (j = 0; supported_src_pixfmts[j] != -1; j++) + for (j = 0; j < ARRAY_SIZE(supported_src_pixfmts); j++) if (fmt.pixelformat == supported_src_pixfmts[j]) { data->supported_src_formats |= 1 << j; break; @@ -100,7 +99,7 @@ int v4lconvert_enum_fmt(struct v4lconvert_data *data, struct v4l2_fmtdesc *fmt) return syscall(SYS_ioctl, data->fd, VIDIOC_ENUM_FMT, fmt); fmt->flags = 0; - fmt->pixelformat = -1; + fmt->pixelformat = 0; memset(fmt->reserved, 0, 4); /* Note bgr24 and yuv420 are the first 2 in our mask of supported formats */ @@ -121,7 +120,7 @@ int v4lconvert_enum_fmt(struct v4lconvert_data *data, struct v4l2_fmtdesc *fmt) } break; } - if (fmt->pixelformat == -1) { + if (fmt->pixelformat == 0) { errno = EINVAL; return -1; } @@ -134,16 +133,16 @@ int v4lconvert_try_format(struct v4lconvert_data *data, struct v4l2_format *dest_fmt, struct v4l2_format *src_fmt) { int i; - unsigned int try_pixfmt, closest_fmt_size_diff = -1; + unsigned int closest_fmt_size_diff = -1; unsigned int desired_pixfmt = dest_fmt->fmt.pix.pixelformat; - struct v4l2_format try_fmt, closest_fmt = { .type = -1 }; + struct v4l2_format try_fmt, closest_fmt = { .type = 0 }; - for (i = 0; supported_dst_pixfmts[i] != -1; i++) + for (i = 0; i < ARRAY_SIZE(supported_dst_pixfmts); i++) if (supported_dst_pixfmts[i] == desired_pixfmt) break; /* Can we do conversion to the requested format & type? */ - if (supported_dst_pixfmts[i] == -1 || + if (i == ARRAY_SIZE(supported_dst_pixfmts) || dest_fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { int ret = syscall(SYS_ioctl, data->fd, VIDIOC_TRY_FMT, dest_fmt); if (src_fmt) @@ -151,17 +150,17 @@ int v4lconvert_try_format(struct v4lconvert_data *data, return ret; } - for (i = 0; (try_pixfmt = supported_src_pixfmts[i]) != -1; i++) { + for (i = 0; i < ARRAY_SIZE(supported_src_pixfmts); i++) { /* is this format supported? */ if (!(data->supported_src_formats & (1 << i))) continue; try_fmt = *dest_fmt; - try_fmt.fmt.pix.pixelformat = try_pixfmt; + try_fmt.fmt.pix.pixelformat = supported_src_pixfmts[i]; if (!syscall(SYS_ioctl, data->fd, VIDIOC_TRY_FMT, &try_fmt)) { - if (try_fmt.fmt.pix.pixelformat == try_pixfmt) { + if (try_fmt.fmt.pix.pixelformat == supported_src_pixfmts[i]) { int size_x_diff = abs((int)try_fmt.fmt.pix.width - (int)dest_fmt->fmt.pix.width); int size_y_diff = abs((int)try_fmt.fmt.pix.height - @@ -176,7 +175,7 @@ int v4lconvert_try_format(struct v4lconvert_data *data, } } - if (closest_fmt.type == -1) { + if (closest_fmt.type == 0) { errno = EINVAL; return -1; } @@ -322,7 +321,7 @@ int v4lconvert_convert(struct v4lconvert_data *data, case V4L2_PIX_FMT_PAC207: { unsigned char tmpbuf[dest_fmt->fmt.pix.width*dest_fmt->fmt.pix.height]; - unsigned int bayer_fmt; + unsigned int bayer_fmt = 0; switch (src_fmt->fmt.pix.pixelformat) { case V4L2_PIX_FMT_SPCA561: diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c b/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c index ba7c8d064..b23ad4630 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/sn9c10x.c @@ -139,7 +139,7 @@ void v4lconvert_decode_sn9c10x(const unsigned char *inp, unsigned char *outp, int val; int bitpos; unsigned char code; - unsigned char *addr; + const unsigned char *addr; if (!init_done) sonix_decompress_init(); diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c b/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c index 1f7c6dd2f..40b0f90db 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/spca561-decompress.c @@ -307,7 +307,7 @@ static int internal_spca561_decode(int width, int height, static int accum[8 * 8 * 8]; static int i_hits[8 * 8 * 8]; - const static int nbits_A[] = + const int nbits_A[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -335,7 +335,7 @@ static int internal_spca561_decode(int width, int height, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, }; - const static int tab_A[] = + const int tab_A[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -370,7 +370,7 @@ static int internal_spca561_decode(int width, int height, 1 }; - const static int nbits_B[] = + const int nbits_B[] = { 0, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, @@ -398,7 +398,7 @@ static int internal_spca561_decode(int width, int height, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; - const static int tab_B[] = + const int tab_B[] = { 0xff, -4, 3, 3, -3, -3, -3, -3, 2, 2, 2, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 1, 1, @@ -433,7 +433,7 @@ static int internal_spca561_decode(int width, int height, 0, 0, 0, 0, 0, 0, 0, }; - const static int nbits_C[] = + const int nbits_C[] = { 0, 0, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, @@ -461,7 +461,7 @@ static int internal_spca561_decode(int width, int height, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, }; - const static int tab_C[] = + const int tab_C[] = { 0xff, 0xfe, 6, -7, 5, 5, -6, -6, 4, 4, 4, 4, -5, -5, -5, -5, 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4, -4, 2, 2, 2, 2, @@ -497,7 +497,7 @@ static int internal_spca561_decode(int width, int height, -1, }; - const static int nbits_D[] = + const int nbits_D[] = { 0, 0, 0, 0, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, @@ -525,7 +525,7 @@ static int internal_spca561_decode(int width, int height, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; - const static int tab_D[] = + const int tab_D[] = { 0xff, 0xfe, 0xfd, 0xfc, 10, -11, 11, -12, 8, 8, -9, -9, 9, 9, -10, -10, 6, 6, 6, 6, -7, -7, -7, -7, 7, 7, 7, 7, -8, -8, -8, -8, @@ -563,7 +563,7 @@ static int internal_spca561_decode(int width, int height, }; /* a_curve[19 + i] = ... [-19..19] => [-160..160] */ - const static int a_curve[] = + const int a_curve[] = { -160, -144, -128, -112, -98, -88, -80, -72, -64, -56, -48, -40, -32, -24, -18, -12, -8, -5, -2, 0, 2, 5, 8, 12, 18, 24, 32, @@ -571,7 +571,7 @@ static int internal_spca561_decode(int width, int height, 72, 80, 88, 98, 112, 128, 144, 160 }; /* clamp0_255[256 + i] = min(max(i,255),0) */ - const static unsigned char clamp0_255[] = + const unsigned char clamp0_255[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -680,14 +680,14 @@ static int internal_spca561_decode(int width, int height, 255 }; /* abs_clamp15[19 + i] = min(abs(i), 15) */ - const static int abs_clamp15[] = + const int abs_clamp15[] = { 15, 15, 15, 15, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 15 }; /* diff_encoding[256 + i] = ... */ - const static int diff_encoding[] = + const int diff_encoding[] = { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, -- cgit v1.2.3 From 407fd9eaa58dc76a5a76d92d5aaf866975c8d4a6 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 19:31:43 +0200 Subject: v4l2-library: libv4l-fix-idct-inline-assembly From: Gregor Jasny This patch fixes the input constraint for the sar instruction. It allows only an immediate or cl as shift width. Signed-off-by: Gregor Jasny Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c b/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c index 4b3ea428c..ba70309c9 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/jidctflt.c @@ -92,7 +92,7 @@ static inline unsigned char descale_and_clamp(int x, int shift) "\tcmpl %4,%1\n" "\tcmovg %4,%1\n" : "=r"(x) - : "0"(x), "Ir"(shift), "ir"(1UL<<(shift-1)), "r" (0xff), "r" (0) + : "0"(x), "Ic"((unsigned char)shift), "ir"(1UL<<(shift-1)), "r" (0xff), "r" (0) ); return x; } -- cgit v1.2.3 From c1a4267284bcd81240221187f5a16454f36ba02e Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Fri, 4 Jul 2008 19:34:02 +0200 Subject: v4l2-library: libv4l-0.3.2-release From: Hans de Goede The current mercurial has been released on my homepage as tarbal release 0.3.2, the only changes from the current mercurial + all patches already send (including the asm patch from Gregor Jasny) is updating of the ChangeLog file and updating the version in the Makefile, these changes are included in the attached patch. Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/ChangeLog | 6 ++++++ v4l2-apps/lib/libv4l/Makefile | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/ChangeLog b/v4l2-apps/lib/libv4l/ChangeLog index 0e4a1af33..63b8b9bd6 100644 --- a/v4l2-apps/lib/libv4l/ChangeLog +++ b/v4l2-apps/lib/libv4l/ChangeLog @@ -1,3 +1,9 @@ +libv4l-0.3.2 +------------ +* Add support for converting from sn9c10x compressed data +* Add support for converting from pac207 compressed data +* Add "make install" Makefile target + libv4l-0.3.1 ------------ * Only serialize V4L2_BUF_TYPE_VIDEO_CAPTURE type ioctls diff --git a/v4l2-apps/lib/libv4l/Makefile b/v4l2-apps/lib/libv4l/Makefile index 78c6893a3..d9654f163 100644 --- a/v4l2-apps/lib/libv4l/Makefile +++ b/v4l2-apps/lib/libv4l/Makefile @@ -1,5 +1,5 @@ LIB_RELEASE=0 -V4L2_LIB_VERSION=$(LIB_RELEASE).3 +V4L2_LIB_VERSION=$(LIB_RELEASE).3.2 all clean install: $(MAKE) -C libv4lconvert $@ -- cgit v1.2.3 From ab715d2e8eb29998b60fa3bbc1ad95441cfb69b3 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Sun, 6 Jul 2008 14:07:34 +0200 Subject: v4l2-library: libv4l-sync-with-0.3.3-release From: Hans de Goede * Add open64 and mmap64 wrappers to the LD_PRELOAD wrapper libs, so that they also work for applications compiled with FILE_OFFSET_BITS=64, this fixes using them with v4l-info * While looking at xawtv in general, found a few bugs in xawtv itself, added a patch to fix those to the appl-patches dir * Talking about the appl-patches dir, restore that as it accidentally got dropped from 0.3.2 * Be more verbose in various places when it comes to logging (esp errors) * Change v4lconvert_enum_fmt code a bit, so that it is easier to add more supported destination formats to libv4lconvert * Don't return -EINVAL from try_fmt when we cannot convert because the cam doesn't have any formats we know. Instead just return as format whatever the cam returns from try_fmt, this new behavior is compliant with the v4l2 api as documented Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/ChangeLog | 17 ++ .../libv4l/appl-patches/camorama-0.19-fixes.patch | 90 ++++++ .../libv4l/appl-patches/vlc-0.8.6-libv4l1.patch | 319 +++++++++++++++++++++ .../lib/libv4l/appl-patches/xawtv-3.95-fixes.patch | 29 ++ v4l2-apps/lib/libv4l/include/libv4l1.h | 2 +- v4l2-apps/lib/libv4l/include/libv4l2.h | 2 +- v4l2-apps/lib/libv4l/libv4l1/libv4l1.c | 8 +- v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c | 29 ++ v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h | 9 + v4l2-apps/lib/libv4l/libv4l2/libv4l2.c | 58 ++-- v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c | 31 ++ v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c | 59 ++-- 12 files changed, 601 insertions(+), 52 deletions(-) create mode 100644 v4l2-apps/lib/libv4l/appl-patches/camorama-0.19-fixes.patch create mode 100644 v4l2-apps/lib/libv4l/appl-patches/vlc-0.8.6-libv4l1.patch create mode 100644 v4l2-apps/lib/libv4l/appl-patches/xawtv-3.95-fixes.patch (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/ChangeLog b/v4l2-apps/lib/libv4l/ChangeLog index 63b8b9bd6..9f747df80 100644 --- a/v4l2-apps/lib/libv4l/ChangeLog +++ b/v4l2-apps/lib/libv4l/ChangeLog @@ -1,3 +1,20 @@ +libv4l-0.3.3 +------------ +* Add open64 and mmap64 wrappers to the LD_PRELOAD wrapper libs, so that + they also work for applications compiled with FILE_OFFSET_BITS=64, this + fixes using them with v4l-info +* While looking at xawtv in general, found a few bugs in xawtv itself, added + a patch to fix those to the appl-patches dir +* Talking about the appl-patches dir, restore that as it accidentally got + dropped from 0.3.2 +* Be more verbose in various places when it comes to logging (esp errors) +* Change v4lconvert_enum_fmt code a bit, so that it is easier to add more + supported destination formats to libv4lconvert +* Don't return -EINVAL from try_fmt when we cannot convert because the cam + doesn't have any formats we know. Instead just return as format whatever the + cam returns from try_fmt, this new behavior is compliant with the v4l2 + api as documented + libv4l-0.3.2 ------------ * Add support for converting from sn9c10x compressed data diff --git a/v4l2-apps/lib/libv4l/appl-patches/camorama-0.19-fixes.patch b/v4l2-apps/lib/libv4l/appl-patches/camorama-0.19-fixes.patch new file mode 100644 index 000000000..1e1333575 --- /dev/null +++ b/v4l2-apps/lib/libv4l/appl-patches/camorama-0.19-fixes.patch @@ -0,0 +1,90 @@ +--- camorama-0.19/src/callbacks.c 2007-09-16 15:36:55.000000000 +0200 ++++ camorama-0.19.new/src/callbacks.c 2008-06-29 22:22:44.000000000 +0200 +@@ -387,9 +387,6 @@ + } + } + +- cam->pixmap = gdk_pixmap_new (NULL, cam->x, cam->y, cam->desk_depth); +- gtk_widget_set_size_request (glade_xml_get_widget (cam->xml, "da"), +- cam->x, cam->y); + + /* + * if(cam->read == FALSE) { +@@ -441,6 +438,11 @@ + * * } + */ + get_win_info (cam); ++ ++ cam->pixmap = gdk_pixmap_new (NULL, cam->x, cam->y, cam->desk_depth); ++ gtk_widget_set_size_request (glade_xml_get_widget (cam->xml, "da"), ++ cam->x, cam->y); ++ + frame = 0; + gtk_window_resize (GTK_WINDOW + (glade_xml_get_widget (cam->xml, "main_window")), 320, +@@ -520,8 +522,14 @@ + gtk_widget_show (about); + } + ++void ++camorama_filter_color_filter(void* filter, guchar *image, int x, int y, int depth); ++ + static void + apply_filters(cam* cam) { ++ /* v4l has reverse rgb order from what camora expect so call the color ++ filter to fix things up before running the user selected filters */ ++ camorama_filter_color_filter(NULL, cam->pic_buf, cam->x, cam->y, cam->depth); + camorama_filter_chain_apply(cam->filter_chain, cam->pic_buf, cam->x, cam->y, cam->depth); + #warning "FIXME: enable the threshold channel filter" + // if((effect_mask & CAMORAMA_FILTER_THRESHOLD_CHANNEL) != 0) +--- camorama-0.19/src/filter.c 2007-09-16 14:48:50.000000000 +0200 ++++ camorama-0.19.new/src/filter.c 2008-06-29 22:11:42.000000000 +0200 +@@ -151,12 +151,12 @@ + static void + camorama_filter_color_init(CamoramaFilterColor* self) {} + +-static void ++void + camorama_filter_color_filter(CamoramaFilterColor* filter, guchar *image, int x, int y, int depth) { + int i; + char tmp; + i = x * y; +- while (--i) { ++ while (i--) { + tmp = image[0]; + image[0] = image[2]; + image[2] = tmp; +--- camorama-0.19/src/main.c 2007-09-16 15:36:55.000000000 +0200 ++++ camorama-0.19.new/src/main.c 2008-06-29 22:20:04.000000000 +0200 +@@ -224,8 +224,7 @@ + + /* get picture attributes */ + get_pic_info (cam); +-// set_pic_info(cam); +- /* set_pic_info(cam); */ ++ set_pic_info (cam); + cam->contrast = cam->vid_pic.contrast; + cam->brightness = cam->vid_pic.brightness; + cam->colour = cam->vid_pic.colour; +--- camorama-0.19/src/v4l.c 2007-09-16 14:48:05.000000000 +0200 ++++ camorama-0.19.new/src/v4l.c 2008-06-29 22:20:23.000000000 +0200 +@@ -158,8 +158,8 @@ + if(cam->debug) { + g_message("SET PIC"); + } +- //cam->vid_pic.palette = VIDEO_PALETTE_RGB24; +- //cam->vid_pic.depth = 24; ++ cam->vid_pic.palette = VIDEO_PALETTE_RGB24; ++ cam->vid_pic.depth = 24; + //cam->vid_pic.palette = VIDEO_PALETTE_YUV420P; + if(ioctl(cam->dev, VIDIOCSPICT, &cam->vid_pic) == -1) { + if(cam->debug) { +@@ -232,6 +232,8 @@ + exit(0); + } + ++ cam->x = cam->vid_win.width; ++ cam->y = cam->vid_win.height; + } + + void set_buffer(cam * cam) diff --git a/v4l2-apps/lib/libv4l/appl-patches/vlc-0.8.6-libv4l1.patch b/v4l2-apps/lib/libv4l/appl-patches/vlc-0.8.6-libv4l1.patch new file mode 100644 index 000000000..132549b55 --- /dev/null +++ b/v4l2-apps/lib/libv4l/appl-patches/vlc-0.8.6-libv4l1.patch @@ -0,0 +1,319 @@ +diff -up vlc-0.8.6f/modules/access/v4l/Makefile.am~ vlc-0.8.6f/modules/access/v4l/Makefile.am +--- vlc-0.8.6f/modules/access/v4l/Makefile.am~ 2008-06-29 17:14:11.000000000 +0200 ++++ vlc-0.8.6f/modules/access/v4l/Makefile.am 2008-06-29 17:16:39.000000000 +0200 +@@ -100,7 +100,7 @@ libv4l_plugin_la_CXXFLAGS = `$(VLC_CONFI + libv4l_plugin_la_OBJCFLAGS = `$(VLC_CONFIG) --objcflags plugin v4l` + libv4l_plugin_la_LDFLAGS = `$(VLC_CONFIG) --libs plugin v4l` \ + -rpath '$(libvlcdir)' -avoid-version -module -shrext $(LIBEXT) +-libv4l_plugin_la_LIBADD = $(LTLIBVLC) ++libv4l_plugin_la_LIBADD = $(LTLIBVLC) -lv4l1 + + libv4l_a_SOURCES = $(SOURCES_v4l) + libv4l_builtin_la_SOURCES = $(SOURCES_v4l) +diff -up vlc-0.8.6f/modules/access/v4l/Makefile.in~ vlc-0.8.6f/modules/access/v4l/Makefile.in +--- vlc-0.8.6f/modules/access/v4l/Makefile.in~ 2008-06-29 17:16:22.000000000 +0200 ++++ vlc-0.8.6f/modules/access/v4l/Makefile.in 2008-06-29 17:16:42.000000000 +0200 +@@ -390,7 +390,7 @@ libv4l_plugin_la_OBJCFLAGS = `$(VLC_CONF + libv4l_plugin_la_LDFLAGS = `$(VLC_CONFIG) --libs plugin v4l` \ + -rpath '$(libvlcdir)' -avoid-version -module -shrext $(LIBEXT) + +-libv4l_plugin_la_LIBADD = $(LTLIBVLC) ++libv4l_plugin_la_LIBADD = $(LTLIBVLC) -lv4l1 + libv4l_a_SOURCES = $(SOURCES_v4l) + libv4l_builtin_la_SOURCES = $(SOURCES_v4l) + libv4l_a_CFLAGS = `$(VLC_CONFIG) --cflags builtin pic v4l` +diff -up vlc-0.8.6f/modules/access/v4l/v4l.c~ vlc-0.8.6f/modules/access/v4l/v4l.c +--- vlc-0.8.6f/modules/access/v4l/v4l.c~ 2008-06-29 17:13:30.000000000 +0200 ++++ vlc-0.8.6f/modules/access/v4l/v4l.c 2008-06-29 17:13:30.000000000 +0200 +@@ -64,6 +64,9 @@ + + #include + ++#include ++ ++ + /***************************************************************************** + * Module descriptior + *****************************************************************************/ +@@ -546,23 +549,23 @@ static void Close( vlc_object_t *p_this + if( p_sys->psz_device ) free( p_sys->psz_device ); + if( p_sys->psz_vdev ) free( p_sys->psz_vdev ); + if( p_sys->psz_adev ) free( p_sys->psz_adev ); +- if( p_sys->fd_video >= 0 ) close( p_sys->fd_video ); ++ if( p_sys->fd_video >= 0 ) v4l1_close( p_sys->fd_video ); + if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio ); + if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio ); + + if( p_sys->b_mjpeg ) + { + int i_noframe = -1; +- ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe ); ++ v4l1_ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe ); + } + + if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED ) + { + if( p_sys->b_mjpeg ) +- munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size * ++ v4l1_munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size * + p_sys->mjpeg_buffers.count ); + else +- munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size ); ++ v4l1_munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size ); + } + + free( p_sys ); +@@ -875,13 +878,13 @@ static int OpenVideoDev( demux_t *p_demu + struct mjpeg_params mjpeg; + int i; + +- if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 ) ++ if( ( i_fd = v4l1_open( psz_device, O_RDWR ) ) < 0 ) + { + msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) ); + goto vdev_failed; + } + +- if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 ) + { + msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -926,7 +929,7 @@ static int OpenVideoDev( demux_t *p_demu + } + + vid_channel.channel = p_sys->i_channel; +- if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 ) + { + msg_Err( p_demux, "cannot get channel infos (%s)", + strerror( errno ) ); +@@ -944,7 +947,7 @@ static int OpenVideoDev( demux_t *p_demu + } + + vid_channel.norm = p_sys->i_norm; +- if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 ) + { + msg_Err( p_demux, "cannot set channel (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -959,7 +962,7 @@ static int OpenVideoDev( demux_t *p_demu + if( p_sys->i_tuner >= 0 ) + { + vid_tuner.tuner = p_sys->i_tuner; +- if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 ) + { + msg_Err( p_demux, "cannot get tuner (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -974,7 +977,7 @@ static int OpenVideoDev( demux_t *p_demu + + /* FIXME FIXME to be checked FIXME FIXME */ + //vid_tuner.mode = p_sys->i_norm; +- if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 ) + { + msg_Err( p_demux, "cannot set tuner (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -990,7 +993,7 @@ static int OpenVideoDev( demux_t *p_demu + if( p_sys->i_frequency >= 0 ) + { + int driver_frequency = p_sys->i_frequency * 16 /1000; +- if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 ) + { + msg_Err( p_demux, "cannot set frequency (%s)", + strerror( errno ) ); +@@ -1010,7 +1013,7 @@ static int OpenVideoDev( demux_t *p_demu + if( p_sys->i_audio >= 0 ) + { + vid_audio.audio = p_sys->i_audio; +- if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 ) + { + msg_Err( p_demux, "cannot get audio (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -1019,7 +1022,7 @@ static int OpenVideoDev( demux_t *p_demu + /* unmute audio */ + vid_audio.flags &= ~VIDEO_AUDIO_MUTE; + +- if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 ) + { + msg_Err( p_demux, "cannot set audio (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -1035,7 +1038,7 @@ static int OpenVideoDev( demux_t *p_demu + struct quicktime_mjpeg_app1 *p_app1; + int32_t i_offset; + +- if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 ) ++ if( v4l1_ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 ) + { + msg_Err( p_demux, "cannot get mjpeg params (%s)", + strerror( errno ) ); +@@ -1086,7 +1089,7 @@ static int OpenVideoDev( demux_t *p_demu + * optional. They will be present in the output. */ + mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT; + +- if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 ) ++ if( v4l1_ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 ) + { + msg_Err( p_demux, "cannot set mjpeg params (%s)", + strerror( errno ) ); +@@ -1103,7 +1106,7 @@ static int OpenVideoDev( demux_t *p_demu + { + struct video_window vid_win; + +- if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 ) + { + msg_Err( p_demux, "cannot get win (%s)", strerror( errno ) ); + goto vdev_failed; +@@ -1130,7 +1133,7 @@ static int OpenVideoDev( demux_t *p_demu + if( !p_sys->b_mjpeg ) + { + /* set hue/color/.. */ +- if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 ) + { + struct video_picture vid_picture = p_sys->vid_picture; + +@@ -1150,7 +1153,7 @@ static int OpenVideoDev( demux_t *p_demu + { + vid_picture.contrast = p_sys->i_contrast; + } +- if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) + { + msg_Dbg( p_demux, "v4l device uses brightness: %d", + vid_picture.brightness ); +@@ -1164,7 +1167,7 @@ static int OpenVideoDev( demux_t *p_demu + } + + /* Find out video format used by device */ +- if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 ) + { + struct video_picture vid_picture = p_sys->vid_picture; + char *psz; +@@ -1191,7 +1194,7 @@ static int OpenVideoDev( demux_t *p_demu + free( psz ); + + if( vid_picture.palette && +- !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) ) ++ !v4l1_ioctl( i_fd, VIDIOCSPICT, &vid_picture ) ) + { + p_sys->vid_picture = vid_picture; + } +@@ -1199,14 +1202,14 @@ static int OpenVideoDev( demux_t *p_demu + { + /* Try to set the format to something easy to encode */ + vid_picture.palette = VIDEO_PALETTE_YUV420P; +- if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) + { + p_sys->vid_picture = vid_picture; + } + else + { + vid_picture.palette = VIDEO_PALETTE_YUV422P; +- if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 ) + { + p_sys->vid_picture = vid_picture; + } +@@ -1237,13 +1240,13 @@ static int OpenVideoDev( demux_t *p_demu + p_sys->mjpeg_buffers.count = 8; + p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE; + +- if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 ) ++ if( v4l1_ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 ) + { + msg_Err( p_demux, "mmap unsupported" ); + goto vdev_failed; + } + +- p_sys->p_video_mmap = mmap( 0, ++ p_sys->p_video_mmap = v4l1_mmap( 0, + p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count, + PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 ); + if( p_sys->p_video_mmap == MAP_FAILED ) +@@ -1258,7 +1261,7 @@ static int OpenVideoDev( demux_t *p_demu + /* queue up all the frames */ + for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ ) + { +- if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 ) ++ if( v4l1_ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 ) + { + msg_Err( p_demux, "unable to queue frame" ); + goto vdev_failed; +@@ -1289,13 +1292,13 @@ static int OpenVideoDev( demux_t *p_demu + (char*)&p_sys->i_fourcc ); + + /* Allocate mmap buffer */ +- if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 ) + { + msg_Err( p_demux, "mmap unsupported" ); + goto vdev_failed; + } + +- p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size, ++ p_sys->p_video_mmap = v4l1_mmap( 0, p_sys->vid_mbuf.size, + PROT_READ|PROT_WRITE, MAP_SHARED, + i_fd, 0 ); + if( p_sys->p_video_mmap == MAP_FAILED ) +@@ -1310,7 +1313,7 @@ static int OpenVideoDev( demux_t *p_demu + p_sys->vid_mmap.width = p_sys->i_width; + p_sys->vid_mmap.height = p_sys->i_height; + p_sys->vid_mmap.format = p_sys->vid_picture.palette; +- if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 ) ++ if( v4l1_ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 ) + { + msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc ); + msg_Err( p_demux, "chroma selection failed" ); +@@ -1321,7 +1324,7 @@ static int OpenVideoDev( demux_t *p_demu + + vdev_failed: + +- if( i_fd >= 0 ) close( i_fd ); ++ if( i_fd >= 0 ) v4l1_close( i_fd ); + return -1; + } + +@@ -1431,7 +1434,7 @@ static uint8_t *GrabCapture( demux_t *p_ + + p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames; + +- while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 ) ++ while( v4l1_ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 ) + { + if( errno != EAGAIN ) + { +@@ -1447,7 +1450,7 @@ static uint8_t *GrabCapture( demux_t *p_ + msg_Dbg( p_demux, "grab failed, trying again" ); + } + +- while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 ) ++ while( v4l1_ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 ) + { + if( errno != EAGAIN && errno != EINTR ) + { +@@ -1473,7 +1476,7 @@ static uint8_t *GrabMJPEG( demux_t *p_de + /* re-queue the last frame we sync'd */ + if( p_sys->i_frame_pos != -1 ) + { +- while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, ++ while( v4l1_ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, + &p_sys->i_frame_pos ) < 0 ) + { + if( errno != EAGAIN && errno != EINTR ) +@@ -1485,7 +1488,7 @@ static uint8_t *GrabMJPEG( demux_t *p_de + } + + /* sync on the next frame */ +- while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 ) ++ while( v4l1_ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 ) + { + if( errno != EAGAIN && errno != EINTR ) + { diff --git a/v4l2-apps/lib/libv4l/appl-patches/xawtv-3.95-fixes.patch b/v4l2-apps/lib/libv4l/appl-patches/xawtv-3.95-fixes.patch new file mode 100644 index 000000000..ccb077be0 --- /dev/null +++ b/v4l2-apps/lib/libv4l/appl-patches/xawtv-3.95-fixes.patch @@ -0,0 +1,29 @@ +--- xawtv-3.95/libng/plugins/drv0-v4l2.c 2005-02-11 18:56:24.000000000 +0100 ++++ xawtv-3.95.new/libng/plugins/drv0-v4l2.c 2008-07-05 21:12:37.000000000 +0200 +@@ -161,7 +161,7 @@ + #define PREFIX "ioctl: " + + static int +-xioctl(int fd, int cmd, void *arg, int mayfail) ++xioctl(int fd, unsigned long int cmd, void *arg, int mayfail) + { + int rc; + +@@ -768,6 +768,7 @@ + /* get it */ + memset(&buf,0,sizeof(buf)); + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ++ buf.memory = V4L2_MEMORY_MMAP; + if (-1 == xioctl(h->fd,VIDIOC_DQBUF,&buf, 0)) + return -1; + h->waiton++; +@@ -813,8 +814,7 @@ + if (-1 == xioctl(h->fd, VIDIOC_QUERYBUF, &h->buf_v4l2[i], 0)) + return -1; + h->buf_me[i].fmt = h->fmt_me; +- h->buf_me[i].size = h->buf_me[i].fmt.bytesperline * +- h->buf_me[i].fmt.height; ++ h->buf_me[i].size = h->buf_v4l2[i].length; + h->buf_me[i].data = mmap(NULL, h->buf_v4l2[i].length, + PROT_READ | PROT_WRITE, MAP_SHARED, + h->fd, h->buf_v4l2[i].m.offset); diff --git a/v4l2-apps/lib/libv4l/include/libv4l1.h b/v4l2-apps/lib/libv4l/include/libv4l1.h index fc188c32d..9036ae869 100644 --- a/v4l2-apps/lib/libv4l/include/libv4l1.h +++ b/v4l2-apps/lib/libv4l/include/libv4l1.h @@ -57,7 +57,7 @@ int v4l1_dup(int fd); int v4l1_ioctl (int fd, unsigned long int request, ...); ssize_t v4l1_read (int fd, void* buffer, size_t n); void *v4l1_mmap(void *start, size_t length, int prot, int flags, int fd, - off_t offset); + __off64_t offset); int v4l1_munmap(void *_start, size_t length); #ifdef __cplusplus diff --git a/v4l2-apps/lib/libv4l/include/libv4l2.h b/v4l2-apps/lib/libv4l/include/libv4l2.h index 0e88039b8..63529cf4b 100644 --- a/v4l2-apps/lib/libv4l/include/libv4l2.h +++ b/v4l2-apps/lib/libv4l/include/libv4l2.h @@ -58,7 +58,7 @@ int v4l2_dup(int fd); int v4l2_ioctl (int fd, unsigned long int request, ...); ssize_t v4l2_read (int fd, void* buffer, size_t n); void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, - off_t offset); + __off64_t offset); int v4l2_munmap(void *_start, size_t length); diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c index 7f5e79e3d..4a54047df 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c @@ -40,9 +40,6 @@ in turn will call v4l1_open, so therefor v4l1_open (for example) may not use the regular open()! */ - -#define _LARGEFILE64_SOURCE 1 - #include #include #include @@ -642,7 +639,8 @@ int v4l1_ioctl (int fd, unsigned long int request, ...) } if (devices[index].v4l1_frame_pointer == MAP_FAILED) { - devices[index].v4l1_frame_pointer = mmap64(NULL, mbuf->size, + devices[index].v4l1_frame_pointer = (void *)syscall(SYS_mmap, NULL, + (size_t)mbuf->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); if (devices[index].v4l1_frame_pointer == MAP_FAILED) { @@ -749,7 +747,7 @@ ssize_t v4l1_read(int fd, void* buffer, size_t n) void *v4l1_mmap(void *start, size_t length, int prot, int flags, int fd, - __off_t offset) + __off64_t offset) { int index; void *result; diff --git a/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c b/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c index 4a4e5c253..db3a0027b 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c +++ b/v4l2-apps/lib/libv4l/libv4l1/v4l1compat.c @@ -19,6 +19,8 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define _LARGEFILE64_SOURCE 1 + #include #include #include @@ -50,6 +52,27 @@ int open (const char *file, int oflag, ...) return fd; } +int open64 (const char *file, int oflag, ...) +{ + int fd; + + if (oflag & O_CREAT) + { + va_list ap; + mode_t mode; + + va_start (ap, oflag); + mode = va_arg (ap, mode_t); + + fd = v4l1_open(file, oflag | O_LARGEFILE, mode); + + va_end(ap); + } else + fd = v4l1_open(file, oflag | O_LARGEFILE); + + return fd; +} + int close(int fd) { return v4l1_close(fd); } @@ -82,6 +105,12 @@ void mmap(void *start, size_t length, int prot, int flags, int fd, return v4l1_mmap(start, length, prot, flags, fd, offset); } +void mmap64(void *start, size_t length, int prot, int flags, int fd, + __off64_t offset) +{ + return v4l1_mmap(start, length, prot, flags, fd, offset); +} + int munmap(void *start, size_t length) { return v4l1_munmap(start, length); diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h b/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h index e4edf91f2..203dcffaf 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2-priv.h @@ -23,6 +23,15 @@ #include #include /* includes videodev2.h for us */ +/* On 32 bits archs we always use mmap2, on 64 bits archs there is no mmap2 */ +#ifdef __NR_mmap2 +#define SYS_mmap2 __NR_mmap2 +#define MMAP2_PAGE_SHIFT 12 +#else +#define SYS_mmap2 SYS_mmap +#define MMAP2_PAGE_SHIFT 0 +#endif + #define V4L2_MAX_DEVICES 16 /* Warning when making this larger the frame_queued and frame_mapped members of the v4l2_dev_info struct can no longer be a bitfield, so the code needs to diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c index 0ed6ebdc6..5830576ae 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c @@ -55,9 +55,6 @@ When modifications are made, one should be carefull that this behavior is preserved. */ - -#define _LARGEFILE64_SOURCE 1 - #include #include #include @@ -161,8 +158,9 @@ static int v4l2_map_buffers(int index) break; } - devices[index].frame_pointers[i] = mmap64(NULL, buf.length, - PROT_READ, MAP_SHARED, devices[index].fd, buf.m.offset); + devices[index].frame_pointers[i] = (void *)syscall(SYS_mmap2, NULL, + (size_t)buf.length, PROT_READ, MAP_SHARED, devices[index].fd, + (__off_t)(buf.m.offset >> MMAP2_PAGE_SHIFT)); if (devices[index].frame_pointers[i] == MAP_FAILED) { int saved_err = errno; V4L2_LOG_ERR("mmapping buffer %u: %s\n", i, strerror(errno)); @@ -583,8 +581,13 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) } } - if (!is_capture_request) - return syscall(SYS_ioctl, fd, request, arg); + if (!is_capture_request) { + result = syscall(SYS_ioctl, fd, request, arg); + saved_err = errno; + v4l2_log_ioctl(request, arg, result); + errno = saved_err; + return result; + } if (stream_needs_locking) @@ -755,16 +758,22 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) struct v4l2_buffer *buf = arg; result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_DQBUF, buf); - if (result || !converting) + if (result) { + V4L2_LOG_ERR("dequeing buffer: %s\n", strerror(errno)); + break; + } + + if (!converting) break; /* An application can do a DQBUF before mmap-ing in the buffer, but we need the buffer _now_ to write our converted data to it! */ if (devices[index].convert_mmap_buf == MAP_FAILED) { - devices[index].convert_mmap_buf = mmap64(NULL, - devices[index].no_frames * - V4L2_FRAME_BUF_SIZE, + devices[index].convert_mmap_buf = (void *)syscall(SYS_mmap2, + (size_t)( + devices[index].no_frames * + V4L2_FRAME_BUF_SIZE), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); @@ -789,8 +798,11 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) devices[index].convert_mmap_buf + buf->index * V4L2_FRAME_BUF_SIZE, V4L2_FRAME_BUF_SIZE); - if (result < 0) + if (result < 0) { + V4L2_LOG_ERR("converting / decoding frame data: %s\n", + v4lconvert_get_error_message(devices[index].convert)); break; + } buf->bytesused = result; buf->length = V4L2_FRAME_BUF_SIZE; @@ -861,6 +873,10 @@ ssize_t v4l2_read (int fd, void* buffer, size_t n) v4l2_queue_read_buffer(index, frame_index); + if (result < 0) + V4L2_LOG_ERR("converting / decoding frame data: %s\n", + v4lconvert_get_error_message(devices[index].convert)); + leave: pthread_mutex_unlock(&devices[index].stream_lock); @@ -868,7 +884,7 @@ leave: } void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, - __off_t offset) + __off64_t offset) { int index; unsigned int buffer_index; @@ -882,7 +898,14 @@ void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, if (index != -1) V4L2_LOG("Passing mmap(%p, %d, ..., %x, through to the driver\n", start, (int)length, (int)offset); - return mmap64(start, length, prot, flags, fd, offset); + + if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1)) { + errno = EINVAL; + return MAP_FAILED; + } + + return (void *)syscall(SYS_mmap2, start, length, prot, flags, fd, + (__off_t)(offset >> MMAP2_PAGE_SHIFT)); } pthread_mutex_lock(&devices[index].stream_lock); @@ -899,9 +922,10 @@ void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, } if (devices[index].convert_mmap_buf == MAP_FAILED) { - devices[index].convert_mmap_buf = mmap64(NULL, - devices[index].no_frames * - V4L2_FRAME_BUF_SIZE, + devices[index].convert_mmap_buf = (void *)syscall(SYS_mmap2, NULL, + (size_t)( + devices[index].no_frames * + V4L2_FRAME_BUF_SIZE), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); diff --git a/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c b/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c index 67f563602..7db1ca6d6 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c +++ b/v4l2-apps/lib/libv4l/libv4l2/v4l2convert.c @@ -20,6 +20,8 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define _LARGEFILE64_SOURCE 1 + #include #include #include @@ -82,6 +84,29 @@ int open (const char *file, int oflag, ...) return fd; } +int open64(const char *file, int oflag, ...) +{ + int fd; + + /* original open code */ + if (oflag & O_CREAT) + { + va_list ap; + mode_t mode; + + va_start (ap, oflag); + mode = va_arg (ap, mode_t); + + fd = open(file, oflag | O_LARGEFILE, mode); + + va_end(ap); + } else + fd = open(file, oflag | O_LARGEFILE); + /* end of original open code */ + + return fd; +} + int close(int fd) { return v4l2_close(fd); @@ -115,6 +140,12 @@ void mmap(void *start, size_t length, int prot, int flags, int fd, return v4l2_mmap(start, length, prot, flags, fd, offset); } +void mmap64(void *start, size_t length, int prot, int flags, int fd, + __off64_t offset) +{ + return v4l2_mmap(start, length, prot, flags, fd, offset); +} + int munmap(void *start, size_t length) { return v4l2_munmap(start, length); diff --git a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c index 35fd52481..cc733554c 100644 --- a/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c +++ b/v4l2-apps/lib/libv4l/libv4lconvert/libv4lconvert.c @@ -27,9 +27,14 @@ #define MIN(a,b) (((a)<(b))?(a):(b)) #define ARRAY_SIZE(x) ((int)sizeof(x)/(int)sizeof((x)[0])) +/* Note for proper functioning of v4lconvert_enum_fmt the first entries in + supported_src_pixfmts must match with the entries in supported_dst_pixfmts */ +#define SUPPORTED_DST_PIXFMTS \ + V4L2_PIX_FMT_BGR24, \ + V4L2_PIX_FMT_YUV420 + static const unsigned int supported_src_pixfmts[] = { - V4L2_PIX_FMT_BGR24, - V4L2_PIX_FMT_YUV420, + SUPPORTED_DST_PIXFMTS, V4L2_PIX_FMT_MJPEG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_SBGGR8, @@ -43,8 +48,7 @@ static const unsigned int supported_src_pixfmts[] = { }; static const unsigned int supported_dst_pixfmts[] = { - V4L2_PIX_FMT_BGR24, - V4L2_PIX_FMT_YUV420, + SUPPORTED_DST_PIXFMTS }; @@ -93,38 +97,35 @@ void v4lconvert_destroy(struct v4lconvert_data *data) /* See libv4lconvert.h for description of in / out parameters */ int v4lconvert_enum_fmt(struct v4lconvert_data *data, struct v4l2_fmtdesc *fmt) { + int i, no_faked_fmts = 0; + unsigned int faked_fmts[ARRAY_SIZE(supported_dst_pixfmts)]; + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || fmt->index < data->no_formats || !data->supported_src_formats) return syscall(SYS_ioctl, data->fd, VIDIOC_ENUM_FMT, fmt); - fmt->flags = 0; - fmt->pixelformat = 0; - memset(fmt->reserved, 0, 4); + for (i = 0; i < ARRAY_SIZE(supported_dst_pixfmts); i++) + if (!(data->supported_src_formats & (1 << i))) { + faked_fmts[no_faked_fmts] = supported_dst_pixfmts[i]; + no_faked_fmts++; + } - /* Note bgr24 and yuv420 are the first 2 in our mask of supported formats */ - switch (fmt->index - data->no_formats) { - case 0: - if (!(data->supported_src_formats & 1)) { - strcpy((char *)fmt->description, "BGR24"); - fmt->pixelformat = V4L2_PIX_FMT_BGR24; - } else if (!(data->supported_src_formats & 2)) { - strcpy((char *)fmt->description, "YUV420"); - fmt->pixelformat = V4L2_PIX_FMT_YUV420; - } - break; - case 1: - if (!(data->supported_src_formats & 3)) { - strcpy((char *)fmt->description, "YUV420"); - fmt->pixelformat = V4L2_PIX_FMT_YUV420; - } - break; - } - if (fmt->pixelformat == 0) { + i = fmt->index - data->no_formats; + if (i >= no_faked_fmts) { errno = EINVAL; return -1; } + fmt->flags = 0; + fmt->pixelformat = faked_fmts[i]; + fmt->description[0] = faked_fmts[i] & 0xff; + fmt->description[1] = (faked_fmts[i] >> 8) & 0xff; + fmt->description[2] = (faked_fmts[i] >> 16) & 0xff; + fmt->description[3] = faked_fmts[i] >> 24; + fmt->description[4] = '\0'; + memset(fmt->reserved, 0, 4); + return 0; } @@ -176,8 +177,10 @@ int v4lconvert_try_format(struct v4lconvert_data *data, } if (closest_fmt.type == 0) { - errno = EINVAL; - return -1; + int ret = syscall(SYS_ioctl, data->fd, VIDIOC_TRY_FMT, dest_fmt); + if (src_fmt) + *src_fmt = *dest_fmt; + return ret; } *dest_fmt = closest_fmt; -- cgit v1.2.3 From 4e748efa7677d0762f4b1738cd8869df5eb106b6 Mon Sep 17 00:00:00 2001 From: Thierry MERLE Date: Tue, 8 Jul 2008 07:01:45 +0200 Subject: v4l2-library: libv4l-really-sync-with-0.3.4.patch From: Hans de Goede * The mmap64 support in 0.3.3, has caused a bug in libv4l1 when running on 32 bit systems (who uses those now a days?), this bug caused v4l1 compatibility to not work at all, this release fixes this * Some apps (xawtv, kopete) use an ioctl wrapper internally for various reasons. This wrappers request argument is an int, but the real ioctl's request argument is an unsigned long. Passing the VIDIOC_xxx defines through to the wrapper, and then to the real ioctl, causes the request to get sign extended on 64 bit args. The kernel seems to ignore the upper 32 bits, causing the sign extension to not make a difference. libv4l now also ignores the upper 32 bits of the libv4lx_ioctl request argument on 64 bit archs * Add a bugfix patch for kopete in the appl-patches dir, currently it assumes that it got the width and height it asked for when doing a S_FMT, which is a wrong assumption Signed-off-by: Hans de Goede Signed-off-by: Thierry MERLE --- v4l2-apps/lib/libv4l/ChangeLog | 18 ++++++++++++++++++ .../libv4l/appl-patches/kdenetwork-4.0.85-kopete.patch | 12 ++++++++++++ v4l2-apps/lib/libv4l/libv4l1/libv4l1.c | 5 +++++ v4l2-apps/lib/libv4l/libv4l2/libv4l2.c | 5 +++++ 4 files changed, 40 insertions(+) create mode 100644 v4l2-apps/lib/libv4l/appl-patches/kdenetwork-4.0.85-kopete.patch (limited to 'v4l2-apps/lib') diff --git a/v4l2-apps/lib/libv4l/ChangeLog b/v4l2-apps/lib/libv4l/ChangeLog index 9f747df80..14c5625a1 100644 --- a/v4l2-apps/lib/libv4l/ChangeLog +++ b/v4l2-apps/lib/libv4l/ChangeLog @@ -1,3 +1,21 @@ +libv4l-0.3.4 +------------ +* The mmap64 support in 0.3.3, has caused a bug in libv4l1 when running on + 32 bit systems (who uses those now a days?), this bug caused v4l1 + compatibility to not work at all, this release fixes this +* Some apps (xawtv, kopete) use an ioctl wrapper internally for various + reasons. This wrappers request argument is an int, but the real ioctl's + request argument is an unsigned long. Passing the VIDIOC_xxx defines through + to the wrapper, and then to the real ioctl, causes the request to get sign + extended on 64 bit args. The kernel seems to ignore the upper 32 bits, + causing the sign extension to not make a difference. libv4l now also + ignores the upper 32 bits of the libv4lx_ioctl request argument on 64 bit + archs +* Add a bugfix patch for kopete in the appl-patches dir, currently it assumes + that it got the width and height it asked for when doing a S_FMT, which is a + wrong assumption + + libv4l-0.3.3 ------------ * Add open64 and mmap64 wrappers to the LD_PRELOAD wrapper libs, so that diff --git a/v4l2-apps/lib/libv4l/appl-patches/kdenetwork-4.0.85-kopete.patch b/v4l2-apps/lib/libv4l/appl-patches/kdenetwork-4.0.85-kopete.patch new file mode 100644 index 000000000..b187f05e4 --- /dev/null +++ b/v4l2-apps/lib/libv4l/appl-patches/kdenetwork-4.0.85-kopete.patch @@ -0,0 +1,12 @@ +diff -up kdenetwork-4.0.85/kopete/libkopete/avdevice/videodevice.cpp~ kdenetwork-4.0.85/kopete/libkopete/avdevice/videodevice.cpp +--- kdenetwork-4.0.85/kopete/libkopete/avdevice/videodevice.cpp~ 2008-07-07 22:40:56.000000000 +0200 ++++ kdenetwork-4.0.85/kopete/libkopete/avdevice/videodevice.cpp 2008-07-07 22:40:56.000000000 +0200 +@@ -679,6 +679,8 @@ kDebug() << "VIDIOC_S_FMT worked (" << e + if (fmt.fmt.pix.sizeimage < min) + fmt.fmt.pix.sizeimage = min; + m_buffer_size=fmt.fmt.pix.sizeimage ; ++ currentwidth = fmt.fmt.pix.width; ++ currentheight = fmt.fmt.pix.height; + } + break; + #endif diff --git a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c index 4a54047df..40e4b2aa1 100644 --- a/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c +++ b/v4l2-apps/lib/libv4l/libv4l1/libv4l1.c @@ -454,6 +454,11 @@ int v4l1_ioctl (int fd, unsigned long int request, ...) if ((index = v4l1_get_index(fd)) == -1) return syscall(SYS_ioctl, fd, request, arg); + /* Appearantly the kernel and / or glibc ignore the 32 most significant bits + when long = 64 bits, and some applications pass an int holding the req to + ioctl, causing it to get sign extended, depending upon this behavior */ + request = (unsigned int)request; + /* do we need to take the stream lock for this ioctl? */ switch (request) { case VIDIOCSPICT: diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c index 5830576ae..8dfcf9b71 100644 --- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c +++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c @@ -541,6 +541,11 @@ int v4l2_ioctl (int fd, unsigned long int request, ...) if ((index = v4l2_get_index(fd)) == -1) return syscall(SYS_ioctl, fd, request, arg); + /* Appearantly the kernel and / or glibc ignore the 32 most significant bits + when long = 64 bits, and some applications pass an int holding the req to + ioctl, causing it to get sign extended, depending upon this behavior */ + request = (unsigned int)request; + /* Is this a capture request and do we need to take the stream lock? */ switch (request) { case VIDIOC_ENUM_FMT: -- cgit v1.2.3