8 #define CACHE_LINE_FACTOR 16
11 #define M_PI 3.14159265358979323846264
16 static const double cutoff = 1.220703668e-4; /* sqrt(sqrt(eps)) */
18 if (abs(x) < cutoff) {
19 /* For small |x|, use Taylor series instead */
20 const double x2 = x * x;
21 const double x4 = x2 * x2;
23 return 1.0 - x2 / 6.0 + x4 / 120.0;
29 double lanczos_tap(double x)
31 if (x < -3.0 || x > 3.0)
34 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
36 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
45 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
47 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
49 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
52 double sf = (double)w / (double)nw;
53 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
55 /* calculate the filter */
56 for (x = 0; x < nw; ++x) {
57 int start = ceil(x * sf - support);
58 int end = floor(x * sf + support);
69 /* round up so we get a multiple of four for the SSE code */
70 int num = (end - start + 1);
72 /* prefer aligning it if possible */
73 if (start % 4 != 0 && start % 4 <= num % 4) {
84 pd[x].startcoeff = num_coeffs;
86 for (sx = start; sx <= end; ++sx) {
87 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
88 double f = lanczos_tap(nd);
89 if (num_coeffs == size_coeffs) {
91 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
94 coeffs[num_coeffs++] = f;
98 for (sx = start; sx <= end; ++sx) {
99 coeffs[pd[x].startcoeff + sx - start] /= sum;
103 for (y = 0; y < h; ++y) {
104 float *sptr = pix + y*sstride;
105 unsigned char *dptr = npix + y*dstride;
107 for (x = 0; x < nw; ++x) {
111 static const float low = 0.0, high = 255.0;
116 "movups (%4,%2),%%xmm1 \n"
117 "movups (%3,%2),%%xmm2 \n"
118 "mulps %%xmm2,%%xmm1 \n"
131 : "r" (&coeffs[pd[x].startcoeff]),
132 "r" (&sptr[pd[x].start]),
133 "r" ((pd[x].end - pd[x].start + 1)/4),
136 : "memory", "xmm1", "xmm2"
142 else if (acc > 255.0)
145 ch = (unsigned char)acc;
148 *dptr++ = (unsigned char)result;
151 for ( ; x < dstride; ++x) {
157 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
159 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
161 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
164 double sf = (double)h / (double)nh;
165 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
167 /* calculate the filter */
168 for (y = 0; y < nh; ++y) {
169 int start = ceil(y * sf - support);
170 int end = floor(y * sf + support);
182 pd[y].startcoeff = num_coeffs;
184 for (sy = start; sy <= end; ++sy) {
185 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
186 double f = lanczos_tap(nd);
187 if (num_coeffs == size_coeffs) {
189 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
192 coeffs[num_coeffs++] = f;
196 for (sy = start; sy <= end; ++sy) {
197 coeffs[pd[y].startcoeff + sy - start] /= sum;
201 #if CACHE_LINE_FACTOR > 1
202 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
203 unsigned char *sptr = pix + x;
204 float *dptr = npix + x;
205 for (y = 0; y < nh; ++y) {
208 float acc[CACHE_LINE_FACTOR];
209 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
211 float *cf = &coeffs[pd[y].startcoeff];
214 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
215 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
216 acc[i] += sptr[sy * w + i] * *cf;
221 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
226 * xmm0 - xmm3: acc[0..15]
227 * xmm4: current filter coefficient
228 * xmm5, xmm6, xmm7: scratchpad
232 "pxor %%xmm0, %%xmm0 \n"
233 "pxor %%xmm1, %%xmm1 \n"
234 "pxor %%xmm2, %%xmm2 \n"
235 "pxor %%xmm3, %%xmm3 \n"
240 /* a zero is useful during unpacking */
241 "pxor %%xmm4, %%xmm4 \n"
243 /* fetch all 16 source bytes */
244 "movups (%0), %%xmm5 \n"
245 "prefetcht0 (%0,%3,4) \n"
247 /* unpack into words (xmm5, xmm7) */
248 "movaps %%xmm5, %%xmm7 \n"
249 "punpcklbw %%xmm4, %%xmm5 \n"
250 "punpckhbw %%xmm4, %%xmm7 \n"
252 /* unpack xmm5 into dwords (xmm5, xmm6) */
253 "movaps %%xmm5, %%xmm6 \n"
254 "punpcklwd %%xmm4, %%xmm5 \n"
255 "punpckhwd %%xmm4, %%xmm6 \n"
257 /* convert xmm5, xmm6 to floats */
258 "cvtdq2ps %%xmm5, %%xmm5 \n"
259 "cvtdq2ps %%xmm6, %%xmm6 \n"
261 /* fetch the coefficient */
262 "movss (%2), %%xmm4 \n"
263 "shufps $0x0, %%xmm4, %%xmm4 \n"
265 /* do the muls for xmm5 and xmm6 */
266 "mulps %%xmm4, %%xmm5 \n"
267 "mulps %%xmm4, %%xmm6 \n"
268 "addps %%xmm5, %%xmm0 \n"
269 "addps %%xmm6, %%xmm1 \n"
271 /* get the zero back again */
272 "pxor %%xmm4, %%xmm4 \n"
274 /* unpack xmm7 into dwords (xmm7, xmm6) */
275 "movaps %%xmm7, %%xmm6 \n"
276 "punpcklwd %%xmm4, %%xmm7 \n"
277 "punpckhwd %%xmm4, %%xmm6 \n"
279 /* convert xmm7, xmm6 to floats */
280 "cvtdq2ps %%xmm7, %%xmm7 \n"
281 "cvtdq2ps %%xmm6, %%xmm6 \n"
283 /* fetch the coefficient */
284 "movss (%2), %%xmm4 \n"
285 "shufps $0x0, %%xmm4, %%xmm4 \n"
287 /* do the second set of muls */
288 "mulps %%xmm4, %%xmm7 \n"
289 "mulps %%xmm4, %%xmm6 \n"
290 "addps %%xmm7, %%xmm2 \n"
291 "addps %%xmm6, %%xmm3 \n"
293 /* move along, and loop */
299 /* store the values */
300 "movaps %%xmm0, (%4) \n"
301 "movaps %%xmm1, 16(%4) \n"
302 "movaps %%xmm2, 32(%4) \n"
303 "movaps %%xmm3, 48(%4) \n"
305 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
306 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
307 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
308 "r" ((long)w), /* 3: stride */
309 "r" (dptr) /* 4: dstptr base */
310 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
316 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
318 for (x = 0; x < w; ++x) {
320 unsigned char *sptr = pix + x;
321 float *dptr = npix + x;
322 for (y = 0; y < nh; ++y) {
324 float *cf = &coeffs[pd[y].startcoeff];
327 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
328 acc += sptr[sy * w] * *cf++;
337 int main(int argc, char **argv)
339 /* user-settable parameters */
340 unsigned nominal_w = atoi(argv[1]);
341 unsigned nominal_h = atoi(argv[2]);
342 unsigned samp_h0 = 2, samp_v0 = 2;
343 unsigned samp_h1 = 1, samp_v1 = 1;
344 unsigned samp_h2 = 1, samp_v2 = 1;
345 unsigned jpeg_quality = 85;
348 unsigned max_samp_h, max_samp_v;
349 max_samp_h = samp_h0;
350 if (samp_h1 > max_samp_h)
351 max_samp_h = samp_h1;
352 if (samp_h2 > max_samp_h)
353 max_samp_h = samp_h2;
355 max_samp_v = samp_v0;
356 if (samp_v1 > max_samp_v)
357 max_samp_v = samp_v1;
358 if (samp_v2 > max_samp_v)
359 max_samp_v = samp_v2;
361 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
362 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
363 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
365 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
366 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
367 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
369 struct jpeg_decompress_struct dinfo;
370 struct jpeg_error_mgr jerr;
371 dinfo.err = jpeg_std_error(&jerr);
372 jpeg_create_decompress(&dinfo);
373 jpeg_stdio_src(&dinfo, stdin);
374 jpeg_read_header(&dinfo, TRUE);
375 dinfo.raw_data_out = TRUE;
376 jpeg_start_decompress(&dinfo);
378 unsigned w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
379 unsigned h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
381 unsigned w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
382 unsigned h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
384 unsigned w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
385 unsigned h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
387 fprintf(stderr, "Scaling using Lanczos filter:\n");
388 fprintf(stderr, " Y component: %ux%u -> %ux%u\n", dinfo.comp_info[0].width_in_blocks * DCTSIZE, dinfo.comp_info[0].height_in_blocks * DCTSIZE, nw0, nh0);
389 fprintf(stderr, " Cb component: %ux%u -> %ux%u\n", dinfo.comp_info[1].width_in_blocks * DCTSIZE, dinfo.comp_info[1].height_in_blocks * DCTSIZE, nw1, nh1);
390 fprintf(stderr, " Cr component: %ux%u -> %ux%u\n", dinfo.comp_info[2].width_in_blocks * DCTSIZE, dinfo.comp_info[2].height_in_blocks * DCTSIZE, nw2, nh2);
392 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
393 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
394 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
395 JSAMPLE *data_ny, *data_ncb, *data_ncr;
397 int total_lines = 0, blocks = 0;
398 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
399 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
401 JSAMPROW y_row_ptrs[max_lines];
402 JSAMPROW cb_row_ptrs[max_lines];
403 JSAMPROW cr_row_ptrs[max_lines];
404 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
407 for (i = 0; i < max_lines; ++i) {
408 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
409 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
410 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
413 total_lines += max_lines;
416 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
421 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
422 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, h0, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
423 data_ny = (unsigned char *)malloc(nw0 * stride0);
424 hscale(npix, data_ny, w0, nh0, nw0, dinfo.comp_info[0].width_in_blocks * DCTSIZE, stride0);
428 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
429 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, h1, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
430 data_ncr = (unsigned char *)malloc(nw1 * stride1);
431 hscale(npix, data_ncr, w1, nh1, nw1, dinfo.comp_info[1].width_in_blocks * DCTSIZE, stride1);
435 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
436 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, h2, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
437 data_ncb = (unsigned char *)malloc(nw2 * stride2);
438 hscale(npix, data_ncb, w2, nh2, nw2, dinfo.comp_info[2].width_in_blocks * DCTSIZE, stride2);
441 jpeg_destroy_decompress(&dinfo);
443 struct jpeg_compress_struct cinfo;
444 cinfo.err = jpeg_std_error(&jerr);
445 jpeg_create_compress(&cinfo);
446 jpeg_stdio_dest(&cinfo, stdout);
447 cinfo.input_components = 3;
448 jpeg_set_defaults(&cinfo);
449 jpeg_set_quality(&cinfo, 85, FALSE);
450 cinfo.image_width = nominal_w;
451 cinfo.image_height = nominal_h;
452 cinfo.raw_data_in = TRUE;
453 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
454 cinfo.comp_info[0].h_samp_factor = samp_h0;
455 cinfo.comp_info[0].v_samp_factor = samp_v0;
456 cinfo.comp_info[1].h_samp_factor = samp_h1;
457 cinfo.comp_info[1].v_samp_factor = samp_v1;
458 cinfo.comp_info[2].h_samp_factor = samp_h2;
459 cinfo.comp_info[2].v_samp_factor = samp_v2;
460 jpeg_start_compress(&cinfo, TRUE);
464 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
465 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
467 JSAMPROW y_row_ptrs[max_lines];
468 JSAMPROW cb_row_ptrs[max_lines];
469 JSAMPROW cr_row_ptrs[max_lines];
470 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
473 for (i = 0; i < max_lines; ++i) {
474 /* simple edge extension */
475 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
479 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
480 if (cbline > nh1 - 1)
483 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
484 if (crline > nh2 - 1)
487 y_row_ptrs[i] = data_ny + yline * stride0;
488 cb_row_ptrs[i] = data_ncb + cbline * stride1;
489 cr_row_ptrs[i] = data_ncr + crline * stride2;
492 total_lines += max_lines;
495 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
497 jpeg_finish_compress(&cinfo);
498 jpeg_destroy_compress(&cinfo);