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) {
107 static const float low = 0.0, high = 255.0;
112 "movups (%4,%2),%%xmm1 \n"
113 "movups (%3,%2),%%xmm2 \n"
114 "mulps %%xmm2,%%xmm1 \n"
127 : "r" (&coeffs[pd[x].startcoeff]),
128 "r" (&sptr[pd[x].start]),
129 "r" ((pd[x].end - pd[x].start + 1)/4),
132 : "memory", "xmm1", "xmm2"
138 else if (acc > 255.0)
141 ch = (unsigned char)acc;
144 *dptr++ = (unsigned char)result;
147 for ( ; x < dstride; ++x) {
153 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
155 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
157 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
160 double sf = (double)h / (double)nh;
161 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
163 /* calculate the filter */
164 for (y = 0; y < nh; ++y) {
165 int start = ceil(y * sf - support);
166 int end = floor(y * sf + support);
178 pd[y].startcoeff = num_coeffs;
180 for (sy = start; sy <= end; ++sy) {
181 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
182 double f = lanczos_tap(nd);
183 if (num_coeffs == size_coeffs) {
185 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
188 coeffs[num_coeffs++] = f;
192 for (sy = start; sy <= end; ++sy) {
193 coeffs[pd[y].startcoeff + sy - start] /= sum;
197 #if CACHE_LINE_FACTOR > 1
198 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
199 unsigned char *sptr = pix + x;
200 float *dptr = npix + x;
201 for (y = 0; y < nh; ++y) {
204 float acc[CACHE_LINE_FACTOR];
205 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
207 float *cf = &coeffs[pd[y].startcoeff];
210 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
211 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
212 acc[i] += sptr[sy * w + i] * *cf;
217 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
222 * xmm0 - xmm3: acc[0..15]
223 * xmm4: current filter coefficient
224 * xmm5, xmm6, xmm7: scratchpad
228 "pxor %%xmm0, %%xmm0 \n"
229 "pxor %%xmm1, %%xmm1 \n"
230 "pxor %%xmm2, %%xmm2 \n"
231 "pxor %%xmm3, %%xmm3 \n"
236 /* a zero is useful during unpacking */
237 "pxor %%xmm4, %%xmm4 \n"
239 /* fetch all 16 source bytes */
240 "movups (%0), %%xmm5 \n"
241 "prefetcht0 (%0,%3,4) \n"
243 /* unpack into words (xmm5, xmm7) */
244 "movaps %%xmm5, %%xmm7 \n"
245 "punpcklbw %%xmm4, %%xmm5 \n"
246 "punpckhbw %%xmm4, %%xmm7 \n"
248 /* unpack xmm5 into dwords (xmm5, xmm6) */
249 "movaps %%xmm5, %%xmm6 \n"
250 "punpcklwd %%xmm4, %%xmm5 \n"
251 "punpckhwd %%xmm4, %%xmm6 \n"
253 /* convert xmm5, xmm6 to floats */
254 "cvtdq2ps %%xmm5, %%xmm5 \n"
255 "cvtdq2ps %%xmm6, %%xmm6 \n"
257 /* fetch the coefficient */
258 "movss (%2), %%xmm4 \n"
259 "shufps $0x0, %%xmm4, %%xmm4 \n"
261 /* do the muls for xmm5 and xmm6 */
262 "mulps %%xmm4, %%xmm5 \n"
263 "mulps %%xmm4, %%xmm6 \n"
264 "addps %%xmm5, %%xmm0 \n"
265 "addps %%xmm6, %%xmm1 \n"
267 /* get the zero back again */
268 "pxor %%xmm4, %%xmm4 \n"
270 /* unpack xmm7 into dwords (xmm7, xmm6) */
271 "movaps %%xmm7, %%xmm6 \n"
272 "punpcklwd %%xmm4, %%xmm7 \n"
273 "punpckhwd %%xmm4, %%xmm6 \n"
275 /* convert xmm7, xmm6 to floats */
276 "cvtdq2ps %%xmm7, %%xmm7 \n"
277 "cvtdq2ps %%xmm6, %%xmm6 \n"
279 /* fetch the coefficient */
280 "movss (%2), %%xmm4 \n"
281 "shufps $0x0, %%xmm4, %%xmm4 \n"
283 /* do the second set of muls */
284 "mulps %%xmm4, %%xmm7 \n"
285 "mulps %%xmm4, %%xmm6 \n"
286 "addps %%xmm7, %%xmm2 \n"
287 "addps %%xmm6, %%xmm3 \n"
289 /* move along, and loop */
295 /* store the values */
296 "movaps %%xmm0, (%4) \n"
297 "movaps %%xmm1, 16(%4) \n"
298 "movaps %%xmm2, 32(%4) \n"
299 "movaps %%xmm3, 48(%4) \n"
301 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
302 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
303 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
304 "r" ((long)w), /* 3: stride */
305 "r" (dptr) /* 4: dstptr base */
306 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
312 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
314 for (x = 0; x < w; ++x) {
316 unsigned char *sptr = pix + x;
317 float *dptr = npix + x;
318 for (y = 0; y < nh; ++y) {
320 float *cf = &coeffs[pd[y].startcoeff];
323 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
324 acc += sptr[sy * w] * *cf++;
333 int main(int argc, char **argv)
335 unsigned nominal_w = atoi(argv[1]);
336 unsigned nominal_h = atoi(argv[2]);
338 unsigned samp_h0 = 2, samp_v0 = 2;
339 unsigned samp_h1 = 1, samp_v1 = 1;
340 unsigned samp_h2 = 1, samp_v2 = 1;
341 unsigned max_samp_h = 2, max_samp_v = 2;
343 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
344 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
345 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
347 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
348 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
349 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
351 struct jpeg_decompress_struct dinfo;
352 struct jpeg_error_mgr jerr;
353 dinfo.err = jpeg_std_error(&jerr);
354 jpeg_create_decompress(&dinfo);
355 jpeg_stdio_src(&dinfo, stdin);
356 jpeg_read_header(&dinfo, TRUE);
357 dinfo.raw_data_out = TRUE;
358 jpeg_start_decompress(&dinfo);
360 unsigned w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
361 unsigned h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
363 unsigned w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
364 unsigned h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
366 unsigned w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
367 unsigned h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
369 fprintf(stderr, "Scaling using Lanczos filter:\n");
370 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);
371 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);
372 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);
374 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
375 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
376 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
377 JSAMPLE *data_ny, *data_ncb, *data_ncr;
379 int total_lines = 0, blocks = 0;
380 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
381 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
383 JSAMPROW y_row_ptrs[max_lines];
384 JSAMPROW cb_row_ptrs[max_lines];
385 JSAMPROW cr_row_ptrs[max_lines];
386 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
389 for (i = 0; i < max_lines; ++i) {
390 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
391 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
392 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
395 total_lines += max_lines;
398 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
403 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
404 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, h0, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
405 data_ny = (unsigned char *)malloc(nw0 * stride0);
406 hscale(npix, data_ny, w0, nh0, nw0, dinfo.comp_info[0].width_in_blocks * DCTSIZE, stride0);
410 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
411 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, h1, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
412 data_ncr = (unsigned char *)malloc(nw1 * stride1);
413 hscale(npix, data_ncr, w1, nh1, nw1, dinfo.comp_info[1].width_in_blocks * DCTSIZE, stride1);
417 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
418 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, h2, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
419 data_ncb = (unsigned char *)malloc(nw2 * stride2);
420 hscale(npix, data_ncb, w2, nh2, nw2, dinfo.comp_info[2].width_in_blocks * DCTSIZE, stride2);
423 jpeg_destroy_decompress(&dinfo);
425 struct jpeg_compress_struct cinfo;
426 cinfo.err = jpeg_std_error(&jerr);
427 jpeg_create_compress(&cinfo);
428 jpeg_stdio_dest(&cinfo, stdout);
429 cinfo.input_components = 3;
430 jpeg_set_defaults(&cinfo);
431 jpeg_set_quality(&cinfo, 85, FALSE);
432 cinfo.image_width = nominal_w;
433 cinfo.image_height = nominal_h;
434 cinfo.raw_data_in = TRUE;
435 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
436 cinfo.comp_info[0].h_samp_factor = samp_h0;
437 cinfo.comp_info[0].v_samp_factor = samp_v0;
438 cinfo.comp_info[1].h_samp_factor = samp_h1;
439 cinfo.comp_info[1].v_samp_factor = samp_v1;
440 cinfo.comp_info[2].h_samp_factor = samp_h2;
441 cinfo.comp_info[2].v_samp_factor = samp_v2;
442 jpeg_start_compress(&cinfo, TRUE);
446 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
447 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
449 JSAMPROW y_row_ptrs[max_lines];
450 JSAMPROW cb_row_ptrs[max_lines];
451 JSAMPROW cr_row_ptrs[max_lines];
452 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
455 for (i = 0; i < max_lines; ++i) {
456 // simple edge extension
457 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
461 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
462 if (cbline > nh1 - 1)
465 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
466 if (crline > nh2 - 1)
469 y_row_ptrs[i] = data_ny + yline * stride0;
470 cb_row_ptrs[i] = data_ncb + cbline * stride1;
471 cr_row_ptrs[i] = data_ncr + crline * stride2;
474 total_lines += max_lines;
477 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
479 jpeg_finish_compress(&cinfo);
480 jpeg_destroy_compress(&cinfo);