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
25 #include "libqscale.h"
27 /* The number of pixels to process at a time when scaling vertically. */
28 #define CACHE_LINE_FACTOR 16
30 /* Whether to use SSE for horizontal scaling or not (requires SSE3). */
31 #define USE_HORIZONTAL_SSE 1
33 /* Whether to use SSE for vertical scaling or not (requires only SSE1). */
34 #define USE_VERTICAL_SSE 1
37 #undef CACHE_LINE_FACTOR
38 #define CACHE_LINE_FACTOR 16
42 #define M_PI 3.14159265358979323846264
45 qscale_img *qscale_load_jpeg(const char *filename)
47 FILE *file = fopen(filename, "rb");
53 img = qscale_load_jpeg_from_stdio(file);
59 qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
61 qscale_img *img = (qscale_img *)malloc(sizeof(qscale_img));
66 img->data_y = img->data_cb = img->data_cr = NULL;
68 /* FIXME: Better error handling here (ie., return NULL). */
69 struct jpeg_decompress_struct dinfo;
70 struct jpeg_error_mgr jerr;
71 dinfo.err = jpeg_std_error(&jerr);
72 jpeg_create_decompress(&dinfo);
73 jpeg_stdio_src(&dinfo, file);
74 jpeg_read_header(&dinfo, TRUE);
75 dinfo.raw_data_out = TRUE;
76 jpeg_start_decompress(&dinfo);
78 /* We do not handle anything but YCbCr images (yet?). */
79 if (dinfo.num_components != 3) {
84 img->width = dinfo.image_width;
85 img->height = dinfo.image_height;
87 img->w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
88 img->h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
90 img->w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
91 img->h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
93 img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
94 img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
96 img->samp_h0 = dinfo.comp_info[0].h_samp_factor;
97 img->samp_v0 = dinfo.comp_info[0].v_samp_factor;
99 img->samp_h1 = dinfo.comp_info[1].h_samp_factor;
100 img->samp_v1 = dinfo.comp_info[1].v_samp_factor;
102 img->samp_h2 = dinfo.comp_info[2].h_samp_factor;
103 img->samp_v2 = dinfo.comp_info[2].v_samp_factor;
105 img->data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
106 img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
107 img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
109 if (img->data_y == NULL || img->data_cb == NULL || img->data_cr == NULL) {
114 int total_lines = 0, blocks = 0;
115 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
116 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
118 JSAMPROW y_row_ptrs[max_lines];
119 JSAMPROW cb_row_ptrs[max_lines];
120 JSAMPROW cr_row_ptrs[max_lines];
121 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
124 for (i = 0; i < max_lines; ++i) {
125 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;
126 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;
127 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;
130 total_lines += max_lines;
133 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
137 jpeg_destroy_decompress(&dinfo);
141 void qscale_destroy(qscale_img *img)
150 static double sinc(double x)
152 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
154 if (abs(x) < cutoff) {
155 /* For small |x|, use Taylor series instead */
156 const double x2 = x * x;
157 const double x4 = x2 * x2;
159 return 1.0 - x2 / 6.0 + x4 / 120.0;
165 static double lanczos_tap(double x)
167 if (x < -3.0 || x > 3.0)
170 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
172 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
175 static double mitchell_tap(double x)
177 const double b = 1.0 / 3.0;
178 const double c = 1.0 / 3.0;
179 const double p0 = ( 6.0 - 2.0*b ) / 6.0;
180 const double p2 = (-18.0 + 12.0*b + 6.0*c) / 6.0;
181 const double p3 = ( 12.0 - 9.0*b - 6.0*c) / 6.0;
182 const double q0 = ( 8.0*b + 24.0*c) / 6.0;
183 const double q1 = ( - 12.0*b - 48.0*c) / 6.0;
184 const double q2 = ( 6.0*b + 30.0*c) / 6.0;
185 const double q3 = ( - b - 6.0*c) / 6.0;
189 } else if (x < -1.0) {
190 return q0 - x * (q1 - x * (q2 - x * q3));
191 } else if (x < 0.0) {
192 return p0 + x * x * (p2 - x * p3);
193 } else if (x < 1.0) {
194 return p0 + x * x * (p2 + x * p3);
195 } else if (x < 2.0) {
196 return q0 + x * (q1 + x * (q2 + x * q3));
207 static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride, enum qscale_scaling_filter scaling_filter)
209 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
211 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
214 double sf = (double)w / (double)nw;
217 if (scaling_filter == LANCZOS) {
218 support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
219 } else { /* Mitchell */
220 support = (w > nw) ? (2.0 * sf) : (2.0 / sf);
223 /* calculate the filter */
224 for (x = 0; x < nw; ++x) {
225 int start = ceil(x * sf - support);
226 int end = floor(x * sf + support);
237 #if USE_HORIZONTAL_SSE
238 /* round up so we get a multiple of four for the SSE code */
239 int num = (end - start + 1);
241 /* prefer aligning it if possible */
242 if (start % 4 != 0 && start % 4 <= num % 4) {
247 end += 4 - (num % 4);
254 pd[x].startcoeff = num_coeffs;
256 for (sx = start; sx <= end; ++sx) {
257 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
259 if (scaling_filter == LANCZOS) {
261 } else { /* Mitchell */
262 f = mitchell_tap(nd);
264 if (num_coeffs == size_coeffs) {
266 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
269 coeffs[num_coeffs++] = f;
273 for (sx = start; sx <= end; ++sx) {
274 coeffs[pd[x].startcoeff + sx - start] /= sum;
278 for (y = 0; y < h; ++y) {
279 float *sptr = pix + y*sstride;
280 unsigned char *dptr = npix + y*dstride;
282 for (x = 0; x < nw; ++x) {
283 #if USE_HORIZONTAL_SSE
287 static const float low = 0.0, high = 255.0;
292 "movups (%4,%2),%%xmm1 \n"
293 "movups (%3,%2),%%xmm2 \n"
294 "mulps %%xmm2,%%xmm1 \n"
307 : "r" (&coeffs[pd[x].startcoeff]),
308 "r" (&sptr[pd[x].start]),
309 "r" ((pd[x].end - pd[x].start + 1)/4),
312 : "memory", "xmm1", "xmm2"
315 *dptr++ = (unsigned char)result;
318 float *cf = &coeffs[pd[x].startcoeff];
321 for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
322 acc += sptr[sx] * *cf++;
327 else if (acc > 255.0)
330 ch = (unsigned char)acc;
335 for ( ; x < dstride; ++x) {
341 static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride, enum qscale_scaling_filter scaling_filter)
343 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
345 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
348 double sf = (double)h / (double)nh;
351 if (scaling_filter == LANCZOS) {
352 support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
353 } else { /* Mitchell */
354 support = (h > nh) ? (2.0 * sf) : (2.0 / sf);
357 /* calculate the filter */
358 for (y = 0; y < nh; ++y) {
359 int start = ceil(y * sf - support);
360 int end = floor(y * sf + support);
372 pd[y].startcoeff = num_coeffs;
374 for (sy = start; sy <= end; ++sy) {
375 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
377 if (scaling_filter == LANCZOS) {
379 } else { /* Mitchell */
380 f = mitchell_tap(nd);
382 if (num_coeffs == size_coeffs) {
384 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
387 coeffs[num_coeffs++] = f;
391 for (sy = start; sy <= end; ++sy) {
392 coeffs[pd[y].startcoeff + sy - start] /= sum;
396 #if CACHE_LINE_FACTOR > 1
397 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
398 unsigned char *sptr = pix + x;
399 float *dptr = npix + x;
400 for (y = 0; y < nh; ++y) {
403 * xmm0 - xmm3: acc[0..15]
404 * xmm4: current filter coefficient
405 * xmm5, xmm6, xmm7: scratchpad
409 "pxor %%xmm0, %%xmm0 \n"
410 "pxor %%xmm1, %%xmm1 \n"
411 "pxor %%xmm2, %%xmm2 \n"
412 "pxor %%xmm3, %%xmm3 \n"
417 /* a zero is useful during unpacking */
418 "pxor %%xmm4, %%xmm4 \n"
420 /* fetch all 16 source bytes */
421 "movups (%0), %%xmm5 \n"
422 "prefetcht0 (%0,%3,4) \n"
424 /* unpack into words (xmm5, xmm7) */
425 "movaps %%xmm5, %%xmm7 \n"
426 "punpcklbw %%xmm4, %%xmm5 \n"
427 "punpckhbw %%xmm4, %%xmm7 \n"
429 /* unpack xmm5 into dwords (xmm5, xmm6) */
430 "movaps %%xmm5, %%xmm6 \n"
431 "punpcklwd %%xmm4, %%xmm5 \n"
432 "punpckhwd %%xmm4, %%xmm6 \n"
434 /* convert xmm5, xmm6 to floats */
435 "cvtdq2ps %%xmm5, %%xmm5 \n"
436 "cvtdq2ps %%xmm6, %%xmm6 \n"
438 /* fetch the coefficient */
439 "movss (%2), %%xmm4 \n"
440 "shufps $0x0, %%xmm4, %%xmm4 \n"
442 /* do the muls for xmm5 and xmm6 */
443 "mulps %%xmm4, %%xmm5 \n"
444 "mulps %%xmm4, %%xmm6 \n"
445 "addps %%xmm5, %%xmm0 \n"
446 "addps %%xmm6, %%xmm1 \n"
448 /* get the zero back again */
449 "pxor %%xmm4, %%xmm4 \n"
451 /* unpack xmm7 into dwords (xmm7, xmm6) */
452 "movaps %%xmm7, %%xmm6 \n"
453 "punpcklwd %%xmm4, %%xmm7 \n"
454 "punpckhwd %%xmm4, %%xmm6 \n"
456 /* convert xmm7, xmm6 to floats */
457 "cvtdq2ps %%xmm7, %%xmm7 \n"
458 "cvtdq2ps %%xmm6, %%xmm6 \n"
460 /* fetch the coefficient */
461 "movss (%2), %%xmm4 \n"
462 "shufps $0x0, %%xmm4, %%xmm4 \n"
464 /* do the second set of muls */
465 "mulps %%xmm4, %%xmm7 \n"
466 "mulps %%xmm4, %%xmm6 \n"
467 "addps %%xmm7, %%xmm2 \n"
468 "addps %%xmm6, %%xmm3 \n"
470 /* move along, and loop */
476 /* store the values */
477 "movaps %%xmm0, (%4) \n"
478 "movaps %%xmm1, 16(%4) \n"
479 "movaps %%xmm2, 32(%4) \n"
480 "movaps %%xmm3, 48(%4) \n"
482 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
483 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
484 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
485 "r" ((long)w), /* 3: stride */
486 "r" (dptr) /* 4: dstptr base */
487 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
491 float acc[CACHE_LINE_FACTOR];
492 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
494 float *cf = &coeffs[pd[y].startcoeff];
497 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
498 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
499 acc[i] += sptr[sy * w + i] * *cf;
504 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
511 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
513 for (x = 0; x < w; ++x) {
515 unsigned char *sptr = pix + x;
516 float *dptr = npix + x;
517 for (y = 0; y < nh; ++y) {
519 float *cf = &coeffs[pd[y].startcoeff];
522 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
523 acc += sptr[sy * w] * *cf++;
532 qscale_img *qscale_clone(const qscale_img *img)
534 qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
541 unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
542 unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
543 unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
545 /* FIXME: handle out-of-memory gracefully */
547 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
548 memcpy(dst->data_y, img->data_y, dst->h0 * dstride0);
551 dst->data_cb = (unsigned char *)malloc(dst->h1 * dstride1);
552 memcpy(dst->data_cb, img->data_cb, dst->h1 * dstride1);
555 dst->data_cr = (unsigned char *)malloc(dst->h2 * dstride2);
556 memcpy(dst->data_cr, img->data_cr, dst->h2 * dstride2);
562 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)
564 qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
570 dst->height = height;
572 unsigned max_samp_h, max_samp_v;
573 max_samp_h = samp_h0;
574 if (samp_h1 > max_samp_h)
575 max_samp_h = samp_h1;
576 if (samp_h2 > max_samp_h)
577 max_samp_h = samp_h2;
579 max_samp_v = samp_v0;
580 if (samp_v1 > max_samp_v)
581 max_samp_v = samp_v1;
582 if (samp_v2 > max_samp_v)
583 max_samp_v = samp_v2;
585 dst->w0 = width * samp_h0 / max_samp_h;
586 dst->h0 = height * samp_v0 / max_samp_v;
588 dst->w1 = width * samp_h1 / max_samp_h;
589 dst->h1 = height * samp_v1 / max_samp_v;
591 dst->w2 = width * samp_h2 / max_samp_h;
592 dst->h2 = height * samp_v2 / max_samp_v;
594 dst->samp_h0 = samp_h0;
595 dst->samp_v0 = samp_v0;
597 dst->samp_h1 = samp_h1;
598 dst->samp_v1 = samp_v1;
600 dst->samp_h2 = samp_h2;
601 dst->samp_v2 = samp_v2;
603 unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
604 unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
605 unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
607 unsigned sstride0 = (src->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
608 unsigned sstride1 = (src->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
609 unsigned sstride2 = (src->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
611 /* FIXME: handle out-of-memory gracefully */
613 float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
614 vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0, scaling_filter);
615 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
616 hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0, scaling_filter);
620 float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
621 vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1, scaling_filter);
622 dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
623 hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1, scaling_filter);
627 float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
628 vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2, scaling_filter);
629 dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
630 hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2, scaling_filter);
637 int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
639 FILE *file = fopen(filename, "wb");
644 int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
650 int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
652 struct jpeg_compress_struct cinfo;
653 struct jpeg_error_mgr jerr;
654 cinfo.err = jpeg_std_error(&jerr);
655 jpeg_create_compress(&cinfo);
656 jpeg_stdio_dest(&cinfo, file);
657 cinfo.input_components = 3;
658 jpeg_set_defaults(&cinfo);
659 jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
661 if (jpeg_mode == PROGRESSIVE) {
662 jpeg_simple_progression(&cinfo);
665 cinfo.image_width = img->width;
666 cinfo.image_height = img->height;
667 cinfo.raw_data_in = TRUE;
668 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
669 cinfo.comp_info[0].h_samp_factor = img->samp_h0;
670 cinfo.comp_info[0].v_samp_factor = img->samp_v0;
671 cinfo.comp_info[1].h_samp_factor = img->samp_h1;
672 cinfo.comp_info[1].v_samp_factor = img->samp_v1;
673 cinfo.comp_info[2].h_samp_factor = img->samp_h2;
674 cinfo.comp_info[2].v_samp_factor = img->samp_v2;
675 jpeg_start_compress(&cinfo, TRUE);
677 unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
678 unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
679 unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
683 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
684 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
686 JSAMPROW y_row_ptrs[max_lines];
687 JSAMPROW cb_row_ptrs[max_lines];
688 JSAMPROW cr_row_ptrs[max_lines];
689 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
692 for (i = 0; i < max_lines; ++i) {
693 /* simple edge extension */
694 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
695 if (yline > img->h0 - 1)
698 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
699 if (cbline > img->h1 - 1)
700 cbline = img->h1 - 1;
702 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
703 if (crline > img->h2 - 1)
704 crline = img->h2 - 1;
706 y_row_ptrs[i] = img->data_y + yline * dstride0;
707 cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
708 cr_row_ptrs[i] = img->data_cr + crline * dstride2;
711 total_lines += max_lines;
714 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
716 jpeg_finish_compress(&cinfo);
717 jpeg_destroy_compress(&cinfo);