8 #define CACHE_LINE_FACTOR 16
12 static const double eps = 2.22045e-016;
13 static const double cutoff = sqrt(sqrt(eps));
15 if (abs(x) < cutoff) {
16 // For small |x|, use Taylor series instead
17 const double x2 = x * x;
18 const double x4 = x2 * x2;
20 return 1.0 - x2 / 6.0 + x4 / 120.0;
26 double lanczos_tap(double x)
28 if (x < -3.0 || x > 3.0)
31 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
33 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
42 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned dstride)
44 struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
46 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
49 double sf = (double)w / (double)nw;
50 double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
52 /* calculate the filter */
53 for (x = 0; x < nw; ++x) {
54 int start = ceil(x * sf - support);
55 int end = floor(x * sf + support);
66 // round up so we get a multiple of four for the SSE code
67 int num = (end - start + 1);
69 // prefer aligning it if possible
70 if (start % 4 != 0 && start % 4 <= num % 4) {
81 pd[x].startcoeff = num_coeffs;
83 for (sx = start; sx <= end; ++sx) {
84 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
85 double f = lanczos_tap(nd);
86 if (num_coeffs == size_coeffs) {
88 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
91 coeffs[num_coeffs++] = f;
95 for (sx = start; sx <= end; ++sx) {
96 coeffs[pd[x].startcoeff + sx - start] /= sum;
100 for (y = 0; y < h; ++y) {
101 float *sptr = pix + y*w;
102 unsigned char *dptr = npix + y*dstride;
104 for (x = 0; x < nw; ++x) {
106 static const float low = 0.0, high = 255.0;
109 "xor %%eax, %%eax \n"
111 "movups (%2,%%eax),%%xmm1 \n"
112 "movups (%1,%%eax),%%xmm2 \n"
113 "mulps %%xmm2,%%xmm1 \n"
123 : "r" (&coeffs[pd[x].startcoeff]),
124 "r" (&sptr[pd[x].start]),
125 "r" ((pd[x].end - pd[x].start + 1)/4),
128 : "eax", "xmm1", "xmm2"
134 else if (acc > 255.0)
137 ch = (unsigned char)acc;
140 *dptr++ = (unsigned char)acc;
143 for ( ; x < dstride; ++x) {
149 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
151 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
153 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
156 double sf = (double)h / (double)nh;
157 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
159 /* calculate the filter */
160 for (y = 0; y < nh; ++y) {
161 int start = ceil(y * sf - support);
162 int end = floor(y * sf + support);
174 pd[y].startcoeff = num_coeffs;
176 for (sy = start; sy <= end; ++sy) {
177 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
178 double f = lanczos_tap(nd);
179 if (num_coeffs == size_coeffs) {
181 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
184 coeffs[num_coeffs++] = f;
188 for (sy = start; sy <= end; ++sy) {
189 coeffs[pd[y].startcoeff + sy - start] /= sum;
193 #if CACHE_LINE_FACTOR > 1
194 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
195 unsigned char *sptr = pix + x;
196 float *dptr = npix + x;
197 for (y = 0; y < nh; ++y) {
200 float acc[CACHE_LINE_FACTOR];
201 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
203 float *cf = &coeffs[pd[y].startcoeff];
206 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
207 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
208 acc[i] += sptr[sy * w + i] * *cf;
213 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
218 * xmm0 - xmm3: acc[0..15]
219 * xmm4: current filter coefficient
220 * xmm5, xmm6, xmm7: scratchpad
224 "pxor %%xmm0, %%xmm0 \n"
225 "pxor %%xmm1, %%xmm1 \n"
226 "pxor %%xmm2, %%xmm2 \n"
227 "pxor %%xmm3, %%xmm3 \n"
232 /* a zero is useful during unpacking */
233 "pxor %%xmm4, %%xmm4 \n"
235 /* fetch all 16 source bytes */
236 "movups (%0), %%xmm5 \n"
237 "prefetcht0 (%0,%3,4) \n"
239 /* unpack into words (xmm5, xmm7) */
240 "movaps %%xmm5, %%xmm7 \n"
241 "punpcklbw %%xmm4, %%xmm5 \n"
242 "punpckhbw %%xmm4, %%xmm7 \n"
244 /* unpack xmm5 into dwords (xmm5, xmm6) */
245 "movaps %%xmm5, %%xmm6 \n"
246 "punpcklwd %%xmm4, %%xmm5 \n"
247 "punpckhwd %%xmm4, %%xmm6 \n"
249 /* convert xmm5, xmm6 to floats */
250 "cvtdq2ps %%xmm5, %%xmm5 \n"
251 "cvtdq2ps %%xmm6, %%xmm6 \n"
253 /* fetch the coefficient */
254 "movss (%2), %%xmm4 \n"
255 "shufps $0x0, %%xmm4, %%xmm4 \n"
257 /* do the muls for xmm5 and xmm6 */
258 "mulps %%xmm4, %%xmm5 \n"
259 "mulps %%xmm4, %%xmm6 \n"
260 "addps %%xmm5, %%xmm0 \n"
261 "addps %%xmm6, %%xmm1 \n"
263 /* get the zero back again */
264 "pxor %%xmm4, %%xmm4 \n"
266 /* unpack xmm7 into dwords (xmm7, xmm6) */
267 "movaps %%xmm7, %%xmm6 \n"
268 "punpcklwd %%xmm4, %%xmm7 \n"
269 "punpckhwd %%xmm4, %%xmm6 \n"
271 /* convert xmm7, xmm6 to floats */
272 "cvtdq2ps %%xmm7, %%xmm7 \n"
273 "cvtdq2ps %%xmm6, %%xmm6 \n"
275 /* fetch the coefficient */
276 "movss (%2), %%xmm4 \n"
277 "shufps $0x0, %%xmm4, %%xmm4 \n"
279 /* do the second set of muls */
280 "mulps %%xmm4, %%xmm7 \n"
281 "mulps %%xmm4, %%xmm6 \n"
282 "addps %%xmm7, %%xmm2 \n"
283 "addps %%xmm6, %%xmm3 \n"
285 /* move along, and loop */
291 /* store the values */
292 "movaps %%xmm0, (%4) \n"
293 "movaps %%xmm1, 16(%4) \n"
294 "movaps %%xmm2, 32(%4) \n"
295 "movaps %%xmm3, 48(%4) \n"
297 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
298 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
299 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
300 "r" (w), /* 3: stride */
301 "r" (dptr) /* 4: dstptr base */
302 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
308 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
310 for (x = 0; x < w; ++x) {
312 unsigned char *sptr = pix + x;
313 float *dptr = npix + x;
314 for (y = 0; y < nh; ++y) {
316 float *cf = &coeffs[pd[y].startcoeff];
319 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
320 acc += sptr[sy * w] * *cf++;
329 int main(int argc, char **argv)
331 unsigned nominal_w = atoi(argv[1]);
332 unsigned nominal_h = atoi(argv[2]);
334 unsigned samp_h0 = 2, samp_v0 = 2;
335 unsigned samp_h1 = 1, samp_v1 = 1;
336 unsigned samp_h2 = 1, samp_v2 = 1;
337 unsigned max_samp_h = 2, max_samp_v = 2;
339 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
340 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
341 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
343 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
344 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
345 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
347 struct jpeg_decompress_struct dinfo;
348 struct jpeg_error_mgr jerr;
349 dinfo.err = jpeg_std_error(&jerr);
350 jpeg_create_decompress(&dinfo);
351 jpeg_stdio_src(&dinfo, stdin);
352 jpeg_read_header(&dinfo, TRUE);
353 dinfo.raw_data_out = TRUE;
354 jpeg_start_decompress(&dinfo);
356 fprintf(stderr, "Scaling using Lanczos filter:\n");
357 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);
358 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);
359 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);
361 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
362 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
363 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
364 JSAMPLE *data_ny, *data_ncb, *data_ncr;
366 int total_lines = 0, blocks = 0;
367 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
368 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
370 JSAMPROW y_row_ptrs[max_lines];
371 JSAMPROW cb_row_ptrs[max_lines];
372 JSAMPROW cr_row_ptrs[max_lines];
373 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
376 for (i = 0; i < max_lines; ++i) {
377 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
378 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
379 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
382 total_lines += max_lines;
385 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
390 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
391 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);
392 data_ny = (unsigned char *)malloc(nw0 * stride0);
393 hscale(npix, data_ny, dinfo.comp_info[0].width_in_blocks * DCTSIZE, nh0, nw0, stride0);
397 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
398 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);
399 data_ncr = (unsigned char *)malloc(nw1 * stride1);
400 hscale(npix, data_ncr, dinfo.comp_info[1].width_in_blocks * DCTSIZE, nh1, nw1, stride1);
404 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
405 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);
406 data_ncb = (unsigned char *)malloc(nw2 * stride2);
407 hscale(npix, data_ncb, dinfo.comp_info[2].width_in_blocks * DCTSIZE, nh2, nw2, stride2);
410 jpeg_destroy_decompress(&dinfo);
412 struct jpeg_compress_struct cinfo;
413 cinfo.err = jpeg_std_error(&jerr);
414 jpeg_create_compress(&cinfo);
415 jpeg_stdio_dest(&cinfo, stdout);
416 cinfo.input_components = 3;
417 jpeg_set_defaults(&cinfo);
418 jpeg_set_quality(&cinfo, 85, FALSE);
419 cinfo.image_width = nominal_w;
420 cinfo.image_height = nominal_h;
421 cinfo.raw_data_in = TRUE;
422 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
423 cinfo.comp_info[0].h_samp_factor = samp_h0;
424 cinfo.comp_info[0].v_samp_factor = samp_v0;
425 cinfo.comp_info[1].h_samp_factor = samp_h1;
426 cinfo.comp_info[1].v_samp_factor = samp_v1;
427 cinfo.comp_info[2].h_samp_factor = samp_h2;
428 cinfo.comp_info[2].v_samp_factor = samp_v2;
429 jpeg_start_compress(&cinfo, TRUE);
433 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
434 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
436 JSAMPROW y_row_ptrs[max_lines];
437 JSAMPROW cb_row_ptrs[max_lines];
438 JSAMPROW cr_row_ptrs[max_lines];
439 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
442 for (i = 0; i < max_lines; ++i) {
443 // simple edge extension
444 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
448 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
449 if (cbline > nh1 - 1)
452 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
453 if (crline > nh2 - 1)
456 y_row_ptrs[i] = data_ny + yline * stride0;
457 cb_row_ptrs[i] = data_ncb + cbline * stride1;
458 cr_row_ptrs[i] = data_ncr + crline * stride2;
461 total_lines += max_lines;
464 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
466 jpeg_finish_compress(&cinfo);
467 jpeg_destroy_compress(&cinfo);