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.
30 #include "mpegvideo.h"
34 * replaces the current MB with a flat dc only version.
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)
38 int dc, dcu, dcv, y, i;
40 dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
42 else if(dc>2040) dc=2040;
46 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
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)];
53 else if(dcu>2040) dcu=2040;
55 else if(dcv>2040) dcv=2040;
59 dest_cb[x + y*(s->uvlinesize)]= dcu/8;
60 dest_cr[x + y*(s->uvlinesize)]= dcv/8;
65 static void filter181(int16_t *data, int width, int height, int stride){
68 /* horizontal filter */
69 for(y=1; y<height-1; y++){
70 int prev_dc= data[0 + y*stride];
72 for(x=1; x<width-1; x++){
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;
85 for(x=1; x<width-1; x++){
88 for(y=1; y<height-1; y++){
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;
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
106 static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
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;
116 mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
118 error= s->error_status_table[mb_index];
120 if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
121 if(!(error&DC_ERROR)) continue; //dc-ok
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];
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];
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];
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];
174 int64_t weight= 256*256*256*16/distance[j];
175 guess+= weight*(int64_t)color[j];
178 guess= (guess + weight_sum/2) / weight_sum;
180 dc[b_x + b_y*stride]= guess;
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
190 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
192 uint8_t *cm = cropTbl + MAX_NEG_CROP;
194 for(b_y=0; b_y<h; b_y++){
195 for(b_x=0; b_x<w-1; b_x++){
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))];
207 if(!(left_damage||right_damage)) continue; // both undamaged
209 if( (!left_intra) && (!right_intra)
210 && ABS(left_mv[0]-right_mv[0]) + ABS(left_mv[1]+right_mv[1]) < 2) continue;
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];
219 d= ABS(b) - ((ABS(a) + ABS(c) + 1)>>1);
225 if(!(left_damage && right_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)];
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)];
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
250 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
252 uint8_t *cm = cropTbl + MAX_NEG_CROP;
254 for(b_y=0; b_y<h-1; b_y++){
255 for(b_x=0; b_x<w; b_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))];
267 if(!(top_damage||bottom_damage)) continue; // both undamaged
269 if( (!top_intra) && (!bottom_intra)
270 && ABS(top_mv[0]-bottom_mv[0]) + ABS(top_mv[1]+bottom_mv[1]) < 2) continue;
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];
279 d= ABS(b) - ((ABS(a) + ABS(c)+1)>>1);
285 if(!(top_damage && bottom_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)];
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)];
305 static void guess_mv(MpegEncContext *s){
306 uint8_t fixed[s->mb_stride * s->mb_height];
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;
317 for(i=0; i<s->mb_num; i++){
318 const int mb_xy= s->mb_index2xy[ i ];
320 int error= s->error_status_table[mb_xy];
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
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;
335 if(IS_INTRA(s->current_picture.mb_type[mb_xy])) continue;
336 if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
338 s->mv_dir = MV_DIR_FORWARD;
340 s->mv_type = MV_TYPE_16X16;
343 s->dsp.clear_blocks(s->block[0]);
349 MPV_decode_mb(s, s->block);
355 for(depth=0;; depth++){
356 int changed, pass, none_left;
360 for(pass=0; (changed || pass<2) && pass<10; pass++){
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}};
371 int best_score=256*256*256*64;
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];
378 if((mb_x^mb_y^pass)&1) continue;
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]);
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;
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;
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];
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];
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];
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];
420 if(pred_count==0) continue;
423 int sum_x=0, sum_y=0;
424 int max_x, max_y, min_x, min_y;
426 for(j=0; j<pred_count; j++){
427 sum_x+= mv_predictor[j][0];
428 sum_y+= mv_predictor[j][1];
432 mv_predictor[pred_count][0] = sum_x/j;
433 mv_predictor[pred_count][1] = sum_y/j;
440 min_x=min_y=max_x=max_y=0;
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]);
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;
452 mv_predictor[pred_count+1][0] /= 2;
453 mv_predictor[pred_count+1][1] /= 2;
462 mv_predictor[pred_count][0]= s->motion_val[mot_index][0];
463 mv_predictor[pred_count][1]= s->motion_val[mot_index][1];
466 s->mv_dir = MV_DIR_FORWARD;
468 s->mv_type = MV_TYPE_16X16;
471 s->dsp.clear_blocks(s->block[0]);
476 for(j=0; j<pred_count; j++){
478 uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
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];
483 MPV_decode_mb(s, s->block);
485 if(mb_x>0 && fixed[mb_xy-1]){
488 score += ABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);
490 if(mb_x+1<mb_width && fixed[mb_xy+1]){
493 score += ABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
495 if(mb_y>0 && fixed[mb_xy-mb_stride]){
498 score += ABS(src[k-s->linesize ]-src[k ]);
500 if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
503 score += ABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
506 if(score <= best_score){ // <= will favor the last MV
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];
516 MPV_decode_mb(s, s->block);
519 if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
520 fixed[mb_xy]=MV_CHANGED;
523 fixed[mb_xy]=MV_UNCHANGED;
527 // printf(".%d/%d", changed, score_sum); fflush(stdout);
533 for(i=0; i<s->mb_num; i++){
534 int mb_xy= s->mb_index2xy[i];
536 fixed[mb_xy]=MV_FROZEN;
538 // printf(":"); fflush(stdout);
542 static int is_intra_more_likely(MpegEncContext *s){
543 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
545 if(s->last_picture_ptr==NULL) return 1; //no previous frame available -> use spatial prediction
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)))
555 if(undamaged_count < 5) return 0; //allmost all MBs damaged -> use temporal prediction
557 skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
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++){
564 const int mb_xy= mb_x + mb_y*s->mb_stride;
566 error= s->error_status_table[mb_xy];
567 if((error&DC_ERROR) && (error&MV_ERROR))
568 continue; //skip damaged
571 if((j%skip_amount) != 0) continue; //skip a few to speed things up
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;
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);
580 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
587 //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
588 return is_intra_likely > 0;
591 void ff_er_frame_start(MpegEncContext *s){
592 if(!s->error_resilience) return;
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;
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
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];
611 if(!s->error_resilience) return;
614 if(status & (AC_ERROR|AC_END)){
615 mask &= ~(AC_ERROR|AC_END);
616 s->error_count -= end_i - start_i + 1;
618 if(status & (DC_ERROR|DC_END)){
619 mask &= ~(DC_ERROR|DC_END);
620 s->error_count -= end_i - start_i + 1;
622 if(status & (MV_ERROR|MV_END)){
623 mask &= ~(MV_ERROR|MV_END);
624 s->error_count -= end_i - start_i + 1;
627 if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
630 memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
633 for(i=start_xy; i<end_xy; i++){
634 s->error_status_table[ i ] &= mask;
638 if(end_i == s->mb_num)
639 s->error_count= INT_MAX;
641 s->error_status_table[end_xy] &= mask;
642 s->error_status_table[end_xy] |= status;
645 s->error_status_table[start_xy] |= VP_START;
648 int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
650 prev_status &= ~ VP_START;
651 if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
655 void ff_er_frame_end(MpegEncContext *s){
656 int i, mb_x, mb_y, error, error_type;
658 int threshold_part[4]= {100,100,100};
662 if(!s->error_resilience || s->error_count==0) return;
664 fprintf(stderr, "concealing errors\n");
666 if(s->motion_val == NULL){
667 int size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
669 fprintf(stderr, "Warning MVs not available\n");
671 s->motion_val= av_mallocz(size * 2 * sizeof(int16_t));
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];
679 printf("%2X ", status);
686 /* handle overlapping slices */
687 for(error_type=1; error_type<=3; error_type++){
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];
694 if(error&(1<<error_type))
696 if(error&(8<<error_type))
700 s->error_status_table[mb_xy]|= 1<<error_type;
708 /* handle slices with partitions of different length */
709 if(s->partitioned_frame){
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];
718 if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
722 s->error_status_table[mb_xy]|= AC_ERROR;
729 /* handle missing slices */
730 if(s->error_resilience>=4){
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];
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
748 s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
753 /* backward mark errors */
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];
760 if(!s->mbskip_table[mb_xy]) //FIXME partition specific
762 if(error&(1<<error_type))
765 if(s->partitioned_frame){
766 if(distance < threshold_part[error_type-1])
767 s->error_status_table[mb_xy]|= 1<<error_type;
769 if(distance < threshold)
770 s->error_status_table[mb_xy]|= 1<<error_type;
779 /* forward mark errors */
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];
785 if(old_error&VP_START)
786 error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
788 error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
789 s->error_status_table[mb_xy]|= error;
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;
804 is_intra_likely= is_intra_more_likely(s);
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)))
814 s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
816 s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
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];
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
830 s->mv_dir = MV_DIR_FORWARD;
834 int mb_index= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
836 s->mv_type = MV_TYPE_8X8;
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];
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];
847 s->dsp.clear_blocks(s->block[0]);
851 MPV_decode_mb(s, s->block);
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];
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
868 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
870 s->mv_type = MV_TYPE_16X16;
874 int time_pp= s->pp_time;
875 int time_pb= s->pb_time;
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;
888 s->dsp.clear_blocks(s->block[0]);
891 MPV_decode_mb(s, s->block);
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;
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];
906 error= s->error_status_table[mb_xy];
908 if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
909 // if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
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;
915 dc_ptr= &s->dc_val[0][mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2)];
921 dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
924 dc_ptr[(n&1) + (n>>1)*(s->mb_width*2+2)]= (dc+4)>>3;
931 dcu+=dest_cb[x + y*(s->uvlinesize)];
932 dcv+=dest_cr[x + y*(s->uvlinesize)];
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;
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);
946 filter181(s->dc_val[0] + s->mb_width*2+3, s->mb_width*2, s->mb_height*2, s->mb_width*2+2);
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];
956 error= s->error_status_table[mb_xy];
958 if(IS_INTER(mb_type)) continue;
959 if(!(error&AC_ERROR)) continue; //undamaged
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;
965 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
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);
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);
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];
987 if(s->pict_type!=B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
988 s->mbskip_table[mb_xy]=0;
990 s->mbintra_table[mb_xy]=1;