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