X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fsnow.h;h=3a5cdc71752f38b583433920d3d95adf3e46bdf7;hb=5c15b78e4a47c46de03d5e7291ed19a717f36885;hp=7d847e4b37f1a5d17187e0dba260cebcd0cade39;hpb=1889c6724a449b2e4826612d3b72efc76dbfb713;p=ffmpeg diff --git a/libavcodec/snow.h b/libavcodec/snow.h index 7d847e4b37f..3a5cdc71752 100644 --- a/libavcodec/snow.h +++ b/libavcodec/snow.h @@ -25,6 +25,10 @@ #include "dsputil.h" #include "dwt.h" +#include "rangecoder.h" +#include "mathops.h" +#include "mpegvideo.h" + #define MID_STATE 128 #define MAX_PLANES 4 @@ -36,6 +40,138 @@ #define LOG2_OBMC_MAX 8 #define OBMC_MAX (1<<(LOG2_OBMC_MAX)) +typedef struct BlockNode{ + int16_t mx; + int16_t my; + uint8_t ref; + uint8_t color[3]; + uint8_t type; +//#define TYPE_SPLIT 1 +#define BLOCK_INTRA 1 +#define BLOCK_OPT 2 +//#define TYPE_NOCOLOR 4 + uint8_t level; //FIXME merge into type? +}BlockNode; + +static const BlockNode null_block= { //FIXME add border maybe + .color= {128,128,128}, + .mx= 0, + .my= 0, + .ref= 0, + .type= 0, + .level= 0, +}; + +#define LOG2_MB_SIZE 4 +#define MB_SIZE (1<b_width << s->block_max_depth; + const int rem_depth= s->block_max_depth - level; + const int index= (x + y*w) << rem_depth; + const int block_w= 1<block[index + i + j*w]= block; + } + } +} + +static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref, + const BlockNode *left, const BlockNode *top, const BlockNode *tr){ + if(s->ref_frames == 1){ + *mx = mid_pred(left->mx, top->mx, tr->mx); + *my = mid_pred(left->my, top->my, tr->my); + }else{ + const int *scale = scale_mv_ref[ref]; + *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8, + (top ->mx * scale[top ->ref] + 128) >>8, + (tr ->mx * scale[tr ->ref] + 128) >>8); + *my = mid_pred((left->my * scale[left->ref] + 128) >>8, + (top ->my * scale[top ->ref] + 128) >>8, + (tr ->my * scale[tr ->ref] + 128) >>8); + } +} + +static av_always_inline int same_block(BlockNode *a, BlockNode *b){ + if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){ + return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2])); + }else{ + return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA)); + } +} + +//FIXME name cleanup (b_w, block_w, b_width stuff) +//XXX should we really inline it? +static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){ + const int b_width = s->b_width << s->block_max_depth; + const int b_height= s->b_height << s->block_max_depth; + const int b_stride= b_width; + BlockNode *lt= &s->block[b_x + b_y*b_stride]; + BlockNode *rt= lt+1; + BlockNode *lb= lt+b_stride; + BlockNode *rb= lb+1; + uint8_t *block[4]; + int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride; + uint8_t *tmp = s->scratchbuf; + uint8_t *ptmp; + int x,y; + + if(b_x<0){ + lt= rt; + lb= rb; + }else if(b_x + 1 >= b_width){ + rt= lt; + rb= lb; + } + if(b_y<0){ + lt= lb; + rt= rb; + }else if(b_y + 1 >= b_height){ + lb= lt; + rb= rt; + } + + if(src_x<0){ //FIXME merge with prev & always round internal width up to *16 + obmc -= src_x; + b_w += src_x; + if(!sliced && !offset_dst) + dst -= src_x; + src_x=0; + }else if(src_x + b_w > w){ + b_w = w - src_x; + } + if(src_y<0){ + obmc -= src_y*obmc_stride; + b_h += src_y; + if(!sliced && !offset_dst) + dst -= src_y*dst_stride; + src_y=0; + }else if(src_y + b_h> h){ + b_h = h - src_y; + } + + if(b_w<=0 || b_h<=0) return; + + assert(src_stride > 2*MB_SIZE + 5); + + if(!sliced && offset_dst) + dst += src_x + src_y*dst_stride; + dst8+= src_x + src_y*src_stride; +// src += src_x + src_y*src_stride; + + ptmp= tmp + 3*tmp_step; + block[0]= ptmp; + ptmp+=tmp_step; + ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h); + + if(same_block(lt, rt)){ + block[1]= block[0]; + }else{ + block[1]= ptmp; + ptmp+=tmp_step; + ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h); + } + + if(same_block(lt, lb)){ + block[2]= block[0]; + }else if(same_block(rt, lb)){ + block[2]= block[1]; + }else{ + block[2]= ptmp; + ptmp+=tmp_step; + ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h); + } + + if(same_block(lt, rb) ){ + block[3]= block[0]; + }else if(same_block(rt, rb)){ + block[3]= block[1]; + }else if(same_block(lb, rb)){ + block[3]= block[2]; + }else{ + block[3]= ptmp; + ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h); + } + if(sliced){ + s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8); + }else{ + for(y=0; y>1); + const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1); + const uint8_t *obmc4= obmc3+ (obmc_stride>>1); + for(x=0; x>= 8 - FRAC_BITS; + } + if(add){ + v += dst[x + y*dst_stride]; + v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS; + if(v&(~255)) v= ~(v>>31); + dst8[x + y*src_stride] = v; + }else{ + dst[x + y*dst_stride] -= v; + } + } + } + } +} + +static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){ + Plane *p= &s->plane[plane_index]; + const int mb_w= s->b_width << s->block_max_depth; + const int mb_h= s->b_height << s->block_max_depth; + int x, y, mb_x; + int block_size = MB_SIZE >> s->block_max_depth; + int block_w = plane_index ? block_size/2 : block_size; + const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth]; + const int obmc_stride= plane_index ? block_size : 2*block_size; + int ref_stride= s->current_picture.linesize[plane_index]; + uint8_t *dst8= s->current_picture.data[plane_index]; + int w= p->width; + int h= p->height; + + if(s->keyframe || (s->avctx->debug&512)){ + if(mb_y==mb_h) + return; + + if(add){ + for(y=block_w*mb_y; y>= FRAC_BITS; + if(v&(~255)) v= ~(v>>31); + dst8[x + y*ref_stride]= v; + } + } + }else{ + for(y=block_w*mb_y; yb_height << s->block_max_depth; + int mb_y; + for(mb_y=0; mb_y<=mb_h; mb_y++) + predict_slice(s, buf, plane_index, add, mb_y); +} + +static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){ + const int w= s->b_width << s->block_max_depth; + const int rem_depth= s->block_max_depth - level; + const int index= (x + y*w) << rem_depth; + const int block_w= 1<block[index + i + j*w]= block; + } + } +} + +static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){ + const int offset[3]= { + y*c-> stride + x, + ((y*c->uvstride + x)>>1), + ((y*c->uvstride + x)>>1), + }; + int i; + for(i=0; i<3; i++){ + c->src[0][i]= src [i]; + c->ref[0][i]= ref [i] + offset[i]; + } + assert(!ref_index); +} + + +/* bitstream functions */ + +extern const int8_t quant3bA[256]; + +#define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0 + +static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){ + int i; + + if(v){ + const int a= FFABS(v); + const int e= av_log2(a); + const int el= FFMIN(e, 10); + put_rac(c, state+0, 0); + + for(i=0; i=el; i--){ + put_rac(c, state+22+9, (a>>i)&1); //22..31 + } + for(; i>=0; i--){ + put_rac(c, state+22+i, (a>>i)&1); //22..31 + } + + if(is_signed) + put_rac(c, state+11 + el, v < 0); //11..21 + }else{ + put_rac(c, state+0, 1); + } +} + +static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){ + if(get_rac(c, state+0)) + return 0; + else{ + int i, e, a; + e= 0; + while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10 + e++; + } + + a= 1; + for(i=e-1; i>=0; i--){ + a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31 + } + + e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21 + return (a^e)-e; + } +} + +static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){ + int i; + int r= log2>=0 ? 1<=0); + assert(log2>=-4); + + while(v >= r){ + put_rac(c, state+4+log2, 1); + v -= r; + log2++; + if(log2>0) r+=r; + } + put_rac(c, state+4+log2, 0); + + for(i=log2-1; i>=0; i--){ + put_rac(c, state+31-i, (v>>i)&1); + } +} + +static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){ + int i; + int r= log2>=0 ? 1<=-4); + + while(get_rac(c, state+4+log2)){ + v+= r; + log2++; + if(log2>0) r+=r; + } + + for(i=log2-1; i>=0; i--){ + v+= get_rac(c, state+31-i)<width; + const int h= b->height; + int x,y; + + int run, runs; + x_and_coeff *xc= b->x_coeff; + x_and_coeff *prev_xc= NULL; + x_and_coeff *prev2_xc= xc; + x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL; + x_and_coeff *prev_parent_xc= parent_xc; + + runs= get_symbol2(&s->c, b->state[30], 0); + if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3); + else run= INT_MAX; + + for(y=0; yx == 0){ + rt= prev_xc->coeff; + } + for(x=0; xx <= x) + prev_xc++; + if(prev_xc->x == x + 1) + rt= prev_xc->coeff; + else + rt=0; + } + if(parent_xc){ + if(x>>1 > parent_xc->x){ + parent_xc++; + } + if(x>>1 == parent_xc->x){ + p= parent_xc->coeff; + } + } + if(/*ll|*/l|lt|t|rt|p){ + int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1)); + + v=get_rac(&s->c, &b->state[0][context]); + if(v){ + v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1); + v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]); + + xc->x=x; + (xc++)->coeff= v; + } + }else{ + if(!run){ + if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3); + else run= INT_MAX; + v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1); + v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]); + + xc->x=x; + (xc++)->coeff= v; + }else{ + int max_run; + run--; + v=0; + + if(y) max_run= FFMIN(run, prev_xc->x - x - 2); + else max_run= FFMIN(run, w-x-1); + if(parent_xc) + max_run= FFMIN(max_run, 2*parent_xc->x - x - 1); + x+= max_run; + run-= max_run; + } + } + } + (xc++)->x= w+1; //end marker + prev_xc= prev2_xc; + prev2_xc= xc; + + if(parent_xc){ + if(y&1){ + while(parent_xc->x != parent->width+1) + parent_xc++; + parent_xc++; + prev_parent_xc= parent_xc; + }else{ + parent_xc= prev_parent_xc; + } + } + } + + (xc++)->x= w+1; //end marker +} + #endif /* AVCODEC_SNOW_H */