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 unsigned nominal_w = atoi(argv[1]);
340 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 max_samp_h = 2, max_samp_v = 2;
347 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
348 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
349 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
351 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
352 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
353 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
355 struct jpeg_decompress_struct dinfo;
356 struct jpeg_error_mgr jerr;
357 dinfo.err = jpeg_std_error(&jerr);
358 jpeg_create_decompress(&dinfo);
359 jpeg_stdio_src(&dinfo, stdin);
360 jpeg_read_header(&dinfo, TRUE);
361 dinfo.raw_data_out = TRUE;
362 jpeg_start_decompress(&dinfo);
364 unsigned w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
365 unsigned h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
367 unsigned w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
368 unsigned h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
370 unsigned w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
371 unsigned h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
373 fprintf(stderr, "Scaling using Lanczos filter:\n");
374 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);
375 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);
376 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);
378 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
379 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
380 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
381 JSAMPLE *data_ny, *data_ncb, *data_ncr;
383 int total_lines = 0, blocks = 0;
384 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
385 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
387 JSAMPROW y_row_ptrs[max_lines];
388 JSAMPROW cb_row_ptrs[max_lines];
389 JSAMPROW cr_row_ptrs[max_lines];
390 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
393 for (i = 0; i < max_lines; ++i) {
394 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
395 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
396 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
399 total_lines += max_lines;
402 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
407 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
408 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, h0, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
409 data_ny = (unsigned char *)malloc(nw0 * stride0);
410 hscale(npix, data_ny, w0, nh0, nw0, dinfo.comp_info[0].width_in_blocks * DCTSIZE, stride0);
414 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
415 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, h1, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
416 data_ncr = (unsigned char *)malloc(nw1 * stride1);
417 hscale(npix, data_ncr, w1, nh1, nw1, dinfo.comp_info[1].width_in_blocks * DCTSIZE, stride1);
421 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
422 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, h2, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
423 data_ncb = (unsigned char *)malloc(nw2 * stride2);
424 hscale(npix, data_ncb, w2, nh2, nw2, dinfo.comp_info[2].width_in_blocks * DCTSIZE, stride2);
427 jpeg_destroy_decompress(&dinfo);
429 struct jpeg_compress_struct cinfo;
430 cinfo.err = jpeg_std_error(&jerr);
431 jpeg_create_compress(&cinfo);
432 jpeg_stdio_dest(&cinfo, stdout);
433 cinfo.input_components = 3;
434 jpeg_set_defaults(&cinfo);
435 jpeg_set_quality(&cinfo, 85, FALSE);
436 cinfo.image_width = nominal_w;
437 cinfo.image_height = nominal_h;
438 cinfo.raw_data_in = TRUE;
439 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
440 cinfo.comp_info[0].h_samp_factor = samp_h0;
441 cinfo.comp_info[0].v_samp_factor = samp_v0;
442 cinfo.comp_info[1].h_samp_factor = samp_h1;
443 cinfo.comp_info[1].v_samp_factor = samp_v1;
444 cinfo.comp_info[2].h_samp_factor = samp_h2;
445 cinfo.comp_info[2].v_samp_factor = samp_v2;
446 jpeg_start_compress(&cinfo, TRUE);
450 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
451 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
453 JSAMPROW y_row_ptrs[max_lines];
454 JSAMPROW cb_row_ptrs[max_lines];
455 JSAMPROW cr_row_ptrs[max_lines];
456 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
459 for (i = 0; i < max_lines; ++i) {
460 /* simple edge extension */
461 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
465 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
466 if (cbline > nh1 - 1)
469 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
470 if (crline > nh2 - 1)
473 y_row_ptrs[i] = data_ny + yline * stride0;
474 cb_row_ptrs[i] = data_ncb + cbline * stride1;
475 cr_row_ptrs[i] = data_ncr + crline * stride2;
478 total_lines += max_lines;
481 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
483 jpeg_finish_compress(&cinfo);
484 jpeg_destroy_compress(&cinfo);