libavcodec/snowenc.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include "libavutil/intmath.h"
00022 #include "libavutil/log.h"
00023 #include "libavutil/opt.h"
00024 #include "avcodec.h"
00025 #include "dsputil.h"
00026 #include "dwt.h"
00027 #include "snow.h"
00028 
00029 #include "rangecoder.h"
00030 #include "mathops.h"
00031 
00032 #include "mpegvideo.h"
00033 #include "h263.h"
00034 
00035 #undef NDEBUG
00036 #include <assert.h>
00037 
00038 #define QUANTIZE2 0
00039 
00040 #if QUANTIZE2==1
00041 #define Q2_STEP 8
00042 
00043 static void find_sse(SnowContext *s, Plane *p, int *score, int score_stride, IDWTELEM *r0, IDWTELEM *r1, int level, int orientation){
00044     SubBand *b= &p->band[level][orientation];
00045     int x, y;
00046     int xo=0;
00047     int yo=0;
00048     int step= 1 << (s->spatial_decomposition_count - level);
00049 
00050     if(orientation&1)
00051         xo= step>>1;
00052     if(orientation&2)
00053         yo= step>>1;
00054 
00055     //FIXME bias for nonzero ?
00056     //FIXME optimize
00057     memset(score, 0, sizeof(*score)*score_stride*((p->height + Q2_STEP-1)/Q2_STEP));
00058     for(y=0; y<p->height; y++){
00059         for(x=0; x<p->width; x++){
00060             int sx= (x-xo + step/2) / step / Q2_STEP;
00061             int sy= (y-yo + step/2) / step / Q2_STEP;
00062             int v= r0[x + y*p->width] - r1[x + y*p->width];
00063             assert(sx>=0 && sy>=0 && sx < score_stride);
00064             v= ((v+8)>>4)<<4;
00065             score[sx + sy*score_stride] += v*v;
00066             assert(score[sx + sy*score_stride] >= 0);
00067         }
00068     }
00069 }
00070 
00071 static void dequantize_all(SnowContext *s, Plane *p, IDWTELEM *buffer, int width, int height){
00072     int level, orientation;
00073 
00074     for(level=0; level<s->spatial_decomposition_count; level++){
00075         for(orientation=level ? 1 : 0; orientation<4; orientation++){
00076             SubBand *b= &p->band[level][orientation];
00077             IDWTELEM *dst= buffer + (b->ibuf - s->spatial_idwt_buffer);
00078 
00079             dequantize(s, b, dst, b->stride);
00080         }
00081     }
00082 }
00083 
00084 static void dwt_quantize(SnowContext *s, Plane *p, DWTELEM *buffer, int width, int height, int stride, int type){
00085     int level, orientation, ys, xs, x, y, pass;
00086     IDWTELEM best_dequant[height * stride];
00087     IDWTELEM idwt2_buffer[height * stride];
00088     const int score_stride= (width + 10)/Q2_STEP;
00089     int best_score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
00090     int score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
00091     int threshold= (s->m.lambda * s->m.lambda) >> 6;
00092 
00093     //FIXME pass the copy cleanly ?
00094 
00095 //    memcpy(dwt_buffer, buffer, height * stride * sizeof(DWTELEM));
00096     ff_spatial_dwt(buffer, width, height, stride, type, s->spatial_decomposition_count);
00097 
00098     for(level=0; level<s->spatial_decomposition_count; level++){
00099         for(orientation=level ? 1 : 0; orientation<4; orientation++){
00100             SubBand *b= &p->band[level][orientation];
00101             IDWTELEM *dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
00102              DWTELEM *src=       buffer + (b-> buf - s->spatial_dwt_buffer);
00103             assert(src == b->buf); // code does not depend on this but it is true currently
00104 
00105             quantize(s, b, dst, src, b->stride, s->qbias);
00106         }
00107     }
00108     for(pass=0; pass<1; pass++){
00109         if(s->qbias == 0) //keyframe
00110             continue;
00111         for(level=0; level<s->spatial_decomposition_count; level++){
00112             for(orientation=level ? 1 : 0; orientation<4; orientation++){
00113                 SubBand *b= &p->band[level][orientation];
00114                 IDWTELEM *dst= idwt2_buffer + (b->ibuf - s->spatial_idwt_buffer);
00115                 IDWTELEM *best_dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
00116 
00117                 for(ys= 0; ys<Q2_STEP; ys++){
00118                     for(xs= 0; xs<Q2_STEP; xs++){
00119                         memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
00120                         dequantize_all(s, p, idwt2_buffer, width, height);
00121                         ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
00122                         find_sse(s, p, best_score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
00123                         memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
00124                         for(y=ys; y<b->height; y+= Q2_STEP){
00125                             for(x=xs; x<b->width; x+= Q2_STEP){
00126                                 if(dst[x + y*b->stride]<0) dst[x + y*b->stride]++;
00127                                 if(dst[x + y*b->stride]>0) dst[x + y*b->stride]--;
00128                                 //FIXME try more than just --
00129                             }
00130                         }
00131                         dequantize_all(s, p, idwt2_buffer, width, height);
00132                         ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
00133                         find_sse(s, p, score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
00134                         for(y=ys; y<b->height; y+= Q2_STEP){
00135                             for(x=xs; x<b->width; x+= Q2_STEP){
00136                                 int score_idx= x/Q2_STEP + (y/Q2_STEP)*score_stride;
00137                                 if(score[score_idx] <= best_score[score_idx] + threshold){
00138                                     best_score[score_idx]= score[score_idx];
00139                                     if(best_dst[x + y*b->stride]<0) best_dst[x + y*b->stride]++;
00140                                     if(best_dst[x + y*b->stride]>0) best_dst[x + y*b->stride]--;
00141                                     //FIXME copy instead
00142                                 }
00143                             }
00144                         }
00145                     }
00146                 }
00147             }
00148         }
00149     }
00150     memcpy(s->spatial_idwt_buffer, best_dequant, height * stride * sizeof(IDWTELEM)); //FIXME work with that directly instead of copy at the end
00151 }
00152 
00153 #endif /* QUANTIZE2==1 */
00154 
00155 #if CONFIG_SNOW_ENCODER
00156 static av_cold int encode_init(AVCodecContext *avctx)
00157 {
00158     SnowContext *s = avctx->priv_data;
00159     int plane_index, ret;
00160 
00161     if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
00162         av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
00163                "Use vstrict=-2 / -strict -2 to use it anyway.\n");
00164         return -1;
00165     }
00166 
00167     if(avctx->prediction_method == DWT_97
00168        && (avctx->flags & CODEC_FLAG_QSCALE)
00169        && avctx->global_quality == 0){
00170         av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
00171         return -1;
00172     }
00173 
00174     s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
00175 
00176     s->mv_scale       = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
00177     s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
00178 
00179     for(plane_index=0; plane_index<3; plane_index++){
00180         s->plane[plane_index].diag_mc= 1;
00181         s->plane[plane_index].htaps= 6;
00182         s->plane[plane_index].hcoeff[0]=  40;
00183         s->plane[plane_index].hcoeff[1]= -10;
00184         s->plane[plane_index].hcoeff[2]=   2;
00185         s->plane[plane_index].fast_mc= 1;
00186     }
00187 
00188     if ((ret = ff_snow_common_init(avctx)) < 0) {
00189         ff_snow_common_end(avctx->priv_data);
00190         return ret;
00191     }
00192     ff_snow_alloc_blocks(s);
00193 
00194     s->version=0;
00195 
00196     s->m.avctx   = avctx;
00197     s->m.flags   = avctx->flags;
00198     s->m.bit_rate= avctx->bit_rate;
00199 
00200     s->m.me.temp      =
00201     s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
00202     s->m.me.map       = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
00203     s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
00204     s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
00205     ff_h263_encode_init(&s->m); //mv_penalty
00206 
00207     s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
00208 
00209     if(avctx->flags&CODEC_FLAG_PASS1){
00210         if(!avctx->stats_out)
00211             avctx->stats_out = av_mallocz(256);
00212     }
00213     if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
00214         if(ff_rate_control_init(&s->m) < 0)
00215             return -1;
00216     }
00217     s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
00218 
00219     avctx->coded_frame= &s->current_picture;
00220     switch(avctx->pix_fmt){
00221 //    case PIX_FMT_YUV444P:
00222 //    case PIX_FMT_YUV422P:
00223     case PIX_FMT_YUV420P:
00224 //     case PIX_FMT_GRAY8:
00225 //    case PIX_FMT_YUV411P:
00226 //    case PIX_FMT_YUV410P:
00227         s->colorspace_type= 0;
00228         break;
00229 /*    case PIX_FMT_RGB32:
00230         s->colorspace= 1;
00231         break;*/
00232     default:
00233         av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
00234         return -1;
00235     }
00236 //    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00237     s->chroma_h_shift= 1;
00238     s->chroma_v_shift= 1;
00239 
00240     ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
00241     ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
00242 
00243     s->avctx->get_buffer(s->avctx, &s->input_picture);
00244 
00245     if(s->avctx->me_method == ME_ITER){
00246         int i;
00247         int size= s->b_width * s->b_height << 2*s->block_max_depth;
00248         for(i=0; i<s->max_ref_frames; i++){
00249             s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
00250             s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
00251         }
00252     }
00253 
00254     return 0;
00255 }
00256 
00257 //near copy & paste from dsputil, FIXME
00258 static int pix_sum(uint8_t * pix, int line_size, int w)
00259 {
00260     int s, i, j;
00261 
00262     s = 0;
00263     for (i = 0; i < w; i++) {
00264         for (j = 0; j < w; j++) {
00265             s += pix[0];
00266             pix ++;
00267         }
00268         pix += line_size - w;
00269     }
00270     return s;
00271 }
00272 
00273 //near copy & paste from dsputil, FIXME
00274 static int pix_norm1(uint8_t * pix, int line_size, int w)
00275 {
00276     int s, i, j;
00277     uint32_t *sq = ff_squareTbl + 256;
00278 
00279     s = 0;
00280     for (i = 0; i < w; i++) {
00281         for (j = 0; j < w; j ++) {
00282             s += sq[pix[0]];
00283             pix ++;
00284         }
00285         pix += line_size - w;
00286     }
00287     return s;
00288 }
00289 
00290 //FIXME copy&paste
00291 #define P_LEFT P[1]
00292 #define P_TOP P[2]
00293 #define P_TOPRIGHT P[3]
00294 #define P_MEDIAN P[4]
00295 #define P_MV1 P[9]
00296 #define FLAG_QPEL   1 //must be 1
00297 
00298 static int encode_q_branch(SnowContext *s, int level, int x, int y){
00299     uint8_t p_buffer[1024];
00300     uint8_t i_buffer[1024];
00301     uint8_t p_state[sizeof(s->block_state)];
00302     uint8_t i_state[sizeof(s->block_state)];
00303     RangeCoder pc, ic;
00304     uint8_t *pbbak= s->c.bytestream;
00305     uint8_t *pbbak_start= s->c.bytestream_start;
00306     int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
00307     const int w= s->b_width  << s->block_max_depth;
00308     const int h= s->b_height << s->block_max_depth;
00309     const int rem_depth= s->block_max_depth - level;
00310     const int index= (x + y*w) << rem_depth;
00311     const int block_w= 1<<(LOG2_MB_SIZE - level);
00312     int trx= (x+1)<<rem_depth;
00313     int try= (y+1)<<rem_depth;
00314     const BlockNode *left  = x ? &s->block[index-1] : &null_block;
00315     const BlockNode *top   = y ? &s->block[index-w] : &null_block;
00316     const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
00317     const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
00318     const BlockNode *tl    = y && x ? &s->block[index-w-1] : left;
00319     const BlockNode *tr    = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
00320     int pl = left->color[0];
00321     int pcb= left->color[1];
00322     int pcr= left->color[2];
00323     int pmx, pmy;
00324     int mx=0, my=0;
00325     int l,cr,cb;
00326     const int stride= s->current_picture.linesize[0];
00327     const int uvstride= s->current_picture.linesize[1];
00328     uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y*  stride)*block_w,
00329                                 s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
00330                                 s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
00331     int P[10][2];
00332     int16_t last_mv[3][2];
00333     int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
00334     const int shift= 1+qpel;
00335     MotionEstContext *c= &s->m.me;
00336     int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
00337     int mx_context= av_log2(2*FFABS(left->mx - top->mx));
00338     int my_context= av_log2(2*FFABS(left->my - top->my));
00339     int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
00340     int ref, best_ref, ref_score, ref_mx, ref_my;
00341 
00342     assert(sizeof(s->block_state) >= 256);
00343     if(s->keyframe){
00344         set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
00345         return 0;
00346     }
00347 
00348 //    clip predictors / edge ?
00349 
00350     P_LEFT[0]= left->mx;
00351     P_LEFT[1]= left->my;
00352     P_TOP [0]= top->mx;
00353     P_TOP [1]= top->my;
00354     P_TOPRIGHT[0]= tr->mx;
00355     P_TOPRIGHT[1]= tr->my;
00356 
00357     last_mv[0][0]= s->block[index].mx;
00358     last_mv[0][1]= s->block[index].my;
00359     last_mv[1][0]= right->mx;
00360     last_mv[1][1]= right->my;
00361     last_mv[2][0]= bottom->mx;
00362     last_mv[2][1]= bottom->my;
00363 
00364     s->m.mb_stride=2;
00365     s->m.mb_x=
00366     s->m.mb_y= 0;
00367     c->skip= 0;
00368 
00369     assert(c->  stride ==   stride);
00370     assert(c->uvstride == uvstride);
00371 
00372     c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
00373     c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
00374     c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
00375     c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
00376 
00377     c->xmin = - x*block_w - 16+3;
00378     c->ymin = - y*block_w - 16+3;
00379     c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
00380     c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
00381 
00382     if(P_LEFT[0]     > (c->xmax<<shift)) P_LEFT[0]    = (c->xmax<<shift);
00383     if(P_LEFT[1]     > (c->ymax<<shift)) P_LEFT[1]    = (c->ymax<<shift);
00384     if(P_TOP[0]      > (c->xmax<<shift)) P_TOP[0]     = (c->xmax<<shift);
00385     if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
00386     if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
00387     if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
00388     if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
00389 
00390     P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
00391     P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
00392 
00393     if (!y) {
00394         c->pred_x= P_LEFT[0];
00395         c->pred_y= P_LEFT[1];
00396     } else {
00397         c->pred_x = P_MEDIAN[0];
00398         c->pred_y = P_MEDIAN[1];
00399     }
00400 
00401     score= INT_MAX;
00402     best_ref= 0;
00403     for(ref=0; ref<s->ref_frames; ref++){
00404         init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
00405 
00406         ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
00407                                          (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
00408 
00409         assert(ref_mx >= c->xmin);
00410         assert(ref_mx <= c->xmax);
00411         assert(ref_my >= c->ymin);
00412         assert(ref_my <= c->ymax);
00413 
00414         ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
00415         ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
00416         ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
00417         if(s->ref_mvs[ref]){
00418             s->ref_mvs[ref][index][0]= ref_mx;
00419             s->ref_mvs[ref][index][1]= ref_my;
00420             s->ref_scores[ref][index]= ref_score;
00421         }
00422         if(score > ref_score){
00423             score= ref_score;
00424             best_ref= ref;
00425             mx= ref_mx;
00426             my= ref_my;
00427         }
00428     }
00429     //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
00430 
00431   //  subpel search
00432     base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
00433     pc= s->c;
00434     pc.bytestream_start=
00435     pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
00436     memcpy(p_state, s->block_state, sizeof(s->block_state));
00437 
00438     if(level!=s->block_max_depth)
00439         put_rac(&pc, &p_state[4 + s_context], 1);
00440     put_rac(&pc, &p_state[1 + left->type + top->type], 0);
00441     if(s->ref_frames > 1)
00442         put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
00443     pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
00444     put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
00445     put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
00446     p_len= pc.bytestream - pc.bytestream_start;
00447     score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
00448 
00449     block_s= block_w*block_w;
00450     sum = pix_sum(current_data[0], stride, block_w);
00451     l= (sum + block_s/2)/block_s;
00452     iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
00453 
00454     block_s= block_w*block_w>>2;
00455     sum = pix_sum(current_data[1], uvstride, block_w>>1);
00456     cb= (sum + block_s/2)/block_s;
00457 //    iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
00458     sum = pix_sum(current_data[2], uvstride, block_w>>1);
00459     cr= (sum + block_s/2)/block_s;
00460 //    iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
00461 
00462     ic= s->c;
00463     ic.bytestream_start=
00464     ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
00465     memcpy(i_state, s->block_state, sizeof(s->block_state));
00466     if(level!=s->block_max_depth)
00467         put_rac(&ic, &i_state[4 + s_context], 1);
00468     put_rac(&ic, &i_state[1 + left->type + top->type], 1);
00469     put_symbol(&ic, &i_state[32],  l-pl , 1);
00470     put_symbol(&ic, &i_state[64], cb-pcb, 1);
00471     put_symbol(&ic, &i_state[96], cr-pcr, 1);
00472     i_len= ic.bytestream - ic.bytestream_start;
00473     iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
00474 
00475 //    assert(score==256*256*256*64-1);
00476     assert(iscore < 255*255*256 + s->lambda2*10);
00477     assert(iscore >= 0);
00478     assert(l>=0 && l<=255);
00479     assert(pl>=0 && pl<=255);
00480 
00481     if(level==0){
00482         int varc= iscore >> 8;
00483         int vard= score >> 8;
00484         if (vard <= 64 || vard < varc)
00485             c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
00486         else
00487             c->scene_change_score+= s->m.qscale;
00488     }
00489 
00490     if(level!=s->block_max_depth){
00491         put_rac(&s->c, &s->block_state[4 + s_context], 0);
00492         score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
00493         score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
00494         score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
00495         score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
00496         score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
00497 
00498         if(score2 < score && score2 < iscore)
00499             return score2;
00500     }
00501 
00502     if(iscore < score){
00503         pred_mv(s, &pmx, &pmy, 0, left, top, tr);
00504         memcpy(pbbak, i_buffer, i_len);
00505         s->c= ic;
00506         s->c.bytestream_start= pbbak_start;
00507         s->c.bytestream= pbbak + i_len;
00508         set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
00509         memcpy(s->block_state, i_state, sizeof(s->block_state));
00510         return iscore;
00511     }else{
00512         memcpy(pbbak, p_buffer, p_len);
00513         s->c= pc;
00514         s->c.bytestream_start= pbbak_start;
00515         s->c.bytestream= pbbak + p_len;
00516         set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
00517         memcpy(s->block_state, p_state, sizeof(s->block_state));
00518         return score;
00519     }
00520 }
00521 
00522 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
00523     const int w= s->b_width  << s->block_max_depth;
00524     const int rem_depth= s->block_max_depth - level;
00525     const int index= (x + y*w) << rem_depth;
00526     int trx= (x+1)<<rem_depth;
00527     BlockNode *b= &s->block[index];
00528     const BlockNode *left  = x ? &s->block[index-1] : &null_block;
00529     const BlockNode *top   = y ? &s->block[index-w] : &null_block;
00530     const BlockNode *tl    = y && x ? &s->block[index-w-1] : left;
00531     const BlockNode *tr    = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
00532     int pl = left->color[0];
00533     int pcb= left->color[1];
00534     int pcr= left->color[2];
00535     int pmx, pmy;
00536     int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
00537     int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
00538     int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
00539     int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
00540 
00541     if(s->keyframe){
00542         set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
00543         return;
00544     }
00545 
00546     if(level!=s->block_max_depth){
00547         if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
00548             put_rac(&s->c, &s->block_state[4 + s_context], 1);
00549         }else{
00550             put_rac(&s->c, &s->block_state[4 + s_context], 0);
00551             encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
00552             encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
00553             encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
00554             encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
00555             return;
00556         }
00557     }
00558     if(b->type & BLOCK_INTRA){
00559         pred_mv(s, &pmx, &pmy, 0, left, top, tr);
00560         put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
00561         put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
00562         put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
00563         put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
00564         set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
00565     }else{
00566         pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
00567         put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
00568         if(s->ref_frames > 1)
00569             put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
00570         put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
00571         put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
00572         set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
00573     }
00574 }
00575 
00576 static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
00577     int i, x2, y2;
00578     Plane *p= &s->plane[plane_index];
00579     const int block_size = MB_SIZE >> s->block_max_depth;
00580     const int block_w    = plane_index ? block_size/2 : block_size;
00581     const uint8_t *obmc  = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
00582     const int obmc_stride= plane_index ? block_size : 2*block_size;
00583     const int ref_stride= s->current_picture.linesize[plane_index];
00584     uint8_t *src= s-> input_picture.data[plane_index];
00585     IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
00586     const int b_stride = s->b_width << s->block_max_depth;
00587     const int w= p->width;
00588     const int h= p->height;
00589     int index= mb_x + mb_y*b_stride;
00590     BlockNode *b= &s->block[index];
00591     BlockNode backup= *b;
00592     int ab=0;
00593     int aa=0;
00594 
00595     b->type|= BLOCK_INTRA;
00596     b->color[plane_index]= 0;
00597     memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
00598 
00599     for(i=0; i<4; i++){
00600         int mb_x2= mb_x + (i &1) - 1;
00601         int mb_y2= mb_y + (i>>1) - 1;
00602         int x= block_w*mb_x2 + block_w/2;
00603         int y= block_w*mb_y2 + block_w/2;
00604 
00605         add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
00606                     x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
00607 
00608         for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
00609             for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
00610                 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
00611                 int obmc_v= obmc[index];
00612                 int d;
00613                 if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
00614                 if(x<0) obmc_v += obmc[index + block_w];
00615                 if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
00616                 if(x+block_w>w) obmc_v += obmc[index - block_w];
00617                 //FIXME precalculate this or simplify it somehow else
00618 
00619                 d = -dst[index] + (1<<(FRAC_BITS-1));
00620                 dst[index] = d;
00621                 ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
00622                 aa += obmc_v * obmc_v; //FIXME precalculate this
00623             }
00624         }
00625     }
00626     *b= backup;
00627 
00628     return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we should not need clipping
00629 }
00630 
00631 static inline int get_block_bits(SnowContext *s, int x, int y, int w){
00632     const int b_stride = s->b_width << s->block_max_depth;
00633     const int b_height = s->b_height<< s->block_max_depth;
00634     int index= x + y*b_stride;
00635     const BlockNode *b     = &s->block[index];
00636     const BlockNode *left  = x ? &s->block[index-1] : &null_block;
00637     const BlockNode *top   = y ? &s->block[index-b_stride] : &null_block;
00638     const BlockNode *tl    = y && x ? &s->block[index-b_stride-1] : left;
00639     const BlockNode *tr    = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
00640     int dmx, dmy;
00641 //  int mx_context= av_log2(2*FFABS(left->mx - top->mx));
00642 //  int my_context= av_log2(2*FFABS(left->my - top->my));
00643 
00644     if(x<0 || x>=b_stride || y>=b_height)
00645         return 0;
00646 /*
00647 1            0      0
00648 01X          1-2    1
00649 001XX        3-6    2-3
00650 0001XXX      7-14   4-7
00651 00001XXXX   15-30   8-15
00652 */
00653 //FIXME try accurate rate
00654 //FIXME intra and inter predictors if surrounding blocks are not the same type
00655     if(b->type & BLOCK_INTRA){
00656         return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
00657                    + av_log2(2*FFABS(left->color[1] - b->color[1]))
00658                    + av_log2(2*FFABS(left->color[2] - b->color[2])));
00659     }else{
00660         pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
00661         dmx-= b->mx;
00662         dmy-= b->my;
00663         return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
00664                     + av_log2(2*FFABS(dmy))
00665                     + av_log2(2*b->ref));
00666     }
00667 }
00668 
00669 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
00670     Plane *p= &s->plane[plane_index];
00671     const int block_size = MB_SIZE >> s->block_max_depth;
00672     const int block_w    = plane_index ? block_size/2 : block_size;
00673     const int obmc_stride= plane_index ? block_size : 2*block_size;
00674     const int ref_stride= s->current_picture.linesize[plane_index];
00675     uint8_t *dst= s->current_picture.data[plane_index];
00676     uint8_t *src= s->  input_picture.data[plane_index];
00677     IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
00678     uint8_t *cur = s->scratchbuf;
00679     uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
00680     const int b_stride = s->b_width << s->block_max_depth;
00681     const int b_height = s->b_height<< s->block_max_depth;
00682     const int w= p->width;
00683     const int h= p->height;
00684     int distortion;
00685     int rate= 0;
00686     const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
00687     int sx= block_w*mb_x - block_w/2;
00688     int sy= block_w*mb_y - block_w/2;
00689     int x0= FFMAX(0,-sx);
00690     int y0= FFMAX(0,-sy);
00691     int x1= FFMIN(block_w*2, w-sx);
00692     int y1= FFMIN(block_w*2, h-sy);
00693     int i,x,y;
00694 
00695     ff_snow_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
00696 
00697     for(y=y0; y<y1; y++){
00698         const uint8_t *obmc1= obmc_edged + y*obmc_stride;
00699         const IDWTELEM *pred1 = pred + y*obmc_stride;
00700         uint8_t *cur1 = cur + y*ref_stride;
00701         uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
00702         for(x=x0; x<x1; x++){
00703 #if FRAC_BITS >= LOG2_OBMC_MAX
00704             int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
00705 #else
00706             int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
00707 #endif
00708             v = (v + pred1[x]) >> FRAC_BITS;
00709             if(v&(~255)) v= ~(v>>31);
00710             dst1[x] = v;
00711         }
00712     }
00713 
00714     /* copy the regions where obmc[] = (uint8_t)256 */
00715     if(LOG2_OBMC_MAX == 8
00716         && (mb_x == 0 || mb_x == b_stride-1)
00717         && (mb_y == 0 || mb_y == b_height-1)){
00718         if(mb_x == 0)
00719             x1 = block_w;
00720         else
00721             x0 = block_w;
00722         if(mb_y == 0)
00723             y1 = block_w;
00724         else
00725             y0 = block_w;
00726         for(y=y0; y<y1; y++)
00727             memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
00728     }
00729 
00730     if(block_w==16){
00731         /* FIXME rearrange dsputil to fit 32x32 cmp functions */
00732         /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
00733         /* FIXME cmps overlap but do not cover the wavelet's whole support.
00734          * So improving the score of one block is not strictly guaranteed
00735          * to improve the score of the whole frame, thus iterative motion
00736          * estimation does not always converge. */
00737         if(s->avctx->me_cmp == FF_CMP_W97)
00738             distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
00739         else if(s->avctx->me_cmp == FF_CMP_W53)
00740             distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
00741         else{
00742             distortion = 0;
00743             for(i=0; i<4; i++){
00744                 int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
00745                 distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
00746             }
00747         }
00748     }else{
00749         assert(block_w==8);
00750         distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
00751     }
00752 
00753     if(plane_index==0){
00754         for(i=0; i<4; i++){
00755 /* ..RRr
00756  * .RXx.
00757  * rxx..
00758  */
00759             rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
00760         }
00761         if(mb_x == b_stride-2)
00762             rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
00763     }
00764     return distortion + rate*penalty_factor;
00765 }
00766 
00767 static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
00768     int i, y2;
00769     Plane *p= &s->plane[plane_index];
00770     const int block_size = MB_SIZE >> s->block_max_depth;
00771     const int block_w    = plane_index ? block_size/2 : block_size;
00772     const uint8_t *obmc  = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
00773     const int obmc_stride= plane_index ? block_size : 2*block_size;
00774     const int ref_stride= s->current_picture.linesize[plane_index];
00775     uint8_t *dst= s->current_picture.data[plane_index];
00776     uint8_t *src= s-> input_picture.data[plane_index];
00777     //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
00778     // const has only been removed from zero_dst to suppress a warning
00779     static IDWTELEM zero_dst[4096]; //FIXME
00780     const int b_stride = s->b_width << s->block_max_depth;
00781     const int w= p->width;
00782     const int h= p->height;
00783     int distortion= 0;
00784     int rate= 0;
00785     const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
00786 
00787     for(i=0; i<9; i++){
00788         int mb_x2= mb_x + (i%3) - 1;
00789         int mb_y2= mb_y + (i/3) - 1;
00790         int x= block_w*mb_x2 + block_w/2;
00791         int y= block_w*mb_y2 + block_w/2;
00792 
00793         add_yblock(s, 0, NULL, zero_dst, dst, obmc,
00794                    x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
00795 
00796         //FIXME find a cleaner/simpler way to skip the outside stuff
00797         for(y2= y; y2<0; y2++)
00798             memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
00799         for(y2= h; y2<y+block_w; y2++)
00800             memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
00801         if(x<0){
00802             for(y2= y; y2<y+block_w; y2++)
00803                 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
00804         }
00805         if(x+block_w > w){
00806             for(y2= y; y2<y+block_w; y2++)
00807                 memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
00808         }
00809 
00810         assert(block_w== 8 || block_w==16);
00811         distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
00812     }
00813 
00814     if(plane_index==0){
00815         BlockNode *b= &s->block[mb_x+mb_y*b_stride];
00816         int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
00817 
00818 /* ..RRRr
00819  * .RXXx.
00820  * .RXXx.
00821  * rxxx.
00822  */
00823         if(merged)
00824             rate = get_block_bits(s, mb_x, mb_y, 2);
00825         for(i=merged?4:0; i<9; i++){
00826             static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
00827             rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
00828         }
00829     }
00830     return distortion + rate*penalty_factor;
00831 }
00832 
00833 static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
00834     const int w= b->width;
00835     const int h= b->height;
00836     int x, y;
00837 
00838     if(1){
00839         int run=0;
00840         int runs[w*h];
00841         int run_index=0;
00842         int max_index;
00843 
00844         for(y=0; y<h; y++){
00845             for(x=0; x<w; x++){
00846                 int v, p=0;
00847                 int /*ll=0, */l=0, lt=0, t=0, rt=0;
00848                 v= src[x + y*stride];
00849 
00850                 if(y){
00851                     t= src[x + (y-1)*stride];
00852                     if(x){
00853                         lt= src[x - 1 + (y-1)*stride];
00854                     }
00855                     if(x + 1 < w){
00856                         rt= src[x + 1 + (y-1)*stride];
00857                     }
00858                 }
00859                 if(x){
00860                     l= src[x - 1 + y*stride];
00861                     /*if(x > 1){
00862                         if(orientation==1) ll= src[y + (x-2)*stride];
00863                         else               ll= src[x - 2 + y*stride];
00864                     }*/
00865                 }
00866                 if(parent){
00867                     int px= x>>1;
00868                     int py= y>>1;
00869                     if(px<b->parent->width && py<b->parent->height)
00870                         p= parent[px + py*2*stride];
00871                 }
00872                 if(!(/*ll|*/l|lt|t|rt|p)){
00873                     if(v){
00874                         runs[run_index++]= run;
00875                         run=0;
00876                     }else{
00877                         run++;
00878                     }
00879                 }
00880             }
00881         }
00882         max_index= run_index;
00883         runs[run_index++]= run;
00884         run_index=0;
00885         run= runs[run_index++];
00886 
00887         put_symbol2(&s->c, b->state[30], max_index, 0);
00888         if(run_index <= max_index)
00889             put_symbol2(&s->c, b->state[1], run, 3);
00890 
00891         for(y=0; y<h; y++){
00892             if(s->c.bytestream_end - s->c.bytestream < w*40){
00893                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00894                 return -1;
00895             }
00896             for(x=0; x<w; x++){
00897                 int v, p=0;
00898                 int /*ll=0, */l=0, lt=0, t=0, rt=0;
00899                 v= src[x + y*stride];
00900 
00901                 if(y){
00902                     t= src[x + (y-1)*stride];
00903                     if(x){
00904                         lt= src[x - 1 + (y-1)*stride];
00905                     }
00906                     if(x + 1 < w){
00907                         rt= src[x + 1 + (y-1)*stride];
00908                     }
00909                 }
00910                 if(x){
00911                     l= src[x - 1 + y*stride];
00912                     /*if(x > 1){
00913                         if(orientation==1) ll= src[y + (x-2)*stride];
00914                         else               ll= src[x - 2 + y*stride];
00915                     }*/
00916                 }
00917                 if(parent){
00918                     int px= x>>1;
00919                     int py= y>>1;
00920                     if(px<b->parent->width && py<b->parent->height)
00921                         p= parent[px + py*2*stride];
00922                 }
00923                 if(/*ll|*/l|lt|t|rt|p){
00924                     int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
00925 
00926                     put_rac(&s->c, &b->state[0][context], !!v);
00927                 }else{
00928                     if(!run){
00929                         run= runs[run_index++];
00930 
00931                         if(run_index <= max_index)
00932                             put_symbol2(&s->c, b->state[1], run, 3);
00933                         assert(v);
00934                     }else{
00935                         run--;
00936                         assert(!v);
00937                     }
00938                 }
00939                 if(v){
00940                     int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
00941                     int l2= 2*FFABS(l) + (l<0);
00942                     int t2= 2*FFABS(t) + (t<0);
00943 
00944                     put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
00945                     put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
00946                 }
00947             }
00948         }
00949     }
00950     return 0;
00951 }
00952 
00953 static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
00954 //    encode_subband_qtree(s, b, src, parent, stride, orientation);
00955 //    encode_subband_z0run(s, b, src, parent, stride, orientation);
00956     return encode_subband_c0run(s, b, src, parent, stride, orientation);
00957 //    encode_subband_dzr(s, b, src, parent, stride, orientation);
00958 }
00959 
00960 static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, const uint8_t *obmc_edged, int *best_rd){
00961     const int b_stride= s->b_width << s->block_max_depth;
00962     BlockNode *block= &s->block[mb_x + mb_y * b_stride];
00963     BlockNode backup= *block;
00964     unsigned value;
00965     int rd, index;
00966 
00967     assert(mb_x>=0 && mb_y>=0);
00968     assert(mb_x<b_stride);
00969 
00970     if(intra){
00971         block->color[0] = p[0];
00972         block->color[1] = p[1];
00973         block->color[2] = p[2];
00974         block->type |= BLOCK_INTRA;
00975     }else{
00976         index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
00977         value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
00978         if(s->me_cache[index] == value)
00979             return 0;
00980         s->me_cache[index]= value;
00981 
00982         block->mx= p[0];
00983         block->my= p[1];
00984         block->type &= ~BLOCK_INTRA;
00985     }
00986 
00987     rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
00988 
00989 //FIXME chroma
00990     if(rd < *best_rd){
00991         *best_rd= rd;
00992         return 1;
00993     }else{
00994         *block= backup;
00995         return 0;
00996     }
00997 }
00998 
00999 /* special case for int[2] args we discard afterwards,
01000  * fixes compilation problem with gcc 2.95 */
01001 static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, const uint8_t *obmc_edged, int *best_rd){
01002     int p[2] = {p0, p1};
01003     return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
01004 }
01005 
01006 static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
01007     const int b_stride= s->b_width << s->block_max_depth;
01008     BlockNode *block= &s->block[mb_x + mb_y * b_stride];
01009     BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
01010     unsigned value;
01011     int rd, index;
01012 
01013     assert(mb_x>=0 && mb_y>=0);
01014     assert(mb_x<b_stride);
01015     assert(((mb_x|mb_y)&1) == 0);
01016 
01017     index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
01018     value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
01019     if(s->me_cache[index] == value)
01020         return 0;
01021     s->me_cache[index]= value;
01022 
01023     block->mx= p0;
01024     block->my= p1;
01025     block->ref= ref;
01026     block->type &= ~BLOCK_INTRA;
01027     block[1]= block[b_stride]= block[b_stride+1]= *block;
01028 
01029     rd= get_4block_rd(s, mb_x, mb_y, 0);
01030 
01031 //FIXME chroma
01032     if(rd < *best_rd){
01033         *best_rd= rd;
01034         return 1;
01035     }else{
01036         block[0]= backup[0];
01037         block[1]= backup[1];
01038         block[b_stride]= backup[2];
01039         block[b_stride+1]= backup[3];
01040         return 0;
01041     }
01042 }
01043 
01044 static void iterative_me(SnowContext *s){
01045     int pass, mb_x, mb_y;
01046     const int b_width = s->b_width  << s->block_max_depth;
01047     const int b_height= s->b_height << s->block_max_depth;
01048     const int b_stride= b_width;
01049     int color[3];
01050 
01051     {
01052         RangeCoder r = s->c;
01053         uint8_t state[sizeof(s->block_state)];
01054         memcpy(state, s->block_state, sizeof(s->block_state));
01055         for(mb_y= 0; mb_y<s->b_height; mb_y++)
01056             for(mb_x= 0; mb_x<s->b_width; mb_x++)
01057                 encode_q_branch(s, 0, mb_x, mb_y);
01058         s->c = r;
01059         memcpy(s->block_state, state, sizeof(s->block_state));
01060     }
01061 
01062     for(pass=0; pass<25; pass++){
01063         int change= 0;
01064 
01065         for(mb_y= 0; mb_y<b_height; mb_y++){
01066             for(mb_x= 0; mb_x<b_width; mb_x++){
01067                 int dia_change, i, j, ref;
01068                 int best_rd= INT_MAX, ref_rd;
01069                 BlockNode backup, ref_b;
01070                 const int index= mb_x + mb_y * b_stride;
01071                 BlockNode *block= &s->block[index];
01072                 BlockNode *tb =                   mb_y            ? &s->block[index-b_stride  ] : NULL;
01073                 BlockNode *lb = mb_x                              ? &s->block[index         -1] : NULL;
01074                 BlockNode *rb = mb_x+1<b_width                    ? &s->block[index         +1] : NULL;
01075                 BlockNode *bb =                   mb_y+1<b_height ? &s->block[index+b_stride  ] : NULL;
01076                 BlockNode *tlb= mb_x           && mb_y            ? &s->block[index-b_stride-1] : NULL;
01077                 BlockNode *trb= mb_x+1<b_width && mb_y            ? &s->block[index-b_stride+1] : NULL;
01078                 BlockNode *blb= mb_x           && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
01079                 BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
01080                 const int b_w= (MB_SIZE >> s->block_max_depth);
01081                 uint8_t obmc_edged[b_w*2][b_w*2];
01082 
01083                 if(pass && (block->type & BLOCK_OPT))
01084                     continue;
01085                 block->type |= BLOCK_OPT;
01086 
01087                 backup= *block;
01088 
01089                 if(!s->me_cache_generation)
01090                     memset(s->me_cache, 0, sizeof(s->me_cache));
01091                 s->me_cache_generation += 1<<22;
01092 
01093                 //FIXME precalculate
01094                 {
01095                     int x, y;
01096                     memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
01097                     if(mb_x==0)
01098                         for(y=0; y<b_w*2; y++)
01099                             memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
01100                     if(mb_x==b_stride-1)
01101                         for(y=0; y<b_w*2; y++)
01102                             memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
01103                     if(mb_y==0){
01104                         for(x=0; x<b_w*2; x++)
01105                             obmc_edged[0][x] += obmc_edged[b_w-1][x];
01106                         for(y=1; y<b_w; y++)
01107                             memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
01108                     }
01109                     if(mb_y==b_height-1){
01110                         for(x=0; x<b_w*2; x++)
01111                             obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
01112                         for(y=b_w; y<b_w*2-1; y++)
01113                             memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
01114                     }
01115                 }
01116 
01117                 //skip stuff outside the picture
01118                 if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
01119                     uint8_t *src= s->  input_picture.data[0];
01120                     uint8_t *dst= s->current_picture.data[0];
01121                     const int stride= s->current_picture.linesize[0];
01122                     const int block_w= MB_SIZE >> s->block_max_depth;
01123                     const int sx= block_w*mb_x - block_w/2;
01124                     const int sy= block_w*mb_y - block_w/2;
01125                     const int w= s->plane[0].width;
01126                     const int h= s->plane[0].height;
01127                     int y;
01128 
01129                     for(y=sy; y<0; y++)
01130                         memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
01131                     for(y=h; y<sy+block_w*2; y++)
01132                         memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
01133                     if(sx<0){
01134                         for(y=sy; y<sy+block_w*2; y++)
01135                             memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
01136                     }
01137                     if(sx+block_w*2 > w){
01138                         for(y=sy; y<sy+block_w*2; y++)
01139                             memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
01140                     }
01141                 }
01142 
01143                 // intra(black) = neighbors' contribution to the current block
01144                 for(i=0; i<3; i++)
01145                     color[i]= get_dc(s, mb_x, mb_y, i);
01146 
01147                 // get previous score (cannot be cached due to OBMC)
01148                 if(pass > 0 && (block->type&BLOCK_INTRA)){
01149                     int color0[3]= {block->color[0], block->color[1], block->color[2]};
01150                     check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
01151                 }else
01152                     check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
01153 
01154                 ref_b= *block;
01155                 ref_rd= best_rd;
01156                 for(ref=0; ref < s->ref_frames; ref++){
01157                     int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
01158                     if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
01159                         continue;
01160                     block->ref= ref;
01161                     best_rd= INT_MAX;
01162 
01163                     check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
01164                     check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
01165                     if(tb)
01166                         check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
01167                     if(lb)
01168                         check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
01169                     if(rb)
01170                         check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
01171                     if(bb)
01172                         check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
01173 
01174                     /* fullpel ME */
01175                     //FIXME avoid subpel interpolation / round to nearest integer
01176                     do{
01177                         dia_change=0;
01178                         for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
01179                             for(j=0; j<i; j++){
01180                                 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
01181                                 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
01182                                 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
01183                                 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
01184                             }
01185                         }
01186                     }while(dia_change);
01187                     /* subpel ME */
01188                     do{
01189                         static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
01190                         dia_change=0;
01191                         for(i=0; i<8; i++)
01192                             dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
01193                     }while(dia_change);
01194                     //FIXME or try the standard 2 pass qpel or similar
01195 
01196                     mvr[0][0]= block->mx;
01197                     mvr[0][1]= block->my;
01198                     if(ref_rd > best_rd){
01199                         ref_rd= best_rd;
01200                         ref_b= *block;
01201                     }
01202                 }
01203                 best_rd= ref_rd;
01204                 *block= ref_b;
01205                 check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
01206                 //FIXME RD style color selection
01207                 if(!same_block(block, &backup)){
01208                     if(tb ) tb ->type &= ~BLOCK_OPT;
01209                     if(lb ) lb ->type &= ~BLOCK_OPT;
01210                     if(rb ) rb ->type &= ~BLOCK_OPT;
01211                     if(bb ) bb ->type &= ~BLOCK_OPT;
01212                     if(tlb) tlb->type &= ~BLOCK_OPT;
01213                     if(trb) trb->type &= ~BLOCK_OPT;
01214                     if(blb) blb->type &= ~BLOCK_OPT;
01215                     if(brb) brb->type &= ~BLOCK_OPT;
01216                     change ++;
01217                 }
01218             }
01219         }
01220         av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
01221         if(!change)
01222             break;
01223     }
01224 
01225     if(s->block_max_depth == 1){
01226         int change= 0;
01227         for(mb_y= 0; mb_y<b_height; mb_y+=2){
01228             for(mb_x= 0; mb_x<b_width; mb_x+=2){
01229                 int i;
01230                 int best_rd, init_rd;
01231                 const int index= mb_x + mb_y * b_stride;
01232                 BlockNode *b[4];
01233 
01234                 b[0]= &s->block[index];
01235                 b[1]= b[0]+1;
01236                 b[2]= b[0]+b_stride;
01237                 b[3]= b[2]+1;
01238                 if(same_block(b[0], b[1]) &&
01239                    same_block(b[0], b[2]) &&
01240                    same_block(b[0], b[3]))
01241                     continue;
01242 
01243                 if(!s->me_cache_generation)
01244                     memset(s->me_cache, 0, sizeof(s->me_cache));
01245                 s->me_cache_generation += 1<<22;
01246 
01247                 init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
01248 
01249                 //FIXME more multiref search?
01250                 check_4block_inter(s, mb_x, mb_y,
01251                                    (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
01252                                    (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
01253 
01254                 for(i=0; i<4; i++)
01255                     if(!(b[i]->type&BLOCK_INTRA))
01256                         check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
01257 
01258                 if(init_rd != best_rd)
01259                     change++;
01260             }
01261         }
01262         av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
01263     }
01264 }
01265 
01266 static void encode_blocks(SnowContext *s, int search){
01267     int x, y;
01268     int w= s->b_width;
01269     int h= s->b_height;
01270 
01271     if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
01272         iterative_me(s);
01273 
01274     for(y=0; y<h; y++){
01275         if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
01276             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
01277             return;
01278         }
01279         for(x=0; x<w; x++){
01280             if(s->avctx->me_method == ME_ITER || !search)
01281                 encode_q_branch2(s, 0, x, y);
01282             else
01283                 encode_q_branch (s, 0, x, y);
01284         }
01285     }
01286 }
01287 
01288 static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
01289     const int w= b->width;
01290     const int h= b->height;
01291     const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
01292     const int qmul= qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
01293     int x,y, thres1, thres2;
01294 
01295     if(s->qlog == LOSSLESS_QLOG){
01296         for(y=0; y<h; y++)
01297             for(x=0; x<w; x++)
01298                 dst[x + y*stride]= src[x + y*stride];
01299         return;
01300     }
01301 
01302     bias= bias ? 0 : (3*qmul)>>3;
01303     thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
01304     thres2= 2*thres1;
01305 
01306     if(!bias){
01307         for(y=0; y<h; y++){
01308             for(x=0; x<w; x++){
01309                 int i= src[x + y*stride];
01310 
01311                 if((unsigned)(i+thres1) > thres2){
01312                     if(i>=0){
01313                         i<<= QEXPSHIFT;
01314                         i/= qmul; //FIXME optimize
01315                         dst[x + y*stride]=  i;
01316                     }else{
01317                         i= -i;
01318                         i<<= QEXPSHIFT;
01319                         i/= qmul; //FIXME optimize
01320                         dst[x + y*stride]= -i;
01321                     }
01322                 }else
01323                     dst[x + y*stride]= 0;
01324             }
01325         }
01326     }else{
01327         for(y=0; y<h; y++){
01328             for(x=0; x<w; x++){
01329                 int i= src[x + y*stride];
01330 
01331                 if((unsigned)(i+thres1) > thres2){
01332                     if(i>=0){
01333                         i<<= QEXPSHIFT;
01334                         i= (i + bias) / qmul; //FIXME optimize
01335                         dst[x + y*stride]=  i;
01336                     }else{
01337                         i= -i;
01338                         i<<= QEXPSHIFT;
01339                         i= (i + bias) / qmul; //FIXME optimize
01340                         dst[x + y*stride]= -i;
01341                     }
01342                 }else
01343                     dst[x + y*stride]= 0;
01344             }
01345         }
01346     }
01347 }
01348 
01349 static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
01350     const int w= b->width;
01351     const int h= b->height;
01352     const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
01353     const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
01354     const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
01355     int x,y;
01356 
01357     if(s->qlog == LOSSLESS_QLOG) return;
01358 
01359     for(y=0; y<h; y++){
01360         for(x=0; x<w; x++){
01361             int i= src[x + y*stride];
01362             if(i<0){
01363                 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
01364             }else if(i>0){
01365                 src[x + y*stride]=  (( i*qmul + qadd)>>(QEXPSHIFT));
01366             }
01367         }
01368     }
01369 }
01370 
01371 static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
01372     const int w= b->width;
01373     const int h= b->height;
01374     int x,y;
01375 
01376     for(y=h-1; y>=0; y--){
01377         for(x=w-1; x>=0; x--){
01378             int i= x + y*stride;
01379 
01380             if(x){
01381                 if(use_median){
01382                     if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
01383                     else  src[i] -= src[i - 1];
01384                 }else{
01385                     if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
01386                     else  src[i] -= src[i - 1];
01387                 }
01388             }else{
01389                 if(y) src[i] -= src[i - stride];
01390             }
01391         }
01392     }
01393 }
01394 
01395 static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
01396     const int w= b->width;
01397     const int h= b->height;
01398     int x,y;
01399 
01400     for(y=0; y<h; y++){
01401         for(x=0; x<w; x++){
01402             int i= x + y*stride;
01403 
01404             if(x){
01405                 if(use_median){
01406                     if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
01407                     else  src[i] += src[i - 1];
01408                 }else{
01409                     if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
01410                     else  src[i] += src[i - 1];
01411                 }
01412             }else{
01413                 if(y) src[i] += src[i - stride];
01414             }
01415         }
01416     }
01417 }
01418 
01419 static void encode_qlogs(SnowContext *s){
01420     int plane_index, level, orientation;
01421 
01422     for(plane_index=0; plane_index<2; plane_index++){
01423         for(level=0; level<s->spatial_decomposition_count; level++){
01424             for(orientation=level ? 1:0; orientation<4; orientation++){
01425                 if(orientation==2) continue;
01426                 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
01427             }
01428         }
01429     }
01430 }
01431 
01432 static void encode_header(SnowContext *s){
01433     int plane_index, i;
01434     uint8_t kstate[32];
01435 
01436     memset(kstate, MID_STATE, sizeof(kstate));
01437 
01438     put_rac(&s->c, kstate, s->keyframe);
01439     if(s->keyframe || s->always_reset){
01440         ff_snow_reset_contexts(s);
01441         s->last_spatial_decomposition_type=
01442         s->last_qlog=
01443         s->last_qbias=
01444         s->last_mv_scale=
01445         s->last_block_max_depth= 0;
01446         for(plane_index=0; plane_index<2; plane_index++){
01447             Plane *p= &s->plane[plane_index];
01448             p->last_htaps=0;
01449             p->last_diag_mc=0;
01450             memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
01451         }
01452     }
01453     if(s->keyframe){
01454         put_symbol(&s->c, s->header_state, s->version, 0);
01455         put_rac(&s->c, s->header_state, s->always_reset);
01456         put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
01457         put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
01458         put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
01459         put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
01460         put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
01461         put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
01462         put_rac(&s->c, s->header_state, s->spatial_scalability);
01463 //        put_rac(&s->c, s->header_state, s->rate_scalability);
01464         put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
01465 
01466         encode_qlogs(s);
01467     }
01468 
01469     if(!s->keyframe){
01470         int update_mc=0;
01471         for(plane_index=0; plane_index<2; plane_index++){
01472             Plane *p= &s->plane[plane_index];
01473             update_mc |= p->last_htaps   != p->htaps;
01474             update_mc |= p->last_diag_mc != p->diag_mc;
01475             update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
01476         }
01477         put_rac(&s->c, s->header_state, update_mc);
01478         if(update_mc){
01479             for(plane_index=0; plane_index<2; plane_index++){
01480                 Plane *p= &s->plane[plane_index];
01481                 put_rac(&s->c, s->header_state, p->diag_mc);
01482                 put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
01483                 for(i= p->htaps/2; i; i--)
01484                     put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
01485             }
01486         }
01487         if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
01488             put_rac(&s->c, s->header_state, 1);
01489             put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
01490             encode_qlogs(s);
01491         }else
01492             put_rac(&s->c, s->header_state, 0);
01493     }
01494 
01495     put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
01496     put_symbol(&s->c, s->header_state, s->qlog            - s->last_qlog    , 1);
01497     put_symbol(&s->c, s->header_state, s->mv_scale        - s->last_mv_scale, 1);
01498     put_symbol(&s->c, s->header_state, s->qbias           - s->last_qbias   , 1);
01499     put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
01500 
01501 }
01502 
01503 static void update_last_header_values(SnowContext *s){
01504     int plane_index;
01505 
01506     if(!s->keyframe){
01507         for(plane_index=0; plane_index<2; plane_index++){
01508             Plane *p= &s->plane[plane_index];
01509             p->last_diag_mc= p->diag_mc;
01510             p->last_htaps  = p->htaps;
01511             memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
01512         }
01513     }
01514 
01515     s->last_spatial_decomposition_type  = s->spatial_decomposition_type;
01516     s->last_qlog                        = s->qlog;
01517     s->last_qbias                       = s->qbias;
01518     s->last_mv_scale                    = s->mv_scale;
01519     s->last_block_max_depth             = s->block_max_depth;
01520     s->last_spatial_decomposition_count = s->spatial_decomposition_count;
01521 }
01522 
01523 static int qscale2qlog(int qscale){
01524     return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
01525            + 61*QROOT/8; 
01526 }
01527 
01528 static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
01529 {
01530     /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
01531      * FIXME we know exact mv bits at this point,
01532      * but ratecontrol isn't set up to include them. */
01533     uint32_t coef_sum= 0;
01534     int level, orientation, delta_qlog;
01535 
01536     for(level=0; level<s->spatial_decomposition_count; level++){
01537         for(orientation=level ? 1 : 0; orientation<4; orientation++){
01538             SubBand *b= &s->plane[0].band[level][orientation];
01539             IDWTELEM *buf= b->ibuf;
01540             const int w= b->width;
01541             const int h= b->height;
01542             const int stride= b->stride;
01543             const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
01544             const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
01545             const int qdiv= (1<<16)/qmul;
01546             int x, y;
01547             //FIXME this is ugly
01548             for(y=0; y<h; y++)
01549                 for(x=0; x<w; x++)
01550                     buf[x+y*stride]= b->buf[x+y*stride];
01551             if(orientation==0)
01552                 decorrelate(s, b, buf, stride, 1, 0);
01553             for(y=0; y<h; y++)
01554                 for(x=0; x<w; x++)
01555                     coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
01556         }
01557     }
01558 
01559     /* ugly, ratecontrol just takes a sqrt again */
01560     coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
01561     assert(coef_sum < INT_MAX);
01562 
01563     if(pict->pict_type == AV_PICTURE_TYPE_I){
01564         s->m.current_picture.mb_var_sum= coef_sum;
01565         s->m.current_picture.mc_mb_var_sum= 0;
01566     }else{
01567         s->m.current_picture.mc_mb_var_sum= coef_sum;
01568         s->m.current_picture.mb_var_sum= 0;
01569     }
01570 
01571     pict->quality= ff_rate_estimate_qscale(&s->m, 1);
01572     if (pict->quality < 0)
01573         return INT_MIN;
01574     s->lambda= pict->quality * 3/2;
01575     delta_qlog= qscale2qlog(pict->quality) - s->qlog;
01576     s->qlog+= delta_qlog;
01577     return delta_qlog;
01578 }
01579 
01580 static void calculate_visual_weight(SnowContext *s, Plane *p){
01581     int width = p->width;
01582     int height= p->height;
01583     int level, orientation, x, y;
01584 
01585     for(level=0; level<s->spatial_decomposition_count; level++){
01586         for(orientation=level ? 1 : 0; orientation<4; orientation++){
01587             SubBand *b= &p->band[level][orientation];
01588             IDWTELEM *ibuf= b->ibuf;
01589             int64_t error=0;
01590 
01591             memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
01592             ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
01593             ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
01594             for(y=0; y<height; y++){
01595                 for(x=0; x<width; x++){
01596                     int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
01597                     error += d*d;
01598                 }
01599             }
01600 
01601             b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
01602         }
01603     }
01604 }
01605 
01606 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
01607     SnowContext *s = avctx->priv_data;
01608     RangeCoder * const c= &s->c;
01609     AVFrame *pict = data;
01610     const int width= s->avctx->width;
01611     const int height= s->avctx->height;
01612     int level, orientation, plane_index, i, y;
01613     uint8_t rc_header_bak[sizeof(s->header_state)];
01614     uint8_t rc_block_bak[sizeof(s->block_state)];
01615 
01616     ff_init_range_encoder(c, buf, buf_size);
01617     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01618 
01619     for(i=0; i<3; i++){
01620         int shift= !!i;
01621         for(y=0; y<(height>>shift); y++)
01622             memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
01623                    &pict->data[i][y * pict->linesize[i]],
01624                    width>>shift);
01625     }
01626     s->new_picture = *pict;
01627 
01628     s->m.picture_number= avctx->frame_number;
01629     if(avctx->flags&CODEC_FLAG_PASS2){
01630         s->m.pict_type =
01631         pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
01632         s->keyframe= pict->pict_type==AV_PICTURE_TYPE_I;
01633         if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
01634             pict->quality= ff_rate_estimate_qscale(&s->m, 0);
01635             if (pict->quality < 0)
01636                 return -1;
01637         }
01638     }else{
01639         s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
01640         s->m.pict_type=
01641         pict->pict_type= s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
01642     }
01643 
01644     if(s->pass1_rc && avctx->frame_number == 0)
01645         pict->quality= 2*FF_QP2LAMBDA;
01646     if(pict->quality){
01647         s->qlog= qscale2qlog(pict->quality);
01648         s->lambda = pict->quality * 3/2;
01649     }
01650     if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
01651         s->qlog= LOSSLESS_QLOG;
01652         s->lambda = 0;
01653     }//else keep previous frame's qlog until after motion estimation
01654 
01655     ff_snow_frame_start(s);
01656 
01657     s->m.current_picture_ptr= &s->m.current_picture;
01658     s->m.last_picture.f.pts = s->m.current_picture.f.pts;
01659     s->m.current_picture.f.pts = pict->pts;
01660     if(pict->pict_type == AV_PICTURE_TYPE_P){
01661         int block_width = (width +15)>>4;
01662         int block_height= (height+15)>>4;
01663         int stride= s->current_picture.linesize[0];
01664 
01665         assert(s->current_picture.data[0]);
01666         assert(s->last_picture[0].data[0]);
01667 
01668         s->m.avctx= s->avctx;
01669         s->m.current_picture.f.data[0] = s->current_picture.data[0];
01670         s->m.   last_picture.f.data[0] = s->last_picture[0].data[0];
01671         s->m.    new_picture.f.data[0] = s->  input_picture.data[0];
01672         s->m.   last_picture_ptr= &s->m.   last_picture;
01673         s->m.linesize=
01674         s->m.   last_picture.f.linesize[0] =
01675         s->m.    new_picture.f.linesize[0] =
01676         s->m.current_picture.f.linesize[0] = stride;
01677         s->m.uvlinesize= s->current_picture.linesize[1];
01678         s->m.width = width;
01679         s->m.height= height;
01680         s->m.mb_width = block_width;
01681         s->m.mb_height= block_height;
01682         s->m.mb_stride=   s->m.mb_width+1;
01683         s->m.b8_stride= 2*s->m.mb_width+1;
01684         s->m.f_code=1;
01685         s->m.pict_type= pict->pict_type;
01686         s->m.me_method= s->avctx->me_method;
01687         s->m.me.scene_change_score=0;
01688         s->m.flags= s->avctx->flags;
01689         s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
01690         s->m.out_format= FMT_H263;
01691         s->m.unrestricted_mv= 1;
01692 
01693         s->m.lambda = s->lambda;
01694         s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
01695         s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
01696 
01697         s->m.dsp= s->dsp; //move
01698         ff_init_me(&s->m);
01699         s->dsp= s->m.dsp;
01700     }
01701 
01702     if(s->pass1_rc){
01703         memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
01704         memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
01705     }
01706 
01707 redo_frame:
01708 
01709     if(pict->pict_type == AV_PICTURE_TYPE_I)
01710         s->spatial_decomposition_count= 5;
01711     else
01712         s->spatial_decomposition_count= 5;
01713 
01714     s->m.pict_type = pict->pict_type;
01715     s->qbias= pict->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
01716 
01717     ff_snow_common_init_after_header(avctx);
01718 
01719     if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
01720         for(plane_index=0; plane_index<3; plane_index++){
01721             calculate_visual_weight(s, &s->plane[plane_index]);
01722         }
01723     }
01724 
01725     encode_header(s);
01726     s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
01727     encode_blocks(s, 1);
01728     s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
01729 
01730     for(plane_index=0; plane_index<3; plane_index++){
01731         Plane *p= &s->plane[plane_index];
01732         int w= p->width;
01733         int h= p->height;
01734         int x, y;
01735 //        int bits= put_bits_count(&s->c.pb);
01736 
01737         if (!s->memc_only) {
01738             //FIXME optimize
01739             if(pict->data[plane_index]) //FIXME gray hack
01740                 for(y=0; y<h; y++){
01741                     for(x=0; x<w; x++){
01742                         s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
01743                     }
01744                 }
01745             predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
01746 
01747             if(   plane_index==0
01748                && pict->pict_type == AV_PICTURE_TYPE_P
01749                && !(avctx->flags&CODEC_FLAG_PASS2)
01750                && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
01751                 ff_init_range_encoder(c, buf, buf_size);
01752                 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01753                 pict->pict_type= AV_PICTURE_TYPE_I;
01754                 s->keyframe=1;
01755                 s->current_picture.key_frame=1;
01756                 goto redo_frame;
01757             }
01758 
01759             if(s->qlog == LOSSLESS_QLOG){
01760                 for(y=0; y<h; y++){
01761                     for(x=0; x<w; x++){
01762                         s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
01763                     }
01764                 }
01765             }else{
01766                 for(y=0; y<h; y++){
01767                     for(x=0; x<w; x++){
01768                         s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
01769                     }
01770                 }
01771             }
01772 
01773             /*  if(QUANTIZE2)
01774                 dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
01775             else*/
01776                 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
01777 
01778             if(s->pass1_rc && plane_index==0){
01779                 int delta_qlog = ratecontrol_1pass(s, pict);
01780                 if (delta_qlog <= INT_MIN)
01781                     return -1;
01782                 if(delta_qlog){
01783                     //reordering qlog in the bitstream would eliminate this reset
01784                     ff_init_range_encoder(c, buf, buf_size);
01785                     memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
01786                     memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
01787                     encode_header(s);
01788                     encode_blocks(s, 0);
01789                 }
01790             }
01791 
01792             for(level=0; level<s->spatial_decomposition_count; level++){
01793                 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01794                     SubBand *b= &p->band[level][orientation];
01795 
01796                     if(!QUANTIZE2)
01797                         quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
01798                     if(orientation==0)
01799                         decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == AV_PICTURE_TYPE_P, 0);
01800                     encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
01801                     assert(b->parent==NULL || b->parent->stride == b->stride*2);
01802                     if(orientation==0)
01803                         correlate(s, b, b->ibuf, b->stride, 1, 0);
01804                 }
01805             }
01806 
01807             for(level=0; level<s->spatial_decomposition_count; level++){
01808                 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01809                     SubBand *b= &p->band[level][orientation];
01810 
01811                     dequantize(s, b, b->ibuf, b->stride);
01812                 }
01813             }
01814 
01815             ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
01816             if(s->qlog == LOSSLESS_QLOG){
01817                 for(y=0; y<h; y++){
01818                     for(x=0; x<w; x++){
01819                         s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
01820                     }
01821                 }
01822             }
01823             predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
01824         }else{
01825             //ME/MC only
01826             if(pict->pict_type == AV_PICTURE_TYPE_I){
01827                 for(y=0; y<h; y++){
01828                     for(x=0; x<w; x++){
01829                         s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
01830                             pict->data[plane_index][y*pict->linesize[plane_index] + x];
01831                     }
01832                 }
01833             }else{
01834                 memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
01835                 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
01836             }
01837         }
01838         if(s->avctx->flags&CODEC_FLAG_PSNR){
01839             int64_t error= 0;
01840 
01841             if(pict->data[plane_index]) //FIXME gray hack
01842                 for(y=0; y<h; y++){
01843                     for(x=0; x<w; x++){
01844                         int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
01845                         error += d*d;
01846                     }
01847                 }
01848             s->avctx->error[plane_index] += error;
01849             s->current_picture.error[plane_index] = error;
01850         }
01851 
01852     }
01853 
01854     update_last_header_values(s);
01855 
01856     ff_snow_release_buffer(avctx);
01857 
01858     s->current_picture.coded_picture_number = avctx->frame_number;
01859     s->current_picture.pict_type = pict->pict_type;
01860     s->current_picture.quality = pict->quality;
01861     s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
01862     s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
01863     s->m.current_picture.f.display_picture_number =
01864     s->m.current_picture.f.coded_picture_number   = avctx->frame_number;
01865     s->m.current_picture.f.quality                = pict->quality;
01866     s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
01867     if(s->pass1_rc)
01868         if (ff_rate_estimate_qscale(&s->m, 0) < 0)
01869             return -1;
01870     if(avctx->flags&CODEC_FLAG_PASS1)
01871         ff_write_pass1_stats(&s->m);
01872     s->m.last_pict_type = s->m.pict_type;
01873     avctx->frame_bits = s->m.frame_bits;
01874     avctx->mv_bits = s->m.mv_bits;
01875     avctx->misc_bits = s->m.misc_bits;
01876     avctx->p_tex_bits = s->m.p_tex_bits;
01877 
01878     emms_c();
01879 
01880     return ff_rac_terminate(c);
01881 }
01882 
01883 static av_cold int encode_end(AVCodecContext *avctx)
01884 {
01885     SnowContext *s = avctx->priv_data;
01886 
01887     ff_snow_common_end(s);
01888     if (s->input_picture.data[0])
01889         avctx->release_buffer(avctx, &s->input_picture);
01890     av_free(avctx->stats_out);
01891 
01892     return 0;
01893 }
01894 
01895 #define OFFSET(x) offsetof(SnowContext, x)
01896 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
01897 static const AVOption options[] = {
01898     { "memc_only",      "Only do ME/MC (I frames -> ref, P frame -> ME+MC).",   OFFSET(memc_only), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
01899     { NULL },
01900 };
01901 
01902 static const AVClass snowenc_class = {
01903     .class_name = "snow encoder",
01904     .item_name  = av_default_item_name,
01905     .option     = options,
01906     .version    = LIBAVUTIL_VERSION_INT,
01907 };
01908 
01909 AVCodec ff_snow_encoder = {
01910     .name           = "snow",
01911     .type           = AVMEDIA_TYPE_VIDEO,
01912     .id             = CODEC_ID_SNOW,
01913     .priv_data_size = sizeof(SnowContext),
01914     .init           = encode_init,
01915     .encode         = encode_frame,
01916     .close          = encode_end,
01917     .long_name = NULL_IF_CONFIG_SMALL("Snow"),
01918     .priv_class     = &snowenc_class,
01919 };
01920 #endif
01921 
01922 
01923 #ifdef TEST
01924 #undef malloc
01925 #undef free
01926 #undef printf
01927 
01928 #include "libavutil/lfg.h"
01929 #include "libavutil/mathematics.h"
01930 
01931 int main(void){
01932     int width=256;
01933     int height=256;
01934     int buffer[2][width*height];
01935     SnowContext s;
01936     int i;
01937     AVLFG prng;
01938     s.spatial_decomposition_count=6;
01939     s.spatial_decomposition_type=1;
01940 
01941     av_lfg_init(&prng, 1);
01942 
01943     printf("testing 5/3 DWT\n");
01944     for(i=0; i<width*height; i++)
01945         buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
01946 
01947     ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01948     ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01949 
01950     for(i=0; i<width*height; i++)
01951         if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
01952 
01953     printf("testing 9/7 DWT\n");
01954     s.spatial_decomposition_type=0;
01955     for(i=0; i<width*height; i++)
01956         buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
01957 
01958     ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01959     ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01960 
01961     for(i=0; i<width*height; i++)
01962         if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
01963 
01964     {
01965     int level, orientation, x, y;
01966     int64_t errors[8][4];
01967     int64_t g=0;
01968 
01969         memset(errors, 0, sizeof(errors));
01970         s.spatial_decomposition_count=3;
01971         s.spatial_decomposition_type=0;
01972         for(level=0; level<s.spatial_decomposition_count; level++){
01973             for(orientation=level ? 1 : 0; orientation<4; orientation++){
01974                 int w= width  >> (s.spatial_decomposition_count-level);
01975                 int h= height >> (s.spatial_decomposition_count-level);
01976                 int stride= width  << (s.spatial_decomposition_count-level);
01977                 DWTELEM *buf= buffer[0];
01978                 int64_t error=0;
01979 
01980                 if(orientation&1) buf+=w;
01981                 if(orientation>1) buf+=stride>>1;
01982 
01983                 memset(buffer[0], 0, sizeof(int)*width*height);
01984                 buf[w/2 + h/2*stride]= 256*256;
01985                 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01986                 for(y=0; y<height; y++){
01987                     for(x=0; x<width; x++){
01988                         int64_t d= buffer[0][x + y*width];
01989                         error += d*d;
01990                         if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
01991                     }
01992                     if(FFABS(height/2-y)<9 && level==2) printf("\n");
01993                 }
01994                 error= (int)(sqrt(error)+0.5);
01995                 errors[level][orientation]= error;
01996                 if(g) g=av_gcd(g, error);
01997                 else g= error;
01998             }
01999         }
02000         printf("static int const visual_weight[][4]={\n");
02001         for(level=0; level<s.spatial_decomposition_count; level++){
02002             printf("  {");
02003             for(orientation=0; orientation<4; orientation++){
02004                 printf("%8"PRId64",", errors[level][orientation]/g);
02005             }
02006             printf("},\n");
02007         }
02008         printf("};\n");
02009         {
02010             int level=2;
02011             int w= width  >> (s.spatial_decomposition_count-level);
02012             //int h= height >> (s.spatial_decomposition_count-level);
02013             int stride= width  << (s.spatial_decomposition_count-level);
02014             DWTELEM *buf= buffer[0];
02015             int64_t error=0;
02016 
02017             buf+=w;
02018             buf+=stride>>1;
02019 
02020             memset(buffer[0], 0, sizeof(int)*width*height);
02021             for(y=0; y<height; y++){
02022                 for(x=0; x<width; x++){
02023                     int tab[4]={0,2,3,1};
02024                     buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
02025                 }
02026             }
02027             ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
02028             for(y=0; y<height; y++){
02029                 for(x=0; x<width; x++){
02030                     int64_t d= buffer[0][x + y*width];
02031                     error += d*d;
02032                     if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
02033                 }
02034                 if(FFABS(height/2-y)<9) printf("\n");
02035             }
02036         }
02037 
02038     }
02039     return 0;
02040 }
02041 #endif /* TEST */