[FFmpeg-devel] [PATCH] avcodec: change type of ff_square_tab from uint32_t to uint16_t

Zhaoxiu Zeng zhaoxiu.zeng at gmail.com
Tue Feb 17 09:31:07 CET 2015


>From bf2964c07fde48c633ca4d8276282010e7c7f084 Mon Sep 17 00:00:00 2001
From: "zhaoxiu.zeng" <zhaoxiu.zeng at gmail.com>
Date: Tue, 17 Feb 2015 16:03:47 +0800
Subject: [PATCH 1/1] avcodec: change type of ff_square_tab from uint32_t to
 uint16_t

uint16_t is big enough except the first element, but the first element
is never used.
This also macroize nsse function, and use ff_square_tab when possible.

Signed-off-by: zhaoxiu.zeng <zhaoxiu.zeng at gmail.com>
---
 libavcodec/me_cmp.c        | 94 ++++++++++++++++++----------------------------
 libavcodec/me_cmp.h        |  3 +-
 libavcodec/mpegvideo_enc.c |  2 +-
 libavcodec/snowenc.c       |  2 +-
 4 files changed, 41 insertions(+), 60 deletions(-)

diff --git a/libavcodec/me_cmp.c b/libavcodec/me_cmp.c
index d4213d2..a7a90b7 100644
--- a/libavcodec/me_cmp.c
+++ b/libavcodec/me_cmp.c
@@ -29,13 +29,13 @@
 #include "mpegvideo.h"
 #include "config.h"

