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