2 * Error resilience / concealment
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
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.
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.
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
25 * Error resilience / concealment.
30 #include "libavutil/internal.h"
32 #include "error_resilience.h"
33 #include "mpegutils.h"
34 #include "mpegvideo.h"
35 #include "rectangle.h"
40 * @param stride the number of MVs to get to the next row
41 * @param mv_step the number of MVs per row or column in a macroblock
43 static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
45 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
46 assert(s->quarter_sample);
48 *stride = s->mb_width * 4;
51 *stride = s->b8_stride;
56 * Replace the current MB with a flat dc-only version.
58 static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
59 uint8_t *dest_cr, int mb_x, int mb_y)
61 int *linesize = s->cur_pic.f->linesize;
62 int dc, dcu, dcv, y, i;
63 for (i = 0; i < 4; i++) {
64 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
69 for (y = 0; y < 8; y++) {
71 for (x = 0; x < 8; x++)
72 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
75 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
76 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
85 for (y = 0; y < 8; y++) {
87 for (x = 0; x < 8; x++) {
88 dest_cb[x + y * linesize[1]] = dcu / 8;
89 dest_cr[x + y * linesize[2]] = dcv / 8;
94 static void filter181(int16_t *data, int width, int height, int stride)
98 /* horizontal filter */
99 for (y = 1; y < height - 1; y++) {
100 int prev_dc = data[0 + y * stride];
102 for (x = 1; x < width - 1; x++) {
105 data[x + y * stride] * 8 -
106 data[x + 1 + y * stride];
107 dc = (dc * 10923 + 32768) >> 16;
108 prev_dc = data[x + y * stride];
109 data[x + y * stride] = dc;
113 /* vertical filter */
114 for (x = 1; x < width - 1; x++) {
115 int prev_dc = data[x];
117 for (y = 1; y < height - 1; y++) {
121 data[x + y * stride] * 8 -
122 data[x + (y + 1) * stride];
123 dc = (dc * 10923 + 32768) >> 16;
124 prev_dc = data[x + y * stride];
125 data[x + y * stride] = dc;
131 * guess the dc of blocks which do not have an undamaged dc
132 * @param w width in 8 pixel blocks
133 * @param h height in 8 pixel blocks
135 static void guess_dc(ERContext *s, int16_t *dc, int w,
136 int h, int stride, int is_luma)
140 for (b_y = 0; b_y < h; b_y++) {
141 for (b_x = 0; b_x < w; b_x++) {
142 int color[4] = { 1024, 1024, 1024, 1024 };
143 int distance[4] = { 9999, 9999, 9999, 9999 };
144 int mb_index, error, j;
145 int64_t guess, weight_sum;
146 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
147 error = s->error_status_table[mb_index];
149 if (IS_INTER(s->cur_pic.mb_type[mb_index]))
151 if (!(error & ER_DC_ERROR))
155 for (j = b_x + 1; j < w; j++) {
156 int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
157 int error_j = s->error_status_table[mb_index_j];
158 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
159 if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
160 color[0] = dc[j + b_y * stride];
161 distance[0] = j - b_x;
167 for (j = b_x - 1; j >= 0; j--) {
168 int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
169 int error_j = s->error_status_table[mb_index_j];
170 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
171 if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
172 color[1] = dc[j + b_y * stride];
173 distance[1] = b_x - j;
179 for (j = b_y + 1; j < h; j++) {
180 int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
181 int error_j = s->error_status_table[mb_index_j];
182 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
184 if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
185 color[2] = dc[b_x + j * stride];
186 distance[2] = j - b_y;
192 for (j = b_y - 1; j >= 0; j--) {
193 int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
194 int error_j = s->error_status_table[mb_index_j];
195 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
196 if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
197 color[3] = dc[b_x + j * stride];
198 distance[3] = b_y - j;
205 for (j = 0; j < 4; j++) {
206 int64_t weight = 256 * 256 * 256 * 16 / distance[j];
207 guess += weight * (int64_t) color[j];
208 weight_sum += weight;
210 guess = (guess + weight_sum / 2) / weight_sum;
211 dc[b_x + b_y * stride] = guess;
217 * simple horizontal deblocking filter used for error resilience
218 * @param w width in 8 pixel blocks
219 * @param h height in 8 pixel blocks
221 static void h_block_filter(ERContext *s, uint8_t *dst, int w,
222 int h, int stride, int is_luma)
224 int b_x, b_y, mvx_stride, mvy_stride;
225 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
226 set_mv_strides(s, &mvx_stride, &mvy_stride);
227 mvx_stride >>= is_luma;
228 mvy_stride *= mvx_stride;
230 for (b_y = 0; b_y < h; b_y++) {
231 for (b_x = 0; b_x < w - 1; b_x++) {
233 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
234 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
235 int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
236 int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
237 int left_damage = left_status & ER_MB_ERROR;
238 int right_damage = right_status & ER_MB_ERROR;
239 int offset = b_x * 8 + b_y * stride * 8;
240 int16_t *left_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
241 int16_t *right_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
242 if (!(left_damage || right_damage))
243 continue; // both undamaged
244 if ((!left_intra) && (!right_intra) &&
245 FFABS(left_mv[0] - right_mv[0]) +
246 FFABS(left_mv[1] + right_mv[1]) < 2)
249 for (y = 0; y < 8; y++) {
252 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
253 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
254 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
256 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
264 if (!(left_damage && right_damage))
268 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
269 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
270 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
271 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
274 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
275 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
276 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
277 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
285 * simple vertical deblocking filter used for error resilience
286 * @param w width in 8 pixel blocks
287 * @param h height in 8 pixel blocks
289 static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
290 int stride, int is_luma)
292 int b_x, b_y, mvx_stride, mvy_stride;
293 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
294 set_mv_strides(s, &mvx_stride, &mvy_stride);
295 mvx_stride >>= is_luma;
296 mvy_stride *= mvx_stride;
298 for (b_y = 0; b_y < h - 1; b_y++) {
299 for (b_x = 0; b_x < w; b_x++) {
301 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
302 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
303 int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
304 int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
305 int top_damage = top_status & ER_MB_ERROR;
306 int bottom_damage = bottom_status & ER_MB_ERROR;
307 int offset = b_x * 8 + b_y * stride * 8;
309 int16_t *top_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
310 int16_t *bottom_mv = s->cur_pic.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
312 if (!(top_damage || bottom_damage))
313 continue; // both undamaged
315 if ((!top_intra) && (!bottom_intra) &&
316 FFABS(top_mv[0] - bottom_mv[0]) +
317 FFABS(top_mv[1] + bottom_mv[1]) < 2)
320 for (x = 0; x < 8; x++) {
323 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
324 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
325 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
327 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
335 if (!(top_damage && bottom_damage))
339 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
340 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
341 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
342 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
345 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
346 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
347 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
348 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
355 static void guess_mv(ERContext *s)
357 uint8_t *fixed = s->er_temp_buffer;
360 #define MV_UNCHANGED 1
361 const int mb_stride = s->mb_stride;
362 const int mb_width = s->mb_width;
363 const int mb_height = s->mb_height;
364 int i, depth, num_avail;
365 int mb_x, mb_y, mot_step, mot_stride;
367 set_mv_strides(s, &mot_step, &mot_stride);
370 for (i = 0; i < s->mb_num; i++) {
371 const int mb_xy = s->mb_index2xy[i];
373 int error = s->error_status_table[mb_xy];
375 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
376 f = MV_FROZEN; // intra // FIXME check
377 if (!(error & ER_MV_ERROR))
378 f = MV_FROZEN; // inter with undamaged MV
385 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
386 num_avail <= mb_width / 2) {
387 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
388 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
389 const int mb_xy = mb_x + mb_y * s->mb_stride;
390 int mv_dir = (s->last_pic.f && s->last_pic.f->data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
392 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
394 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
399 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
406 for (depth = 0; ; depth++) {
407 int changed, pass, none_left;
411 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
416 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
417 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
418 const int mb_xy = mb_x + mb_y * s->mb_stride;
419 int mv_predictor[8][2] = { { 0 } };
423 int best_score = 256 * 256 * 256 * 64;
425 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
426 int prev_x = 0, prev_y = 0, prev_ref = 0;
428 if ((mb_x ^ mb_y ^ pass) & 1)
431 if (fixed[mb_xy] == MV_FROZEN)
435 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
437 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
439 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
441 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
447 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
449 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
451 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
453 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
455 if (j == 0 && pass > 1)
460 if (mb_x > 0 && fixed[mb_xy - 1]) {
461 mv_predictor[pred_count][0] =
462 s->cur_pic.motion_val[0][mot_index - mot_step][0];
463 mv_predictor[pred_count][1] =
464 s->cur_pic.motion_val[0][mot_index - mot_step][1];
466 s->cur_pic.ref_index[0][4 * (mb_xy - 1)];
469 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
470 mv_predictor[pred_count][0] =
471 s->cur_pic.motion_val[0][mot_index + mot_step][0];
472 mv_predictor[pred_count][1] =
473 s->cur_pic.motion_val[0][mot_index + mot_step][1];
475 s->cur_pic.ref_index[0][4 * (mb_xy + 1)];
478 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
479 mv_predictor[pred_count][0] =
480 s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][0];
481 mv_predictor[pred_count][1] =
482 s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][1];
484 s->cur_pic.ref_index[0][4 * (mb_xy - s->mb_stride)];
487 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
488 mv_predictor[pred_count][0] =
489 s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][0];
490 mv_predictor[pred_count][1] =
491 s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][1];
493 s->cur_pic.ref_index[0][4 * (mb_xy + s->mb_stride)];
499 if (pred_count > 1) {
500 int sum_x = 0, sum_y = 0, sum_r = 0;
501 int max_x, max_y, min_x, min_y, max_r, min_r;
503 for (j = 0; j < pred_count; j++) {
504 sum_x += mv_predictor[j][0];
505 sum_y += mv_predictor[j][1];
507 if (j && ref[j] != ref[j - 1])
508 goto skip_mean_and_median;
512 mv_predictor[pred_count][0] = sum_x / j;
513 mv_predictor[pred_count][1] = sum_y / j;
514 ref[pred_count] = sum_r / j;
517 if (pred_count >= 3) {
518 min_y = min_x = min_r = 99999;
519 max_y = max_x = max_r = -99999;
521 min_x = min_y = max_x = max_y = min_r = max_r = 0;
523 for (j = 0; j < pred_count; j++) {
524 max_x = FFMAX(max_x, mv_predictor[j][0]);
525 max_y = FFMAX(max_y, mv_predictor[j][1]);
526 max_r = FFMAX(max_r, ref[j]);
527 min_x = FFMIN(min_x, mv_predictor[j][0]);
528 min_y = FFMIN(min_y, mv_predictor[j][1]);
529 min_r = FFMIN(min_r, ref[j]);
531 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
532 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
533 ref[pred_count + 1] = sum_r - max_r - min_r;
535 if (pred_count == 4) {
536 mv_predictor[pred_count + 1][0] /= 2;
537 mv_predictor[pred_count + 1][1] /= 2;
538 ref[pred_count + 1] /= 2;
543 skip_mean_and_median:
548 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
551 ff_thread_await_progress(s->last_pic.tf,
554 if (!s->last_pic.motion_val[0] ||
555 !s->last_pic.ref_index[0])
557 prev_x = s->last_pic.motion_val[0][mot_index][0];
558 prev_y = s->last_pic.motion_val[0][mot_index][1];
559 prev_ref = s->last_pic.ref_index[0][4 * mb_xy];
561 prev_x = s->cur_pic.motion_val[0][mot_index][0];
562 prev_y = s->cur_pic.motion_val[0][mot_index][1];
563 prev_ref = s->cur_pic.ref_index[0][4 * mb_xy];
567 mv_predictor[pred_count][0] = prev_x;
568 mv_predictor[pred_count][1] = prev_y;
569 ref[pred_count] = prev_ref;
574 for (j = 0; j < pred_count; j++) {
575 int *linesize = s->cur_pic.f->linesize;
577 uint8_t *src = s->cur_pic.f->data[0] +
578 mb_x * 16 + mb_y * 16 * linesize[0];
580 s->cur_pic.motion_val[0][mot_index][0] =
581 s->mv[0][0][0] = mv_predictor[j][0];
582 s->cur_pic.motion_val[0][mot_index][1] =
583 s->mv[0][0][1] = mv_predictor[j][1];
585 // predictor intra or otherwise not available
589 s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
590 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
592 if (mb_x > 0 && fixed[mb_xy - 1]) {
594 for (k = 0; k < 16; k++)
595 score += FFABS(src[k * linesize[0] - 1] -
596 src[k * linesize[0]]);
598 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
600 for (k = 0; k < 16; k++)
601 score += FFABS(src[k * linesize[0] + 15] -
602 src[k * linesize[0] + 16]);
604 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
606 for (k = 0; k < 16; k++)
607 score += FFABS(src[k - linesize[0]] - src[k]);
609 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
611 for (k = 0; k < 16; k++)
612 score += FFABS(src[k + linesize[0] * 15] -
613 src[k + linesize[0] * 16]);
616 if (score <= best_score) { // <= will favor the last MV
621 score_sum += best_score;
622 s->mv[0][0][0] = mv_predictor[best_pred][0];
623 s->mv[0][0][1] = mv_predictor[best_pred][1];
625 for (i = 0; i < mot_step; i++)
626 for (j = 0; j < mot_step; j++) {
627 s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
628 s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
631 s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
632 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
635 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
636 fixed[mb_xy] = MV_CHANGED;
639 fixed[mb_xy] = MV_UNCHANGED;
647 for (i = 0; i < s->mb_num; i++) {
648 int mb_xy = s->mb_index2xy[i];
650 fixed[mb_xy] = MV_FROZEN;
655 static int is_intra_more_likely(ERContext *s)
657 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
659 if (!s->last_pic.f || !s->last_pic.f->data[0])
660 return 1; // no previous frame available -> use spatial prediction
663 for (i = 0; i < s->mb_num; i++) {
664 const int mb_xy = s->mb_index2xy[i];
665 const int error = s->error_status_table[mb_xy];
666 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
670 if (s->avctx->codec_id == AV_CODEC_ID_H264 && s->ref_count <= 0)
673 if (undamaged_count < 5)
674 return 0; // almost all MBs damaged -> use temporal prediction
677 FF_DISABLE_DEPRECATION_WARNINGS
678 // prevent dsp.sad() check, that requires access to the image
679 if (CONFIG_MPEG_XVMC_DECODER &&
680 s->avctx->xvmc_acceleration &&
681 s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I)
683 FF_ENABLE_DEPRECATION_WARNINGS
684 #endif /* FF_API_XVMC */
686 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
690 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
691 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
693 const int mb_xy = mb_x + mb_y * s->mb_stride;
695 error = s->error_status_table[mb_xy];
696 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
697 continue; // skip damaged
700 // skip a few to speed things up
701 if ((j % skip_amount) != 0)
704 if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) {
705 int *linesize = s->cur_pic.f->linesize;
706 uint8_t *mb_ptr = s->cur_pic.f->data[0] +
707 mb_x * 16 + mb_y * 16 * linesize[0];
708 uint8_t *last_mb_ptr = s->last_pic.f->data[0] +
709 mb_x * 16 + mb_y * 16 * linesize[0];
711 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
714 ff_thread_await_progress(s->last_pic.tf, mb_y, 0);
716 is_intra_likely += s->mecc->sad[0](NULL, last_mb_ptr, mb_ptr,
718 is_intra_likely -= s->mecc->sad[0](NULL, last_mb_ptr,
719 last_mb_ptr + linesize[0] * 16,
722 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
729 return is_intra_likely > 0;
732 void ff_er_frame_start(ERContext *s)
734 if (!s->avctx->error_concealment)
737 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
738 s->mb_stride * s->mb_height * sizeof(uint8_t));
739 s->error_count = 3 * s->mb_num;
740 s->error_occurred = 0;
745 * @param endx x component of the last macroblock, can be -1
746 * for the last of the previous line
747 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
748 * assumed that no earlier end or error of the same type occurred
750 void ff_er_add_slice(ERContext *s, int startx, int starty,
751 int endx, int endy, int status)
753 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
754 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
755 const int start_xy = s->mb_index2xy[start_i];
756 const int end_xy = s->mb_index2xy[end_i];
759 if (s->avctx->hwaccel)
762 if (start_i > end_i || start_xy > end_xy) {
763 av_log(s->avctx, AV_LOG_ERROR,
764 "internal error, slice end before start\n");
768 if (!s->avctx->error_concealment)
772 if (status & (ER_AC_ERROR | ER_AC_END)) {
773 mask &= ~(ER_AC_ERROR | ER_AC_END);
774 s->error_count -= end_i - start_i + 1;
776 if (status & (ER_DC_ERROR | ER_DC_END)) {
777 mask &= ~(ER_DC_ERROR | ER_DC_END);
778 s->error_count -= end_i - start_i + 1;
780 if (status & (ER_MV_ERROR | ER_MV_END)) {
781 mask &= ~(ER_MV_ERROR | ER_MV_END);
782 s->error_count -= end_i - start_i + 1;
785 if (status & ER_MB_ERROR) {
786 s->error_occurred = 1;
787 s->error_count = INT_MAX;
791 memset(&s->error_status_table[start_xy], 0,
792 (end_xy - start_xy) * sizeof(uint8_t));
795 for (i = start_xy; i < end_xy; i++)
796 s->error_status_table[i] &= mask;
799 if (end_i == s->mb_num)
800 s->error_count = INT_MAX;
802 s->error_status_table[end_xy] &= mask;
803 s->error_status_table[end_xy] |= status;
806 s->error_status_table[start_xy] |= VP_START;
808 if (start_xy > 0 && s->avctx->thread_count <= 1 &&
809 s->avctx->skip_top * s->mb_width < start_i) {
810 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
812 prev_status &= ~ VP_START;
813 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
814 s->error_count = INT_MAX;
818 void ff_er_frame_end(ERContext *s)
820 int *linesize = s->cur_pic.f->linesize;
821 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
823 int threshold_part[4] = { 100, 100, 100 };
827 /* We do not support ER of field pictures yet,
828 * though it should not crash if enabled. */
829 if (!s->avctx->error_concealment || s->error_count == 0 ||
832 s->cur_pic.field_picture ||
833 s->error_count == 3 * s->mb_width *
834 (s->avctx->skip_top + s->avctx->skip_bottom)) {
838 if (!s->cur_pic.motion_val[0] || !s->cur_pic.ref_index[0]) {
839 av_log(s->avctx, AV_LOG_ERROR, "MVs not available, ER not possible.\n");
843 if (s->avctx->debug & FF_DEBUG_ER) {
844 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
845 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
846 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
848 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
850 av_log(s->avctx, AV_LOG_DEBUG, "\n");
854 /* handle overlapping slices */
855 for (error_type = 1; error_type <= 3; error_type++) {
858 for (i = s->mb_num - 1; i >= 0; i--) {
859 const int mb_xy = s->mb_index2xy[i];
860 int error = s->error_status_table[mb_xy];
862 if (error & (1 << error_type))
864 if (error & (8 << error_type))
868 s->error_status_table[mb_xy] |= 1 << error_type;
870 if (error & VP_START)
875 /* handle slices with partitions of different length */
876 if (s->partitioned_frame) {
879 for (i = s->mb_num - 1; i >= 0; i--) {
880 const int mb_xy = s->mb_index2xy[i];
881 int error = s->error_status_table[mb_xy];
883 if (error & ER_AC_END)
885 if ((error & ER_MV_END) ||
886 (error & ER_DC_END) ||
887 (error & ER_AC_ERROR))
891 s->error_status_table[mb_xy]|= ER_AC_ERROR;
893 if (error & VP_START)
898 /* handle missing slices */
899 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
903 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
904 const int mb_xy = s->mb_index2xy[i];
905 int error1 = s->error_status_table[mb_xy];
906 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
908 if (error1 & VP_START)
911 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
912 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
913 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
914 (error1 & ER_MV_END))) {
920 s->error_status_table[mb_xy] |= ER_MB_ERROR;
924 /* backward mark errors */
926 for (error_type = 1; error_type <= 3; error_type++) {
927 for (i = s->mb_num - 1; i >= 0; i--) {
928 const int mb_xy = s->mb_index2xy[i];
929 int error = s->error_status_table[mb_xy];
931 if (!s->mbskip_table[mb_xy]) // FIXME partition specific
933 if (error & (1 << error_type))
936 if (s->partitioned_frame) {
937 if (distance < threshold_part[error_type - 1])
938 s->error_status_table[mb_xy] |= 1 << error_type;
940 if (distance < threshold)
941 s->error_status_table[mb_xy] |= 1 << error_type;
944 if (error & VP_START)
949 /* forward mark errors */
951 for (i = 0; i < s->mb_num; i++) {
952 const int mb_xy = s->mb_index2xy[i];
953 int old_error = s->error_status_table[mb_xy];
955 if (old_error & VP_START) {
956 error = old_error & ER_MB_ERROR;
958 error |= old_error & ER_MB_ERROR;
959 s->error_status_table[mb_xy] |= error;
963 /* handle not partitioned case */
964 if (!s->partitioned_frame) {
965 for (i = 0; i < s->mb_num; i++) {
966 const int mb_xy = s->mb_index2xy[i];
967 error = s->error_status_table[mb_xy];
968 if (error & ER_MB_ERROR)
969 error |= ER_MB_ERROR;
970 s->error_status_table[mb_xy] = error;
974 dc_error = ac_error = mv_error = 0;
975 for (i = 0; i < s->mb_num; i++) {
976 const int mb_xy = s->mb_index2xy[i];
977 error = s->error_status_table[mb_xy];
978 if (error & ER_DC_ERROR)
980 if (error & ER_AC_ERROR)
982 if (error & ER_MV_ERROR)
985 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
986 dc_error, ac_error, mv_error);
988 is_intra_likely = is_intra_more_likely(s);
990 /* set unknown mb-type to most likely */
991 for (i = 0; i < s->mb_num; i++) {
992 const int mb_xy = s->mb_index2xy[i];
993 error = s->error_status_table[mb_xy];
994 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
998 s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1000 s->cur_pic.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1003 // change inter to intra blocks if no reference frames are available
1004 if (!(s->last_pic.f && s->last_pic.f->data[0]) &&
1005 !(s->next_pic.f && s->next_pic.f->data[0]))
1006 for (i = 0; i < s->mb_num; i++) {
1007 const int mb_xy = s->mb_index2xy[i];
1008 if (!IS_INTRA(s->cur_pic.mb_type[mb_xy]))
1009 s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1012 /* handle inter blocks with damaged AC */
1013 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1014 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1015 const int mb_xy = mb_x + mb_y * s->mb_stride;
1016 const int mb_type = s->cur_pic.mb_type[mb_xy];
1017 const int dir = !(s->last_pic.f && s->last_pic.f->data[0]);
1018 const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1021 error = s->error_status_table[mb_xy];
1023 if (IS_INTRA(mb_type))
1025 if (error & ER_MV_ERROR)
1026 continue; // inter with damaged MV
1027 if (!(error & ER_AC_ERROR))
1028 continue; // undamaged inter
1030 if (IS_8X8(mb_type)) {
1031 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1033 mv_type = MV_TYPE_8X8;
1034 for (j = 0; j < 4; j++) {
1035 s->mv[0][j][0] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1036 s->mv[0][j][1] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1039 mv_type = MV_TYPE_16X16;
1040 s->mv[0][0][0] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1041 s->mv[0][0][1] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1044 s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
1045 mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
1050 if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_B) {
1051 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1052 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1053 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1054 const int mb_xy = mb_x + mb_y * s->mb_stride;
1055 const int mb_type = s->cur_pic.mb_type[mb_xy];
1056 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1058 error = s->error_status_table[mb_xy];
1060 if (IS_INTRA(mb_type))
1062 if (!(error & ER_MV_ERROR))
1063 continue; // inter with undamaged MV
1064 if (!(error & ER_AC_ERROR))
1065 continue; // undamaged inter
1067 if (!(s->last_pic.f && s->last_pic.f->data[0]))
1068 mv_dir &= ~MV_DIR_FORWARD;
1069 if (!(s->next_pic.f && s->next_pic.f->data[0]))
1070 mv_dir &= ~MV_DIR_BACKWARD;
1073 int time_pp = s->pp_time;
1074 int time_pb = s->pb_time;
1076 ff_thread_await_progress(s->next_pic.tf, mb_y, 0);
1078 s->mv[0][0][0] = s->next_pic.motion_val[0][xy][0] * time_pb / time_pp;
1079 s->mv[0][0][1] = s->next_pic.motion_val[0][xy][1] * time_pb / time_pp;
1080 s->mv[1][0][0] = s->next_pic.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1081 s->mv[1][0][1] = s->next_pic.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1089 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
1097 FF_DISABLE_DEPRECATION_WARNINGS
1098 /* the filters below are not XvMC compatible, skip them */
1099 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1101 FF_ENABLE_DEPRECATION_WARNINGS
1102 #endif /* FF_API_XVMC */
1103 /* fill DC for inter blocks */
1104 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1105 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1106 int dc, dcu, dcv, y, n;
1108 uint8_t *dest_y, *dest_cb, *dest_cr;
1109 const int mb_xy = mb_x + mb_y * s->mb_stride;
1110 const int mb_type = s->cur_pic.mb_type[mb_xy];
1112 error = s->error_status_table[mb_xy];
1114 if (IS_INTRA(mb_type) && s->partitioned_frame)
1116 // if (error & ER_MV_ERROR)
1117 // continue; // inter data damaged FIXME is this good?
1119 dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1120 dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1121 dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1123 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1124 for (n = 0; n < 4; n++) {
1126 for (y = 0; y < 8; y++) {
1128 for (x = 0; x < 8; x++)
1129 dc += dest_y[x + (n & 1) * 8 +
1130 (y + (n >> 1) * 8) * linesize[0]];
1132 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1136 for (y = 0; y < 8; y++) {
1138 for (x = 0; x < 8; x++) {
1139 dcu += dest_cb[x + y * linesize[1]];
1140 dcv += dest_cr[x + y * linesize[2]];
1143 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1144 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1148 /* guess DC for damaged blocks */
1149 guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);
1150 guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);
1151 guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);
1153 /* filter luma DC */
1154 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1156 /* render DC only intra */
1157 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1158 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1159 uint8_t *dest_y, *dest_cb, *dest_cr;
1160 const int mb_xy = mb_x + mb_y * s->mb_stride;
1161 const int mb_type = s->cur_pic.mb_type[mb_xy];
1163 error = s->error_status_table[mb_xy];
1165 if (IS_INTER(mb_type))
1167 if (!(error & ER_AC_ERROR))
1168 continue; // undamaged
1170 dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1171 dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1172 dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1174 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1178 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1179 /* filter horizontal block boundaries */
1180 h_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
1181 s->mb_height * 2, linesize[0], 1);
1182 h_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
1183 s->mb_height, linesize[1], 0);
1184 h_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
1185 s->mb_height, linesize[2], 0);
1187 /* filter vertical block boundaries */
1188 v_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
1189 s->mb_height * 2, linesize[0], 1);
1190 v_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
1191 s->mb_height, linesize[1], 0);
1192 v_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
1193 s->mb_height, linesize[2], 0);
1197 /* clean a few tables */
1198 for (i = 0; i < s->mb_num; i++) {
1199 const int mb_xy = s->mb_index2xy[i];
1200 int error = s->error_status_table[mb_xy];
1202 if (s->cur_pic.f->pict_type != AV_PICTURE_TYPE_B &&
1203 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1204 s->mbskip_table[mb_xy] = 0;
1206 s->mbintra_table[mb_xy] = 1;
1209 memset(&s->cur_pic, 0, sizeof(ERPicture));
1210 memset(&s->last_pic, 0, sizeof(ERPicture));
1211 memset(&s->next_pic, 0, sizeof(ERPicture));