00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "avcodec.h"
00029 #include "bitstream.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,
00077 3, 3, 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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 av_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= FFABS(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) && e<9){
00255 e++;
00256 }
00257
00258 a= 1;
00259 for(i=e-1; i>=0; i--){
00260 a += a + get_rac(c, state+22 + i);
00261 }
00262
00263 if(is_signed && get_rac(c, state+11 + e))
00264 return -a;
00265 else
00266 return a;
00267 }
00268 }
00269
00270 static inline void update_vlc_state(VlcState * const state, const int v){
00271 int drift= state->drift;
00272 int count= state->count;
00273 state->error_sum += FFABS(v);
00274 drift += v;
00275
00276 if(count == 128){
00277 count >>= 1;
00278 drift >>= 1;
00279 state->error_sum >>= 1;
00280 }
00281 count++;
00282
00283 if(drift <= -count){
00284 if(state->bias > -128) state->bias--;
00285
00286 drift += count;
00287 if(drift <= -count)
00288 drift= -count + 1;
00289 }else if(drift > 0){
00290 if(state->bias < 127) state->bias++;
00291
00292 drift -= count;
00293 if(drift > 0)
00294 drift= 0;
00295 }
00296
00297 state->drift= drift;
00298 state->count= count;
00299 }
00300
00301 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00302 int i, k, code;
00303
00304 v = fold(v - state->bias, bits);
00305
00306 i= state->count;
00307 k=0;
00308 while(i < state->error_sum){
00309 k++;
00310 i += i;
00311 }
00312
00313 assert(k<=8);
00314
00315 #if 0 // JPEG LS
00316 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00317 else code= v;
00318 #else
00319 code= v ^ ((2*state->drift + state->count)>>31);
00320 #endif
00321
00322
00323 set_sr_golomb(pb, code, k, 12, bits);
00324
00325 update_vlc_state(state, v);
00326 }
00327
00328 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00329 int k, i, v, ret;
00330
00331 i= state->count;
00332 k=0;
00333 while(i < state->error_sum){
00334 k++;
00335 i += i;
00336 }
00337
00338 assert(k<=8);
00339
00340 v= get_sr_golomb(gb, k, 12, bits);
00341
00342
00343 #if 0 // JPEG LS
00344 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00345 #else
00346 v ^= ((2*state->drift + state->count)>>31);
00347 #endif
00348
00349 ret= fold(v + state->bias, bits);
00350
00351 update_vlc_state(state, v);
00352
00353 return ret;
00354 }
00355
00356 #ifdef CONFIG_ENCODERS
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 #endif
00531
00532 static int common_init(AVCodecContext *avctx){
00533 FFV1Context *s = avctx->priv_data;
00534 int width, height;
00535
00536 s->avctx= avctx;
00537 s->flags= avctx->flags;
00538
00539 dsputil_init(&s->dsp, avctx);
00540
00541 width= s->width= avctx->width;
00542 height= s->height= avctx->height;
00543
00544 assert(width && height);
00545
00546 return 0;
00547 }
00548
00549 #ifdef CONFIG_ENCODERS
00550 static int encode_init(AVCodecContext *avctx)
00551 {
00552 FFV1Context *s = avctx->priv_data;
00553 int i;
00554
00555 common_init(avctx);
00556
00557 s->version=0;
00558 s->ac= avctx->coder_type;
00559
00560 s->plane_count=2;
00561 for(i=0; i<256; i++){
00562 s->quant_table[0][i]= quant11[i];
00563 s->quant_table[1][i]= 11*quant11[i];
00564 if(avctx->context_model==0){
00565 s->quant_table[2][i]= 11*11*quant11[i];
00566 s->quant_table[3][i]=
00567 s->quant_table[4][i]=0;
00568 }else{
00569 s->quant_table[2][i]= 11*11*quant5 [i];
00570 s->quant_table[3][i]= 5*11*11*quant5 [i];
00571 s->quant_table[4][i]= 5*5*11*11*quant5 [i];
00572 }
00573 }
00574
00575 for(i=0; i<s->plane_count; i++){
00576 PlaneContext * const p= &s->plane[i];
00577
00578 if(avctx->context_model==0){
00579 p->context_count= (11*11*11+1)/2;
00580 }else{
00581 p->context_count= (11*11*5*5*5+1)/2;
00582 }
00583
00584 if(s->ac){
00585 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00586 }else{
00587 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00588 }
00589 }
00590
00591 avctx->coded_frame= &s->picture;
00592 switch(avctx->pix_fmt){
00593 case PIX_FMT_YUV444P:
00594 case PIX_FMT_YUV422P:
00595 case PIX_FMT_YUV420P:
00596 case PIX_FMT_YUV411P:
00597 case PIX_FMT_YUV410P:
00598 s->colorspace= 0;
00599 break;
00600 case PIX_FMT_RGB32:
00601 s->colorspace= 1;
00602 break;
00603 default:
00604 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00605 return -1;
00606 }
00607 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00608
00609 s->picture_number=0;
00610
00611 return 0;
00612 }
00613 #endif
00614
00615
00616 static void clear_state(FFV1Context *f){
00617 int i, j;
00618
00619 for(i=0; i<f->plane_count; i++){
00620 PlaneContext *p= &f->plane[i];
00621
00622 p->interlace_bit_state[0]= 128;
00623 p->interlace_bit_state[1]= 128;
00624
00625 for(j=0; j<p->context_count; j++){
00626 if(f->ac){
00627 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
00628 }else{
00629 p->vlc_state[j].drift= 0;
00630 p->vlc_state[j].error_sum= 4;
00631 p->vlc_state[j].bias= 0;
00632 p->vlc_state[j].count= 1;
00633 }
00634 }
00635 }
00636 }
00637
00638 #ifdef CONFIG_ENCODERS
00639 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00640 FFV1Context *f = avctx->priv_data;
00641 RangeCoder * const c= &f->c;
00642 AVFrame *pict = data;
00643 const int width= f->width;
00644 const int height= f->height;
00645 AVFrame * const p= &f->picture;
00646 int used_count= 0;
00647 uint8_t keystate=128;
00648
00649 ff_init_range_encoder(c, buf, buf_size);
00650
00651 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00652
00653 *p = *pict;
00654 p->pict_type= FF_I_TYPE;
00655
00656 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
00657 put_rac(c, &keystate, 1);
00658 p->key_frame= 1;
00659 write_header(f);
00660 clear_state(f);
00661 }else{
00662 put_rac(c, &keystate, 0);
00663 p->key_frame= 0;
00664 }
00665
00666 if(!f->ac){
00667 used_count += ff_rac_terminate(c);
00668
00669 init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
00670 }
00671
00672 if(f->colorspace==0){
00673 const int chroma_width = -((-width )>>f->chroma_h_shift);
00674 const int chroma_height= -((-height)>>f->chroma_v_shift);
00675
00676 encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00677
00678 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00679 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00680 }else{
00681 encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
00682 }
00683 emms_c();
00684
00685 f->picture_number++;
00686
00687 if(f->ac){
00688 return ff_rac_terminate(c);
00689 }else{
00690 flush_put_bits(&f->pb);
00691 return used_count + (put_bits_count(&f->pb)+7)/8;
00692 }
00693 }
00694 #endif
00695
00696 static int common_end(AVCodecContext *avctx){
00697 FFV1Context *s = avctx->priv_data;
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 av_freep(&p->vlc_state);
00705 }
00706
00707 return 0;
00708 }
00709
00710 static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00711 PlaneContext * const p= &s->plane[plane_index];
00712 RangeCoder * const c= &s->c;
00713 int x;
00714 int run_count=0;
00715 int run_mode=0;
00716 int run_index= s->run_index;
00717
00718 for(x=0; x<w; x++){
00719 int diff, context, sign;
00720
00721 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
00722 if(context < 0){
00723 context= -context;
00724 sign=1;
00725 }else
00726 sign=0;
00727
00728
00729 if(s->ac){
00730 diff= get_symbol(c, p->state[context], 1);
00731 }else{
00732 if(context == 0 && run_mode==0) run_mode=1;
00733
00734 if(run_mode){
00735 if(run_count==0 && run_mode==1){
00736 if(get_bits1(&s->gb)){
00737 run_count = 1<<log2_run[run_index];
00738 if(x + run_count <= w) run_index++;
00739 }else{
00740 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
00741 else run_count=0;
00742 if(run_index) run_index--;
00743 run_mode=2;
00744 }
00745 }
00746 run_count--;
00747 if(run_count < 0){
00748 run_mode=0;
00749 run_count=0;
00750 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00751 if(diff>=0) diff++;
00752 }else
00753 diff=0;
00754 }else
00755 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00756
00757
00758 }
00759
00760 if(sign) diff= -diff;
00761
00762 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
00763 }
00764 s->run_index= run_index;
00765 }
00766
00767 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00768 int x, y;
00769 int_fast16_t sample_buffer[2][w+6];
00770 int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
00771
00772 s->run_index=0;
00773
00774 memset(sample_buffer, 0, sizeof(sample_buffer));
00775
00776 for(y=0; y<h; y++){
00777 int_fast16_t *temp= sample[0];
00778
00779 sample[0]= sample[1];
00780 sample[1]= temp;
00781
00782 sample[1][-1]= sample[0][0 ];
00783 sample[0][ w]= sample[0][w-1];
00784
00785
00786 decode_line(s, w, sample, plane_index, 8);
00787 for(x=0; x<w; x++){
00788 src[x + stride*y]= sample[1][x];
00789 }
00790
00791 }
00792 }
00793
00794 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00795 int x, y, p;
00796 int_fast16_t sample_buffer[3][2][w+6];
00797 int_fast16_t *sample[3][2]= {
00798 {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
00799 {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
00800 {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
00801
00802 s->run_index=0;
00803
00804 memset(sample_buffer, 0, sizeof(sample_buffer));
00805
00806 for(y=0; y<h; y++){
00807 for(p=0; p<3; p++){
00808 int_fast16_t *temp= sample[p][0];
00809
00810 sample[p][0]= sample[p][1];
00811 sample[p][1]= temp;
00812
00813 sample[p][1][-1]= sample[p][0][0 ];
00814 sample[p][0][ w]= sample[p][0][w-1];
00815 decode_line(s, w, sample[p], FFMIN(p, 1), 9);
00816 }
00817 for(x=0; x<w; x++){
00818 int g= sample[0][1][x];
00819 int b= sample[1][1][x];
00820 int r= sample[2][1][x];
00821
00822
00823
00824
00825 b -= 0x100;
00826 r -= 0x100;
00827 g -= (b + r)>>2;
00828 b += g;
00829 r += g;
00830
00831 src[x + stride*y]= b + (g<<8) + (r<<16);
00832 }
00833 }
00834 }
00835
00836 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
00837 int v;
00838 int i=0;
00839 uint8_t state[CONTEXT_SIZE];
00840
00841 memset(state, 128, sizeof(state));
00842
00843 for(v=0; i<128 ; v++){
00844 int len= get_symbol(c, state, 0) + 1;
00845
00846 if(len + i > 128) return -1;
00847
00848 while(len--){
00849 quant_table[i] = scale*v;
00850 i++;
00851
00852
00853 }
00854 }
00855
00856 for(i=1; i<128; i++){
00857 quant_table[256-i]= -quant_table[i];
00858 }
00859 quant_table[128]= -quant_table[127];
00860
00861 return 2*v - 1;
00862 }
00863
00864 static int read_header(FFV1Context *f){
00865 uint8_t state[CONTEXT_SIZE];
00866 int i, context_count;
00867 RangeCoder * const c= &f->c;
00868
00869 memset(state, 128, sizeof(state));
00870
00871 f->version= get_symbol(c, state, 0);
00872 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
00873 f->colorspace= get_symbol(c, state, 0);
00874 get_rac(c, state);
00875 f->chroma_h_shift= get_symbol(c, state, 0);
00876 f->chroma_v_shift= get_symbol(c, state, 0);
00877 get_rac(c, state);
00878 f->plane_count= 2;
00879
00880 if(f->colorspace==0){
00881 switch(16*f->chroma_h_shift + f->chroma_v_shift){
00882 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
00883 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
00884 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
00885 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
00886 case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
00887 default:
00888 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00889 return -1;
00890 }
00891 }else if(f->colorspace==1){
00892 if(f->chroma_h_shift || f->chroma_v_shift){
00893 av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
00894 return -1;
00895 }
00896 f->avctx->pix_fmt= PIX_FMT_RGB32;
00897 }else{
00898 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
00899 return -1;
00900 }
00901
00902
00903
00904 context_count=1;
00905 for(i=0; i<5; i++){
00906 context_count*= read_quant_table(c, f->quant_table[i], context_count);
00907 if(context_count < 0 || context_count > 32768){
00908 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
00909 return -1;
00910 }
00911 }
00912 context_count= (context_count+1)/2;
00913
00914 for(i=0; i<f->plane_count; i++){
00915 PlaneContext * const p= &f->plane[i];
00916
00917 p->context_count= context_count;
00918
00919 if(f->ac){
00920 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00921 }else{
00922 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00923 }
00924 }
00925
00926 return 0;
00927 }
00928
00929 static int decode_init(AVCodecContext *avctx)
00930 {
00931
00932
00933 common_init(avctx);
00934
00935 return 0;
00936 }
00937
00938 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
00939 FFV1Context *f = avctx->priv_data;
00940 RangeCoder * const c= &f->c;
00941 const int width= f->width;
00942 const int height= f->height;
00943 AVFrame * const p= &f->picture;
00944 int bytes_read;
00945 uint8_t keystate= 128;
00946
00947 AVFrame *picture = data;
00948
00949 ff_init_range_decoder(c, buf, buf_size);
00950 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00951
00952
00953 p->pict_type= FF_I_TYPE;
00954 if(get_rac(c, &keystate)){
00955 p->key_frame= 1;
00956 if(read_header(f) < 0)
00957 return -1;
00958 clear_state(f);
00959 }else{
00960 p->key_frame= 0;
00961 }
00962 if(!f->plane[0].state && !f->plane[0].vlc_state)
00963 return -1;
00964
00965 p->reference= 0;
00966 if(avctx->get_buffer(avctx, p) < 0){
00967 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00968 return -1;
00969 }
00970
00971 if(avctx->debug&FF_DEBUG_PICT_INFO)
00972 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
00973
00974 if(!f->ac){
00975 bytes_read = c->bytestream - c->bytestream_start - 1;
00976 if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n");
00977
00978 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
00979 } else {
00980 bytes_read = 0;
00981 }
00982
00983 if(f->colorspace==0){
00984 const int chroma_width = -((-width )>>f->chroma_h_shift);
00985 const int chroma_height= -((-height)>>f->chroma_v_shift);
00986 decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00987
00988 decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00989 decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00990 }else{
00991 decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
00992 }
00993
00994 emms_c();
00995
00996 f->picture_number++;
00997
00998 *picture= *p;
00999
01000 avctx->release_buffer(avctx, p);
01001
01002 *data_size = sizeof(AVFrame);
01003
01004 if(f->ac){
01005 bytes_read= c->bytestream - c->bytestream_start - 1;
01006 if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
01007 }else{
01008 bytes_read+= (get_bits_count(&f->gb)+7)/8;
01009 }
01010
01011 return bytes_read;
01012 }
01013
01014 AVCodec ffv1_decoder = {
01015 "ffv1",
01016 CODEC_TYPE_VIDEO,
01017 CODEC_ID_FFV1,
01018 sizeof(FFV1Context),
01019 decode_init,
01020 NULL,
01021 common_end,
01022 decode_frame,
01023 CODEC_CAP_DR1 ,
01024 NULL
01025 };
01026
01027 #ifdef CONFIG_ENCODERS
01028 AVCodec ffv1_encoder = {
01029 "ffv1",
01030 CODEC_TYPE_VIDEO,
01031 CODEC_ID_FFV1,
01032 sizeof(FFV1Context),
01033 encode_init,
01034 encode_frame,
01035 common_end,
01036 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, -1},
01037 };
01038 #endif