Statistics
| Branch: | Revision:

ffmpeg / libavcodec / error_resilience.c @ bb4fcfa3

History | View | Annotate | Download (40.4 KB)

1
/*
2
 * Error resilience / concealment
3
 *
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
/**
24
 * @file libavcodec/error_resilience.c
25
 * Error resilience / concealment.
26
 */
27

    
28
#include <limits.h>
29

    
30
#include "avcodec.h"
31
#include "dsputil.h"
32
#include "mpegvideo.h"
33
#include "h264.h"
34

    
35
/*
36
 * H264 redefines mb_intra so it is not mistakely used (its uninitialized in h264)
37
 * but error concealment must support both h264 and h263 thus we must undo this
38
 */
39
#undef mb_intra
40

    
41
static void decode_mb(MpegEncContext *s){
42
    s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize  ) + s->mb_x * 16;
43
    s->dest[1] = s->current_picture.data[1] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);
44
    s->dest[2] = s->current_picture.data[2] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);
45

    
46
    MPV_decode_mb(s, s->block);
47
}
48

    
49
/**
50
 * @param stride the number of MVs to get to the next row
51
 * @param mv_step the number of MVs per row or column in a macroblock
52
 */
53
static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride){
54
    if(s->codec_id == CODEC_ID_H264){
55
        H264Context *h= (void*)s;
56
        assert(s->quarter_sample);
57
        *mv_step= 4;
58
        *stride= h->b_stride;
59
    }else{
60
        *mv_step= 2;
61
        *stride= s->b8_stride;
62
    }
63
}
64

    
65
/**
66
 * replaces the current MB with a flat dc only version.
67
 */
68
static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
69
{
70
    int dc, dcu, dcv, y, i;
71
    for(i=0; i<4; i++){
72
        dc= s->dc_val[0][mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*s->b8_stride];
73
        if(dc<0) dc=0;
74
        else if(dc>2040) dc=2040;
75
        for(y=0; y<8; y++){
76
            int x;
77
            for(x=0; x<8; x++){
78
                dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
79
            }
80
        }
81
    }
82
    dcu = s->dc_val[1][mb_x + mb_y*s->mb_stride];
83
    dcv = s->dc_val[2][mb_x + mb_y*s->mb_stride];
84
    if     (dcu<0   ) dcu=0;
85
    else if(dcu>2040) dcu=2040;
86
    if     (dcv<0   ) dcv=0;
87
    else if(dcv>2040) dcv=2040;
88
    for(y=0; y<8; y++){
89
        int x;
90
        for(x=0; x<8; x++){
91
            dest_cb[x + y*(s->uvlinesize)]= dcu/8;
92
            dest_cr[x + y*(s->uvlinesize)]= dcv/8;
93
        }
94
    }
95
}
96

    
97
static void filter181(int16_t *data, int width, int height, int stride){
98
    int x,y;
99

    
100
    /* horizontal filter */
101
    for(y=1; y<height-1; y++){
102
        int prev_dc= data[0 + y*stride];
103

    
104
        for(x=1; x<width-1; x++){
105
            int dc;
106

    
107
            dc= - prev_dc
108
                + data[x     + y*stride]*8
109
                - data[x + 1 + y*stride];
110
            dc= (dc*10923 + 32768)>>16;
111
            prev_dc= data[x + y*stride];
112
            data[x + y*stride]= dc;
113
        }
114
    }
115

    
116
    /* vertical filter */
117
    for(x=1; x<width-1; x++){
118
        int prev_dc= data[x];
119

    
120
        for(y=1; y<height-1; y++){
121
            int dc;
122

    
123
            dc= - prev_dc
124
                + data[x +  y   *stride]*8
125
                - data[x + (y+1)*stride];
126
            dc= (dc*10923 + 32768)>>16;
127
            prev_dc= data[x + y*stride];
128
            data[x + y*stride]= dc;
129
        }
130
    }
131
}
132

    
133
/**
134
 * guess the dc of blocks which do not have an undamaged dc
135
 * @param w     width in 8 pixel blocks
136
 * @param h     height in 8 pixel blocks
137
 */
138
static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
139
    int b_x, b_y;
