9 #define WIDTH_BLOCKS (WIDTH/8)
10 #define WIDTH_BLOCKS_CHROMA (WIDTH/16)
11 #define HEIGHT_BLOCKS (HEIGHT/8)
12 #define NUM_BLOCKS (WIDTH_BLOCKS * HEIGHT_BLOCKS)
13 #define NUM_BLOCKS_CHROMA (WIDTH_BLOCKS_CHROMA * HEIGHT_BLOCKS)
16 #define ESCAPE_LIMIT (NUM_SYMS - 1)
17 #define BLOCKS_PER_STREAM 320
19 #include "ryg_rans/rans_byte.h"
23 void fdct_int32(short *const In);
24 void idct_int32(short *const In);
26 unsigned char pix[WIDTH * HEIGHT];
27 short coeff[WIDTH * HEIGHT];
32 static constexpr uint32_t prob_bits = 12;
33 static constexpr uint32_t prob_scale = 1 << prob_bits;
35 struct RansDecodeTable {
36 int cum2sym[prob_scale];
37 RansDecSymbol dsyms[NUM_SYMS];
40 RansDecodeTable decode_tables[NUM_TABLES];
42 static const unsigned char std_luminance_quant_tbl[64] = {
44 16, 11, 10, 16, 24, 40, 51, 61,
45 12, 12, 14, 19, 26, 58, 60, 55,
46 14, 13, 16, 24, 40, 57, 69, 56,
47 14, 17, 22, 29, 51, 87, 80, 62,
48 18, 22, 37, 56, 68, 109, 103, 77,
49 24, 35, 55, 64, 81, 104, 113, 92,
50 49, 64, 78, 87, 103, 121, 120, 101,
51 72, 92, 95, 98, 112, 100, 103, 99
53 // ff_mpeg1_default_intra_matrix
54 8, 16, 19, 22, 26, 27, 29, 34,
55 16, 16, 22, 24, 27, 29, 34, 37,
56 19, 22, 26, 27, 29, 34, 34, 38,
57 22, 22, 26, 27, 29, 34, 37, 40,
58 22, 26, 27, 29, 32, 35, 40, 48,
59 26, 27, 29, 32, 35, 40, 48, 58,
60 26, 27, 29, 34, 38, 46, 56, 69,
61 27, 29, 35, 38, 46, 56, 69, 83
66 const int luma_mapping[64] = {
67 0, 0, 1, 1, 2, 2, 3, 3,
68 0, 0, 1, 2, 2, 2, 3, 3,
69 1, 1, 2, 2, 2, 3, 3, 3,
70 1, 1, 2, 2, 2, 3, 3, 3,
71 1, 2, 2, 2, 2, 3, 3, 3,
72 2, 2, 2, 2, 3, 3, 3, 3,
73 2, 2, 3, 3, 3, 3, 3, 3,
74 3, 3, 3, 3, 3, 3, 3, 3,
76 const int chroma_mapping[64] = {
77 0, 1, 1, 2, 2, 2, 3, 3,
78 1, 1, 2, 2, 2, 3, 3, 3,
79 2, 2, 2, 2, 3, 3, 3, 3,
80 2, 2, 2, 3, 3, 3, 3, 3,
81 2, 3, 3, 3, 3, 3, 3, 3,
82 3, 3, 3, 3, 3, 3, 3, 3,
83 3, 3, 3, 3, 3, 3, 3, 3,
84 3, 3, 3, 3, 3, 3, 3, 3,
87 int pick_stats_for(int x, int y, bool is_chroma)
90 return chroma_mapping[y * 8 + x] + 4;
92 return luma_mapping[y * 8 + x];
96 uint32_t read_varint(FILE *fp)
103 fprintf(stderr, "Premature EOF\n");
107 x |= (ch & 0x7f) << shift;
108 if ((ch & 0x80) == 0) return x;
113 static constexpr int dc_scalefac = 8; // Matches the FDCT's gain.
114 static constexpr double quant_scalefac = 4.0; // whatever?
116 static inline int unquantize(int qf, int coeff_idx)
118 if (coeff_idx == 0) {
119 return qf * dc_scalefac;
125 const int w = std_luminance_quant_tbl[coeff_idx];
126 const int s = quant_scalefac;
127 return (2 * qf * w * s) / 32;
132 FILE *fp = fopen("coded.dat", "rb");
138 uint32_t sign_bias[NUM_TABLES];
139 for (unsigned table = 0; table < NUM_TABLES; ++table) {
140 uint32_t cum_freq = 0;
141 for (unsigned sym = 0; sym < NUM_SYMS; ++sym) {
142 uint32_t freq = read_varint(fp);
143 fprintf(stderr, "sym=%u/%u: freq=%u\n", sym, NUM_SYMS, freq);
144 RansDecSymbolInit(&decode_tables[table].dsyms[(sym + 1) & 255], cum_freq, freq);
145 for (uint32_t i = 0; i < freq; ++i) {
146 if (cum_freq < prob_scale)
147 decode_tables[table].cum2sym[cum_freq] = (sym + 1) & 255;
151 sign_bias[table] = cum_freq;
152 printf("sign_bias=%u (of %d)\n", sign_bias[table], prob_scale * 2);
155 // loop over all coefficients
156 for (unsigned y = 0; y < 8; ++y) {
157 for (unsigned x = 0; x < 8; ++x) {
158 unsigned tbl = pick_stats_for(x, y, false);
162 unique_ptr<uint8_t[]> rans_bytes;
163 uint8_t *rans_ptr = nullptr;
165 // loop over all DCT blocks
166 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
169 uint32_t num_rans_bytes = read_varint(fp);
170 rans_bytes.reset(new uint8_t[num_rans_bytes]);
171 fread(rans_bytes.get(), 1, num_rans_bytes, fp);
173 printf("%d,%d: read %d rANS bytes\n", x, y, num_rans_bytes);
174 //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]);
178 rans_ptr = rans_bytes.get();
179 RansDecInit(&rans, &rans_ptr);
181 for (unsigned xb = 0; xb < WIDTH; xb += 8) {
182 uint32_t bottom_bits = RansDecGet(&rans, prob_bits + 1);
184 if (bottom_bits >= sign_bias[tbl]) {
185 bottom_bits -= sign_bias[tbl];
186 rans -= sign_bias[tbl];
189 uint32_t k = decode_tables[tbl].cum2sym[std::min(bottom_bits, prob_scale - 1)];
190 RansDecAdvanceSymbol(&rans, &rans_ptr, &decode_tables[tbl].dsyms[k], prob_bits + 1);
191 assert(k <= ESCAPE_LIMIT);
192 if (k == ESCAPE_LIMIT) {
193 k = RansDecGet(&rans, prob_bits);
194 assert(k >= ESCAPE_LIMIT);
195 RansDecAdvance(&rans, &rans_ptr, k, 1, prob_bits);
203 int reversed_yb = yb ^ 8;
204 int reversed_xb = WIDTH - 8 - xb;
205 coeff[(reversed_yb + y) * WIDTH + (reversed_xb + x)] = k;
206 // printf("coeff %d xb,yb=%d,%d: decoded %d\n", y * 8 + x, reversed_xb, reversed_yb, k);
211 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
212 for (unsigned xb = 0; xb < WIDTH; xb += 8) {
218 // DC coefficient pred from the right to left (within each slice)
219 for (unsigned block_idx = 0; block_idx < NUM_BLOCKS; block_idx += BLOCKS_PER_STREAM) {
222 for (unsigned subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) {
223 unsigned yb = (block_idx + subblock_idx) / WIDTH_BLOCKS;
224 unsigned xb = (block_idx + subblock_idx) % WIDTH_BLOCKS;
225 int k = coeff[(yb * 8) * WIDTH + (xb * 8)];
228 coeff[(yb * 8) * WIDTH + (xb * 8)] = prev_k;
233 for (unsigned yb = 0; yb < HEIGHT; yb += 8) {
234 for (unsigned xb = 0; xb < WIDTH; xb += 8) {
237 for (unsigned y = 0; y < 8; ++y) {
238 for (unsigned x = 0; x < 8; ++x) {
239 int k = coeff[(yb + y) * WIDTH + (xb + x)];
240 in[y * 8 + x] = unquantize(k, y * 8 + x);
241 printf("%3d ", in[y * 8 + x]);
249 // Clamp and move back
250 for (unsigned y = 0; y < 8; ++y) {
251 for (unsigned x = 0; x < 8; ++x) {
252 int k = in[y * 8 + x];
255 if (k > 255) k = 255;
256 pix[(yb + y) * WIDTH + (xb + x)] = k;
264 fp = fopen("output.pgm", "wb");
265 fprintf(fp, "P5\n%d %d\n255\n", WIDTH, HEIGHT);
266 fwrite(pix, 1, WIDTH * HEIGHT, fp);