mpegvideo.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:230k
源码类别:

Audio

开发平台:

Visual C++

  1. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
  2. {
  3.     int mb_x, mb_y;
  4.     const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  5. #ifdef HAVE_XVMC
  6.     if(s->avctx->xvmc_acceleration){
  7.         XVMC_decode_mb(s);//xvmc uses pblocks
  8.         return;
  9.     }
  10. #endif
  11.     mb_x = s->mb_x;
  12.     mb_y = s->mb_y;
  13.     if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  14.        /* save DCT coefficients */
  15.        int i,j;
  16.        DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  17.        for(i=0; i<6; i++)
  18.            for(j=0; j<64; j++)
  19.                *dct++ = block[i][s->dsp.idct_permutation[j]];
  20.     }
  21.     s->current_picture.qscale_table[mb_xy]= s->qscale;
  22.     /* update DC predictors for P macroblocks */
  23.     if (!s->mb_intra) {
  24.         if (s->h263_pred || s->h263_aic) {
  25.             if(s->mbintra_table[mb_xy])
  26.                 ff_clean_intra_table_entries(s);
  27.         } else {
  28.             s->last_dc[0] =
  29.             s->last_dc[1] =
  30.             s->last_dc[2] = 128 << s->intra_dc_precision;
  31.         }
  32.     }
  33.     else if (s->h263_pred || s->h263_aic)
  34.         s->mbintra_table[mb_xy]=1;
  35.     if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  36.         uint8_t *dest_y, *dest_cb, *dest_cr;
  37.         int dct_linesize, dct_offset;
  38.         op_pixels_func (*op_pix)[4];
  39.         qpel_mc_func (*op_qpix)[16];
  40.         const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  41.         const int uvlinesize= s->current_picture.linesize[1];
  42.         const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band;
  43.         /* avoid copy if macroblock skipped in last frame too */
  44.         /* skip only during decoding as we might trash the buffers during encoding a bit */
  45.         if(!s->encoding){
  46.             uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  47.             const int age= s->current_picture.age;
  48.             assert(age);
  49.             if (s->mb_skiped) {
  50.                 s->mb_skiped= 0;
  51.                 assert(s->pict_type!=I_TYPE);
  52.  
  53.                 (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  54.                 if(*mbskip_ptr >99) *mbskip_ptr= 99;
  55.                 /* if previous was skipped too, then nothing to do !  */
  56.                 if (*mbskip_ptr >= age && s->current_picture.reference){
  57.                     return;
  58.                 }
  59.             } else if(!s->current_picture.reference){
  60.                 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  61.                 if(*mbskip_ptr >99) *mbskip_ptr= 99;
  62.             } else{
  63.                 *mbskip_ptr = 0; /* not skipped */
  64.             }
  65.         }
  66.         dct_linesize = linesize << s->interlaced_dct;
  67.         dct_offset =(s->interlaced_dct)? linesize : linesize*8;
  68.         if(readable){
  69.             dest_y=  s->dest[0];
  70.             dest_cb= s->dest[1];
  71.             dest_cr= s->dest[2];
  72.         }else{
  73.             dest_y = s->b_scratchpad;
  74.             dest_cb= s->b_scratchpad+16*linesize;
  75.             dest_cr= s->b_scratchpad+32*linesize;
  76.         }
  77.         if (!s->mb_intra) {
  78.             /* motion handling */
  79.             /* decoding or more than one mb_type (MC was allready done otherwise) */
  80.             if(!s->encoding){
  81.                 if ((!s->no_rounding) || s->pict_type==B_TYPE){                
  82.     op_pix = s->dsp.put_pixels_tab;
  83.                     op_qpix= s->dsp.put_qpel_pixels_tab;
  84.                 }else{
  85.                     op_pix = s->dsp.put_no_rnd_pixels_tab;
  86.                     op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  87.                 }
  88.                 if (s->mv_dir & MV_DIR_FORWARD) {
  89.                     MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  90.     op_pix = s->dsp.avg_pixels_tab;
  91.                     op_qpix= s->dsp.avg_qpel_pixels_tab;
  92.                 }
  93.                 if (s->mv_dir & MV_DIR_BACKWARD) {
  94.                     MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  95.                 }
  96.             }
  97.             /* skip dequant / idct if we are really late ;) */
  98.             if(s->hurry_up>1) return;
  99.             /* add dct residue */
  100.             if(s->encoding || !(   s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  101.                                 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  102.                 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  103.                 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  104.                 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  105.                 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  106.                 if(!(s->flags&CODEC_FLAG_GRAY)){
  107.                     add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  108.                     add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  109.                 }
  110.             } else if(s->codec_id != CODEC_ID_WMV2){
  111.                 add_dct(s, block[0], 0, dest_y, dct_linesize);
  112.                 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  113.                 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  114.                 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  115.                 if(!(s->flags&CODEC_FLAG_GRAY)){
  116.                     if(s->chroma_y_shift){//Chroma420
  117.                         add_dct(s, block[4], 4, dest_cb, uvlinesize);
  118.                         add_dct(s, block[5], 5, dest_cr, uvlinesize);
  119.                     }else{
  120.                         //chroma422
  121.                         dct_linesize = uvlinesize << s->interlaced_dct;
  122.                         dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  123.                         add_dct(s, block[4], 4, dest_cb, dct_linesize);
  124.                         add_dct(s, block[5], 5, dest_cr, dct_linesize);
  125.                         add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  126.                         add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  127.                         if(!s->chroma_x_shift){//Chroma444
  128.                             add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  129.                             add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  130.                             add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  131.                             add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  132.                         }
  133.                     }
  134.                 }//fi gray
  135.             }
  136. #ifdef CONFIG_RISKY
  137.             else{
  138.                 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  139.             }
  140. #endif
  141.         } else {
  142.             /* dct only in intra block */
  143.             if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  144.                 put_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  145.                 put_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  146.                 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  147.                 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  148.                 if(!(s->flags&CODEC_FLAG_GRAY)){
  149.                     put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  150.                     put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  151.                 }
  152.             }else{
  153.                 s->dsp.idct_put(dest_y                 , dct_linesize, block[0]);
  154.                 s->dsp.idct_put(dest_y              + 8, dct_linesize, block[1]);
  155.                 s->dsp.idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
  156.                 s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  157.                 if(!(s->flags&CODEC_FLAG_GRAY)){
  158.                     if(s->chroma_y_shift){
  159.                         s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  160.                         s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  161.                     }else{
  162.                         dct_linesize = uvlinesize << s->interlaced_dct;
  163.                         dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  164.                         s->dsp.idct_put(dest_cb,              dct_linesize, block[4]);
  165.                         s->dsp.idct_put(dest_cr,              dct_linesize, block[5]);
  166.                         s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  167.                         s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  168.                         if(!s->chroma_x_shift){//Chroma444
  169.                             s->dsp.idct_put(dest_cb + 8,              dct_linesize, block[8]);
  170.                             s->dsp.idct_put(dest_cr + 8,              dct_linesize, block[9]);
  171.                             s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  172.                             s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  173.                         }
  174.                     }
  175.                 }//gray
  176.             }
  177.         }
  178.         if(!readable){
  179.             s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);
  180.             s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  181.             s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  182.         }
  183.     }
  184. }
  185. //#ifdef CONFIG_ENCODERS
  186. //static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  187. //{
  188. //    static const char tab[64]=
  189. //        {3,2,2,1,1,1,1,1,
  190. //         1,1,1,1,1,1,1,1,
  191. //         1,1,1,1,1,1,1,1,
  192. //         0,0,0,0,0,0,0,0,
  193. //         0,0,0,0,0,0,0,0,
  194. //         0,0,0,0,0,0,0,0,
  195. //         0,0,0,0,0,0,0,0,
  196. //         0,0,0,0,0,0,0,0};
  197. //    int score=0;
  198. //    int run=0;
  199. //    int i;
  200. //    DCTELEM *block= s->block[n];
  201. //    const int last_index= s->block_last_index[n];
  202. //    int skip_dc;
  203. //    if(threshold<0){
  204. //        skip_dc=0;
  205. //        threshold= -threshold;
  206. //    }else
  207. //        skip_dc=1;
  208. //    /* are all which we could set to zero are allready zero? */
  209. //    if(last_index<=skip_dc - 1) return;
  210. //    for(i=0; i<=last_index; i++){
  211. //        const int j = s->intra_scantable.permutated[i];
  212. //        const int level = ABS(block[j]);
  213. //        if(level==1){
  214. //            if(skip_dc && i==0) continue;
  215. //            score+= tab[run];
  216. //            run=0;
  217. //        }else if(level>1){
  218. //            return;
  219. //        }else{
  220. //            run++;
  221. //        }
  222. //    }
  223. //    if(score >= threshold) return;
  224. //    for(i=skip_dc; i<=last_index; i++){
  225. //        const int j = s->intra_scantable.permutated[i];
  226. //        block[j]=0;
  227. //    }
  228. //    if(block[0]) s->block_last_index[n]= 0;
  229. //    else         s->block_last_index[n]= -1;
  230. //}
  231. //static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  232. //{
  233. //    int i;
  234. //    const int maxlevel= s->max_qcoeff;
  235. //    const int minlevel= s->min_qcoeff;
  236. //    int overflow=0;
  237. //    
  238. //    if(s->mb_intra){
  239. //        i=1; //skip clipping of intra dc
  240. //    }else
  241. //        i=0;
  242. //    
  243. //    for(;i<=last_index; i++){
  244. //        const int j= s->intra_scantable.permutated[i];
  245. //        int level = block[j];
  246. //       
  247. //        if     (level>maxlevel){
  248. //            level=maxlevel;
  249. //            overflow++;
  250. //        }else if(level<minlevel){
  251. //            level=minlevel;
  252. //            overflow++;
  253. //        }
  254. //        
  255. //        block[j]= level;
  256. //    }
  257. //    
  258. //    if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  259. //        av_log(s->avctx, AV_LOG_INFO, "warning, cliping %d dct coefficents to %d..%dn", overflow, minlevel, maxlevel);
  260. //}
  261. //#endif //CONFIG_ENCODERS
  262. /**
  263.  *
  264.  * @param h is the normal height, this will be reduced automatically if needed for the last row
  265.  */
  266. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  267.     if (s->avctx->draw_horiz_band) {
  268.         AVFrame *src;
  269.         int offset[4];
  270.         
  271.         if(s->picture_structure != PICT_FRAME){
  272.             h <<= 1;
  273.             y <<= 1;
  274.             if(s->first_field  && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  275.         }
  276.         h= FFMIN(h, s->height - y);
  277.         if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER)) 
  278.             src= (AVFrame*)s->current_picture_ptr;
  279.         else if(s->last_picture_ptr)
  280.             src= (AVFrame*)s->last_picture_ptr;
  281.         else
  282.             return;
  283.             
  284.         if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  285.             offset[0]=
  286.             offset[1]=
  287.             offset[2]=
  288.             offset[3]= 0;
  289.         }else{
  290.             offset[0]= y * s->linesize;;
  291.             offset[1]= 
  292.             offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  293.             offset[3]= 0;
  294.         }
  295.         emms_c();
  296.         s->avctx->draw_horiz_band(s->avctx, src, offset,
  297.                                   y, s->picture_structure, h);
  298.     }
  299. }
  300. //void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  301. //    const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  302. //    const int uvlinesize= s->current_picture.linesize[1];
  303. //        
  304. //    s->block_index[0]= s->b8_stride*(s->mb_y*2    ) - 2 + s->mb_x*2;
  305. //    s->block_index[1]= s->b8_stride*(s->mb_y*2    ) - 1 + s->mb_x*2;
  306. //    s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  307. //    s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  308. //    s->block_index[4]= s->mb_stride*(s->mb_y + 1)                + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  309. //    s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  310. //    //block_index is not used by mpeg2, so it is not affected by chroma_format
  311. //    s->dest[0] = s->current_picture.data[0] + (s->mb_x - 1)*16;
  312. //    s->dest[1] = s->current_picture.data[1] + (s->mb_x - 1)*(16 >> s->chroma_x_shift);
  313. //    s->dest[2] = s->current_picture.data[2] + (s->mb_x - 1)*(16 >> s->chroma_x_shift);
  314. //    
  315. //    if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  316. //    {
  317. //        s->dest[0] += s->mb_y *   linesize * 16;
  318. //        s->dest[1] += s->mb_y * uvlinesize * (16 >> s->chroma_y_shift);
  319. //        s->dest[2] += s->mb_y * uvlinesize * (16 >> s->chroma_y_shift);
  320. //    }
  321. //}
  322. #ifdef CONFIG_ENCODERS
  323. static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){
  324.     int x, y;
  325. //FIXME optimize
  326.     for(y=0; y<8; y++){
  327.         for(x=0; x<8; x++){
  328.             int x2, y2;
  329.             int sum=0;
  330.             int sqr=0;
  331.             int count=0;
  332.             for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
  333.                 for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
  334.                     int v= ptr[x2 + y2*stride];
  335.                     sum += v;
  336.                     sqr += v*v;
  337.                     count++;
  338.                 }
  339.             }
  340.             weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
  341.         }
  342.     }
  343. }
  344. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  345. {
  346.     int16_t weight[6][64];
  347.     DCTELEM orig[6][64];
  348.     const int mb_x= s->mb_x;
  349.     const int mb_y= s->mb_y;
  350.     int i;
  351.     int skip_dct[6];
  352.     int dct_offset   = s->linesize*8; //default for progressive frames
  353.     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  354.     int wrap_y, wrap_c;
  355.     
  356.     for(i=0; i<6; i++) skip_dct[i]=0;
  357.     
  358.     if(s->adaptive_quant){
  359.         const int last_qp= s->qscale;
  360.         const int mb_xy= mb_x + mb_y*s->mb_stride;
  361.         s->lambda= s->lambda_table[mb_xy];
  362.         update_qscale(s);
  363.     
  364.         if(!(s->flags&CODEC_FLAG_QP_RD)){
  365.             s->dquant= s->qscale - last_qp;
  366.             if(s->out_format==FMT_H263){
  367.                 s->dquant= clip(s->dquant, -2, 2); //FIXME RD
  368.             
  369.                 if(s->codec_id==CODEC_ID_MPEG4){        
  370.                     if(!s->mb_intra){
  371.                         if(s->pict_type == B_TYPE){
  372.                             if(s->dquant&1) 
  373.                                 s->dquant= (s->dquant/2)*2;
  374.                             if(s->mv_dir&MV_DIRECT)
  375.                                 s->dquant= 0;
  376.                         }
  377.                         if(s->mv_type==MV_TYPE_8X8)
  378.                             s->dquant=0;
  379.                     }
  380.                 }
  381.             }
  382.         }
  383.         ff_set_qscale(s, last_qp + s->dquant);
  384.     }else if(s->flags&CODEC_FLAG_QP_RD)
  385.         ff_set_qscale(s, s->qscale + s->dquant);
  386.     wrap_y = s->linesize;
  387.     wrap_c = s->uvlinesize;
  388.     ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  389.     ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  390.     ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  391.     if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  392.         ff_emulated_edge_mc(s->edge_emu_buffer            , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width   , s->height);
  393.         ptr_y= s->edge_emu_buffer;
  394.         ff_emulated_edge_mc(s->edge_emu_buffer+18*wrap_y  , ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  395.         ptr_cb= s->edge_emu_buffer+18*wrap_y;
  396.         ff_emulated_edge_mc(s->edge_emu_buffer+18*wrap_y+9, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  397.         ptr_cr= s->edge_emu_buffer+18*wrap_y+9;
  398.     }
  399.     if (s->mb_intra) {
  400.         if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  401.             int progressive_score, interlaced_score;
  402.             s->interlaced_dct=0;
  403.             progressive_score= s->dsp.ildct_cmp[4](s, ptr_y           , NULL, wrap_y, 8) 
  404.                               +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
  405.             if(progressive_score > 0){
  406.                 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y           , NULL, wrap_y*2, 8) 
  407.                                   +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y  , NULL, wrap_y*2, 8);
  408.                 if(progressive_score > interlaced_score){
  409.                     s->interlaced_dct=1;
  410.             
  411.                     dct_offset= wrap_y;
  412.                     wrap_y<<=1;
  413.                 }
  414.             }
  415.         }
  416.         
  417. s->dsp.get_pixels(s->block[0], ptr_y                 , wrap_y);
  418.         s->dsp.get_pixels(s->block[1], ptr_y              + 8, wrap_y);
  419.         s->dsp.get_pixels(s->block[2], ptr_y + dct_offset    , wrap_y);
  420.         s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  421.         if(s->flags&CODEC_FLAG_GRAY){
  422.             skip_dct[4]= 1;
  423.             skip_dct[5]= 1;
  424.         }else{
  425.     s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  426.             s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  427.         }
  428.     }else{
  429.         op_pixels_func (*op_pix)[4];
  430.         qpel_mc_func (*op_qpix)[16];
  431.         uint8_t *dest_y, *dest_cb, *dest_cr;
  432.         dest_y  = s->dest[0];
  433.         dest_cb = s->dest[1];
  434.         dest_cr = s->dest[2];
  435.         if ((!s->no_rounding) || s->pict_type==B_TYPE){
  436.     op_pix = s->dsp.put_pixels_tab;
  437.             op_qpix= s->dsp.put_qpel_pixels_tab;
  438.         }else{
  439.             op_pix = s->dsp.put_no_rnd_pixels_tab;
  440.             op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  441.         }
  442.         if (s->mv_dir & MV_DIR_FORWARD) {
  443.             MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  444.             op_pix = s->dsp.avg_pixels_tab;
  445.             op_qpix= s->dsp.avg_qpel_pixels_tab;
  446.         }
  447.         if (s->mv_dir & MV_DIR_BACKWARD) {
  448.             MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  449.         }
  450.         if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  451.             int progressive_score, interlaced_score;
  452.             s->interlaced_dct=0;
  453.             progressive_score= s->dsp.ildct_cmp[0](s, dest_y           , ptr_y           , wrap_y, 8) 
  454.                               +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
  455.             
  456.             if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
  457.             if(progressive_score>0){
  458.                 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y           , ptr_y           , wrap_y*2, 8) 
  459.                                   +s->dsp.ildct_cmp[0](s, dest_y + wrap_y  , ptr_y + wrap_y  , wrap_y*2, 8);
  460.             
  461.                 if(progressive_score > interlaced_score){
  462.                     s->interlaced_dct=1;
  463.             
  464.                     dct_offset= wrap_y;
  465.                     wrap_y<<=1;
  466.                 }
  467.             }
  468.         }
  469.         
  470. s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
  471.         s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
  472.         s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
  473.         s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  474.         
  475.         if(s->flags&CODEC_FLAG_GRAY){
  476.             skip_dct[4]= 1;
  477.             skip_dct[5]= 1;
  478.         }else{
  479.             s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  480.             s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  481.         }
  482.         /* pre quantization */         
  483.         if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  484.             //FIXME optimize
  485.     if(s->dsp.sad[1](NULL, ptr_y               , dest_y               , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
  486.             if(s->dsp.sad[1](NULL, ptr_y            + 8, dest_y            + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
  487.             if(s->dsp.sad[1](NULL, ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
  488.             if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
  489.             if(s->dsp.sad[1](NULL, ptr_cb              , dest_cb              , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
  490.             if(s->dsp.sad[1](NULL, ptr_cr              , dest_cr              , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
  491.         }
  492.     }
  493.     if(s->avctx->quantizer_noise_shaping){
  494.         if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y                 , wrap_y);
  495.         if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y              + 8, wrap_y);
  496.         if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
  497.         if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  498.         if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb                , wrap_c);
  499.         if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr                , wrap_c);
  500.         memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
  501.     }
  502.             
  503.     /* DCT & quantize */
  504.     assert(s->out_format!=FMT_MJPEG || s->qscale==8);
  505.     {
  506.         for(i=0;i<6;i++) {
  507.             if(!skip_dct[i]){
  508.                 int overflow;
  509.                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  510.             // FIXME we could decide to change to quantizer instead of clipping
  511.             // JS: I don't think that would be a good idea it could lower quality instead
  512.             //     of improve it. Just INTRADC clipping deserves changes in quantizer
  513.                 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  514.             }else
  515.                 s->block_last_index[i]= -1;
  516.         }
  517.         if(s->avctx->quantizer_noise_shaping){
  518.             for(i=0;i<6;i++) {
  519.                 if(!skip_dct[i]){
  520.                     s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
  521.                 }
  522.             }
  523.         }
  524.         
  525.         if(s->luma_elim_threshold && !s->mb_intra)
  526.             for(i=0; i<4; i++)
  527.                 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  528.         if(s->chroma_elim_threshold && !s->mb_intra)
  529.             for(i=4; i<6; i++)
  530.                 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  531.         if(s->flags & CODEC_FLAG_CBP_RD){
  532.             for(i=0;i<6;i++) {
  533.                 if(s->block_last_index[i] == -1)
  534.                     s->coded_score[i]= INT_MAX/256;
  535.             }
  536.         }
  537.     }
  538.     if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  539.         s->block_last_index[4]=
  540.         s->block_last_index[5]= 0;
  541.         s->block[4][0]=
  542.         s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  543.     }
  544.     //non c quantize code returns incorrect block_last_index FIXME
  545.     if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
  546.         for(i=0; i<6; i++){
  547.             int j;
  548.             if(s->block_last_index[i]>0){
  549.                 for(j=63; j>0; j--){
  550.                     if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
  551.                 }
  552.                 s->block_last_index[i]= j;
  553.             }
  554.         }
  555.     }
  556.     /* huffman encode */
  557.     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  558.     case CODEC_ID_MPEG1VIDEO:
  559.     case CODEC_ID_MPEG2VIDEO:
  560.         mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  561. #ifdef CONFIG_RISKY
  562.     case CODEC_ID_MPEG4:
  563.         mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  564.     case CODEC_ID_MSMPEG4V2:
  565.     case CODEC_ID_MSMPEG4V3:
  566.     case CODEC_ID_WMV1:
  567.         msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  568.     case CODEC_ID_WMV2:
  569.          ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  570.     case CODEC_ID_H263:
  571.     case CODEC_ID_H263P:
  572.     case CODEC_ID_FLV1:
  573.     case CODEC_ID_RV10:
  574.         h263_encode_mb(s, s->block, motion_x, motion_y); break;
  575. #endif
  576.     case CODEC_ID_MJPEG:
  577.         mjpeg_encode_mb(s, s->block); break;
  578.     default:
  579.         assert(0);
  580.     }
  581. }
  582. #endif //CONFIG_ENCODERS
  583. //void ff_mpeg_flush(AVCodecContext *avctx){
  584. //    int i;
  585. //    MpegEncContext *s = avctx->priv_data;
  586. //    
  587. //    if(s==NULL || s->picture==NULL) 
  588. //        return;
  589. //    
  590. //    for(i=0; i<MAX_PICTURE_COUNT; i++){
  591. //       if(s->picture[i].data[0] && (   s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  592. //                                    || s->picture[i].type == FF_BUFFER_TYPE_USER))
  593. //        avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  594. //    }
  595. //    s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  596. //    
  597. //    s->parse_context.state= -1;
  598. //    s->parse_context.frame_start_found= 0;
  599. //    s->parse_context.overread= 0;
  600. //    s->parse_context.overread_index= 0;
  601. //    s->parse_context.index= 0;
  602. //    s->parse_context.last_index= 0;
  603. //    s->bitstream_buffer_size=0;
  604. //}
  605. //#ifdef CONFIG_ENCODERS
  606. //void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  607. //{
  608. //    const uint16_t *srcw= (uint16_t*)src;
  609. //    int words= length>>4;
  610. //    int bits= length&15;
  611. //    int i;
  612. //    if(length==0) return;
  613. //    
  614. //    if(words < 16){
  615. //        for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  616. //    }else if(put_bits_count(pb)&7){
  617. //        for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  618. //    }else{
  619. //        for(i=0; put_bits_count(pb)&31; i++)
  620. //            put_bits(pb, 8, src[i]);
  621. //        flush_put_bits(pb);
  622. //        memcpy(pbBufPtr(pb), src+i, 2*words-i);
  623. //        skip_put_bytes(pb, 2*words-i);
  624. //    }
  625. //        
  626. //    put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
  627. //}
  628. //static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  629. //    int i;
  630. //    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  631. //    /* mpeg1 */
  632. //    d->mb_skip_run= s->mb_skip_run;
  633. //    for(i=0; i<3; i++)
  634. //        d->last_dc[i]= s->last_dc[i];
  635. //    
  636. //    /* statistics */
  637. //    d->mv_bits= s->mv_bits;
  638. //    d->i_tex_bits= s->i_tex_bits;
  639. //    d->p_tex_bits= s->p_tex_bits;
  640. //    d->i_count= s->i_count;
  641. //    d->f_count= s->f_count;
  642. //    d->b_count= s->b_count;
  643. //    d->skip_count= s->skip_count;
  644. //    d->misc_bits= s->misc_bits;
  645. //    d->last_bits= 0;
  646. //    d->mb_skiped= 0;
  647. //    d->qscale= s->qscale;
  648. //    d->dquant= s->dquant;
  649. //}
  650. //static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  651. //    int i;
  652. //    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
  653. //    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  654. //    
  655. //    /* mpeg1 */
  656. //    d->mb_skip_run= s->mb_skip_run;
  657. //    for(i=0; i<3; i++)
  658. //        d->last_dc[i]= s->last_dc[i];
  659. //    
  660. //    /* statistics */
  661. //    d->mv_bits= s->mv_bits;
  662. //    d->i_tex_bits= s->i_tex_bits;
  663. //    d->p_tex_bits= s->p_tex_bits;
  664. //    d->i_count= s->i_count;
  665. //    d->f_count= s->f_count;
  666. //    d->b_count= s->b_count;
  667. //    d->skip_count= s->skip_count;
  668. //    d->misc_bits= s->misc_bits;
  669. //    d->mb_intra= s->mb_intra;
  670. //    d->mb_skiped= s->mb_skiped;
  671. //    d->mv_type= s->mv_type;
  672. //    d->mv_dir= s->mv_dir;
  673. //    d->pb= s->pb;
  674. //    if(s->data_partitioning){
  675. //        d->pb2= s->pb2;
  676. //        d->tex_pb= s->tex_pb;
  677. //    }
  678. //    d->block= s->block;
  679. //    for(i=0; i<6; i++)
  680. //        d->block_last_index[i]= s->block_last_index[i];
  681. //    d->interlaced_dct= s->interlaced_dct;
  682. //    d->qscale= s->qscale;
  683. //}
  684. //static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
  685. //                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  686. //                           int *dmin, int *next_block, int motion_x, int motion_y)
  687. //{
  688. //    int score;
  689. //    uint8_t *dest_backup[3];
  690. //    
  691. //    copy_context_before_encode(s, backup, type);
  692. //    s->block= s->blocks[*next_block];
  693. //    s->pb= pb[*next_block];
  694. //    if(s->data_partitioning){
  695. //        s->pb2   = pb2   [*next_block];
  696. //        s->tex_pb= tex_pb[*next_block];
  697. //    }
  698. //    
  699. //    if(*next_block){
  700. //        memcpy(dest_backup, s->dest, sizeof(s->dest));
  701. //        s->dest[0] = s->rd_scratchpad;
  702. //        s->dest[1] = s->rd_scratchpad + 16*s->linesize;
  703. //        s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
  704. //        assert(s->linesize >= 32); //FIXME
  705. //    }
  706. //    encode_mb(s, motion_x, motion_y);
  707. //    
  708. //    score= put_bits_count(&s->pb);
  709. //    if(s->data_partitioning){
  710. //        score+= put_bits_count(&s->pb2);
  711. //        score+= put_bits_count(&s->tex_pb);
  712. //    }
  713. //   
  714. //    if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  715. //        MPV_decode_mb(s, s->block);
  716. //        score *= s->lambda2;
  717. //        score += sse_mb(s) << FF_LAMBDA_SHIFT;
  718. //    }
  719. //    
  720. //    if(*next_block){
  721. //        memcpy(s->dest, dest_backup, sizeof(s->dest));
  722. //    }
  723. //    if(score<*dmin){
  724. //        *dmin= score;
  725. //        *next_block^=1;
  726. //        copy_context_after_encode(best, s, type);
  727. //    }
  728. //}
  729. //                
  730. //static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  731. //    uint32_t *sq = squareTbl + 256;
  732. //    int acc=0;
  733. //    int x,y;
  734. //    
  735. //    if(w==16 && h==16) 
  736. //        return s->dsp.sse[0](NULL, src1, src2, stride, 16);
  737. //    else if(w==8 && h==8)
  738. //        return s->dsp.sse[1](NULL, src1, src2, stride, 8);
  739. //    
  740. //    for(y=0; y<h; y++){
  741. //        for(x=0; x<w; x++){
  742. //            acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  743. //        } 
  744. //    }
  745. //    
  746. //    assert(acc>=0);
  747. //    
  748. //    return acc;
  749. //}
  750. //static int sse_mb(MpegEncContext *s){
  751. //    int w= 16;
  752. //    int h= 16;
  753. //    if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  754. //    if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  755. //    if(w==16 && h==16)
  756. //      if(s->avctx->mb_cmp == FF_CMP_NSSE){
  757. //        return  s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  758. //               +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  759. //               +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  760. //      }else{
  761. //        return  s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  762. //               +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  763. //               +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  764. //      }
  765. //    else
  766. //        return  sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
  767. //               +sse(s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
  768. //               +sse(s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
  769. //}
  770. //static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  771. //    MpegEncContext *s= arg;
  772. //    
  773. //    s->me.pre_pass=1;
  774. //    s->me.dia_size= s->avctx->pre_dia_size;
  775. //    s->first_slice_line=1;
  776. //    for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  777. //        for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  778. //            ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  779. //        }
  780. //        s->first_slice_line=0;
  781. //    }
  782. //    
  783. //    s->me.pre_pass=0;
  784. //    
  785. //    return 0;
  786. //}
  787. //static int estimate_motion_thread(AVCodecContext *c, void *arg){
  788. //    MpegEncContext *s= arg;
  789. //    s->me.dia_size= s->avctx->dia_size;
  790. //    s->first_slice_line=1;
  791. //    for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  792. //        s->mb_x=0; //for block init below
  793. //        ff_init_block_index(s);
  794. //        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  795. //            s->block_index[0]+=2;
  796. //            s->block_index[1]+=2;
  797. //            s->block_index[2]+=2;
  798. //            s->block_index[3]+=2;
  799. //            
  800. //            /* compute motion vector & mb_type and store in context */
  801. //            if(s->pict_type==B_TYPE)
  802. //                ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  803. //            else
  804. //                ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  805. //        }
  806. //        s->first_slice_line=0;
  807. //    }
  808. //    return 0;
  809. //}
  810. //static int mb_var_thread(AVCodecContext *c, void *arg){
  811. //    MpegEncContext *s= arg;
  812. //    int mb_x, mb_y;
  813. //    for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  814. //        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  815. //            int xx = mb_x * 16;
  816. //            int yy = mb_y * 16;
  817. //            uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  818. //            int varc;
  819. //            int sum = s->dsp.pix_sum(pix, s->linesize);
  820. //    
  821. //            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  822. //            s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  823. //            s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  824. //            s->me.mb_var_sum_temp    += varc;
  825. //        }
  826. //    }
  827. //    return 0;
  828. //}
  829. //static void write_slice_end(MpegEncContext *s){
  830. //    if(s->codec_id==CODEC_ID_MPEG4){
  831. //        if(s->partitioned_frame){
  832. //            ff_mpeg4_merge_partitions(s);
  833. //        }
  834. //    
  835. //        ff_mpeg4_stuffing(&s->pb);
  836. //    }else if(s->out_format == FMT_MJPEG){
  837. //        ff_mjpeg_stuffing(&s->pb);
  838. //    }
  839. //    align_put_bits(&s->pb);
  840. //    flush_put_bits(&s->pb);
  841. //}
  842. //static int encode_thread(AVCodecContext *c, void *arg){
  843. //    MpegEncContext *s= arg;
  844. //    int mb_x, mb_y, pdif = 0;
  845. //    int i, j;
  846. //    MpegEncContext best_s, backup_s;
  847. //    uint8_t bit_buf[2][3000];
  848. //    uint8_t bit_buf2[2][3000];
  849. //    uint8_t bit_buf_tex[2][3000];
  850. //    PutBitContext pb[2], pb2[2], tex_pb[2];
  851. ////printf("%d->%dn", s->resync_mb_y, s->end_mb_y);
  852. //    for(i=0; i<2; i++){
  853. //        init_put_bits(&pb    [i], bit_buf    [i], 3000);
  854. //        init_put_bits(&pb2   [i], bit_buf2   [i], 3000);
  855. //        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
  856. //    }
  857. //    s->last_bits= put_bits_count(&s->pb);
  858. //    s->mv_bits=0;
  859. //    s->misc_bits=0;
  860. //    s->i_tex_bits=0;
  861. //    s->p_tex_bits=0;
  862. //    s->i_count=0;
  863. //    s->f_count=0;
  864. //    s->b_count=0;
  865. //    s->skip_count=0;
  866. //    for(i=0; i<3; i++){
  867. //        /* init last dc values */
  868. //        /* note: quant matrix value (8) is implied here */
  869. //        s->last_dc[i] = 128 << s->intra_dc_precision;
  870. //        
  871. //        s->current_picture_ptr->error[i] = 0;
  872. //    }
  873. //    s->mb_skip_run = 0;
  874. //    memset(s->last_mv, 0, sizeof(s->last_mv));
  875. //     
  876. //    s->last_mv_dir = 0;
  877. //#ifdef CONFIG_RISKY
  878. //    switch(s->codec_id){
  879. //    case CODEC_ID_H263:
  880. //    case CODEC_ID_H263P:
  881. //    case CODEC_ID_FLV1:
  882. //        s->gob_index = ff_h263_get_gob_height(s);
  883. //        break;
  884. //    case CODEC_ID_MPEG4:
  885. //        if(s->partitioned_frame)
  886. //            ff_mpeg4_init_partitions(s);
  887. //        break;
  888. //    }
  889. //#endif
  890. //    s->resync_mb_x=0;
  891. //    s->resync_mb_y=0; 
  892. //    s->first_slice_line = 1;
  893. //    s->ptr_lastgob = s->pb.buf;
  894. //    for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  895. ////    printf("row %d at %Xn", s->mb_y, (int)s);
  896. //        s->mb_x=0;
  897. //        s->mb_y= mb_y;
  898. //        ff_set_qscale(s, s->qscale);
  899. //        ff_init_block_index(s);
  900. //        
  901. //        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  902. //            const int xy= mb_y*s->mb_stride + mb_x;
  903. //            int mb_type= s->mb_type[xy];
  904. ////            int d;
  905. //            int dmin= INT_MAX;
  906. //            int dir;
  907. //            s->mb_x = mb_x;
  908. //            ff_update_block_index(s);
  909. //            /* write gob / video packet header  */
  910. //#ifdef CONFIG_RISKY
  911. //            if(s->rtp_mode){
  912. //                int current_packet_size, is_gob_start;
  913. //                
  914. //                current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  915. //                
  916. //                is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0; 
  917. //                
  918. //                if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  919. //                
  920. //                switch(s->codec_id){
  921. //                case CODEC_ID_H263:
  922. //                case CODEC_ID_H263P:
  923. //                    if(!s->h263_slice_structured)
  924. //                        if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  925. //                    break;
  926. //                case CODEC_ID_MPEG2VIDEO:
  927. //                    if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  928. //                case CODEC_ID_MPEG1VIDEO:
  929. //                    if(s->mb_skip_run) is_gob_start=0;
  930. //                    break;
  931. //                }
  932. //                if(is_gob_start){
  933. //                    if(s->start_mb_y != mb_y || mb_x!=0){
  934. //                        write_slice_end(s);
  935. //                        if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
  936. //                            ff_mpeg4_init_partitions(s);
  937. //                        }
  938. //                    }
  939. //                
  940. //                    assert((put_bits_count(&s->pb)&7) == 0);
  941. //                    current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  942. //                    
  943. //                    if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
  944. //                        int r= put_bits_count(&s->pb)/8 + s->picture_number + s->codec_id + s->mb_x + s->mb_y;
  945. //                        int d= 100 / s->avctx->error_rate;
  946. //                        if(r % d == 0){
  947. //                            current_packet_size=0;
  948. //#ifndef ALT_BITSTREAM_WRITER
  949. //                            s->pb.buf_ptr= s->ptr_lastgob;
  950. //#endif
  951. //                            assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
  952. //                        }
  953. //                    }
  954. //        
  955. //                    if (s->avctx->rtp_callback)
  956. //                        s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, 0);
  957. //                    
  958. //                    switch(s->codec_id){
  959. //                    case CODEC_ID_MPEG4:
  960. //                        ff_mpeg4_encode_video_packet_header(s);
  961. //                        ff_mpeg4_clean_buffers(s);
  962. //                    break;
  963. //                    case CODEC_ID_MPEG1VIDEO:
  964. //                    case CODEC_ID_MPEG2VIDEO:
  965. //                        ff_mpeg1_encode_slice_header(s);
  966. //                        ff_mpeg1_clean_buffers(s);
  967. //                    break;
  968. //                    case CODEC_ID_H263:
  969. //                    case CODEC_ID_H263P:
  970. //                        h263_encode_gob_header(s, mb_y);                       
  971. //                    break;
  972. //                    }
  973. //                    if(s->flags&CODEC_FLAG_PASS1){
  974. //                        int bits= put_bits_count(&s->pb);
  975. //                        s->misc_bits+= bits - s->last_bits;
  976. //                        s->last_bits= bits;
  977. //                    }
  978. //    
  979. //                    s->ptr_lastgob += current_packet_size;
  980. //                    s->first_slice_line=1;
  981. //                    s->resync_mb_x=mb_x;
  982. //                    s->resync_mb_y=mb_y;
  983. //                }
  984. //            }
  985. //#endif
  986. //            if(  (s->resync_mb_x   == s->mb_x)
  987. //               && s->resync_mb_y+1 == s->mb_y){
  988. //                s->first_slice_line=0; 
  989. //            }
  990. //            s->mb_skiped=0;
  991. //            s->dquant=0; //only for QP_RD
  992. //            if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible
  993. //                int next_block=0;
  994. //                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  995. //                copy_context_before_encode(&backup_s, s, -1);
  996. //                backup_s.pb= s->pb;
  997. //                best_s.data_partitioning= s->data_partitioning;
  998. //                best_s.partitioned_frame= s->partitioned_frame;
  999. //                if(s->data_partitioning){
  1000. //                    backup_s.pb2= s->pb2;
  1001. //                    backup_s.tex_pb= s->tex_pb;
  1002. //                }
  1003. //                if(mb_type&CANDIDATE_MB_TYPE_INTER){
  1004. //                    s->mv_dir = MV_DIR_FORWARD;
  1005. //                    s->mv_type = MV_TYPE_16X16;
  1006. //                    s->mb_intra= 0;
  1007. //                    s->mv[0][0][0] = s->p_mv_table[xy][0];
  1008. //                    s->mv[0][0][1] = s->p_mv_table[xy][1];
  1009. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb, 
  1010. //                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1011. //                }
  1012. //                if(mb_type&CANDIDATE_MB_TYPE_INTER_I){ 
  1013. //                    s->mv_dir = MV_DIR_FORWARD;
  1014. //                    s->mv_type = MV_TYPE_FIELD;
  1015. //                    s->mb_intra= 0;
  1016. //                    for(i=0; i<2; i++){
  1017. //                        j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  1018. //                        s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  1019. //                        s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  1020. //                    }
  1021. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb, 
  1022. //                                 &dmin, &next_block, 0, 0);
  1023. //                }
  1024. //                if(mb_type&CANDIDATE_MB_TYPE_SKIPED){
  1025. //                    s->mv_dir = MV_DIR_FORWARD;
  1026. //                    s->mv_type = MV_TYPE_16X16;
  1027. //                    s->mb_intra= 0;
  1028. //                    s->mv[0][0][0] = 0;
  1029. //                    s->mv[0][0][1] = 0;
  1030. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPED, pb, pb2, tex_pb, 
  1031. //                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1032. //                }
  1033. //                if(mb_type&CANDIDATE_MB_TYPE_INTER4V){                 
  1034. //                    s->mv_dir = MV_DIR_FORWARD;
  1035. //                    s->mv_type = MV_TYPE_8X8;
  1036. //                    s->mb_intra= 0;
  1037. //                    for(i=0; i<4; i++){
  1038. //                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  1039. //                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  1040. //                    }
  1041. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb, 
  1042. //                                 &dmin, &next_block, 0, 0);
  1043. //                }
  1044. //                if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  1045. //                    s->mv_dir = MV_DIR_FORWARD;
  1046. //                    s->mv_type = MV_TYPE_16X16;
  1047. //                    s->mb_intra= 0;
  1048. //                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1049. //                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1050. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb, 
  1051. //                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1052. //                }
  1053. //                if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  1054. //                    s->mv_dir = MV_DIR_BACKWARD;
  1055. //                    s->mv_type = MV_TYPE_16X16;
  1056. //                    s->mb_intra= 0;
  1057. //                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1058. //                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1059. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
  1060. //                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1061. //                }
  1062. //                if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  1063. //                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1064. //                    s->mv_type = MV_TYPE_16X16;
  1065. //                    s->mb_intra= 0;
  1066. //                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1067. //                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1068. //                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1069. //                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1070. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb, 
  1071. //                                 &dmin, &next_block, 0, 0);
  1072. //                }
  1073. //                if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
  1074. //                    int mx= s->b_direct_mv_table[xy][0];
  1075. //                    int my= s->b_direct_mv_table[xy][1];
  1076. //                    
  1077. //                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1078. //                    s->mb_intra= 0;
  1079. //#ifdef CONFIG_RISKY
  1080. //                    ff_mpeg4_set_direct_mv(s, mx, my);
  1081. //#endif
  1082. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb, 
  1083. //                                 &dmin, &next_block, mx, my);
  1084. //                }
  1085. //                if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){ 
  1086. //                    s->mv_dir = MV_DIR_FORWARD;
  1087. //                    s->mv_type = MV_TYPE_FIELD;
  1088. //                    s->mb_intra= 0;
  1089. //                    for(i=0; i<2; i++){
  1090. //                        j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  1091. //                        s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  1092. //                        s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  1093. //                    }
  1094. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb, 
  1095. //                                 &dmin, &next_block, 0, 0);
  1096. //                }
  1097. //                if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){ 
  1098. //                    s->mv_dir = MV_DIR_BACKWARD;
  1099. //                    s->mv_type = MV_TYPE_FIELD;
  1100. //                    s->mb_intra= 0;
  1101. //                    for(i=0; i<2; i++){
  1102. //                        j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  1103. //                        s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  1104. //                        s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  1105. //                    }
  1106. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb, 
  1107. //                                 &dmin, &next_block, 0, 0);
  1108. //                }
  1109. //                if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){ 
  1110. //                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1111. //                    s->mv_type = MV_TYPE_FIELD;
  1112. //                    s->mb_intra= 0;
  1113. //                    for(dir=0; dir<2; dir++){
  1114. //                        for(i=0; i<2; i++){
  1115. //                            j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  1116. //                            s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  1117. //                            s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  1118. //                        }
  1119. //                    }
  1120. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb, 
  1121. //                                 &dmin, &next_block, 0, 0);
  1122. //                }
  1123. //                if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  1124. //                    s->mv_dir = 0;
  1125. //                    s->mv_type = MV_TYPE_16X16;
  1126. //                    s->mb_intra= 1;
  1127. //                    s->mv[0][0][0] = 0;
  1128. //                    s->mv[0][0][1] = 0;
  1129. //                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb, 
  1130. //                                 &dmin, &next_block, 0, 0);
  1131. //                    if(s->h263_pred || s->h263_aic){
  1132. //                        if(best_s.mb_intra)
  1133. //                            s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  1134. //                        else
  1135. //                            ff_clean_intra_table_entries(s); //old mode?
  1136. //                    }
  1137. //                }
  1138. //                if(s->flags & CODEC_FLAG_QP_RD){
  1139. //                    if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
  1140. //                        const int last_qp= backup_s.qscale;
  1141. //                        int dquant, dir, qp, dc[6];
  1142. //                        DCTELEM ac[6][16];
  1143. //                        const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  1144. //                        
  1145. //                        assert(backup_s.dquant == 0);
  1146. //                        //FIXME intra
  1147. //                        s->mv_dir= best_s.mv_dir;
  1148. //                        s->mv_type = MV_TYPE_16X16;
  1149. //                        s->mb_intra= best_s.mb_intra;
  1150. //                        s->mv[0][0][0] = best_s.mv[0][0][0];
  1151. //                        s->mv[0][0][1] = best_s.mv[0][0][1];
  1152. //                        s->mv[1][0][0] = best_s.mv[1][0][0];
  1153. //                        s->mv[1][0][1] = best_s.mv[1][0][1];
  1154. //                        
  1155. //                        dir= s->pict_type == B_TYPE ? 2 : 1;
  1156. //                        if(last_qp + dir > s->avctx->qmax) dir= -dir;
  1157. //                        for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
  1158. //                            qp= last_qp + dquant;
  1159. //                            if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  1160. //                                break;
  1161. //                            backup_s.dquant= dquant;
  1162. //                            if(s->mb_intra){
  1163. //                                for(i=0; i<6; i++){
  1164. //                                    dc[i]= s->dc_val[0][ s->block_index[i] ];
  1165. //                                    memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  1166. //                                }
  1167. //                            }
  1168. //                            encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, 
  1169. //                                         &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  1170. //                            if(best_s.qscale != qp){
  1171. //                                if(s->mb_intra){
  1172. //                                    for(i=0; i<6; i++){
  1173. //                                        s->dc_val[0][ s->block_index[i] ]= dc[i];
  1174. //                                        memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  1175. //                                    }
  1176. //                                }
  1177. //                                if(dir > 0 && dquant==dir){
  1178. //                                    dquant= 0;
  1179. //                                    dir= -dir;
  1180. //                                }else
  1181. //                                    break;
  1182. //                            }
  1183. //                        }
  1184. //                        qp= best_s.qscale;
  1185. //                        s->current_picture.qscale_table[xy]= qp;
  1186. //                    }
  1187. //                }
  1188. //                copy_context_after_encode(s, &best_s, -1);
  1189. //                
  1190. //                pb_bits_count= put_bits_count(&s->pb);
  1191. //                flush_put_bits(&s->pb);
  1192. //                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  1193. //                s->pb= backup_s.pb;
  1194. //                
  1195. //                if(s->data_partitioning){
  1196. //                    pb2_bits_count= put_bits_count(&s->pb2);
  1197. //                    flush_put_bits(&s->pb2);
  1198. //                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  1199. //                    s->pb2= backup_s.pb2;
  1200. //                    
  1201. //                    tex_pb_bits_count= put_bits_count(&s->tex_pb);
  1202. //                    flush_put_bits(&s->tex_pb);
  1203. //                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  1204. //                    s->tex_pb= backup_s.tex_pb;
  1205. //                }
  1206. //                s->last_bits= put_bits_count(&s->pb);
  1207. //               
  1208. //#ifdef CONFIG_RISKY
  1209. //                if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  1210. //                    ff_h263_update_motion_val(s);
  1211. //#endif
  1212. //        
  1213. //                if(next_block==0){ //FIXME 16 vs linesize16
  1214. //                    s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
  1215. //                    s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
  1216. //                    s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  1217. //                }
  1218. //                if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  1219. //                    MPV_decode_mb(s, s->block);
  1220. //            } else {
  1221. //                int motion_x, motion_y;
  1222. //                s->mv_type=MV_TYPE_16X16;
  1223. //                // only one MB-Type possible
  1224. //                
  1225. //                switch(mb_type){
  1226. //                case CANDIDATE_MB_TYPE_INTRA:
  1227. //                    s->mv_dir = 0;
  1228. //                    s->mb_intra= 1;
  1229. //                    motion_x= s->mv[0][0][0] = 0;
  1230. //                    motion_y= s->mv[0][0][1] = 0;
  1231. //                    break;
  1232. //                case CANDIDATE_MB_TYPE_INTER:
  1233. //                    s->mv_dir = MV_DIR_FORWARD;
  1234. //                    s->mb_intra= 0;
  1235. //                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1236. //                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1237. //                    break;
  1238. //                case CANDIDATE_MB_TYPE_INTER_I:
  1239. //                    s->mv_dir = MV_DIR_FORWARD;
  1240. //                    s->mv_type = MV_TYPE_FIELD;
  1241. //                    s->mb_intra= 0;
  1242. //                    for(i=0; i<2; i++){
  1243. //                        j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  1244. //                        s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  1245. //                        s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  1246. //                    }
  1247. //                    motion_x = motion_y = 0;
  1248. //                    break;
  1249. //                case CANDIDATE_MB_TYPE_INTER4V:
  1250. //                    s->mv_dir = MV_DIR_FORWARD;
  1251. //                    s->mv_type = MV_TYPE_8X8;
  1252. //                    s->mb_intra= 0;
  1253. //                    for(i=0; i<4; i++){
  1254. //                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  1255. //                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  1256. //                    }
  1257. //                    motion_x= motion_y= 0;
  1258. //                    break;
  1259. //                case CANDIDATE_MB_TYPE_DIRECT:
  1260. //                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1261. //                    s->mb_intra= 0;
  1262. //                    motion_x=s->b_direct_mv_table[xy][0];
  1263. //                    motion_y=s->b_direct_mv_table[xy][1];
  1264. //#ifdef CONFIG_RISKY
  1265. //                    ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  1266. //#endif
  1267. //                    break;
  1268. //                case CANDIDATE_MB_TYPE_BIDIR:
  1269. //                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1270. //                    s->mb_intra= 0;
  1271. //                    motion_x=0;
  1272. //                    motion_y=0;
  1273. //                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1274. //                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1275. //                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1276. //                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1277. //                    break;
  1278. //                case CANDIDATE_MB_TYPE_BACKWARD:
  1279. //                    s->mv_dir = MV_DIR_BACKWARD;
  1280. //                    s->mb_intra= 0;
  1281. //                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1282. //                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1283. //                    break;
  1284. //                case CANDIDATE_MB_TYPE_FORWARD:
  1285. //                    s->mv_dir = MV_DIR_FORWARD;
  1286. //                    s->mb_intra= 0;
  1287. //                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1288. //                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1289. ////                    printf(" %d %d ", motion_x, motion_y);
  1290. //                    break;
  1291. //                case CANDIDATE_MB_TYPE_FORWARD_I:
  1292. //                    s->mv_dir = MV_DIR_FORWARD;
  1293. //                    s->mv_type = MV_TYPE_FIELD;
  1294. //                    s->mb_intra= 0;
  1295. //                    for(i=0; i<2; i++){
  1296. //                        j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  1297. //                        s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  1298. //                        s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  1299. //                    }
  1300. //                    motion_x=motion_y=0;
  1301. //                    break;
  1302. //                case CANDIDATE_MB_TYPE_BACKWARD_I:
  1303. //                    s->mv_dir = MV_DIR_BACKWARD;
  1304. //                    s->mv_type = MV_TYPE_FIELD;
  1305. //                    s->mb_intra= 0;
  1306. //                    for(i=0; i<2; i++){
  1307. //                        j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  1308. //                        s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  1309. //                        s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  1310. //                    }
  1311. //                    motion_x=motion_y=0;
  1312. //                    break;
  1313. //                case CANDIDATE_MB_TYPE_BIDIR_I:
  1314. //                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1315. //                    s->mv_type = MV_TYPE_FIELD;
  1316. //                    s->mb_intra= 0;
  1317. //                    for(dir=0; dir<2; dir++){
  1318. //                        for(i=0; i<2; i++){
  1319. //                            j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  1320. //                            s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  1321. //                            s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  1322. //                        }
  1323. //                    }
  1324. //                    motion_x=motion_y=0;
  1325. //                    break;
  1326. //                default:
  1327. //                    motion_x=motion_y=0; //gcc warning fix
  1328. //                    av_log(s->avctx, AV_LOG_ERROR, "illegal MB typen");
  1329. //                }
  1330. //                encode_mb(s, motion_x, motion_y);
  1331. //                // RAL: Update last macrobloc type
  1332. //                s->last_mv_dir = s->mv_dir;
  1333. //            
  1334. //#ifdef CONFIG_RISKY
  1335. //                if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  1336. //                    ff_h263_update_motion_val(s);
  1337. //#endif
  1338. //
  1339. //                MPV_decode_mb(s, s->block);
  1340. //            }
  1341. //            /* clean the MV table in IPS frames for direct mode in B frames */
  1342. //            if(s->mb_intra /* && I,P,S_TYPE */){
  1343. //                s->p_mv_table[xy][0]=0;
  1344. //                s->p_mv_table[xy][1]=0;
  1345. //            }
  1346. //            
  1347. //            if(s->flags&CODEC_FLAG_PSNR){
  1348. //                int w= 16;
  1349. //                int h= 16;
  1350. //                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  1351. //                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  1352. //                s->current_picture_ptr->error[0] += sse(
  1353. //                    s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  1354. //                    s->dest[0], w, h, s->linesize);
  1355. //                s->current_picture_ptr->error[1] += sse(
  1356. //                    s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
  1357. //                    s->dest[1], w>>1, h>>1, s->uvlinesize);
  1358. //                s->current_picture_ptr->error[2] += sse(
  1359. //                    s, s->new_picture    .data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
  1360. //                    s->dest[2], w>>1, h>>1, s->uvlinesize);
  1361. //            }
  1362. //            if(s->loop_filter)
  1363. //                ff_h263_loop_filter(s);
  1364. ////printf("MB %d %d bitsn", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
  1365. //        }
  1366. //    }
  1367. //#ifdef CONFIG_RISKY
  1368. //    //not beautifull here but we must write it before flushing so it has to be here
  1369. //    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  1370. //        msmpeg4_encode_ext_header(s);
  1371. //#endif
  1372. //    write_slice_end(s);
  1373. //    /* Send the last GOB if RTP */    
  1374. //    if (s->avctx->rtp_callback) {
  1375. //        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1376. //        /* Call the RTP callback to send the last GOB */
  1377. //        emms_c();
  1378. //        s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, 0);
  1379. //    }
  1380. //    return 0;
  1381. //}
  1382. //#define MERGE(field) dst->field += src->field; src->field=0
  1383. //static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  1384. //    MERGE(me.scene_change_score);
  1385. //    MERGE(me.mc_mb_var_sum_temp);
  1386. //    MERGE(me.mb_var_sum_temp);
  1387. //}
  1388. //static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  1389. //    int i;
  1390. //    MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  1391. //    MERGE(dct_count[1]);
  1392. //    MERGE(mv_bits);
  1393. //    MERGE(header_bits);
  1394. //    MERGE(i_tex_bits);
  1395. //    MERGE(p_tex_bits);
  1396. //    MERGE(i_count);
  1397. //    MERGE(f_count);
  1398. //    MERGE(b_count);
  1399. //    MERGE(skip_count);
  1400. //    MERGE(misc_bits);
  1401. //    MERGE(error_count);
  1402. //    MERGE(padding_bug_score);
  1403. //    if(dst->avctx->noise_reduction){
  1404. //        for(i=0; i<64; i++){
  1405. //            MERGE(dct_error_sum[0][i]);
  1406. //            MERGE(dct_error_sum[1][i]);
  1407. //        }
  1408. //    }
  1409. //    
  1410. //    assert(put_bits_count(&src->pb) % 8 ==0);
  1411. //    assert(put_bits_count(&dst->pb) % 8 ==0);
  1412. //    ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  1413. //    flush_put_bits(&dst->pb);
  1414. //}
  1415. //static void encode_picture(MpegEncContext *s, int picture_number)
  1416. //{
  1417. //    int i;
  1418. //    int bits;
  1419. //    s->picture_number = picture_number;
  1420. //    
  1421. //    /* Reset the average MB variance */
  1422. //    s->me.mb_var_sum_temp    =
  1423. //    s->me.mc_mb_var_sum_temp = 0;
  1424. //#ifdef CONFIG_RISKY
  1425. //    /* we need to initialize some time vars before we can encode b-frames */
  1426. //    // RAL: Condition added for MPEG1VIDEO
  1427. //    if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  1428. //        ff_set_mpeg4_time(s, s->picture_number);  //FIXME rename and use has_b_frames or similar
  1429. //#endif
  1430. //        
  1431. //    s->me.scene_change_score=0;
  1432. //    
  1433. ////    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
  1434. //    
  1435. //    if(s->pict_type==I_TYPE){
  1436. //        if(s->msmpeg4_version >= 3) s->no_rounding=1;
  1437. //        else                        s->no_rounding=0;
  1438. //    }else if(s->pict_type!=B_TYPE){
  1439. //        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  1440. //            s->no_rounding ^= 1;          
  1441. //    }
  1442. //    
  1443. //    s->mb_intra=0; //for the rate distoration & bit compare functions
  1444. //    for(i=1; i<s->avctx->thread_count; i++){
  1445. //        ff_update_duplicate_context(s->thread_context[i], s);
  1446. //    }
  1447. //    ff_init_me(s);
  1448. //    /* Estimate motion for every MB */
  1449. //    if(s->pict_type != I_TYPE){
  1450. //        if(s->pict_type != B_TYPE && s->avctx->me_threshold==0){
  1451. //            if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  1452. //                s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  1453. //            }
  1454. //        }
  1455. //        s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  1456. //    }else /* if(s->pict_type == I_TYPE) */{
  1457. //        /* I-Frame */
  1458. //        for(i=0; i<s->mb_stride*s->mb_height; i++)
  1459. //            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  1460. //        
  1461. //        if(!s->fixed_qscale){
  1462. //            /* finding spatial complexity for I-frame rate control */
  1463. //            s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  1464. //        }
  1465. //    }
  1466. //    for(i=1; i<s->avctx->thread_count; i++){
  1467. //        merge_context_after_me(s, s->thread_context[i]);
  1468. //    }
  1469. //    s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  1470. //    s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
  1471. //    emms_c();
  1472. //    if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
  1473. //        s->pict_type= I_TYPE;
  1474. //        for(i=0; i<s->mb_stride*s->mb_height; i++)
  1475. //            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  1476. ////printf("Scene change detected, encoding as I Frame %d %dn", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  1477. //    }
  1478. //    if(!s->umvplus){
  1479. //        if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  1480. //            s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  1481. //            if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1482. //                int a,b;
  1483. //                a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  1484. //                b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  1485. //                s->f_code= FFMAX(s->f_code, FFMAX(a,b));
  1486. //            }
  1487. //                    
  1488. //            ff_fix_long_p_mvs(s);
  1489. //            ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  1490. //            if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1491. //                int j;
  1492. //                for(i=0; i<2; i++){
  1493. //                    for(j=0; j<2; j++)
  1494. //                        ff_fix_long_mvs(s, s->p_field_select_table[i], j, 
  1495. //                                        s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  1496. //                }
  1497. //            }
  1498. //        }
  1499. //        if(s->pict_type==B_TYPE){
  1500. //            int a, b;
  1501. //            a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  1502. //            b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  1503. //            s->f_code = FFMAX(a, b);
  1504. //            a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  1505. //            b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  1506. //            s->b_code = FFMAX(a, b);
  1507. //            ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  1508. //            ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  1509. //            ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  1510. //            ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  1511. //            if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1512. //                int dir, j;
  1513. //                for(dir=0; dir<2; dir++){
  1514. //                    for(i=0; i<2; i++){
  1515. //                        for(j=0; j<2; j++){
  1516. //                            int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I) 
  1517. //                                          : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  1518. //                            ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j, 
  1519. //                                            s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  1520. //                        }
  1521. //                    }
  1522. //                }
  1523. //            }
  1524. //        }
  1525. //    }
  1526. //    if (!s->fixed_qscale) 
  1527. //        s->current_picture.quality = ff_rate_estimate_qscale(s); //FIXME pic_ptr
  1528. //    if(s->adaptive_quant){
  1529. //#ifdef CONFIG_RISKY
  1530. //        switch(s->codec_id){
  1531. //        case CODEC_ID_MPEG4:
  1532. //            ff_clean_mpeg4_qscales(s);
  1533. //            break;
  1534. //        case CODEC_ID_H263:
  1535. //        case CODEC_ID_H263P:
  1536. //        case CODEC_ID_FLV1:
  1537. //            ff_clean_h263_qscales(s);
  1538. //            break;
  1539. //        }
  1540. //#endif
  1541. //        s->lambda= s->lambda_table[0];
  1542. //        //FIXME broken
  1543. //    }else
  1544. //        s->lambda= s->current_picture.quality;
  1545. ////printf("%d %dn", s->avctx->global_quality, s->current_picture.quality);
  1546. //    update_qscale(s);
  1547. //    
  1548. //    if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE)) 
  1549. //        s->qscale= 3; //reduce cliping problems
  1550. //        
  1551. //    if (s->out_format == FMT_MJPEG) {
  1552. //        /* for mjpeg, we do include qscale in the matrix */
  1553. //        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  1554. //        for(i=1;i<64;i++){
  1555. //            int j= s->dsp.idct_permutation[i];
  1556. //            s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  1557. //        }
  1558. //        convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 
  1559. //                       s->intra_matrix, s->intra_quant_bias, 8, 8);
  1560. //        s->qscale= 8;
  1561. //    }
  1562. //    
  1563. //    //FIXME var duplication
  1564. //    s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr
  1565. //    s->current_picture.pict_type= s->pict_type;
  1566. //    if(s->current_picture.key_frame)
  1567. //        s->picture_in_gop_number=0;
  1568. //    s->last_bits= put_bits_count(&s->pb);
  1569. //    switch(s->out_format) {
  1570. //    case FMT_MJPEG:
  1571. //        mjpeg_picture_header(s);
  1572. //        break;
  1573. //#ifdef CONFIG_RISKY
  1574. //    case FMT_H263:
  1575. //        if (s->codec_id == CODEC_ID_WMV2) 
  1576. //            ff_wmv2_encode_picture_header(s, picture_number);
  1577. //        else if (s->h263_msmpeg4) 
  1578. //            msmpeg4_encode_picture_header(s, picture_number);
  1579. //        else if (s->h263_pred)
  1580. //            mpeg4_encode_picture_header(s, picture_number);
  1581. //        else if (s->codec_id == CODEC_ID_RV10) 
  1582. //            rv10_encode_picture_header(s, picture_number);
  1583. //        else if (s->codec_id == CODEC_ID_FLV1)
  1584. //            ff_flv_encode_picture_header(s, picture_number);
  1585. //        else
  1586. //            h263_encode_picture_header(s, picture_number);
  1587. //        break;
  1588. //#endif
  1589. //    case FMT_MPEG1:
  1590. //        mpeg1_encode_picture_header(s, picture_number);
  1591. //        break;
  1592. //    case FMT_H264:
  1593. //        break;
  1594. //    default:
  1595. //        assert(0);
  1596. //    }
  1597. //    bits= put_bits_count(&s->pb);
  1598. //    s->header_bits= bits - s->last_bits;
  1599. //        
  1600. //    for(i=1; i<s->avctx->thread_count; i++){
  1601. //        update_duplicate_context_after_me(s->thread_context[i], s);
  1602. //    }
  1603. //    s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  1604. //    for(i=1; i<s->avctx->thread_count; i++){
  1605. //        merge_context_after_encode(s, s->thread_context[i]);
  1606. //    }
  1607. //    emms_c();
  1608. //}
  1609. //#endif //CONFIG_ENCODERS
  1610. //static void  denoise_dct_c(MpegEncContext *s, DCTELEM *block){
  1611. //    const int intra= s->mb_intra;
  1612. //    int i;
  1613. //    s->dct_count[intra]++;
  1614. //    for(i=0; i<64; i++){
  1615. //        int level= block[i];
  1616. //        if(level){
  1617. //            if(level>0){
  1618. //                s->dct_error_sum[intra][i] += level;
  1619. //                level -= s->dct_offset[intra][i];
  1620. //                if(level<0) level=0;
  1621. //            }else{
  1622. //                s->dct_error_sum[intra][i] -= level;
  1623. //                level += s->dct_offset[intra][i];
  1624. //                if(level>0) level=0;
  1625. //            }
  1626. //            block[i]= level;
  1627. //        }
  1628. //    }
  1629. //}
  1630. //#ifdef CONFIG_ENCODERS
  1631. //static int dct_quantize_trellis_c(MpegEncContext *s, 
  1632. //                        DCTELEM *block, int n,
  1633. //                        int qscale, int *overflow){
  1634. //    const int *qmat;
  1635. //    const uint8_t *scantable= s->intra_scantable.scantable;
  1636. //    const uint8_t *perm_scantable= s->intra_scantable.permutated;
  1637. //    int max=0;
  1638. //    unsigned int threshold1, threshold2;
  1639. //    int bias=0;
  1640. //    int run_tab[65];
  1641. //    int level_tab[65];
  1642. //    int score_tab[65];
  1643. //    int survivor[65];
  1644. //    int survivor_count;
  1645. //    int last_run=0;
  1646. //    int last_level=0;
  1647. //    int last_score= 0;
  1648. //    int last_i;
  1649. //    int coeff[2][64];
  1650. //    int coeff_count[64];
  1651. //    int qmul, qadd, start_i, last_non_zero, i, dc;
  1652. //    const int esc_length= s->ac_esc_length;
  1653. //    uint8_t * length;
  1654. //    uint8_t * last_length;
  1655. //    const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  1656. //        
  1657. //    s->dsp.fdct (block);
  1658. //    
  1659. //    if(s->dct_error_sum)
  1660. //        s->denoise_dct(s, block);
  1661. //    qmul= qscale*16;
  1662. //    qadd= ((qscale-1)|1)*8;
  1663. //    if (s->mb_intra) {
  1664. //        int q;
  1665. //        if (!s->h263_aic) {
  1666. //            if (n < 4)
  1667. //                q = s->y_dc_scale;
  1668. //            else
  1669. //                q = s->c_dc_scale;
  1670. //            q = q << 3;
  1671. //        } else{
  1672. //            /* For AIC we skip quant/dequant of INTRADC */
  1673. //            q = 1 << 3;
  1674. //            qadd=0;
  1675. //        }
  1676. //            
  1677. //        /* note: block[0] is assumed to be positive */
  1678. //        block[0] = (block[0] + (q >> 1)) / q;
  1679. //        start_i = 1;
  1680. //        last_non_zero = 0;
  1681. //        qmat = s->q_intra_matrix[qscale];
  1682. //        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  1683. //            bias= 1<<(QMAT_SHIFT-1);
  1684. //        length     = s->intra_ac_vlc_length;
  1685. //        last_length= s->intra_ac_vlc_last_length;
  1686. //    } else {
  1687. //        start_i = 0;
  1688. //        last_non_zero = -1;
  1689. //        qmat = s->q_inter_matrix[qscale];
  1690. //        length     = s->inter_ac_vlc_length;
  1691. //        last_length= s->inter_ac_vlc_last_length;
  1692. //    }
  1693. //    last_i= start_i;
  1694. //    threshold1= (1<<QMAT_SHIFT) - bias - 1;
  1695. //    threshold2= (threshold1<<1);
  1696. //    for(i=63; i>=start_i; i--) {
  1697. //        const int j = scantable[i];
  1698. //        int level = block[j] * qmat[j];
  1699. //        if(((unsigned)(level+threshold1))>threshold2){
  1700. //            last_non_zero = i;
  1701. //            break;
  1702. //        }
  1703. //    }
  1704. //    for(i=start_i; i<=last_non_zero; i++) {
  1705. //        const int j = scantable[i];
  1706. //        int level = block[j] * qmat[j];
  1707. ////        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
  1708. ////           || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1709. //        if(((unsigned)(level+threshold1))>threshold2){
  1710. //            if(level>0){
  1711. //                level= (bias + level)>>QMAT_SHIFT;
  1712. //                coeff[0][i]= level;
  1713. //                coeff[1][i]= level-1;
  1714. ////                coeff[2][k]= level-2;
  1715. //            }else{
  1716. //                level= (bias - level)>>QMAT_SHIFT;
  1717. //                coeff[0][i]= -level;
  1718. //                coeff[1][i]= -level+1;
  1719. ////                coeff[2][k]= -level+2;
  1720. //            }
  1721. //            coeff_count[i]= FFMIN(level, 2);
  1722. //            assert(coeff_count[i]);
  1723. //            max |=level;
  1724. //        }else{
  1725. //            coeff[0][i]= (level>>31)|1;
  1726. //            coeff_count[i]= 1;
  1727. //        }
  1728. //    }
  1729. //    
  1730. //    *overflow= s->max_qcoeff < max; //overflow might have happend
  1731. //    
  1732. //    if(last_non_zero < start_i){
  1733. //        memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  1734. //        return last_non_zero;
  1735. //    }
  1736. //    score_tab[start_i]= 0;
  1737. //    survivor[0]= start_i;
  1738. //    survivor_count= 1;
  1739. //    
  1740. //    for(i=start_i; i<=last_non_zero; i++){
  1741. //        int level_index, j;
  1742. //        const int dct_coeff= ABS(block[ scantable[i] ]);
  1743. //        const int zero_distoration= dct_coeff*dct_coeff;
  1744. //        int best_score=256*256*256*120;
  1745. //        for(level_index=0; level_index < coeff_count[i]; level_index++){
  1746. //            int distoration;
  1747. //            int level= coeff[level_index][i];
  1748. //            const int alevel= ABS(level);
  1749. //            int unquant_coeff;
  1750. //            
  1751. //            assert(level);
  1752. //            if(s->out_format == FMT_H263){
  1753. //                unquant_coeff= alevel*qmul + qadd;
  1754. //            }else{ //MPEG1
  1755. //                j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
  1756. //                if(s->mb_intra){
  1757. //                        unquant_coeff = (int)(  alevel  * qscale * s->intra_matrix[j]) >> 3;
  1758. //                        unquant_coeff =   (unquant_coeff - 1) | 1;
  1759. //                }else{
  1760. //                        unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  1761. //                        unquant_coeff =   (unquant_coeff - 1) | 1;
  1762. //                }
  1763. //                unquant_coeff<<= 3;
  1764. //            }
  1765. //            distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration;
  1766. //            level+=64;
  1767. //            if((level&(~127)) == 0){
  1768. //                for(j=survivor_count-1; j>=0; j--){
  1769. //                    int run= i - survivor[j];
  1770. //                    int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  1771. //                    score += score_tab[i-run];
  1772. //                    
  1773. //                    if(score < best_score){
  1774. //                        best_score= score;
  1775. //                        run_tab[i+1]= run;
  1776. //                        level_tab[i+1]= level-64;
  1777. //                    }
  1778. //                }
  1779. //                if(s->out_format == FMT_H263){
  1780. //                    for(j=survivor_count-1; j>=0; j--){
  1781. //                        int run= i - survivor[j];
  1782. //                        int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  1783. //                        score += score_tab[i-run];
  1784. //                        if(score < last_score){
  1785. //                            last_score= score;
  1786. //                            last_run= run;
  1787. //                            last_level= level-64;
  1788. //                            last_i= i+1;
  1789. //                        }
  1790. //                    }
  1791. //                }
  1792. //            }else{
  1793. //                distoration += esc_length*lambda;
  1794. //                for(j=survivor_count-1; j>=0; j--){
  1795. //                    int run= i - survivor[j];
  1796. //                    int score= distoration + score_tab[i-run];
  1797. //                    
  1798. //                    if(score < best_score){
  1799. //                        best_score= score;
  1800. //                        run_tab[i+1]= run;
  1801. //                        level_tab[i+1]= level-64;
  1802. //                    }
  1803. //                }
  1804. //                if(s->out_format == FMT_H263){
  1805. //                  for(j=survivor_count-1; j>=0; j--){
  1806. //                        int run= i - survivor[j];
  1807. //                        int score= distoration + score_tab[i-run];
  1808. //                        if(score < last_score){
  1809. //                            last_score= score;
  1810. //                            last_run= run;
  1811. //                            last_level= level-64;
  1812. //                            last_i= i+1;
  1813. //                        }
  1814. //                    }
  1815. //                }
  1816. //            }
  1817. //        }
  1818. //        
  1819. //        score_tab[i+1]= best_score;
  1820. //        //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  1821. //        if(last_non_zero <= 27){
  1822. //            for(; survivor_count; survivor_count--){
  1823. //                if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  1824. //                    break;
  1825. //            }
  1826. //        }else{
  1827. //            for(; survivor_count; survivor_count--){
  1828. //                if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  1829. //                    break;
  1830. //            }
  1831. //        }
  1832. //        survivor[ survivor_count++ ]= i+1;
  1833. //    }
  1834. //    if(s->out_format != FMT_H263){
  1835. //        last_score= 256*256*256*120;
  1836. //        for(i= survivor[0]; i<=last_non_zero + 1; i++){
  1837. //            int score= score_tab[i];
  1838. //            if(i) score += lambda*2; //FIXME exacter?
  1839. //            if(score < last_score){
  1840. //                last_score= score;
  1841. //                last_i= i;
  1842. //                last_level= level_tab[i];
  1843. //                last_run= run_tab[i];
  1844. //            }
  1845. //        }
  1846. //    }
  1847. //    s->coded_score[n] = last_score;
  1848. //    
  1849. //    dc= ABS(block[0]);
  1850. //    last_non_zero= last_i - 1;
  1851. //    memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  1852. //    
  1853. //    if(last_non_zero < start_i)
  1854. //        return last_non_zero;
  1855. //    if(last_non_zero == 0 && start_i == 0){
  1856. //        int best_level= 0;
  1857. //        int best_score= dc * dc;
  1858. //        
  1859. //        for(i=0; i<coeff_count[0]; i++){
  1860. //            int level= coeff[i][0];
  1861. //            int alevel= ABS(level);
  1862. //            int unquant_coeff, score, distortion;
  1863. //            if(s->out_format == FMT_H263){
  1864. //                    unquant_coeff= (alevel*qmul + qadd)>>3;
  1865. //            }else{ //MPEG1
  1866. //                    unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  1867. //                    unquant_coeff =   (unquant_coeff - 1) | 1;
  1868. //            }
  1869. //            unquant_coeff = (unquant_coeff + 4) >> 3;
  1870. //            unquant_coeff<<= 3 + 3;
  1871. //            distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  1872. //            level+=64;
  1873. //            if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  1874. //            else                    score= distortion + esc_length*lambda;
  1875. //            if(score < best_score){
  1876. //                best_score= score;
  1877. //                best_level= level - 64;
  1878. //            }
  1879. //        }
  1880. //        block[0]= best_level;
  1881. //        s->coded_score[n] = best_score - dc*dc;
  1882. //        if(best_level == 0) return -1;
  1883. //        else                return last_non_zero;
  1884. //    }
  1885. //    i= last_i;
  1886. //    assert(last_level);
  1887. //    block[ perm_scantable[last_non_zero] ]= last_level;
  1888. //    i -= last_run + 1;
  1889. //    
  1890. //    for(; i>start_i; i -= run_tab[i] + 1){
  1891. //        block[ perm_scantable[i-1] ]= level_tab[i];
  1892. //    }
  1893. //    return last_non_zero;
  1894. //}
  1895. ////#define REFINE_STATS 1
  1896. //static int16_t basis[64][64];
  1897. //static void build_basis(uint8_t *perm){
  1898. //    int i, j, x, y;
  1899. //    emms_c();
  1900. //    for(i=0; i<8; i++){
  1901. //        for(j=0; j<8; j++){
  1902. //            for(y=0; y<8; y++){
  1903. //                for(x=0; x<8; x++){
  1904. //                    double s= 0.25*(1<<BASIS_SHIFT);
  1905. //                    int index= 8*i + j;
  1906. //                    int perm_index= perm[index];
  1907. //                    if(i==0) s*= sqrt(0.5);
  1908. //                    if(j==0) s*= sqrt(0.5);
  1909. //                    basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
  1910. //                }
  1911. //            }
  1912. //        }
  1913. //    }
  1914. //}
  1915. //static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
  1916. //                        DCTELEM *block, int16_t *weight, DCTELEM *orig,
  1917. //                        int n, int qscale){
  1918. //    int16_t rem[64];
  1919. //    DCTELEM d1[64];
  1920. //    const int *qmat;
  1921. //    const uint8_t *scantable= s->intra_scantable.scantable;
  1922. //    const uint8_t *perm_scantable= s->intra_scantable.permutated;
  1923. ////    unsigned int threshold1, threshold2;
  1924. ////    int bias=0;
  1925. //    int run_tab[65];
  1926. //    int prev_run=0;
  1927. //    int prev_level=0;
  1928. //    int qmul, qadd, start_i, last_non_zero, i, dc;
  1929. //    uint8_t * length;
  1930. //    uint8_t * last_length;
  1931. //    int lambda;
  1932. //    int rle_index, run, q, sum;
  1933. //#ifdef REFINE_STATS
  1934. //static int count=0;
  1935. //static int after_last=0;
  1936. //static int to_zero=0;
  1937. //static int from_zero=0;
  1938. //static int raise=0;
  1939. //static int lower=0;
  1940. //static int messed_sign=0;
  1941. //#endif
  1942. //    if(basis[0][0] == 0)
  1943. //        build_basis(s->dsp.idct_permutation);
  1944. //    
  1945. //    qmul= qscale*2;
  1946. //    qadd= (qscale-1)|1;
  1947. //    if (s->mb_intra) {
  1948. //        if (!s->h263_aic) {
  1949. //            if (n < 4)
  1950. //                q = s->y_dc_scale;
  1951. //            else
  1952. //                q = s->c_dc_scale;
  1953. //        } else{
  1954. //            /* For AIC we skip quant/dequant of INTRADC */
  1955. //            q = 1;
  1956. //            qadd=0;
  1957. //        }
  1958. //        q <<= RECON_SHIFT-3;
  1959. //        /* note: block[0] is assumed to be positive */
  1960. //        dc= block[0]*q;
  1961. ////        block[0] = (block[0] + (q >> 1)) / q;
  1962. //        start_i = 1;
  1963. //        qmat = s->q_intra_matrix[qscale];
  1964. ////        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  1965. ////            bias= 1<<(QMAT_SHIFT-1);
  1966. //        length     = s->intra_ac_vlc_length;
  1967. //        last_length= s->intra_ac_vlc_last_length;
  1968. //    } else {
  1969. //        dc= 0;
  1970. //        start_i = 0;
  1971. //        qmat = s->q_inter_matrix[qscale];
  1972. //        length     = s->inter_ac_vlc_length;
  1973. //        last_length= s->inter_ac_vlc_last_length;
  1974. //    }
  1975. //    last_non_zero = s->block_last_index[n];
  1976. //#ifdef REFINE_STATS
  1977. //{START_TIMER
  1978. //#endif
  1979. //    dc += (1<<(RECON_SHIFT-1));
  1980. //    for(i=0; i<64; i++){
  1981. //        rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly insteadof copying to rem[]
  1982. //    }
  1983. //#ifdef REFINE_STATS
  1984. //STOP_TIMER("memset rem[]")}
  1985. //#endif
  1986. //    sum=0;
  1987. //    for(i=0; i<64; i++){
  1988. //        int one= 36;
  1989. //        int qns=4;
  1990. //        int w;
  1991. //        w= ABS(weight[i]) + qns*one;
  1992. //        w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  1993. //        weight[i] = w;
  1994. ////        w=weight[i] = (63*qns + (w/2)) / w;
  1995. //         
  1996. //        assert(w>0);
  1997. //        assert(w<(1<<6));
  1998. //        sum += w*w;
  1999. //    }
  2000. //    lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  2001. //#ifdef REFINE_STATS
  2002. //{START_TIMER
  2003. //#endif
  2004. //    run=0;
  2005. //    rle_index=0;
  2006. //    for(i=start_i; i<=last_non_zero; i++){
  2007. //        int j= perm_scantable[i];
  2008. //        const int level= block[j];
  2009. //        int coeff;
  2010. //        
  2011. //        if(level){
  2012. //            if(level<0) coeff= qmul*level - qadd;
  2013. //            else        coeff= qmul*level + qadd;
  2014. //            run_tab[rle_index++]=run;
  2015. //            run=0;
  2016. //            s->dsp.add_8x8basis(rem, basis[j], coeff);
  2017. //        }else{
  2018. //            run++;
  2019. //        }
  2020. //    }
  2021. //#ifdef REFINE_STATS
  2022. //if(last_non_zero>0){
  2023. //STOP_TIMER("init rem[]")
  2024. //}
  2025. //}
  2026. //{START_TIMER
  2027. //#endif
  2028. //    for(;;){
  2029. //        int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
  2030. //        int best_coeff=0;
  2031. //        int best_change=0;
  2032. //        int run2, best_unquant_change=0, analyze_gradient;
  2033. //#ifdef REFINE_STATS
  2034. //{START_TIMER
  2035. //#endif
  2036. //        analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
  2037. //        if(analyze_gradient){
  2038. //#ifdef REFINE_STATS
  2039. //{START_TIMER
  2040. //#endif
  2041. //            for(i=0; i<64; i++){
  2042. //                int w= weight[i];
  2043. //            
  2044. //                d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  2045. //            }
  2046. //#ifdef REFINE_STATS
  2047. //STOP_TIMER("rem*w*w")}
  2048. //{START_TIMER
  2049. //#endif
  2050. //            s->dsp.fdct(d1);
  2051. //#ifdef REFINE_STATS
  2052. //STOP_TIMER("dct")}
  2053. //#endif
  2054. //        }
  2055. //        if(start_i){
  2056. //            const int level= block[0];
  2057. //            int change, old_coeff;
  2058. //            assert(s->mb_intra);
  2059. //            
  2060. //            old_coeff= q*level;
  2061. //            
  2062. //            for(change=-1; change<=1; change+=2){
  2063. //                int new_level= level + change;
  2064. //                int score, new_coeff;
  2065. //                
  2066. //                new_coeff= q*new_level;
  2067. //                if(new_coeff >= 2048 || new_coeff < 0)
  2068. //                    continue;
  2069. //                score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
  2070. //                if(score<best_score){
  2071. //                    best_score= score;
  2072. //                    best_coeff= 0;
  2073. //                    best_change= change;
  2074. //                    best_unquant_change= new_coeff - old_coeff;
  2075. //                }
  2076. //            }
  2077. //        }
  2078. //        
  2079. //        run=0;
  2080. //        rle_index=0;
  2081. //        run2= run_tab[rle_index++];
  2082. //        prev_level=0;
  2083. //        prev_run=0;
  2084. //        for(i=start_i; i<64; i++){
  2085. //            int j= perm_scantable[i];
  2086. //            const int level= block[j];
  2087. //            int change, old_coeff;
  2088. //            if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  2089. //                break;
  2090. //            if(level){
  2091. //                if(level<0) old_coeff= qmul*level - qadd;
  2092. //                else        old_coeff= qmul*level + qadd;
  2093. //                run2= run_tab[rle_index++]; //FIXME ! maybe after last
  2094. //            }else{
  2095. //                old_coeff=0;
  2096. //                run2--;
  2097. //                assert(run2>=0 || i >= last_non_zero );
  2098. //            }
  2099. //            
  2100. //            for(change=-1; change<=1; change+=2){
  2101. //                int new_level= level + change;
  2102. //                int score, new_coeff, unquant_change;
  2103. //                
  2104. //                score=0;
  2105. //                if(s->avctx->quantizer_noise_shaping < 2 && ABS(new_level) > ABS(level))
  2106. //                   continue;
  2107. //                if(new_level){
  2108. //                    if(new_level<0) new_coeff= qmul*new_level - qadd;
  2109. //                    else            new_coeff= qmul*new_level + qadd;
  2110. //                    if(new_coeff >= 2048 || new_coeff <= -2048)
  2111. //                        continue;
  2112. //                    //FIXME check for overflow
  2113. //                    
  2114. //                    if(level){
  2115. //                        if(level < 63 && level > -63){
  2116. //                            if(i < last_non_zero)
  2117. //                                score +=   length[UNI_AC_ENC_INDEX(run, new_level+64)]
  2118. //                                         - length[UNI_AC_ENC_INDEX(run, level+64)];
  2119. //                            else
  2120. //                                score +=   last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  2121. //                                         - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  2122. //                        }
  2123. //                    }else{
  2124. //                        assert(ABS(new_level)==1);
  2125. //                        
  2126. //                        if(analyze_gradient){
  2127. //                            int g= d1[ scantable[i] ];
  2128. //                            if(g && (g^new_level) >= 0)
  2129. //                                continue;
  2130. //                        }
  2131. //                        if(i < last_non_zero){
  2132. //                            int next_i= i + run2 + 1;
  2133. //                            int next_level= block[ perm_scantable[next_i] ] + 64;
  2134. //                            
  2135. //                            if(next_level&(~127))
  2136. //                                next_level= 0;
  2137. //                            if(next_i < last_non_zero)
  2138. //                                score +=   length[UNI_AC_ENC_INDEX(run, 65)]
  2139. //                                         + length[UNI_AC_ENC_INDEX(run2, next_level)]
  2140. //                                         - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  2141. //                            else
  2142. //                                score +=  length[UNI_AC_ENC_INDEX(run, 65)]
  2143. //                                        + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  2144. //                                        - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  2145. //                        }else{
  2146. //                            score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  2147. //                            if(prev_level){
  2148. //                                score +=  length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  2149. //                                        - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  2150. //                            }
  2151. //                        }
  2152. //                    }
  2153. //                }else{
  2154. //                    new_coeff=0;
  2155. //                    assert(ABS(level)==1);
  2156. //                    if(i < last_non_zero){
  2157. //                        int next_i= i + run2 + 1;
  2158. //                        int next_level= block[ perm_scantable[next_i] ] + 64;
  2159. //                            
  2160. //                        if(next_level&(~127))
  2161. //                            next_level= 0;
  2162. //                        if(next_i < last_non_zero)
  2163. //                            score +=   length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  2164. //                                     - length[UNI_AC_ENC_INDEX(run2, next_level)]
  2165. //                                     - length[UNI_AC_ENC_INDEX(run, 65)];
  2166. //                        else
  2167. //                            score +=   last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  2168. //                                     - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  2169. //                                     - length[UNI_AC_ENC_INDEX(run, 65)];
  2170. //                    }else{
  2171. //                        score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  2172. //                        if(prev_level){
  2173. //                            score +=  last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  2174. //                                    - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  2175. //                        }
  2176. //                    }
  2177. //                }
  2178. //                
  2179. //                score *= lambda;
  2180. //                unquant_change= new_coeff - old_coeff;
  2181. //                assert((score < 100*lambda && score > -100*lambda) || lambda==0);
  2182. //                
  2183. //                score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
  2184. //                if(score<best_score){
  2185. //                    best_score= score;
  2186. //                    best_coeff= i;
  2187. //                    best_change= change;
  2188. //                    best_unquant_change= unquant_change;
  2189. //                }
  2190. //            }
  2191. //            if(level){
  2192. //                prev_level= level + 64;
  2193. //                if(prev_level&(~127))
  2194. //                    prev_level= 0;
  2195. //                prev_run= run;
  2196. //                run=0;
  2197. //            }else{
  2198. //                run++;
  2199. //            }
  2200. //        }
  2201. //#ifdef REFINE_STATS
  2202. //STOP_TIMER("iterative step")}
  2203. //#endif
  2204. //        if(best_change){
  2205. //            int j= perm_scantable[ best_coeff ];
  2206. //            
  2207. //            block[j] += best_change;
  2208. //            
  2209. //            if(best_coeff > last_non_zero){
  2210. //                last_non_zero= best_coeff;
  2211. //                assert(block[j]);
  2212. //#ifdef REFINE_STATS
  2213. //after_last++;
  2214. //#endif
  2215. //            }else{
  2216. //#ifdef REFINE_STATS
  2217. //if(block[j]){
  2218. //    if(block[j] - best_change){
  2219. //        if(ABS(block[j]) > ABS(block[j] - best_change)){
  2220. //            raise++;
  2221. //        }else{
  2222. //            lower++;
  2223. //        }
  2224. //    }else{
  2225. //        from_zero++;
  2226. //    }
  2227. //}else{
  2228. //    to_zero++;
  2229. //}
  2230. //#endif
  2231. //                for(; last_non_zero>=start_i; last_non_zero--){
  2232. //                    if(block[perm_scantable[last_non_zero]])
  2233. //                        break;
  2234. //                }
  2235. //            }
  2236. //#ifdef REFINE_STATS
  2237. //count++;
  2238. //if(256*256*256*64 % count == 0){
  2239. //    printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%dn", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
  2240. //}
  2241. //#endif
  2242. //            run=0;
  2243. //            rle_index=0;
  2244. //            for(i=start_i; i<=last_non_zero; i++){
  2245. //                int j= perm_scantable[i];
  2246. //                const int level= block[j];
  2247. //        
  2248. //                 if(level){
  2249. //                     run_tab[rle_index++]=run;
  2250. //                     run=0;
  2251. //                 }else{
  2252. //                     run++;
  2253. //                 }
  2254. //            }
  2255. //            
  2256. //            s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
  2257. //        }else{
  2258. //            break;
  2259. //        }
  2260. //    }
  2261. //#ifdef REFINE_STATS
  2262. //if(last_non_zero>0){
  2263. //STOP_TIMER("iterative search")
  2264. //}
  2265. //}
  2266. //#endif
  2267. //    return last_non_zero;
  2268. //}
  2269. //static int dct_quantize_c(MpegEncContext *s, 
  2270. //                        DCTELEM *block, int n,
  2271. //                        int qscale, int *overflow)
  2272. //{
  2273. //    int i, j, level, last_non_zero, q, start_i;
  2274. //    const int *qmat;
  2275. //    const uint8_t *scantable= s->intra_scantable.scantable;
  2276. //    int bias;
  2277. //    int max=0;
  2278. //    unsigned int threshold1, threshold2;
  2279. //    s->dsp.fdct (block);
  2280. //    if(s->dct_error_sum)
  2281. //        s->denoise_dct(s, block);
  2282. //    if (s->mb_intra) {
  2283. //        if (!s->h263_aic) {
  2284. //            if (n < 4)
  2285. //                q = s->y_dc_scale;
  2286. //            else
  2287. //                q = s->c_dc_scale;
  2288. //            q = q << 3;
  2289. //        } else
  2290. //            /* For AIC we skip quant/dequant of INTRADC */
  2291. //            q = 1 << 3;
  2292. //            
  2293. //        /* note: block[0] is assumed to be positive */
  2294. //        block[0] = (block[0] + (q >> 1)) / q;
  2295. //        start_i = 1;
  2296. //        last_non_zero = 0;
  2297. //        qmat = s->q_intra_matrix[qscale];
  2298. //        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  2299. //    } else {
  2300. //        start_i = 0;
  2301. //        last_non_zero = -1;
  2302. //        qmat = s->q_inter_matrix[qscale];
  2303. //        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  2304. //    }
  2305. //    threshold1= (1<<QMAT_SHIFT) - bias - 1;
  2306. //    threshold2= (threshold1<<1);
  2307. //    for(i=63;i>=start_i;i--) {
  2308. //        j = scantable[i];
  2309. //        level = block[j] * qmat[j];
  2310. //        if(((unsigned)(level+threshold1))>threshold2){
  2311. //            last_non_zero = i;
  2312. //            break;
  2313. //        }else{
  2314. //            block[j]=0;
  2315. //        }
  2316. //    }
  2317. //    for(i=start_i; i<=last_non_zero; i++) {
  2318. //        j = scantable[i];
  2319. //        level = block[j] * qmat[j];
  2320. ////        if(   bias+level >= (1<<QMAT_SHIFT)
  2321. ////           || bias-level >= (1<<QMAT_SHIFT)){
  2322. //        if(((unsigned)(level+threshold1))>threshold2){
  2323. //            if(level>0){
  2324. //                level= (bias + level)>>QMAT_SHIFT;
  2325. //                block[j]= level;
  2326. //            }else{
  2327. //                level= (bias - level)>>QMAT_SHIFT;
  2328. //                block[j]= -level;
  2329. //            }
  2330. //            max |=level;
  2331. //        }else{
  2332. //            block[j]=0;
  2333. //        }
  2334. //    }
  2335. //    *overflow= s->max_qcoeff < max; //overflow might have happend
  2336. //    
  2337. //    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  2338. //    if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  2339. // ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  2340. //    return last_non_zero;
  2341. //}
  2342. //#endif //CONFIG_ENCODERS
  2343. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, 
  2344.                                    DCTELEM *block, int n, int qscale)
  2345. {
  2346.     int i, level, nCoeffs;
  2347.     const uint16_t *quant_matrix;
  2348.     nCoeffs= s->block_last_index[n];
  2349.     
  2350.     if (n < 4) 
  2351.         block[0] = block[0] * s->y_dc_scale;
  2352.     else
  2353.         block[0] = block[0] * s->c_dc_scale;
  2354.     /* XXX: only mpeg1 */
  2355.     quant_matrix = s->intra_matrix;
  2356.     for(i=1;i<=nCoeffs;i++) {
  2357.         int j= s->intra_scantable.permutated[i];
  2358.         level = block[j];
  2359.         if (level) {
  2360.             if (level < 0) {
  2361.                 level = -level;
  2362.                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2363.                 level = (level - 1) | 1;
  2364.                 level = -level;
  2365.             } else {
  2366.                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2367.                 level = (level - 1) | 1;
  2368.             }
  2369.             block[j] = level;
  2370.         }
  2371.     }
  2372. }
  2373. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, 
  2374.                                    DCTELEM *block, int n, int qscale)
  2375. {
  2376.     int i, level, nCoeffs;
  2377.     const uint16_t *quant_matrix;
  2378.     nCoeffs= s->block_last_index[n];
  2379.     
  2380.     quant_matrix = s->inter_matrix;
  2381.     for(i=0; i<=nCoeffs; i++) {
  2382.         int j= s->intra_scantable.permutated[i];
  2383.         level = block[j];
  2384.         if (level) {
  2385.             if (level < 0) {
  2386.                 level = -level;
  2387.                 level = (((level << 1) + 1) * qscale *
  2388.                          ((int) (quant_matrix[j]))) >> 4;
  2389.                 level = (level - 1) | 1;
  2390.                 level = -level;
  2391.             } else {
  2392.                 level = (((level << 1) + 1) * qscale *
  2393.                          ((int) (quant_matrix[j]))) >> 4;
  2394.                 level = (level - 1) | 1;
  2395.             }
  2396.             block[j] = level;
  2397.         }
  2398.     }
  2399. }
  2400. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, 
  2401.                                    DCTELEM *block, int n, int qscale)
  2402. {
  2403.     int i, level, nCoeffs;
  2404.     const uint16_t *quant_matrix;
  2405.     if(s->alternate_scan) nCoeffs= 63;
  2406.     else nCoeffs= s->block_last_index[n];
  2407.     
  2408.     if (n < 4) 
  2409.         block[0] = block[0] * s->y_dc_scale;
  2410.     else
  2411.         block[0] = block[0] * s->c_dc_scale;
  2412.     quant_matrix = s->intra_matrix;
  2413.     for(i=1;i<=nCoeffs;i++) {
  2414.         int j= s->intra_scantable.permutated[i];
  2415.         level = block[j];
  2416.         if (level) {
  2417.             if (level < 0) {
  2418.                 level = -level;
  2419.                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2420.                 level = -level;
  2421.             } else {
  2422.                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2423.             }
  2424.             block[j] = level;
  2425.         }
  2426.     }
  2427. }
  2428. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, 
  2429.                                    DCTELEM *block, int n, int qscale)
  2430. {
  2431.     int i, level, nCoeffs;
  2432.     const uint16_t *quant_matrix;
  2433.     int sum=-1;
  2434.     if(s->alternate_scan) nCoeffs= 63;
  2435.     else nCoeffs= s->block_last_index[n];
  2436.     
  2437.     quant_matrix = s->inter_matrix;
  2438.     for(i=0; i<=nCoeffs; i++) {
  2439.         int j= s->intra_scantable.permutated[i];
  2440.         level = block[j];
  2441.         if (level) {
  2442.             if (level < 0) {
  2443.                 level = -level;
  2444.                 level = (((level << 1) + 1) * qscale *
  2445.                          ((int) (quant_matrix[j]))) >> 4;
  2446.                 level = -level;
  2447.             } else {
  2448.                 level = (((level << 1) + 1) * qscale *
  2449.                          ((int) (quant_matrix[j]))) >> 4;
  2450.             }
  2451.             block[j] = level;
  2452.             sum+=level;
  2453.         }
  2454.     }
  2455.     block[63]^=sum&1;
  2456. }
  2457. static void dct_unquantize_h263_intra_c(MpegEncContext *s, 
  2458.                                   DCTELEM *block, int n, int qscale)
  2459. {
  2460.     int i, level, qmul, qadd;
  2461.     int nCoeffs;
  2462.     
  2463.     assert(s->block_last_index[n]>=0);
  2464.     
  2465.     qmul = qscale << 1;
  2466.     
  2467.     if (!s->h263_aic) {
  2468.         if (n < 4) 
  2469.             block[0] = block[0] * s->y_dc_scale;
  2470.         else
  2471.             block[0] = block[0] * s->c_dc_scale;
  2472.         qadd = (qscale - 1) | 1;
  2473.     }else{
  2474.         qadd = 0;
  2475.     }
  2476.     if(s->ac_pred)
  2477.         nCoeffs=63;
  2478.     else
  2479.         nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2480.     for(i=1; i<=nCoeffs; i++) {
  2481.         level = block[i];
  2482.         if (level) {
  2483.             if (level < 0) {
  2484.                 level = level * qmul - qadd;
  2485.             } else {
  2486.                 level = level * qmul + qadd;
  2487.             }
  2488.             block[i] = level;
  2489.         }
  2490.     }
  2491. }
  2492. static void dct_unquantize_h263_inter_c(MpegEncContext *s, 
  2493.                                   DCTELEM *block, int n, int qscale)
  2494. {
  2495.     int i, level, qmul, qadd;
  2496.     int nCoeffs;
  2497.     
  2498.     assert(s->block_last_index[n]>=0);
  2499.     
  2500.     qadd = (qscale - 1) | 1;
  2501.     qmul = qscale << 1;
  2502.     
  2503.     nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2504.     for(i=0; i<=nCoeffs; i++) {
  2505.         level = block[i];
  2506.         if (level) {
  2507.             if (level < 0) {
  2508.                 level = level * qmul - qadd;
  2509.             } else {
  2510.                 level = level * qmul + qadd;
  2511.             }
  2512.             block[i] = level;
  2513.         }
  2514.     }
  2515. }
  2516. static void dct_unquantize_h261_intra_c(MpegEncContext *s, 
  2517.                                   DCTELEM *block, int n, int qscale)
  2518. {
  2519.     int i, level, even;
  2520.     int nCoeffs;
  2521.     
  2522.     assert(s->block_last_index[n]>=0);
  2523.     
  2524.     if (n < 4) 
  2525.         block[0] = block[0] * s->y_dc_scale;
  2526.     else
  2527.         block[0] = block[0] * s->c_dc_scale;
  2528.     even = (qscale & 1)^1;
  2529.     nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2530.     for(i=1; i<=nCoeffs; i++){
  2531.         level = block[i];
  2532.         if (level){
  2533.             if (level < 0){
  2534.                 level = qscale * ((level << 1) - 1) + even;
  2535.             }else{
  2536.                 level = qscale * ((level << 1) + 1) - even;
  2537.             }
  2538.         }
  2539.         block[i] = level;
  2540.     }
  2541. }
  2542. static void dct_unquantize_h261_inter_c(MpegEncContext *s, 
  2543.                                   DCTELEM *block, int n, int qscale)
  2544. {
  2545.     int i, level, even;
  2546.     int nCoeffs;
  2547.     
  2548.     assert(s->block_last_index[n]>=0);
  2549.     even = (qscale & 1)^1;
  2550.     
  2551.     nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2552.     for(i=0; i<=nCoeffs; i++){
  2553.         level = block[i];
  2554.         if (level){
  2555.             if (level < 0){
  2556.                 level = qscale * ((level << 1) - 1) + even;
  2557.             }else{
  2558.                 level = qscale * ((level << 1) + 1) - even;
  2559.             }
  2560.         }
  2561.         block[i] = level;
  2562.     }
  2563. }
  2564. //static const AVOption mpeg4_options[] =
  2565. //{
  2566. //    AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  2567. //    AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  2568. //        "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  2569. //        bit_rate_tolerance, 4, 240000000, 8000),
  2570. //    AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  2571. //    AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  2572. //    AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  2573. //   rc_eq, "tex^qComp,option1,options2", 0),
  2574. //    AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  2575. //        rc_min_rate, 4, 24000000, 0),
  2576. //    AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  2577. //        rc_max_rate, 4, 24000000, 0),
  2578. //    AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  2579. //   rc_buffer_aggressivity, 4, 24000000, 0),
  2580. //    AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  2581. //   rc_initial_cplx, 0., 9999999., 0),
  2582. //    AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  2583. //   i_quant_factor, 0., 0., 0),
  2584. //    AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  2585. //   i_quant_factor, -999999., 999999., 0),
  2586. //    AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  2587. //        dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  2588. //    AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  2589. //   lumi_masking, 0., 999999., 0),
  2590. //    AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  2591. //   temporal_cplx_masking, 0., 999999., 0),
  2592. //    AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  2593. //   spatial_cplx_masking, 0., 999999., 0),
  2594. //    AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  2595. //   p_masking, 0., 999999., 0),
  2596. //    AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  2597. //   dark_masking, 0., 999999., 0),
  2598. //    AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  2599. //        idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  2600. //    AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  2601. //        mb_qmin, 0, 8, 0),
  2602. //    AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  2603. //        mb_qmin, 0, 8, 0),
  2604. //    AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  2605. //        me_cmp, 0, 24000000, 0),
  2606. //    AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  2607. //        me_sub_cmp, 0, 24000000, 0),
  2608. //    AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  2609. //        dia_size, 0, 24000000, 0),
  2610. //    AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  2611. //        last_predictor_count, 0, 24000000, 0),
  2612. //    AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  2613. //        pre_me, 0, 24000000, 0),
  2614. //    AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  2615. //        me_pre_cmp, 0, 24000000, 0),
  2616. //    AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  2617. //        me_range, 0, 24000000, 0),
  2618. //    AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  2619. //        pre_dia_size, 0, 24000000, 0),
  2620. //    AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  2621. //        me_subpel_quality, 0, 24000000, 0),
  2622. //    AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  2623. //        me_range, 0, 24000000, 0),
  2624. //    AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  2625. //         flags, CODEC_FLAG_PSNR, 0),
  2626. //    AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  2627. //       rc_override),
  2628. //    AVOPTION_SUB(avoptions_common),
  2629. //    AVOPTION_END()
  2630. //};
  2631. //#ifdef CONFIG_ENCODERS
  2632. //#ifdef CONFIG_RISKY
  2633. //AVCodec h263_encoder = {
  2634. //    "h263",
  2635. //    CODEC_TYPE_VIDEO,
  2636. //    CODEC_ID_H263,
  2637. //    sizeof(MpegEncContext),
  2638. //    MPV_encode_init,
  2639. //    MPV_encode_picture,
  2640. //    MPV_encode_end,
  2641. //};
  2642. //AVCodec h263p_encoder = {
  2643. //    "h263p",
  2644. //    CODEC_TYPE_VIDEO,
  2645. //    CODEC_ID_H263P,
  2646. //    sizeof(MpegEncContext),
  2647. //    MPV_encode_init,
  2648. //    MPV_encode_picture,
  2649. //    MPV_encode_end,
  2650. //};
  2651. //AVCodec flv_encoder = {
  2652. //    "flv",
  2653. //    CODEC_TYPE_VIDEO,
  2654. //    CODEC_ID_FLV1,
  2655. //    sizeof(MpegEncContext),
  2656. //    MPV_encode_init,
  2657. //    MPV_encode_picture,
  2658. //    MPV_encode_end,
  2659. //};
  2660. //AVCodec rv10_encoder = {
  2661. //    "rv10",
  2662. //    CODEC_TYPE_VIDEO,
  2663. //    CODEC_ID_RV10,
  2664. //    sizeof(MpegEncContext),
  2665. //    MPV_encode_init,
  2666. //    MPV_encode_picture,
  2667. //    MPV_encode_end,
  2668. //};
  2669. //AVCodec mpeg4_encoder = {
  2670. //    "mpeg4",
  2671. //    CODEC_TYPE_VIDEO,
  2672. //    CODEC_ID_MPEG4,
  2673. //    sizeof(MpegEncContext),
  2674. //    MPV_encode_init,
  2675. //    MPV_encode_picture,
  2676. //    MPV_encode_end,
  2677. //    .options = mpeg4_options,
  2678. //    .capabilities= CODEC_CAP_DELAY,
  2679. //};
  2680. //AVCodec msmpeg4v1_encoder = {
  2681. //    "msmpeg4v1",
  2682. //    CODEC_TYPE_VIDEO,
  2683. //    CODEC_ID_MSMPEG4V1,
  2684. //    sizeof(MpegEncContext),
  2685. //    MPV_encode_init,
  2686. //    MPV_encode_picture,
  2687. //    MPV_encode_end,
  2688. //    .options = mpeg4_options,
  2689. //};
  2690. //AVCodec msmpeg4v2_encoder = {
  2691. //    "msmpeg4v2",
  2692. //    CODEC_TYPE_VIDEO,
  2693. //    CODEC_ID_MSMPEG4V2,
  2694. //    sizeof(MpegEncContext),
  2695. //    MPV_encode_init,
  2696. //    MPV_encode_picture,
  2697. //    MPV_encode_end,
  2698. //    .options = mpeg4_options,
  2699. //};
  2700. //AVCodec msmpeg4v3_encoder = {
  2701. //    "msmpeg4",
  2702. //    CODEC_TYPE_VIDEO,
  2703. //    CODEC_ID_MSMPEG4V3,
  2704. //    sizeof(MpegEncContext),
  2705. //    MPV_encode_init,
  2706. //    MPV_encode_picture,
  2707. //    MPV_encode_end,
  2708. //    .options = mpeg4_options,
  2709. //};
  2710. //AVCodec wmv1_encoder = {
  2711. //    "wmv1",
  2712. //    CODEC_TYPE_VIDEO,
  2713. //    CODEC_ID_WMV1,
  2714. //    sizeof(MpegEncContext),
  2715. //    MPV_encode_init,
  2716. //    MPV_encode_picture,
  2717. //    MPV_encode_end,
  2718. //    .options = mpeg4_options,
  2719. //};
  2720. //#endif
  2721. //AVCodec mjpeg_encoder = {
  2722. //    "mjpeg",
  2723. //    CODEC_TYPE_VIDEO,
  2724. //    CODEC_ID_MJPEG,
  2725. //    sizeof(MpegEncContext),
  2726. //    MPV_encode_init,
  2727. //    MPV_encode_picture,
  2728. //    MPV_encode_end,
  2729. //};
  2730. //#endif //CONFIG_ENCODERS