error_resilience.c
上传用户:lctgjx
上传日期:2022-06-04
资源大小:8887k
文件大小:40k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*
  2.  * Error resilience / concealment
  3.  *
  4.  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5.  *
  6.  * This file is part of FFmpeg.
  7.  *
  8.  * FFmpeg is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * FFmpeg is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with FFmpeg; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21.  */
  22. /**
  23.  * @file error_resilience.c
  24.  * Error resilience / concealment.
  25.  */
  26. #include <limits.h>
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "mpegvideo.h"
  30. #include "log.h"
  31. static void decode_mb(MpegEncContext *s){
  32.     s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize  ) + s->mb_x * 16;
  33.     s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * s->uvlinesize) + s->mb_x * 8;
  34.     s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * s->uvlinesize) + s->mb_x * 8;
  35.     MPV_decode_mb(s, s->block);
  36. }
  37. /**
  38.  * replaces the current MB with a flat dc only version.
  39.  */
  40. static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  41. {
  42.     int dc, dcu, dcv, y, i;
  43.     for(i=0; i<4; i++){
  44.         dc= s->dc_val[0][mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*s->b8_stride];
  45.         if(dc<0) dc=0;
  46.         else if(dc>2040) dc=2040;
  47.         for(y=0; y<8; y++){
  48.             int x;
  49.             for(x=0; x<8; x++){
  50.                 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  51.             }
  52.         }
  53.     }
  54.     dcu = s->dc_val[1][mb_x + mb_y*s->mb_stride];
  55.     dcv = s->dc_val[2][mb_x + mb_y*s->mb_stride];
  56.     if     (dcu<0   ) dcu=0;
  57.     else if(dcu>2040) dcu=2040;
  58.     if     (dcv<0   ) dcv=0;
  59.     else if(dcv>2040) dcv=2040;
  60.     for(y=0; y<8; y++){
  61.         int x;
  62.         for(x=0; x<8; x++){
  63.             dest_cb[x + y*(s->uvlinesize)]= dcu/8;
  64.             dest_cr[x + y*(s->uvlinesize)]= dcv/8;
  65.         }
  66.     }
  67. }
  68. static void filter181(int16_t *data, int width, int height, int stride){
  69.     int x,y;
  70.     /* horizontal filter */
  71.     for(y=1; y<height-1; y++){
  72.         int prev_dc= data[0 + y*stride];
  73.         for(x=1; x<width-1; x++){
  74.             int dc;
  75.             dc= - prev_dc
  76.                 + data[x     + y*stride]*8
  77.                 - data[x + 1 + y*stride];
  78.             dc= (dc*10923 + 32768)>>16;
  79.             prev_dc= data[x + y*stride];
  80.             data[x + y*stride]= dc;
  81.         }
  82.     }
  83.     /* vertical filter */
  84.     for(x=1; x<width-1; x++){
  85.         int prev_dc= data[x];
  86.         for(y=1; y<height-1; y++){
  87.             int dc;
  88.             dc= - prev_dc
  89.                 + data[x +  y   *stride]*8
  90.                 - data[x + (y+1)*stride];
  91.             dc= (dc*10923 + 32768)>>16;
  92.             prev_dc= data[x + y*stride];
  93.             data[x + y*stride]= dc;
  94.         }
  95.     }
  96. }
  97. /**
  98.  * guess the dc of blocks which do not have an undamaged dc
  99.  * @param w     width in 8 pixel blocks
  100.  * @param h     height in 8 pixel blocks
  101.  */
  102. static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
  103.     int b_x, b_y;
  104.     for(b_y=0; b_y<h; b_y++){
  105.         for(b_x=0; b_x<w; b_x++){
  106.             int color[4]={1024,1024,1024,1024};
  107.             int distance[4]={9999,9999,9999,9999};
  108.             int mb_index, error, j;
  109.             int64_t guess, weight_sum;
  110.             mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
  111.             error= s->error_status_table[mb_index];
  112.             if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
  113.             if(!(error&DC_ERROR)) continue;           //dc-ok
  114.             /* right block */
  115.             for(j=b_x+1; j<w; j++){
  116.                 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
  117.                 int error_j= s->error_status_table[mb_index_j];
  118.                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
  119.                 if(intra_j==0 || !(error_j&DC_ERROR)){
  120.                     color[0]= dc[j + b_y*stride];
  121.                     distance[0]= j-b_x;
  122.                     break;
  123.                 }
  124.             }
  125.             /* left block */
  126.             for(j=b_x-1; j>=0; j--){
  127.                 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
  128.                 int error_j= s->error_status_table[mb_index_j];
  129.                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
  130.                 if(intra_j==0 || !(error_j&DC_ERROR)){
  131.                     color[1]= dc[j + b_y*stride];
  132.                     distance[1]= b_x-j;
  133.                     break;
  134.                 }
  135.             }
  136.             /* bottom block */
  137.             for(j=b_y+1; j<h; j++){
  138.                 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
  139.                 int error_j= s->error_status_table[mb_index_j];
  140.                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
  141.                 if(intra_j==0 || !(error_j&DC_ERROR)){
  142.                     color[2]= dc[b_x + j*stride];
  143.                     distance[2]= j-b_y;
  144.                     break;
  145.                 }
  146.             }
  147.             /* top block */
  148.             for(j=b_y-1; j>=0; j--){
  149.                 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
  150.                 int error_j= s->error_status_table[mb_index_j];
  151.                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
  152.                 if(intra_j==0 || !(error_j&DC_ERROR)){
  153.                     color[3]= dc[b_x + j*stride];
  154.                     distance[3]= b_y-j;
  155.                     break;
  156.                 }
  157.             }
  158.             weight_sum=0;
  159.             guess=0;
  160.             for(j=0; j<4; j++){
  161.                 int64_t weight= 256*256*256*16/distance[j];
  162.                 guess+= weight*(int64_t)color[j];
  163.                 weight_sum+= weight;
  164.             }
  165.             guess= (guess + weight_sum/2) / weight_sum;
  166.             dc[b_x + b_y*stride]= guess;
  167.         }
  168.     }
  169. }
  170. /**
  171.  * simple horizontal deblocking filter used for error resilience
  172.  * @param w     width in 8 pixel blocks
  173.  * @param h     height in 8 pixel blocks
  174.  */
  175. static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
  176.     int b_x, b_y;
  177.     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  178.     for(b_y=0; b_y<h; b_y++){
  179.         for(b_x=0; b_x<w-1; b_x++){
  180.             int y;
  181.             int left_status = s->error_status_table[( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride];
  182.             int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
  183.             int left_intra=   IS_INTRA(s->current_picture.mb_type      [( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
  184.             int right_intra=  IS_INTRA(s->current_picture.mb_type      [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
  185.             int left_damage =  left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  186.             int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  187.             int offset= b_x*8 + b_y*stride*8;
  188.             int16_t *left_mv=  s->current_picture.motion_val[0][s->b8_stride*(b_y<<(1-is_luma)) + ( b_x   <<(1-is_luma))];
  189.             int16_t *right_mv= s->current_picture.motion_val[0][s->b8_stride*(b_y<<(1-is_luma)) + ((b_x+1)<<(1-is_luma))];
  190.             if(!(left_damage||right_damage)) continue; // both undamaged
  191.             if(   (!left_intra) && (!right_intra)
  192.                && FFABS(left_mv[0]-right_mv[0]) + FFABS(left_mv[1]+right_mv[1]) < 2) continue;
  193.             for(y=0; y<8; y++){
  194.                 int a,b,c,d;
  195.                 a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
  196.                 b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
  197.                 c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
  198.                 d= FFABS(b) - ((FFABS(a) + FFABS(c) + 1)>>1);
  199.                 d= FFMAX(d, 0);
  200.                 if(b<0) d= -d;
  201.                 if(d==0) continue;
  202.                 if(!(left_damage && right_damage))
  203.                     d= d*16/9;
  204.                 if(left_damage){
  205.                     dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
  206.                     dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
  207.                     dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
  208.                     dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
  209.                 }
  210.                 if(right_damage){
  211.                     dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
  212.                     dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
  213.                     dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
  214.                     dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
  215.                 }
  216.             }
  217.         }
  218.     }
  219. }
  220. /**
  221.  * simple vertical deblocking filter used for error resilience
  222.  * @param w     width in 8 pixel blocks
  223.  * @param h     height in 8 pixel blocks
  224.  */
  225. static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
  226.     int b_x, b_y;
  227.     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  228.     for(b_y=0; b_y<h-1; b_y++){
  229.         for(b_x=0; b_x<w; b_x++){
  230.             int x;
  231.             int top_status   = s->error_status_table[(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride];
  232.             int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
  233.             int top_intra=     IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride]);
  234.             int bottom_intra=  IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
  235.             int top_damage =      top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  236.             int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  237.             int offset= b_x*8 + b_y*stride*8;
  238.             int16_t *top_mv=    s->current_picture.motion_val[0][s->b8_stride*( b_y   <<(1-is_luma)) + (b_x<<(1-is_luma))];
  239.             int16_t *bottom_mv= s->current_picture.motion_val[0][s->b8_stride*((b_y+1)<<(1-is_luma)) + (b_x<<(1-is_luma))];
  240.             if(!(top_damage||bottom_damage)) continue; // both undamaged
  241.             if(   (!top_intra) && (!bottom_intra)
  242.                && FFABS(top_mv[0]-bottom_mv[0]) + FFABS(top_mv[1]+bottom_mv[1]) < 2) continue;
  243.             for(x=0; x<8; x++){
  244.                 int a,b,c,d;
  245.                 a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
  246.                 b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
  247.                 c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
  248.                 d= FFABS(b) - ((FFABS(a) + FFABS(c)+1)>>1);
  249.                 d= FFMAX(d, 0);
  250.                 if(b<0) d= -d;
  251.                 if(d==0) continue;
  252.                 if(!(top_damage && bottom_damage))
  253.                     d= d*16/9;
  254.                 if(top_damage){
  255.                     dst[offset + x +  7*stride] = cm[dst[offset + x +  7*stride] + ((d*7)>>4)];
  256.                     dst[offset + x +  6*stride] = cm[dst[offset + x +  6*stride] + ((d*5)>>4)];
  257.                     dst[offset + x +  5*stride] = cm[dst[offset + x +  5*stride] + ((d*3)>>4)];
  258.                     dst[offset + x +  4*stride] = cm[dst[offset + x +  4*stride] + ((d*1)>>4)];
  259.                 }
  260.                 if(bottom_damage){
  261.                     dst[offset + x +  8*stride] = cm[dst[offset + x +  8*stride] - ((d*7)>>4)];
  262.                     dst[offset + x +  9*stride] = cm[dst[offset + x +  9*stride] - ((d*5)>>4)];
  263.                     dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
  264.                     dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
  265.                 }
  266.             }
  267.         }
  268.     }
  269. }
  270. static void guess_mv(MpegEncContext *s){
  271. //    uint8_t fixed[s->mb_stride * s->mb_height];
  272. uint8_t fixed[2000];
  273. #define MV_FROZEN    3
  274. #define MV_CHANGED   2
  275. #define MV_UNCHANGED 1
  276.     const int mb_stride = s->mb_stride;
  277.     const int mb_width = s->mb_width;
  278.     const int mb_height= s->mb_height;
  279.     int i, depth, num_avail;
  280.     int mb_x, mb_y;
  281.     num_avail=0;
  282.     for(i=0; i<s->mb_num; i++){
  283.         const int mb_xy= s->mb_index2xy[ i ];
  284.         int f=0;
  285.         int error= s->error_status_table[mb_xy];
  286.         if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
  287.         if(!(error&MV_ERROR)) f=MV_FROZEN;           //inter with undamaged MV
  288.         fixed[mb_xy]= f;
  289.         if(f==MV_FROZEN)
  290.             num_avail++;
  291.     }
  292.     if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
  293.         for(mb_y=0; mb_y<s->mb_height; mb_y++){
  294.             for(mb_x=0; mb_x<s->mb_width; mb_x++){
  295.                 const int mb_xy= mb_x + mb_y*s->mb_stride;
  296.                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))  continue;
  297.                 if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
  298.                 s->mv_dir = MV_DIR_FORWARD;
  299.                 s->mb_intra=0;
  300.                 s->mv_type = MV_TYPE_16X16;
  301.                 s->mb_skipped=0;
  302.                 s->dsp.clear_blocks(s->block[0]);
  303.                 s->mb_x= mb_x;
  304.                 s->mb_y= mb_y;
  305.                 s->mv[0][0][0]= 0;
  306.                 s->mv[0][0][1]= 0;
  307.                 decode_mb(s);
  308.             }
  309.         }
  310.         return;
  311.     }
  312.     for(depth=0;; depth++){
  313.         int changed, pass, none_left;
  314.         none_left=1;
  315.         changed=1;
  316.         for(pass=0; (changed || pass<2) && pass<10; pass++){
  317.             int mb_x, mb_y;
  318. int score_sum=0;
  319.             changed=0;
  320.             for(mb_y=0; mb_y<s->mb_height; mb_y++){
  321.                 for(mb_x=0; mb_x<s->mb_width; mb_x++){
  322.                     const int mb_xy= mb_x + mb_y*s->mb_stride;
  323.                     int mv_predictor[8][2]={{0}};
  324.                     int pred_count=0;
  325.                     int j;
  326.                     int best_score=256*256*256*64;
  327.                     int best_pred=0;
  328.                     const int mot_stride= s->b8_stride;
  329.                     const int mot_index= mb_x*2 + mb_y*2*mot_stride;
  330.                     int prev_x= s->current_picture.motion_val[0][mot_index][0];
  331.                     int prev_y= s->current_picture.motion_val[0][mot_index][1];
  332.                     if((mb_x^mb_y^pass)&1) continue;
  333.                     if(fixed[mb_xy]==MV_FROZEN) continue;
  334.                     assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
  335.                     assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
  336.                     j=0;
  337.                     if(mb_x>0           && fixed[mb_xy-1        ]==MV_FROZEN) j=1;
  338.                     if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_FROZEN) j=1;
  339.                     if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
  340.                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
  341.                     if(j==0) continue;
  342.                     j=0;
  343.                     if(mb_x>0           && fixed[mb_xy-1        ]==MV_CHANGED) j=1;
  344.                     if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_CHANGED) j=1;
  345.                     if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
  346.                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
  347.                     if(j==0 && pass>1) continue;
  348.                     none_left=0;
  349.                     if(mb_x>0 && fixed[mb_xy-1]){
  350.                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - 2][0];
  351.                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - 2][1];
  352.                         pred_count++;
  353.                     }
  354.                     if(mb_x+1<mb_width && fixed[mb_xy+1]){
  355.                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + 2][0];
  356.                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + 2][1];
  357.                         pred_count++;
  358.                     }
  359.                     if(mb_y>0 && fixed[mb_xy-mb_stride]){
  360.                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*2][0];
  361.                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*2][1];
  362.                         pred_count++;
  363.                     }
  364.                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
  365.                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*2][0];
  366.                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*2][1];
  367.                         pred_count++;
  368.                     }
  369.                     if(pred_count==0) continue;
  370.                     if(pred_count>1){
  371.                         int sum_x=0, sum_y=0;
  372.                         int max_x, max_y, min_x, min_y;
  373.                         for(j=0; j<pred_count; j++){
  374.                             sum_x+= mv_predictor[j][0];
  375.                             sum_y+= mv_predictor[j][1];
  376.                         }
  377.                         /* mean */
  378.                         mv_predictor[pred_count][0] = sum_x/j;
  379.                         mv_predictor[pred_count][1] = sum_y/j;
  380.                         /* median */
  381.                         if(pred_count>=3){
  382.                             min_y= min_x= 99999;
  383.                             max_y= max_x=-99999;
  384.                         }else{
  385.                             min_x=min_y=max_x=max_y=0;
  386.                         }
  387.                         for(j=0; j<pred_count; j++){
  388.                             max_x= FFMAX(max_x, mv_predictor[j][0]);
  389.                             max_y= FFMAX(max_y, mv_predictor[j][1]);
  390.                             min_x= FFMIN(min_x, mv_predictor[j][0]);
  391.                             min_y= FFMIN(min_y, mv_predictor[j][1]);
  392.                         }
  393.                         mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
  394.                         mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
  395.                         if(pred_count==4){
  396.                             mv_predictor[pred_count+1][0] /= 2;
  397.                             mv_predictor[pred_count+1][1] /= 2;
  398.                         }
  399.                         pred_count+=2;
  400.                     }
  401.                     /* zero MV */
  402.                     pred_count++;
  403.                     /* last MV */
  404.                     mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];
  405.                     mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];
  406.                     pred_count++;
  407.                     s->mv_dir = MV_DIR_FORWARD;
  408.                     s->mb_intra=0;
  409.                     s->mv_type = MV_TYPE_16X16;
  410.                     s->mb_skipped=0;
  411.                     s->dsp.clear_blocks(s->block[0]);
  412.                     s->mb_x= mb_x;
  413.                     s->mb_y= mb_y;
  414.                     for(j=0; j<pred_count; j++){
  415.                         int score=0;
  416.                         uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  417.                         s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
  418.                         s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
  419.                         decode_mb(s);
  420.                         if(mb_x>0 && fixed[mb_xy-1]){
  421.                             int k;
  422.                             for(k=0; k<16; k++)
  423.                                 score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize   ]);
  424.                         }
  425.                         if(mb_x+1<mb_width && fixed[mb_xy+1]){
  426.                             int k;
  427.                             for(k=0; k<16; k++)
  428.                                 score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
  429.                         }
  430.                         if(mb_y>0 && fixed[mb_xy-mb_stride]){
  431.                             int k;
  432.                             for(k=0; k<16; k++)
  433.                                 score += FFABS(src[k-s->linesize   ]-src[k               ]);
  434.                         }
  435.                         if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
  436.                             int k;
  437.                             for(k=0; k<16; k++)
  438.                                 score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
  439.                         }
  440.                         if(score <= best_score){ // <= will favor the last MV
  441.                             best_score= score;
  442.                             best_pred= j;
  443.                         }
  444.                     }
  445. score_sum+= best_score;
  446. //FIXME no need to set s->current_picture.motion_val[0][mot_index][0] explicit
  447.                     s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
  448.                     s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
  449.                     decode_mb(s);
  450.                     if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
  451.                         fixed[mb_xy]=MV_CHANGED;
  452.                         changed++;
  453.                     }else
  454.                         fixed[mb_xy]=MV_UNCHANGED;
  455.                 }
  456.             }
  457. //            printf(".%d/%d", changed, score_sum); fflush(stdout);
  458.         }
  459.         if(none_left)
  460.             return;
  461.         for(i=0; i<s->mb_num; i++){
  462.             int mb_xy= s->mb_index2xy[i];
  463.             if(fixed[mb_xy])
  464.                 fixed[mb_xy]=MV_FROZEN;
  465.         }
  466. //        printf(":"); fflush(stdout);
  467.     }
  468. }
  469. static int is_intra_more_likely(MpegEncContext *s){
  470.     int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
  471.     if(s->last_picture_ptr==NULL) return 1; //no previous frame available -> use spatial prediction
  472.     undamaged_count=0;
  473.     for(i=0; i<s->mb_num; i++){
  474.         const int mb_xy= s->mb_index2xy[i];
  475.         const int error= s->error_status_table[mb_xy];
  476.         if(!((error&DC_ERROR) && (error&MV_ERROR)))
  477.             undamaged_count++;
  478.     }
  479.     if(undamaged_count < 5) return 0; //almost all MBs damaged -> use temporal prediction
  480. #ifdef HAVE_XVMC
  481.     //prevent dsp.sad() check, that requires access to the image
  482.     if(s->avctx->xvmc_acceleration && s->pict_type==FF_I_TYPE) return 1;
  483. #endif
  484.     skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
  485.     is_intra_likely=0;
  486.     j=0;
  487.     for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
  488.         for(mb_x= 0; mb_x<s->mb_width; mb_x++){
  489.             int error;
  490.             const int mb_xy= mb_x + mb_y*s->mb_stride;
  491.             error= s->error_status_table[mb_xy];
  492.             if((error&DC_ERROR) && (error&MV_ERROR))
  493.                 continue; //skip damaged
  494.             j++;
  495.             if((j%skip_amount) != 0) continue; //skip a few to speed things up
  496.             if(s->pict_type==FF_I_TYPE){
  497.                 uint8_t *mb_ptr     = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  498.                 uint8_t *last_mb_ptr= s->last_picture.data   [0] + mb_x*16 + mb_y*16*s->linesize;
  499.                 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr                    , s->linesize, 16);
  500.                 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
  501.             }else{
  502.                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
  503.                    is_intra_likely++;
  504.                 else
  505.                    is_intra_likely--;
  506.             }
  507.         }
  508.     }
  509. //printf("is_intra_likely: %d type:%dn", is_intra_likely, s->pict_type);
  510.     return is_intra_likely > 0;
  511. }
  512. void ff_er_frame_start(MpegEncContext *s){
  513.     if(!s->error_resilience) return;
  514.     memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_stride*s->mb_height*sizeof(uint8_t));
  515.     s->error_count= 3*s->mb_num;
  516. }
  517. /**
  518.  * adds a slice.
  519.  * @param endx x component of the last macroblock, can be -1 for the last of the previous line
  520.  * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
  521.  *               error of the same type occurred
  522.  */
  523. void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
  524.     const int start_i= av_clip(startx + starty * s->mb_width    , 0, s->mb_num-1);
  525.     const int end_i  = av_clip(endx   + endy   * s->mb_width    , 0, s->mb_num);
  526.     const int start_xy= s->mb_index2xy[start_i];
  527.     const int end_xy  = s->mb_index2xy[end_i];
  528.     int mask= -1;
  529.     if(start_i > end_i || start_xy > end_xy){
  530.         av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before startn");
  531.         return;
  532.     }
  533.     if(!s->error_resilience) return;
  534.     mask &= ~VP_START;
  535.     if(status & (AC_ERROR|AC_END)){
  536.         mask &= ~(AC_ERROR|AC_END);
  537.         s->error_count -= end_i - start_i + 1;
  538.     }
  539.     if(status & (DC_ERROR|DC_END)){
  540.         mask &= ~(DC_ERROR|DC_END);
  541.         s->error_count -= end_i - start_i + 1;
  542.     }
  543.     if(status & (MV_ERROR|MV_END)){
  544.         mask &= ~(MV_ERROR|MV_END);
  545.         s->error_count -= end_i - start_i + 1;
  546.     }
  547.     if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
  548.     if(mask == ~0x7F){
  549.         memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
  550.     }else{
  551.         int i;
  552.         for(i=start_xy; i<end_xy; i++){
  553.             s->error_status_table[ i ] &= mask;
  554.         }
  555.     }
  556.     if(end_i == s->mb_num)
  557.         s->error_count= INT_MAX;
  558.     else{
  559.         s->error_status_table[end_xy] &= mask;
  560.         s->error_status_table[end_xy] |= status;
  561.     }
  562.     s->error_status_table[start_xy] |= VP_START;
  563.     if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
  564.         int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
  565.         prev_status &= ~ VP_START;
  566.         if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
  567.     }
  568. }
  569. void ff_er_frame_end(MpegEncContext *s){
  570.     int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
  571.     int distance;
  572.     int threshold_part[4]= {100,100,100};
  573.     int threshold= 50;
  574.     int is_intra_likely;
  575.     int size = s->b8_stride * 2 * s->mb_height;
  576.     Picture *pic= s->current_picture_ptr;
  577.     if(!s->error_resilience || s->error_count==0 ||
  578.        s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
  579.     if(s->current_picture.motion_val[0] == NULL){
  580.         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not availablen");
  581.         for(i=0; i<2; i++){
  582.             pic->ref_index[i]= av_mallocz(size * sizeof(uint8_t));
  583.             pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
  584.             pic->motion_val[i]= pic->motion_val_base[i]+4;
  585.         }
  586.         pic->motion_subsample_log2= 3;
  587.         s->current_picture= *s->current_picture_ptr;
  588.     }
  589.     for(i=0; i<2; i++){
  590.         if(pic->ref_index[i])
  591.             memset(pic->ref_index[i], 0, size * sizeof(uint8_t));
  592.     }
  593.     if(s->avctx->debug&FF_DEBUG_ER){
  594.         for(mb_y=0; mb_y<s->mb_height; mb_y++){
  595.             for(mb_x=0; mb_x<s->mb_width; mb_x++){
  596.                 int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
  597.                 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
  598.             }
  599.             av_log(s->avctx, AV_LOG_DEBUG, "n");
  600.         }
  601.     }
  602. #if 1
  603.     /* handle overlapping slices */
  604.     for(error_type=1; error_type<=3; error_type++){
  605.         int end_ok=0;
  606.         for(i=s->mb_num-1; i>=0; i--){
  607.             const int mb_xy= s->mb_index2xy[i];
  608.             int error= s->error_status_table[mb_xy];
  609.             if(error&(1<<error_type))
  610.                 end_ok=1;
  611.             if(error&(8<<error_type))
  612.                 end_ok=1;
  613.             if(!end_ok)
  614.                 s->error_status_table[mb_xy]|= 1<<error_type;
  615.             if(error&VP_START)
  616.                 end_ok=0;
  617.         }
  618.     }
  619. #endif
  620. #if 1
  621.     /* handle slices with partitions of different length */
  622.     if(s->partitioned_frame){
  623.         int end_ok=0;
  624.         for(i=s->mb_num-1; i>=0; i--){
  625.             const int mb_xy= s->mb_index2xy[i];
  626.             int error= s->error_status_table[mb_xy];
  627.             if(error&AC_END)
  628.                 end_ok=0;
  629.             if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
  630.                 end_ok=1;
  631.             if(!end_ok)
  632.                 s->error_status_table[mb_xy]|= AC_ERROR;
  633.             if(error&VP_START)
  634.                 end_ok=0;
  635.         }
  636.     }
  637. #endif
  638.     /* handle missing slices */
  639.     if(s->error_resilience>=4){
  640.         int end_ok=1;
  641.         for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
  642.             const int mb_xy= s->mb_index2xy[i];
  643.             int error1= s->error_status_table[mb_xy  ];
  644.             int error2= s->error_status_table[s->mb_index2xy[i+1]];
  645.             if(error1&VP_START)
  646.                 end_ok=1;
  647.             if(   error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
  648.                && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
  649.                && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
  650.                 end_ok=0;
  651.             }
  652.             if(!end_ok)
  653.                 s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
  654.         }
  655.     }
  656. #if 1
  657.     /* backward mark errors */
  658.     distance=9999999;
  659.     for(error_type=1; error_type<=3; error_type++){
  660.         for(i=s->mb_num-1; i>=0; i--){
  661.             const int mb_xy= s->mb_index2xy[i];
  662.             int error= s->error_status_table[mb_xy];
  663.             if(!s->mbskip_table[mb_xy]) //FIXME partition specific
  664.                 distance++;
  665.             if(error&(1<<error_type))
  666.                 distance= 0;
  667.             if(s->partitioned_frame){
  668.                 if(distance < threshold_part[error_type-1])
  669.                     s->error_status_table[mb_xy]|= 1<<error_type;
  670.             }else{
  671.                 if(distance < threshold)
  672.                     s->error_status_table[mb_xy]|= 1<<error_type;
  673.             }
  674.             if(error&VP_START)
  675.                 distance= 9999999;
  676.         }
  677.     }
  678. #endif
  679.     /* forward mark errors */
  680.     error=0;
  681.     for(i=0; i<s->mb_num; i++){
  682.         const int mb_xy= s->mb_index2xy[i];
  683.         int old_error= s->error_status_table[mb_xy];
  684.         if(old_error&VP_START)
  685.             error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
  686.         else{
  687.             error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
  688.             s->error_status_table[mb_xy]|= error;
  689.         }
  690.     }
  691. #if 1
  692.     /* handle not partitioned case */
  693.     if(!s->partitioned_frame){
  694.         for(i=0; i<s->mb_num; i++){
  695.             const int mb_xy= s->mb_index2xy[i];
  696.             error= s->error_status_table[mb_xy];
  697.             if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
  698.                 error|= AC_ERROR|DC_ERROR|MV_ERROR;
  699.             s->error_status_table[mb_xy]= error;
  700.         }
  701.     }
  702. #endif
  703.     dc_error= ac_error= mv_error=0;
  704.     for(i=0; i<s->mb_num; i++){
  705.         const int mb_xy= s->mb_index2xy[i];
  706.         error= s->error_status_table[mb_xy];
  707.         if(error&DC_ERROR) dc_error ++;
  708.         if(error&AC_ERROR) ac_error ++;
  709.         if(error&MV_ERROR) mv_error ++;
  710.     }
  711.     av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errorsn", dc_error, ac_error, mv_error);
  712.     is_intra_likely= is_intra_more_likely(s);
  713.     /* set unknown mb-type to most likely */
  714.     for(i=0; i<s->mb_num; i++){
  715.         const int mb_xy= s->mb_index2xy[i];
  716.         error= s->error_status_table[mb_xy];
  717.         if(!((error&DC_ERROR) && (error&MV_ERROR)))
  718.             continue;
  719.         if(is_intra_likely)
  720.             s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
  721.         else
  722.             s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  723.     }
  724.     /* handle inter blocks with damaged AC */
  725.     for(mb_y=0; mb_y<s->mb_height; mb_y++){
  726.         for(mb_x=0; mb_x<s->mb_width; mb_x++){
  727.             const int mb_xy= mb_x + mb_y * s->mb_stride;
  728.             const int mb_type= s->current_picture.mb_type[mb_xy];
  729.             error= s->error_status_table[mb_xy];
  730.             if(IS_INTRA(mb_type)) continue; //intra
  731.             if(error&MV_ERROR) continue;              //inter with damaged MV
  732.             if(!(error&AC_ERROR)) continue;           //undamaged inter
  733.             s->mv_dir = MV_DIR_FORWARD;
  734.             s->mb_intra=0;
  735.             s->mb_skipped=0;
  736.             if(IS_8X8(mb_type)){
  737.                 int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
  738.                 int j;
  739.                 s->mv_type = MV_TYPE_8X8;
  740.                 for(j=0; j<4; j++){
  741.                     s->mv[0][j][0] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
  742.                     s->mv[0][j][1] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
  743.                 }
  744.             }else{
  745.                 s->mv_type = MV_TYPE_16X16;
  746.                 s->mv[0][0][0] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][0];
  747.                 s->mv[0][0][1] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][1];
  748.             }
  749.             s->dsp.clear_blocks(s->block[0]);
  750.             s->mb_x= mb_x;
  751.             s->mb_y= mb_y;
  752.             decode_mb(s);
  753.         }
  754.     }
  755.     /* guess MVs */
  756.     if(s->pict_type==FF_B_TYPE){
  757.         for(mb_y=0; mb_y<s->mb_height; mb_y++){
  758.             for(mb_x=0; mb_x<s->mb_width; mb_x++){
  759.                 int xy= mb_x*2 + mb_y*2*s->b8_stride;
  760.                 const int mb_xy= mb_x + mb_y * s->mb_stride;
  761.                 const int mb_type= s->current_picture.mb_type[mb_xy];
  762.                 error= s->error_status_table[mb_xy];
  763.                 if(IS_INTRA(mb_type)) continue;
  764.                 if(!(error&MV_ERROR)) continue;           //inter with undamaged MV
  765.                 if(!(error&AC_ERROR)) continue;           //undamaged inter
  766.                 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
  767.                 s->mb_intra=0;
  768.                 s->mv_type = MV_TYPE_16X16;
  769.                 s->mb_skipped=0;
  770.                 if(s->pp_time){
  771.                     int time_pp= s->pp_time;
  772.                     int time_pb= s->pb_time;
  773.                     s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
  774.                     s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
  775.                     s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
  776.                     s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
  777.                 }else{
  778.                     s->mv[0][0][0]= 0;
  779.                     s->mv[0][0][1]= 0;
  780.                     s->mv[1][0][0]= 0;
  781.                     s->mv[1][0][1]= 0;
  782.                 }
  783.                 s->dsp.clear_blocks(s->block[0]);
  784.                 s->mb_x= mb_x;
  785.                 s->mb_y= mb_y;
  786.                 decode_mb(s);
  787.             }
  788.         }
  789.     }else
  790.         guess_mv(s);
  791. #ifdef HAVE_XVMC
  792.     /* the filters below are not XvMC compatible, skip them */
  793.     if(s->avctx->xvmc_acceleration) goto ec_clean;
  794. #endif
  795.     /* fill DC for inter blocks */
  796.     for(mb_y=0; mb_y<s->mb_height; mb_y++){
  797.         for(mb_x=0; mb_x<s->mb_width; mb_x++){
  798.             int dc, dcu, dcv, y, n;
  799.             int16_t *dc_ptr;
  800.             uint8_t *dest_y, *dest_cb, *dest_cr;
  801.             const int mb_xy= mb_x + mb_y * s->mb_stride;
  802.             const int mb_type= s->current_picture.mb_type[mb_xy];
  803.             error= s->error_status_table[mb_xy];
  804.             if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
  805. //            if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
  806.             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  807.             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
  808.             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
  809.             dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
  810.             for(n=0; n<4; n++){
  811.                 dc=0;
  812.                 for(y=0; y<8; y++){
  813.                     int x;
  814.                     for(x=0; x<8; x++){
  815.                        dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
  816.                     }
  817.                 }
  818.                 dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
  819.             }
  820.             dcu=dcv=0;
  821.             for(y=0; y<8; y++){
  822.                 int x;
  823.                 for(x=0; x<8; x++){
  824.                     dcu+=dest_cb[x + y*(s->uvlinesize)];
  825.                     dcv+=dest_cr[x + y*(s->uvlinesize)];
  826.                 }
  827.             }
  828.             s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
  829.             s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
  830.         }
  831.     }
  832. #if 1
  833.     /* guess DC for damaged blocks */
  834.     guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
  835.     guess_dc(s, s->dc_val[1], s->mb_width  , s->mb_height  , s->mb_stride, 0);
  836.     guess_dc(s, s->dc_val[2], s->mb_width  , s->mb_height  , s->mb_stride, 0);
  837. #endif
  838.     /* filter luma DC */
  839.     filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
  840. #if 1
  841.     /* render DC only intra */
  842.     for(mb_y=0; mb_y<s->mb_height; mb_y++){
  843.         for(mb_x=0; mb_x<s->mb_width; mb_x++){
  844.             uint8_t *dest_y, *dest_cb, *dest_cr;
  845.             const int mb_xy= mb_x + mb_y * s->mb_stride;
  846.             const int mb_type= s->current_picture.mb_type[mb_xy];
  847.             error= s->error_status_table[mb_xy];
  848.             if(IS_INTER(mb_type)) continue;
  849.             if(!(error&AC_ERROR)) continue;              //undamaged
  850.             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  851.             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
  852.             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
  853.             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  854.         }
  855.     }
  856. #endif
  857.     if(s->avctx->error_concealment&FF_EC_DEBLOCK){
  858.         /* filter horizontal block boundaries */
  859.         h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
  860.         h_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
  861.         h_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
  862.         /* filter vertical block boundaries */
  863.         v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
  864.         v_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
  865.         v_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
  866.     }
  867. #ifdef HAVE_XVMC
  868. ec_clean:
  869. #endif
  870.     /* clean a few tables */
  871.     for(i=0; i<s->mb_num; i++){
  872.         const int mb_xy= s->mb_index2xy[i];
  873.         int error= s->error_status_table[mb_xy];
  874.         if(s->pict_type!=FF_B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
  875.             s->mbskip_table[mb_xy]=0;
  876.         }
  877.         s->mbintra_table[mb_xy]=1;
  878.     }
  879. }