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 #if USE_VERTICAL_SSE || USE_HORIZONTAL_SSE
46 typedef float v4sf __attribute__((vector_size(16)));
47 typedef int v4si __attribute__((vector_size(16)));
48 typedef short v8hi __attribute__((vector_size(16)));
49 typedef char v16qi __attribute__((vector_size(16)));
52 qscale_img *qscale_load_jpeg(const char *filename)
54 FILE *file = fopen(filename, "rb");
60 img = qscale_load_jpeg_from_stdio(file);
66 qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
68 qscale_img *img = (qscale_img *)malloc(sizeof(qscale_img));
73 img->data_y = img->data_cb = img->data_cr = NULL;
75 /* FIXME: Better error handling here (ie., return NULL). */
76 struct jpeg_decompress_struct dinfo;
77 struct jpeg_error_mgr jerr;
78 dinfo.err = jpeg_std_error(&jerr);
79 jpeg_create_decompress(&dinfo);
80 jpeg_stdio_src(&dinfo, file);
81 jpeg_read_header(&dinfo, TRUE);
82 dinfo.raw_data_out = TRUE;
83 jpeg_start_decompress(&dinfo);
85 if (dinfo.num_components != 1 && dinfo.num_components != 3) {
89 img->num_components = dinfo.num_components;
91 img->width = dinfo.image_width;
92 img->height = dinfo.image_height;
94 img->w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
95 img->h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
97 if (img->num_components == 3) {
98 img->w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
99 img->h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
101 img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
102 img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
105 img->samp_h0 = dinfo.comp_info[0].h_samp_factor;
106 img->samp_v0 = dinfo.comp_info[0].v_samp_factor;
108 if (img->num_components == 3) {
109 img->samp_h1 = dinfo.comp_info[1].h_samp_factor;
110 img->samp_v1 = dinfo.comp_info[1].v_samp_factor;
112 img->samp_h2 = dinfo.comp_info[2].h_samp_factor;
113 img->samp_v2 = dinfo.comp_info[2].v_samp_factor;
116 img->data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
117 if (img->data_y == NULL) {
122 if (img->num_components == 3) {
123 img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
124 img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
125 if (img->data_cb == NULL || img->data_cr == NULL) {
131 int total_lines = 0, blocks = 0;
132 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
133 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
135 JSAMPROW y_row_ptrs[max_lines];
136 JSAMPROW cb_row_ptrs[max_lines];
137 JSAMPROW cr_row_ptrs[max_lines];
138 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
141 for (i = 0; i < max_lines; ++i) {
142 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;
143 if (img->num_components == 3) {
144 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;
145 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;
149 total_lines += max_lines;
152 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
156 jpeg_destroy_decompress(&dinfo);
160 void qscale_destroy(qscale_img *img)
169 static double sinc(double x)
171 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
173 if (abs(x) < cutoff) {
174 /* For small |x|, use Taylor series instead */
175 const double x2 = x * x;
176 const double x4 = x2 * x2;
178 return 1.0 - x2 / 6.0 + x4 / 120.0;
184 static double lanczos_tap(double x)
186 if (x < -3.0 || x > 3.0)
189 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
191 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
194 static double mitchell_tap(double x)
196 const double b = 1.0 / 3.0;
197 const double c = 1.0 / 3.0;
198 const double p0 = ( 6.0 - 2.0*b ) / 6.0;
199 const double p2 = (-18.0 + 12.0*b + 6.0*c) / 6.0;
200 const double p3 = ( 12.0 - 9.0*b - 6.0*c) / 6.0;
201 const double q0 = ( 8.0*b + 24.0*c) / 6.0;
202 const double q1 = ( - 12.0*b - 48.0*c) / 6.0;
203 const double q2 = ( 6.0*b + 30.0*c) / 6.0;
204 const double q3 = ( - b - 6.0*c) / 6.0;
208 } else if (x < -1.0) {
209 return q0 - x * (q1 - x * (q2 - x * q3));
210 } else if (x < 0.0) {
211 return p0 + x * x * (p2 - x * p3);
212 } else if (x < 1.0) {
213 return p0 + x * x * (p2 + x * p3);
214 } else if (x < 2.0) {
215 return q0 + x * (q1 + x * (q2 + x * q3));
226 static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride, enum qscale_scaling_filter scaling_filter)
228 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
230 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
233 double sf = (double)w / (double)nw;
236 if (scaling_filter == LANCZOS) {
237 support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
238 } else { /* Mitchell */
239 support = (w > nw) ? (2.0 * sf) : (2.0 / sf);
242 /* calculate the filter */
243 for (x = 0; x < nw; ++x) {
244 int start = ceil(x * sf - support);
245 int end = floor(x * sf + support);
256 #if USE_HORIZONTAL_SSE
257 /* round up so we get a multiple of four for the SSE code */
258 int num = (end - start + 1);
260 /* prefer aligning it if possible */
261 if (start % 4 != 0 && start % 4 <= num % 4) {
266 end += 4 - (num % 4);
273 pd[x].startcoeff = num_coeffs;
275 for (sx = start; sx <= end; ++sx) {
276 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
278 if (scaling_filter == LANCZOS) {
280 } else { /* Mitchell */
281 f = mitchell_tap(nd);
283 if (num_coeffs == size_coeffs) {
285 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
288 coeffs[num_coeffs++] = f;
292 for (sx = start; sx <= end; ++sx) {
293 coeffs[pd[x].startcoeff + sx - start] /= sum;
297 for (y = 0; y < h; ++y) {
298 float *sptr = pix + y*sstride;
299 unsigned char *dptr = npix + y*dstride;
301 for (x = 0; x < nw; ++x) {
302 #if USE_HORIZONTAL_SSE
306 static const float low = 0.0, high = 255.0;
311 "movups (%4,%2),%%xmm1 \n"
312 "movups (%3,%2),%%xmm2 \n"
313 "mulps %%xmm2,%%xmm1 \n"
326 : "r" (&coeffs[pd[x].startcoeff]),
327 "r" (&sptr[pd[x].start]),
328 "r" ((pd[x].end - pd[x].start + 1)/4),
331 : "memory", "xmm1", "xmm2"
334 *dptr++ = (unsigned char)result;
337 float *cf = &coeffs[pd[x].startcoeff];
340 for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
341 acc += sptr[sx] * *cf++;
346 else if (acc > 255.0)
349 ch = (unsigned char)acc;
354 for ( ; x < dstride; ++x) {
360 static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride, enum qscale_scaling_filter scaling_filter)
362 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
364 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
367 double sf = (double)h / (double)nh;
370 if (scaling_filter == LANCZOS) {
371 support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
372 } else { /* Mitchell */
373 support = (h > nh) ? (2.0 * sf) : (2.0 / sf);
376 /* calculate the filter */
377 for (y = 0; y < nh; ++y) {
378 int start = ceil(y * sf - support);
379 int end = floor(y * sf + support);
391 pd[y].startcoeff = num_coeffs;
393 for (sy = start; sy <= end; ++sy) {
394 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
396 if (scaling_filter == LANCZOS) {
398 } else { /* Mitchell */
399 f = mitchell_tap(nd);
401 if (num_coeffs == size_coeffs) {
403 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
406 coeffs[num_coeffs++] = f;
410 for (sy = start; sy <= end; ++sy) {
411 coeffs[pd[y].startcoeff + sy - start] /= sum;
415 #if CACHE_LINE_FACTOR > 1
416 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
417 unsigned char *sptr = pix + x;
418 float *dptr = npix + x;
419 for (y = 0; y < nh; ++y) {
421 /* A zero is useful during unpacking. */
422 static const v4sf zero = { 0.0f, 0.0f, 0.0f, 0.0f };
423 const unsigned char *sptr_xmm = &sptr[pd[y].start * w];
424 const float *coeffptr = &coeffs[pd[y].startcoeff];
425 const int filter_len = pd[y].end - pd[y].start + 1;
428 v4sf acc0 = { 0.0f, 0.0f, 0.0f, 0.0f };
429 v4sf acc1 = { 0.0f, 0.0f, 0.0f, 0.0f };
430 v4sf acc2 = { 0.0f, 0.0f, 0.0f, 0.0f };
431 v4sf acc3 = { 0.0f, 0.0f, 0.0f, 0.0f };
433 for (i = 0; i < filter_len; ++i, ++coeffptr, sptr_xmm += w) {
434 __builtin_prefetch(sptr_xmm + w, 0);
435 v16qi src = (v16qi)__builtin_ia32_loadups((float*)sptr_xmm);
438 v8hi src_lo = (v8hi)__builtin_ia32_punpcklbw128(src, (v16qi)zero);
439 v8hi src_hi = (v8hi)__builtin_ia32_punpckhbw128(src, (v16qi)zero);
441 // unpack into dwords, convert to floats
442 v4si src0_i = (v4si)__builtin_ia32_punpcklwd128(src_lo, (v8hi)zero);
443 v4si src1_i = (v4si)__builtin_ia32_punpckhwd128(src_lo, (v8hi)zero);
444 v4si src2_i = (v4si)__builtin_ia32_punpcklwd128(src_hi, (v8hi)zero);
445 v4si src3_i = (v4si)__builtin_ia32_punpckhwd128(src_hi, (v8hi)zero);
447 v4sf src0 = __builtin_ia32_cvtdq2ps(src0_i);
448 v4sf src1 = __builtin_ia32_cvtdq2ps(src1_i);
449 v4sf src2 = __builtin_ia32_cvtdq2ps(src2_i);
450 v4sf src3 = __builtin_ia32_cvtdq2ps(src3_i);
452 // fetch the coefficient, and replicate it
453 v4sf coeff = { *coeffptr, *coeffptr, *coeffptr, *coeffptr };
455 // do the actual muladds
456 acc0 = __builtin_ia32_addps(acc0, __builtin_ia32_mulps(src0, coeff));
457 acc1 = __builtin_ia32_addps(acc1, __builtin_ia32_mulps(src1, coeff));
458 acc2 = __builtin_ia32_addps(acc2, __builtin_ia32_mulps(src2, coeff));
459 acc3 = __builtin_ia32_addps(acc3, __builtin_ia32_mulps(src3, coeff));
462 *(v4sf *)(&dptr[0]) = acc0;
463 *(v4sf *)(&dptr[4]) = acc1;
464 *(v4sf *)(&dptr[8]) = acc2;
465 *(v4sf *)(&dptr[12]) = acc3;
468 float acc[CACHE_LINE_FACTOR];
469 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
471 float *cf = &coeffs[pd[y].startcoeff];
474 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
475 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
476 acc[i] += sptr[sy * w + i] * *cf;
481 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
488 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
490 for (x = 0; x < w; ++x) {
492 unsigned char *sptr = pix + x;
493 float *dptr = npix + x;
494 for (y = 0; y < nh; ++y) {
496 float *cf = &coeffs[pd[y].startcoeff];
499 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
500 acc += sptr[sy * w] * *cf++;
509 qscale_img *qscale_clone(const qscale_img *img)
511 qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
518 unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
519 unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
520 unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
522 /* FIXME: handle out-of-memory gracefully */
524 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
525 memcpy(dst->data_y, img->data_y, dst->h0 * dstride0);
528 dst->data_cb = (unsigned char *)malloc(dst->h1 * dstride1);
529 memcpy(dst->data_cb, img->data_cb, dst->h1 * dstride1);
532 dst->data_cr = (unsigned char *)malloc(dst->h2 * dstride2);
533 memcpy(dst->data_cr, img->data_cr, dst->h2 * dstride2);
539 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)
541 qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
547 dst->height = height;
548 dst->num_components = src->num_components;
550 unsigned max_samp_h, max_samp_v;
551 max_samp_h = samp_h0;
552 if (src->num_components == 3) {
553 if (samp_h1 > max_samp_h)
554 max_samp_h = samp_h1;
555 if (samp_h2 > max_samp_h)
556 max_samp_h = samp_h2;
559 max_samp_v = samp_v0;
560 if (src->num_components == 3) {
561 if (samp_v1 > max_samp_v)
562 max_samp_v = samp_v1;
563 if (samp_v2 > max_samp_v)
564 max_samp_v = samp_v2;
567 dst->w0 = width * samp_h0 / max_samp_h;
568 dst->h0 = height * samp_v0 / max_samp_v;
570 if (src->num_components == 3) {
571 dst->w1 = width * samp_h1 / max_samp_h;
572 dst->h1 = height * samp_v1 / max_samp_v;
574 dst->w2 = width * samp_h2 / max_samp_h;
575 dst->h2 = height * samp_v2 / max_samp_v;
578 dst->samp_h0 = samp_h0;
579 dst->samp_v0 = samp_v0;
581 if (src->num_components == 3) {
582 dst->samp_h1 = samp_h1;
583 dst->samp_v1 = samp_v1;
585 dst->samp_h2 = samp_h2;
586 dst->samp_v2 = samp_v2;
589 unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
590 unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
591 unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
593 unsigned sstride0 = (src->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
594 unsigned sstride1 = (src->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
595 unsigned sstride2 = (src->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
597 /* FIXME: handle out-of-memory gracefully */
599 float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
600 vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0, scaling_filter);
601 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
602 hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0, scaling_filter);
605 if (src->num_components == 3) {
607 float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
608 vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1, scaling_filter);
609 dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
610 hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1, scaling_filter);
614 float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
615 vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2, scaling_filter);
616 dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
617 hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2, scaling_filter);
625 int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
627 FILE *file = fopen(filename, "wb");
632 int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
638 int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
640 struct jpeg_compress_struct cinfo;
641 struct jpeg_error_mgr jerr;
642 cinfo.err = jpeg_std_error(&jerr);
643 jpeg_create_compress(&cinfo);
644 jpeg_stdio_dest(&cinfo, file);
645 cinfo.input_components = img->num_components;
646 jpeg_set_defaults(&cinfo);
647 jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
649 if (jpeg_mode == PROGRESSIVE) {
650 jpeg_simple_progression(&cinfo);
653 cinfo.image_width = img->width;
654 cinfo.image_height = img->height;
655 cinfo.raw_data_in = TRUE;
656 if (img->num_components == 3) {
657 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
659 jpeg_set_colorspace(&cinfo, JCS_GRAYSCALE);
661 cinfo.comp_info[0].h_samp_factor = img->samp_h0;
662 cinfo.comp_info[0].v_samp_factor = img->samp_v0;
663 if (img->num_components == 3) {
664 cinfo.comp_info[1].h_samp_factor = img->samp_h1;
665 cinfo.comp_info[1].v_samp_factor = img->samp_v1;
666 cinfo.comp_info[2].h_samp_factor = img->samp_h2;
667 cinfo.comp_info[2].v_samp_factor = img->samp_v2;
669 jpeg_start_compress(&cinfo, TRUE);
671 unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
672 unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
673 unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
677 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
678 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
680 JSAMPROW y_row_ptrs[max_lines];
681 JSAMPROW cb_row_ptrs[max_lines];
682 JSAMPROW cr_row_ptrs[max_lines];
683 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
686 for (i = 0; i < max_lines; ++i) {
687 /* simple edge extension */
688 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
689 if (yline > img->h0 - 1)
692 y_row_ptrs[i] = img->data_y + yline * dstride0;
694 if (img->num_components == 3) {
695 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
696 if (cbline > img->h1 - 1)
697 cbline = img->h1 - 1;
699 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
700 if (crline > img->h2 - 1)
701 crline = img->h2 - 1;
703 cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
704 cr_row_ptrs[i] = img->data_cr + crline * dstride2;
708 total_lines += max_lines;
711 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
713 jpeg_finish_compress(&cinfo);
714 jpeg_destroy_compress(&cinfo);