2 * Error resilience / concealment
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Error resilience / concealment.
31 #include "error_resilience.h"
32 #include "mpegvideo.h"
33 #include "rectangle.h"
37 * @param stride the number of MVs to get to the next row
38 * @param mv_step the number of MVs per row or column in a macroblock
40 static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
42 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
43 av_assert0(s->quarter_sample);
45 *stride = s->mb_width * 4;
48 *stride = s->b8_stride;
53 * Replace the current MB with a flat dc-only version.
55 static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
56 uint8_t *dest_cr, int mb_x, int mb_y)
58 int *linesize = s->cur_pic->f.linesize;
59 int dc, dcu, dcv, y, i;
60 for (i = 0; i < 4; i++) {
61 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
66 for (y = 0; y < 8; y++) {
68 for (x = 0; x < 8; x++)
69 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
72 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
73 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
82 for (y = 0; y < 8; y++) {
84 for (x = 0; x < 8; x++) {
85 dest_cb[x + y * linesize[1]] = dcu / 8;
86 dest_cr[x + y * linesize[2]] = dcv / 8;
91 static void filter181(int16_t *data, int width, int height, int stride)
95 /* horizontal filter */
96 for (y = 1; y < height - 1; y++) {
97 int prev_dc = data[0 + y * stride];
99 for (x = 1; x < width - 1; x++) {
102 data[x + y * stride] * 8 -
103 data[x + 1 + y * stride];
104 dc = (dc * 10923 + 32768) >> 16;
105 prev_dc = data[x + y * stride];
106 data[x + y * stride] = dc;
110 /* vertical filter */
111 for (x = 1; x < width - 1; x++) {
112 int prev_dc = data[x];
114 for (y = 1; y < height - 1; y++) {
118 data[x + y * stride] * 8 -
119 data[x + (y + 1) * stride];
120 dc = (dc * 10923 + 32768) >> 16;
121 prev_dc = data[x + y * stride];
122 data[x + y * stride] = dc;
128 * guess the dc of blocks which do not have an undamaged dc
129 * @param w width in 8 pixel blocks
130 * @param h height in 8 pixel blocks
132 static void guess_dc(ERContext *s, int16_t *dc, int w,
133 int h, int stride, int is_luma)
136 int16_t (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
137 uint32_t (*dist)[4] = av_malloc(stride*h*sizeof(uint32_t)*4);
140 av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
144 for(b_y=0; b_y<h; b_y++){
147 for(b_x=0; b_x<w; b_x++){
148 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
149 int error_j= s->error_status_table[mb_index_j];
150 int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
151 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
152 color= dc[b_x + b_y*stride];
155 col [b_x + b_y*stride][1]= color;
156 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
160 for(b_x=w-1; b_x>=0; b_x--){
161 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
162 int error_j= s->error_status_table[mb_index_j];
163 int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
164 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
165 color= dc[b_x + b_y*stride];
168 col [b_x + b_y*stride][0]= color;
169 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
172 for(b_x=0; b_x<w; b_x++){
175 for(b_y=0; b_y<h; b_y++){
176 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
177 int error_j= s->error_status_table[mb_index_j];
178 int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
179 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
180 color= dc[b_x + b_y*stride];
183 col [b_x + b_y*stride][3]= color;
184 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
188 for(b_y=h-1; b_y>=0; b_y--){
189 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
190 int error_j= s->error_status_table[mb_index_j];
191 int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
192 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
193 color= dc[b_x + b_y*stride];
196 col [b_x + b_y*stride][2]= color;
197 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
201 for (b_y = 0; b_y < h; b_y++) {
202 for (b_x = 0; b_x < w; b_x++) {
203 int mb_index, error, j;
204 int64_t guess, weight_sum;
205 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
206 error = s->error_status_table[mb_index];
208 if (IS_INTER(s->cur_pic->mb_type[mb_index]))
210 if (!(error & ER_DC_ERROR))
215 for (j = 0; j < 4; j++) {
216 int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
217 guess += weight*(int64_t)col[b_x + b_y*stride][j];
218 weight_sum += weight;
220 guess = (guess + weight_sum / 2) / weight_sum;
221 dc[b_x + b_y * stride] = guess;
231 * simple horizontal deblocking filter used for error resilience
232 * @param w width in 8 pixel blocks
233 * @param h height in 8 pixel blocks
235 static void h_block_filter(ERContext *s, uint8_t *dst, int w,
236 int h, int stride, int is_luma)
238 int b_x, b_y, mvx_stride, mvy_stride;
239 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
240 set_mv_strides(s, &mvx_stride, &mvy_stride);
241 mvx_stride >>= is_luma;
242 mvy_stride *= mvx_stride;
244 for (b_y = 0; b_y < h; b_y++) {
245 for (b_x = 0; b_x < w - 1; b_x++) {
247 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
248 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
249 int left_intra = IS_INTRA(s->cur_pic->mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
250 int right_intra = IS_INTRA(s->cur_pic->mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
251 int left_damage = left_status & ER_MB_ERROR;
252 int right_damage = right_status & ER_MB_ERROR;
253 int offset = b_x * 8 + b_y * stride * 8;
254 int16_t *left_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
255 int16_t *right_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
256 if (!(left_damage || right_damage))
257 continue; // both undamaged
258 if ((!left_intra) && (!right_intra) &&
259 FFABS(left_mv[0] - right_mv[0]) +
260 FFABS(left_mv[1] + right_mv[1]) < 2)
263 for (y = 0; y < 8; y++) {
266 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
267 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
268 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
270 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
278 if (!(left_damage && right_damage))
282 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
283 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
284 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
285 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
288 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
289 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
290 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
291 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
299 * simple vertical deblocking filter used for error resilience
300 * @param w width in 8 pixel blocks
301 * @param h height in 8 pixel blocks
303 static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
304 int stride, int is_luma)
306 int b_x, b_y, mvx_stride, mvy_stride;
307 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
308 set_mv_strides(s, &mvx_stride, &mvy_stride);
309 mvx_stride >>= is_luma;
310 mvy_stride *= mvx_stride;
312 for (b_y = 0; b_y < h - 1; b_y++) {
313 for (b_x = 0; b_x < w; b_x++) {
315 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
316 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
317 int top_intra = IS_INTRA(s->cur_pic->mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
318 int bottom_intra = IS_INTRA(s->cur_pic->mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
319 int top_damage = top_status & ER_MB_ERROR;
320 int bottom_damage = bottom_status & ER_MB_ERROR;
321 int offset = b_x * 8 + b_y * stride * 8;
323 int16_t *top_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
324 int16_t *bottom_mv = s->cur_pic->motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
326 if (!(top_damage || bottom_damage))
327 continue; // both undamaged
329 if ((!top_intra) && (!bottom_intra) &&
330 FFABS(top_mv[0] - bottom_mv[0]) +
331 FFABS(top_mv[1] + bottom_mv[1]) < 2)
334 for (x = 0; x < 8; x++) {
337 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
338 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
339 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
341 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
349 if (!(top_damage && bottom_damage))
353 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
354 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
355 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
356 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
359 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
360 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
361 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
362 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
369 static void guess_mv(ERContext *s)
371 uint8_t *fixed = s->er_temp_buffer;
374 #define MV_UNCHANGED 1
375 const int mb_stride = s->mb_stride;
376 const int mb_width = s->mb_width;
377 const int mb_height = s->mb_height;
378 int i, depth, num_avail;
379 int mb_x, mb_y, mot_step, mot_stride;
381 set_mv_strides(s, &mot_step, &mot_stride);
384 for (i = 0; i < s->mb_num; i++) {
385 const int mb_xy = s->mb_index2xy[i];
387 int error = s->error_status_table[mb_xy];
389 if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
390 f = MV_FROZEN; // intra // FIXME check
391 if (!(error & ER_MV_ERROR))
392 f = MV_FROZEN; // inter with undamaged MV
397 else if(s->last_pic->f.data[0] && s->last_pic->motion_val[0]){
398 const int mb_y= mb_xy / s->mb_stride;
399 const int mb_x= mb_xy % s->mb_stride;
400 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
401 s->cur_pic->motion_val[0][mot_index][0]= s->last_pic->motion_val[0][mot_index][0];
402 s->cur_pic->motion_val[0][mot_index][1]= s->last_pic->motion_val[0][mot_index][1];
403 s->cur_pic->ref_index[0][4*mb_xy] = s->last_pic->ref_index[0][4*mb_xy];
407 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
408 num_avail <= mb_width / 2) {
409 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
410 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
411 const int mb_xy = mb_x + mb_y * s->mb_stride;
412 int mv_dir = (s->last_pic && s->last_pic->f.data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
414 if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
416 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
421 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
428 for (depth = 0; ; depth++) {
429 int changed, pass, none_left;
433 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
438 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
439 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
440 const int mb_xy = mb_x + mb_y * s->mb_stride;
441 int mv_predictor[8][2] = { { 0 } };
445 int best_score = 256 * 256 * 256 * 64;
447 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
448 int prev_x, prev_y, prev_ref;
450 if ((mb_x ^ mb_y ^ pass) & 1)
453 if (fixed[mb_xy] == MV_FROZEN)
455 av_assert1(!IS_INTRA(s->cur_pic->mb_type[mb_xy]));
456 av_assert1(s->last_pic && s->last_pic->f.data[0]);
459 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
461 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
463 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
465 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
471 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
473 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
475 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
477 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
479 if (j == 0 && pass > 1)
484 if (mb_x > 0 && fixed[mb_xy - 1]) {
485 mv_predictor[pred_count][0] =
486 s->cur_pic->motion_val[0][mot_index - mot_step][0];
487 mv_predictor[pred_count][1] =
488 s->cur_pic->motion_val[0][mot_index - mot_step][1];
490 s->cur_pic->ref_index[0][4 * (mb_xy - 1)];
493 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
494 mv_predictor[pred_count][0] =
495 s->cur_pic->motion_val[0][mot_index + mot_step][0];
496 mv_predictor[pred_count][1] =
497 s->cur_pic->motion_val[0][mot_index + mot_step][1];
499 s->cur_pic->ref_index[0][4 * (mb_xy + 1)];
502 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
503 mv_predictor[pred_count][0] =
504 s->cur_pic->motion_val[0][mot_index - mot_stride * mot_step][0];
505 mv_predictor[pred_count][1] =
506 s->cur_pic->motion_val[0][mot_index - mot_stride * mot_step][1];
508 s->cur_pic->ref_index[0][4 * (mb_xy - s->mb_stride)];
511 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
512 mv_predictor[pred_count][0] =
513 s->cur_pic->motion_val[0][mot_index + mot_stride * mot_step][0];
514 mv_predictor[pred_count][1] =
515 s->cur_pic->motion_val[0][mot_index + mot_stride * mot_step][1];
517 s->cur_pic->ref_index[0][4 * (mb_xy + s->mb_stride)];
523 if (pred_count > 1) {
524 int sum_x = 0, sum_y = 0, sum_r = 0;
525 int max_x, max_y, min_x, min_y, max_r, min_r;
527 for (j = 0; j < pred_count; j++) {
528 sum_x += mv_predictor[j][0];
529 sum_y += mv_predictor[j][1];
531 if (j && ref[j] != ref[j - 1])
532 goto skip_mean_and_median;
536 mv_predictor[pred_count][0] = sum_x / j;
537 mv_predictor[pred_count][1] = sum_y / j;
538 ref[pred_count] = sum_r / j;
541 if (pred_count >= 3) {
542 min_y = min_x = min_r = 99999;
543 max_y = max_x = max_r = -99999;
545 min_x = min_y = max_x = max_y = min_r = max_r = 0;
547 for (j = 0; j < pred_count; j++) {
548 max_x = FFMAX(max_x, mv_predictor[j][0]);
549 max_y = FFMAX(max_y, mv_predictor[j][1]);
550 max_r = FFMAX(max_r, ref[j]);
551 min_x = FFMIN(min_x, mv_predictor[j][0]);
552 min_y = FFMIN(min_y, mv_predictor[j][1]);
553 min_r = FFMIN(min_r, ref[j]);
555 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
556 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
557 ref[pred_count + 1] = sum_r - max_r - min_r;
559 if (pred_count == 4) {
560 mv_predictor[pred_count + 1][0] /= 2;
561 mv_predictor[pred_count + 1][1] /= 2;
562 ref[pred_count + 1] /= 2;
567 skip_mean_and_median:
571 if (!fixed[mb_xy] && 0) {
572 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
575 ff_thread_await_progress(&s->last_pic->tf,
578 if (!s->last_pic->motion_val[0] ||
579 !s->last_pic->ref_index[0])
581 prev_x = s->last_pic->motion_val[0][mot_index][0];
582 prev_y = s->last_pic->motion_val[0][mot_index][1];
583 prev_ref = s->last_pic->ref_index[0][4 * mb_xy];
585 prev_x = s->cur_pic->motion_val[0][mot_index][0];
586 prev_y = s->cur_pic->motion_val[0][mot_index][1];
587 prev_ref = s->cur_pic->ref_index[0][4 * mb_xy];
591 mv_predictor[pred_count][0] = prev_x;
592 mv_predictor[pred_count][1] = prev_y;
593 ref[pred_count] = prev_ref;
598 for (j = 0; j < pred_count; j++) {
599 int *linesize = s->cur_pic->f.linesize;
601 uint8_t *src = s->cur_pic->f.data[0] +
602 mb_x * 16 + mb_y * 16 * linesize[0];
604 s->cur_pic->motion_val[0][mot_index][0] =
605 s->mv[0][0][0] = mv_predictor[j][0];
606 s->cur_pic->motion_val[0][mot_index][1] =
607 s->mv[0][0][1] = mv_predictor[j][1];
609 // predictor intra or otherwise not available
613 s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
614 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
616 if (mb_x > 0 && fixed[mb_xy - 1]) {
618 for (k = 0; k < 16; k++)
619 score += FFABS(src[k * linesize[0] - 1] -
620 src[k * linesize[0]]);
622 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
624 for (k = 0; k < 16; k++)
625 score += FFABS(src[k * linesize[0] + 15] -
626 src[k * linesize[0] + 16]);
628 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
630 for (k = 0; k < 16; k++)
631 score += FFABS(src[k - linesize[0]] - src[k]);
633 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
635 for (k = 0; k < 16; k++)
636 score += FFABS(src[k + linesize[0] * 15] -
637 src[k + linesize[0] * 16]);
640 if (score <= best_score) { // <= will favor the last MV
645 score_sum += best_score;
646 s->mv[0][0][0] = mv_predictor[best_pred][0];
647 s->mv[0][0][1] = mv_predictor[best_pred][1];
649 for (i = 0; i < mot_step; i++)
650 for (j = 0; j < mot_step; j++) {
651 s->cur_pic->motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
652 s->cur_pic->motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
655 s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
656 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
659 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
660 fixed[mb_xy] = MV_CHANGED;
663 fixed[mb_xy] = MV_UNCHANGED;
671 for (i = 0; i < s->mb_num; i++) {
672 int mb_xy = s->mb_index2xy[i];
674 fixed[mb_xy] = MV_FROZEN;
679 static int is_intra_more_likely(ERContext *s)
681 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
683 if (!s->last_pic || !s->last_pic->f.data[0])
684 return 1; // no previous frame available -> use spatial prediction
687 for (i = 0; i < s->mb_num; i++) {
688 const int mb_xy = s->mb_index2xy[i];
689 const int error = s->error_status_table[mb_xy];
690 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
694 if (s->avctx->codec_id == AV_CODEC_ID_H264 && s->ref_count <= 0)
697 if (undamaged_count < 5)
698 return 0; // almost all MBs damaged -> use temporal prediction
700 // prevent dsp.sad() check, that requires access to the image
701 if (CONFIG_MPEG_XVMC_DECODER &&
702 s->avctx->xvmc_acceleration &&
703 s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I)
706 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
710 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
711 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
713 const int mb_xy = mb_x + mb_y * s->mb_stride;
715 error = s->error_status_table[mb_xy];
716 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
717 continue; // skip damaged
720 // skip a few to speed things up
721 if ((j % skip_amount) != 0)
724 if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I) {
725 int *linesize = s->cur_pic->f.linesize;
726 uint8_t *mb_ptr = s->cur_pic->f.data[0] +
727 mb_x * 16 + mb_y * 16 * linesize[0];
728 uint8_t *last_mb_ptr = s->last_pic->f.data[0] +
729 mb_x * 16 + mb_y * 16 * linesize[0];
731 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
734 ff_thread_await_progress(&s->last_pic->tf, mb_y, 0);
736 is_intra_likely += s->dsp->sad[0](NULL, last_mb_ptr, mb_ptr,
738 // FIXME need await_progress() here
739 is_intra_likely -= s->dsp->sad[0](NULL, last_mb_ptr,
740 last_mb_ptr + linesize[0] * 16,
743 if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
750 // av_log(NULL, AV_LOG_ERROR, "is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
751 return is_intra_likely > 0;
754 void ff_er_frame_start(ERContext *s)
756 if (!s->avctx->error_concealment)
759 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
760 s->mb_stride * s->mb_height * sizeof(uint8_t));
761 s->error_count = 3 * s->mb_num;
762 s->error_occurred = 0;
767 * @param endx x component of the last macroblock, can be -1
768 * for the last of the previous line
769 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
770 * assumed that no earlier end or error of the same type occurred
772 void ff_er_add_slice(ERContext *s, int startx, int starty,
773 int endx, int endy, int status)
775 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
776 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
777 const int start_xy = s->mb_index2xy[start_i];
778 const int end_xy = s->mb_index2xy[end_i];
781 if (s->avctx->hwaccel)
784 if (start_i > end_i || start_xy > end_xy) {
785 av_log(s->avctx, AV_LOG_ERROR,
786 "internal error, slice end before start\n");
790 if (!s->avctx->error_concealment)
794 if (status & (ER_AC_ERROR | ER_AC_END)) {
795 mask &= ~(ER_AC_ERROR | ER_AC_END);
796 s->error_count -= end_i - start_i + 1;
798 if (status & (ER_DC_ERROR | ER_DC_END)) {
799 mask &= ~(ER_DC_ERROR | ER_DC_END);
800 s->error_count -= end_i - start_i + 1;
802 if (status & (ER_MV_ERROR | ER_MV_END)) {
803 mask &= ~(ER_MV_ERROR | ER_MV_END);
804 s->error_count -= end_i - start_i + 1;
807 if (status & ER_MB_ERROR) {
808 s->error_occurred = 1;
809 s->error_count = INT_MAX;
813 memset(&s->error_status_table[start_xy], 0,
814 (end_xy - start_xy) * sizeof(uint8_t));
817 for (i = start_xy; i < end_xy; i++)
818 s->error_status_table[i] &= mask;
821 if (end_i == s->mb_num)
822 s->error_count = INT_MAX;
824 s->error_status_table[end_xy] &= mask;
825 s->error_status_table[end_xy] |= status;
828 s->error_status_table[start_xy] |= VP_START;
830 if (start_xy > 0 && !(s->avctx->active_thread_type & FF_THREAD_SLICE) &&
831 s->avctx->skip_top * s->mb_width < start_i) {
832 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
834 prev_status &= ~ VP_START;
835 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END)) {
836 s->error_occurred = 1;
837 s->error_count = INT_MAX;
842 void ff_er_frame_end(ERContext *s)
844 int *linesize = s->cur_pic->f.linesize;
845 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
847 int threshold_part[4] = { 100, 100, 100 };
850 int size = s->b8_stride * 2 * s->mb_height;
852 /* We do not support ER of field pictures yet,
853 * though it should not crash if enabled. */
854 if (!s->avctx->error_concealment || s->error_count == 0 ||
857 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
858 !s->cur_pic || s->cur_pic->field_picture ||
859 s->error_count == 3 * s->mb_width *
860 (s->avctx->skip_top + s->avctx->skip_bottom)) {
863 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
864 int status = s->error_status_table[mb_x + (s->mb_height - 1) * s->mb_stride];
869 if ( mb_x == s->mb_width
870 && s->avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO
871 && (s->avctx->height&16)
872 && s->error_count == 3 * s->mb_width * (s->avctx->skip_top + s->avctx->skip_bottom + 1)
874 av_log(s->avctx, AV_LOG_DEBUG, "ignoring last missing slice\n");
879 if (s->last_pic->f.width != s->cur_pic->f.width ||
880 s->last_pic->f.height != s->cur_pic->f.height ||
881 s->last_pic->f.format != s->cur_pic->f.format) {
882 av_log(s->avctx, AV_LOG_WARNING, "Cannot use previous picture in error concealment\n");
887 if (s->next_pic->f.width != s->cur_pic->f.width ||
888 s->next_pic->f.height != s->cur_pic->f.height ||
889 s->next_pic->f.format != s->cur_pic->f.format) {
890 av_log(s->avctx, AV_LOG_WARNING, "Cannot use next picture in error concealment\n");
895 if (s->cur_pic->motion_val[0] == NULL) {
896 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
898 for (i = 0; i < 2; i++) {
899 s->cur_pic->ref_index_buf[i] = av_buffer_allocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
900 s->cur_pic->motion_val_buf[i] = av_buffer_allocz((size + 4) * 2 * sizeof(uint16_t));
901 if (!s->cur_pic->ref_index_buf[i] || !s->cur_pic->motion_val_buf[i])
903 s->cur_pic->ref_index[i] = s->cur_pic->ref_index_buf[i]->data;
904 s->cur_pic->motion_val[i] = (int16_t (*)[2])s->cur_pic->motion_val_buf[i]->data + 4;
907 for (i = 0; i < 2; i++) {
908 av_buffer_unref(&s->cur_pic->ref_index_buf[i]);
909 av_buffer_unref(&s->cur_pic->motion_val_buf[i]);
910 s->cur_pic->ref_index[i] = NULL;
911 s->cur_pic->motion_val[i] = NULL;
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];
922 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
924 av_log(s->avctx, AV_LOG_DEBUG, "\n");
929 /* handle overlapping slices */
930 for (error_type = 1; error_type <= 3; error_type++) {
933 for (i = s->mb_num - 1; i >= 0; i--) {
934 const int mb_xy = s->mb_index2xy[i];
935 int error = s->error_status_table[mb_xy];
937 if (error & (1 << error_type))
939 if (error & (8 << error_type))
943 s->error_status_table[mb_xy] |= 1 << error_type;
945 if (error & VP_START)
951 /* handle slices with partitions of different length */
952 if (s->partitioned_frame) {
955 for (i = s->mb_num - 1; i >= 0; i--) {
956 const int mb_xy = s->mb_index2xy[i];
957 int error = s->error_status_table[mb_xy];
959 if (error & ER_AC_END)
961 if ((error & ER_MV_END) ||
962 (error & ER_DC_END) ||
963 (error & ER_AC_ERROR))
967 s->error_status_table[mb_xy]|= ER_AC_ERROR;
969 if (error & VP_START)
974 /* handle missing slices */
975 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
979 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
980 const int mb_xy = s->mb_index2xy[i];
981 int error1 = s->error_status_table[mb_xy];
982 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
984 if (error1 & VP_START)
987 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
988 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
989 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
990 (error1 & ER_MV_END))) {
996 s->error_status_table[mb_xy] |= ER_MB_ERROR;
1001 /* backward mark errors */
1003 for (error_type = 1; error_type <= 3; error_type++) {
1004 for (i = s->mb_num - 1; i >= 0; i--) {
1005 const int mb_xy = s->mb_index2xy[i];
1006 int error = s->error_status_table[mb_xy];
1008 if (!s->mbskip_table[mb_xy]) // FIXME partition specific
1010 if (error & (1 << error_type))
1013 if (s->partitioned_frame) {
1014 if (distance < threshold_part[error_type - 1])
1015 s->error_status_table[mb_xy] |= 1 << error_type;
1017 if (distance < threshold)
1018 s->error_status_table[mb_xy] |= 1 << error_type;
1021 if (error & VP_START)
1027 /* forward mark errors */
1029 for (i = 0; i < s->mb_num; i++) {
1030 const int mb_xy = s->mb_index2xy[i];
1031 int old_error = s->error_status_table[mb_xy];
1033 if (old_error & VP_START) {
1034 error = old_error & ER_MB_ERROR;
1036 error |= old_error & ER_MB_ERROR;
1037 s->error_status_table[mb_xy] |= error;
1041 /* handle not partitioned case */
1042 if (!s->partitioned_frame) {
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_MB_ERROR)
1047 error |= ER_MB_ERROR;
1048 s->error_status_table[mb_xy] = error;
1053 dc_error = ac_error = mv_error = 0;
1054 for (i = 0; i < s->mb_num; i++) {
1055 const int mb_xy = s->mb_index2xy[i];
1056 error = s->error_status_table[mb_xy];
1057 if (error & ER_DC_ERROR)
1059 if (error & ER_AC_ERROR)
1061 if (error & ER_MV_ERROR)
1064 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
1065 dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic->f.pict_type));
1067 is_intra_likely = is_intra_more_likely(s);
1069 /* set unknown mb-type to most likely */
1070 for (i = 0; i < s->mb_num; i++) {
1071 const int mb_xy = s->mb_index2xy[i];
1072 error = s->error_status_table[mb_xy];
1073 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1076 if (is_intra_likely)
1077 s->cur_pic->mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1079 s->cur_pic->mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1082 // change inter to intra blocks if no reference frames are available
1083 if (!(s->last_pic && s->last_pic->f.data[0]) &&
1084 !(s->next_pic && s->next_pic->f.data[0]))
1085 for (i = 0; i < s->mb_num; i++) {
1086 const int mb_xy = s->mb_index2xy[i];
1087 if (!IS_INTRA(s->cur_pic->mb_type[mb_xy]))
1088 s->cur_pic->mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1091 /* handle inter blocks with damaged AC */
1092 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1093 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1094 const int mb_xy = mb_x + mb_y * s->mb_stride;
1095 const int mb_type = s->cur_pic->mb_type[mb_xy];
1096 const int dir = !(s->last_pic && s->last_pic->f.data[0]);
1097 const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1100 error = s->error_status_table[mb_xy];
1102 if (IS_INTRA(mb_type))
1104 if (error & ER_MV_ERROR)
1105 continue; // inter with damaged MV
1106 if (!(error & ER_AC_ERROR))
1107 continue; // undamaged inter
1109 if (IS_8X8(mb_type)) {
1110 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1112 mv_type = MV_TYPE_8X8;
1113 for (j = 0; j < 4; j++) {
1114 s->mv[0][j][0] = s->cur_pic->motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1115 s->mv[0][j][1] = s->cur_pic->motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1118 mv_type = MV_TYPE_16X16;
1119 s->mv[0][0][0] = s->cur_pic->motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1120 s->mv[0][0][1] = s->cur_pic->motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1123 s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
1124 mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
1129 if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_B) {
1130 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
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->cur_pic->mb_type[mb_xy];
1135 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1137 error = s->error_status_table[mb_xy];
1139 if (IS_INTRA(mb_type))
1141 if (!(error & ER_MV_ERROR))
1142 continue; // inter with undamaged MV
1143 if (!(error & ER_AC_ERROR))
1144 continue; // undamaged inter
1146 if (!(s->last_pic && s->last_pic->f.data[0]))
1147 mv_dir &= ~MV_DIR_FORWARD;
1148 if (!(s->next_pic && s->next_pic->f.data[0]))
1149 mv_dir &= ~MV_DIR_BACKWARD;
1152 int time_pp = s->pp_time;
1153 int time_pb = s->pb_time;
1155 av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
1156 ff_thread_await_progress(&s->next_pic->tf, mb_y, 0);
1158 s->mv[0][0][0] = s->next_pic->motion_val[0][xy][0] * time_pb / time_pp;
1159 s->mv[0][0][1] = s->next_pic->motion_val[0][xy][1] * time_pb / time_pp;
1160 s->mv[1][0][0] = s->next_pic->motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1161 s->mv[1][0][1] = s->next_pic->motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1169 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
1176 /* the filters below are not XvMC compatible, skip them */
1177 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1179 /* fill DC for inter blocks */
1180 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1181 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1182 int dc, dcu, dcv, y, n;
1184 uint8_t *dest_y, *dest_cb, *dest_cr;
1185 const int mb_xy = mb_x + mb_y * s->mb_stride;
1186 const int mb_type = s->cur_pic->mb_type[mb_xy];
1188 error = s->error_status_table[mb_xy];
1190 if (IS_INTRA(mb_type) && s->partitioned_frame)
1192 // if (error & ER_MV_ERROR)
1193 // continue; // inter data damaged FIXME is this good?
1195 dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1196 dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1197 dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1199 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1200 for (n = 0; n < 4; n++) {
1202 for (y = 0; y < 8; y++) {
1204 for (x = 0; x < 8; x++)
1205 dc += dest_y[x + (n & 1) * 8 +
1206 (y + (n >> 1) * 8) * linesize[0]];
1208 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1212 for (y = 0; y < 8; y++) {
1214 for (x = 0; x < 8; x++) {
1215 dcu += dest_cb[x + y * linesize[1]];
1216 dcv += dest_cr[x + y * linesize[2]];
1219 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1220 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1224 /* guess DC for damaged blocks */
1225 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1226 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
1227 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
1230 /* filter luma DC */
1231 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1234 /* render DC only intra */
1235 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1236 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1237 uint8_t *dest_y, *dest_cb, *dest_cr;
1238 const int mb_xy = mb_x + mb_y * s->mb_stride;
1239 const int mb_type = s->cur_pic->mb_type[mb_xy];
1241 error = s->error_status_table[mb_xy];
1243 if (IS_INTER(mb_type))
1245 if (!(error & ER_AC_ERROR))
1246 continue; // undamaged
1248 dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1249 dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1250 dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1252 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1257 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1258 /* filter horizontal block boundaries */
1259 h_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
1260 s->mb_height * 2, linesize[0], 1);
1261 h_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
1262 s->mb_height, linesize[1], 0);
1263 h_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
1264 s->mb_height, linesize[2], 0);
1266 /* filter vertical block boundaries */
1267 v_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
1268 s->mb_height * 2, linesize[0], 1);
1269 v_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
1270 s->mb_height, linesize[1], 0);
1271 v_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
1272 s->mb_height, linesize[2], 0);
1276 /* clean a few tables */
1277 for (i = 0; i < s->mb_num; i++) {
1278 const int mb_xy = s->mb_index2xy[i];
1279 int error = s->error_status_table[mb_xy];
1281 if (s->cur_pic->f.pict_type != AV_PICTURE_TYPE_B &&
1282 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1283 s->mbskip_table[mb_xy] = 0;
1285 s->mbintra_table[mb_xy] = 1;