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