[Spice-commits] 5 commits - common/quic.c common/quic_rgb_tmpl.c common/quic_tmpl.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu May 24 11:20:09 UTC 2018


 common/quic.c          |  116 ++++----------
 common/quic_rgb_tmpl.c |  190 ++++++++++++------------
 common/quic_tmpl.c     |  384 ++++++++++++++++++++++++++-----------------------
 3 files changed, 343 insertions(+), 347 deletions(-)

New commits:
commit 0e550416f1af7fc169db11789e8380715cd0ca6e
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Aug 3 16:24:36 2017 +0200

    quic: Remove unused argument in uncompress_row{0, }
    
    'correlation_row' is always set to channel->colleration_row, and we
    already pass 'channel' as an argument.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/common/quic_tmpl.c b/common/quic_tmpl.c
index 122f84a..99a31a1 100644
--- a/common/quic_tmpl.c
+++ b/common/quic_tmpl.c
@@ -322,7 +322,6 @@ static void FNAME(compress_row)(Encoder *encoder, Channel *channel,
     decode_eatbits(encoder, codewordlen);
 
 static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i,
-                                       BYTE * const correlate_row,
                                        PIXEL * const cur_row,
                                        const int end,
                                        const unsigned int waitmask,
@@ -330,6 +329,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
                                        const unsigned int bpc_mask)
 {
     CommonState *state = &channel->state;
+    BYTE * const correlate_row = channel->correlate_row;
     int stopidx;
 
     spice_assert(end - i > 0);
@@ -381,12 +381,11 @@ static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel,
     CommonState *state = &channel->state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
-    BYTE * const correlate_row = channel->correlate_row;
     unsigned int pos = 0;
 
     while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
         if (state->wmileft) {
-            FNAME(uncompress_row0_seg)(encoder, channel, pos, correlate_row, cur_row,
+            FNAME(uncompress_row0_seg)(encoder, channel, pos, cur_row,
                                        pos + state->wmileft, bppmask[state->wmidx],
                                        bpc, bpc_mask);
             pos += state->wmileft;
@@ -399,7 +398,7 @@ static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel,
     }
 
     if (width) {
-        FNAME(uncompress_row0_seg)(encoder, channel, pos, correlate_row, cur_row, pos + width,
+        FNAME(uncompress_row0_seg)(encoder, channel, pos, cur_row, pos + width,
                                    bppmask[state->wmidx], bpc, bpc_mask);
         if (DEFwmimax > (int)state->wmidx) {
             state->wmileft -= width;
@@ -428,7 +427,6 @@ static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel,
     decode_eatbits(encoder, codewordlen);
 
 static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
-                                      BYTE *correlate_row,
                                       const PIXEL * const prev_row,
                                       PIXEL * const cur_row,
                                       int i,
@@ -437,6 +435,7 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
                                       const unsigned int bpc_mask)
 {
     CommonState *state = &channel->state;
+    BYTE * const correlate_row = channel->correlate_row;
     const unsigned int waitmask = bppmask[state->wmidx];
     int stopidx;
     int run_index = 0;
@@ -513,12 +512,11 @@ static void FNAME(uncompress_row)(Encoder *encoder, Channel *channel,
     CommonState *state = &channel->state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
-    BYTE * const correlate_row = channel->correlate_row;
     unsigned int pos = 0;
 
     while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
         if (state->wmileft) {
-            FNAME(uncompress_row_seg)(encoder, channel, correlate_row, prev_row, cur_row, pos,
+            FNAME(uncompress_row_seg)(encoder, channel, prev_row, cur_row, pos,
                                       pos + state->wmileft, bpc, bpc_mask);
             pos += state->wmileft;
             width -= state->wmileft;
@@ -530,7 +528,7 @@ static void FNAME(uncompress_row)(Encoder *encoder, Channel *channel,
     }
 
     if (width) {
-        FNAME(uncompress_row_seg)(encoder, channel, correlate_row, prev_row, cur_row, pos,
+        FNAME(uncompress_row_seg)(encoder, channel, prev_row, cur_row, pos,
                                   pos + width, bpc, bpc_mask);
         if (DEFwmimax > (int)state->wmidx) {
             state->wmileft -= width;
commit 81522386181d68f9d66d61aff3233b622bd6e927
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Aug 3 16:24:35 2017 +0200

    quic: Add macros to make quic_tmpl.c much closer to quic_rgb_tmpl.c
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/common/quic_tmpl.c b/common/quic_tmpl.c
index 57c2af6..122f84a 100644
--- a/common/quic_tmpl.c
+++ b/common/quic_tmpl.c
@@ -40,6 +40,9 @@
 #define BPC 8
 #define BPC_MASK 0xffU
 
+#define SET_a(pix, val) ((pix)->a = val)
+#define GET_a(pix) ((pix)->a)
+
 #define _PIXEL_A ((unsigned int)curr[-1].a)
 #define _PIXEL_B ((unsigned int)prev[0].a)
 
@@ -77,6 +80,22 @@ static inline void FNAME(correlate)(const PIXEL *prev, PIXEL *curr, const BYTE c
     curr->a = (family.xlatL2U[correlate] + (int)((_PIXEL_A + _PIXEL_B) >> 1)) & bpc_mask;
 }
 
+#define COMPRESS_ONE_ROW0_0(channel)                                                      \
+    correlate_row[0] = family.xlatU2L[cur_row->a];                                        \
+    golomb_coding(correlate_row[0], find_bucket(channel, correlate_row[-1])->bestcode,    \
+                  &codeword, &codewordlen);                                               \
+    encode(encoder, codeword, codewordlen);
+
+#define COMPRESS_ONE_ROW0(channel, index)                                            \
+    correlate_row[index] = FNAME(decorrelate_0)(&cur_row[index], bpc_mask);          \
+    golomb_coding(correlate_row[index], find_bucket(channel,                         \
+                  correlate_row[index-1])->bestcode,                                 \
+                  &codeword, &codewordlen);                                          \
+    encode(encoder, codeword, codewordlen);
+
+#define UPDATE_MODEL(index)                                                          \
+    update_model(state, find_bucket(channel, correlate_row[index - 1]),              \
+                 correlate_row[index]);
 
 static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
                                      const PIXEL * const cur_row,
@@ -94,17 +113,13 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
     if (i == 0) {
         unsigned int codeword, codewordlen;
 
-        correlate_row[0] = family.xlatU2L[cur_row->a];
-        golomb_coding(correlate_row[0], find_bucket(channel, correlate_row[-1])->bestcode,
-                      &codeword, &codewordlen);
-        encode(encoder, codeword, codewordlen);
+        COMPRESS_ONE_ROW0_0(channel);
 
         if (state->waitcnt) {
             state->waitcnt--;
         } else {
             state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
-            update_model(state, find_bucket(channel, correlate_row[-1]),
-                         correlate_row[i]);
+            UPDATE_MODEL(0);
         }
         stopidx = ++i + state->waitcnt;
     } else {
@@ -114,28 +129,24 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
     while (stopidx < end) {
         for (; i <= stopidx; i++) {
             unsigned int codeword, codewordlen;
-            correlate_row[i] = FNAME(decorrelate_0)(&cur_row[i], bpc_mask);
-            golomb_coding(correlate_row[i],
-                          find_bucket(channel, correlate_row[i - 1])->bestcode, &codeword,
-                          &codewordlen);
-            encode(encoder, codeword, codewordlen);
+            COMPRESS_ONE_ROW0(channel, i);
         }
 
-        update_model(state, find_bucket(channel, correlate_row[stopidx - 1]),
-                     correlate_row[stopidx]);
+        UPDATE_MODEL(stopidx);
         stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
         unsigned int codeword, codewordlen;
-        correlate_row[i] = FNAME(decorrelate_0)(&cur_row[i], bpc_mask);
-        golomb_coding(correlate_row[i], find_bucket(channel, correlate_row[i - 1])->bestcode,
-                      &codeword, &codewordlen);
-        encode(encoder, codeword, codewordlen);
+
+        COMPRESS_ONE_ROW0(channel, i);
     }
     state->waitcnt = stopidx - end;
 }
 
+#undef COMPRESS_ONE_ROW0_0
+#undef COMPRESS_ONE_ROW0
+
 static void FNAME(compress_row0)(Encoder *encoder, Channel *channel, const PIXEL *cur_row,
                                  unsigned int width)
 {
@@ -170,6 +181,21 @@ static void FNAME(compress_row0)(Encoder *encoder, Channel *channel, const PIXEL
     spice_assert(DEFwminext > 0);
 }
 
+#define COMPRESS_ONE_0(channel) \
+    correlate_row[0] = family.xlatU2L[(unsigned)((int)GET_a(cur_row) -                  \
+                                                    (int)GET_a(prev_row) ) & bpc_mask]; \
+        golomb_coding(correlate_row[0],                                                 \
+                      find_bucket(channel, correlate_row[-1])->bestcode,                \
+                      &codeword, &codewordlen);                                         \
+        encode(encoder, codeword, codewordlen);
+
+#define COMPRESS_ONE(channel, index)                                                            \
+     correlate_row[index] = FNAME(decorrelate)(&prev_row[index], &cur_row[index], bpc_mask);    \
+     golomb_coding(correlate_row[index],                                                        \
+                  find_bucket(channel, correlate_row[index - 1])->bestcode,                     \
+                  &codeword, &codewordlen);                                                     \
+     encode(encoder, codeword, codewordlen);
+
 static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
                                     const PIXEL * const prev_row,
                                     const PIXEL * const cur_row,
@@ -189,14 +215,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
     if (i == 0) {
         unsigned int codeword, codewordlen;
 
-        correlate_row[0] = family.xlatU2L[(unsigned)((int)cur_row->a -
-                                                     (int)prev_row->a) & bpc_mask];
-
-        golomb_coding(correlate_row[0],
-                      find_bucket(channel, correlate_row[-1])->bestcode,
-                      &codeword,
-                      &codewordlen);
-        encode(encoder, codeword, codewordlen);
+        COMPRESS_ONE_0(channel);
 
         if (state->waitcnt) {
             state->waitcnt--;
@@ -214,26 +233,17 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
             for (; i <= stopidx; i++) {
                 unsigned int codeword, codewordlen;
                 RLE_PRED_IMP;
-                correlate_row[i] = FNAME(decorrelate)(&prev_row[i], &cur_row[i], bpc_mask);
-                golomb_coding(correlate_row[i],
-                              find_bucket(channel, correlate_row[i - 1])->bestcode, &codeword,
-                              &codewordlen);
-                encode(encoder, codeword, codewordlen);
+                COMPRESS_ONE(channel, i);
             }
 
-            update_model(state, find_bucket(channel, correlate_row[stopidx - 1]),
-                         correlate_row[stopidx]);
+            UPDATE_MODEL(stopidx);
             stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
 
         for (; i < end; i++) {
             unsigned int codeword, codewordlen;
             RLE_PRED_IMP;
-            correlate_row[i] = FNAME(decorrelate)(&prev_row[i], &cur_row[i], bpc_mask);
-            golomb_coding(correlate_row[i], find_bucket(channel,
-                                                        correlate_row[i - 1])->bestcode,
-                          &codeword, &codewordlen);
-            encode(encoder, codeword, codewordlen);
+            COMPRESS_ONE(channel, i);
         }
         state->waitcnt = stopidx - end;
 
@@ -294,6 +304,23 @@ static void FNAME(compress_row)(Encoder *encoder, Channel *channel,
     spice_assert(DEFwminext > 0);
 }
 
+#define UNCOMPRESS_PIX_START(row) do { } while (0)
+
+#define UNCOMPRESS_ONE_ROW0_0(channel)                                                          \
+    correlate_row[0] = (BYTE)golomb_decoding(find_bucket(channel,                               \
+                                                         correlate_row[-1])->bestcode,          \
+                                                         encoder->io_word, &codewordlen);       \
+    SET_a(&cur_row[0], (BYTE)family.xlatL2U[correlate_row[0]]);                                 \
+    decode_eatbits(encoder, codewordlen);
+
+#define UNCOMPRESS_ONE_ROW0(channel)                                            \
+    correlate_row[i] = (BYTE)golomb_decoding(find_bucket(channel,               \
+                                             correlate_row[i - 1])->bestcode,   \
+                                             encoder->io_word,                  \
+                                             &codewordlen);                     \
+    FNAME(correlate_0)(&cur_row[i], correlate_row[i], bpc_mask);                \
+    decode_eatbits(encoder, codewordlen);
+
 static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i,
                                        BYTE * const correlate_row,
                                        PIXEL * const cur_row,
@@ -310,18 +337,14 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
     if (i == 0) {
         unsigned int codewordlen;
 
-        correlate_row[0] = (BYTE)golomb_decoding(find_bucket(channel,
-                                                             correlate_row[-1])->bestcode,
-                                                 encoder->io_word, &codewordlen);
-        cur_row[0].a = (BYTE)family.xlatL2U[correlate_row[0]];
-        decode_eatbits(encoder, codewordlen);
+        UNCOMPRESS_PIX_START(&cur_row[i]);
+        UNCOMPRESS_ONE_ROW0_0(channel);
 
         if (state->waitcnt) {
             --state->waitcnt;
         } else {
             state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
-            update_model(state, find_bucket(channel, correlate_row[-1]),
-                         correlate_row[0]);
+            UPDATE_MODEL(0);
         }
         stopidx = ++i + state->waitcnt;
     } else {
@@ -329,31 +352,23 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
     }
 
     while (stopidx < end) {
-        struct s_bucket * pbucket = NULL;
 
         for (; i <= stopidx; i++) {
             unsigned int codewordlen;
 
-            pbucket = find_bucket(channel, correlate_row[i - 1]);
-            correlate_row[i] = (BYTE)golomb_decoding(pbucket->bestcode, encoder->io_word,
-                                                     &codewordlen);
-            FNAME(correlate_0)(&cur_row[i], correlate_row[i], bpc_mask);
-            decode_eatbits(encoder, codewordlen);
+            UNCOMPRESS_PIX_START(&cur_row[i]);
+            UNCOMPRESS_ONE_ROW0(channel);
         }
 
-        update_model(state, pbucket, correlate_row[stopidx]);
-
+        UPDATE_MODEL(stopidx);
         stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
         unsigned int codewordlen;
 
-        correlate_row[i] = (BYTE)golomb_decoding(find_bucket(channel,
-                                                             correlate_row[i - 1])->bestcode,
-                                                 encoder->io_word, &codewordlen);
-        FNAME(correlate_0)(&cur_row[i], correlate_row[i], bpc_mask);
-        decode_eatbits(encoder, codewordlen);
+        UNCOMPRESS_PIX_START(&cur_row[i]);
+        UNCOMPRESS_ONE_ROW0(channel);
     }
     state->waitcnt = stopidx - end;
 }
@@ -396,6 +411,22 @@ static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel,
     spice_assert(DEFwminext > 0);
 }
 
+#define UNCOMPRESS_ONE_0(channel) \
+    correlate_row[0] = (BYTE)golomb_decoding(find_bucket(channel,                               \
+                                                         correlate_row[-1])->bestcode,          \
+                                                         encoder->io_word, &codewordlen);       \
+    SET_a(&cur_row[0], (family.xlatL2U[correlate_row[0]] +                                      \
+                  GET_a(prev_row)) & bpc_mask);                                                 \
+    decode_eatbits(encoder, codewordlen);
+
+#define UNCOMPRESS_ONE(channel)                                                                 \
+    correlate_row[i] = (BYTE)golomb_decoding(find_bucket(channel,                               \
+                                                         correlate_row[i - 1])->bestcode,       \
+                                                         encoder->io_word,                      \
+                                                         &codewordlen);                         \
+    FNAME(correlate)(&prev_row[i], &cur_row[i], correlate_row[i], bpc_mask);                    \
+    decode_eatbits(encoder, codewordlen);
+
 static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
                                       BYTE *correlate_row,
                                       const PIXEL * const prev_row,
@@ -416,17 +447,14 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
     if (i == 0) {
         unsigned int codewordlen;
 
-        correlate_row[0] = (BYTE)golomb_decoding(find_bucket(channel, correlate_row[-1])->bestcode,
-                                                             encoder->io_word, &codewordlen);
-        cur_row[0].a = (family.xlatL2U[correlate_row[0]] + prev_row[0].a) & bpc_mask;
-        decode_eatbits(encoder, codewordlen);
+        UNCOMPRESS_PIX_START(&cur_row[i]);
+        UNCOMPRESS_ONE_0(channel);
 
         if (state->waitcnt) {
             --state->waitcnt;
         } else {
             state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
-            update_model(state, find_bucket(channel, correlate_row[-1]),
-                         correlate_row[0]);
+            UPDATE_MODEL(0);
         }
         stopidx = ++i + state->waitcnt;
     } else {
@@ -434,19 +462,15 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
     }
     for (;;) {
         while (stopidx < end) {
-            struct s_bucket * pbucket = NULL;
-
             for (; i <= stopidx; i++) {
                 unsigned int codewordlen;
                 RLE_PRED_IMP;
-                pbucket = find_bucket(channel, correlate_row[i - 1]);
-                correlate_row[i] = (BYTE)golomb_decoding(pbucket->bestcode, encoder->io_word,
-                                                         &codewordlen);
-                FNAME(correlate)(&prev_row[i], &cur_row[i], correlate_row[i], bpc_mask);
-                decode_eatbits(encoder, codewordlen);
+
+                UNCOMPRESS_PIX_START(&cur_row[i]);
+                UNCOMPRESS_ONE(channel);
             }
 
-            update_model(state, pbucket, correlate_row[stopidx]);
+            UPDATE_MODEL(stopidx);
 
             stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
@@ -454,11 +478,8 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
         for (; i < end; i++) {
             unsigned int codewordlen;
             RLE_PRED_IMP;
-            correlate_row[i] = (BYTE)golomb_decoding(find_bucket(channel,
-                                                                 correlate_row[i - 1])->bestcode,
-                                                     encoder->io_word, &codewordlen);
-            FNAME(correlate)(&prev_row[i], &cur_row[i], correlate_row[i], bpc_mask);
-            decode_eatbits(encoder, codewordlen);
+            UNCOMPRESS_PIX_START(&cur_row[i]);
+            UNCOMPRESS_ONE(channel);
         }
 
         state->waitcnt = stopidx - end;
@@ -471,7 +492,8 @@ do_run:
         run_end = i + decode_channel_run(encoder, channel);
 
         for (; i < run_end; i++) {
-            cur_row[i].a = cur_row[i - 1].a;
+            UNCOMPRESS_PIX_START(&cur_row[i]);
+            SET_a(&cur_row[i], GET_a(&cur_row[i - 1]));
         }
 
         if (i == end) {
@@ -532,3 +554,11 @@ static void FNAME(uncompress_row)(Encoder *encoder, Channel *channel,
 #undef family
 #undef BPC
 #undef BPC_MASK
+#undef UPDATE_MODEL
+#undef UNCOMPRESS_PIX_START
+#undef COMPRESS_ONE_0
+#undef COMPRESS_ONE
+#undef UNCOMPRESS_ONE_ROW0
+#undef UNCOMPRESS_ONE_ROW0_0
+#undef UNCOMPRESS_ONE_0
+#undef UNCOMPRESS_ONE
commit 423518768ea937eabe64ad4f0cae3673c593d401
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Aug 3 16:24:34 2017 +0200

    quic: s/decorrelate_drow/correlate_row
    
    The naming is odd as this is just an alias for channel->correlate_row.
    This will also help in subsequent commits to make things more
    consistent with quic_rgb_tmpl.c
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/common/quic_tmpl.c b/common/quic_tmpl.c
index 56f04fa..57c2af6 100644
--- a/common/quic_tmpl.c
+++ b/common/quic_tmpl.c
@@ -86,7 +86,7 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
                                      const unsigned int bpc_mask)
 {
     CommonState *state = &channel->state;
-    BYTE * const decorrelate_drow = channel->correlate_row;
+    BYTE * const correlate_row = channel->correlate_row;
     int stopidx;
 
     spice_assert(end - i > 0);
@@ -94,8 +94,8 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
     if (i == 0) {
         unsigned int codeword, codewordlen;
 
-        decorrelate_drow[0] = family.xlatU2L[cur_row->a];
-        golomb_coding(decorrelate_drow[0], find_bucket(channel, decorrelate_drow[-1])->bestcode,
+        correlate_row[0] = family.xlatU2L[cur_row->a];
+        golomb_coding(correlate_row[0], find_bucket(channel, correlate_row[-1])->bestcode,
                       &codeword, &codewordlen);
         encode(encoder, codeword, codewordlen);
 
@@ -103,8 +103,8 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
             state->waitcnt--;
         } else {
             state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
-            update_model(state, find_bucket(channel, decorrelate_drow[-1]),
-                         decorrelate_drow[i]);
+            update_model(state, find_bucket(channel, correlate_row[-1]),
+                         correlate_row[i]);
         }
         stopidx = ++i + state->waitcnt;
     } else {
@@ -114,22 +114,22 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
     while (stopidx < end) {
         for (; i <= stopidx; i++) {
             unsigned int codeword, codewordlen;
-            decorrelate_drow[i] = FNAME(decorrelate_0)(&cur_row[i], bpc_mask);
-            golomb_coding(decorrelate_drow[i],
-                          find_bucket(channel, decorrelate_drow[i - 1])->bestcode, &codeword,
+            correlate_row[i] = FNAME(decorrelate_0)(&cur_row[i], bpc_mask);
+            golomb_coding(correlate_row[i],
+                          find_bucket(channel, correlate_row[i - 1])->bestcode, &codeword,
                           &codewordlen);
             encode(encoder, codeword, codewordlen);
         }
 
-        update_model(state, find_bucket(channel, decorrelate_drow[stopidx - 1]),
-                     decorrelate_drow[stopidx]);
+        update_model(state, find_bucket(channel, correlate_row[stopidx - 1]),
+                     correlate_row[stopidx]);
         stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
         unsigned int codeword, codewordlen;
-        decorrelate_drow[i] = FNAME(decorrelate_0)(&cur_row[i], bpc_mask);
-        golomb_coding(decorrelate_drow[i], find_bucket(channel, decorrelate_drow[i - 1])->bestcode,
+        correlate_row[i] = FNAME(decorrelate_0)(&cur_row[i], bpc_mask);
+        golomb_coding(correlate_row[i], find_bucket(channel, correlate_row[i - 1])->bestcode,
                       &codeword, &codewordlen);
         encode(encoder, codeword, codewordlen);
     }
@@ -179,7 +179,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
                                     const unsigned int bpc_mask)
 {
     CommonState *state = &channel->state;
-    BYTE * const decorrelate_drow = channel->correlate_row;
+    BYTE * const correlate_row = channel->correlate_row;
     int stopidx;
     int run_index = 0;
     int run_size;
@@ -189,11 +189,11 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
     if (i == 0) {
         unsigned int codeword, codewordlen;
 
-        decorrelate_drow[0] = family.xlatU2L[(unsigned)((int)cur_row->a -
-                                                       (int)prev_row->a) & bpc_mask];
+        correlate_row[0] = family.xlatU2L[(unsigned)((int)cur_row->a -
+                                                     (int)prev_row->a) & bpc_mask];
 
-        golomb_coding(decorrelate_drow[0],
-                      find_bucket(channel, decorrelate_drow[-1])->bestcode,
+        golomb_coding(correlate_row[0],
+                      find_bucket(channel, correlate_row[-1])->bestcode,
                       &codeword,
                       &codewordlen);
         encode(encoder, codeword, codewordlen);
@@ -202,8 +202,8 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
             state->waitcnt--;
         } else {
             state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
-            update_model(state, find_bucket(channel, decorrelate_drow[-1]),
-                         decorrelate_drow[0]);
+            update_model(state, find_bucket(channel, correlate_row[-1]),
+                         correlate_row[0]);
         }
         stopidx = ++i + state->waitcnt;
     } else {
@@ -214,24 +214,24 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
             for (; i <= stopidx; i++) {
                 unsigned int codeword, codewordlen;
                 RLE_PRED_IMP;
-                decorrelate_drow[i] = FNAME(decorrelate)(&prev_row[i], &cur_row[i], bpc_mask);
-                golomb_coding(decorrelate_drow[i],
-                              find_bucket(channel, decorrelate_drow[i - 1])->bestcode, &codeword,
+                correlate_row[i] = FNAME(decorrelate)(&prev_row[i], &cur_row[i], bpc_mask);
+                golomb_coding(correlate_row[i],
+                              find_bucket(channel, correlate_row[i - 1])->bestcode, &codeword,
                               &codewordlen);
                 encode(encoder, codeword, codewordlen);
             }
 
-            update_model(state, find_bucket(channel, decorrelate_drow[stopidx - 1]),
-                         decorrelate_drow[stopidx]);
+            update_model(state, find_bucket(channel, correlate_row[stopidx - 1]),
+                         correlate_row[stopidx]);
             stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
 
         for (; i < end; i++) {
             unsigned int codeword, codewordlen;
             RLE_PRED_IMP;
-            decorrelate_drow[i] = FNAME(decorrelate)(&prev_row[i], &cur_row[i], bpc_mask);
-            golomb_coding(decorrelate_drow[i], find_bucket(channel,
-                                                          decorrelate_drow[i - 1])->bestcode,
+            correlate_row[i] = FNAME(decorrelate)(&prev_row[i], &cur_row[i], bpc_mask);
+            golomb_coding(correlate_row[i], find_bucket(channel,
+                                                        correlate_row[i - 1])->bestcode,
                           &codeword, &codewordlen);
             encode(encoder, codeword, codewordlen);
         }
commit 4690e7239d7c2bdb84a0e915533b33f1130965e0
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Aug 3 16:24:33 2017 +0200

    quic: Introduce CommonState *state variable in templates
    
    Most functions in quic_tmpl.c/quic_rgb_tmpl.c have only superficial
    differences. One of them is using channel->state or encoder->rgb_state.
    
    This commit adds a local CommonState *state in all template methods
    which will be used instead of channel->state or encoder->rgb_state.
    This makes it easier to spot the common code between the 2 template
    files...
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/common/quic_rgb_tmpl.c b/common/quic_rgb_tmpl.c
index 0a1b9e5..4800ece 100644
--- a/common/quic_rgb_tmpl.c
+++ b/common/quic_rgb_tmpl.c
@@ -144,12 +144,12 @@
                   &codeword, &codewordlen);                                             \
     encode(encoder, codeword, codewordlen);
 
-#define UPDATE_MODEL(index)                                                                 \
-    update_model(&encoder->rgb_state, find_bucket(channel_r, correlate_row_r[index - 1]),   \
+#define UPDATE_MODEL(index)                                                            \
+    update_model(state, find_bucket(channel_r, correlate_row_r[index - 1]),            \
                 correlate_row_r[index]);                                               \
-    update_model(&encoder->rgb_state, find_bucket(channel_g, correlate_row_g[index - 1]),   \
+    update_model(state, find_bucket(channel_g, correlate_row_g[index - 1]),            \
                 correlate_row_g[index]);                                               \
-    update_model(&encoder->rgb_state, find_bucket(channel_b, correlate_row_b[index - 1]),   \
+    update_model(state, find_bucket(channel_b, correlate_row_b[index - 1]),            \
                 correlate_row_b[index]);
 
 
@@ -169,6 +169,7 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, int i,
                                      SPICE_GNUC_UNUSED const unsigned int bpc,
                                      const unsigned int bpc_mask)
 {
+    CommonState *state = &encoder->rgb_state;
     Channel * const channel_r = encoder->channels;
     Channel * const channel_g = channel_r + 1;
     Channel * const channel_b = channel_g + 1;
@@ -187,15 +188,15 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, int i,
         COMPRESS_ONE_ROW0_0(g);
         COMPRESS_ONE_ROW0_0(b);
 
-        if (encoder->rgb_state.waitcnt) {
-            encoder->rgb_state.waitcnt--;
+        if (state->waitcnt) {
+            state->waitcnt--;
         } else {
-            encoder->rgb_state.waitcnt = (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
             UPDATE_MODEL(0);
         }
-        stopidx = ++i + encoder->rgb_state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + encoder->rgb_state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 
     while (stopidx < end) {
@@ -207,7 +208,7 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, int i,
         }
 
         UPDATE_MODEL(stopidx);
-        stopidx = i + (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+        stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
@@ -217,39 +218,40 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, int i,
         COMPRESS_ONE_ROW0(g, i);
         COMPRESS_ONE_ROW0(b, i);
     }
-    encoder->rgb_state.waitcnt = stopidx - end;
+    state->waitcnt = stopidx - end;
 }
 
 static void FNAME(compress_row0)(Encoder *encoder, const PIXEL *cur_row,
                                  unsigned int width)
 {
+    CommonState *state = &encoder->rgb_state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     int pos = 0;
 
-    while ((DEFwmimax > (int)encoder->rgb_state.wmidx) && (encoder->rgb_state.wmileft <= width)) {
-        if (encoder->rgb_state.wmileft) {
-            FNAME(compress_row0_seg)(encoder, pos, cur_row, pos + encoder->rgb_state.wmileft,
-                                     bppmask[encoder->rgb_state.wmidx], bpc, bpc_mask);
-            width -= encoder->rgb_state.wmileft;
-            pos += encoder->rgb_state.wmileft;
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
+            FNAME(compress_row0_seg)(encoder, pos, cur_row, pos + state->wmileft,
+                                     bppmask[state->wmidx], bpc, bpc_mask);
+            width -= state->wmileft;
+            pos += state->wmileft;
         }
 
-        encoder->rgb_state.wmidx++;
-        set_wm_trigger(&encoder->rgb_state);
-        encoder->rgb_state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(compress_row0_seg)(encoder, pos, cur_row, pos + width,
-                                 bppmask[encoder->rgb_state.wmidx], bpc, bpc_mask);
-        if (DEFwmimax > (int)encoder->rgb_state.wmidx) {
-            encoder->rgb_state.wmileft -= width;
+                                 bppmask[state->wmidx], bpc, bpc_mask);
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)encoder->rgb_state.wmidx <= DEFwmimax);
-    spice_assert(encoder->rgb_state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
@@ -277,6 +279,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, int i,
                                     SPICE_GNUC_UNUSED const unsigned int bpc,
                                     const unsigned int bpc_mask)
 {
+    CommonState *state = &encoder->rgb_state;
     Channel * const channel_r = encoder->channels;
     Channel * const channel_g = channel_r + 1;
     Channel * const channel_b = channel_g + 1;
@@ -297,15 +300,15 @@ static void FNAME(compress_row_seg)(Encoder *encoder, int i,
         COMPRESS_ONE_0(g);
         COMPRESS_ONE_0(b);
 
-        if (encoder->rgb_state.waitcnt) {
-            encoder->rgb_state.waitcnt--;
+        if (state->waitcnt) {
+            state->waitcnt--;
         } else {
-            encoder->rgb_state.waitcnt = (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
             UPDATE_MODEL(0);
         }
-        stopidx = ++i + encoder->rgb_state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + encoder->rgb_state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
     for (;;) {
         while (stopidx < end) {
@@ -318,7 +321,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, int i,
             }
 
             UPDATE_MODEL(stopidx);
-            stopidx = i + (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+            stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
 
         for (; i < end; i++) {
@@ -328,13 +331,13 @@ static void FNAME(compress_row_seg)(Encoder *encoder, int i,
             COMPRESS_ONE(g, i);
             COMPRESS_ONE(b, i);
         }
-        encoder->rgb_state.waitcnt = stopidx - end;
+        state->waitcnt = stopidx - end;
 
         return;
 
 do_run:
         run_index = i;
-        encoder->rgb_state.waitcnt = stopidx - i;
+        state->waitcnt = stopidx - i;
         run_size = 0;
 
         while (SAME_PIXEL(&cur_row[i], &cur_row[i - 1])) {
@@ -345,7 +348,7 @@ do_run:
             }
         }
         encode_run(encoder, run_size);
-        stopidx = i + encoder->rgb_state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 }
 
@@ -355,35 +358,36 @@ static void FNAME(compress_row)(Encoder *encoder,
                                 unsigned int width)
 
 {
+    CommonState *state = &encoder->rgb_state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     unsigned int pos = 0;
 
-    while ((DEFwmimax > (int)encoder->rgb_state.wmidx) && (encoder->rgb_state.wmileft <= width)) {
-        if (encoder->rgb_state.wmileft) {
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
             FNAME(compress_row_seg)(encoder, pos, prev_row, cur_row,
-                                    pos + encoder->rgb_state.wmileft,
-                                    bppmask[encoder->rgb_state.wmidx],
+                                    pos + state->wmileft,
+                                    bppmask[state->wmidx],
                                     bpc, bpc_mask);
-            width -= encoder->rgb_state.wmileft;
-            pos += encoder->rgb_state.wmileft;
+            width -= state->wmileft;
+            pos += state->wmileft;
         }
 
-        encoder->rgb_state.wmidx++;
-        set_wm_trigger(&encoder->rgb_state);
-        encoder->rgb_state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(compress_row_seg)(encoder, pos, prev_row, cur_row, pos + width,
-                                bppmask[encoder->rgb_state.wmidx], bpc, bpc_mask);
-        if (DEFwmimax > (int)encoder->rgb_state.wmidx) {
-            encoder->rgb_state.wmileft -= width;
+                                bppmask[state->wmidx], bpc, bpc_mask);
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)encoder->rgb_state.wmidx <= DEFwmimax);
-    spice_assert(encoder->rgb_state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
@@ -412,6 +416,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, int i,
                                        SPICE_GNUC_UNUSED const unsigned int bpc,
                                        const unsigned int bpc_mask)
 {
+    CommonState *state = &encoder->rgb_state;
     Channel * const channel_r = encoder->channels;
     Channel * const channel_g = channel_r + 1;
     Channel * const channel_b = channel_g + 1;
@@ -431,15 +436,15 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, int i,
         UNCOMPRESS_ONE_ROW0_0(g);
         UNCOMPRESS_ONE_ROW0_0(b);
 
-        if (encoder->rgb_state.waitcnt) {
-            --encoder->rgb_state.waitcnt;
+        if (state->waitcnt) {
+            --state->waitcnt;
         } else {
-            encoder->rgb_state.waitcnt = (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
             UPDATE_MODEL(0);
         }
-        stopidx = ++i + encoder->rgb_state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + encoder->rgb_state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 
     while (stopidx < end) {
@@ -452,7 +457,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, int i,
             UNCOMPRESS_ONE_ROW0(b);
         }
         UPDATE_MODEL(stopidx);
-        stopidx = i + (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+        stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
@@ -463,7 +468,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, int i,
         UNCOMPRESS_ONE_ROW0(g);
         UNCOMPRESS_ONE_ROW0(b);
     }
-    encoder->rgb_state.waitcnt = stopidx - end;
+    state->waitcnt = stopidx - end;
 }
 
 static void FNAME(uncompress_row0)(Encoder *encoder,
@@ -471,35 +476,36 @@ static void FNAME(uncompress_row0)(Encoder *encoder,
                                    unsigned int width)
 
 {
+    CommonState *state = &encoder->rgb_state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     unsigned int pos = 0;
 
-    while ((DEFwmimax > (int)encoder->rgb_state.wmidx) && (encoder->rgb_state.wmileft <= width)) {
-        if (encoder->rgb_state.wmileft) {
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
             FNAME(uncompress_row0_seg)(encoder, pos, cur_row,
-                                       pos + encoder->rgb_state.wmileft,
-                                       bppmask[encoder->rgb_state.wmidx],
+                                       pos + state->wmileft,
+                                       bppmask[state->wmidx],
                                        bpc, bpc_mask);
-            pos += encoder->rgb_state.wmileft;
-            width -= encoder->rgb_state.wmileft;
+            pos += state->wmileft;
+            width -= state->wmileft;
         }
 
-        encoder->rgb_state.wmidx++;
-        set_wm_trigger(&encoder->rgb_state);
-        encoder->rgb_state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(uncompress_row0_seg)(encoder, pos, cur_row, pos + width,
-                                   bppmask[encoder->rgb_state.wmidx], bpc, bpc_mask);
-        if (DEFwmimax > (int)encoder->rgb_state.wmidx) {
-            encoder->rgb_state.wmileft -= width;
+                                   bppmask[state->wmidx], bpc, bpc_mask);
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)encoder->rgb_state.wmidx <= DEFwmimax);
-    spice_assert(encoder->rgb_state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
@@ -528,6 +534,7 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder,
                                       SPICE_GNUC_UNUSED const unsigned int bpc,
                                       const unsigned int bpc_mask)
 {
+    CommonState *state = &encoder->rgb_state;
     Channel * const channel_r = encoder->channels;
     Channel * const channel_g = channel_r + 1;
     Channel * const channel_b = channel_g + 1;
@@ -535,7 +542,7 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder,
     BYTE * const correlate_row_r = channel_r->correlate_row;
     BYTE * const correlate_row_g = channel_g->correlate_row;
     BYTE * const correlate_row_b = channel_b->correlate_row;
-    const unsigned int waitmask = bppmask[encoder->rgb_state.wmidx];
+    const unsigned int waitmask = bppmask[state->wmidx];
     int stopidx;
     int run_index = 0;
     int run_end;
@@ -550,15 +557,15 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder,
         UNCOMPRESS_ONE_0(g);
         UNCOMPRESS_ONE_0(b);
 
-        if (encoder->rgb_state.waitcnt) {
-            --encoder->rgb_state.waitcnt;
+        if (state->waitcnt) {
+            --state->waitcnt;
         } else {
-            encoder->rgb_state.waitcnt = (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
             UPDATE_MODEL(0);
         }
-        stopidx = ++i + encoder->rgb_state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + encoder->rgb_state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
     for (;;) {
         while (stopidx < end) {
@@ -573,7 +580,7 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder,
 
             UPDATE_MODEL(stopidx);
 
-            stopidx = i + (tabrand(&encoder->rgb_state.tabrand_seed) & waitmask);
+            stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
 
         for (; i < end; i++) {
@@ -585,12 +592,12 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder,
             UNCOMPRESS_ONE(b);
         }
 
-        encoder->rgb_state.waitcnt = stopidx - end;
+        state->waitcnt = stopidx - end;
 
         return;
 
 do_run:
-        encoder->rgb_state.waitcnt = stopidx - i;
+        state->waitcnt = stopidx - i;
         run_index = i;
         run_end = i + decode_run(encoder);
 
@@ -605,7 +612,7 @@ do_run:
             return;
         }
 
-        stopidx = i + encoder->rgb_state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 }
 
@@ -615,33 +622,34 @@ static void FNAME(uncompress_row)(Encoder *encoder,
                                   unsigned int width)
 
 {
+    CommonState *state = &encoder->rgb_state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     unsigned int pos = 0;
 
-    while ((DEFwmimax > (int)encoder->rgb_state.wmidx) && (encoder->rgb_state.wmileft <= width)) {
-        if (encoder->rgb_state.wmileft) {
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
             FNAME(uncompress_row_seg)(encoder, prev_row, cur_row, pos,
-                                      pos + encoder->rgb_state.wmileft, bpc, bpc_mask);
-            pos += encoder->rgb_state.wmileft;
-            width -= encoder->rgb_state.wmileft;
+                                      pos + state->wmileft, bpc, bpc_mask);
+            pos += state->wmileft;
+            width -= state->wmileft;
         }
 
-        encoder->rgb_state.wmidx++;
-        set_wm_trigger(&encoder->rgb_state);
-        encoder->rgb_state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(uncompress_row_seg)(encoder, prev_row, cur_row, pos,
                                   pos + width, bpc, bpc_mask);
-        if (DEFwmimax > (int)encoder->rgb_state.wmidx) {
-            encoder->rgb_state.wmileft -= width;
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)encoder->rgb_state.wmidx <= DEFwmimax);
-    spice_assert(encoder->rgb_state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
diff --git a/common/quic_tmpl.c b/common/quic_tmpl.c
index d025627..56f04fa 100644
--- a/common/quic_tmpl.c
+++ b/common/quic_tmpl.c
@@ -85,6 +85,7 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
                                      SPICE_GNUC_UNUSED const unsigned int bpc,
                                      const unsigned int bpc_mask)
 {
+    CommonState *state = &channel->state;
     BYTE * const decorrelate_drow = channel->correlate_row;
     int stopidx;
 
@@ -98,16 +99,16 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
                       &codeword, &codewordlen);
         encode(encoder, codeword, codewordlen);
 
-        if (channel->state.waitcnt) {
-            channel->state.waitcnt--;
+        if (state->waitcnt) {
+            state->waitcnt--;
         } else {
-            channel->state.waitcnt = (tabrand(&channel->state.tabrand_seed) & waitmask);
-            update_model(&channel->state, find_bucket(channel, decorrelate_drow[-1]),
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
+            update_model(state, find_bucket(channel, decorrelate_drow[-1]),
                          decorrelate_drow[i]);
         }
-        stopidx = ++i + channel->state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + channel->state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 
     while (stopidx < end) {
@@ -120,9 +121,9 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
             encode(encoder, codeword, codewordlen);
         }
 
-        update_model(&channel->state, find_bucket(channel, decorrelate_drow[stopidx - 1]),
+        update_model(state, find_bucket(channel, decorrelate_drow[stopidx - 1]),
                      decorrelate_drow[stopidx]);
-        stopidx = i + (tabrand(&channel->state.tabrand_seed) & waitmask);
+        stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
@@ -132,39 +133,40 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i,
                       &codeword, &codewordlen);
         encode(encoder, codeword, codewordlen);
     }
-    channel->state.waitcnt = stopidx - end;
+    state->waitcnt = stopidx - end;
 }
 
 static void FNAME(compress_row0)(Encoder *encoder, Channel *channel, const PIXEL *cur_row,
                                  unsigned int width)
 {
+    CommonState *state = &channel->state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     int pos = 0;
 
-    while ((DEFwmimax > (int)channel->state.wmidx) && (channel->state.wmileft <= width)) {
-        if (channel->state.wmileft) {
-            FNAME(compress_row0_seg)(encoder, channel, pos, cur_row, pos + channel->state.wmileft,
-                                     bppmask[channel->state.wmidx], bpc, bpc_mask);
-            width -= channel->state.wmileft;
-            pos += channel->state.wmileft;
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
+            FNAME(compress_row0_seg)(encoder, channel, pos, cur_row, pos + state->wmileft,
+                                     bppmask[state->wmidx], bpc, bpc_mask);
+            width -= state->wmileft;
+            pos += state->wmileft;
         }
 
-        channel->state.wmidx++;
-        set_wm_trigger(&channel->state);
-        channel->state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(compress_row0_seg)(encoder, channel, pos, cur_row, pos + width,
-                                 bppmask[channel->state.wmidx], bpc, bpc_mask);
-        if (DEFwmimax > (int)channel->state.wmidx) {
-            channel->state.wmileft -= width;
+                                 bppmask[state->wmidx], bpc, bpc_mask);
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)channel->state.wmidx <= DEFwmimax);
-    spice_assert(channel->state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
@@ -176,6 +178,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
                                     SPICE_GNUC_UNUSED const unsigned int bpc,
                                     const unsigned int bpc_mask)
 {
+    CommonState *state = &channel->state;
     BYTE * const decorrelate_drow = channel->correlate_row;
     int stopidx;
     int run_index = 0;
@@ -195,16 +198,16 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
                       &codewordlen);
         encode(encoder, codeword, codewordlen);
 
-        if (channel->state.waitcnt) {
-            channel->state.waitcnt--;
+        if (state->waitcnt) {
+            state->waitcnt--;
         } else {
-            channel->state.waitcnt = (tabrand(&channel->state.tabrand_seed) & waitmask);
-            update_model(&channel->state, find_bucket(channel, decorrelate_drow[-1]),
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
+            update_model(state, find_bucket(channel, decorrelate_drow[-1]),
                          decorrelate_drow[0]);
         }
-        stopidx = ++i + channel->state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + channel->state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
     for (;;) {
         while (stopidx < end) {
@@ -218,9 +221,9 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
                 encode(encoder, codeword, codewordlen);
             }
 
-            update_model(&channel->state, find_bucket(channel, decorrelate_drow[stopidx - 1]),
+            update_model(state, find_bucket(channel, decorrelate_drow[stopidx - 1]),
                          decorrelate_drow[stopidx]);
-            stopidx = i + (tabrand(&channel->state.tabrand_seed) & waitmask);
+            stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
 
         for (; i < end; i++) {
@@ -232,13 +235,13 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i,
                           &codeword, &codewordlen);
             encode(encoder, codeword, codewordlen);
         }
-        channel->state.waitcnt = stopidx - end;
+        state->waitcnt = stopidx - end;
 
         return;
 
 do_run:
         run_index = i;
-        channel->state.waitcnt = stopidx - i;
+        state->waitcnt = stopidx - i;
         run_size = 0;
 
         while (cur_row[i].a == cur_row[i - 1].a) {
@@ -249,7 +252,7 @@ do_run:
             }
         }
         encode_channel_run(encoder, channel, run_size);
-        stopidx = i + channel->state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 }
 
@@ -259,34 +262,35 @@ static void FNAME(compress_row)(Encoder *encoder, Channel *channel,
                                 unsigned int width)
 
 {
+    CommonState *state = &channel->state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     unsigned int pos = 0;
 
-    while ((DEFwmimax > (int)channel->state.wmidx) && (channel->state.wmileft <= width)) {
-        if (channel->state.wmileft) {
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
             FNAME(compress_row_seg)(encoder, channel, pos, prev_row, cur_row,
-                                    pos + channel->state.wmileft, bppmask[channel->state.wmidx],
+                                    pos + state->wmileft, bppmask[state->wmidx],
                                     bpc, bpc_mask);
-            width -= channel->state.wmileft;
-            pos += channel->state.wmileft;
+            width -= state->wmileft;
+            pos += state->wmileft;
         }
 
-        channel->state.wmidx++;
-        set_wm_trigger(&channel->state);
-        channel->state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(compress_row_seg)(encoder, channel, pos, prev_row, cur_row, pos + width,
-                                bppmask[channel->state.wmidx], bpc, bpc_mask);
-        if (DEFwmimax > (int)channel->state.wmidx) {
-            channel->state.wmileft -= width;
+                                bppmask[state->wmidx], bpc, bpc_mask);
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)channel->state.wmidx <= DEFwmimax);
-    spice_assert(channel->state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
@@ -298,6 +302,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
                                        SPICE_GNUC_UNUSED const unsigned int bpc,
                                        const unsigned int bpc_mask)
 {
+    CommonState *state = &channel->state;
     int stopidx;
 
     spice_assert(end - i > 0);
@@ -311,16 +316,16 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
         cur_row[0].a = (BYTE)family.xlatL2U[correlate_row[0]];
         decode_eatbits(encoder, codewordlen);
 
-        if (channel->state.waitcnt) {
-            --channel->state.waitcnt;
+        if (state->waitcnt) {
+            --state->waitcnt;
         } else {
-            channel->state.waitcnt = (tabrand(&channel->state.tabrand_seed) & waitmask);
-            update_model(&channel->state, find_bucket(channel, correlate_row[-1]),
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
+            update_model(state, find_bucket(channel, correlate_row[-1]),
                          correlate_row[0]);
         }
-        stopidx = ++i + channel->state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + channel->state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 
     while (stopidx < end) {
@@ -336,9 +341,9 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
             decode_eatbits(encoder, codewordlen);
         }
 
-        update_model(&channel->state, pbucket, correlate_row[stopidx]);
+        update_model(state, pbucket, correlate_row[stopidx]);
 
-        stopidx = i + (tabrand(&channel->state.tabrand_seed) & waitmask);
+        stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
     }
 
     for (; i < end; i++) {
@@ -350,7 +355,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i
         FNAME(correlate_0)(&cur_row[i], correlate_row[i], bpc_mask);
         decode_eatbits(encoder, codewordlen);
     }
-    channel->state.waitcnt = stopidx - end;
+    state->waitcnt = stopidx - end;
 }
 
 static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel,
@@ -358,35 +363,36 @@ static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel,
                                    unsigned int width)
 
 {
+    CommonState *state = &channel->state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     BYTE * const correlate_row = channel->correlate_row;
     unsigned int pos = 0;
 
-    while ((DEFwmimax > (int)channel->state.wmidx) && (channel->state.wmileft <= width)) {
-        if (channel->state.wmileft) {
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
             FNAME(uncompress_row0_seg)(encoder, channel, pos, correlate_row, cur_row,
-                                       pos + channel->state.wmileft, bppmask[channel->state.wmidx],
+                                       pos + state->wmileft, bppmask[state->wmidx],
                                        bpc, bpc_mask);
-            pos += channel->state.wmileft;
-            width -= channel->state.wmileft;
+            pos += state->wmileft;
+            width -= state->wmileft;
         }
 
-        channel->state.wmidx++;
-        set_wm_trigger(&channel->state);
-        channel->state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(uncompress_row0_seg)(encoder, channel, pos, correlate_row, cur_row, pos + width,
-                                   bppmask[channel->state.wmidx], bpc, bpc_mask);
-        if (DEFwmimax > (int)channel->state.wmidx) {
-            channel->state.wmileft -= width;
+                                   bppmask[state->wmidx], bpc, bpc_mask);
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)channel->state.wmidx <= DEFwmimax);
-    spice_assert(channel->state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
@@ -399,7 +405,8 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
                                       SPICE_GNUC_UNUSED const unsigned int bpc,
                                       const unsigned int bpc_mask)
 {
-    const unsigned int waitmask = bppmask[channel->state.wmidx];
+    CommonState *state = &channel->state;
+    const unsigned int waitmask = bppmask[state->wmidx];
     int stopidx;
     int run_index = 0;
     int run_end;
@@ -414,16 +421,16 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
         cur_row[0].a = (family.xlatL2U[correlate_row[0]] + prev_row[0].a) & bpc_mask;
         decode_eatbits(encoder, codewordlen);
 
-        if (channel->state.waitcnt) {
-            --channel->state.waitcnt;
+        if (state->waitcnt) {
+            --state->waitcnt;
         } else {
-            channel->state.waitcnt = (tabrand(&channel->state.tabrand_seed) & waitmask);
-            update_model(&channel->state, find_bucket(channel, correlate_row[-1]),
+            state->waitcnt = (tabrand(&state->tabrand_seed) & waitmask);
+            update_model(state, find_bucket(channel, correlate_row[-1]),
                          correlate_row[0]);
         }
-        stopidx = ++i + channel->state.waitcnt;
+        stopidx = ++i + state->waitcnt;
     } else {
-        stopidx = i + channel->state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
     for (;;) {
         while (stopidx < end) {
@@ -439,9 +446,9 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
                 decode_eatbits(encoder, codewordlen);
             }
 
-            update_model(&channel->state, pbucket, correlate_row[stopidx]);
+            update_model(state, pbucket, correlate_row[stopidx]);
 
-            stopidx = i + (tabrand(&channel->state.tabrand_seed) & waitmask);
+            stopidx = i + (tabrand(&state->tabrand_seed) & waitmask);
         }
 
         for (; i < end; i++) {
@@ -454,12 +461,12 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel,
             decode_eatbits(encoder, codewordlen);
         }
 
-        channel->state.waitcnt = stopidx - end;
+        state->waitcnt = stopidx - end;
 
         return;
 
 do_run:
-        channel->state.waitcnt = stopidx - i;
+        state->waitcnt = stopidx - i;
         run_index = i;
         run_end = i + decode_channel_run(encoder, channel);
 
@@ -471,7 +478,7 @@ do_run:
             return;
         }
 
-        stopidx = i + channel->state.waitcnt;
+        stopidx = i + state->waitcnt;
     }
 }
 
@@ -481,34 +488,35 @@ static void FNAME(uncompress_row)(Encoder *encoder, Channel *channel,
                                   unsigned int width)
 
 {
+    CommonState *state = &channel->state;
     const unsigned int bpc = BPC;
     const unsigned int bpc_mask = BPC_MASK;
     BYTE * const correlate_row = channel->correlate_row;
     unsigned int pos = 0;
 
-    while ((DEFwmimax > (int)channel->state.wmidx) && (channel->state.wmileft <= width)) {
-        if (channel->state.wmileft) {
+    while ((DEFwmimax > (int)state->wmidx) && (state->wmileft <= width)) {
+        if (state->wmileft) {
             FNAME(uncompress_row_seg)(encoder, channel, correlate_row, prev_row, cur_row, pos,
-                                      pos + channel->state.wmileft, bpc, bpc_mask);
-            pos += channel->state.wmileft;
-            width -= channel->state.wmileft;
+                                      pos + state->wmileft, bpc, bpc_mask);
+            pos += state->wmileft;
+            width -= state->wmileft;
         }
 
-        channel->state.wmidx++;
-        set_wm_trigger(&channel->state);
-        channel->state.wmileft = DEFwminext;
+        state->wmidx++;
+        set_wm_trigger(state);
+        state->wmileft = DEFwminext;
     }
 
     if (width) {
         FNAME(uncompress_row_seg)(encoder, channel, correlate_row, prev_row, cur_row, pos,
                                   pos + width, bpc, bpc_mask);
-        if (DEFwmimax > (int)channel->state.wmidx) {
-            channel->state.wmileft -= width;
+        if (DEFwmimax > (int)state->wmidx) {
+            state->wmileft -= width;
         }
     }
 
-    spice_assert((int)channel->state.wmidx <= DEFwmimax);
-    spice_assert(channel->state.wmidx <= 32);
+    spice_assert((int)state->wmidx <= DEFwmimax);
+    spice_assert(state->wmidx <= 32);
     spice_assert(DEFwminext > 0);
 }
 
commit b9dd7ed8f17c97f973a71d186223198d0ba0fefb
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Aug 3 16:24:32 2017 +0200

    quic: Factor common code
    
    We don't need 2 different implementations when the only difference is
    the CommonState which is being used.
    
    Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/common/quic.c b/common/quic.c
index a3a1208..12e9b0b 100644
--- a/common/quic.c
+++ b/common/quic.c
@@ -514,16 +514,16 @@ static void encoder_init_rle(CommonState *state)
 }
 
 
-static void encode_run(Encoder *encoder, unsigned int runlen) //todo: try use end of line
+static void encode_state_run(Encoder *encoder, CommonState *state, unsigned int runlen)
 {
     int hits = 0;
 
-    while (runlen >= encoder->rgb_state.melcorder) {
+    while (runlen >= state->melcorder) {
         hits++;
-        runlen -= encoder->rgb_state.melcorder;
-        if (encoder->rgb_state.melcstate < MELCSTATES - 1) {
-            encoder->rgb_state.melclen = J[++encoder->rgb_state.melcstate];
-            encoder->rgb_state.melcorder = (1L << encoder->rgb_state.melclen);
+        runlen -= state->melcorder;
+        if (state->melcstate < MELCSTATES - 1) {
+            state->melclen = J[++state->melcstate];
+            state->melcorder = (1L << state->melclen);
         }
     }
 
@@ -534,49 +534,30 @@ static void encode_run(Encoder *encoder, unsigned int runlen) //todo: try use en
     */
     encode_ones(encoder, hits);
 
-    encode(encoder, runlen, encoder->rgb_state.melclen + 1);
+    encode(encoder, runlen, state->melclen + 1);
 
     /* adjust melcoder parameters */
-    if (encoder->rgb_state.melcstate) {
-        encoder->rgb_state.melclen = J[--encoder->rgb_state.melcstate];
-        encoder->rgb_state.melcorder = (1L << encoder->rgb_state.melclen);
+    if (state->melcstate) {
+        state->melclen = J[--state->melcstate];
+        state->melcorder = (1L << state->melclen);
     }
 }
 
-static void encode_channel_run(Encoder *encoder, Channel *channel, unsigned int runlen)
+static void encode_run(Encoder *encoder, unsigned int runlen) //todo: try use end of line
 {
-    //todo: try use end of line
-    int hits = 0;
-
-    while (runlen >= channel->state.melcorder) {
-        hits++;
-        runlen -= channel->state.melcorder;
-        if (channel->state.melcstate < MELCSTATES - 1) {
-            channel->state.melclen = J[++channel->state.melcstate];
-            channel->state.melcorder = (1L << channel->state.melclen);
-        }
-    }
-
-    /* send the required number of "hit" bits (one per occurrence
-       of a run of length melcorder). This number is never too big:
-       after 31 such "hit" bits, each "hit" would represent a run of 32K
-       pixels.
-    */
-    encode_ones(encoder, hits);
-
-    encode(encoder, runlen, channel->state.melclen + 1);
+    encode_state_run(encoder, &encoder->rgb_state, runlen);
+}
 
-    /* adjust melcoder parameters */
-    if (channel->state.melcstate) {
-        channel->state.melclen = J[--channel->state.melcstate];
-        channel->state.melcorder = (1L << channel->state.melclen);
-    }
+static void encode_channel_run(Encoder *encoder, Channel *channel, unsigned int runlen)
+{
+    encode_state_run(encoder, &channel->state, runlen);
 }
 
+
 /* decoding routine: reads bits from the input and returns a run length. */
 /* argument is the number of pixels left to end-of-line (bound on run length) */
 
-static int decode_run(Encoder *encoder)
+static int decode_state_run(Encoder *encoder, CommonState *state)
 {
     int runlen = 0;
 
@@ -585,11 +566,11 @@ static int decode_run(Encoder *encoder)
         temp = lzeroes[(BYTE)(~(encoder->io_word >> 24))];/* number of leading ones in the
                                                                       input stream, up to 8 */
         for (hits = 1; hits <= temp; hits++) {
-            runlen += encoder->rgb_state.melcorder;
+            runlen += state->melcorder;
 
-            if (encoder->rgb_state.melcstate < MELCSTATES - 1) {
-                encoder->rgb_state.melclen = J[++encoder->rgb_state.melcstate];
-                encoder->rgb_state.melcorder = (1U << encoder->rgb_state.melclen);
+            if (state->melcstate < MELCSTATES - 1) {
+                state->melclen = J[++state->melcstate];
+                state->melcorder = (1U << state->melclen);
             }
         }
         if (temp != 8) {
@@ -601,58 +582,29 @@ static int decode_run(Encoder *encoder)
     } while (1);
 
     /* read the length of the remainder */
-    if (encoder->rgb_state.melclen) {
-        runlen += encoder->io_word >> (32 - encoder->rgb_state.melclen);
-        decode_eatbits(encoder, encoder->rgb_state.melclen);
+    if (state->melclen) {
+        runlen += encoder->io_word >> (32 - state->melclen);
+        decode_eatbits(encoder, state->melclen);
     }
 
     /* adjust melcoder parameters */
-    if (encoder->rgb_state.melcstate) {
-        encoder->rgb_state.melclen = J[--encoder->rgb_state.melcstate];
-        encoder->rgb_state.melcorder = (1U << encoder->rgb_state.melclen);
+    if (state->melcstate) {
+        state->melclen = J[--state->melcstate];
+        state->melcorder = (1U << state->melclen);
     }
 
     return runlen;
 }
 
-
-static int decode_channel_run(Encoder *encoder, Channel *channel)
+static int decode_run(Encoder *encoder)
 {
-    int runlen = 0;
-
-    do {
-        register int temp, hits;
-        temp = lzeroes[(BYTE)(~(encoder->io_word >> 24))];/* number of leading ones in the
-                                                                      input stream, up to 8 */
-        for (hits = 1; hits <= temp; hits++) {
-            runlen += channel->state.melcorder;
-
-            if (channel->state.melcstate < MELCSTATES - 1) {
-                channel->state.melclen = J[++channel->state.melcstate];
-                channel->state.melcorder = (1U << channel->state.melclen);
-            }
-        }
-        if (temp != 8) {
-            decode_eatbits(encoder, temp + 1);  /* consume the leading
-                                                            0 of the remainder encoding */
-            break;
-        }
-        decode_eatbits(encoder, 8);
-    } while (1);
-
-    /* read the length of the remainder */
-    if (channel->state.melclen) {
-        runlen += encoder->io_word >> (32 - channel->state.melclen);
-        decode_eatbits(encoder, channel->state.melclen);
-    }
+    return decode_state_run(encoder, &encoder->rgb_state);
+}
 
-    /* adjust melcoder parameters */
-    if (channel->state.melcstate) {
-        channel->state.melclen = J[--channel->state.melcstate];
-        channel->state.melcorder = (1U << channel->state.melclen);
-    }
 
-    return runlen;
+static int decode_channel_run(Encoder *encoder, Channel *channel)
+{
+    return decode_state_run(encoder, &channel->state);
 }
 
 static inline void init_decode_io(Encoder *encoder)


More information about the Spice-commits mailing list