8 #define CACHE_LINE_FACTOR 16
12 // This is bad for very small x, should use power series instead.
19 double lanczos_tap(double x)
21 if (x < -3.0 || x > 3.0)
24 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
26 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
35 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned dstride)
37 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
39 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
42 double sf = (double)w / (double)nw;
43 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
45 /* calculate the filter */
46 for (x = 0; x < nw; ++x) {
47 int start = ceil(x * sf - support);
48 int end = floor(x * sf + support);
59 // round up so we get a multiple of four for the SSE code
60 int num = (end - start + 1);
62 // prefer aligning it if possible
63 if (start % 4 != 0 && start % 4 <= num % 4) {
74 pd[x].startcoeff = num_coeffs;
76 for (sx = start; sx <= end; ++sx) {
77 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
78 double f = lanczos_tap(nd);
79 if (num_coeffs == size_coeffs) {
81 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
84 coeffs[num_coeffs++] = f;
88 for (sx = start; sx <= end; ++sx) {
89 coeffs[pd[x].startcoeff + sx - start] /= sum;
93 for (y = 0; y < h; ++y) {
94 float *sptr = pix + y*w;
95 unsigned char *dptr = npix + y*dstride;
97 for (x = 0; x < nw; ++x) {
99 static const float low = 0.0, high = 255.0;
102 "xor %%eax, %%eax \n"
104 "movups (%2,%%eax),%%xmm1 \n"
105 "movups (%1,%%eax),%%xmm2 \n"
106 "mulps %%xmm2,%%xmm1 \n"
116 : "r" (&coeffs[pd[x].startcoeff]),
117 "r" (&sptr[pd[x].start]),
118 "r" ((pd[x].end - pd[x].start + 1)/4),
121 : "eax", "xmm1", "xmm2"
127 else if (acc > 255.0)
130 ch = (unsigned char)acc;
133 *dptr++ = (unsigned char)acc;
136 for ( ; x < dstride; ++x) {
142 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
144 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
146 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
149 double sf = (double)h / (double)nh;
150 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
152 /* calculate the filter */
153 for (y = 0; y < nh; ++y) {
154 int start = ceil(y * sf - support);
155 int end = floor(y * sf + support);
167 pd[y].startcoeff = num_coeffs;
169 for (sy = start; sy <= end; ++sy) {
170 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
171 double f = lanczos_tap(nd);
172 if (num_coeffs == size_coeffs) {
174 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
177 coeffs[num_coeffs++] = f;
181 for (sy = start; sy <= end; ++sy) {
182 coeffs[pd[y].startcoeff + sy - start] /= sum;
186 #if CACHE_LINE_FACTOR > 1
187 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
188 unsigned char *sptr = pix + x;
189 float *dptr = npix + x;
190 for (y = 0; y < nh; ++y) {
193 float acc[CACHE_LINE_FACTOR];
194 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
196 float *cf = &coeffs[pd[y].startcoeff];
199 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
200 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
201 acc[i] += sptr[sy * w + i] * *cf;
206 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
211 * xmm0 - xmm3: acc[0..15]
212 * xmm4: current filter coefficient
213 * xmm5, xmm6, xmm7: scratchpad
217 "pxor %%xmm0, %%xmm0 \n"
218 "pxor %%xmm1, %%xmm1 \n"
219 "pxor %%xmm2, %%xmm2 \n"
220 "pxor %%xmm3, %%xmm3 \n"
225 /* a zero is useful during unpacking */
226 "pxor %%xmm4, %%xmm4 \n"
228 /* fetch all 16 source bytes */
229 "movups (%0), %%xmm5 \n"
230 "prefetcht0 (%0,%3,4) \n"
232 /* unpack into words (xmm5, xmm7) */
233 "movaps %%xmm5, %%xmm7 \n"
234 "punpcklbw %%xmm4, %%xmm5 \n"
235 "punpckhbw %%xmm4, %%xmm7 \n"
237 /* unpack xmm5 into dwords (xmm5, xmm6) */
238 "movaps %%xmm5, %%xmm6 \n"
239 "punpcklwd %%xmm4, %%xmm5 \n"
240 "punpckhwd %%xmm4, %%xmm6 \n"
242 /* convert xmm5, xmm6 to floats */
243 "cvtdq2ps %%xmm5, %%xmm5 \n"
244 "cvtdq2ps %%xmm6, %%xmm6 \n"
246 /* fetch the coefficient */
247 "movss (%2), %%xmm4 \n"
248 "shufps $0x0, %%xmm4, %%xmm4 \n"
250 /* do the muls for xmm5 and xmm6 */
251 "mulps %%xmm4, %%xmm5 \n"
252 "mulps %%xmm4, %%xmm6 \n"
253 "addps %%xmm5, %%xmm0 \n"
254 "addps %%xmm6, %%xmm1 \n"
256 /* get the zero back again */
257 "pxor %%xmm4, %%xmm4 \n"
259 /* unpack xmm7 into dwords (xmm7, xmm6) */
260 "movaps %%xmm7, %%xmm6 \n"
261 "punpcklwd %%xmm4, %%xmm7 \n"
262 "punpckhwd %%xmm4, %%xmm6 \n"
264 /* convert xmm7, xmm6 to floats */
265 "cvtdq2ps %%xmm7, %%xmm7 \n"
266 "cvtdq2ps %%xmm6, %%xmm6 \n"
268 /* fetch the coefficient */
269 "movss (%2), %%xmm4 \n"
270 "shufps $0x0, %%xmm4, %%xmm4 \n"
272 /* do the second set of muls */
273 "mulps %%xmm4, %%xmm7 \n"
274 "mulps %%xmm4, %%xmm6 \n"
275 "addps %%xmm7, %%xmm2 \n"
276 "addps %%xmm6, %%xmm3 \n"
278 /* move along, and loop */
284 /* store the values */
285 "movaps %%xmm0, (%4) \n"
286 "movaps %%xmm1, 16(%4) \n"
287 "movaps %%xmm2, 32(%4) \n"
288 "movaps %%xmm3, 48(%4) \n"
290 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
291 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
292 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
293 "r" (w), /* 3: stride */
294 "r" (dptr) /* 4: dstptr base */
295 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
301 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
303 for (x = 0; x < w; ++x) {
305 unsigned char *sptr = pix + x;
306 float *dptr = npix + x;
307 for (y = 0; y < nh; ++y) {
309 float *cf = &coeffs[pd[y].startcoeff];
312 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
313 acc += sptr[sy * w] * *cf++;
322 int main(int argc, char **argv)
324 unsigned nominal_w = atoi(argv[1]);
325 unsigned nominal_h = atoi(argv[2]);
327 unsigned samp_h0 = 2, samp_v0 = 2;
328 unsigned samp_h1 = 1, samp_v1 = 1;
329 unsigned samp_h2 = 1, samp_v2 = 1;
330 unsigned max_samp_h = 2, max_samp_v = 2;
332 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
333 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
334 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
336 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
337 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
338 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
340 struct jpeg_decompress_struct dinfo;
341 struct jpeg_error_mgr jerr;
342 dinfo.err = jpeg_std_error(&jerr);
343 jpeg_create_decompress(&dinfo);
344 jpeg_stdio_src(&dinfo, stdin);
345 jpeg_read_header(&dinfo, TRUE);
346 dinfo.raw_data_out = TRUE;
347 jpeg_start_decompress(&dinfo);
349 fprintf(stderr, "Scaling using Lanczos filter:\n");
350 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);
351 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);
352 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);
354 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
355 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
356 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
357 JSAMPLE *data_ny, *data_ncb, *data_ncr;
359 int total_lines = 0, blocks = 0;
360 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
361 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
363 JSAMPROW y_row_ptrs[max_lines];
364 JSAMPROW cb_row_ptrs[max_lines];
365 JSAMPROW cr_row_ptrs[max_lines];
366 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
369 for (i = 0; i < max_lines; ++i) {
370 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
371 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
372 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
375 total_lines += max_lines;
378 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
383 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
384 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);
385 data_ny = (unsigned char *)malloc(nw0 * stride0);
386 hscale(npix, data_ny, dinfo.comp_info[0].width_in_blocks * DCTSIZE, nh0, nw0, stride0);
390 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
391 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);
392 data_ncr = (unsigned char *)malloc(nw1 * stride1);
393 hscale(npix, data_ncr, dinfo.comp_info[1].width_in_blocks * DCTSIZE, nh1, nw1, stride1);
397 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
398 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);
399 data_ncb = (unsigned char *)malloc(nw2 * stride2);
400 hscale(npix, data_ncb, dinfo.comp_info[2].width_in_blocks * DCTSIZE, nh2, nw2, stride2);
403 jpeg_destroy_decompress(&dinfo);
405 struct jpeg_compress_struct cinfo;
406 cinfo.err = jpeg_std_error(&jerr);
407 jpeg_create_compress(&cinfo);
408 jpeg_stdio_dest(&cinfo, stdout);
409 cinfo.input_components = 3;
410 jpeg_set_defaults(&cinfo);
411 jpeg_set_quality(&cinfo, 85, FALSE);
412 cinfo.image_width = nominal_w;
413 cinfo.image_height = nominal_h;
414 cinfo.raw_data_in = TRUE;
415 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
416 cinfo.comp_info[0].h_samp_factor = samp_h0;
417 cinfo.comp_info[0].v_samp_factor = samp_v0;
418 cinfo.comp_info[1].h_samp_factor = samp_h1;
419 cinfo.comp_info[1].v_samp_factor = samp_v1;
420 cinfo.comp_info[2].h_samp_factor = samp_h2;
421 cinfo.comp_info[2].v_samp_factor = samp_v2;
422 jpeg_start_compress(&cinfo, TRUE);
426 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
427 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
429 JSAMPROW y_row_ptrs[max_lines];
430 JSAMPROW cb_row_ptrs[max_lines];
431 JSAMPROW cr_row_ptrs[max_lines];
432 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
435 for (i = 0; i < max_lines; ++i) {
436 // simple edge extension
437 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
441 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
442 if (cbline > nh1 - 1)
445 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
446 if (crline > nh2 - 1)
449 y_row_ptrs[i] = data_ny + yline * stride0;
450 cb_row_ptrs[i] = data_ncb + cbline * stride1;
451 cr_row_ptrs[i] = data_ncr + crline * stride2;
454 total_lines += max_lines;
457 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
459 jpeg_finish_compress(&cinfo);
460 jpeg_destroy_compress(&cinfo);