• 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 
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 
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 
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 
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 
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 || !s->last_picture_ptr->data[0]) 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     //prevent dsp.sad() check, that requires access to the image
00567     if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration && s->pict_type == FF_I_TYPE)
00568         return 1;
00569 
00570     skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
00571     is_intra_likely=0;
00572 
00573     j=0;
00574     for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
00575         for(mb_x= 0; mb_x<s->mb_width; mb_x++){
00576             int error;
00577             const int mb_xy= mb_x + mb_y*s->mb_stride;
00578 
00579             error= s->error_status_table[mb_xy];
00580             if((error&DC_ERROR) && (error&MV_ERROR))
00581                 continue; //skip damaged
00582 
00583             j++;
00584             if((j%skip_amount) != 0) continue; //skip a few to speed things up
00585 
00586             if(s->pict_type==FF_I_TYPE){
00587                 uint8_t *mb_ptr     = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
00588                 uint8_t *last_mb_ptr= s->last_picture.data   [0] + mb_x*16 + mb_y*16*s->linesize;
00589 
00590                 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr                    , s->linesize, 16);
00591                 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
00592             }else{
00593                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
00594                    is_intra_likely++;
00595                 else
00596                    is_intra_likely--;
00597             }
00598         }
00599     }
00600 //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
00601     return is_intra_likely > 0;
00602 }
00603 
00604 void ff_er_frame_start(MpegEncContext *s){
00605     if(!s->error_recognition) return;
00606 
00607     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));
00608     s->error_count= 3*s->mb_num;
00609 }
00610 
00617 void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
00618     const int start_i= av_clip(startx + starty * s->mb_width    , 0, s->mb_num-1);
00619     const int end_i  = av_clip(endx   + endy   * s->mb_width    , 0, s->mb_num);
00620     const int start_xy= s->mb_index2xy[start_i];
00621     const int end_xy  = s->mb_index2xy[end_i];
00622     int mask= -1;
00623 
00624     if(s->avctx->hwaccel)
00625         return;
00626 
00627     if(start_i > end_i || start_xy > end_xy){
00628         av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before start\n");
00629         return;
00630     }
00631 
00632     if(!s->error_recognition) return;
00633 
00634     mask &= ~VP_START;
00635     if(status & (AC_ERROR|AC_END)){
00636         mask &= ~(AC_ERROR|AC_END);
00637         s->error_count -= end_i - start_i + 1;
00638     }
00639     if(status & (DC_ERROR|DC_END)){
00640         mask &= ~(DC_ERROR|DC_END);
00641         s->error_count -= end_i - start_i + 1;
00642     }
00643     if(status & (MV_ERROR|MV_END)){
00644         mask &= ~(MV_ERROR|MV_END);
00645         s->error_count -= end_i - start_i + 1;
00646     }
00647 
00648     if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
00649 
00650     if(mask == ~0x7F){
00651         memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
00652     }else{
00653         int i;
00654         for(i=start_xy; i<end_xy; i++){
00655             s->error_status_table[ i ] &= mask;
00656         }
00657     }
00658 
00659     if(end_i == s->mb_num)
00660         s->error_count= INT_MAX;
00661     else{
00662         s->error_status_table[end_xy] &= mask;
00663         s->error_status_table[end_xy] |= status;
00664     }
00665 
00666     s->error_status_table[start_xy] |= VP_START;
00667 
00668     if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
00669         int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
00670 
00671         prev_status &= ~ VP_START;
00672         if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
00673     }
00674 }
00675 
00676 void ff_er_frame_end(MpegEncContext *s){
00677     int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
00678     int distance;
00679     int threshold_part[4]= {100,100,100};
00680     int threshold= 50;
00681     int is_intra_likely;
00682     int size = s->b8_stride * 2 * s->mb_height;
00683     Picture *pic= s->current_picture_ptr;
00684 
00685     if(!s->error_recognition || s->error_count==0 || s->avctx->lowres ||
00686        s->avctx->hwaccel ||
00687        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
00688        s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
00689 
00690     if(s->current_picture.motion_val[0] == NULL){
00691         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
00692 
00693         for(i=0; i<2; i++){
00694             pic->ref_index[i]= av_mallocz(size * sizeof(uint8_t));
00695             pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
00696             pic->motion_val[i]= pic->motion_val_base[i]+4;
00697         }
00698         pic->motion_subsample_log2= 3;
00699         s->current_picture= *s->current_picture_ptr;
00700     }
00701 
00702     for(i=0; i<2; i++){
00703         if(pic->ref_index[i])
00704             memset(pic->ref_index[i], 0, size * sizeof(uint8_t));
00705     }
00706 
00707     if(s->avctx->debug&FF_DEBUG_ER){
00708         for(mb_y=0; mb_y<s->mb_height; mb_y++){
00709             for(mb_x=0; mb_x<s->mb_width; mb_x++){
00710                 int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
00711 
00712                 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
00713             }
00714             av_log(s->avctx, AV_LOG_DEBUG, "\n");
00715         }
00716     }
00717 
00718 #if 1
00719     /* handle overlapping slices */
00720     for(error_type=1; error_type<=3; error_type++){
00721         int end_ok=0;
00722 
00723         for(i=s->mb_num-1; i>=0; i--){
00724             const int mb_xy= s->mb_index2xy[i];
00725             int error= s->error_status_table[mb_xy];
00726 
00727             if(error&(1<<error_type))
00728                 end_ok=1;
00729             if(error&(8<<error_type))
00730                 end_ok=1;
00731 
00732             if(!end_ok)
00733                 s->error_status_table[mb_xy]|= 1<<error_type;
00734 
00735             if(error&VP_START)
00736                 end_ok=0;
00737         }
00738     }
00739 #endif
00740 #if 1
00741     /* handle slices with partitions of different length */
00742     if(s->partitioned_frame){
00743         int end_ok=0;
00744 
00745         for(i=s->mb_num-1; i>=0; i--){
00746             const int mb_xy= s->mb_index2xy[i];
00747             int error= s->error_status_table[mb_xy];
00748 
00749             if(error&AC_END)
00750                 end_ok=0;
00751             if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
00752                 end_ok=1;
00753 
00754             if(!end_ok)
00755                 s->error_status_table[mb_xy]|= AC_ERROR;
00756 
00757             if(error&VP_START)
00758                 end_ok=0;
00759         }
00760     }
00761 #endif
00762     /* handle missing slices */
00763     if(s->error_recognition>=4){
00764         int end_ok=1;
00765 
00766         for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
00767             const int mb_xy= s->mb_index2xy[i];
00768             int error1= s->error_status_table[mb_xy  ];
00769             int error2= s->error_status_table[s->mb_index2xy[i+1]];
00770 
00771             if(error1&VP_START)
00772                 end_ok=1;
00773 
00774             if(   error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
00775                && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
00776                && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
00777                 end_ok=0;
00778             }
00779 
00780             if(!end_ok)
00781                 s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
00782         }
00783     }
00784 
00785 #if 1
00786     /* backward mark errors */
00787     distance=9999999;
00788     for(error_type=1; error_type<=3; error_type++){
00789         for(i=s->mb_num-1; i>=0; i--){
00790             const int mb_xy= s->mb_index2xy[i];
00791             int error= s->error_status_table[mb_xy];
00792 
00793             if(!s->mbskip_table[mb_xy]) //FIXME partition specific
00794                 distance++;
00795             if(error&(1<<error_type))
00796                 distance= 0;
00797 
00798             if(s->partitioned_frame){
00799                 if(distance < threshold_part[error_type-1])
00800                     s->error_status_table[mb_xy]|= 1<<error_type;
00801             }else{
00802                 if(distance < threshold)
00803                     s->error_status_table[mb_xy]|= 1<<error_type;
00804             }
00805 
00806             if(error&VP_START)
00807                 distance= 9999999;
00808         }
00809     }
00810 #endif
00811 
00812     /* forward mark errors */
00813     error=0;
00814     for(i=0; i<s->mb_num; i++){
00815         const int mb_xy= s->mb_index2xy[i];
00816         int old_error= s->error_status_table[mb_xy];
00817 
00818         if(old_error&VP_START)
00819             error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
00820         else{
00821             error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
00822             s->error_status_table[mb_xy]|= error;
00823         }
00824     }
00825 #if 1
00826     /* handle not partitioned case */
00827     if(!s->partitioned_frame){
00828         for(i=0; i<s->mb_num; i++){
00829             const int mb_xy= s->mb_index2xy[i];
00830             error= s->error_status_table[mb_xy];
00831             if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
00832                 error|= AC_ERROR|DC_ERROR|MV_ERROR;
00833             s->error_status_table[mb_xy]= error;
00834         }
00835     }
00836 #endif
00837 
00838     dc_error= ac_error= mv_error=0;
00839     for(i=0; i<s->mb_num; i++){
00840         const int mb_xy= s->mb_index2xy[i];
00841         error= s->error_status_table[mb_xy];
00842         if(error&DC_ERROR) dc_error ++;
00843         if(error&AC_ERROR) ac_error ++;
00844         if(error&MV_ERROR) mv_error ++;
00845     }
00846     av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
00847 
00848     is_intra_likely= is_intra_more_likely(s);
00849 
00850     /* set unknown mb-type to most likely */
00851     for(i=0; i<s->mb_num; i++){
00852         const int mb_xy= s->mb_index2xy[i];
00853         error= s->error_status_table[mb_xy];
00854         if(!((error&DC_ERROR) && (error&MV_ERROR)))
00855             continue;
00856 
00857         if(is_intra_likely)
00858             s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
00859         else
00860             s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
00861     }
00862 
00863     /* handle inter blocks with damaged AC */
00864     for(mb_y=0; mb_y<s->mb_height; mb_y++){
00865         for(mb_x=0; mb_x<s->mb_width; mb_x++){
00866             const int mb_xy= mb_x + mb_y * s->mb_stride;
00867             const int mb_type= s->current_picture.mb_type[mb_xy];
00868             error= s->error_status_table[mb_xy];
00869 
00870             if(IS_INTRA(mb_type)) continue; //intra
00871             if(error&MV_ERROR) continue;              //inter with damaged MV
00872             if(!(error&AC_ERROR)) continue;           //undamaged inter
00873 
00874             s->mv_dir = MV_DIR_FORWARD;
00875             s->mb_intra=0;
00876             s->mb_skipped=0;
00877             if(IS_8X8(mb_type)){
00878                 int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
00879                 int j;
00880                 s->mv_type = MV_TYPE_8X8;
00881                 for(j=0; j<4; j++){
00882                     s->mv[0][j][0] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
00883                     s->mv[0][j][1] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
00884                 }
00885             }else{
00886                 s->mv_type = MV_TYPE_16X16;
00887                 s->mv[0][0][0] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][0];
00888                 s->mv[0][0][1] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][1];
00889             }
00890 
00891             s->dsp.clear_blocks(s->block[0]);
00892 
00893             s->mb_x= mb_x;
00894             s->mb_y= mb_y;
00895             decode_mb(s);
00896         }
00897     }
00898 
00899     /* guess MVs */
00900     if(s->pict_type==FF_B_TYPE){
00901         for(mb_y=0; mb_y<s->mb_height; mb_y++){
00902             for(mb_x=0; mb_x<s->mb_width; mb_x++){
00903                 int xy= mb_x*2 + mb_y*2*s->b8_stride;
00904                 const int mb_xy= mb_x + mb_y * s->mb_stride;
00905                 const int mb_type= s->current_picture.mb_type[mb_xy];
00906                 error= s->error_status_table[mb_xy];
00907 
00908                 if(IS_INTRA(mb_type)) continue;
00909                 if(!(error&MV_ERROR)) continue;           //inter with undamaged MV
00910                 if(!(error&AC_ERROR)) continue;           //undamaged inter
00911 
00912                 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
00913                 s->mb_intra=0;
00914                 s->mv_type = MV_TYPE_16X16;
00915                 s->mb_skipped=0;
00916 
00917                 if(s->pp_time){
00918                     int time_pp= s->pp_time;
00919                     int time_pb= s->pb_time;
00920 
00921                     s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
00922                     s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
00923                     s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
00924                     s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
00925                 }else{
00926                     s->mv[0][0][0]= 0;
00927                     s->mv[0][0][1]= 0;
00928                     s->mv[1][0][0]= 0;
00929                     s->mv[1][0][1]= 0;
00930                 }
00931 
00932                 s->dsp.clear_blocks(s->block[0]);
00933                 s->mb_x= mb_x;
00934                 s->mb_y= mb_y;
00935                 decode_mb(s);
00936             }
00937         }
00938     }else
00939         guess_mv(s);
00940 
00941     /* the filters below are not XvMC compatible, skip them */
00942     if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
00943         goto ec_clean;
00944     /* fill DC for inter blocks */
00945     for(mb_y=0; mb_y<s->mb_height; mb_y++){
00946         for(mb_x=0; mb_x<s->mb_width; mb_x++){
00947             int dc, dcu, dcv, y, n;
00948             int16_t *dc_ptr;
00949             uint8_t *dest_y, *dest_cb, *dest_cr;
00950             const int mb_xy= mb_x + mb_y * s->mb_stride;
00951             const int mb_type= s->current_picture.mb_type[mb_xy];
00952 
00953             error= s->error_status_table[mb_xy];
00954 
00955             if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
00956 //            if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
00957 
00958             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
00959             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
00960             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
00961 
00962             dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
00963             for(n=0; n<4; n++){
00964                 dc=0;
00965                 for(y=0; y<8; y++){
00966                     int x;
00967                     for(x=0; x<8; x++){
00968                        dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
00969                     }
00970                 }
00971                 dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
00972             }
00973 
00974             dcu=dcv=0;
00975             for(y=0; y<8; y++){
00976                 int x;
00977                 for(x=0; x<8; x++){
00978                     dcu+=dest_cb[x + y*(s->uvlinesize)];
00979                     dcv+=dest_cr[x + y*(s->uvlinesize)];
00980                 }
00981             }
00982             s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
00983             s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
00984         }
00985     }
00986 #if 1
00987     /* guess DC for damaged blocks */
00988     guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
00989     guess_dc(s, s->dc_val[1], s->mb_width  , s->mb_height  , s->mb_stride, 0);
00990     guess_dc(s, s->dc_val[2], s->mb_width  , s->mb_height  , s->mb_stride, 0);
00991 #endif
00992     /* filter luma DC */
00993     filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
00994 
00995 #if 1
00996     /* render DC only intra */
00997     for(mb_y=0; mb_y<s->mb_height; mb_y++){
00998         for(mb_x=0; mb_x<s->mb_width; mb_x++){
00999             uint8_t *dest_y, *dest_cb, *dest_cr;
01000             const int mb_xy= mb_x + mb_y * s->mb_stride;
01001             const int mb_type= s->current_picture.mb_type[mb_xy];
01002 
01003             error= s->error_status_table[mb_xy];
01004 
01005             if(IS_INTER(mb_type)) continue;
01006             if(!(error&AC_ERROR)) continue;              //undamaged
01007 
01008             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
01009             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
01010             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
01011 
01012             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
01013         }
01014     }
01015 #endif
01016 
01017     if(s->avctx->error_concealment&FF_EC_DEBLOCK){
01018         /* filter horizontal block boundaries */
01019         h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
01020         h_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01021         h_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01022 
01023         /* filter vertical block boundaries */
01024         v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
01025         v_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01026         v_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
01027     }
01028 
01029 ec_clean:
01030     /* clean a few tables */
01031     for(i=0; i<s->mb_num; i++){
01032         const int mb_xy= s->mb_index2xy[i];
01033         int error= s->error_status_table[mb_xy];
01034 
01035         if(s->pict_type!=FF_B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
01036             s->mbskip_table[mb_xy]=0;
01037         }
01038         s->mbintra_table[mb_xy]=1;
01039     }
01040 }

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1