2 * qscale: Quick, high-quality JPEG-to-JPEG scaler.
3 * Copyright (C) 2008 Steinar H. Gunderson <sgunderson@bigfoot.com>
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.
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.
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
26 #define CACHE_LINE_FACTOR 16
29 #define M_PI 3.14159265358979323846264
34 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
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;
41 return 1.0 - x2 / 6.0 + x4 / 120.0;
47 double lanczos_tap(double x)
49 if (x < -3.0 || x > 3.0)
52 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
54 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
63 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
65 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
67 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
70 double sf = (double)w / (double)nw;
71 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
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);
87 /* round up so we get a multiple of four for the SSE code */
88 int num = (end - start + 1);
90 /* prefer aligning it if possible */
91 if (start % 4 != 0 && start % 4 <= num % 4) {
102 pd[x].startcoeff = num_coeffs;
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) {
109 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
112 coeffs[num_coeffs++] = f;
116 for (sx = start; sx <= end; ++sx) {
117 coeffs[pd[x].startcoeff + sx - start] /= sum;
121 for (y = 0; y < h; ++y) {
122 float *sptr = pix + y*sstride;
123 unsigned char *dptr = npix + y*dstride;
125 for (x = 0; x < nw; ++x) {
129 static const float low = 0.0, high = 255.0;
134 "movups (%4,%2),%%xmm1 \n"
135 "movups (%3,%2),%%xmm2 \n"
136 "mulps %%xmm2,%%xmm1 \n"
149 : "r" (&coeffs[pd[x].startcoeff]),
150 "r" (&sptr[pd[x].start]),
151 "r" ((pd[x].end - pd[x].start + 1)/4),
154 : "memory", "xmm1", "xmm2"
160 else if (acc > 255.0)
163 ch = (unsigned char)acc;
166 *dptr++ = (unsigned char)result;
169 for ( ; x < dstride; ++x) {
175 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
177 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
179 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
182 double sf = (double)h / (double)nh;
183 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
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);
200 pd[y].startcoeff = num_coeffs;
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) {
207 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
210 coeffs[num_coeffs++] = f;
214 for (sy = start; sy <= end; ++sy) {
215 coeffs[pd[y].startcoeff + sy - start] /= sum;
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) {
226 float acc[CACHE_LINE_FACTOR];
227 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
229 float *cf = &coeffs[pd[y].startcoeff];
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;
239 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
244 * xmm0 - xmm3: acc[0..15]
245 * xmm4: current filter coefficient
246 * xmm5, xmm6, xmm7: scratchpad
250 "pxor %%xmm0, %%xmm0 \n"
251 "pxor %%xmm1, %%xmm1 \n"
252 "pxor %%xmm2, %%xmm2 \n"
253 "pxor %%xmm3, %%xmm3 \n"
258 /* a zero is useful during unpacking */
259 "pxor %%xmm4, %%xmm4 \n"
261 /* fetch all 16 source bytes */
262 "movups (%0), %%xmm5 \n"
263 "prefetcht0 (%0,%3,4) \n"
265 /* unpack into words (xmm5, xmm7) */
266 "movaps %%xmm5, %%xmm7 \n"
267 "punpcklbw %%xmm4, %%xmm5 \n"
268 "punpckhbw %%xmm4, %%xmm7 \n"
270 /* unpack xmm5 into dwords (xmm5, xmm6) */
271 "movaps %%xmm5, %%xmm6 \n"
272 "punpcklwd %%xmm4, %%xmm5 \n"
273 "punpckhwd %%xmm4, %%xmm6 \n"
275 /* convert xmm5, xmm6 to floats */
276 "cvtdq2ps %%xmm5, %%xmm5 \n"
277 "cvtdq2ps %%xmm6, %%xmm6 \n"
279 /* fetch the coefficient */
280 "movss (%2), %%xmm4 \n"
281 "shufps $0x0, %%xmm4, %%xmm4 \n"
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"
289 /* get the zero back again */
290 "pxor %%xmm4, %%xmm4 \n"
292 /* unpack xmm7 into dwords (xmm7, xmm6) */
293 "movaps %%xmm7, %%xmm6 \n"
294 "punpcklwd %%xmm4, %%xmm7 \n"
295 "punpckhwd %%xmm4, %%xmm6 \n"
297 /* convert xmm7, xmm6 to floats */
298 "cvtdq2ps %%xmm7, %%xmm7 \n"
299 "cvtdq2ps %%xmm6, %%xmm6 \n"
301 /* fetch the coefficient */
302 "movss (%2), %%xmm4 \n"
303 "shufps $0x0, %%xmm4, %%xmm4 \n"
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"
311 /* move along, and loop */
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"
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"
334 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
336 for (x = 0; x < w; ++x) {
338 unsigned char *sptr = pix + x;
339 float *dptr = npix + x;
340 for (y = 0; y < nh; ++y) {
342 float *cf = &coeffs[pd[y].startcoeff];
345 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
346 acc += sptr[sy * w] * *cf++;
355 int main(int argc, char **argv)
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;
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;
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;
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;
383 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
384 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
385 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
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);
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;
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;
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;
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);
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;
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;
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 };
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;
431 total_lines += max_lines;
434 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
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);
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);
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);
459 jpeg_destroy_decompress(&dinfo);
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);
482 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
483 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
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 };
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;
497 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
498 if (cbline > nh1 - 1)
501 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
502 if (crline > nh2 - 1)
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;
510 total_lines += max_lines;
513 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
515 jpeg_finish_compress(&cinfo);
516 jpeg_destroy_compress(&cinfo);