]> git.sesse.net Git - narabu/blobdiff - qdc.cpp
Switch to 64-bit rANS, although probably due for immediate revert (just want to prese...
[narabu] / qdc.cpp
diff --git a/qdc.cpp b/qdc.cpp
index f1ff2542575f866ae4de3c063b2c57acac1586f3..4bb3e9e75c3696eec5b60ed7a976fbefb212d912 100644 (file)
--- a/qdc.cpp
+++ b/qdc.cpp
@@ -5,8 +5,8 @@
 #include <assert.h>
 #include <math.h>
 
-//#include "ryg_rans/rans64.h"
-#include "ryg_rans/rans_byte.h"
+#include "ryg_rans/rans64.h"
+//#include "ryg_rans/rans_byte.h"
 #include "ryg_rans/renormalize.h"
 
 #include <algorithm>
 #define HEIGHT_BLOCKS (HEIGHT/8)
 #define NUM_BLOCKS (WIDTH_BLOCKS * HEIGHT_BLOCKS)
 #define NUM_BLOCKS_CHROMA (WIDTH_BLOCKS_CHROMA * HEIGHT_BLOCKS)
+
 #define NUM_SYMS 256
 #define ESCAPE_LIMIT (NUM_SYMS - 1)
+#define BLOCKS_PER_STREAM 320
 
 // If you set this to 1, the program will try to optimize the placement
 // of coefficients to rANS probability distributions. This is randomized,
@@ -196,11 +198,7 @@ int pick_stats_for(int x, int y, bool is_chroma)
 
 void write_varint(int x, FILE *fp)
 {
-       while (x >= 128) {
-               putc((x & 0x7f) | 0x80, fp);
-               x >>= 7;
-       }
-       putc(x, fp);
+       fwrite(&x, sizeof(x), 1, fp);
 }
 
 class RansEncoder {
@@ -214,8 +212,8 @@ public:
        void init_prob(SymbolStats &s)
        {
                for (int i = 0; i < NUM_SYMS; i++) {
-                       printf("%d: cumfreqs=%d freqs=%d prob_bits=%d\n", i, s.cum_freqs[i], s.freqs[i], prob_bits + 1);
-                       RansEncSymbolInit(&esyms[i], s.cum_freqs[i], s.freqs[i], prob_bits + 1);
+                       //printf("%d: cumfreqs=%d freqs=%d prob_bits=%d\n", i, s.cum_freqs[i], s.freqs[i], prob_bits + 1);
+                       Rans64EncSymbolInit(&esyms[i], s.cum_freqs[i], s.freqs[i], prob_bits + 1);
                }
                sign_bias = s.cum_freqs[NUM_SYMS];
        }
@@ -224,12 +222,12 @@ public:
        {
                out_end = out_buf.get() + out_max_size;
                ptr = out_end; // *end* of output buffer
-               RansEncInit(&rans);
+               Rans64EncInit(&rans);
        }
 
        uint32_t save_block(FILE *codedfp)  // Returns number of bytes.
        {
-               RansEncFlush(&rans, &ptr);
+               Rans64EncFlush(&rans, (uint32_t **)&ptr);
                //printf("post-flush = %08x\n", rans);
 
                uint32_t num_rans_bytes = out_end - ptr;
@@ -259,23 +257,23 @@ public:
 
                clear();
 
-               printf("Saving block: %d rANS bytes\n", num_rans_bytes);
+               //printf("Saving block: %d rANS bytes\n", num_rans_bytes);
                return num_rans_bytes;
                //return num_rans_bytes;
        }
 
        void encode_coeff(short signed_k)
        {
-               //printf("encoding coeff %d (sym %d), rans before encoding = %08x\n", signed_k, ((abs(signed_k) - 1) & 255), rans);
+               //printf("encoding coeff %d (sym %d), rans before encoding = %016lx\n", signed_k, ((abs(signed_k) - 1) & 255), rans);
                unsigned short k = abs(signed_k);
                if (k >= ESCAPE_LIMIT) {
                        // Put the coefficient as a 1/(2^12) symbol _before_
                        // the 255 coefficient, since the decoder will read the
                        // 255 coefficient first.
-                       RansEncPut(&rans, &ptr, k, 1, prob_bits);
+                       Rans64EncPut(&rans, (uint32_t **)&ptr, k, 1, prob_bits);
                        k = ESCAPE_LIMIT;
                }
-               RansEncPutSymbol(&rans, &ptr, &esyms[(k - 1) & (NUM_SYMS - 1)]);
+               Rans64EncPutSymbol(&rans, (uint32_t **)&ptr, &esyms[(k - 1) & (NUM_SYMS - 1)], prob_bits + 1);
                if (signed_k < 0) {
                        rans += sign_bias;
                }
@@ -288,8 +286,8 @@ private:
        unique_ptr<uint8_t[]> out_buf;
        uint8_t *out_end;
        uint8_t *ptr;
-       RansState rans;
-       RansEncSymbol esyms[NUM_SYMS];
+       Rans64State rans;
+       Rans64EncSymbol esyms[NUM_SYMS];
        uint32_t sign_bias;
 
        uint32_t last_block = 0;  // Not a valid 4-byte rANS block (?)
@@ -720,10 +718,10 @@ int main(int argc, char **argv)
 #endif
 
        // DC coefficient pred from the right to left (within each slice)
-       for (unsigned block_idx = 0; block_idx < NUM_BLOCKS; block_idx += 320) {
-               int prev_k = 0;
+       for (unsigned block_idx = 0; block_idx < NUM_BLOCKS; block_idx += BLOCKS_PER_STREAM) {
+               int prev_k = 128;
 
-               for (unsigned subblock_idx = 320; subblock_idx --> 0; ) {
+               for (unsigned subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) {
                        unsigned yb = (block_idx + subblock_idx) / WIDTH_BLOCKS;
                        unsigned xb = (block_idx + subblock_idx) % WIDTH_BLOCKS;
                        int k = coeff_y[(yb * 8) * WIDTH + (xb * 8)];
@@ -733,11 +731,11 @@ int main(int argc, char **argv)
                        prev_k = k;
                }
        }
-       for (unsigned block_idx = 0; block_idx < NUM_BLOCKS_CHROMA; block_idx += 320) {
+       for (unsigned block_idx = 0; block_idx < NUM_BLOCKS_CHROMA; block_idx += BLOCKS_PER_STREAM) {
                int prev_k_cb = 0;
                int prev_k_cr = 0;
 
-               for (unsigned subblock_idx = 320; subblock_idx --> 0; ) {
+               for (unsigned subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) {
                        unsigned yb = (block_idx + subblock_idx) / WIDTH_BLOCKS_CHROMA;
                        unsigned xb = (block_idx + subblock_idx) % WIDTH_BLOCKS_CHROMA;
                        int k_cb = coeff_cb[(yb * 8) * WIDTH/2 + (xb * 8)];
@@ -880,7 +878,7 @@ int main(int argc, char **argv)
                                //printf("encoding coeff %d xb,yb=%d,%d: %d\n", y*8+x, xb, yb, k);
                                rans_encoder.encode_coeff(k);
 
-                               if (block_idx % 320 == 319 || block_idx == NUM_BLOCKS - 1) {
+                               if (block_idx % BLOCKS_PER_STREAM == (BLOCKS_PER_STREAM - 1) || block_idx == NUM_BLOCKS - 1) {
                                        int l = rans_encoder.save_block(codedfp);
                                        num_bytes += l;
                                        lens.push_back(l);
@@ -920,7 +918,7 @@ int main(int argc, char **argv)
                                //printf("encoding coeff %d xb,yb=%d,%d: %d\n", y*8+x, xb, yb, k);
                                rans_encoder.encode_coeff(k);
 
-                               if (block_idx % 320 == 319 || block_idx == NUM_BLOCKS - 1) {
+                               if (block_idx % BLOCKS_PER_STREAM == (BLOCKS_PER_STREAM - 1) || block_idx == NUM_BLOCKS - 1) {
                                        num_bytes += rans_encoder.save_block(codedfp);
                                }
                        }
@@ -945,7 +943,7 @@ int main(int argc, char **argv)
                                //printf("encoding coeff %d xb,yb=%d,%d: %d\n", y*8+x, xb, yb, k);
                                rans_encoder.encode_coeff(k);
 
-                               if (block_idx % 320 == 319 || block_idx == NUM_BLOCKS - 1) {
+                               if (block_idx % BLOCKS_PER_STREAM == (BLOCKS_PER_STREAM - 1) || block_idx == NUM_BLOCKS - 1) {
                                        num_bytes += rans_encoder.save_block(codedfp);
                                }
                        }