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