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