Make 1pass statistic collection in ffv1 put_symbol_inline() conditional to avoid...
[ffmpeg.git] / libavcodec / ffv1.c
index 0094197..0f1433b 100644 (file)
@@ -38,6 +38,7 @@
 #define CONTEXT_SIZE 32
 
 #define MAX_QUANT_TABLES 8
+#define MAX_CONTEXT_INPUTS 5
 
 extern const uint8_t ff_log2_run[32];
 
@@ -216,7 +217,8 @@ typedef struct VlcState{
 } VlcState;
 
 typedef struct PlaneContext{
-    int16_t quant_table[5][256];
+    int16_t quant_table[MAX_CONTEXT_INPUTS][256];
+    int quant_table_index;
     int context_count;
     uint8_t (*state)[CONTEXT_SIZE];
     VlcState *vlc_state;
@@ -230,6 +232,7 @@ typedef struct FFV1Context{
     RangeCoder c;
     GetBitContext gb;
     PutBitContext pb;
+    uint64_t rc_stat[256][2];
     int version;
     int width, height;
     int chroma_h_shift, chroma_v_shift;
@@ -239,8 +242,8 @@ typedef struct FFV1Context{
     int plane_count;
     int ac;                              ///< 1=range coder <-> 0=golomb rice
     PlaneContext plane[MAX_PLANES];
-    int16_t quant_table[5][256];
-    int16_t quant_tables[MAX_QUANT_TABLES][5][256];
+    int16_t quant_table[MAX_CONTEXT_INPUTS][256];
+    int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
     int context_count[MAX_QUANT_TABLES];
     uint8_t state_transition[256];
     int run_index;
@@ -296,9 +299,17 @@ static inline int get_context(PlaneContext *p, int_fast16_t *src, int_fast16_t *
         return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
 }
 
-static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed){
+static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2]){
     int i;
 
+#define put_rac(C,S,B) \
+do{\
+    if(rc_stat){\
+    rc_stat[*(S)][B]++;\
+    }\
+    put_rac(C,S,B);\
+}while(0)
+
     if(v){
         const int a= FFABS(v);
         const int e= av_log2(a);
@@ -331,10 +342,11 @@ static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int i
     }else{
         put_rac(c, state+0, 1);
     }
+#undef put_rac
 }
 
 static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
-    put_symbol_inline(c, state, v, is_signed);
+    put_symbol_inline(c, state, v, is_signed, NULL);
 }
 
 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
@@ -448,7 +460,7 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int
 }
 
 #if CONFIG_FFV1_ENCODER
-static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
+static av_always_inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
     PlaneContext * const p= &s->plane[plane_index];
     RangeCoder * const c= &s->c;
     int x;
