ffv1.c

Go to the documentation of this file.
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 
00023 /**
00024  * @file ffv1.c
00025  * FF Video Codec 1 (an experimental lossless codec)
00026  */
00027 
00028 #include "avcodec.h"
00029 #include "bitstream.h"
00030 #include "dsputil.h"
00031 #include "rangecoder.h"
00032 #include "golomb.h"
00033 
00034 #define MAX_PLANES 4
00035 #define CONTEXT_SIZE 32
00036 
00037 extern const uint8_t ff_log2_run[32];
00038 
00039 static const int8_t quant3[256]={
00040  0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00041  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00042  1, 1, 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,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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,-2,-1,-1,
00092 };
00093 static const int8_t quant9[256]={
00094  0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00095  3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00096  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00097  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00098  4, 4, 4, 4, 4, 4, 4, 4, 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,-4,-4,-4,-4,-4,-4,-4,
00108 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
00109 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
00110 };
00111 static const int8_t quant11[256]={
00112  0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
00113  4, 4, 4,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6, 6, 6, 6, 6,
00135  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00136  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00137  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00138 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00139 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00140 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00141 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00142 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
00143 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00144 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00145 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
00146 };
00147 
00148 typedef struct VlcState{
00149     int16_t drift;
00150     uint16_t error_sum;
00151     int8_t bias;
00152     uint8_t count;
00153 } VlcState;
00154 
00155 typedef struct PlaneContext{
00156     int context_count;
00157     uint8_t (*state)[CONTEXT_SIZE];
00158     VlcState *vlc_state;
00159     uint8_t interlace_bit_state[2];
00160 } PlaneContext;
00161 
00162 typedef struct FFV1Context{
00163     AVCodecContext *avctx;
00164     RangeCoder c;
00165     GetBitContext gb;
00166     PutBitContext pb;
00167     int version;
00168     int width, height;
00169     int chroma_h_shift, chroma_v_shift;
00170     int flags;
00171     int picture_number;
00172     AVFrame picture;
00173     int plane_count;
00174     int ac;                              ///< 1-> CABAC 0-> golomb rice
00175     PlaneContext plane[MAX_PLANES];
00176     int16_t quant_table[5][256];
00177     int run_index;
00178     int colorspace;
00179 
00180     DSPContext dsp;
00181 }FFV1Context;
00182 
00183 static av_always_inline int fold(int diff, int bits){
00184     if(bits==8)
00185         diff= (int8_t)diff;
00186     else{
00187         diff+= 1<<(bits-1);
00188         diff&=(1<<bits)-1;
00189         diff-= 1<<(bits-1);
00190     }
00191 
00192     return diff;
00193 }
00194 
00195 static inline int predict(int_fast16_t *src, int_fast16_t *last){
00196     const int LT= last[-1];
00197     const int  T= last[ 0];
00198     const int L =  src[-1];
00199 
00200     return mid_pred(L, L + T - LT, T);
00201 }
00202 
00203 static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
00204     const int LT= last[-1];
00205     const int  T= last[ 0];
00206     const int RT= last[ 1];
00207     const int L =  src[-1];
00208 
00209     if(f->quant_table[3][127]){
00210         const int TT= last2[0];
00211         const int LL=  src[-2];
00212         return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
00213               +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
00214     }else
00215         return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
00216 }
00217 
00218 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00219     int i;
00220 
00221     if(v){
00222         const int a= FFABS(v);
00223         const int e= av_log2(a);
00224         put_rac(c, state+0, 0);
00225 
00226         assert(e<=9);
00227 
00228         for(i=0; i<e; i++){
00229             put_rac(c, state+1+i, 1);  //1..10
00230         }
00231         put_rac(c, state+1+i, 0);
00232 
00233         for(i=e-1; i>=0; i--){
00234             put_rac(c, state+22+i, (a>>i)&1); //22..31
00235         }
00236 
00237         if(is_signed)
00238             put_rac(c, state+11 + e, v < 0); //11..21
00239     }else{
00240         put_rac(c, state+0, 1);
00241     }
00242 }
00243 
00244 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00245     if(get_rac(c, state+0))
00246         return 0;
00247     else{
00248         int i, e, a;
00249         e= 0;
00250         while(get_rac(c, state+1 + e)){ //1..10
00251             e++;
00252         }
00253         assert(e<=9);
00254 
00255         a= 1;
00256         for(i=e-1; i>=0; i--){
00257             a += a + get_rac(c, state+22 + i); //22..31
00258         }
00259 
00260         if(is_signed && get_rac(c, state+11 + e)) //11..21
00261             return -a;
00262         else
00263             return a;
00264     }
00265 }
00266 
00267 static inline void update_vlc_state(VlcState * const state, const int v){
00268     int drift= state->drift;
00269     int count= state->count;
00270     state->error_sum += FFABS(v);
00271     drift += v;
00272 
00273     if(count == 128){ //FIXME variable
00274         count >>= 1;
00275         drift >>= 1;
00276         state->error_sum >>= 1;
00277     }
00278     count++;
00279 
00280     if(drift <= -count){
00281         if(state->bias > -128) state->bias--;
00282 
00283         drift += count;
00284         if(drift <= -count)
00285             drift= -count + 1;
00286     }else if(drift > 0){
00287         if(state->bias <  127) state->bias++;
00288 
00289         drift -= count;
00290         if(drift > 0)
00291             drift= 0;
00292     }
00293 
00294     state->drift= drift;
00295     state->count= count;
00296 }
00297 
00298 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00299     int i, k, code;
00300 //printf("final: %d ", v);
00301     v = fold(v - state->bias, bits);
00302 
00303     i= state->count;
00304     k=0;
00305     while(i < state->error_sum){ //FIXME optimize
00306         k++;
00307         i += i;
00308     }
00309 
00310     assert(k<=8);
00311 
00312 #if 0 // JPEG LS
00313     if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00314     else                                         code= v;
00315 #else
00316      code= v ^ ((2*state->drift + state->count)>>31);
00317 #endif
00318 
00319 //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);
00320     set_sr_golomb(pb, code, k, 12, bits);
00321 
00322     update_vlc_state(state, v);
00323 }
00324 
00325 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00326     int k, i, v, ret;
00327 
00328     i= state->count;
00329     k=0;
00330     while(i < state->error_sum){ //FIXME optimize
00331         k++;
00332         i += i;
00333     }
00334 
00335     assert(k<=8);
00336 
00337     v= get_sr_golomb(gb, k, 12, bits);
00338 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
00339 
00340 #if 0 // JPEG LS
00341     if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00342 #else
00343      v ^= ((2*state->drift + state->count)>>31);
00344 #endif
00345 
00346     ret= fold(v + state->bias, bits);
00347 
00348     update_vlc_state(state, v);
00349 //printf("final: %d\n", ret);
00350     return ret;
00351 }
00352 
00353 #ifdef CONFIG_ENCODERS
00354 static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00355     PlaneContext * const p= &s->plane[plane_index];
00356     RangeCoder * const c= &s->c;
00357     int x;
00358     int run_index= s->run_index;
00359     int run_count=0;
00360     int run_mode=0;
00361 
00362     if(s->ac){
00363         if(c->bytestream_end - c->bytestream < w*20){
00364             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00365             return -1;
00366         }
00367     }else{
00368         if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00369             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00370             return -1;
00371         }
00372     }
00373 
00374     for(x=0; x<w; x++){
00375         int diff, context;
00376 
00377         context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
00378         diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00379 
00380         if(context < 0){
00381             context = -context;
00382             diff= -diff;
00383         }
00384 
00385         diff= fold(diff, bits);
00386 
00387         if(s->ac){
00388             put_symbol(c, p->state[context], diff, 1);
00389         }else{
00390             if(context == 0) run_mode=1;
00391 
00392             if(run_mode){
00393 
00394                 if(diff){
00395                     while(run_count >= 1<<ff_log2_run[run_index]){
00396                         run_count -= 1<<ff_log2_run[run_index];
00397                         run_index++;
00398                         put_bits(&s->pb, 1, 1);
00399                     }
00400 
00401                     put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
00402                     if(run_index) run_index--;
00403                     run_count=0;
00404                     run_mode=0;
00405                     if(diff>0) diff--;
00406                 }else{
00407                     run_count++;
00408                 }
00409             }
00410 
00411 //            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));
00412 
00413             if(run_mode == 0)
00414                 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00415         }
00416     }
00417     if(run_mode){
00418         while(run_count >= 1<<ff_log2_run[run_index]){
00419             run_count -= 1<<ff_log2_run[run_index];
00420             run_index++;
00421             put_bits(&s->pb, 1, 1);
00422         }
00423 
00424         if(run_count)
00425             put_bits(&s->pb, 1, 1);
00426     }
00427     s->run_index= run_index;
00428 
00429     return 0;
00430 }
00431 
00432 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00433     int x,y,i;
00434     const int ring_size= s->avctx->context_model ? 3 : 2;
00435     int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
00436     s->run_index=0;
00437 
00438     memset(sample_buffer, 0, sizeof(sample_buffer));
00439 
00440     for(y=0; y<h; y++){
00441         for(i=0; i<ring_size; i++)
00442             sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
00443 
00444         sample[0][-1]= sample[1][0  ];
00445         sample[1][ w]= sample[1][w-1];
00446 //{START_TIMER
00447         for(x=0; x<w; x++){
00448             sample[0][x]= src[x + stride*y];
00449         }
00450         encode_line(s, w, sample, plane_index, 8);
00451 //STOP_TIMER("encode line")}
00452     }
00453 }
00454 
00455 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00456     int x, y, p, i;
00457     const int ring_size= s->avctx->context_model ? 3 : 2;
00458     int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
00459     s->run_index=0;
00460 
00461     memset(sample_buffer, 0, sizeof(sample_buffer));
00462 
00463     for(y=0; y<h; y++){
00464         for(i=0; i<ring_size; i++)
00465             for(p=0; p<3; p++)
00466                 sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
00467 
00468         for(x=0; x<w; x++){
00469             int v= src[x + stride*y];
00470             int b= v&0xFF;
00471             int g= (v>>8)&0xFF;
00472             int r= (v>>16)&0xFF;
00473 
00474             b -= g;
00475             r -= g;
00476             g += (b + r)>>2;
00477             b += 0x100;
00478             r += 0x100;
00479 
00480 //            assert(g>=0 && b>=0 && r>=0);
00481 //            assert(g<256 && b<512 && r<512);
00482             sample[0][0][x]= g;
00483             sample[1][0][x]= b;
00484             sample[2][0][x]= r;
00485         }
00486         for(p=0; p<3; p++){
00487             sample[p][0][-1]= sample[p][1][0  ];
00488             sample[p][1][ w]= sample[p][1][w-1];
00489             encode_line(s, w, sample[p], FFMIN(p, 1), 9);
00490         }
00491     }
00492 }
00493 
00494 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00495     int last=0;
00496     int i;
00497     uint8_t state[CONTEXT_SIZE];
00498     memset(state, 128, sizeof(state));
00499 
00500     for(i=1; i<128 ; i++){
00501         if(quant_table[i] != quant_table[i-1]){
00502             put_symbol(c, state, i-last-1, 0);
00503             last= i;
00504         }
00505     }
00506     put_symbol(c, state, i-last-1, 0);
00507 }
00508 
00509 static void write_header(FFV1Context *f){
00510     uint8_t state[CONTEXT_SIZE];
00511     int i;
00512     RangeCoder * const c= &f->c;
00513 
00514     memset(state, 128, sizeof(state));
00515 
00516     put_symbol(c, state, f->version, 0);
00517     put_symbol(c, state, f->avctx->coder_type, 0);
00518     put_symbol(c, state, f->colorspace, 0); //YUV cs type
00519     put_rac(c, state, 1); //chroma planes
00520         put_symbol(c, state, f->chroma_h_shift, 0);
00521         put_symbol(c, state, f->chroma_v_shift, 0);
00522     put_rac(c, state, 0); //no transparency plane
00523 
00524     for(i=0; i<5; i++)
00525         write_quant_table(c, f->quant_table[i]);
00526 }
00527 #endif /* CONFIG_ENCODERS */
00528 
00529 static av_cold int common_init(AVCodecContext *avctx){
00530     FFV1Context *s = avctx->priv_data;
00531     int width, height;
00532 
00533     s->avctx= avctx;
00534     s->flags= avctx->flags;
00535 
00536     dsputil_init(&s->dsp, avctx);
00537 
00538     width= s->width= avctx->width;
00539     height= s->height= avctx->height;
00540 
00541     assert(width && height);
00542 
00543     return 0;
00544 }
00545 
00546 #ifdef CONFIG_ENCODERS
00547 static av_cold int encode_init(AVCodecContext *avctx)
00548 {
00549     FFV1Context *s = avctx->priv_data;
00550     int i;
00551 
00552     common_init(avctx);
00553 
00554     s->version=0;
00555     s->ac= avctx->coder_type;
00556 
00557     s->plane_count=2;
00558     for(i=0; i<256; i++){
00559         s->quant_table[0][i]=           quant11[i];
00560         s->quant_table[1][i]=        11*quant11[i];
00561         if(avctx->context_model==0){
00562             s->quant_table[2][i]=     11*11*quant11[i];
00563             s->quant_table[3][i]=
00564             s->quant_table[4][i]=0;
00565         }else{
00566             s->quant_table[2][i]=     11*11*quant5 [i];
00567             s->quant_table[3][i]=   5*11*11*quant5 [i];
00568             s->quant_table[4][i]= 5*5*11*11*quant5 [i];
00569         }
00570     }
00571 
00572     for(i=0; i<s->plane_count; i++){
00573         PlaneContext * const p= &s->plane[i];
00574 
00575         if(avctx->context_model==0){
00576             p->context_count= (11*11*11+1)/2;
00577         }else{
00578             p->context_count= (11*11*5*5*5+1)/2;
00579         }
00580 
00581         if(s->ac){
00582             if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00583         }else{
00584             if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00585         }
00586     }
00587 
00588     avctx->coded_frame= &s->picture;
00589     switch(avctx->pix_fmt){
00590     case PIX_FMT_YUV444P:
00591     case PIX_FMT_YUV422P:
00592     case PIX_FMT_YUV420P:
00593     case PIX_FMT_YUV411P:
00594     case PIX_FMT_YUV410P:
00595         s->colorspace= 0;
00596         break;
00597     case PIX_FMT_RGB32:
00598         s->colorspace= 1;
00599         break;
00600     default:
00601         av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00602         return -1;
00603     }
00604     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00605 
00606     s->picture_number=0;
00607 
00608     return 0;
00609 }
00610 #endif /* CONFIG_ENCODERS */
00611 
00612 
00613 static void clear_state(FFV1Context *f){
00614     int i, j;
00615 
00616     for(i=0; i<f->plane_count; i++){
00617         PlaneContext *p= &f->plane[i];
00618 
00619         p->interlace_bit_state[0]= 128;
00620         p->interlace_bit_state[1]= 128;
00621 
00622         for(j=0; j<p->context_count; j++){
00623             if(f->ac){
00624                 memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
00625             }else{
00626                 p->vlc_state[j].drift= 0;
00627                 p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
00628                 p->vlc_state[j].bias= 0;
00629                 p->vlc_state[j].count= 1;
00630             }
00631         }
00632     }
00633 }
00634 
00635 #ifdef CONFIG_ENCODERS
00636 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00637     FFV1Context *f = avctx->priv_data;
00638     RangeCoder * const c= &f->c;
00639     AVFrame *pict = data;
00640     const int width= f->width;
00641     const int height= f->height;
00642     AVFrame * const p= &f->picture;
00643     int used_count= 0;
00644     uint8_t keystate=128;
00645 
00646     ff_init_range_encoder(c, buf, buf_size);
00647 //    ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
00648     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00649 
00650     *p = *pict;
00651     p->pict_type= FF_I_TYPE;
00652 
00653     if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
00654         put_rac(c, &keystate, 1);
00655         p->key_frame= 1;
00656         write_header(f);
00657         clear_state(f);
00658     }else{
00659         put_rac(c, &keystate, 0);
00660         p->key_frame= 0;
00661     }
00662 
00663     if(!f->ac){
00664         used_count += ff_rac_terminate(c);
00665 //printf("pos=%d\n", used_count);
00666         init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
00667     }
00668 
00669     if(f->colorspace==0){
00670         const int chroma_width = -((-width )>>f->chroma_h_shift);
00671         const int chroma_height= -((-height)>>f->chroma_v_shift);
00672 
00673         encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00674 
00675         encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00676         encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00677     }else{
00678         encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
00679     }
00680     emms_c();
00681 
00682     f->picture_number++;
00683 
00684     if(f->ac){
00685         return ff_rac_terminate(c);
00686     }else{
00687         flush_put_bits(&f->pb); //nicer padding FIXME
00688         return used_count + (put_bits_count(&f->pb)+7)/8;
00689     }
00690 }
00691 #endif /* CONFIG_ENCODERS */
00692 
00693 static av_cold int common_end(AVCodecContext *avctx){
00694     FFV1Context *s = avctx->priv_data;
00695     int i;
00696 
00697     for(i=0; i<s->plane_count; i++){
00698         PlaneContext *p= &s->plane[i];
00699 
00700         av_freep(&p->state);
00701         av_freep(&p->vlc_state);
00702     }
00703 
00704     return 0;
00705 }
00706 
00707 static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
00708     PlaneContext * const p= &s->plane[plane_index];
00709     RangeCoder * const c= &s->c;
00710     int x;
00711     int run_count=0;
00712     int run_mode=0;
00713     int run_index= s->run_index;
00714 
00715     for(x=0; x<w; x++){
00716         int diff, context, sign;
00717 
00718         context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
00719         if(context < 0){
00720             context= -context;
00721             sign=1;
00722         }else
00723             sign=0;
00724 
00725 
00726         if(s->ac){
00727             diff= get_symbol(c, p->state[context], 1);
00728         }else{
00729             if(context == 0 && run_mode==0) run_mode=1;
00730 
00731             if(run_mode){
00732                 if(run_count==0 && run_mode==1){
00733                     if(get_bits1(&s->gb)){
00734                         run_count = 1<<ff_log2_run[run_index];
00735                         if(x + run_count <= w) run_index++;
00736                     }else{
00737                         if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
00738                         else run_count=0;
00739                         if(run_index) run_index--;
00740                         run_mode=2;
00741                     }
00742                 }
00743                 run_count--;
00744                 if(run_count < 0){
00745                     run_mode=0;
00746                     run_count=0;
00747                     diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00748                     if(diff>=0) diff++;
00749                 }else
00750                     diff=0;
00751             }else
00752                 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00753 
00754 //            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));
00755         }
00756 
00757         if(sign) diff= -diff;
00758 
00759         sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
00760     }
00761     s->run_index= run_index;
00762 }
00763 
00764 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00765     int x, y;
00766     int_fast16_t sample_buffer[2][w+6];
00767     int_fast16_t *sample[2];
00768     sample[0]=sample_buffer[0]+3;
00769     sample[1]=sample_buffer[1]+3;
00770 
00771     s->run_index=0;
00772 
00773     memset(sample_buffer, 0, sizeof(sample_buffer));
00774 
00775     for(y=0; y<h; y++){
00776         int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
00777 
00778         sample[0]= sample[1];
00779         sample[1]= temp;
00780 
00781         sample[1][-1]= sample[0][0  ];
00782         sample[0][ w]= sample[0][w-1];
00783 
00784 //{START_TIMER
00785         decode_line(s, w, sample, plane_index, 8);
00786         for(x=0; x<w; x++){
00787             src[x + stride*y]= sample[1][x];
00788         }
00789 //STOP_TIMER("decode-line")}
00790     }
00791 }
00792 
00793 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00794     int x, y, p;
00795     int_fast16_t sample_buffer[3][2][w+6];
00796     int_fast16_t *sample[3][2];
00797     for(x=0; x<3; x++){
00798         sample[x][0] = sample_buffer[x][0]+3;
00799         sample[x][1] = sample_buffer[x][1]+3;
00800     }
00801 
00802     s->run_index=0;
00803 
00804     memset(sample_buffer, 0, sizeof(sample_buffer));
00805 
00806     for(y=0; y<h; y++){
00807         for(p=0; p<3; p++){
00808             int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
00809 
00810             sample[p][0]= sample[p][1];
00811             sample[p][1]= temp;
00812 
00813             sample[p][1][-1]= sample[p][0][0  ];
00814             sample[p][0][ w]= sample[p][0][w-1];
00815             decode_line(s, w, sample[p], FFMIN(p, 1), 9);
00816         }
00817         for(x=0; x<w; x++){
00818             int g= sample[0][1][x];
00819             int b= sample[1][1][x];
00820             int r= sample[2][1][x];
00821 
00822 //            assert(g>=0 && b>=0 && r>=0);
00823 //            assert(g<256 && b<512 && r<512);
00824 
00825             b -= 0x100;
00826             r -= 0x100;
00827             g -= (b + r)>>2;
00828             b += g;
00829             r += g;
00830 
00831             src[x + stride*y]= b + (g<<8) + (r<<16);
00832         }
00833     }
00834 }
00835 
00836 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
00837     int v;
00838     int i=0;
00839     uint8_t state[CONTEXT_SIZE];
00840 
00841     memset(state, 128, sizeof(state));
00842 
00843     for(v=0; i<128 ; v++){
00844         int len= get_symbol(c, state, 0) + 1;
00845 
00846         if(len + i > 128) return -1;
00847 
00848         while(len--){
00849             quant_table[i] = scale*v;
00850             i++;
00851 //printf("%2d ",v);
00852 //if(i%16==0) printf("\n");
00853         }
00854     }
00855 
00856     for(i=1; i<128; i++){
00857         quant_table[256-i]= -quant_table[i];
00858     }
00859     quant_table[128]= -quant_table[127];
00860 
00861     return 2*v - 1;
00862 }
00863 
00864 static int read_header(FFV1Context *f){
00865     uint8_t state[CONTEXT_SIZE];
00866     int i, context_count;
00867     RangeCoder * const c= &f->c;
00868 
00869     memset(state, 128, sizeof(state));
00870 
00871     f->version= get_symbol(c, state, 0);
00872     f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
00873     f->colorspace= get_symbol(c, state, 0); //YUV cs type
00874     get_rac(c, state); //no chroma = false
00875     f->chroma_h_shift= get_symbol(c, state, 0);
00876     f->chroma_v_shift= get_symbol(c, state, 0);
00877     get_rac(c, state); //transparency plane
00878     f->plane_count= 2;
00879 
00880     if(f->colorspace==0){
00881         switch(16*f->chroma_h_shift + f->chroma_v_shift){
00882         case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
00883         case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
00884         case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
00885         case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
00886         case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
00887         default:
00888             av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00889             return -1;
00890         }
00891     }else if(f->colorspace==1){
00892         if(f->chroma_h_shift || f->chroma_v_shift){
00893             av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
00894             return -1;
00895         }
00896         f->avctx->pix_fmt= PIX_FMT_RGB32;
00897     }else{
00898         av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
00899         return -1;
00900     }
00901 
00902 //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
00903 
00904     context_count=1;
00905     for(i=0; i<5; i++){
00906         context_count*= read_quant_table(c, f->quant_table[i], context_count);
00907         if(context_count < 0 || context_count > 32768){
00908             av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
00909             return -1;
00910         }
00911     }
00912     context_count= (context_count+1)/2;
00913 
00914     for(i=0; i<f->plane_count; i++){
00915         PlaneContext * const p= &f->plane[i];
00916 
00917         p->context_count= context_count;
00918 
00919         if(f->ac){
00920             if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00921         }else{
00922             if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00923         }
00924     }
00925 
00926     return 0;
00927 }
00928 
00929 static av_cold int decode_init(AVCodecContext *avctx)
00930 {
00931 //    FFV1Context *s = avctx->priv_data;
00932 
00933     common_init(avctx);
00934 
00935     return 0;
00936 }
00937 
00938 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
00939     FFV1Context *f = avctx->priv_data;
00940     RangeCoder * const c= &f->c;
00941     const int width= f->width;
00942     const int height= f->height;
00943     AVFrame * const p= &f->picture;
00944     int bytes_read;
00945     uint8_t keystate= 128;
00946 
00947     AVFrame *picture = data;
00948 
00949     ff_init_range_decoder(c, buf, buf_size);
00950     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00951 
00952 
00953     p->pict_type= FF_I_TYPE; //FIXME I vs. P
00954     if(get_rac(c, &keystate)){
00955         p->key_frame= 1;
00956         if(read_header(f) < 0)
00957             return -1;
00958         clear_state(f);
00959     }else{
00960         p->key_frame= 0;
00961     }
00962     if(!f->plane[0].state && !f->plane[0].vlc_state)
00963         return -1;
00964 
00965     p->reference= 0;
00966     if(avctx->get_buffer(avctx, p) < 0){
00967         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00968         return -1;
00969     }
00970 
00971     if(avctx->debug&FF_DEBUG_PICT_INFO)
00972         av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
00973 
00974     if(!f->ac){
00975         bytes_read = c->bytestream - c->bytestream_start - 1;
00976         if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
00977 //printf("pos=%d\n", bytes_read);
00978         init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
00979     } else {
00980         bytes_read = 0; /* avoid warning */
00981     }
00982 
00983     if(f->colorspace==0){
00984         const int chroma_width = -((-width )>>f->chroma_h_shift);
00985         const int chroma_height= -((-height)>>f->chroma_v_shift);
00986         decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
00987 
00988         decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
00989         decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
00990     }else{
00991         decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
00992     }
00993 
00994     emms_c();
00995 
00996     f->picture_number++;
00997 
00998     *picture= *p;
00999 
01000     avctx->release_buffer(avctx, p); //FIXME
01001 
01002     *data_size = sizeof(AVFrame);
01003 
01004     if(f->ac){
01005         bytes_read= c->bytestream - c->bytestream_start - 1;
01006         if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
01007     }else{
01008         bytes_read+= (get_bits_count(&f->gb)+7)/8;
01009     }
01010 
01011     return bytes_read;
01012 }
01013 
01014 AVCodec ffv1_decoder = {
01015     "ffv1",
01016     CODEC_TYPE_VIDEO,
01017     CODEC_ID_FFV1,
01018     sizeof(FFV1Context),
01019     decode_init,
01020     NULL,
01021     common_end,
01022     decode_frame,
01023     CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
01024     NULL,
01025     .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
01026 };
01027 
01028 #ifdef CONFIG_ENCODERS
01029 AVCodec ffv1_encoder = {
01030     "ffv1",
01031     CODEC_TYPE_VIDEO,
01032     CODEC_ID_FFV1,
01033     sizeof(FFV1Context),
01034     encode_init,
01035     encode_frame,
01036     common_end,
01037     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_NONE},
01038     .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
01039 };
01040 #endif

Generated on Fri Jan 9 13:44:29 2009 for libextractor by  doxygen 1.5.1