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