-uint32_t ff_square_tab[512] = { 0, };
+uint16_t ff_square_tab[512] = { 0, };

 static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
                   ptrdiff_t stride, int h)
 {
     int s = 0, i;
-    uint32_t *sq = ff_square_tab + 256;
+    uint16_t *sq = ff_square_tab + 256;

     for (i = 0; i < h; i++) {
         s    += sq[pix1[0] - pix2[0]];
@@ -52,7 +52,7 @@ static int sse8_c(MpegEncContext *v, uint8_t *pix1,
uint8_t *pix2,
                   ptrdiff_t stride, int h)
 {
     int s = 0, i;
-    uint32_t *sq = ff_square_tab + 256;
+    uint16_t *sq = ff_square_tab + 256;

     for (i = 0; i < h; i++) {
         s    += sq[pix1[0] - pix2[0]];
@@ -73,7 +73,7 @@ static int sse16_c(MpegEncContext *v, uint8_t *pix1,
uint8_t *pix2,
                    ptrdiff_t stride, int h)
 {
     int s = 0, i;
-    uint32_t *sq = ff_square_tab + 256;
+    uint16_t *sq = ff_square_tab + 256;

     for (i = 0; i < h; i++) {
         s += sq[pix1[0]  - pix2[0]];
@@ -311,55 +311,34 @@ static int pix_abs8_xy2_c(MpegEncContext *v,
uint8_t *pix1, uint8_t *pix2,
     return s;
 }

-static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
-                    ptrdiff_t stride, int h)
-{
-    int score1 = 0, score2 = 0, x, y;
-
-    for (y = 0; y < h; y++) {
-        for (x = 0; x < 16; x++)
-            score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
-        if (y + 1 < h) {
-            for (x = 0; x < 15; x++)
-                score2 += FFABS(s1[x]     - s1[x + stride] -
-                                s1[x + 1] + s1[x + stride + 1]) -
-                          FFABS(s2[x]     - s2[x + stride] -
-                                s2[x + 1] + s2[x + stride + 1]);
-        }
-        s1 += stride;
-        s2 += stride;
-    }
-
-    if (c)
-        return score1 + FFABS(score2) * c->avctx->nsse_weight;
-    else
-        return score1 + FFABS(score2) * 8;
-}
-
-static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
-                   ptrdiff_t stride, int h)
-{
-    int score1 = 0, score2 = 0, x, y;
-
-    for (y = 0; y < h; y++) {
-        for (x = 0; x < 8; x++)
-            score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
-        if (y + 1 < h) {
-            for (x = 0; x < 7; x++)
-                score2 += FFABS(s1[x]     - s1[x + stride] -
-                                s1[x + 1] + s1[x + stride + 1]) -
-                          FFABS(s2[x]     - s2[x + stride] -
-                                s2[x + 1] + s2[x + stride + 1]);
-        }
-        s1 += stride;
-        s2 += stride;
-    }
-
-    if (c)
-        return score1 + FFABS(score2) * c->avctx->nsse_weight;
-    else
-        return score1 + FFABS(score2) * 8;
-}
+#define NSSE(size)                                                         \
+static int nsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, \
+                              ptrdiff_t stride, int h)                     \
+{                                                                          \
+    int score1 = 0, score2 = 0, x, y;                                      \
+    uint16_t *sq = ff_square_tab + 256;                                    \
+                                                                           \
+    for (y = 0; y < h; y++) {                                              \
+        for (x = 0; x < size; x++)                                         \
+            score1 += sq[s1[x] - s2[x]];                                   \
+        if (y + 1 < h) {                                                   \
+            for (x = 0; x < size - 1; x++)                                 \
+                score2 += FFABS(s1[x]     - s1[x + stride] -               \
+                                s1[x + 1] + s1[x + stride + 1]) -          \
+                          FFABS(s2[x]     - s2[x + stride] -               \
+                                s2[x + 1] + s2[x + stride + 1]);           \
+        }                                                                  \
+        s1 += stride;                                                      \
+        s2 += stride;                                                      \
+    }                                                                      \
+                                                                           \
+    if (c)                                                                 \
+        return score1 + FFABS(score2) * c->avctx->nsse_weight;             \
+    else                                                                   \
+        return score1 + FFABS(score2) * 8;                                 \
+}
+NSSE(8)
+NSSE(16)

 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
                     ptrdiff_t stride, int h)
@@ -842,13 +821,14 @@ static int vsse_intra ## size ##
_c(MpegEncContext *c,                  \
                                     ptrdiff_t stride, int h)            \
 {                                                                       \
     int score = 0, x, y;                                                \
+    uint16_t *sq = ff_square_tab + 256;                                 \
                                                                         \
     for (y = 1; y < h; y++) {                                           \
         for (x = 0; x < size; x += 4) {                                 \
-            score += SQ(s[x]     - s[x + stride]) +                     \
-                     SQ(s[x + 1] - s[x + stride + 1]) +                 \
-                     SQ(s[x + 2] - s[x + stride + 2]) +                 \
-                     SQ(s[x + 3] - s[x + stride + 3]);                  \
+            score += sq[s[x]     - s[x + stride]] +                     \
+                     sq[s[x + 1] - s[x + stride + 1]] +                 \
+                     sq[s[x + 2] - s[x + stride + 2]] +                 \
+                     sq[s[x + 3] - s[x + stride + 3]];                  \
         }                                                               \
         s += stride;                                                    \
     }                                                                   \
diff --git a/libavcodec/me_cmp.h b/libavcodec/me_cmp.h
index 98ee53c..40061ea 100644
--- a/libavcodec/me_cmp.h
+++ b/libavcodec/me_cmp.h
@@ -23,7 +23,8 @@

 #include "avcodec.h"

-extern uint32_t ff_square_tab[512];
+/* the first element is worthless */
+extern uint16_t ff_square_tab[512];


 /* minimum alignment rules ;)
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 16f88aa..fd062c5 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -2546,7 +2546,7 @@ static inline void encode_mb_hq(MpegEncContext
*s, MpegEncContext *backup, MpegE
 }

 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int
w, int h, int stride){
-    uint32_t *sq = ff_square_tab + 256;
+    uint16_t *sq = ff_square_tab + 256;
     int acc=0;
     int x,y;

diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index cd8fef3..f7b2e1b 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -163,7 +163,7 @@ static int pix_sum(uint8_t * pix, int line_size,
int w, int h)
 static int pix_norm1(uint8_t * pix, int line_size, int w)
 {
     int s, i, j;
-    uint32_t *sq = ff_square_tab + 256;
+    uint16_t *sq = ff_square_tab + 256;

     s = 0;
     for (i = 0; i < w; i++) {
-- 
2.1.4


More information about the ffmpeg-devel mailing list