[FFmpeg-cvslog] Add gamma encodign/decoding before/after scaling in libswscale

Pedro Arthur git at videolan.org
Thu Apr 23 20:35:29 CEST 2015


ffmpeg | branch: master | Pedro Arthur <bygrandao at gmail.com> | Fri Apr 17 17:08:42 2015 -0300| [2a7128f4ed00dbc9209d6f24501bb76a0c14b490] | committer: Michael Niedermayer

Add gamma encodign/decoding before/after scaling in libswscale

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=2a7128f4ed00dbc9209d6f24501bb76a0c14b490
---

 libswscale/options.c          |    3 ++
 libswscale/swscale.c          |   51 +++++++++++++++++++++++++++
 libswscale/swscale.h          |    1 +
 libswscale/swscale_internal.h |    9 ++++-
 libswscale/utils.c            |   76 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 139 insertions(+), 1 deletion(-)

diff --git a/libswscale/options.c b/libswscale/options.c
index 4d49c3e..f08267c 100644
--- a/libswscale/options.c
+++ b/libswscale/options.c
@@ -75,6 +75,9 @@ static const AVOption swscale_options[] = {
     { "ed",              "error diffusion",               0,                 AV_OPT_TYPE_CONST,  { .i64  = SWS_DITHER_ED      }, INT_MIN, INT_MAX,        VE, "sws_dither" },
     { "a_dither",        "arithmetic addition dither",    0,                 AV_OPT_TYPE_CONST,  { .i64  = SWS_DITHER_A_DITHER}, INT_MIN, INT_MAX,        VE, "sws_dither" },
     { "x_dither",        "arithmetic xor dither",         0,                 AV_OPT_TYPE_CONST,  { .i64  = SWS_DITHER_X_DITHER}, INT_MIN, INT_MAX,        VE, "sws_dither" },
+    { "gamma",           "gamma correct scaling", OFFSET(gamma_flag),        AV_OPT_TYPE_INT,    { .i64  = 0                  }, 0,       INT_MAX,        VE, "gamma" },
+    { "true",            "enable",                        0,                 AV_OPT_TYPE_CONST,  { .i64  = 1                  }, INT_MIN, INT_MAX,        VE, "gamma" },
+    { "false",           "disable",                       0,                 AV_OPT_TYPE_CONST,  { .i64  = 0                  }, INT_MIN, INT_MAX,        VE, "gamma" },
 
     { NULL }
 };
diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index dff44dd..8b244ef 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -52,6 +52,22 @@ DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
     64, 64, 64, 64, 64, 64, 64, 64
 };
 
+static void gamma_convert(uint8_t * src[], int width, uint16_t *gamma)
+{
+    int i;
+    uint16_t *src1 = (uint16_t*)src[0];
+
+    for (i = 0; i < width; ++i) {
+        uint16_t r = AV_RL16(src1 + i*4 + 0);
+        uint16_t g = AV_RL16(src1 + i*4 + 1);
+        uint16_t b = AV_RL16(src1 + i*4 + 2);
+
+        AV_WL16(src1 + i*4 + 0, gamma[r]);
+        AV_WL16(src1 + i*4 + 1, gamma[g]);
+        AV_WL16(src1 + i*4 + 2, gamma[b]);
+    }
+}
+
 static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
                                        int height, int y, uint8_t val)
 {
@@ -353,6 +369,8 @@ static int swscale(SwsContext *c, const uint8_t *src[],
     int chrBufIndex  = c->chrBufIndex;
     int lastInLumBuf = c->lastInLumBuf;
     int lastInChrBuf = c->lastInChrBuf;
+    int perform_gamma = c->flags & SWS_GAMMA_CORRECT;
+
 
     if (!usePal(c->srcFormat)) {
         pal = c->input_rgb2yuv_table;
@@ -480,6 +498,10 @@ static int swscale(SwsContext *c, const uint8_t *src[],
             av_assert0(lumBufIndex < 2 * vLumBufSize);
             av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
             av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
+
+            if (perform_gamma)
+                gamma_convert((uint8_t **)src1, srcW, c->inv_gamma);
+
             hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
                     hLumFilter, hLumFilterPos, hLumFilterSize,
                     formatConvBuffer, pal, 0);
@@ -641,6 +663,8 @@ static int swscale(SwsContext *c, const uint8_t *src[],
                          chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
                          alpSrcPtr, dest, dstW, dstY);
             }
+            if (perform_gamma)
+                gamma_convert(dest, dstW, c->gamma);
         }
     }
     if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
@@ -900,6 +924,33 @@ int attribute_align_arg sws_scale(struct SwsContext *c,
         av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
         return 0;
     }
