8 #define CACHE_LINE_FACTOR 16
12 static const double cutoff = 1.220703668e-4; // sqrt(sqrt(eps))
14 if (abs(x) < cutoff) {
15 // For small |x|, use Taylor series instead
16 const double x2 = x * x;
17 const double x4 = x2 * x2;
19 return 1.0 - x2 / 6.0 + x4 / 120.0;
25 double lanczos_tap(double x)
27 if (x < -3.0 || x > 3.0)
30 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
32 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
41 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned dstride)
43 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
45 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
48 double sf = (double)w / (double)nw;
49 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
51 /* calculate the filter */
52 for (x = 0; x < nw; ++x) {
53 int start = ceil(x * sf - support);
54 int end = floor(x * sf + support);
65 // round up so we get a multiple of four for the SSE code
66 int num = (end - start + 1);
68 // prefer aligning it if possible
69 if (start % 4 != 0 && start % 4 <= num % 4) {
80 pd[x].startcoeff = num_coeffs;
82 for (sx = start; sx <= end; ++sx) {
83 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
84 double f = lanczos_tap(nd);
85 if (num_coeffs == size_coeffs) {
87 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
90 coeffs[num_coeffs++] = f;
94 for (sx = start; sx <= end; ++sx) {
95 coeffs[pd[x].startcoeff + sx - start] /= sum;
99 for (y = 0; y < h; ++y) {
100 float *sptr = pix + y*w;
101 unsigned char *dptr = npix + y*dstride;
103 for (x = 0; x < nw; ++x) {
105 static const float low = 0.0, high = 255.0;
108 "xor %%eax, %%eax \n"
110 "movups (%2,%%eax),%%xmm1 \n"
111 "movups (%1,%%eax),%%xmm2 \n"
112 "mulps %%xmm2,%%xmm1 \n"
122 : "r" (&coeffs[pd[x].startcoeff]),
123 "r" (&sptr[pd[x].start]),
124 "r" ((pd[x].end - pd[x].start + 1)/4),
127 : "eax", "xmm1", "xmm2"
133 else if (acc > 255.0)
136 ch = (unsigned char)acc;
139 *dptr++ = (unsigned char)acc;
142 for ( ; x < dstride; ++x) {
148 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
150 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
152 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
155 double sf = (double)h / (double)nh;
156 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
158 /* calculate the filter */
159 for (y = 0; y < nh; ++y) {
160 int start = ceil(y * sf - support);
161 int end = floor(y * sf + support);
173 pd[y].startcoeff = num_coeffs;
175 for (sy = start; sy <= end; ++sy) {
176 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
177 double f = lanczos_tap(nd);
178 if (num_coeffs == size_coeffs) {
180 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
183 coeffs[num_coeffs++] = f;
187 for (sy = start; sy <= end; ++sy) {
188 coeffs[pd[y].startcoeff + sy - start] /= sum;
192 #if CACHE_LINE_FACTOR > 1
193 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
194 unsigned char *sptr = pix + x;
195 float *dptr = npix + x;
196 for (y = 0; y < nh; ++y) {
199 float acc[CACHE_LINE_FACTOR];
200 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
202 float *cf = &coeffs[pd[y].startcoeff];
205 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
206 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
207 acc[i] += sptr[sy * w + i] * *cf;
212 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
217 * xmm0 - xmm3: acc[0..15]
218 * xmm4: current filter coefficient
219 * xmm5, xmm6, xmm7: scratchpad
223 "pxor %%xmm0, %%xmm0 \n"
224 "pxor %%xmm1, %%xmm1 \n"
225 "pxor %%xmm2, %%xmm2 \n"
226 "pxor %%xmm3, %%xmm3 \n"
231 /* a zero is useful during unpacking */
232 "pxor %%xmm4, %%xmm4 \n"
234 /* fetch all 16 source bytes */
235 "movups (%0), %%xmm5 \n"
236 "prefetcht0 (%0,%3,4) \n"
238 /* unpack into words (xmm5, xmm7) */
239 "movaps %%xmm5, %%xmm7 \n"
240 "punpcklbw %%xmm4, %%xmm5 \n"
241 "punpckhbw %%xmm4, %%xmm7 \n"
243 /* unpack xmm5 into dwords (xmm5, xmm6) */
244 "movaps %%xmm5, %%xmm6 \n"
245 "punpcklwd %%xmm4, %%xmm5 \n"
246 "punpckhwd %%xmm4, %%xmm6 \n"
248 /* convert xmm5, xmm6 to floats */
249 "cvtdq2ps %%xmm5, %%xmm5 \n"
250 "cvtdq2ps %%xmm6, %%xmm6 \n"
252 /* fetch the coefficient */
253 "movss (%2), %%xmm4 \n"
254 "shufps $0x0, %%xmm4, %%xmm4 \n"
256 /* do the muls for xmm5 and xmm6 */
257 "mulps %%xmm4, %%xmm5 \n"
258 "mulps %%xmm4, %%xmm6 \n"
259 "addps %%xmm5, %%xmm0 \n"
260 "addps %%xmm6, %%xmm1 \n"
262 /* get the zero back again */
263 "pxor %%xmm4, %%xmm4 \n"
265 /* unpack xmm7 into dwords (xmm7, xmm6) */
266 "movaps %%xmm7, %%xmm6 \n"
267 "punpcklwd %%xmm4, %%xmm7 \n"
268 "punpckhwd %%xmm4, %%xmm6 \n"
270 /* convert xmm7, xmm6 to floats */
271 "cvtdq2ps %%xmm7, %%xmm7 \n"
272 "cvtdq2ps %%xmm6, %%xmm6 \n"
274 /* fetch the coefficient */
275 "movss (%2), %%xmm4 \n"
276 "shufps $0x0, %%xmm4, %%xmm4 \n"
278 /* do the second set of muls */
279 "mulps %%xmm4, %%xmm7 \n"
280 "mulps %%xmm4, %%xmm6 \n"
281 "addps %%xmm7, %%xmm2 \n"
282 "addps %%xmm6, %%xmm3 \n"
284 /* move along, and loop */
290 /* store the values */
291 "movaps %%xmm0, (%4) \n"
292 "movaps %%xmm1, 16(%4) \n"
293 "movaps %%xmm2, 32(%4) \n"
294 "movaps %%xmm3, 48(%4) \n"
296 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
297 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
298 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
299 "r" (w), /* 3: stride */
300 "r" (dptr) /* 4: dstptr base */
301 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
307 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
309 for (x = 0; x < w; ++x) {
311 unsigned char *sptr = pix + x;
312 float *dptr = npix + x;
313 for (y = 0; y < nh; ++y) {
315 float *cf = &coeffs[pd[y].startcoeff];
318 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
319 acc += sptr[sy * w] * *cf++;
328 int main(int argc, char **argv)
330 unsigned nominal_w = atoi(argv[1]);
331 unsigned nominal_h = atoi(argv[2]);
333 unsigned samp_h0 = 2, samp_v0 = 2;
334 unsigned samp_h1 = 1, samp_v1 = 1;
335 unsigned samp_h2 = 1, samp_v2 = 1;
336 unsigned max_samp_h = 2, max_samp_v = 2;
338 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
339 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
340 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
342 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
343 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
344 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
346 struct jpeg_decompress_struct dinfo;
347 struct jpeg_error_mgr jerr;
348 dinfo.err = jpeg_std_error(&jerr);
349 jpeg_create_decompress(&dinfo);
350 jpeg_stdio_src(&dinfo, stdin);
351 jpeg_read_header(&dinfo, TRUE);
352 dinfo.raw_data_out = TRUE;
353 jpeg_start_decompress(&dinfo);
355 fprintf(stderr, "Scaling using Lanczos filter:\n");
356 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);
357 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);
358 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);
360 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
361 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
362 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
363 JSAMPLE *data_ny, *data_ncb, *data_ncr;
365 int total_lines = 0, blocks = 0;
366 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
367 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
369 JSAMPROW y_row_ptrs[max_lines];
370 JSAMPROW cb_row_ptrs[max_lines];
371 JSAMPROW cr_row_ptrs[max_lines];
372 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
375 for (i = 0; i < max_lines; ++i) {
376 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
377 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
378 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
381 total_lines += max_lines;
384 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
389 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
390 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, dinfo.comp_info[0].height_in_blocks * DCTSIZE, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
391 data_ny = (unsigned char *)malloc(nw0 * stride0);
392 hscale(npix, data_ny, dinfo.comp_info[0].width_in_blocks * DCTSIZE, nh0, nw0, stride0);
396 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
397 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, dinfo.comp_info[1].height_in_blocks * DCTSIZE, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
398 data_ncr = (unsigned char *)malloc(nw1 * stride1);
399 hscale(npix, data_ncr, dinfo.comp_info[1].width_in_blocks * DCTSIZE, nh1, nw1, stride1);
403 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
404 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, dinfo.comp_info[2].height_in_blocks * DCTSIZE, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
405 data_ncb = (unsigned char *)malloc(nw2 * stride2);
406 hscale(npix, data_ncb, dinfo.comp_info[2].width_in_blocks * DCTSIZE, nh2, nw2, stride2);
409 jpeg_destroy_decompress(&dinfo);
411 struct jpeg_compress_struct cinfo;
412 cinfo.err = jpeg_std_error(&jerr);
413 jpeg_create_compress(&cinfo);
414 jpeg_stdio_dest(&cinfo, stdout);
415 cinfo.input_components = 3;
416 jpeg_set_defaults(&cinfo);
417 jpeg_set_quality(&cinfo, 85, FALSE);
418 cinfo.image_width = nominal_w;
419 cinfo.image_height = nominal_h;
420 cinfo.raw_data_in = TRUE;
421 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
422 cinfo.comp_info[0].h_samp_factor = samp_h0;
423 cinfo.comp_info[0].v_samp_factor = samp_v0;
424 cinfo.comp_info[1].h_samp_factor = samp_h1;
425 cinfo.comp_info[1].v_samp_factor = samp_v1;
426 cinfo.comp_info[2].h_samp_factor = samp_h2;
427 cinfo.comp_info[2].v_samp_factor = samp_v2;
428 jpeg_start_compress(&cinfo, TRUE);
432 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
433 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
435 JSAMPROW y_row_ptrs[max_lines];
436 JSAMPROW cb_row_ptrs[max_lines];
437 JSAMPROW cr_row_ptrs[max_lines];
438 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
441 for (i = 0; i < max_lines; ++i) {
442 // simple edge extension
443 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
447 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
448 if (cbline > nh1 - 1)
451 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
452 if (crline > nh2 - 1)
455 y_row_ptrs[i] = data_ny + yline * stride0;
456 cb_row_ptrs[i] = data_ncb + cbline * stride1;
457 cr_row_ptrs[i] = data_ncr + crline * stride2;
460 total_lines += max_lines;
463 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
465 jpeg_finish_compress(&cinfo);
466 jpeg_destroy_compress(&cinfo);