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