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