simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead...
[ffmpeg.git] / libavcodec / error_resilience.c
1 /*
2  * Error resilience / concealment
3  *
4  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20  
21 /**
22  * @file error_resilience.c
23  * Error resilience / concealment.
24  */
25
26 #include <limits.h>
27  
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "mpegvideo.h"
31 #include "common.h"
32
33 /**
34  * replaces the current MB with a flat dc only version.
35  */
36 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
37 {
38     int dc, dcu, dcv, y, i;
39     for(i=0; i<4; i++){
40         dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
41         if(dc<0) dc=0;
42         else if(dc>2040) dc=2040;
43         for(y=0; y<8; y++){
44             int x;
45             for(x=0; x<8; x++){
46                 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
47             }
48         }
49     }
50     dcu = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
51     dcv = s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
52     if     (dcu<0   ) dcu=0;
53     else if(dcu>2040) dcu=2040;
54     if     (dcv<0   ) dcv=0;
55     else if(dcv>2040) dcv=2040;
56     for(y=0; y<8; y++){
57         int x;
58         for(x=0; x<8; x++){
59             dest_cb[x + y*(s->uvlinesize)]= dcu/8;
60             dest_cr[x + y*(s->uvlinesize)]= dcv/8;
61         }
62     }
63 }
64
65 static void filter181(int16_t *data, int width, int height, int stride){
66     int x,y;
67
68     /* horizontal filter */
69     for(y=1; y<height-1; y++){
70         int prev_dc= data[0 + y*stride];
71
72         for(x=1; x<width-1; x++){
73             int dc;
74             
75             dc= - prev_dc 
76                 + data[x     + y*stride]*8
77                 - data[x + 1 + y*stride];
78             dc= (dc*10923 + 32768)>>16;
79             prev_dc= data[x + y*stride];
80             data[x + y*stride]= dc;
81         }
82     }
83     
84     /* vertical filter */
85     for(x=1; x<width-1; x++){
86         int prev_dc= data[x];
87
88         for(y=1; y<height-1; y++){
89             int dc;
90             
91             dc= - prev_dc 
92                 + data[x +  y   *stride]*8
93                 - data[x + (y+1)*stride];
94             dc= (dc*10923 + 32768)>>16;
95             prev_dc= data[x + y*stride];
96             data[x + y*stride]= dc;
97         }
98     }
99 }
100
101 /**
102  * guess the dc of blocks which dont have a undamaged dc
103  * @param w     width in 8 pixel blocks
104  * @param h     height in 8 pixel blocks
105  */
106 static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
107     int b_x, b_y;
108
109     for(b_y=0; b_y<h; b_y++){
110         for(b_x=0; b_x<w; b_x++){
111             int color[4]={1024,1024,1024,1024};
112             int distance[4]={9999,9999,9999,9999};
113             int mb_index, error, j;
114             int64_t guess, weight_sum;
115             
116             mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
117             
118             error= s->error_status_table[mb_index];
119             
120             if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
121             if(!(error&DC_ERROR)) continue;           //dc-ok
122             
123             /* right block */
124             for(j=b_x+1; j<w; j++){
125                 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
126                 int error_j= s->error_status_table[mb_index_j];
127                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
128                 if(intra_j==0 || !(error_j&DC_ERROR)){
129                     color[0]= dc[j + b_y*stride];
130                     distance[0]= j-b_x;
131                     break;
132                 }
133             }
134             
135             /* left block */
136             for(j=b_x-1; j>=0; j--){
137                 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
138                 int error_j= s->error_status_table[mb_index_j];
139                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
140                 if(intra_j==0 || !(error_j&DC_ERROR)){
141                     color[1]= dc[j + b_y*stride];
142                     distance[1]= b_x-j;
143                     break;
144                 }
145             }
146
147             /* bottom block */
148             for(j=b_y+1; j<h; j++){
149                 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
150                 int error_j= s->error_status_table[mb_index_j];
151                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
152                 if(intra_j==0 || !(error_j&DC_ERROR)){
153                     color[2]= dc[b_x + j*stride];
154                     distance[2]= j-b_y;
155                     break;
156                 }
157             }
158
159             /* top block */
160             for(j=b_y-1; j>=0; j--){
161                 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
162                 int error_j= s->error_status_table[mb_index_j];
163                 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
164                 if(intra_j==0 || !(error_j&DC_ERROR)){
165                     color[3]= dc[b_x + j*stride];
166                     distance[3]= b_y-j;
167                     break;
168                 }
169             }
170             
171             weight_sum=0;
172             guess=0;
173             for(j=0; j<4; j++){
174                 int64_t weight= 256*256*256*16/distance[j];
175                 guess+= weight*(int64_t)color[j];
176                 weight_sum+= weight;
177             }
178             guess= (guess + weight_sum/2) / weight_sum;
179
180             dc[b_x + b_y*stride]= guess;
181         }
182     }
183 }
184
185 /**
186  * simple horizontal deblocking filter used for error resilience
187  * @param w     width in 8 pixel blocks
188  * @param h     height in 8 pixel blocks
189  */
190 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
191     int b_x, b_y;
192     uint8_t *cm = cropTbl + MAX_NEG_CROP;
193
194     for(b_y=0; b_y<h; b_y++){
195         for(b_x=0; b_x<w-1; b_x++){
196             int y;
197             int left_status = s->error_status_table[( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride];
198             int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
199             int left_intra=   IS_INTRA(s->current_picture.mb_type      [( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
200             int right_intra=  IS_INTRA(s->current_picture.mb_type      [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
201             int left_damage =  left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
202             int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
203             int offset= b_x*8 + b_y*stride*8;
204             int16_t *left_mv=  s->motion_val[s->block_wrap[0]*((b_y<<(1-is_luma)) + 1) + ( b_x   <<(1-is_luma))];
205             int16_t *right_mv= s->motion_val[s->block_wrap[0]*((b_y<<(1-is_luma)) + 1) + ((b_x+1)<<(1-is_luma))];
206             
207             if(!(left_damage||right_damage)) continue; // both undamaged
208             
209             if(   (!left_intra) && (!right_intra) 
210                && ABS(left_mv[0]-right_mv[0]) + ABS(left_mv[1]+right_mv[1]) < 2) continue;
211             
212             for(y=0; y<8; y++){
213                 int a,b,c,d;
214                 
215                 a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
216                 b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
217                 c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
218                 
219                 d= ABS(b) - ((ABS(a) + ABS(c) + 1)>>1);
220                 d= FFMAX(d, 0);
221                 if(b<0) d= -d;
222                 
223                 if(d==0) continue;
224
225                 if(!(left_damage && right_damage))
226                     d= d*16/9;
227                 
228                 if(left_damage){
229                     dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
230                     dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
231                     dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
232                     dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
233                 }
234                 if(right_damage){
235                     dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
236                     dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
237                     dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
238                     dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
239                 }
240             }
241         }
242     }
243 }
244
245 /**
246  * simple vertical deblocking filter used for error resilience
247  * @param w     width in 8 pixel blocks
248  * @param h     height in 8 pixel blocks
249  */
250 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
251     int b_x, b_y;
252     uint8_t *cm = cropTbl + MAX_NEG_CROP;
253
254     for(b_y=0; b_y<h-1; b_y++){
255         for(b_x=0; b_x<w; b_x++){
256             int x;
257             int top_status   = s->error_status_table[(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride];
258             int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
259             int top_intra=     IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride]);
260             int bottom_intra=  IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
261             int top_damage =      top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
262             int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
263             int offset= b_x*8 + b_y*stride*8;
264             int16_t *top_mv=    s->motion_val[s->block_wrap[0]*(( b_y   <<(1-is_luma)) + 1) + (b_x<<(1-is_luma))];
265             int16_t *bottom_mv= s->motion_val[s->block_wrap[0]*(((b_y+1)<<(1-is_luma)) + 1) + (b_x<<(1-is_luma))];
266             
267             if(!(top_damage||bottom_damage)) continue; // both undamaged
268             
269             if(   (!top_intra) && (!bottom_intra) 
270                && ABS(top_mv[0]-bottom_mv[0]) + ABS(top_mv[1]+bottom_mv[1]) < 2) continue;
271             
272             for(x=0; x<8; x++){
273                 int a,b,c,d;
274                 
275                 a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
276                 b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
277                 c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
278                 
279                 d= ABS(b) - ((ABS(a) + ABS(c)+1)>>1);
280                 d= FFMAX(d, 0);
281                 if(b<0) d= -d;
282                 
283                 if(d==0) continue;
284
285                 if(!(top_damage && bottom_damage))
286                     d= d*16/9;
287                 
288                 if(top_damage){
289                     dst[offset + x +  7*stride] = cm[dst[offset + x +  7*stride] + ((d*7)>>4)];
290                     dst[offset + x +  6*stride] = cm[dst[offset + x +  6*stride] + ((d*5)>>4)];
291                     dst[offset + x +  5*stride] = cm[dst[offset + x +  5*stride] + ((d*3)>>4)];
292                     dst[offset + x +  4*stride] = cm[dst[offset + x +  4*stride] + ((d*1)>>4)];
293                 }
294                 if(bottom_damage){
295                     dst[offset + x +  8*stride] = cm[dst[offset + x +  8*stride] - ((d*7)>>4)];
296                     dst[offset + x +  9*stride] = cm[dst[offset + x +  9*stride] - ((d*5)>>4)];
297                     dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
298                     dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
299                 }
300             }
301         }
302     }
303 }
304
305 static void guess_mv(MpegEncContext *s){
306     uint8_t fixed[s->mb_stride * s->mb_height];
307 #define MV_FROZEN    3
308 #define MV_CHANGED   2
309 #define MV_UNCHANGED 1
310     const int mb_stride = s->mb_stride;
311     const int mb_width = s->mb_width;
312     const int mb_height= s->mb_height;
313     int i, depth, num_avail;
314     int mb_x, mb_y;
315    
316     num_avail=0;
317     for(i=0; i<s->mb_num; i++){
318         const int mb_xy= s->mb_index2xy[ i ];
319         int f=0;
320         int error= s->error_status_table[mb_xy];
321
322         if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
323         if(!(error&MV_ERROR)) f=MV_FROZEN;           //inter with undamaged MV
324         
325         fixed[mb_xy]= f;
326         if(f==MV_FROZEN)
327             num_avail++;
328     }
329     
330     if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
331         for(mb_y=0; mb_y<s->mb_height; mb_y++){
332             for(mb_x=0; mb_x<s->mb_width; mb_x++){
333                 const int mb_xy= mb_x + mb_y*s->mb_stride;
334                 
335                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))  continue;
336                 if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
337
338                 s->mv_dir = MV_DIR_FORWARD;
339                 s->mb_intra=0;
340                 s->mv_type = MV_TYPE_16X16;
341                 s->mb_skiped=0;
342
343                 s->dsp.clear_blocks(s->block[0]);
344
345                 s->mb_x= mb_x;
346                 s->mb_y= mb_y;
347                 s->mv[0][0][0]= 0;
348                 s->mv[0][0][1]= 0;
349                 MPV_decode_mb(s, s->block);
350             }
351         }
352         return;
353     }
354     
355     for(depth=0;; depth++){
356         int changed, pass, none_left;
357
358         none_left=1;
359         changed=1;
360         for(pass=0; (changed || pass<2) && pass<10; pass++){
361             int mb_x, mb_y;
362 int score_sum=0;
363  
364             changed=0;
365             for(mb_y=0; mb_y<s->mb_height; mb_y++){
366                 for(mb_x=0; mb_x<s->mb_width; mb_x++){
367                     const int mb_xy= mb_x + mb_y*s->mb_stride;
368                     int mv_predictor[8][2]={{0}};
369                     int pred_count=0;
370                     int j;
371                     int best_score=256*256*256*64;
372                     int best_pred=0;
373                     const int mot_stride= mb_width*2+2;
374                     const int mot_index= mb_x*2 + 1 + (mb_y*2+1)*mot_stride;
375                     int prev_x= s->motion_val[mot_index][0];
376                     int prev_y= s->motion_val[mot_index][1];
377
378                     if((mb_x^mb_y^pass)&1) continue;
379                     
380                     if(fixed[mb_xy]==MV_FROZEN) continue;
381                     assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
382                     assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
383                     
384                     j=0;
385                     if(mb_x>0           && fixed[mb_xy-1        ]==MV_FROZEN) j=1;
386                     if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_FROZEN) j=1;
387                     if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
388                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
389                     if(j==0) continue;
390
391                     j=0;
392                     if(mb_x>0           && fixed[mb_xy-1        ]==MV_CHANGED) j=1;
393                     if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_CHANGED) j=1;
394                     if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
395                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
396                     if(j==0 && pass>1) continue;
397                     
398                     none_left=0;
399                     
400                     if(mb_x>0 && fixed[mb_xy-1]){
401                         mv_predictor[pred_count][0]= s->motion_val[mot_index - 2][0];
402                         mv_predictor[pred_count][1]= s->motion_val[mot_index - 2][1];
403                         pred_count++;
404                     }
405                     if(mb_x+1<mb_width && fixed[mb_xy+1]){
406                         mv_predictor[pred_count][0]= s->motion_val[mot_index + 2][0];
407                         mv_predictor[pred_count][1]= s->motion_val[mot_index + 2][1];
408                         pred_count++;
409                     }
410                     if(mb_y>0 && fixed[mb_xy-mb_stride]){
411                         mv_predictor[pred_count][0]= s->motion_val[mot_index - mot_stride*2][0];
412                         mv_predictor[pred_count][1]= s->motion_val[mot_index - mot_stride*2][1];
413                         pred_count++;
414                     }
415                     if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
416                         mv_predictor[pred_count][0]= s->motion_val[mot_index + mot_stride*2][0];
417                         mv_predictor[pred_count][1]= s->motion_val[mot_index + mot_stride*2][1];
418                         pred_count++;
419                     }
420                     if(pred_count==0) continue;
421                     
422                     if(pred_count>1){
423                         int sum_x=0, sum_y=0;
424                         int max_x, max_y, min_x, min_y;
425
426                         for(j=0; j<pred_count; j++){
427                             sum_x+= mv_predictor[j][0];
428                             sum_y+= mv_predictor[j][1];
429                         }
430                     
431                         /* mean */
432                         mv_predictor[pred_count][0] = sum_x/j;
433                         mv_predictor[pred_count][1] = sum_y/j;
434                     
435                         /* median */
436                         if(pred_count>=3){
437                             min_y= min_x= 99999;
438                             max_y= max_x=-99999;
439                         }else{
440                             min_x=min_y=max_x=max_y=0;
441                         }
442                         for(j=0; j<pred_count; j++){
443                             max_x= FFMAX(max_x, mv_predictor[j][0]);
444                             max_y= FFMAX(max_y, mv_predictor[j][1]);
445                             min_x= FFMIN(min_x, mv_predictor[j][0]);
446                             min_y= FFMIN(min_y, mv_predictor[j][1]);
447                         }
448                         mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
449                         mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
450                         
451                         if(pred_count==4){
452                             mv_predictor[pred_count+1][0] /= 2;
453                             mv_predictor[pred_count+1][1] /= 2;
454                         }
455                         pred_count+=2;
456                     }
457                     
458                     /* zero MV */
459                     pred_count++;
460
461                     /* last MV */
462                     mv_predictor[pred_count][0]= s->motion_val[mot_index][0];
463                     mv_predictor[pred_count][1]= s->motion_val[mot_index][1];
464                     pred_count++;                    
465                     
466                     s->mv_dir = MV_DIR_FORWARD;
467                     s->mb_intra=0;
468                     s->mv_type = MV_TYPE_16X16;
469                     s->mb_skiped=0;
470
471                     s->dsp.clear_blocks(s->block[0]);
472
473                     s->mb_x= mb_x;
474                     s->mb_y= mb_y;
475
476                     for(j=0; j<pred_count; j++){
477                         int score=0;
478                         uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
479
480                         s->motion_val[mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
481                         s->motion_val[mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
482
483                         MPV_decode_mb(s, s->block);
484                         
485                         if(mb_x>0 && fixed[mb_xy-1]){
486                             int k;
487                             for(k=0; k<16; k++)
488                                 score += ABS(src[k*s->linesize-1 ]-src[k*s->linesize   ]);
489                         }
490                         if(mb_x+1<mb_width && fixed[mb_xy+1]){
491                             int k;
492                             for(k=0; k<16; k++)
493                                 score += ABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
494                         }
495                         if(mb_y>0 && fixed[mb_xy-mb_stride]){
496                             int k;
497                             for(k=0; k<16; k++)
498                                 score += ABS(src[k-s->linesize   ]-src[k               ]);
499                         }
500                         if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
501                             int k;
502                             for(k=0; k<16; k++)
503                                 score += ABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
504                         }
505                         
506                         if(score <= best_score){ // <= will favor the last MV
507                             best_score= score;
508                             best_pred= j;
509                         }
510                     }
511 score_sum+= best_score;
512 //FIXME no need to set s->motion_val[mot_index][0] explicit
513                     s->motion_val[mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
514                     s->motion_val[mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
515
516                     MPV_decode_mb(s, s->block);
517
518                     
519                     if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
520                         fixed[mb_xy]=MV_CHANGED;
521                         changed++;
522                     }else
523                         fixed[mb_xy]=MV_UNCHANGED;
524                 }
525             }
526
527 //            printf(".%d/%d", changed, score_sum); fflush(stdout);
528         }
529         
530         if(none_left) 
531             return;
532             
533         for(i=0; i<s->mb_num; i++){
534             int mb_xy= s->mb_index2xy[i];
535             if(fixed[mb_xy])
536                 fixed[mb_xy]=MV_FROZEN;
537         }
538 //        printf(":"); fflush(stdout);
539     }
540 }
541     
542 static int is_intra_more_likely(MpegEncContext *s){
543     int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
544     
545     if(s->last_picture_ptr==NULL) return 1; //no previous frame available -> use spatial prediction
546
547     undamaged_count=0;
548     for(i=0; i<s->mb_num; i++){
549         const int mb_xy= s->mb_index2xy[i];
550         const int error= s->error_status_table[mb_xy];
551         if(!((error&DC_ERROR) && (error&MV_ERROR)))
552             undamaged_count++;
553     }
554     
555     if(undamaged_count < 5) return 0; //allmost all MBs damaged -> use temporal prediction
556     
557     skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs 
558     is_intra_likely=0;
559
560     j=0;
561     for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
562         for(mb_x= 0; mb_x<s->mb_width; mb_x++){
563             int error;
564             const int mb_xy= mb_x + mb_y*s->mb_stride;
565
566             error= s->error_status_table[mb_xy];
567             if((error&DC_ERROR) && (error&MV_ERROR))
568                 continue; //skip damaged
569         
570             j++;    
571             if((j%skip_amount) != 0) continue; //skip a few to speed things up
572     
573             if(s->pict_type==I_TYPE){
574                 uint8_t *mb_ptr     = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
575                 uint8_t *last_mb_ptr= s->last_picture.data   [0] + mb_x*16 + mb_y*16*s->linesize;
576     
577                 is_intra_likely += s->dsp.pix_abs16x16(last_mb_ptr, mb_ptr                    , s->linesize);
578                 is_intra_likely -= s->dsp.pix_abs16x16(last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize);
579             }else{
580                 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
581                    is_intra_likely++;
582                 else
583                    is_intra_likely--;
584             }
585         }
586     }
587 //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
588     return is_intra_likely > 0;    
589 }
590
591 void ff_er_frame_start(MpegEncContext *s){
592     if(!s->error_resilience) return;
593
594     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));
595     s->error_count= 3*s->mb_num;
596 }
597
598 /**
599  * adds a slice.
600  * @param endx x component of the last macroblock, can be -1 for the last of the previous line
601  * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
602  *               error of the same type occured
603  */
604 void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
605     const int start_i= clip(startx + starty * s->mb_width    , 0, s->mb_num-1);
606     const int end_i  = clip(endx   + endy   * s->mb_width    , 0, s->mb_num);
607     const int start_xy= s->mb_index2xy[start_i];
608     const int end_xy  = s->mb_index2xy[end_i];
609     int mask= -1;
610     
611     if(!s->error_resilience) return;
612
613     mask &= ~VP_START;
614     if(status & (AC_ERROR|AC_END)){
615         mask &= ~(AC_ERROR|AC_END);
616         s->error_count -= end_i - start_i + 1;
617     }
618     if(status & (DC_ERROR|DC_END)){
619         mask &= ~(DC_ERROR|DC_END);
620         s->error_count -= end_i - start_i + 1;
621     }
622     if(status & (MV_ERROR|MV_END)){
623         mask &= ~(MV_ERROR|MV_END);
624         s->error_count -= end_i - start_i + 1;
625     }
626
627     if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
628
629     if(mask == ~0x7F){
630         memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
631     }else{
632         int i;
633         for(i=start_xy; i<end_xy; i++){
634             s->error_status_table[ i ] &= mask;
635         }
636     }
637
638     if(end_i == s->mb_num) 
639         s->error_count= INT_MAX;
640     else{
641         s->error_status_table[end_xy] &= mask;
642         s->error_status_table[end_xy] |= status;
643     }
644  
645     s->error_status_table[start_xy] |= VP_START;
646
647     if(start_xy > 0){
648         int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
649         
650         prev_status &= ~ VP_START;
651         if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
652     }
653 }
654
655 void ff_er_frame_end(MpegEncContext *s){
656     int i, mb_x, mb_y, error, error_type;
657     int distance;
658     int threshold_part[4]= {100,100,100};
659     int threshold= 50;
660     int is_intra_likely;
661     
662     if(!s->error_resilience || s->error_count==0) return;
663
664     fprintf(stderr, "concealing errors\n");
665     
666     if(s->motion_val == NULL){
667         int size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
668         
669         fprintf(stderr, "Warning MVs not available\n");
670         
671         s->motion_val= av_mallocz(size * 2 * sizeof(int16_t));
672     }
673     
674     if(s->avctx->debug&FF_DEBUG_ER){
675         for(mb_y=0; mb_y<s->mb_height; mb_y++){
676             for(mb_x=0; mb_x<s->mb_width; mb_x++){
677                 int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
678             
679                 printf("%2X ", status); 
680             }
681             printf("\n");
682         }
683     }
684     
685 #if 1
686     /* handle overlapping slices */
687     for(error_type=1; error_type<=3; error_type++){
688         int end_ok=0;
689
690         for(i=s->mb_num-1; i>=0; i--){
691             const int mb_xy= s->mb_index2xy[i];
692             int error= s->error_status_table[mb_xy];
693         
694             if(error&(1<<error_type))
695                 end_ok=1;
696             if(error&(8<<error_type))
697                 end_ok=1;
698
699             if(!end_ok)
700                 s->error_status_table[mb_xy]|= 1<<error_type;
701
702             if(error&VP_START)
703                 end_ok=0;
704         }
705     }
706 #endif
707 #if 1
708     /* handle slices with partitions of different length */
709     if(s->partitioned_frame){
710         int end_ok=0;
711
712         for(i=s->mb_num-1; i>=0; i--){
713             const int mb_xy= s->mb_index2xy[i];
714             int error= s->error_status_table[mb_xy];
715         
716             if(error&AC_END)
717                 end_ok=0;
718             if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
719                 end_ok=1;
720
721             if(!end_ok)
722                 s->error_status_table[mb_xy]|= AC_ERROR;
723
724             if(error&VP_START)
725                 end_ok=0;
726         }
727     }
728 #endif
729     /* handle missing slices */
730     if(s->error_resilience>=4){
731         int end_ok=1;
732                 
733         for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
734             const int mb_xy= s->mb_index2xy[i];
735             int error1= s->error_status_table[mb_xy  ];
736             int error2= s->error_status_table[mb_xy+1];
737         
738             if(error1&VP_START)
739                 end_ok=1;
740              
741             if(   error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
742                && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END) 
743                && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninited
744                 end_ok=0;
745             }
746         
747             if(!end_ok)
748                 s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
749         }
750     }
751     
752 #if 1
753     /* backward mark errors */
754     distance=9999999;
755     for(error_type=1; error_type<=3; error_type++){
756         for(i=s->mb_num-1; i>=0; i--){
757             const int mb_xy= s->mb_index2xy[i];
758             int error= s->error_status_table[mb_xy];
759             
760             if(!s->mbskip_table[mb_xy]) //FIXME partition specific
761                 distance++;            
762             if(error&(1<<error_type))
763                 distance= 0;
764
765             if(s->partitioned_frame){
766                 if(distance < threshold_part[error_type-1])
767                     s->error_status_table[mb_xy]|= 1<<error_type;
768             }else{
769                 if(distance < threshold)
770                     s->error_status_table[mb_xy]|= 1<<error_type;
771             }
772
773             if(error&VP_START)
774                 distance= 9999999;
775         }
776     }
777 #endif
778
779     /* forward mark errors */
780     error=0;
781     for(i=0; i<s->mb_num; i++){
782         const int mb_xy= s->mb_index2xy[i];
783         int old_error= s->error_status_table[mb_xy];
784         
785         if(old_error&VP_START)
786             error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
787         else{
788             error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
789             s->error_status_table[mb_xy]|= error;
790         }
791     }
792 #if 1
793     /* handle not partitioned case */
794     if(!s->partitioned_frame){
795         for(i=0; i<s->mb_num; i++){
796             const int mb_xy= s->mb_index2xy[i];
797             error= s->error_status_table[mb_xy];
798             if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
799                 error|= AC_ERROR|DC_ERROR|MV_ERROR;
800             s->error_status_table[mb_xy]= error;
801         }
802     }
803 #endif
804     is_intra_likely= is_intra_more_likely(s);
805
806     /* set unknown mb-type to most likely */
807     for(i=0; i<s->mb_num; i++){
808         const int mb_xy= s->mb_index2xy[i];
809         error= s->error_status_table[mb_xy];
810         if(!((error&DC_ERROR) && (error&MV_ERROR)))
811             continue;
812
813         if(is_intra_likely)
814             s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
815         else
816             s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
817     }
818     
819     /* handle inter blocks with damaged AC */
820     for(mb_y=0; mb_y<s->mb_height; mb_y++){
821         for(mb_x=0; mb_x<s->mb_width; mb_x++){
822             const int mb_xy= mb_x + mb_y * s->mb_stride;
823             const int mb_type= s->current_picture.mb_type[mb_xy];
824             error= s->error_status_table[mb_xy];
825
826             if(IS_INTRA(mb_type)) continue; //intra
827             if(error&MV_ERROR) continue;              //inter with damaged MV
828             if(!(error&AC_ERROR)) continue;           //undamaged inter
829             
830             s->mv_dir = MV_DIR_FORWARD;
831             s->mb_intra=0;
832             s->mb_skiped=0;
833             if(IS_8X8(mb_type)){
834                 int mb_index= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
835                 int j;
836                 s->mv_type = MV_TYPE_8X8;
837                 for(j=0; j<4; j++){
838                     s->mv[0][j][0] = s->motion_val[ mb_index + (j&1) + (j>>1)*s->block_wrap[0] ][0];
839                     s->mv[0][j][1] = s->motion_val[ mb_index + (j&1) + (j>>1)*s->block_wrap[0] ][1];
840                 }
841             }else{
842                 s->mv_type = MV_TYPE_16X16;
843                 s->mv[0][0][0] = s->motion_val[ mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0] ][0];
844                 s->mv[0][0][1] = s->motion_val[ mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0] ][1];
845             }
846         
847             s->dsp.clear_blocks(s->block[0]);
848
849             s->mb_x= mb_x;
850             s->mb_y= mb_y;
851             MPV_decode_mb(s, s->block);
852         }
853     }
854
855     /* guess MVs */
856     if(s->pict_type==B_TYPE){
857         for(mb_y=0; mb_y<s->mb_height; mb_y++){
858             for(mb_x=0; mb_x<s->mb_width; mb_x++){
859                 int xy= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
860                 const int mb_xy= mb_x + mb_y * s->mb_stride;
861                 const int mb_type= s->current_picture.mb_type[mb_xy];
862                 error= s->error_status_table[mb_xy];
863
864                 if(IS_INTRA(mb_type)) continue;
865                 if(!(error&MV_ERROR)) continue;           //inter with undamaged MV
866                 if(!(error&AC_ERROR)) continue;           //undamaged inter
867             
868                 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
869                 s->mb_intra=0;
870                 s->mv_type = MV_TYPE_16X16;
871                 s->mb_skiped=0;
872                 
873                 if(s->pp_time){
874                     int time_pp= s->pp_time;
875                     int time_pb= s->pb_time;
876             
877                     s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp;
878                     s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp;
879                     s->mv[1][0][0] = s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
880                     s->mv[1][0][1] = s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
881                 }else{
882                     s->mv[0][0][0]= 0;
883                     s->mv[0][0][1]= 0;
884                     s->mv[1][0][0]= 0;
885                     s->mv[1][0][1]= 0;
886                 }
887
888                 s->dsp.clear_blocks(s->block[0]);
889                 s->mb_x= mb_x;
890                 s->mb_y= mb_y;
891                 MPV_decode_mb(s, s->block);
892             }
893         }
894     }else
895         guess_mv(s);
896
897     /* fill DC for inter blocks */
898     for(mb_y=0; mb_y<s->mb_height; mb_y++){
899         for(mb_x=0; mb_x<s->mb_width; mb_x++){
900             int dc, dcu, dcv, y, n;
901             int16_t *dc_ptr;
902             uint8_t *dest_y, *dest_cb, *dest_cr;
903             const int mb_xy= mb_x + mb_y * s->mb_stride;
904             const int mb_type= s->current_picture.mb_type[mb_xy];
905            
906             error= s->error_status_table[mb_xy];
907
908             if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
909 //            if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
910             
911             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
912             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
913             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
914            
915             dc_ptr= &s->dc_val[0][mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2)];
916             for(n=0; n<4; n++){
917                 dc=0;
918                 for(y=0; y<8; y++){
919                     int x;
920                     for(x=0; x<8; x++){
921                        dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
922                     }
923                 }
924                 dc_ptr[(n&1) + (n>>1)*(s->mb_width*2+2)]= (dc+4)>>3;
925             }
926
927             dcu=dcv=0;
928             for(y=0; y<8; y++){
929                 int x;
930                 for(x=0; x<8; x++){
931                     dcu+=dest_cb[x + y*(s->uvlinesize)];
932                     dcv+=dest_cr[x + y*(s->uvlinesize)];
933                 }
934             }
935             s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)]= (dcu+4)>>3;
936             s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)]= (dcv+4)>>3;   
937         }
938     }
939 #if 1
940     /* guess DC for damaged blocks */
941     guess_dc(s, s->dc_val[0] + s->mb_width*2+3, s->mb_width*2, s->mb_height*2, s->mb_width*2+2, 1);
942     guess_dc(s, s->dc_val[1] + s->mb_width  +3, s->mb_width  , s->mb_height  , s->mb_width  +2, 0);
943     guess_dc(s, s->dc_val[2] + s->mb_width  +3, s->mb_width  , s->mb_height  , s->mb_width  +2, 0);
944 #endif   
945     /* filter luma DC */
946     filter181(s->dc_val[0] + s->mb_width*2+3, s->mb_width*2, s->mb_height*2, s->mb_width*2+2);
947     
948 #if 1
949     /* render DC only intra */
950     for(mb_y=0; mb_y<s->mb_height; mb_y++){
951         for(mb_x=0; mb_x<s->mb_width; mb_x++){
952             uint8_t *dest_y, *dest_cb, *dest_cr;
953             const int mb_xy= mb_x + mb_y * s->mb_stride;
954             const int mb_type= s->current_picture.mb_type[mb_xy];
955
956             error= s->error_status_table[mb_xy];
957
958             if(IS_INTER(mb_type)) continue;
959             if(!(error&AC_ERROR)) continue;              //undamaged
960             
961             dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
962             dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
963             dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
964             
965             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
966         }
967     }
968 #endif
969     
970     if(s->avctx->error_concealment&FF_EC_DEBLOCK){
971         /* filter horizontal block boundaries */
972         h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
973         h_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
974         h_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
975
976         /* filter vertical block boundaries */
977         v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
978         v_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
979         v_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
980     }
981
982     /* clean a few tables */
983     for(i=0; i<s->mb_num; i++){
984         const int mb_xy= s->mb_index2xy[i];
985         int error= s->error_status_table[mb_xy];
986         
987         if(s->pict_type!=B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
988             s->mbskip_table[mb_xy]=0;
989         }
990         s->mbintra_table[mb_xy]=1;
991     }    
992 }