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)){
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 += FFABS(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 #ifdef CONFIG_ENCODERS
00358 static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00359 PlaneContext * const p= &s->plane[plane_index];
00360 RangeCoder * const c= &s->c;
00361 int x;
00362 int run_index= s->run_index;
00363 int run_count=0;
00364 int run_mode=0;
00365
00366 if(s->ac){
00367 if(c->bytestream_end - c->bytestream < w*20){
00368 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00369 return -1;
00370 }
00371 }else{
00372 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00373 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00374 return -1;
00375 }
00376 }
00377
00378 for(x=0; x<w; x++){
00379 int diff, context;
00380
00381 context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
00382 diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00383
00384 if(context < 0){
00385 context = -context;
00386 diff= -diff;
00387 }
00388
00389 diff= fold(diff, bits);
00390
00391 if(s->ac){
00392 put_symbol(c, p->state[context], diff, 1);
00393 }else{
00394 if(context == 0) run_mode=1;
00395
00396 if(run_mode){
00397
00398 if(diff){
00399 while(run_count >= 1<<log2_run[run_index]){
00400 run_count -= 1<<log2_run[run_index];
00401 run_index++;
00402 put_bits(&s->pb, 1, 1);
00403 }
00404
00405 put_bits(&s->pb, 1 + log2_run[run_index], run_count);
00406 if(run_index) run_index--;
00407 run_count=0;
00408 run_mode=0;
00409 if(diff>0) diff--;
00410 }else{
00411 run_count++;
00412 }
00413 }
00414
00415
00416
00417 if(run_mode == 0)
00418 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00419 }
00420 }
00421 if(run_mode){
00422 while(run_count >= 1<<log2_run[run_index]){
00423 run_count -= 1<<log2_run[run_index];
00424 run_index++;
00425 put_bits(&s->pb, 1, 1);
00426 }
00427
00428 if(run_count)
00429 put_bits(&s->pb, 1, 1);
00430 }
00431 s->run_index= run_index;
00432
00433 return 0;
00434 }
00435
00436 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00437 int x,y,i;
00438 const int ring_size= s->avctx->context_model ? 3 : 2;
00439 int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
00440 s->run_index=0;
00441
00442 memset(sample_buffer, 0, sizeof(sample_buffer));
00443
00444 for(y=0; y<h; y++){
00445 for(i=0; i<ring_size; i++)
00446 sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
00447
00448 sample[0][-1]= sample[1][0 ];
00449 sample[1][ w]= sample[1][w-1];
00450
00451 for(x=0; x<w; x++){
00452 sample[0][x]= src[x + stride*y];
00453 }
00454 encode_line(s, w, sample, plane_index, 8);
00455
00456 }
00457 }
00458
00459 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00460 int x, y, p, i;
00461 const int ring_size= s->avctx->context_model ? 3 : 2;
00462 int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
00463 s->run_index=0;
00464
00465 memset(sample_buffer, 0, sizeof(sample_buffer));
00466
00467 for(y=0; y<h; y++){
00468 for(i=0; i<ring_size; i++)
00469 for(p=0; p<3; p++)
00470 sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
00471
00472 for(x=0; x<w; x++){
00473 int v= src[x + stride*y];
00474 int b= v&0xFF;
00475 int g= (v>>8)&0xFF;
00476 int r= (v>>16)&0xFF;
00477
00478 b -= g;
00479 r -= g;
00480 g += (b + r)>>2;
00481 b += 0x100;
00482 r += 0x100;
00483
00484
00485
00486 sample[0][0][x]= g;
00487 sample[1][0][x]= b;
00488 sample[2][0][x]= r;
00489 }
00490 for(p=0; p<3; p++){
00491 sample[p][0][-1]= sample[p][1][0 ];
00492 sample[p][1][ w]= sample[p][1][w-1];
00493 encode_line(s, w, sample[p], FFMIN(p, 1), 9);
00494 }
00495 }
00496 }
00497
00498 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00499 int last=0;
00500 int i;
00501 uint8_t state[CONTEXT_SIZE];
00502 memset(state, 128, sizeof(state));
00503
00504 for(i=1; i<128 ; i++){
00505 if(quant_table[i] != quant_table[i-1]){
00506 put_symbol(c, state, i-last-1, 0);
00507 last= i;
00508 }
00509 }
00510 put_symbol(c, state, i-last-1, 0);
00511 }
00512
00513 static void write_header(FFV1Context *f){
00514 uint8_t state[CONTEXT_SIZE];
00515 int i;
00516 RangeCoder * const c= &f->c;
00517
00518 memset(state, 128, sizeof(state));
00519
00520 put_symbol(c, state, f->version, 0);
00521 put_symbol(c, state, f->avctx->coder_type, 0);
00522 put_symbol(c, state, f->colorspace, 0);
00523 put_rac(c, state, 1);
00524 put_symbol(c, state, f->chroma_h_shift, 0);
00525 put_symbol(c, state, f->chroma_v_shift, 0);
00526 put_rac(c, state, 0);
00527
00528 for(i=0; i<5; i++)
00529 write_quant_table(c, f->quant_table[i]);
00530 }
00531 #endif
00532
00533 static int common_init(AVCodecContext *avctx){
00534 FFV1Context *s = avctx->priv_data;
00535 int width, height;
00536
00537 s->avctx= avctx;
00538 s->flags= avctx->flags;
00539
00540 dsputil_init(&s->dsp, avctx);
00541
00542 width= s->width= avctx->width;
00543 height= s->height= avctx->height;
00544
00545 assert(width && height);
00546
00547 return 0;
00548 }
00549
00550 #ifdef CONFIG_ENCODERS
00551 static int encode_init(AVCodecContext *avctx)
00552 {
00553 FFV1Context *s = avctx->priv_data;
00554 int i;
00555
00556 common_init(avctx);
00557
00558 s->version=0;
00559 s->ac= avctx->coder_type;
00560
00561 s->plane_count=2;
00562 for(i=0; i<256; i++){
00563 s->quant_table[0][i]= quant11[i];
00564 s->quant_table[1][i]= 11*quant11[i];
00565 if(avctx->context_model==0){
00566 s->quant_table[2][i]= 11*11*quant11[i];
00567 s->quant_table[3][i]=
00568 s->quant_table[4][i]=0;
00569 }else{
00570 s->quant_table[2][i]= 11*11*quant5 [i];
00571 s->quant_table[3][i]= 5*11*11*quant5 [i];
00572 s->quant_table[4][i]= 5*5*11*11*quant5 [i];
00573 }
00574 }
00575
00576 for(i=0; i<s->plane_count; i++){
00577 PlaneContext * const p= &s->plane[i];
00578
00579 if(avctx->context_model==0){
00580 p->context_count= (11*11*11+1)/2;
00581 }else{
00582 p->context_count= (11*11*5*5*5+1)/2;
00583 }
00584
00585 if(s->ac){
00586 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00587 }else{
00588 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00589 }
00590 }
00591
00592 avctx->coded_frame= &s->picture;
00593 switch(avctx->pix_fmt){
00594 case PIX_FMT_YUV444P:
00595 case PIX_FMT_YUV422P:
00596 case PIX_FMT_YUV420P:
00597 case PIX_FMT_YUV411P:
00598 case PIX_FMT_YUV410P:
00599 s->colorspace= 0;
00600 break;
00601 case PIX_FMT_RGB32:
00602 s->colorspace= 1;
00603 break;
00604 default:
00605 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00606 return -1;
00607 }
00608 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00609
00610 s->picture_number=0;
00611
00612 return 0;
00613 }
00614 #endif
00615
00616
00617 static void clear_state(FFV1Context *f){
00618 int i, j;
00619
00620 for(i=0; i<f->plane_count; i++){
00621 PlaneContext *p= &f->plane[i];
00622
00623 p->interlace_bit_state[0]= 128;
00624 p->interlace_bit_state[1]= 128;
00625
00626 for(j=0; j<p->context_count; j++){
00627 if(f->ac){
00628 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
00629 }else{
00630 p->vlc_state[j].drift= 0;
00631 p->vlc_state[j].error_sum= 4;
00632 p->vlc_state[j].bias= 0;
00633 p->vlc_state[j].count= 1;
00634 }
00635 }
00636 }
00637 }
00638
00639 #ifdef CONFIG_ENCODERS
00640 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00641 FFV1Context *f = avctx->priv_data;
00642 RangeCoder * const c= &f->c;
00643 AVFrame *pict = data;
00644 const int width= f->width;
00645 const int height= f->height;
00646 AVFrame * const p= &f->picture;
00647 int used_count= 0;
00648 uint8_t keystate=128;
00649
00650 ff_init_range_encoder(c, buf, buf_size);
00651
00652 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00653
00654 *p = *pict;
00655 p->pict_type= FF_I_TYPE;
00656
00657 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
00658 put_rac(c, &keystate, 1);
00659 p->key_frame= 1;
00660 write_header(f);
00661 clear_state(f);
00662 }else{
00663 put_rac(c, &keystate, 0);
00664 p->key_frame= 0;
00665 }
00666
00667 if(!f->ac){
00668 used_count += ff_rac_terminate(c);
00669
00670 init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
00671 }
00672
00673 if(f->colorspace==0){
00674 const int chroma_width = -((-width )>>f->chroma_h_shift);
00675 const int chroma_height= -((-height)>>f->chroma_v_shift);
00676
00677 encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00678
00679 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00680 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00681 }else{
00682 encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
00683 }
00684 emms_c();
00685
00686 f->picture_number++;
00687
00688 if(f->ac){
00689 return ff_rac_terminate(c);
00690 }else{
00691 flush_put_bits(&f->pb);
00692 return used_count + (put_bits_count(&f->pb)+7)/8;
00693 }
00694 }
00695 #endif
00696
00697 static int common_end(AVCodecContext *avctx){
00698 FFV1Context *s = avctx->priv_data;
00699 int i;
00700
00701 for(i=0; i<s->plane_count; i++){
00702 PlaneContext *p= &s->plane[i];
00703
00704 av_freep(&p->state);
00705 av_freep(&p->vlc_state);
00706 }
00707
00708 return 0;
00709 }
00710
00711 static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00712 PlaneContext * const p= &s->plane[plane_index];
00713 RangeCoder * const c= &s->c;
00714 int x;
00715 int run_count=0;
00716 int run_mode=0;
00717 int run_index= s->run_index;
00718
00719 for(x=0; x<w; x++){
00720 int diff, context, sign;
00721
00722 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
00723 if(context < 0){
00724 context= -context;
00725 sign=1;
00726 }else
00727 sign=0;
00728
00729
00730 if(s->ac){
00731 diff= get_symbol(c, p->state[context], 1);
00732 }else{
00733 if(context == 0 && run_mode==0) run_mode=1;
00734
00735 if(run_mode){
00736 if(run_count==0 && run_mode==1){
00737 if(get_bits1(&s->gb)){
00738 run_count = 1<<log2_run[run_index];
00739 if(x + run_count <= w) run_index++;
00740 }else{
00741 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
00742 else run_count=0;
00743 if(run_index) run_index--;
00744 run_mode=2;
00745 }
00746 }
00747 run_count--;
00748 if(run_count < 0){
00749 run_mode=0;
00750 run_count=0;
00751 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00752 if(diff>=0) diff++;
00753 }else
00754 diff=0;
00755 }else
00756 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00757
00758
00759 }
00760
00761 if(sign) diff= -diff;
00762
00763 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
00764 }
00765 s->run_index= run_index;
00766 }
00767
00768 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00769 int x, y;
00770 int_fast16_t sample_buffer[2][w+6];
00771 int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
00772
00773 s->run_index=0;
00774
00775 memset(sample_buffer, 0, sizeof(sample_buffer));
00776
00777 for(y=0; y<h; y++){
00778 int_fast16_t *temp= sample[0];
00779
00780 sample[0]= sample[1];
00781 sample[1]= temp;
00782
00783 sample[1][-1]= sample[0][0 ];
00784 sample[0][ w]= sample[0][w-1];
00785
00786
00787 decode_line(s, w, sample, plane_index, 8);
00788 for(x=0; x<w; x++){
00789 src[x + stride*y]= sample[1][x];
00790 }
00791
00792 }
00793 }
00794
00795 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00796 int x, y, p;
00797 int_fast16_t sample_buffer[3][2][w+6];
00798 int_fast16_t *sample[3][2]= {
00799 {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
00800 {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
00801 {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
00802
00803 s->run_index=0;
00804
00805 memset(sample_buffer, 0, sizeof(sample_buffer));
00806
00807 for(y=0; y<h; y++){
00808 for(p=0; p<3; p++){
00809 int_fast16_t *temp= sample[p][0];
00810
00811 sample[p][0]= sample[p][1];
00812 sample[p][1]= temp;
00813
00814 sample[p][1][-1]= sample[p][0][0 ];
00815 sample[p][0][ w]= sample[p][0][w-1];
00816 decode_line(s, w, sample[p], FFMIN(p, 1), 9);
00817 }
00818 for(x=0; x<w; x++){
00819 int g= sample[0][1][x];
00820 int b= sample[1][1][x];
00821 int r= sample[2][1][x];
00822
00823
00824
00825
00826 b -= 0x100;
00827 r -= 0x100;
00828 g -= (b + r)>>2;
00829 b += g;
00830 r += g;
00831
00832 src[x + stride*y]= b + (g<<8) + (r<<16);
00833 }
00834 }
00835 }
00836
00837 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
00838 int v;
00839 int i=0;
00840 uint8_t state[CONTEXT_SIZE];
00841
00842 memset(state, 128, sizeof(state));
00843
00844 for(v=0; i<128 ; v++){
00845 int len= get_symbol(c, state, 0) + 1;
00846
00847 if(len + i > 128) return -1;
00848
00849 while(len--){
00850 quant_table[i] = scale*v;
00851 i++;
00852
00853
00854 }
00855 }
00856
00857 for(i=1; i<128; i++){
00858 quant_table[256-i]= -quant_table[i];
00859 }
00860 quant_table[128]= -quant_table[127];
00861
00862 return 2*v - 1;
00863 }
00864
00865 static int read_header(FFV1Context *f){
00866 uint8_t state[CONTEXT_SIZE];
00867 int i, context_count;
00868 RangeCoder * const c= &f->c;
00869
00870 memset(state, 128, sizeof(state));
00871
00872 f->version= get_symbol(c, state, 0);
00873 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
00874 f->colorspace= get_symbol(c, state, 0);
00875 get_rac(c, state);
00876 f->chroma_h_shift= get_symbol(c, state, 0);
00877 f->chroma_v_shift= get_symbol(c, state, 0);
00878 get_rac(c, state);
00879 f->plane_count= 2;
00880
00881 if(f->colorspace==0){
00882 switch(16*f->chroma_h_shift + f->chroma_v_shift){
00883 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
00884 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
00885 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
00886 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
00887 case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
00888 default:
00889 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00890 return -1;
00891 }
00892 }else if(f->colorspace==1){
00893 if(f->chroma_h_shift || f->chroma_v_shift){
00894 av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
00895 return -1;
00896 }
00897 f->avctx->pix_fmt= PIX_FMT_RGB32;
00898 }else{
00899 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
00900 return -1;
00901 }
00902
00903
00904
00905 context_count=1;
00906 for(i=0; i<5; i++){
00907 context_count*= read_quant_table(c, f->quant_table[i], context_count);
00908 if(context_count < 0 || context_count > 32768){
00909 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
00910 return -1;
00911 }
00912 }
00913 context_count= (context_count+1)/2;
00914
00915 for(i=0; i<f->plane_count; i++){
00916 PlaneContext * const p= &f->plane[i];
00917
00918 p->context_count= context_count;
00919
00920 if(f->ac){
00921 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00922 }else{
00923 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00924 }
00925 }
00926
00927 return 0;
00928 }
00929
00930 static int decode_init(AVCodecContext *avctx)
00931 {
00932
00933
00934 common_init(avctx);
00935
00936 return 0;
00937 }
00938
00939 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
00940 FFV1Context *f = avctx->priv_data;
00941 RangeCoder * const c= &f->c;
00942 const int width= f->width;
00943 const int height= f->height;
00944 AVFrame * const p= &f->picture;
00945 int bytes_read;
00946 uint8_t keystate= 128;
00947
00948 AVFrame *picture = data;
00949
00950 ff_init_range_decoder(c, buf, buf_size);
00951 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00952
00953
00954 p->pict_type= FF_I_TYPE;
00955 if(get_rac(c, &keystate)){
00956 p->key_frame= 1;
00957 if(read_header(f) < 0)
00958 return -1;
00959 clear_state(f);
00960 }else{
00961 p->key_frame= 0;
00962 }
00963 if(!f->plane[0].state && !f->plane[0].vlc_state)
00964 return -1;
00965
00966 p->reference= 0;
00967 if(avctx->get_buffer(avctx, p) < 0){
00968 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00969 return -1;
00970 }
00971
00972 if(avctx->debug&FF_DEBUG_PICT_INFO)
00973 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
00974
00975 if(!f->ac){
00976 bytes_read = c->bytestream - c->bytestream_start - 1;
00977 if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n");
00978
00979 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
00980 } else {
00981 bytes_read = 0;
00982 }
00983
00984 if(f->colorspace==0){
00985 const int chroma_width = -((-width )>>f->chroma_h_shift);
00986 const int chroma_height= -((-height)>>f->chroma_v_shift);
00987 decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00988
00989 decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00990 decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00991 }else{
00992 decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
00993 }
00994
00995 emms_c();
00996
00997 f->picture_number++;
00998
00999 *picture= *p;
01000
01001 avctx->release_buffer(avctx, p);
01002
01003 *data_size = sizeof(AVFrame);
01004
01005 if(f->ac){
01006 bytes_read= c->bytestream - c->bytestream_start - 1;
01007 if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
01008 }else{
01009 bytes_read+= (get_bits_count(&f->gb)+7)/8;
01010 }
01011
01012 return bytes_read;
01013 }
01014
01015 AVCodec ffv1_decoder = {
01016 "ffv1",
01017 CODEC_TYPE_VIDEO,
01018 CODEC_ID_FFV1,
01019 sizeof(FFV1Context),
01020 decode_init,
01021 NULL,
01022 common_end,
01023 decode_frame,
01024 CODEC_CAP_DR1 ,
01025 NULL
01026 };
01027
01028 #ifdef CONFIG_ENCODERS
01029 AVCodec ffv1_encoder = {
01030 "ffv1",
01031 CODEC_TYPE_VIDEO,
01032 CODEC_ID_FFV1,
01033 sizeof(FFV1Context),
01034 encode_init,
01035 encode_frame,
01036 common_end,
01037 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, -1},
01038 };
01039 #endif