comparison error_resilience.c @ 745:25d7fb7c89be libavcodec

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