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