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