bayer2rgb: Rewrite signal processing. Much faster.

This commit is contained in:
David Schleef 2011-05-30 16:56:33 -07:00
parent 03bb5dc8ba
commit f0c75b06a7

View file

@ -382,284 +382,282 @@ gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
return FALSE; return FALSE;
} }
/* #define RECONSTRUCT_SQUARE(x) \
* We define values for the colors, just to make the code more readable. do { \
*/ int _h1 = next[i-1]; \
#define RED 0 /* Pure red element */ int _h2 = prev[i+1]; \
#define GREENB 1 /* Green element which is on a blue line */ int _v1 = next[i+1]; \
#define BLUE 2 /* Pure blue element */ int _v2 = prev[i-1]; \
#define GREENR 3 /* Green element which is on a red line */ (x) = (_h1+_h2+_v1+_v2+2)>>2; \
} while (0)
static int #define RECONSTRUCT_DIAMOND(x) \
get_pixel_type (GstBayer2RGB * filter, int x, int y) do { \
int _h1 = src[i-1]; \
int _h2 = src[i+1]; \
int _v1 = next[i]; \
int _v2 = prev[i]; \
(x) = (_h1+_h2+_v1+_v2+2)>>2; \
} while (0)
#define RECONSTRUCT_HORIZ(x) \
do { \
(x) = (src[i-1] + src[i+1] + 1) >> 1; \
} while (0)
#define RECONSTRUCT_VERT(x) \
do { \
(x) = (next[i] + prev[i] + 1) >> 1; \
} while (0)
static void
reconstruct_blue_green (GstBayer2RGB * bayer2rgb, uint8_t * dest,
uint8_t * src, int src_stride, int blue_loc)
{ {
int type; int i;
int r, g, b;
uint8_t *prev;
uint8_t *next;
int width = bayer2rgb->width;
if (((x ^ filter->format) & 1)) { prev = src - src_stride;
if ((y ^ (filter->format >> 1)) & 1) next = src + src_stride;
type = RED;
else i = 0;
type = GREENB; if ((i & 1) == blue_loc) {
b = src[i];
r = (next[i + 1] + prev[i + 1] + 1) >> 1;
g = (next[i] + prev[i] + 1) >> 1;
} else { } else {
if ((y ^ (filter->format >> 1)) & 1) b = src[i + 1];
type = GREENR; r = (next[i] + prev[i] + 1) >> 1;
else g = src[i];
type = BLUE;
} }
return type; dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
for (i = 1; i < width - 1; i++) {
if ((i & 1) == blue_loc) {
b = src[i];
RECONSTRUCT_SQUARE (r);
RECONSTRUCT_DIAMOND (g);
} else {
RECONSTRUCT_HORIZ (b);
RECONSTRUCT_VERT (r);
g = src[i];
} }
dest[i * 4 + bayer2rgb->r_off] = r;
/* Routine to generate the top and bottom edges (not including corners) */ dest[i * 4 + bayer2rgb->g_off] = g;
static void dest[i * 4 + bayer2rgb->b_off] = b;
hborder (uint8_t * input, uint8_t * output, int bot_top,
int typ, GstBayer2RGB * filter)
{
uint8_t *op; /* output pointer */
uint8_t *ip; /* input pointer */
uint8_t *nx; /* next line pointer */
int ix; /* loop index */
op = output + (bot_top * filter->width * (filter->height - 1) + 1) *
filter->pixsize;
ip = input + bot_top * filter->stride * (filter->height - 1);
/* calculate minus or plus one line, depending upon bot_top flag */
nx = ip + (1 - 2 * bot_top) * filter->stride;
/* Stepping horizontally */
for (ix = 1; ix < filter->width - 1; ix++, op += filter->pixsize) {
switch (typ) {
case RED:
op[filter->r_off] = ip[ix];
op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
op[filter->b_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
typ = GREENR;
break;
case GREENR:
op[filter->r_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
op[filter->g_off] = ip[ix];
op[filter->b_off] = nx[ix];
typ = RED;
break;
case GREENB:
op[filter->r_off] = nx[ix];
op[filter->g_off] = ip[ix];
op[filter->b_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
typ = BLUE;
break;
case BLUE:
op[filter->r_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
op[filter->b_off] = ip[ix];
typ = GREENB;
break;
} }
if ((i & 1) == blue_loc) {
b = src[i];
r = (next[i - 1] + prev[i - 1] + 1) >> 1;
g = (next[i] + prev[i] + 1) >> 1;
} else {
b = src[i - 1];
r = (next[i] + prev[i] + 1) >> 1;
g = src[i];
} }
} dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
/* Routine to generate the left and right edges, not including corners */ dest[i * 4 + bayer2rgb->b_off] = b;
static void
vborder (uint8_t * input, uint8_t * output, int right_left,
int typ, GstBayer2RGB * filter)
{
uint8_t *op; /* output pointer */
uint8_t *ip; /* input pointer */
uint8_t *la; /* line above pointer */
uint8_t *lb; /* line below pointer */
int ix; /* loop index */
int lr; /* 'left-right' flag - +1 is right, -1 is left */
lr = (1 - 2 * right_left);
/* stepping vertically */
for (ix = 1; ix < filter->height - 1; ix++) {
ip = input + right_left * (filter->width - 1) + ix * filter->stride;
op = output + (right_left * (filter->width - 1) + ix * filter->width) *
filter->pixsize;
la = ip + filter->stride;
lb = ip - filter->stride;
switch (typ) {
case RED:
op[filter->r_off] = ip[0];
op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
typ = GREENB;
break;
case GREENR:
op[filter->r_off] = ip[lr];
op[filter->g_off] = ip[0];
op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
typ = BLUE;
break;
case GREENB:
op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
op[filter->g_off] = ip[0];
op[filter->b_off] = ip[lr];
typ = RED;
break;
case BLUE:
op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
op[filter->b_off] = ip[0];
typ = GREENR;
break;
}
}
}
/* Produce the four (top, bottom, left, right) edges */
static void
do_row0_col0 (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
{
/* Horizontal edges */
hborder (input, output, 0, get_pixel_type (filter, 1, 0), filter);
hborder (input, output, 1, get_pixel_type (filter, 1, filter->height - 1),
filter);
/* Vertical edges */
vborder (input, output, 0, get_pixel_type (filter, 0, 1), filter);
vborder (input, output, 1, get_pixel_type (filter, filter->width - 1, 1),
filter);
} }
static void static void
corner (uint8_t * input, uint8_t * output, int x, int y, reconstruct_green_red (GstBayer2RGB * bayer2rgb, uint8_t * dest,
int xd, int yd, int typ, GstBayer2RGB * filter) uint8_t * src, int src_stride, int red_loc)
{ {
uint8_t *ip; /* input pointer */ int i;
uint8_t *op; /* output pointer */ int r, g, b;
uint8_t *nx; /* adjacent line */ uint8_t *prev;
uint8_t *next;
int width = bayer2rgb->width;
op = output + y * filter->width * filter->pixsize + x * filter->pixsize; prev = src - src_stride;
ip = input + y * filter->stride + x; next = src + src_stride;
nx = ip + yd * filter->stride;
switch (typ) { i = 0;
case RED: if ((i & 1) == red_loc) {
op[filter->r_off] = ip[0]; r = src[i];
op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2; b = (next[i + 1] + prev[i + 1] + 1) >> 1;
op[filter->b_off] = nx[xd]; g = (next[i] + prev[i] + 1) >> 1;
break; } else {
case GREENR: r = src[i + 1];
op[filter->r_off] = ip[xd]; b = (next[i] + prev[i] + 1) >> 1;
op[filter->g_off] = ip[0]; g = src[i];
op[filter->b_off] = nx[0];
break;
case GREENB:
op[filter->r_off] = nx[0];
op[filter->g_off] = ip[0];
op[filter->b_off] = ip[xd];
break;
case BLUE:
op[filter->r_off] = nx[xd];
op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
op[filter->b_off] = ip[0];
break;
} }
dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
for (i = 1; i < width - 1; i++) {
if ((i & 1) == red_loc) {
r = src[i];
RECONSTRUCT_SQUARE (b);
RECONSTRUCT_DIAMOND (g);
} else {
RECONSTRUCT_HORIZ (r);
RECONSTRUCT_VERT (b);
g = src[i];
}
dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
}
if ((i & 1) == red_loc) {
r = src[i];
b = (next[i - 1] + prev[i - 1] + 1) >> 1;
g = (next[i] + prev[i] + 1) >> 1;
} else {
r = src[i - 1];
b = (next[i] + prev[i] + 1) >> 1;
g = src[i];
}
dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
} }
static void static void
do_corners (uint8_t * input, uint8_t * output, GstBayer2RGB * filter) reconstruct_blue_green_edge (GstBayer2RGB * bayer2rgb, uint8_t * dest,
uint8_t * src, int src_stride, int blue_loc, int offset)
{ {
/* Top left */ int i;
corner (input, output, 0, 0, 1, 1, get_pixel_type (filter, 0, 0), filter); int r, g, b;
/* Bottom left */ uint8_t *next;
corner (input, output, 0, filter->height - 1, 1, -1, int width = bayer2rgb->width;
get_pixel_type (filter, 0, filter->height - 1), filter);
/* Top right */ next = src + offset * src_stride;
corner (input, output, filter->width - 1, 0, -1, 0,
get_pixel_type (filter, filter->width - 1, 0), filter); i = 0;
/* Bottom right */ if ((i & 1) == blue_loc) {
corner (input, output, filter->width - 1, filter->height - 1, -1, -1, b = src[i];
get_pixel_type (filter, filter->width - 1, filter->height - 1), filter); r = next[i + 1];
g = next[i];
} else {
b = src[i + 1];
r = next[i];
g = src[i];
}
dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
for (i = 1; i < width - 1; i++) {
if ((i & 1) == blue_loc) {
b = src[i];
r = (next[i - 1] + next[i + 1] + 1) >> 1;
g = (src[i - 1] + src[i + 1] + 1) >> 1;
} else {
b = (src[i - 1] + src[i + 1] + 1) >> 1;
r = next[i];
g = src[i];
}
dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
}
if ((i & 1) == blue_loc) {
b = src[i];
r = next[i - 1];
g = next[i];
} else {
b = src[i - 1];
r = next[i];
g = src[i];
}
dest[i * 4 + bayer2rgb->r_off] = r;
dest[i * 4 + bayer2rgb->g_off] = g;
dest[i * 4 + bayer2rgb->b_off] = b;
} }
static void static void
do_body (uint8_t * input, uint8_t * output, GstBayer2RGB * filter) reconstruct_green_red_edge (GstBayer2RGB * bayer2rgb, uint8_t * dest,
uint8_t * src, int src_stride, int red_loc, int offset)
{ {
int ip, op; /* input and output pointers */ int i;
int w, h; /* loop indices */ int r, g, b;
int type; /* calculated colour of current element */ uint8_t *next;
int a1, a2; int width = bayer2rgb->width;
int v1, v2, h1, h2;
/* next = src + offset * src_stride;
* We are processing row (line) by row, starting with the second
* row and continuing through the next to last. Each row is processed i = 0;
* column by column, starting with the second and continuing through if ((i & 1) == red_loc) {
* to the next to last. r = src[i];
*/ b = next[i + 1];
for (h = 1; h < filter->height - 1; h++) { g = next[i];
/* } else {
* Remember we are processing "row by row". For each row, we need r = src[i + 1];
* to set the type of the first element to be processed. Since we b = next[i];
* have already processed the edges, the "first element" will be g = src[i];
* the pixel at position (1,1). Assuming BG format, this should }
* be RED for odd-numbered rows and GREENB for even rows. dest[i * 4 + bayer2rgb->r_off] = r;
*/ dest[i * 4 + bayer2rgb->g_off] = g;
type = get_pixel_type (filter, 1, h); dest[i * 4 + bayer2rgb->b_off] = b;
/* Calculate the starting position for the row */ for (i = 1; i < width - 1; i++) {
op = h * filter->width * filter->pixsize; /* output (converted) pos */ if ((i & 1) == red_loc) {
ip = h * filter->stride; /* input (bayer data) pos */ r = src[i];
for (w = 1; w < filter->width - 1; w++) { b = (next[i - 1] + next[i + 1] + 1) >> 1;
op += filter->pixsize; /* we are processing "horizontally" */ g = (src[i - 1] + src[i + 1] + 1) >> 1;
ip++; } else {
switch (type) { r = (src[i - 1] + src[i + 1] + 1) >> 1;
case RED: b = next[i];
output[op + filter->r_off] = input[ip]; g = src[i];
output[op + filter->b_off] = (input[ip - filter->stride - 1] + }
input[ip - filter->stride + 1] + dest[i * 4 + bayer2rgb->r_off] = r;
input[ip + filter->stride - 1] + dest[i * 4 + bayer2rgb->g_off] = g;
input[ip + filter->stride + 1] + 2) / 4; dest[i * 4 + bayer2rgb->b_off] = b;
v1 = input[ip + filter->stride]; }
v2 = input[ip - filter->stride]; if ((i & 1) == red_loc) {
h1 = input[ip + 1]; r = src[i];
h2 = input[ip - 1]; b = next[i - 1];
a1 = abs (v1 - v2); g = next[i];
a2 = abs (h1 - h2); } else {
if (a1 < a2) r = src[i - 1];
output[op + filter->g_off] = (v1 + v2 + 1) / 2; b = next[i];
else if (a1 > a2) g = src[i];
output[op + filter->g_off] = (h1 + h2 + 1) / 2; }
else dest[i * 4 + bayer2rgb->r_off] = r;
output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4; dest[i * 4 + bayer2rgb->g_off] = g;
type = GREENR; dest[i * 4 + bayer2rgb->b_off] = b;
break; }
case GREENR:
output[op + filter->r_off] = (input[ip + 1] + input[ip - 1] + 1) / 2; static void
output[op + filter->g_off] = input[ip]; gst_bayer2rgb_process_ref (GstBayer2RGB * bayer2rgb, uint8_t * dest,
output[op + filter->b_off] = (input[ip - filter->stride] + int dest_stride, uint8_t * src, int src_stride)
input[ip + filter->stride] + 1) / 2; {
type = RED; int j;
break; int format = bayer2rgb->format;
case GREENB:
output[op + filter->r_off] = (input[ip - filter->stride] + j = 0;
input[ip + filter->stride] + 1) / 2; if ((j & 1) == (format & 2) >> 1) {
output[op + filter->g_off] = input[ip]; reconstruct_blue_green_edge (bayer2rgb, dest + j * dest_stride,
output[op + filter->b_off] = (input[ip + 1] + input[ip - 1] + 1) / 2; src + j * src_stride, src_stride, format & 1, 1);
type = BLUE; } else {
break; reconstruct_green_red_edge (bayer2rgb, dest + j * dest_stride,
case BLUE: src + j * src_stride, src_stride, (format & 1) ^ 1, 1);
output[op + filter->r_off] = (input[ip - filter->stride - 1] + }
input[ip - filter->stride + 1] + for (j = 1; j < bayer2rgb->height - 1; j++) {
input[ip + filter->stride - 1] + if ((j & 1) == (format & 2) >> 1) {
input[ip + filter->stride + 1] + 2) / 4; reconstruct_blue_green (bayer2rgb, dest + j * dest_stride,
output[op + filter->b_off] = input[ip]; src + j * src_stride, src_stride, format & 1);
v1 = input[ip + filter->stride]; } else {
v2 = input[ip - filter->stride]; reconstruct_green_red (bayer2rgb, dest + j * dest_stride,
h1 = input[ip + 1]; src + j * src_stride, src_stride, (format & 1) ^ 1);
h2 = input[ip - 1];
a1 = abs (v1 - v2);
a2 = abs (h1 - h2);
if (a1 < a2)
output[op + filter->g_off] = (v1 + v2 + 1) / 2;
else if (a1 > a2)
output[op + filter->g_off] = (h1 + h2 + 1) / 2;
else
output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
type = GREENB;
break;
} }
} }
if ((j & 1) == (format & 2) >> 1) {
reconstruct_blue_green_edge (bayer2rgb, dest + j * dest_stride,
src + j * src_stride, src_stride, format & 1, -1);
} else {
reconstruct_green_red_edge (bayer2rgb, dest + j * dest_stride,
src + j * src_stride, src_stride, (format & 1) ^ 1, -1);
} }
} }
static GstFlowReturn static GstFlowReturn
gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf, gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
GstBuffer * outbuf) GstBuffer * outbuf)
@ -677,9 +675,8 @@ gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
GST_DEBUG ("transforming buffer"); GST_DEBUG ("transforming buffer");
input = (uint8_t *) GST_BUFFER_DATA (inbuf); input = (uint8_t *) GST_BUFFER_DATA (inbuf);
output = (uint8_t *) GST_BUFFER_DATA (outbuf); output = (uint8_t *) GST_BUFFER_DATA (outbuf);
do_corners (input, output, filter); gst_bayer2rgb_process_ref (filter, output, filter->width * 4,
do_row0_col0 (input, output, filter); input, filter->width);
do_body (input, output, filter);
GST_OBJECT_UNLOCK (filter); GST_OBJECT_UNLOCK (filter);
return GST_FLOW_OK; return GST_FLOW_OK;