140

    
141
    for(b_y=0; b_y<h; b_y++){
142
        for(b_x=0; b_x<w; b_x++){
143
            int color[4]={1024,1024,1024,1024};
144
            int distance[4]={9999,9999,9999,9999};
145
            int mb_index, error, j;
146
            int64_t guess, weight_sum;
147

    
148
            mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
149

    
150
            error= s->error_status_table[mb_index];
151

    
152
            if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
153
            if(!(error&DC_ERROR)) continue;           //dc-ok
154

    
155
            /* right block */
156
            for(j=b_x+1; j<w; j++){
157
                int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
158
                int error_j= s->error_status_table[mb_index_j];
159
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
160
                if(intra_j==0 || !(error_j&DC_ERROR)){
161
                    color[0]= dc[j + b_y*stride];
162
                    distance[0]= j-b_x;
163
                    break;
164
                }
165
            }
166

    
167
            /* left block */
168
            for(j=b_x-1; j>=0; j--){
169
                int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
170
                int error_j= s->error_status_table[mb_index_j];
171
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
172
                if(intra_j==0 || !(error_j&DC_ERROR)){
173
                    color[1]= dc[j + b_y*stride];
174
                    distance[1]= b_x-j;
175
                    break;
176
                }
177
            }
178

    
179
            /* bottom block */
180
            for(j=b_y+1; j<h; j++){
181
                int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
182
                int error_j= s->error_status_table[mb_index_j];
183
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
184
                if(intra_j==0 || !(error_j&DC_ERROR)){
185
                    color[2]= dc[b_x + j*stride];
186
                    distance[2]= j-b_y;
187
                    break;
188
                }
189
            }
190

    
191
            /* top block */
192
            for(j=b_y-1; j>=0; j--){
193
                int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
194
                int error_j= s->error_status_table[mb_index_j];
195
                int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
196
                if(intra_j==0 || !(error_j&DC_ERROR)){
197
                    color[3]= dc[b_x + j*stride];
198
                    distance[3]= b_y-j;
199
                    break;
200
                }
201
            }
202

    
203
            weight_sum=0;
204
            guess=0;
205
            for(j=0; j<4; j++){
206
                int64_t weight= 256*256*256*16/distance[j];
207
                guess+= weight*(int64_t)color[j];
208
                weight_sum+= weight;
209
            }
210
            guess= (guess + weight_sum/2) / weight_sum;
211

    
212
            dc[b_x + b_y*stride]= guess;
213
        }
214
    }
215
}
216

    
217
/**
218
 * simple horizontal deblocking filter used for error resilience
219
 * @param w     width in 8 pixel blocks
220
 * @param h     height in 8 pixel blocks
221
 */
222
static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
223
    int b_x, b_y, mvx_stride, mvy_stride;
224
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
225
    set_mv_strides(s, &mvx_stride, &mvy_stride);
226
    mvx_stride >>= is_luma;
227
    mvy_stride *= mvx_stride;
