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 sstride, 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*sstride;
101 unsigned char *dptr = npix + y*dstride;
103 for (x = 0; x < nw; ++x) {
106 static const float low = 0.0, high = 255.0;
111 "movups (%3,%1),%%xmm1 \n"
112 "movups (%2,%1),%%xmm2 \n"
113 "mulps %%xmm2,%%xmm1 \n"
124 : "r" (&coeffs[pd[x].startcoeff]),
125 "r" (&sptr[pd[x].start]),
126 "r" ((pd[x].end - pd[x].start + 1)/4),
135 else if (acc > 255.0)
138 ch = (unsigned char)acc;
141 *dptr++ = (unsigned char)acc;
144 for ( ; x < dstride; ++x) {
150 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
152 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
154 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
157 double sf = (double)h / (double)nh;
158 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
160 /* calculate the filter */
161 for (y = 0; y < nh; ++y) {
162 int start = ceil(y * sf - support);
163 int end = floor(y * sf + support);
175 pd[y].startcoeff = num_coeffs;
177 for (sy = start; sy <= end; ++sy) {
178 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
179 double f = lanczos_tap(nd);
180 if (num_coeffs == size_coeffs) {
182 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
185 coeffs[num_coeffs++] = f;
189 for (sy = start; sy <= end; ++sy) {
190 coeffs[pd[y].startcoeff + sy - start] /= sum;
194 #if CACHE_LINE_FACTOR > 1
195 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
196 unsigned char *sptr = pix + x;
197 float *dptr = npix + x;
198 for (y = 0; y < nh; ++y) {
201 float acc[CACHE_LINE_FACTOR];
202 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
204 float *cf = &coeffs[pd[y].startcoeff];
207 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
208 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
209 acc[i] += sptr[sy * w + i] * *cf;
214 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
219 * xmm0 - xmm3: acc[0..15]
220 * xmm4: current filter coefficient
221 * xmm5, xmm6, xmm7: scratchpad
225 "pxor %%xmm0, %%xmm0 \n"
226 "pxor %%xmm1, %%xmm1 \n"
227 "pxor %%xmm2, %%xmm2 \n"
228 "pxor %%xmm3, %%xmm3 \n"
233 /* a zero is useful during unpacking */
234 "pxor %%xmm4, %%xmm4 \n"
236 /* fetch all 16 source bytes */
237 "movups (%0), %%xmm5 \n"
238 "prefetcht0 (%0,%3,4) \n"
240 /* unpack into words (xmm5, xmm7) */
241 "movaps %%xmm5, %%xmm7 \n"
242 "punpcklbw %%xmm4, %%xmm5 \n"
243 "punpckhbw %%xmm4, %%xmm7 \n"
245 /* unpack xmm5 into dwords (xmm5, xmm6) */
246 "movaps %%xmm5, %%xmm6 \n"
247 "punpcklwd %%xmm4, %%xmm5 \n"
248 "punpckhwd %%xmm4, %%xmm6 \n"
250 /* convert xmm5, xmm6 to floats */
251 "cvtdq2ps %%xmm5, %%xmm5 \n"
252 "cvtdq2ps %%xmm6, %%xmm6 \n"
254 /* fetch the coefficient */
255 "movss (%2), %%xmm4 \n"
256 "shufps $0x0, %%xmm4, %%xmm4 \n"
258 /* do the muls for xmm5 and xmm6 */
259 "mulps %%xmm4, %%xmm5 \n"
260 "mulps %%xmm4, %%xmm6 \n"
261 "addps %%xmm5, %%xmm0 \n"
262 "addps %%xmm6, %%xmm1 \n"
264 /* get the zero back again */
265 "pxor %%xmm4, %%xmm4 \n"
267 /* unpack xmm7 into dwords (xmm7, xmm6) */
268 "movaps %%xmm7, %%xmm6 \n"
269 "punpcklwd %%xmm4, %%xmm7 \n"
270 "punpckhwd %%xmm4, %%xmm6 \n"
272 /* convert xmm7, xmm6 to floats */
273 "cvtdq2ps %%xmm7, %%xmm7 \n"
274 "cvtdq2ps %%xmm6, %%xmm6 \n"
276 /* fetch the coefficient */
277 "movss (%2), %%xmm4 \n"
278 "shufps $0x0, %%xmm4, %%xmm4 \n"
280 /* do the second set of muls */
281 "mulps %%xmm4, %%xmm7 \n"
282 "mulps %%xmm4, %%xmm6 \n"
283 "addps %%xmm7, %%xmm2 \n"
284 "addps %%xmm6, %%xmm3 \n"
286 /* move along, and loop */
292 /* store the values */
293 "movaps %%xmm0, (%4) \n"
294 "movaps %%xmm1, 16(%4) \n"
295 "movaps %%xmm2, 32(%4) \n"
296 "movaps %%xmm3, 48(%4) \n"
298 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
299 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
300 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
301 "r" (w), /* 3: stride */
302 "r" (dptr) /* 4: dstptr base */
303 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
309 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
311 for (x = 0; x < w; ++x) {
313 unsigned char *sptr = pix + x;
314 float *dptr = npix + x;
315 for (y = 0; y < nh; ++y) {
317 float *cf = &coeffs[pd[y].startcoeff];
320 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
321 acc += sptr[sy * w] * *cf++;
330 int main(int argc, char **argv)
332 unsigned nominal_w = atoi(argv[1]);
333 unsigned nominal_h = atoi(argv[2]);
335 unsigned samp_h0 = 2, samp_v0 = 2;
336 unsigned samp_h1 = 1, samp_v1 = 1;
337 unsigned samp_h2 = 1, samp_v2 = 1;
338 unsigned max_samp_h = 2, max_samp_v = 2;
340 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
341 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
342 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
344 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
345 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
346 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
348 struct jpeg_decompress_struct dinfo;
349 struct jpeg_error_mgr jerr;
350 dinfo.err = jpeg_std_error(&jerr);
351 jpeg_create_decompress(&dinfo);
352 jpeg_stdio_src(&dinfo, stdin);
353 jpeg_read_header(&dinfo, TRUE);
354 dinfo.raw_data_out = TRUE;
355 jpeg_start_decompress(&dinfo);
357 unsigned w0 = dinfo.image_width * samp_h0 / max_samp_h, h0 = dinfo.image_height * samp_v0 / max_samp_v;
358 unsigned w1 = dinfo.image_width * samp_h1 / max_samp_h, h1 = dinfo.image_height * samp_v1 / max_samp_v;
359 unsigned w2 = dinfo.image_width * samp_h2 / max_samp_h, h2 = dinfo.image_height * samp_v2 / max_samp_v;
361 fprintf(stderr, "Scaling using Lanczos filter:\n");
362 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);
363 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);
364 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);
366 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
367 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
368 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
369 JSAMPLE *data_ny, *data_ncb, *data_ncr;
371 int total_lines = 0, blocks = 0;
372 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
373 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
375 JSAMPROW y_row_ptrs[max_lines];
376 JSAMPROW cb_row_ptrs[max_lines];
377 JSAMPROW cr_row_ptrs[max_lines];
378 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
381 for (i = 0; i < max_lines; ++i) {
382 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
383 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
384 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
387 total_lines += max_lines;
390 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
395 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
396 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, h0, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
397 data_ny = (unsigned char *)malloc(nw0 * stride0);
398 hscale(npix, data_ny, w0, nh0, nw0, dinfo.comp_info[0].width_in_blocks * DCTSIZE, stride0);
402 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
403 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, h1, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
404 data_ncr = (unsigned char *)malloc(nw1 * stride1);
405 hscale(npix, data_ncr, w1, nh1, nw1, dinfo.comp_info[1].width_in_blocks * DCTSIZE, stride1);
409 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
410 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, h2, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
411 data_ncb = (unsigned char *)malloc(nw2 * stride2);
412 hscale(npix, data_ncb, w2, nh2, nw2, dinfo.comp_info[2].width_in_blocks * DCTSIZE, stride2);
415 jpeg_destroy_decompress(&dinfo);
417 struct jpeg_compress_struct cinfo;
418 cinfo.err = jpeg_std_error(&jerr);
419 jpeg_create_compress(&cinfo);
420 jpeg_stdio_dest(&cinfo, stdout);
421 cinfo.input_components = 3;
422 jpeg_set_defaults(&cinfo);
423 jpeg_set_quality(&cinfo, 85, FALSE);
424 cinfo.image_width = nominal_w;
425 cinfo.image_height = nominal_h;
426 cinfo.raw_data_in = TRUE;
427 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
428 cinfo.comp_info[0].h_samp_factor = samp_h0;
429 cinfo.comp_info[0].v_samp_factor = samp_v0;
430 cinfo.comp_info[1].h_samp_factor = samp_h1;
431 cinfo.comp_info[1].v_samp_factor = samp_v1;
432 cinfo.comp_info[2].h_samp_factor = samp_h2;
433 cinfo.comp_info[2].v_samp_factor = samp_v2;
434 jpeg_start_compress(&cinfo, TRUE);
438 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
439 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
441 JSAMPROW y_row_ptrs[max_lines];
442 JSAMPROW cb_row_ptrs[max_lines];
443 JSAMPROW cr_row_ptrs[max_lines];
444 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
447 for (i = 0; i < max_lines; ++i) {
448 // simple edge extension
449 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
453 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
454 if (cbline > nh1 - 1)
457 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
458 if (crline > nh2 - 1)
461 y_row_ptrs[i] = data_ny + yline * stride0;
462 cb_row_ptrs[i] = data_ncb + cbline * stride1;
463 cr_row_ptrs[i] = data_ncr + crline * stride2;
466 total_lines += max_lines;
469 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
471 jpeg_finish_compress(&cinfo);
472 jpeg_destroy_compress(&cinfo);