From 2e52b44e50aabd92f584a23d240e9451e8d2f6da Mon Sep 17 00:00:00 2001
From: Dan Maas <dmaas@maasdigital.com>
Date: Mon, 27 Feb 2006 04:50:06 +0000
Subject: [PATCH] AC coefficient weighing (per SMPTE 314M) for the DV encoder
 and decoder. The performance impact is less than 1%.

Patch by Dan Maas  (dmaas at maasdigital dot com)

Originally committed as revision 5070 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/dv.c               | 22 +++++++++++++----
 libavcodec/dvdata.h           | 45 +++++++++++++++++++++++++++++++++++
 tests/ffmpeg.regression.ref   |  6 ++---
 tests/libav.regression.ref    |  4 ++--
 tests/rotozoom.regression.ref |  6 ++---
 5 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/libavcodec/dv.c b/libavcodec/dv.c
index f132217ee5..824679f6dd 100644
--- a/libavcodec/dv.c
+++ b/libavcodec/dv.c
@@ -253,6 +253,7 @@ static int dvvideo_init(AVCodecContext *avctx)
 typedef struct BlockInfo {
     const uint8_t *shift_table;
     const uint8_t *scan_table;
+    const int *iweight_table;
     uint8_t pos; /* position in block */
     uint8_t dct_mode;
     uint8_t partial_bit_count;
@@ -295,6 +296,7 @@ static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
     int last_index = get_bits_size(gb);
     const uint8_t *scan_table = mb->scan_table;
     const uint8_t *shift_table = mb->shift_table;
+    const int *iweight_table = mb->iweight_table;
     int pos = mb->pos;
     int partial_bit_count = mb->partial_bit_count;
     int level, pos1, run, vlc_len, index;
@@ -343,7 +345,12 @@ static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
             break;
 
         pos1 = scan_table[pos];
-        block[pos1] = level << shift_table[pos1];
+        level <<= shift_table[pos1];
+
+        /* unweigh, round, and shift down */
+        level = (level*iweight_table[pos] + (1 << (dv_iweight_bits-1))) >> dv_iweight_bits;
+
+        block[pos1] = level;
 
         UPDATE_CACHE(re, gb);
     }
@@ -409,6 +416,7 @@ static inline void dv_decode_video_segment(DVVideoContext *s,
             dct_mode = get_bits1(&gb);
             mb->dct_mode = dct_mode;
             mb->scan_table = s->dv_zigzag[dct_mode];
+            mb->iweight_table = dct_mode ? dv_iweight_248 : dv_iweight_88;
             class1 = get_bits(&gb, 2);
             mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
                 [quant + dv_quant_offset[class1]];
@@ -647,7 +655,7 @@ static always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, PutBitContext
 }
 
 static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