228

    
229
    for(b_y=0; b_y<h; b_y++){
230
        for(b_x=0; b_x<w-1; b_x++){
231
            int y;
232
            int left_status = s->error_status_table[( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride];
233
            int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
234
            int left_intra=   IS_INTRA(s->current_picture.mb_type      [( b_x   >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
235
            int right_intra=  IS_INTRA(s->current_picture.mb_type      [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
236
            int left_damage =  left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
237
            int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
238
            int offset= b_x*8 + b_y*stride*8;
239
            int16_t *left_mv=  s->current_picture.motion_val[0][mvy_stride*b_y + mvx_stride* b_x   ];
240
            int16_t *right_mv= s->current_picture.motion_val[0][mvy_stride*b_y + mvx_stride*(b_x+1)];
241

    
242
            if(!(left_damage||right_damage)) continue; // both undamaged
243

    
244
            if(   (!left_intra) && (!right_intra)
245
               && FFABS(left_mv[0]-right_mv[0]) + FFABS(left_mv[1]+right_mv[1]) < 2) continue;
246

    
247
            for(y=0; y<8; y++){
248
                int a,b,c,d;
249

    
250
                a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
251
                b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
252
                c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
253

    
254
                d= FFABS(b) - ((FFABS(a) + FFABS(c) + 1)>>1);
255
                d= FFMAX(d, 0);
256
                if(b<0) d= -d;
257

    
258
                if(d==0) continue;
259

    
260
                if(!(left_damage && right_damage))
261
                    d= d*16/9;
262

    
263
                if(left_damage){
264
                    dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
265
                    dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
266
                    dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
267
                    dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
268
                }
269
                if(right_damage){
270
                    dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
271
                    dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
272
                    dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
273
                    dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
274
                }
275
            }
276
        }
277
    }
278
}
279

    
280
/**
281
 * simple vertical deblocking filter used for error resilience
282
 * @param w     width in 8 pixel blocks
283
 * @param h     height in 8 pixel blocks
284
 */
285
static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
286
    int b_x, b_y, mvx_stride, mvy_stride;
287
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
288
    set_mv_strides(s, &mvx_stride, &mvy_stride);
289
    mvx_stride >>= is_luma;
290
    mvy_stride *= mvx_stride;
291

    
292
    for(b_y=0; b_y<h-1; b_y++){
293
        for(b_x=0; b_x<w; b_x++){
294
            int x;
295
            int top_status   = s->error_status_table[(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride];
296
            int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
297
            int top_intra=     IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ( b_y   >>is_luma)*s->mb_stride]);
298
            int bottom_intra=  IS_INTRA(s->current_picture.mb_type      [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
299
            int top_damage =      top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
300
            int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
301
            int offset= b_x*8 + b_y*stride*8;
302
            int16_t *top_mv=    s->current_picture.motion_val[0][mvy_stride* b_y    + mvx_stride*b_x];
303
            int16_t *bottom_mv= s->current_picture.motion_val[0][mvy_stride*(b_y+1) + mvx_stride*b_x];
304

    
305
            if(!(top_damage||bottom_damage)) continue; // both undamaged
306

    
307
            if(   (!top_intra) && (!bottom_intra)
308
               && FFABS(top_mv[0]-bottom_mv[0]) + FFABS(top_mv[1]+bottom_mv[1]) < 2) continue;
309

    
310
            for(x=0; x<8; x++){
311
                int a,b,c,d;
312

    
313
                a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
314
                b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
315
                c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
316

    
317
                d= FFABS(b) - ((FFABS(a) + FFABS(c)+1)>>1);
318
                d= FFMAX(d, 0);
319
                if(b<0) d= -d;
320

    
321
                if(d==0) continue;
322

    
323
                if(!(top_damage && bottom_damage))
324
                    d= d*16/9;
325

    
326
                if(top_damage){
327
                    dst[offset + x +  7*stride] = cm[dst[offset + x +  7*stride] + ((d*7)>>4)];
328
                    dst[offset + x +  6*stride] = cm[dst[offset + x +  6*stride] + ((d*5)>>4)];
329
                    dst[offset + x +  5*stride] = cm[dst[offset + x +  5*stride] + ((d*3)>>4)];
330
                    dst[offset + x +  4*stride] = cm[dst[offset + x +  4*stride] + ((d*1)>>4)];
331
                }
332
                if(bottom_damage){
333
                    dst[offset + x +  8*stride] = cm[dst[offset + x +  8*stride] - ((d*7)>>4)];
334
                    dst[offset + x +  9*stride] = cm[dst[offset + x +  9*stride] - ((d*5)>>4)];
335
                    dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
336
                    dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
337
                }
338
            }
339
        }
340
    }
341
}
342

    
343
static void guess_mv(MpegEncContext *s){
344
    uint8_t fixed[s->mb_stride * s->mb_height];
345
#define MV_FROZEN    3
346
#define MV_CHANGED   2
347
#define MV_UNCHANGED 1
348
    const int mb_stride = s->mb_stride;
349
    const int mb_width = s->mb_width;
350
    const int mb_height= s->mb_height;
351
    int i, depth, num_avail;
352
    int mb_x, mb_y, mot_step, mot_stride;
353

    
354
    set_mv_strides(s, &mot_step, &mot_stride);
355

    
356
    num_avail=0;
357
    for(i=0; i<s->mb_num; i++){
358
        const int mb_xy= s->mb_index2xy[ i ];
359
        int f=0;
360
        int error= s->error_status_table[mb_xy];
361

    
362
        if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
363
        if(!(error&MV_ERROR)) f=MV_FROZEN;           //inter with undamaged MV
364

    
365
        fixed[mb_xy]= f;
366
        if(f==MV_FROZEN)
367
            num_avail++;
368
    }
369

    
370
    if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
371
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
372
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
373
                const int mb_xy= mb_x + mb_y*s->mb_stride;
374

    
375
                if(IS_INTRA(s->current_picture.mb_type[mb_xy]))  continue;
376
                if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
377

    
378
                s->mv_dir = s->last_picture.data[0] ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
379
                s->mb_intra=0;
380
                s->mv_type = MV_TYPE_16X16;
381
                s->mb_skipped=0;
382

    
383
                s->dsp.clear_blocks(s->block[0]);
384

    
385
                s->mb_x= mb_x;
386
                s->mb_y= mb_y;
387
                s->mv[0][0][0]= 0;
388
                s->mv[0][0][1]= 0;
389
                decode_mb(s);
390
            }
391
        }
392
        return;
393
    }
394

    
395
    for(depth=0;; depth++){
396
        int changed, pass, none_left;
397

    
398
        none_left=1;
399
        changed=1;
400
        for(pass=0; (changed || pass<2) && pass<10; pass++){
401
            int mb_x, mb_y;
402
int score_sum=0;
403

    
404
            changed=0;
405
            for(mb_y=0; mb_y<s->mb_height; mb_y++){
406
                for(mb_x=0; mb_x<s->mb_width; mb_x++){
407
                    const int mb_xy= mb_x + mb_y*s->mb_stride;
408
                    int mv_predictor[8][2]={{0}};
409
                    int pred_count=0;
410
                    int j;
411
                    int best_score=256*256*256*64;
412
                    int best_pred=0;
413
                    const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
414
                    int prev_x= s->current_picture.motion_val[0][mot_index][0];
415
                    int prev_y= s->current_picture.motion_val[0][mot_index][1];
416

    
417
                    if((mb_x^mb_y^pass)&1) continue;
418

    
419
                    if(fixed[mb_xy]==MV_FROZEN) continue;
420
                    assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
421
                    assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
422

    
423
                    j=0;
424
                    if(mb_x>0           && fixed[mb_xy-1        ]==MV_FROZEN) j=1;
425
                    if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_FROZEN) j=1;
426
                    if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
427
                    if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
428
                    if(j==0) continue;
429

    
430
                    j=0;
431
                    if(mb_x>0           && fixed[mb_xy-1        ]==MV_CHANGED) j=1;
432
                    if(mb_x+1<mb_width  && fixed[mb_xy+1        ]==MV_CHANGED) j=1;
433
                    if(mb_y>0           && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
434
                    if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
435
                    if(j==0 && pass>1) continue;
436

    
437
                    none_left=0;
438

    
439
                    if(mb_x>0 && fixed[mb_xy-1]){
440
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_step][0];
441
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_step][1];
442
                        pred_count++;
443
                    }
444
                    if(mb_x+1<mb_width && fixed[mb_xy+1]){
445
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_step][0];
446
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_step][1];
447
                        pred_count++;
448
                    }
449
                    if(mb_y>0 && fixed[mb_xy-mb_stride]){
450
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*mot_step][0];
451
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*mot_step][1];
452
                        pred_count++;
453
                    }
454
                    if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
455
                        mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*mot_step][0];
