#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>
void write_varint(int x, FILE *fp)
{
- fwrite(&x, sizeof(x), 1, fp);
+ while (x >= 128) {
+ putc((x & 0x7f) | 0x80, fp);
+ x >>= 7;
+ }
+ putc(x, fp);
}
class RansEncoder {
{
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);
- Rans64EncSymbolInit(&esyms[i], s.cum_freqs[i], s.freqs[i], prob_bits + 1);
+ RansEncSymbolInit(&esyms[i], s.cum_freqs[i], s.freqs[i], prob_bits + 1);
}
sign_bias = s.cum_freqs[NUM_SYMS];
}
{
out_end = out_buf.get() + out_max_size;
ptr = out_end; // *end* of output buffer
- Rans64EncInit(&rans);
+ RansEncInit(&rans);
}
uint32_t save_block(FILE *codedfp) // Returns number of bytes.
{
- Rans64EncFlush(&rans, (uint32_t **)&ptr);
+ RansEncFlush(&rans, &ptr);
//printf("post-flush = %08x\n", rans);
uint32_t num_rans_bytes = out_end - ptr;
-#if 0
- if (num_rans_bytes == 4) {
- uint32_t block;
- memcpy(&block, ptr, 4);
-
- if (block == last_block) {
- write_varint(0, codedfp);
- clear();
- return 1;
- }
-
- last_block = block;
+ if (num_rans_bytes == last_block.size() &&
+ memcmp(last_block.data(), ptr, last_block.size()) == 0) {
+ write_varint(0, codedfp);
+ clear();
+ return 1;
} else {
- last_block = 0;
+ last_block = string((const char *)ptr, num_rans_bytes);
}
-#endif
write_varint(num_rans_bytes, codedfp);
//fwrite(&num_rans_bytes, 1, 4, codedfp);
void encode_coeff(short signed_k)
{
- //printf("encoding coeff %d (sym %d), rans before encoding = %016lx\n", signed_k, ((abs(signed_k) - 1) & 255), rans);
+ //printf("encoding coeff %d (sym %d), rans before encoding = %08x\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.
- Rans64EncPut(&rans, (uint32_t **)&ptr, k, 1, prob_bits);
+ RansEncPut(&rans, &ptr, k, 1, prob_bits);
k = ESCAPE_LIMIT;
}
- Rans64EncPutSymbol(&rans, (uint32_t **)&ptr, &esyms[(k - 1) & (NUM_SYMS - 1)], prob_bits + 1);
+ RansEncPutSymbol(&rans, &ptr, &esyms[(k - 1) & (NUM_SYMS - 1)]);
if (signed_k < 0) {
rans += sign_bias;
}
unique_ptr<uint8_t[]> out_buf;
uint8_t *out_end;
uint8_t *ptr;
- Rans64State rans;
- Rans64EncSymbol esyms[NUM_SYMS];
+ RansState rans;
+ RansEncSymbol esyms[NUM_SYMS];
uint32_t sign_bias;
- uint32_t last_block = 0; // Not a valid 4-byte rANS block (?)
+ std::string last_block;
};
static constexpr int dc_scalefac = 8; // Matches the FDCT's gain.
//double last_cb_cfl_fac = 0.0;
//double last_cr_cfl_fac = 0.0;
+ int max_val_x[8] = {0}, min_val_x[8] = {0};
+ int max_val_y[8] = {0}, min_val_y[8] = {0};
+
// DCT and quantize luma
for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
for (unsigned xb = 0; xb < WIDTH; xb += 8) {
int k = quantize(in_y[coeff_idx], coeff_idx);
coeff_y[(yb + y) * WIDTH + (xb + x)] = k;
+ max_val_x[x] = std::max(max_val_x[x], k);
+ min_val_x[x] = std::min(min_val_x[x], k);
+ max_val_y[y] = std::max(max_val_y[y], k);
+ min_val_y[y] = std::min(min_val_y[y], k);
+
// Store back for reconstruction / PSNR calculation
in_y[coeff_idx] = unquantize(k, coeff_idx);
}
extra_bits,
extra_bits / 8,
tot_bytes);
+
+#if 0
+ printf("Max coefficient ranges (as a function of x):\n\n");
+ for (unsigned x = 0; x < 8; ++x) {
+ int range = std::max(max_val_x[x], -min_val_x[x]);
+ printf(" [%4d, %4d] (%.2f bits)\n", min_val_x[x], max_val_x[x], log2(range * 2 + 1));
+ }
+
+ printf("Max coefficient ranges (as a function of y):\n\n");
+ for (unsigned y = 0; y < 8; ++y) {
+ int range = std::max(max_val_y[y], -min_val_y[y]);
+ printf(" [%4d, %4d] (%.2f bits)\n", min_val_y[y], max_val_y[y], log2(range * 2 + 1));
+ }
+#endif
}