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