456
                        mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*mot_step][1];
457
                        pred_count++;
458
                    }
459
                    if(pred_count==0) continue;
460

    
461
                    if(pred_count>1){
462
                        int sum_x=0, sum_y=0;
463
                        int max_x, max_y, min_x, min_y;
464

    
465
                        for(j=0; j<pred_count; j++){
466
                            sum_x+= mv_predictor[j][0];
467
                            sum_y+= mv_predictor[j][1];
468
                        }
469

    
470
                        /* mean */
471
                        mv_predictor[pred_count][0] = sum_x/j;
472
                        mv_predictor[pred_count][1] = sum_y/j;
473

    
474
                        /* median */
475
                        if(pred_count>=3){
476
                            min_y= min_x= 99999;
477
                            max_y= max_x=-99999;
478
                        }else{
479
                            min_x=min_y=max_x=max_y=0;
480
                        }
481
                        for(j=0; j<pred_count; j++){
482
                            max_x= FFMAX(max_x, mv_predictor[j][0]);
483
                            max_y= FFMAX(max_y, mv_predictor[j][1]);
484
                            min_x= FFMIN(min_x, mv_predictor[j][0]);
485
                            min_y= FFMIN(min_y, mv_predictor[j][1]);
486
                        }
487
                        mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
488
                        mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
489

    
490
                        if(pred_count==4){
491
                            mv_predictor[pred_count+1][0] /= 2;
492
                            mv_predictor[pred_count+1][1] /= 2;
493
                        }
494
                        pred_count+=2;
495
                    }
496

    
497
                    /* zero MV */
498
                    pred_count++;
499

    
500
                    /* last MV */
501
                    mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];
502
                    mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];
503
                    pred_count++;
504

    
505
                    s->mv_dir = MV_DIR_FORWARD;
506
                    s->mb_intra=0;
507
                    s->mv_type = MV_TYPE_16X16;
508
                    s->mb_skipped=0;
509

    
510
                    s->dsp.clear_blocks(s->block[0]);
511

    
512
                    s->mb_x= mb_x;
513
                    s->mb_y= mb_y;
514

    
515
                    for(j=0; j<pred_count; j++){
516
                        int score=0;
517
                        uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
518

    
519
                        s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
520
                        s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
521

    
522
                        decode_mb(s);
523

    
524
                        if(mb_x>0 && fixed[mb_xy-1]){
525
                            int k;
526
                            for(k=0; k<16; k++)
527
                                score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize   ]);
528
                        }
529
                        if(mb_x+1<mb_width && fixed[mb_xy+1]){
530
                            int k;
531
                            for(k=0; k<16; k++)
532
                                score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
533
                        }
534
                        if(mb_y>0 && fixed[mb_xy-mb_stride]){
535
                            int k;
536
                            for(k=0; k<16; k++)
537
                                score += FFABS(src[k-s->linesize   ]-src[k               ]);
538
                        }