@@ -482,7 +494,11 @@ static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], in
         diff= fold(diff, bits);
 
         if(s->ac){
-            put_symbol_inline(c, p->state[context], diff, 1);
+            if(s->flags & CODEC_FLAG_PASS1){
+            put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat);
+            }else{
+                put_symbol_inline(c, p->state[context], diff, 1, NULL);
+            }
         }else{
             if(context == 0) run_mode=1;
 
@@ -610,7 +626,7 @@ static void write_quant_table(RangeCoder *c, int16_t *quant_table){
     put_symbol(c, state, i-last-1, 0);
 }
 
-static void write_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
+static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
     int i;
     for(i=0; i<5; i++)
         write_quant_table(c, quant_table[i]);
@@ -618,7 +634,7 @@ static void write_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
 
 static void write_header(FFV1Context *f){
     uint8_t state[CONTEXT_SIZE];
-    int i;
+    int i, j;
     RangeCoder * const c= &f->slice_context[0]->c;
 
     memset(state, 128, sizeof(state));
@@ -628,8 +644,7 @@ static void write_header(FFV1Context *f){
         put_symbol(c, state, f->ac, 0);
         if(f->ac>1){
             for(i=1; i<256; i++){
-                f->state_transition[i]=ver2_state[i];
-                put_symbol(c, state, ver2_state[i] - c->one_state[i], 1);
+                put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
             }
         }
         put_symbol(c, state, f->colorspace, 0); //YUV cs type
@@ -642,8 +657,18 @@ static void write_header(FFV1Context *f){
 
         write_quant_tables(c, f->quant_table);
     }else{
-        for(i=0; i<f->slice_count * f->plane_count; i++)
-            put_symbol(c, state, f->avctx->context_model, 0);
+        put_symbol(c, state, f->slice_count, 0);
+        for(i=0; i<f->slice_count; i++){
+            FFV1Context *fs= f->slice_context[i];
+            put_symbol(c, state, (fs->slice_x     +1)*f->num_h_slices / f->width   , 0);
+            put_symbol(c, state, (fs->slice_y     +1)*f->num_v_slices / f->height  , 0);
+            put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
+            put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
+            for(j=0; j<f->plane_count; j++){
+                put_symbol(c, state, f->plane[j].quant_table_index, 0);
+                av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
+            }
+        }
     }
 }
 #endif /* CONFIG_FFV1_ENCODER */
@@ -668,43 +693,6 @@ static av_cold int common_init(AVCodecContext *avctx){
     return 0;
 }
 
-#if CONFIG_FFV1_ENCODER
-static int write_extra_header(FFV1Context *f){
-    RangeCoder * const c= &f->c;
-    uint8_t state[CONTEXT_SIZE];
-    int i;
-    memset(state, 128, sizeof(state));
-
-    f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000);
-    ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
-    ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
-
-    put_symbol(c, state, f->version, 0);
-    put_symbol(c, state, f->ac, 0);
-    if(f->ac>1){
-        for(i=1; i<256; i++){
-            f->state_transition[i]=ver2_state[i];
-            put_symbol(c, state, ver2_state[i] - c->one_state[i], 1);
-        }
-    }
-    put_symbol(c, state, f->colorspace, 0); //YUV cs type
-    put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
-    put_rac(c, state, 1); //chroma planes
-        put_symbol(c, state, f->chroma_h_shift, 0);
-        put_symbol(c, state, f->chroma_v_shift, 0);
-    put_rac(c, state, 0); //no transparency plane
-    put_symbol(c, state, f->num_h_slices-1, 0);
-    put_symbol(c, state, f->num_v_slices-1, 0);
-
-    put_symbol(c, state, f->quant_table_count, 0);
-    for(i=0; i<f->quant_table_count; i++)
-        write_quant_tables(c, f->quant_tables[i]);
-
-    f->avctx->extradata_size= ff_rac_terminate(c);
-
-    return 0;
-}
-
 static int init_slice_state(FFV1Context *f){
     int i, j;
 
@@ -757,23 +745,106 @@ static av_cold int init_slice_contexts(FFV1Context *f){
         fs->slice_x     = sxs;
         fs->slice_y     = sys;
 
-        fs->sample_buffer = av_malloc(6 * (fs->slice_width+6) * sizeof(*fs->sample_buffer));
+        fs->sample_buffer = av_malloc(6 * (fs->width+6) * sizeof(*fs->sample_buffer));
         if (!fs->sample_buffer)
             return AVERROR(ENOMEM);
     }
     return 0;
 }
 
+#if CONFIG_FFV1_ENCODER
+static int write_extra_header(FFV1Context *f){
+    RangeCoder * const c= &f->c;
+    uint8_t state[CONTEXT_SIZE];
+    int i;
+    memset(state, 128, sizeof(state));
+
+    f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000);
+    ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
+    ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
+
+    put_symbol(c, state, f->version, 0);
+    put_symbol(c, state, f->ac, 0);
+    if(f->ac>1){
+        for(i=1; i<256; i++){
+            put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
+        }
+    }
+    put_symbol(c, state, f->colorspace, 0); //YUV cs type
+    put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
+    put_rac(c, state, 1); //chroma planes
+        put_symbol(c, state, f->chroma_h_shift, 0);
+        put_symbol(c, state, f->chroma_v_shift, 0);
+    put_rac(c, state, 0); //no transparency plane
+    put_symbol(c, state, f->num_h_slices-1, 0);
+    put_symbol(c, state, f->num_v_slices-1, 0);
+
+    put_symbol(c, state, f->quant_table_count, 0);
+    for(i=0; i<f->quant_table_count; i++)
+        write_quant_tables(c, f->quant_tables[i]);
+
+    f->avctx->extradata_size= ff_rac_terminate(c);
+
+    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;
+    int i, j;
 
     common_init(avctx);
 
     s->version=0;
     s->ac= avctx->coder_type ? 2:0;
 
+    if(s->ac>1)
+        for(i=1; i<256; i++)
+            s->state_transition[i]=ver2_state[i];
+
     s->plane_count=2;
     for(i=0; i<256; i++){
         s->quant_table_count=2;
@@ -797,17 +868,16 @@ static av_cold int encode_init(AVCodecContext *avctx)
             s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
         }
     }
