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