changeset 583:d6955d0d7d27 libavcodec

Add conversions to and from RGBA32 and BGRA32.
author philipjsg
date Sat, 27 Jul 2002 03:08:04 +0000
parents 5132a4ee50cd
children 03e395b31197
files avcodec.h imgconvert.c utils.c
diffstat 3 files changed, 258 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/avcodec.h	Thu Jul 25 20:22:36 2002 +0000
+++ b/avcodec.h	Sat Jul 27 03:08:04 2002 +0000
@@ -59,6 +59,8 @@
     PIX_FMT_BGR24,
     PIX_FMT_YUV422P,
     PIX_FMT_YUV444P,
+    PIX_FMT_RGBA32,
+    PIX_FMT_BGRA32,
     PIX_FMT_YUV410P
 };
 
--- a/imgconvert.c	Thu Jul 25 20:22:36 2002 +0000
+++ b/imgconvert.c	Sat Jul 27 03:08:04 2002 +0000
@@ -117,6 +117,69 @@
     }
 }
 
+static void rgba32_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
+                              UINT8 *src, int width, int height)
+{
+    int wrap, wrap4, x, y;
+    int r, g, b, r1, g1, b1;
+    UINT8 *p;
+
+    wrap = width;
+    wrap4 = width * 4;
+    p = src;
+    for(y=0;y<height;y+=2) {
+        for(x=0;x<width;x+=2) {
+            r = p[0];
+            g = p[1];
+            b = p[2];
+            r1 = r;
+            g1 = g;
+            b1 = b;
+            lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            r = p[4];
+            g = p[5];
+            b = p[6];
+            r1 += r;
+            g1 += g;
+            b1 += b;
+            lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            p += wrap4;
+            lum += wrap;
+
+            r = p[0];
+            g = p[1];
+            b = p[2];
+            r1 += r;
+            g1 += g;
+            b1 += b;
+            lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            r = p[4];
+            g = p[5];
+            b = p[6];
+            r1 += r;
+            g1 += g;
+            b1 += b;
+            lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            
+            cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + 
+                      FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
+            cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - 
+                     FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
+
+            cb++;
+            cr++;
+            p += -wrap4 + 2 * 4;
+            lum += -wrap + 2;
+        }
+        p += wrap4;
+        lum += wrap;
+    }
+}
+
 static void bgr24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
                               UINT8 *src, int width, int height)
 {
@@ -180,6 +243,69 @@
     }
 }
 
+static void bgra32_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
+                              UINT8 *src, int width, int height)
+{
+    int wrap, wrap4, x, y;
+    int r, g, b, r1, g1, b1;
+    UINT8 *p;
+
+    wrap = width;
+    wrap4 = width * 4;
+    p = src;
+    for(y=0;y<height;y+=2) {
+        for(x=0;x<width;x+=2) {
+            b = p[0];
+            g = p[1];
+            r = p[2];
+            r1 = r;
+            g1 = g;
+            b1 = b;
+            lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            b = p[4];
+            g = p[5];
+            r = p[6];
+            r1 += r;
+            g1 += g;
+            b1 += b;
+            lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            p += wrap4;
+            lum += wrap;
+
+            b = p[0];
+            g = p[1];
+            r = p[2];
+            r1 += r;
+            g1 += g;
+            b1 += b;
+            lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            b = p[4];
+            g = p[5];
+            r = p[6];
+            r1 += r;
+            g1 += g;
+            b1 += b;
+            lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + 
+                      FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
+            
+            cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + 
+                      FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
+            cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - 
+                     FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
+
+            cb++;
+            cr++;
+            p += -wrap4 + 2 * 4;
+            lum += -wrap + 2;
+        }
+        p += wrap4;
+        lum += wrap;
+    }
+}
+
 /* XXX: use generic filter ? */
 /* 1x2 -> 1x1 */
 static void shrink2(UINT8 *dst, int dst_wrap, 
@@ -301,6 +427,98 @@
 }
 
 /* XXX: no chroma interpolating is done */
