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