]> git.sesse.net Git - ffmpeg/blob - libavcodec/error_resilience.c
tiff: check bppcount
[ffmpeg] / libavcodec / error_resilience.c
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
25  * Error resilience / concealment.
26  */
27
28 #include <limits.h>
29
30 #include "avcodec.h"
31 #include "dsputil.h"
32 #include "error_resilience.h"
33 #include "mpegvideo.h"
34 #include "rectangle.h"
35 #include "thread.h"
36
37 /**
38  * @param stride the number of MVs to get to the next row
39  * @param mv_step the number of MVs per row or column in a macroblock
40  */
41 static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
42 {
43     if (s->avctx->codec_id == AV_CODEC_ID_H264) {
44         av_assert0(s->quarter_sample);
45         *mv_step = 4;
46         *stride  = s->mb_width * 4;
47     } else {
48         *mv_step = 2;
49         *stride  = s->b8_stride;
50     }
51 }
52
53 /**
54  * Replace the current MB with a flat dc-only version.
55  */
56 static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
57                    uint8_t *dest_cr, int mb_x, int mb_y)
58 {
59     int *linesize = s->cur_pic->f.linesize;
60     int dc, dcu, dcv, y, i;
61     for (i = 0; i < 4; i++) {
62         dc = s->dc_val[0][mb_x * 2 + (i &  1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
63         if (dc < 0)
64             dc = 0;
65         else if (dc > 2040)
66             dc = 2040;
67         for (y = 0; y < 8; y++) {
68             int x;
69             for (x = 0; x < 8; x++)
70                 dest_y[x + (i &  1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
71         }
72     }
73     dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
74     dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
75     if (dcu < 0)
76         dcu = 0;
77     else if (dcu > 2040)
78         dcu = 2040;
79     if (dcv < 0)
80         dcv = 0;
81     else if (dcv > 2040)
82         dcv = 2040;
83     for (y = 0; y < 8; y++) {
84         int x;
85         for (x = 0; x < 8; x++) {
86             dest_cb[x + y * linesize[1]] = dcu / 8;
87             dest_cr[x + y * linesize[2]] = dcv / 8;
88         }
89     }
90 }
91
92 static void filter181(int16_t *data, int width, int height, int stride)
93 {
94     int x, y;
95
96     /* horizontal filter */
97     for (y = 1; y < height - 1; y++) {
98         int prev_dc = data[0 + y * stride];
99
100         for (x = 1; x < width - 1; x++) {
101             int dc;
102             dc = -prev_dc +
103                  data[x     + y * stride] * 8 -
104                  data[x + 1 + y * stride];
105             dc = (dc * 10923 + 32768) >> 16;
106             prev_dc = data[x + y * stride];
107             data[x + y * stride] = dc;
108         }
109     }
110
111     /* vertical filter */
112     for (x = 1; x < width - 1; x++) {
113         int prev_dc = data[x];
114
115         for (y = 1; y < height - 1; y++) {
116             int dc;
117
118             dc = -prev_dc +
119                  data[x +  y      * stride] * 8 -
120                  data[x + (y + 1) * stride];
121             dc = (dc * 10923 + 32768) >> 16;
122             prev_dc = data[x + y * stride];
123             data[x + y * stride] = dc;
124         }
125     }
126 }
127
128 /**
129  * guess the dc of blocks which do not have an undamaged dc
130  * @param w     width in 8 pixel blocks
131  * @param h     height in 8 pixel blocks
132  */
133 static void guess_dc(ERContext *s, int16_t *dc, int w,
134                      int h, int stride, int is_luma)
135 {
136     int b_x, b_y;
137     int16_t  (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
138     uint32_t (*dist)[4] = av_malloc(stride*h*sizeof(uint32_t)*4);
139
140     if(!col || !dist) {
141         av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
142         goto fail;
143     }
144
145     for(b_y=0; b_y<h; b_y++){
146         int color= 1024;
147         int distance= -1;
148         for(b_x=0; b_x<w; b_x++){
149             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
150             int error_j= s->error_status_table[mb_index_j];
151             int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
152             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
153                 color= dc[b_x + b_y*stride];
154                 distance= b_x;
155             }
156             col [b_x + b_y*stride][1]= color;
157             dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
158         }
159         color= 1024;
160         distance= -1;
161         for(b_x=w-1; b_x>=0; b_x--){
162             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
163             int error_j= s->error_status_table[mb_index_j];
164             int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
165             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
166                 color= dc[b_x + b_y*stride];
167                 distance= b_x;
168             }
169             col [b_x + b_y*stride][0]= color;
170             dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
171         }
172     }
173     for(b_x=0; b_x<w; b_x++){
174         int color= 1024;
175         int distance= -1;
176         for(b_y=0; b_y<h; b_y++){
177             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
178             int error_j= s->error_status_table[mb_index_j];
179             int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
180             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
181                 color= dc[b_x + b_y*stride];
182                 distance= b_y;
183             }
184             col [b_x + b_y*stride][3]= color;
185             dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
186         }
187         color= 1024;
188         distance= -1;
189         for(b_y=h-1; b_y>=0; b_y--){
190             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
191             int error_j= s->error_status_table[mb_index_j];
192             int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
193             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
194                 color= dc[b_x + b_y*stride];
195                 distance= b_y;
196             }
197             col [b_x + b_y*stride][2]= color;
198             dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
199         }
200     }
201
202     for (b_y = 0; b_y < h; b_y++) {
203         for (b_x = 0; b_x < w; b_x++) {
204             int mb_index, error, j;
205             int64_t guess, weight_sum;
206             mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
207             error    = s->error_status_table[mb_index];
208
209             if (IS_INTER(s->cur_pic->f.mb_type[mb_index]))
210                 continue; // inter
211             if (!(error & ER_DC_ERROR))
212                 continue; // dc-ok
213
214             weight_sum = 0;
215             guess      = 0;
216             for (j = 0; j < 4; j++) {
217                 int64_t weight  = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
218                 guess          += weight*(int64_t)col[b_x + b_y*stride][j];
219                 weight_sum     += weight;
220             }
221             guess = (guess + weight_sum / 2) / weight_sum;
222             dc[b_x + b_y * stride] = guess;
223         }
224     }
225
226 fail:
227     av_freep(&col);
228     av_freep(&dist);
229 }
230
231 /**
232  * simple horizontal deblocking filter used for error resilience
233  * @param w     width in 8 pixel blocks
234  * @param h     height in 8 pixel blocks
235  */
236 static void h_block_filter(ERContext *s, uint8_t *dst, int w,
237                            int h, int stride, int is_luma)
238 {
239     int b_x, b_y, mvx_stride, mvy_stride;
240     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
241     set_mv_strides(s, &mvx_stride, &mvy_stride);
242     mvx_stride >>= is_luma;
243     mvy_stride *= mvx_stride;
244
245     for (b_y = 0; b_y < h; b_y++) {
246         for (b_x = 0; b_x < w - 1; b_x++) {
247             int y;
248             int left_status  = s->error_status_table[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride];
249             int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
250             int left_intra   = IS_INTRA(s->cur_pic->f.mb_type[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
251             int right_intra  = IS_INTRA(s->cur_pic->f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
252             int left_damage  = left_status & ER_MB_ERROR;
253             int right_damage = right_status & ER_MB_ERROR;
254             int offset       = b_x * 8 + b_y * stride * 8;
255             int16_t *left_mv  = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride *  b_x];
256             int16_t *right_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
257             if (!(left_damage || right_damage))
258                 continue; // both undamaged
259             if ((!left_intra) && (!right_intra) &&
260                 FFABS(left_mv[0] - right_mv[0]) +
261                 FFABS(left_mv[1] + right_mv[1]) < 2)
262                 continue;
263
264             for (y = 0; y < 8; y++) {
265                 int a, b, c, d;
266
267                 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
268                 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
269                 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
270
271                 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
272                 d = FFMAX(d, 0);
273                 if (b < 0)
274                     d = -d;
275
276                 if (d == 0)
277                     continue;
278
279                 if (!(left_damage && right_damage))
280                     d = d * 16 / 9;
281
282                 if (left_damage) {
283                     dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
284                     dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
285                     dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
286                     dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
287                 }
288                 if (right_damage) {
289                     dst[offset + 8 + y * stride] = cm[dst[offset +  8 + y * stride] - ((d * 7) >> 4)];
290                     dst[offset + 9 + y * stride] = cm[dst[offset +  9 + y * stride] - ((d * 5) >> 4)];
291                     dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
292                     dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
293                 }
294             }
295         }
296     }
297 }
298
299 /**
300  * simple vertical deblocking filter used for error resilience
301  * @param w     width in 8 pixel blocks
302  * @param h     height in 8 pixel blocks
303  */
304 static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
305                            int stride, int is_luma)
306 {
307     int b_x, b_y, mvx_stride, mvy_stride;
308     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
309     set_mv_strides(s, &mvx_stride, &mvy_stride);
310     mvx_stride >>= is_luma;
311     mvy_stride *= mvx_stride;
312
313     for (b_y = 0; b_y < h - 1; b_y++) {
314         for (b_x = 0; b_x < w; b_x++) {
315             int x;
316             int top_status    = s->error_status_table[(b_x >> is_luma) +  (b_y      >> is_luma) * s->mb_stride];
317             int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
318             int top_intra     = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ( b_y      >> is_luma) * s->mb_stride]);
319             int bottom_intra  = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
320             int top_damage    = top_status & ER_MB_ERROR;
321             int bottom_damage = bottom_status & ER_MB_ERROR;
322             int offset        = b_x * 8 + b_y * stride * 8;
323
324             int16_t *top_mv    = s->cur_pic->f.motion_val[0][mvy_stride *  b_y      + mvx_stride * b_x];
325             int16_t *bottom_mv = s->cur_pic->f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
326
327             if (!(top_damage || bottom_damage))
328                 continue; // both undamaged
329
330             if ((!top_intra) && (!bottom_intra) &&
331                 FFABS(top_mv[0] - bottom_mv[0]) +
332                 FFABS(top_mv[1] + bottom_mv[1]) < 2)
333                 continue;
334
335             for (x = 0; x < 8; x++) {
336                 int a, b, c, d;
337
338                 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
339                 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
340                 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
341
342                 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
343                 d = FFMAX(d, 0);
344                 if (b < 0)
345                     d = -d;
346
347                 if (d == 0)
348                     continue;
349
350                 if (!(top_damage && bottom_damage))
351                     d = d * 16 / 9;
352
353                 if (top_damage) {
354                     dst[offset + x +  7 * stride] = cm[dst[offset + x +  7 * stride] + ((d * 7) >> 4)];
355                     dst[offset + x +  6 * stride] = cm[dst[offset + x +  6 * stride] + ((d * 5) >> 4)];
356                     dst[offset + x +  5 * stride] = cm[dst[offset + x +  5 * stride] + ((d * 3) >> 4)];
357                     dst[offset + x +  4 * stride] = cm[dst[offset + x +  4 * stride] + ((d * 1) >> 4)];
358                 }
359                 if (bottom_damage) {
360                     dst[offset + x +  8 * stride] = cm[dst[offset + x +  8 * stride] - ((d * 7) >> 4)];
361                     dst[offset + x +  9 * stride] = cm[dst[offset + x +  9 * stride] - ((d * 5) >> 4)];
362                     dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
363                     dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
364                 }
365             }
366         }
367     }
368 }
369
370 static void guess_mv(ERContext *s)
371 {
372     uint8_t *fixed = s->er_temp_buffer;
373 #define MV_FROZEN    3
374 #define MV_CHANGED   2
375 #define MV_UNCHANGED 1
376     const int mb_stride = s->mb_stride;
377     const int mb_width  = s->mb_width;
378     const int mb_height = s->mb_height;
379     int i, depth, num_avail;
380     int mb_x, mb_y, mot_step, mot_stride;
381
382     set_mv_strides(s, &mot_step, &mot_stride);
383
384     num_avail = 0;
385     for (i = 0; i < s->mb_num; i++) {
386         const int mb_xy = s->mb_index2xy[i];
387         int f = 0;
388         int error = s->error_status_table[mb_xy];
389
390         if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
391             f = MV_FROZEN; // intra // FIXME check
392         if (!(error & ER_MV_ERROR))
393             f = MV_FROZEN; // inter with undamaged MV
394
395         fixed[mb_xy] = f;
396         if (f == MV_FROZEN)
397             num_avail++;
398         else if(s->last_pic->f.data[0] && s->last_pic->f.motion_val[0]){
399             const int mb_y= mb_xy / s->mb_stride;
400             const int mb_x= mb_xy % s->mb_stride;
401             const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
402             s->cur_pic->f.motion_val[0][mot_index][0]= s->last_pic->f.motion_val[0][mot_index][0];
403             s->cur_pic->f.motion_val[0][mot_index][1]= s->last_pic->f.motion_val[0][mot_index][1];
404             s->cur_pic->f.ref_index[0][4*mb_xy]      = s->last_pic->f.ref_index[0][4*mb_xy];
405         }
406     }
407
408     if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
409         num_avail <= mb_width / 2) {
410         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
411             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
412                 const int mb_xy = mb_x + mb_y * s->mb_stride;
413                 int mv_dir = (s->last_pic && s->last_pic->f.data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
414
415                 if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
416                     continue;
417                 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
418                     continue;
419
420                 s->mv[0][0][0] = 0;
421                 s->mv[0][0][1] = 0;
422                 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
423                              mb_x, mb_y, 0, 0);
424             }
425         }
426         return;
427     }
428
429     for (depth = 0; ; depth++) {
430         int changed, pass, none_left;
431
432         none_left = 1;
433         changed   = 1;
434         for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
435             int mb_x, mb_y;
436             int score_sum = 0;
437
438             changed = 0;
439             for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
440                 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
441                     const int mb_xy        = mb_x + mb_y * s->mb_stride;
442                     int mv_predictor[8][2] = { { 0 } };
443                     int ref[8]             = { 0 };
444                     int pred_count         = 0;
445                     int j;
446                     int best_score         = 256 * 256 * 256 * 64;
447                     int best_pred          = 0;
448                     const int mot_index    = (mb_x + mb_y * mot_stride) * mot_step;
449                     int prev_x, prev_y, prev_ref;
450
451                     if ((mb_x ^ mb_y ^ pass) & 1)
452                         continue;
453
454                     if (fixed[mb_xy] == MV_FROZEN)
455                         continue;
456                     av_assert1(!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]));
457                     av_assert1(s->last_pic && s->last_pic->f.data[0]);
458
459                     j = 0;
460                     if (mb_x > 0             && fixed[mb_xy - 1]         == MV_FROZEN)
461                         j = 1;
462                     if (mb_x + 1 < mb_width  && fixed[mb_xy + 1]         == MV_FROZEN)
463                         j = 1;
464                     if (mb_y > 0             && fixed[mb_xy - mb_stride] == MV_FROZEN)
465                         j = 1;
466                     if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
467                         j = 1;
468                     if (j == 0)
469                         continue;
470
471                     j = 0;
472                     if (mb_x > 0             && fixed[mb_xy - 1        ] == MV_CHANGED)
473                         j = 1;
474                     if (mb_x + 1 < mb_width  && fixed[mb_xy + 1        ] == MV_CHANGED)
475                         j = 1;
476                     if (mb_y > 0             && fixed[mb_xy - mb_stride] == MV_CHANGED)
477                         j = 1;
478                     if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
479                         j = 1;
480                     if (j == 0 && pass > 1)
481                         continue;
482
483                     none_left = 0;
484
485                     if (mb_x > 0 && fixed[mb_xy - 1]) {
486                         mv_predictor[pred_count][0] =
487                             s->cur_pic->f.motion_val[0][mot_index - mot_step][0];
488                         mv_predictor[pred_count][1] =
489                             s->cur_pic->f.motion_val[0][mot_index - mot_step][1];
490                         ref[pred_count] =
491                             s->cur_pic->f.ref_index[0][4 * (mb_xy - 1)];
492                         pred_count++;
493                     }
494                     if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
495                         mv_predictor[pred_count][0] =
496                             s->cur_pic->f.motion_val[0][mot_index + mot_step][0];
497                         mv_predictor[pred_count][1] =
498                             s->cur_pic->f.motion_val[0][mot_index + mot_step][1];
499                         ref[pred_count] =
500                             s->cur_pic->f.ref_index[0][4 * (mb_xy + 1)];
501                         pred_count++;
502                     }
503                     if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
504                         mv_predictor[pred_count][0] =
505                             s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][0];
506                         mv_predictor[pred_count][1] =
507                             s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][1];
508                         ref[pred_count] =
509                             s->cur_pic->f.ref_index[0][4 * (mb_xy - s->mb_stride)];
510                         pred_count++;
511                     }
512                     if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
513                         mv_predictor[pred_count][0] =
514                             s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][0];
515                         mv_predictor[pred_count][1] =
516                             s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][1];
517                         ref[pred_count] =
518                             s->cur_pic->f.ref_index[0][4 * (mb_xy + s->mb_stride)];
519                         pred_count++;
520                     }
521                     if (pred_count == 0)
522                         continue;
523
524                     if (pred_count > 1) {
525                         int sum_x = 0, sum_y = 0, sum_r = 0;
526                         int max_x, max_y, min_x, min_y, max_r, min_r;
527
528                         for (j = 0; j < pred_count; j++) {
529                             sum_x += mv_predictor[j][0];
530                             sum_y += mv_predictor[j][1];
531                             sum_r += ref[j];
532                             if (j && ref[j] != ref[j - 1])
533                                 goto skip_mean_and_median;
534                         }
535
536                         /* mean */
537                         mv_predictor[pred_count][0] = sum_x / j;
538                         mv_predictor[pred_count][1] = sum_y / j;
539                                  ref[pred_count]    = sum_r / j;
540
541                         /* median */
542                         if (pred_count >= 3) {
543                             min_y = min_x = min_r =  99999;
544                             max_y = max_x = max_r = -99999;
545                         } else {
546                             min_x = min_y = max_x = max_y = min_r = max_r = 0;
547                         }
548                         for (j = 0; j < pred_count; j++) {
549                             max_x = FFMAX(max_x, mv_predictor[j][0]);
550                             max_y = FFMAX(max_y, mv_predictor[j][1]);
551                             max_r = FFMAX(max_r, ref[j]);
552                             min_x = FFMIN(min_x, mv_predictor[j][0]);
553                             min_y = FFMIN(min_y, mv_predictor[j][1]);
554                             min_r = FFMIN(min_r, ref[j]);
555                         }
556                         mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
557                         mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
558                                  ref[pred_count + 1]    = sum_r - max_r - min_r;
559
560                         if (pred_count == 4) {
561                             mv_predictor[pred_count + 1][0] /= 2;
562                             mv_predictor[pred_count + 1][1] /= 2;
563                                      ref[pred_count + 1]    /= 2;
564                         }
565                         pred_count += 2;
566                     }
567
568 skip_mean_and_median:
569                     /* zero MV */
570                     pred_count++;
571
572                     if (!fixed[mb_xy] && 0) {
573                         if (s->avctx->codec_id == AV_CODEC_ID_H264) {
574                             // FIXME
575                         } else {
576                             ff_thread_await_progress(&s->last_pic->f,
577                                                      mb_y, 0);
578                         }
579                         if (!s->last_pic->f.motion_val[0] ||
580                             !s->last_pic->f.ref_index[0])
581                             goto skip_last_mv;
582                         prev_x   = s->last_pic->f.motion_val[0][mot_index][0];
583                         prev_y   = s->last_pic->f.motion_val[0][mot_index][1];
584                         prev_ref = s->last_pic->f.ref_index[0][4 * mb_xy];
585                     } else {
586                         prev_x   = s->cur_pic->f.motion_val[0][mot_index][0];
587                         prev_y   = s->cur_pic->f.motion_val[0][mot_index][1];
588                         prev_ref = s->cur_pic->f.ref_index[0][4 * mb_xy];
589                     }
590
591                     /* last MV */
592                     mv_predictor[pred_count][0] = prev_x;
593                     mv_predictor[pred_count][1] = prev_y;
594                              ref[pred_count]    = prev_ref;
595                     pred_count++;
596
597 skip_last_mv:
598
599                     for (j = 0; j < pred_count; j++) {
600                         int *linesize = s->cur_pic->f.linesize;
601                         int score = 0;
602                         uint8_t *src = s->cur_pic->f.data[0] +
603                                        mb_x * 16 + mb_y * 16 * linesize[0];
604
605                         s->cur_pic->f.motion_val[0][mot_index][0] =
606                             s->mv[0][0][0] = mv_predictor[j][0];
607                         s->cur_pic->f.motion_val[0][mot_index][1] =
608                             s->mv[0][0][1] = mv_predictor[j][1];
609
610                         // predictor intra or otherwise not available
611                         if (ref[j] < 0)
612                             continue;
613
614                         s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
615                                      MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
616
617                         if (mb_x > 0 && fixed[mb_xy - 1]) {
618                             int k;
619                             for (k = 0; k < 16; k++)
620                                 score += FFABS(src[k * linesize[0] - 1] -
621                                                src[k * linesize[0]]);
622                         }
623                         if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
624                             int k;
625                             for (k = 0; k < 16; k++)
626                                 score += FFABS(src[k * linesize[0] + 15] -
627                                                src[k * linesize[0] + 16]);
628                         }
629                         if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
630                             int k;
631                             for (k = 0; k < 16; k++)
632                                 score += FFABS(src[k - linesize[0]] - src[k]);
633                         }
634                         if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
635                             int k;
636                             for (k = 0; k < 16; k++)
637                                 score += FFABS(src[k + linesize[0] * 15] -
638                                                src[k + linesize[0] * 16]);
639                         }
640
641                         if (score <= best_score) { // <= will favor the last MV
642                             best_score = score;
643                             best_pred  = j;
644                         }
645                     }
646                     score_sum += best_score;
647                     s->mv[0][0][0] = mv_predictor[best_pred][0];
648                     s->mv[0][0][1] = mv_predictor[best_pred][1];
649
650                     for (i = 0; i < mot_step; i++)
651                         for (j = 0; j < mot_step; j++) {
652                             s->cur_pic->f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
653                             s->cur_pic->f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
654                         }
655
656                     s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
657                                  MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
658
659
660                     if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
661                         fixed[mb_xy] = MV_CHANGED;
662                         changed++;
663                     } else
664                         fixed[mb_xy] = MV_UNCHANGED;
665                 }
666             }
667         }
668
669         if (none_left)
670             return;
671
672         for (i = 0; i < s->mb_num; i++) {
673             int mb_xy = s->mb_index2xy[i];
674             if (fixed[mb_xy])
675                 fixed[mb_xy] = MV_FROZEN;
676         }
677     }
678 }
679
680 static int is_intra_more_likely(ERContext *s)
681 {
682     int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
683
684     if (!s->last_pic || !s->last_pic->f.data[0])
685         return 1; // no previous frame available -> use spatial prediction
686
687     undamaged_count = 0;
688     for (i = 0; i < s->mb_num; i++) {
689         const int mb_xy = s->mb_index2xy[i];
690         const int error = s->error_status_table[mb_xy];
691         if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
692             undamaged_count++;
693     }
694
695     if (s->avctx->codec_id == AV_CODEC_ID_H264 && s->ref_count <= 0)
696         return 1;
697
698     if (undamaged_count < 5)
699         return 0; // almost all MBs damaged -> use temporal prediction
700
701     // prevent dsp.sad() check, that requires access to the image
702     if (CONFIG_MPEG_XVMC_DECODER    &&
703         s->avctx->xvmc_acceleration &&
704         s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I)
705         return 1;
706
707     skip_amount     = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
708     is_intra_likely = 0;
709
710     j = 0;
711     for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
712         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
713             int error;
714             const int mb_xy = mb_x + mb_y * s->mb_stride;
715
716             error = s->error_status_table[mb_xy];
717             if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
718                 continue; // skip damaged
719
720             j++;
721             // skip a few to speed things up
722             if ((j % skip_amount) != 0)
723                 continue;
724
725             if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I) {
726                 int *linesize = s->cur_pic->f.linesize;
727                 uint8_t *mb_ptr      = s->cur_pic->f.data[0] +
728                                        mb_x * 16 + mb_y * 16 * linesize[0];
729                 uint8_t *last_mb_ptr = s->last_pic->f.data[0] +
730                                        mb_x * 16 + mb_y * 16 * linesize[0];
731
732                 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
733                     // FIXME
734                 } else {
735                     ff_thread_await_progress(&s->last_pic->f, mb_y, 0);
736                 }
737                 is_intra_likely += s->dsp->sad[0](NULL, last_mb_ptr, mb_ptr,
738                                                  linesize[0], 16);
739                 // FIXME need await_progress() here
740                 is_intra_likely -= s->dsp->sad[0](NULL, last_mb_ptr,
741                                                  last_mb_ptr + linesize[0] * 16,
742                                                  linesize[0], 16);
743             } else {
744                 if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
745                    is_intra_likely++;
746                 else
747                    is_intra_likely--;
748             }
749         }
750     }
751     // printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
752     return is_intra_likely > 0;
753 }
754
755 void ff_er_frame_start(ERContext *s)
756 {
757     if (!s->avctx->err_recognition)
758         return;
759
760     memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
761            s->mb_stride * s->mb_height * sizeof(uint8_t));
762     s->error_count    = 3 * s->mb_num;
763     s->error_occurred = 0;
764 }
765
766 /**
767  * Add a slice.
768  * @param endx   x component of the last macroblock, can be -1
769  *               for the last of the previous line
770  * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
771  *               assumed that no earlier end or error of the same type occurred
772  */
773 void ff_er_add_slice(ERContext *s, int startx, int starty,
774                      int endx, int endy, int status)
775 {
776     const int start_i  = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
777     const int end_i    = av_clip(endx   + endy   * s->mb_width, 0, s->mb_num);
778     const int start_xy = s->mb_index2xy[start_i];
779     const int end_xy   = s->mb_index2xy[end_i];
780     int mask           = -1;
781
782     if (s->avctx->hwaccel)
783         return;
784
785     if (start_i > end_i || start_xy > end_xy) {
786         av_log(s->avctx, AV_LOG_ERROR,
787                "internal error, slice end before start\n");
788         return;
789     }
790
791     if (!s->avctx->err_recognition)
792         return;
793
794     mask &= ~VP_START;
795     if (status & (ER_AC_ERROR | ER_AC_END)) {
796         mask           &= ~(ER_AC_ERROR | ER_AC_END);
797         s->error_count -= end_i - start_i + 1;
798     }
799     if (status & (ER_DC_ERROR | ER_DC_END)) {
800         mask           &= ~(ER_DC_ERROR | ER_DC_END);
801         s->error_count -= end_i - start_i + 1;
802     }
803     if (status & (ER_MV_ERROR | ER_MV_END)) {
804         mask           &= ~(ER_MV_ERROR | ER_MV_END);
805         s->error_count -= end_i - start_i + 1;
806     }
807
808     if (status & ER_MB_ERROR) {
809         s->error_occurred = 1;
810         s->error_count    = INT_MAX;
811     }
812
813     if (mask == ~0x7F) {
814         memset(&s->error_status_table[start_xy], 0,
815                (end_xy - start_xy) * sizeof(uint8_t));
816     } else {
817         int i;
818         for (i = start_xy; i < end_xy; i++)
819             s->error_status_table[i] &= mask;
820     }
821
822     if (end_i == s->mb_num)
823         s->error_count = INT_MAX;
824     else {
825         s->error_status_table[end_xy] &= mask;
826         s->error_status_table[end_xy] |= status;
827     }
828
829     s->error_status_table[start_xy] |= VP_START;
830
831     if (start_xy > 0 && s->avctx->thread_count <= 1 &&
832         s->avctx->skip_top * s->mb_width < start_i) {
833         int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
834
835         prev_status &= ~ VP_START;
836         if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
837             s->error_count = INT_MAX;
838     }
839 }
840
841 void ff_er_frame_end(ERContext *s)
842 {
843     int *linesize = s->cur_pic->f.linesize;
844     int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
845     int distance;
846     int threshold_part[4] = { 100, 100, 100 };
847     int threshold = 50;
848     int is_intra_likely;
849     int size = s->b8_stride * 2 * s->mb_height;
850
851     /* We do not support ER of field pictures yet,
852      * though it should not crash if enabled. */
853     if (!s->avctx->err_recognition || s->error_count == 0              ||
854         s->avctx->lowres                                               ||
855         s->avctx->hwaccel                                              ||
856         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU          ||
857         !s->cur_pic || s->cur_pic->field_picture                               ||
858         s->error_count == 3 * s->mb_width *
859                           (s->avctx->skip_top + s->avctx->skip_bottom)) {
860         return;
861     }
862
863     if (s->cur_pic->f.motion_val[0] == NULL) {
864         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
865
866         for (i = 0; i < 2; i++) {
867             s->cur_pic->f.ref_index[i]     = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
868             s->cur_pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
869             s->cur_pic->f.motion_val[i]    = s->cur_pic->motion_val_base[i] + 4;
870         }
871         s->cur_pic->f.motion_subsample_log2 = 3;
872     }
873
874     if (s->avctx->debug & FF_DEBUG_ER) {
875         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
876             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
877                 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
878
879                 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
880             }
881             av_log(s->avctx, AV_LOG_DEBUG, "\n");
882         }
883     }
884
885 #if 1
886     /* handle overlapping slices */
887     for (error_type = 1; error_type <= 3; error_type++) {
888         int end_ok = 0;
889
890         for (i = s->mb_num - 1; i >= 0; i--) {
891             const int mb_xy = s->mb_index2xy[i];
892             int error       = s->error_status_table[mb_xy];
893
894             if (error & (1 << error_type))
895                 end_ok = 1;
896             if (error & (8 << error_type))
897                 end_ok = 1;
898
899             if (!end_ok)
900                 s->error_status_table[mb_xy] |= 1 << error_type;
901
902             if (error & VP_START)
903                 end_ok = 0;
904         }
905     }
906 #endif
907 #if 1
908     /* handle slices with partitions of different length */
909     if (s->partitioned_frame) {
910         int end_ok = 0;
911
912         for (i = s->mb_num - 1; i >= 0; i--) {
913             const int mb_xy = s->mb_index2xy[i];
914             int error       = s->error_status_table[mb_xy];
915
916             if (error & ER_AC_END)
917                 end_ok = 0;
918             if ((error & ER_MV_END) ||
919                 (error & ER_DC_END) ||
920                 (error & ER_AC_ERROR))
921                 end_ok = 1;
922
923             if (!end_ok)
924                 s->error_status_table[mb_xy]|= ER_AC_ERROR;
925
926             if (error & VP_START)
927                 end_ok = 0;
928         }
929     }
930 #endif
931     /* handle missing slices */
932     if (s->avctx->err_recognition & AV_EF_EXPLODE) {
933         int end_ok = 1;
934
935         // FIXME + 100 hack
936         for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
937             const int mb_xy = s->mb_index2xy[i];
938             int error1 = s->error_status_table[mb_xy];
939             int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
940
941             if (error1 & VP_START)
942                 end_ok = 1;
943
944             if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
945                 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
946                 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
947                 (error1 & ER_MV_END))) {
948                 // end & uninit
949                 end_ok = 0;
950             }
951
952             if (!end_ok)
953                 s->error_status_table[mb_xy] |= ER_MB_ERROR;
954         }
955     }
956
957 #if 1
958     /* backward mark errors */
959     distance = 9999999;
960     for (error_type = 1; error_type <= 3; error_type++) {
961         for (i = s->mb_num - 1; i >= 0; i--) {
962             const int mb_xy = s->mb_index2xy[i];
963             int       error = s->error_status_table[mb_xy];
964
965             if (!s->mbskip_table[mb_xy]) // FIXME partition specific
966                 distance++;
967             if (error & (1 << error_type))
968                 distance = 0;
969
970             if (s->partitioned_frame) {
971                 if (distance < threshold_part[error_type - 1])
972                     s->error_status_table[mb_xy] |= 1 << error_type;
973             } else {
974                 if (distance < threshold)
975                     s->error_status_table[mb_xy] |= 1 << error_type;
976             }
977
978             if (error & VP_START)
979                 distance = 9999999;
980         }
981     }
982 #endif
983
984     /* forward mark errors */
985     error = 0;
986     for (i = 0; i < s->mb_num; i++) {
987         const int mb_xy = s->mb_index2xy[i];
988         int old_error   = s->error_status_table[mb_xy];
989
990         if (old_error & VP_START) {
991             error = old_error & ER_MB_ERROR;
992         } else {
993             error |= old_error & ER_MB_ERROR;
994             s->error_status_table[mb_xy] |= error;
995         }
996     }
997 #if 1
998     /* handle not partitioned case */
999     if (!s->partitioned_frame) {
1000         for (i = 0; i < s->mb_num; i++) {
1001             const int mb_xy = s->mb_index2xy[i];
1002             error = s->error_status_table[mb_xy];
1003             if (error & ER_MB_ERROR)
1004                 error |= ER_MB_ERROR;
1005             s->error_status_table[mb_xy] = error;
1006         }
1007     }
1008 #endif
1009
1010     dc_error = ac_error = mv_error = 0;
1011     for (i = 0; i < s->mb_num; i++) {
1012         const int mb_xy = s->mb_index2xy[i];
1013         error = s->error_status_table[mb_xy];
1014         if (error & ER_DC_ERROR)
1015             dc_error++;
1016         if (error & ER_AC_ERROR)
1017             ac_error++;
1018         if (error & ER_MV_ERROR)
1019             mv_error++;
1020     }
1021     av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
1022            dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic->f.pict_type));
1023
1024     is_intra_likely = is_intra_more_likely(s);
1025
1026     /* set unknown mb-type to most likely */
1027     for (i = 0; i < s->mb_num; i++) {
1028         const int mb_xy = s->mb_index2xy[i];
1029         error = s->error_status_table[mb_xy];
1030         if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1031             continue;
1032
1033         if (is_intra_likely)
1034             s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1035         else
1036             s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1037     }
1038
1039     // change inter to intra blocks if no reference frames are available
1040     if (!(s->last_pic && s->last_pic->f.data[0]) &&
1041         !(s->next_pic && s->next_pic->f.data[0]))
1042         for (i = 0; i < s->mb_num; i++) {
1043             const int mb_xy = s->mb_index2xy[i];
1044             if (!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
1045                 s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1046         }
1047
1048     /* handle inter blocks with damaged AC */
1049     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1050         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1051             const int mb_xy   = mb_x + mb_y * s->mb_stride;
1052             const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1053             const int dir     = !(s->last_pic && s->last_pic->f.data[0]);
1054             const int mv_dir  = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1055             int mv_type;
1056
1057             error = s->error_status_table[mb_xy];
1058
1059             if (IS_INTRA(mb_type))
1060                 continue; // intra
1061             if (error & ER_MV_ERROR)
1062                 continue; // inter with damaged MV
1063             if (!(error & ER_AC_ERROR))
1064                 continue; // undamaged inter
1065
1066             if (IS_8X8(mb_type)) {
1067                 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1068                 int j;
1069                 mv_type = MV_TYPE_8X8;
1070                 for (j = 0; j < 4; j++) {
1071                     s->mv[0][j][0] = s->cur_pic->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1072                     s->mv[0][j][1] = s->cur_pic->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1073                 }
1074             } else {
1075                 mv_type     = MV_TYPE_16X16;
1076                 s->mv[0][0][0] = s->cur_pic->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1077                 s->mv[0][0][1] = s->cur_pic->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1078             }
1079
1080             s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
1081                          mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
1082         }
1083     }
1084
1085     /* guess MVs */
1086     if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_B) {
1087         for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1088             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1089                 int       xy      = mb_x * 2 + mb_y * 2 * s->b8_stride;
1090                 const int mb_xy   = mb_x + mb_y * s->mb_stride;
1091                 const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1092                 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1093
1094                 error = s->error_status_table[mb_xy];
1095
1096                 if (IS_INTRA(mb_type))
1097                     continue;
1098                 if (!(error & ER_MV_ERROR))
1099                     continue; // inter with undamaged MV
1100                 if (!(error & ER_AC_ERROR))
1101                     continue; // undamaged inter
1102
1103                 if (!(s->last_pic && s->last_pic->f.data[0]))
1104                     mv_dir &= ~MV_DIR_FORWARD;
1105                 if (!(s->next_pic && s->next_pic->f.data[0]))
1106                     mv_dir &= ~MV_DIR_BACKWARD;
1107
1108                 if (s->pp_time) {
1109                     int time_pp = s->pp_time;
1110                     int time_pb = s->pb_time;
1111
1112                     av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
1113                     ff_thread_await_progress(&s->next_pic->f, mb_y, 0);
1114
1115                     s->mv[0][0][0] = s->next_pic->f.motion_val[0][xy][0] *  time_pb            / time_pp;
1116                     s->mv[0][0][1] = s->next_pic->f.motion_val[0][xy][1] *  time_pb            / time_pp;
1117                     s->mv[1][0][0] = s->next_pic->f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1118                     s->mv[1][0][1] = s->next_pic->f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1119                 } else {
1120                     s->mv[0][0][0] = 0;
1121                     s->mv[0][0][1] = 0;
1122                     s->mv[1][0][0] = 0;
1123                     s->mv[1][0][1] = 0;
1124                 }
1125
1126                 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
1127                              mb_x, mb_y, 0, 0);
1128             }
1129         }
1130     } else
1131         guess_mv(s);
1132
1133     /* the filters below are not XvMC compatible, skip them */
1134     if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1135         goto ec_clean;
1136     /* fill DC for inter blocks */
1137     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1138         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1139             int dc, dcu, dcv, y, n;
1140             int16_t *dc_ptr;
1141             uint8_t *dest_y, *dest_cb, *dest_cr;
1142             const int mb_xy   = mb_x + mb_y * s->mb_stride;
1143             const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1144
1145             error = s->error_status_table[mb_xy];
1146
1147             if (IS_INTRA(mb_type) && s->partitioned_frame)
1148                 continue;
1149             // if (error & ER_MV_ERROR)
1150             //     continue; // inter data damaged FIXME is this good?
1151
1152             dest_y  = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1153             dest_cb = s->cur_pic->f.data[1] + mb_x *  8 + mb_y *  8 * linesize[1];
1154             dest_cr = s->cur_pic->f.data[2] + mb_x *  8 + mb_y *  8 * linesize[2];
1155
1156             dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1157             for (n = 0; n < 4; n++) {
1158                 dc = 0;
1159                 for (y = 0; y < 8; y++) {
1160                     int x;
1161                     for (x = 0; x < 8; x++)
1162                        dc += dest_y[x + (n & 1) * 8 +
1163                              (y + (n >> 1) * 8) * linesize[0]];
1164                 }
1165                 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1166             }
1167
1168             dcu = dcv = 0;
1169             for (y = 0; y < 8; y++) {
1170                 int x;
1171                 for (x = 0; x < 8; x++) {
1172                     dcu += dest_cb[x + y * linesize[1]];
1173                     dcv += dest_cr[x + y * linesize[2]];
1174                 }
1175             }
1176             s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1177             s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1178         }
1179     }
1180 #if 1
1181     /* guess DC for damaged blocks */
1182     guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1183     guess_dc(s, s->dc_val[1], s->mb_width  , s->mb_height  , s->mb_stride, 0);
1184     guess_dc(s, s->dc_val[2], s->mb_width  , s->mb_height  , s->mb_stride, 0);
1185 #endif
1186
1187     /* filter luma DC */
1188     filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1189
1190 #if 1
1191     /* render DC only intra */
1192     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1193         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1194             uint8_t *dest_y, *dest_cb, *dest_cr;
1195             const int mb_xy   = mb_x + mb_y * s->mb_stride;
1196             const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1197
1198             error = s->error_status_table[mb_xy];
1199
1200             if (IS_INTER(mb_type))
1201                 continue;
1202             if (!(error & ER_AC_ERROR))
1203                 continue; // undamaged
1204
1205             dest_y  = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1206             dest_cb = s->cur_pic->f.data[1] + mb_x *  8 + mb_y *  8 * linesize[1];
1207             dest_cr = s->cur_pic->f.data[2] + mb_x *  8 + mb_y *  8 * linesize[2];
1208
1209             put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1210         }
1211     }
1212 #endif
1213
1214     if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1215         /* filter horizontal block boundaries */
1216         h_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
1217                        s->mb_height * 2, linesize[0], 1);
1218         h_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
1219                        s->mb_height, linesize[1], 0);
1220         h_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
1221                        s->mb_height, linesize[2], 0);
1222
1223         /* filter vertical block boundaries */
1224         v_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
1225                        s->mb_height * 2, linesize[0], 1);
1226         v_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
1227                        s->mb_height, linesize[1], 0);
1228         v_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
1229                        s->mb_height, linesize[2], 0);
1230     }
1231
1232 ec_clean:
1233     /* clean a few tables */
1234     for (i = 0; i < s->mb_num; i++) {
1235         const int mb_xy = s->mb_index2xy[i];
1236         int       error = s->error_status_table[mb_xy];
1237
1238         if (s->cur_pic->f.pict_type != AV_PICTURE_TYPE_B &&
1239             (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1240             s->mbskip_table[mb_xy] = 0;
1241         }
1242         s->mbintra_table[mb_xy] = 1;
1243     }
1244     s->cur_pic = NULL;
1245     s->next_pic    = NULL;
1246     s->last_pic    = NULL;
1247 }