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