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