comparison ffv1.c @ 2339:b7bb00973f99 libavcodec

switch ffv1 to the put/get_symbol() from snow
author michael
date Sun, 31 Oct 2004 12:22:00 +0000
parents 37516ec97cd6
children 582e635cfa08
comparison
equal deleted inserted replaced
2338:37516ec97cd6 2339:b7bb00973f99
216 +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF]; 216 +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
217 }else 217 }else
218 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]; 218 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
219 } 219 }
220 220
221 /** 221 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
222 * put
223 */
224 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed, int max_exp){
225 int i; 222 int i;
226 223
227 if(v){ 224 if(v){
228 const int a= ABS(v); 225 const int a= ABS(v);
229 const int e= av_log2(a); 226 const int e= av_log2(a);
230
231 put_rac(c, state+0, 0); 227 put_rac(c, state+0, 0);
232 228
229 assert(e<=9);
230
233 for(i=0; i<e; i++){ 231 for(i=0; i<e; i++){
234 put_rac(c, state+1+i, 1); //1..8 232 put_rac(c, state+1+i, 1); //1..10
235 } 233 }
236 234 put_rac(c, state+1+i, 0);
237 if(e<max_exp){ 235
238 put_rac(c, state+1+i, 0); //1..8 236 for(i=e-1; i>=0; i--){
239 237 put_rac(c, state+22+i, (a>>i)&1); //22..31
240 for(i=e-1; i>=0; i--){ 238 }
241 put_rac(c, state+16+e+i, (a>>i)&1); //17..29 239
242 } 240 if(is_signed)
243 if(is_signed) 241 put_rac(c, state+11 + e, v < 0); //11..21
244 put_rac(c, state+9 + e, v < 0); //9..16
245 }
246 }else{ 242 }else{
247 put_rac(c, state+0, 1); 243 put_rac(c, state+0, 1);
248 } 244 }
249 } 245 }
250 246
251 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed, int max_exp){ 247 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
252 if(get_rac(c, state+0)) 248 if(get_rac(c, state+0))
253 return 0; 249 return 0;
254 else{ 250 else{
255 int i, e; 251 int i, e, a;
256 252 e= 0;
257 for(e=0; e<max_exp; e++){ 253 while(get_rac(c, state+1 + e)){ //1..10
258 int a= 1<<e; 254 e++;
259 255 }
260 if(get_rac(c, state + 1 + e)==0){ // 1..8 256 assert(e<=9);
261 for(i=e-1; i>=0; i--){ 257
262 a += get_rac(c, state+16+e+i)<<i; //17..29 258 a= 1;
263 } 259 for(i=e-1; i>=0; i--){
264 260 a += a + get_rac(c, state+22 + i); //22..31
265 if(is_signed && get_rac(c, state+9 + e)) //9..16 261 }
266 return -a; 262
267 else 263 if(is_signed && get_rac(c, state+11 + e)) //11..21
268 return a; 264 return -a;
269 } 265 else
270 } 266 return a;
271 return -(1<<e);
272 } 267 }
273 } 268 }
274 269
275 static inline void update_vlc_state(VlcState * const state, const int v){ 270 static inline void update_vlc_state(VlcState * const state, const int v){
276 int drift= state->drift; 271 int drift= state->drift;
378 } 373 }
379 374
380 diff= fold(diff, bits); 375 diff= fold(diff, bits);
381 376
382 if(s->ac){ 377 if(s->ac){
383 put_symbol(c, p->state[context], diff, 1, bits-1); 378 put_symbol(c, p->state[context], diff, 1);
384 }else{ 379 }else{
385 if(context == 0) run_mode=1; 380 if(context == 0) run_mode=1;
386 381
387 if(run_mode){ 382 if(run_mode){
388 383
490 uint8_t state[CONTEXT_SIZE]; 485 uint8_t state[CONTEXT_SIZE];
491 memset(state, 128, sizeof(state)); 486 memset(state, 128, sizeof(state));
492 487
493 for(i=1; i<128 ; i++){ 488 for(i=1; i<128 ; i++){
494 if(quant_table[i] != quant_table[i-1]){ 489 if(quant_table[i] != quant_table[i-1]){
495 put_symbol(c, state, i-last-1, 0, 7); 490 put_symbol(c, state, i-last-1, 0);
496 last= i; 491 last= i;
497 } 492 }
498 } 493 }
499 put_symbol(c, state, i-last-1, 0, 7); 494 put_symbol(c, state, i-last-1, 0);
500 } 495 }
501 496
502 static void write_header(FFV1Context *f){ 497 static void write_header(FFV1Context *f){
503 uint8_t state[CONTEXT_SIZE]; 498 uint8_t state[CONTEXT_SIZE];
504 int i; 499 int i;
505 RangeCoder * const c= &f->c; 500 RangeCoder * const c= &f->c;
506 501
507 memset(state, 128, sizeof(state)); 502 memset(state, 128, sizeof(state));
508 503
509 put_symbol(c, state, f->version, 0, 7); 504 put_symbol(c, state, f->version, 0);
510 put_symbol(c, state, f->avctx->coder_type, 0, 7); 505 put_symbol(c, state, f->avctx->coder_type, 0);
511 put_symbol(c, state, f->colorspace, 0, 7); //YUV cs type 506 put_symbol(c, state, f->colorspace, 0); //YUV cs type
512 put_rac(c, state, 1); //chroma planes 507 put_rac(c, state, 1); //chroma planes
513 put_symbol(c, state, f->chroma_h_shift, 0, 7); 508 put_symbol(c, state, f->chroma_h_shift, 0);
514 put_symbol(c, state, f->chroma_v_shift, 0, 7); 509 put_symbol(c, state, f->chroma_v_shift, 0);
515 put_rac(c, state, 0); //no transparency plane 510 put_rac(c, state, 0); //no transparency plane
516 511
517 for(i=0; i<5; i++) 512 for(i=0; i<5; i++)
518 write_quant_table(c, f->quant_table[i]); 513 write_quant_table(c, f->quant_table[i]);
519 } 514 }
616 p->interlace_bit_state[1]= 128; 611 p->interlace_bit_state[1]= 128;
617 612
618 for(j=0; j<p->context_count; j++){ 613 for(j=0; j<p->context_count; j++){
619 if(f->ac){ 614 if(f->ac){
620 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE); 615 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
621 p->state[j][7] = 256-8;
622 }else{ 616 }else{
623 p->vlc_state[j].drift= 0; 617 p->vlc_state[j].drift= 0;
624 p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2); 618 p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
625 p->vlc_state[j].bias= 0; 619 p->vlc_state[j].bias= 0;
626 p->vlc_state[j].count= 1; 620 p->vlc_state[j].count= 1;
721 sign=1; 715 sign=1;
722 }else 716 }else
723 sign=0; 717 sign=0;
724 718
725 719
726 if(s->ac) 720 if(s->ac){
727 diff= get_symbol(c, p->state[context], 1, bits-1); 721 diff= get_symbol(c, p->state[context], 1);
728 else{ 722 }else{
729 if(context == 0 && run_mode==0) run_mode=1; 723 if(context == 0 && run_mode==0) run_mode=1;
730 724
731 if(run_mode){ 725 if(run_mode){
732 if(run_count==0 && run_mode==1){ 726 if(run_count==0 && run_mode==1){
733 if(get_bits1(&s->gb)){ 727 if(get_bits1(&s->gb)){
836 uint8_t state[CONTEXT_SIZE]; 830 uint8_t state[CONTEXT_SIZE];
837 831
838 memset(state, 128, sizeof(state)); 832 memset(state, 128, sizeof(state));
839 833
840 for(v=0; i<128 ; v++){ 834 for(v=0; i<128 ; v++){
841 int len= get_symbol(c, state, 0, 7) + 1; 835 int len= get_symbol(c, state, 0) + 1;
842 836
843 if(len + i > 128) return -1; 837 if(len + i > 128) return -1;
844 838
845 while(len--){ 839 while(len--){
846 quant_table[i] = scale*v; 840 quant_table[i] = scale*v;
863 int i, context_count; 857 int i, context_count;
864 RangeCoder * const c= &f->c; 858 RangeCoder * const c= &f->c;
865 859
866 memset(state, 128, sizeof(state)); 860 memset(state, 128, sizeof(state));
867 861
868 f->version= get_symbol(c, state, 0, 7); 862 f->version= get_symbol(c, state, 0);
869 f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7); 863 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
870 f->colorspace= get_symbol(c, state, 0, 7); //YUV cs type 864 f->colorspace= get_symbol(c, state, 0); //YUV cs type
871 get_rac(c, state); //no chroma = false 865 get_rac(c, state); //no chroma = false
872 f->chroma_h_shift= get_symbol(c, state, 0, 7); 866 f->chroma_h_shift= get_symbol(c, state, 0);
873 f->chroma_v_shift= get_symbol(c, state, 0, 7); 867 f->chroma_v_shift= get_symbol(c, state, 0);
874 get_rac(c, state); //transparency plane 868 get_rac(c, state); //transparency plane
875 f->plane_count= 2; 869 f->plane_count= 2;
876 870
877 if(f->colorspace==0){ 871 if(f->colorspace==0){
878 switch(16*f->chroma_h_shift + f->chroma_v_shift){ 872 switch(16*f->chroma_h_shift + f->chroma_v_shift){