539
                        if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
540
                            int k;
541
                            for(k=0; k<16; k++)
542
                                score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
543
                        }
544

    
545
                        if(score <= best_score){ // <= will favor the last MV
546
                            best_score= score;
547
                            best_pred= j;
548
                        }
549
                    }
550
score_sum+= best_score;
551
//FIXME no need to set s->current_picture.motion_val[0][mot_index][0] explicit
552
                    s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
553
                    s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
554

    
555
                    decode_mb(s);
556

    
557

    
558
                    if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
559
                        fixed[mb_xy]=MV_CHANGED;
560
                        changed++;
561
                    }else
562
                        fixed[mb_xy]=MV_UNCHANGED;
563
                }
564
            }
565

    
566
//            printf(".%d/%d", changed, score_sum); fflush(stdout);
567
        }
568

    
569
        if(none_left)
570
            return;
571

    
572
        for(i=0; i<s->mb_num; i++){
573
            int mb_xy= s->mb_index2xy[i];
574
            if(fixed[mb_xy])
575
                fixed[mb_xy]=MV_FROZEN;
576
        }
577
//        printf(":"); fflush(stdout);
578
    }
579
}
580

    
581
static int is_intra_more_likely(MpegEncContext *s){
582
    int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
583

    
584
    if(!s->last_picture_ptr || !s->last_picture_ptr->data[0]) return 1; //no previous frame available -> use spatial prediction
585

    
586
    undamaged_count=0;
587
    for(i=0; i<s->mb_num; i++){
588
        const int mb_xy= s->mb_index2xy[i];
589
        const int error= s->error_status_table[mb_xy];
590
        if(!((error&DC_ERROR) && (error&MV_ERROR)))
591
            undamaged_count++;
592
    }
593

    
594
    if(undamaged_count < 5) return 0; //almost all MBs damaged -> use temporal prediction
595

    
596
    //prevent dsp.sad() check, that requires access to the image
597
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration && s->pict_type == FF_I_TYPE)
598
        return 1;
599

    
600
    skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
601
    is_intra_likely=0;
602

    
603
    j=0;
604
    for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
605
        for(mb_x= 0; mb_x<s->mb_width; mb_x++){
606
            int error;
607
            const int mb_xy= mb_x + mb_y*s->mb_stride;
608

    
609
            error= s->error_status_table[mb_xy];
610
            if((error&DC_ERROR) && (error&MV_ERROR))
611
                continue; //skip damaged
612

    
613
            j++;
614
            if((j%skip_amount) != 0) continue; //skip a few to speed things up
615

    
616
            if(s->pict_type==FF_I_TYPE){
617
                uint8_t *mb_ptr     = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
618
                uint8_t *last_mb_ptr= s->last_picture.data   [0] + mb_x*16 + mb_y*16*s->linesize;
619

    
620
                is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr                    , s->linesize, 16);
621
                is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
622
            }else{
623
                if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
624
                   is_intra_likely++;
625
                else
626
                   is_intra_likely--;
627
            }
628
        }
629
    }
630
//printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
631
    return is_intra_likely > 0;
632
}
633

    
634
void ff_er_frame_start(MpegEncContext *s){
635
    if(!s->error_recognition) return;
636

    
637
    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));
638
    s->error_count= 3*s->mb_num;
639
}
640

    
641
/**
642
 * adds a slice.
643
 * @param endx x component of the last macroblock, can be -1 for the last of the previous line
644
 * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
645
 *               error of the same type occurred
646
 */
647
void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
648
    const int start_i= av_clip(startx + starty * s->mb_width    , 0, s->mb_num-1);
649
    const int end_i  = av_clip(endx   + endy   * s->mb_width    , 0, s->mb_num);
650
    const int start_xy= s->mb_index2xy[start_i];
651
    const int end_xy  = s->mb_index2xy[end_i];
652
    int mask= -1;
653

    
654
    if(s->avctx->hwaccel)
655
        return;
656

    
657
    if(start_i > end_i || start_xy > end_xy){
658
        av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before start\n");
659
        return;
660
    }
661

    
662
    if(!s->error_recognition) return;
663

    
664
    mask &= ~VP_START;
665
    if(status & (AC_ERROR|AC_END)){
666
        mask &= ~(AC_ERROR|AC_END);
667
        s->error_count -= end_i - start_i + 1;
668
    }
669
    if(status & (DC_ERROR|DC_END)){
670
        mask &= ~(DC_ERROR|DC_END);
671
        s->error_count -= end_i - start_i + 1;
672
    }
