]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/imgresample.c
lame bit_rate calculation
[ffmpeg] / libavcodec / imgresample.c
index da57ad773f825183f1224a5bdde925e85fa4790b..53471f4510eef224433615885adaa2c83e505620 100644 (file)
@@ -55,6 +55,8 @@ struct ImgReSampleContext {
     uint8_t *line_buf;
 };
 
+void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_count, int scale, int type);
+
 static inline int get_phase(int pos)
 {
     return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1);
@@ -540,48 +542,6 @@ static void component_resample(ImgReSampleContext *s,
     }
 }
 
-/* XXX: the following filter is quite naive, but it seems to suffice
-   for 4 taps */
-static void build_filter(int16_t *filter, float factor)
-{
-    int ph, i, v;
-    float x, y, tab[NB_TAPS], norm, mult, target;
-
-    /* if upsampling, only need to interpolate, no filter */
-    if (factor > 1.0)
-        factor = 1.0;
-
-    for(ph=0;ph<NB_PHASES;ph++) {
-        norm = 0;
-        for(i=0;i<NB_TAPS;i++) {
-#if 1
-            const float d= -0.5; //first order derivative = -0.5
-            x = fabs(((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor);
-            if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*(            -x*x + x*x*x);
-            else      y=                       d*(-4 + 8*x - 5*x*x + x*x*x);
-#else
-            x = M_PI * ((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor;
-            if (x == 0)
-                y = 1.0;
-            else
-                y = sin(x) / x;
-#endif
-            tab[i] = y;
-            norm += y;
-        }
-
-        /* normalize so that an uniform color remains the same */
-        target= 1 << FILTER_BITS;
-        for(i=0;i<NB_TAPS;i++) {
-            mult = target / norm;
-            v = lrintf(tab[i] * mult);
-            filter[ph * NB_TAPS + i] = v;
-            norm -= tab[i];
-            target -= v;
-        }
-    }
-}
-
 ImgReSampleContext *img_resample_init(int owidth, int oheight,
                                       int iwidth, int iheight)
 {
@@ -601,6 +561,8 @@ ImgReSampleContext *img_resample_full_init(int owidth, int oheight,
     s = av_mallocz(sizeof(ImgReSampleContext));
     if (!s)
         return NULL;
+    if((unsigned)owidth >= UINT_MAX / (LINE_BUF_HEIGHT + NB_TAPS))
+        return NULL;
     s->line_buf = av_mallocz(owidth * (LINE_BUF_HEIGHT + NB_TAPS));
     if (!s->line_buf) 
         goto fail;
@@ -626,10 +588,10 @@ ImgReSampleContext *img_resample_full_init(int owidth, int oheight,
     s->h_incr = ((iwidth - leftBand - rightBand) * POS_FRAC) / s->pad_owidth;
     s->v_incr = ((iheight - topBand - bottomBand) * POS_FRAC) / s->pad_oheight; 
 
-    build_filter(&s->h_filters[0][0], (float) s->pad_owidth  / 
-            (float) (iwidth - leftBand - rightBand));
-    build_filter(&s->v_filters[0][0], (float) s->pad_oheight / 
-            (float) (iheight - topBand - bottomBand));
+    av_build_filter(&s->h_filters[0][0], (float) s->pad_owidth  / 
+            (float) (iwidth - leftBand - rightBand), NB_TAPS, NB_PHASES, 1<<FILTER_BITS, 0);
+    av_build_filter(&s->v_filters[0][0], (float) s->pad_oheight / 
+            (float) (iheight - topBand - bottomBand), NB_TAPS, NB_PHASES, 1<<FILTER_BITS, 0);
 
     return s;
 fail:
@@ -666,21 +628,7 @@ void img_resample_close(ImgReSampleContext *s)
 }
 
 #ifdef TEST
-
-void *av_mallocz(int size)
-{
-    void *ptr;
-    ptr = malloc(size);
-    memset(ptr, 0, size);
-    return ptr;
-}
-
-void av_free(void *ptr)
-{
-    /* XXX: this test should not be needed on most libcs */
-    if (ptr)
-        free(ptr);
-}
+#include <stdio.h>
 
 /* input */
 #define XSIZE 256
@@ -695,11 +643,13 @@ uint8_t img2[XSIZE1 * YSIZE1];
 
 void save_pgm(const char *filename, uint8_t *img, int xsize, int ysize)
 {
+#undef fprintf
     FILE *f;
     f=fopen(filename,"w");
     fprintf(f,"P5\n%d %d\n%d\n", xsize, ysize, 255);
     fwrite(img,1, xsize * ysize,f);
     fclose(f);
+#define fprintf please_use_av_log
 }
 
 static void dump_filter(int16_t *filter)
@@ -707,11 +657,11 @@ static void dump_filter(int16_t *filter)
     int i, ph;
 
     for(ph=0;ph<NB_PHASES;ph++) {
-        printf("%2d: ", ph);
+        av_log(NULL, AV_LOG_INFO, "%2d: ", ph);
         for(i=0;i<NB_TAPS;i++) {
-            printf(" %5.2f", filter[ph * NB_TAPS + i] / 256.0);
+            av_log(NULL, AV_LOG_INFO, " %5.2f", filter[ph * NB_TAPS + i] / 256.0);
         }
-        printf("\n");
+        av_log(NULL, AV_LOG_INFO, "\n");
     }
 }
 
@@ -775,20 +725,20 @@ int main(int argc, char **argv)
         fact = factors[i];
         xsize = (int)(XSIZE * fact);
         ysize = (int)((YSIZE - 100) * fact);
-        s = img_resample_full_init(xsize, ysize, XSIZE, YSIZE, 50 ,50, 0, 0);
-        printf("Factor=%0.2f\n", fact);
+        s = img_resample_full_init(xsize, ysize, XSIZE, YSIZE, 50 ,50, 0, 0, 0, 0, 0, 0);
+        av_log(NULL, AV_LOG_INFO, "Factor=%0.2f\n", fact);
         dump_filter(&s->h_filters[0][0]);
         component_resample(s, img1, xsize, xsize, ysize,
                            img + 50 * XSIZE, XSIZE, XSIZE, YSIZE - 100);
         img_resample_close(s);
 
-        sprintf(buf, "/tmp/out%d.pgm", i);
+        snprintf(buf, sizeof(buf), "/tmp/out%d.pgm", i);
         save_pgm(buf, img1, xsize, ysize);
     }
 
     /* mmx test */
 #ifdef HAVE_MMX
-    printf("MMX test\n");
+    av_log(NULL, AV_LOG_INFO, "MMX test\n");
     fact = 0.72;
     xsize = (int)(XSIZE * fact);
     ysize = (int)(YSIZE * fact);
@@ -802,10 +752,10 @@ int main(int argc, char **argv)
     component_resample(s, img2, xsize, xsize, ysize,
                        img, XSIZE, XSIZE, YSIZE);
     if (memcmp(img1, img2, xsize * ysize) != 0) {
-        fprintf(stderr, "mmx error\n");
+        av_log(NULL, AV_LOG_ERROR, "mmx error\n");
         exit(1);
     }
-    printf("MMX OK\n");
+    av_log(NULL, AV_LOG_INFO, "MMX OK\n");
 #endif
     return 0;
 }