+
+    if (c->gamma_flag && c->cascaded_context[0]) {
+
+
+        ret = sws_scale(c->cascaded_context[0],
+                    srcSlice, srcStride, srcSliceY, srcSliceH,
+                    c->cascaded_tmp, c->cascaded_tmpStride);
+
+        if (ret < 0)
+            return ret;
+
+        if (c->cascaded_context[2])
+            ret = sws_scale(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp, c->cascaded_tmpStride, srcSliceY, srcSliceH, c->cascaded1_tmp, c->cascaded1_tmpStride);
+        else
+            ret = sws_scale(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp, c->cascaded_tmpStride, srcSliceY, srcSliceH, dst, dstStride);
+
+        if (ret < 0)
+            return ret;
+
+        if (c->cascaded_context[2]) {
+            ret = sws_scale(c->cascaded_context[2],
+                        (const uint8_t * const *)c->cascaded1_tmp, c->cascaded1_tmpStride, c->cascaded_context[1]->dstY - ret, c->cascaded_context[1]->dstY,
+                        dst, dstStride);
+        }
+        return ret;
+    }
+
     if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->srcH) {
         ret = sws_scale(c->cascaded_context[0],
                         srcSlice, srcStride, srcSliceY, srcSliceH,
diff --git a/libswscale/swscale.h b/libswscale/swscale.h
index 903e120..6e88323 100644
--- a/libswscale/swscale.h
+++ b/libswscale/swscale.h
@@ -64,6 +64,7 @@ const char *swscale_license(void);
 #define SWS_SINC          0x100
 #define SWS_LANCZOS       0x200
 #define SWS_SPLINE        0x400
+#define SWS_GAMMA_CORRECT 0x800
 
 #define SWS_SRC_V_CHR_DROP_MASK     0x30000
 #define SWS_SRC_V_CHR_DROP_SHIFT    16
diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h
index 55f683f..fad33b2 100644
--- a/libswscale/swscale_internal.h
+++ b/libswscale/swscale_internal.h
@@ -307,9 +307,16 @@ typedef struct SwsContext {
      * sequential steps, this is for example used to limit the maximum
      * downscaling factor that needs to be supported in one scaler.
      */
-    struct SwsContext *cascaded_context[2];
+    struct SwsContext *cascaded_context[3];
     int cascaded_tmpStride[4];
     uint8_t *cascaded_tmp[4];
+    int cascaded1_tmpStride[4];
+    uint8_t *cascaded1_tmp[4];
+
+    double gamma_value;
+    int gamma_flag;
+    uint16_t *gamma;
+    uint16_t *inv_gamma;
 
     uint32_t pal_yuv[256];
     uint32_t pal_rgb[256];
diff --git a/libswscale/utils.c b/libswscale/utils.c
index 7810719..d1cdf00 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -960,6 +960,20 @@ SwsContext *sws_alloc_context(void)
     return c;
 }
 
+static uint16_t * alloc_gamma_tbl(double e)
+{
+    int i = 0;
+    uint16_t * tbl;
+    tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
+    if (!tbl)
+        return NULL;
+
+    for (i = 0; i < 65536; ++i) {
+        tbl[i] = pow(i / 65535.0, e) * 65535.0;
+    }
+    return tbl;
+}
+
 av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
                              SwsFilter *dstFilter)
 {
@@ -978,6 +992,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
     const AVPixFmtDescriptor *desc_src;
     const AVPixFmtDescriptor *desc_dst;
     int ret = 0;
+    enum AVPixelFormat tmpFmt;
 
     cpu_flags = av_get_cpu_flags();
     flags     = c->flags;
@@ -1235,6 +1250,61 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
         }
     }
 
+    // hardcoded for now
+    c->gamma_value = 2.2;
+    tmpFmt = AV_PIX_FMT_RGBA64LE;
+
+
+    if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
+        c->cascaded_context[0] = NULL;
+
+        ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
+                            srcW, srcH, tmpFmt, 64);
+        if (ret < 0)
+            return ret;
+
+        c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
+                                                srcW, srcH, tmpFmt,
+                                                flags, NULL, NULL, c->param);
+        if (!c->cascaded_context[0]) {
+            return -1;
+        }
+
+        c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
+                                                dstW, dstH, tmpFmt,
+                                                flags | SWS_GAMMA_CORRECT, srcFilter, dstFilter, c->param);
+
+        if (!c->cascaded_context[1])
+            return -1;
+
+        c->cascaded_context[2] = NULL;
+        if (dstFormat != tmpFmt) {
+            ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
+                                dstW, dstH, tmpFmt, 64);
+            if (ret < 0)
+                return ret;
+
+            c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
+                                                dstW, dstH, dstFormat,
+                                                flags, NULL, NULL, c->param);
+            if (!c->cascaded_context[2])
+                return -1;
+        }
+        return 0;
+    }
+
+    c->gamma = NULL;
+    c->inv_gamma = NULL;
+    if (c->flags & SWS_GAMMA_CORRECT) {
+        c->gamma = alloc_gamma_tbl(c->gamma_value);
+        if (!c->gamma)
+            return AVERROR(ENOMEM);
+        c->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
+        if (!c->inv_gamma) {
+            return AVERROR(ENOMEM);
+        }
+    }
+
     if (isBayer(srcFormat)) {
         if (!unscaled ||
             (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
@@ -1977,8 +2047,14 @@ void sws_freeContext(SwsContext *c)
 
     sws_freeContext(c->cascaded_context[0]);
     sws_freeContext(c->cascaded_context[1]);
+    sws_freeContext(c->cascaded_context[2]);
     memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
     av_freep(&c->cascaded_tmp[0]);
+    av_freep(&c->cascaded1_tmp[0]);
+
+    av_freep(&c->gamma);
+    av_freep(&c->inv_gamma);
+
 
     av_free(c);
 }



More information about the ffmpeg-cvslog mailing list