Libav
|
00001 /* 00002 * FFV1 codec for libavcodec 00003 * 00004 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 00036 #define MAX_PLANES 4 00037 #define CONTEXT_SIZE 32 00038 00039 extern const uint8_t ff_log2_run[32]; 00040 00041 static const int8_t quant3[256]={ 00042 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00043 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00044 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00045 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00046 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00047 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00048 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00049 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 00050 -1,-1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,-2,-2,-2,-2, 00095 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1, 00096 }; 00097 static const int8_t quant7[256]={ 00098 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 00099 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 00100 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 00101 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 00102 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 00103 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 00104 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 00105 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 00106 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, 00107 -3,-3,-3,-3,-3,-3,-3,-3,-3,-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,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, 00110 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, 00111 -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2, 00112 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, 00113 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1, 00114 }; 00115 static const int8_t quant9[256]={ 00116 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 00117 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,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6,-6,-6, 00183 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5, 00184 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, 00185 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, 00186 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1, 00187 }; 00188 00189 static const uint8_t ver2_state[256]= { 00190 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 00191 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 00192 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 00193 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69, 00194 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97, 00195 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98, 00196 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125, 00197 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129, 00198 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148, 00199 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160, 00200 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178, 00201 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196, 00202 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214, 00203 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225, 00204 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242, 00205 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255, 00206 }; 00207 00208 typedef struct VlcState{ 00209 int16_t drift; 00210 uint16_t error_sum; 00211 int8_t bias; 00212 uint8_t count; 00213 } VlcState; 00214 00215 typedef struct PlaneContext{ 00216 int context_count; 00217 uint8_t (*state)[CONTEXT_SIZE]; 00218 VlcState *vlc_state; 00219 uint8_t interlace_bit_state[2]; 00220 } PlaneContext; 00221 00222 typedef struct FFV1Context{ 00223 AVCodecContext *avctx; 00224 RangeCoder c; 00225 GetBitContext gb; 00226 PutBitContext pb; 00227 int version; 00228 int width, height; 00229 int chroma_h_shift, chroma_v_shift; 00230 int flags; 00231 int picture_number; 00232 AVFrame picture; 00233 int plane_count; 00234 int ac; 00235 PlaneContext plane[MAX_PLANES]; 00236 int16_t quant_table[5][256]; 00237 uint8_t state_transition[256]; 00238 int run_index; 00239 int colorspace; 00240 00241 DSPContext dsp; 00242 }FFV1Context; 00243 00244 static av_always_inline int fold(int diff, int bits){ 00245 if(bits==8) 00246 diff= (int8_t)diff; 00247 else{ 00248 diff+= 1<<(bits-1); 00249 diff&=(1<<bits)-1; 00250 diff-= 1<<(bits-1); 00251 } 00252 00253 return diff; 00254 } 00255 00256 static inline int predict(int_fast16_t *src, int_fast16_t *last){ 00257 const int LT= last[-1]; 00258 const int T= last[ 0]; 00259 const int L = src[-1]; 00260 00261 return mid_pred(L, L + T - LT, T); 00262 } 00263 00264 static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){ 00265 const int LT= last[-1]; 00266 const int T= last[ 0]; 00267 const int RT= last[ 1]; 00268 const int L = src[-1]; 00269 00270 if(f->quant_table[3][127]){ 00271 const int TT= last2[0]; 00272 const int LL= src[-2]; 00273 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF] 00274 +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF]; 00275 }else 00276 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]; 00277 } 00278 00279 static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed){ 00280 int i; 00281 00282 if(v){ 00283 const int a= FFABS(v); 00284 const int e= av_log2(a); 00285 put_rac(c, state+0, 0); 00286 if(e<=9){ 00287 for(i=0; i<e; i++){ 00288 put_rac(c, state+1+i, 1); //1..10 00289 } 00290 put_rac(c, state+1+i, 0); 00291 00292 for(i=e-1; i>=0; i--){ 00293 put_rac(c, state+22+i, (a>>i)&1); //22..31 00294 } 00295 00296 if(is_signed) 00297 put_rac(c, state+11 + e, v < 0); //11..21 00298 }else{ 00299 for(i=0; i<e; i++){ 00300 put_rac(c, state+1+FFMIN(i,9), 1); //1..10 00301 } 00302 put_rac(c, state+1+9, 0); 00303 00304 for(i=e-1; i>=0; i--){ 00305 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31 00306 } 00307 00308 if(is_signed) 00309 put_rac(c, state+11 + 10, v < 0); //11..21 00310 } 00311 }else{ 00312 put_rac(c, state+0, 1); 00313 } 00314 } 00315 00316 static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){ 00317 put_symbol_inline(c, state, v, is_signed); 00318 } 00319 00320 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){ 00321 if(get_rac(c, state+0)) 00322 return 0; 00323 else{ 00324 int i, e, a; 00325 e= 0; 00326 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10 00327 e++; 00328 } 00329 00330 a= 1; 00331 for(i=e-1; i>=0; i--){ 00332 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31 00333 } 00334 00335 e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21 00336 return (a^e)-e; 00337 } 00338 } 00339 00340 static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){ 00341 return get_symbol_inline(c, state, is_signed); 00342 } 00343 00344 static inline void update_vlc_state(VlcState * const state, const int v){ 00345 int drift= state->drift; 00346 int count= state->count; 00347 state->error_sum += FFABS(v); 00348 drift += v; 00349 00350 if(count == 128){ //FIXME variable 00351 count >>= 1; 00352 drift >>= 1; 00353 state->error_sum >>= 1; 00354 } 00355 count++; 00356 00357 if(drift <= -count){ 00358 if(state->bias > -128) state->bias--; 00359 00360 drift += count; 00361 if(drift <= -count) 00362 drift= -count + 1; 00363 }else if(drift > 0){ 00364 if(state->bias < 127) state->bias++; 00365 00366 drift -= count; 00367 if(drift > 0) 00368 drift= 0; 00369 } 00370 00371 state->drift= drift; 00372 state->count= count; 00373 } 00374 00375 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){ 00376 int i, k, code; 00377 //printf("final: %d ", v); 00378 v = fold(v - state->bias, bits); 00379 00380 i= state->count; 00381 k=0; 00382 while(i < state->error_sum){ //FIXME optimize 00383 k++; 00384 i += i; 00385 } 00386 00387 assert(k<=8); 00388 00389 #if 0 // JPEG LS 00390 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1); 00391 else code= v; 00392 #else 00393 code= v ^ ((2*state->drift + state->count)>>31); 00394 #endif 00395 00396 //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k); 00397 set_sr_golomb(pb, code, k, 12, bits); 00398 00399 update_vlc_state(state, v); 00400 } 00401 00402 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){ 00403 int k, i, v, ret; 00404 00405 i= state->count; 00406 k=0; 00407 while(i < state->error_sum){ //FIXME optimize 00408 k++; 00409 i += i; 00410 } 00411 00412 assert(k<=8); 00413 00414 v= get_sr_golomb(gb, k, 12, bits); 00415 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k); 00416 00417 #if 0 // JPEG LS 00418 if(k==0 && 2*state->drift <= - state->count) v ^= (-1); 00419 #else 00420 v ^= ((2*state->drift + state->count)>>31); 00421 #endif 00422 00423 ret= fold(v + state->bias, bits); 00424 00425 update_vlc_state(state, v); 00426 //printf("final: %d\n", ret); 00427 return ret; 00428 } 00429 00430 #if CONFIG_FFV1_ENCODER 00431 static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){ 00432 PlaneContext * const p= &s->plane[plane_index]; 00433 RangeCoder * const c= &s->c; 00434 int x; 00435 int run_index= s->run_index; 00436 int run_count=0; 00437 int run_mode=0; 00438 00439 if(s->ac){ 00440 if(c->bytestream_end - c->bytestream < w*20){ 00441 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); 00442 return -1; 00443 } 00444 }else{ 00445 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){ 00446 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); 00447 return -1; 00448 } 00449 } 00450 00451 for(x=0; x<w; x++){ 00452 int diff, context; 00453 00454 context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x); 00455 diff= sample[0][x] - predict(sample[0]+x, sample[1]+x); 00456 00457 if(context < 0){ 00458 context = -context; 00459 diff= -diff; 00460 } 00461 00462 diff= fold(diff, bits); 00463 00464 if(s->ac){ 00465 put_symbol_inline(c, p->state[context], diff, 1); 00466 }else{ 00467 if(context == 0) run_mode=1; 00468 00469 if(run_mode){ 00470 00471 if(diff){ 00472 while(run_count >= 1<<ff_log2_run[run_index]){ 00473 run_count -= 1<<ff_log2_run[run_index]; 00474 run_index++; 00475 put_bits(&s->pb, 1, 1); 00476 } 00477 00478 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count); 00479 if(run_index) run_index--; 00480 run_count=0; 00481 run_mode=0; 00482 if(diff>0) diff--; 00483 }else{ 00484 run_count++; 00485 } 00486 } 00487 00488 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)put_bits_count(&s->pb)); 00489 00490 if(run_mode == 0) 00491 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits); 00492 } 00493 } 00494 if(run_mode){ 00495 while(run_count >= 1<<ff_log2_run[run_index]){ 00496 run_count -= 1<<ff_log2_run[run_index]; 00497 run_index++; 00498 put_bits(&s->pb, 1, 1); 00499 } 00500 00501 if(run_count) 00502 put_bits(&s->pb, 1, 1); 00503 } 00504 s->run_index= run_index; 00505 00506 return 0; 00507 } 00508 00509 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){ 00510 int x,y,i; 00511 const int ring_size= s->avctx->context_model ? 3 : 2; 00512 int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size]; 00513 s->run_index=0; 00514 00515 memset(sample_buffer, 0, sizeof(sample_buffer)); 00516 00517 for(y=0; y<h; y++){ 00518 for(i=0; i<ring_size; i++) 00519 sample[i]= sample_buffer[(h+i-y)%ring_size]+3; 00520 00521 sample[0][-1]= sample[1][0 ]; 00522 sample[1][ w]= sample[1][w-1]; 00523 //{START_TIMER 00524 if(s->avctx->bits_per_raw_sample<=8){ 00525 for(x=0; x<w; x++){ 00526 sample[0][x]= src[x + stride*y]; 00527 } 00528 encode_line(s, w, sample, plane_index, 8); 00529 }else{ 00530 for(x=0; x<w; x++){ 00531 sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample); 00532 } 00533 encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample); 00534 } 00535 //STOP_TIMER("encode line")} 00536 } 00537 } 00538 00539 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){ 00540 int x, y, p, i; 00541 const int ring_size= s->avctx->context_model ? 3 : 2; 00542 int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size]; 00543 s->run_index=0; 00544 00545 memset(sample_buffer, 0, sizeof(sample_buffer)); 00546 00547 for(y=0; y<h; y++){ 00548 for(i=0; i<ring_size; i++) 00549 for(p=0; p<3; p++) 00550 sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3; 00551 00552 for(x=0; x<w; x++){ 00553 int v= src[x + stride*y]; 00554 int b= v&0xFF; 00555 int g= (v>>8)&0xFF; 00556 int r= (v>>16)&0xFF; 00557 00558 b -= g; 00559 r -= g; 00560 g += (b + r)>>2; 00561 b += 0x100; 00562 r += 0x100; 00563 00564 // assert(g>=0 && b>=0 && r>=0); 00565 // assert(g<256 && b<512 && r<512); 00566 sample[0][0][x]= g; 00567 sample[1][0][x]= b; 00568 sample[2][0][x]= r; 00569 } 00570 for(p=0; p<3; p++){ 00571 sample[p][0][-1]= sample[p][1][0 ]; 00572 sample[p][1][ w]= sample[p][1][w-1]; 00573 encode_line(s, w, sample[p], FFMIN(p, 1), 9); 00574 } 00575 } 00576 } 00577 00578 static void write_quant_table(RangeCoder *c, int16_t *quant_table){ 00579 int last=0; 00580 int i; 00581 uint8_t state[CONTEXT_SIZE]; 00582 memset(state, 128, sizeof(state)); 00583 00584 for(i=1; i<128 ; i++){ 00585 if(quant_table[i] != quant_table[i-1]){ 00586 put_symbol(c, state, i-last-1, 0); 00587 last= i; 00588 } 00589 } 00590 put_symbol(c, state, i-last-1, 0); 00591 } 00592 00593 static void write_header(FFV1Context *f){ 00594 uint8_t state[CONTEXT_SIZE]; 00595 int i; 00596 RangeCoder * const c= &f->c; 00597 00598 memset(state, 128, sizeof(state)); 00599 00600 put_symbol(c, state, f->version, 0); 00601 put_symbol(c, state, f->ac, 0); 00602 if(f->ac>1){ 00603 for(i=1; i<256; i++){ 00604 f->state_transition[i]=ver2_state[i]; 00605 put_symbol(c, state, ver2_state[i] - c->one_state[i], 1); 00606 } 00607 } 00608 put_symbol(c, state, f->colorspace, 0); //YUV cs type 00609 if(f->version>0) 00610 put_symbol(c, state, f->avctx->bits_per_raw_sample, 0); 00611 put_rac(c, state, 1); //chroma planes 00612 put_symbol(c, state, f->chroma_h_shift, 0); 00613 put_symbol(c, state, f->chroma_v_shift, 0); 00614 put_rac(c, state, 0); //no transparency plane 00615 00616 for(i=0; i<5; i++) 00617 write_quant_table(c, f->quant_table[i]); 00618 } 00619 #endif /* CONFIG_FFV1_ENCODER */ 00620 00621 static av_cold int common_init(AVCodecContext *avctx){ 00622 FFV1Context *s = avctx->priv_data; 00623 00624 s->avctx= avctx; 00625 s->flags= avctx->flags; 00626 00627 dsputil_init(&s->dsp, avctx); 00628 00629 s->width = avctx->width; 00630 s->height= avctx->height; 00631 00632 assert(s->width && s->height); 00633 00634 return 0; 00635 } 00636 00637 #if CONFIG_FFV1_ENCODER 00638 static av_cold int encode_init(AVCodecContext *avctx) 00639 { 00640 FFV1Context *s = avctx->priv_data; 00641 int i; 00642 00643 common_init(avctx); 00644 00645 s->version=0; 00646 s->ac= avctx->coder_type ? 2:0; 00647 00648 s->plane_count=2; 00649 for(i=0; i<256; i++){ 00650 if(avctx->bits_per_raw_sample <=8){ 00651 s->quant_table[0][i]= quant11[i]; 00652 s->quant_table[1][i]= 11*quant11[i]; 00653 if(avctx->context_model==0){ 00654 s->quant_table[2][i]= 11*11*quant11[i]; 00655 s->quant_table[3][i]= 00656 s->quant_table[4][i]=0; 00657 }else{ 00658 s->quant_table[2][i]= 11*11*quant5 [i]; 00659 s->quant_table[3][i]= 5*11*11*quant5 [i]; 00660 s->quant_table[4][i]= 5*5*11*11*quant5 [i]; 00661 } 00662 }else{ 00663 s->quant_table[0][i]= quant9_10bit[i]; 00664 s->quant_table[1][i]= 11*quant9_10bit[i]; 00665 if(avctx->context_model==0){ 00666 s->quant_table[2][i]= 11*11*quant9_10bit[i]; 00667 s->quant_table[3][i]= 00668 s->quant_table[4][i]=0; 00669 }else{ 00670 s->quant_table[2][i]= 11*11*quant5_10bit[i]; 00671 s->quant_table[3][i]= 5*11*11*quant5_10bit[i]; 00672 s->quant_table[4][i]= 5*5*11*11*quant5_10bit[i]; 00673 } 00674 } 00675 } 00676 00677 for(i=0; i<s->plane_count; i++){ 00678 PlaneContext * const p= &s->plane[i]; 00679 00680 if(avctx->context_model==0){ 00681 p->context_count= (11*11*11+1)/2; 00682 }else{ 00683 p->context_count= (11*11*5*5*5+1)/2; 00684 } 00685 00686 if(s->ac){ 00687 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); 00688 }else{ 00689 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState)); 00690 } 00691 } 00692 00693 avctx->coded_frame= &s->picture; 00694 switch(avctx->pix_fmt){ 00695 case PIX_FMT_YUV444P16: 00696 case PIX_FMT_YUV422P16: 00697 case PIX_FMT_YUV420P16: 00698 if(avctx->bits_per_raw_sample <=8){ 00699 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n"); 00700 return -1; 00701 } 00702 if(!s->ac){ 00703 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n"); 00704 return -1; 00705 } 00706 s->version= 1; 00707 case PIX_FMT_YUV444P: 00708 case PIX_FMT_YUV422P: 00709 case PIX_FMT_YUV420P: 00710 case PIX_FMT_YUV411P: 00711 case PIX_FMT_YUV410P: 00712 s->colorspace= 0; 00713 break; 00714 case PIX_FMT_RGB32: 00715 s->colorspace= 1; 00716 break; 00717 default: 00718 av_log(avctx, AV_LOG_ERROR, "format not supported\n"); 00719 return -1; 00720 } 00721 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift); 00722 00723 s->picture_number=0; 00724 00725 return 0; 00726 } 00727 #endif /* CONFIG_FFV1_ENCODER */ 00728 00729 00730 static void clear_state(FFV1Context *f){ 00731 int i, j; 00732 00733 for(i=0; i<f->plane_count; i++){ 00734 PlaneContext *p= &f->plane[i]; 00735 00736 p->interlace_bit_state[0]= 128; 00737 p->interlace_bit_state[1]= 128; 00738 00739 for(j=0; j<p->context_count; j++){ 00740 if(f->ac){ 00741 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE); 00742 }else{ 00743 p->vlc_state[j].drift= 0; 00744 p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2); 00745 p->vlc_state[j].bias= 0; 00746 p->vlc_state[j].count= 1; 00747 } 00748 } 00749 } 00750 } 00751 00752 #if CONFIG_FFV1_ENCODER 00753 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 00754 FFV1Context *f = avctx->priv_data; 00755 RangeCoder * const c= &f->c; 00756 AVFrame *pict = data; 00757 const int width= f->width; 00758 const int height= f->height; 00759 AVFrame * const p= &f->picture; 00760 int used_count= 0; 00761 uint8_t keystate=128; 00762 00763 ff_init_range_encoder(c, buf, buf_size); 00764 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 00765 00766 *p = *pict; 00767 p->pict_type= FF_I_TYPE; 00768 00769 if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){ 00770 put_rac(c, &keystate, 1); 00771 p->key_frame= 1; 00772 write_header(f); 00773 clear_state(f); 00774 }else{ 00775 put_rac(c, &keystate, 0); 00776 p->key_frame= 0; 00777 } 00778 00779 if(!f->ac){ 00780 used_count += ff_rac_terminate(c); 00781 //printf("pos=%d\n", used_count); 00782 init_put_bits(&f->pb, buf + used_count, buf_size - used_count); 00783 }else if (f->ac>1){ 00784 int i; 00785 for(i=1; i<256; i++){ 00786 c->one_state[i]= f->state_transition[i]; 00787 c->zero_state[256-i]= 256-c->one_state[i]; 00788 } 00789 } 00790 00791 if(f->colorspace==0){ 00792 const int chroma_width = -((-width )>>f->chroma_h_shift); 00793 const int chroma_height= -((-height)>>f->chroma_v_shift); 00794 00795 encode_plane(f, p->data[0], width, height, p->linesize[0], 0); 00796 00797 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1); 00798 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1); 00799 }else{ 00800 encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4); 00801 } 00802 emms_c(); 00803 00804 f->picture_number++; 00805 00806 if(f->ac){ 00807 return ff_rac_terminate(c); 00808 }else{ 00809 flush_put_bits(&f->pb); //nicer padding FIXME 00810 return used_count + (put_bits_count(&f->pb)+7)/8; 00811 } 00812 } 00813 #endif /* CONFIG_FFV1_ENCODER */ 00814 00815 static av_cold int common_end(AVCodecContext *avctx){ 00816 FFV1Context *s = avctx->priv_data; 00817 int i; 00818 00819 for(i=0; i<s->plane_count; i++){ 00820 PlaneContext *p= &s->plane[i]; 00821 00822 av_freep(&p->state); 00823 av_freep(&p->vlc_state); 00824 } 00825 00826 return 0; 00827 } 00828 00829 static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){ 00830 PlaneContext * const p= &s->plane[plane_index]; 00831 RangeCoder * const c= &s->c; 00832 int x; 00833 int run_count=0; 00834 int run_mode=0; 00835 int run_index= s->run_index; 00836 00837 for(x=0; x<w; x++){ 00838 int diff, context, sign; 00839 00840 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x); 00841 if(context < 0){ 00842 context= -context; 00843 sign=1; 00844 }else 00845 sign=0; 00846 00847 00848 if(s->ac){ 00849 diff= get_symbol_inline(c, p->state[context], 1); 00850 }else{ 00851 if(context == 0 && run_mode==0) run_mode=1; 00852 00853 if(run_mode){ 00854 if(run_count==0 && run_mode==1){ 00855 if(get_bits1(&s->gb)){ 00856 run_count = 1<<ff_log2_run[run_index]; 00857 if(x + run_count <= w) run_index++; 00858 }else{ 00859 if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]); 00860 else run_count=0; 00861 if(run_index) run_index--; 00862 run_mode=2; 00863 } 00864 } 00865 run_count--; 00866 if(run_count < 0){ 00867 run_mode=0; 00868 run_count=0; 00869 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); 00870 if(diff>=0) diff++; 00871 }else 00872 diff=0; 00873 }else 00874 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); 00875 00876 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb)); 00877 } 00878 00879 if(sign) diff= -diff; 00880 00881 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1); 00882 } 00883 s->run_index= run_index; 00884 } 00885 00886 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){ 00887 int x, y; 00888 int_fast16_t sample_buffer[2][w+6]; 00889 int_fast16_t *sample[2]; 00890 sample[0]=sample_buffer[0]+3; 00891 sample[1]=sample_buffer[1]+3; 00892 00893 s->run_index=0; 00894 00895 memset(sample_buffer, 0, sizeof(sample_buffer)); 00896 00897 for(y=0; y<h; y++){ 00898 int_fast16_t *temp= sample[0]; //FIXME try a normal buffer 00899 00900 sample[0]= sample[1]; 00901 sample[1]= temp; 00902 00903 sample[1][-1]= sample[0][0 ]; 00904 sample[0][ w]= sample[0][w-1]; 00905 00906 //{START_TIMER 00907 if(s->avctx->bits_per_raw_sample <= 8){ 00908 decode_line(s, w, sample, plane_index, 8); 00909 for(x=0; x<w; x++){ 00910 src[x + stride*y]= sample[1][x]; 00911 } 00912 }else{ 00913 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample); 00914 for(x=0; x<w; x++){ 00915 ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample); 00916 } 00917 } 00918 //STOP_TIMER("decode-line")} 00919 } 00920 } 00921 00922 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){ 00923 int x, y, p; 00924 int_fast16_t sample_buffer[3][2][w+6]; 00925 int_fast16_t *sample[3][2]; 00926 for(x=0; x<3; x++){ 00927 sample[x][0] = sample_buffer[x][0]+3; 00928 sample[x][1] = sample_buffer[x][1]+3; 00929 } 00930 00931 s->run_index=0; 00932 00933 memset(sample_buffer, 0, sizeof(sample_buffer)); 00934 00935 for(y=0; y<h; y++){ 00936 for(p=0; p<3; p++){ 00937 int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer 00938 00939 sample[p][0]= sample[p][1]; 00940 sample[p][1]= temp; 00941 00942 sample[p][1][-1]= sample[p][0][0 ]; 00943 sample[p][0][ w]= sample[p][0][w-1]; 00944 decode_line(s, w, sample[p], FFMIN(p, 1), 9); 00945 } 00946 for(x=0; x<w; x++){ 00947 int g= sample[0][1][x]; 00948 int b= sample[1][1][x]; 00949 int r= sample[2][1][x]; 00950 00951 // assert(g>=0 && b>=0 && r>=0); 00952 // assert(g<256 && b<512 && r<512); 00953 00954 b -= 0x100; 00955 r -= 0x100; 00956 g -= (b + r)>>2; 00957 b += g; 00958 r += g; 00959 00960 src[x + stride*y]= b + (g<<8) + (r<<16); 00961 } 00962 } 00963 } 00964 00965 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){ 00966 int v; 00967 int i=0; 00968 uint8_t state[CONTEXT_SIZE]; 00969 00970 memset(state, 128, sizeof(state)); 00971 00972 for(v=0; i<128 ; v++){ 00973 int len= get_symbol(c, state, 0) + 1; 00974 00975 if(len + i > 128) return -1; 00976 00977 while(len--){ 00978 quant_table[i] = scale*v; 00979 i++; 00980 //printf("%2d ",v); 00981 //if(i%16==0) printf("\n"); 00982 } 00983 } 00984 00985 for(i=1; i<128; i++){ 00986 quant_table[256-i]= -quant_table[i]; 00987 } 00988 quant_table[128]= -quant_table[127]; 00989 00990 return 2*v - 1; 00991 } 00992 00993 static int read_header(FFV1Context *f){ 00994 uint8_t state[CONTEXT_SIZE]; 00995 int i, context_count; 00996 RangeCoder * const c= &f->c; 00997 00998 memset(state, 128, sizeof(state)); 00999 01000 f->version= get_symbol(c, state, 0); 01001 f->ac= f->avctx->coder_type= get_symbol(c, state, 0); 01002 if(f->ac>1){ 01003 for(i=1; i<256; i++){ 01004 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i]; 01005 } 01006 } 01007 f->colorspace= get_symbol(c, state, 0); //YUV cs type 01008 if(f->version>0) 01009 f->avctx->bits_per_raw_sample= get_symbol(c, state, 0); 01010 get_rac(c, state); //no chroma = false 01011 f->chroma_h_shift= get_symbol(c, state, 0); 01012 f->chroma_v_shift= get_symbol(c, state, 0); 01013 get_rac(c, state); //transparency plane 01014 f->plane_count= 2; 01015 01016 if(f->colorspace==0){ 01017 if(f->avctx->bits_per_raw_sample<=8){ 01018 switch(16*f->chroma_h_shift + f->chroma_v_shift){ 01019 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break; 01020 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break; 01021 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break; 01022 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break; 01023 case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break; 01024 default: 01025 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n"); 01026 return -1; 01027 } 01028 }else{ 01029 switch(16*f->chroma_h_shift + f->chroma_v_shift){ 01030 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break; 01031 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break; 01032 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break; 01033 default: 01034 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n"); 01035 return -1; 01036 } 01037 } 01038 }else if(f->colorspace==1){ 01039 if(f->chroma_h_shift || f->chroma_v_shift){ 01040 av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n"); 01041 return -1; 01042 } 01043 f->avctx->pix_fmt= PIX_FMT_RGB32; 01044 }else{ 01045 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n"); 01046 return -1; 01047 } 01048 01049 //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt); 01050 01051 context_count=1; 01052 for(i=0; i<5; i++){ 01053 context_count*= read_quant_table(c, f->quant_table[i], context_count); 01054 if(context_count < 0 || context_count > 32768){ 01055 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n"); 01056 return -1; 01057 } 01058 } 01059 context_count= (context_count+1)/2; 01060 01061 for(i=0; i<f->plane_count; i++){ 01062 PlaneContext * const p= &f->plane[i]; 01063 01064 p->context_count= context_count; 01065 01066 if(f->ac){ 01067 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); 01068 }else{ 01069 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState)); 01070 } 01071 } 01072 01073 return 0; 01074 } 01075 01076 static av_cold int decode_init(AVCodecContext *avctx) 01077 { 01078 // FFV1Context *s = avctx->priv_data; 01079 01080 common_init(avctx); 01081 01082 return 0; 01083 } 01084 01085 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){ 01086 const uint8_t *buf = avpkt->data; 01087 int buf_size = avpkt->size; 01088 FFV1Context *f = avctx->priv_data; 01089 RangeCoder * const c= &f->c; 01090 const int width= f->width; 01091 const int height= f->height; 01092 AVFrame * const p= &f->picture; 01093 int bytes_read; 01094 uint8_t keystate= 128; 01095 01096 AVFrame *picture = data; 01097 01098 ff_init_range_decoder(c, buf, buf_size); 01099 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 01100 01101 01102 p->pict_type= FF_I_TYPE; //FIXME I vs. P 01103 if(get_rac(c, &keystate)){ 01104 p->key_frame= 1; 01105 if(read_header(f) < 0) 01106 return -1; 01107 clear_state(f); 01108 }else{ 01109 p->key_frame= 0; 01110 } 01111 if(f->ac>1){ 01112 int i; 01113 for(i=1; i<256; i++){ 01114 c->one_state[i]= f->state_transition[i]; 01115 c->zero_state[256-i]= 256-c->one_state[i]; 01116 } 01117 } 01118 01119 if(!f->plane[0].state && !f->plane[0].vlc_state) 01120 return -1; 01121 01122 p->reference= 0; 01123 if(avctx->get_buffer(avctx, p) < 0){ 01124 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 01125 return -1; 01126 } 01127 01128 if(avctx->debug&FF_DEBUG_PICT_INFO) 01129 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac); 01130 01131 if(!f->ac){ 01132 bytes_read = c->bytestream - c->bytestream_start - 1; 01133 if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME 01134 //printf("pos=%d\n", bytes_read); 01135 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read); 01136 } else { 01137 bytes_read = 0; /* avoid warning */ 01138 } 01139 01140 if(f->colorspace==0){ 01141 const int chroma_width = -((-width )>>f->chroma_h_shift); 01142 const int chroma_height= -((-height)>>f->chroma_v_shift); 01143 decode_plane(f, p->data[0], width, height, p->linesize[0], 0); 01144 01145 decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1); 01146 decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1); 01147 }else{ 01148 decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4); 01149 } 01150 01151 emms_c(); 01152 01153 f->picture_number++; 01154 01155 *picture= *p; 01156 01157 avctx->release_buffer(avctx, p); //FIXME 01158 01159 *data_size = sizeof(AVFrame); 01160 01161 if(f->ac){ 01162 bytes_read= c->bytestream - c->bytestream_start - 1; 01163 if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n"); 01164 }else{ 01165 bytes_read+= (get_bits_count(&f->gb)+7)/8; 01166 } 01167 01168 return bytes_read; 01169 } 01170 01171 AVCodec ffv1_decoder = { 01172 "ffv1", 01173 AVMEDIA_TYPE_VIDEO, 01174 CODEC_ID_FFV1, 01175 sizeof(FFV1Context), 01176 decode_init, 01177 NULL, 01178 common_end, 01179 decode_frame, 01180 CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/, 01181 NULL, 01182 .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"), 01183 }; 01184 01185 #if CONFIG_FFV1_ENCODER 01186 AVCodec ffv1_encoder = { 01187 "ffv1", 01188 AVMEDIA_TYPE_VIDEO, 01189 CODEC_ID_FFV1, 01190 sizeof(FFV1Context), 01191 encode_init, 01192 encode_frame, 01193 common_end, 01194 .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}, 01195 .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"), 01196 }; 01197 #endif