8 /* The number of pixels to process at a time when scaling vertically. */
9 #define CACHE_LINE_FACTOR 16
11 /* Whether to use SSE for horizontal scaling or not (requires SSE3). */
12 #define USE_HORIZONTAL_SSE 1
14 /* Whether to use SSE for vertical scaling or not (requires only SSE1). */
15 #define USE_VERTICAL_SSE 1
18 #undef CACHE_LINE_FACTOR
19 #define CACHE_LINE_FACTOR 16
23 #define M_PI 3.14159265358979323846264
26 qscale_img *qscale_load_jpeg(const char *filename)
28 FILE *file = fopen(filename, "rb");
34 img = qscale_load_jpeg_from_stdio(file);
40 qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
42 qscale_img *img = (qscale_img *)malloc(sizeof(qscale_img));
47 img->data_y = img->data_cb = img->data_cr = NULL;
49 /* FIXME: Better error handling here (ie., return NULL). */
50 struct jpeg_decompress_struct dinfo;
51 struct jpeg_error_mgr jerr;
52 dinfo.err = jpeg_std_error(&jerr);
53 jpeg_create_decompress(&dinfo);
54 jpeg_stdio_src(&dinfo, stdin);
55 jpeg_read_header(&dinfo, TRUE);
56 dinfo.raw_data_out = TRUE;
57 jpeg_start_decompress(&dinfo);
59 /* We do not handle anything but YCbCr images (yet?). */
60 if (dinfo.num_components != 3) {
65 img->width = dinfo.image_width;
66 img->height = dinfo.image_height;
68 img->w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
69 img->h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
71 img->w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
72 img->h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
74 img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
75 img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
77 img->data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
78 img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
79 img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
81 if (img->data_y == NULL || img->data_cb == NULL || img->data_cr == NULL) {
86 int total_lines = 0, blocks = 0;
87 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
88 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
90 JSAMPROW y_row_ptrs[max_lines];
91 JSAMPROW cb_row_ptrs[max_lines];
92 JSAMPROW cr_row_ptrs[max_lines];
93 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
96 for (i = 0; i < max_lines; ++i) {
97 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;
98 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;
99 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;
102 total_lines += max_lines;
105 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
109 jpeg_destroy_decompress(&dinfo);
113 void qscale_destroy(qscale_img *img)
122 static double sinc(double x)
124 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
126 if (abs(x) < cutoff) {
127 /* For small |x|, use Taylor series instead */
128 const double x2 = x * x;
129 const double x4 = x2 * x2;
131 return 1.0 - x2 / 6.0 + x4 / 120.0;
137 static double lanczos_tap(double x)
139 if (x < -3.0 || x > 3.0)
142 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
144 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
152 static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
154 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
156 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
159 double sf = (double)w / (double)nw;
160 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
162 /* calculate the filter */
163 for (x = 0; x < nw; ++x) {
164 int start = ceil(x * sf - support);
165 int end = floor(x * sf + support);
176 #if USE_HORIZONTAL_SSE
177 /* round up so we get a multiple of four for the SSE code */
178 int num = (end - start + 1);
180 /* prefer aligning it if possible */
181 if (start % 4 != 0 && start % 4 <= num % 4) {
186 end += 4 - (num % 4);
193 pd[x].startcoeff = num_coeffs;
195 for (sx = start; sx <= end; ++sx) {
196 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
197 double f = lanczos_tap(nd);
198 if (num_coeffs == size_coeffs) {
200 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
203 coeffs[num_coeffs++] = f;
207 for (sx = start; sx <= end; ++sx) {
208 coeffs[pd[x].startcoeff + sx - start] /= sum;
212 for (y = 0; y < h; ++y) {
213 float *sptr = pix + y*sstride;
214 unsigned char *dptr = npix + y*dstride;
216 for (x = 0; x < nw; ++x) {
217 #if USE_HORIZONTAL_SSE
221 static const float low = 0.0, high = 255.0;
226 "movups (%4,%2),%%xmm1 \n"
227 "movups (%3,%2),%%xmm2 \n"
228 "mulps %%xmm2,%%xmm1 \n"
241 : "r" (&coeffs[pd[x].startcoeff]),
242 "r" (&sptr[pd[x].start]),
243 "r" ((pd[x].end - pd[x].start + 1)/4),
246 : "memory", "xmm1", "xmm2"
249 *dptr++ = (unsigned char)result;
252 float *cf = &coeffs[pd[x].startcoeff];
255 for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
256 acc += sptr[sx] * *cf++;
261 else if (acc > 255.0)
264 ch = (unsigned char)acc;
269 for ( ; x < dstride; ++x) {
275 static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
277 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
279 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
282 double sf = (double)h / (double)nh;
283 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
285 /* calculate the filter */
286 for (y = 0; y < nh; ++y) {
287 int start = ceil(y * sf - support);
288 int end = floor(y * sf + support);
300 pd[y].startcoeff = num_coeffs;
302 for (sy = start; sy <= end; ++sy) {
303 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
304 double f = lanczos_tap(nd);
305 if (num_coeffs == size_coeffs) {
307 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
310 coeffs[num_coeffs++] = f;
314 for (sy = start; sy <= end; ++sy) {
315 coeffs[pd[y].startcoeff + sy - start] /= sum;
319 #if CACHE_LINE_FACTOR > 1
320 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
321 unsigned char *sptr = pix + x;
322 float *dptr = npix + x;
323 for (y = 0; y < nh; ++y) {
326 * xmm0 - xmm3: acc[0..15]
327 * xmm4: current filter coefficient
328 * xmm5, xmm6, xmm7: scratchpad
332 "pxor %%xmm0, %%xmm0 \n"
333 "pxor %%xmm1, %%xmm1 \n"
334 "pxor %%xmm2, %%xmm2 \n"
335 "pxor %%xmm3, %%xmm3 \n"
340 /* a zero is useful during unpacking */
341 "pxor %%xmm4, %%xmm4 \n"
343 /* fetch all 16 source bytes */
344 "movups (%0), %%xmm5 \n"
345 "prefetcht0 (%0,%3,4) \n"
347 /* unpack into words (xmm5, xmm7) */
348 "movaps %%xmm5, %%xmm7 \n"
349 "punpcklbw %%xmm4, %%xmm5 \n"
350 "punpckhbw %%xmm4, %%xmm7 \n"
352 /* unpack xmm5 into dwords (xmm5, xmm6) */
353 "movaps %%xmm5, %%xmm6 \n"
354 "punpcklwd %%xmm4, %%xmm5 \n"
355 "punpckhwd %%xmm4, %%xmm6 \n"
357 /* convert xmm5, xmm6 to floats */
358 "cvtdq2ps %%xmm5, %%xmm5 \n"
359 "cvtdq2ps %%xmm6, %%xmm6 \n"
361 /* fetch the coefficient */
362 "movss (%2), %%xmm4 \n"
363 "shufps $0x0, %%xmm4, %%xmm4 \n"
365 /* do the muls for xmm5 and xmm6 */
366 "mulps %%xmm4, %%xmm5 \n"
367 "mulps %%xmm4, %%xmm6 \n"
368 "addps %%xmm5, %%xmm0 \n"
369 "addps %%xmm6, %%xmm1 \n"
371 /* get the zero back again */
372 "pxor %%xmm4, %%xmm4 \n"
374 /* unpack xmm7 into dwords (xmm7, xmm6) */
375 "movaps %%xmm7, %%xmm6 \n"
376 "punpcklwd %%xmm4, %%xmm7 \n"
377 "punpckhwd %%xmm4, %%xmm6 \n"
379 /* convert xmm7, xmm6 to floats */
380 "cvtdq2ps %%xmm7, %%xmm7 \n"
381 "cvtdq2ps %%xmm6, %%xmm6 \n"
383 /* fetch the coefficient */
384 "movss (%2), %%xmm4 \n"
385 "shufps $0x0, %%xmm4, %%xmm4 \n"
387 /* do the second set of muls */
388 "mulps %%xmm4, %%xmm7 \n"
389 "mulps %%xmm4, %%xmm6 \n"
390 "addps %%xmm7, %%xmm2 \n"
391 "addps %%xmm6, %%xmm3 \n"
393 /* move along, and loop */
399 /* store the values */
400 "movaps %%xmm0, (%4) \n"
401 "movaps %%xmm1, 16(%4) \n"
402 "movaps %%xmm2, 32(%4) \n"
403 "movaps %%xmm3, 48(%4) \n"
405 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
406 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
407 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
408 "r" ((long)w), /* 3: stride */
409 "r" (dptr) /* 4: dstptr base */
410 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
414 float acc[CACHE_LINE_FACTOR];
415 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
417 float *cf = &coeffs[pd[y].startcoeff];
420 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
421 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
422 acc[i] += sptr[sy * w + i] * *cf;
427 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
434 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
436 for (x = 0; x < w; ++x) {
438 unsigned char *sptr = pix + x;
439 float *dptr = npix + x;
440 for (y = 0; y < nh; ++y) {
442 float *cf = &coeffs[pd[y].startcoeff];
445 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
446 acc += sptr[sy * w] * *cf++;
455 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)
457 qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
463 dst->height = height;
465 unsigned max_samp_h, max_samp_v;
466 max_samp_h = samp_h0;
467 if (samp_h1 > max_samp_h)
468 max_samp_h = samp_h1;
469 if (samp_h2 > max_samp_h)
470 max_samp_h = samp_h2;
472 max_samp_v = samp_v0;
473 if (samp_v1 > max_samp_v)
474 max_samp_v = samp_v1;
475 if (samp_v2 > max_samp_v)
476 max_samp_v = samp_v2;
478 dst->w0 = width * samp_h0 / max_samp_h;
479 dst->h0 = height * samp_v0 / max_samp_v;
481 dst->w1 = width * samp_h1 / max_samp_h;
482 dst->h1 = height * samp_v1 / max_samp_v;
484 dst->w2 = width * samp_h2 / max_samp_h;
485 dst->h2 = height * samp_v2 / max_samp_v;
487 unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
488 unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
489 unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
491 unsigned sstride0 = (src->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
492 unsigned sstride1 = (src->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
493 unsigned sstride2 = (src->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
495 /* FIXME: handle out-of-memory gracefully */
497 float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
498 vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0);
499 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
500 hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0);
504 float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
505 vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1);
506 dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
507 hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1);
511 float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
512 vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2);
513 dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
514 hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2);