Move ffv1 state transition table sorting to its own function.
authorMichael Niedermayer <michaelni@gmx.at>
Sun, 24 Oct 2010 16:55:42 +0000 (16:55 +0000)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 24 Oct 2010 16:55:42 +0000 (16:55 +0000)
Originally committed as revision 25563 to svn://svn.ffmpeg.org/ffmpeg/trunk

libavcodec/ffv1.c

index 2ac13a3..825b926 100644 (file)
@@ -780,10 +780,53 @@ static int write_extra_header(FFV1Context *f){
     return 0;
 }
 
+static int sort_stt(FFV1Context *s, uint8_t stt[256]){
+    int i,i2,changed,print=0;
+
+    do{
+        changed=0;
+        for(i=12; i<244; i++){
+            for(i2=i+1; i2<245 && i2<i+4; i2++){
+#define COST(old, new) \
+    s->rc_stat[old][0]*-log2((256-(new))/256.0)\
+   +s->rc_stat[old][1]*-log2(     (new) /256.0)
+
+#define COST2(old, new) \
+    COST(old, new)\
+   +COST(256-(old), 256-(new))
+
+                double size0= COST2(i, i ) + COST2(i2, i2);
+                double sizeX= COST2(i, i2) + COST2(i2, i );
+                if(sizeX < size0 && i!=128 && i2!=128){
+                    int j;
+                    FFSWAP(int, stt[    i], stt[    i2]);
+                    FFSWAP(int, s->rc_stat[i    ][0],s->rc_stat[    i2][0]);
+                    FFSWAP(int, s->rc_stat[i    ][1],s->rc_stat[    i2][1]);
+                    if(i != 256-i2){
+                        FFSWAP(int, stt[256-i], stt[256-i2]);
+                        FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
+                        FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
+                    }
+                    for(j=1; j<256; j++){
+                        if     (stt[j] == i ) stt[j] = i2;
+                        else if(stt[j] == i2) stt[j] = i ;
+                        if(i != 256-i2){
+                            if     (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
+                            else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
+                        }
+                    }
+                    print=changed=1;
+                }
+            }
+        }
+    }while(changed);
+    return print;
+}
+
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     FFV1Context *s = avctx->priv_data;
-    int i, j, i2;
+    int i, j;
 
     common_init(avctx);
 
@@ -864,7 +907,6 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     if(avctx->stats_in){
         char *p= avctx->stats_in;
-        int changed;
 
         for(;;){
             for(j=0; j<256; j++){
@@ -881,44 +923,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
             while(*p=='\n' || *p==' ') p++;
             if(p[0]==0) break;
         }
-
-        do{
-            changed=0;
-            for(i=12; i<244; i++){
-                for(i2=i+1; i2<245 && i2<i+4; i2++){
-#define COST(old, new) \
-    s->rc_stat[old][0]*-log2((256-(new))/256.0)\
-   +s->rc_stat[old][1]*-log2(     (new) /256.0)
-
-#define COST2(old, new) \
-    COST(old, new)\
-   +COST(256-(old), 256-(new))
-
-                    double size0= COST2(i, i ) + COST2(i2, i2);
-                    double sizeX= COST2(i, i2) + COST2(i2, i );
-                    if(sizeX < size0 && i!=128 && i2!=128){
-                        int j;
-                        FFSWAP(int, s->state_transition[    i], s->state_transition[    i2]);
-                        FFSWAP(int, s->rc_stat[i    ][0],s->rc_stat[    i2][0]);
-                        FFSWAP(int, s->rc_stat[i    ][1],s->rc_stat[    i2][1]);
-                        if(i != 256-i2){
-                            FFSWAP(int, s->state_transition[256-i], s->state_transition[256-i2]);
-                        FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
-                        FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
-                        }
-                        for(j=1; j<256; j++){
-                            if     (s->state_transition[j] == i ) s->state_transition[j] = i2;
-                            else if(s->state_transition[j] == i2) s->state_transition[j] = i ;
-                            if(i != 256-i2){
-                            if     (s->state_transition[256-j] == 256-i ) s->state_transition[256-j] = 256-i2;
-                            else if(s->state_transition[256-j] == 256-i2) s->state_transition[256-j] = 256-i ;
-                            }
-                        }
-                        changed=1;
-                    }
-                }
-            }
-        }while(changed);
+        sort_stt(s, s->state_transition);
     }
 
     if(s->version>1){