]> git.sesse.net Git - qscale/blob - qscale.c
Implement first round of SSE.
[qscale] / qscale.c
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <math.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include "jpeglib.h"
7
8 #define CACHE_LINE_FACTOR 16
9
10 double sinc(double x)
11 {
12         // This is bad for very small x, should use power series instead.
13         if (x == 0.0)
14                 return 1.0;
15         else
16                 return sin(x) / x;
17 }
18
19 double lanczos_tap(double x)
20 {
21         if (x < -3.0 || x > 3.0)
22                 return 0.0;
23         if (x < 0.0)
24                 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
25         else
26                 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
27 }
28
29
30 struct pix_desc {
31         unsigned start, end;
32         unsigned startcoeff;
33 };
34
35 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned dstride)
36 {
37         struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
38         int size_coeffs = 8;
39         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
40         int num_coeffs = 0;
41         int x, y, sx;
42         double sf = (double)w / (double)nw;
43         double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
44
45         /* calculate the filter */
46         for (x = 0; x < nw; ++x) {
47                 int start = ceil(x * sf - support);
48                 int end = floor(x * sf + support);
49                 double sum = 0.0;
50
51                 if (start < 0) {
52                         start = 0;
53                 }
54                 if (end > w - 1) {
55                         end = w - 1;
56                 }
57
58                 pd[x].start = start;
59                 pd[x].end = end;
60                 pd[x].startcoeff = num_coeffs;
61
62                 for (sx = start; sx <= end; ++sx) {
63                         double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
64                         double f = lanczos_tap(nd);
65                         if (num_coeffs == size_coeffs) {
66                                 size_coeffs <<= 1;
67                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
68                         }
69                 
70                         coeffs[num_coeffs++] = f;
71                         sum += f;
72                 }
73
74                 for (sx = start; sx <= end; ++sx) {
75                         coeffs[pd[x].startcoeff + sx - start] /= sum;
76                 }
77         }
78         
79         for (y = 0; y < h; ++y) {
80                 float *sptr = pix + y*w;
81                 unsigned char *dptr = npix + y*dstride;
82                 unsigned char ch = 0;
83                 for (x = 0; x < nw; ++x) {
84                         float acc = 0.0;
85                         float *cf = &coeffs[pd[x].startcoeff];
86                         unsigned sx;
87                         
88                         for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
89                                 acc += sptr[sx] * *cf++;
90                         }
91
92                         if (acc < 0.0)
93                                 ch = 0;
94                         else if (acc > 255.0)
95                                 ch = 255;
96                         else
97                                 ch = (unsigned char)acc;
98                         *dptr++ = ch;
99                 }
100                 for ( ; x < dstride; ++x) {
101                         *dptr++ = ch;
102                 }
103         }
104 }
105
106 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
107 {
108         struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
109         int size_coeffs = 8;
110         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
111         int num_coeffs = 0;
112         int x, y, sy;
113         double sf = (double)h / (double)nh;
114         double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
115
116         /* calculate the filter */
117         for (y = 0; y < nh; ++y) {
118                 int start = ceil(y * sf - support);
119                 int end = floor(y * sf + support);
120                 double sum = 0.0;
121
122                 if (start < 0) {
123                         start = 0;
124                 }
125                 if (end > h - 1) {
126                         end = h - 1;
127                 }
128
129                 pd[y].start = start;
130                 pd[y].end = end;
131                 pd[y].startcoeff = num_coeffs;
132
133                 for (sy = start; sy <= end; ++sy) {
134                         double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
135                         double f = lanczos_tap(nd);
136                         if (num_coeffs == size_coeffs) {
137                                 size_coeffs <<= 1;
138                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
139                         }
140                         
141                         coeffs[num_coeffs++] = f;
142                         sum += f;
143                 }
144
145                 for (sy = start; sy <= end; ++sy) {
146                         coeffs[pd[y].startcoeff + sy - start] /= sum;
147                 }
148         }
149
150 #if CACHE_LINE_FACTOR > 1
151         for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
152                 unsigned char *sptr = pix + x;
153                 float *dptr = npix + x;
154                 for (y = 0; y < nh; ++y) {
155 #if 0
156                         int i;
157                         float acc[CACHE_LINE_FACTOR];
158                         for (i = 0; i < CACHE_LINE_FACTOR; ++i)
159                                 acc[i] = 0.0;
160                         float *cf = &coeffs[pd[y].startcoeff];
161                         unsigned sy;
162                 
163                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
164                                 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
165                                         acc[i] += sptr[sy * w + i] * *cf;
166                                 }
167                                 ++cf;
168                         }
169
170                         for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
171                                 dptr[i] = acc[i];
172                         }
173 #else
174                         /*
175                          * xmm0 - xmm3: acc[0..15]
176                          * xmm4: current filter coefficient
177                          * xmm5, xmm6, xmm7: scratchpad
178                          */
179                         asm (
180                                 /* clear */
181                                 "pxor %%xmm0, %%xmm0    \n"
182                                 "pxor %%xmm1, %%xmm1    \n"
183                                 "pxor %%xmm2, %%xmm2    \n"
184                                 "pxor %%xmm3, %%xmm3    \n"
185
186                                 /* main loop */
187                                 ".lbl:                   \n"
188                                 
189                                 /* a zero is useful during unpacking */
190                                 "pxor %%xmm4, %%xmm4     \n"
191                                 
192                                 /* fetch all 16 source bytes */
193                                 "movups (%0), %%xmm5     \n"
194                                 "prefetcht0 (%0,%3,4)    \n"
195
196                                 /* unpack into words (xmm5, xmm7) */
197                                 "movaps %%xmm5, %%xmm7    \n"
198                                 "punpcklbw %%xmm4, %%xmm5 \n"
199                                 "punpckhbw %%xmm4, %%xmm7 \n"
200
201                                 /* unpack xmm5 into dwords (xmm5, xmm6) */
202                                 "movaps %%xmm5, %%xmm6    \n"
203                                 "punpcklwd %%xmm4, %%xmm5 \n"
204                                 "punpckhwd %%xmm4, %%xmm6 \n"
205
206                                 /* convert xmm5, xmm6 to floats */
207                                 "cvtdq2ps %%xmm5, %%xmm5 \n"
208                                 "cvtdq2ps %%xmm6, %%xmm6 \n"
209
210                                 /* fetch the coefficient */
211                                 "movss (%2), %%xmm4      \n"
212                                 "shufps $0x0, %%xmm4, %%xmm4 \n"
213
214                                 /* do the muls for xmm5 and xmm6 */
215                                 "mulps %%xmm4, %%xmm5    \n"
216                                 "mulps %%xmm4, %%xmm6    \n"
217                                 "addps %%xmm5, %%xmm0    \n"
218                                 "addps %%xmm6, %%xmm1    \n"
219
220                                 /* get the zero back again */
221                                 "pxor %%xmm4, %%xmm4     \n"
222
223                                 /* unpack xmm7 into dwords (xmm7, xmm6) */
224                                 "movaps %%xmm7, %%xmm6    \n"
225                                 "punpcklwd %%xmm4, %%xmm7 \n"
226                                 "punpckhwd %%xmm4, %%xmm6 \n"
227
228                                 /* convert xmm7, xmm6 to floats */
229                                 "cvtdq2ps %%xmm7, %%xmm7 \n"
230                                 "cvtdq2ps %%xmm6, %%xmm6 \n"
231
232                                 /* fetch the coefficient */
233                                 "movss (%2), %%xmm4      \n"
234                                 "shufps $0x0, %%xmm4, %%xmm4 \n"
235
236                                 /* do the second set of muls */
237                                 "mulps %%xmm4, %%xmm7    \n"
238                                 "mulps %%xmm4, %%xmm6    \n"
239                                 "addps %%xmm7, %%xmm2    \n"
240                                 "addps %%xmm6, %%xmm3    \n"
241
242                                 /* move along, and loop */
243                                 "addl $4, %2             \n"
244                                 "addl %3, %0             \n"
245                                 "decl %1                 \n"
246                                 "jnz .lbl                \n"
247
248                                 /* store the values */
249                                 "movaps %%xmm0, (%4)     \n"
250                                 "movaps %%xmm1, 16(%4)   \n"
251                                 "movaps %%xmm2, 32(%4)   \n"
252                                 "movaps %%xmm3, 48(%4)   \n"
253                                 : :
254                                 "r" (&sptr[pd[y].start * w]),        /* 0: srcptr base */
255                                 "r" (pd[y].end - pd[y].start + 1),   /* 1: filter len */
256                                 "r" (&coeffs[pd[y].startcoeff]),     /* 2: coeffs base */
257                                 "r" (w),                             /* 3: stride */
258                                 "r" (dptr)                           /* 4: dstptr base */
259                                 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
260                         );
261 #endif
262                         dptr += dstride;
263                 }
264         }
265         for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
266 #else
267         for (x = 0; x < w; ++x) {
268 #endif
269                 unsigned char *sptr = pix + x;
270                 float *dptr = npix + x;
271                 for (y = 0; y < nh; ++y) {
272                         float acc = 0.0;
273                         float *cf = &coeffs[pd[y].startcoeff];
274                         unsigned sy;
275                         
276                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
277                                 acc += sptr[sy * w] * *cf++;
278                         }
279
280                         *dptr = acc;
281                         dptr += dstride;
282                 }
283         }
284 }
285
286 int main(int argc, char **argv)
287 {
288         unsigned nominal_w = atoi(argv[1]);
289         unsigned nominal_h = atoi(argv[2]);
290
291         unsigned samp_h0 = 2, samp_v0 = 2;
292         unsigned samp_h1 = 1, samp_v1 = 1;
293         unsigned samp_h2 = 1, samp_v2 = 1;
294         unsigned max_samp_h = 2, max_samp_v = 2;
295
296         unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
297         unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
298         unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
299
300         unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
301         unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
302         unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
303
304         struct jpeg_decompress_struct dinfo;
305         struct jpeg_error_mgr jerr;
306         dinfo.err = jpeg_std_error(&jerr);
307         jpeg_create_decompress(&dinfo);
308         jpeg_stdio_src(&dinfo, stdin);
309         jpeg_read_header(&dinfo, TRUE);
310         dinfo.raw_data_out = TRUE;
311         jpeg_start_decompress(&dinfo);
312
313         fprintf(stderr, "Scaling using Lanczos filter:\n");
314         fprintf(stderr, "  Y component: %ux%u -> %ux%u\n", dinfo.comp_info[0].width_in_blocks * DCTSIZE, dinfo.comp_info[0].height_in_blocks * DCTSIZE, nw0, nh0);
315         fprintf(stderr, "  Cb component: %ux%u -> %ux%u\n", dinfo.comp_info[1].width_in_blocks * DCTSIZE, dinfo.comp_info[1].height_in_blocks * DCTSIZE, nw1, nh1);
316         fprintf(stderr, "  Cr component: %ux%u -> %ux%u\n", dinfo.comp_info[2].width_in_blocks * DCTSIZE, dinfo.comp_info[2].height_in_blocks * DCTSIZE, nw2, nh2);
317
318         JSAMPLE *data_y  = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
319         JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
320         JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
321         JSAMPLE *data_ny, *data_ncb, *data_ncr;
322
323         int total_lines = 0, blocks = 0;
324         while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
325                 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
326
327                 JSAMPROW y_row_ptrs[max_lines];
328                 JSAMPROW cb_row_ptrs[max_lines];
329                 JSAMPROW cr_row_ptrs[max_lines];
330                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
331                 int i;
332
333                 for (i = 0; i < max_lines; ++i) {
334                         y_row_ptrs[i]  = data_y  + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
335                         cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
336                         cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
337                 }
338                 
339                 total_lines += max_lines;
340                 ++blocks;
341
342                 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
343                         break;
344         }
345
346         {
347                 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float)); 
348                 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, dinfo.comp_info[0].height_in_blocks * DCTSIZE, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
349                 data_ny = (unsigned char *)malloc(nw0 * stride0);
350                 hscale(npix, data_ny, dinfo.comp_info[0].width_in_blocks * DCTSIZE, nh0, nw0, stride0);
351                 free(npix);
352         }
353         {
354                 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float)); 
355                 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, dinfo.comp_info[1].height_in_blocks * DCTSIZE, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
356                 data_ncr = (unsigned char *)malloc(nw1 * stride1);
357                 hscale(npix, data_ncr, dinfo.comp_info[1].width_in_blocks * DCTSIZE, nh1, nw1, stride1);
358                 free(npix);
359         }
360         {
361                 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float)); 
362                 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, dinfo.comp_info[2].height_in_blocks * DCTSIZE, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
363                 data_ncb = (unsigned char *)malloc(nw2 * stride2);
364                 hscale(npix, data_ncb, dinfo.comp_info[2].width_in_blocks * DCTSIZE, nh2, nw2, stride2);
365                 free(npix);
366         }
367         jpeg_destroy_decompress(&dinfo);
368         
369         struct jpeg_compress_struct cinfo;
370         cinfo.err = jpeg_std_error(&jerr);
371         jpeg_create_compress(&cinfo);
372         jpeg_stdio_dest(&cinfo, stdout);
373         cinfo.input_components = 3;
374         jpeg_set_defaults(&cinfo);
375         jpeg_set_quality(&cinfo, 85, FALSE);
376         cinfo.image_width = nominal_w;
377         cinfo.image_height = nominal_h;
378         cinfo.raw_data_in = TRUE;
379         jpeg_set_colorspace(&cinfo, JCS_YCbCr);
380         cinfo.comp_info[0].h_samp_factor = samp_h0;
381         cinfo.comp_info[0].v_samp_factor = samp_v0;
382         cinfo.comp_info[1].h_samp_factor = samp_h1;
383         cinfo.comp_info[1].v_samp_factor = samp_v1;
384         cinfo.comp_info[2].h_samp_factor = samp_h2;
385         cinfo.comp_info[2].v_samp_factor = samp_v2;
386         jpeg_start_compress(&cinfo, TRUE);
387
388         total_lines = 0;
389         blocks = 0;
390         while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
391                 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
392
393                 JSAMPROW y_row_ptrs[max_lines];
394                 JSAMPROW cb_row_ptrs[max_lines];
395                 JSAMPROW cr_row_ptrs[max_lines];
396                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
397                 int i;
398
399                 for (i = 0; i < max_lines; ++i) {
400                         // simple edge extension
401                         int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
402                         if (yline > nh0 - 1)
403                                 yline = nh0 - 1;
404
405                         int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
406                         if (cbline > nh1 - 1)
407                                 cbline = nh1 - 1;
408
409                         int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
410                         if (crline > nh2 - 1)
411                                 crline = nh2 - 1;
412
413                         y_row_ptrs[i]  = data_ny  + yline * stride0;
414                         cb_row_ptrs[i] = data_ncb + cbline * stride1;
415                         cr_row_ptrs[i] = data_ncr + crline * stride2;
416                 }
417                 
418                 total_lines += max_lines;
419                 ++blocks;
420
421                 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
422         }
423         jpeg_finish_compress(&cinfo);
424         jpeg_destroy_compress(&cinfo);
425
426         return 0;
427 }
428