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