The common non inlined code goes in snow.c, the common inlined code in snow.h, tables move in snowdata.h (included only by snow.c)tags/n0.9
| @@ -341,12 +341,12 @@ OBJS-$(CONFIG_SIPR_DECODER) += sipr.o acelp_pitch_delay.o \ | |||
| OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o | |||
| OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o | |||
| OBJS-$(CONFIG_SMC_DECODER) += smc.o | |||
| OBJS-$(CONFIG_SNOW_DECODER) += snow.o rangecoder.o | |||
| OBJS-$(CONFIG_SNOW_ENCODER) += snow.o rangecoder.o motion_est.o \ | |||
| ratecontrol.o h263.o \ | |||
| mpegvideo.o error_resilience.o \ | |||
| ituh263enc.o mpegvideo_enc.o \ | |||
| mpeg12data.o | |||
| OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o rangecoder.o | |||
| OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o rangecoder.o \ | |||
| motion_est.o ratecontrol.o \ | |||
| h263.o mpegvideo.o \ | |||
| error_resilience.o ituh263enc.o \ | |||
| mpegvideo_enc.o mpeg12data.o | |||
| OBJS-$(CONFIG_SOL_DPCM_DECODER) += dpcm.o | |||
| OBJS-$(CONFIG_SP5X_DECODER) += sp5xdec.o mjpegdec.o mjpeg.o | |||
| OBJS-$(CONFIG_SRT_DECODER) += srtdec.o ass.o | |||
| @@ -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<<LOG2_MB_SIZE) | |||
| #define ENCODER_EXTRA_BITS 4 | |||
| #define HTAPS_MAX 8 | |||
| typedef struct x_and_coeff{ | |||
| int16_t x; | |||
| uint16_t coeff; | |||
| } x_and_coeff; | |||
| typedef struct SubBand{ | |||
| int level; | |||
| int stride; | |||
| int width; | |||
| int height; | |||
| int qlog; ///< log(qscale)/log[2^(1/6)] | |||
| DWTELEM *buf; | |||
| IDWTELEM *ibuf; | |||
| int buf_x_offset; | |||
| int buf_y_offset; | |||
| int stride_line; ///< Stride measured in lines, not pixels. | |||
| x_and_coeff * x_coeff; | |||
| struct SubBand *parent; | |||
| uint8_t state[/*7*2*/ 7 + 512][32]; | |||
| }SubBand; | |||
| typedef struct Plane{ | |||
| int width; | |||
| int height; | |||
| SubBand band[MAX_DECOMPOSITIONS][4]; | |||
| int htaps; | |||
| int8_t hcoeff[HTAPS_MAX/2]; | |||
| int diag_mc; | |||
| int fast_mc; | |||
| int last_htaps; | |||
| int8_t last_hcoeff[HTAPS_MAX/2]; | |||
| int last_diag_mc; | |||
| }Plane; | |||
| typedef struct SnowContext{ | |||
| AVClass *class; | |||
| AVCodecContext *avctx; | |||
| RangeCoder c; | |||
| DSPContext dsp; | |||
| DWTContext dwt; | |||
| AVFrame new_picture; | |||
| AVFrame input_picture; ///< new_picture with the internal linesizes | |||
| AVFrame current_picture; | |||
| AVFrame last_picture[MAX_REF_FRAMES]; | |||
| uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4]; | |||
| AVFrame mconly_picture; | |||
| // uint8_t q_context[16]; | |||
| uint8_t header_state[32]; | |||
| uint8_t block_state[128 + 32*128]; | |||
| int keyframe; | |||
| int always_reset; | |||
| int version; | |||
| int spatial_decomposition_type; | |||
| int last_spatial_decomposition_type; | |||
| int temporal_decomposition_type; | |||
| int spatial_decomposition_count; | |||
| int last_spatial_decomposition_count; | |||
| int temporal_decomposition_count; | |||
| int max_ref_frames; | |||
| int ref_frames; | |||
| int16_t (*ref_mvs[MAX_REF_FRAMES])[2]; | |||
| uint32_t *ref_scores[MAX_REF_FRAMES]; | |||
| DWTELEM *spatial_dwt_buffer; | |||
| IDWTELEM *spatial_idwt_buffer; | |||
| int colorspace_type; | |||
| int chroma_h_shift; | |||
| int chroma_v_shift; | |||
| int spatial_scalability; | |||
| int qlog; | |||
| int last_qlog; | |||
| int lambda; | |||
| int lambda2; | |||
| int pass1_rc; | |||
| int mv_scale; | |||
| int last_mv_scale; | |||
| int qbias; | |||
| int last_qbias; | |||
| #define QBIAS_SHIFT 3 | |||
| int b_width; | |||
| int b_height; | |||
| int block_max_depth; | |||
| int last_block_max_depth; | |||
| Plane plane[MAX_PLANES]; | |||
| BlockNode *block; | |||
| #define ME_CACHE_SIZE 1024 | |||
| int me_cache[ME_CACHE_SIZE]; | |||
| int me_cache_generation; | |||
| slice_buffer sb; | |||
| int memc_only; | |||
| MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX) | |||
| uint8_t *scratchbuf; | |||
| }SnowContext; | |||
| /* Tables */ | |||
| extern const uint8_t * const obmc_tab[4]; | |||
| #ifdef __sgi | |||
| // Avoid a name clash on SGI IRIX | |||
| #undef qexp | |||
| #endif | |||
| extern uint8_t qexp[QROOT]; | |||
| extern int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES]; | |||
| /* C bits used by mmx/sse2/altivec */ | |||
| @@ -75,4 +211,482 @@ static av_always_inline void snow_horizontal_compose_liftS_lead_out(int i, IDWTE | |||
| } | |||
| } | |||
| /* common code */ | |||
| int ff_snow_common_init(AVCodecContext *avctx); | |||
| int ff_snow_common_init_after_header(AVCodecContext *avctx); | |||
| void ff_snow_common_end(SnowContext *s); | |||
| void ff_snow_release_buffer(AVCodecContext *avctx); | |||
| void ff_snow_reset_contexts(SnowContext *s); | |||
| int ff_snow_alloc_blocks(SnowContext *s); | |||
| int ff_snow_frame_start(SnowContext *s); | |||
| void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, | |||
| int sx, int sy, int b_w, int b_h, BlockNode *block, | |||
| int plane_index, int w, int h); | |||
| /* common inline functions */ | |||
| //XXX doublecheck all of them should stay inlined | |||
| static inline void snow_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<<rem_depth; | |||
| BlockNode block; | |||
| int i,j; | |||
| block.color[0]= l; | |||
| block.color[1]= cb; | |||
| block.color[2]= cr; | |||
| block.mx= mx; | |||
| block.my= my; | |||
| block.ref= ref; | |||
| block.type= type; | |||
| block.level= level; | |||
| for(j=0; j<block_w; j++){ | |||
| for(i=0; i<block_w; i++){ | |||
| s->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<b_h; y++){ | |||
| //FIXME ugly misuse of obmc_stride | |||
| const uint8_t *obmc1= obmc + y*obmc_stride; | |||
| const uint8_t *obmc2= obmc1+ (obmc_stride>>1); | |||
| const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1); | |||
| const uint8_t *obmc4= obmc3+ (obmc_stride>>1); | |||
| for(x=0; x<b_w; x++){ | |||
| int v= obmc1[x] * block[3][x + y*src_stride] | |||
| +obmc2[x] * block[2][x + y*src_stride] | |||
| +obmc3[x] * block[1][x + y*src_stride] | |||
| +obmc4[x] * block[0][x + y*src_stride]; | |||
| v <<= 8 - LOG2_OBMC_MAX; | |||
| if(FRAC_BITS != 8){ | |||
| v >>= 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<FFMIN(h,block_w*(mb_y+1)); y++){ | |||
| for(x=0; x<w; x++){ | |||
| int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1)); | |||
| v >>= FRAC_BITS; | |||
| if(v&(~255)) v= ~(v>>31); | |||
| dst8[x + y*ref_stride]= v; | |||
| } | |||
| } | |||
| }else{ | |||
| for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){ | |||
| for(x=0; x<w; x++){ | |||
| buf[x + y*w]-= 128<<FRAC_BITS; | |||
| } | |||
| } | |||
| } | |||
| return; | |||
| } | |||
| for(mb_x=0; mb_x<=mb_w; mb_x++){ | |||
| add_yblock(s, 0, NULL, buf, dst8, obmc, | |||
| block_w*mb_x - block_w/2, | |||
| block_w*mb_y - block_w/2, | |||
| block_w, block_w, | |||
| w, h, | |||
| w, ref_stride, obmc_stride, | |||
| mb_x - 1, mb_y - 1, | |||
| add, 1, plane_index); | |||
| } | |||
| } | |||
| static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){ | |||
| const int mb_h= s->b_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<<rem_depth; | |||
| BlockNode block; | |||
| int i,j; | |||
| block.color[0]= l; | |||
| block.color[1]= cb; | |||
| block.color[2]= cr; | |||
| block.mx= mx; | |||
| block.my= my; | |||
| block.ref= ref; | |||
| block.type= type; | |||
| block.level= level; | |||
| for(j=0; j<block_w; j++){ | |||
| for(i=0; i<block_w; i++){ | |||
| s->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+1+i, 1); //1..10 | |||
| } | |||
| for(; i<e; i++){ | |||
| put_rac(c, state+1+9, 1); //1..10 | |||
| } | |||
| put_rac(c, state+1+FFMIN(i,9), 0); | |||
| for(i=e-1; 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<<log2 : 1; | |||
| assert(v>=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<<log2 : 1; | |||
| int v=0; | |||
| assert(log2>=-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)<<i; | |||
| } | |||
| return v; | |||
| } | |||
| static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){ | |||
| const int w= b->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; y<h; y++){ | |||
| int v=0; | |||
| int lt=0, t=0, rt=0; | |||
| if(y && prev_xc->x == 0){ | |||
| rt= prev_xc->coeff; | |||
| } | |||
| for(x=0; x<w; x++){ | |||
| int p=0; | |||
| const int l= v; | |||
| lt= t; t= rt; | |||
| if(y){ | |||
| if(prev_xc->x <= 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 */ | |||
| @@ -0,0 +1,132 @@ | |||
| /* | |||
| * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at> | |||
| * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net> | |||
| * | |||
| * This file is part of Libav. | |||
| * | |||
| * Libav is free software; you can redistribute it and/or | |||
| * modify it under the terms of the GNU Lesser General Public | |||
| * License as published by the Free Software Foundation; either | |||
| * version 2.1 of the License, or (at your option) any later version. | |||
| * | |||
| * Libav is distributed in the hope that it will be useful, | |||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
| * Lesser General Public License for more details. | |||
| * | |||
| * You should have received a copy of the GNU Lesser General Public | |||
| * License along with Libav; if not, write to the Free Software | |||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #ifndef AVCODEC_SNOWDATA_H | |||
| #define AVCODEC_SNOWDATA_H | |||
| #include "snow.h" | |||
| static const uint8_t obmc32[1024]={ | |||
| 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, | |||
| 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0, | |||
| 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0, | |||
| 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0, | |||
| 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4, | |||
| 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4, | |||
| 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4, | |||
| 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4, | |||
| 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4, | |||
| 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4, | |||
| 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4, | |||
| 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4, | |||
| 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8, | |||
| 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8, | |||
| 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8, | |||
| 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8, | |||
| 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8, | |||
| 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8, | |||
| 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8, | |||
| 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8, | |||
| 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4, | |||
| 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4, | |||
| 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4, | |||
| 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4, | |||
| 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4, | |||
| 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4, | |||
| 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4, | |||
| 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4, | |||
| 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0, | |||
| 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0, | |||
| 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0, | |||
| 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, | |||
| //error:0.000020 | |||
| }; | |||
| static const uint8_t obmc16[256]={ | |||
| 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0, | |||
| 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4, | |||
| 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4, | |||
| 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8, | |||
| 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8, | |||
| 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12, | |||
| 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12, | |||
| 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16, | |||
| 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16, | |||
| 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12, | |||
| 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12, | |||
| 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8, | |||
| 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8, | |||
| 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4, | |||
| 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4, | |||
| 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0, | |||
| //error:0.000015 | |||
| }; | |||
| //linear *64 | |||
| static const uint8_t obmc8[64]={ | |||
| 4, 12, 20, 28, 28, 20, 12, 4, | |||
| 12, 36, 60, 84, 84, 60, 36, 12, | |||
| 20, 60,100,140,140,100, 60, 20, | |||
| 28, 84,140,196,196,140, 84, 28, | |||
| 28, 84,140,196,196,140, 84, 28, | |||
| 20, 60,100,140,140,100, 60, 20, | |||
| 12, 36, 60, 84, 84, 60, 36, 12, | |||
| 4, 12, 20, 28, 28, 20, 12, 4, | |||
| //error:0.000000 | |||
| }; | |||
| //linear *64 | |||
| static const uint8_t obmc4[16]={ | |||
| 16, 48, 48, 16, | |||
| 48,144,144, 48, | |||
| 48,144,144, 48, | |||
| 16, 48, 48, 16, | |||
| //error:0.000000 | |||
| }; | |||
| const int8_t quant3bA[256]={ | |||
| 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, | |||
| }; | |||
| const uint8_t * const obmc_tab[4]= { | |||
| obmc32, obmc16, obmc8, obmc4 | |||
| }; | |||
| /* runtime generated tables */ | |||
| uint8_t qexp[QROOT]; | |||
| int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES]; | |||
| #endif /* AVCODEC_SNOW_H */ | |||
| @@ -0,0 +1,546 @@ | |||
| /* | |||
| * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at> | |||
| * | |||
| * This file is part of Libav. | |||
| * | |||
| * Libav is free software; you can redistribute it and/or | |||
| * modify it under the terms of the GNU Lesser General Public | |||
| * License as published by the Free Software Foundation; either | |||
| * version 2.1 of the License, or (at your option) any later version. | |||
| * | |||
| * Libav is distributed in the hope that it will be useful, | |||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
| * Lesser General Public License for more details. | |||
| * | |||
| * You should have received a copy of the GNU Lesser General Public | |||
| * License along with Libav; if not, write to the Free Software | |||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #include "libavutil/intmath.h" | |||
| #include "libavutil/log.h" | |||
| #include "libavutil/opt.h" | |||
| #include "avcodec.h" | |||
| #include "dsputil.h" | |||
| #include "dwt.h" | |||
| #include "snow.h" | |||
| #include "rangecoder.h" | |||
| #include "mathops.h" | |||
| #include "mpegvideo.h" | |||
| #include "h263.h" | |||
| #undef NDEBUG | |||
| #include <assert.h> | |||
| static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, 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]; | |||
| 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<FFMIN(h,block_w*(mb_y+1)); y++){ | |||
| // DWTELEM * line = slice_buffer_get_line(sb, y); | |||
| IDWTELEM * line = sb->line[y]; | |||
| for(x=0; x<w; x++){ | |||
| // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1)); | |||
| int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1)); | |||
| v >>= FRAC_BITS; | |||
| if(v&(~255)) v= ~(v>>31); | |||
| dst8[x + y*ref_stride]= v; | |||
| } | |||
| } | |||
| }else{ | |||
| for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){ | |||
| // DWTELEM * line = slice_buffer_get_line(sb, y); | |||
| IDWTELEM * line = sb->line[y]; | |||
| for(x=0; x<w; x++){ | |||
| line[x] -= 128 << FRAC_BITS; | |||
| // buf[x + y*w]-= 128<<FRAC_BITS; | |||
| } | |||
| } | |||
| } | |||
| return; | |||
| } | |||
| for(mb_x=0; mb_x<=mb_w; mb_x++){ | |||
| add_yblock(s, 1, sb, old_buffer, dst8, obmc, | |||
| block_w*mb_x - block_w/2, | |||
| block_w*mb_y - block_w/2, | |||
| block_w, block_w, | |||
| w, h, | |||
| w, ref_stride, obmc_stride, | |||
| mb_x - 1, mb_y - 1, | |||
| add, 0, plane_index); | |||
| } | |||
| } | |||
| static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){ | |||
| const int w= b->width; | |||
| int y; | |||
| const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16); | |||
| int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); | |||
| int qadd= (s->qbias*qmul)>>QBIAS_SHIFT; | |||
| int new_index = 0; | |||
| if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){ | |||
| qadd= 0; | |||
| qmul= 1<<QEXPSHIFT; | |||
| } | |||
| /* If we are on the second or later slice, restore our index. */ | |||
| if (start_y != 0) | |||
| new_index = save_state[0]; | |||
| for(y=start_y; y<h; y++){ | |||
| int x = 0; | |||
| int v; | |||
| IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset; | |||
| memset(line, 0, b->width*sizeof(IDWTELEM)); | |||
| v = b->x_coeff[new_index].coeff; | |||
| x = b->x_coeff[new_index++].x; | |||
| while(x < w){ | |||
| register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT; | |||
| register int u= -(v&1); | |||
| line[x] = (t^u) - u; | |||
| v = b->x_coeff[new_index].coeff; | |||
| x = b->x_coeff[new_index++].x; | |||
| } | |||
| } | |||
| /* Save our variables for the next slice. */ | |||
| save_state[0] = new_index; | |||
| return; | |||
| } | |||
| static void decode_q_branch(SnowContext *s, int level, int x, int y){ | |||
| 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; | |||
| int trx= (x+1)<<rem_depth; | |||
| const BlockNode *left = x ? &s->block[index-1] : &null_block; | |||
| const BlockNode *top = y ? &s->block[index-w] : &null_block; | |||
| const BlockNode *tl = y && x ? &s->block[index-w-1] : left; | |||
| const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt | |||
| int s_context= 2*left->level + 2*top->level + tl->level + tr->level; | |||
| if(s->keyframe){ | |||
| set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA); | |||
| return; | |||
| } | |||
| if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){ | |||
| int type, mx, my; | |||
| int l = left->color[0]; | |||
| int cb= left->color[1]; | |||
| int cr= left->color[2]; | |||
| int ref = 0; | |||
| int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref); | |||
| int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx)); | |||
| int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my)); | |||
| type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0; | |||
| if(type){ | |||
| pred_mv(s, &mx, &my, 0, left, top, tr); | |||
| l += get_symbol(&s->c, &s->block_state[32], 1); | |||
| cb+= get_symbol(&s->c, &s->block_state[64], 1); | |||
| cr+= get_symbol(&s->c, &s->block_state[96], 1); | |||
| }else{ | |||
| if(s->ref_frames > 1) | |||
| ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0); | |||
| pred_mv(s, &mx, &my, ref, left, top, tr); | |||
| mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1); | |||
| my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1); | |||
| } | |||
| set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type); | |||
| }else{ | |||
| decode_q_branch(s, level+1, 2*x+0, 2*y+0); | |||
| decode_q_branch(s, level+1, 2*x+1, 2*y+0); | |||
| decode_q_branch(s, level+1, 2*x+0, 2*y+1); | |||
| decode_q_branch(s, level+1, 2*x+1, 2*y+1); | |||
| } | |||
| } | |||
| static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){ | |||
| const int w= b->width; | |||
| const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16); | |||
| const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); | |||
| const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT; | |||
| int x,y; | |||
| if(s->qlog == LOSSLESS_QLOG) return; | |||
| for(y=start_y; y<end_y; y++){ | |||
| // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride)); | |||
| IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset; | |||
| for(x=0; x<w; x++){ | |||
| int i= line[x]; | |||
| if(i<0){ | |||
| line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias | |||
| }else if(i>0){ | |||
| line[x]= (( i*qmul + qadd)>>(QEXPSHIFT)); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){ | |||
| const int w= b->width; | |||
| int x,y; | |||
| IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning | |||
| IDWTELEM * prev; | |||
| if (start_y != 0) | |||
| line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset; | |||
| for(y=start_y; y<end_y; y++){ | |||
| prev = line; | |||
| // line = slice_buffer_get_line_from_address(sb, src + (y * stride)); | |||
| line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset; | |||
| for(x=0; x<w; x++){ | |||
| if(x){ | |||
| if(use_median){ | |||
| if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]); | |||
| else line[x] += line[x - 1]; | |||
| }else{ | |||
| if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]); | |||
| else line[x] += line[x - 1]; | |||
| } | |||
| }else{ | |||
| if(y) line[x] += prev[x]; | |||
| } | |||
| } | |||
| } | |||
| } | |||
| static void decode_qlogs(SnowContext *s){ | |||
| int plane_index, level, orientation; | |||
| for(plane_index=0; plane_index<3; plane_index++){ | |||
| for(level=0; level<s->spatial_decomposition_count; level++){ | |||
| for(orientation=level ? 1:0; orientation<4; orientation++){ | |||
| int q; | |||
| if (plane_index==2) q= s->plane[1].band[level][orientation].qlog; | |||
| else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog; | |||
| else q= get_symbol(&s->c, s->header_state, 1); | |||
| s->plane[plane_index].band[level][orientation].qlog= q; | |||
| } | |||
| } | |||
| } | |||
| } | |||
| #define GET_S(dst, check) \ | |||
| tmp= get_symbol(&s->c, s->header_state, 0);\ | |||
| if(!(check)){\ | |||
| av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\ | |||
| return -1;\ | |||
| }\ | |||
| dst= tmp; | |||
| static int decode_header(SnowContext *s){ | |||
| int plane_index, tmp; | |||
| uint8_t kstate[32]; | |||
| memset(kstate, MID_STATE, sizeof(kstate)); | |||
| s->keyframe= get_rac(&s->c, kstate); | |||
| if(s->keyframe || s->always_reset){ | |||
| ff_snow_reset_contexts(s); | |||
| s->spatial_decomposition_type= | |||
| s->qlog= | |||
| s->qbias= | |||
| s->mv_scale= | |||
| s->block_max_depth= 0; | |||
| } | |||
| if(s->keyframe){ | |||
| GET_S(s->version, tmp <= 0U) | |||
| s->always_reset= get_rac(&s->c, s->header_state); | |||
| s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0); | |||
| s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0); | |||
| GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS) | |||
| s->colorspace_type= get_symbol(&s->c, s->header_state, 0); | |||
| s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0); | |||
| s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0); | |||
| s->spatial_scalability= get_rac(&s->c, s->header_state); | |||
| // s->rate_scalability= get_rac(&s->c, s->header_state); | |||
| GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES) | |||
| s->max_ref_frames++; | |||
| decode_qlogs(s); | |||
| } | |||
| if(!s->keyframe){ | |||
| if(get_rac(&s->c, s->header_state)){ | |||
| for(plane_index=0; plane_index<2; plane_index++){ | |||
| int htaps, i, sum=0; | |||
| Plane *p= &s->plane[plane_index]; | |||
| p->diag_mc= get_rac(&s->c, s->header_state); | |||
| htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2; | |||
| if((unsigned)htaps > HTAPS_MAX || htaps==0) | |||
| return -1; | |||
| p->htaps= htaps; | |||
| for(i= htaps/2; i; i--){ | |||
| p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1)); | |||
| sum += p->hcoeff[i]; | |||
| } | |||
| p->hcoeff[0]= 32-sum; | |||
| } | |||
| s->plane[2].diag_mc= s->plane[1].diag_mc; | |||
| s->plane[2].htaps = s->plane[1].htaps; | |||
| memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff)); | |||
| } | |||
| if(get_rac(&s->c, s->header_state)){ | |||
| GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS) | |||
| decode_qlogs(s); | |||
| } | |||
| } | |||
| s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1); | |||
| if(s->spatial_decomposition_type > 1U){ | |||
| av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type); | |||
| return -1; | |||
| } | |||
| if(FFMIN(s->avctx-> width>>s->chroma_h_shift, | |||
| s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){ | |||
| av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size", s->spatial_decomposition_count); | |||
| return -1; | |||
| } | |||
| s->qlog += get_symbol(&s->c, s->header_state, 1); | |||
| s->mv_scale += get_symbol(&s->c, s->header_state, 1); | |||
| s->qbias += get_symbol(&s->c, s->header_state, 1); | |||
| s->block_max_depth+= get_symbol(&s->c, s->header_state, 1); | |||
| if(s->block_max_depth > 1 || s->block_max_depth < 0){ | |||
| av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth); | |||
| s->block_max_depth= 0; | |||
| return -1; | |||
| } | |||
| return 0; | |||
| } | |||
| static av_cold int decode_init(AVCodecContext *avctx) | |||
| { | |||
| avctx->pix_fmt= PIX_FMT_YUV420P; | |||
| ff_snow_common_init(avctx); | |||
| return 0; | |||
| } | |||
| static void decode_blocks(SnowContext *s){ | |||
| int x, y; | |||
| int w= s->b_width; | |||
| int h= s->b_height; | |||
| for(y=0; y<h; y++){ | |||
| for(x=0; x<w; x++){ | |||
| decode_q_branch(s, 0, x, y); | |||
| } | |||
| } | |||
| } | |||
| static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){ | |||
| const uint8_t *buf = avpkt->data; | |||
| int buf_size = avpkt->size; | |||
| SnowContext *s = avctx->priv_data; | |||
| RangeCoder * const c= &s->c; | |||
| int bytes_read; | |||
| AVFrame *picture = data; | |||
| int level, orientation, plane_index; | |||
| ff_init_range_decoder(c, buf, buf_size); | |||
| ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); | |||
| s->current_picture.pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P | |||
| if(decode_header(s)<0) | |||
| return -1; | |||
| ff_snow_common_init_after_header(avctx); | |||
| // realloc slice buffer for the case that spatial_decomposition_count changed | |||
| ff_slice_buffer_destroy(&s->sb); | |||
| ff_slice_buffer_init(&s->sb, s->plane[0].height, (MB_SIZE >> s->block_max_depth) + s->spatial_decomposition_count * 8 + 1, s->plane[0].width, s->spatial_idwt_buffer); | |||
| for(plane_index=0; plane_index<3; plane_index++){ | |||
| Plane *p= &s->plane[plane_index]; | |||
| p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40 | |||
| && p->hcoeff[1]==-10 | |||
| && p->hcoeff[2]==2; | |||
| } | |||
| ff_snow_alloc_blocks(s); | |||
| if(ff_snow_frame_start(s) < 0) | |||
| return -1; | |||
| //keyframe flag duplication mess FIXME | |||
| if(avctx->debug&FF_DEBUG_PICT_INFO) | |||
| av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog); | |||
| decode_blocks(s); | |||
| for(plane_index=0; plane_index<3; plane_index++){ | |||
| Plane *p= &s->plane[plane_index]; | |||
| int w= p->width; | |||
| int h= p->height; | |||
| int x, y; | |||
| int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */ | |||
| if(s->avctx->debug&2048){ | |||
| memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h); | |||
| predict_plane(s, s->spatial_idwt_buffer, plane_index, 1); | |||
| for(y=0; y<h; y++){ | |||
| for(x=0; x<w; x++){ | |||
| int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]; | |||
| s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v; | |||
| } | |||
| } | |||
| } | |||
| { | |||
| for(level=0; level<s->spatial_decomposition_count; level++){ | |||
| for(orientation=level ? 1 : 0; orientation<4; orientation++){ | |||
| SubBand *b= &p->band[level][orientation]; | |||
| unpack_coeffs(s, b, b->parent, orientation); | |||
| } | |||
| } | |||
| } | |||
| { | |||
| const int mb_h= s->b_height << s->block_max_depth; | |||
| const int block_size = MB_SIZE >> s->block_max_depth; | |||
| const int block_w = plane_index ? block_size/2 : block_size; | |||
| int mb_y; | |||
| DWTCompose cs[MAX_DECOMPOSITIONS]; | |||
| int yd=0, yq=0; | |||
| int y; | |||
| int end_y; | |||
| ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count); | |||
| for(mb_y=0; mb_y<=mb_h; mb_y++){ | |||
| int slice_starty = block_w*mb_y; | |||
| int slice_h = block_w*(mb_y+1); | |||
| if (!(s->keyframe || s->avctx->debug&512)){ | |||
| slice_starty = FFMAX(0, slice_starty - (block_w >> 1)); | |||
| slice_h -= (block_w >> 1); | |||
| } | |||
| for(level=0; level<s->spatial_decomposition_count; level++){ | |||
| for(orientation=level ? 1 : 0; orientation<4; orientation++){ | |||
| SubBand *b= &p->band[level][orientation]; | |||
| int start_y; | |||
| int end_y; | |||
| int our_mb_start = mb_y; | |||
| int our_mb_end = (mb_y + 1); | |||
| const int extra= 3; | |||
| start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0); | |||
| end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra); | |||
| if (!(s->keyframe || s->avctx->debug&512)){ | |||
| start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level))); | |||
| end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level))); | |||
| } | |||
| start_y = FFMIN(b->height, start_y); | |||
| end_y = FFMIN(b->height, end_y); | |||
| if (start_y != end_y){ | |||
| if (orientation == 0){ | |||
| SubBand * correlate_band = &p->band[0][0]; | |||
| int correlate_end_y = FFMIN(b->height, end_y + 1); | |||
| int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0)); | |||
| decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]); | |||
| correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y); | |||
| dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y); | |||
| } | |||
| else | |||
| decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]); | |||
| } | |||
| } | |||
| } | |||
| for(; yd<slice_h; yd+=4){ | |||
| ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd); | |||
| } | |||
| if(s->qlog == LOSSLESS_QLOG){ | |||
| for(; yq<slice_h && yq<h; yq++){ | |||
| IDWTELEM * line = slice_buffer_get_line(&s->sb, yq); | |||
| for(x=0; x<w; x++){ | |||
| line[x] <<= FRAC_BITS; | |||
| } | |||
| } | |||
| } | |||
| predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y); | |||
| y = FFMIN(p->height, slice_starty); | |||
| end_y = FFMIN(p->height, slice_h); | |||
| while(y < end_y) | |||
| ff_slice_buffer_release(&s->sb, y++); | |||
| } | |||
| ff_slice_buffer_flush(&s->sb); | |||
| } | |||
| } | |||
| emms_c(); | |||
| ff_snow_release_buffer(avctx); | |||
| if(!(s->avctx->debug&2048)) | |||
| *picture= s->current_picture; | |||
| else | |||
| *picture= s->mconly_picture; | |||
| *data_size = sizeof(AVFrame); | |||
| bytes_read= c->bytestream - c->bytestream_start; | |||
| if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME | |||
| return bytes_read; | |||
| } | |||
| static av_cold int decode_end(AVCodecContext *avctx) | |||
| { | |||
| SnowContext *s = avctx->priv_data; | |||
| ff_slice_buffer_destroy(&s->sb); | |||
| ff_snow_common_end(s); | |||
| return 0; | |||
| } | |||
| AVCodec ff_snow_decoder = { | |||
| .name = "snow", | |||
| .type = AVMEDIA_TYPE_VIDEO, | |||
| .id = CODEC_ID_SNOW, | |||
| .priv_data_size = sizeof(SnowContext), | |||
| .init = decode_init, | |||
| .close = decode_end, | |||
| .decode = decode_frame, | |||
| .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/, | |||
| .long_name = NULL_IF_CONFIG_SMALL("Snow"), | |||
| }; | |||