00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00028 #include <limits.h>
00029 
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 #include "h264.h"
00034 #include "rectangle.h"
00035 #include "thread.h"
00036 
00037 
00038 
00039 
00040 
00041 #undef mb_intra
00042 
00043 static void decode_mb(MpegEncContext *s, int ref)
00044 {
00045     s->dest[0] = s->current_picture.f.data[0] + (s->mb_y *  16                       * s->linesize)   + s->mb_x *  16;
00046     s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
00047     s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
00048 
00049     ff_init_block_index(s);
00050     ff_update_block_index(s);
00051     s->dest[1] += (16 >> s->chroma_x_shift) - 8;
00052     s->dest[2] += (16 >> s->chroma_x_shift) - 8;
00053 
00054     if (CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264) {
00055         H264Context *h = (void*)s;
00056         h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
00057         memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
00058         av_assert1(ref >= 0);
00059         
00060 
00061 
00062 
00063         if (ref >= h->ref_count[0])
00064             ref = 0;
00065         if (!h->ref_list[0][ref].f.data[0]) {
00066             av_log(s->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
00067             ref = 0;
00068         }
00069         fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],
00070                        2, 2, 2, ref, 1);
00071         fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
00072         fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
00073                        pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);
00074         h->mb_mbaff =
00075         h->mb_field_decoding_flag = 0;
00076         ff_h264_hl_decode_mb(h);
00077     } else {
00078         assert(ref == 0);
00079         ff_MPV_decode_mb(s, s->block);
00080     }
00081 }
00082 
00087 static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
00088 {
00089     if (s->codec_id == CODEC_ID_H264) {
00090         H264Context *h = (void*)s;
00091         av_assert0(s->quarter_sample);
00092         *mv_step = 4;
00093         *stride  = h->b_stride;
00094     } else {
00095         *mv_step = 2;
00096         *stride  = s->b8_stride;
00097     }
00098 }
00099 
00103 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
00104                    uint8_t *dest_cr, int mb_x, int mb_y)
00105 {
00106     int dc, dcu, dcv, y, i;
00107     for (i = 0; i < 4; i++) {
00108         dc = s->dc_val[0][mb_x * 2 + (i &  1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
00109         if (dc < 0)
00110             dc = 0;
00111         else if (dc > 2040)
00112             dc = 2040;
00113         for (y = 0; y < 8; y++) {
00114             int x;
00115             for (x = 0; x < 8; x++)
00116                 dest_y[x + (i &  1) * 8 + (y + (i >> 1) * 8) * s->linesize] = dc / 8;
00117         }
00118     }
00119     dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
00120     dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
00121     if (dcu < 0)
00122         dcu = 0;
00123     else if (dcu > 2040)
00124         dcu = 2040;
00125     if (dcv < 0)
00126         dcv = 0;
00127     else if (dcv > 2040)
00128         dcv = 2040;
00129     for (y = 0; y < 8; y++) {
00130         int x;
00131         for (x = 0; x < 8; x++) {
00132             dest_cb[x + y * s->uvlinesize] = dcu / 8;
00133             dest_cr[x + y * s->uvlinesize] = dcv / 8;
00134         }
00135     }
00136 }
00137 
00138 static void filter181(int16_t *data, int width, int height, int stride)
00139 {
00140     int x, y;
00141 
00142     
00143     for (y = 1; y < height - 1; y++) {
00144         int prev_dc = data[0 + y * stride];
00145 
00146         for (x = 1; x < width - 1; x++) {
00147             int dc;
00148             dc = -prev_dc +
00149                  data[x     + y * stride] * 8 -
00150                  data[x + 1 + y * stride];
00151             dc = (dc * 10923 + 32768) >> 16;
00152             prev_dc = data[x + y * stride];
00153             data[x + y * stride] = dc;
00154         }
00155     }
00156 
00157     
00158     for (x = 1; x < width - 1; x++) {
00159         int prev_dc = data[x];
00160 
00161         for (y = 1; y < height - 1; y++) {
00162             int dc;
00163 
00164             dc = -prev_dc +
00165                  data[x +  y      * stride] * 8 -
00166                  data[x + (y + 1) * stride];
00167             dc = (dc * 10923 + 32768) >> 16;
00168             prev_dc = data[x + y * stride];
00169             data[x + y * stride] = dc;
00170         }
00171     }
00172 }
00173 
00179 static void guess_dc(MpegEncContext *s, int16_t *dc, int w,
00180                      int h, int stride, int is_luma)
00181 {
00182     int b_x, b_y;
00183     int16_t  (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
00184     uint32_t (*dist)[4] = av_malloc(stride*h*sizeof(uint32_t)*4);
00185 
00186     for(b_y=0; b_y<h; b_y++){
00187         int color= 1024;
00188         int distance= -1;
00189         for(b_x=0; b_x<w; b_x++){
00190             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00191             int error_j= s->error_status_table[mb_index_j];
00192             int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00193             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00194                 color= dc[b_x + b_y*stride];
00195                 distance= b_x;
00196             }
00197             col [b_x + b_y*stride][1]= color;
00198             dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
00199         }
00200         color= 1024;
00201         distance= -1;
00202         for(b_x=w-1; b_x>=0; b_x--){
00203             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00204             int error_j= s->error_status_table[mb_index_j];
00205             int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00206             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00207                 color= dc[b_x + b_y*stride];
00208                 distance= b_x;
00209             }
00210             col [b_x + b_y*stride][0]= color;
00211             dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
00212         }
00213     }
00214     for(b_x=0; b_x<w; b_x++){
00215         int color= 1024;
00216         int distance= -1;
00217         for(b_y=0; b_y<h; b_y++){
00218             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00219             int error_j= s->error_status_table[mb_index_j];
00220             int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00221             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00222                 color= dc[b_x + b_y*stride];
00223                 distance= b_y;
00224             }
00225             col [b_x + b_y*stride][3]= color;
00226             dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
00227         }
00228         color= 1024;
00229         distance= -1;
00230         for(b_y=h-1; b_y>=0; b_y--){
00231             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00232             int error_j= s->error_status_table[mb_index_j];
00233             int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00234             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00235                 color= dc[b_x + b_y*stride];
00236                 distance= b_y;
00237             }
00238             col [b_x + b_y*stride][2]= color;
00239             dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
00240         }
00241     }
00242 
00243     for (b_y = 0; b_y < h; b_y++) {
00244         for (b_x = 0; b_x < w; b_x++) {
00245             int mb_index, error, j;
00246             int64_t guess, weight_sum;
00247             mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
00248             error    = s->error_status_table[mb_index];
00249 
00250             if (IS_INTER(s->current_picture.f.mb_type[mb_index]))
00251                 continue; 
00252             if (!(error & ER_DC_ERROR))
00253                 continue; 
00254 
00255             weight_sum = 0;
00256             guess      = 0;
00257             for (j = 0; j < 4; j++) {
00258                 int64_t weight  = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
00259                 guess          += weight*(int64_t)col[b_x + b_y*stride][j];
00260                 weight_sum     += weight;
00261             }
00262             guess = (guess + weight_sum / 2) / weight_sum;
00263             dc[b_x + b_y * stride] = guess;
00264         }
00265     }
00266     av_freep(&col);
00267     av_freep(&dist);
00268 }
00269 
00275 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w,
00276                            int h, int stride, int is_luma)
00277 {
00278     int b_x, b_y, mvx_stride, mvy_stride;
00279     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00280     set_mv_strides(s, &mvx_stride, &mvy_stride);
00281     mvx_stride >>= is_luma;
00282     mvy_stride *= mvx_stride;
00283 
00284     for (b_y = 0; b_y < h; b_y++) {
00285         for (b_x = 0; b_x < w - 1; b_x++) {
00286             int y;
00287             int left_status  = s->error_status_table[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride];
00288             int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
00289             int left_intra   = IS_INTRA(s->current_picture.f.mb_type[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
00290             int right_intra  = IS_INTRA(s->current_picture.f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
00291             int left_damage  = left_status & ER_MB_ERROR;
00292             int right_damage = right_status & ER_MB_ERROR;
00293             int offset       = b_x * 8 + b_y * stride * 8;
00294             int16_t *left_mv  = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride *  b_x];
00295             int16_t *right_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
00296             if (!(left_damage || right_damage))
00297                 continue; 
00298             if ((!left_intra) && (!right_intra) &&
00299                 FFABS(left_mv[0] - right_mv[0]) +
00300                 FFABS(left_mv[1] + right_mv[1]) < 2)
00301                 continue;
00302 
00303             for (y = 0; y < 8; y++) {
00304                 int a, b, c, d;
00305 
00306                 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
00307                 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
00308                 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
00309 
00310                 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
00311                 d = FFMAX(d, 0);
00312                 if (b < 0)
00313                     d = -d;
00314 
00315                 if (d == 0)
00316                     continue;
00317 
00318                 if (!(left_damage && right_damage))
00319                     d = d * 16 / 9;
00320 
00321                 if (left_damage) {
00322                     dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
00323                     dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
00324                     dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
00325                     dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
00326                 }
00327                 if (right_damage) {
00328                     dst[offset + 8 + y * stride] = cm[dst[offset +  8 + y * stride] - ((d * 7) >> 4)];
00329                     dst[offset + 9 + y * stride] = cm[dst[offset +  9 + y * stride] - ((d * 5) >> 4)];
00330                     dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
00331                     dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
00332                 }
00333             }
00334         }
00335     }
00336 }
00337 
00343 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h,
00344                            int stride, int is_luma)
00345 {
00346     int b_x, b_y, mvx_stride, mvy_stride;
00347     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00348     set_mv_strides(s, &mvx_stride, &mvy_stride);
00349     mvx_stride >>= is_luma;
00350     mvy_stride *= mvx_stride;
00351 
00352     for (b_y = 0; b_y < h - 1; b_y++) {
00353         for (b_x = 0; b_x < w; b_x++) {
00354             int x;
00355             int top_status    = s->error_status_table[(b_x >> is_luma) +  (b_y      >> is_luma) * s->mb_stride];
00356             int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
00357             int top_intra     = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ( b_y      >> is_luma) * s->mb_stride]);
00358             int bottom_intra  = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
00359             int top_damage    = top_status & ER_MB_ERROR;
00360             int bottom_damage = bottom_status & ER_MB_ERROR;
00361             int offset        = b_x * 8 + b_y * stride * 8;
00362 
00363             int16_t *top_mv    = s->current_picture.f.motion_val[0][mvy_stride *  b_y      + mvx_stride * b_x];
00364             int16_t *bottom_mv = s->current_picture.f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
00365 
00366             if (!(top_damage || bottom_damage))
00367                 continue; 
00368 
00369             if ((!top_intra) && (!bottom_intra) &&
00370                 FFABS(top_mv[0] - bottom_mv[0]) +
00371                 FFABS(top_mv[1] + bottom_mv[1]) < 2)
00372                 continue;
00373 
00374             for (x = 0; x < 8; x++) {
00375                 int a, b, c, d;
00376 
00377                 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
00378                 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
00379                 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
00380 
00381                 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
00382                 d = FFMAX(d, 0);
00383                 if (b < 0)
00384                     d = -d;
00385 
00386                 if (d == 0)
00387                     continue;
00388 
00389                 if (!(top_damage && bottom_damage))
00390                     d = d * 16 / 9;
00391 
00392                 if (top_damage) {
00393                     dst[offset + x +  7 * stride] = cm[dst[offset + x +  7 * stride] + ((d * 7) >> 4)];
00394                     dst[offset + x +  6 * stride] = cm[dst[offset + x +  6 * stride] + ((d * 5) >> 4)];
00395                     dst[offset + x +  5 * stride] = cm[dst[offset + x +  5 * stride] + ((d * 3) >> 4)];
00396                     dst[offset + x +  4 * stride] = cm[dst[offset + x +  4 * stride] + ((d * 1) >> 4)];
00397                 }
00398                 if (bottom_damage) {
00399                     dst[offset + x +  8 * stride] = cm[dst[offset + x +  8 * stride] - ((d * 7) >> 4)];
00400                     dst[offset + x +  9 * stride] = cm[dst[offset + x +  9 * stride] - ((d * 5) >> 4)];
00401                     dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
00402                     dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
00403                 }
00404             }
00405         }
00406     }
00407 }
00408 
00409 static void guess_mv(MpegEncContext *s)
00410 {
00411     uint8_t *fixed = av_malloc(s->mb_stride * s->mb_height);
00412 #define MV_FROZEN    3
00413 #define MV_CHANGED   2
00414 #define MV_UNCHANGED 1
00415     const int mb_stride = s->mb_stride;
00416     const int mb_width  = s->mb_width;
00417     const int mb_height = s->mb_height;
00418     int i, depth, num_avail;
00419     int mb_x, mb_y, mot_step, mot_stride;
00420 
00421     set_mv_strides(s, &mot_step, &mot_stride);
00422 
00423     num_avail = 0;
00424     for (i = 0; i < s->mb_num; i++) {
00425         const int mb_xy = s->mb_index2xy[i];
00426         int f = 0;
00427         int error = s->error_status_table[mb_xy];
00428 
00429         if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00430             f = MV_FROZEN; 
00431         if (!(error & ER_MV_ERROR))
00432             f = MV_FROZEN; 
00433 
00434         fixed[mb_xy] = f;
00435         if (f == MV_FROZEN)
00436             num_avail++;
00437         else if(s->last_picture.f.data[0] && s->last_picture.f.motion_val[0]){
00438             const int mb_y= mb_xy / s->mb_stride;
00439             const int mb_x= mb_xy % s->mb_stride;
00440             const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
00441             s->current_picture.f.motion_val[0][mot_index][0]= s->last_picture.f.motion_val[0][mot_index][0];
00442             s->current_picture.f.motion_val[0][mot_index][1]= s->last_picture.f.motion_val[0][mot_index][1];
00443             s->current_picture.f.ref_index[0][4*mb_xy]      = s->last_picture.f.ref_index[0][4*mb_xy];
00444         }
00445     }
00446 
00447     if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
00448         num_avail <= mb_width / 2) {
00449         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00450             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00451                 const int mb_xy = mb_x + mb_y * s->mb_stride;
00452 
00453                 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00454                     continue;
00455                 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
00456                     continue;
00457 
00458                 s->mv_dir     = s->last_picture.f.data[0] ? MV_DIR_FORWARD
00459                                                           : MV_DIR_BACKWARD;
00460                 s->mb_intra   = 0;
00461                 s->mv_type    = MV_TYPE_16X16;
00462                 s->mb_skipped = 0;
00463 
00464                 s->dsp.clear_blocks(s->block[0]);
00465 
00466                 s->mb_x        = mb_x;
00467                 s->mb_y        = mb_y;
00468                 s->mv[0][0][0] = 0;
00469                 s->mv[0][0][1] = 0;
00470                 decode_mb(s, 0);
00471             }
00472         }
00473         goto end;
00474     }
00475 
00476     for (depth = 0; ; depth++) {
00477         int changed, pass, none_left;
00478 
00479         none_left = 1;
00480         changed   = 1;
00481         for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
00482             int mb_x, mb_y;
00483             int score_sum = 0;
00484 
00485             changed = 0;
00486             for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00487                 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00488                     const int mb_xy        = mb_x + mb_y * s->mb_stride;
00489                     int mv_predictor[8][2] = { { 0 } };
00490                     int ref[8]             = { 0 };
00491                     int pred_count         = 0;
00492                     int j;
00493                     int best_score         = 256 * 256 * 256 * 64;
00494                     int best_pred          = 0;
00495                     const int mot_index    = (mb_x + mb_y * mot_stride) * mot_step;
00496                     int prev_x, prev_y, prev_ref;
00497 
00498                     if ((mb_x ^ mb_y ^ pass) & 1)
00499                         continue;
00500 
00501                     if (fixed[mb_xy] == MV_FROZEN)
00502                         continue;
00503                     av_assert1(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
00504                     av_assert1(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
00505 
00506                     j = 0;
00507                     if (mb_x > 0             && fixed[mb_xy - 1]         == MV_FROZEN)
00508                         j = 1;
00509                     if (mb_x + 1 < mb_width  && fixed[mb_xy + 1]         == MV_FROZEN)
00510                         j = 1;
00511                     if (mb_y > 0             && fixed[mb_xy - mb_stride] == MV_FROZEN)
00512                         j = 1;
00513                     if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
00514                         j = 1;
00515                     if (j == 0)
00516                         continue;
00517 
00518                     j = 0;
00519                     if (mb_x > 0             && fixed[mb_xy - 1        ] == MV_CHANGED)
00520                         j = 1;
00521                     if (mb_x + 1 < mb_width  && fixed[mb_xy + 1        ] == MV_CHANGED)
00522                         j = 1;
00523                     if (mb_y > 0             && fixed[mb_xy - mb_stride] == MV_CHANGED)
00524                         j = 1;
00525                     if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
00526                         j = 1;
00527                     if (j == 0 && pass > 1)
00528                         continue;
00529 
00530                     none_left = 0;
00531 
00532                     if (mb_x > 0 && fixed[mb_xy - 1]) {
00533                         mv_predictor[pred_count][0] =
00534                             s->current_picture.f.motion_val[0][mot_index - mot_step][0];
00535                         mv_predictor[pred_count][1] =
00536                             s->current_picture.f.motion_val[0][mot_index - mot_step][1];
00537                         ref[pred_count] =
00538                             s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
00539                         pred_count++;
00540                     }
00541                     if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
00542                         mv_predictor[pred_count][0] =
00543                             s->current_picture.f.motion_val[0][mot_index + mot_step][0];
00544                         mv_predictor[pred_count][1] =
00545                             s->current_picture.f.motion_val[0][mot_index + mot_step][1];
00546                         ref[pred_count] =
00547                             s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
00548                         pred_count++;
00549                     }
00550                     if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
00551                         mv_predictor[pred_count][0] =
00552                             s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
00553                         mv_predictor[pred_count][1] =
00554                             s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
00555                         ref[pred_count] =
00556                             s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
00557                         pred_count++;
00558                     }
00559                     if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
00560                         mv_predictor[pred_count][0] =
00561                             s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
00562                         mv_predictor[pred_count][1] =
00563                             s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
00564                         ref[pred_count] =
00565                             s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
00566                         pred_count++;
00567                     }
00568                     if (pred_count == 0)
00569                         continue;
00570 
00571                     if (pred_count > 1) {
00572                         int sum_x = 0, sum_y = 0, sum_r = 0;
00573                         int max_x, max_y, min_x, min_y, max_r, min_r;
00574 
00575                         for (j = 0; j < pred_count; j++) {
00576                             sum_x += mv_predictor[j][0];
00577                             sum_y += mv_predictor[j][1];
00578                             sum_r += ref[j];
00579                             if (j && ref[j] != ref[j - 1])
00580                                 goto skip_mean_and_median;
00581                         }
00582 
00583                         
00584                         mv_predictor[pred_count][0] = sum_x / j;
00585                         mv_predictor[pred_count][1] = sum_y / j;
00586                                  ref[pred_count]    = sum_r / j;
00587 
00588                         
00589                         if (pred_count >= 3) {
00590                             min_y = min_x = min_r =  99999;
00591                             max_y = max_x = max_r = -99999;
00592                         } else {
00593                             min_x = min_y = max_x = max_y = min_r = max_r = 0;
00594                         }
00595                         for (j = 0; j < pred_count; j++) {
00596                             max_x = FFMAX(max_x, mv_predictor[j][0]);
00597                             max_y = FFMAX(max_y, mv_predictor[j][1]);
00598                             max_r = FFMAX(max_r, ref[j]);
00599                             min_x = FFMIN(min_x, mv_predictor[j][0]);
00600                             min_y = FFMIN(min_y, mv_predictor[j][1]);
00601                             min_r = FFMIN(min_r, ref[j]);
00602                         }
00603                         mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
00604                         mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
00605                                  ref[pred_count + 1]    = sum_r - max_r - min_r;
00606 
00607                         if (pred_count == 4) {
00608                             mv_predictor[pred_count + 1][0] /= 2;
00609                             mv_predictor[pred_count + 1][1] /= 2;
00610                                      ref[pred_count + 1]    /= 2;
00611                         }
00612                         pred_count += 2;
00613                     }
00614 
00615 skip_mean_and_median:
00616                     
00617                     pred_count++;
00618 
00619                     if (!fixed[mb_xy] && 0) {
00620                         if (s->avctx->codec_id == CODEC_ID_H264) {
00621                             
00622                         } else {
00623                             ff_thread_await_progress(&s->last_picture_ptr->f,
00624                                                      mb_y, 0);
00625                         }
00626                         if (!s->last_picture.f.motion_val[0] ||
00627                             !s->last_picture.f.ref_index[0])
00628                             goto skip_last_mv;
00629                         prev_x   = s->last_picture.f.motion_val[0][mot_index][0];
00630                         prev_y   = s->last_picture.f.motion_val[0][mot_index][1];
00631                         prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
00632                     } else {
00633                         prev_x   = s->current_picture.f.motion_val[0][mot_index][0];
00634                         prev_y   = s->current_picture.f.motion_val[0][mot_index][1];
00635                         prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
00636                     }
00637 
00638                     
00639                     mv_predictor[pred_count][0] = prev_x;
00640                     mv_predictor[pred_count][1] = prev_y;
00641                              ref[pred_count]    = prev_ref;
00642                     pred_count++;
00643 
00644 skip_last_mv:
00645                     s->mv_dir     = MV_DIR_FORWARD;
00646                     s->mb_intra   = 0;
00647                     s->mv_type    = MV_TYPE_16X16;
00648                     s->mb_skipped = 0;
00649 
00650                     s->dsp.clear_blocks(s->block[0]);
00651 
00652                     s->mb_x = mb_x;
00653                     s->mb_y = mb_y;
00654 
00655                     for (j = 0; j < pred_count; j++) {
00656                         int score = 0;
00657                         uint8_t *src = s->current_picture.f.data[0] +
00658                                        mb_x * 16 + mb_y * 16 * s->linesize;
00659 
00660                         s->current_picture.f.motion_val[0][mot_index][0] =
00661                             s->mv[0][0][0] = mv_predictor[j][0];
00662                         s->current_picture.f.motion_val[0][mot_index][1] =
00663                             s->mv[0][0][1] = mv_predictor[j][1];
00664 
00665                         
00666                         if (ref[j] < 0)
00667                             continue;
00668 
00669                         decode_mb(s, ref[j]);
00670 
00671                         if (mb_x > 0 && fixed[mb_xy - 1]) {
00672                             int k;
00673                             for (k = 0; k < 16; k++)
00674                                 score += FFABS(src[k * s->linesize - 1] -
00675                                                src[k * s->linesize]);
00676                         }
00677                         if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
00678                             int k;
00679                             for (k = 0; k < 16; k++)
00680                                 score += FFABS(src[k * s->linesize + 15] -
00681                                                src[k * s->linesize + 16]);
00682                         }
00683                         if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
00684                             int k;
00685                             for (k = 0; k < 16; k++)
00686                                 score += FFABS(src[k - s->linesize] - src[k]);
00687                         }
00688                         if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
00689                             int k;
00690                             for (k = 0; k < 16; k++)
00691                                 score += FFABS(src[k + s->linesize * 15] -
00692                                                src[k + s->linesize * 16]);
00693                         }
00694 
00695                         if (score <= best_score) { 
00696                             best_score = score;
00697                             best_pred  = j;
00698                         }
00699                     }
00700                     score_sum += best_score;
00701                     s->mv[0][0][0] = mv_predictor[best_pred][0];
00702                     s->mv[0][0][1] = mv_predictor[best_pred][1];
00703 
00704                     for (i = 0; i < mot_step; i++)
00705                         for (j = 0; j < mot_step; j++) {
00706                             s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
00707                             s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
00708                         }
00709 
00710                     decode_mb(s, ref[best_pred]);
00711 
00712 
00713                     if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
00714                         fixed[mb_xy] = MV_CHANGED;
00715                         changed++;
00716                     } else
00717                         fixed[mb_xy] = MV_UNCHANGED;
00718                 }
00719             }
00720 
00721             
00722         }
00723 
00724         if (none_left)
00725             goto end;
00726 
00727         for (i = 0; i < s->mb_num; i++) {
00728             int mb_xy = s->mb_index2xy[i];
00729             if (fixed[mb_xy])
00730                 fixed[mb_xy] = MV_FROZEN;
00731         }
00732         
00733     }
00734 end:
00735     av_free(fixed);
00736 }
00737 
00738 static int is_intra_more_likely(MpegEncContext *s)
00739 {
00740     int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
00741 
00742     if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
00743         return 1; 
00744 
00745     undamaged_count = 0;
00746     for (i = 0; i < s->mb_num; i++) {
00747         const int mb_xy = s->mb_index2xy[i];
00748         const int error = s->error_status_table[mb_xy];
00749         if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
00750             undamaged_count++;
00751     }
00752 
00753     if (s->codec_id == CODEC_ID_H264) {
00754         H264Context *h = (void*) s;
00755         if (h->list_count <= 0 || h->ref_count[0] <= 0 ||
00756             !h->ref_list[0][0].f.data[0])
00757             return 1;
00758     }
00759 
00760     if (undamaged_count < 5)
00761         return 0; 
00762 
00763     
00764     if (CONFIG_MPEG_XVMC_DECODER    &&
00765         s->avctx->xvmc_acceleration &&
00766         s->pict_type == AV_PICTURE_TYPE_I)
00767         return 1;
00768 
00769     skip_amount     = FFMAX(undamaged_count / 50, 1); 
00770     is_intra_likely = 0;
00771 
00772     j = 0;
00773     for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
00774         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00775             int error;
00776             const int mb_xy = mb_x + mb_y * s->mb_stride;
00777 
00778             error = s->error_status_table[mb_xy];
00779             if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
00780                 continue; 
00781 
00782             j++;
00783             
00784             if ((j % skip_amount) != 0)
00785                 continue;
00786 
00787             if (s->pict_type == AV_PICTURE_TYPE_I) {
00788                 uint8_t *mb_ptr      = s->current_picture.f.data[0] +
00789                                        mb_x * 16 + mb_y * 16 * s->linesize;
00790                 uint8_t *last_mb_ptr = s->last_picture.f.data[0] +
00791                                        mb_x * 16 + mb_y * 16 * s->linesize;
00792 
00793                 if (s->avctx->codec_id == CODEC_ID_H264) {
00794                     
00795                 } else {
00796                     ff_thread_await_progress(&s->last_picture_ptr->f,
00797                                              mb_y, 0);
00798                 }
00799                 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr                    , s->linesize, 16);
00800                 
00801                 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
00802             } else {
00803                 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00804                    is_intra_likely++;
00805                 else
00806                    is_intra_likely--;
00807             }
00808         }
00809     }
00810     
00811     return is_intra_likely > 0;
00812 }
00813 
00814 void ff_er_frame_start(MpegEncContext *s)
00815 {
00816     if (!s->err_recognition)
00817         return;
00818 
00819     memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
00820            s->mb_stride * s->mb_height * sizeof(uint8_t));
00821     s->error_count    = 3 * s->mb_num;
00822     s->error_occurred = 0;
00823 }
00824 
00832 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
00833                      int endx, int endy, int status)
00834 {
00835     const int start_i  = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
00836     const int end_i    = av_clip(endx   + endy   * s->mb_width, 0, s->mb_num);
00837     const int start_xy = s->mb_index2xy[start_i];
00838     const int end_xy   = s->mb_index2xy[end_i];
00839     int mask           = -1;
00840 
00841     if (s->avctx->hwaccel)
00842         return;
00843 
00844     if (start_i > end_i || start_xy > end_xy) {
00845         av_log(s->avctx, AV_LOG_ERROR,
00846                "internal error, slice end before start\n");
00847         return;
00848     }
00849 
00850     if (!s->err_recognition)
00851         return;
00852 
00853     mask &= ~VP_START;
00854     if (status & (ER_AC_ERROR | ER_AC_END)) {
00855         mask           &= ~(ER_AC_ERROR | ER_AC_END);
00856         s->error_count -= end_i - start_i + 1;
00857     }
00858     if (status & (ER_DC_ERROR | ER_DC_END)) {
00859         mask           &= ~(ER_DC_ERROR | ER_DC_END);
00860         s->error_count -= end_i - start_i + 1;
00861     }
00862     if (status & (ER_MV_ERROR | ER_MV_END)) {
00863         mask           &= ~(ER_MV_ERROR | ER_MV_END);
00864         s->error_count -= end_i - start_i + 1;
00865     }
00866 
00867     if (status & ER_MB_ERROR) {
00868         s->error_occurred = 1;
00869         s->error_count    = INT_MAX;
00870     }
00871 
00872     if (mask == ~0x7F) {
00873         memset(&s->error_status_table[start_xy], 0,
00874                (end_xy - start_xy) * sizeof(uint8_t));
00875     } else {
00876         int i;
00877         for (i = start_xy; i < end_xy; i++)
00878             s->error_status_table[i] &= mask;
00879     }
00880 
00881     if (end_i == s->mb_num)
00882         s->error_count = INT_MAX;
00883     else {
00884         s->error_status_table[end_xy] &= mask;
00885         s->error_status_table[end_xy] |= status;
00886     }
00887 
00888     s->error_status_table[start_xy] |= VP_START;
00889 
00890     if (start_xy > 0 && s->avctx->thread_count <= 1 &&
00891         s->avctx->skip_top * s->mb_width < start_i) {
00892         int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
00893 
00894         prev_status &= ~ VP_START;
00895         if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
00896             s->error_count = INT_MAX;
00897     }
00898 }
00899 
00900 void ff_er_frame_end(MpegEncContext *s)
00901 {
00902     int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
00903     int distance;
00904     int threshold_part[4] = { 100, 100, 100 };
00905     int threshold = 50;
00906     int is_intra_likely;
00907     int size = s->b8_stride * 2 * s->mb_height;
00908     Picture *pic = s->current_picture_ptr;
00909 
00910     
00911 
00912     if (!s->err_recognition || s->error_count == 0 || s->avctx->lowres ||
00913         s->avctx->hwaccel                                              ||
00914         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU          ||
00915         s->picture_structure != PICT_FRAME                             ||
00916         s->error_count == 3 * s->mb_width *
00917                           (s->avctx->skip_top + s->avctx->skip_bottom)) {
00918         return;
00919     };
00920 
00921     if (s->current_picture.f.motion_val[0] == NULL) {
00922         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
00923 
00924         for (i = 0; i < 2; i++) {
00925             pic->f.ref_index[i]     = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
00926             pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
00927             pic->f.motion_val[i]    = pic->motion_val_base[i] + 4;
00928         }
00929         pic->f.motion_subsample_log2 = 3;
00930         s->current_picture = *s->current_picture_ptr;
00931     }
00932 
00933     if (s->avctx->debug & FF_DEBUG_ER) {
00934         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00935             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00936                 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
00937 
00938                 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
00939             }
00940             av_log(s->avctx, AV_LOG_DEBUG, "\n");
00941         }
00942     }
00943 
00944 #if 1
00945     
00946     for (error_type = 1; error_type <= 3; error_type++) {
00947         int end_ok = 0;
00948 
00949         for (i = s->mb_num - 1; i >= 0; i--) {
00950             const int mb_xy = s->mb_index2xy[i];
00951             int error       = s->error_status_table[mb_xy];
00952 
00953             if (error & (1 << error_type))
00954                 end_ok = 1;
00955             if (error & (8 << error_type))
00956                 end_ok = 1;
00957 
00958             if (!end_ok)
00959                 s->error_status_table[mb_xy] |= 1 << error_type;
00960 
00961             if (error & VP_START)
00962                 end_ok = 0;
00963         }
00964     }
00965 #endif
00966 #if 1
00967     
00968     if (s->partitioned_frame) {
00969         int end_ok = 0;
00970 
00971         for (i = s->mb_num - 1; i >= 0; i--) {
00972             const int mb_xy = s->mb_index2xy[i];
00973             int error       = s->error_status_table[mb_xy];
00974 
00975             if (error & ER_AC_END)
00976                 end_ok = 0;
00977             if ((error & ER_MV_END) ||
00978                 (error & ER_DC_END) ||
00979                 (error & ER_AC_ERROR))
00980                 end_ok = 1;
00981 
00982             if (!end_ok)
00983                 s->error_status_table[mb_xy]|= ER_AC_ERROR;
00984 
00985             if (error & VP_START)
00986                 end_ok = 0;
00987         }
00988     }
00989 #endif
00990     
00991     if (s->err_recognition & AV_EF_EXPLODE) {
00992         int end_ok = 1;
00993 
00994         
00995         for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
00996             const int mb_xy = s->mb_index2xy[i];
00997             int error1 = s->error_status_table[mb_xy];
00998             int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
00999 
01000             if (error1 & VP_START)
01001                 end_ok = 1;
01002 
01003             if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
01004                 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
01005                 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
01006                 (error1 & ER_MV_END))) {
01007                 
01008                 end_ok = 0;
01009             }
01010 
01011             if (!end_ok)
01012                 s->error_status_table[mb_xy] |= ER_MB_ERROR;
01013         }
01014     }
01015 
01016 #if 1
01017     
01018     distance = 9999999;
01019     for (error_type = 1; error_type <= 3; error_type++) {
01020         for (i = s->mb_num - 1; i >= 0; i--) {
01021             const int mb_xy = s->mb_index2xy[i];
01022             int       error = s->error_status_table[mb_xy];
01023 
01024             if (!s->mbskip_table[mb_xy]) 
01025                 distance++;
01026             if (error & (1 << error_type))
01027                 distance = 0;
01028 
01029             if (s->partitioned_frame) {
01030                 if (distance < threshold_part[error_type - 1])
01031                     s->error_status_table[mb_xy] |= 1 << error_type;
01032             } else {
01033                 if (distance < threshold)
01034                     s->error_status_table[mb_xy] |= 1 << error_type;
01035             }
01036 
01037             if (error & VP_START)
01038                 distance = 9999999;
01039         }
01040     }
01041 #endif
01042 
01043     
01044     error = 0;
01045     for (i = 0; i < s->mb_num; i++) {
01046         const int mb_xy = s->mb_index2xy[i];
01047         int old_error   = s->error_status_table[mb_xy];
01048 
01049         if (old_error & VP_START) {
01050             error = old_error & ER_MB_ERROR;
01051         } else {
01052             error |= old_error & ER_MB_ERROR;
01053             s->error_status_table[mb_xy] |= error;
01054         }
01055     }
01056 #if 1
01057     
01058     if (!s->partitioned_frame) {
01059         for (i = 0; i < s->mb_num; i++) {
01060             const int mb_xy = s->mb_index2xy[i];
01061             error = s->error_status_table[mb_xy];
01062             if (error & ER_MB_ERROR)
01063                 error |= ER_MB_ERROR;
01064             s->error_status_table[mb_xy] = error;
01065         }
01066     }
01067 #endif
01068 
01069     dc_error = ac_error = mv_error = 0;
01070     for (i = 0; i < s->mb_num; i++) {
01071         const int mb_xy = s->mb_index2xy[i];
01072         error = s->error_status_table[mb_xy];
01073         if (error & ER_DC_ERROR)
01074             dc_error++;
01075         if (error & ER_AC_ERROR)
01076             ac_error++;
01077         if (error & ER_MV_ERROR)
01078             mv_error++;
01079     }
01080     av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
01081            dc_error, ac_error, mv_error);
01082 
01083     is_intra_likely = is_intra_more_likely(s);
01084 
01085     
01086     for (i = 0; i < s->mb_num; i++) {
01087         const int mb_xy = s->mb_index2xy[i];
01088         error = s->error_status_table[mb_xy];
01089         if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
01090             continue;
01091 
01092         if (is_intra_likely)
01093             s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
01094         else
01095             s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
01096     }
01097 
01098     
01099     if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
01100         for (i = 0; i < s->mb_num; i++) {
01101             const int mb_xy = s->mb_index2xy[i];
01102             if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
01103                 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
01104         }
01105 
01106     
01107     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01108         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01109             const int mb_xy   = mb_x + mb_y * s->mb_stride;
01110             const int mb_type = s->current_picture.f.mb_type[mb_xy];
01111             int dir           = !s->last_picture.f.data[0];
01112 
01113             error = s->error_status_table[mb_xy];
01114 
01115             if (IS_INTRA(mb_type))
01116                 continue; 
01117             if (error & ER_MV_ERROR)
01118                 continue; 
01119             if (!(error & ER_AC_ERROR))
01120                 continue; 
01121 
01122             s->mv_dir     = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
01123             s->mb_intra   = 0;
01124             s->mb_skipped = 0;
01125             if (IS_8X8(mb_type)) {
01126                 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
01127                 int j;
01128                 s->mv_type = MV_TYPE_8X8;
01129                 for (j = 0; j < 4; j++) {
01130                     s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
01131                     s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
01132                 }
01133             } else {
01134                 s->mv_type     = MV_TYPE_16X16;
01135                 s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
01136                 s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
01137             }
01138 
01139             s->dsp.clear_blocks(s->block[0]);
01140 
01141             s->mb_x = mb_x;
01142             s->mb_y = mb_y;
01143             decode_mb(s, 0 );
01144         }
01145     }
01146 
01147     
01148     if (s->pict_type == AV_PICTURE_TYPE_B) {
01149         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01150             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01151                 int       xy      = mb_x * 2 + mb_y * 2 * s->b8_stride;
01152                 const int mb_xy   = mb_x + mb_y * s->mb_stride;
01153                 const int mb_type = s->current_picture.f.mb_type[mb_xy];
01154 
01155                 error = s->error_status_table[mb_xy];
01156 
01157                 if (IS_INTRA(mb_type))
01158                     continue;
01159                 if (!(error & ER_MV_ERROR))
01160                     continue; 
01161                 if (!(error & ER_AC_ERROR))
01162                     continue; 
01163 
01164                 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
01165                 if (!s->last_picture.f.data[0])
01166                     s->mv_dir &= ~MV_DIR_FORWARD;
01167                 if (!s->next_picture.f.data[0])
01168                     s->mv_dir &= ~MV_DIR_BACKWARD;
01169                 s->mb_intra   = 0;
01170                 s->mv_type    = MV_TYPE_16X16;
01171                 s->mb_skipped = 0;
01172 
01173                 if (s->pp_time) {
01174                     int time_pp = s->pp_time;
01175                     int time_pb = s->pb_time;
01176 
01177                     if (s->avctx->codec_id == CODEC_ID_H264) {
01178                         
01179                     } else {
01180                         ff_thread_await_progress(&s->next_picture_ptr->f, mb_y, 0);
01181                     }
01182                     s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] *  time_pb            / time_pp;
01183                     s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] *  time_pb            / time_pp;
01184                     s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
01185                     s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
01186                 } else {
01187                     s->mv[0][0][0] = 0;
01188                     s->mv[0][0][1] = 0;
01189                     s->mv[1][0][0] = 0;
01190                     s->mv[1][0][1] = 0;
01191                 }
01192 
01193                 s->dsp.clear_blocks(s->block[0]);
01194                 s->mb_x = mb_x;
01195                 s->mb_y = mb_y;
01196                 decode_mb(s, 0);
01197             }
01198         }
01199     } else
01200         guess_mv(s);
01201 
01202     
01203     if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01204         goto ec_clean;
01205     
01206     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01207         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01208             int dc, dcu, dcv, y, n;
01209             int16_t *dc_ptr;
01210             uint8_t *dest_y, *dest_cb, *dest_cr;
01211             const int mb_xy   = mb_x + mb_y * s->mb_stride;
01212             const int mb_type = s->current_picture.f.mb_type[mb_xy];
01213 
01214             error = s->error_status_table[mb_xy];
01215 
01216             if (IS_INTRA(mb_type) && s->partitioned_frame)
01217                 continue;
01218             
01219             
01220 
01221             dest_y  = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
01222             dest_cb = s->current_picture.f.data[1] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01223             dest_cr = s->current_picture.f.data[2] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01224 
01225             dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
01226             for (n = 0; n < 4; n++) {
01227                 dc = 0;
01228                 for (y = 0; y < 8; y++) {
01229                     int x;
01230                     for (x = 0; x < 8; x++)
01231                        dc += dest_y[x + (n & 1) * 8 +
01232                              (y + (n >> 1) * 8) * s->linesize];
01233                 }
01234                 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
01235             }
01236 
01237             dcu = dcv = 0;
01238             for (y = 0; y < 8; y++) {
01239                 int x;
01240                 for (x = 0; x < 8; x++) {
01241                     dcu += dest_cb[x + y * s->uvlinesize];
01242                     dcv += dest_cr[x + y * s->uvlinesize];
01243                 }
01244             }
01245             s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
01246             s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
01247         }
01248     }
01249 #if 1
01250     
01251     guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
01252     guess_dc(s, s->dc_val[1], s->mb_width  , s->mb_height  , s->mb_stride, 0);
01253     guess_dc(s, s->dc_val[2], s->mb_width  , s->mb_height  , s->mb_stride, 0);
01254 #endif
01255 
01256     
01257     filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
01258 
01259 #if 1
01260     
01261     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01262         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01263             uint8_t *dest_y, *dest_cb, *dest_cr;
01264             const int mb_xy   = mb_x + mb_y * s->mb_stride;
01265             const int mb_type = s->current_picture.f.mb_type[mb_xy];
01266 
01267             error = s->error_status_table[mb_xy];
01268 
01269             if (IS_INTER(mb_type))
01270                 continue;
01271             if (!(error & ER_AC_ERROR))
01272                 continue; 
01273 
01274             dest_y  = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
01275             dest_cb = s->current_picture.f.data[1] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01276             dest_cr = s->current_picture.f.data[2] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01277 
01278             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
01279         }
01280     }
01281 #endif
01282 
01283     if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
01284         
01285         h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
01286                        s->mb_height * 2, s->linesize, 1);
01287         h_block_filter(s, s->current_picture.f.data[1], s->mb_width,
01288                        s->mb_height  , s->uvlinesize, 0);
01289         h_block_filter(s, s->current_picture.f.data[2], s->mb_width,
01290                        s->mb_height  , s->uvlinesize, 0);
01291 
01292         
01293         v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
01294                        s->mb_height * 2, s->linesize, 1);
01295         v_block_filter(s, s->current_picture.f.data[1], s->mb_width,
01296                        s->mb_height  , s->uvlinesize, 0);
01297         v_block_filter(s, s->current_picture.f.data[2], s->mb_width,
01298                        s->mb_height  , s->uvlinesize, 0);
01299     }
01300 
01301 ec_clean:
01302     
01303     for (i = 0; i < s->mb_num; i++) {
01304         const int mb_xy = s->mb_index2xy[i];
01305         int       error = s->error_status_table[mb_xy];
01306 
01307         if (s->pict_type != AV_PICTURE_TYPE_B &&
01308             (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
01309             s->mbskip_table[mb_xy] = 0;
01310         }
01311         s->mbintra_table[mb_xy] = 1;
01312     }
01313 }