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 0
33 /* Whether to use SSE for vertical scaling or not (requires only SSE1). */
34 #define USE_VERTICAL_SSE 0
37 #undef CACHE_LINE_FACTOR
38 #define CACHE_LINE_FACTOR 16
42 #define M_PI 3.14159265358979323846264
47 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
49 if (abs(x) < cutoff) {
50 /* For small |x|, use Taylor series instead */
51 const double x2 = x * x;
52 const double x4 = x2 * x2;
54 return 1.0 - x2 / 6.0 + x4 / 120.0;
60 double lanczos_tap(double x)
62 if (x < -3.0 || x > 3.0)
65 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
67 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
76 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
78 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
80 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
83 double sf = (double)w / (double)nw;
84 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
86 /* calculate the filter */
87 for (x = 0; x < nw; ++x) {
88 int start = ceil(x * sf - support);
89 int end = floor(x * sf + support);
100 #if USE_HORIZONTAL_SSE
101 /* round up so we get a multiple of four for the SSE code */
102 int num = (end - start + 1);
104 /* prefer aligning it if possible */
105 if (start % 4 != 0 && start % 4 <= num % 4) {
110 end += 4 - (num % 4);
117 pd[x].startcoeff = num_coeffs;
119 for (sx = start; sx <= end; ++sx) {
120 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
121 double f = lanczos_tap(nd);
122 if (num_coeffs == size_coeffs) {
124 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
127 coeffs[num_coeffs++] = f;
131 for (sx = start; sx <= end; ++sx) {
132 coeffs[pd[x].startcoeff + sx - start] /= sum;
136 for (y = 0; y < h; ++y) {
137 float *sptr = pix + y*sstride;
138 unsigned char *dptr = npix + y*dstride;
140 for (x = 0; x < nw; ++x) {
141 #if USE_HORIZONTAL_SSE
145 static const float low = 0.0, high = 255.0;
150 "movups (%4,%2),%%xmm1 \n"
151 "movups (%3,%2),%%xmm2 \n"
152 "mulps %%xmm2,%%xmm1 \n"
165 : "r" (&coeffs[pd[x].startcoeff]),
166 "r" (&sptr[pd[x].start]),
167 "r" ((pd[x].end - pd[x].start + 1)/4),
170 : "memory", "xmm1", "xmm2"
173 *dptr++ = (unsigned char)result;
176 float *cf = &coeffs[pd[x].startcoeff];
179 for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
180 acc += sptr[sx] * *cf++;
185 else if (acc > 255.0)
188 ch = (unsigned char)acc;
193 for ( ; x < dstride; ++x) {
199 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
201 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
203 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
206 double sf = (double)h / (double)nh;
207 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
209 /* calculate the filter */
210 for (y = 0; y < nh; ++y) {
211 int start = ceil(y * sf - support);
212 int end = floor(y * sf + support);
224 pd[y].startcoeff = num_coeffs;
226 for (sy = start; sy <= end; ++sy) {
227 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
228 double f = lanczos_tap(nd);
229 if (num_coeffs == size_coeffs) {
231 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
234 coeffs[num_coeffs++] = f;
238 for (sy = start; sy <= end; ++sy) {
239 coeffs[pd[y].startcoeff + sy - start] /= sum;
243 #if CACHE_LINE_FACTOR > 1
244 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
245 unsigned char *sptr = pix + x;
246 float *dptr = npix + x;
247 for (y = 0; y < nh; ++y) {
250 * xmm0 - xmm3: acc[0..15]
251 * xmm4: current filter coefficient
252 * xmm5, xmm6, xmm7: scratchpad
256 "pxor %%xmm0, %%xmm0 \n"
257 "pxor %%xmm1, %%xmm1 \n"
258 "pxor %%xmm2, %%xmm2 \n"
259 "pxor %%xmm3, %%xmm3 \n"
264 /* a zero is useful during unpacking */
265 "pxor %%xmm4, %%xmm4 \n"
267 /* fetch all 16 source bytes */
268 "movups (%0), %%xmm5 \n"
269 "prefetcht0 (%0,%3,4) \n"
271 /* unpack into words (xmm5, xmm7) */
272 "movaps %%xmm5, %%xmm7 \n"
273 "punpcklbw %%xmm4, %%xmm5 \n"
274 "punpckhbw %%xmm4, %%xmm7 \n"
276 /* unpack xmm5 into dwords (xmm5, xmm6) */
277 "movaps %%xmm5, %%xmm6 \n"
278 "punpcklwd %%xmm4, %%xmm5 \n"
279 "punpckhwd %%xmm4, %%xmm6 \n"
281 /* convert xmm5, xmm6 to floats */
282 "cvtdq2ps %%xmm5, %%xmm5 \n"
283 "cvtdq2ps %%xmm6, %%xmm6 \n"
285 /* fetch the coefficient */
286 "movss (%2), %%xmm4 \n"
287 "shufps $0x0, %%xmm4, %%xmm4 \n"
289 /* do the muls for xmm5 and xmm6 */
290 "mulps %%xmm4, %%xmm5 \n"
291 "mulps %%xmm4, %%xmm6 \n"
292 "addps %%xmm5, %%xmm0 \n"
293 "addps %%xmm6, %%xmm1 \n"
295 /* get the zero back again */
296 "pxor %%xmm4, %%xmm4 \n"
298 /* unpack xmm7 into dwords (xmm7, xmm6) */
299 "movaps %%xmm7, %%xmm6 \n"
300 "punpcklwd %%xmm4, %%xmm7 \n"
301 "punpckhwd %%xmm4, %%xmm6 \n"
303 /* convert xmm7, xmm6 to floats */
304 "cvtdq2ps %%xmm7, %%xmm7 \n"
305 "cvtdq2ps %%xmm6, %%xmm6 \n"
307 /* fetch the coefficient */
308 "movss (%2), %%xmm4 \n"
309 "shufps $0x0, %%xmm4, %%xmm4 \n"
311 /* do the second set of muls */
312 "mulps %%xmm4, %%xmm7 \n"
313 "mulps %%xmm4, %%xmm6 \n"
314 "addps %%xmm7, %%xmm2 \n"
315 "addps %%xmm6, %%xmm3 \n"
317 /* move along, and loop */
323 /* store the values */
324 "movaps %%xmm0, (%4) \n"
325 "movaps %%xmm1, 16(%4) \n"
326 "movaps %%xmm2, 32(%4) \n"
327 "movaps %%xmm3, 48(%4) \n"
329 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
330 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
331 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
332 "r" ((long)w), /* 3: stride */
333 "r" (dptr) /* 4: dstptr base */
334 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
338 float acc[CACHE_LINE_FACTOR];
339 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
341 float *cf = &coeffs[pd[y].startcoeff];
344 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
345 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
346 acc[i] += sptr[sy * w + i] * *cf;
351 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
358 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
360 for (x = 0; x < w; ++x) {
362 unsigned char *sptr = pix + x;
363 float *dptr = npix + x;
364 for (y = 0; y < nh; ++y) {
366 float *cf = &coeffs[pd[y].startcoeff];
369 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
370 acc += sptr[sy * w] * *cf++;
379 int main(int argc, char **argv)
381 /* user-settable parameters */
382 unsigned nominal_w = atoi(argv[1]);
383 unsigned nominal_h = atoi(argv[2]);
384 unsigned samp_h0 = 2, samp_v0 = 2;
385 unsigned samp_h1 = 1, samp_v1 = 1;
386 unsigned samp_h2 = 1, samp_v2 = 1;
387 unsigned jpeg_quality = 85;
390 unsigned max_samp_h, max_samp_v;
391 max_samp_h = samp_h0;
392 if (samp_h1 > max_samp_h)
393 max_samp_h = samp_h1;
394 if (samp_h2 > max_samp_h)
395 max_samp_h = samp_h2;
397 max_samp_v = samp_v0;
398 if (samp_v1 > max_samp_v)
399 max_samp_v = samp_v1;
400 if (samp_v2 > max_samp_v)
401 max_samp_v = samp_v2;
403 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
404 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
405 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
407 unsigned dstride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
408 unsigned dstride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
409 unsigned dstride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
411 qscale_img *img = qscale_load_jpeg_from_stdio(stdin);
412 unsigned sstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
413 unsigned sstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
414 unsigned sstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
416 JSAMPLE *data_ny, *data_ncb, *data_ncr;
419 float *npix = (float*)memalign(16, sstride0 * nh0 * sizeof(float));
420 vscale(img->data_y, npix, sstride0, img->h0, nh0, sstride0);
421 data_ny = (unsigned char *)malloc(nh0 * dstride0);
422 hscale(npix, data_ny, img->w0, nh0, nw0, sstride0, dstride0);
426 float *npix = (float*)memalign(16, sstride1 * nh1 * sizeof(float));
427 vscale(img->data_cr, npix, sstride1, img->h1, nh1, sstride1);
428 data_ncr = (unsigned char *)malloc(nh1 * dstride1);
429 hscale(npix, data_ncr, img->w1, nh1, nw1, sstride1, dstride1);
433 float *npix = (float*)memalign(16, sstride2 * nh2 * sizeof(float));
434 vscale(img->data_cb, npix, sstride2, img->h2, nh2, sstride2);
435 data_ncb = (unsigned char *)malloc(nh2 * dstride2);
436 hscale(npix, data_ncb, img->w2, nh2, nw2, sstride2, dstride2);
440 struct jpeg_compress_struct cinfo;
441 struct jpeg_error_mgr jerr;
442 cinfo.err = jpeg_std_error(&jerr);
443 jpeg_create_compress(&cinfo);
444 jpeg_stdio_dest(&cinfo, stdout);
445 cinfo.input_components = 3;
446 jpeg_set_defaults(&cinfo);
447 jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
448 cinfo.image_width = nominal_w;
449 cinfo.image_height = nominal_h;
450 cinfo.raw_data_in = TRUE;
451 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
452 cinfo.comp_info[0].h_samp_factor = samp_h0;
453 cinfo.comp_info[0].v_samp_factor = samp_v0;
454 cinfo.comp_info[1].h_samp_factor = samp_h1;
455 cinfo.comp_info[1].v_samp_factor = samp_v1;
456 cinfo.comp_info[2].h_samp_factor = samp_h2;
457 cinfo.comp_info[2].v_samp_factor = samp_v2;
458 jpeg_start_compress(&cinfo, TRUE);
462 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
463 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
465 JSAMPROW y_row_ptrs[max_lines];
466 JSAMPROW cb_row_ptrs[max_lines];
467 JSAMPROW cr_row_ptrs[max_lines];
468 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
471 for (i = 0; i < max_lines; ++i) {
472 /* simple edge extension */
473 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
477 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
478 if (cbline > nh1 - 1)
481 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
482 if (crline > nh2 - 1)
485 y_row_ptrs[i] = data_ny + yline * dstride0;
486 cb_row_ptrs[i] = data_ncb + cbline * dstride1;
487 cr_row_ptrs[i] = data_ncr + crline * dstride2;
490 total_lines += max_lines;
493 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
495 jpeg_finish_compress(&cinfo);
496 jpeg_destroy_compress(&cinfo);