673
    if(status & (MV_ERROR|MV_END)){
674
        mask &= ~(MV_ERROR|MV_END);
675
        s->error_count -= end_i - start_i + 1;
676
    }
677

    
678
    if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
679

    
680
    if(mask == ~0x7F){
681
        memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
682
    }else{
683
        int i;
684
        for(i=start_xy; i<end_xy; i++){
685
            s->error_status_table[ i ] &= mask;
686
        }
687
    }
688

    
689
    if(end_i == s->mb_num)
690
        s->error_count= INT_MAX;
691
    else{
692
        s->error_status_table[end_xy] &= mask;
693
        s->error_status_table[end_xy] |= status;
694
    }
695

    
696
    s->error_status_table[start_xy] |= VP_START;
697

    
698
    if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
699
        int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
700

    
701
        prev_status &= ~ VP_START;
702
        if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
703
    }
704
}
705

    
706
void ff_er_frame_end(MpegEncContext *s){
707
    int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
708
    int distance;
709
    int threshold_part[4]= {100,100,100};
710
    int threshold= 50;
711
    int is_intra_likely;
712
    int size = s->b8_stride * 2 * s->mb_height;
713
    Picture *pic= s->current_picture_ptr;
714

    
715
    if(!s->error_recognition || s->error_count==0 || s->avctx->lowres ||
716
       s->avctx->hwaccel ||
717
       s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
718
       s->picture_structure != PICT_FRAME || // we dont support ER of field pictures yet, though it should not crash if enabled
719
       s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
720

    
721
    if(s->current_picture.motion_val[0] == NULL){
722
        av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
723

    
724
        for(i=0; i<2; i++){
725
            pic->ref_index[i]= av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
726
            pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
727
            pic->motion_val[i]= pic->motion_val_base[i]+4;
728
        }
729
        pic->motion_subsample_log2= 3;
730
        s->current_picture= *s->current_picture_ptr;
731
    }
732

    
733
    for(i=0; i<2; i++){
734
        if(pic->ref_index[i])
735
            memset(pic->ref_index[i], 0, size * sizeof(uint8_t));
736
    }
737

    
738
    if(s->avctx->debug&FF_DEBUG_ER){
739
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
740
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
741
                int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
742

    
743
                av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
744
            }
745
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
746
        }
747
    }
748

    
749
#if 1
750
    /* handle overlapping slices */
751
    for(error_type=1; error_type<=3; error_type++){
752
        int end_ok=0;
753

    
754
        for(i=s->mb_num-1; i>=0; i--){
755
            const int mb_xy= s->mb_index2xy[i];
756
            int error= s->error_status_table[mb_xy];
757

    
758
            if(error&(1<<error_type))
759
                end_ok=1;
760
            if(error&(8<<error_type))
761
                end_ok=1;
762

    
763
            if(!end_ok)
764
                s->error_status_table[mb_xy]|= 1<<error_type;
765

    
766
            if(error&VP_START)
767
                end_ok=0;
768
        }
769
    }
770
#endif
771
#if 1
772
    /* handle slices with partitions of different length */
773
    if(s->partitioned_frame){
774
        int end_ok=0;
775

    
776
        for(i=s->mb_num-1; i>=0; i--){
777
            const int mb_xy= s->mb_index2xy[i];
778
            int error= s->error_status_table[mb_xy];
779

    
780
            if(error&AC_END)
781
                end_ok=0;
782
            if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
783
                end_ok=1;
784

    
785
            if(!end_ok)
786
                s->error_status_table[mb_xy]|= AC_ERROR;
787

    
788
            if(error&VP_START)
789
                end_ok=0;
790
        }
791
    }
792
#endif
793
    /* handle missing slices */
794
    if(s->error_recognition>=4){
795
        int end_ok=1;
796

    
797
        for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
798
            const int mb_xy= s->mb_index2xy[i];
799
            int error1= s->error_status_table[mb_xy  ];
800
            int error2= s->error_status_table[s->mb_index2xy[i+1]];
801

    
802
            if(error1&VP_START)
803
                end_ok=1;
804

    
805
            if(   error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
806
               && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
807
               && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
808
                end_ok=0;
809
            }
810

    
811
            if(!end_ok)
812
                s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
813
        }
814
    }
815

    
816
#if 1
817
    /* backward mark errors */
818
    distance=9999999;
819
    for(error_type=1; error_type<=3; error_type++){
820
        for(i=s->mb_num-1; i>=0; i--){
821
            const int mb_xy= s->mb_index2xy[i];
822
            int error= s->error_status_table[mb_xy];
823

    
824
            if(!s->mbskip_table[mb_xy]) //FIXME partition specific
825
                distance++;
826
            if(error&(1<<error_type))
827
                distance= 0;
828

    
829
            if(s->partitioned_frame){
830
                if(distance < threshold_part[error_type-1])
831
                    s->error_status_table[mb_xy]|= 1<<error_type;
832
            }else{
833
                if(distance < threshold)
834
                    s->error_status_table[mb_xy]|= 1<<error_type;
835
            }
836

    
837
            if(error&VP_START)
838
                distance= 9999999;
839
        }
840
    }
