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
24 #include "libqscale.h"
26 /* The number of pixels to process at a time when scaling vertically. */
27 #define CACHE_LINE_FACTOR 16
29 /* Whether to use SSE for horizontal scaling or not (requires SSE3). */
30 #define USE_HORIZONTAL_SSE 1
32 /* Whether to use SSE for vertical scaling or not (requires only SSE1). */
33 #define USE_VERTICAL_SSE 1
36 #undef CACHE_LINE_FACTOR
37 #define CACHE_LINE_FACTOR 16
41 #define M_PI 3.14159265358979323846264
44 qscale_img *qscale_load_jpeg(const char *filename)
46 FILE *file = fopen(filename, "rb");
52 img = qscale_load_jpeg_from_stdio(file);
58 qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
60 qscale_img *img = (qscale_img *)malloc(sizeof(qscale_img));
65 img->data_y = img->data_cb = img->data_cr = NULL;
67 /* FIXME: Better error handling here (ie., return NULL). */
68 struct jpeg_decompress_struct dinfo;
69 struct jpeg_error_mgr jerr;
70 dinfo.err = jpeg_std_error(&jerr);
71 jpeg_create_decompress(&dinfo);
72 jpeg_stdio_src(&dinfo, file);
73 jpeg_read_header(&dinfo, TRUE);
74 dinfo.raw_data_out = TRUE;
75 jpeg_start_decompress(&dinfo);
77 /* We do not handle anything but YCbCr images (yet?). */
78 if (dinfo.num_components != 3) {
83 img->width = dinfo.image_width;
84 img->height = dinfo.image_height;
86 img->w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
87 img->h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
89 img->w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
90 img->h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
92 img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
93 img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
95 img->samp_h0 = dinfo.comp_info[0].h_samp_factor;
96 img->samp_v0 = dinfo.comp_info[0].v_samp_factor;
98 img->samp_h1 = dinfo.comp_info[1].h_samp_factor;
99 img->samp_v1 = dinfo.comp_info[1].v_samp_factor;
101 img->samp_h2 = dinfo.comp_info[2].h_samp_factor;
102 img->samp_v2 = dinfo.comp_info[2].v_samp_factor;
104 img->data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
105 img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
106 img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
108 if (img->data_y == NULL || img->data_cb == NULL || img->data_cr == NULL) {
113 int total_lines = 0, blocks = 0;
114 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
115 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
117 JSAMPROW y_row_ptrs[max_lines];
118 JSAMPROW cb_row_ptrs[max_lines];
119 JSAMPROW cr_row_ptrs[max_lines];
120 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
123 for (i = 0; i < max_lines; ++i) {
124 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;
125 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;
126 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;
129 total_lines += max_lines;
132 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
136 jpeg_destroy_decompress(&dinfo);
140 void qscale_destroy(qscale_img *img)
149 static double sinc(double x)
151 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
153 if (abs(x) < cutoff) {
154 /* For small |x|, use Taylor series instead */
155 const double x2 = x * x;
156 const double x4 = x2 * x2;
158 return 1.0 - x2 / 6.0 + x4 / 120.0;
164 static double lanczos_tap(double x)
166 if (x < -3.0 || x > 3.0)
169 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
171 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
179 static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
181 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
183 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
186 double sf = (double)w / (double)nw;
187 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
189 /* calculate the filter */
190 for (x = 0; x < nw; ++x) {
191 int start = ceil(x * sf - support);
192 int end = floor(x * sf + support);
203 #if USE_HORIZONTAL_SSE
204 /* round up so we get a multiple of four for the SSE code */
205 int num = (end - start + 1);
207 /* prefer aligning it if possible */
208 if (start % 4 != 0 && start % 4 <= num % 4) {
213 end += 4 - (num % 4);
220 pd[x].startcoeff = num_coeffs;
222 for (sx = start; sx <= end; ++sx) {
223 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
224 double f = lanczos_tap(nd);
225 if (num_coeffs == size_coeffs) {
227 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
230 coeffs[num_coeffs++] = f;
234 for (sx = start; sx <= end; ++sx) {
235 coeffs[pd[x].startcoeff + sx - start] /= sum;
239 for (y = 0; y < h; ++y) {
240 float *sptr = pix + y*sstride;
241 unsigned char *dptr = npix + y*dstride;
243 for (x = 0; x < nw; ++x) {
244 #if USE_HORIZONTAL_SSE
248 static const float low = 0.0, high = 255.0;
253 "movups (%4,%2),%%xmm1 \n"
254 "movups (%3,%2),%%xmm2 \n"
255 "mulps %%xmm2,%%xmm1 \n"
268 : "r" (&coeffs[pd[x].startcoeff]),
269 "r" (&sptr[pd[x].start]),
270 "r" ((pd[x].end - pd[x].start + 1)/4),
273 : "memory", "xmm1", "xmm2"
276 *dptr++ = (unsigned char)result;
279 float *cf = &coeffs[pd[x].startcoeff];
282 for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
283 acc += sptr[sx] * *cf++;
288 else if (acc > 255.0)
291 ch = (unsigned char)acc;
296 for ( ; x < dstride; ++x) {
302 static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
304 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
306 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
309 double sf = (double)h / (double)nh;
310 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
312 /* calculate the filter */
313 for (y = 0; y < nh; ++y) {
314 int start = ceil(y * sf - support);
315 int end = floor(y * sf + support);
327 pd[y].startcoeff = num_coeffs;
329 for (sy = start; sy <= end; ++sy) {
330 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
331 double f = lanczos_tap(nd);
332 if (num_coeffs == size_coeffs) {
334 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
337 coeffs[num_coeffs++] = f;
341 for (sy = start; sy <= end; ++sy) {
342 coeffs[pd[y].startcoeff + sy - start] /= sum;
346 #if CACHE_LINE_FACTOR > 1
347 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
348 unsigned char *sptr = pix + x;
349 float *dptr = npix + x;
350 for (y = 0; y < nh; ++y) {
353 * xmm0 - xmm3: acc[0..15]
354 * xmm4: current filter coefficient
355 * xmm5, xmm6, xmm7: scratchpad
359 "pxor %%xmm0, %%xmm0 \n"
360 "pxor %%xmm1, %%xmm1 \n"
361 "pxor %%xmm2, %%xmm2 \n"
362 "pxor %%xmm3, %%xmm3 \n"
367 /* a zero is useful during unpacking */
368 "pxor %%xmm4, %%xmm4 \n"
370 /* fetch all 16 source bytes */
371 "movups (%0), %%xmm5 \n"
372 "prefetcht0 (%0,%3,4) \n"
374 /* unpack into words (xmm5, xmm7) */
375 "movaps %%xmm5, %%xmm7 \n"
376 "punpcklbw %%xmm4, %%xmm5 \n"
377 "punpckhbw %%xmm4, %%xmm7 \n"
379 /* unpack xmm5 into dwords (xmm5, xmm6) */
380 "movaps %%xmm5, %%xmm6 \n"
381 "punpcklwd %%xmm4, %%xmm5 \n"
382 "punpckhwd %%xmm4, %%xmm6 \n"
384 /* convert xmm5, xmm6 to floats */
385 "cvtdq2ps %%xmm5, %%xmm5 \n"
386 "cvtdq2ps %%xmm6, %%xmm6 \n"
388 /* fetch the coefficient */
389 "movss (%2), %%xmm4 \n"
390 "shufps $0x0, %%xmm4, %%xmm4 \n"
392 /* do the muls for xmm5 and xmm6 */
393 "mulps %%xmm4, %%xmm5 \n"
394 "mulps %%xmm4, %%xmm6 \n"
395 "addps %%xmm5, %%xmm0 \n"
396 "addps %%xmm6, %%xmm1 \n"
398 /* get the zero back again */
399 "pxor %%xmm4, %%xmm4 \n"
401 /* unpack xmm7 into dwords (xmm7, xmm6) */
402 "movaps %%xmm7, %%xmm6 \n"
403 "punpcklwd %%xmm4, %%xmm7 \n"
404 "punpckhwd %%xmm4, %%xmm6 \n"
406 /* convert xmm7, xmm6 to floats */
407 "cvtdq2ps %%xmm7, %%xmm7 \n"
408 "cvtdq2ps %%xmm6, %%xmm6 \n"
410 /* fetch the coefficient */
411 "movss (%2), %%xmm4 \n"
412 "shufps $0x0, %%xmm4, %%xmm4 \n"
414 /* do the second set of muls */
415 "mulps %%xmm4, %%xmm7 \n"
416 "mulps %%xmm4, %%xmm6 \n"
417 "addps %%xmm7, %%xmm2 \n"
418 "addps %%xmm6, %%xmm3 \n"
420 /* move along, and loop */
426 /* store the values */
427 "movaps %%xmm0, (%4) \n"
428 "movaps %%xmm1, 16(%4) \n"
429 "movaps %%xmm2, 32(%4) \n"
430 "movaps %%xmm3, 48(%4) \n"
432 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
433 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
434 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
435 "r" ((long)w), /* 3: stride */
436 "r" (dptr) /* 4: dstptr base */
437 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
441 float acc[CACHE_LINE_FACTOR];
442 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
444 float *cf = &coeffs[pd[y].startcoeff];
447 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
448 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
449 acc[i] += sptr[sy * w + i] * *cf;
454 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
461 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
463 for (x = 0; x < w; ++x) {
465 unsigned char *sptr = pix + x;
466 float *dptr = npix + x;
467 for (y = 0; y < nh; ++y) {
469 float *cf = &coeffs[pd[y].startcoeff];
472 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
473 acc += sptr[sy * w] * *cf++;
482 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)
484 qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
490 dst->height = height;
492 unsigned max_samp_h, max_samp_v;
493 max_samp_h = samp_h0;
494 if (samp_h1 > max_samp_h)
495 max_samp_h = samp_h1;
496 if (samp_h2 > max_samp_h)
497 max_samp_h = samp_h2;
499 max_samp_v = samp_v0;
500 if (samp_v1 > max_samp_v)
501 max_samp_v = samp_v1;
502 if (samp_v2 > max_samp_v)
503 max_samp_v = samp_v2;
505 dst->w0 = width * samp_h0 / max_samp_h;
506 dst->h0 = height * samp_v0 / max_samp_v;
508 dst->w1 = width * samp_h1 / max_samp_h;
509 dst->h1 = height * samp_v1 / max_samp_v;
511 dst->w2 = width * samp_h2 / max_samp_h;
512 dst->h2 = height * samp_v2 / max_samp_v;
514 dst->samp_h0 = samp_h0;
515 dst->samp_v0 = samp_v0;
517 dst->samp_h1 = samp_h1;
518 dst->samp_v1 = samp_v1;
520 dst->samp_h2 = samp_h2;
521 dst->samp_v2 = samp_v2;
523 unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
524 unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
525 unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
527 unsigned sstride0 = (src->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
528 unsigned sstride1 = (src->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
529 unsigned sstride2 = (src->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
531 /* FIXME: handle out-of-memory gracefully */
533 float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
534 vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0);
535 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
536 hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0);
540 float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
541 vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1);
542 dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
543 hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1);
547 float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
548 vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2);
549 dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
550 hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2);
557 int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
559 FILE *file = fopen(filename, "wb");
564 int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
570 int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
572 struct jpeg_compress_struct cinfo;
573 struct jpeg_error_mgr jerr;
574 cinfo.err = jpeg_std_error(&jerr);
575 jpeg_create_compress(&cinfo);
576 jpeg_stdio_dest(&cinfo, file);
577 cinfo.input_components = 3;
578 jpeg_set_defaults(&cinfo);
579 jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
580 cinfo.image_width = img->width;
581 cinfo.image_height = img->height;
582 cinfo.raw_data_in = TRUE;
583 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
584 cinfo.comp_info[0].h_samp_factor = img->samp_h0;
585 cinfo.comp_info[0].v_samp_factor = img->samp_v0;
586 cinfo.comp_info[1].h_samp_factor = img->samp_h1;
587 cinfo.comp_info[1].v_samp_factor = img->samp_v1;
588 cinfo.comp_info[2].h_samp_factor = img->samp_h2;
589 cinfo.comp_info[2].v_samp_factor = img->samp_v2;
590 jpeg_start_compress(&cinfo, TRUE);
592 unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
593 unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
594 unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
598 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
599 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
601 JSAMPROW y_row_ptrs[max_lines];
602 JSAMPROW cb_row_ptrs[max_lines];
603 JSAMPROW cr_row_ptrs[max_lines];
604 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
607 for (i = 0; i < max_lines; ++i) {
608 /* simple edge extension */
609 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
610 if (yline > img->h0 - 1)
613 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
614 if (cbline > img->h1 - 1)
615 cbline = img->h1 - 1;
617 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
618 if (crline > img->h2 - 1)
619 crline = img->h2 - 1;
621 y_row_ptrs[i] = img->data_y + yline * dstride0;
622 cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
623 cr_row_ptrs[i] = img->data_cr + crline * dstride2;
626 total_lines += max_lines;
629 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
631 jpeg_finish_compress(&cinfo);
632 jpeg_destroy_compress(&cinfo);