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 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 
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 
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;
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);
67  } else {
68  assert(ref == 0);
69  ff_MPV_decode_mb(s, s->block);
70  }
71 }
72 
77 static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
78 {
79  if (s->codec_id == AV_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 
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 
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 
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;
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 
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;
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->er_temp_buffer;
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  s->mb_x = 0;
423  s->mb_y = mb_y;
425  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
426  const int mb_xy = mb_x + mb_y * s->mb_stride;
427 
429 
430  if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
431  continue;
432  if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
433  continue;
434 
436  : MV_DIR_BACKWARD;
437  s->mb_intra = 0;
438  s->mv_type = MV_TYPE_16X16;
439  s->mb_skipped = 0;
440 
441  s->dsp.clear_blocks(s->block[0]);
442 
443  s->mb_x = mb_x;
444  s->mb_y = mb_y;
445  s->mv[0][0][0] = 0;
446  s->mv[0][0][1] = 0;
447  decode_mb(s, 0);
448  }
449  }
450  return;
451  }
452 
453  for (depth = 0; ; depth++) {
454  int changed, pass, none_left;
455 
456  none_left = 1;
457  changed = 1;
458  for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
459  int mb_x, mb_y;
460  int score_sum = 0;
461 
462  changed = 0;
463  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
464  s->mb_x = 0;
465  s->mb_y = mb_y;
467  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
468  const int mb_xy = mb_x + mb_y * s->mb_stride;
469  int mv_predictor[8][2] = { { 0 } };
470  int ref[8] = { 0 };
471  int pred_count = 0;
472  int j;
473  int best_score = 256 * 256 * 256 * 64;
474  int best_pred = 0;
475  const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
476  int prev_x, prev_y, prev_ref;
477 
479 
480  if ((mb_x ^ mb_y ^ pass) & 1)
481  continue;
482 
483  if (fixed[mb_xy] == MV_FROZEN)
484  continue;
485  assert(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
486  assert(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
487 
488  j = 0;
489  if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
490  j = 1;
491  if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
492  j = 1;
493  if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
494  j = 1;
495  if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
496  j = 1;
497  if (j == 0)
498  continue;
499 
500  j = 0;
501  if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
502  j = 1;
503  if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
504  j = 1;
505  if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
506  j = 1;
507  if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
508  j = 1;
509  if (j == 0 && pass > 1)
510  continue;
511 
512  none_left = 0;
513 
514  if (mb_x > 0 && fixed[mb_xy - 1]) {
515  mv_predictor[pred_count][0] =
516  s->current_picture.f.motion_val[0][mot_index - mot_step][0];
517  mv_predictor[pred_count][1] =
518  s->current_picture.f.motion_val[0][mot_index - mot_step][1];
519  ref[pred_count] =
520  s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
521  pred_count++;
522  }
523  if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
524  mv_predictor[pred_count][0] =
525  s->current_picture.f.motion_val[0][mot_index + mot_step][0];
526  mv_predictor[pred_count][1] =
527  s->current_picture.f.motion_val[0][mot_index + mot_step][1];
528  ref[pred_count] =
529  s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
530  pred_count++;
531  }
532  if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
533  mv_predictor[pred_count][0] =
534  s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
535  mv_predictor[pred_count][1] =
536  s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
537  ref[pred_count] =
538  s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
539  pred_count++;
540  }
541  if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
542  mv_predictor[pred_count][0] =
543  s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
544  mv_predictor[pred_count][1] =
545  s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
546  ref[pred_count] =
547  s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
548  pred_count++;
549  }
550  if (pred_count == 0)
551  continue;
552 
553  if (pred_count > 1) {
554  int sum_x = 0, sum_y = 0, sum_r = 0;
555  int max_x, max_y, min_x, min_y, max_r, min_r;
556 
557  for (j = 0; j < pred_count; j++) {
558  sum_x += mv_predictor[j][0];
559  sum_y += mv_predictor[j][1];
560  sum_r += ref[j];
561  if (j && ref[j] != ref[j - 1])
562  goto skip_mean_and_median;
563  }
564 
565  /* mean */
566  mv_predictor[pred_count][0] = sum_x / j;
567  mv_predictor[pred_count][1] = sum_y / j;
568  ref[pred_count] = sum_r / j;
569 
570  /* median */
571  if (pred_count >= 3) {
572  min_y = min_x = min_r = 99999;
573  max_y = max_x = max_r = -99999;
574  } else {
575  min_x = min_y = max_x = max_y = min_r = max_r = 0;
576  }
577  for (j = 0; j < pred_count; j++) {
578  max_x = FFMAX(max_x, mv_predictor[j][0]);
579  max_y = FFMAX(max_y, mv_predictor[j][1]);
580  max_r = FFMAX(max_r, ref[j]);
581  min_x = FFMIN(min_x, mv_predictor[j][0]);
582  min_y = FFMIN(min_y, mv_predictor[j][1]);
583  min_r = FFMIN(min_r, ref[j]);
584  }
585  mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
586  mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
587  ref[pred_count + 1] = sum_r - max_r - min_r;
588 
589  if (pred_count == 4) {
590  mv_predictor[pred_count + 1][0] /= 2;
591  mv_predictor[pred_count + 1][1] /= 2;
592  ref[pred_count + 1] /= 2;
593  }
594  pred_count += 2;
595  }
596 
597 skip_mean_and_median:
598  /* zero MV */
599  pred_count++;
600 
601  if (!fixed[mb_xy]) {
602  if (s->avctx->codec_id == AV_CODEC_ID_H264) {
603  // FIXME
604  } else {
606  mb_y, 0);
607  }
608  if (!s->last_picture.f.motion_val[0] ||
609  !s->last_picture.f.ref_index[0])
610  goto skip_last_mv;
611  prev_x = s->last_picture.f.motion_val[0][mot_index][0];
612  prev_y = s->last_picture.f.motion_val[0][mot_index][1];
613  prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
614  } else {
615  prev_x = s->current_picture.f.motion_val[0][mot_index][0];
616  prev_y = s->current_picture.f.motion_val[0][mot_index][1];
617  prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
618  }
619 
620  /* last MV */
621  mv_predictor[pred_count][0] = prev_x;
622  mv_predictor[pred_count][1] = prev_y;
623  ref[pred_count] = prev_ref;
624  pred_count++;
625 
626 skip_last_mv:
627  s->mv_dir = MV_DIR_FORWARD;
628  s->mb_intra = 0;
629  s->mv_type = MV_TYPE_16X16;
630  s->mb_skipped = 0;
631 
632  s->dsp.clear_blocks(s->block[0]);
633 
634  s->mb_x = mb_x;
635  s->mb_y = mb_y;
636 
637  for (j = 0; j < pred_count; j++) {
638  int score = 0;
639  uint8_t *src = s->current_picture.f.data[0] +
640  mb_x * 16 + mb_y * 16 * s->linesize;
641 
642  s->current_picture.f.motion_val[0][mot_index][0] =
643  s->mv[0][0][0] = mv_predictor[j][0];
644  s->current_picture.f.motion_val[0][mot_index][1] =
645  s->mv[0][0][1] = mv_predictor[j][1];
646 
647  // predictor intra or otherwise not available
648  if (ref[j] < 0)
649  continue;
650 
651  decode_mb(s, ref[j]);
652 
653  if (mb_x > 0 && fixed[mb_xy - 1]) {
654  int k;
655  for (k = 0; k < 16; k++)
656  score += FFABS(src[k * s->linesize - 1] -
657  src[k * s->linesize]);
658  }
659  if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
660  int k;
661  for (k = 0; k < 16; k++)
662  score += FFABS(src[k * s->linesize + 15] -
663  src[k * s->linesize + 16]);
664  }
665  if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
666  int k;
667  for (k = 0; k < 16; k++)
668  score += FFABS(src[k - s->linesize] - src[k]);
669  }
670  if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
671  int k;
672  for (k = 0; k < 16; k++)
673  score += FFABS(src[k + s->linesize * 15] -
674  src[k + s->linesize * 16]);
675  }
676 
677  if (score <= best_score) { // <= will favor the last MV
678  best_score = score;
679  best_pred = j;
680  }
681  }
682  score_sum += best_score;
683  s->mv[0][0][0] = mv_predictor[best_pred][0];
684  s->mv[0][0][1] = mv_predictor[best_pred][1];
685 
686  for (i = 0; i < mot_step; i++)
687  for (j = 0; j < mot_step; j++) {
688  s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
689  s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
690  }
691 
692  decode_mb(s, ref[best_pred]);
693 
694 
695  if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
696  fixed[mb_xy] = MV_CHANGED;
697  changed++;
698  } else
699  fixed[mb_xy] = MV_UNCHANGED;
700  }
701  }
702  }
703 
704  if (none_left)
705  return;
706 
707  for (i = 0; i < s->mb_num; i++) {
708  int mb_xy = s->mb_index2xy[i];
709  if (fixed[mb_xy])
710  fixed[mb_xy] = MV_FROZEN;
711  }
712  }
713 }
714 
716 {
717  int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
718 
719  if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
720  return 1; // no previous frame available -> use spatial prediction
721 
722  undamaged_count = 0;
723  for (i = 0; i < s->mb_num; i++) {
724  const int mb_xy = s->mb_index2xy[i];
725  const int error = s->error_status_table[mb_xy];
726  if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
727  undamaged_count++;
728  }
729 
730  if (s->codec_id == AV_CODEC_ID_H264) {
731  H264Context *h = (void*) s;
732  if (h->list_count <= 0 || h->ref_count[0] <= 0 ||
733  !h->ref_list[0][0].f.data[0])
734  return 1;
735  }
736 
737  if (undamaged_count < 5)
738  return 0; // almost all MBs damaged -> use temporal prediction
739 
740  // prevent dsp.sad() check, that requires access to the image
742  s->avctx->xvmc_acceleration &&
744  return 1;
745 
746  skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
747  is_intra_likely = 0;
748 
749  j = 0;
750  for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
751  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
752  int error;
753  const int mb_xy = mb_x + mb_y * s->mb_stride;
754 
755  error = s->error_status_table[mb_xy];
756  if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
757  continue; // skip damaged
758 
759  j++;
760  // skip a few to speed things up
761  if ((j % skip_amount) != 0)
762  continue;
763 
764  if (s->pict_type == AV_PICTURE_TYPE_I) {
765  uint8_t *mb_ptr = s->current_picture.f.data[0] +
766  mb_x * 16 + mb_y * 16 * s->linesize;
767  uint8_t *last_mb_ptr = s->last_picture.f.data[0] +
768  mb_x * 16 + mb_y * 16 * s->linesize;
769 
770  if (s->avctx->codec_id == AV_CODEC_ID_H264) {
771  // FIXME
772  } else {
774  mb_y, 0);
775  }
776  is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr,
777  s->linesize, 16);
778  is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr,
779  last_mb_ptr + s->linesize * 16,
780  s->linesize, 16);
781  } else {
782  if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
783  is_intra_likely++;
784  else
785  is_intra_likely--;
786  }
787  }
788  }
789  return is_intra_likely > 0;
790 }
791 
793 {
794  if (!s->err_recognition)
795  return;
796 
798  s->mb_stride * s->mb_height * sizeof(uint8_t));
799  s->error_count = 3 * s->mb_num;
800  s->error_occurred = 0;
801 }
802 
810 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
811  int endx, int endy, int status)
812 {
813  const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
814  const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
815  const int start_xy = s->mb_index2xy[start_i];
816  const int end_xy = s->mb_index2xy[end_i];
817  int mask = -1;
818 
819  if (s->avctx->hwaccel)
820  return;
821 
822  if (start_i > end_i || start_xy > end_xy) {
824  "internal error, slice end before start\n");
825  return;
826  }
827 
828  if (!s->err_recognition)
829  return;
830 
831  mask &= ~VP_START;
832  if (status & (ER_AC_ERROR | ER_AC_END)) {
833  mask &= ~(ER_AC_ERROR | ER_AC_END);
834  s->error_count -= end_i - start_i + 1;
835  }
836  if (status & (ER_DC_ERROR | ER_DC_END)) {
837  mask &= ~(ER_DC_ERROR | ER_DC_END);
838  s->error_count -= end_i - start_i + 1;
839  }
840  if (status & (ER_MV_ERROR | ER_MV_END)) {
841  mask &= ~(ER_MV_ERROR | ER_MV_END);
842  s->error_count -= end_i - start_i + 1;
843  }
844 
845  if (status & ER_MB_ERROR) {
846  s->error_occurred = 1;
847  s->error_count = INT_MAX;
848  }
849 
850  if (mask == ~0x7F) {
851  memset(&s->error_status_table[start_xy], 0,
852  (end_xy - start_xy) * sizeof(uint8_t));
853  } else {
854  int i;
855  for (i = start_xy; i < end_xy; i++)
856  s->error_status_table[i] &= mask;
857  }
858 
859  if (end_i == s->mb_num)
860  s->error_count = INT_MAX;
861  else {
862  s->error_status_table[end_xy] &= mask;
863  s->error_status_table[end_xy] |= status;
864  }
865 
866  s->error_status_table[start_xy] |= VP_START;
867 
868  if (start_xy > 0 && s->avctx->thread_count <= 1 &&
869  s->avctx->skip_top * s->mb_width < start_i) {
870  int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
871 
872  prev_status &= ~ VP_START;
873  if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
874  s->error_count = INT_MAX;
875  }
876 }
877 
879 {
880  int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
881  int distance;
882  int threshold_part[4] = { 100, 100, 100 };
883  int threshold = 50;
884  int is_intra_likely;
885  int size = s->b8_stride * 2 * s->mb_height;
886  Picture *pic = s->current_picture_ptr;
887 
888  /* We do not support ER of field pictures yet,
889  * though it should not crash if enabled. */
890  if (!s->err_recognition || s->error_count == 0 ||
891  s->avctx->hwaccel ||
894  s->error_count == 3 * s->mb_width *
895  (s->avctx->skip_top + s->avctx->skip_bottom)) {
896  return;
897  };
898 
899  if (s->picture_structure == PICT_FRAME &&
901  av_log(s->avctx, AV_LOG_ERROR, "Error concealment not possible, frame not fully initialized\n");
902  return;
903  }
904 
905  if (s->current_picture.f.motion_val[0] == NULL) {
906  av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
907 
908  for (i = 0; i < 2; i++) {
909  pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
910  pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
911  pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
912  }
913  pic->f.motion_subsample_log2 = 3;
915  }
916 
917  if (s->avctx->debug & FF_DEBUG_ER) {
918  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
919  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
920  int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
921 
922  av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
923  }
924  av_log(s->avctx, AV_LOG_DEBUG, "\n");
925  }
926  }
927 
928  /* handle overlapping slices */
929  for (error_type = 1; error_type <= 3; error_type++) {
930  int end_ok = 0;
931 
932  for (i = s->mb_num - 1; i >= 0; i--) {
933  const int mb_xy = s->mb_index2xy[i];
934  int error = s->error_status_table[mb_xy];
935 
936  if (error & (1 << error_type))
937  end_ok = 1;
938  if (error & (8 << error_type))
939  end_ok = 1;
940 
941  if (!end_ok)
942  s->error_status_table[mb_xy] |= 1 << error_type;
943 
944  if (error & VP_START)
945  end_ok = 0;
946  }
947  }
948 
949  /* handle slices with partitions of different length */
950  if (s->partitioned_frame) {
951  int end_ok = 0;
952 
953  for (i = s->mb_num - 1; i >= 0; i--) {
954  const int mb_xy = s->mb_index2xy[i];
955  int error = s->error_status_table[mb_xy];
956 
957  if (error & ER_AC_END)
958  end_ok = 0;
959  if ((error & ER_MV_END) ||
960  (error & ER_DC_END) ||
961  (error & ER_AC_ERROR))
962  end_ok = 1;
963 
964  if (!end_ok)
965  s->error_status_table[mb_xy]|= ER_AC_ERROR;
966 
967  if (error & VP_START)
968  end_ok = 0;
969  }
970  }
971 
972  /* handle missing slices */
973  if (s->err_recognition & AV_EF_EXPLODE) {
974  int end_ok = 1;
975 
976  // FIXME + 100 hack
977  for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
978  const int mb_xy = s->mb_index2xy[i];
979  int error1 = s->error_status_table[mb_xy];
980  int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
981 
982  if (error1 & VP_START)
983  end_ok = 1;
984 
985  if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
986  error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
987  ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
988  (error1 & ER_MV_END))) {
989  // end & uninit
990  end_ok = 0;
991  }
992 
993  if (!end_ok)
994  s->error_status_table[mb_xy] |= ER_MB_ERROR;
995  }
996  }
997 
998  /* backward mark errors */
999  distance = 9999999;
1000  for (error_type = 1; error_type <= 3; error_type++) {
1001  for (i = s->mb_num - 1; i >= 0; i--) {
1002  const int mb_xy = s->mb_index2xy[i];
1003  int error = s->error_status_table[mb_xy];
1004 
1005  if (!s->mbskip_table[mb_xy]) // FIXME partition specific
1006  distance++;
1007  if (error & (1 << error_type))
1008  distance = 0;
1009 
1010  if (s->partitioned_frame) {
1011  if (distance < threshold_part[error_type - 1])
1012  s->error_status_table[mb_xy] |= 1 << error_type;
1013  } else {
1014  if (distance < threshold)
1015  s->error_status_table[mb_xy] |= 1 << error_type;
1016  }
1017 
1018  if (error & VP_START)
1019  distance = 9999999;
1020  }
1021  }
1022 
1023  /* forward mark errors */
1024  error = 0;
1025  for (i = 0; i < s->mb_num; i++) {
1026  const int mb_xy = s->mb_index2xy[i];
1027  int old_error = s->error_status_table[mb_xy];
1028 
1029  if (old_error & VP_START) {
1030  error = old_error & ER_MB_ERROR;
1031  } else {
1032  error |= old_error & ER_MB_ERROR;
1033  s->error_status_table[mb_xy] |= error;
1034  }
1035  }
1036 
1037  /* handle not partitioned case */
1038  if (!s->partitioned_frame) {
1039  for (i = 0; i < s->mb_num; i++) {
1040  const int mb_xy = s->mb_index2xy[i];
1041  error = s->error_status_table[mb_xy];
1042  if (error & ER_MB_ERROR)
1043  error |= ER_MB_ERROR;
1044  s->error_status_table[mb_xy] = error;
1045  }
1046  }
1047 
1048  dc_error = ac_error = mv_error = 0;
1049  for (i = 0; i < s->mb_num; i++) {
1050  const int mb_xy = s->mb_index2xy[i];
1051  error = s->error_status_table[mb_xy];
1052  if (error & ER_DC_ERROR)
1053  dc_error++;
1054  if (error & ER_AC_ERROR)
1055  ac_error++;
1056  if (error & ER_MV_ERROR)
1057  mv_error++;
1058  }
1059  av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
1060  dc_error, ac_error, mv_error);
1061 
1062  is_intra_likely = is_intra_more_likely(s);
1063 
1064  /* set unknown mb-type to most likely */
1065  for (i = 0; i < s->mb_num; i++) {
1066  const int mb_xy = s->mb_index2xy[i];
1067  error = s->error_status_table[mb_xy];
1068  if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1069  continue;
1070 
1071  if (is_intra_likely)
1073  else
1075  }
1076 
1077  // change inter to intra blocks if no reference frames are available
1078  if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
1079  for (i = 0; i < s->mb_num; i++) {
1080  const int mb_xy = s->mb_index2xy[i];
1081  if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
1083  }
1084 
1085  /* handle inter blocks with damaged AC */
1086  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1087  s->mb_x = 0;
1088  s->mb_y = mb_y;
1090  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1091  const int mb_xy = mb_x + mb_y * s->mb_stride;
1092  const int mb_type = s->current_picture.f.mb_type[mb_xy];
1093  int dir = !s->last_picture.f.data[0];
1094 
1096 
1097  error = s->error_status_table[mb_xy];
1098 
1099  if (IS_INTRA(mb_type))
1100  continue; // intra
1101  if (error & ER_MV_ERROR)
1102  continue; // inter with damaged MV
1103  if (!(error & ER_AC_ERROR))
1104  continue; // undamaged inter
1105 
1106  s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1107  s->mb_intra = 0;
1108  s->mb_skipped = 0;
1109  if (IS_8X8(mb_type)) {
1110  int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1111  int j;
1112  s->mv_type = MV_TYPE_8X8;
1113  for (j = 0; j < 4; j++) {
1114  s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1115  s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1116  }
1117  } else {
1118  s->mv_type = MV_TYPE_16X16;
1119  s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1120  s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1121  }
1122 
1123  s->dsp.clear_blocks(s->block[0]);
1124 
1125  s->mb_x = mb_x;
1126  s->mb_y = mb_y;
1127  decode_mb(s, 0 /* FIXME h264 partitioned slices need this set */);
1128  }
1129  }
1130 
1131  /* guess MVs */
1132  if (s->pict_type == AV_PICTURE_TYPE_B) {
1133  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1134  s->mb_x = 0;
1135  s->mb_y = mb_y;
1137  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1138  int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1139  const int mb_xy = mb_x + mb_y * s->mb_stride;
1140  const int mb_type = s->current_picture.f.mb_type[mb_xy];
1141 
1143 
1144  error = s->error_status_table[mb_xy];
1145 
1146  if (IS_INTRA(mb_type))
1147  continue;
1148  if (!(error & ER_MV_ERROR))
1149  continue; // inter with undamaged MV
1150  if (!(error & ER_AC_ERROR))
1151  continue; // undamaged inter
1152 
1154  if (!s->last_picture.f.data[0])
1155  s->mv_dir &= ~MV_DIR_FORWARD;
1156  if (!s->next_picture.f.data[0])
1157  s->mv_dir &= ~MV_DIR_BACKWARD;
1158  s->mb_intra = 0;
1159  s->mv_type = MV_TYPE_16X16;
1160  s->mb_skipped = 0;
1161 
1162  if (s->pp_time) {
1163  int time_pp = s->pp_time;
1164  int time_pb = s->pb_time;
1165 
1166  if (s->avctx->codec_id == AV_CODEC_ID_H264) {
1167  // FIXME
1168  } else {
1170  }
1171  s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;
1172  s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;
1173  s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1174  s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1175  } else {
1176  s->mv[0][0][0] = 0;
1177  s->mv[0][0][1] = 0;
1178  s->mv[1][0][0] = 0;
1179  s->mv[1][0][1] = 0;
1180  }
1181 
1182  s->dsp.clear_blocks(s->block[0]);
1183  s->mb_x = mb_x;
1184  s->mb_y = mb_y;
1185  decode_mb(s, 0);
1186  }
1187  }
1188  } else
1189  guess_mv(s);
1190 
1191  /* the filters below are not XvMC compatible, skip them */
1193  goto ec_clean;
1194  /* fill DC for inter blocks */
1195  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1196  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1197  int dc, dcu, dcv, y, n;
1198  int16_t *dc_ptr;
1199  uint8_t *dest_y, *dest_cb, *dest_cr;
1200  const int mb_xy = mb_x + mb_y * s->mb_stride;
1201  const int mb_type = s->current_picture.f.mb_type[mb_xy];
1202 
1203  error = s->error_status_table[mb_xy];
1204 
1205  if (IS_INTRA(mb_type) && s->partitioned_frame)
1206  continue;
1207  // if (error & ER_MV_ERROR)
1208  // continue; // inter data damaged FIXME is this good?
1209 
1210  dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1211  dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1212  dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1213 
1214  dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1215  for (n = 0; n < 4; n++) {
1216  dc = 0;
1217  for (y = 0; y < 8; y++) {
1218  int x;
1219  for (x = 0; x < 8; x++)
1220  dc += dest_y[x + (n & 1) * 8 +
1221  (y + (n >> 1) * 8) * s->linesize];
1222  }
1223  dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1224  }
1225 
1226  dcu = dcv = 0;
1227  for (y = 0; y < 8; y++) {
1228  int x;
1229  for (x = 0; x < 8; x++) {
1230  dcu += dest_cb[x + y * s->uvlinesize];
1231  dcv += dest_cr[x + y * s->uvlinesize];
1232  }
1233  }
1234  s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1235  s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1236  }
1237  }
1238 
1239  /* guess DC for damaged blocks */
1240  guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);
1241  guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);
1242  guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);
1243 
1244  /* filter luma DC */
1245  filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1246 
1247  /* render DC only intra */
1248  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1249  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1250  uint8_t *dest_y, *dest_cb, *dest_cr;
1251  const int mb_xy = mb_x + mb_y * s->mb_stride;
1252  const int mb_type = s->current_picture.f.mb_type[mb_xy];
1253 
1254  error = s->error_status_table[mb_xy];
1255 
1256  if (IS_INTER(mb_type))
1257  continue;
1258  if (!(error & ER_AC_ERROR))
1259  continue; // undamaged
1260 
1261  dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1262  dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1263  dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1264 
1265  put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1266  }
1267  }
1268 
1270  /* filter horizontal block boundaries */
1271  h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1272  s->mb_height * 2, s->linesize, 1);
1274  s->mb_height , s->uvlinesize, 0);
1276  s->mb_height , s->uvlinesize, 0);
1277 
1278  /* filter vertical block boundaries */
1279  v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1280  s->mb_height * 2, s->linesize, 1);
1282  s->mb_height , s->uvlinesize, 0);
1284  s->mb_height , s->uvlinesize, 0);
1285  }
1286 
1287 ec_clean:
1288  /* clean a few tables */
1289  for (i = 0; i < s->mb_num; i++) {
1290  const int mb_xy = s->mb_index2xy[i];
1291  int error = s->error_status_table[mb_xy];
1292 
1293  if (s->pict_type != AV_PICTURE_TYPE_B &&
1294  (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1295  s->mbskip_table[mb_xy] = 0;
1296  }
1297  s->mbintra_table[mb_xy] = 1;
1298  }
1299 }
#define ER_AC_END
Definition: mpegvideo.h:498
static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma)
guess the dc of blocks which do not have an undamaged dc
const struct AVCodec * codec
Definition: avcodec.h:1348
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2466
#define ff_cropTbl
int size
static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma)
simple horizontal deblocking filter used for error resilience
#define MV_CHANGED
#define VP_START
current MB is the first after a resync marker
Definition: mpegvideo.h:494
#define ER_MV_END
Definition: mpegvideo.h:500
#define MAX_NEG_CROP
Definition: dsputil.h:83
static void filter181(int16_t *data, int width, int height, int stride)
enum AVCodecID codec_id
Definition: mpegvideo.h:227
int16_t(*[2] motion_val_base)[2]
Definition: mpegvideo.h:102
#define pass
Definition: fft.c:334
static void decode_mb(MpegEncContext *s, int ref)
mpegvideo header.
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264.h:302
H264Context.
Definition: h264.h:254
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:252
int stride
Definition: mace.c:144
#define MV_FROZEN
static av_always_inline uint32_t pack16to32(int a, int b)
Definition: h264.h:753
int chroma_x_shift
Definition: mpegvideo.h:656
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2622
#define IS_INTER(a)
Definition: mpegvideo.h:109
uint8_t
#define PICT_FRAME
Definition: mpegvideo.h:641
uint8_t * er_temp_buffer
Definition: mpegvideo.h:710
#define b
Definition: input.c:52
Picture ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:378
int mb_xy
Definition: h264.h:427
uint8_t motion_subsample_log2
log2 of the size of the block which a single vector in motion_val represents: (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)
Definition: avcodec.h:1302
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:375
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:313
const char data[16]
Definition: mxf.c:66
uint16_t pp_time
time distance between the last 2 p,s,i frames
Definition: mpegvideo.h:538
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:247
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:823
#define cm
Definition: dvbsubdec.c:34
uint8_t * error_status_table
table of the error status of each MB
Definition: mpegvideo.h:493
H.264 / AVC / MPEG4 part10 codec.
int16_t * dc_val[3]
used for mpeg4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:323
Multithreading support functions.
static const uint16_t mask[17]
Definition: lzw.c:38
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:331
void ff_h264_hl_decode_mb(H264Context *h)
Definition: h264.c:1978
int chroma_y_shift
Definition: mpegvideo.h:657
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:567
#define ER_MB_ERROR
Definition: mpegvideo.h:502
int error_concealment
error concealment flags
Definition: avcodec.h:2559
int capabilities
Codec capabilities.
Definition: avcodec.h:2979
#define ER_MV_ERROR
Definition: mpegvideo.h:497
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
#define ER_DC_ERROR
Definition: mpegvideo.h:496
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:557
#define IS_INTRA(a)
Definition: mpegvideo.h:108
int error_occurred
Definition: mpegvideo.h:492
static const uint8_t scan8[16 *3+3]
Definition: h264.h:737
static float distance(float x, float y, int band)
static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
Replace the current MB with a flat dc-only version.
useful rectangle filling function
void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
#define IS_8X8(a)
Definition: mpegvideo.h:118
int err_recognition
Definition: mpegvideo.h:510
#define FRAME_MBAFF
Definition: h264.h:64
uint8_t * mbskip_table
used to avoid copy if macroblock skipped (for black regions for example) and used for b-frame encodin...
Definition: mpegvideo.h:332
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:317
Picture.
Definition: mpegvideo.h:94
static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color)
Definition: avplay.c:401
void ff_er_frame_end(MpegEncContext *s)
#define ER_AC_ERROR
Definition: mpegvideo.h:495
int skip_top
Number of macroblock rows at the top which are skipped.
Definition: avcodec.h:1958
uint8_t * mbintra_table
used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
Definition: mpegvideo.h:334
unsigned int list_count
Definition: h264.h:376
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2733
int xvmc_acceleration
XVideo Motion Acceleration.
Definition: avcodec.h:1875
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:437
uint32_t * mb_type
macroblock type table mb_type_base + mb_width + 2
Definition: avcodec.h:1180
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:389
NULL
Definition: eval.c:52
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:386
static int width
Definition: utils.c:156
external API header
void ff_thread_await_progress(AVFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread.c:684
enum AVCodecID codec_id
Definition: avcodec.h:1350
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
int debug
debug
Definition: avcodec.h:2568
int16_t(*[2] motion_val)[2]
motion vector table
Definition: avcodec.h:1172
static void guess_mv(MpegEncContext *s)
#define ER_MB_END
Definition: mpegvideo.h:503
#define CONFIG_H264_DECODER
Definition: config.h:418
void ff_er_frame_start(MpegEncContext *s)
int8_t * ref_index[2]
motion reference frame index the order in which these are stored can depend on the codec...
Definition: avcodec.h:1195
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:361
static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma)
simple vertical deblocking filter used for error resilience
#define MV_DIR_FORWARD
Definition: mpegvideo.h:385
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:349
DCTELEM(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:674
int skip_bottom
Number of macroblock rows at the bottom which are skipped.
Definition: avcodec.h:1965
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:399
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:249
void(* clear_blocks)(DCTELEM *blocks)
Definition: dsputil.h:219
int height
Definition: gxfenc.c:72
MpegEncContext.
Definition: mpegvideo.h:211
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:316
struct AVCodecContext * avctx
Definition: mpegvideo.h:213
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:248
#define MV_UNCHANGED
uint8_t * dest[3]
Definition: mpegvideo.h:435
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:295
#define ER_DC_END
Definition: mpegvideo.h:499
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:315
Bi-dir predicted.
Definition: avutil.h:247
me_cmp_func sad[6]
Definition: dsputil.h:224
static const uint8_t color[]
Definition: log.c:52
DSP utils.
int picture_structure
Definition: mpegvideo.h:637
void ff_MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
Definition: mpegvideo.c:2387
uint8_t non_zero_count_cache[15 *8]
non zero coeff count cache.
Definition: h264.h:295
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:301
int linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:253
struct AVFrame f
Definition: mpegvideo.h:95
static int is_intra_more_likely(MpegEncContext *s)
#define MV_TYPE_8X8
4 vectors (h263, mpeg4 4MV)
Definition: mpegvideo.h:390
#define CONFIG_MPEG_XVMC_DECODER
Definition: config.h:441
int uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:254
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
int b_stride
Definition: h264.h:320
uint16_t pb_time
time distance between the last b and p,s,i frame
Definition: mpegvideo.h:539
int8_t ref_cache[2][5 *8]
Definition: h264.h:303
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)