841
#endif
842

    
843
    /* forward mark errors */
844
    error=0;
845
    for(i=0; i<s->mb_num; i++){
846
        const int mb_xy= s->mb_index2xy[i];
847
        int old_error= s->error_status_table[mb_xy];
848

    
849
        if(old_error&VP_START)
850
            error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
851
        else{
852
            error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
853
            s->error_status_table[mb_xy]|= error;
854
        }
855
    }
856
#if 1
857
    /* handle not partitioned case */
858
    if(!s->partitioned_frame){
859
        for(i=0; i<s->mb_num; i++){
860
            const int mb_xy= s->mb_index2xy[i];
861
            error= s->error_status_table[mb_xy];
862
            if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
863
                error|= AC_ERROR|DC_ERROR|MV_ERROR;
864
            s->error_status_table[mb_xy]= error;
865
        }
866
    }
867
#endif
868

    
869
    dc_error= ac_error= mv_error=0;
870
    for(i=0; i<s->mb_num; i++){
871
        const int mb_xy= s->mb_index2xy[i];
872
        error= s->error_status_table[mb_xy];
873
        if(error&DC_ERROR) dc_error ++;
874
        if(error&AC_ERROR) ac_error ++;
875
        if(error&MV_ERROR) mv_error ++;
876
    }
877
    av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
878

    
879
    is_intra_likely= is_intra_more_likely(s);
880

    
881
    /* set unknown mb-type to most likely */
882
    for(i=0; i<s->mb_num; i++){
883
        const int mb_xy= s->mb_index2xy[i];
884
        error= s->error_status_table[mb_xy];
885
        if(!((error&DC_ERROR) && (error&MV_ERROR)))
886
            continue;
887

    
888
        if(is_intra_likely)
889
            s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
890
        else
891
            s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
892
    }
893

    
894
    // change inter to intra blocks if no reference frames are available
895
    if (!s->last_picture.data[0] && !s->next_picture.data[0])
896
        for(i=0; i<s->mb_num; i++){
897
            const int mb_xy= s->mb_index2xy[i];
898
            if(!IS_INTRA(s->current_picture.mb_type[mb_xy]))
899
                s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
900
        }
901

    
902
    /* handle inter blocks with damaged AC */
903
    for(mb_y=0; mb_y<s->mb_height; mb_y++){
904
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
905
            const int mb_xy= mb_x + mb_y * s->mb_stride;
906
            const int mb_type= s->current_picture.mb_type[mb_xy];
907
            int dir = !s->last_picture.data[0];
908
            error= s->error_status_table[mb_xy];
909

    
910
            if(IS_INTRA(mb_type)) continue; //intra
911
            if(error&MV_ERROR) continue;              //inter with damaged MV
912
            if(!(error&AC_ERROR)) continue;           //undamaged inter
913

    
914
            s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
915
            s->mb_intra=0;
916
            s->mb_skipped=0;
917
            if(IS_8X8(mb_type)){
918
                int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
919
                int j;
920
                s->mv_type = MV_TYPE_8X8;
921
                for(j=0; j<4; j++){
922
                    s->mv[0][j][0] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
923
                    s->mv[0][j][1] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
924
                }
925
            }else{
926
                s->mv_type = MV_TYPE_16X16;
927
                s->mv[0][0][0] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][0];
928
                s->mv[0][0][1] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][1];
929
            }
930

    
931
            s->dsp.clear_blocks(s->block[0]);
932

    
933
            s->mb_x= mb_x;
934
            s->mb_y= mb_y;
935
            decode_mb(s);
936
        }
937
    }
938

    
939
    /* guess MVs */