+static void yuv420p_to_bgra32(AVPicture *dst, AVPicture *src, 
+                             int width, int height)
+{
+    UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
+    int w, y, cb, cr, r_add, g_add, b_add, width2;
+    UINT8 *cm = cropTbl + MAX_NEG_CROP;
+
+    d = dst->data[0];
+    y1_ptr = src->data[0];
+    cb_ptr = src->data[1];
+    cr_ptr = src->data[2];
+    width2 = width >> 1;
+    for(;height > 0; height -= 2) {
+        d1 = d;
+        d2 = d + dst->linesize[0];
+        y2_ptr = y1_ptr + src->linesize[0];
+        for(w = width2; w > 0; w --) {
+            cb = cb_ptr[0] - 128;
+            cr = cr_ptr[0] - 128;
+            r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
+            g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
+            b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
+            
+            /* output 4 pixels */
+            RGBOUT(d1[2], d1[1], d1[0], y1_ptr[0]);
+            RGBOUT(d1[6], d1[5], d1[4], y1_ptr[1]);
+            RGBOUT(d2[2], d2[1], d2[0], y2_ptr[0]);
+            RGBOUT(d2[6], d2[5], d2[4], y2_ptr[1]);
+
+            d1[3] = d1[7] = d2[3] = d2[7] = 255;
+
+            d1 += 8;
+            d2 += 8;
+            y1_ptr += 2;
+            y2_ptr += 2;
+            cb_ptr++;
+            cr_ptr++;
+        }
+        d += 2 * dst->linesize[0];
+        y1_ptr += 2 * src->linesize[0] - width;
+        cb_ptr += src->linesize[1] - width2;
+        cr_ptr += src->linesize[2] - width2;
+    }
+}
+
+/* XXX: no chroma interpolating is done */
+static void yuv420p_to_rgba32(AVPicture *dst, AVPicture *src, 
+                             int width, int height)
+{
+    UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
+    int w, y, cb, cr, r_add, g_add, b_add, width2;
+    UINT8 *cm = cropTbl + MAX_NEG_CROP;
+
+    d = dst->data[0];
+    y1_ptr = src->data[0];
+    cb_ptr = src->data[1];
+    cr_ptr = src->data[2];
+    width2 = width >> 1;
+    for(;height > 0; height -= 2) {
+        d1 = d;
+        d2 = d + dst->linesize[0];
+        y2_ptr = y1_ptr + src->linesize[0];
+        for(w = width2; w > 0; w --) {
+            cb = cb_ptr[0] - 128;
+            cr = cr_ptr[0] - 128;
+            r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
+            g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
+            b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
+            
+            /* output 4 pixels */
+            RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
+            RGBOUT(d1[4], d1[5], d1[6], y1_ptr[1]);
+            RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]);
+            RGBOUT(d2[4], d2[5], d2[6], y2_ptr[1]);
+
+            d1[3] = d1[7] = d2[3] = d2[7] = 255;
+
+            d1 += 8;
+            d2 += 8;
+            y1_ptr += 2;
+            y2_ptr += 2;
+            cb_ptr++;
+            cr_ptr++;
+        }
+        d += 2 * dst->linesize[0];
+        y1_ptr += 2 * src->linesize[0] - width;
+        cb_ptr += src->linesize[1] - width2;
+        cr_ptr += src->linesize[2] - width2;
+    }
+}
+
+/* XXX: no chroma interpolating is done */
 static void yuv420p_to_rgb24(AVPicture *dst, AVPicture *src, 
                              int width, int height)
 {
@@ -460,10 +678,18 @@
             rgb24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2], 
                              src->data[0], width, height);
             break;
+        case PIX_FMT_RGBA32:
+            rgba32_to_yuv420p(dst->data[0], dst->data[1], dst->data[2], 
+                             src->data[0], width, height);
+            break;
         case PIX_FMT_BGR24:
             bgr24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2], 
                              src->data[0], width, height);
             break;
+        case PIX_FMT_BGRA32:
+            bgra32_to_yuv420p(dst->data[0], dst->data[1], dst->data[2], 
+                             src->data[0], width, height);
+            break;
         default:
             return -1;
         }
@@ -478,6 +704,22 @@
         default:
             return -1;
         }
+    } else if (dst_pix_fmt == PIX_FMT_RGBA32) {
+        switch(pix_fmt) {
+        case PIX_FMT_YUV420P:
+            yuv420p_to_rgba32(dst, src, width, height);
+            break;
+        default:
+            return -1;
+        }
+    } else if (dst_pix_fmt == PIX_FMT_BGRA32) {
+        switch(pix_fmt) {
+        case PIX_FMT_YUV420P:
+            yuv420p_to_bgra32(dst, src, width, height);
+            break;
+        default:
+            return -1;
+        }
     } else {
         return -1;
     }
--- a/utils.c	Thu Jul 25 20:22:36 2002 +0000
+++ b/utils.c	Sat Jul 27 03:08:04 2002 +0000
@@ -199,6 +199,8 @@
     "bgr24",
     "yuv422p",
     "yuv444p",
+    "rgba32",
+    "bgra32",
     "yuv410p"
 };
     
@@ -299,7 +301,7 @@
         }
         break;
     default:
-        abort();
+        av_abort();
     }
     if (bitrate != 0) {
         snprintf(buf + strlen(buf), buf_size - strlen(buf), 
@@ -346,6 +348,13 @@
         picture->data[2] = NULL;
         picture->linesize[0] = width * 3;
         break;
+    case PIX_FMT_RGBA32:
+    case PIX_FMT_BGRA32:
+        picture->data[0] = ptr;
+        picture->data[1] = NULL;
+        picture->data[2] = NULL;
+        picture->linesize[0] = width * 4;
+        break;
     case PIX_FMT_YUV422:
         picture->data[0] = ptr;
         picture->data[1] = NULL;
@@ -379,6 +388,10 @@
     case PIX_FMT_BGR24:
         size = (size * 3);
         break;
+    case PIX_FMT_RGBA32:
+    case PIX_FMT_BGRA32:
+        size = (size * 4);
+        break;
     case PIX_FMT_YUV422:
         size = (size * 2);
         break;