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