• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00423                 const int mb_xy = mb_x + mb_y * s->mb_stride;
00424 
00425                 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00426                     continue;
00427                 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
00428                     continue;
00429 
00430                 s->mv_dir     = s->last_picture.f.data[0] ? MV_DIR_FORWARD
00431                                                           : MV_DIR_BACKWARD;
00432                 s->mb_intra   = 0;
00433                 s->mv_type    = MV_TYPE_16X16;
00434                 s->mb_skipped = 0;
00435 
00436                 s->dsp.clear_blocks(s->block[0]);
00437 
00438                 s->mb_x        = mb_x;
00439                 s->mb_y        = mb_y;
00440                 s->mv[0][0][0] = 0;
00441                 s->mv[0][0][1] = 0;
00442                 decode_mb(s, 0);
00443             }
00444         }
00445         return;
00446     }
00447 
00448     for (depth = 0; ; depth++) {
00449         int changed, pass, none_left;
00450 
00451         none_left = 1;
00452         changed   = 1;
00453         for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
00454             int mb_x, mb_y;
00455             int score_sum = 0;
00456 
00457             changed = 0;
00458             for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00459                 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00460                     const int mb_xy        = mb_x + mb_y * s->mb_stride;
00461                     int mv_predictor[8][2] = { { 0 } };
00462                     int ref[8]             = { 0 };
00463                     int pred_count         = 0;
00464                     int j;
00465                     int best_score         = 256 * 256 * 256 * 64;
00466                     int best_pred          = 0;
00467                     const int mot_index    = (mb_x + mb_y * mot_stride) * mot_step;
00468                     int prev_x, prev_y, prev_ref;
00469 
00470                     if ((mb_x ^ mb_y ^ pass) & 1)
00471                         continue;
00472 
00473                     if (fixed[mb_xy] == MV_FROZEN)
00474                         continue;
00475                     assert(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
00476                     assert(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
00477 
00478                     j = 0;
00479                     if (mb_x > 0             && fixed[mb_xy - 1]         == MV_FROZEN)
00480                         j = 1;
00481                     if (mb_x + 1 < mb_width  && fixed[mb_xy + 1]         == MV_FROZEN)
00482                         j = 1;
00483                     if (mb_y > 0             && fixed[mb_xy - mb_stride] == MV_FROZEN)
00484                         j = 1;
00485                     if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
00486                         j = 1;
00487                     if (j == 0)
00488                         continue;
00489 
00490                     j = 0;
00491                     if (mb_x > 0             && fixed[mb_xy - 1        ] == MV_CHANGED)
00492                         j = 1;
00493                     if (mb_x + 1 < mb_width  && fixed[mb_xy + 1        ] == MV_CHANGED)
00494                         j = 1;
00495                     if (mb_y > 0             && fixed[mb_xy - mb_stride] == MV_CHANGED)
00496                         j = 1;
00497                     if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
00498                         j = 1;
00499                     if (j == 0 && pass > 1)
00500                         continue;
00501 
00502                     none_left = 0;
00503 
00504                     if (mb_x > 0 && fixed[mb_xy - 1]) {
00505                         mv_predictor[pred_count][0] =
00506                             s->current_picture.f.motion_val[0][mot_index - mot_step][0];
00507                         mv_predictor[pred_count][1] =
00508                             s->current_picture.f.motion_val[0][mot_index - mot_step][1];
00509                         ref[pred_count] =
00510                             s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
00511                         pred_count++;
00512                     }
00513                     if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
00514                         mv_predictor[pred_count][0] =
00515                             s->current_picture.f.motion_val[0][mot_index + mot_step][0];
00516                         mv_predictor[pred_count][1] =
00517                             s->current_picture.f.motion_val[0][mot_index + mot_step][1];
00518                         ref[pred_count] =
00519                             s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
00520                         pred_count++;
00521                     }
00522                     if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
00523                         mv_predictor[pred_count][0] =
00524                             s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
00525                         mv_predictor[pred_count][1] =
00526                             s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
00527                         ref[pred_count] =
00528                             s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
00529                         pred_count++;
00530                     }
00531                     if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
00532                         mv_predictor[pred_count][0] =
00533                             s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
00534                         mv_predictor[pred_count][1] =
00535                             s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
00536                         ref[pred_count] =
00537                             s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
00538                         pred_count++;
00539                     }
00540                     if (pred_count == 0)
00541                         continue;
00542 
00543                     if (pred_count > 1) {
00544                         int sum_x = 0, sum_y = 0, sum_r = 0;
00545                         int max_x, max_y, min_x, min_y, max_r, min_r;
00546 
00547                         for (j = 0; j < pred_count; j++) {
00548                             sum_x += mv_predictor[j][0];
00549                             sum_y += mv_predictor[j][1];
00550                             sum_r += ref[j];
00551                             if (j && ref[j] != ref[j - 1])
00552                                 goto skip_mean_and_median;
00553                         }
00554 
00555                         /* mean */
00556                         mv_predictor[pred_count][0] = sum_x / j;
00557                         mv_predictor[pred_count][1] = sum_y / j;
00558                                  ref[pred_count]    = sum_r / j;
00559 
00560                         /* median */
00561                         if (pred_count >= 3) {
00562                             min_y = min_x = min_r =  99999;
00563                             max_y = max_x = max_r = -99999;
00564                         } else {
00565                             min_x = min_y = max_x = max_y = min_r = max_r = 0;
00566                         }
00567                         for (j = 0; j < pred_count; j++) {
00568                             max_x = FFMAX(max_x, mv_predictor[j][0]);
00569                             max_y = FFMAX(max_y, mv_predictor[j][1]);
00570                             max_r = FFMAX(max_r, ref[j]);
00571                             min_x = FFMIN(min_x, mv_predictor[j][0]);
00572                             min_y = FFMIN(min_y, mv_predictor[j][1]);
00573                             min_r = FFMIN(min_r, ref[j]);
00574                         }
00575                         mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
00576                         mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
00577                                  ref[pred_count + 1]    = sum_r - max_r - min_r;
00578 
00579                         if (pred_count == 4) {
00580                             mv_predictor[pred_count + 1][0] /= 2;
00581                             mv_predictor[pred_count + 1][1] /= 2;
00582                                      ref[pred_count + 1]    /= 2;
00583                         }
00584                         pred_count += 2;
00585                     }
00586 
00587 skip_mean_and_median:
00588                     /* zero MV */
00589                     pred_count++;
00590 
00591                     if (!fixed[mb_xy]) {
00592                         if (s->avctx->codec_id == CODEC_ID_H264) {
00593                             // FIXME
00594                         } else {
00595                             ff_thread_await_progress((AVFrame *) s->last_picture_ptr,
00596                                                      mb_y, 0);
00597                         }
00598                         if (!s->last_picture.f.motion_val[0] ||
00599                             !s->last_picture.f.ref_index[0])
00600                             goto skip_last_mv;
00601                         prev_x   = s->last_picture.f.motion_val[0][mot_index][0];
00602                         prev_y   = s->last_picture.f.motion_val[0][mot_index][1];
00603                         prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
00604                     } else {
00605                         prev_x   = s->current_picture.f.motion_val[0][mot_index][0];
00606                         prev_y   = s->current_picture.f.motion_val[0][mot_index][1];
00607                         prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
00608                     }
00609 
00610                     /* last MV */
00611                     mv_predictor[pred_count][0] = prev_x;
00612                     mv_predictor[pred_count][1] = prev_y;
00613                              ref[pred_count]    = prev_ref;
00614                     pred_count++;
00615 
00616 skip_last_mv:
00617                     s->mv_dir     = MV_DIR_FORWARD;
00618                     s->mb_intra   = 0;
00619                     s->mv_type    = MV_TYPE_16X16;
00620                     s->mb_skipped = 0;
00621 
00622                     s->dsp.clear_blocks(s->block[0]);
00623 
00624                     s->mb_x = mb_x;
00625                     s->mb_y = mb_y;
00626 
00627                     for (j = 0; j < pred_count; j++) {
00628                         int score = 0;
00629                         uint8_t *src = s->current_picture.f.data[0] +
00630                                        mb_x * 16 + mb_y * 16 * s->linesize;
00631 
00632                         s->current_picture.f.motion_val[0][mot_index][0] =
00633                             s->mv[0][0][0] = mv_predictor[j][0];
00634                         s->current_picture.f.motion_val[0][mot_index][1] =
00635                             s->mv[0][0][1] = mv_predictor[j][1];
00636 
00637                         // predictor intra or otherwise not available
00638                         if (ref[j] < 0)
00639                             continue;
00640 
00641                         decode_mb(s, ref[j]);
00642 
00643                         if (mb_x > 0 && fixed[mb_xy - 1]) {
00644                             int k;
00645                             for (k = 0; k < 16; k++)
00646                                 score += FFABS(src[k * s->linesize - 1] -
00647                                                src[k * s->linesize]);
00648                         }
00649                         if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
00650                             int k;
00651                             for (k = 0; k < 16; k++)
00652                                 score += FFABS(src[k * s->linesize + 15] -
00653                                                src[k * s->linesize + 16]);
00654                         }
00655                         if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
00656                             int k;
00657                             for (k = 0; k < 16; k++)
00658                                 score += FFABS(src[k - s->linesize] - src[k]);
00659                         }
00660                         if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
00661                             int k;
00662                             for (k = 0; k < 16; k++)
00663                                 score += FFABS(src[k + s->linesize * 15] -
00664                                                src[k + s->linesize * 16]);
00665                         }
00666 
00667                         if (score <= best_score) { // <= will favor the last MV
00668                             best_score = score;
00669                             best_pred  = j;
00670                         }
00671                     }
00672                     score_sum += best_score;
00673                     s->mv[0][0][0] = mv_predictor[best_pred][0];
00674                     s->mv[0][0][1] = mv_predictor[best_pred][1];
00675 
00676                     for (i = 0; i < mot_step; i++)
00677                         for (j = 0; j < mot_step; j++) {
00678                             s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
00679                             s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
00680                         }
00681 
00682                     decode_mb(s, ref[best_pred]);
00683 
00684 
00685                     if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
00686                         fixed[mb_xy] = MV_CHANGED;
00687                         changed++;
00688                     } else
00689                         fixed[mb_xy] = MV_UNCHANGED;
00690                 }
00691             }
00692 
00693             // printf(".%d/%d", changed, score_sum); fflush(stdout);
00694         }
00695 
00696         if (none_left)
00697             return;
00698 
00699         for (i = 0; i < s->mb_num; i++) {
00700             int mb_xy = s->mb_index2xy[i];
00701             if (fixed[mb_xy])
00702                 fixed[mb_xy] = MV_FROZEN;
00703         }
00704         // printf(":"); fflush(stdout);
00705     }
00706 }
00707 
00708 static int is_intra_more_likely(MpegEncContext *s)
00709 {
00710     int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
00711 
00712     if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
00713         return 1; // no previous frame available -> use spatial prediction
00714 
00715     undamaged_count = 0;
00716     for (i = 0; i < s->mb_num; i++) {
00717         const int mb_xy = s->mb_index2xy[i];
00718         const int error = s->error_status_table[mb_xy];
00719         if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
00720             undamaged_count++;
00721     }
00722 
00723     if (s->codec_id == CODEC_ID_H264) {
00724         H264Context *h = (void*) s;
00725         if (h->list_count <= 0 || h->ref_count[0] <= 0 ||
00726             !h->ref_list[0][0].f.data[0])
00727             return 1;
00728     }
00729 
00730     if (undamaged_count < 5)
00731         return 0; // almost all MBs damaged -> use temporal prediction
00732 
00733     // prevent dsp.sad() check, that requires access to the image
00734     if (CONFIG_MPEG_XVMC_DECODER    &&
00735         s->avctx->xvmc_acceleration &&
00736         s->pict_type == AV_PICTURE_TYPE_I)
00737         return 1;
00738 
00739     skip_amount     = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
00740     is_intra_likely = 0;
00741 
00742     j = 0;
00743     for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
00744         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00745             int error;
00746             const int mb_xy = mb_x + mb_y * s->mb_stride;
00747 
00748             error = s->error_status_table[mb_xy];
00749             if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
00750                 continue; // skip damaged
00751 
00752             j++;
00753             // skip a few to speed things up
00754             if ((j % skip_amount) != 0)
00755                 continue;
00756 
00757             if (s->pict_type == AV_PICTURE_TYPE_I) {
00758                 uint8_t *mb_ptr      = s->current_picture.f.data[0] +
00759                                        mb_x * 16 + mb_y * 16 * s->linesize;
00760                 uint8_t *last_mb_ptr = s->last_picture.f.data[0] +
00761                                        mb_x * 16 + mb_y * 16 * s->linesize;
00762 
00763                 if (s->avctx->codec_id == CODEC_ID_H264) {
00764                     // FIXME
00765                 } else {
00766                     ff_thread_await_progress((AVFrame *) s->last_picture_ptr,
00767                                              mb_y, 0);
00768                 }
00769                 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr,
00770                                                  s->linesize, 16);
00771                 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr,
00772                                                  last_mb_ptr + s->linesize * 16,
00773                                                  s->linesize, 16);
00774             } else {
00775                 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00776                    is_intra_likely++;
00777                 else
00778                    is_intra_likely--;
00779             }
00780         }
00781     }
00782     // printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
00783     return is_intra_likely > 0;
00784 }
00785 
00786 void ff_er_frame_start(MpegEncContext *s)
00787 {
00788     if (!s->err_recognition)
00789         return;
00790 
00791     memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
00792            s->mb_stride * s->mb_height * sizeof(uint8_t));
00793     s->error_count    = 3 * s->mb_num;
00794     s->error_occurred = 0;
00795 }
00796 
00804 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
00805                      int endx, int endy, int status)
00806 {
00807     const int start_i  = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
00808     const int end_i    = av_clip(endx   + endy   * s->mb_width, 0, s->mb_num);
00809     const int start_xy = s->mb_index2xy[start_i];
00810     const int end_xy   = s->mb_index2xy[end_i];
00811     int mask           = -1;
00812 
00813     if (s->avctx->hwaccel)
00814         return;
00815 
00816     if (start_i > end_i || start_xy > end_xy) {
00817         av_log(s->avctx, AV_LOG_ERROR,
00818                "internal error, slice end before start\n");
00819         return;
00820     }
00821 
00822     if (!s->err_recognition)
00823         return;
00824 
00825     mask &= ~VP_START;
00826     if (status & (ER_AC_ERROR | ER_AC_END)) {
00827         mask           &= ~(ER_AC_ERROR | ER_AC_END);
00828         s->error_count -= end_i - start_i + 1;
00829     }
00830     if (status & (ER_DC_ERROR | ER_DC_END)) {
00831         mask           &= ~(ER_DC_ERROR | ER_DC_END);
00832         s->error_count -= end_i - start_i + 1;
00833     }
00834     if (status & (ER_MV_ERROR | ER_MV_END)) {
00835         mask           &= ~(ER_MV_ERROR | ER_MV_END);
00836         s->error_count -= end_i - start_i + 1;
00837     }
00838 
00839     if (status & ER_MB_ERROR) {
00840         s->error_occurred = 1;
00841         s->error_count    = INT_MAX;
00842     }
00843 
00844     if (mask == ~0x7F) {
00845         memset(&s->error_status_table[start_xy], 0,
00846                (end_xy - start_xy) * sizeof(uint8_t));
00847     } else {
00848         int i;
00849         for (i = start_xy; i < end_xy; i++)
00850             s->error_status_table[i] &= mask;
00851     }
00852 
00853     if (end_i == s->mb_num)
00854         s->error_count = INT_MAX;
00855     else {
00856         s->error_status_table[end_xy] &= mask;
00857         s->error_status_table[end_xy] |= status;
00858     }
00859 
00860     s->error_status_table[start_xy] |= VP_START;
00861 
00862     if (start_xy > 0 && s->avctx->thread_count <= 1 &&
00863         s->avctx->skip_top * s->mb_width < start_i) {
00864         int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
00865 
00866         prev_status &= ~ VP_START;
00867         if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
00868             s->error_count = INT_MAX;
00869     }
00870 }
00871 
00872 void ff_er_frame_end(MpegEncContext *s)
00873 {
00874     int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
00875     int distance;
00876     int threshold_part[4] = { 100, 100, 100 };
00877     int threshold = 50;
00878     int is_intra_likely;
00879     int size = s->b8_stride * 2 * s->mb_height;
00880     Picture *pic = s->current_picture_ptr;
00881 
00882     /* We do not support ER of field pictures yet,
00883      * though it should not crash if enabled. */
00884     if (!s->err_recognition || s->error_count == 0 || s->avctx->lowres ||
00885         s->avctx->hwaccel                                              ||
00886         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU          ||
00887         s->picture_structure != PICT_FRAME                             ||
00888         s->error_count == 3 * s->mb_width *
00889                           (s->avctx->skip_top + s->avctx->skip_bottom)) {
00890         return;
00891     };
00892 
00893     if (s->current_picture.f.motion_val[0] == NULL) {
00894         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
00895 
00896         for (i = 0; i < 2; i++) {
00897             pic->f.ref_index[i]     = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
00898             pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
00899             pic->f.motion_val[i]    = pic->motion_val_base[i] + 4;
00900         }
00901         pic->f.motion_subsample_log2 = 3;
00902         s->current_picture = *s->current_picture_ptr;
00903     }
00904 
00905     if (s->avctx->debug & FF_DEBUG_ER) {
00906         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00907             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00908                 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
00909 
00910                 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
00911             }
00912             av_log(s->avctx, AV_LOG_DEBUG, "\n");
00913         }
00914     }
00915 
00916     /* handle overlapping slices */
00917     for (error_type = 1; error_type <= 3; error_type++) {
00918         int end_ok = 0;
00919 
00920         for (i = s->mb_num - 1; i >= 0; i--) {
00921             const int mb_xy = s->mb_index2xy[i];
00922             int error       = s->error_status_table[mb_xy];
00923 
00924             if (error & (1 << error_type))
00925                 end_ok = 1;
00926             if (error & (8 << error_type))
00927                 end_ok = 1;
00928 
00929             if (!end_ok)
00930                 s->error_status_table[mb_xy] |= 1 << error_type;
00931 
00932             if (error & VP_START)
00933                 end_ok = 0;
00934         }
00935     }
00936 
00937     /* handle slices with partitions of different length */
00938     if (s->partitioned_frame) {
00939         int end_ok = 0;
00940 
00941         for (i = s->mb_num - 1; i >= 0; i--) {
00942             const int mb_xy = s->mb_index2xy[i];
00943             int error       = s->error_status_table[mb_xy];
00944 
00945             if (error & ER_AC_END)
00946                 end_ok = 0;
00947             if ((error & ER_MV_END) ||
00948                 (error & ER_DC_END) ||
00949                 (error & ER_AC_ERROR))
00950                 end_ok = 1;
00951 
00952             if (!end_ok)
00953                 s->error_status_table[mb_xy]|= ER_AC_ERROR;
00954 
00955             if (error & VP_START)
00956                 end_ok = 0;
00957         }
00958     }
00959 
00960     /* handle missing slices */
00961     if (s->err_recognition & AV_EF_EXPLODE) {
00962         int end_ok = 1;
00963 
00964         // FIXME + 100 hack
00965         for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
00966             const int mb_xy = s->mb_index2xy[i];
00967             int error1 = s->error_status_table[mb_xy];
00968             int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
00969 
00970             if (error1 & VP_START)
00971                 end_ok = 1;
00972 
00973             if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
00974                 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
00975                 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
00976                 (error1 & ER_MV_END))) {
00977                 // end & uninit
00978                 end_ok = 0;
00979             }
00980 
00981             if (!end_ok)
00982                 s->error_status_table[mb_xy] |= ER_MB_ERROR;
00983         }
00984     }
00985 
00986     /* backward mark errors */
00987     distance = 9999999;
00988     for (error_type = 1; error_type <= 3; error_type++) {
00989         for (i = s->mb_num - 1; i >= 0; i--) {
00990             const int mb_xy = s->mb_index2xy[i];
00991             int       error = s->error_status_table[mb_xy];
00992 
00993             if (!s->mbskip_table[mb_xy]) // FIXME partition specific
00994                 distance++;
00995             if (error & (1 << error_type))
00996                 distance = 0;
00997 
00998             if (s->partitioned_frame) {
00999                 if (distance < threshold_part[error_type - 1])
01000                     s->error_status_table[mb_xy] |= 1 << error_type;
01001             } else {
01002                 if (distance < threshold)
01003                     s->error_status_table[mb_xy] |= 1 << error_type;
01004             }
01005 
01006             if (error & VP_START)
01007                 distance = 9999999;
01008         }
01009     }
01010 
01011     /* forward mark errors */
01012     error = 0;
01013     for (i = 0; i < s->mb_num; i++) {
01014         const int mb_xy = s->mb_index2xy[i];
01015         int old_error   = s->error_status_table[mb_xy];
01016 
01017         if (old_error & VP_START) {
01018             error = old_error & ER_MB_ERROR;
01019         } else {
01020             error |= old_error & ER_MB_ERROR;
01021             s->error_status_table[mb_xy] |= error;
01022         }
01023     }
01024 
01025     /* handle not partitioned case */
01026     if (!s->partitioned_frame) {
01027         for (i = 0; i < s->mb_num; i++) {
01028             const int mb_xy = s->mb_index2xy[i];
01029             error = s->error_status_table[mb_xy];
01030             if (error & ER_MB_ERROR)
01031                 error |= ER_MB_ERROR;
01032             s->error_status_table[mb_xy] = error;
01033         }
01034     }
01035 
01036     dc_error = ac_error = mv_error = 0;
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_DC_ERROR)
01041             dc_error++;
01042         if (error & ER_AC_ERROR)
01043             ac_error++;
01044         if (error & ER_MV_ERROR)
01045             mv_error++;
01046     }
01047     av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
01048            dc_error, ac_error, mv_error);
01049 
01050     is_intra_likely = is_intra_more_likely(s);
01051 
01052     /* set unknown mb-type to most likely */
01053     for (i = 0; i < s->mb_num; i++) {
01054         const int mb_xy = s->mb_index2xy[i];
01055         error = s->error_status_table[mb_xy];
01056         if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
01057             continue;
01058 
01059         if (is_intra_likely)
01060             s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
01061         else
01062             s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
01063     }
01064 
01065     // change inter to intra blocks if no reference frames are available
01066     if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
01067         for (i = 0; i < s->mb_num; i++) {
01068             const int mb_xy = s->mb_index2xy[i];
01069             if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
01070                 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
01071         }
01072 
01073     /* handle inter blocks with damaged AC */
01074     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01075         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01076             const int mb_xy   = mb_x + mb_y * s->mb_stride;
01077             const int mb_type = s->current_picture.f.mb_type[mb_xy];
01078             int dir           = !s->last_picture.f.data[0];
01079 
01080             error = s->error_status_table[mb_xy];
01081 
01082             if (IS_INTRA(mb_type))
01083                 continue; // intra
01084             if (error & ER_MV_ERROR)
01085                 continue; // inter with damaged MV
01086             if (!(error & ER_AC_ERROR))
01087                 continue; // undamaged inter
01088 
01089             s->mv_dir     = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
01090             s->mb_intra   = 0;
01091             s->mb_skipped = 0;
01092             if (IS_8X8(mb_type)) {
01093                 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
01094                 int j;
01095                 s->mv_type = MV_TYPE_8X8;
01096                 for (j = 0; j < 4; j++) {
01097                     s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
01098                     s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
01099                 }
01100             } else {
01101                 s->mv_type     = MV_TYPE_16X16;
01102                 s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
01103                 s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
01104             }
01105 
01106             s->dsp.clear_blocks(s->block[0]);
01107 
01108             s->mb_x = mb_x;
01109             s->mb_y = mb_y;
01110             decode_mb(s, 0 /* FIXME h264 partitioned slices need this set */);
01111         }
01112     }
01113 
01114     /* guess MVs */
01115     if (s->pict_type == AV_PICTURE_TYPE_B) {
01116         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01117             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01118                 int       xy      = mb_x * 2 + mb_y * 2 * s->b8_stride;
01119                 const int mb_xy   = mb_x + mb_y * s->mb_stride;
01120                 const int mb_type = s->current_picture.f.mb_type[mb_xy];
01121 
01122                 error = s->error_status_table[mb_xy];
01123 
01124                 if (IS_INTRA(mb_type))
01125                     continue;
01126                 if (!(error & ER_MV_ERROR))
01127                     continue; // inter with undamaged MV
01128                 if (!(error & ER_AC_ERROR))
01129                     continue; // undamaged inter
01130 
01131                 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
01132                 if (!s->last_picture.f.data[0])
01133                     s->mv_dir &= ~MV_DIR_FORWARD;
01134                 if (!s->next_picture.f.data[0])
01135                     s->mv_dir &= ~MV_DIR_BACKWARD;
01136                 s->mb_intra   = 0;
01137                 s->mv_type    = MV_TYPE_16X16;
01138                 s->mb_skipped = 0;
01139 
01140                 if (s->pp_time) {
01141                     int time_pp = s->pp_time;
01142                     int time_pb = s->pb_time;
01143 
01144                     if (s->avctx->codec_id == CODEC_ID_H264) {
01145                         // FIXME
01146                     } else {
01147                         ff_thread_await_progress((AVFrame *) s->next_picture_ptr, mb_y, 0);
01148                     }
01149                     s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] *  time_pb            / time_pp;
01150                     s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] *  time_pb            / time_pp;
01151                     s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
01152                     s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
01153                 } else {
01154                     s->mv[0][0][0] = 0;
01155                     s->mv[0][0][1] = 0;
01156                     s->mv[1][0][0] = 0;
01157                     s->mv[1][0][1] = 0;
01158                 }
01159 
01160                 s->dsp.clear_blocks(s->block[0]);
01161                 s->mb_x = mb_x;
01162                 s->mb_y = mb_y;
01163                 decode_mb(s, 0);
01164             }
01165         }
01166     } else
01167         guess_mv(s);
01168 
01169     /* the filters below are not XvMC compatible, skip them */
01170     if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01171         goto ec_clean;
01172     /* fill DC for inter blocks */
01173     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01174         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01175             int dc, dcu, dcv, y, n;
01176             int16_t *dc_ptr;
01177             uint8_t *dest_y, *dest_cb, *dest_cr;
01178             const int mb_xy   = mb_x + mb_y * s->mb_stride;
01179             const int mb_type = s->current_picture.f.mb_type[mb_xy];
01180 
01181             error = s->error_status_table[mb_xy];
01182 
01183             if (IS_INTRA(mb_type) && s->partitioned_frame)
01184                 continue;
01185             // if (error & ER_MV_ERROR)
01186             //     continue; // inter data damaged FIXME is this good?
01187 
01188             dest_y  = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
01189             dest_cb = s->current_picture.f.data[1] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01190             dest_cr = s->current_picture.f.data[2] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01191 
01192             dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
01193             for (n = 0; n < 4; n++) {
01194                 dc = 0;
01195                 for (y = 0; y < 8; y++) {
01196                     int x;
01197                     for (x = 0; x < 8; x++)
01198                        dc += dest_y[x + (n & 1) * 8 +
01199                              (y + (n >> 1) * 8) * s->linesize];
01200                 }
01201                 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
01202             }
01203 
01204             dcu = dcv = 0;
01205             for (y = 0; y < 8; y++) {
01206                 int x;
01207                 for (x = 0; x < 8; x++) {
01208                     dcu += dest_cb[x + y * s->uvlinesize];
01209                     dcv += dest_cr[x + y * s->uvlinesize];
01210                 }
01211             }
01212             s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
01213             s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
01214         }
01215     }
01216 
01217     /* guess DC for damaged blocks */
01218     guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);
01219     guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);
01220     guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);
01221 
01222     /* filter luma DC */
01223     filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
01224 
01225     /* render DC only intra */
01226     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01227         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01228             uint8_t *dest_y, *dest_cb, *dest_cr;
01229             const int mb_xy   = mb_x + mb_y * s->mb_stride;
01230             const int mb_type = s->current_picture.f.mb_type[mb_xy];
01231 
01232             error = s->error_status_table[mb_xy];
01233 
01234             if (IS_INTER(mb_type))
01235                 continue;
01236             if (!(error & ER_AC_ERROR))
01237                 continue; // undamaged
01238 
01239             dest_y  = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
01240             dest_cb = s->current_picture.f.data[1] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01241             dest_cr = s->current_picture.f.data[2] + mb_x *  8 + mb_y *  8 * s->uvlinesize;
01242 
01243             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
01244         }
01245     }
01246 
01247     if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
01248         /* filter horizontal block boundaries */
01249         h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
01250                        s->mb_height * 2, s->linesize, 1);
01251         h_block_filter(s, s->current_picture.f.data[1], s->mb_width,
01252                        s->mb_height  , s->uvlinesize, 0);
01253         h_block_filter(s, s->current_picture.f.data[2], s->mb_width,
01254                        s->mb_height  , s->uvlinesize, 0);
01255 
01256         /* filter vertical block boundaries */
01257         v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
01258                        s->mb_height * 2, s->linesize, 1);
01259         v_block_filter(s, s->current_picture.f.data[1], s->mb_width,
01260                        s->mb_height  , s->uvlinesize, 0);
01261         v_block_filter(s, s->current_picture.f.data[2], s->mb_width,
01262                        s->mb_height  , s->uvlinesize, 0);
01263     }
01264 
01265 ec_clean:
01266     /* clean a few tables */
01267     for (i = 0; i < s->mb_num; i++) {
01268         const int mb_xy = s->mb_index2xy[i];
01269         int       error = s->error_status_table[mb_xy];
01270 
01271         if (s->pict_type != AV_PICTURE_TYPE_B &&
01272             (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
01273             s->mbskip_table[mb_xy] = 0;
01274         }
01275         s->mbintra_table[mb_xy] = 1;
01276     }
01277 }
Generated on Sat Mar 17 2012 12:57:44 for Libav by doxygen 1.7.1