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 "internal.h"
00030 #include "get_bits.h"
00031 #include "put_bits.h"
00032 #include "dsputil.h"
00033 #include "rangecoder.h"
00034 #include "golomb.h"
00035 #include "mathops.h"
00036 #include "libavutil/pixdesc.h"
00037 #include "libavutil/avassert.h"
00038 #include "libavutil/crc.h"
00039 #include "libavutil/opt.h"
00040 #include "libavutil/imgutils.h"
00041 #include "libavutil/timer.h"
00042
00043 #ifdef __INTEL_COMPILER
00044 #undef av_flatten
00045 #define av_flatten
00046 #endif
00047
00048 #define MAX_PLANES 4
00049 #define CONTEXT_SIZE 32
00050
00051 #define MAX_QUANT_TABLES 8
00052 #define MAX_CONTEXT_INPUTS 5
00053
00054 extern const uint8_t ff_log2_run[41];
00055
00056 static const int8_t quant5_10bit[256]={
00057 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00061 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00062 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00063 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00064 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00065 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00066 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00067 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00068 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00069 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,
00070 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00071 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00072 -1,-1,-1,-1,-1,-1,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,
00073 };
00074
00075 static const int8_t quant5[256]={
00076 0, 1, 1, 1, 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, 2, 2, 2,
00081 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00082 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00083 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00084 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00085 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00086 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00087 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00088 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00089 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00090 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00091 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
00092 };
00093
00094 static const int8_t quant9_10bit[256]={
00095 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
00096 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 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, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
00099 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00100 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00101 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00102 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00103 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-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,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00106 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00107 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,
00108 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00109 -3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00110 -2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-1,-1,-0,-0,-0,-0,
00111 };
00112
00113 static const int8_t quant11[256]={
00114 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
00115 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00116 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00117 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00118 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00119 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00120 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00121 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00122 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00123 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00124 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00125 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00126 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00127 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
00128 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00129 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
00130 };
00131
00132 static const uint8_t ver2_state[256]= {
00133 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
00134 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
00135 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
00136 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
00137 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
00138 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
00139 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
00140 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
00141 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
00142 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
00143 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
00144 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
00145 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
00146 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
00147 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
00148 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
00149 };
00150
00151 typedef struct VlcState{
00152 int16_t drift;
00153 uint16_t error_sum;
00154 int8_t bias;
00155 uint8_t count;
00156 } VlcState;
00157
00158 typedef struct PlaneContext{
00159 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00160 int quant_table_index;
00161 int context_count;
00162 uint8_t (*state)[CONTEXT_SIZE];
00163 VlcState *vlc_state;
00164 uint8_t interlace_bit_state[2];
00165 } PlaneContext;
00166
00167 #define MAX_SLICES 256
00168
00169 typedef struct FFV1Context{
00170 AVClass *class;
00171 AVCodecContext *avctx;
00172 RangeCoder c;
00173 GetBitContext gb;
00174 PutBitContext pb;
00175 uint64_t rc_stat[256][2];
00176 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
00177 int version;
00178 int minor_version;
00179 int width, height;
00180 int chroma_h_shift, chroma_v_shift;
00181 int chroma_planes;
00182 int transparency;
00183 int flags;
00184 int picture_number;
00185 AVFrame picture;
00186 AVFrame last_picture;
00187 int plane_count;
00188 int ac;
00189 int ac_byte_count;
00190 PlaneContext plane[MAX_PLANES];
00191 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00192 int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
00193 int context_count[MAX_QUANT_TABLES];
00194 uint8_t state_transition[256];
00195 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
00196 int run_index;
00197 int colorspace;
00198 int16_t *sample_buffer;
00199 int gob_count;
00200 int packed_at_lsb;
00201 int ec;
00202 int slice_damaged;
00203 int key_frame_ok;
00204
00205 int quant_table_count;
00206
00207 DSPContext dsp;
00208
00209 struct FFV1Context *slice_context[MAX_SLICES];
00210 int slice_count;
00211 int num_v_slices;
00212 int num_h_slices;
00213 int slice_width;
00214 int slice_height;
00215 int slice_x;
00216 int slice_y;
00217 int bits_per_raw_sample;
00218 }FFV1Context;
00219
00220 static av_always_inline int fold(int diff, int bits){
00221 if(bits==8)
00222 diff= (int8_t)diff;
00223 else{
00224 diff+= 1<<(bits-1);
00225 diff&=(1<<bits)-1;
00226 diff-= 1<<(bits-1);
00227 }
00228
00229 return diff;
00230 }
00231
00232 static inline int predict(int16_t *src, int16_t *last)
00233 {
00234 const int LT= last[-1];
00235 const int T= last[ 0];
00236 const int L = src[-1];
00237
00238 return mid_pred(L, L + T - LT, T);
00239 }
00240
00241 static inline int get_context(PlaneContext *p, int16_t *src,
00242 int16_t *last, int16_t *last2)
00243 {
00244 const int LT= last[-1];
00245 const int T= last[ 0];
00246 const int RT= last[ 1];
00247 const int L = src[-1];
00248
00249 if(p->quant_table[3][127]){
00250 const int TT= last2[0];
00251 const int LL= src[-2];
00252 return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
00253 +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
00254 }else
00255 return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
00256 }
00257
00258 static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256]){
00259 int i,j,k,m;
00260 double l2tab[256];
00261
00262 for(i=1; i<256; i++)
00263 l2tab[i]= log2(i/256.0);
00264
00265 for(i=0; i<256; i++){
00266 double best_len[256];
00267 double p= i/256.0;
00268
00269 for(j=0; j<256; j++)
00270 best_len[j]= 1<<30;
00271
00272 for(j=FFMAX(i-10,1); j<FFMIN(i+11,256); j++){
00273 double occ[256]={0};
00274 double len=0;
00275 occ[j]=1.0;
00276 for(k=0; k<256; k++){
00277 double newocc[256]={0};
00278 for(m=0; m<256; m++){
00279 if(occ[m]){
00280 len -=occ[m]*( p *l2tab[ m]
00281 + (1-p)*l2tab[256-m]);
00282 }
00283 }
00284 if(len < best_len[k]){
00285 best_len[k]= len;
00286 best_state[i][k]= j;
00287 }
00288 for(m=0; m<256; m++){
00289 if(occ[m]){
00290 newocc[ one_state[ m]] += occ[m]* p ;
00291 newocc[256-one_state[256-m]] += occ[m]*(1-p);
00292 }
00293 }
00294 memcpy(occ, newocc, sizeof(occ));
00295 }
00296 }
00297 }
00298 }
00299
00300 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]){
00301 int i;
00302
00303 #define put_rac(C,S,B) \
00304 do{\
00305 if(rc_stat){\
00306 rc_stat[*(S)][B]++;\
00307 rc_stat2[(S)-state][B]++;\
00308 }\
00309 put_rac(C,S,B);\
00310 }while(0)
00311
00312 if(v){
00313 const int a= FFABS(v);
00314 const int e= av_log2(a);
00315 put_rac(c, state+0, 0);
00316 if(e<=9){
00317 for(i=0; i<e; i++){
00318 put_rac(c, state+1+i, 1);
00319 }
00320 put_rac(c, state+1+i, 0);
00321
00322 for(i=e-1; i>=0; i--){
00323 put_rac(c, state+22+i, (a>>i)&1);
00324 }
00325
00326 if(is_signed)
00327 put_rac(c, state+11 + e, v < 0);
00328 }else{
00329 for(i=0; i<e; i++){
00330 put_rac(c, state+1+FFMIN(i,9), 1);
00331 }
00332 put_rac(c, state+1+9, 0);
00333
00334 for(i=e-1; i>=0; i--){
00335 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1);
00336 }
00337
00338 if(is_signed)
00339 put_rac(c, state+11 + 10, v < 0);
00340 }
00341 }else{
00342 put_rac(c, state+0, 1);
00343 }
00344 #undef put_rac
00345 }
00346
00347 static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00348 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
00349 }
00350
00351 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
00352 if(get_rac(c, state+0))
00353 return 0;
00354 else{
00355 int i, e, a;
00356 e= 0;
00357 while(get_rac(c, state+1 + FFMIN(e,9))){
00358 e++;
00359 }
00360
00361 a= 1;
00362 for(i=e-1; i>=0; i--){
00363 a += a + get_rac(c, state+22 + FFMIN(i,9));
00364 }
00365
00366 e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10)));
00367 return (a^e)-e;
00368 }
00369 }
00370
00371 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00372 return get_symbol_inline(c, state, is_signed);
00373 }
00374
00375 static inline void update_vlc_state(VlcState * const state, const int v){
00376 int drift= state->drift;
00377 int count= state->count;
00378 state->error_sum += FFABS(v);
00379 drift += v;
00380
00381 if(count == 128){
00382 count >>= 1;
00383 drift >>= 1;
00384 state->error_sum >>= 1;
00385 }
00386 count++;
00387
00388 if(drift <= -count){
00389 if(state->bias > -128) state->bias--;
00390
00391 drift += count;
00392 if(drift <= -count)
00393 drift= -count + 1;
00394 }else if(drift > 0){
00395 if(state->bias < 127) state->bias++;
00396
00397 drift -= count;
00398 if(drift > 0)
00399 drift= 0;
00400 }
00401
00402 state->drift= drift;
00403 state->count= count;
00404 }
00405
00406 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00407 int i, k, code;
00408
00409 v = fold(v - state->bias, bits);
00410
00411 i= state->count;
00412 k=0;
00413 while(i < state->error_sum){
00414 k++;
00415 i += i;
00416 }
00417
00418 assert(k<=8);
00419
00420 #if 0 // JPEG LS
00421 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00422 else code= v;
00423 #else
00424 code= v ^ ((2*state->drift + state->count)>>31);
00425 #endif
00426
00427
00428 set_sr_golomb(pb, code, k, 12, bits);
00429
00430 update_vlc_state(state, v);
00431 }
00432
00433 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00434 int k, i, v, ret;
00435
00436 i= state->count;
00437 k=0;
00438 while(i < state->error_sum){
00439 k++;
00440 i += i;
00441 }
00442
00443 assert(k<=8);
00444
00445 v= get_sr_golomb(gb, k, 12, bits);
00446
00447
00448 #if 0 // JPEG LS
00449 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00450 #else
00451 v ^= ((2*state->drift + state->count)>>31);
00452 #endif
00453
00454 ret= fold(v + state->bias, bits);
00455
00456 update_vlc_state(state, v);
00457
00458 return ret;
00459 }
00460
00461 #if CONFIG_FFV1_ENCODER
00462 static av_always_inline int encode_line(FFV1Context *s, int w,
00463 int16_t *sample[3],
00464 int plane_index, int bits)
00465 {
00466 PlaneContext * const p= &s->plane[plane_index];
00467 RangeCoder * const c= &s->c;
00468 int x;
00469 int run_index= s->run_index;
00470 int run_count=0;
00471 int run_mode=0;
00472
00473 if(s->ac){
00474 if(c->bytestream_end - c->bytestream < w*20){
00475 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00476 return -1;
00477 }
00478 }else{
00479 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00480 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00481 return -1;
00482 }
00483 }
00484
00485 for(x=0; x<w; x++){
00486 int diff, context;
00487
00488 context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
00489 diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00490
00491 if(context < 0){
00492 context = -context;
00493 diff= -diff;
00494 }
00495
00496 diff= fold(diff, bits);
00497
00498 if(s->ac){
00499 if(s->flags & CODEC_FLAG_PASS1){
00500 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat, s->rc_stat2[p->quant_table_index][context]);
00501 }else{
00502 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
00503 }
00504 }else{
00505 if(context == 0) run_mode=1;
00506
00507 if(run_mode){
00508
00509 if(diff){
00510 while(run_count >= 1<<ff_log2_run[run_index]){
00511 run_count -= 1<<ff_log2_run[run_index];
00512 run_index++;
00513 put_bits(&s->pb, 1, 1);
00514 }
00515
00516 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
00517 if(run_index) run_index--;
00518 run_count=0;
00519 run_mode=0;
00520 if(diff>0) diff--;
00521 }else{
00522 run_count++;
00523 }
00524 }
00525
00526
00527
00528 if(run_mode == 0)
00529 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00530 }
00531 }
00532 if(run_mode){
00533 while(run_count >= 1<<ff_log2_run[run_index]){
00534 run_count -= 1<<ff_log2_run[run_index];
00535 run_index++;
00536 put_bits(&s->pb, 1, 1);
00537 }
00538
00539 if(run_count)
00540 put_bits(&s->pb, 1, 1);
00541 }
00542 s->run_index= run_index;
00543
00544 return 0;
00545 }
00546
00547 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00548 int x,y,i;
00549 const int ring_size= s->avctx->context_model ? 3 : 2;
00550 int16_t *sample[3];
00551 s->run_index=0;
00552
00553 memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
00554
00555 for(y=0; y<h; y++){
00556 for(i=0; i<ring_size; i++)
00557 sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
00558
00559 sample[0][-1]= sample[1][0 ];
00560 sample[1][ w]= sample[1][w-1];
00561
00562 if(s->bits_per_raw_sample<=8){
00563 for(x=0; x<w; x++){
00564 sample[0][x]= src[x + stride*y];
00565 }
00566 encode_line(s, w, sample, plane_index, 8);
00567 }else{
00568 if(s->packed_at_lsb){
00569 for(x=0; x<w; x++){
00570 sample[0][x]= ((uint16_t*)(src + stride*y))[x];
00571 }
00572 }else{
00573 for(x=0; x<w; x++){
00574 sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->bits_per_raw_sample);
00575 }
00576 }
00577 encode_line(s, w, sample, plane_index, s->bits_per_raw_sample);
00578 }
00579
00580 }
00581 }
00582
00583 static void encode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3]){
00584 int x, y, p, i;
00585 const int ring_size= s->avctx->context_model ? 3 : 2;
00586 int16_t *sample[4][3];
00587 int lbd= s->avctx->bits_per_raw_sample <= 8;
00588 int bits= s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
00589 int offset= 1 << bits;
00590 s->run_index=0;
00591
00592 memset(s->sample_buffer, 0, ring_size*4*(w+6)*sizeof(*s->sample_buffer));
00593
00594 for(y=0; y<h; y++){
00595 for(i=0; i<ring_size; i++)
00596 for(p=0; p<4; p++)
00597 sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
00598
00599 for(x=0; x<w; x++){
00600 int b,g,r,av_uninit(a);
00601 if(lbd){
00602 unsigned v= *((uint32_t*)(src[0] + x*4 + stride[0]*y));
00603 b= v&0xFF;
00604 g= (v>>8)&0xFF;
00605 r= (v>>16)&0xFF;
00606 a= v>>24;
00607 }else{
00608 b= *((uint16_t*)(src[0] + x*2 + stride[0]*y));
00609 g= *((uint16_t*)(src[1] + x*2 + stride[1]*y));
00610 r= *((uint16_t*)(src[2] + x*2 + stride[2]*y));
00611 }
00612
00613 b -= g;
00614 r -= g;
00615 g += (b + r)>>2;
00616 b += offset;
00617 r += offset;
00618
00619
00620
00621 sample[0][0][x]= g;
00622 sample[1][0][x]= b;
00623 sample[2][0][x]= r;
00624 sample[3][0][x]= a;
00625 }
00626 for(p=0; p<3 + s->transparency; p++){
00627 sample[p][0][-1]= sample[p][1][0 ];
00628 sample[p][1][ w]= sample[p][1][w-1];
00629 if (lbd)
00630 encode_line(s, w, sample[p], (p+1)/2, 9);
00631 else
00632 encode_line(s, w, sample[p], (p+1)/2, bits+1);
00633 }
00634 }
00635 }
00636
00637 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00638 int last=0;
00639 int i;
00640 uint8_t state[CONTEXT_SIZE];
00641 memset(state, 128, sizeof(state));
00642
00643 for(i=1; i<128 ; i++){
00644 if(quant_table[i] != quant_table[i-1]){
00645 put_symbol(c, state, i-last-1, 0);
00646 last= i;
00647 }
00648 }
00649 put_symbol(c, state, i-last-1, 0);
00650 }
00651
00652 static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
00653 int i;
00654 for(i=0; i<5; i++)
00655 write_quant_table(c, quant_table[i]);
00656 }
00657
00658 static void write_header(FFV1Context *f){
00659 uint8_t state[CONTEXT_SIZE];
00660 int i, j;
00661 RangeCoder * const c= &f->slice_context[0]->c;
00662
00663 memset(state, 128, sizeof(state));
00664
00665 if(f->version < 2){
00666 put_symbol(c, state, f->version, 0);
00667 put_symbol(c, state, f->ac, 0);
00668 if(f->ac>1){
00669 for(i=1; i<256; i++){
00670 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00671 }
00672 }
00673 put_symbol(c, state, f->colorspace, 0);
00674 if(f->version>0)
00675 put_symbol(c, state, f->bits_per_raw_sample, 0);
00676 put_rac(c, state, f->chroma_planes);
00677 put_symbol(c, state, f->chroma_h_shift, 0);
00678 put_symbol(c, state, f->chroma_v_shift, 0);
00679 put_rac(c, state, f->transparency);
00680
00681 write_quant_tables(c, f->quant_table);
00682 }else if(f->version < 3){
00683 put_symbol(c, state, f->slice_count, 0);
00684 for(i=0; i<f->slice_count; i++){
00685 FFV1Context *fs= f->slice_context[i];
00686 put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
00687 put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
00688 put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
00689 put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
00690 for(j=0; j<f->plane_count; j++){
00691 put_symbol(c, state, f->plane[j].quant_table_index, 0);
00692 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
00693 }
00694 }
00695 }
00696 }
00697 #endif
00698
00699 static av_cold int common_init(AVCodecContext *avctx){
00700 FFV1Context *s = avctx->priv_data;
00701
00702 s->avctx= avctx;
00703 s->flags= avctx->flags;
00704
00705 avcodec_get_frame_defaults(&s->picture);
00706
00707 ff_dsputil_init(&s->dsp, avctx);
00708
00709 s->width = avctx->width;
00710 s->height= avctx->height;
00711
00712 assert(s->width && s->height);
00713
00714 s->num_h_slices=1;
00715 s->num_v_slices=1;
00716
00717
00718 return 0;
00719 }
00720
00721 static int init_slice_state(FFV1Context *f, FFV1Context *fs){
00722 int j;
00723
00724 fs->plane_count= f->plane_count;
00725 fs->transparency= f->transparency;
00726 for(j=0; j<f->plane_count; j++){
00727 PlaneContext * const p= &fs->plane[j];
00728
00729 if(fs->ac){
00730 if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00731 if(!p-> state)
00732 return AVERROR(ENOMEM);
00733 }else{
00734 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00735 if(!p->vlc_state)
00736 return AVERROR(ENOMEM);
00737 }
00738 }
00739
00740 if (fs->ac>1){
00741
00742 for(j=1; j<256; j++){
00743 fs->c.one_state [ j]= f->state_transition[j];
00744 fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
00745 }
00746 }
00747
00748 return 0;
00749 }
00750
00751 static int init_slices_state(FFV1Context *f){
00752 int i;
00753 for(i=0; i<f->slice_count; i++){
00754 FFV1Context *fs= f->slice_context[i];
00755 if(init_slice_state(f, fs) < 0)
00756 return -1;
00757 }
00758 return 0;
00759 }
00760
00761 static av_cold int init_slice_contexts(FFV1Context *f){
00762 int i;
00763
00764 f->slice_count= f->num_h_slices * f->num_v_slices;
00765
00766 for(i=0; i<f->slice_count; i++){
00767 FFV1Context *fs= av_mallocz(sizeof(*fs));
00768 int sx= i % f->num_h_slices;
00769 int sy= i / f->num_h_slices;
00770 int sxs= f->avctx->width * sx / f->num_h_slices;
00771 int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
00772 int sys= f->avctx->height* sy / f->num_v_slices;
00773 int sye= f->avctx->height*(sy+1) / f->num_v_slices;
00774 f->slice_context[i]= fs;
00775 memcpy(fs, f, sizeof(*fs));
00776 memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
00777
00778 fs->slice_width = sxe - sxs;
00779 fs->slice_height= sye - sys;
00780 fs->slice_x = sxs;
00781 fs->slice_y = sys;
00782
00783 fs->sample_buffer = av_malloc(3*4 * (fs->width+6) * sizeof(*fs->sample_buffer));
00784 if (!fs->sample_buffer)
00785 return AVERROR(ENOMEM);
00786 }
00787 return 0;
00788 }
00789
00790 static int allocate_initial_states(FFV1Context *f){
00791 int i;
00792
00793 for(i=0; i<f->quant_table_count; i++){
00794 f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
00795 if(!f->initial_states[i])
00796 return AVERROR(ENOMEM);
00797 memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
00798 }
00799 return 0;
00800 }
00801
00802 #if CONFIG_FFV1_ENCODER
00803 static int write_extra_header(FFV1Context *f){
00804 RangeCoder * const c= &f->c;
00805 uint8_t state[CONTEXT_SIZE];
00806 int i, j, k;
00807 uint8_t state2[32][CONTEXT_SIZE];
00808 unsigned v;
00809
00810 memset(state2, 128, sizeof(state2));
00811 memset(state, 128, sizeof(state));
00812
00813 f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
00814 ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
00815 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00816
00817 put_symbol(c, state, f->version, 0);
00818 if(f->version > 2) {
00819 if(f->version == 3)
00820 f->minor_version = 2;
00821 put_symbol(c, state, f->minor_version, 0);
00822 }
00823 put_symbol(c, state, f->ac, 0);
00824 if(f->ac>1){
00825 for(i=1; i<256; i++){
00826 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00827 }
00828 }
00829 put_symbol(c, state, f->colorspace, 0);
00830 put_symbol(c, state, f->bits_per_raw_sample, 0);
00831 put_rac(c, state, f->chroma_planes);
00832 put_symbol(c, state, f->chroma_h_shift, 0);
00833 put_symbol(c, state, f->chroma_v_shift, 0);
00834 put_rac(c, state, f->transparency);
00835 put_symbol(c, state, f->num_h_slices-1, 0);
00836 put_symbol(c, state, f->num_v_slices-1, 0);
00837
00838 put_symbol(c, state, f->quant_table_count, 0);
00839 for(i=0; i<f->quant_table_count; i++)
00840 write_quant_tables(c, f->quant_tables[i]);
00841
00842 for(i=0; i<f->quant_table_count; i++){
00843 for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
00844 if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
00845 break;
00846 if(j<f->context_count[i]*CONTEXT_SIZE){
00847 put_rac(c, state, 1);
00848 for(j=0; j<f->context_count[i]; j++){
00849 for(k=0; k<CONTEXT_SIZE; k++){
00850 int pred= j ? f->initial_states[i][j-1][k] : 128;
00851 put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
00852 }
00853 }
00854 }else{
00855 put_rac(c, state, 0);
00856 }
00857 }
00858
00859 if(f->version > 2){
00860 put_symbol(c, state, f->ec, 0);
00861 }
00862
00863 f->avctx->extradata_size= ff_rac_terminate(c);
00864 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
00865 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
00866 f->avctx->extradata_size += 4;
00867
00868 return 0;
00869 }
00870
00871 static int sort_stt(FFV1Context *s, uint8_t stt[256]){
00872 int i,i2,changed,print=0;
00873
00874 do{
00875 changed=0;
00876 for(i=12; i<244; i++){
00877 for(i2=i+1; i2<245 && i2<i+4; i2++){
00878 #define COST(old, new) \
00879 s->rc_stat[old][0]*-log2((256-(new))/256.0)\
00880 +s->rc_stat[old][1]*-log2( (new) /256.0)
00881
00882 #define COST2(old, new) \
00883 COST(old, new)\
00884 +COST(256-(old), 256-(new))
00885
00886 double size0= COST2(i, i ) + COST2(i2, i2);
00887 double sizeX= COST2(i, i2) + COST2(i2, i );
00888 if(sizeX < size0 && i!=128 && i2!=128){
00889 int j;
00890 FFSWAP(int, stt[ i], stt[ i2]);
00891 FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
00892 FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
00893 if(i != 256-i2){
00894 FFSWAP(int, stt[256-i], stt[256-i2]);
00895 FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
00896 FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
00897 }
00898 for(j=1; j<256; j++){
00899 if (stt[j] == i ) stt[j] = i2;
00900 else if(stt[j] == i2) stt[j] = i ;
00901 if(i != 256-i2){
00902 if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
00903 else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
00904 }
00905 }
00906 print=changed=1;
00907 }
00908 }
00909 }
00910 }while(changed);
00911 return print;
00912 }
00913
00914 static av_cold int encode_init(AVCodecContext *avctx)
00915 {
00916 FFV1Context *s = avctx->priv_data;
00917 int i, j, k, m;
00918
00919 common_init(avctx);
00920
00921 s->version=0;
00922
00923 if((avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)) || avctx->slices>1)
00924 s->version = FFMAX(s->version, 2);
00925
00926 if(avctx->level == 3){
00927 s->version = 3;
00928 }
00929
00930 if(s->ec < 0){
00931 s->ec = (s->version >= 3);
00932 }
00933
00934 if(s->version >= 2 && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
00935 av_log(avctx, AV_LOG_ERROR, "Version 2 needed for requested features but version 2 is experimental and not enabled\n");
00936 return AVERROR_INVALIDDATA;
00937 }
00938
00939 s->ac= avctx->coder_type > 0 ? 2 : 0;
00940
00941 s->plane_count=3;
00942 switch(avctx->pix_fmt){
00943 case PIX_FMT_YUV444P9:
00944 case PIX_FMT_YUV422P9:
00945 case PIX_FMT_YUV420P9:
00946 if (!avctx->bits_per_raw_sample)
00947 s->bits_per_raw_sample = 9;
00948 case PIX_FMT_YUV444P10:
00949 case PIX_FMT_YUV420P10:
00950 case PIX_FMT_YUV422P10:
00951 s->packed_at_lsb = 1;
00952 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
00953 s->bits_per_raw_sample = 10;
00954 case PIX_FMT_GRAY16:
00955 case PIX_FMT_YUV444P16:
00956 case PIX_FMT_YUV422P16:
00957 case PIX_FMT_YUV420P16:
00958 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
00959 s->bits_per_raw_sample = 16;
00960 } else if (!s->bits_per_raw_sample){
00961 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
00962 }
00963 if(s->bits_per_raw_sample <=8){
00964 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
00965 return AVERROR_INVALIDDATA;
00966 }
00967 if(!s->ac && avctx->coder_type == -1) {
00968 av_log(avctx, AV_LOG_INFO, "bits_per_raw_sample > 8, forcing coder 1\n");
00969 s->ac = 2;
00970 }
00971 if(!s->ac){
00972 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
00973 return AVERROR_INVALIDDATA;
00974 }
00975 s->version= FFMAX(s->version, 1);
00976 case PIX_FMT_GRAY8:
00977 case PIX_FMT_YUV444P:
00978 case PIX_FMT_YUV440P:
00979 case PIX_FMT_YUV422P:
00980 case PIX_FMT_YUV420P:
00981 case PIX_FMT_YUV411P:
00982 case PIX_FMT_YUV410P:
00983 s->chroma_planes= av_pix_fmt_descriptors[avctx->pix_fmt].nb_components < 3 ? 0 : 1;
00984 s->colorspace= 0;
00985 break;
00986 case PIX_FMT_YUVA444P:
00987 case PIX_FMT_YUVA422P:
00988 case PIX_FMT_YUVA420P:
00989 s->chroma_planes= 1;
00990 s->colorspace= 0;
00991 s->transparency= 1;
00992 break;
00993 case PIX_FMT_RGB32:
00994 s->colorspace= 1;
00995 s->transparency= 1;
00996 break;
00997 case PIX_FMT_0RGB32:
00998 s->colorspace= 1;
00999 break;
01000 case PIX_FMT_GBRP9:
01001 if (!avctx->bits_per_raw_sample)
01002 s->bits_per_raw_sample = 9;
01003 case PIX_FMT_GBRP10:
01004 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
01005 s->bits_per_raw_sample = 10;
01006 case PIX_FMT_GBRP12:
01007 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
01008 s->bits_per_raw_sample = 12;
01009 case PIX_FMT_GBRP14:
01010 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
01011 s->bits_per_raw_sample = 14;
01012 else if (!s->bits_per_raw_sample)
01013 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
01014 s->colorspace= 1;
01015 s->chroma_planes= 1;
01016 s->version= FFMAX(s->version, 1);
01017 break;
01018 default:
01019 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
01020 return AVERROR_INVALIDDATA;
01021 }
01022 if (s->transparency) {
01023 av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
01024 }
01025 if (avctx->context_model > 1U) {
01026 av_log(avctx, AV_LOG_ERROR, "Invalid context model %d, valid values are 0 and 1\n", avctx->context_model);
01027 return AVERROR(EINVAL);
01028 }
01029
01030 if(s->ac>1)
01031 for(i=1; i<256; i++)
01032 s->state_transition[i]=ver2_state[i];
01033
01034 for(i=0; i<256; i++){
01035 s->quant_table_count=2;
01036 if(s->bits_per_raw_sample <=8){
01037 s->quant_tables[0][0][i]= quant11[i];
01038 s->quant_tables[0][1][i]= 11*quant11[i];
01039 s->quant_tables[0][2][i]= 11*11*quant11[i];
01040 s->quant_tables[1][0][i]= quant11[i];
01041 s->quant_tables[1][1][i]= 11*quant11[i];
01042 s->quant_tables[1][2][i]= 11*11*quant5 [i];
01043 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
01044 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
01045 }else{
01046 s->quant_tables[0][0][i]= quant9_10bit[i];
01047 s->quant_tables[0][1][i]= 11*quant9_10bit[i];
01048 s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
01049 s->quant_tables[1][0][i]= quant9_10bit[i];
01050 s->quant_tables[1][1][i]= 11*quant9_10bit[i];
01051 s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
01052 s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
01053 s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
01054 }
01055 }
01056 s->context_count[0]= (11*11*11+1)/2;
01057 s->context_count[1]= (11*11*5*5*5+1)/2;
01058 memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
01059
01060 for(i=0; i<s->plane_count; i++){
01061 PlaneContext * const p= &s->plane[i];
01062
01063 memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
01064 p->quant_table_index= avctx->context_model;
01065 p->context_count= s->context_count[p->quant_table_index];
01066 }
01067
01068 if(allocate_initial_states(s) < 0)
01069 return AVERROR(ENOMEM);
01070
01071 avctx->coded_frame= &s->picture;
01072 if(!s->transparency)
01073 s->plane_count= 2;
01074 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
01075
01076 s->picture_number=0;
01077
01078 if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
01079 for(i=0; i<s->quant_table_count; i++){
01080 s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
01081 if(!s->rc_stat2[i])
01082 return AVERROR(ENOMEM);
01083 }
01084 }
01085 if(avctx->stats_in){
01086 char *p= avctx->stats_in;
01087 uint8_t best_state[256][256];
01088 int gob_count=0;
01089 char *next;
01090
01091 av_assert0(s->version>=2);
01092
01093 for(;;){
01094 for(j=0; j<256; j++){
01095 for(i=0; i<2; i++){
01096 s->rc_stat[j][i]= strtol(p, &next, 0);
01097 if(next==p){
01098 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
01099 return -1;
01100 }
01101 p=next;
01102 }
01103 }
01104 for(i=0; i<s->quant_table_count; i++){
01105 for(j=0; j<s->context_count[i]; j++){
01106 for(k=0; k<32; k++){
01107 for(m=0; m<2; m++){
01108 s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
01109 if(next==p){
01110 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
01111 return AVERROR_INVALIDDATA;
01112 }
01113 p=next;
01114 }
01115 }
01116 }
01117 }
01118 gob_count= strtol(p, &next, 0);
01119 if(next==p || gob_count <0){
01120 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
01121 return AVERROR_INVALIDDATA;
01122 }
01123 p=next;
01124 while(*p=='\n' || *p==' ') p++;
01125 if(p[0]==0) break;
01126 }
01127 sort_stt(s, s->state_transition);
01128
01129 find_best_state(best_state, s->state_transition);
01130
01131 for(i=0; i<s->quant_table_count; i++){
01132 for(j=0; j<s->context_count[i]; j++){
01133 for(k=0; k<32; k++){
01134 double p= 128;
01135 if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
01136 p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
01137 }
01138 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)];
01139 }
01140 }
01141 }
01142 }
01143
01144 if(s->version>1){
01145 for(s->num_v_slices=2; s->num_v_slices<9; s->num_v_slices++){
01146 for(s->num_h_slices=s->num_v_slices; s->num_h_slices<2*s->num_v_slices; s->num_h_slices++){
01147 if(avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= 64 || !avctx->slices)
01148 goto slices_ok;
01149 }
01150 }
01151 av_log(avctx, AV_LOG_ERROR, "Unsupported number %d of slices requested, please specify a supported number with -slices (ex:4,6,9,12,16, ...)\n", avctx->slices);
01152 return -1;
01153 slices_ok:
01154 write_extra_header(s);
01155 }
01156
01157 if(init_slice_contexts(s) < 0)
01158 return -1;
01159 if(init_slices_state(s) < 0)
01160 return -1;
01161
01162 #define STATS_OUT_SIZE 1024*1024*6
01163 if(avctx->flags & CODEC_FLAG_PASS1){
01164 avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
01165 for(i=0; i<s->quant_table_count; i++){
01166 for(j=0; j<s->slice_count; j++){
01167 FFV1Context *sf= s->slice_context[j];
01168 av_assert0(!sf->rc_stat2[i]);
01169 sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
01170 if(!sf->rc_stat2[i])
01171 return AVERROR(ENOMEM);
01172 }
01173 }
01174 }
01175
01176 return 0;
01177 }
01178 #endif
01179
01180
01181 static void clear_slice_state(FFV1Context *f, FFV1Context *fs){
01182 int i, j;
01183
01184 for(i=0; i<f->plane_count; i++){
01185 PlaneContext *p= &fs->plane[i];
01186
01187 p->interlace_bit_state[0]= 128;
01188 p->interlace_bit_state[1]= 128;
01189
01190 if(fs->ac){
01191 if(f->initial_states[p->quant_table_index]){
01192 memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
01193 }else
01194 memset(p->state, 128, CONTEXT_SIZE*p->context_count);
01195 }else{
01196 for(j=0; j<p->context_count; j++){
01197 p->vlc_state[j].drift= 0;
01198 p->vlc_state[j].error_sum= 4;
01199 p->vlc_state[j].bias= 0;
01200 p->vlc_state[j].count= 1;
01201 }
01202 }
01203 }
01204 }
01205
01206 #if CONFIG_FFV1_ENCODER
01207
01208 static void encode_slice_header(FFV1Context *f, FFV1Context *fs){
01209 RangeCoder *c = &fs->c;
01210 uint8_t state[CONTEXT_SIZE];
01211 int j;
01212 memset(state, 128, sizeof(state));
01213
01214 put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
01215 put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
01216 put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
01217 put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
01218 for(j=0; j<f->plane_count; j++){
01219 put_symbol(c, state, f->plane[j].quant_table_index, 0);
01220 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
01221 }
01222 if(!f->picture.interlaced_frame) put_symbol(c, state, 3, 0);
01223 else put_symbol(c, state, 1 + !f->picture.top_field_first, 0);
01224 put_symbol(c, state, f->picture.sample_aspect_ratio.num, 0);
01225 put_symbol(c, state, f->picture.sample_aspect_ratio.den, 0);
01226 }
01227
01228 static int encode_slice(AVCodecContext *c, void *arg){
01229 FFV1Context *fs= *(void**)arg;
01230 FFV1Context *f= fs->avctx->priv_data;
01231 int width = fs->slice_width;
01232 int height= fs->slice_height;
01233 int x= fs->slice_x;
01234 int y= fs->slice_y;
01235 AVFrame * const p= &f->picture;
01236 const int ps= (f->bits_per_raw_sample>8)+1;
01237
01238 if(p->key_frame)
01239 clear_slice_state(f, fs);
01240 if(f->version > 2){
01241 encode_slice_header(f, fs);
01242 }
01243 if(!fs->ac){
01244 if(f->version > 2)
01245 put_rac(&fs->c, (int[]){129}, 0);
01246 fs->ac_byte_count = f->version > 2 || (!x&&!y) ? ff_rac_terminate(&fs->c) : 0;
01247 init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count, fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
01248 }
01249
01250 if(f->colorspace==0){
01251 const int chroma_width = -((-width )>>f->chroma_h_shift);
01252 const int chroma_height= -((-height)>>f->chroma_v_shift);
01253 const int cx= x>>f->chroma_h_shift;
01254 const int cy= y>>f->chroma_v_shift;
01255
01256 encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
01257
01258 if (f->chroma_planes){
01259 encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01260 encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
01261 }
01262 if (fs->transparency)
01263 encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
01264 }else{
01265 uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
01266 p->data[1] + ps*x + y*p->linesize[1],
01267 p->data[2] + ps*x + y*p->linesize[2]};
01268 encode_rgb_frame(fs, planes, width, height, p->linesize);
01269 }
01270 emms_c();
01271
01272 return 0;
01273 }
01274
01275 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
01276 const AVFrame *pict, int *got_packet)
01277 {
01278 FFV1Context *f = avctx->priv_data;
01279 RangeCoder * const c= &f->slice_context[0]->c;
01280 AVFrame * const p= &f->picture;
01281 int used_count= 0;
01282 uint8_t keystate=128;
01283 uint8_t *buf_p;
01284 int i, ret;
01285
01286 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*((8*2+1+1)*4)/8
01287 + FF_MIN_BUFFER_SIZE)) < 0)
01288 return ret;
01289
01290 ff_init_range_encoder(c, pkt->data, pkt->size);
01291 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01292
01293 *p = *pict;
01294 p->pict_type= AV_PICTURE_TYPE_I;
01295
01296 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
01297 put_rac(c, &keystate, 1);
01298 p->key_frame= 1;
01299 f->gob_count++;
01300 write_header(f);
01301 }else{
01302 put_rac(c, &keystate, 0);
01303 p->key_frame= 0;
01304 }
01305
01306 if (f->ac>1){
01307 int i;
01308 for(i=1; i<256; i++){
01309 c->one_state[i]= f->state_transition[i];
01310 c->zero_state[256-i]= 256-c->one_state[i];
01311 }
01312 }
01313
01314 for(i=1; i<f->slice_count; i++){
01315 FFV1Context *fs= f->slice_context[i];
01316 uint8_t *start = pkt->data + (pkt->size-used_count)*i/f->slice_count;
01317 int len = pkt->size/f->slice_count;
01318
01319 ff_init_range_encoder(&fs->c, start, len);
01320 }
01321 avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01322
01323 buf_p = pkt->data;
01324 for(i=0; i<f->slice_count; i++){
01325 FFV1Context *fs= f->slice_context[i];
01326 int bytes;
01327
01328 if(fs->ac){
01329 uint8_t state=129;
01330 put_rac(&fs->c, &state, 0);
01331 bytes= ff_rac_terminate(&fs->c);
01332 }else{
01333 flush_put_bits(&fs->pb);
01334 bytes= fs->ac_byte_count + (put_bits_count(&fs->pb)+7)/8;
01335 }
01336 if(i>0 || f->version>2){
01337 av_assert0(bytes < pkt->size/f->slice_count);
01338 memmove(buf_p, fs->c.bytestream_start, bytes);
01339 av_assert0(bytes < (1<<24));
01340 AV_WB24(buf_p+bytes, bytes);
01341 bytes+=3;
01342 }
01343 if(f->ec){
01344 unsigned v;
01345 buf_p[bytes++] = 0;
01346 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
01347 AV_WL32(buf_p + bytes, v); bytes += 4;
01348 }
01349 buf_p += bytes;
01350 }
01351
01352 if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
01353 int j, k, m;
01354 char *p= avctx->stats_out;
01355 char *end= p + STATS_OUT_SIZE;
01356
01357 memset(f->rc_stat, 0, sizeof(f->rc_stat));
01358 for(i=0; i<f->quant_table_count; i++)
01359 memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
01360
01361 for(j=0; j<f->slice_count; j++){
01362 FFV1Context *fs= f->slice_context[j];
01363 for(i=0; i<256; i++){
01364 f->rc_stat[i][0] += fs->rc_stat[i][0];
01365 f->rc_stat[i][1] += fs->rc_stat[i][1];
01366 }
01367 for(i=0; i<f->quant_table_count; i++){
01368 for(k=0; k<f->context_count[i]; k++){
01369 for(m=0; m<32; m++){
01370 f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
01371 f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
01372 }
01373 }
01374 }
01375 }
01376
01377 for(j=0; j<256; j++){
01378 snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
01379 p+= strlen(p);
01380 }
01381 snprintf(p, end-p, "\n");
01382
01383 for(i=0; i<f->quant_table_count; i++){
01384 for(j=0; j<f->context_count[i]; j++){
01385 for(m=0; m<32; m++){
01386 snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
01387 p+= strlen(p);
01388 }
01389 }
01390 }
01391 snprintf(p, end-p, "%d\n", f->gob_count);
01392 } else if(avctx->flags&CODEC_FLAG_PASS1)
01393 avctx->stats_out[0] = '\0';
01394
01395 f->picture_number++;
01396 pkt->size = buf_p - pkt->data;
01397 pkt->flags |= AV_PKT_FLAG_KEY*p->key_frame;
01398 *got_packet = 1;
01399
01400 return 0;
01401 }
01402 #endif
01403
01404 static av_cold int common_end(AVCodecContext *avctx){
01405 FFV1Context *s = avctx->priv_data;
01406 int i, j;
01407
01408 if (avctx->codec->decode && s->picture.data[0])
01409 avctx->release_buffer(avctx, &s->picture);
01410 if (avctx->codec->decode && s->last_picture.data[0])
01411 avctx->release_buffer(avctx, &s->last_picture);
01412
01413 for(j=0; j<s->slice_count; j++){
01414 FFV1Context *fs= s->slice_context[j];
01415 for(i=0; i<s->plane_count; i++){
01416 PlaneContext *p= &fs->plane[i];
01417
01418 av_freep(&p->state);
01419 av_freep(&p->vlc_state);
01420 }
01421 av_freep(&fs->sample_buffer);
01422 }
01423
01424 av_freep(&avctx->stats_out);
01425 for(j=0; j<s->quant_table_count; j++){
01426 av_freep(&s->initial_states[j]);
01427 for(i=0; i<s->slice_count; i++){
01428 FFV1Context *sf= s->slice_context[i];
01429 av_freep(&sf->rc_stat2[j]);
01430 }
01431 av_freep(&s->rc_stat2[j]);
01432 }
01433
01434 for(i=0; i<s->slice_count; i++){
01435 av_freep(&s->slice_context[i]);
01436 }
01437
01438 return 0;
01439 }
01440
01441 static av_always_inline void decode_line(FFV1Context *s, int w,
01442 int16_t *sample[2],
01443 int plane_index, int bits)
01444 {
01445 PlaneContext * const p= &s->plane[plane_index];
01446 RangeCoder * const c= &s->c;
01447 int x;
01448 int run_count=0;
01449 int run_mode=0;
01450 int run_index= s->run_index;
01451
01452 for(x=0; x<w; x++){
01453 int diff, context, sign;
01454
01455 context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
01456 if(context < 0){
01457 context= -context;
01458 sign=1;
01459 }else
01460 sign=0;
01461
01462 av_assert2(context < p->context_count);
01463
01464 if(s->ac){
01465 diff= get_symbol_inline(c, p->state[context], 1);
01466 }else{
01467 if(context == 0 && run_mode==0) run_mode=1;
01468
01469 if(run_mode){
01470 if(run_count==0 && run_mode==1){
01471 if(get_bits1(&s->gb)){
01472 run_count = 1<<ff_log2_run[run_index];
01473 if(x + run_count <= w) run_index++;
01474 }else{
01475 if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
01476 else run_count=0;
01477 if(run_index) run_index--;
01478 run_mode=2;
01479 }
01480 }
01481 run_count--;
01482 if(run_count < 0){
01483 run_mode=0;
01484 run_count=0;
01485 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01486 if(diff>=0) diff++;
01487 }else
01488 diff=0;
01489 }else
01490 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01491
01492
01493 }
01494
01495 if(sign) diff= -diff;
01496
01497 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
01498 }
01499 s->run_index= run_index;
01500 }
01501
01502 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
01503 int x, y;
01504 int16_t *sample[2];
01505 sample[0]=s->sample_buffer +3;
01506 sample[1]=s->sample_buffer+w+6+3;
01507
01508 s->run_index=0;
01509
01510 memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
01511
01512 for(y=0; y<h; y++){
01513 int16_t *temp = sample[0];
01514
01515 sample[0]= sample[1];
01516 sample[1]= temp;
01517
01518 sample[1][-1]= sample[0][0 ];
01519 sample[0][ w]= sample[0][w-1];
01520
01521
01522 if(s->avctx->bits_per_raw_sample <= 8){
01523 decode_line(s, w, sample, plane_index, 8);
01524 for(x=0; x<w; x++){
01525 src[x + stride*y]= sample[1][x];
01526 }
01527 }else{
01528 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
01529 if(s->packed_at_lsb){
01530 for(x=0; x<w; x++){
01531 ((uint16_t*)(src + stride*y))[x]= sample[1][x];
01532 }
01533 }else{
01534 for(x=0; x<w; x++){
01535 ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
01536 }
01537 }
01538 }
01539
01540 }
01541 }
01542
01543 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3]){
01544 int x, y, p;
01545 int16_t *sample[4][2];
01546 int lbd= s->avctx->bits_per_raw_sample <= 8;
01547 int bits= s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
01548 int offset= 1 << bits;
01549 for(x=0; x<4; x++){
01550 sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
01551 sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
01552 }
01553
01554 s->run_index=0;
01555
01556 memset(s->sample_buffer, 0, 8*(w+6)*sizeof(*s->sample_buffer));
01557
01558 for(y=0; y<h; y++){
01559 for(p=0; p<3 + s->transparency; p++){
01560 int16_t *temp = sample[p][0];
01561
01562 sample[p][0]= sample[p][1];
01563 sample[p][1]= temp;
01564
01565 sample[p][1][-1]= sample[p][0][0 ];
01566 sample[p][0][ w]= sample[p][0][w-1];
01567 if (lbd)
01568 decode_line(s, w, sample[p], (p+1)/2, 9);
01569 else
01570 decode_line(s, w, sample[p], (p+1)/2, bits+1);
01571 }
01572 for(x=0; x<w; x++){
01573 int g= sample[0][1][x];
01574 int b= sample[1][1][x];
01575 int r= sample[2][1][x];
01576 int a= sample[3][1][x];
01577
01578
01579
01580
01581 b -= offset;
01582 r -= offset;
01583 g -= (b + r)>>2;
01584 b += g;
01585 r += g;
01586
01587 if(lbd)
01588 *((uint32_t*)(src[0] + x*4 + stride[0]*y))= b + (g<<8) + (r<<16) + (a<<24);
01589 else{
01590 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
01591 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
01592 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
01593 }
01594 }
01595 }
01596 }
01597
01598 static int decode_slice_header(FFV1Context *f, FFV1Context *fs){
01599 RangeCoder *c = &fs->c;
01600 uint8_t state[CONTEXT_SIZE];
01601 unsigned ps, i, context_count;
01602 memset(state, 128, sizeof(state));
01603
01604 av_assert0(f->version > 2);
01605
01606 fs->slice_x = get_symbol(c, state, 0) *f->width ;
01607 fs->slice_y = get_symbol(c, state, 0) *f->height;
01608 fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
01609 fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
01610
01611 fs->slice_x /= f->num_h_slices;
01612 fs->slice_y /= f->num_v_slices;
01613 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
01614 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
01615 if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
01616 return -1;
01617 if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
01618 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
01619 return -1;
01620
01621 for(i=0; i<f->plane_count; i++){
01622 PlaneContext * const p= &fs->plane[i];
01623 int idx=get_symbol(c, state, 0);
01624 if(idx > (unsigned)f->quant_table_count){
01625 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
01626 return -1;
01627 }
01628 p->quant_table_index= idx;
01629 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
01630 context_count= f->context_count[idx];
01631
01632 if(p->context_count < context_count){
01633 av_freep(&p->state);
01634 av_freep(&p->vlc_state);
01635 }
01636 p->context_count= context_count;
01637 }
01638
01639 ps = get_symbol(c, state, 0);
01640 if(ps==1){
01641 f->picture.interlaced_frame = 1;
01642 f->picture.top_field_first = 1;
01643 } else if(ps==2){
01644 f->picture.interlaced_frame = 1;
01645 f->picture.top_field_first = 0;
01646 } else if(ps==3){
01647 f->picture.interlaced_frame = 0;
01648 }
01649 f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
01650 f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
01651
01652 return 0;
01653 }
01654
01655 static int decode_slice(AVCodecContext *c, void *arg){
01656 FFV1Context *fs= *(void**)arg;
01657 FFV1Context *f= fs->avctx->priv_data;
01658 int width, height, x, y;
01659 const int ps= (c->bits_per_raw_sample>8)+1;
01660 AVFrame * const p= &f->picture;
01661
01662 if(f->version > 2){
01663 if(init_slice_state(f, fs) < 0)
01664 return AVERROR(ENOMEM);
01665 if(decode_slice_header(f, fs) < 0) {
01666 fs->slice_damaged = 1;
01667 return AVERROR_INVALIDDATA;
01668 }
01669 }
01670 if(init_slice_state(f, fs) < 0)
01671 return AVERROR(ENOMEM);
01672 if(f->picture.key_frame)
01673 clear_slice_state(f, fs);
01674 width = fs->slice_width;
01675 height= fs->slice_height;
01676 x= fs->slice_x;
01677 y= fs->slice_y;
01678
01679 if(!fs->ac){
01680 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
01681 get_rac(&fs->c, (int[]){129});
01682 fs->ac_byte_count = f->version > 2 || (!x&&!y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
01683 init_get_bits(&fs->gb,
01684 fs->c.bytestream_start + fs->ac_byte_count,
01685 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
01686 }
01687
01688 av_assert1(width && height);
01689 if(f->colorspace==0){
01690 const int chroma_width = -((-width )>>f->chroma_h_shift);
01691 const int chroma_height= -((-height)>>f->chroma_v_shift);
01692 const int cx= x>>f->chroma_h_shift;
01693 const int cy= y>>f->chroma_v_shift;
01694 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
01695
01696 if (f->chroma_planes){
01697 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01698 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
01699 }
01700 if (fs->transparency)
01701 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
01702 }else{
01703 uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
01704 p->data[1] + ps*x + y*p->linesize[1],
01705 p->data[2] + ps*x + y*p->linesize[2]};
01706 decode_rgb_frame(fs, planes, width, height, p->linesize);
01707 }
01708 if(fs->ac && f->version > 2) {
01709 int v;
01710 get_rac(&fs->c, (int[]){129});
01711 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
01712 if(v) {
01713 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
01714 fs->slice_damaged = 1;
01715 }
01716 }
01717
01718 emms_c();
01719
01720 return 0;
01721 }
01722
01723 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
01724 int v;
01725 int i=0;
01726 uint8_t state[CONTEXT_SIZE];
01727
01728 memset(state, 128, sizeof(state));
01729
01730 for(v=0; i<128 ; v++){
01731 unsigned len= get_symbol(c, state, 0) + 1;
01732
01733 if(len > 128 - i) return -1;
01734
01735 while(len--){
01736 quant_table[i] = scale*v;
01737 i++;
01738
01739
01740 }
01741 }
01742
01743 for(i=1; i<128; i++){
01744 quant_table[256-i]= -quant_table[i];
01745 }
01746 quant_table[128]= -quant_table[127];
01747
01748 return 2*v - 1;
01749 }
01750
01751 static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
01752 int i;
01753 int context_count=1;
01754
01755 for(i=0; i<5; i++){
01756 context_count*= read_quant_table(c, quant_table[i], context_count);
01757 if(context_count > 32768U){
01758 return -1;
01759 }
01760 }
01761 return (context_count+1)/2;
01762 }
01763
01764 static int read_extra_header(FFV1Context *f){
01765 RangeCoder * const c= &f->c;
01766 uint8_t state[CONTEXT_SIZE];
01767 int i, j, k;
01768 uint8_t state2[32][CONTEXT_SIZE];
01769
01770 memset(state2, 128, sizeof(state2));
01771 memset(state, 128, sizeof(state));
01772
01773 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
01774 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01775
01776 f->version= get_symbol(c, state, 0);
01777 if(f->version > 2) {
01778 c->bytestream_end -= 4;
01779 f->minor_version= get_symbol(c, state, 0);
01780 }
01781 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01782 if(f->ac>1){
01783 for(i=1; i<256; i++){
01784 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01785 }
01786 }
01787 f->colorspace= get_symbol(c, state, 0);
01788 f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01789 f->chroma_planes= get_rac(c, state);
01790 f->chroma_h_shift= get_symbol(c, state, 0);
01791 f->chroma_v_shift= get_symbol(c, state, 0);
01792 f->transparency= get_rac(c, state);
01793 f->plane_count= 2 + f->transparency;
01794 f->num_h_slices= 1 + get_symbol(c, state, 0);
01795 f->num_v_slices= 1 + get_symbol(c, state, 0);
01796 if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
01797 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
01798 return -1;
01799 }
01800
01801 f->quant_table_count= get_symbol(c, state, 0);
01802 if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
01803 return -1;
01804 for(i=0; i<f->quant_table_count; i++){
01805 if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
01806 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01807 return -1;
01808 }
01809 }
01810
01811 if(allocate_initial_states(f) < 0)
01812 return AVERROR(ENOMEM);
01813
01814 for(i=0; i<f->quant_table_count; i++){
01815 if(get_rac(c, state)){
01816 for(j=0; j<f->context_count[i]; j++){
01817 for(k=0; k<CONTEXT_SIZE; k++){
01818 int pred= j ? f->initial_states[i][j-1][k] : 128;
01819 f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
01820 }
01821 }
01822 }
01823 }
01824
01825 if(f->version > 2){
01826 f->ec = get_symbol(c, state, 0);
01827 }
01828
01829 if(f->version > 2){
01830 unsigned v;
01831 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
01832 if(v){
01833 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
01834 return AVERROR_INVALIDDATA;
01835 }
01836 }
01837
01838 return 0;
01839 }
01840
01841 static int read_header(FFV1Context *f){
01842 uint8_t state[CONTEXT_SIZE];
01843 int i, j, context_count = -1;
01844 RangeCoder * const c= &f->slice_context[0]->c;
01845
01846 memset(state, 128, sizeof(state));
01847
01848 if(f->version < 2){
01849 unsigned v= get_symbol(c, state, 0);
01850 if(v >= 2){
01851 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
01852 return AVERROR_INVALIDDATA;
01853 }
01854 f->version = v;
01855 f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01856 if(f->ac>1){
01857 for(i=1; i<256; i++){
01858 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01859 }
01860 }
01861 f->colorspace= get_symbol(c, state, 0);
01862 if(f->version>0)
01863 f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01864 f->chroma_planes= get_rac(c, state);
01865 f->chroma_h_shift= get_symbol(c, state, 0);
01866 f->chroma_v_shift= get_symbol(c, state, 0);
01867 f->transparency= get_rac(c, state);
01868 f->plane_count= 2 + f->transparency;
01869 }
01870
01871 if(f->colorspace==0){
01872 if(!f->transparency && !f->chroma_planes){
01873 if (f->avctx->bits_per_raw_sample<=8)
01874 f->avctx->pix_fmt= PIX_FMT_GRAY8;
01875 else
01876 f->avctx->pix_fmt= PIX_FMT_GRAY16;
01877 }else if(f->avctx->bits_per_raw_sample<=8 && !f->transparency){
01878 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01879 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
01880 case 0x01: f->avctx->pix_fmt= PIX_FMT_YUV440P; break;
01881 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
01882 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
01883 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
01884 case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
01885 default:
01886 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01887 return -1;
01888 }
01889 }else if(f->avctx->bits_per_raw_sample<=8 && f->transparency){
01890 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01891 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUVA444P; break;
01892 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUVA422P; break;
01893 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUVA420P; break;
01894 default:
01895 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01896 return -1;
01897 }
01898 }else if(f->avctx->bits_per_raw_sample==9) {
01899 f->packed_at_lsb=1;
01900 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01901 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P9; break;
01902 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P9; break;
01903 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P9; break;
01904 default:
01905 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01906 return -1;
01907 }
01908 }else if(f->avctx->bits_per_raw_sample==10) {
01909 f->packed_at_lsb=1;
01910 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01911 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P10; break;
01912 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P10; break;
01913 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P10; break;
01914 default:
01915 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01916 return -1;
01917 }
01918 }else {
01919 switch(16*f->chroma_h_shift + f->chroma_v_shift){
01920 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
01921 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
01922 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
01923 default:
01924 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01925 return -1;
01926 }
01927 }
01928 }else if(f->colorspace==1){
01929 if(f->chroma_h_shift || f->chroma_v_shift){
01930 av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
01931 return -1;
01932 }
01933 if(f->avctx->bits_per_raw_sample==9)
01934 f->avctx->pix_fmt= PIX_FMT_GBRP9;
01935 else if(f->avctx->bits_per_raw_sample==10)
01936 f->avctx->pix_fmt= PIX_FMT_GBRP10;
01937 else if(f->avctx->bits_per_raw_sample==12)
01938 f->avctx->pix_fmt= PIX_FMT_GBRP12;
01939 else if(f->avctx->bits_per_raw_sample==14)
01940 f->avctx->pix_fmt= PIX_FMT_GBRP14;
01941 else
01942 if(f->transparency) f->avctx->pix_fmt= PIX_FMT_RGB32;
01943 else f->avctx->pix_fmt= PIX_FMT_0RGB32;
01944 }else{
01945 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
01946 return -1;
01947 }
01948
01949
01950 if(f->version < 2){
01951 context_count= read_quant_tables(c, f->quant_table);
01952 if(context_count < 0){
01953 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01954 return -1;
01955 }
01956 }else if(f->version < 3){
01957 f->slice_count= get_symbol(c, state, 0);
01958 }else{
01959 const uint8_t *p= c->bytestream_end;
01960 for(f->slice_count = 0; f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start; f->slice_count++){
01961 int trailer = 3 + 5*!!f->ec;
01962 int size = AV_RB24(p-trailer);
01963 if(size + trailer > p - c->bytestream_start)
01964 break;
01965 p -= size + trailer;
01966 }
01967 }
01968 if(f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0){
01969 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
01970 return -1;
01971 }
01972
01973 for(j=0; j<f->slice_count; j++){
01974 FFV1Context *fs= f->slice_context[j];
01975 fs->ac= f->ac;
01976 fs->packed_at_lsb= f->packed_at_lsb;
01977
01978 fs->slice_damaged = 0;
01979
01980 if(f->version == 2){
01981 fs->slice_x = get_symbol(c, state, 0) *f->width ;
01982 fs->slice_y = get_symbol(c, state, 0) *f->height;
01983 fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
01984 fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
01985
01986 fs->slice_x /= f->num_h_slices;
01987 fs->slice_y /= f->num_v_slices;
01988 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
01989 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
01990 if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
01991 return -1;
01992 if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
01993 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
01994 return -1;
01995 }
01996
01997 for(i=0; i<f->plane_count; i++){
01998 PlaneContext * const p= &fs->plane[i];
01999
02000 if(f->version == 2){
02001 int idx=get_symbol(c, state, 0);
02002 if(idx > (unsigned)f->quant_table_count){
02003 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
02004 return -1;
02005 }
02006 p->quant_table_index= idx;
02007 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
02008 context_count= f->context_count[idx];
02009 }else{
02010 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
02011 }
02012
02013 if(f->version <= 2){
02014 av_assert0(context_count>=0);
02015 if(p->context_count < context_count){
02016 av_freep(&p->state);
02017 av_freep(&p->vlc_state);
02018 }
02019 p->context_count= context_count;
02020 }
02021 }
02022 }
02023 return 0;
02024 }
02025
02026 static av_cold int decode_init(AVCodecContext *avctx)
02027 {
02028 FFV1Context *f = avctx->priv_data;
02029
02030 common_init(avctx);
02031
02032 if(avctx->extradata && read_extra_header(f) < 0)
02033 return -1;
02034
02035 if(init_slice_contexts(f) < 0)
02036 return -1;
02037
02038 return 0;
02039 }
02040
02041 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
02042 const uint8_t *buf = avpkt->data;
02043 int buf_size = avpkt->size;
02044 FFV1Context *f = avctx->priv_data;
02045 RangeCoder * const c= &f->slice_context[0]->c;
02046 AVFrame * const p= &f->picture;
02047 int i;
02048 uint8_t keystate= 128;
02049 const uint8_t *buf_p;
02050
02051 AVFrame *picture = data;
02052
02053
02054 if (p->data[0])
02055 avctx->release_buffer(avctx, p);
02056
02057 ff_init_range_decoder(c, buf, buf_size);
02058 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
02059
02060
02061 p->pict_type= AV_PICTURE_TYPE_I;
02062 if(get_rac(c, &keystate)){
02063 p->key_frame= 1;
02064 f->key_frame_ok = 0;
02065 if(read_header(f) < 0)
02066 return -1;
02067 f->key_frame_ok = 1;
02068 }else{
02069 if (!f->key_frame_ok) {
02070 av_log(avctx, AV_LOG_ERROR, "Cant decode non keyframe without valid keyframe\n");
02071 return AVERROR_INVALIDDATA;
02072 }
02073 p->key_frame= 0;
02074 }
02075
02076 p->reference= 3;
02077 if(avctx->get_buffer(avctx, p) < 0){
02078 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
02079 return -1;
02080 }
02081
02082 if(avctx->debug&FF_DEBUG_PICT_INFO)
02083 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d\n",
02084 f->version, p->key_frame, f->ac, f->ec, f->slice_count);
02085
02086 buf_p= buf + buf_size;
02087 for(i=f->slice_count-1; i>=0; i--){
02088 FFV1Context *fs= f->slice_context[i];
02089 int trailer = 3 + 5*!!f->ec;
02090 int v;
02091
02092 if(i || f->version>2) v = AV_RB24(buf_p-trailer)+trailer;
02093 else v = buf_p - c->bytestream_start;
02094 if(buf_p - c->bytestream_start < v){
02095 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
02096 return -1;
02097 }
02098 buf_p -= v;
02099
02100 if(f->ec){
02101 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
02102 if(crc){
02103 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
02104 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
02105 if(ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
02106 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n",ts*av_q2d(avctx->pkt_timebase));
02107 } else if(ts != AV_NOPTS_VALUE) {
02108 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
02109 } else {
02110 av_log(f->avctx, AV_LOG_ERROR, "\n");
02111 }
02112 fs->slice_damaged = 1;
02113 }
02114 }
02115
02116 if(i){
02117 ff_init_range_decoder(&fs->c, buf_p, v);
02118 }else
02119 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
02120 }
02121
02122 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
02123
02124 for(i=f->slice_count-1; i>=0; i--){
02125 FFV1Context *fs= f->slice_context[i];
02126 int j;
02127 if(fs->slice_damaged && f->last_picture.data[0]){
02128 uint8_t *dst[4], *src[4];
02129 for(j=0; j<4; j++){
02130 int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
02131 int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
02132 dst[j] = f->picture .data[j] + f->picture .linesize[j]*
02133 (fs->slice_y>>sv) + (fs->slice_x>>sh);
02134 src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
02135 (fs->slice_y>>sv) + (fs->slice_x>>sh);
02136 }
02137 av_image_copy(dst, f->picture.linesize, (const uint8_t **)src, f->last_picture.linesize,
02138 avctx->pix_fmt, fs->slice_width, fs->slice_height);
02139 }
02140 }
02141
02142 f->picture_number++;
02143
02144 *picture= *p;
02145 *data_size = sizeof(AVFrame);
02146
02147 FFSWAP(AVFrame, f->picture, f->last_picture);
02148
02149 return buf_size;
02150 }
02151
02152 AVCodec ff_ffv1_decoder = {
02153 .name = "ffv1",
02154 .type = AVMEDIA_TYPE_VIDEO,
02155 .id = AV_CODEC_ID_FFV1,
02156 .priv_data_size = sizeof(FFV1Context),
02157 .init = decode_init,
02158 .close = common_end,
02159 .decode = decode_frame,
02160 .capabilities = CODEC_CAP_DR1 |
02161 CODEC_CAP_SLICE_THREADS,
02162 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
02163 };
02164
02165 #if CONFIG_FFV1_ENCODER
02166
02167 #define OFFSET(x) offsetof(FFV1Context, x)
02168 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
02169 static const AVOption options[] = {
02170 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
02171 {NULL}
02172 };
02173
02174 static const AVClass class = {
02175 .class_name = "ffv1 encoder",
02176 .item_name = av_default_item_name,
02177 .option = options,
02178 .version = LIBAVUTIL_VERSION_INT,
02179 };
02180
02181 static const AVCodecDefault ffv1_defaults[] = {
02182 { "coder", "-1" },
02183 { NULL },
02184 };
02185
02186 AVCodec ff_ffv1_encoder = {
02187 .name = "ffv1",
02188 .type = AVMEDIA_TYPE_VIDEO,
02189 .id = AV_CODEC_ID_FFV1,
02190 .priv_data_size = sizeof(FFV1Context),
02191 .init = encode_init,
02192 .encode2 = encode_frame,
02193 .close = common_end,
02194 .capabilities = CODEC_CAP_SLICE_THREADS,
02195 .defaults = ffv1_defaults,
02196 .pix_fmts = (const enum PixelFormat[]){
02197 PIX_FMT_YUV420P, PIX_FMT_YUVA420P, PIX_FMT_YUVA422P, PIX_FMT_YUV444P,
02198 PIX_FMT_YUVA444P, PIX_FMT_YUV440P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
02199 PIX_FMT_YUV410P, PIX_FMT_0RGB32, PIX_FMT_RGB32, PIX_FMT_YUV420P16,
02200 PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_YUV444P9, PIX_FMT_YUV422P9,
02201 PIX_FMT_YUV420P9, PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_YUV444P10,
02202 PIX_FMT_GRAY16, PIX_FMT_GRAY8, PIX_FMT_GBRP9, PIX_FMT_GBRP10,
02203 PIX_FMT_GBRP12, PIX_FMT_GBRP14,
02204 PIX_FMT_NONE
02205 },
02206 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
02207 .priv_class = &class,
02208 };
02209 #endif