Revision 085565f7 libavcodec/ffv1.c

View differences:

libavcodec/ffv1.c
209 209
/**
210 210
 * put 
211 211
 */
212
static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){
212
static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed, int max_exp){
213 213
    int i;
214 214

  
215 215
    if(v){
......
217 217
        const int e= av_log2(a);
218 218

  
219 219
        put_cabac(c, state+0, 0);
220
        put_cabac_u(c, state+1, e, 7, 6, 1); //1..7
221
        if(e<7){
220
        
221
        for(i=0; i<e; i++){
222
            put_cabac(c, state+1+i, 1);  //1..8
223
        }
224

  
225
        if(e<max_exp){
226
            put_cabac(c, state+1+i, 0);      //1..8
227

  
222 228
            for(i=e-1; i>=0; i--){
223
                static const int offset[7]= {15+0, 15+0, 15+1, 15+3, 15+6, 15+10, 15+11};
224
                put_cabac(c, state+offset[e]+i, (a>>i)&1); //15..31
229
                put_cabac(c, state+16+e+i, (a>>i)&1); //17..29
225 230
            }
226 231
            if(is_signed)
227
                put_cabac(c, state+8 + e, v < 0); //8..14
232
                put_cabac(c, state+9 + e, v < 0); //9..16
228 233
        }
229 234
    }else{
230 235
        put_cabac(c, state+0, 1);
231 236
    }
232 237
}
233 238

  
234
static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed){
235
    int i;
236

  
239
static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed, int max_exp){
237 240
    if(get_cabac(c, state+0))
238 241
        return 0;
239 242
    else{
240
        const int e= get_cabac_u(c, state+1, 7, 6, 1); //1..7
241
        
242
        if(e<7){
243
        int i, e;
244
 
245
        for(e=0; e<max_exp; e++){ 
243 246
            int a= 1<<e;
244 247

  
245
            for(i=e-1; i>=0; i--){
246
                static const int offset[7]= {15+0, 15+0, 15+1, 15+3, 15+6, 15+10, 15+11};
247
                a += get_cabac(c, state+offset[e]+i)<<i; //14..31
248
            }
248
            if(get_cabac(c, state + 1 + e)==0){ // 1..8
249
                for(i=e-1; i>=0; i--){
250
                    a += get_cabac(c, state+16+e+i)<<i; //17..29
251
                }
249 252

  
250
            if(is_signed && get_cabac(c, state+8 + e)) //8..14
251
                return -a;
252
            else
253
                return a;
254
        }else
255
            return -128;
253
                if(is_signed && get_cabac(c, state+9 + e)) //9..16
254
                    return -a;
255
                else
256
                    return a;
257
            }
258
        }
259
        return -(1<<e);
256 260
    }
257 261
}
258 262

  
......
382 386

  
383 387
            diff= (int8_t)diff;
384 388

  
385
            if(s->ac)
386
                put_symbol(c, p->state[context], diff, 1);