940
    if(s->pict_type==FF_B_TYPE){
941
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
942
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
943
                int xy= mb_x*2 + mb_y*2*s->b8_stride;
944
                const int mb_xy= mb_x + mb_y * s->mb_stride;
945
                const int mb_type= s->current_picture.mb_type[mb_xy];
946
                error= s->error_status_table[mb_xy];
947

    
948
                if(IS_INTRA(mb_type)) continue;
949
                if(!(error&MV_ERROR)) continue;           //inter with undamaged MV
950
                if(!(error&AC_ERROR)) continue;           //undamaged inter
951

    
952
                s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
953
                if(!s->last_picture.data[0]) s->mv_dir &= ~MV_DIR_FORWARD;
954
                if(!s->next_picture.data[0]) s->mv_dir &= ~MV_DIR_BACKWARD;
955
                s->mb_intra=0;
956
                s->mv_type = MV_TYPE_16X16;
957
                s->mb_skipped=0;
958

    
959
                if(s->pp_time){
960
                    int time_pp= s->pp_time;
961
                    int time_pb= s->pb_time;
962

    
963
                    s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
964
                    s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
965
                    s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
966
                    s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
967
                }else{
968
                    s->mv[0][0][0]= 0;
969
                    s->mv[0][0][1]= 0;
970
                    s->mv[1][0][0]= 0;
971
                    s->mv[1][0][1]= 0;
972
                }
973

    
974
                s->dsp.clear_blocks(s->block[0]);
975
                s->mb_x= mb_x;
976
                s->mb_y= mb_y;
977
                decode_mb(s);
978
            }
979
        }
980
    }else
981
        guess_mv(s);
982

    
983
    /* the filters below are not XvMC compatible, skip them */
984
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
985
        goto ec_clean;
986
    /* fill DC for inter blocks */
987
    for(mb_y=0; mb_y<s->mb_height; mb_y++){
988
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
989
            int dc, dcu, dcv, y, n;
990
            int16_t *dc_ptr;
991
            uint8_t *dest_y, *dest_cb, *dest_cr;
992
            const int mb_xy= mb_x + mb_y * s->mb_stride;
993
            const int mb_type= s->current_picture.mb_type[mb_xy];
994

    
995
            error= s->error_status_table[mb_xy];
996

    
997
            if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
998
//            if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
999

    
1000
            dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
1001
            dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
1002
            dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
1003

    
1004
            dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
1005
            for(n=0; n<4; n++){
1006
                dc=0;
1007
                for(y=0; y<8; y++){
1008
                    int x;
1009
                    for(x=0; x<8; x++){
1010
                       dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
1011
                    }
1012
                }
1013
                dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
1014
            }
1015

    
1016
            dcu=dcv=0;
1017
            for(y=0; y<8; y++){
1018
                int x;
1019
                for(x=0; x<8; x++){
1020
                    dcu+=dest_cb[x + y*(s->uvlinesize)];
1021
                    dcv+=dest_cr[x + y*(s->uvlinesize)];
1022
                }
1023
            }
1024
            s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
1025
            s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
1026
        }
1027
    }
1028
#if 1
1029
    /* guess DC for damaged blocks */
1030
    guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1031
    guess_dc(s, s->dc_val[1], s->mb_width  , s->mb_height  , s->mb_stride, 0);
1032
    guess_dc(s, s->dc_val[2], s->mb_width  , s->mb_height  , s->mb_stride, 0);
1033
#endif
1034
    /* filter luma DC */
1035
    filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
1036

    
1037
#if 1
1038
    /* render DC only intra */
1039
    for(mb_y=0; mb_y<s->mb_height; mb_y++){
1040
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
1041
            uint8_t *dest_y, *dest_cb, *dest_cr;
1042
            const int mb_xy= mb_x + mb_y * s->mb_stride;
1043
            const int mb_type= s->current_picture.mb_type[mb_xy];
1044

    
1045
            error= s->error_status_table[mb_xy];
1046

    
1047
            if(IS_INTER(mb_type)) continue;
1048
            if(!(error&AC_ERROR)) continue;              //undamaged
1049

    
1050
            dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
1051
            dest_cb= s->current_picture.data[1] + mb_x*8  + mb_y*8 *s->uvlinesize;
1052
            dest_cr= s->current_picture.data[2] + mb_x*8  + mb_y*8 *s->uvlinesize;
1053

    
1054
            put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1055
        }
1056
    }
1057
#endif
1058

    
1059
    if(s->avctx->error_concealment&FF_EC_DEBLOCK){
1060
        /* filter horizontal block boundaries */
1061
        h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
1062
        h_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1063
        h_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1064

    
1065
        /* filter vertical block boundaries */
1066
        v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize  , 1);
1067
        v_block_filter(s, s->current_picture.data[1], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1068
        v_block_filter(s, s->current_picture.data[2], s->mb_width  , s->mb_height  , s->uvlinesize, 0);
1069
    }
1070

    
1071
ec_clean:
1072
    /* clean a few tables */
1073
    for(i=0; i<s->mb_num; i++){
1074
        const int mb_xy= s->mb_index2xy[i];
1075
        int error= s->error_status_table[mb_xy];
1076

    
1077
        if(s->pict_type!=FF_B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
1078
            s->mbskip_table[mb_xy]=0;
1079
        }
1080
        s->mbintra_table[mb_xy]=1;
1081
    }
1082
}