10 #define ESCAPE_LIMIT (NUM_SYMS - 1)
12 #include "ryg_rans/rans_byte.h"
16 void fdct_int32(short *const In);
17 void idct_int32(short *const In);
19 unsigned char pix[WIDTH * HEIGHT];
20 short coeff[WIDTH * HEIGHT];
25 static constexpr uint32_t prob_bits = 12;
26 static constexpr uint32_t prob_scale = 1 << prob_bits;
28 struct RansDecodeTable {
29 int cum2sym[prob_scale];
30 RansDecSymbol dsyms[NUM_SYMS];
33 RansDecodeTable decode_tables[NUM_TABLES];
35 static const unsigned char std_luminance_quant_tbl[64] = {
37 16, 11, 10, 16, 24, 40, 51, 61,
38 12, 12, 14, 19, 26, 58, 60, 55,
39 14, 13, 16, 24, 40, 57, 69, 56,
40 14, 17, 22, 29, 51, 87, 80, 62,
41 18, 22, 37, 56, 68, 109, 103, 77,
42 24, 35, 55, 64, 81, 104, 113, 92,
43 49, 64, 78, 87, 103, 121, 120, 101,
44 72, 92, 95, 98, 112, 100, 103, 99
46 // ff_mpeg1_default_intra_matrix
47 8, 16, 19, 22, 26, 27, 29, 34,
48 16, 16, 22, 24, 27, 29, 34, 37,
49 19, 22, 26, 27, 29, 34, 34, 38,
50 22, 22, 26, 27, 29, 34, 37, 40,
51 22, 26, 27, 29, 32, 35, 40, 48,
52 26, 27, 29, 32, 35, 40, 48, 58,
53 26, 27, 29, 34, 38, 46, 56, 69,
54 27, 29, 35, 38, 46, 56, 69, 83
59 const int luma_mapping[64] = {
60 0, 0, 1, 1, 2, 2, 3, 3,
61 0, 0, 1, 2, 2, 2, 3, 3,
62 1, 1, 2, 2, 2, 3, 3, 3,
63 1, 1, 2, 2, 2, 3, 3, 3,
64 1, 2, 2, 2, 2, 3, 3, 3,
65 2, 2, 2, 2, 3, 3, 3, 3,
66 2, 2, 3, 3, 3, 3, 3, 3,
67 3, 3, 3, 3, 3, 3, 3, 3,
69 const int chroma_mapping[64] = {
70 0, 1, 1, 2, 2, 2, 3, 3,
71 1, 1, 2, 2, 2, 3, 3, 3,
72 2, 2, 2, 2, 3, 3, 3, 3,
73 2, 2, 2, 3, 3, 3, 3, 3,
74 2, 3, 3, 3, 3, 3, 3, 3,
75 3, 3, 3, 3, 3, 3, 3, 3,
76 3, 3, 3, 3, 3, 3, 3, 3,
77 3, 3, 3, 3, 3, 3, 3, 3,
80 int pick_stats_for(int x, int y, bool is_chroma)
83 return chroma_mapping[y * 8 + x] + 4;
85 return luma_mapping[y * 8 + x];
89 uint32_t read_varint(FILE *fp)
96 fprintf(stderr, "Premature EOF\n");
100 x |= (ch & 0x7f) << shift;
101 if ((ch & 0x80) == 0) return x;
106 static constexpr int dc_scalefac = 8; // Matches the FDCT's gain.
107 static constexpr double quant_scalefac = 4.0; // whatever?
109 static inline int unquantize(int qf, int coeff_idx)
111 if (coeff_idx == 0) {
112 return qf * dc_scalefac;
118 const int w = std_luminance_quant_tbl[coeff_idx];
119 const int s = quant_scalefac;
120 return (2 * qf * w * s) / 32;
125 FILE *fp = fopen("coded.dat", "rb");
131 uint32_t sign_bias[NUM_TABLES];
132 for (unsigned table = 0; table < NUM_TABLES; ++table) {
133 uint32_t cum_freq = 0;
134 for (unsigned sym = 0; sym < NUM_SYMS; ++sym) {
135 uint32_t freq = read_varint(fp);
136 fprintf(stderr, "sym=%u/%u: freq=%u\n", sym, NUM_SYMS, freq);
137 RansDecSymbolInit(&decode_tables[table].dsyms[(sym + 1) & 255], cum_freq, freq);
138 for (uint32_t i = 0; i < freq; ++i) {
139 if (cum_freq < prob_scale)
140 decode_tables[table].cum2sym[cum_freq] = (sym + 1) & 255;
144 sign_bias[table] = cum_freq;
145 printf("sign_bias=%u (of %d)\n", sign_bias[table], prob_scale * 2);
148 // loop over all coefficients
149 for (unsigned y = 0; y < 8; ++y) {
150 for (unsigned x = 0; x < 8; ++x) {
151 unsigned tbl = pick_stats_for(x, y, false);
155 unique_ptr<uint8_t[]> rans_bytes;
156 uint8_t *rans_ptr = nullptr;
158 // loop over all DCT blocks
159 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
162 uint32_t num_rans_bytes = read_varint(fp);
163 rans_bytes.reset(new uint8_t[num_rans_bytes]);
164 fread(rans_bytes.get(), 1, num_rans_bytes, fp);
166 printf("%d,%d: read %d rANS bytes\n", x, y, num_rans_bytes);
167 //printf("first bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n", rans_bytes[0], rans_bytes[1], rans_bytes[2], rans_bytes[3], rans_bytes[4], rans_bytes[5], rans_bytes[6], rans_bytes[7]);
171 rans_ptr = rans_bytes.get();
172 RansDecInit(&rans, &rans_ptr);
174 for (unsigned xb = 0; xb < WIDTH; xb += 8) {
175 uint32_t bottom_bits = RansDecGet(&rans, prob_bits + 1);
177 if (bottom_bits >= sign_bias[tbl]) {
178 bottom_bits -= sign_bias[tbl];
179 rans -= sign_bias[tbl];
182 uint32_t k = decode_tables[tbl].cum2sym[std::min(bottom_bits, prob_scale - 1)];
183 RansDecAdvanceSymbol(&rans, &rans_ptr, &decode_tables[tbl].dsyms[k], prob_bits + 1);
184 assert(k <= ESCAPE_LIMIT);
185 if (k == ESCAPE_LIMIT) {
186 k = RansDecGet(&rans, prob_bits);
187 assert(k >= ESCAPE_LIMIT);
188 RansDecAdvance(&rans, &rans_ptr, k, 1, prob_bits);
196 int reversed_yb = yb ^ 8;
197 int reversed_xb = WIDTH - 8 - xb;
198 coeff[(reversed_yb + y) * WIDTH + (reversed_xb + x)] = k;
199 // printf("coeff %d xb,yb=%d,%d: decoded %d\n", y * 8 + x, reversed_xb, reversed_yb, k);
204 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
205 for (unsigned xb = 0; xb < WIDTH; xb += 8) {
211 // DC coefficient pred from the right to left
212 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
213 for (int xb = WIDTH - 16; xb >= 0; xb -= 8) {
214 coeff[yb * WIDTH + xb] += coeff[yb * WIDTH + (xb + 8)];
219 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
220 for (unsigned xb = 0; xb < WIDTH; xb += 8) {
223 for (unsigned y = 0; y < 8; ++y) {
224 for (unsigned x = 0; x < 8; ++x) {
225 int k = coeff[(yb + y) * WIDTH + (xb + x)];
226 in[y * 8 + x] = unquantize(k, y * 8 + x);
227 printf("%3d ", in[y * 8 + x]);
235 // Clamp and move back
236 for (unsigned y = 0; y < 8; ++y) {
237 for (unsigned x = 0; x < 8; ++x) {
238 int k = in[y * 8 + x];
241 if (k > 255) k = 255;
242 pix[(yb + y) * WIDTH + (xb + x)] = k;
250 fp = fopen("output.pgm", "wb");
251 fprintf(fp, "P5\n%d %d\n255\n", WIDTH, HEIGHT);
252 fwrite(pix, 1, WIDTH * HEIGHT, fp);