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,-5,-5,-5,-5,-5,
115 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
116 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
117 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
118 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
122 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
123 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
124 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
125 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
126 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
127 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
128 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
129 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
130 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
131 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
132 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
133 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
134 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
135 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
136 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
137 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
156 #define MAX_SLICES 256
210 static inline int predict(int16_t *src, int16_t *last)
212 const int LT= last[-1];
213 const int T= last[ 0];
214 const int L = src[-1];
220 int16_t *last, int16_t *last2)
222 const int LT= last[-1];
223 const int T= last[ 0];
224 const int RT= last[ 1];
225 const int L = src[-1];
228 const int TT= last2[0];
229 const int LL= src[-2];
236 static void find_best_state(uint8_t best_state[256][256],
const uint8_t one_state[256]){
241 l2tab[i]=
log2(i/256.0);
243 for(i=0; i<256; i++){
244 double best_len[256];
254 for(k=0; k<256; k++){
255 double newocc[256]={0};
256 for(m=0; m<256; m++){
258 len -=occ[m]*( p *l2tab[ m]
259 + (1-p)*l2tab[256-m]);
262 if(len < best_len[k]){
266 for(m=0; m<256; m++){
268 newocc[ one_state[ m]] += occ[m]* p ;
269 newocc[256-one_state[256-m]] += occ[m]*(1-p);
272 memcpy(occ, newocc,
sizeof(occ));
281 #define put_rac(C,S,B) \
285 rc_stat2[(S)-state][B]++;\
292 const int e= av_log2(a);
300 for(i=e-1; i>=0; i--){
301 put_rac(c, state+22+i, (a>>i)&1);
305 put_rac(c, state+11 + e, v < 0);
312 for(i=e-1; i>=0; i--){
317 put_rac(c, state+11 + 10, v < 0);
340 for(i=e-1; i>=0; i--){
354 int drift= state->
drift;
355 int count= state->
count;
367 if(state->
bias > -128) state->
bias--;
373 if(state->
bias < 127) state->
bias++;
391 while(i < state->error_sum){
399 if(k==0 && 2*state->
drift <= - state->
count) code= v ^ (-1);
402 code= v ^ ((2*state->
drift + state->
count)>>31);
416 while(i < state->error_sum){
427 if(k==0 && 2*state->
drift <= - state->
count) v ^= (-1);
439 #if CONFIG_FFV1_ENCODER
442 int plane_index,
int bits)
466 context=
get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
467 diff= sample[0][x] -
predict(sample[0]+x, sample[1]+x);
474 diff=
fold(diff, bits);
483 if(context == 0) run_mode=1;
495 if(run_index) run_index--;
525 static void encode_plane(
FFV1Context *s, uint8_t *src,
int w,
int h,
int stride,
int plane_index){
534 for(i=0; i<ring_size; i++)
537 sample[0][-1]= sample[1][0 ];
538 sample[1][ w]= sample[1][w-1];
542 sample[0][x]= src[x + stride*y];
544 encode_line(s, w, sample, plane_index, 8);
555 static void encode_rgb_frame(
FFV1Context *s, uint32_t *src,
int w,
int h,
int stride){
558 int16_t *sample[3][3];
564 for(i=0; i<ring_size; i++)
566 sample[p][i]= s->
sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
569 int v= src[x + stride*y];
587 sample[p][0][-1]= sample[p][1][0 ];
588 sample[p][1][ w]= sample[p][1][w-1];
589 encode_line(s, w, sample[p],
FFMIN(p, 1), 9);
598 memset(state, 128,
sizeof(state));
600 for(i=1; i<128 ; i++){
601 if(quant_table[i] != quant_table[i-1]){
612 write_quant_table(c, quant_table[i]);
620 memset(state, 128,
sizeof(state));
626 for(i=1; i<256; i++){
697 for(j=1; j<256; j++){
721 memcpy(fs, f,
sizeof(*fs));
748 #if CONFIG_FFV1_ENCODER
755 memset(state2, 128,
sizeof(state2));
756 memset(state, 128,
sizeof(state));
765 for(i=1; i<256; i++){
804 static int sort_stt(
FFV1Context *s, uint8_t stt[256]){
805 int i,i2,changed,print=0;
809 for(i=12; i<244; i++){
810 for(i2=i+1; i2<245 && i2<i+4; i2++){
811 #define COST(old, new) \
812 s->rc_stat[old][0]*-log2((256-(new))/256.0)\
813 +s->rc_stat[old][1]*-log2( (new) /256.0)
815 #define COST2(old, new) \
817 +COST(256-(old), 256-(new))
819 double size0= COST2(i, i ) + COST2(i2, i2);
820 double sizeX= COST2(i, i2) + COST2(i2, i );
821 if(sizeX < size0 && i!=128 && i2!=128){
823 FFSWAP(
int, stt[ i], stt[ i2]);
827 FFSWAP(
int, stt[256-i], stt[256-i2]);
831 for(j=1; j<256; j++){
832 if (stt[j] == i ) stt[j] = i2;
833 else if(stt[j] == i2) stt[j] = i ;
835 if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
836 else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
862 for(i=0; i<256; i++){
909 av_log(avctx,
AV_LOG_ERROR,
"bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
940 uint8_t best_state[256][256];
947 for(j=0; j<256; j++){
949 s->
rc_stat[j][i]= strtol(p, &next, 0);
961 s->
rc_stat2[i][j][k][m]= strtol(p, &next, 0);
971 gob_count= strtol(p, &next, 0);
972 if(next==p || gob_count <0){
977 while(*p==
'\n' || *p==
' ') p++;
1000 write_extra_header(s);
1008 #define STATS_OUT_SIZE 1024*1024*6
1055 #if CONFIG_FFV1_ENCODER
1073 encode_plane(fs, p->
data[1] + cx+cy*p->
linesize[1], chroma_width, chroma_height, p->
linesize[1], 1);
1074 encode_plane(fs, p->
data[2] + cx+cy*p->
linesize[2], chroma_width, chroma_height, p->
linesize[2], 1);
1076 encode_rgb_frame(fs, (uint32_t*)(p->
data[0]) + x + y*(p->
linesize[0]/4), width, height, p->
linesize[0]/4);
1089 uint8_t keystate=128;
1116 for(i=1; i<256; i++){
1124 uint8_t *start= buf + (buf_size-used_count)*i/f->
slice_count;
1162 char *end= p + STATS_OUT_SIZE;
1170 for(i=0; i<256; i++){
1176 for(m=0; m<32; m++){
1184 for(j=0; j<256; j++){
1185 snprintf(p, end-p,
"%"PRIu64
" %"PRIu64
" ", f->
rc_stat[j][0], f->
rc_stat[j][1]);
1188 snprintf(p, end-p,
"\n");
1192 for(m=0; m<32; m++){
1193 snprintf(p, end-p,
"%"PRIu64
" %"PRIu64
" ", f->
rc_stat2[i][j][m][0], f->
rc_stat2[i][j][m][1]);
1198 snprintf(p, end-p,
"%d\n", f->
gob_count);
1244 int plane_index,
int bits)
1254 int diff, context, sign;
1256 context=
get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
1268 if(context == 0 && run_mode==0) run_mode=1;
1271 if(run_count==0 && run_mode==1){
1274 if(x + run_count <= w) run_index++;
1278 if(run_index) run_index--;
1296 if(sign) diff= -diff;
1298 sample[1][x]= (
predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
1314 int16_t *temp = sample[0];
1316 sample[0]= sample[1];
1319 sample[1][-1]= sample[0][0 ];
1320 sample[0][ w]= sample[0][w-1];
1326 src[x + stride*y]= sample[1][x];
1340 int16_t *sample[3][2];
1352 int16_t *temp = sample[p][0];
1354 sample[p][0]= sample[p][1];
1357 sample[p][1][-1]= sample[p][0][0 ];
1358 sample[p][0][ w]= sample[p][0][w-1];
1362 int g= sample[0][1][x];
1363 int b= sample[1][1][x];
1364 int r= sample[2][1][x];
1375 src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
1413 memset(state, 128,
sizeof(state));
1415 for(v=0; i<128 ; v++){
1418 if(len + i > 128)
return -1;
1421 quant_table[i] = scale*
v;
1428 for(i=1; i<128; i++){
1429 quant_table[256-i]= -quant_table[i];
1431 quant_table[128]= -quant_table[127];
1442 if(context_count > 32768
U){
1446 return (context_count+1)/2;
1455 memset(state2, 128,
sizeof(state2));
1456 memset(state, 128,
sizeof(state));
1464 for(i=1; i<256; i++){
1514 memset(state, 128,
sizeof(state));
1520 for(i=1; i<256; i++){
1570 if(context_count < 0){
1644 const uint8_t *buf = avpkt->
data;
1645 int buf_size = avpkt->
size;
1650 uint8_t keystate= 128;
1651 const uint8_t *buf_p;
1677 for(i=1; i<256; i++){
1701 buf_p= buf + buf_size;
1705 if(buf_p - buf <= v){
1738 #if CONFIG_FFV1_ENCODER
1748 .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},