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