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.
32 #include "mpegvideo.h"
34 #include "rectangle.h"
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
43 static void decode_mb(MpegEncContext *s, int ref)
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);
49 if (CONFIG_H264_DECODER && s->codec_id == AV_CODEC_ID_H264) {
50 H264Context *h = (void*)s;
51 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
52 memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
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])
60 fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],
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);
66 ff_h264_hl_decode_mb(h);
69 ff_MPV_decode_mb(s, s->block);
74 * @param stride the number of MVs to get to the next row
75 * @param mv_step the number of MVs per row or column in a macroblock
77 static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
79 if (s->codec_id == AV_CODEC_ID_H264) {
80 H264Context *h = (void*)s;
81 assert(s->quarter_sample);
83 *stride = h->b_stride;
86 *stride = s->b8_stride;
91 * Replace the current MB with a flat dc-only version.
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)
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];
103 for (y = 0; y < 8; y++) {
105 for (x = 0; x < 8; x++)
106 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * s->linesize] = dc / 8;
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];
119 for (y = 0; y < 8; y++) {
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;
128 static void filter181(int16_t *data, int width, int height, int stride)
132 /* horizontal filter */
133 for (y = 1; y < height - 1; y++) {
134 int prev_dc = data[0 + y * stride];
136 for (x = 1; x < width - 1; x++) {
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;
147 /* vertical filter */
148 for (x = 1; x < width - 1; x++) {
149 int prev_dc = data[x];
151 for (y = 1; y < height - 1; y++) {
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;
165 * guess the dc of blocks which do not have an undamaged dc
166 * @param w width in 8 pixel blocks
167 * @param h height in 8 pixel blocks
169 static void guess_dc(MpegEncContext *s, int16_t *dc, int w,
170 int h, int stride, int is_luma)
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];
183 if (IS_INTER(s->current_picture.f.mb_type[mb_index]))
185 if (!(error & ER_DC_ERROR))
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;
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;
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]);
218 if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
219 color[2] = dc[b_x + j * stride];
220 distance[2] = j - b_y;
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;
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;
244 guess = (guess + weight_sum / 2) / weight_sum;
245 dc[b_x + b_y * stride] = guess;
251 * simple horizontal deblocking filter used for error resilience
252 * @param w width in 8 pixel blocks
253 * @param h height in 8 pixel blocks
255 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w,
256 int h, int stride, int is_luma)
258 int b_x, b_y, mvx_stride, mvy_stride;
259 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
260 set_mv_strides(s, &mvx_stride, &mvy_stride);
261 mvx_stride >>= is_luma;
262 mvy_stride *= mvx_stride;
264 for (b_y = 0; b_y < h; b_y++) {
265 for (b_x = 0; b_x < w - 1; b_x++) {
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)
283 for (y = 0; y < 8; y++) {
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];
290 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
298 if (!(left_damage && right_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)];
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)];
319 * simple vertical deblocking filter used for error resilience
320 * @param w width in 8 pixel blocks
321 * @param h height in 8 pixel blocks
323 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h,
324 int stride, int is_luma)
326 int b_x, b_y, mvx_stride, mvy_stride;
327 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
328 set_mv_strides(s, &mvx_stride, &mvy_stride);
329 mvx_stride >>= is_luma;
330 mvy_stride *= mvx_stride;
332 for (b_y = 0; b_y < h - 1; b_y++) {
333 for (b_x = 0; b_x < w; b_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;
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];
346 if (!(top_damage || bottom_damage))
347 continue; // both undamaged
349 if ((!top_intra) && (!bottom_intra) &&
350 FFABS(top_mv[0] - bottom_mv[0]) +
351 FFABS(top_mv[1] + bottom_mv[1]) < 2)
354 for (x = 0; x < 8; x++) {
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];
361 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
369 if (!(top_damage && bottom_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)];
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)];
389 static void guess_mv(MpegEncContext *s)
391 uint8_t *fixed = s->er_temp_buffer;
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;
401 set_mv_strides(s, &mot_step, &mot_stride);
404 for (i = 0; i < s->mb_num; i++) {
405 const int mb_xy = s->mb_index2xy[i];
407 int error = s->error_status_table[mb_xy];
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
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++) {
424 ff_init_block_index(s);
425 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
426 const int mb_xy = mb_x + mb_y * s->mb_stride;
428 ff_update_block_index(s);
430 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
432 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
435 s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD
438 s->mv_type = MV_TYPE_16X16;
441 s->dsp.clear_blocks(s->block[0]);
453 for (depth = 0; ; depth++) {
454 int changed, pass, none_left;
458 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
463 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
466 ff_init_block_index(s);
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 } };
473 int best_score = 256 * 256 * 256 * 64;
475 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
476 int prev_x, prev_y, prev_ref;
478 ff_update_block_index(s);
480 if ((mb_x ^ mb_y ^ pass) & 1)
483 if (fixed[mb_xy] == MV_FROZEN)
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]);
489 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
491 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
493 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
495 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
501 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
503 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
505 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
507 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
509 if (j == 0 && pass > 1)
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];
520 s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
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];
529 s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
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];
538 s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
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];
547 s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
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;
557 for (j = 0; j < pred_count; j++) {
558 sum_x += mv_predictor[j][0];
559 sum_y += mv_predictor[j][1];
561 if (j && ref[j] != ref[j - 1])
562 goto skip_mean_and_median;
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;
571 if (pred_count >= 3) {
572 min_y = min_x = min_r = 99999;
573 max_y = max_x = max_r = -99999;
575 min_x = min_y = max_x = max_y = min_r = max_r = 0;
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]);
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;
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;
597 skip_mean_and_median:
602 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
605 ff_thread_await_progress(&s->last_picture_ptr->f,
608 if (!s->last_picture.f.motion_val[0] ||
609 !s->last_picture.f.ref_index[0])
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];
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];
621 mv_predictor[pred_count][0] = prev_x;
622 mv_predictor[pred_count][1] = prev_y;
623 ref[pred_count] = prev_ref;
627 s->mv_dir = MV_DIR_FORWARD;
629 s->mv_type = MV_TYPE_16X16;
632 s->dsp.clear_blocks(s->block[0]);
637 for (j = 0; j < pred_count; j++) {
639 uint8_t *src = s->current_picture.f.data[0] +
640 mb_x * 16 + mb_y * 16 * s->linesize;
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];
647 // predictor intra or otherwise not available
651 decode_mb(s, ref[j]);
653 if (mb_x > 0 && fixed[mb_xy - 1]) {
655 for (k = 0; k < 16; k++)
656 score += FFABS(src[k * s->linesize - 1] -
657 src[k * s->linesize]);
659 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
661 for (k = 0; k < 16; k++)
662 score += FFABS(src[k * s->linesize + 15] -
663 src[k * s->linesize + 16]);
665 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
667 for (k = 0; k < 16; k++)
668 score += FFABS(src[k - s->linesize] - src[k]);
670 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
672 for (k = 0; k < 16; k++)
673 score += FFABS(src[k + s->linesize * 15] -
674 src[k + s->linesize * 16]);
677 if (score <= best_score) { // <= will favor the last MV
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];
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];
692 decode_mb(s, ref[best_pred]);
695 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
696 fixed[mb_xy] = MV_CHANGED;
699 fixed[mb_xy] = MV_UNCHANGED;
707 for (i = 0; i < s->mb_num; i++) {
708 int mb_xy = s->mb_index2xy[i];
710 fixed[mb_xy] = MV_FROZEN;
715 static int is_intra_more_likely(MpegEncContext *s)
717 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
719 if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
720 return 1; // no previous frame available -> use spatial prediction
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)))
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])
737 if (undamaged_count < 5)
738 return 0; // almost all MBs damaged -> use temporal prediction
740 // prevent dsp.sad() check, that requires access to the image
741 if (CONFIG_MPEG_XVMC_DECODER &&
742 s->avctx->xvmc_acceleration &&
743 s->pict_type == AV_PICTURE_TYPE_I)
746 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
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++) {
753 const int mb_xy = mb_x + mb_y * s->mb_stride;
755 error = s->error_status_table[mb_xy];
756 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
757 continue; // skip damaged
760 // skip a few to speed things up
761 if ((j % skip_amount) != 0)
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;
770 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
773 ff_thread_await_progress(&s->last_picture_ptr->f,
776 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr,
778 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr,
779 last_mb_ptr + s->linesize * 16,
782 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
789 return is_intra_likely > 0;
792 void ff_er_frame_start(MpegEncContext *s)
794 if (!s->err_recognition)
797 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
798 s->mb_stride * s->mb_height * sizeof(uint8_t));
799 s->error_count = 3 * s->mb_num;
800 s->error_occurred = 0;
805 * @param endx x component of the last macroblock, can be -1
806 * for the last of the previous line
807 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
808 * assumed that no earlier end or error of the same type occurred
810 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
811 int endx, int endy, int status)
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];
819 if (s->avctx->hwaccel)
822 if (start_i > end_i || start_xy > end_xy) {
823 av_log(s->avctx, AV_LOG_ERROR,
824 "internal error, slice end before start\n");
828 if (!s->err_recognition)
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;
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;
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;
845 if (status & ER_MB_ERROR) {
846 s->error_occurred = 1;
847 s->error_count = INT_MAX;
851 memset(&s->error_status_table[start_xy], 0,
852 (end_xy - start_xy) * sizeof(uint8_t));
855 for (i = start_xy; i < end_xy; i++)
856 s->error_status_table[i] &= mask;
859 if (end_i == s->mb_num)
860 s->error_count = INT_MAX;
862 s->error_status_table[end_xy] &= mask;
863 s->error_status_table[end_xy] |= status;
866 s->error_status_table[start_xy] |= VP_START;
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]];
872 prev_status &= ~ VP_START;
873 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
874 s->error_count = INT_MAX;
878 void ff_er_frame_end(MpegEncContext *s)
880 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
882 int threshold_part[4] = { 100, 100, 100 };
885 int size = s->b8_stride * 2 * s->mb_height;
886 Picture *pic = s->current_picture_ptr;
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 ||
892 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
893 s->picture_structure != PICT_FRAME ||
894 s->error_count == 3 * s->mb_width *
895 (s->avctx->skip_top + s->avctx->skip_bottom)) {
899 if (s->current_picture.f.motion_val[0] == NULL) {
900 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
902 for (i = 0; i < 2; i++) {
903 pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
904 pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
905 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
907 pic->f.motion_subsample_log2 = 3;
908 s->current_picture = *s->current_picture_ptr;
911 if (s->avctx->debug & FF_DEBUG_ER) {
912 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
913 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
914 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
916 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
918 av_log(s->avctx, AV_LOG_DEBUG, "\n");
922 /* handle overlapping slices */
923 for (error_type = 1; error_type <= 3; error_type++) {
926 for (i = s->mb_num - 1; i >= 0; i--) {
927 const int mb_xy = s->mb_index2xy[i];
928 int error = s->error_status_table[mb_xy];
930 if (error & (1 << error_type))
932 if (error & (8 << error_type))
936 s->error_status_table[mb_xy] |= 1 << error_type;
938 if (error & VP_START)
943 /* handle slices with partitions of different length */
944 if (s->partitioned_frame) {
947 for (i = s->mb_num - 1; i >= 0; i--) {
948 const int mb_xy = s->mb_index2xy[i];
949 int error = s->error_status_table[mb_xy];
951 if (error & ER_AC_END)
953 if ((error & ER_MV_END) ||
954 (error & ER_DC_END) ||
955 (error & ER_AC_ERROR))
959 s->error_status_table[mb_xy]|= ER_AC_ERROR;
961 if (error & VP_START)
966 /* handle missing slices */
967 if (s->err_recognition & AV_EF_EXPLODE) {
971 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
972 const int mb_xy = s->mb_index2xy[i];
973 int error1 = s->error_status_table[mb_xy];
974 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
976 if (error1 & VP_START)
979 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
980 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
981 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
982 (error1 & ER_MV_END))) {
988 s->error_status_table[mb_xy] |= ER_MB_ERROR;
992 /* backward mark errors */
994 for (error_type = 1; error_type <= 3; error_type++) {
995 for (i = s->mb_num - 1; i >= 0; i--) {
996 const int mb_xy = s->mb_index2xy[i];
997 int error = s->error_status_table[mb_xy];
999 if (!s->mbskip_table[mb_xy]) // FIXME partition specific
1001 if (error & (1 << error_type))
1004 if (s->partitioned_frame) {
1005 if (distance < threshold_part[error_type - 1])
1006 s->error_status_table[mb_xy] |= 1 << error_type;
1008 if (distance < threshold)
1009 s->error_status_table[mb_xy] |= 1 << error_type;
1012 if (error & VP_START)
1017 /* forward mark errors */
1019 for (i = 0; i < s->mb_num; i++) {
1020 const int mb_xy = s->mb_index2xy[i];
1021 int old_error = s->error_status_table[mb_xy];
1023 if (old_error & VP_START) {
1024 error = old_error & ER_MB_ERROR;
1026 error |= old_error & ER_MB_ERROR;
1027 s->error_status_table[mb_xy] |= error;
1031 /* handle not partitioned case */
1032 if (!s->partitioned_frame) {
1033 for (i = 0; i < s->mb_num; i++) {
1034 const int mb_xy = s->mb_index2xy[i];
1035 error = s->error_status_table[mb_xy];
1036 if (error & ER_MB_ERROR)
1037 error |= ER_MB_ERROR;
1038 s->error_status_table[mb_xy] = error;
1042 dc_error = ac_error = mv_error = 0;
1043 for (i = 0; i < s->mb_num; i++) {
1044 const int mb_xy = s->mb_index2xy[i];
1045 error = s->error_status_table[mb_xy];
1046 if (error & ER_DC_ERROR)
1048 if (error & ER_AC_ERROR)
1050 if (error & ER_MV_ERROR)
1053 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
1054 dc_error, ac_error, mv_error);
1056 is_intra_likely = is_intra_more_likely(s);
1058 /* set unknown mb-type to most likely */
1059 for (i = 0; i < s->mb_num; i++) {
1060 const int mb_xy = s->mb_index2xy[i];
1061 error = s->error_status_table[mb_xy];
1062 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1065 if (is_intra_likely)
1066 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1068 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1071 // change inter to intra blocks if no reference frames are available
1072 if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
1073 for (i = 0; i < s->mb_num; i++) {
1074 const int mb_xy = s->mb_index2xy[i];
1075 if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
1076 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1079 /* handle inter blocks with damaged AC */
1080 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1083 ff_init_block_index(s);
1084 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1085 const int mb_xy = mb_x + mb_y * s->mb_stride;
1086 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1087 int dir = !s->last_picture.f.data[0];
1089 ff_update_block_index(s);
1091 error = s->error_status_table[mb_xy];
1093 if (IS_INTRA(mb_type))
1095 if (error & ER_MV_ERROR)
1096 continue; // inter with damaged MV
1097 if (!(error & ER_AC_ERROR))
1098 continue; // undamaged inter
1100 s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1103 if (IS_8X8(mb_type)) {
1104 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1106 s->mv_type = MV_TYPE_8X8;
1107 for (j = 0; j < 4; j++) {
1108 s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1109 s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1112 s->mv_type = MV_TYPE_16X16;
1113 s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1114 s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1117 s->dsp.clear_blocks(s->block[0]);
1121 decode_mb(s, 0 /* FIXME h264 partitioned slices need this set */);
1126 if (s->pict_type == AV_PICTURE_TYPE_B) {
1127 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1130 ff_init_block_index(s);
1131 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1132 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1133 const int mb_xy = mb_x + mb_y * s->mb_stride;
1134 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1136 ff_update_block_index(s);
1138 error = s->error_status_table[mb_xy];
1140 if (IS_INTRA(mb_type))
1142 if (!(error & ER_MV_ERROR))
1143 continue; // inter with undamaged MV
1144 if (!(error & ER_AC_ERROR))
1145 continue; // undamaged inter
1147 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1148 if (!s->last_picture.f.data[0])
1149 s->mv_dir &= ~MV_DIR_FORWARD;
1150 if (!s->next_picture.f.data[0])
1151 s->mv_dir &= ~MV_DIR_BACKWARD;
1153 s->mv_type = MV_TYPE_16X16;
1157 int time_pp = s->pp_time;
1158 int time_pb = s->pb_time;
1160 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
1163 ff_thread_await_progress(&s->next_picture_ptr->f, mb_y, 0);
1165 s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;
1166 s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;
1167 s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1168 s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1176 s->dsp.clear_blocks(s->block[0]);
1185 /* the filters below are not XvMC compatible, skip them */
1186 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
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;
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->current_picture.f.mb_type[mb_xy];
1197 error = s->error_status_table[mb_xy];
1199 if (IS_INTRA(mb_type) && s->partitioned_frame)
1201 // if (error & ER_MV_ERROR)
1202 // continue; // inter data damaged FIXME is this good?
1204 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1205 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1206 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1208 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1209 for (n = 0; n < 4; n++) {
1211 for (y = 0; y < 8; y++) {
1213 for (x = 0; x < 8; x++)
1214 dc += dest_y[x + (n & 1) * 8 +
1215 (y + (n >> 1) * 8) * s->linesize];
1217 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1221 for (y = 0; y < 8; y++) {
1223 for (x = 0; x < 8; x++) {
1224 dcu += dest_cb[x + y * s->uvlinesize];
1225 dcv += dest_cr[x + y * s->uvlinesize];
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;
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);
1238 /* filter luma DC */
1239 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1241 /* render DC only intra */
1242 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1243 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1244 uint8_t *dest_y, *dest_cb, *dest_cr;
1245 const int mb_xy = mb_x + mb_y * s->mb_stride;
1246 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1248 error = s->error_status_table[mb_xy];
1250 if (IS_INTER(mb_type))
1252 if (!(error & ER_AC_ERROR))
1253 continue; // undamaged
1255 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1256 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1257 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1259 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1263 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1264 /* filter horizontal block boundaries */
1265 h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1266 s->mb_height * 2, s->linesize, 1);
1267 h_block_filter(s, s->current_picture.f.data[1], s->mb_width,
1268 s->mb_height , s->uvlinesize, 0);
1269 h_block_filter(s, s->current_picture.f.data[2], s->mb_width,
1270 s->mb_height , s->uvlinesize, 0);
1272 /* filter vertical block boundaries */
1273 v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1274 s->mb_height * 2, s->linesize, 1);
1275 v_block_filter(s, s->current_picture.f.data[1], s->mb_width,
1276 s->mb_height , s->uvlinesize, 0);
1277 v_block_filter(s, s->current_picture.f.data[2], s->mb_width,
1278 s->mb_height , s->uvlinesize, 0);
1282 /* clean a few tables */
1283 for (i = 0; i < s->mb_num; i++) {
1284 const int mb_xy = s->mb_index2xy[i];
1285 int error = s->error_status_table[mb_xy];
1287 if (s->pict_type != AV_PICTURE_TYPE_B &&
1288 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1289 s->mbskip_table[mb_xy] = 0;
1291 s->mbintra_table[mb_xy] = 1;