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);
60 pd[x].startcoeff = num_coeffs;
62 for (sx = start; sx <= end; ++sx) {
63 double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
64 double f = lanczos_tap(nd);
65 if (num_coeffs == size_coeffs) {
67 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
70 coeffs[num_coeffs++] = f;
74 for (sx = start; sx <= end; ++sx) {
75 coeffs[pd[x].startcoeff + sx - start] /= sum;
79 for (y = 0; y < h; ++y) {
80 float *sptr = pix + y*w;
81 unsigned char *dptr = npix + y*dstride;
83 for (x = 0; x < nw; ++x) {
85 float *cf = &coeffs[pd[x].startcoeff];
88 for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
89 acc += sptr[sx] * *cf++;
97 ch = (unsigned char)acc;
100 for ( ; x < dstride; ++x) {
106 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
108 struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
110 float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
113 double sf = (double)h / (double)nh;
114 double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
116 /* calculate the filter */
117 for (y = 0; y < nh; ++y) {
118 int start = ceil(y * sf - support);
119 int end = floor(y * sf + support);
131 pd[y].startcoeff = num_coeffs;
133 for (sy = start; sy <= end; ++sy) {
134 double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
135 double f = lanczos_tap(nd);
136 if (num_coeffs == size_coeffs) {
138 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
141 coeffs[num_coeffs++] = f;
145 for (sy = start; sy <= end; ++sy) {
146 coeffs[pd[y].startcoeff + sy - start] /= sum;
150 #if CACHE_LINE_FACTOR > 1
151 for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
152 unsigned char *sptr = pix + x;
153 float *dptr = npix + x;
154 for (y = 0; y < nh; ++y) {
157 float acc[CACHE_LINE_FACTOR];
158 for (i = 0; i < CACHE_LINE_FACTOR; ++i)
160 float *cf = &coeffs[pd[y].startcoeff];
163 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
164 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
165 acc[i] += sptr[sy * w + i] * *cf;
170 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
175 * xmm0 - xmm3: acc[0..15]
176 * xmm4: current filter coefficient
177 * xmm5, xmm6, xmm7: scratchpad
181 "pxor %%xmm0, %%xmm0 \n"
182 "pxor %%xmm1, %%xmm1 \n"
183 "pxor %%xmm2, %%xmm2 \n"
184 "pxor %%xmm3, %%xmm3 \n"
189 /* a zero is useful during unpacking */
190 "pxor %%xmm4, %%xmm4 \n"
192 /* fetch all 16 source bytes */
193 "movups (%0), %%xmm5 \n"
194 "prefetcht0 (%0,%3,4) \n"
196 /* unpack into words (xmm5, xmm7) */
197 "movaps %%xmm5, %%xmm7 \n"
198 "punpcklbw %%xmm4, %%xmm5 \n"
199 "punpckhbw %%xmm4, %%xmm7 \n"
201 /* unpack xmm5 into dwords (xmm5, xmm6) */
202 "movaps %%xmm5, %%xmm6 \n"
203 "punpcklwd %%xmm4, %%xmm5 \n"
204 "punpckhwd %%xmm4, %%xmm6 \n"
206 /* convert xmm5, xmm6 to floats */
207 "cvtdq2ps %%xmm5, %%xmm5 \n"
208 "cvtdq2ps %%xmm6, %%xmm6 \n"
210 /* fetch the coefficient */
211 "movss (%2), %%xmm4 \n"
212 "shufps $0x0, %%xmm4, %%xmm4 \n"
214 /* do the muls for xmm5 and xmm6 */
215 "mulps %%xmm4, %%xmm5 \n"
216 "mulps %%xmm4, %%xmm6 \n"
217 "addps %%xmm5, %%xmm0 \n"
218 "addps %%xmm6, %%xmm1 \n"
220 /* get the zero back again */
221 "pxor %%xmm4, %%xmm4 \n"
223 /* unpack xmm7 into dwords (xmm7, xmm6) */
224 "movaps %%xmm7, %%xmm6 \n"
225 "punpcklwd %%xmm4, %%xmm7 \n"
226 "punpckhwd %%xmm4, %%xmm6 \n"
228 /* convert xmm7, xmm6 to floats */
229 "cvtdq2ps %%xmm7, %%xmm7 \n"
230 "cvtdq2ps %%xmm6, %%xmm6 \n"
232 /* fetch the coefficient */
233 "movss (%2), %%xmm4 \n"
234 "shufps $0x0, %%xmm4, %%xmm4 \n"
236 /* do the second set of muls */
237 "mulps %%xmm4, %%xmm7 \n"
238 "mulps %%xmm4, %%xmm6 \n"
239 "addps %%xmm7, %%xmm2 \n"
240 "addps %%xmm6, %%xmm3 \n"
242 /* move along, and loop */
248 /* store the values */
249 "movaps %%xmm0, (%4) \n"
250 "movaps %%xmm1, 16(%4) \n"
251 "movaps %%xmm2, 32(%4) \n"
252 "movaps %%xmm3, 48(%4) \n"
254 "r" (&sptr[pd[y].start * w]), /* 0: srcptr base */
255 "r" (pd[y].end - pd[y].start + 1), /* 1: filter len */
256 "r" (&coeffs[pd[y].startcoeff]), /* 2: coeffs base */
257 "r" (w), /* 3: stride */
258 "r" (dptr) /* 4: dstptr base */
259 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
265 for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
267 for (x = 0; x < w; ++x) {
269 unsigned char *sptr = pix + x;
270 float *dptr = npix + x;
271 for (y = 0; y < nh; ++y) {
273 float *cf = &coeffs[pd[y].startcoeff];
276 for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
277 acc += sptr[sy * w] * *cf++;
286 int main(int argc, char **argv)
288 unsigned nominal_w = atoi(argv[1]);
289 unsigned nominal_h = atoi(argv[2]);
291 unsigned samp_h0 = 2, samp_v0 = 2;
292 unsigned samp_h1 = 1, samp_v1 = 1;
293 unsigned samp_h2 = 1, samp_v2 = 1;
294 unsigned max_samp_h = 2, max_samp_v = 2;
296 unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
297 unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
298 unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
300 unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
301 unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
302 unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
304 struct jpeg_decompress_struct dinfo;
305 struct jpeg_error_mgr jerr;
306 dinfo.err = jpeg_std_error(&jerr);
307 jpeg_create_decompress(&dinfo);
308 jpeg_stdio_src(&dinfo, stdin);
309 jpeg_read_header(&dinfo, TRUE);
310 dinfo.raw_data_out = TRUE;
311 jpeg_start_decompress(&dinfo);
313 fprintf(stderr, "Scaling using Lanczos filter:\n");
314 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);
315 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);
316 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);
318 JSAMPLE *data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
319 JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
320 JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
321 JSAMPLE *data_ny, *data_ncb, *data_ncr;
323 int total_lines = 0, blocks = 0;
324 while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
325 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
327 JSAMPROW y_row_ptrs[max_lines];
328 JSAMPROW cb_row_ptrs[max_lines];
329 JSAMPROW cr_row_ptrs[max_lines];
330 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
333 for (i = 0; i < max_lines; ++i) {
334 y_row_ptrs[i] = data_y + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
335 cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
336 cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
339 total_lines += max_lines;
342 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
347 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float));
348 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);
349 data_ny = (unsigned char *)malloc(nw0 * stride0);
350 hscale(npix, data_ny, dinfo.comp_info[0].width_in_blocks * DCTSIZE, nh0, nw0, stride0);
354 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float));
355 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);
356 data_ncr = (unsigned char *)malloc(nw1 * stride1);
357 hscale(npix, data_ncr, dinfo.comp_info[1].width_in_blocks * DCTSIZE, nh1, nw1, stride1);
361 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float));
362 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);
363 data_ncb = (unsigned char *)malloc(nw2 * stride2);
364 hscale(npix, data_ncb, dinfo.comp_info[2].width_in_blocks * DCTSIZE, nh2, nw2, stride2);
367 jpeg_destroy_decompress(&dinfo);
369 struct jpeg_compress_struct cinfo;
370 cinfo.err = jpeg_std_error(&jerr);
371 jpeg_create_compress(&cinfo);
372 jpeg_stdio_dest(&cinfo, stdout);
373 cinfo.input_components = 3;
374 jpeg_set_defaults(&cinfo);
375 jpeg_set_quality(&cinfo, 85, FALSE);
376 cinfo.image_width = nominal_w;
377 cinfo.image_height = nominal_h;
378 cinfo.raw_data_in = TRUE;
379 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
380 cinfo.comp_info[0].h_samp_factor = samp_h0;
381 cinfo.comp_info[0].v_samp_factor = samp_v0;
382 cinfo.comp_info[1].h_samp_factor = samp_h1;
383 cinfo.comp_info[1].v_samp_factor = samp_v1;
384 cinfo.comp_info[2].h_samp_factor = samp_h2;
385 cinfo.comp_info[2].v_samp_factor = samp_v2;
386 jpeg_start_compress(&cinfo, TRUE);
390 while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
391 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
393 JSAMPROW y_row_ptrs[max_lines];
394 JSAMPROW cb_row_ptrs[max_lines];
395 JSAMPROW cr_row_ptrs[max_lines];
396 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
399 for (i = 0; i < max_lines; ++i) {
400 // simple edge extension
401 int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
405 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
406 if (cbline > nh1 - 1)
409 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
410 if (crline > nh2 - 1)
413 y_row_ptrs[i] = data_ny + yline * stride0;
414 cb_row_ptrs[i] = data_ncb + cbline * stride1;
415 cr_row_ptrs[i] = data_ncr + crline * stride2;
418 total_lines += max_lines;
421 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
423 jpeg_finish_compress(&cinfo);
424 jpeg_destroy_compress(&cinfo);