387
            else{
389
            if(s->ac){
390
                put_symbol(c, p->state[context], diff, 1, 7);
391
            }else{
388 392
                if(context == 0) run_mode=1;
389 393
                
390 394
                if(run_mode){
......
434 438

  
435 439
    for(i=1; i<128 ; i++){
436 440
        if(quant_table[i] != quant_table[i-1]){
437
            put_symbol(c, state, i-last-1, 0);
441
            put_symbol(c, state, i-last-1, 0, 7);
438 442
            last= i;
439 443
        }
440 444
    }
441
    put_symbol(c, state, i-last-1, 0);
445
    put_symbol(c, state, i-last-1, 0, 7);
442 446
}
443 447

  
444 448
static void write_header(FFV1Context *f){
......
446 450
    int i;
447 451
    CABACContext * const c= &f->c;
448 452

  
449
    put_symbol(c, state, f->version, 0);
450
    put_symbol(c, state, f->avctx->coder_type, 0);
451
    put_symbol(c, state, 0, 0); //YUV cs type 
453
    put_symbol(c, state, f->version, 0, 7);
454
    put_symbol(c, state, f->avctx->coder_type, 0, 7);
455
    put_symbol(c, state, 0, 0, 7); //YUV cs type 
452 456
    put_cabac(c, state, 1); //chroma planes
453
        put_symbol(c, state, f->chroma_h_shift, 0);
454
        put_symbol(c, state, f->chroma_v_shift, 0);
457
        put_symbol(c, state, f->chroma_h_shift, 0, 7);
458
        put_symbol(c, state, f->chroma_v_shift, 0, 7);
455 459
    put_cabac(c, state, 0); //no transparency plane
456 460

  
457 461
    for(i=0; i<5; i++)
......
673 677
            
674 678

  
675 679
            if(s->ac)
676
                diff= get_symbol(c, p->state[context], 1);
680
                diff= get_symbol(c, p->state[context], 1, 7);
677 681
            else{
678 682
                if(context == 0 && run_mode==0) run_mode=1;
679 683
                
......
719 723
    uint8_t state[CONTEXT_SIZE]={0};
720 724

  
721 725
    for(v=0; i<128 ; v++){
722
        int len= get_symbol(c, state, 0) + 1;
726
        int len= get_symbol(c, state, 0, 7) + 1;
723 727

  
724 728
        if(len + i > 128) return -1;
725 729
        
......
744 748
    int i, context_count;
745 749
    CABACContext * const c= &f->c;
746 750
    
747
    f->version= get_symbol(c, state, 0);
748
    f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
749
    get_symbol(c, state, 0); //YUV cs type
751
    f->version= get_symbol(c, state, 0, 7);
752
    f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7);
753
    get_symbol(c, state, 0, 7); //YUV cs type
750 754
    get_cabac(c, state); //no chroma = false
751
    f->chroma_h_shift= get_symbol(c, state, 0);
752
    f->chroma_v_shift= get_symbol(c, state, 0);
755
    f->chroma_h_shift= get_symbol(c, state, 0, 7);
756
    f->chroma_v_shift= get_symbol(c, state, 0, 7);
753 757
    get_cabac(c, state); //transparency plane
754 758
    f->plane_count= 3;
755
    
759

  
760
    switch(16*f->chroma_h_shift + f->chroma_v_shift){
761
    case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
762
    case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
763
    case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
764
    case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
765
    case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
766
    default:
767
        fprintf(stderr, "format not supported\n");
768
        return -1;
769
    }
770
//printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
771

  
756 772
    context_count=1;
757 773
    for(i=0; i<5; i++){
758 774
        context_count*= read_quant_table(c, f->quant_table[i], context_count);
775
        if(context_count < 0){
776
            printf("read_quant_table error\n");
777
            return -1;
778
        }
759 779
    }
760 780
    context_count= (context_count+1)/2;
761 781
    
......
823 843
    ff_init_cabac_decoder(c, buf, buf_size);
824 844
    ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
825 845

  
826
    p->reference= 0;
827
    if(avctx->get_buffer(avctx, p) < 0){
828
        fprintf(stderr, "get_buffer() failed\n");
829
        return -1;
830
    }
831

  
832 846
    p->pict_type= FF_I_TYPE; //FIXME I vs. P
833 847
    if(get_cabac_bypass(c)){
834 848
        p->key_frame= 1;
......
837 851
    }else{
838 852
        p->key_frame= 0;
839 853
    }
854

  
855
    p->reference= 0;
856
    if(avctx->get_buffer(avctx, p) < 0){
857
        fprintf(stderr, "get_buffer() failed\n");
858
        return -1;
859
    }
860

  
840 861
    if(avctx->debug&FF_DEBUG_PICT_INFO)
841
        printf("keyframe:%d\n", p->key_frame);
862
        printf("keyframe:%d coder:%d\n", p->key_frame, f->ac);
842 863
    
843 864
    if(!f->ac){
844 865
        bytes_read = get_cabac_terminate(c);

Also available in: Unified diff