]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgresample.c
fixing ac prediction encoding with adaptive quantization
[ffmpeg] / libavcodec / imgresample.c
1 /*
2  * High quality image resampling with polyphase filters 
3  * Copyright (c) 2001 Fabrice Bellard.
4  *
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.
9  *
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.
14  *
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
18  */
19 #include "avcodec.h"
20 #include "dsputil.h"
21
22 #ifdef USE_FASTMEMCPY
23 #include "fastmemcpy.h"
24 #endif
25
26
27 #define NB_COMPONENTS 3
28
29 #define PHASE_BITS 4
30 #define NB_PHASES  (1 << PHASE_BITS)
31 #define NB_TAPS    4
32 #define FCENTER    1  /* index of the center of the filter */
33 //#define TEST    1  /* Test it */
34
35 #define POS_FRAC_BITS 16
36 #define POS_FRAC      (1 << POS_FRAC_BITS)
37 /* 6 bits precision is needed for MMX */
38 #define FILTER_BITS   8
39
40 #define LINE_BUF_HEIGHT (NB_TAPS * 4)
41
42 struct ImgReSampleContext {
43     int iwidth, iheight, owidth, oheight, topBand, bottomBand, leftBand, rightBand;
44     int h_incr, v_incr;
45     INT16 h_filters[NB_PHASES][NB_TAPS] __align8; /* horizontal filters */
46     INT16 v_filters[NB_PHASES][NB_TAPS] __align8; /* vertical filters */
47     UINT8 *line_buf;
48 };
49
50 static inline int get_phase(int pos)
51 {
52     return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1);
53 }
54
55 /* This function must be optimized */
56 static void h_resample_fast(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
57                             int src_start, int src_incr, INT16 *filters)
58 {
59     int src_pos, phase, sum, i;
60     UINT8 *s;
61     INT16 *filter;
62
63     src_pos = src_start;
64     for(i=0;i<dst_width;i++) {
65 #ifdef TEST
66         /* test */
67         if ((src_pos >> POS_FRAC_BITS) < 0 ||
68             (src_pos >> POS_FRAC_BITS) > (src_width - NB_TAPS))
69             av_abort();
70 #endif
71         s = src + (src_pos >> POS_FRAC_BITS);
72         phase = get_phase(src_pos);
73         filter = filters + phase * NB_TAPS;
74 #if NB_TAPS == 4
75         sum = s[0] * filter[0] +
76             s[1] * filter[1] +
77             s[2] * filter[2] +
78             s[3] * filter[3];
79 #else
80         {
81             int j;
82             sum = 0;
83             for(j=0;j<NB_TAPS;j++)
84                 sum += s[j] * filter[j];
85         }
86 #endif
87         sum = sum >> FILTER_BITS;
88         if (sum < 0)
89             sum = 0;
90         else if (sum > 255)
91             sum = 255;
92         dst[0] = sum;
93         src_pos += src_incr;
94         dst++;
95     }
96 }
97
98 /* This function must be optimized */
99 static void v_resample(UINT8 *dst, int dst_width, UINT8 *src, int wrap, 
100                        INT16 *filter)
101 {
102     int sum, i;
103     UINT8 *s;
104
105     s = src;
106     for(i=0;i<dst_width;i++) {
107 #if NB_TAPS == 4
108         sum = s[0 * wrap] * filter[0] +
109             s[1 * wrap] * filter[1] +
110             s[2 * wrap] * filter[2] +
111             s[3 * wrap] * filter[3];
112 #else
113         {
114             int j;
115             UINT8 *s1 = s;
116
117             sum = 0;
118             for(j=0;j<NB_TAPS;j++) {
119                 sum += s1[0] * filter[j];
120                 s1 += wrap;
121             }
122         }
123 #endif
124         sum = sum >> FILTER_BITS;
125         if (sum < 0)
126             sum = 0;
127         else if (sum > 255)
128             sum = 255;
129         dst[0] = sum;
130         dst++;
131         s++;
132     }
133 }
134
135 #ifdef HAVE_MMX
136
137 #include "i386/mmx.h"
138
139 #define FILTER4(reg) \
140 {\
141         s = src + (src_pos >> POS_FRAC_BITS);\
142         phase = get_phase(src_pos);\
143         filter = filters + phase * NB_TAPS;\
144         movq_m2r(*s, reg);\
145         punpcklbw_r2r(mm7, reg);\
146         movq_m2r(*filter, mm6);\
147         pmaddwd_r2r(reg, mm6);\
148         movq_r2r(mm6, reg);\
149         psrlq_i2r(32, reg);\
150         paddd_r2r(mm6, reg);\
151         psrad_i2r(FILTER_BITS, reg);\
152         src_pos += src_incr;\
153 }
154
155 #define DUMP(reg) movq_r2m(reg, tmp); printf(#reg "=%016Lx\n", tmp.uq);
156
157 /* XXX: do four pixels at a time */
158 static void h_resample_fast4_mmx(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
159                                  int src_start, int src_incr, INT16 *filters)
160 {
161     int src_pos, phase;
162     UINT8 *s;
163     INT16 *filter;
164     mmx_t tmp;
165     
166     src_pos = src_start;
167     pxor_r2r(mm7, mm7);
168
169     while (dst_width >= 4) {
170
171         FILTER4(mm0);
172         FILTER4(mm1);
173         FILTER4(mm2);
174         FILTER4(mm3);
175
176         packuswb_r2r(mm7, mm0);
177         packuswb_r2r(mm7, mm1);
178         packuswb_r2r(mm7, mm3);
179         packuswb_r2r(mm7, mm2);
180         movq_r2m(mm0, tmp);
181         dst[0] = tmp.ub[0];
182         movq_r2m(mm1, tmp);
183         dst[1] = tmp.ub[0];
184         movq_r2m(mm2, tmp);
185         dst[2] = tmp.ub[0];
186         movq_r2m(mm3, tmp);
187         dst[3] = tmp.ub[0];
188         dst += 4;
189         dst_width -= 4;
190     }
191     while (dst_width > 0) {
192         FILTER4(mm0);
193         packuswb_r2r(mm7, mm0);
194         movq_r2m(mm0, tmp);
195         dst[0] = tmp.ub[0];
196         dst++;
197         dst_width--;
198     }
199     emms();
200 }
201
202 static void v_resample4_mmx(UINT8 *dst, int dst_width, UINT8 *src, int wrap, 
203                             INT16 *filter)
204 {
205     int sum, i, v;
206     UINT8 *s;
207     mmx_t tmp;
208     mmx_t coefs[4];
209     
210     for(i=0;i<4;i++) {
211         v = filter[i];
212         coefs[i].uw[0] = v;
213         coefs[i].uw[1] = v;
214         coefs[i].uw[2] = v;
215         coefs[i].uw[3] = v;
216     }
217     
218     pxor_r2r(mm7, mm7);
219     s = src;
220     while (dst_width >= 4) {
221         movq_m2r(s[0 * wrap], mm0);
222         punpcklbw_r2r(mm7, mm0);
223         movq_m2r(s[1 * wrap], mm1);
224         punpcklbw_r2r(mm7, mm1);
225         movq_m2r(s[2 * wrap], mm2);
226         punpcklbw_r2r(mm7, mm2);
227         movq_m2r(s[3 * wrap], mm3);
228         punpcklbw_r2r(mm7, mm3);
229
230         pmullw_m2r(coefs[0], mm0);
231         pmullw_m2r(coefs[1], mm1);
232         pmullw_m2r(coefs[2], mm2);
233         pmullw_m2r(coefs[3], mm3);
234
235         paddw_r2r(mm1, mm0);
236         paddw_r2r(mm3, mm2);
237         paddw_r2r(mm2, mm0);
238         psraw_i2r(FILTER_BITS, mm0);
239         
240         packuswb_r2r(mm7, mm0);
241         movq_r2m(mm0, tmp);
242
243         *(UINT32 *)dst = tmp.ud[0];
244         dst += 4;
245         s += 4;
246         dst_width -= 4;
247     }
248     while (dst_width > 0) {
249         sum = s[0 * wrap] * filter[0] +
250             s[1 * wrap] * filter[1] +
251             s[2 * wrap] * filter[2] +
252             s[3 * wrap] * filter[3];
253         sum = sum >> FILTER_BITS;
254         if (sum < 0)
255             sum = 0;
256         else if (sum > 255)
257             sum = 255;
258         dst[0] = sum;
259         dst++;
260         s++;
261         dst_width--;
262     }
263     emms();
264 }
265 #endif
266
267 /* slow version to handle limit cases. Does not need optimisation */
268 static void h_resample_slow(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
269                             int src_start, int src_incr, INT16 *filters)
270 {
271     int src_pos, phase, sum, j, v, i;
272     UINT8 *s, *src_end;
273     INT16 *filter;
274
275     src_end = src + src_width;
276     src_pos = src_start;
277     for(i=0;i<dst_width;i++) {
278         s = src + (src_pos >> POS_FRAC_BITS);
279         phase = get_phase(src_pos);
280         filter = filters + phase * NB_TAPS;
281         sum = 0;
282         for(j=0;j<NB_TAPS;j++) {
283             if (s < src)
284                 v = src[0];
285             else if (s >= src_end)
286                 v = src_end[-1];
287             else
288                 v = s[0];
289             sum += v * filter[j];
290             s++;
291         }
292         sum = sum >> FILTER_BITS;
293         if (sum < 0)
294             sum = 0;
295         else if (sum > 255)
296             sum = 255;
297         dst[0] = sum;
298         src_pos += src_incr;
299         dst++;
300     }
301 }
302
303 static void h_resample(UINT8 *dst, int dst_width, UINT8 *src, int src_width,
304                        int src_start, int src_incr, INT16 *filters)
305 {
306     int n, src_end;
307
308     if (src_start < 0) {
309         n = (0 - src_start + src_incr - 1) / src_incr;
310         h_resample_slow(dst, n, src, src_width, src_start, src_incr, filters);
311         dst += n;
312         dst_width -= n;
313         src_start += n * src_incr;
314     }
315     src_end = src_start + dst_width * src_incr;
316     if (src_end > ((src_width - NB_TAPS) << POS_FRAC_BITS)) {
317         n = (((src_width - NB_TAPS + 1) << POS_FRAC_BITS) - 1 - src_start) / 
318             src_incr;
319     } else {
320         n = dst_width;
321     }
322 #ifdef HAVE_MMX
323     if ((mm_flags & MM_MMX) && NB_TAPS == 4)
324         h_resample_fast4_mmx(dst, n, 
325                              src, src_width, src_start, src_incr, filters);
326     else
327 #endif
328         h_resample_fast(dst, n, 
329                         src, src_width, src_start, src_incr, filters);
330     if (n < dst_width) {
331         dst += n;
332         dst_width -= n;
333         src_start += n * src_incr;
334         h_resample_slow(dst, dst_width, 
335                         src, src_width, src_start, src_incr, filters);
336     }
337 }
338
339 static void component_resample(ImgReSampleContext *s, 
340                                UINT8 *output, int owrap, int owidth, int oheight,
341                                UINT8 *input, int iwrap, int iwidth, int iheight)
342 {
343     int src_y, src_y1, last_src_y, ring_y, phase_y, y1, y;
344     UINT8 *new_line, *src_line;
345
346     last_src_y = - FCENTER - 1;
347     /* position of the bottom of the filter in the source image */
348     src_y = (last_src_y + NB_TAPS) * POS_FRAC; 
349     ring_y = NB_TAPS; /* position in ring buffer */
350     for(y=0;y<oheight;y++) {
351         /* apply horizontal filter on new lines from input if needed */
352         src_y1 = src_y >> POS_FRAC_BITS;
353         while (last_src_y < src_y1) {
354             if (++ring_y >= LINE_BUF_HEIGHT + NB_TAPS)
355                 ring_y = NB_TAPS;
356             last_src_y++;
357             /* handle limit conditions : replicate line (slightly
358                inefficient because we filter multiple times) */
359             y1 = last_src_y;
360             if (y1 < 0) {
361                 y1 = 0;
362             } else if (y1 >= iheight) {
363                 y1 = iheight - 1;
364             }
365             src_line = input + y1 * iwrap;
366             new_line = s->line_buf + ring_y * owidth;
367             /* apply filter and handle limit cases correctly */
368             h_resample(new_line, owidth, 
369                        src_line, iwidth, - FCENTER * POS_FRAC, s->h_incr, 
370                        &s->h_filters[0][0]);
371             /* handle ring buffer wraping */
372             if (ring_y >= LINE_BUF_HEIGHT) {
373                 memcpy(s->line_buf + (ring_y - LINE_BUF_HEIGHT) * owidth,
374                        new_line, owidth);
375             }
376         }
377         /* apply vertical filter */
378         phase_y = get_phase(src_y);
379 #ifdef HAVE_MMX
380         /* desactivated MMX because loss of precision */
381         if ((mm_flags & MM_MMX) && NB_TAPS == 4 && 0)
382             v_resample4_mmx(output, owidth, 
383                             s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth, 
384                             &s->v_filters[phase_y][0]);
385         else
386 #endif
387             v_resample(output, owidth, 
388                        s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth, 
389                        &s->v_filters[phase_y][0]);
390             
391         src_y += s->v_incr;
392         output += owrap;
393     }
394 }
395
396 /* XXX: the following filter is quite naive, but it seems to suffice
397    for 4 taps */
398 static void build_filter(INT16 *filter, float factor)
399 {
400     int ph, i, v;
401     float x, y, tab[NB_TAPS], norm, mult;
402
403     /* if upsampling, only need to interpolate, no filter */
404     if (factor > 1.0)
405         factor = 1.0;
406
407     for(ph=0;ph<NB_PHASES;ph++) {
408         norm = 0;
409         for(i=0;i<NB_TAPS;i++) {
410             
411             x = M_PI * ((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor;
412             if (x == 0)
413                 y = 1.0;
414             else
415                 y = sin(x) / x;
416             tab[i] = y;
417             norm += y;
418         }
419
420         /* normalize so that an uniform color remains the same */
421         mult = (float)(1 << FILTER_BITS) / norm;
422         for(i=0;i<NB_TAPS;i++) {
423             v = (int)(tab[i] * mult);
424             filter[ph * NB_TAPS + i] = v;
425         }
426     }
427 }
428
429 ImgReSampleContext *img_resample_init(int owidth, int oheight,
430                                       int iwidth, int iheight)
431 {
432         return img_resample_full_init(owidth, oheight, iwidth, iheight, 0, 0, 0, 0);
433 }
434
435 ImgReSampleContext *img_resample_full_init(int owidth, int oheight,
436                                       int iwidth, int iheight,
437                                       int topBand, int bottomBand,
438                                       int leftBand, int rightBand)
439 {
440     ImgReSampleContext *s;
441
442     s = av_mallocz(sizeof(ImgReSampleContext));
443     if (!s)
444         return NULL;
445     s->line_buf = av_mallocz(owidth * (LINE_BUF_HEIGHT + NB_TAPS));
446     if (!s->line_buf) 
447         goto fail;
448     
449     s->owidth = owidth;
450     s->oheight = oheight;
451     s->iwidth = iwidth;
452     s->iheight = iheight;
453     s->topBand = topBand;
454     s->bottomBand = bottomBand;
455     s->leftBand = leftBand;
456     s->rightBand = rightBand;
457     
458     s->h_incr = ((iwidth - leftBand - rightBand) * POS_FRAC) / owidth;
459     s->v_incr = ((iheight - topBand - bottomBand) * POS_FRAC) / oheight;
460     
461     build_filter(&s->h_filters[0][0], (float) owidth  / (float) (iwidth - leftBand - rightBand));
462     build_filter(&s->v_filters[0][0], (float) oheight / (float) (iheight - topBand - bottomBand));
463
464     return s;
465  fail:
466     av_free(s);
467     return NULL;
468 }
469
470 void img_resample(ImgReSampleContext *s, 
471                   AVPicture *output, AVPicture *input)
472 {
473     int i, shift;
474
475     for(i=0;i<3;i++) {
476         shift = (i == 0) ? 0 : 1;
477         component_resample(s, output->data[i], output->linesize[i], 
478                            s->owidth >> shift, s->oheight >> shift,
479                            input->data[i] + (input->linesize[i] * (s->topBand >> shift)) + (s->leftBand >> shift),
480                            input->linesize[i], ((s->iwidth - s->leftBand - s->rightBand) >> shift),
481                            (s->iheight - s->topBand - s->bottomBand) >> shift);
482     }
483 }
484
485 void img_resample_close(ImgReSampleContext *s)
486 {
487     av_free(s->line_buf);
488     av_free(s);
489 }
490
491 #ifdef TEST
492
493 void *av_mallocz(int size)
494 {
495     void *ptr;
496     ptr = malloc(size);
497     memset(ptr, 0, size);
498     return ptr;
499 }
500
501 void av_free(void *ptr)
502 {
503     /* XXX: this test should not be needed on most libcs */
504     if (ptr)
505         free(ptr);
506 }
507
508 /* input */
509 #define XSIZE 256
510 #define YSIZE 256
511 UINT8 img[XSIZE * YSIZE];
512
513 /* output */
514 #define XSIZE1 512
515 #define YSIZE1 512
516 UINT8 img1[XSIZE1 * YSIZE1];
517 UINT8 img2[XSIZE1 * YSIZE1];
518
519 void save_pgm(const char *filename, UINT8 *img, int xsize, int ysize)
520 {
521     FILE *f;
522     f=fopen(filename,"w");
523     fprintf(f,"P5\n%d %d\n%d\n", xsize, ysize, 255);
524     fwrite(img,1, xsize * ysize,f);
525     fclose(f);
526 }
527
528 static void dump_filter(INT16 *filter)
529 {
530     int i, ph;
531
532     for(ph=0;ph<NB_PHASES;ph++) {
533         printf("%2d: ", ph);
534         for(i=0;i<NB_TAPS;i++) {
535             printf(" %5.2f", filter[ph * NB_TAPS + i] / 256.0);
536         }
537         printf("\n");
538     }
539 }
540
541 #ifdef HAVE_MMX
542 int mm_flags;
543 #endif
544
545 int main(int argc, char **argv)
546 {
547     int x, y, v, i, xsize, ysize;
548     ImgReSampleContext *s;
549     float fact, factors[] = { 1/2.0, 3.0/4.0, 1.0, 4.0/3.0, 16.0/9.0, 2.0 };
550     char buf[256];
551
552     /* build test image */
553     for(y=0;y<YSIZE;y++) {
554         for(x=0;x<XSIZE;x++) {
555             if (x < XSIZE/2 && y < YSIZE/2) {
556                 if (x < XSIZE/4 && y < YSIZE/4) {
557                     if ((x % 10) <= 6 &&
558                         (y % 10) <= 6)
559                         v = 0xff;
560                     else
561                         v = 0x00;
562                 } else if (x < XSIZE/4) {
563                     if (x & 1) 
564                         v = 0xff;
565                     else 
566                         v = 0;
567                 } else if (y < XSIZE/4) {
568                     if (y & 1) 
569                         v = 0xff;
570                     else 
571                         v = 0;
572                 } else {
573                     if (y < YSIZE*3/8) {
574                         if ((y+x) & 1) 
575                             v = 0xff;
576                         else 
577                             v = 0;
578                     } else {
579                         if (((x+3) % 4) <= 1 &&
580                             ((y+3) % 4) <= 1)
581                             v = 0xff;
582                         else
583                             v = 0x00;
584                     }
585                 }
586             } else if (x < XSIZE/2) {
587                 v = ((x - (XSIZE/2)) * 255) / (XSIZE/2);
588             } else if (y < XSIZE/2) {
589                 v = ((y - (XSIZE/2)) * 255) / (XSIZE/2);
590             } else {
591                 v = ((x + y - XSIZE) * 255) / XSIZE;
592             }
593             img[(YSIZE - y) * XSIZE + (XSIZE - x)] = v;
594         }
595     }
596     save_pgm("/tmp/in.pgm", img, XSIZE, YSIZE);
597     for(i=0;i<sizeof(factors)/sizeof(float);i++) {
598         fact = factors[i];
599         xsize = (int)(XSIZE * fact);
600         ysize = (int)((YSIZE - 100) * fact);
601         s = img_resample_full_init(xsize, ysize, XSIZE, YSIZE, 50 ,50, 0, 0);
602         printf("Factor=%0.2f\n", fact);
603         dump_filter(&s->h_filters[0][0]);
604         component_resample(s, img1, xsize, xsize, ysize,
605                            img + 50 * XSIZE, XSIZE, XSIZE, YSIZE - 100);
606         img_resample_close(s);
607
608         sprintf(buf, "/tmp/out%d.pgm", i);
609         save_pgm(buf, img1, xsize, ysize);
610     }
611
612     /* mmx test */
613 #ifdef HAVE_MMX
614     printf("MMX test\n");
615     fact = 0.72;
616     xsize = (int)(XSIZE * fact);
617     ysize = (int)(YSIZE * fact);
618     mm_flags = MM_MMX;
619     s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
620     component_resample(s, img1, xsize, xsize, ysize,
621                        img, XSIZE, XSIZE, YSIZE);
622
623     mm_flags = 0;
624     s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
625     component_resample(s, img2, xsize, xsize, ysize,
626                        img, XSIZE, XSIZE, YSIZE);
627     if (memcmp(img1, img2, xsize * ysize) != 0) {
628         fprintf(stderr, "mmx error\n");
629         exit(1);
630     }
631     printf("MMX OK\n");
632 #endif
633     return 0;
634 }
635
636 #endif