From bab97b2f6c16e18869c62bcd50c36d27fe8e3b28 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Sat, 8 May 2004 11:54:55 +0000
Subject: [PATCH] width height %64 != 0 fix print average RD score at the end
 precalculated codebook vector mean tables

Originally committed as revision 3119 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/svq1.c    | 14 +++++++---
 libavcodec/svq1_cb.h | 64 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/libavcodec/svq1.c b/libavcodec/svq1.c
index 6d7fc104db8..97b9495aba8 100644
--- a/libavcodec/svq1.c
+++ b/libavcodec/svq1.c
@@ -85,6 +85,7 @@ typedef struct SVQ1Context {
 
     unsigned char *c_plane;
 
+    int64_t rd_total;
 } SVQ1Context;
 
 /* motion vector (prediction) */
@@ -1340,7 +1341,7 @@ static int encode_block(SVQ1Context *s, uint8_t *src, int stride, int level, int
             int8_t *vector;
     
             for(i=0; i<16; i++){
-                int sum=0;
+                int sum= svq1_intra_codebook_sum[level][stage*16 + i];
                 int sqr=0;
                 int diff, mean, score;
     
@@ -1348,7 +1349,6 @@ static int encode_block(SVQ1Context *s, uint8_t *src, int stride, int level, int
     
                 for(j=0; j<size; j++){
                     int v= vector[j];
-                    sum += v;
                     sqr += (v - block[stage][j])*(v - block[stage][j]);
                 }
                 diff= block_sum[stage] - sum;
@@ -1463,6 +1463,8 @@ av_log(s->avctx, AV_LOG_INFO, "********* frame #%d\n", frame++);
 
         for (x = 0; x < block_width; x++) {
             uint8_t reorder_buffer[6][7*32];
+            uint8_t *src= plane + y * 16 * stride + x * 16;
+            uint8_t buf[stride*16];
 
 #ifdef DEBUG_SVQ1
 av_log(s->avctx, AV_LOG_INFO, "* level 5 vector @ %d, %d:\n", x * 16, y * 16);
@@ -1474,7 +1476,11 @@ av_log(s->avctx, AV_LOG_INFO, "* level 5 vector @ %d, %d:\n", x * 16, y * 16);
             for(i=0; i<6; i++){
                 init_put_bits(&s->reorder_pb[i], reorder_buffer[i], 7*32);
             }
-            encode_block(s, &plane[left_edge], stride, 5, 256, (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT));
+            if(x*16 + 16 > width || y*16 + 16 > height){
+                ff_emulated_edge_mc(buf, src, stride, 16, 16, 16*x, 16*y, width, height);
+                src= buf;
+            }
+            s->rd_total += encode_block(s, src, stride, 5, 256, (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT));
             for(i=5; i>=0; i--){
                 int count= put_bits_count(&s->reorder_pb[i]);
                 
@@ -1676,6 +1682,8 @@ static int svq1_encode_end(AVCodecContext *avctx)
 {
     SVQ1Context * const s = avctx->priv_data;
 
+    av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", s->rd_total/(double)(avctx->width*avctx->height*avctx->frame_number));
+    
     av_free(s->c_plane);
 
     return 0;
diff --git a/libavcodec/svq1_cb.h b/libavcodec/svq1_cb.h
index 77ee56dbbe3..0f766aa882c 100644
--- a/libavcodec/svq1_cb.h
+++ b/libavcodec/svq1_cb.h
@@ -769,6 +769,38 @@ static const int8_t* const svq1_inter_codebooks[4] = {
     svq1_inter_codebook_8x4, svq1_inter_codebook_8x8
 };
 
+static const int8_t const svq1_inter_codebook_sum[4][16*6] = {
+ {
+ -1,  1, -2,  0,  1, -1, -1, -1, -2, -1,  1, -1, -1,  0, -1, -1,
+  0, -1, -1, -1, -1,  0, -1,  0,  0,  0, -3,  1, -1,  0,  1, -1,
+  1, -1,  2,  2,  1,  1,  2,  0,  0,  0, -1,  1,  1,  0,  0,  0,
+  1, -1,  0,  1, -1,  1,  1,  0,  1,  0, -1,  1,  1,  0,  0,  0,
+ -2,  0,  0, -2,  0,  0, -2,  0, -2, -1, -2, -1,  0,  0, -1,  0,
+  1,  0,  1, -1,  2,  2,  1,  2,  2,  1,  0,  1,  1,  0,  1,  1,
+ },{
+ -2,  1, -1, -1,  1,  0,  1, -1, -1, -1,  1, -1,  0, -1,  0, -1,
+  0,  0,  0, -2,  0,  1,  0, -1, -1,  0,  2, -3,  1, -2,  3, -1,
+  2,  0,  2,  1,  1, -1,  1,  1,  0,  0,  1,  1,  2, -2,  1,  0,
+ -2, -1,  2, -2, -2,  0, -3,  0, -1,  0, -1,  0, -1,  0, -2, -3,
+  1, -2, -2, -1,  1, -1, -1,  1, -1,  1,  1,  0, -2,  0,  1,  1,
+  1,  1,  2,  1,  0,  0, -1,  0,  0,  1,  0,  1, -1,  1,  0,  2,
+ },{
+  0,  0,  0, -3,  1,  1,  1, -3,  0, -1,  0, -3,  1, -3,  0, -2,
+  1,  2, -1, -3,  0, -3,  1, -1,  0, -1,  0,  0,  1,  2,  1,  1,
+ -1,  2, -3,  3,  1,  0, -5,  1,  0, -1, -3,  1,  0,  2,  0, -3,
+  4,  2,  0, -2,  1, -2,  3, -2,  1,  1,  0, -1,  2,  5,  3,  1,
+ -1,  0,  2, -3, -2,  0,  0, -2,  2, -3, -1, -1,  2,  1,  0, -2,
+  3, -1,  1, -1,  2,  4,  0,  1,  0,  1,  0, -1, -3, -2, -1,  0,
+ },{
+  0,  2, -1, -1,  2, -4, -2,  3,  0, -1, -5,  1,  0,  1,  0,  6,
+ -2,  2,  0,  1,  1, -1, -1, -2,  1, -2, -1,  0,  2, -2, -2, -1,
+ -4,  2, -1, -3, -1, -2,  2, -1,  2, -1,  2,  0,  3, -3, -3,  0,
+ -3,  0,  0, -2,  4, -4,  0, -1,  4,  0, -2, -2,  3, -2,  0,  4,
+  5,  0,  1,  0, -3,  3,  3,  2,  0,  0,  1,  2, -5, -2, -3,  0,
+ -3,  2, -2,  2, -2,  4,  7, -3,  4,  2,  3,  2, -1,  0, -3,  1,
+ }
+};
+
 /* 6x16-entry codebook for intra-coded 4x2 vectors */
 static const int8_t svq1_intra_codebook_4x2[768] = {
    12, 13, 13, 11, -7,-10,-15,-17,-16,-15,-12,-10, 11, 15, 15, 12,
@@ -1510,3 +1542,35 @@ static const int8_t* const svq1_intra_codebooks[4] = {
     svq1_intra_codebook_4x2, svq1_intra_codebook_4x4,
     svq1_intra_codebook_8x4, svq1_intra_codebook_8x8
 };
+
+static const int8_t const svq1_intra_codebook_sum[4][16*6] = {
+ {
+  0,  0,  0, -1, -1, -1, -1, -2,  0, -1, -1,  0, -1,  0,  1,  0,
+  1,  0, -1,  1,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1,  0,  0,
+ -1,  0,  0,  1, -1,  1,  0, -1, -1,  0,  1,  1,  0,  0, -1,  1,
+  0,  1,  0,  0,  1, -1,  0,  0,  0, -1,  1,  0,  1,  0, -2,  1,
+  0, -1,  1,  0,  0,  0,  1,  0, -1,  0,  0,  0, -1,  0,  0,  0,
+  0,  1,  1,  0,  0, -1,  0,  1,  0,  0,  0,  0, -1,  1,  1, -1,
+ },{
+ -1, -2,  0, -1,  1,  0, -1,  0, -1, -4, -1, -2, -1, -2,  1, -2,
+  0,  0,  4, -2, -1,  1,  1,  0,  2,  1,  1,  0,  2,  0,  0,  0,
+  1,  1,  0, -1, -1, -1,  1,  0, -1, -3, -3,  1, -1,  1, -2, -1,
+  1, -1,  0,  1,  2,  1, -1, -1,  1,  1,  1,  2,  1,  0,  1, -2,
+ -2,  0, -1, -2, -2,  0, -1, -1, -1,  0,  1,  0, -1, -1,  0, -1,
+  0,  2,  1,  2,  2,  1, -1,  1,  0,  2,  0, -1,  1,  0,  0,  0,
+ },{
+ -2,  0, -1, -1,  1,  1, -2,  0, -2,  0,  1, -2, -2,  1, -1, -1,
+  3, -2,  0, -3, -4, -3,  2,  1,  0,  3, -2,  2,  3,  2,  2, -1,
+ -3,  1,  0,  1,  0,  0,  0,  1, -2,  1, -2, -2, -1, -2, -2,  2,
+  0, -4,  0,  2, -1,  0,  2,  2,  2,  1,  0, -1, -1,  1, -3,  2,
+  2,  1,  0,  3,  1, -1,  1,  3,  1,  0,  1,  1,  2, -1,  1, -1,
+ -2, -1,  0, -1,  1, -1,  1, -2, -2, -1, -1, -3,  1, -4, -3,  1,
+ },{
+ -2,  0, -2,  3, -1, -1,  0,  2,  2, -1, -3,  2,  1,  0, -2, -1,
+ -3, -2, -2,  1,  2, -3,  0,  1, -5, -2, -3,  0, -2, -1,  2,  0,
+ -1, -1,  0, -2,  1,  3, -7, -2, -2, -1,  2, -1,  0,  3,  1,  3,
+  1,  0,  0,  1,  2,  3,  1,  2,  0, -2, -2,  1,  1,  2,  2,  3,
+  4,  1, -1,  2, -2,  4,  0,  0,  0,  4,  2,  0, -2, -2,  2, -4,
+ -1,  5, -2, -2, -3,  2, -3, -1,  3, -3,  0,  4,  3,  0,  1, -2,
+ }
+};
-- 
GitLab