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 
00023 /**
00024  * @file error_resilience.c
00025  * Error resilience / concealment.
00026  */
00027 
00028 #include <limits.h>
00029 
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 
00034 static void decode_mb(MpegEncContext *s){
00035     s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize  ) + s->mb_x * 16;
00036     s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * s->uvlinesize) + s->mb_x * 8;
00037     s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * s->uvlinesize) + s->mb_x * 8;
00038 
00039     MPV_decode_mb(s, s->block);
00040 }
00041 
00042 /**
00043  * replaces the current MB with a flat dc only version.
00044  */
00045 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
00046 {
00047     int dc, dcu, dcv, y, i;
00048     for(i=0; i<4; i++){
00049         dc= s->dc_val[0][mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*s->b8_stride];
00050         if(dc<0) dc=0;
00051         else if(dc>2040) dc=2040;
00052         for(y=0; y<8; y++){
00053             int x;
00054             for(x=0; x<8; x++){
00055                 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
00056             }
00057         }
00058     }
00059     dcu = s->dc_val[1][mb_x + mb_y*s->mb_stride];
00060     dcv = s->dc_val[2][mb_x + mb_y*s->mb_stride];
00061     if     (dcu<0   ) dcu=0;
00062     else if(dcu>2040) dcu=2040;
00063     if     (dcv<0   ) dcv=0;
00064     else if(dcv>2040) dcv=2040;
00065     for(y=0; y<8; y++){
00066         int x;
00067         for(x=0; x<8; x++){
00068             dest_cb[x + y*(s->uvlinesize)]= dcu/8;
00069             dest_cr[x + y*(s->uvlinesize)]= dcv/8;
00070         }
00071     }
00072 }
00073 
00074 static void filter181(int16_t *data, int width, int height, int stride){
00075     int x,y;
00076 
00077     /* horizontal filter */
00078     for(y=1; y<height-1; y++){
00079         int prev_dc= data[0 + y*stride];
00080 
00081         for(x=1; x<width-1; x++){
00082             int dc;
00083 
00084             dc= - prev_dc
00085                 + data[x     + y*stride]*8
00086                 - data[x + 1 + y*stride];
00087             dc= (dc*10923 + 32768)>>16;
00088             prev_dc= data[x + y*stride];
00089             data[x + y*stride]= dc;
00090         }
00091     }
00092 
00093     /* vertical filter */
00094     for(x=1; x<width-1; x++){
00095         int prev_dc= data[x];
00096 
00097         for(y=1; y<height-1; y++){
00098             int dc;
00099 
00100             dc= - prev_dc
00101                 + data[x +  y   *stride]*8
00102                 - data[x + (y+1)*stride];
00103             dc= (dc*10923 + 32768)>>16;
00104             prev_dc= data[x + y*stride];
00105             data[x + y*stride]= dc;
00106         }
00107     }
00108 }
00109 
00110 /**
00111  * guess the dc of blocks which do not have an undamaged dc
00112  * @param w     width in 8 pixel blocks
00113  * @param h     height in 8 pixel blocks
00114  */
00115 static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
00116     int b_x, b_y;
00117 
00118     for(b_y=0; b_y<h; b_y++){
00119         for(b_x=0; b_x<w; b_x++){
00120             int color[4]={1024,1024,1024,1024};
00121             int distance[4]={9999,9999,9999,9999};
00122             int mb_index, error, j;
00123             int64_t guess, weight_sum;
00124 
00125             mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00126 
00127             error= s->error_status_table[mb_index];
00128 
00129             if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
00130             if(!(error&DC_ERROR)) continue;           //dc-ok
00131 
00132             /* right block */
00133             for(j=b_x+1; j<w; j++){
00134                 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00135                 int error_j= s->error_status_table[mb_index_j];
00136                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
00137                 if(intra_j==0 || !(error_j&DC_ERROR)){
00138                     color[0]= dc[j + b_y*stride];
00139                     distance[0]= j-b_x;
00140                     break;
00141                 }
00142             }
00143 
00144             /* left block */
00145             for(j=b_x-1; j>=0; j--){
00146                 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00147                 int error_j= s->error_status_table[mb_index_j];
00148                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
00149                 if(intra_j==0 || !(error_j&DC_ERROR)){
00150                     color[1]= dc[j + b_y*stride];
00151                     distance[1]= b_x-j;
00152                     break;
00153                 }
00154             }
00155 
00156             /* bottom block */
00157             for(j=b_y+1; j<h; j++){
00158                 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
00159                 int error_j= s->error_status_table[mb_index_j];
00160                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
00161                 if(intra_j==0 || !(error_j&DC_ERROR)){
00162                     color[2]= dc[b_x + j*stride];
00163                     distance[2]= j-b_y;
00164                     break;
00165                 }
00166             }
00167 
00168             /* top block */
00169             for(j=b_y-1; j>=0; j--){
00170                 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
00171                 int error_j= s->error_status_table[mb_index_j];
00172                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
00173                 if(intra_j==0 || !(error_j&DC_ERROR)){
00174                     color[3]= dc[b_x + j*stride];
00175                     distance[3]= b_y-j;
00176                     break;
00177                 }
00178             }
00179 
00180             weight_sum=0;
00181             guess=0;
00182             for(j=0; j<4; j++){
00183                 int64_t weight= 256*256*256*16/distance[j];
00184                 guess+= weight*(int64_t)color[j];
00185                 weight_sum+= weight;
00186             }
00187             guess= (guess + weight_sum/2) / weight_sum;
00188 
00189             dc[b_x + b_y*stride]= guess;
00190         }
00191     }
00192 }
00193 
00194 /**
00195  * simple horizontal deblocking filter used for error resilience
00196  * @param w     width in 8 pixel blocks
00197  * @param h     height in 8 pixel blocks
00198  */
00199 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
00200     int b_x, b_y;
00201     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00202 
00203     for(b_y=0; b_y<h; b_y++){
00204         for(b_x=0; b_x<w-1; b_x++){
00205             int y;
00206             int left_status = s->error_status_table[( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride];
00207             int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
00208             int left_intra=   IS_INTRA(s->current_picture.mb_type      [( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
00209             int right_intra=  IS_INTRA(s->current_picture.mb_type      [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
00210             int left_damage =  left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
00211             int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
00212             int offset= b_x*8 + b_y*stride*8;
00213             int16_t *left_mv=  s->current_picture.motion_val[0][s->b8_stride*(b_y<<(1-is_luma)) + ( b_x   <<(1-is_luma))];
00214             int16_t *right_mv= s->current_picture.motion_val[0][s->b8_stride*(b_y<<(1-is_luma)) + ((b_x+1)<<(1-is_luma))];
00215 
00216             if(!(left_damage||right_damage)) continue; // both undamaged
00217 
00218             if(   (!left_intra) && (!right_intra)
00219                && FFABS(left_mv[0]-right_mv[0]) + FFABS(left_mv[1]+right_mv[1]) < 2) continue;
00220 
00221             for(y=0; y<8; y++){
00222                 int a,b,c,d;
00223 
00224                 a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
00225                 b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
00226                 c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
00227 
00228                 d= FFABS(b) - ((FFABS(a) + FFABS(c) + 1)>>1);
00229                 d= FFMAX(d, 0);
00230                 if(b<0) d= -d;
00231 
00232                 if(d==0) continue;
00233 
00234                 if(!(left_damage && right_damage))
00235                     d= d*16/9;
00236 
00237                 if(left_damage){
00238                     dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
00239                     dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
00240                     dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
00241                     dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
00242                 }
00243                 if(right_damage){
00244                     dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
00245                     dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
00246                     dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
00247                     dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
00248                 }
00249             }
00250         }
00251     }
00252 }
00253 
00254 /**
00255  * simple vertical deblocking filter used for error resilience
00256  * @param w     width in 8 pixel blocks
00257  * @param h     height in 8 pixel blocks
00258  */
00259 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
00260     int b_x, b_y;
00261     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00262 
00263     for(b_y=0; b_y<h-1; b_y++){
00264         for(b_x=0; b_x<w; b_x++){
00265             int x;
00266             int top_status   = s->error_status_table[(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride];
00267             int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
00268             int top_intra=     IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride]);
00269             int bottom_intra=  IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
00270             int top_damage =      top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
00271             int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
00272             int offset= b_x*8 + b_y*stride*8;
00273             int16_t *top_mv=    s->current_picture.motion_val[0][s->b8_stride*( b_y   <<(1-is_luma)) + (b_x<<(1-is_luma))];
00274             int16_t *bottom_mv= s->current_picture.motion_val[0][s->b8_stride*((b_y+1)<<(1-is_luma)) + (b_x<<(1-is_luma))];
00275 
00276             if(!(top_damage||bottom_damage)) continue; // both undamaged
00277 
00278             if(   (!top_intra) && (!bottom_intra)
00279                && FFABS(top_mv[0]-bottom_mv[0]) + FFABS(top_mv[1]+bottom_mv[1]) < 2) continue;
00280 
00281             for(x=0; x<8; x++){
00282                 int a,b,c,d;
00283 
00284                 a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
00285                 b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
00286                 c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
00287 
00288                 d= FFABS(b) - ((FFABS(a) + FFABS(c)+1)>>1);
00289                 d= FFMAX(d, 0);
00290                 if(b<0) d= -d;
00291 
00292                 if(d==0) continue;
00293 
00294                 if(!(top_damage && bottom_damage))
00295                     d= d*16/9;
00296 
00297                 if(top_damage){
00298                     dst[offset + x +  7*stride] = cm[dst[offset + x +  7*stride] + ((d*7)>>4)];
00299                     dst[offset + x +  6*stride] = cm[dst[offset + x +  6*stride] + ((d*5)>>4)];
00300                     dst[offset + x +  5*stride] = cm[dst[offset + x +  5*stride] + ((d*3)>>4)];
00301                     dst[offset + x +  4*stride] = cm[dst[offset + x +  4*stride] + ((d*1)>>4)];
00302                 }
00303                 if(bottom_damage){
00304                     dst[offset + x +  8*stride] = cm[dst[offset + x +  8*stride] - ((d*7)>>4)];
00305                     dst[offset + x +  9*stride] = cm[dst[offset + x +  9*stride] - ((d*5)>>4)];
00306                     dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
00307                     dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
00308                 }
00309             }
00310         }
00311     }
00312 }
00313 
00314 static void guess_mv(MpegEncContext *s){
00315     uint8_t fixed[s->mb_stride * s->mb_height];
00316 #define MV_FROZEN    3
00317 #define MV_CHANGED   2
00318 #define MV_UNCHANGED 1
00319     const int mb_stride = s->mb_stride;
00320     const int mb_width = s->mb_width;
00321     const int mb_height= s->mb_height;
00322     int i, depth, num_avail;
00323     int mb_x, mb_y;
00324 
00325     num_avail=0;
00326     for(i=0; i<s->mb_num; i++){
00327         const int mb_xy= s->mb_index2xy[ i ];
00328         int f=0;
00329         int error= s->error_status_table[mb_xy];
00330 
00331         if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
00332         if(!(error&MV_ERROR)) f=MV_FROZEN;           //inter with undamaged MV
00333 
00334         fixed[mb_xy]= f;
00335         if(f==MV_FROZEN)
00336             num_avail++;
00337     }
00338 
00339     if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
00340         for(mb_y=0; mb_y<s->mb_height; mb_y++){
00341             for(mb_x=0; mb_x<s->mb_width; mb_x++){
00342                 const int mb_xy= mb_x + mb_y*s->mb_stride;
00343 
00344                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))  continue;
00345                 if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
00346 
00347                 s->mv_dir = MV_DIR_FORWARD;
00348                 s->mb_intra=0;
00349                 s->mv_type = MV_TYPE_16X16;
00350                 s->mb_skipped=0;
00351 
00352                 s->dsp.clear_blocks(s->block[0]);
00353 
00354                 s->mb_x= mb_x;
00355                 s->mb_y= mb_y;
00356                 s->mv[0][0][0]= 0;
00357                 s->mv[0][0][1]= 0;
00358                 decode_mb(s);
00359             }
00360         }
00361         return;
00362     }
00363 
00364     for(depth=0;; depth++){
00365         int changed, pass, none_left;
00366 
00367         none_left=1;
00368         changed=1;
00369         for(pass=0; (changed || pass<2) && pass<10; pass++){
00370             int mb_x, mb_y;
00371 int score_sum=0;
00372 
00373             changed=0;
00374             for(mb_y=0; mb_y<s->mb_height; mb_y++){
00375                 for(mb_x=0; mb_x<s->mb_width; mb_x++){
00376                     const int mb_xy= mb_x + mb_y*s->mb_stride;
00377                     int mv_predictor[8][2]={{0}};
00378                     int pred_count=0;
00379                     int j;
00380                     int best_score=256*256*256*64;
00381                     int best_pred=0;
00382                     const int mot_stride= s->b8_stride;
00383                     const int mot_index= mb_x*2 + mb_y*2*mot_stride;
00384                     int prev_x= s->current_picture.motion_val[0][mot_index][0];
00385                     int prev_y= s->current_picture.motion_val[0][mot_index][1];
00386 
00387                     if((mb_x^mb_y^pass)&1) continue;
00388 
00389                     if(fixed[mb_xy]==MV_FROZEN) continue;
00390                     assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
00391                     assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
00392 
00393                     j=0;
00394                     if(mb_x>0           && fixed[mb_xy-1        ]==MV_FROZEN) j=1;
00395                     if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_FROZEN) j=1;
00396                     if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
00397                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
00398                     if(j==0) continue;
00399 
00400                     j=0;
00401                     if(mb_x>0           && fixed[mb_xy-1        ]==MV_CHANGED) j=1;
00402                     if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_CHANGED) j=1;
00403                     if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
00404                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
00405                     if(j==0 && pass>1) continue;
00406 
00407                     none_left=0;
00408 
00409                     if(mb_x>0 && fixed[mb_xy-1]){
00410                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - 2][0];
00411                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - 2][1];
00412                         pred_count++;
00413                     }
00414                     if(mb_x+1<mb_width && fixed[mb_xy+1]){
00415                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + 2][0];
00416                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + 2][1];
00417                         pred_count++;
00418                     }
00419                     if(mb_y>0 && fixed[mb_xy-mb_stride]){
00420                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*2][0];
00421                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*2][1];
00422                         pred_count++;
00423                     }
00424                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
00425                         mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*2][0];
00426                         mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*2][1];
00427                         pred_count++;
00428                     }
00429                     if(pred_count==0) continue;
00430 
00431                     if(pred_count>1){
00432                         int sum_x=0, sum_y=0;
00433                         int max_x, max_y, min_x, min_y;
00434 
00435                         for(j=0; j<pred_count; j++){
00436                             sum_x+= mv_predictor[j][0];
00437                             sum_y+= mv_predictor[j][1];
00438                         }
00439 
00440                         /* mean */
00441                         mv_predictor[pred_count][0] = sum_x/j;
00442                         mv_predictor[pred_count][1] = sum_y/j;
00443 
00444                         /* median */
00445                         if(pred_count>=3){
00446                             min_y= min_x= 99999;
00447                             max_y= max_x=-99999;
00448                         }else{
00449                             min_x=min_y=max_x=max_y=0;
00450                         }
00451                         for(j=0; j<pred_count; j++){
00452                             max_x= FFMAX(max_x, mv_predictor[j][0]);
00453                             max_y= FFMAX(max_y, mv_predictor[j][1]);
00454                             min_x= FFMIN(min_x, mv_predictor[j][0]);
00455                             min_y= FFMIN(min_y, mv_predictor[j][1]);
00456                         }
00457                         mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
00458                         mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
00459 
00460                         if(pred_count==4){
00461                             mv_predictor[pred_count+1][0] /= 2;
00462                             mv_predictor[pred_count+1][1] /= 2;
00463                         }
00464                         pred_count+=2;
00465                     }
00466 
00467                     /* zero MV */
00468                     pred_count++;
00469 
00470                     /* last MV */
00471                     mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];
00472                     mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];
00473                     pred_count++;
00474 
00475                     s->mv_dir = MV_DIR_FORWARD;
00476                     s->mb_intra=0;
00477                     s->mv_type = MV_TYPE_16X16;
00478                     s->mb_skipped=0;
00479 
00480                     s->dsp.clear_blocks(s->block[0]);
00481 
00482                     s->mb_x= mb_x;
00483                     s->mb_y= mb_y;
00484 
00485                     for(j=0; j<pred_count; j++){
00486                         int score=0;
00487                         uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
00488 
00489                         s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
00490                         s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
00491 
00492                         decode_mb(s);
00493 
00494                         if(mb_x>0 && fixed[mb_xy-1]){
00495                             int k;
00496                             for(k=0; k<16; k++)
00497                                 score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize   ]);
00498                         }
00499                         if(mb_x+1<mb_width && fixed[mb_xy+1]){
00500                             int k;
00501                             for(k=0; k<16; k++)
00502                                 score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
00503                         }
00504                         if(mb_y>0 && fixed[mb_xy-mb_stride]){
00505                             int k;
00506                             for(k=0; k<16; k++)
00507                                 score += FFABS(src[k-s->linesize   ]-src[k               ]);
00508                         }
00509                         if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
00510                             int k;
00511                             for(k=0; k<16; k++)
00512                                 score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
00513                         }
00514 
00515                         if(score <= best_score){ // <= will favor the last MV
00516                             best_score= score;
00517                             best_pred= j;
00518                         }
00519                     }
00520 score_sum+= best_score;
00521 //FIXME no need to set s->current_picture.motion_val[0][mot_index][0] explicit
00522                     s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
00523                     s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
00524 
00525                     decode_mb(s);
00526 
00527 
00528                     if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
00529                         fixed[mb_xy]=MV_CHANGED;
00530                         changed++;
00531                     }else
00532                         fixed[mb_xy]=MV_UNCHANGED;
00533                 }
00534             }
00535 
00536 //            printf(".%d/%d", changed, score_sum); fflush(stdout);
00537         }
00538 
00539         if(none_left)
00540             return;
00541 
00542         for(i=0; i<s->mb_num; i++){
00543             int mb_xy= s->mb_index2xy[i];
00544             if(fixed[mb_xy])
00545                 fixed[mb_xy]=MV_FROZEN;
00546         }
00547 //        printf(":"); fflush(stdout);
00548     }
00549 }
00550 
00551 static int is_intra_more_likely(MpegEncContext *s){
00552     int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
00553 
00554     if(s->last_picture_ptr==NULL) return 1; //no previous frame available -> use spatial prediction
00555 
00556     undamaged_count=0;
00557     for(i=0; i<s->mb_num; i++){
00558         const int mb_xy= s->mb_index2xy[i];
00559         const int error= s->error_status_table[mb_xy];
00560         if(!((error&DC_ERROR) && (error&MV_ERROR)))
00561             undamaged_count++;
00562     }
00563 
00564     if(undamaged_count < 5) return 0; //almost all MBs damaged -> use temporal prediction
00565 
00566 #ifdef HAVE_XVMC
00567     //prevent dsp.sad() check, that requires access to the image
00568     if(s->avctx->xvmc_acceleration && s->pict_type==FF_I_TYPE) return 1;
00569 #endif
00570 
00571     skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
00572     is_intra_likely=0;
00573 
00574     j=0;
00575     for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
00576         for(mb_x= 0; mb_x<s->mb_width; mb_x++){
00577             int error;
00578             const int mb_xy= mb_x + mb_y*s->mb_stride;
00579 
00580             error= s->error_status_table[mb_xy];
00581             if((error&DC_ERROR) && (error&MV_ERROR))
00582                 continue; //skip damaged
00583 
00584             j++;
00585             if((j%skip_amount) != 0) continue; //skip a few to speed things up
00586 
00587             if(s->pict_type==FF_I_TYPE){
00588                 uint8_t *mb_ptr     = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
00589                 uint8_t *last_mb_ptr= s->last_picture.data   [0] + mb_x*16 + mb_y*16*s->linesize;
00590 
00591                 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr                    , s->linesize, 16);
00592                 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
00593             }else{
00594                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
00595                    is_intra_likely++;
00596                 else
00597                    is_intra_likely--;
00598             }
00599         }
00600     }
00601 //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
00602     return is_intra_likely > 0;
00603 }
00604 
00605 void ff_er_frame_start(MpegEncContext *s){
00606     if(!s->error_resilience) return;
00607 
00608     memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_stride*s->mb_height*sizeof(uint8_t));
00609     s->error_count= 3*s->mb_num;
00610 }
00611 
00612 /**
00613  * adds a slice.
00614  * @param endx x component of the last macroblock, can be -1 for the last of the previous line
00615  * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
00616  *               error of the same type occurred
00617  */
00618 void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
00619     const int start_i= av_clip(startx + starty * s->mb_width    , 0, s->mb_num-1);
00620     const int end_i  = av_clip(endx   + endy   * s->mb_width    , 0, s->mb_num);
00621     const int start_xy= s->mb_index2xy[start_i];
00622     const int end_xy  = s->mb_index2xy[end_i];
00623     int mask= -1;
00624 
00625     if(start_i > end_i || start_xy > end_xy){
00626         av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before start\n");
00627         return;
00628     }
00629 
00630     if(!s->error_resilience) return;
00631 
00632     mask &= ~VP_START;
00633     if(status & (AC_ERROR|AC_END)){
00634         mask &= ~(AC_ERROR|AC_END);
00635         s->error_count -= end_i - start_i + 1;
00636     }
00637     if(status & (DC_ERROR|DC_END)){
00638         mask &= ~(DC_ERROR|DC_END);
00639         s->error_count -= end_i - start_i + 1;
00640     }
00641     if(status & (MV_ERROR|MV_END)){
00642         mask &= ~(MV_ERROR|MV_END);
00643         s->error_count -= end_i - start_i + 1;
00644     }
00645 
00646     if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
00647 
00648     if(mask == ~0x7F){
00649         memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
00650     }else{
00651         int i;
00652         for(i=start_xy; i<end_xy; i++){
00653             s->error_status_table[ i ] &= mask;
00654         }
00655     }
00656 
00657     if(end_i == s->mb_num)
00658         s->error_count= INT_MAX;
00659     else{
00660         s->error_status_table[end_xy] &= mask;
00661         s->error_status_table[end_xy] |= status;
00662     }
00663 
00664     s->error_status_table[start_xy] |= VP_START;
00665 
00666     if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
00667         int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
00668 
00669         prev_status &= ~ VP_START;
00670         if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
00671     }
00672 }
00673 
00674 void ff_er_frame_end(MpegEncContext *s){
00675     int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
00676     int distance;
00677     int threshold_part[4]= {100,100,100};
00678     int threshold= 50;
00679     int is_intra_likely;
00680     int size = s->b8_stride * 2 * s->mb_height;
00681     Picture *pic= s->current_picture_ptr;
00682 
00683     if(!s->error_resilience || s->error_count==0 ||
00684        s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
00685 
00686     if(s->current_picture.motion_val[0] == NULL){
00687         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
00688 
00689         for(i=0; i<2; i++){
00690             pic->ref_index[i]= av_mallocz(size * sizeof(uint8_t));
00691             pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
00692             pic->motion_val[i]= pic->motion_val_base[i]+4;
00693         }
00694         pic->motion_subsample_log2= 3;
00695         s->current_picture= *s->current_picture_ptr;
00696     }
00697 
00698     for(i=0; i<2; i++){
00699         if(pic->ref_index[i])
00700             memset(pic->ref_index[i], 0, size * sizeof(uint8_t));
00701     }
00702 
00703     if(s->avctx->debug&FF_DEBUG_ER){
00704         for(mb_y=0; mb_y<s->mb_height; mb_y++){
00705             for(mb_x=0; mb_x<s->mb_width; mb_x++){
00706                 int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
00707 
00708                 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
00709             }
00710             av_log(s->avctx, AV_LOG_DEBUG, "\n");
00711         }
00712     }
00713 
00714 #if 1
00715     /* handle overlapping slices */
00716     for(error_type=1; error_type<=3; error_type++){
00717         int end_ok=0;
00718 
00719         for(i=s->mb_num-1; i>=0; i--){
00720             const int mb_xy= s->mb_index2xy[i];
00721             int error= s->error_status_table[mb_xy];
00722 
00723             if(error&(1<<error_type))
00724                 end_ok=1;
00725             if(error&(8<<error_type))
00726                 end_ok=1;
00727 
00728             if(!end_ok)
00729                 s->error_status_table[mb_xy]|= 1<<error_type;
00730 
00731             if(error&VP_START)
00732                 end_ok=0;
00733         }
00734     }
00735 #endif
00736 #if 1
00737     /* handle slices with partitions of different length */
00738     if(s->partitioned_frame){
00739         int end_ok=0;
00740 
00741         for(i=s->mb_num-1; i>=0; i--){
00742             const int mb_xy= s->mb_index2xy[i];
00743             int error= s->error_status_table[mb_xy];
00744 
00745             if(error&AC_END)
00746                 end_ok=0;
00747             if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
00748                 end_ok=1;
00749 
00750             if(!end_ok)
00751                 s->error_status_table[mb_xy]|= AC_ERROR;
00752 
00753             if(error&VP_START)
00754                 end_ok=0;
00755         }
00756     }
00757 #endif
00758     /* handle missing slices */
00759     if(s->error_resilience>=4){
00760         int end_ok=1;
00761 
00762         for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
00763             const int mb_xy= s->mb_index2xy[i];
00764             int error1= s->error_status_table[mb_xy  ];
00765             int error2= s->error_status_table[s->mb_index2xy[i+1]];
00766 
00767             if(error1&VP_START)
00768                 end_ok=1;
00769 
00770             if(   error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
00771                && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
00772                && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
00773                 end_ok=0;
00774             }
00775 
00776             if(!end_ok)
00777                 s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
00778         }
00779     }
00780 
00781 #if 1
00782     /* backward mark errors */
00783     distance=9999999;
00784     for(error_type=1; error_type<=3; error_type++){
00785         for(i=s->mb_num-1; i>=0; i--){
00786             const int mb_xy= s->mb_index2xy[i];
00787             int error= s->error_status_table[mb_xy];
00788 
00789             if(!s->mbskip_table[mb_xy]) //FIXME partition specific
00790                 distance++;
00791             if(error&(1<<error_type))
00792                 distance= 0;
00793 
00794             if(s->partitioned_frame){
00795                 if(distance < threshold_part[error_type-1])
00796                     s->error_status_table[mb_xy]|= 1<<error_type;
00797             }else{
00798                 if(distance < threshold)
00799                     s->error_status_table[mb_xy]|= 1<<error_type;
00800             }
00801 
00802             if(error&VP_START)
00803                 distance= 9999999;
00804         }
00805     }
00806 #endif
00807 
00808     /* forward mark errors */
00809     error=0;
00810     for(i=0; i<s->mb_num; i++){
00811         const int mb_xy= s->mb_index2xy[i];
00812         int old_error= s->error_status_table[mb_xy];
00813 
00814         if(old_error&VP_START)
00815             error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
00816         else{
00817             error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
00818             s->error_status_table[mb_xy]|= error;
00819         }
00820     }
00821 #if 1
00822     /* handle not partitioned case */
00823     if(!s->partitioned_frame){
00824         for(i=0; i<s->mb_num; i++){
00825             const int mb_xy= s->mb_index2xy[i];
00826             error= s->error_status_table[mb_xy];
00827             if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
00828                 error|= AC_ERROR|DC_ERROR|MV_ERROR;
00829             s->error_status_table[mb_xy]= error;
00830         }
00831     }
00832 #endif
00833 
00834     dc_error= ac_error= mv_error=0;
00835     for(i=0; i<s->mb_num; i++){
00836         const int mb_xy= s->mb_index2xy[i];
00837         error= s->error_status_table[mb_xy];
00838         if(error&DC_ERROR) dc_error ++;
00839         if(error&AC_ERROR) ac_error ++;
00840         if(error&MV_ERROR) mv_error ++;
00841     }
00842     av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
00843 
00844     is_intra_likely= is_intra_more_likely(s);
00845 
00846     /* set unknown mb-type to most likely */
00847     for(i=0; i<s->mb_num; i++){
00848         const int mb_xy= s->mb_index2xy[i];
00849         error= s->error_status_table[mb_xy];
00850         if(!((error&DC_ERROR) && (error&MV_ERROR)))
00851             continue;
00852 
00853         if(is_intra_likely)
00854             s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
00855         else
00856             s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
00857     }
00858 
00859     /* handle inter blocks with damaged AC */
00860     for(mb_y=0; mb_y<s->mb_height; mb_y++){
00861         for(mb_x=0; mb_x<s->mb_width; mb_x++){
00862             const int mb_xy= mb_x + mb_y * s->mb_stride;
00863             const int mb_type= s->current_picture.mb_type[mb_xy];
00864             error= s->error_status_table[mb_xy];
00865 
00866             if(IS_INTRA(mb_type)) continue; //intra
00867             if(error&MV_ERROR) continue;              //inter with damaged MV
00868             if(!(error&AC_ERROR)) continue;           //undamaged inter
00869 
00870             s->mv_dir = MV_DIR_FORWARD;
00871             s->mb_intra=0;
00872             s->mb_skipped=0;
00873             if(IS_8X8(mb_type)){
00874                 int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
00875                 int j;
00876                 s->mv_type = MV_TYPE_8X8;
00877                 for(j=0; j<4; j++){
00878                     s->mv[0][j][0] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
00879                     s->mv[0][j][1] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
00880                 }
00881             }else{
00882                 s->mv_type = MV_TYPE_16X16;
00883                 s->mv[0][0][0] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][0];
00884                 s->mv[0][0][1] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][1];
00885             }
00886 
00887             s->dsp.clear_blocks(s->block[0]);
00888 
00889             s->mb_x= mb_x;
00890             s->mb_y= mb_y;
00891             decode_mb(s);
00892         }
00893     }
00894 
00895     /* guess MVs */
00896     if(s->pict_type==FF_B_TYPE){
00897         for(mb_y=0; mb_y<s->mb_height; mb_y++){
00898             for(mb_x=0; mb_x<s->mb_width; mb_x++){
00899                 int xy= mb_x*2 + mb_y*2*s->b8_stride;
00900                 const int mb_xy= mb_x + mb_y * s->mb_stride;
00901                 const int mb_type= s->current_picture.mb_type[mb_xy];
00902                 error= s->error_status_table[mb_xy];
00903 
00904                 if(IS_INTRA(mb_type)) continue;
00905                 if(!(error&MV_ERROR)) continue;           //inter with undamaged MV
00906                 if(!(error&AC_ERROR)) continue;           //undamaged inter
00907 
00908                 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
00909                 s->mb_intra=0;
00910                 s->mv_type = MV_TYPE_16X16;
00911                 s->mb_skipped=0;
00912 
00913                 if(s->pp_time){
00914                     int time_pp= s->pp_time;
00915                     int time_pb= s->pb_time;
00916 
00917                     s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
00918                     s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
00919                     s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
00920                     s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
00921                 }else{
00922                     s->mv[0][0][0]= 0;
00923                     s->mv[0][0][1]= 0;
00924                     s->mv[1][0][0]= 0;
00925                     s->mv[1][0][1]= 0;
00926                 }
00927 
00928                 s->dsp.clear_blocks(s->block[0]);
00929                 s->mb_x= mb_x;
00930                 s->mb_y= mb_y;
00931                 decode_mb(s);
00932             }
00933         }
00934     }else
00935         guess_mv(s);
00936 
00937 #ifdef HAVE_XVMC
00938     /* the filters below are not XvMC compatible, skip them */
00939     if(s->avctx->xvmc_acceleration) goto ec_clean;
00940 #endif
00941     /* fill DC for inter blocks */
00942     for(mb_y=0; mb_y<s->mb_height; mb_y++){
00943         for(mb_x=0; mb_x<s->mb_width; mb_x++){
00944             int dc, dcu, dcv, y, n;
00945             int16_t *dc_ptr;
00946             uint8_t *dest_y, *dest_cb, *dest_cr;
00947             const int mb_xy= mb_x + mb_y * s->mb_stride;
00948             const int mb_type= s->current_picture.mb_type[mb_xy];
00949 
00950             error= s->error_status_table[mb_xy];
00951 
00952             if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
00953 //            if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
00954 
00955             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
00956             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
00957             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
00958 
00959             dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
00960             for(n=0; n<4; n++){
00961                 dc=0;
00962                 for(y=0; y<8; y++){
00963                     int x;
00964                     for(x=0; x<8; x++){
00965                        dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
00966                     }
00967                 }
00968                 dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
00969             }
00970 
00971             dcu=dcv=0;
00972             for(y=0; y<8; y++){
00973                 int x;
00974                 for(x=0; x<8; x++){
00975                     dcu+=dest_cb[x + y*(s->uvlinesize)];
00976                     dcv+=dest_cr[x + y*(s->uvlinesize)];
00977                 }
00978             }
00979             s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
00980             s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
00981         }
00982     }
00983 #if 1
00984     /* guess DC for damaged blocks */
00985     guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
00986     guess_dc(s, s->dc_val[1], s->mb_width  , s->mb_height  , s->mb_stride, 0);
00987     guess_dc(s, s->dc_val[2], s->mb_width  , s->mb_height  , s->mb_stride, 0);
00988 #endif
00989     /* filter luma DC */
00990     filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
00991 
00992 #if 1
00993     /* render DC only intra */
00994     for(mb_y=0; mb_y<s->mb_height; mb_y++){
00995         for(mb_x=0; mb_x<s->mb_width; mb_x++){
00996             uint8_t *dest_y, *dest_cb, *dest_cr;
00997             const int mb_xy= mb_x + mb_y * s->mb_stride;
00998             const int mb_type= s->current_picture.mb_type[mb_xy];
00999 
01000             error= s->error_status_table[mb_xy];
01001 
01002             if(IS_INTER(mb_type)) continue;
01003             if(!(error&AC_ERROR)) continue;              //undamaged
01004 
01005             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
01006             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
01007             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
01008 
01009             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
01010         }
01011     }
01012 #endif
01013 
01014     if(s->avctx->error_concealment&FF_EC_DEBLOCK){
01015         /* filter horizontal block boundaries */
01016         h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
01017         h_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01018         h_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01019 
01020         /* filter vertical block boundaries */
01021         v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
01022         v_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01023         v_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01024     }
01025 
01026 #ifdef HAVE_XVMC
01027 ec_clean:
01028 #endif
01029     /* clean a few tables */
01030     for(i=0; i<s->mb_num; i++){
01031         const int mb_xy= s->mb_index2xy[i];
01032         int error= s->error_status_table[mb_xy];
01033 
01034         if(s->pict_type!=FF_B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
01035             s->mbskip_table[mb_xy]=0;
01036         }
01037         s->mbintra_table[mb_xy]=1;
01038     }
01039 }

Generated on Fri Jan 9 13:44:27 2009 for libextractor by  doxygen 1.5.1