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