+    s->context_count[0]= (11*11*11+1)/2;
+    s->context_count[1]= (11*11*5*5*5+1)/2;
     memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
 
     for(i=0; i<s->plane_count; i++){
         PlaneContext * const p= &s->plane[i];
 
         memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
-        if(avctx->context_model==0){
-            p->context_count= (11*11*11+1)/2;
-        }else{
-            p->context_count= (11*11*5*5*5+1)/2;
-        }
+        p->quant_table_index= avctx->context_model;
+        p->context_count= s->context_count[p->quant_table_index];
     }
 
     avctx->coded_frame= &s->picture;
@@ -842,6 +912,27 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     s->picture_number=0;
 
+    if(avctx->stats_in){
+        char *p= avctx->stats_in;
+
+        for(;;){
+            for(j=0; j<256; j++){
+                for(i=0; i<2; i++){
+                    char *next;
+                    s->rc_stat[j][i]= strtol(p, &next, 0);
+                    if(next==p){
+                        av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
+                        return -1;
+                    }
+                    p=next;
+                }
+            }
+            while(*p=='\n' || *p==' ') p++;
+            if(p[0]==0) break;
+        }
+        sort_stt(s, s->state_transition);
+    }
+
     if(s->version>1){
         s->num_h_slices=2;
         s->num_v_slices=2;
@@ -853,6 +944,8 @@ static av_cold int encode_init(AVCodecContext *avctx)
     if(init_slice_state(s) < 0)
         return -1;
 
+    avctx->stats_out= av_mallocz(1024*30);
+
     return 0;
 }
 #endif /* CONFIG_FFV1_ENCODER */
@@ -986,6 +1079,28 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
         buf_p += bytes;
     }
 
+    if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
+        int j;
+        char *p= avctx->stats_out;
+        char *end= p + 1024*30;
+
+        memset(f->rc_stat, 0, sizeof(f->rc_stat));
+        for(j=0; j<f->slice_count; j++){
+            FFV1Context *fs= f->slice_context[j];
+            for(i=0; i<256; i++){
+                f->rc_stat[i][0] += fs->rc_stat[i][0];
+                f->rc_stat[i][1] += fs->rc_stat[i][1];
+            }
+        }
+
+        for(j=0; j<256; j++){
+            snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
+            p+= strlen(p);
+        }
+        snprintf(p, end-p, "\n");
+    } else
+        avctx->stats_out[0] = '\0';
+
     f->picture_number++;
     return buf_p-buf;
 }
@@ -1006,6 +1121,8 @@ static av_cold int common_end(AVCodecContext *avctx){
         av_freep(&fs->sample_buffer);
     }
 
+    av_freep(&avctx->stats_out);
+
     return 0;
 }
 
@@ -1200,7 +1317,7 @@ static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
     return 2*v - 1;
 }
 
-static int read_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
+static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
     int i;
     int context_count=1;
 
@@ -1239,7 +1356,7 @@ static int read_extra_header(FFV1Context *f){
     f->plane_count= 2;
     f->num_h_slices= 1 + get_symbol(c, state, 0);
     f->num_v_slices= 1 + get_symbol(c, state, 0);
-    if(f->num_h_slices > 256U || f->num_v_slices > 256U || f->num_h_slices*f->num_v_slices > MAX_SLICES){
+    if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
         av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
         return -1;
     }
@@ -1322,10 +1439,33 @@ static int read_header(FFV1Context *f){
                 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
                 return -1;
         }
+    }else{
+        f->slice_count= get_symbol(c, state, 0);
+        if(f->slice_count > (unsigned)MAX_SLICES)
+            return -1;
     }
+
     for(j=0; j<f->slice_count; j++){
         FFV1Context *fs= f->slice_context[j];
         fs->ac= f->ac;
+
+        if(f->version >= 2){
+            fs->slice_x     = get_symbol(c, state, 0)   *f->width ;
+            fs->slice_y     = get_symbol(c, state, 0)   *f->height;
+            fs->slice_width =(get_symbol(c, state, 0)+1)*f->width  + fs->slice_x;
+            fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
+
+            fs->slice_x /= f->num_h_slices;
+            fs->slice_y /= f->num_v_slices;
+            fs->slice_width  = fs->slice_width /f->num_h_slices - fs->slice_x;
+            fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
+            if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
+                return -1;
+            if(    (unsigned)fs->slice_x + (uint64_t)fs->slice_width  > f->width
+                || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
+                return -1;
+        }
+
         for(i=0; i<f->plane_count; i++){
             PlaneContext * const p= &fs->plane[i];
 
@@ -1335,6 +1475,7 @@ static int read_header(FFV1Context *f){
                     av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
                     return -1;
                 }
+                p->quant_table_index= idx;
                 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
                 context_count= f->context_count[idx];
             }else{