00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "common.h"
00028 #include "bitstream.h"
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "rangecoder.h"
00032 #include "golomb.h"
00033
00034 #define MAX_PLANES 4
00035 #define CONTEXT_SIZE 32
00036
00037 static const int8_t quant3[256]={
00038 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00039 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00040 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00041 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00042 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00043 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00044 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00045 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00046 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00047 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00048 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00049 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00050 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00051 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00052 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00053 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
00054 };
00055 static const int8_t quant5[256]={
00056 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00057 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00058 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00059 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00060 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00061 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00062 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00063 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00064 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00065 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00066 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00067 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00068 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00069 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00070 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00071 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
00072 };
00073 static const int8_t quant7[256]={
00074 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00075 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00076 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
00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,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00142 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00143 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
00144 };
00145
00146 static const uint8_t log2_run[32]={
00147 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
00148 4, 4, 5, 5, 6, 6, 7, 7,
00149 8, 9,10,11,12,13,14,15,
00150 };
00151
00152 typedef struct VlcState{
00153 int16_t drift;
00154 uint16_t error_sum;
00155 int8_t bias;
00156 uint8_t count;
00157 } VlcState;
00158
00159 typedef struct PlaneContext{
00160 int context_count;
00161 uint8_t (*state)[CONTEXT_SIZE];
00162 VlcState *vlc_state;
00163 uint8_t interlace_bit_state[2];
00164 } PlaneContext;
00165
00166 typedef struct FFV1Context{
00167 AVCodecContext *avctx;
00168 RangeCoder c;
00169 GetBitContext gb;
00170 PutBitContext pb;
00171 int version;
00172 int width, height;
00173 int chroma_h_shift, chroma_v_shift;
00174 int flags;
00175 int picture_number;
00176 AVFrame picture;
00177 int plane_count;
00178 int ac;
00179 PlaneContext plane[MAX_PLANES];
00180 int16_t quant_table[5][256];
00181 int run_index;
00182 int colorspace;
00183
00184 DSPContext dsp;
00185 }FFV1Context;
00186
00187 static always_inline int fold(int diff, int bits){
00188 if(bits==8)
00189 diff= (int8_t)diff;
00190 else{
00191 diff+= 1<<(bits-1);
00192 diff&=(1<<bits)-1;
00193 diff-= 1<<(bits-1);
00194 }
00195
00196 return diff;
00197 }
00198
00199 static inline int predict(int_fast16_t *src, int_fast16_t *last){
00200 const int LT= last[-1];
00201 const int T= last[ 0];
00202 const int L = src[-1];
00203
00204 return mid_pred(L, L + T - LT, T);
00205 }
00206
00207 static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
00208 const int LT= last[-1];
00209 const int T= last[ 0];
00210 const int RT= last[ 1];
00211 const int L = src[-1];
00212
00213 if(f->quant_table[3][127]){
00214 const int TT= last2[0];
00215 const int LL= src[-2];
00216 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
00217 +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
00218 }else
00219 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
00220 }
00221
00222 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00223 int i;
00224
00225 if(v){
00226 const int a= ABS(v);
00227 const int e= av_log2(a);
00228 put_rac(c, state+0, 0);
00229
00230 assert(e<=9);
00231
00232 for(i=0; i<e; i++){
00233 put_rac(c, state+1+i, 1);
00234 }
00235 put_rac(c, state+1+i, 0);
00236
00237 for(i=e-1; i>=0; i--){
00238 put_rac(c, state+22+i, (a>>i)&1);
00239 }
00240
00241 if(is_signed)
00242 put_rac(c, state+11 + e, v < 0);
00243 }else{
00244 put_rac(c, state+0, 1);
00245 }
00246 }
00247
00248 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00249 if(get_rac(c, state+0))
00250 return 0;
00251 else{
00252 int i, e, a;
00253 e= 0;
00254 while(get_rac(c, state+1 + e)){
00255 e++;
00256 }
00257 assert(e<=9);
00258
00259 a= 1;
00260 for(i=e-1; i>=0; i--){
00261 a += a + get_rac(c, state+22 + i);
00262 }
00263
00264 if(is_signed && get_rac(c, state+11 + e))
00265 return -a;
00266 else
00267 return a;
00268 }
00269 }
00270
00271 static inline void update_vlc_state(VlcState * const state, const int v){
00272 int drift= state->drift;
00273 int count= state->count;
00274 state->error_sum += ABS(v);
00275 drift += v;
00276
00277 if(count == 128){
00278 count >>= 1;
00279 drift >>= 1;
00280 state->error_sum >>= 1;
00281 }
00282 count++;
00283
00284 if(drift <= -count){
00285 if(state->bias > -128) state->bias--;
00286
00287 drift += count;
00288 if(drift <= -count)
00289 drift= -count + 1;
00290 }else if(drift > 0){
00291 if(state->bias < 127) state->bias++;
00292
00293 drift -= count;
00294 if(drift > 0)
00295 drift= 0;
00296 }
00297
00298 state->drift= drift;
00299 state->count= count;
00300 }
00301
00302 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00303 int i, k, code;
00304
00305 v = fold(v - state->bias, bits);
00306
00307 i= state->count;
00308 k=0;
00309 while(i < state->error_sum){
00310 k++;
00311 i += i;
00312 }
00313
00314 assert(k<=8);
00315
00316 #if 0 // JPEG LS
00317 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00318 else code= v;
00319 #else
00320 code= v ^ ((2*state->drift + state->count)>>31);
00321 #endif
00322
00323
00324 set_sr_golomb(pb, code, k, 12, bits);
00325
00326 update_vlc_state(state, v);
00327 }
00328
00329 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00330 int k, i, v, ret;
00331
00332 i= state->count;
00333 k=0;
00334 while(i < state->error_sum){
00335 k++;
00336 i += i;
00337 }
00338
00339 assert(k<=8);
00340
00341 v= get_sr_golomb(gb, k, 12, bits);
00342
00343
00344 #if 0 // JPEG LS
00345 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00346 #else
00347 v ^= ((2*state->drift + state->count)>>31);
00348 #endif
00349
00350 ret= fold(v + state->bias, bits);
00351
00352 update_vlc_state(state, v);
00353
00354 return ret;
00355 }
00356
00357 static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00358 PlaneContext * const p= &s->plane[plane_index];
00359 RangeCoder * const c= &s->c;
00360 int x;
00361 int run_index= s->run_index;
00362 int run_count=0;
00363 int run_mode=0;
00364
00365 if(s->ac){
00366 if(c->bytestream_end - c->bytestream < w*20){
00367 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00368 return -1;
00369 }
00370 }else{
00371 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00372 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00373 return -1;
00374 }
00375 }
00376
00377 for(x=0; x<w; x++){
00378 int diff, context;
00379
00380 context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
00381 diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00382
00383 if(context < 0){
00384 context = -context;
00385 diff= -diff;
00386 }
00387
00388 diff= fold(diff, bits);
00389
00390 if(s->ac){
00391 put_symbol(c, p->state[context], diff, 1);
00392 }else{
00393 if(context == 0) run_mode=1;
00394
00395 if(run_mode){
00396
00397 if(diff){
00398 while(run_count >= 1<<log2_run[run_index]){
00399 run_count -= 1<<log2_run[run_index];
00400 run_index++;
00401 put_bits(&s->pb, 1, 1);
00402 }
00403
00404 put_bits(&s->pb, 1 + log2_run[run_index], run_count);
00405 if(run_index) run_index--;
00406 run_count=0;
00407 run_mode=0;
00408 if(diff>0) diff--;
00409 }else{
00410 run_count++;
00411 }
00412 }
00413
00414
00415
00416 if(run_mode == 0)
00417 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00418 }
00419 }
00420 if(run_mode){
00421 while(run_count >= 1<<log2_run[run_index]){
00422 run_count -= 1<<log2_run[run_index];
00423 run_index++;
00424 put_bits(&s->pb, 1, 1);
00425 }
00426
00427 if(run_count)
00428 put_bits(&s->pb, 1, 1);
00429 }
00430 s->run_index= run_index;
00431
00432 return 0;
00433 }
00434
00435 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00436 int x,y,i;
00437 const int ring_size= s->avctx->context_model ? 3 : 2;
00438 int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
00439 s->run_index=0;
00440
00441 memset(sample_buffer, 0, sizeof(sample_buffer));
00442
00443 for(y=0; y<h; y++){
00444 for(i=0; i<ring_size; i++)
00445 sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
00446
00447 sample[0][-1]= sample[1][0 ];
00448 sample[1][ w]= sample[1][w-1];
00449
00450 for(x=0; x<w; x++){
00451 sample[0][x]= src[x + stride*y];
00452 }
00453 encode_line(s, w, sample, plane_index, 8);
00454
00455 }
00456 }
00457
00458 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00459 int x, y, p, i;
00460 const int ring_size= s->avctx->context_model ? 3 : 2;
00461 int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
00462 s->run_index=0;
00463
00464 memset(sample_buffer, 0, sizeof(sample_buffer));
00465
00466 for(y=0; y<h; y++){
00467 for(i=0; i<ring_size; i++)
00468 for(p=0; p<3; p++)
00469 sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
00470
00471 for(x=0; x<w; x++){
00472 int v= src[x + stride*y];
00473 int b= v&0xFF;
00474 int g= (v>>8)&0xFF;
00475 int r= (v>>16)&0xFF;
00476
00477 b -= g;
00478 r -= g;
00479 g += (b + r)>>2;
00480 b += 0x100;
00481 r += 0x100;
00482
00483
00484
00485 sample[0][0][x]= g;
00486 sample[1][0][x]= b;
00487 sample[2][0][x]= r;
00488 }
00489 for(p=0; p<3; p++){
00490 sample[p][0][-1]= sample[p][1][0 ];
00491 sample[p][1][ w]= sample[p][1][w-1];
00492 encode_line(s, w, sample[p], FFMIN(p, 1), 9);
00493 }
00494 }
00495 }
00496
00497 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00498 int last=0;
00499 int i;
00500 uint8_t state[CONTEXT_SIZE];
00501 memset(state, 128, sizeof(state));
00502
00503 for(i=1; i<128 ; i++){
00504 if(quant_table[i] != quant_table[i-1]){
00505 put_symbol(c, state, i-last-1, 0);
00506 last= i;
00507 }
00508 }
00509 put_symbol(c, state, i-last-1, 0);
00510 }
00511
00512 static void write_header(FFV1Context *f){
00513 uint8_t state[CONTEXT_SIZE];
00514 int i;
00515 RangeCoder * const c= &f->c;
00516
00517 memset(state, 128, sizeof(state));
00518
00519 put_symbol(c, state, f->version, 0);
00520 put_symbol(c, state, f->avctx->coder_type, 0);
00521 put_symbol(c, state, f->colorspace, 0);
00522 put_rac(c, state, 1);
00523 put_symbol(c, state, f->chroma_h_shift, 0);
00524 put_symbol(c, state, f->chroma_v_shift, 0);
00525 put_rac(c, state, 0);
00526
00527 for(i=0; i<5; i++)
00528 write_quant_table(c, f->quant_table[i]);
00529 }
00530
00531 static int common_init(AVCodecContext *avctx){
00532 FFV1Context *s = avctx->priv_data;
00533 int width, height;
00534
00535 s->avctx= avctx;
00536 s->flags= avctx->flags;
00537
00538 dsputil_init(&s->dsp, avctx);
00539
00540 width= s->width= avctx->width;
00541 height= s->height= avctx->height;
00542
00543 assert(width && height);
00544
00545 return 0;
00546 }
00547
00548 static int encode_init(AVCodecContext *avctx)
00549 {
00550 FFV1Context *s = avctx->priv_data;
00551 int i;
00552
00553 if(avctx->strict_std_compliance >FF_COMPLIANCE_EXPERIMENTAL){
00554 av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it may not be decodeable with future versions!!!\n"
00555 "use vstrict=-2 / -strict -2 to use it anyway\n");
00556 return -1;
00557 }
00558
00559 common_init(avctx);
00560
00561 s->version=0;
00562 s->ac= avctx->coder_type;
00563
00564 s->plane_count=2;
00565 for(i=0; i<256; i++){
00566 s->quant_table[0][i]= quant11[i];
00567 s->quant_table[1][i]= 11*quant11[i];
00568 if(avctx->context_model==0){
00569 s->quant_table[2][i]= 11*11*quant11[i];
00570 s->quant_table[3][i]=
00571 s->quant_table[4][i]=0;
00572 }else{
00573 s->quant_table[2][i]= 11*11*quant5 [i];
00574 s->quant_table[3][i]= 5*11*11*quant5 [i];
00575 s->quant_table[4][i]= 5*5*11*11*quant5 [i];
00576 }
00577 }
00578
00579 for(i=0; i<s->plane_count; i++){
00580 PlaneContext * const p= &s->plane[i];
00581
00582 if(avctx->context_model==0){
00583 p->context_count= (11*11*11+1)/2;
00584 }else{
00585 p->context_count= (11*11*5*5*5+1)/2;
00586 }
00587
00588 if(s->ac){
00589 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00590 }else{
00591 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00592 }
00593 }
00594
00595 avctx->coded_frame= &s->picture;
00596 switch(avctx->pix_fmt){
00597 case PIX_FMT_YUV444P:
00598 case PIX_FMT_YUV422P:
00599 case PIX_FMT_YUV420P:
00600 case PIX_FMT_YUV411P:
00601 case PIX_FMT_YUV410P:
00602 s->colorspace= 0;
00603 break;
00604 case PIX_FMT_RGBA32:
00605 s->colorspace= 1;
00606 break;
00607 default:
00608 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00609 return -1;
00610 }
00611 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00612
00613 s->picture_number=0;
00614
00615 return 0;
00616 }
00617
00618
00619 static void clear_state(FFV1Context *f){
00620 int i, j;
00621
00622 for(i=0; i<f->plane_count; i++){
00623 PlaneContext *p= &f->plane[i];
00624
00625 p->interlace_bit_state[0]= 128;
00626 p->interlace_bit_state[1]= 128;
00627
00628 for(j=0; j<p->context_count; j++){
00629 if(f->ac){
00630 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
00631 }else{
00632 p->vlc_state[j].drift= 0;
00633 p->vlc_state[j].error_sum= 4;
00634 p->vlc_state[j].bias= 0;
00635 p->vlc_state[j].count= 1;
00636 }
00637 }
00638 }
00639 }
00640
00641 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00642 FFV1Context *f = avctx->priv_data;
00643 RangeCoder * const c= &f->c;
00644 AVFrame *pict = data;
00645 const int width= f->width;
00646 const int height= f->height;
00647 AVFrame * const p= &f->picture;
00648 int used_count= 0;
00649 uint8_t keystate=128;
00650
00651 ff_init_range_encoder(c, buf, buf_size);
00652
00653 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00654
00655 *p = *pict;
00656 p->pict_type= FF_I_TYPE;
00657
00658 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
00659 put_rac(c, &keystate, 1);
00660 p->key_frame= 1;
00661 write_header(f);
00662 clear_state(f);
00663 }else{
00664 put_rac(c, &keystate, 0);
00665 p->key_frame= 0;
00666 }
00667
00668 if(!f->ac){
00669 used_count += ff_rac_terminate(c);
00670
00671 init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
00672 }
00673
00674 if(f->colorspace==0){
00675 const int chroma_width = -((-width )>>f->chroma_h_shift);
00676 const int chroma_height= -((-height)>>f->chroma_v_shift);
00677
00678 encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00679
00680 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00681 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00682 }else{
00683 encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
00684 }
00685 emms_c();
00686
00687 f->picture_number++;
00688
00689 if(f->ac){
00690 return ff_rac_terminate(c);
00691 }else{
00692 flush_put_bits(&f->pb);
00693 return used_count + (put_bits_count(&f->pb)+7)/8;
00694 }
00695 }
00696
00697 static void common_end(FFV1Context *s){
00698 int i;
00699
00700 for(i=0; i<s->plane_count; i++){
00701 PlaneContext *p= &s->plane[i];
00702
00703 av_freep(&p->state);
00704 }
00705 }
00706
00707 static int encode_end(AVCodecContext *avctx)
00708 {
00709 FFV1Context *s = avctx->priv_data;
00710
00711 common_end(s);
00712
00713 return 0;
00714 }
00715
00716 static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00717 PlaneContext * const p= &s->plane[plane_index];
00718 RangeCoder * const c= &s->c;
00719 int x;
00720 int run_count=0;
00721 int run_mode=0;
00722 int run_index= s->run_index;
00723
00724 for(x=0; x<w; x++){
00725 int diff, context, sign;
00726
00727 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
00728 if(context < 0){
00729 context= -context;
00730 sign=1;
00731 }else
00732 sign=0;
00733
00734
00735 if(s->ac){
00736 diff= get_symbol(c, p->state[context], 1);
00737 }else{
00738 if(context == 0 && run_mode==0) run_mode=1;
00739
00740 if(run_mode){
00741 if(run_count==0 && run_mode==1){
00742 if(get_bits1(&s->gb)){
00743 run_count = 1<<log2_run[run_index];
00744 if(x + run_count <= w) run_index++;
00745 }else{
00746 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
00747 else run_count=0;
00748 if(run_index) run_index--;
00749 run_mode=2;
00750 }
00751 }
00752 run_count--;
00753 if(run_count < 0){
00754 run_mode=0;
00755 run_count=0;
00756 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00757 if(diff>=0) diff++;
00758 }else
00759 diff=0;
00760 }else
00761 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00762
00763
00764 }
00765
00766 if(sign) diff= -diff;
00767
00768 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
00769 }
00770 s->run_index= run_index;
00771 }
00772
00773 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00774 int x, y;
00775 int_fast16_t sample_buffer[2][w+6];
00776 int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
00777
00778 s->run_index=0;
00779
00780 memset(sample_buffer, 0, sizeof(sample_buffer));
00781
00782 for(y=0; y<h; y++){
00783 int_fast16_t *temp= sample[0];
00784
00785 sample[0]= sample[1];
00786 sample[1]= temp;
00787
00788 sample[1][-1]= sample[0][0 ];
00789 sample[0][ w]= sample[0][w-1];
00790
00791
00792 decode_line(s, w, sample, plane_index, 8);
00793 for(x=0; x<w; x++){
00794 src[x + stride*y]= sample[1][x];
00795 }
00796
00797 }
00798 }
00799
00800 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00801 int x, y, p;
00802 int_fast16_t sample_buffer[3][2][w+6];
00803 int_fast16_t *sample[3][2]= {
00804 {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
00805 {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
00806 {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
00807
00808 s->run_index=0;
00809
00810 memset(sample_buffer, 0, sizeof(sample_buffer));
00811
00812 for(y=0; y<h; y++){
00813 for(p=0; p<3; p++){
00814 int_fast16_t *temp= sample[p][0];
00815
00816 sample[p][0]= sample[p][1];
00817 sample[p][1]= temp;
00818
00819 sample[p][1][-1]= sample[p][0][0 ];
00820 sample[p][0][ w]= sample[p][0][w-1];
00821 decode_line(s, w, sample[p], FFMIN(p, 1), 9);
00822 }
00823 for(x=0; x<w; x++){
00824 int g= sample[0][1][x];
00825 int b= sample[1][1][x];
00826 int r= sample[2][1][x];
00827
00828
00829
00830
00831 b -= 0x100;
00832 r -= 0x100;
00833 g -= (b + r)>>2;
00834 b += g;
00835 r += g;
00836
00837 src[x + stride*y]= b + (g<<8) + (r<<16);
00838 }
00839 }
00840 }
00841
00842 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
00843 int v;
00844 int i=0;
00845 uint8_t state[CONTEXT_SIZE];
00846
00847 memset(state, 128, sizeof(state));
00848
00849 for(v=0; i<128 ; v++){
00850 int len= get_symbol(c, state, 0) + 1;
00851
00852 if(len + i > 128) return -1;
00853
00854 while(len--){
00855 quant_table[i] = scale*v;
00856 i++;
00857
00858
00859 }
00860 }
00861
00862 for(i=1; i<128; i++){
00863 quant_table[256-i]= -quant_table[i];
00864 }
00865 quant_table[128]= -quant_table[127];
00866
00867 return 2*v - 1;
00868 }
00869
00870 static int read_header(FFV1Context *f){
00871 uint8_t state[CONTEXT_SIZE];
00872 int i, context_count;
00873 RangeCoder * const c= &f->c;
00874
00875 memset(state, 128, sizeof(state));
00876
00877 f->version= get_symbol(c, state, 0);
00878 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
00879 f->colorspace= get_symbol(c, state, 0);
00880 get_rac(c, state);
00881 f->chroma_h_shift= get_symbol(c, state, 0);
00882 f->chroma_v_shift= get_symbol(c, state, 0);
00883 get_rac(c, state);
00884 f->plane_count= 2;
00885
00886 if(f->colorspace==0){
00887 switch(16*f->chroma_h_shift + f->chroma_v_shift){
00888 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
00889 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
00890 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
00891 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
00892 case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
00893 default:
00894 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00895 return -1;
00896 }
00897 }else if(f->colorspace==1){
00898 if(f->chroma_h_shift || f->chroma_v_shift){
00899 av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
00900 return -1;
00901 }
00902 f->avctx->pix_fmt= PIX_FMT_RGBA32;
00903 }else{
00904 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
00905 return -1;
00906 }
00907
00908
00909
00910 context_count=1;
00911 for(i=0; i<5; i++){
00912 context_count*= read_quant_table(c, f->quant_table[i], context_count);
00913 if(context_count < 0 || context_count > 32768){
00914 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
00915 return -1;
00916 }
00917 }
00918 context_count= (context_count+1)/2;
00919
00920 for(i=0; i<f->plane_count; i++){
00921 PlaneContext * const p= &f->plane[i];
00922
00923 p->context_count= context_count;
00924
00925 if(f->ac){
00926 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00927 }else{
00928 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00929 }
00930 }
00931
00932 return 0;
00933 }
00934
00935 static int decode_init(AVCodecContext *avctx)
00936 {
00937
00938
00939 common_init(avctx);
00940
00941 return 0;
00942 }
00943
00944 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
00945 FFV1Context *f = avctx->priv_data;
00946 RangeCoder * const c= &f->c;
00947 const int width= f->width;
00948 const int height= f->height;
00949 AVFrame * const p= &f->picture;
00950 int bytes_read;
00951 uint8_t keystate= 128;
00952
00953 AVFrame *picture = data;
00954
00955 ff_init_range_decoder(c, buf, buf_size);
00956 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00957
00958
00959 p->pict_type= FF_I_TYPE;
00960 if(get_rac(c, &keystate)){
00961 p->key_frame= 1;
00962 read_header(f);
00963 clear_state(f);
00964 }else{
00965 p->key_frame= 0;
00966 }
00967
00968 p->reference= 0;
00969 if(avctx->get_buffer(avctx, p) < 0){
00970 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00971 return -1;
00972 }
00973
00974 if(avctx->debug&FF_DEBUG_PICT_INFO)
00975 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
00976
00977 if(!f->ac){
00978 bytes_read = c->bytestream - c->bytestream_start - 1;
00979 if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n");
00980
00981 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
00982 } else {
00983 bytes_read = 0;
00984 }
00985
00986 if(f->colorspace==0){
00987 const int chroma_width = -((-width )>>f->chroma_h_shift);
00988 const int chroma_height= -((-height)>>f->chroma_v_shift);
00989 decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00990
00991 decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00992 decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00993 }else{
00994 decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
00995 }
00996
00997 emms_c();
00998
00999 f->picture_number++;
01000
01001 *picture= *p;
01002
01003 avctx->release_buffer(avctx, p);
01004
01005 *data_size = sizeof(AVFrame);
01006
01007 if(f->ac){
01008 bytes_read= c->bytestream - c->bytestream_start - 1;
01009 if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
01010 }else{
01011 bytes_read+= (get_bits_count(&f->gb)+7)/8;
01012 }
01013
01014 return bytes_read;
01015 }
01016
01017 AVCodec ffv1_decoder = {
01018 "ffv1",
01019 CODEC_TYPE_VIDEO,
01020 CODEC_ID_FFV1,
01021 sizeof(FFV1Context),
01022 decode_init,
01023 NULL,
01024 NULL,
01025 decode_frame,
01026 CODEC_CAP_DR1 ,
01027 NULL
01028 };
01029
01030 #ifdef CONFIG_ENCODERS
01031 AVCodec ffv1_encoder = {
01032 "ffv1",
01033 CODEC_TYPE_VIDEO,
01034 CODEC_ID_FFV1,
01035 sizeof(FFV1Context),
01036 encode_init,
01037 encode_frame,
01038 encode_end,
01039 };
01040 #endif