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