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