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