aboutsummaryrefslogtreecommitdiff
path: root/drivers/media/video/usbvideo/ibmcam.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/usbvideo/ibmcam.c')
-rw-r--r--drivers/media/video/usbvideo/ibmcam.c3932
1 files changed, 3932 insertions, 0 deletions
diff --git a/drivers/media/video/usbvideo/ibmcam.c b/drivers/media/video/usbvideo/ibmcam.c
new file mode 100644
index 00000000000..a42c2229412
--- /dev/null
+++ b/drivers/media/video/usbvideo/ibmcam.c
@@ -0,0 +1,3932 @@
+/*
+ * USB IBM C-It Video Camera driver
+ *
+ * Supports Xirlink C-It Video Camera, IBM PC Camera,
+ * IBM NetCamera and Veo Stingray.
+ *
+ * This driver is based on earlier work of:
+ *
+ * (C) Copyright 1999 Johannes Erdfelt
+ * (C) Copyright 1999 Randy Dunlap
+ *
+ * 5/24/00 Removed optional (and unnecessary) locking of the driver while
+ * the device remains plugged in. Corrected race conditions in ibmcam_open
+ * and ibmcam_probe() routines using this as a guideline:
+ */
+
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/module.h>
+#include <linux/init.h>
+
+#include "usbvideo.h"
+
+#define IBMCAM_VENDOR_ID 0x0545
+#define IBMCAM_PRODUCT_ID 0x8080
+#define NETCAM_PRODUCT_ID 0x8002 /* IBM NetCamera, close to model 2 */
+#define VEO_800C_PRODUCT_ID 0x800C /* Veo Stingray, repackaged Model 2 */
+#define VEO_800D_PRODUCT_ID 0x800D /* Veo Stingray, repackaged Model 4 */
+
+#define MAX_IBMCAM 4 /* How many devices we allow to connect */
+#define USES_IBMCAM_PUTPIXEL 0 /* 0=Fast/oops 1=Slow/secure */
+
+/* Header signatures */
+
+/* Model 1 header: 00 FF 00 xx */
+#define HDRSIG_MODEL1_128x96 0x06 /* U Y V Y ... */
+#define HDRSIG_MODEL1_176x144 0x0e /* U Y V Y ... */
+#define HDRSIG_MODEL1_352x288 0x00 /* V Y U Y ... */
+
+#define IBMCAM_MODEL_1 1 /* XVP-501, 3 interfaces, rev. 0.02 */
+#define IBMCAM_MODEL_2 2 /* KSX-X9903, 2 interfaces, rev. 3.0a */
+#define IBMCAM_MODEL_3 3 /* KSX-X9902, 2 interfaces, rev. 3.01 */
+#define IBMCAM_MODEL_4 4 /* IBM NetCamera, 0545/8002/3.0a */
+
+/* Video sizes supported */
+#define VIDEOSIZE_128x96 VIDEOSIZE(128, 96)
+#define VIDEOSIZE_176x144 VIDEOSIZE(176,144)
+#define VIDEOSIZE_352x288 VIDEOSIZE(352,288)
+#define VIDEOSIZE_320x240 VIDEOSIZE(320,240)
+#define VIDEOSIZE_352x240 VIDEOSIZE(352,240)
+#define VIDEOSIZE_640x480 VIDEOSIZE(640,480)
+#define VIDEOSIZE_160x120 VIDEOSIZE(160,120)
+
+/* Video sizes supported */
+enum {
+ SIZE_128x96 = 0,
+ SIZE_160x120,
+ SIZE_176x144,
+ SIZE_320x240,
+ SIZE_352x240,
+ SIZE_352x288,
+ SIZE_640x480,
+ /* Add/remove/rearrange items before this line */
+ SIZE_LastItem
+};
+
+/*
+ * This structure lives in uvd->user field.
+ */
+typedef struct {
+ int initialized; /* Had we already sent init sequence? */
+ int camera_model; /* What type of IBM camera we got? */
+ int has_hdr;
+} ibmcam_t;
+#define IBMCAM_T(uvd) ((ibmcam_t *)((uvd)->user_data))
+
+static struct usbvideo *cams;
+
+static int debug;
+
+static int flags; /* = FLAGS_DISPLAY_HINTS | FLAGS_OVERLAY_STATS; */
+
+static const int min_canvasWidth = 8;
+static const int min_canvasHeight = 4;
+
+static int lighting = 1; /* Medium */
+
+#define SHARPNESS_MIN 0
+#define SHARPNESS_MAX 6
+static int sharpness = 4; /* Low noise, good details */
+
+#define FRAMERATE_MIN 0
+#define FRAMERATE_MAX 6
+static int framerate = -1;
+
+static int size = SIZE_352x288;
+
+/*
+ * Here we define several initialization variables. They may
+ * be used to automatically set color, hue, brightness and
+ * contrast to desired values. This is particularly useful in
+ * case of webcams (which have no controls and no on-screen
+ * output) and also when a client V4L software is used that
+ * does not have some of those controls. In any case it's
+ * good to have startup values as options.
+ *
+ * These values are all in [0..255] range. This simplifies
+ * operation. Note that actual values of V4L variables may
+ * be scaled up (as much as << 8). User can see that only
+ * on overlay output, however, or through a V4L client.
+ */
+static int init_brightness = 128;
+static int init_contrast = 192;
+static int init_color = 128;
+static int init_hue = 128;
+static int hue_correction = 128;
+
+/* Settings for camera model 2 */
+static int init_model2_rg2 = -1;
+static int init_model2_sat = -1;
+static int init_model2_yb = -1;
+
+/* 01.01.08 - Added for RCA video in support -LO */
+/* Settings for camera model 3 */
+static int init_model3_input = 0;
+
+module_param(debug, int, 0);
+MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
+module_param(flags, int, 0);
+MODULE_PARM_DESC(flags, "Bitfield: 0=VIDIOCSYNC, 1=B/W, 2=show hints, 3=show stats, 4=test pattern, 5=separate frames, 6=clean frames");
+module_param(framerate, int, 0);
+MODULE_PARM_DESC(framerate, "Framerate setting: 0=slowest, 6=fastest (default=2)");
+module_param(lighting, int, 0);
+MODULE_PARM_DESC(lighting, "Photosensitivity: 0=bright, 1=medium (default), 2=low light");
+module_param(sharpness, int, 0);
+MODULE_PARM_DESC(sharpness, "Model1 noise reduction: 0=smooth, 6=sharp (default=4)");
+module_param(size, int, 0);
+MODULE_PARM_DESC(size, "Image size: 0=128x96 1=160x120 2=176x144 3=320x240 4=352x240 5=352x288 6=640x480 (default=5)");
+module_param(init_brightness, int, 0);
+MODULE_PARM_DESC(init_brightness, "Brightness preconfiguration: 0-255 (default=128)");
+module_param(init_contrast, int, 0);
+MODULE_PARM_DESC(init_contrast, "Contrast preconfiguration: 0-255 (default=192)");
+module_param(init_color, int, 0);
+MODULE_PARM_DESC(init_color, "Color preconfiguration: 0-255 (default=128)");
+module_param(init_hue, int, 0);
+MODULE_PARM_DESC(init_hue, "Hue preconfiguration: 0-255 (default=128)");
+module_param(hue_correction, int, 0);
+MODULE_PARM_DESC(hue_correction, "YUV colorspace regulation: 0-255 (default=128)");
+
+module_param(init_model2_rg2, int, 0);
+MODULE_PARM_DESC(init_model2_rg2, "Model2 preconfiguration: 0-255 (default=47)");
+module_param(init_model2_sat, int, 0);
+MODULE_PARM_DESC(init_model2_sat, "Model2 preconfiguration: 0-255 (default=52)");
+module_param(init_model2_yb, int, 0);
+MODULE_PARM_DESC(init_model2_yb, "Model2 preconfiguration: 0-255 (default=160)");
+
+/* 01.01.08 - Added for RCA video in support -LO */
+module_param(init_model3_input, int, 0);
+MODULE_PARM_DESC(init_model3_input, "Model3 input: 0=CCD 1=RCA");
+
+MODULE_AUTHOR ("Dmitri");
+MODULE_DESCRIPTION ("IBM/Xirlink C-it USB Camera Driver for Linux (c) 2000");
+MODULE_LICENSE("GPL");
+
+/* Still mysterious i2c commands */
+static const unsigned short unknown_88 = 0x0088;
+static const unsigned short unknown_89 = 0x0089;
+static const unsigned short bright_3x[3] = { 0x0031, 0x0032, 0x0033 };
+static const unsigned short contrast_14 = 0x0014;
+static const unsigned short light_27 = 0x0027;
+static const unsigned short sharp_13 = 0x0013;
+
+/* i2c commands for Model 2 cameras */
+static const unsigned short mod2_brightness = 0x001a; /* $5b .. $ee; default=$5a */
+static const unsigned short mod2_set_framerate = 0x001c; /* 0 (fast).. $1F (slow) */
+static const unsigned short mod2_color_balance_rg2 = 0x001e; /* 0 (red) .. $7F (green) */
+static const unsigned short mod2_saturation = 0x0020; /* 0 (b/w) - $7F (full color) */
+static const unsigned short mod2_color_balance_yb = 0x0022; /* 0..$7F, $50 is about right */
+static const unsigned short mod2_hue = 0x0024; /* 0..$7F, $70 is about right */
+static const unsigned short mod2_sensitivity = 0x0028; /* 0 (min) .. $1F (max) */
+
+struct struct_initData {
+ unsigned char req;
+ unsigned short value;
+ unsigned short index;
+};
+
+/*
+ * ibmcam_size_to_videosize()
+ *
+ * This procedure converts module option 'size' into the actual
+ * videosize_t that defines the image size in pixels. We need
+ * simplified 'size' because user wants a simple enumerated list
+ * of choices, not an infinite set of possibilities.
+ */
+static videosize_t ibmcam_size_to_videosize(int size)
+{
+ videosize_t vs = VIDEOSIZE_352x288;
+ RESTRICT_TO_RANGE(size, 0, (SIZE_LastItem-1));
+ switch (size) {
+ case SIZE_128x96:
+ vs = VIDEOSIZE_128x96;
+ break;
+ case SIZE_160x120:
+ vs = VIDEOSIZE_160x120;
+ break;
+ case SIZE_176x144:
+ vs = VIDEOSIZE_176x144;
+ break;
+ case SIZE_320x240:
+ vs = VIDEOSIZE_320x240;
+ break;
+ case SIZE_352x240:
+ vs = VIDEOSIZE_352x240;
+ break;
+ case SIZE_352x288:
+ vs = VIDEOSIZE_352x288;
+ break;
+ case SIZE_640x480:
+ vs = VIDEOSIZE_640x480;
+ break;
+ default:
+ err("size=%d. is not valid", size);
+ break;
+ }
+ return vs;
+}
+
+/*
+ * ibmcam_find_header()
+ *
+ * Locate one of supported header markers in the queue.
+ * Once found, remove all preceding bytes AND the marker (4 bytes)
+ * from the data pump queue. Whatever follows must be video lines.
+ *
+ * History:
+ * 1/21/00 Created.
+ */
+static enum ParseState ibmcam_find_header(struct uvd *uvd) /* FIXME: Add frame here */
+{
+ struct usbvideo_frame *frame;
+ ibmcam_t *icam;
+
+ if ((uvd->curframe) < 0 || (uvd->curframe >= USBVIDEO_NUMFRAMES)) {
+ err("ibmcam_find_header: Illegal frame %d.", uvd->curframe);
+ return scan_EndParse;
+ }
+ icam = IBMCAM_T(uvd);
+ assert(icam != NULL);
+ frame = &uvd->frame[uvd->curframe];
+ icam->has_hdr = 0;
+ switch (icam->camera_model) {
+ case IBMCAM_MODEL_1:
+ {
+ const int marker_len = 4;
+ while (RingQueue_GetLength(&uvd->dp) >= marker_len) {
+ if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
+ (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xFF) &&
+ (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00))
+ {
+#if 0 /* This code helps to detect new frame markers */
+ info("Header sig: 00 FF 00 %02X", RING_QUEUE_PEEK(&uvd->dp, 3));
+#endif
+ frame->header = RING_QUEUE_PEEK(&uvd->dp, 3);
+ if ((frame->header == HDRSIG_MODEL1_128x96) ||
+ (frame->header == HDRSIG_MODEL1_176x144) ||
+ (frame->header == HDRSIG_MODEL1_352x288))
+ {
+#if 0
+ info("Header found.");
+#endif
+ RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, marker_len);
+ icam->has_hdr = 1;
+ break;
+ }
+ }
+ /* If we are still here then this doesn't look like a header */
+ RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
+ }
+ break;
+ }
+ case IBMCAM_MODEL_2:
+case IBMCAM_MODEL_4:
+ {
+ int marker_len = 0;
+ switch (uvd->videosize) {
+ case VIDEOSIZE_176x144:
+ marker_len = 10;
+ break;
+ default:
+ marker_len = 2;
+ break;
+ }
+ while (RingQueue_GetLength(&uvd->dp) >= marker_len) {
+ if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
+ (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xFF))
+ {
+#if 0
+ info("Header found.");
+#endif
+ RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, marker_len);
+ icam->has_hdr = 1;
+ frame->header = HDRSIG_MODEL1_176x144;
+ break;
+ }
+ /* If we are still here then this doesn't look like a header */
+ RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
+ }
+ break;
+ }
+ case IBMCAM_MODEL_3:
+ { /*
+ * Headers: (one precedes every frame). nc=no compression,
+ * bq=best quality bf=best frame rate.
+ *
+ * 176x144: 00 FF 02 { 0A=nc CA=bq EA=bf }
+ * 320x240: 00 FF 02 { 08=nc 28=bq 68=bf }
+ * 640x480: 00 FF 03 { 08=nc 28=bq 68=bf }
+ *
+ * Bytes '00 FF' seem to indicate header. Other two bytes
+ * encode the frame type. This is a set of bit fields that
+ * encode image size, compression type etc. These fields
+ * do NOT contain frame number because all frames carry
+ * the same header.
+ */
+ const int marker_len = 4;
+ while (RingQueue_GetLength(&uvd->dp) >= marker_len) {
+ if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
+ (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xFF) &&
+ (RING_QUEUE_PEEK(&uvd->dp, 2) != 0xFF))
+ {
+ /*
+ * Combine 2 bytes of frame type into one
+ * easy to use value
+ */
+ unsigned long byte3, byte4;
+
+ byte3 = RING_QUEUE_PEEK(&uvd->dp, 2);
+ byte4 = RING_QUEUE_PEEK(&uvd->dp, 3);
+ frame->header = (byte3 << 8) | byte4;
+#if 0
+ info("Header found.");
+#endif
+ RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, marker_len);
+ icam->has_hdr = 1;
+ break;
+ }
+ /* If we are still here then this doesn't look like a header */
+ RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ if (!icam->has_hdr) {
+ if (uvd->debug > 2)
+ info("Skipping frame, no header");
+ return scan_EndParse;
+ }
+
+ /* Header found */
+ icam->has_hdr = 1;
+ uvd->stats.header_count++;
+ frame->scanstate = ScanState_Lines;
+ frame->curline = 0;
+
+ if (flags & FLAGS_FORCE_TESTPATTERN) {
+ usbvideo_TestPattern(uvd, 1, 1);
+ return scan_NextFrame;
+ }
+ return scan_Continue;
+}
+
+/*
+ * ibmcam_parse_lines()
+ *
+ * Parse one line (interlaced) from the buffer, put
+ * decoded RGB value into the current frame buffer
+ * and add the written number of bytes (RGB) to
+ * the *pcopylen.
+ *
+ * History:
+ * 21-Jan-2000 Created.
+ * 12-Oct-2000 Reworked to reflect interlaced nature of the data.
+ */
+static enum ParseState ibmcam_parse_lines(
+ struct uvd *uvd,
+ struct usbvideo_frame *frame,
+ long *pcopylen)
+{
+ unsigned char *f;
+ ibmcam_t *icam;
+ unsigned int len, scanLength, scanHeight, order_uv, order_yc;
+ int v4l_linesize; /* V4L line offset */
+ const int hue_corr = (uvd->vpic.hue - 0x8000) >> 10; /* -32..+31 */
+ const int hue2_corr = (hue_correction - 128) / 4; /* -32..+31 */
+ const int ccm = 128; /* Color correction median - see below */
+ int y, u, v, i, frame_done=0, color_corr;
+ static unsigned char lineBuffer[640*3];
+ unsigned const char *chromaLine, *lumaLine;
+
+ assert(uvd != NULL);
+ assert(frame != NULL);
+ icam = IBMCAM_T(uvd);
+ assert(icam != NULL);
+ color_corr = (uvd->vpic.colour - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
+ RESTRICT_TO_RANGE(color_corr, -ccm, ccm+1);
+
+ v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
+
+ if (IBMCAM_T(uvd)->camera_model == IBMCAM_MODEL_4) {
+ /* Model 4 frame markers do not carry image size identification */
+ switch (uvd->videosize) {
+ case VIDEOSIZE_128x96:
+ case VIDEOSIZE_160x120:
+ case VIDEOSIZE_176x144:
+ scanLength = VIDEOSIZE_X(uvd->videosize);
+ scanHeight = VIDEOSIZE_Y(uvd->videosize);
+ break;
+ default:
+ err("ibmcam_parse_lines: Wrong mode.");
+ return scan_Out;
+ }
+ order_yc = 1; /* order_yc: true=Yc false=cY ('c'=either U or V) */
+ order_uv = 1; /* Always true in this algorithm */
+ } else {
+ switch (frame->header) {
+ case HDRSIG_MODEL1_128x96:
+ scanLength = 128;
+ scanHeight = 96;
+ order_uv = 1; /* U Y V Y ... */
+ break;
+ case HDRSIG_MODEL1_176x144:
+ scanLength = 176;
+ scanHeight = 144;
+ order_uv = 1; /* U Y V Y ... */
+ break;
+ case HDRSIG_MODEL1_352x288:
+ scanLength = 352;
+ scanHeight = 288;
+ order_uv = 0; /* Y V Y V ... */
+ break;
+ default:
+ err("Unknown header signature 00 FF 00 %02lX", frame->header);
+ return scan_NextFrame;
+ }
+ /* order_yc: true=Yc false=cY ('c'=either U or V) */
+ order_yc = (IBMCAM_T(uvd)->camera_model == IBMCAM_MODEL_2);
+ }
+
+ len = scanLength * 3;
+ assert(len <= sizeof(lineBuffer));
+
+ /*
+ * Lines are organized this way:
+ *
+ * I420:
+ * ~~~~
+ * <scanLength->
+ * ___________________________________
+ * |-----Y-----|---UVUVUV...UVUV-----| \
+ * |-----------+---------------------| \
+ * |<-- 176 -->|<------ 176*2 ------>| Total 72. lines (interlaced)
+ * |... ... | ... | /
+ * |<-- 352 -->|<------ 352*2 ------>| Total 144. lines (interlaced)
+ * |___________|_____________________| /
+ * \ \
+ * lumaLine chromaLine
+ */
+
+ /* Make sure there's enough data for the entire line */
+ if (RingQueue_GetLength(&uvd->dp) < len)
+ return scan_Out;
+
+ /* Suck one line out of the ring queue */
+ RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
+
+ /*
+ * Make sure that our writing into output buffer
+ * will not exceed the buffer. Mind that we may write
+ * not into current output scanline but in several after
+ * it as well (if we enlarge image vertically.)
+ */
+ if ((frame->curline + 2) >= VIDEOSIZE_Y(frame->request))
+ return scan_NextFrame;
+
+ /*
+ * Now we are sure that entire line (representing all 'scanLength'
+ * pixels from the camera) is available in the buffer. We
+ * start copying the line left-aligned to the V4L buffer.
+ * If the camera line is shorter then we should pad the V4L
+ * buffer with something (black) to complete the line.
+ */
+ assert(frame->data != NULL);
+ f = frame->data + (v4l_linesize * frame->curline);
+
+ /*
+ * To obtain chrominance data from the 'chromaLine' use this:
+ * v = chromaLine[0]; // 0-1:[0], 2-3:[4], 4-5:[8]...
+ * u = chromaLine[2]; // 0-1:[2], 2-3:[6], 4-5:[10]...
+ *
+ * Indices must be calculated this way:
+ * v_index = (i >> 1) << 2;
+ * u_index = (i >> 1) << 2 + 2;
+ *
+ * where 'i' is the column number [0..VIDEOSIZE_X(frame->request)-1]
+ */
+ lumaLine = lineBuffer;
+ chromaLine = lineBuffer + scanLength;
+ for (i = 0; i < VIDEOSIZE_X(frame->request); i++)
+ {
+ unsigned char rv, gv, bv; /* RGB components */
+
+ /* Check for various visual debugging hints (colorized pixels) */
+ if ((flags & FLAGS_DISPLAY_HINTS) && (icam->has_hdr)) {
+ /*
+ * This is bad and should not happen. This means that
+ * we somehow overshoot the line and encountered new
+ * frame! Obviously our camera/V4L frame size is out
+ * of whack. This cyan dot will help you to figure
+ * out where exactly the new frame arrived.
+ */
+ if (icam->has_hdr == 1) {
+ bv = 0; /* Yellow marker */
+ gv = 0xFF;
+ rv = 0xFF;
+ } else {
+ bv = 0xFF; /* Cyan marker */
+ gv = 0xFF;
+ rv = 0;
+ }
+ icam->has_hdr = 0;
+ goto make_pixel;
+ }
+
+ /*
+ * Check if we are still in range. We may be out of range if our
+ * V4L canvas is wider or taller than the camera "native" image.
+ * Then we quickly fill the remainder of the line with zeros to
+ * make black color and quit the horizontal scanning loop.
+ */
+ if (((frame->curline + 2) >= scanHeight) || (i >= scanLength)) {
+ const int j = i * V4L_BYTES_PER_PIXEL;
+#if USES_IBMCAM_PUTPIXEL
+ /* Refresh 'f' because we don't use it much with PUTPIXEL */
+ f = frame->data + (v4l_linesize * frame->curline) + j;
+#endif
+ memset(f, 0, v4l_linesize - j);
+ break;
+ }
+
+ y = lumaLine[i];
+ if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
+ rv = gv = bv = y;
+ else {
+ int off_0, off_2;
+
+ off_0 = (i >> 1) << 2;
+ off_2 = off_0 + 2;
+
+ if (order_yc) {
+ off_0++;
+ off_2++;
+ }
+ if (!order_uv) {
+ off_0 += 2;
+ off_2 -= 2;
+ }
+ u = chromaLine[off_0] + hue_corr;
+ v = chromaLine[off_2] + hue2_corr;
+
+ /* Apply color correction */
+ if (color_corr != 0) {
+ /* Magnify up to 2 times, reduce down to zero saturation */
+ u = 128 + ((ccm + color_corr) * (u - 128)) / ccm;
+ v = 128 + ((ccm + color_corr) * (v - 128)) / ccm;
+ }
+ YUV_TO_RGB_BY_THE_BOOK(y, u, v, rv, gv, bv);
+ }
+
+ make_pixel:
+ /*
+ * The purpose of creating the pixel here, in one,
+ * dedicated place is that we may need to make the
+ * pixel wider and taller than it actually is. This
+ * may be used if camera generates small frames for
+ * sake of frame rate (or any other reason.)
+ *
+ * The output data consists of B, G, R bytes
+ * (in this order).
+ */
+#if USES_IBMCAM_PUTPIXEL
+ RGB24_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
+#else
+ *f++ = bv;
+ *f++ = gv;
+ *f++ = rv;
+#endif
+ /*
+ * Typically we do not decide within a legitimate frame
+ * that we want to end the frame. However debugging code
+ * may detect marker of new frame within the data. Then
+ * this condition activates. The 'data' pointer is already
+ * pointing at the new marker, so we'd better leave it as is.
+ */
+ if (frame_done)
+ break; /* End scanning of lines */
+ }
+ /*
+ * Account for number of bytes that we wrote into output V4L frame.
+ * We do it here, after we are done with the scanline, because we
+ * may fill more than one output scanline if we do vertical
+ * enlargement.
+ */
+ frame->curline += 2;
+ if (pcopylen != NULL)
+ *pcopylen += 2 * v4l_linesize;
+ frame->deinterlace = Deinterlace_FillOddLines;
+
+ if (frame_done || (frame->curline >= VIDEOSIZE_Y(frame->request)))
+ return scan_NextFrame;
+ else
+ return scan_Continue;
+}
+
+/*
+ * ibmcam_model2_320x240_parse_lines()
+ *
+ * This procedure deals with a weird RGB format that is produced by IBM
+ * camera model 2 in modes 320x240 and above; 'x' below is 159 or 175,
+ * depending on horizontal size of the picture:
+ *
+ * <--- 160 or 176 pairs of RA,RB bytes ----->
+ * *-----------------------------------------* \
+ * | RA0 | RB0 | RA1 | RB1 | ... | RAx | RBx | \ This is pair of horizontal lines,
+ * |-----+-----+-----+-----+ ... +-----+-----| *- or one interlaced line, total
+ * | B0 | G0 | B1 | G1 | ... | Bx | Gx | / 120 or 144 such pairs which yield
+ * |=====+=====+=====+=====+ ... +=====+=====| / 240 or 288 lines after deinterlacing.
+ *
+ * Each group of FOUR bytes (RAi, RBi, Bi, Gi) where i=0..frame_width/2-1
+ * defines ONE pixel. Therefore this format yields 176x144 "decoded"
+ * resolution at best. I do not know why camera sends such format - the
+ * previous model (1) just used interlaced I420 and everyone was happy.
+ *
+ * I do not know what is the difference between RAi and RBi bytes. Both
+ * seemingly represent R component, but slightly vary in value (so that
+ * the picture looks a bit colored if one or another is used). I use
+ * them both as R component in attempt to at least partially recover the
+ * lost resolution.
+ */
+static enum ParseState ibmcam_model2_320x240_parse_lines(
+ struct uvd *uvd,
+ struct usbvideo_frame *frame,
+ long *pcopylen)
+{
+ unsigned char *f, *la, *lb;
+ unsigned int len;
+ int v4l_linesize; /* V4L line offset */
+ int i, j, frame_done=0, color_corr;
+ int scanLength, scanHeight;
+ static unsigned char lineBuffer[352*2];
+
+ switch (uvd->videosize) {
+ case VIDEOSIZE_320x240:
+ case VIDEOSIZE_352x240:
+ case VIDEOSIZE_352x288:
+ scanLength = VIDEOSIZE_X(uvd->videosize);
+ scanHeight = VIDEOSIZE_Y(uvd->videosize);
+ break;
+ default:
+ err("ibmcam_model2_320x240_parse_lines: Wrong mode.");
+ return scan_Out;
+ }
+
+ color_corr = (uvd->vpic.colour) >> 8; /* 0..+255 */
+ v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
+
+ len = scanLength * 2; /* See explanation above */
+ assert(len <= sizeof(lineBuffer));
+
+ /* Make sure there's enough data for the entire line */
+ if (RingQueue_GetLength(&uvd->dp) < len)
+ return scan_Out;
+
+ /* Suck one line out of the ring queue */
+ RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
+
+ /*
+ * Make sure that our writing into output buffer
+ * will not exceed the buffer. Mind that we may write
+ * not into current output scanline but in several after
+ * it as well (if we enlarge image vertically.)
+ */
+ if ((frame->curline + 2) >= VIDEOSIZE_Y(frame->request))
+ return scan_NextFrame;
+
+ la = lineBuffer;
+ lb = lineBuffer + scanLength;
+
+ /*
+ * Now we are sure that entire line (representing all
+ * VIDEOSIZE_X(frame->request)
+ * pixels from the camera) is available in the scratch buffer. We
+ * start copying the line left-aligned to the V4L buffer (which
+ * might be larger - not smaller, hopefully). If the camera
+ * line is shorter then we should pad the V4L buffer with something
+ * (black in this case) to complete the line.
+ */
+ f = frame->data + (v4l_linesize * frame->curline);
+
+ /* Fill the 2-line strip */
+ for (i = 0; i < VIDEOSIZE_X(frame->request); i++) {
+ int y, rv, gv, bv; /* RGB components */
+
+ j = i & (~1);
+
+ /* Check for various visual debugging hints (colorized pixels) */
+ if ((flags & FLAGS_DISPLAY_HINTS) && (IBMCAM_T(uvd)->has_hdr)) {
+ if (IBMCAM_T(uvd)->has_hdr == 1) {
+ bv = 0; /* Yellow marker */
+ gv = 0xFF;
+ rv = 0xFF;
+ } else {
+ bv = 0xFF; /* Cyan marker */
+ gv = 0xFF;
+ rv = 0;
+ }
+ IBMCAM_T(uvd)->has_hdr = 0;
+ goto make_pixel;
+ }
+
+ /*
+ * Check if we are still in range. We may be out of range if our
+ * V4L canvas is wider or taller than the camera "native" image.
+ * Then we quickly fill the remainder of the line with zeros to
+ * make black color and quit the horizontal scanning loop.
+ */
+ if (((frame->curline + 2) >= scanHeight) || (i >= scanLength)) {
+ const int j = i * V4L_BYTES_PER_PIXEL;
+#if USES_IBMCAM_PUTPIXEL
+ /* Refresh 'f' because we don't use it much with PUTPIXEL */
+ f = frame->data + (v4l_linesize * frame->curline) + j;
+#endif
+ memset(f, 0, v4l_linesize - j);
+ break;
+ }
+
+ /*
+ * Here I use RA and RB components, one per physical pixel.
+ * This causes fine vertical grid on the picture but may improve
+ * horizontal resolution. If you prefer replicating, use this:
+ * rv = la[j + 0]; ... or ... rv = la[j + 1];
+ * then the pixel will be replicated.
+ */
+ rv = la[i];
+ gv = lb[j + 1];
+ bv = lb[j + 0];
+
+ y = (rv + gv + bv) / 3; /* Brightness (badly calculated) */
+
+ if (flags & FLAGS_MONOCHROME) /* Use monochrome for debugging */
+ rv = gv = bv = y;
+ else if (color_corr != 128) {
+
+ /* Calculate difference between color and brightness */
+ rv -= y;
+ gv -= y;
+ bv -= y;
+
+ /* Scale differences */
+ rv = (rv * color_corr) / 128;
+ gv = (gv * color_corr) / 128;
+ bv = (bv * color_corr) / 128;
+
+ /* Reapply brightness */
+ rv += y;
+ gv += y;
+ bv += y;
+
+ /* Watch for overflows */
+ RESTRICT_TO_RANGE(rv, 0, 255);
+ RESTRICT_TO_RANGE(gv, 0, 255);
+ RESTRICT_TO_RANGE(bv, 0, 255);
+ }
+
+ make_pixel:
+ RGB24_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
+ }
+ /*
+ * Account for number of bytes that we wrote into output V4L frame.
+ * We do it here, after we are done with the scanline, because we
+ * may fill more than one output scanline if we do vertical
+ * enlargement.
+ */
+ frame->curline += 2;
+ *pcopylen += v4l_linesize * 2;
+ frame->deinterlace = Deinterlace_FillOddLines;
+
+ if (frame_done || (frame->curline >= VIDEOSIZE_Y(frame->request)))
+ return scan_NextFrame;
+ else
+ return scan_Continue;
+}
+
+static enum ParseState ibmcam_model3_parse_lines(
+ struct uvd *uvd,
+ struct usbvideo_frame *frame,
+ long *pcopylen)
+{
+ unsigned char *data;
+ const unsigned char *color;
+ unsigned int len;
+ int v4l_linesize; /* V4L line offset */
+ const int hue_corr = (uvd->vpic.hue - 0x8000) >> 10; /* -32..+31 */
+ const int hue2_corr = (hue_correction - 128) / 4; /* -32..+31 */
+ const int ccm = 128; /* Color correction median - see below */
+ int i, u, v, rw, data_w=0, data_h=0, color_corr;
+ static unsigned char lineBuffer[640*3];
+
+ color_corr = (uvd->vpic.colour - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/
+ RESTRICT_TO_RANGE(color_corr, -ccm, ccm+1);
+
+ v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
+
+ /* The header tells us what sort of data is in this frame */
+ switch (frame->header) {
+ /*
+ * Uncompressed modes (that are easy to decode).
+ */
+ case 0x0308:
+ data_w = 640;
+ data_h = 480;
+ break;
+ case 0x0208:
+ data_w = 320;
+ data_h = 240;
+ break;
+ case 0x020A:
+ data_w = 160;
+ data_h = 120;
+ break;
+ /*
+ * Compressed modes (ViCE - that I don't know how to decode).
+ */
+ case 0x0328: /* 640x480, best quality compression */
+ case 0x0368: /* 640x480, best frame rate compression */
+ case 0x0228: /* 320x240, best quality compression */
+ case 0x0268: /* 320x240, best frame rate compression */
+ case 0x02CA: /* 160x120, best quality compression */
+ case 0x02EA: /* 160x120, best frame rate compression */
+ /* Do nothing with this - not supported */
+ err("Unsupported mode $%04lx", frame->header);
+ return scan_NextFrame;
+ default:
+ /* Catch unknown headers, may help in learning new headers */
+ err("Strange frame->header=$%08lx", frame->header);
+ return scan_NextFrame;
+ }
+
+ /*
+ * Make sure that our writing into output buffer
+ * will not exceed the buffer. Note that we may write
+ * not into current output scanline but in several after
+ * it as well (if we enlarge image vertically.)
+ */
+ if ((frame->curline + 1) >= data_h) {
+ if (uvd->debug >= 3)
+ info("Reached line %d. (frame is done)", frame->curline);
+ return scan_NextFrame;
+ }
+
+ /* Make sure there's enough data for the entire line */
+ len = 3 * data_w; /* <y-data> <uv-data> */
+ assert(len <= sizeof(lineBuffer));
+
+ /* Make sure there's enough data for the entire line */
+ if (RingQueue_GetLength(&uvd->dp) < len)
+ return scan_Out;
+
+ /* Suck one line out of the ring queue */
+ RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
+
+ data = lineBuffer;
+ color = data + data_w; /* Point to where color planes begin */
+
+ /* Bottom-to-top scanning */
+ rw = (int)VIDEOSIZE_Y(frame->request) - (int)(frame->curline) - 1;
+ RESTRICT_TO_RANGE(rw, 0, VIDEOSIZE_Y(frame->request)-1);
+
+ for (i = 0; i < VIDEOSIZE_X(frame->request); i++) {
+ int y, rv, gv, bv; /* RGB components */
+
+ if (i < data_w) {
+ y = data[i]; /* Luminosity is the first line */
+
+ /* Apply static color correction */
+ u = color[i*2] + hue_corr;
+ v = color[i*2 + 1] + hue2_corr;
+
+ /* Apply color correction */
+ if (color_corr != 0) {
+ /* Magnify up to 2 times, reduce down to zero saturation */
+ u = 128 + ((ccm + color_corr) * (u - 128)) / ccm;
+ v = 128 + ((ccm + color_corr) * (v - 128)) / ccm;
+ }
+ } else
+ y = 0, u = v = 128;
+
+ YUV_TO_RGB_BY_THE_BOOK(y, u, v, rv, gv, bv);
+ RGB24_PUTPIXEL(frame, i, rw, rv, gv, bv); /* Done by deinterlacing now */
+ }
+ frame->deinterlace = Deinterlace_FillEvenLines;
+
+ /*
+ * Account for number of bytes that we wrote into output V4L frame.
+ * We do it here, after we are done with the scanline, because we
+ * may fill more than one output scanline if we do vertical
+ * enlargement.
+ */
+ frame->curline += 2;
+ *pcopylen += 2 * v4l_linesize;
+
+ if (frame->curline >= VIDEOSIZE_Y(frame->request)) {
+ if (uvd->debug >= 3) {
+ info("All requested lines (%ld.) done.",
+ VIDEOSIZE_Y(frame->request));
+ }
+ return scan_NextFrame;
+ } else
+ return scan_Continue;
+}
+
+/*
+ * ibmcam_model4_128x96_parse_lines()
+ *
+ * This decoder is for one strange data format that is produced by Model 4
+ * camera only in 128x96 mode. This is RGB format and here is its description.
+ * First of all, this is non-interlaced stream, meaning that all scan lines
+ * are present in the datastream. There are 96 consecutive blocks of data
+ * that describe all 96 lines of the image. Each block is 5*128 bytes long
+ * and carries R, G, B components. The format of the block is shown in the
+ * code below. First 128*2 bytes are interleaved R and G components. Then
+ * we have a gap (junk data) 64 bytes long. Then follow B and something
+ * else, also interleaved (this makes another 128*2 bytes). After that
+ * probably another 64 bytes of junk follow.
+ *
+ * History:
+ * 10-Feb-2001 Created.
+ */
+static enum ParseState ibmcam_model4_128x96_parse_lines(
+ struct uvd *uvd,
+ struct usbvideo_frame *frame,
+ long *pcopylen)
+{
+ const unsigned char *data_rv, *data_gv, *data_bv;
+ unsigned int len;
+ int i, v4l_linesize; /* V4L line offset */
+ const int data_w=128, data_h=96;
+ static unsigned char lineBuffer[128*5];
+
+ v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL;
+
+ /*
+ * Make sure that our writing into output buffer
+ * will not exceed the buffer. Note that we may write
+ * not into current output scanline but in several after
+ * it as well (if we enlarge image vertically.)
+ */
+ if ((frame->curline + 1) >= data_h) {
+ if (uvd->debug >= 3)
+ info("Reached line %d. (frame is done)", frame->curline);
+ return scan_NextFrame;
+ }
+
+ /*
+ * RGRGRG .... RGRG_____________B?B?B? ... B?B?____________
+ * <---- 128*2 ---><---- 64 ---><--- 128*2 ---><--- 64 --->
+ */
+
+ /* Make sure there's enough data for the entire line */
+ len = 5 * data_w;
+ assert(len <= sizeof(lineBuffer));
+
+ /* Make sure there's enough data for the entire line */
+ if (RingQueue_GetLength(&uvd->dp) < len)
+ return scan_Out;
+
+ /* Suck one line out of the ring queue */
+ RingQueue_Dequeue(&uvd->dp, lineBuffer, len);
+
+ data_rv = lineBuffer;
+ data_gv = lineBuffer + 1;
+ data_bv = lineBuffer + data_w*2 + data_w/2;
+ for (i = 0; i < VIDEOSIZE_X(frame->request); i++) {
+ int rv, gv, bv; /* RGB components */
+ if (i < data_w) {
+ const int j = i * 2;
+ gv = data_rv[j];
+ rv = data_gv[j];
+ bv = data_bv[j];
+ if (flags & FLAGS_MONOCHROME) {
+ unsigned long y;
+ y = rv + gv + bv;
+ y /= 3;
+ if (y > 0xFF)
+ y = 0xFF;
+ rv = gv = bv = (unsigned char) y;
+ }
+ } else {
+ rv = gv = bv = 0;
+ }
+ RGB24_PUTPIXEL(frame, i, frame->curline, rv, gv, bv);
+ }
+ frame->deinterlace = Deinterlace_None;
+ frame->curline++;
+ *pcopylen += v4l_linesize;
+
+ if (frame->curline >= VIDEOSIZE_Y(frame->request)) {
+ if (uvd->debug >= 3) {
+ info("All requested lines (%ld.) done.",
+ VIDEOSIZE_Y(frame->request));
+ }
+ return scan_NextFrame;
+ } else
+ return scan_Continue;
+}
+
+/*
+ * ibmcam_ProcessIsocData()
+ *
+ * Generic routine to parse the ring queue data. It employs either
+ * ibmcam_find_header() or ibmcam_parse_lines() to do most
+ * of work.
+ *
+ * History:
+ * 1/21/00 Created.
+ */
+static void ibmcam_ProcessIsocData(struct uvd *uvd,
+ struct usbvideo_frame *frame)
+{
+ enum ParseState newstate;
+ long copylen = 0;
+ int mod = IBMCAM_T(uvd)->camera_model;
+
+ while (1) {
+ newstate = scan_Out;
+ if (RingQueue_GetLength(&uvd->dp) > 0) {
+ if (frame->scanstate == ScanState_Scanning) {
+ newstate = ibmcam_find_header(uvd);
+ } else if (frame->scanstate == ScanState_Lines) {
+ if ((mod == IBMCAM_MODEL_2) &&
+ ((uvd->videosize == VIDEOSIZE_352x288) ||
+ (uvd->videosize == VIDEOSIZE_320x240) ||
+ (uvd->videosize == VIDEOSIZE_352x240)))
+ {
+ newstate = ibmcam_model2_320x240_parse_lines(
+ uvd, frame, &copylen);
+ } else if (mod == IBMCAM_MODEL_4) {
+ /*
+ * Model 4 cameras (IBM NetCamera) use Model 2 decoder (RGB)
+ * for 320x240 and above; 160x120 and 176x144 uses Model 1
+ * decoder (YUV), and 128x96 mode uses ???
+ */
+ if ((uvd->videosize == VIDEOSIZE_352x288) ||
+ (uvd->videosize == VIDEOSIZE_320x240) ||
+ (uvd->videosize == VIDEOSIZE_352x240))
+ {
+ newstate = ibmcam_model2_320x240_parse_lines(uvd, frame, &copylen);
+ } else if (uvd->videosize == VIDEOSIZE_128x96) {
+ newstate = ibmcam_model4_128x96_parse_lines(uvd, frame, &copylen);
+ } else {
+ newstate = ibmcam_parse_lines(uvd, frame, &copylen);
+ }
+ } else if (mod == IBMCAM_MODEL_3) {
+ newstate = ibmcam_model3_parse_lines(uvd, frame, &copylen);
+ } else {
+ newstate = ibmcam_parse_lines(uvd, frame, &copylen);
+ }
+ }
+ }
+ if (newstate == scan_Continue)
+ continue;
+ else if ((newstate == scan_NextFrame) || (newstate == scan_Out))
+ break;
+ else
+ return; /* scan