2 * Error resilience / concealment
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
22 * @file error_resilience.c
23 * Error resilience / concealment.
28 #include "mpegvideo.h"
32 * replaces the current MB with a flat dc only version.
34 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
36 int dc, dcu, dcv, y, i;
38 dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
40 else if(dc>2040) dc=2040;
44 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
48 dcu = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
49 dcv = s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
51 else if(dcu>2040) dcu=2040;
53 else if(dcv>2040) dcv=2040;
57 dest_cb[x + y*(s->uvlinesize)]= dcu/8;
58 dest_cr[x + y*(s->uvlinesize)]= dcv/8;
63 static void filter181(int16_t *data, int width, int height, int stride){
66 /* horizontal filter */
67 for(y=1; y<height-1; y++){
68 int prev_dc= data[0 + y*stride];
70 for(x=1; x<width-1; x++){
74 + data[x + y*stride]*8
75 - data[x + 1 + y*stride];
76 dc= (dc*10923 + 32768)>>16;
77 prev_dc= data[x + y*stride];
78 data[x + y*stride]= dc;
83 for(x=1; x<width-1; x++){
86 for(y=1; y<height-1; y++){
90 + data[x + y *stride]*8
91 - data[x + (y+1)*stride];
92 dc= (dc*10923 + 32768)>>16;
93 prev_dc= data[x + y*stride];
94 data[x + y*stride]= dc;
100 * guess the dc of blocks which dont have a undamaged dc
101 * @param w width in 8 pixel blocks
102 * @param h height in 8 pixel blocks
104 static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
107 for(b_y=0; b_y<h; b_y++){
108 for(b_x=0; b_x<w; b_x++){
109 int color[4]={1024,1024,1024,1024};
110 int distance[4]={9999,9999,9999,9999};
111 int mb_index, error, j;
112 int64_t guess, weight_sum;
114 mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_width;
116 error= s->error_status_table[mb_index];
118 if(!(s->mb_type[mb_index]&MB_TYPE_INTRA)) continue; //inter
119 if(!(error&DC_ERROR)) continue; //dc-ok
122 for(j=b_x+1; j<w; j++){
123 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_width;
124 int error_j= s->error_status_table[mb_index_j];
125 int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
126 if(intra_j==0 || !(error_j&DC_ERROR)){
127 color[0]= dc[j + b_y*stride];
134 for(j=b_x-1; j>=0; j--){
135 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_width;
136 int error_j= s->error_status_table[mb_index_j];
137 int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
138 if(intra_j==0 || !(error_j&DC_ERROR)){
139 color[1]= dc[j + b_y*stride];
146 for(j=b_y+1; j<h; j++){
147 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_width;
148 int error_j= s->error_status_table[mb_index_j];
149 int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
150 if(intra_j==0 || !(error_j&DC_ERROR)){
151 color[2]= dc[b_x + j*stride];
158 for(j=b_y-1; j>=0; j--){
159 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_width;
160 int error_j= s->error_status_table[mb_index_j];
161 int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
162 if(intra_j==0 || !(error_j&DC_ERROR)){
163 color[3]= dc[b_x + j*stride];
172 int64_t weight= 256*256*256*16/distance[j];
173 guess+= weight*(int64_t)color[j];
176 guess= (guess + weight_sum/2) / weight_sum;
178 dc[b_x + b_y*stride]= guess;
184 * simple horizontal deblocking filter used for error resilience
185 * @param w width in 8 pixel blocks
186 * @param h height in 8 pixel blocks
188 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
190 uint8_t *cm = cropTbl + MAX_NEG_CROP;
192 for(b_y=0; b_y<h; b_y++){
193 for(b_x=0; b_x<w-1; b_x++){
195 int left_status = s->error_status_table[( b_x >>is_luma) + (b_y>>is_luma)*s->mb_width];
196 int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_width];
197 int left_intra= s->mb_type [( b_x >>is_luma) + (b_y>>is_luma)*s->mb_width]&MB_TYPE_INTRA;
198 int right_intra= s->mb_type [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_width]&MB_TYPE_INTRA;
199 int left_damage = left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
200 int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
201 int offset= b_x*8 + b_y*stride*8;
202 int16_t *left_mv= s->motion_val[s->block_wrap[0]*((b_y<<(1-is_luma)) + 1) + ( b_x <<(1-is_luma))];
203 int16_t *right_mv= s->motion_val[s->block_wrap[0]*((b_y<<(1-is_luma)) + 1) + ((b_x+1)<<(1-is_luma))];
205 if(!(left_damage||right_damage)) continue; // both undamaged
207 if( (!left_intra) && (!right_intra)
208 && ABS(left_mv[0]-right_mv[0]) + ABS(left_mv[1]+right_mv[1]) < 2) continue;
213 a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
214 b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
215 c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
217 d= ABS(b) - ((ABS(a) + ABS(c) + 1)>>1);
223 if(!(left_damage && right_damage))
227 dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
228 dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
229 dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
230 dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
233 dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
234 dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
235 dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
236 dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
244 * simple vertical deblocking filter used for error resilience
245 * @param w width in 8 pixel blocks
246 * @param h height in 8 pixel blocks
248 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
250 uint8_t *cm = cropTbl + MAX_NEG_CROP;
252 for(b_y=0; b_y<h-1; b_y++){
253 for(b_x=0; b_x<w; b_x++){
255 int top_status = s->error_status_table[(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_width];
256 int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_width];
257 int top_intra= s->mb_type [(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_width]&MB_TYPE_INTRA;
258 int bottom_intra= s->mb_type [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_width]&MB_TYPE_INTRA;
259 int top_damage = top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
260 int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
261 int offset= b_x*8 + b_y*stride*8;
262 int16_t *top_mv= s->motion_val[s->block_wrap[0]*(( b_y <<(1-is_luma)) + 1) + (b_x<<(1-is_luma))];
263 int16_t *bottom_mv= s->motion_val[s->block_wrap[0]*(((b_y+1)<<(1-is_luma)) + 1) + (b_x<<(1-is_luma))];
265 if(!(top_damage||bottom_damage)) continue; // both undamaged
267 if( (!top_intra) && (!bottom_intra)
268 && ABS(top_mv[0]-bottom_mv[0]) + ABS(top_mv[1]+bottom_mv[1]) < 2) continue;
273 a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
274 b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
275 c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
277 d= ABS(b) - ((ABS(a) + ABS(c)+1)>>1);
283 if(!(top_damage && bottom_damage))
287 dst[offset + x + 7*stride] = cm[dst[offset + x + 7*stride] + ((d*7)>>4)];
288 dst[offset + x + 6*stride] = cm[dst[offset + x + 6*stride] + ((d*5)>>4)];
289 dst[offset + x + 5*stride] = cm[dst[offset + x + 5*stride] + ((d*3)>>4)];
290 dst[offset + x + 4*stride] = cm[dst[offset + x + 4*stride] + ((d*1)>>4)];
293 dst[offset + x + 8*stride] = cm[dst[offset + x + 8*stride] - ((d*7)>>4)];
294 dst[offset + x + 9*stride] = cm[dst[offset + x + 9*stride] - ((d*5)>>4)];
295 dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
296 dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
303 static void guess_mv(MpegEncContext *s){
304 uint8_t fixed[s->mb_num];
307 #define MV_UNCHANGED 1
308 const int mb_width = s->mb_width;
309 const int mb_height= s->mb_height;
310 int i, depth, num_avail;
313 for(i=0; i<s->mb_num; i++){
315 int error= s->error_status_table[i];
317 if(s->mb_type[i]&MB_TYPE_INTRA) f=MV_FROZEN; //intra //FIXME check
318 if(!(error&MV_ERROR)) f=MV_FROZEN; //inter with undamaged MV
325 if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
328 for(mb_y=0; mb_y<s->mb_height; mb_y++){
329 for(mb_x=0; mb_x<s->mb_width; mb_x++){
332 if(s->mb_type[i]&MB_TYPE_INTRA) continue;
333 if(!(s->error_status_table[i]&MV_ERROR)) continue;
335 s->mv_dir = MV_DIR_FORWARD;
337 s->mv_type = MV_TYPE_16X16;
340 s->dsp.clear_blocks(s->block[0]);
346 MPV_decode_mb(s, s->block);
352 for(depth=0;; depth++){
353 int changed, pass, none_left;
357 for(pass=0; (changed || pass<2) && pass<10; pass++){
363 for(mb_y=0; mb_y<s->mb_height; mb_y++){
364 for(mb_x=0; mb_x<s->mb_width; mb_x++){
365 int mv_predictor[8][2]={{0}};
368 int best_score=256*256*256*64;
370 const int mot_stride= mb_width*2+2;
371 const int mot_index= mb_x*2 + 1 + (mb_y*2+1)*mot_stride;
372 int prev_x= s->motion_val[mot_index][0];
373 int prev_y= s->motion_val[mot_index][1];
376 if((mb_x^mb_y^pass)&1) continue;
378 if(fixed[i]==MV_FROZEN) continue;
381 if(mb_x>0 && fixed[i-1 ]==MV_FROZEN) j=1;
382 if(mb_x+1<mb_width && fixed[i+1 ]==MV_FROZEN) j=1;
383 if(mb_y>0 && fixed[i-mb_width]==MV_FROZEN) j=1;
384 if(mb_y+1<mb_height && fixed[i+mb_width]==MV_FROZEN) j=1;
388 if(mb_x>0 && fixed[i-1 ]==MV_CHANGED) j=1;
389 if(mb_x+1<mb_width && fixed[i+1 ]==MV_CHANGED) j=1;
390 if(mb_y>0 && fixed[i-mb_width]==MV_CHANGED) j=1;
391 if(mb_y+1<mb_height && fixed[i+mb_width]==MV_CHANGED) j=1;
392 if(j==0 && pass>1) continue;
396 if(mb_x>0 && fixed[i-1]){
397 mv_predictor[pred_count][0]= s->motion_val[mot_index - 2][0];
398 mv_predictor[pred_count][1]= s->motion_val[mot_index - 2][1];
401 if(mb_x+1<mb_width && fixed[i+1]){
402 mv_predictor[pred_count][0]= s->motion_val[mot_index + 2][0];
403 mv_predictor[pred_count][1]= s->motion_val[mot_index + 2][1];
406 if(mb_y>0 && fixed[i-mb_width]){
407 mv_predictor[pred_count][0]= s->motion_val[mot_index - mot_stride*2][0];
408 mv_predictor[pred_count][1]= s->motion_val[mot_index - mot_stride*2][1];
411 if(mb_y+1<mb_height && fixed[i+mb_width]){
412 mv_predictor[pred_count][0]= s->motion_val[mot_index + mot_stride*2][0];
413 mv_predictor[pred_count][1]= s->motion_val[mot_index + mot_stride*2][1];
416 if(pred_count==0) continue;
419 int sum_x=0, sum_y=0;
420 int max_x, max_y, min_x, min_y;
422 for(j=0; j<pred_count; j++){
423 sum_x+= mv_predictor[j][0];
424 sum_y+= mv_predictor[j][1];
428 mv_predictor[pred_count][0] = sum_x/j;
429 mv_predictor[pred_count][1] = sum_y/j;
436 min_x=min_y=max_x=max_y=0;
438 for(j=0; j<pred_count; j++){
439 max_x= FFMAX(max_x, mv_predictor[j][0]);
440 max_y= FFMAX(max_y, mv_predictor[j][1]);
441 min_x= FFMIN(min_x, mv_predictor[j][0]);
442 min_y= FFMIN(min_y, mv_predictor[j][1]);
444 mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
445 mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
448 mv_predictor[pred_count+1][0] /= 2;
449 mv_predictor[pred_count+1][1] /= 2;
458 mv_predictor[pred_count][0]= s->motion_val[mot_index][0];
459 mv_predictor[pred_count][1]= s->motion_val[mot_index][1];
462 s->mv_dir = MV_DIR_FORWARD;
464 s->mv_type = MV_TYPE_16X16;
467 s->dsp.clear_blocks(s->block[0]);
471 for(j=0; j<pred_count; j++){
473 uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
475 s->motion_val[mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
476 s->motion_val[mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
477 MPV_decode_mb(s, s->block);
479 if(mb_x>0 && fixed[i-1]){
482 score += ABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);
484 if(mb_x+1<mb_width && fixed[i+1]){
487 score += ABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
489 if(mb_y>0 && fixed[i-mb_width]){
492 score += ABS(src[k-s->linesize ]-src[k ]);
494 if(mb_y+1<mb_height && fixed[i+mb_width]){
497 score += ABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
500 if(score <= best_score){ // <= will favor the last MV
505 score_sum+= best_score;
506 //FIXME no need to set s->motion_val[mot_index][0] explicit
507 s->motion_val[mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
508 s->motion_val[mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
510 MPV_decode_mb(s, s->block);
513 if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
517 fixed[i]=MV_UNCHANGED;
521 // printf(".%d/%d", changed, score_sum); fflush(stdout);
527 for(i=0; i<s->mb_num; i++){
531 // printf(":"); fflush(stdout);
535 static int is_intra_more_likely(MpegEncContext *s){
536 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
538 if(s->last_picture_ptr==NULL) return 1; //no previous frame available -> use spatial prediction
541 for(i=0; i<s->mb_num; i++){
542 int error= s->error_status_table[i];
543 if(!((error&DC_ERROR) && (error&MV_ERROR)))
547 if(undamaged_count < 5) return 0; //allmost all MBs damaged -> use temporal prediction
549 skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
554 for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
555 for(mb_x= 0; mb_x<s->mb_width; mb_x++){
559 error= s->error_status_table[i];
560 if((error&DC_ERROR) && (error&MV_ERROR))
561 continue; //skip damaged
564 if((j%skip_amount) != 0) continue; //skip a few to speed things up
566 if(s->pict_type==I_TYPE){
567 uint8_t *mb_ptr = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
568 uint8_t *last_mb_ptr= s->last_picture.data [0] + mb_x*16 + mb_y*16*s->linesize;
570 is_intra_likely += s->dsp.pix_abs16x16(last_mb_ptr, mb_ptr , s->linesize);
571 is_intra_likely -= s->dsp.pix_abs16x16(last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize);
573 if(s->mbintra_table[i]) //HACK (this is allways inited but we should use mb_type[])
580 //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
581 return is_intra_likely > 0;
584 void ff_er_frame_start(MpegEncContext *s){
585 if(!s->error_resilience) return;
587 memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_num*sizeof(uint8_t));
592 * @param endx x component of the last macroblock, can be -1 for the last of the previous line
593 * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
594 * error of the same type occured
596 void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
597 const int start_xy= clip(startx + starty * s->mb_width, 0, s->mb_num-1);
598 const int end_xy = clip(endx + endy * s->mb_width, 0, s->mb_num);
599 const int mb_count= end_xy - start_xy;
602 if(!s->error_resilience) return;
605 if(status & (AC_ERROR|AC_END)) mask &= ~(AC_ERROR|AC_END);
606 if(status & (DC_ERROR|DC_END)) mask &= ~(DC_ERROR|DC_END);
607 if(status & (MV_ERROR|MV_END)) mask &= ~(MV_ERROR|MV_END);
610 memset(&s->error_status_table[start_xy], 0, mb_count * sizeof(uint8_t));
613 for(i=start_xy; i<end_xy; i++){
614 s->error_status_table[i] &= mask;
618 if(end_xy < s->mb_num){
619 s->error_status_table[end_xy] &= mask;
620 s->error_status_table[end_xy] |= status;
623 s->error_status_table[start_xy] |= VP_START;
626 void ff_er_frame_end(MpegEncContext *s){
627 int i, mb_x, mb_y, error, error_type;
629 int threshold_part[4]= {100,100,100};
632 int num_end_markers=0;
634 if(!s->error_resilience) return;
637 for(i=0; i<s->mb_num; i++){
638 int status= s->error_status_table[i];
640 if(status==0) continue;
642 if(status&(DC_ERROR|AC_ERROR|MV_ERROR))
656 if(num_end_markers==0 && error==0)
659 fprintf(stderr, "concealing errors\n");
661 if(s->avctx->debug&FF_DEBUG_ER){
662 for(i=0; i<s->mb_num; i++){
663 int status= s->error_status_table[i];
665 if(i%s->mb_width == 0) printf("\n");
666 printf("%2X ", status);
671 /* handle overlapping slices */
672 for(error_type=1; error_type<=3; error_type++){
675 for(i=s->mb_num-1; i>=0; i--){
676 int error= s->error_status_table[i];
678 if(error&(1<<error_type))
680 if(error&(8<<error_type))
684 s->error_status_table[i]|= 1<<error_type;
692 /* handle slices with partitions of different length */
693 if(s->partitioned_frame){
696 for(i=s->mb_num-1; i>=0; i--){
697 int error= s->error_status_table[i];
701 if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
705 s->error_status_table[i]|= AC_ERROR;
712 /* handle missing slices */
713 if(s->error_resilience>=4){
716 for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
717 int error1= s->error_status_table[i ];
718 int error2= s->error_status_table[i+1];
723 if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
724 && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
725 && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninited
730 s->error_status_table[i]|= DC_ERROR|AC_ERROR|MV_ERROR;
735 /* backward mark errors */
737 for(error_type=1; error_type<=3; error_type++){
738 for(i=s->mb_num-1; i>=0; i--){
739 int error= s->error_status_table[i];
741 if(!s->mbskip_table[i]) //FIXME partition specific
743 if(error&(1<<error_type))
746 if(s->partitioned_frame){
747 if(distance < threshold_part[error_type-1])
748 s->error_status_table[i]|= 1<<error_type;
750 if(distance < threshold)
751 s->error_status_table[i]|= 1<<error_type;
760 /* forward mark errors */
762 for(i=0; i<s->mb_num; i++){
763 int old_error= s->error_status_table[i];
765 if(old_error&VP_START)
766 error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
768 error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
769 s->error_status_table[i]|= error;
773 /* handle not partitioned case */
774 if(!s->partitioned_frame){
775 for(i=0; i<s->mb_num; i++){
776 error= s->error_status_table[i];
777 if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
778 error|= AC_ERROR|DC_ERROR|MV_ERROR;
779 s->error_status_table[i]= error;
783 is_intra_likely= is_intra_more_likely(s);
785 /* set unknown mb-type to most likely */
786 for(i=0; i<s->mb_num; i++){
788 error= s->error_status_table[i];
789 if((error&DC_ERROR) && (error&MV_ERROR))
790 intra= is_intra_likely;
792 intra= s->mbintra_table[i];
795 s->mb_type[i]|= MB_TYPE_INTRA;
797 s->mb_type[i]&= ~MB_TYPE_INTRA;
800 /* handle inter blocks with damaged AC */
802 for(mb_y=0; mb_y<s->mb_height; mb_y++){
803 for(mb_x=0; mb_x<s->mb_width; mb_x++){
805 error= s->error_status_table[i];
807 if(s->mb_type[i]&MB_TYPE_INTRA) continue; //intra
808 if(error&MV_ERROR) continue; //inter with damaged MV
809 if(!(error&AC_ERROR)) continue; //undamaged inter
811 s->mv_dir = MV_DIR_FORWARD;
814 if(s->mb_type[i]&MB_TYPE_INTER4V){
815 int mb_index= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
817 s->mv_type = MV_TYPE_8X8;
819 s->mv[0][j][0] = s->motion_val[ mb_index + (j&1) + (j>>1)*s->block_wrap[0] ][0];
820 s->mv[0][j][1] = s->motion_val[ mb_index + (j&1) + (j>>1)*s->block_wrap[0] ][1];
823 s->mv_type = MV_TYPE_16X16;
824 s->mv[0][0][0] = s->motion_val[ mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0] ][0];
825 s->mv[0][0][1] = s->motion_val[ mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0] ][1];
828 s->dsp.clear_blocks(s->block[0]);
832 MPV_decode_mb(s, s->block);
837 if(s->pict_type==B_TYPE){
839 for(mb_y=0; mb_y<s->mb_height; mb_y++){
840 for(mb_x=0; mb_x<s->mb_width; mb_x++){
841 int xy= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
843 error= s->error_status_table[i];
845 if(s->mb_type[i]&MB_TYPE_INTRA) continue; //intra
846 if(!(error&MV_ERROR)) continue; //inter with undamaged MV
847 if(!(error&AC_ERROR)) continue; //undamaged inter
849 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
851 s->mv_type = MV_TYPE_16X16;
855 int time_pp= s->pp_time;
856 int time_pb= s->pb_time;
858 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp;
859 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp;
860 s->mv[1][0][0] = s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
861 s->mv[1][0][1] = s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
869 s->dsp.clear_blocks(s->block[0]);
872 MPV_decode_mb(s, s->block);
878 /* fill DC for inter blocks */
880 for(mb_y=0; mb_y<s->mb_height; mb_y++){
881 for(mb_x=0; mb_x<s->mb_width; mb_x++){
882 int dc, dcu, dcv, y, n;
884 uint8_t *dest_y, *dest_cb, *dest_cr;
887 error= s->error_status_table[i];
889 if(s->mb_type[i]&MB_TYPE_INTRA) continue; //intra
890 // if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
892 dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
893 dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
894 dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
896 dc_ptr= &s->dc_val[0][mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2)];
902 dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
905 dc_ptr[(n&1) + (n>>1)*(s->mb_width*2+2)]= (dc+4)>>3;
912 dcu+=dest_cb[x + y*(s->uvlinesize)];
913 dcv+=dest_cr[x + y*(s->uvlinesize)];
916 s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)]= (dcu+4)>>3;
917 s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)]= (dcv+4)>>3;
921 /* guess DC for damaged blocks */
922 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);
923 guess_dc(s, s->dc_val[1] + s->mb_width +3, s->mb_width , s->mb_height , s->mb_width +2, 0);
924 guess_dc(s, s->dc_val[2] + s->mb_width +3, s->mb_width , s->mb_height , s->mb_width +2, 0);
927 filter181(s->dc_val[0] + s->mb_width*2+3, s->mb_width*2, s->mb_height*2, s->mb_width*2+2);
930 /* render DC only intra */
932 for(mb_y=0; mb_y<s->mb_height; mb_y++){
933 for(mb_x=0; mb_x<s->mb_width; mb_x++){
934 uint8_t *dest_y, *dest_cb, *dest_cr;
937 error= s->error_status_table[i];
939 if(!(s->mb_type[i]&MB_TYPE_INTRA)) continue; //inter
940 if(!(error&AC_ERROR)) continue; //undamaged
942 dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
943 dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
944 dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
946 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
951 if(s->avctx->error_concealment&FF_EC_DEBLOCK){
952 /* filter horizontal block boundaries */
953 h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
954 h_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
955 h_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
957 /* filter vertical block boundaries */
958 v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
959 v_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
960 v_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
963 /* clean a few tables */
964 for(i=0; i<s->mb_num; i++){
965 int error= s->error_status_table[i];
967 if(s->pict_type!=B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
968 s->mbskip_table[i]=0;
970 s->mbintra_table[i]=1;