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