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