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 "get_bits.h"
00030 #include "put_bits.h"
00031 #include "dsputil.h"
00032 #include "rangecoder.h"
00033 #include "golomb.h"
00034 #include "mathops.h"
00035 #include "libavutil/avassert.h"
00036
00037 #define MAX_PLANES 4
00038 #define CONTEXT_SIZE 32
00039
00040 #define MAX_QUANT_TABLES 8
00041 #define MAX_CONTEXT_INPUTS 5
00042
00043 extern const uint8_t ff_log2_run[32];
00044
00045 static const int8_t quant3[256]={
00046 0, 0, 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,-1,
00057 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00058 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00059 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00060 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00061 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
00062 };
00063
00064 static const int8_t quant5_10bit[256]={
00065 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
00066 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00067 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00068 1, 1, 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,-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,-2,-2,-2,-2,-2,-2,-2,-2,
00077 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,
00078 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00079 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00080 -1,-1,-1,-1,-1,-1,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,
00081 };
00082
00083 static const int8_t quant5[256]={
00084 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2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4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00172 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
00173 };
00174 static const int8_t quant13[256]={
00175 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
00176 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00177 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00178 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00179 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00180 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00181 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00182 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00183 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00184 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00185 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00186 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00187 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
00188 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00189 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00190 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
00191 };
00192
00193 static const uint8_t ver2_state[256]= {
00194 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
00195 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
00196 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
00197 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
00198 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
00199 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
00200 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
00201 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
00202 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
00203 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
00204 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
00205 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
00206 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
00207 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
00208 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
00209 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
00210 };
00211
00212 typedef struct VlcState{
00213 int16_t drift;
00214 uint16_t error_sum;
00215 int8_t bias;
00216 uint8_t count;
00217 } VlcState;
00218
00219 typedef struct PlaneContext{
00220 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00221 int quant_table_index;
00222 int context_count;
00223 uint8_t (*state)[CONTEXT_SIZE];
00224 VlcState *vlc_state;
00225 uint8_t interlace_bit_state[2];
00226 } PlaneContext;
00227
00228 #define MAX_SLICES 256
00229
00230 typedef struct FFV1Context{
00231 AVCodecContext *avctx;
00232 RangeCoder c;
00233 GetBitContext gb;
00234 PutBitContext pb;
00235 uint64_t rc_stat[256][2];
00236 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
00237 int version;
00238 int width, height;
00239 int chroma_h_shift, chroma_v_shift;
00240 int flags;
00241 int picture_number;
00242 AVFrame picture;
00243 int plane_count;
00244 int ac;
00245 PlaneContext plane[MAX_PLANES];
00246 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00247 int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
00248 int context_count[MAX_QUANT_TABLES];
00249 uint8_t state_transition[256];
00250 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
00251 int run_index;
00252 int colorspace;
00253 int_fast16_t *sample_buffer;
00254 int gob_count;
00255
00256 int quant_table_count;
00257
00258 DSPContext dsp;
00259
00260 struct FFV1Context *slice_context[MAX_SLICES];
00261 int slice_count;
00262 int num_v_slices;
00263 int num_h_slices;
00264 int slice_width;
00265 int slice_height;
00266 int slice_x;
00267 int slice_y;
00268 }FFV1Context;
00269
00270 static av_always_inline int fold(int diff, int bits){
00271 if(bits==8)
00272 diff= (int8_t)diff;
00273 else{
00274 diff+= 1<<(bits-1);
00275 diff&=(1<<bits)-1;
00276 diff-= 1<<(bits-1);
00277 }
00278
00279 return diff;
00280 }
00281
00282 static inline int predict(int_fast16_t *src, int_fast16_t *last){
00283 const int LT= last[-1];
00284 const int T= last[ 0];
00285 const int L = src[-1];
00286
00287 return mid_pred(L, L + T - LT, T);
00288 }
00289
00290 static inline int get_context(PlaneContext *p, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
00291 const int LT= last[-1];
00292 const int T= last[ 0];
00293 const int RT= last[ 1];
00294 const int L = src[-1];
00295
00296 if(p->quant_table[3][127]){
00297 const int TT= last2[0];
00298 const int LL= src[-2];
00299 return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
00300 +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
00301 }else
00302 return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
00303 }
00304
00305 static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256]){
00306 int i,j,k,m;
00307 double l2tab[256];
00308
00309 for(i=1; i<256; i++)
00310 l2tab[i]= log2(i/256.0);
00311
00312 for(i=0; i<256; i++){
00313 double best_len[256];
00314 double p= i/256.0;
00315
00316 for(j=0; j<256; j++)
00317 best_len[j]= 1<<30;
00318
00319 for(j=FFMAX(i-10,1); j<FFMIN(i+11,256); j++){
00320 double occ[256]={0};
00321 double len=0;
00322 occ[j]=1.0;
00323 for(k=0; k<256; k++){
00324 double newocc[256]={0};
00325 for(m=0; m<256; m++){
00326 if(occ[m]){
00327 len -=occ[m]*( p *l2tab[ m]
00328 + (1-p)*l2tab[256-m]);
00329 }
00330 }
00331 if(len < best_len[k]){
00332 best_len[k]= len;
00333 best_state[i][k]= j;
00334 }
00335 for(m=0; m<256; m++){
00336 if(occ[m]){
00337 newocc[ one_state[ m]] += occ[m]* p ;
00338 newocc[256-one_state[256-m]] += occ[m]*(1-p);
00339 }
00340 }
00341 memcpy(occ, newocc, sizeof(occ));
00342 }
00343 }
00344 }
00345 }
00346
00347 static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2]){
00348 int i;
00349
00350 #define put_rac(C,S,B) \
00351 do{\
00352 if(rc_stat){\
00353 rc_stat[*(S)][B]++;\
00354 rc_stat2[(S)-state][B]++;\
00355 }\
00356 put_rac(C,S,B);\
00357 }while(0)
00358
00359 if(v){
00360 const int a= FFABS(v);
00361 const int e= av_log2(a);
00362 put_rac(c, state+0, 0);
00363 if(e<=9){
00364 for(i=0; i<e; i++){
00365 put_rac(c, state+1+i, 1);
00366 }
00367 put_rac(c, state+1+i, 0);
00368
00369 for(i=e-1; i>=0; i--){
00370 put_rac(c, state+22+i, (a>>i)&1);
00371 }
00372
00373 if(is_signed)
00374 put_rac(c, state+11 + e, v < 0);
00375 }else{
00376 for(i=0; i<e; i++){
00377 put_rac(c, state+1+FFMIN(i,9), 1);
00378 }
00379 put_rac(c, state+1+9, 0);
00380
00381 for(i=e-1; i>=0; i--){
00382 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1);
00383 }
00384
00385 if(is_signed)
00386 put_rac(c, state+11 + 10, v < 0);
00387 }
00388 }else{
00389 put_rac(c, state+0, 1);
00390 }
00391 #undef put_rac
00392 }
00393
00394 static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00395 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
00396 }
00397
00398 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
00399 if(get_rac(c, state+0))
00400 return 0;
00401 else{
00402 int i, e, a;
00403 e= 0;
00404 while(get_rac(c, state+1 + FFMIN(e,9))){
00405 e++;
00406 }
00407
00408 a= 1;
00409 for(i=e-1; i>=0; i--){
00410 a += a + get_rac(c, state+22 + FFMIN(i,9));
00411 }
00412
00413 e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10)));
00414 return (a^e)-e;
00415 }
00416 }
00417
00418 static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00419 return get_symbol_inline(c, state, is_signed);
00420 }
00421
00422 static inline void update_vlc_state(VlcState * const state, const int v){
00423 int drift= state->drift;
00424 int count= state->count;
00425 state->error_sum += FFABS(v);
00426 drift += v;
00427
00428 if(count == 128){
00429 count >>= 1;
00430 drift >>= 1;
00431 state->error_sum >>= 1;
00432 }
00433 count++;
00434
00435 if(drift <= -count){
00436 if(state->bias > -128) state->bias--;
00437
00438 drift += count;
00439 if(drift <= -count)
00440 drift= -count + 1;
00441 }else if(drift > 0){
00442 if(state->bias < 127) state->bias++;
00443
00444 drift -= count;
00445 if(drift > 0)
00446 drift= 0;
00447 }
00448
00449 state->drift= drift;
00450 state->count= count;
00451 }
00452
00453 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00454 int i, k, code;
00455
00456 v = fold(v - state->bias, bits);
00457
00458 i= state->count;
00459 k=0;
00460 while(i < state->error_sum){
00461 k++;
00462 i += i;
00463 }
00464
00465 assert(k<=8);
00466
00467 #if 0 // JPEG LS
00468 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00469 else code= v;
00470 #else
00471 code= v ^ ((2*state->drift + state->count)>>31);
00472 #endif
00473
00474
00475 set_sr_golomb(pb, code, k, 12, bits);
00476
00477 update_vlc_state(state, v);
00478 }
00479
00480 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00481 int k, i, v, ret;
00482
00483 i= state->count;
00484 k=0;
00485 while(i < state->error_sum){
00486 k++;
00487 i += i;
00488 }
00489
00490 assert(k<=8);
00491
00492 v= get_sr_golomb(gb, k, 12, bits);
00493
00494
00495 #if 0 // JPEG LS
00496 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00497 #else
00498 v ^= ((2*state->drift + state->count)>>31);
00499 #endif
00500
00501 ret= fold(v + state->bias, bits);
00502
00503 update_vlc_state(state, v);
00504
00505 return ret;
00506 }
00507
00508 #if CONFIG_FFV1_ENCODER
00509 static av_always_inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00510 PlaneContext * const p= &s->plane[plane_index];
00511 RangeCoder * const c= &s->c;
00512 int x;
00513 int run_index= s->run_index;
00514 int run_count=0;
00515 int run_mode=0;
00516
00517 if(s->ac){
00518 if(c->bytestream_end - c->bytestream < w*20){
00519 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00520 return -1;
00521 }
00522 }else{
00523 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00524 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00525 return -1;
00526 }
00527 }
00528
00529 for(x=0; x<w; x++){
00530 int diff, context;
00531
00532 context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
00533 diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00534
00535 if(context < 0){
00536 context = -context;
00537 diff= -diff;
00538 }
00539
00540 diff= fold(diff, bits);
00541
00542 if(s->ac){
00543 if(s->flags & CODEC_FLAG_PASS1){
00544 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat, s->rc_stat2[p->quant_table_index][context]);
00545 }else{
00546 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
00547 }
00548 }else{
00549 if(context == 0) run_mode=1;
00550
00551 if(run_mode){
00552
00553 if(diff){
00554 while(run_count >= 1<<ff_log2_run[run_index]){
00555 run_count -= 1<<ff_log2_run[run_index];
00556 run_index++;
00557 put_bits(&s->pb, 1, 1);
00558 }
00559
00560 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
00561 if(run_index) run_index--;
00562 run_count=0;
00563 run_mode=0;
00564 if(diff>0) diff--;
00565 }else{
00566 run_count++;
00567 }
00568 }
00569
00570
00571
00572 if(run_mode == 0)
00573 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00574 }
00575 }
00576 if(run_mode){
00577 while(run_count >= 1<<ff_log2_run[run_index]){
00578 run_count -= 1<<ff_log2_run[run_index];
00579 run_index++;
00580 put_bits(&s->pb, 1, 1);
00581 }
00582
00583 if(run_count)
00584 put_bits(&s->pb, 1, 1);
00585 }
00586 s->run_index= run_index;
00587
00588 return 0;
00589 }
00590
00591 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00592 int x,y,i;
00593 const int ring_size= s->avctx->context_model ? 3 : 2;
00594 int_fast16_t *sample[3];
00595 s->run_index=0;
00596
00597 memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
00598
00599 for(y=0; y<h; y++){
00600 for(i=0; i<ring_size; i++)
00601 sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
00602
00603 sample[0][-1]= sample[1][0 ];
00604 sample[1][ w]= sample[1][w-1];
00605
00606 if(s->avctx->bits_per_raw_sample<=8){
00607 for(x=0; x<w; x++){
00608 sample[0][x]= src[x + stride*y];
00609 }
00610 encode_line(s, w, sample, plane_index, 8);
00611 }else{
00612 for(x=0; x<w; x++){
00613 sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
00614 }
00615 encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
00616 }
00617
00618 }
00619 }
00620
00621 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00622 int x, y, p, i;
00623 const int ring_size= s->avctx->context_model ? 3 : 2;
00624 int_fast16_t *sample[3][3];
00625 s->run_index=0;
00626
00627 memset(s->sample_buffer, 0, ring_size*3*(w+6)*sizeof(*s->sample_buffer));
00628
00629 for(y=0; y<h; y++){
00630 for(i=0; i<ring_size; i++)
00631 for(p=0; p<3; p++)
00632 sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
00633
00634 for(x=0; x<w; x++){
00635 int v= src[x + stride*y];
00636 int b= v&0xFF;
00637 int g= (v>>8)&0xFF;
00638 int r= (v>>16)&0xFF;
00639
00640 b -= g;
00641 r -= g;
00642 g += (b + r)>>2;
00643 b += 0x100;
00644 r += 0x100;
00645
00646
00647
00648 sample[0][0][x]= g;
00649 sample[1][0][x]= b;
00650 sample[2][0][x]= r;
00651 }
00652 for(p=0; p<3; p++){
00653 sample[p][0][-1]= sample[p][1][0 ];
00654 sample[p][1][ w]= sample[p][1][w-1];
00655 encode_line(s, w, sample[p], FFMIN(p, 1), 9);
00656 }
00657 }
00658 }
00659
00660 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00661 int last=0;
00662 int i;
00663 uint8_t state[CONTEXT_SIZE];
00664 memset(state, 128, sizeof(state));
00665
00666 for(i=1; i<128 ; i++){
00667 if(quant_table[i] != quant_table[i-1]){
00668 put_symbol(c, state, i-last-1, 0);
00669 last= i;
00670 }
00671 }
00672 put_symbol(c, state, i-last-1, 0);
00673 }
00674
00675 static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
00676 int i;
00677 for(i=0; i<5; i++)
00678 write_quant_table(c, quant_table[i]);
00679 }
00680
00681 static void write_header(FFV1Context *f){
00682 uint8_t state[CONTEXT_SIZE];
00683 int i, j;
00684 RangeCoder * const c= &f->slice_context[0]->c;
00685
00686 memset(state, 128, sizeof(state));
00687
00688 if(f->version < 2){
00689 put_symbol(c, state, f->version, 0);
00690 put_symbol(c, state, f->ac, 0);
00691 if(f->ac>1){
00692 for(i=1; i<256; i++){
00693 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00694 }
00695 }
00696 put_symbol(c, state, f->colorspace, 0);
00697 if(f->version>0)
00698 put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
00699 put_rac(c, state, 1);
00700 put_symbol(c, state, f->chroma_h_shift, 0);
00701 put_symbol(c, state, f->chroma_v_shift, 0);
00702 put_rac(c, state, 0);
00703
00704 write_quant_tables(c, f->quant_table);
00705 }else{
00706 put_symbol(c, state, f->slice_count, 0);
00707 for(i=0; i<f->slice_count; i++){
00708 FFV1Context *fs= f->slice_context[i];
00709 put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
00710 put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
00711 put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
00712 put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
00713 for(j=0; j<f->plane_count; j++){
00714 put_symbol(c, state, f->plane[j].quant_table_index, 0);
00715 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
00716 }
00717 }
00718 }
00719 }
00720 #endif
00721
00722 static av_cold int common_init(AVCodecContext *avctx){
00723 FFV1Context *s = avctx->priv_data;
00724
00725 s->avctx= avctx;
00726 s->flags= avctx->flags;
00727
00728 dsputil_init(&s->dsp, avctx);
00729
00730 s->width = avctx->width;
00731 s->height= avctx->height;
00732
00733 assert(s->width && s->height);
00734
00735 s->num_h_slices=1;
00736 s->num_v_slices=1;
00737
00738
00739 return 0;
00740 }
00741
00742 static int init_slice_state(FFV1Context *f){
00743 int i, j;
00744
00745 for(i=0; i<f->slice_count; i++){
00746 FFV1Context *fs= f->slice_context[i];
00747 for(j=0; j<f->plane_count; j++){
00748 PlaneContext * const p= &fs->plane[j];
00749
00750 if(fs->ac){
00751 if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00752 if(!p-> state)
00753 return AVERROR(ENOMEM);
00754 }else{
00755 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00756 if(!p->vlc_state)
00757 return AVERROR(ENOMEM);
00758 }
00759 }
00760
00761 if (fs->ac>1){
00762
00763 for(j=1; j<256; j++){
00764 fs->c.one_state [ j]= fs->state_transition[j];
00765 fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
00766 }
00767 }
00768 }
00769
00770 return 0;
00771 }
00772
00773 static av_cold int init_slice_contexts(FFV1Context *f){
00774 int i;
00775
00776 f->slice_count= f->num_h_slices * f->num_v_slices;
00777
00778 for(i=0; i<f->slice_count; i++){
00779 FFV1Context *fs= av_mallocz(sizeof(*fs));
00780 int sx= i % f->num_h_slices;
00781 int sy= i / f->num_h_slices;
00782 int sxs= f->avctx->width * sx / f->num_h_slices;
00783 int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
00784 int sys= f->avctx->height* sy / f->num_v_slices;
00785 int sye= f->avctx->height*(sy+1) / f->num_v_slices;
00786 f->slice_context[i]= fs;
00787 memcpy(fs, f, sizeof(*fs));
00788 memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
00789
00790 fs->slice_width = sxe - sxs;
00791 fs->slice_height= sye - sys;
00792 fs->slice_x = sxs;
00793 fs->slice_y = sys;
00794
00795 fs->sample_buffer = av_malloc(6 * (fs->width+6) * sizeof(*fs->sample_buffer));
00796 if (!fs->sample_buffer)
00797 return AVERROR(ENOMEM);
00798 }
00799 return 0;
00800 }
00801
00802 static int allocate_initial_states(FFV1Context *f){
00803 int i;
00804
00805 for(i=0; i<f->quant_table_count; i++){
00806 f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
00807 if(!f->initial_states[i])
00808 return AVERROR(ENOMEM);
00809 memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
00810 }
00811 return 0;
00812 }
00813
00814 #if CONFIG_FFV1_ENCODER
00815 static int write_extra_header(FFV1Context *f){
00816 RangeCoder * const c= &f->c;
00817 uint8_t state[CONTEXT_SIZE];
00818 int i, j, k;
00819 uint8_t state2[32][CONTEXT_SIZE];
00820
00821 memset(state2, 128, sizeof(state2));
00822 memset(state, 128, sizeof(state));
00823
00824 f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
00825 ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
00826 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00827
00828 put_symbol(c, state, f->version, 0);
00829 put_symbol(c, state, f->ac, 0);
00830 if(f->ac>1){
00831 for(i=1; i<256; i++){
00832 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00833 }
00834 }
00835 put_symbol(c, state, f->colorspace, 0);
00836 put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
00837 put_rac(c, state, 1);
00838 put_symbol(c, state, f->chroma_h_shift, 0);
00839 put_symbol(c, state, f->chroma_v_shift, 0);
00840 put_rac(c, state, 0);
00841 put_symbol(c, state, f->num_h_slices-1, 0);
00842 put_symbol(c, state, f->num_v_slices-1, 0);
00843
00844 put_symbol(c, state, f->quant_table_count, 0);
00845 for(i=0; i<f->quant_table_count; i++)
00846 write_quant_tables(c, f->quant_tables[i]);
00847
00848 for(i=0; i<f->quant_table_count; i++){
00849 for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
00850 if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
00851 break;
00852 if(j<f->context_count[i]*CONTEXT_SIZE){
00853 put_rac(c, state, 1);
00854 for(j=0; j<f->context_count[i]; j++){
00855 for(k=0; k<CONTEXT_SIZE; k++){
00856 int pred= j ? f->initial_states[i][j-1][k] : 128;
00857 put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
00858 }
00859 }
00860 }else{
00861 put_rac(c, state, 0);
00862 }
00863 }
00864
00865 f->avctx->extradata_size= ff_rac_terminate(c);
00866
00867 return 0;
00868 }
00869
00870 static int sort_stt(FFV1Context *s, uint8_t stt[256]){
00871 int i,i2,changed,print=0;
00872
00873 do{
00874 changed=0;
00875 for(i=12; i<244; i++){
00876 for(i2=i+1; i2<245 && i2<i+4; i2++){
00877 #define COST(old, new) \
00878 s->rc_stat[old][0]*-log2((256-(new))/256.0)\
00879 +s->rc_stat[old][1]*-log2( (new) /256.0)
00880
00881 #define COST2(old, new) \
00882 COST(old, new)\
00883 +COST(256-(old), 256-(new))
00884
00885 double size0= COST2(i, i ) + COST2(i2, i2);
00886 double sizeX= COST2(i, i2) + COST2(i2, i );
00887 if(sizeX < size0 && i!=128 && i2!=128){
00888 int j;
00889 FFSWAP(int, stt[ i], stt[ i2]);
00890 FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
00891 FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
00892 if(i != 256-i2){
00893 FFSWAP(int, stt[256-i], stt[256-i2]);
00894 FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
00895 FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
00896 }
00897 for(j=1; j<256; j++){
00898 if (stt[j] == i ) stt[j] = i2;
00899 else if(stt[j] == i2) stt[j] = i ;
00900 if(i != 256-i2){
00901 if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
00902 else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
00903 }
00904 }
00905 print=changed=1;
00906 }
00907 }
00908 }
00909 }while(changed);
00910 return print;
00911 }
00912
00913 static av_cold int encode_init(AVCodecContext *avctx)
00914 {
00915 FFV1Context *s = avctx->priv_data;
00916 int i, j, k, m;
00917
00918 common_init(avctx);
00919
00920 s->version=0;
00921 s->ac= avctx->coder_type ? 2:0;
00922
00923 if(s->ac>1)
00924 for(i=1; i<256; i++)
00925 s->state_transition[i]=ver2_state[i];
00926
00927 s->plane_count=2;
00928 for(i=0; i<256; i++){
00929 s->quant_table_count=2;
00930 if(avctx->bits_per_raw_sample <=8){
00931 s->quant_tables[0][0][i]= quant11[i];
00932 s->quant_tables[0][1][i]= 11*quant11[i];
00933 s->quant_tables[0][2][i]= 11*11*quant11[i];
00934 s->quant_tables[1][0][i]= quant11[i];
00935 s->quant_tables[1][1][i]= 11*quant11[i];
00936 s->quant_tables[1][2][i]= 11*11*quant5 [i];
00937 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
00938 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
00939 }else{
00940 s->quant_tables[0][0][i]= quant9_10bit[i];
00941 s->quant_tables[0][1][i]= 11*quant9_10bit[i];
00942 s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
00943 s->quant_tables[1][0][i]= quant9_10bit[i];
00944 s->quant_tables[1][1][i]= 11*quant9_10bit[i];
00945 s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
00946 s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
00947 s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
00948 }
00949 }
00950 s->context_count[0]= (11*11*11+1)/2;
00951 s->context_count[1]= (11*11*5*5*5+1)/2;
00952 memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
00953
00954 for(i=0; i<s->plane_count; i++){
00955 PlaneContext * const p= &s->plane[i];
00956
00957 memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
00958 p->quant_table_index= avctx->context_model;
00959 p->context_count= s->context_count[p->quant_table_index];
00960 }
00961
00962 if(allocate_initial_states(s) < 0)
00963 return AVERROR(ENOMEM);
00964
00965 avctx->coded_frame= &s->picture;
00966 switch(avctx->pix_fmt){
00967 case PIX_FMT_YUV444P16:
00968 case PIX_FMT_YUV422P16:
00969 case PIX_FMT_YUV420P16:
00970 if(avctx->bits_per_raw_sample <=8){
00971 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
00972 return -1;
00973 }
00974 if(!s->ac){
00975 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
00976 return -1;
00977 }
00978 s->version= FFMAX(s->version, 1);
00979 case PIX_FMT_YUV444P:
00980 case PIX_FMT_YUV422P:
00981 case PIX_FMT_YUV420P:
00982 case PIX_FMT_YUV411P:
00983 case PIX_FMT_YUV410P:
00984 s->colorspace= 0;
00985 break;
00986 case PIX_FMT_RGB32:
00987 s->colorspace= 1;
00988 break;
00989 default:
00990 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00991 return -1;
00992 }
00993 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00994
00995 s->picture_number=0;
00996
00997 if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
00998 for(i=0; i<s->quant_table_count; i++){
00999 s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
01000 if(!s->rc_stat2[i])
01001 return AVERROR(ENOMEM);
01002 }
01003 }
01004 if(avctx->stats_in){
01005 char *p= avctx->stats_in;
01006 uint8_t best_state[256][256];
01007 int gob_count=0;
01008 char *next;
01009
01010 av_assert0(s->version>=2);
01011
01012 for(;;){
01013 for(j=0; j<256; j++){
01014 for(i=0; i<2; i++){
01015 s->rc_stat[j][i]= strtol(p, &next, 0);
01016 if(next==p){
01017 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
01018 return -1;
01019 }
01020 p=next;
01021 }
01022 }
01023 for(i=0; i<s->quant_table_count; i++){
01024 for(j=0; j<s->context_count[i]; j++){
01025 for(k=0; k<32; k++){
01026 for(m=0; m<2; m++){
01027 s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
01028 if(next==p){
01029 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
01030 return -1;
01031 }
01032 p=next;
01033 }
01034 }
01035 }
01036 }
01037 gob_count= strtol(p, &next, 0);
01038 if(next==p || gob_count <0){
01039 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
01040 return -1;
01041 }
01042 p=next;
01043 while(*p=='\n' || *p==' ') p++;
01044 if(p[0]==0) break;
01045 }
01046 sort_stt(s, s->state_transition);
01047
01048 find_best_state(best_state, s->state_transition);
01049
01050 for(i=0; i<s->quant_table_count; i++){
01051 for(j=0; j<s->context_count[i]; j++){
01052 for(k=0; k<32; k++){
01053 double p= 128;
01054 if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
01055 p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
01056 }
01057 s->initial_states[i][j][k]= best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1])/gob_count, 0, 255)];
01058 }
01059 }
01060 }
01061 }
01062
01063 if(s->version>1){
01064 s->num_h_slices=2;
01065 s->num_v_slices=2;
01066 write_extra_header(s);
01067 }
01068
01069 if(init_slice_contexts(s) < 0)
01070 return -1;
01071 if(init_slice_state(s) < 0)
01072 return -1;
01073
01074 #define STATS_OUT_SIZE 1024*1024*6
01075 if(avctx->flags & CODEC_FLAG_PASS1){
01076 avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
01077 for(i=0; i<s->quant_table_count; i++){
01078 for(j=0; j<s->slice_count; j++){
01079 FFV1Context *sf= s->slice_context[j];
01080 av_assert0(!sf->rc_stat2[i]);
01081 sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
01082 if(!sf->rc_stat2[i])
01083 return AVERROR(ENOMEM);
01084 }
01085 }
01086 }
01087
01088 return 0;
01089 }
01090 #endif
01091
01092
01093 static void clear_state(FFV1Context *f){
01094 int i, si, j;
01095
01096 for(si=0; si<f->slice_count; si++){
01097 FFV1Context *fs= f->slice_context[si];
01098 for(i=0; i<f->plane_count; i++){
01099 PlaneContext *p= &fs->plane[i];
01100
01101 p->interlace_bit_state[0]= 128;
01102 p->interlace_bit_state[1]= 128;
01103
01104 if(fs->ac){
01105 if(f->initial_states[p->quant_table_index]){
01106 memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
01107 }else
01108 memset(p->state, 128, CONTEXT_SIZE*p->context_count);
01109 }else{
01110 for(j=0; j<p->context_count; j++){
01111 p->vlc_state[j].drift= 0;
01112 p->vlc_state[j].error_sum= 4;
01113 p->vlc_state[j].bias= 0;
01114 p->vlc_state[j].count= 1;
01115 }
01116 }
01117 }
01118 }
01119 }
01120
01121 #if CONFIG_FFV1_ENCODER
01122 static int encode_slice(AVCodecContext *c, void *arg){
01123 FFV1Context *fs= *(void**)arg;
01124 FFV1Context *f= fs->avctx->priv_data;
01125 int width = fs->slice_width;
01126 int height= fs->slice_height;
01127 int x= fs->slice_x;
01128 int y= fs->slice_y;
01129 AVFrame * const p= &f->picture;
01130
01131 if(f->colorspace==0){
01132 const int chroma_width = -((-width )>>f->chroma_h_shift);
01133 const int chroma_height= -((-height)>>f->chroma_v_shift);
01134 const int cx= x>>f->chroma_h_shift;
01135 const int cy= y>>f->chroma_v_shift;
01136
01137 encode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
01138
01139 encode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01140 encode_plane(fs, p->data[2] + cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
01141 }else{
01142 encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
01143 }
01144 emms_c();
01145
01146 return 0;
01147 }
01148
01149 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
01150 FFV1Context *f = avctx->priv_data;
01151 RangeCoder * const c= &f->slice_context[0]->c;
01152 AVFrame *pict = data;
01153 AVFrame * const p= &f->picture;
01154 int used_count= 0;
01155 uint8_t keystate=128;
01156 uint8_t *buf_p;
01157 int i;
01158
01159 ff_init_range_encoder(c, buf, buf_size);
01160 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01161
01162 *p = *pict;
01163 p->pict_type= FF_I_TYPE;
01164
01165 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
01166 put_rac(c, &keystate, 1);
01167 p->key_frame= 1;
01168 f->gob_count++;
01169 write_header(f);
01170 clear_state(f);
01171 }else{
01172 put_rac(c, &keystate, 0);
01173 p->key_frame= 0;
01174 }
01175
01176 if(!f->ac){
01177 used_count += ff_rac_terminate(c);
01178
01179 init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
01180 }else if (f->ac>1){
01181 int i;
01182 for(i=1; i<256; i++){
01183 c->one_state[i]= f->state_transition[i];
01184 c->zero_state[256-i]= 256-c->one_state[i];
01185 }
01186 }
01187
01188 for(i=1; i<f->slice_count; i++){
01189 FFV1Context *fs= f->slice_context[i];
01190 uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
01191 int len= buf_size/f->slice_count;
01192
01193 if(fs->ac){
01194 ff_init_range_encoder(&fs->c, start, len);
01195 }else{
01196 init_put_bits(&fs->pb, start, len);
01197 }
01198 }
01199 avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01200
01201 buf_p=buf;
01202 for(i=0; i<f->slice_count; i++){
01203 FFV1Context *fs= f->slice_context[i];
01204 int bytes;
01205
01206 if(fs->ac){
01207 uint8_t state=128;
01208 put_rac(&fs->c, &state, 0);
01209 bytes= ff_rac_terminate(&fs->c);
01210 }else{
01211 flush_put_bits(&fs->pb);
01212 bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
01213 used_count= 0;
01214 }
01215 if(i>0){
01216 av_assert0(bytes < buf_size/f->slice_count);
01217 memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
01218 av_assert0(bytes < (1<<24));
01219 AV_WB24(buf_p+bytes, bytes);
01220 bytes+=3;
01221 }
01222 buf_p += bytes;
01223 }
01224
01225 if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
01226 int j, k, m;
01227 char *p= avctx->stats_out;
01228 char *end= p + STATS_OUT_SIZE;
01229
01230 memset(f->rc_stat, 0, sizeof(f->rc_stat));
01231 for(i=0; i<f->quant_table_count; i++)
01232 memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
01233
01234 for(j=0; j<f->slice_count; j++){
01235 FFV1Context *fs= f->slice_context[j];
01236 for(i=0; i<256; i++){
01237 f->rc_stat[i][0] += fs->rc_stat[i][0];
01238 f->rc_stat[i][1] += fs->rc_stat[i][1];
01239 }
01240 for(i=0; i<f->quant_table_count; i++){
01241 for(k=0; k<f->context_count[i]; k++){
01242 for(m=0; m<32; m++){
01243 f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
01244 f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
01245 }
01246 }
01247 }
01248 }
01249
01250 for(j=0; j<256; j++){
01251 snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
01252 p+= strlen(p);
01253 }
01254 snprintf(p, end-p, "\n");
01255
01256 for(i=0; i<f->quant_table_count; i++){
01257 for(j=0; j<f->context_count[i]; j++){
01258 for(m=0; m<32; m++){
01259 snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
01260 p+= strlen(p);
01261 }
01262 }
01263 }
01264 snprintf(p, end-p, "%d\n", f->gob_count);
01265 } else if(avctx->flags&CODEC_FLAG_PASS1)
01266 avctx->stats_out[0] = '\0';
01267
01268 f->picture_number++;
01269 return buf_p-buf;
01270 }
01271 #endif
01272
01273 static av_cold int common_end(AVCodecContext *avctx){
01274 FFV1Context *s = avctx->priv_data;
01275 int i, j;
01276
01277 if (avctx->codec->decode && s->picture.data[0])
01278 avctx->release_buffer(avctx, &s->picture);
01279
01280 for(j=0; j<s->slice_count; j++){
01281 FFV1Context *fs= s->slice_context[j];
01282 for(i=0; i<s->plane_count; i++){
01283 PlaneContext *p= &fs->plane[i];
01284
01285 av_freep(&p->state);
01286 av_freep(&p->vlc_state);
01287 }
01288 av_freep(&fs->sample_buffer);
01289 }
01290
01291 av_freep(&avctx->stats_out);
01292 for(j=0; j<s->quant_table_count; j++){
01293 av_freep(&s->initial_states[j]);
01294 for(i=0; i<s->slice_count; i++){
01295 FFV1Context *sf= s->slice_context[i];
01296 av_freep(&sf->rc_stat2[j]);
01297 }
01298 av_freep(&s->rc_stat2[j]);
01299 }
01300
01301 for(i=0; i<s->slice_count; i++){
01302 av_freep(&s->slice_context[i]);
01303 }
01304
01305 return 0;
01306 }
01307
01308 static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
01309 PlaneContext * const p= &s->plane[plane_index];
01310 RangeCoder * const c= &s->c;
01311 int x;
01312 int run_count=0;
01313 int run_mode=0;
01314 int run_index= s->run_index;
01315
01316 for(x=0; x<w; x++){
01317 int diff, context, sign;
01318
01319 context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
01320 if(context < 0){
01321 context= -context;
01322 sign=1;
01323 }else
01324 sign=0;
01325
01326 av_assert2(context < p->context_count);
01327
01328 if(s->ac){
01329 diff= get_symbol_inline(c, p->state[context], 1);
01330 }else{
01331 if(context == 0 && run_mode==0) run_mode=1;
01332
01333 if(run_mode){
01334 if(run_count==0 && run_mode==1){
01335 if(get_bits1(&s->gb)){
01336 run_count = 1<<ff_log2_run[run_index];
01337 if(x + run_count <= w) run_index++;
01338 }else{
01339 if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
01340 else run_count=0;
01341 if(run_index) run_index--;
01342 run_mode=2;
01343 }
01344 }
01345 run_count--;
01346 if(run_count < 0){
01347 run_mode=0;
01348 run_count=0;
01349 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01350 if(diff>=0) diff++;
01351 }else
01352 diff=0;
01353 }else
01354 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01355
01356
01357 }
01358
01359 if(sign) diff= -diff;
01360
01361 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
01362 }
01363 s->run_index= run_index;
01364 }
01365
01366 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
01367 int x, y;
01368 int_fast16_t *sample[2];
01369 sample[0]=s->sample_buffer +3;
01370 sample[1]=s->sample_buffer+w+6+3;
01371
01372 s->run_index=0;
01373
01374 memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
01375
01376 for(y=0; y<h; y++){
01377 int_fast16_t *temp= sample[0];
01378
01379 sample[0]= sample[1];
01380 sample[1]= temp;
01381
01382 sample[1][-1]= sample[0][0 ];
01383 sample[0][ w]= sample[0][w-1];
01384
01385
01386 if(s->avctx->bits_per_raw_sample <= 8){
01387 decode_line(s, w, sample, plane_index, 8);
01388 for(x=0; x<w; x++){
01389 src[x + stride*y]= sample[1][x];
01390 }
01391 }else{
01392 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
01393 for(x=0; x<w; x++){
01394 ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
01395 }
01396 }
01397
01398 }
01399 }
01400
01401 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
01402 int x, y, p;
01403 int_fast16_t *sample[3][2];
01404 for(x=0; x<3; x++){
01405 sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
01406 sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
01407 }
01408
01409 s->run_index=0;
01410
01411 memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
01412
01413 for(y=0; y<h; y++){
01414 for(p=0; p<3; p++){
01415 int_fast16_t *temp= sample[p][0];
01416
01417 sample[p][0]= sample[p][1];
01418 sample[p][1]= temp;
01419
01420 sample[p][1][-1]= sample[p][0][0 ];
01421 sample[p][0][ w]= sample[p][0][w-1];
01422 decode_line(s, w, sample[p], FFMIN(p, 1), 9);
01423 }
01424 for(x=0; x<w; x++){
01425 int g= sample[0][1][x];
01426 int b= sample[1][1][x];
01427 int r= sample[2][1][x];
01428
01429
01430
01431
01432 b -= 0x100;
01433 r -= 0x100;
01434 g -= (b + r)>>2;
01435 b += g;
01436 r += g;
01437
01438 src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
01439 }
01440 }
01441 }
01442
01443 static int decode_slice(AVCodecContext *c, void *arg){
01444 FFV1Context *fs= *(void**)arg;
01445 FFV1Context *f= fs->avctx->priv_data;
01446 int width = fs->slice_width;
01447 int height= fs->slice_height;
01448 int x= fs->slice_x;
01449 int y= fs->slice_y;
01450 AVFrame * const p= &f->picture;
01451
01452 av_assert1(width && height);
01453 if(f->colorspace==0){
01454 const int chroma_width = -((-width )>>f->chroma_h_shift);
01455 const int chroma_height= -((-height)>>f->chroma_v_shift);
01456 const int cx= x>>f->chroma_h_shift;
01457 const int cy= y>>f->chroma_v_shift;
01458 decode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
01459
01460 decode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01461 decode_plane(fs, p->data[2] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[2], 1);
01462 }else{
01463 decode_rgb_frame(fs, (uint32_t*)p->data[0] + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
01464 }
01465
01466 emms_c();
01467
01468 return 0;
01469 }
01470
01471 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
01472 int v;
01473 int i=0;
01474 uint8_t state[CONTEXT_SIZE];
01475
01476 memset(state, 128, sizeof(state));
01477
01478 for(v=0; i<128 ; v++){
01479 int len= get_symbol(c, state, 0) + 1;
01480
01481 if(len + i > 128) return -1;
01482
01483 while(len--){
01484 quant_table[i] = scale*v;
01485 i++;
01486
01487
01488 }
01489 }
01490
01491 for(i=1; i<128; i++){
01492 quant_table[256-i]= -quant_table[i];
01493 }
01494 quant_table[128]= -quant_table[127];
01495
01496 return 2*v - 1;
01497 }
01498
01499 static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
01500 int i;
01501 int context_count=1;
01502
01503 for(i=0; i<5; i++){
01504 context_count*= read_quant_table(c, quant_table[i], context_count);
01505 if(context_count > 32768U){
01506 return -1;
01507 }
01508 }
01509 return (context_count+1)/2;
01510 }
01511
01512 static int read_extra_header(FFV1Context *f){
01513 RangeCoder * const c= &f->c;
01514 uint8_t state[CONTEXT_SIZE];
01515 int i, j, k;
01516 uint8_t state2[32][CONTEXT_SIZE];
01517
01518 memset(state2, 128, sizeof(state2));
01519 memset(state, 128, sizeof(state));
01520
01521 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
01522 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01523
01524 f->version= get_symbol(c, state, 0);
01525 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01526 if(f->ac>1){
01527 for(i=1; i<256; i++){
01528 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01529 }
01530 }
01531 f->colorspace= get_symbol(c, state, 0);
01532 f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01533 get_rac(c, state);
01534 f->chroma_h_shift= get_symbol(c, state, 0);
01535 f->chroma_v_shift= get_symbol(c, state, 0);
01536 get_rac(c, state);
01537 f->plane_count= 2;
01538 f->num_h_slices= 1 + get_symbol(c, state, 0);
01539 f->num_v_slices= 1 + get_symbol(c, state, 0);
01540 if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
01541 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
01542 return -1;
01543 }
01544
01545 f->quant_table_count= get_symbol(c, state, 0);
01546 if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
01547 return -1;
01548 for(i=0; i<f->quant_table_count; i++){
01549 if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
01550 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01551 return -1;
01552 }
01553 }
01554
01555 if(allocate_initial_states(f) < 0)
01556 return AVERROR(ENOMEM);
01557
01558 for(i=0; i<f->quant_table_count; i++){
01559 if(get_rac(c, state)){
01560 for(j=0; j<f->context_count[i]; j++){
01561 for(k=0; k<CONTEXT_SIZE; k++){
01562 int pred= j ? f->initial_states[i][j-1][k] : 128;
01563 f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
01564 }
01565 }
01566 }
01567 }
01568
01569 return 0;
01570 }
01571
01572 static int read_header(FFV1Context *f){
01573 uint8_t state[CONTEXT_SIZE];
01574 int i, j, context_count;
01575 RangeCoder * const c= &f->slice_context[0]->c;
01576
01577 memset(state, 128, sizeof(state));
01578
01579 if(f->version < 2){
01580 f->version= get_symbol(c, state, 0);
01581 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01582 if(f->ac>1){
01583 for(i=1; i<256; i++){
01584 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01585 }
01586 }
01587 f->colorspace= get_symbol(c, state, 0);
01588 if(f->version>0)
01589 f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01590 get_rac(c, state);
01591 f->chroma_h_shift= get_symbol(c, state, 0);
01592 f->chroma_v_shift= get_symbol(c, state, 0);
01593 get_rac(c, state);
01594 f->plane_count= 2;
01595 }
01596
01597 if(f->colorspace==0){
01598 if(f->avctx->bits_per_raw_sample<=8){
01599 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01600 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
01601 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
01602 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
01603 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
01604 case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
01605 default:
01606 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01607 return -1;
01608 }
01609 }else{
01610 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01611 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
01612 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
01613 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
01614 default:
01615 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01616 return -1;
01617 }
01618 }
01619 }else if(f->colorspace==1){
01620 if(f->chroma_h_shift || f->chroma_v_shift){
01621 av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
01622 return -1;
01623 }
01624 f->avctx->pix_fmt= PIX_FMT_RGB32;
01625 }else{
01626 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
01627 return -1;
01628 }
01629
01630
01631 if(f->version < 2){
01632 context_count= read_quant_tables(c, f->quant_table);
01633 if(context_count < 0){
01634 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01635 return -1;
01636 }
01637 }else{
01638 f->slice_count= get_symbol(c, state, 0);
01639 if(f->slice_count > (unsigned)MAX_SLICES)
01640 return -1;
01641 }
01642
01643 for(j=0; j<f->slice_count; j++){
01644 FFV1Context *fs= f->slice_context[j];
01645 fs->ac= f->ac;
01646
01647 if(f->version >= 2){
01648 fs->slice_x = get_symbol(c, state, 0) *f->width ;
01649 fs->slice_y = get_symbol(c, state, 0) *f->height;
01650 fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
01651 fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
01652
01653 fs->slice_x /= f->num_h_slices;
01654 fs->slice_y /= f->num_v_slices;
01655 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
01656 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
01657 if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
01658 return -1;
01659 if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
01660 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
01661 return -1;
01662 }
01663
01664 for(i=0; i<f->plane_count; i++){
01665 PlaneContext * const p= &fs->plane[i];
01666
01667 if(f->version >= 2){
01668 int idx=get_symbol(c, state, 0);
01669 if(idx > (unsigned)f->quant_table_count){
01670 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
01671 return -1;
01672 }
01673 p->quant_table_index= idx;
01674 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
01675 context_count= f->context_count[idx];
01676 }else{
01677 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
01678 }
01679
01680 if(p->context_count < context_count){
01681 av_freep(&p->state);
01682 av_freep(&p->vlc_state);
01683 }
01684 p->context_count= context_count;
01685 }
01686 }
01687
01688 return 0;
01689 }
01690
01691 static av_cold int decode_init(AVCodecContext *avctx)
01692 {
01693 FFV1Context *f = avctx->priv_data;
01694
01695 common_init(avctx);
01696
01697 if(avctx->extradata && read_extra_header(f) < 0)
01698 return -1;
01699
01700 if(init_slice_contexts(f) < 0)
01701 return -1;
01702
01703 return 0;
01704 }
01705
01706 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
01707 const uint8_t *buf = avpkt->data;
01708 int buf_size = avpkt->size;
01709 FFV1Context *f = avctx->priv_data;
01710 RangeCoder * const c= &f->slice_context[0]->c;
01711 AVFrame * const p= &f->picture;
01712 int bytes_read, i;
01713 uint8_t keystate= 128;
01714 const uint8_t *buf_p;
01715
01716 AVFrame *picture = data;
01717
01718
01719 if (p->data[0])
01720 avctx->release_buffer(avctx, p);
01721
01722 ff_init_range_decoder(c, buf, buf_size);
01723 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01724
01725
01726 p->pict_type= FF_I_TYPE;
01727 if(get_rac(c, &keystate)){
01728 p->key_frame= 1;
01729 if(read_header(f) < 0)
01730 return -1;
01731 if(init_slice_state(f) < 0)
01732 return -1;
01733
01734 clear_state(f);
01735 }else{
01736 p->key_frame= 0;
01737 }
01738 if(f->ac>1){
01739 int i;
01740 for(i=1; i<256; i++){
01741 c->one_state[i]= f->state_transition[i];
01742 c->zero_state[256-i]= 256-c->one_state[i];
01743 }
01744 }
01745
01746 p->reference= 0;
01747 if(avctx->get_buffer(avctx, p) < 0){
01748 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01749 return -1;
01750 }
01751
01752 if(avctx->debug&FF_DEBUG_PICT_INFO)
01753 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
01754
01755 if(!f->ac){
01756 bytes_read = c->bytestream - c->bytestream_start - 1;
01757 if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n");
01758
01759 init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, buf_size - bytes_read);
01760 } else {
01761 bytes_read = 0;
01762 }
01763
01764 buf_p= buf + buf_size;
01765 for(i=f->slice_count-1; i>0; i--){
01766 FFV1Context *fs= f->slice_context[i];
01767 int v= AV_RB24(buf_p-3)+3;
01768 if(buf_p - buf <= v){
01769 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
01770 return -1;
01771 }
01772 buf_p -= v;
01773 if(fs->ac){
01774 ff_init_range_decoder(&fs->c, buf_p, v);
01775 }else{
01776 init_get_bits(&fs->gb, buf_p, v);
01777 }
01778 }
01779
01780 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01781 f->picture_number++;
01782
01783 *picture= *p;
01784 *data_size = sizeof(AVFrame);
01785
01786 return buf_size;
01787 }
01788
01789 AVCodec ff_ffv1_decoder = {
01790 "ffv1",
01791 AVMEDIA_TYPE_VIDEO,
01792 CODEC_ID_FFV1,
01793 sizeof(FFV1Context),
01794 decode_init,
01795 NULL,
01796 common_end,
01797 decode_frame,
01798 CODEC_CAP_DR1 ,
01799 NULL,
01800 .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
01801 };
01802
01803 #if CONFIG_FFV1_ENCODER
01804 AVCodec ff_ffv1_encoder = {
01805 "ffv1",
01806 AVMEDIA_TYPE_VIDEO,
01807 CODEC_ID_FFV1,
01808 sizeof(FFV1Context),
01809 encode_init,
01810 encode_frame,
01811 common_end,
01812 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_NONE},
01813 .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
01814 };
01815 #endif