Add a stupid integerization of the AA&N IDCT -- 30% faster or so, mostly
[fjl] / driver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "bytesource.h"
5 #include "choice.h"
6 #include "dehuff.h"
7 #include "input.h"
8
9 struct jpeg_image {
10         unsigned precision;
11         unsigned width, height;
12         unsigned num_components;
13         unsigned hsample[256], vsample[256], qtable[256];
14 };
15
16 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count) 
17 {
18         return fread(buf, 1, count, (FILE*)userdata);
19 }
20
21 void read_sof(struct byte_source* source, struct jpeg_image* image)
22 {
23         unsigned len = read_uint16(byte_source_input_func, source);
24         assert(len >= 8);
25         image->precision = read_uint8(byte_source_input_func, source);
26         assert(image->precision == 8);
27         image->width = read_uint16(byte_source_input_func, source);
28         image->height = read_uint16(byte_source_input_func, source);
29         image->num_components = read_uint8(byte_source_input_func, source);
30         len -= 8;
31
32         fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
33                 image->precision, image->width, image->height, image->num_components);
34
35         for (unsigned i = 0; i < image->num_components; ++i) {
36                 assert(len >= 3);
37                 unsigned c = read_uint8(byte_source_input_func, source);
38                 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
39                 image->hsample[c] = sampling_factors >> 4;
40                 image->vsample[c] = sampling_factors & 0x0f;
41                 image->qtable[c] = read_uint8(byte_source_input_func, source);
42                 len -= 3;
43
44                 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
45                         c, image->hsample[c], image->vsample[c], image->qtable[c]);
46         }
47 }
48
49 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
50 {
51         unsigned len = read_uint16(byte_source_input_func, source);
52         assert(len >= 2);
53         len -= 2;
54
55         assert(len >= 1);
56         unsigned num_components = read_uint8(byte_source_input_func, source);
57         --len;
58
59         unsigned component_num[256];
60         unsigned dc_huffman_table[256], ac_huffman_table[256];
61         unsigned ss, se, ah_al;
62         int last_dc[256];
63
64         for (unsigned i = 0; i < num_components; ++i) {
65                 unsigned char td_ta;
66                 assert(len >= 2);
67                 component_num[i] = read_uint8(byte_source_input_func, source);
68                 td_ta = read_uint8(byte_source_input_func, source);
69                 len -= 2;
70                 dc_huffman_table[i] = td_ta >> 4;
71                 ac_huffman_table[i] = td_ta & 0x0f;
72                 last_dc[i] = 0;
73         }
74
75         assert(len >= 3);
76         ss = read_uint8(byte_source_input_func, source);
77         se = read_uint8(byte_source_input_func, source);
78         ah_al = read_uint8(byte_source_input_func, source);
79         len -= 3;
80
81         if (len != 0) {
82                 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
83         }
84
85         struct bit_source bits;
86         init_bit_source(&bits, byte_source_input_func, source);
87
88         for ( ;; ) {
89                 for (unsigned c = 0; c < num_components; ++c) {
90                         unsigned cn = component_num[c];
91                         unsigned nc = image->vsample[cn] * image->hsample[cn];
92                         for (unsigned n = 0; n < nc; ++n) {
93                                 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
94                                 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
95
96                                 // decode DC component
97                                 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
98                                 possibly_refill(&bits, dc_category);
99                                 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
100
101                 //              printf("dc=%d ac=", last_dc[c]);
102                                 putchar(last_dc[c]);
103
104                                 // decode AC components
105                                 int zz[63] = { 0 };
106                                 for (unsigned i = 0; i < 63; ++i) {
107                                         unsigned rs = read_huffman_symbol(ac_table, &bits);
108                                         unsigned r = rs >> 4;
109                                         unsigned s = rs & 0xf;
110
111                                         if (rs == 0x00) {
112                                                 /* end of block */
113                                                 break;
114                                         }
115                                         if (rs == 0xf0) {
116                                                 /* 16 zero coefficients */
117                                                 i += 15;
118                                                 continue;
119                                         }
120
121                                         possibly_refill(&bits, s);
122
123                                         i += r;
124                                         zz[i] = extend(read_bits(&bits, s), s);
125                                 }
126                                 
127                                 for (unsigned i = 0; i < 63; ++i) {
128                                         putchar(zz[i]);
129                                         //printf("%d ", zz[i]);
130                                 }
131                                 //printf("\n");
132                         }
133                 }
134         }
135         if (len != 0) {
136                 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
137         }
138 }
139                         
140 void skip_segment(struct byte_source* source)
141 {
142         uint8_t buf[4096];
143         for ( ;; ) {
144                 ssize_t ret = byte_source_input_func(source, buf, 4096);
145                 if (ret == -1) {
146                         fprintf(stderr, "Input error!\n");
147                         exit(1);
148                 }
149                 if (ret == 0) {
150                         return;
151                 }
152         }
153 }
154         
155 int main(void)
156 {
157         struct jpeg_image jpeg;
158         init_choices();
159
160         struct byte_source source;
161         init_byte_source(&source, stdio_read, stdin);
162
163         huffman_tables_t tables;
164
165         for ( ;; ) {
166                 uint8_t m2 = byte_source_read_marker(&source);
167                 assert(m2 != 0x00);
168
169                 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
170
171                 switch (m2) {
172                 case 0xe0:
173                 case 0xe1:
174                 case 0xe2:
175                 case 0xe3:
176                 case 0xe4:
177                 case 0xe5:
178                 case 0xe6:
179                 case 0xe7:
180                 case 0xe8:
181                 case 0xe9:
182                 case 0xea:
183                 case 0xeb:
184                 case 0xec:
185                 case 0xed:
186                 case 0xee:
187                 case 0xef:
188                         /* APP0 through APPF */
189                 case 0xfc:
190                         /* some EXIF stuff */
191                 case 0xfe:
192                         /* comment */
193                 case 0xff:
194                         /* ignore */
195                 case 0xdb:
196                         /* DQT */
197                         skip_segment(&source);
198                         break;
199                 case 0xc0:
200                         /* SOF0 (baseline DCT, Huffman encoded) */
201                         read_sof(&source, &jpeg);
202                         break;
203                 case 0xd8:
204                         /* SOI */
205                         break;
206                 case 0xc4:
207                         /* DHT (define Huffman tables) */
208                         read_huffman_tables(&tables, byte_source_input_func, &source);
209                         break;
210                 case 0xda:
211                         /* SOS (start of scan) */
212                         read_scan(&source, &jpeg, &tables);
213                         break;
214                 default:
215                         fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);
216                         exit(1);
217                 }
218         }
219 }