-                                              const uint8_t* zigzag_scan, int bias)
+                                              const uint8_t* zigzag_scan, const int *weight, int bias)
 {
     int i, area;
     static const int classes[] = {12, 24, 36, 0xffff};
@@ -664,7 +672,11 @@ static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
 
           if (level+15 > 30U) {
               bi->sign[i] = (level>>31)&1;
-              bi->mb[i] = level= ABS(level)>>4;
+              /* weigh it and and shift down into range, adding for rounding */
+              /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
+                 AND the 2x doubling of the weights */
+              level = (ABS(level) * weight[i] + (1<<(dv_weight_bits+3))) >> (dv_weight_bits+4);
+              bi->mb[i] = level;
               if(level>max) max= level;
               bi->bit_size[area] += dv_rl2vlc_size(i - prev  - 1, level);
               bi->next[prev]= i;
@@ -875,7 +887,9 @@ static inline void dv_encode_video_segment(DVVideoContext *s,
             s->fdct[enc_blk->dct_mode](block);
 
             dv_set_class_number(block, enc_blk,
-                                enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct, j/4);
+                                enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
+                                enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
+                                j/4);
 
             init_put_bits(pb, ptr, block_sizes[j]/8);
             put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
diff --git a/libavcodec/dvdata.h b/libavcodec/dvdata.h
index f817ead2a6..40d783a0b0 100644
--- a/libavcodec/dvdata.h
+++ b/libavcodec/dvdata.h
@@ -1256,6 +1256,51 @@ static const uint16_t dv_place_411[1350] = {
  0x0834, 0x2320, 0x2f44, 0x3810, 0x1658,
 };
 
+/* DV25/50 DCT coefficient weights and inverse weights */
+/* created by dvtables.py */
+static const int dv_weight_bits = 18;
+static const int dv_weight_88[64] = {
+ 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
+ 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
+ 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
+ 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
+ 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
+ 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
+ 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
+ 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
+};
+static const int dv_weight_248[64] = {
+ 131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
+ 224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
+ 211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
+ 242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
+ 200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
+ 229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
+ 175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
+ 195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
+};
+static const int dv_iweight_bits = 14;
+static const int dv_iweight_88[64] = {
+ 32768, 16710, 16710, 17735, 17015, 17735, 18197, 18079,
+ 18079, 18197, 18725, 18559, 19196, 18559, 18725, 19284,
+ 19108, 19692, 19692, 19108, 19284, 21400, 19645, 20262,
+ 20214, 20262, 19645, 21400, 22733, 21845, 20867, 20815,
+ 20815, 20867, 21845, 22733, 23173, 23173, 21400, 21400,
+ 21400, 23173, 23173, 24600, 23764, 22017, 22017, 23764,
+ 24600, 25267, 24457, 22672, 24457, 25267, 25971, 25191,
+ 25191, 25971, 26715, 27962, 26715, 29642, 29642, 31536,
+};
+static const int dv_iweight_248[64] = {
+ 32768, 17735, 16710, 18079, 18725, 21400, 17735, 19196,
+ 19108, 21845, 16384, 17735, 18725, 21400, 16710, 18079,
+ 20262, 23173, 18197, 19692, 18725, 20262, 20815, 23764,
+ 17735, 19196, 19108, 21845, 20262, 23173, 18197, 19692,
+ 21400, 24457, 19284, 20867, 21400, 23173, 22017, 25191,
+ 18725, 20262, 20815, 23764, 21400, 24457, 19284, 20867,
+ 24457, 27962, 22733, 24600, 25971, 29642, 21400, 23173,
+ 22017, 25191, 24457, 27962, 22733, 24600, 25971, 29642,
+};
+
 static const uint16_t dv_audio_shuffle525[10][9] = {
   {  0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
   {  6, 36, 66, 26, 56, 86, 16, 46, 76 },
diff --git a/tests/ffmpeg.regression.ref b/tests/ffmpeg.regression.ref
index c869b3a6cd..5684a57330 100644
--- a/tests/ffmpeg.regression.ref
+++ b/tests/ffmpeg.regression.ref
@@ -133,10 +133,10 @@ stddev: 23.20 PSNR:20.81 bytes:7602176
 3533710 ./data/a-snow53.avi
 799d3db687f6cdd7a837ec156efc171f *./data/out.yuv
 stddev:  0.00 PSNR:99.99 bytes:7602176
-a071e3aa523e0f91fce08bdba0d47199 *./data/a-dv.dv
+1352049a75c5c94f21a360888b12d75d *./data/a-dv.dv
 7200000 ./data/a-dv.dv
-a99de8648a78970065b834219d9fa27a *./data/out.yuv
-stddev:  9.10 PSNR:28.94 bytes:7602176
+2c7f745a74c1ac25566b6c49ed619649 *./data/out.yuv
+stddev:  9.00 PSNR:29.03 bytes:7602176
 bd0db310a36ad94bcd4448abe0a88368 *./data/a-svq1.mov
 1379827 ./data/a-svq1.mov
 bbff871d1475e1eee4231a08e075de2c *./data/out.yuv
diff --git a/tests/libav.regression.ref b/tests/libav.regression.ref
index 673fd51a5a..eafc4f3276 100644
--- a/tests/libav.regression.ref
+++ b/tests/libav.regression.ref
@@ -28,9 +28,9 @@ e4ed8d635d867e2f5980fd9c73c9cf3d *./data/b-libav.mov
 8bf16d40a2ec19fa36b124a928e47e23 *./data/b-libav.nut
  332358 ./data/b-libav.nut
 ./data/b-libav.nut CRC=0xccab3a27
-5a040cc1353f17f6a7305e972c0667d0 *./data/b-libav.dv
+afe55c5769eb6d1873d4106b3ef2b691 *./data/b-libav.dv
 3600000 ./data/b-libav.dv
-./data/b-libav.dv CRC=0x5e1f4cdc
+./data/b-libav.dv CRC=0x9292dedd
 9a9da315747599f7718cc9a9a09c21ff *./data/b-libav.pbm
  317075 ./data/b-libav.pbm
 ./data/b-libav.pbm CRC=0xb92906cb
diff --git a/tests/rotozoom.regression.ref b/tests/rotozoom.regression.ref
index 1a482cab61..5f9639ea6e 100644
--- a/tests/rotozoom.regression.ref
+++ b/tests/rotozoom.regression.ref
@@ -133,10 +133,10 @@ stddev: 10.94 PSNR:27.34 bytes:7602176
 2725570 ./data/a-snow53.avi
 dde5895817ad9d219f79a52d0bdfb001 *./data/out.yuv
 stddev:  0.00 PSNR:99.99 bytes:7602176
-41165e82124bf9984f784bc007f6a0ac *./data/a-dv.dv
+3d7def2ed47e896790945c3c634fa4e8 *./data/a-dv.dv
 7200000 ./data/a-dv.dv
-b252172f3768271ab0e87a1a2bfc553d *./data/out.yuv
-stddev:  3.16 PSNR:38.13 bytes:7602176
+49020c3651d58137db4dc56ca13ff26d *./data/out.yuv
+stddev:  3.06 PSNR:38.40 bytes:7602176
 5b02b6ae7ffa257a66ae9857a992fdfe *./data/a-svq1.mov
 769527 ./data/a-svq1.mov
 44777d1ddbccd0ef7f8d08394465670c *./data/out.yuv