<html><head><meta name="qrichtext" content="1" /></head><body style="font-size:10pt;font-family:Sans">
<p><span style="font-family:Monospace">Hello,</span></p>
<p><span style="font-family:Monospace"> could you integrate the SBGGR8 pixel format in VLC, which is defined by</span></p>
<p><span style="font-family:Monospace">the V4L2 API (in videodev2.h)? This way I could write and test a simple v4l2 input module for webcams (and video capture devices in general) in 1 day.</span></p>
<p></p>
<p><span style="font-family:Monospace">The functions below that I wrote for xawtv might help. I think they are already clear enough to be mostly copied and placed in the right place without any problems. The code is released under GPL.</span></p>
<p></p>
<p><span style="font-family:Monospace">Thanks.</span></p>
<p></p>
<p><span style="font-family:Monospace">#define RED 0</span></p>
<p><span style="font-family:Monospace">#define GREEN 1</span></p>
<p><span style="font-family:Monospace">#define BLUE 2</span></p>
<p></p>
<p><span style="font-family:Monospace">#define avg2(x,y) ((((int)(x)) + ((int)(y))) / 2)</span></p>
<p><span style="font-family:Monospace">#define avg3(x,y,z) ((((int)(x)) + ((int)(y)) + ((int)(z))) / 3)</span></p>
<p><span style="font-family:Monospace">#define avg4(w,x,y,z) ((((int)(w)) + ((int)(x)) + ((int)(y)) + ((int)(z))) / 4)</span></p>
<p></p>
<p><span style="font-family:Monospace">/* ------------------------------------------------------------------- */</span></p>
<p><span style="font-family:Monospace">/* bayer bgbg...grgr...to rgb24 */</span></p>
<p></p>
<p><span style="font-family:Monospace">/*</span></p>
<p><span style="font-family:Monospace"> bgbgbg</span></p>
<p><span style="font-family:Monospace"> grgrgr</span></p>
<p><span style="font-family:Monospace"> bgbgbg</span></p>
<p><span style="font-family:Monospace"> grgrgr</span></p>
<p><span style="font-family:Monospace">*/</span></p>
<p></p>
<p><span style="font-family:Monospace">static void</span></p>
<p><span style="font-family:Monospace">sbggr8_</span><span style="font-family:Monospace;color:#ff0000">to</span><span style="font-family:Monospace">_rgb24(void *h, struct ng_video_buf *out, struct ng_video_buf *in)</span></p>
<p><span style="font-family:Monospace">{</span></p>
<p><span style="font-family:Monospace"> unsigned char *restrict s, *restrict d;</span></p>
<p><span style="font-family:Monospace"> unsigned long x, y, i, j;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d = out->data;</span></p>
<p><span style="font-family:Monospace"> s = in->data;</span></p>
<p><span style="font-family:Monospace"> x = in->fmt.width;</span></p>
<p><span style="font-family:Monospace"> y = in->fmt.height;</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* upper left corner */</span></p>
<p><span style="font-family:Monospace"> d[0 + </span><span style="font-family:Monospace;color:#ff0000">RED</span><span style="font-family:Monospace">] = s[x + 1];</span></p>
<p><span style="font-family:Monospace"> d[0 + </span><span style="font-family:Monospace;color:#ff0000">BLUE</span><span style="font-family:Monospace">] = s[0];</span></p>
<p><span style="font-family:Monospace"> d[0 + </span><span style="font-family:Monospace;color:#ff0000">GREEN</span><span style="font-family:Monospace">] = ((int)s[1] + (int)s[x]) / 2;</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* top line (minus corners) */</span></p>
<p><span style="font-family:Monospace"> i = 1;</span></p>
<p><span style="font-family:Monospace"> while (i < x - 2) {</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + RED] = s[x + i];</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + GREEN] = s[i];</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + BLUE] = avg2(s[i - 1], s[i + 1]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d[i * 3 + RED] = avg2(s[x + i - 1], s[x + i + 1]);</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + GREEN] = avg3(s[i - 1], s[i + 1], s[x + 1]);</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + BLUE] = s[i];</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* upper right corner */</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + RED] = s[x + i];</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + GREEN] = s[i];</span></p>
<p><span style="font-family:Monospace"> d[i * 3 + BLUE] = s[i - 1];</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* middle lines */</span></p>
<p><span style="font-family:Monospace"> j = 1;</span></p>
<p><span style="font-family:Monospace"> while (j < y - 2) {</span></p>
<p><span style="font-family:Monospace"> d[j * x * 3 + RED] = s[j * x + 1];</span></p>
<p><span style="font-family:Monospace"> d[j * x * 3 + GREEN] = s[j * x];</span></p>
<p><span style="font-family:Monospace"> d[j * x * 3 + BLUE] = avg2(s[(j - 1) * x], s[(j + 1) * x]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i = 1;</span></p>
<p><span style="font-family:Monospace"> while (i < x - 2) {</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = avg4(s[(j - 1) * x + i],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i + 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = avg4(s[(j - 1) * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[(j - 1) * x + i + 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i + 1]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = avg2(s[j * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i + 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = avg2(s[(j - 1) * x + i],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = avg2(s[(j - 1) * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i - 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = avg2(s[(j - 1) * x + i],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> j++;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = avg2(s[(j - 1) * x + i + 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i + 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = avg3(s[(j - 1) * x + i],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i + 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i = 1;</span></p>
<p><span style="font-family:Monospace"> while (i < x - 2) {</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = avg2(s[(j - 1) * x + i],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = avg2(s[j * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i + 1]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = avg4(s[(j - 1) * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[(j - 1) * x + i + 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i + 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = avg4(s[(j - 1) * x + i],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[j * x + i + 1],</span></p>
<p><span style="font-family:Monospace"> s[(j + 1) * x + i]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = s[j * x + i];</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p></p>
<p><span style="font-family:Monospace"> j++;</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* lower left corner */</span></p>
<p><span style="font-family:Monospace"> d[(j * x) * 3 + RED] = s[j * x + 1];</span></p>
<p><span style="font-family:Monospace"> d[(j * x) * 3 + GREEN] = s[j * x];</span></p>
<p><span style="font-family:Monospace"> d[(j * x) * 3 + BLUE] = s[(j - 1) * x];</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* bottom line */</span></p>
<p><span style="font-family:Monospace"> i = 1;</span></p>
<p><span style="font-family:Monospace"> while (i < x - 2) {</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = avg2(s[j * x + i - 1], s[j * x + i + 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = avg2(s[(j - 1) * x + i - 1],</span></p>
<p><span style="font-family:Monospace"> s[(j - 1) * x + i + 1]);</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = avg2(s[j * x + i - 1], s[j * x + i + 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = s[(j - 1) * x + i];</span></p>
<p></p>
<p><span style="font-family:Monospace"> i++;</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p></p>
<p><span style="font-family:Monospace"> /* lower right corner */</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + RED] = s[j * x + i];</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + GREEN] = avg2(s[(j - 1) * x + i], s[j * x + i - 1]);</span></p>
<p><span style="font-family:Monospace"> d[(j * x + i) * 3 + BLUE] = s[(j - 1) * x + i - 1];</span></p>
<p><span style="font-family:Monospace">}</span></p>
<p></p>
<p><span style="font-family:Monospace">/* ------------------------------------------------------------------- */</span></p>
<p><span style="font-family:Monospace">/* bayer bgbg...grgr...to rgb16 */</span></p>
<p></p>
<p><span style="font-family:Monospace">static void</span></p>
<p><span style="font-family:Monospace">sbggr8_to_rgb16(void *p, struct ng_video_buf *out, struct ng_video_buf *in)</span></p>
<p><span style="font-family:Monospace">{</span></p>
<p><span style="font-family:Monospace"> unsigned char *restrict s;</span></p>
<p><span style="font-family:Monospace"> uint16_t *d1, *d2;</span></p>
<p><span style="font-family:Monospace"> unsigned long i, j, x, y, w, h;</span></p>
<p><span style="font-family:Monospace"> unsigned char r, g1, g2, b, g;</span></p>
<p></p>
<p><span style="font-family:Monospace"> s = in->data;</span></p>
<p><span style="font-family:Monospace"> w = in->fmt.width;</span></p>
<p><span style="font-family:Monospace"> h = in->fmt.height;</span></p>
<p><span style="font-family:Monospace"> d1 = (uint16_t *)out->data;</span></p>
<p><span style="font-family:Monospace"> d2 = d1 + w;</span></p>
<p><span style="font-family:Monospace"> i = 0;</span></p>
<p><span style="font-family:Monospace"> j = w;</span></p>
<p></p>
<p><span style="font-family:Monospace"> for (y = 0; y < h; y += 2, i += w * 2, j += w * 2, d1 += w, d2 += w) {</span></p>
<p><span style="font-family:Monospace"> for (x = 0; x < w; x += 2) {</span></p>
<p></p>
<p><span style="font-family:Monospace"> b = s[i + x] ;</span></p>
<p><span style="font-family:Monospace"> g1 = s[i + 1 + x];</span></p>
<p><span style="font-family:Monospace"> g2 = s[j + x];</span></p>
<p><span style="font-family:Monospace"> r = s[j + 1 + x];</span></p>
<p></p>
<p><span style="font-family:Monospace"> g = (g1 >> 1) + (g2 >> 1);</span></p>
<p></p>
<p><span style="font-family:Monospace"> *d1++ = (((b) & 0xF8) >> 3 |</span></p>
<p><span style="font-family:Monospace"> (((g1) & 0xFC) << 3) |</span></p>
<p><span style="font-family:Monospace"> (((r) & 0xF8) << 8)) ;</span></p>
<p></p>
<p><span style="font-family:Monospace"> *d1++ = (((b) & 0xF8) >> 3 |</span></p>
<p><span style="font-family:Monospace"> (((g) & 0xFC) << 3) |</span></p>
<p><span style="font-family:Monospace"> (((r) & 0xF8) << 8)) ;</span></p>
<p></p>
<p><span style="font-family:Monospace"> *d2++ = (((b) & 0xF8) >> 3 |</span></p>
<p><span style="font-family:Monospace"> (((g) & 0xFC) << 3) |</span></p>
<p><span style="font-family:Monospace"> (((r) & 0xF8) << 8)) ;</span></p>
<p></p>
<p><span style="font-family:Monospace"> *d2++ = (((b) & 0xF8) >> 3 |</span></p>
<p><span style="font-family:Monospace"> (((g2) & 0xFC) << 3) |</span></p>
<p><span style="font-family:Monospace"> (((r) & 0xF8) << 8)) ;</span></p>
<p></p>
<p><span style="font-family:Monospace"> }</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p><span style="font-family:Monospace">}</span></p>
<p></p>
<p><span style="font-family:Monospace">static void</span></p>
<p><span style="font-family:Monospace">sbggr8_to_rgb32(void *p, struct ng_video_buf *out, struct ng_video_buf *in)</span></p>
<p><span style="font-family:Monospace">{</span></p>
<p><span style="font-family:Monospace"> unsigned char *restrict s;</span></p>
<p><span style="font-family:Monospace"> unsigned char *d1, *d2;</span></p>
<p><span style="font-family:Monospace"> unsigned long i, j, x, y, w, h;</span></p>
<p><span style="font-family:Monospace"> unsigned char r, g1, g2, b, g;</span></p>
<p></p>
<p><span style="font-family:Monospace"> s = in->data;</span></p>
<p><span style="font-family:Monospace"> w = in->fmt.width;</span></p>
<p><span style="font-family:Monospace"> h = in->fmt.height;</span></p>
<p><span style="font-family:Monospace"> d1 = out->data;</span></p>
<p><span style="font-family:Monospace"> d2 = d1 + 4*w;</span></p>
<p><span style="font-family:Monospace"> i = 0;</span></p>
<p><span style="font-family:Monospace"> j = w;</span></p>
<p></p>
<p><span style="font-family:Monospace"> for (y = 0; y < h; y += 2, i += w * 2, j += w * 2, d1 += 4*w, d2 += 4*w) {</span></p>
<p><span style="font-family:Monospace"> for (x = 0; x < w; x += 2) {</span></p>
<p></p>
<p><span style="font-family:Monospace"> b = s[i + x] ;</span></p>
<p><span style="font-family:Monospace"> g1 = s[i + 1 + x];</span></p>
<p><span style="font-family:Monospace"> g2 = s[j + x];</span></p>
<p><span style="font-family:Monospace"> r = s[j + 1 + x];</span></p>
<p><span style="font-family:Monospace"> g = (g1 >> 1) + (g2 >> 1);</span></p>
<p></p>
<p><span style="font-family:Monospace"> d1[0] = (r);</span></p>
<p><span style="font-family:Monospace"> d1[1] = (g1);</span></p>
<p><span style="font-family:Monospace"> d1[2] = (b);</span></p>
<p><span style="font-family:Monospace"> d1 += 4;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d1[0] = (r);</span></p>
<p><span style="font-family:Monospace"> d1[1] = (g);</span></p>
<p><span style="font-family:Monospace"> d1[2] = (b);</span></p>
<p><span style="font-family:Monospace"> d1 += 4;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d2[0] = (r);</span></p>
<p><span style="font-family:Monospace"> d2[1] = (g);</span></p>
<p><span style="font-family:Monospace"> d2[2] = (b);</span></p>
<p><span style="font-family:Monospace"> d2 += 4;</span></p>
<p></p>
<p><span style="font-family:Monospace"> d2[0] = (r);</span></p>
<p><span style="font-family:Monospace"> d2[1] = (g2);</span></p>
<p><span style="font-family:Monospace"> d2[2] = (b);</span></p>
<p><span style="font-family:Monospace"> d2 += 4;</span></p>
<p></p>
<p><span style="font-family:Monospace"> }</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p><span style="font-family:Monospace">}</span></p>
<p></p>
<p><span style="font-family:Monospace">static void</span></p>
<p><span style="font-family:Monospace">sbggr8_to_yuv420p(void *p, struct ng_video_buf *out, struct ng_video_buf *in)</span></p>
<p><span style="font-family:Monospace">{</span></p>
<p><span style="font-family:Monospace"> unsigned char *restrict s;</span></p>
<p><span style="font-family:Monospace"> unsigned char *d1, *d2, *U, *V;</span></p>
<p><span style="font-family:Monospace"> unsigned long i, j, x, y, w, h;</span></p>
<p><span style="font-family:Monospace"> unsigned char tr, tb, r, g1, g2, b, g, y1, y2, y3, y4;</span></p>
<p><span style="font-family:Monospace"> int u, v;</span></p>
<p></p>
<p><span style="font-family:Monospace"> #define CLIP(color) ((color > 0xff) ? 0xff : ((color < 0) ? 0 : color))</span></p>
<p></p>
<p><span style="font-family:Monospace"> s = in->data;</span></p>
<p><span style="font-family:Monospace"> w = in->fmt.width;</span></p>
<p><span style="font-family:Monospace"> h = in->fmt.height;</span></p>
<p><span style="font-family:Monospace"> d1 = out->data;</span></p>
<p><span style="font-family:Monospace"> d2 = d1 + w;</span></p>
<p><span style="font-family:Monospace"> i = 0;</span></p>
<p><span style="font-family:Monospace"> j = w;</span></p>
<p><span style="font-family:Monospace"> U = d1 + w*h;</span></p>
<p><span style="font-family:Monospace"> V = U + ((w*h) >> 2);</span></p>
<p></p>
<p><span style="font-family:Monospace"> for (y = 0; y < h; y += 2, i += w * 2, j += w * 2, d1 += w, d2 += w) {</span></p>
<p><span style="font-family:Monospace"> for (x = 0; x < w; x += 2) {</span></p>
<p></p>
<p><span style="font-family:Monospace"> tb = s[i + x] ;</span></p>
<p><span style="font-family:Monospace"> g1 = s[i + 1 + x];</span></p>
<p><span style="font-family:Monospace"> g2 = s[j + x];</span></p>
<p><span style="font-family:Monospace"> tr = s[j + 1 + x];</span></p>
<p></p>
<p><span style="font-family:Monospace"> g = (g1 >> 1) + (g2 >> 1);</span></p>
<p><span style="font-family:Monospace"> r = ((tr << 8) - (tr << 4) - (tr << 3)) >> 10;</span></p>
<p><span style="font-family:Monospace"> b = ((tb << 7) >> 10);</span></p>
<p><span style="font-family:Monospace"> g1 = ((g1 << 9) + (g1 << 7) + (g1 << 5)) >> 10;</span></p>
<p><span style="font-family:Monospace"> g2 = ((g2 << 9) + (g2 << 7) + (g2 << 5)) >> 10;</span></p>
<p><span style="font-family:Monospace"> g = ((g << 9) + (g << 7) + (g << 5)) >> 10;</span></p>
<p></p>
<p><span style="font-family:Monospace"> y1 = CLIP((r + g1 + b));</span></p>
<p><span style="font-family:Monospace"> y2 = CLIP((r + g + b));</span></p>
<p><span style="font-family:Monospace"> y3 = CLIP((r + g + b));</span></p>
<p><span style="font-family:Monospace"> y4 = CLIP((r + g2 + b));</span></p>
<p></p>
<p><span style="font-family:Monospace"> *d1++ = y1;</span></p>
<p><span style="font-family:Monospace"> *d1++ = y2;</span></p>
<p><span style="font-family:Monospace"> *d2++ = y3;</span></p>
<p><span style="font-family:Monospace"> *d2++ = y4;</span></p>
<p></p>
<p><span style="font-family:Monospace"> v = tr - ((y1 + y2 + y3 + y4) >> 2);</span></p>
<p><span style="font-family:Monospace"> u = ((v << 9) + (v << 7) + (v << 5)) >> 10;</span></p>
<p><span style="font-family:Monospace"> v = (tb - ((y1 + y2 + y3 + y4) >> 2)) >> 1;</span></p>
<p></p>
<p><span style="font-family:Monospace"> *U++ = 128 + v;</span></p>
<p><span style="font-family:Monospace"> *V++ = 128 + u;</span></p>
<p></p>
<p><span style="font-family:Monospace"> }</span></p>
<p><span style="font-family:Monospace"> }</span></p>
<p><span style="font-family:Monospace">}</span></p>
<p></p>
</body></html>