Browse code

ffv1enc: better heuristic to calculate initial states

Slightly improves compression of 2pass files

Tested-by: "Peter B." <pb@das-werkstatt.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/02/05 01:33:07
Showing 1 changed files
... ...
@@ -856,18 +856,29 @@ static av_cold int encode_init(AVCodecContext *avctx)
856 856
         find_best_state(best_state, s->state_transition);
857 857
 
858 858
         for (i = 0; i < s->quant_table_count; i++) {
859
-            for (j = 0; j < s->context_count[i]; j++)
860
-                for (k = 0; k < 32; k++) {
859
+            for (k = 0; k < 32; k++) {
860
+                double a=0, b=0;
861
+                int jp = 0;
862
+                for (j = 0; j < s->context_count[i]; j++) {
861 863
                     double p = 128;
862
-                    if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
863
-                        p = 256.0 * s->rc_stat2[i][j][k][1] /
864
-                            (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
864
+                    if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
865
+                        if (a+b)
866
+                            p = 256.0 * b / (a + b);
867
+                        s->initial_states[i][jp][k] =
868
+                            best_state[av_clip(round(p), 1, 255)][av_clip((a + b) / gob_count, 0, 255)];
869
+                        for(jp++; jp<j; jp++)
870
+                            s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
871
+                        a=b=0;
872
+                    }
873
+                    a += s->rc_stat2[i][j][k][0];
874
+                    b += s->rc_stat2[i][j][k][1];
875
+                    if (a+b) {
876
+                        p = 256.0 * b / (a + b);
865 877
                     }
866 878
                     s->initial_states[i][j][k] =
867
-                        best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
868
-                                                                       s->rc_stat2[i][j][k][1]) /
869
-                                                                      gob_count, 0, 255)];
879
+                        best_state[av_clip(round(p), 1, 255)][av_clip((a + b) / gob_count, 0, 255)];
870 880
                 }
881
+            }
871 882
         }
872 883
     }
873 884