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