2 * High quality image resampling with polyphase filters
3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "fastmemcpy.h"
27 #define NB_COMPONENTS 3
30 #define NB_PHASES (1 << PHASE_BITS)
32 #define FCENTER 1 /* index of the center of the filter */
34 #define POS_FRAC_BITS 16
35 #define POS_FRAC (1 << POS_FRAC_BITS)
36 /* 6 bits precision is needed for MMX */
39 #define LINE_BUF_HEIGHT (NB_TAPS * 4)
41 struct ImgReSampleContext {
42 int iwidth, iheight, owidth, oheight;
44 INT16 h_filters[NB_PHASES][NB_TAPS] __align8; /* horizontal filters */
45 INT16 v_filters[NB_PHASES][NB_TAPS] __align8; /* vertical filters */
49 static inline int get_phase(int pos)
51 return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1);
54 /* This function must be optimized */
55 static void h_resample_fast(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
56 int src_start, int src_incr, INT16 *filters)
58 int src_pos, phase, sum, i;
63 for(i=0;i<dst_width;i++) {
66 if ((src_pos >> POS_FRAC_BITS) < 0 ||
67 (src_pos >> POS_FRAC_BITS) > (src_width - NB_TAPS))
70 s = src + (src_pos >> POS_FRAC_BITS);
71 phase = get_phase(src_pos);
72 filter = filters + phase * NB_TAPS;
74 sum = s[0] * filter[0] +
82 for(j=0;j<NB_TAPS;j++)
83 sum += s[j] * filter[j];
86 sum = sum >> FILTER_BITS;
97 /* This function must be optimized */
98 static void v_resample(UINT8 *dst, int dst_width, UINT8 *src, int wrap,
105 for(i=0;i<dst_width;i++) {
107 sum = s[0 * wrap] * filter[0] +
108 s[1 * wrap] * filter[1] +
109 s[2 * wrap] * filter[2] +
110 s[3 * wrap] * filter[3];
117 for(j=0;j<NB_TAPS;j++) {
118 sum += s1[0] * filter[j];
123 sum = sum >> FILTER_BITS;
136 #include "i386/mmx.h"
138 #define FILTER4(reg) \
140 s = src + (src_pos >> POS_FRAC_BITS);\
141 phase = get_phase(src_pos);\
142 filter = filters + phase * NB_TAPS;\
144 punpcklbw_r2r(mm7, reg);\
145 movq_m2r(*filter, mm6);\
146 pmaddwd_r2r(reg, mm6);\
149 paddd_r2r(mm6, reg);\
150 psrad_i2r(FILTER_BITS, reg);\
151 src_pos += src_incr;\
154 #define DUMP(reg) movq_r2m(reg, tmp); printf(#reg "=%016Lx\n", tmp.uq);
156 /* XXX: do four pixels at a time */
157 static void h_resample_fast4_mmx(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
158 int src_start, int src_incr, INT16 *filters)
168 while (dst_width >= 4) {
175 packuswb_r2r(mm7, mm0);
176 packuswb_r2r(mm7, mm1);
177 packuswb_r2r(mm7, mm3);
178 packuswb_r2r(mm7, mm2);
190 while (dst_width > 0) {
192 packuswb_r2r(mm7, mm0);
201 static void v_resample4_mmx(UINT8 *dst, int dst_width, UINT8 *src, int wrap,
219 while (dst_width >= 4) {
220 movq_m2r(s[0 * wrap], mm0);
221 punpcklbw_r2r(mm7, mm0);
222 movq_m2r(s[1 * wrap], mm1);
223 punpcklbw_r2r(mm7, mm1);
224 movq_m2r(s[2 * wrap], mm2);
225 punpcklbw_r2r(mm7, mm2);
226 movq_m2r(s[3 * wrap], mm3);
227 punpcklbw_r2r(mm7, mm3);
229 pmullw_m2r(coefs[0], mm0);
230 pmullw_m2r(coefs[1], mm1);
231 pmullw_m2r(coefs[2], mm2);
232 pmullw_m2r(coefs[3], mm3);
237 psraw_i2r(FILTER_BITS, mm0);
239 packuswb_r2r(mm7, mm0);
242 *(UINT32 *)dst = tmp.ud[0];
247 while (dst_width > 0) {
248 sum = s[0 * wrap] * filter[0] +
249 s[1 * wrap] * filter[1] +
250 s[2 * wrap] * filter[2] +
251 s[3 * wrap] * filter[3];
252 sum = sum >> FILTER_BITS;
266 /* slow version to handle limit cases. Does not need optimisation */
267 static void h_resample_slow(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
268 int src_start, int src_incr, INT16 *filters)
270 int src_pos, phase, sum, j, v, i;
274 src_end = src + src_width;
276 for(i=0;i<dst_width;i++) {
277 s = src + (src_pos >> POS_FRAC_BITS);
278 phase = get_phase(src_pos);
279 filter = filters + phase * NB_TAPS;
281 for(j=0;j<NB_TAPS;j++) {
284 else if (s >= src_end)
288 sum += v * filter[j];
291 sum = sum >> FILTER_BITS;
302 static void h_resample(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
303 int src_start, int src_incr, INT16 *filters)
308 n = (0 - src_start + src_incr - 1) / src_incr;
309 h_resample_slow(dst, n, src, src_width, src_start, src_incr, filters);
312 src_start += n * src_incr;
314 src_end = src_start + dst_width * src_incr;
315 if (src_end > ((src_width - NB_TAPS) << POS_FRAC_BITS)) {
316 n = (((src_width - NB_TAPS + 1) << POS_FRAC_BITS) - 1 - src_start) /
322 if ((mm_flags & MM_MMX) && NB_TAPS == 4)
323 h_resample_fast4_mmx(dst, n,
324 src, src_width, src_start, src_incr, filters);
327 h_resample_fast(dst, n,
328 src, src_width, src_start, src_incr, filters);
332 src_start += n * src_incr;
333 h_resample_slow(dst, dst_width,
334 src, src_width, src_start, src_incr, filters);
338 static void component_resample(ImgReSampleContext *s,
339 UINT8 *output, int owrap, int owidth, int oheight,
340 UINT8 *input, int iwrap, int iwidth, int iheight)
342 int src_y, src_y1, last_src_y, ring_y, phase_y, y1, y;
343 UINT8 *new_line, *src_line;
345 last_src_y = - FCENTER - 1;
346 /* position of the bottom of the filter in the source image */
347 src_y = (last_src_y + NB_TAPS) * POS_FRAC;
348 ring_y = NB_TAPS; /* position in ring buffer */
349 for(y=0;y<oheight;y++) {
350 /* apply horizontal filter on new lines from input if needed */
351 src_y1 = src_y >> POS_FRAC_BITS;
352 while (last_src_y < src_y1) {
353 if (++ring_y >= LINE_BUF_HEIGHT + NB_TAPS)
356 /* handle limit conditions : replicate line (slighly
357 inefficient because we filter multiple times */
361 } else if (y1 >= iheight) {
364 src_line = input + y1 * iwrap;
365 new_line = s->line_buf + ring_y * owidth;
366 /* apply filter and handle limit cases correctly */
367 h_resample(new_line, owidth,
368 src_line, iwidth, - FCENTER * POS_FRAC, s->h_incr,
369 &s->h_filters[0][0]);
370 /* handle ring buffer wraping */
371 if (ring_y >= LINE_BUF_HEIGHT) {
372 memcpy(s->line_buf + (ring_y - LINE_BUF_HEIGHT) * owidth,
376 /* apply vertical filter */
377 phase_y = get_phase(src_y);
379 /* desactivated MMX because loss of precision */
380 if ((mm_flags & MM_MMX) && NB_TAPS == 4 && 0)
381 v_resample4_mmx(output, owidth,
382 s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
383 &s->v_filters[phase_y][0]);
386 v_resample(output, owidth,
387 s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
388 &s->v_filters[phase_y][0]);
395 /* XXX: the following filter is quite naive, but it seems to suffice
397 static void build_filter(INT16 *filter, float factor)
400 float x, y, tab[NB_TAPS], norm, mult;
402 /* if upsampling, only need to interpolate, no filter */
406 for(ph=0;ph<NB_PHASES;ph++) {
408 for(i=0;i<NB_TAPS;i++) {
410 x = M_PI * ((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor;
419 /* normalize so that an uniform color remains the same */
420 mult = (float)(1 << FILTER_BITS) / norm;
421 for(i=0;i<NB_TAPS;i++) {
422 v = (int)(tab[i] * mult);
423 filter[ph * NB_TAPS + i] = v;
428 ImgReSampleContext *img_resample_init(int owidth, int oheight,
429 int iwidth, int iheight)
431 ImgReSampleContext *s;
433 s = av_mallocz(sizeof(ImgReSampleContext));
436 s->line_buf = av_mallocz(owidth * (LINE_BUF_HEIGHT + NB_TAPS));
441 s->oheight = oheight;
443 s->iheight = iheight;
445 s->h_incr = (iwidth * POS_FRAC) / owidth;
446 s->v_incr = (iheight * POS_FRAC) / oheight;
448 build_filter(&s->h_filters[0][0], (float)owidth / (float)iwidth);
449 build_filter(&s->v_filters[0][0], (float)oheight / (float)iheight);
457 void img_resample(ImgReSampleContext *s,
458 AVPicture *output, AVPicture *input)
463 shift = (i == 0) ? 0 : 1;
464 component_resample(s, output->data[i], output->linesize[i],
465 s->owidth >> shift, s->oheight >> shift,
466 input->data[i], input->linesize[i],
467 s->iwidth >> shift, s->iheight >> shift);
471 void img_resample_close(ImgReSampleContext *s)
473 av_free(s->line_buf);
479 void *av_mallocz(int size)
483 memset(ptr, 0, size);
490 UINT8 img[XSIZE * YSIZE];
495 UINT8 img1[XSIZE1 * YSIZE1];
496 UINT8 img2[XSIZE1 * YSIZE1];
498 void save_pgm(const char *filename, UINT8 *img, int xsize, int ysize)
501 f=fopen(filename,"w");
502 fprintf(f,"P5\n%d %d\n%d\n", xsize, ysize, 255);
503 fwrite(img,1, xsize * ysize,f);
507 static void dump_filter(INT16 *filter)
511 for(ph=0;ph<NB_PHASES;ph++) {
513 for(i=0;i<NB_TAPS;i++) {
514 printf(" %5.2f", filter[ph * NB_TAPS + i] / 256.0);
524 int main(int argc, char **argv)
526 int x, y, v, i, xsize, ysize;
527 ImgReSampleContext *s;
528 float fact, factors[] = { 1/2.0, 3.0/4.0, 1.0, 4.0/3.0, 16.0/9.0, 2.0 };
531 /* build test image */
532 for(y=0;y<YSIZE;y++) {
533 for(x=0;x<XSIZE;x++) {
534 if (x < XSIZE/2 && y < YSIZE/2) {
535 if (x < XSIZE/4 && y < YSIZE/4) {
541 } else if (x < XSIZE/4) {
546 } else if (y < XSIZE/4) {
558 if (((x+3) % 4) <= 1 &&
565 } else if (x < XSIZE/2) {
566 v = ((x - (XSIZE/2)) * 255) / (XSIZE/2);
567 } else if (y < XSIZE/2) {
568 v = ((y - (XSIZE/2)) * 255) / (XSIZE/2);
570 v = ((x + y - XSIZE) * 255) / XSIZE;
572 img[y * XSIZE + x] = v;
575 save_pgm("/tmp/in.pgm", img, XSIZE, YSIZE);
576 for(i=0;i<sizeof(factors)/sizeof(float);i++) {
578 xsize = (int)(XSIZE * fact);
579 ysize = (int)(YSIZE * fact);
580 s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
581 printf("Factor=%0.2f\n", fact);
582 dump_filter(&s->h_filters[0][0]);
583 component_resample(s, img1, xsize, xsize, ysize,
584 img, XSIZE, XSIZE, YSIZE);
585 img_resample_close(s);
587 sprintf(buf, "/tmp/out%d.pgm", i);
588 save_pgm(buf, img1, xsize, ysize);
593 printf("MMX test\n");
595 xsize = (int)(XSIZE * fact);
596 ysize = (int)(YSIZE * fact);
598 s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
599 component_resample(s, img1, xsize, xsize, ysize,
600 img, XSIZE, XSIZE, YSIZE);
603 s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
604 component_resample(s, img2, xsize, xsize, ysize,
605 img, XSIZE, XSIZE, YSIZE);
606 if (memcmp(img1, img2, xsize * ysize) != 0) {
607 fprintf(stderr, "mmx error\n");