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 #include "mathops.h"
00034
00035 #define MAX_PLANES 4
00036 #define CONTEXT_SIZE 32
00037
00038 extern const uint8_t ff_log2_run[32];
00039
00040 static const int8_t quant3[256]={
00041 0, 0, 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,-1,
00054 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00055 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00056 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
00057 };
00058 static const int8_t quant5[256]={
00059 0, 1, 1, 1, 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,-2,-2,-2,
00072 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00073 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00074 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
00075 };
00076 static const int8_t quant7[256]={
00077 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00078 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00079 2, 2, 2, 2, 2, 2, 2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-6,-6,-5,
00144 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00145 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00146 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
00147 };
00148
00149 typedef struct VlcState{
00150 int16_t drift;
00151 uint16_t error_sum;
00152 int8_t bias;
00153 uint8_t count;
00154 } VlcState;
00155
00156 typedef struct PlaneContext{
00157 int context_count;
00158 uint8_t (*state)[CONTEXT_SIZE];
00159 VlcState *vlc_state;
00160 uint8_t interlace_bit_state[2];
00161 } PlaneContext;
00162
00163 typedef struct FFV1Context{
00164 AVCodecContext *avctx;
00165 RangeCoder c;
00166 GetBitContext gb;
00167 PutBitContext pb;
00168 int version;
00169 int width, height;
00170 int chroma_h_shift, chroma_v_shift;
00171 int flags;
00172 int picture_number;
00173 AVFrame picture;
00174 int plane_count;
00175 int ac;
00176 PlaneContext plane[MAX_PLANES];
00177 int16_t quant_table[5][256];
00178 int run_index;
00179 int colorspace;
00180
00181 DSPContext dsp;
00182 }FFV1Context;
00183
00184 static av_always_inline int fold(int diff, int bits){
00185 if(bits==8)
00186 diff= (int8_t)diff;
00187 else{
00188 diff+= 1<<(bits-1);
00189 diff&=(1<<bits)-1;
00190 diff-= 1<<(bits-1);
00191 }
00192
00193 return diff;
00194 }
00195
00196 static inline int predict(int_fast16_t *src, int_fast16_t *last){
00197 const int LT= last[-1];
00198 const int T= last[ 0];
00199 const int L = src[-1];
00200
00201 return mid_pred(L, L + T - LT, T);
00202 }
00203
00204 static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
00205 const int LT= last[-1];
00206 const int T= last[ 0];
00207 const int RT= last[ 1];
00208 const int L = src[-1];
00209
00210 if(f->quant_table[3][127]){
00211 const int TT= last2[0];
00212 const int LL= src[-2];
00213 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
00214 +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
00215 }else
00216 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
00217 }
00218
00219 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00220 int i;
00221
00222 if(v){
00223 const int a= FFABS(v);
00224 const int e= av_log2(a);
00225 put_rac(c, state+0, 0);
00226
00227 assert(e<=9);
00228
00229 for(i=0; i<e; i++){
00230 put_rac(c, state+1+i, 1);
00231 }
00232 put_rac(c, state+1+i, 0);
00233
00234 for(i=e-1; i>=0; i--){
00235 put_rac(c, state+22+i, (a>>i)&1);
00236 }
00237
00238 if(is_signed)
00239 put_rac(c, state+11 + e, v < 0);
00240 }else{
00241 put_rac(c, state+0, 1);
00242 }
00243 }
00244
00245 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00246 if(get_rac(c, state+0))
00247 return 0;
00248 else{
00249 int i, e, a;
00250 e= 0;
00251 while(get_rac(c, state+1 + e)){
00252 e++;
00253 }
00254 assert(e<=9);
00255
00256 a= 1;
00257 for(i=e-1; i>=0; i--){
00258 a += a + get_rac(c, state+22 + i);
00259 }
00260
00261 if(is_signed && get_rac(c, state+11 + e))
00262 return -a;
00263 else
00264 return a;
00265 }
00266 }
00267
00268 static inline void update_vlc_state(VlcState * const state, const int v){
00269 int drift= state->drift;
00270 int count= state->count;
00271 state->error_sum += FFABS(v);
00272 drift += v;
00273
00274 if(count == 128){
00275 count >>= 1;
00276 drift >>= 1;
00277 state->error_sum >>= 1;
00278 }
00279 count++;
00280
00281 if(drift <= -count){
00282 if(state->bias > -128) state->bias--;
00283
00284 drift += count;
00285 if(drift <= -count)
00286 drift= -count + 1;
00287 }else if(drift > 0){
00288 if(state->bias < 127) state->bias++;
00289
00290 drift -= count;
00291 if(drift > 0)
00292 drift= 0;
00293 }
00294
00295 state->drift= drift;
00296 state->count= count;
00297 }
00298
00299 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00300 int i, k, code;
00301
00302 v = fold(v - state->bias, bits);
00303
00304 i= state->count;
00305 k=0;
00306 while(i < state->error_sum){
00307 k++;
00308 i += i;
00309 }
00310
00311 assert(k<=8);
00312
00313 #if 0 // JPEG LS
00314 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00315 else code= v;
00316 #else
00317 code= v ^ ((2*state->drift + state->count)>>31);
00318 #endif
00319
00320
00321 set_sr_golomb(pb, code, k, 12, bits);
00322
00323 update_vlc_state(state, v);
00324 }
00325
00326 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00327 int k, i, v, ret;
00328
00329 i= state->count;
00330 k=0;
00331 while(i < state->error_sum){
00332 k++;
00333 i += i;
00334 }
00335
00336 assert(k<=8);
00337
00338 v= get_sr_golomb(gb, k, 12, bits);
00339
00340
00341 #if 0 // JPEG LS
00342 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00343 #else
00344 v ^= ((2*state->drift + state->count)>>31);
00345 #endif
00346
00347 ret= fold(v + state->bias, bits);
00348
00349 update_vlc_state(state, v);
00350
00351 return ret;
00352 }
00353
00354 #if CONFIG_FFV1_ENCODER
00355 static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00356 PlaneContext * const p= &s->plane[plane_index];
00357 RangeCoder * const c= &s->c;
00358 int x;
00359 int run_index= s->run_index;
00360 int run_count=0;
00361 int run_mode=0;
00362
00363 if(s->ac){
00364 if(c->bytestream_end - c->bytestream < w*20){
00365 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00366 return -1;
00367 }
00368 }else{
00369 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00370 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00371 return -1;
00372 }
00373 }
00374
00375 for(x=0; x<w; x++){
00376 int diff, context;
00377
00378 context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
00379 diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00380
00381 if(context < 0){
00382 context = -context;
00383 diff= -diff;
00384 }
00385
00386 diff= fold(diff, bits);
00387
00388 if(s->ac){
00389 put_symbol(c, p->state[context], diff, 1);
00390 }else{
00391 if(context == 0) run_mode=1;
00392
00393 if(run_mode){
00394
00395 if(diff){
00396 while(run_count >= 1<<ff_log2_run[run_index]){
00397 run_count -= 1<<ff_log2_run[run_index];
00398 run_index++;
00399 put_bits(&s->pb, 1, 1);
00400 }
00401
00402 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
00403 if(run_index) run_index--;
00404 run_count=0;
00405 run_mode=0;
00406 if(diff>0) diff--;
00407 }else{
00408 run_count++;
00409 }
00410 }
00411
00412
00413
00414 if(run_mode == 0)
00415 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00416 }
00417 }
00418 if(run_mode){
00419 while(run_count >= 1<<ff_log2_run[run_index]){
00420 run_count -= 1<<ff_log2_run[run_index];
00421 run_index++;
00422 put_bits(&s->pb, 1, 1);
00423 }
00424
00425 if(run_count)
00426 put_bits(&s->pb, 1, 1);
00427 }
00428 s->run_index= run_index;
00429
00430 return 0;
00431 }
00432
00433 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00434 int x,y,i;
00435 const int ring_size= s->avctx->context_model ? 3 : 2;
00436 int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
00437 s->run_index=0;
00438
00439 memset(sample_buffer, 0, sizeof(sample_buffer));
00440
00441 for(y=0; y<h; y++){
00442 for(i=0; i<ring_size; i++)
00443 sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
00444
00445 sample[0][-1]= sample[1][0 ];
00446 sample[1][ w]= sample[1][w-1];
00447
00448 for(x=0; x<w; x++){
00449 sample[0][x]= src[x + stride*y];
00450 }
00451 encode_line(s, w, sample, plane_index, 8);
00452
00453 }
00454 }
00455
00456 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00457 int x, y, p, i;
00458 const int ring_size= s->avctx->context_model ? 3 : 2;
00459 int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
00460 s->run_index=0;
00461
00462 memset(sample_buffer, 0, sizeof(sample_buffer));
00463
00464 for(y=0; y<h; y++){
00465 for(i=0; i<ring_size; i++)
00466 for(p=0; p<3; p++)
00467 sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
00468
00469 for(x=0; x<w; x++){
00470 int v= src[x + stride*y];
00471 int b= v&0xFF;
00472 int g= (v>>8)&0xFF;
00473 int r= (v>>16)&0xFF;
00474
00475 b -= g;
00476 r -= g;
00477 g += (b + r)>>2;
00478 b += 0x100;
00479 r += 0x100;
00480
00481
00482
00483 sample[0][0][x]= g;
00484 sample[1][0][x]= b;
00485 sample[2][0][x]= r;
00486 }
00487 for(p=0; p<3; p++){
00488 sample[p][0][-1]= sample[p][1][0 ];
00489 sample[p][1][ w]= sample[p][1][w-1];
00490 encode_line(s, w, sample[p], FFMIN(p, 1), 9);
00491 }
00492 }
00493 }
00494
00495 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00496 int last=0;
00497 int i;
00498 uint8_t state[CONTEXT_SIZE];
00499 memset(state, 128, sizeof(state));
00500
00501 for(i=1; i<128 ; i++){
00502 if(quant_table[i] != quant_table[i-1]){
00503 put_symbol(c, state, i-last-1, 0);
00504 last= i;
00505 }
00506 }
00507 put_symbol(c, state, i-last-1, 0);
00508 }
00509
00510 static void write_header(FFV1Context *f){
00511 uint8_t state[CONTEXT_SIZE];
00512 int i;
00513 RangeCoder * const c= &f->c;
00514
00515 memset(state, 128, sizeof(state));
00516
00517 put_symbol(c, state, f->version, 0);
00518 put_symbol(c, state, f->avctx->coder_type, 0);
00519 put_symbol(c, state, f->colorspace, 0);
00520 put_rac(c, state, 1);
00521 put_symbol(c, state, f->chroma_h_shift, 0);
00522 put_symbol(c, state, f->chroma_v_shift, 0);
00523 put_rac(c, state, 0);
00524
00525 for(i=0; i<5; i++)
00526 write_quant_table(c, f->quant_table[i]);
00527 }
00528 #endif
00529
00530 static av_cold int common_init(AVCodecContext *avctx){
00531 FFV1Context *s = avctx->priv_data;
00532 int width, height;
00533
00534 s->avctx= avctx;
00535 s->flags= avctx->flags;
00536
00537 dsputil_init(&s->dsp, avctx);
00538
00539 width= s->width= avctx->width;
00540 height= s->height= avctx->height;
00541
00542 assert(width && height);
00543
00544 return 0;
00545 }
00546
00547 #if CONFIG_FFV1_ENCODER
00548 static av_cold int encode_init(AVCodecContext *avctx)
00549 {
00550 FFV1Context *s = avctx->priv_data;
00551 int i;
00552
00553 common_init(avctx);
00554
00555 s->version=0;
00556 s->ac= avctx->coder_type;
00557
00558 s->plane_count=2;
00559 for(i=0; i<256; i++){
00560 s->quant_table[0][i]= quant11[i];
00561 s->quant_table[1][i]= 11*quant11[i];
00562 if(avctx->context_model==0){
00563 s->quant_table[2][i]= 11*11*quant11[i];
00564 s->quant_table[3][i]=
00565 s->quant_table[4][i]=0;
00566 }else{
00567 s->quant_table[2][i]= 11*11*quant5 [i];
00568 s->quant_table[3][i]= 5*11*11*quant5 [i];
00569 s->quant_table[4][i]= 5*5*11*11*quant5 [i];
00570 }
00571 }
00572
00573 for(i=0; i<s->plane_count; i++){
00574 PlaneContext * const p= &s->plane[i];
00575
00576 if(avctx->context_model==0){
00577 p->context_count= (11*11*11+1)/2;
00578 }else{
00579 p->context_count= (11*11*5*5*5+1)/2;
00580 }
00581
00582 if(s->ac){
00583 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00584 }else{
00585 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00586 }
00587 }
00588
00589 avctx->coded_frame= &s->picture;
00590 switch(avctx->pix_fmt){
00591 case PIX_FMT_YUV444P:
00592 case PIX_FMT_YUV422P:
00593 case PIX_FMT_YUV420P:
00594 case PIX_FMT_YUV411P:
00595 case PIX_FMT_YUV410P:
00596 s->colorspace= 0;
00597 break;
00598 case PIX_FMT_RGB32:
00599 s->colorspace= 1;
00600 break;
00601 default:
00602 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00603 return -1;
00604 }
00605 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00606
00607 s->picture_number=0;
00608
00609 return 0;
00610 }
00611 #endif
00612
00613
00614 static void clear_state(FFV1Context *f){
00615 int i, j;
00616
00617 for(i=0; i<f->plane_count; i++){
00618 PlaneContext *p= &f->plane[i];
00619
00620 p->interlace_bit_state[0]= 128;
00621 p->interlace_bit_state[1]= 128;
00622
00623 for(j=0; j<p->context_count; j++){
00624 if(f->ac){
00625 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
00626 }else{
00627 p->vlc_state[j].drift= 0;
00628 p->vlc_state[j].error_sum= 4;
00629 p->vlc_state[j].bias= 0;
00630 p->vlc_state[j].count= 1;
00631 }
00632 }
00633 }
00634 }
00635
00636 #if CONFIG_FFV1_ENCODER
00637 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00638 FFV1Context *f = avctx->priv_data;
00639 RangeCoder * const c= &f->c;
00640 AVFrame *pict = data;
00641 const int width= f->width;
00642 const int height= f->height;
00643 AVFrame * const p= &f->picture;
00644 int used_count= 0;
00645 uint8_t keystate=128;
00646
00647 ff_init_range_encoder(c, buf, buf_size);
00648
00649 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00650
00651 *p = *pict;
00652 p->pict_type= FF_I_TYPE;
00653
00654 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
00655 put_rac(c, &keystate, 1);
00656 p->key_frame= 1;
00657 write_header(f);
00658 clear_state(f);
00659 }else{
00660 put_rac(c, &keystate, 0);
00661 p->key_frame= 0;
00662 }
00663
00664 if(!f->ac){
00665 used_count += ff_rac_terminate(c);
00666
00667 init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
00668 }
00669
00670 if(f->colorspace==0){
00671 const int chroma_width = -((-width )>>f->chroma_h_shift);
00672 const int chroma_height= -((-height)>>f->chroma_v_shift);
00673
00674 encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00675
00676 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00677 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00678 }else{
00679 encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
00680 }
00681 emms_c();
00682
00683 f->picture_number++;
00684
00685 if(f->ac){
00686 return ff_rac_terminate(c);
00687 }else{
00688 flush_put_bits(&f->pb);
00689 return used_count + (put_bits_count(&f->pb)+7)/8;
00690 }
00691 }
00692 #endif
00693
00694 static av_cold int common_end(AVCodecContext *avctx){
00695 FFV1Context *s = avctx->priv_data;
00696 int i;
00697
00698 for(i=0; i<s->plane_count; i++){
00699 PlaneContext *p= &s->plane[i];
00700
00701 av_freep(&p->state);
00702 av_freep(&p->vlc_state);
00703 }
00704
00705 return 0;
00706 }
00707
00708 static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00709 PlaneContext * const p= &s->plane[plane_index];
00710 RangeCoder * const c= &s->c;
00711 int x;
00712 int run_count=0;
00713 int run_mode=0;
00714 int run_index= s->run_index;
00715
00716 for(x=0; x<w; x++){
00717 int diff, context, sign;
00718
00719 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
00720 if(context < 0){
00721 context= -context;
00722 sign=1;
00723 }else
00724 sign=0;
00725
00726
00727 if(s->ac){
00728 diff= get_symbol(c, p->state[context], 1);
00729 }else{
00730 if(context == 0 && run_mode==0) run_mode=1;
00731
00732 if(run_mode){
00733 if(run_count==0 && run_mode==1){
00734 if(get_bits1(&s->gb)){
00735 run_count = 1<<ff_log2_run[run_index];
00736 if(x + run_count <= w) run_index++;
00737 }else{
00738 if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
00739 else run_count=0;
00740 if(run_index) run_index--;
00741 run_mode=2;
00742 }
00743 }
00744 run_count--;
00745 if(run_count < 0){
00746 run_mode=0;
00747 run_count=0;
00748 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00749 if(diff>=0) diff++;
00750 }else
00751 diff=0;
00752 }else
00753 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00754
00755
00756 }
00757
00758 if(sign) diff= -diff;
00759
00760 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
00761 }
00762 s->run_index= run_index;
00763 }
00764
00765 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00766 int x, y;
00767 int_fast16_t sample_buffer[2][w+6];
00768 int_fast16_t *sample[2];
00769 sample[0]=sample_buffer[0]+3;
00770 sample[1]=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 for(x=0; x<3; x++){
00799 sample[x][0] = sample_buffer[x][0]+3;
00800 sample[x][1] = sample_buffer[x][1]+3;
00801 }
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 av_cold 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 .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
01027 };
01028
01029 #if CONFIG_FFV1_ENCODER
01030 AVCodec ffv1_encoder = {
01031 "ffv1",
01032 CODEC_TYPE_VIDEO,
01033 CODEC_ID_FFV1,
01034 sizeof(FFV1Context),
01035 encode_init,
01036 encode_frame,
01037 common_end,
01038 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_NONE},
01039 .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
01040 };
01041 #endif