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.
30 #include "libavutil/atomic.h"
31 #include "libavutil/internal.h"
33 #include "error_resilience.h"
35 #include "mpegutils.h"
36 #include "mpegvideo.h"
37 #include "rectangle.h"
42 * @param stride the number of MVs to get to the next row
43 * @param mv_step the number of MVs per row or column in a macroblock
45 static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
47 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
48 av_assert0(s->quarter_sample);
50 *stride = s->mb_width * 4;
53 *stride = s->b8_stride;
58 * Replace the current MB with a flat dc-only version.
60 static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
61 uint8_t *dest_cr, int mb_x, int mb_y)
63 int *linesize = s->cur_pic.f->linesize;
64 int dc, dcu, dcv, y, i;
65 for (i = 0; i < 4; i++) {
66 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
71 for (y = 0; y < 8; y++) {
73 for (x = 0; x < 8; x++)
74 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
77 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
78 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
87 for (y = 0; y < 8; y++) {
89 for (x = 0; x < 8; x++) {
90 dest_cb[x + y * linesize[1]] = dcu / 8;
91 dest_cr[x + y * linesize[2]] = dcv / 8;
96 static void filter181(int16_t *data, int width, int height, int stride)
100 /* horizontal filter */
101 for (y = 1; y < height - 1; y++) {
102 int prev_dc = data[0 + y * stride];
104 for (x = 1; x < width - 1; x++) {
107 data[x + y * stride] * 8 -
108 data[x + 1 + y * stride];
109 dc = (dc * 10923 + 32768) >> 16;
110 prev_dc = data[x + y * stride];
111 data[x + y * stride] = dc;
115 /* vertical filter */
116 for (x = 1; x < width - 1; x++) {
117 int prev_dc = data[x];
119 for (y = 1; y < height - 1; y++) {
123 data[x + y * stride] * 8 -
124 data[x + (y + 1) * stride];
125 dc = (dc * 10923 + 32768) >> 16;
126 prev_dc = data[x + y * stride];
127 data[x + y * stride] = dc;
133 * guess the dc of blocks which do not have an undamaged dc
134 * @param w width in 8 pixel blocks
135 * @param h height in 8 pixel blocks
137 static void guess_dc(ERContext *s, int16_t *dc, int w,
138 int h, int stride, int is_luma)
141 int16_t (*col )[4] = av_malloc_array(stride, h*sizeof( int16_t)*4);
142 uint32_t (*dist)[4] = av_malloc_array(stride, h*sizeof(uint32_t)*4);
145 av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
149 for(b_y=0; b_y<h; b_y++){
152 for(b_x=0; b_x<w; b_x++){
153 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
154 int error_j= s->error_status_table[mb_index_j];
155 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
156 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
157 color= dc[b_x + b_y*stride];
160 col [b_x + b_y*stride][1]= color;
161 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
165 for(b_x=w-1; b_x>=0; b_x--){
166 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
167 int error_j= s->error_status_table[mb_index_j];
168 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
169 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
170 color= dc[b_x + b_y*stride];
173 col [b_x + b_y*stride][0]= color;
174 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
177 for(b_x=0; b_x<w; b_x++){
180 for(b_y=0; b_y<h; b_y++){
181 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
182 int error_j= s->error_status_table[mb_index_j];
183 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= dc[b_x + b_y*stride];
188 col [b_x + b_y*stride][3]= color;
189 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
193 for(b_y=h-1; b_y>=0; b_y--){
194 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
195 int error_j= s->error_status_table[mb_index_j];
196 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
197 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
198 color= dc[b_x + b_y*stride];
201 col [b_x + b_y*stride][2]= color;
202 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
206 for (b_y = 0; b_y < h; b_y++) {
207 for (b_x = 0; b_x < w; b_x++) {
208 int mb_index, error, j;
209 int64_t guess, weight_sum;
210 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
211 error = s->error_status_table[mb_index];
213 if (IS_INTER(s->cur_pic.mb_type[mb_index]))
215 if (!(error & ER_DC_ERROR))
220 for (j = 0; j < 4; j++) {
221 int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
222 guess += weight*(int64_t)col[b_x + b_y*stride][j];
223 weight_sum += weight;
225 guess = (guess + weight_sum / 2) / weight_sum;
226 dc[b_x + b_y * stride] = guess;
236 * simple horizontal deblocking filter used for error resilience
237 * @param w width in 8 pixel blocks
238 * @param h height in 8 pixel blocks
240 static void h_block_filter(ERContext *s, uint8_t *dst, int w,
241 int h, int stride, int is_luma)
243 int b_x, b_y, mvx_stride, mvy_stride;
244 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
245 set_mv_strides(s, &mvx_stride, &mvy_stride);
246 mvx_stride >>= is_luma;
247 mvy_stride *= mvx_stride;
249 for (b_y = 0; b_y < h; b_y++) {
250 for (b_x = 0; b_x < w - 1; b_x++) {
252 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
253 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
254 int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
255 int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
256 int left_damage = left_status & ER_MB_ERROR;
257 int right_damage = right_status & ER_MB_ERROR;
258 int offset = b_x * 8 + b_y * stride * 8;
259 int16_t *left_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
260 int16_t *right_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
261 if (!(left_damage || right_damage))
262 continue; // both undamaged
263 if ((!left_intra) && (!right_intra) &&
264 FFABS(left_mv[0] - right_mv[0]) +
265 FFABS(left_mv[1] + right_mv[1]) < 2)
268 for (y = 0; y < 8; y++) {
271 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
272 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
273 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
275 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
283 if (!(left_damage && right_damage))
287 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
288 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
289 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
290 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
293 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
294 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
295 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
296 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
304 * simple vertical deblocking filter used for error resilience
305 * @param w width in 8 pixel blocks
306 * @param h height in 8 pixel blocks
308 static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
309 int stride, int is_luma)
311 int b_x, b_y, mvx_stride, mvy_stride;
312 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
313 set_mv_strides(s, &mvx_stride, &mvy_stride);
314 mvx_stride >>= is_luma;
315 mvy_stride *= mvx_stride;
317 for (b_y = 0; b_y < h - 1; b_y++) {
318 for (b_x = 0; b_x < w; b_x++) {
320 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
321 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
322 int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
323 int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
324 int top_damage = top_status & ER_MB_ERROR;
325 int bottom_damage = bottom_status & ER_MB_ERROR;
326 int offset = b_x * 8 + b_y * stride * 8;
328 int16_t *top_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
329 int16_t *bottom_mv = s->cur_pic.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
331 if (!(top_damage || bottom_damage))
332 continue; // both undamaged
334 if ((!top_intra) && (!bottom_intra) &&
335 FFABS(top_mv[0] - bottom_mv[0]) +
336 FFABS(top_mv[1] + bottom_mv[1]) < 2)
339 for (x = 0; x < 8; x++) {
342 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
343 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
344 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
346 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
354 if (!(top_damage && bottom_damage))
358 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
359 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
360 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
361 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
364 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
365 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
366 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
367 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
374 static void guess_mv(ERContext *s)
376 uint8_t *fixed = s->er_temp_buffer;
379 #define MV_UNCHANGED 1
380 const int mb_stride = s->mb_stride;
381 const int mb_width = s->mb_width;
382 const int mb_height = s->mb_height;
383 int i, depth, num_avail;
384 int mb_x, mb_y, mot_step, mot_stride;
386 set_mv_strides(s, &mot_step, &mot_stride);
389 for (i = 0; i < s->mb_num; i++) {
390 const int mb_xy = s->mb_index2xy[i];
392 int error = s->error_status_table[mb_xy];
394 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
395 f = MV_FROZEN; // intra // FIXME check
396 if (!(error & ER_MV_ERROR))
397 f = MV_FROZEN; // inter with undamaged MV
402 else if(s->last_pic.f->data[0] && s->last_pic.motion_val[0]){
403 const int mb_y= mb_xy / s->mb_stride;
404 const int mb_x= mb_xy % s->mb_stride;
405 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
406 s->cur_pic.motion_val[0][mot_index][0]= s->last_pic.motion_val[0][mot_index][0];
407 s->cur_pic.motion_val[0][mot_index][1]= s->last_pic.motion_val[0][mot_index][1];
408 s->cur_pic.ref_index[0][4*mb_xy] = s->last_pic.ref_index[0][4*mb_xy];
412 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
413 num_avail <= mb_width / 2) {
414 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
415 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
416 const int mb_xy = mb_x + mb_y * s->mb_stride;
417 int mv_dir = (s->last_pic.f && s->last_pic.f->data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
419 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
421 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
426 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
433 for (depth = 0; ; depth++) {
434 int changed, pass, none_left;
438 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
443 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
444 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
445 const int mb_xy = mb_x + mb_y * s->mb_stride;
446 int mv_predictor[8][2] = { { 0 } };
450 int best_score = 256 * 256 * 256 * 64;
452 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
453 int prev_x = 0, prev_y = 0, prev_ref = 0;
455 if ((mb_x ^ mb_y ^ pass) & 1)
458 if (fixed[mb_xy] == MV_FROZEN)
460 av_assert1(!IS_INTRA(s->cur_pic.mb_type[mb_xy]));
461 av_assert1(s->last_pic.f && s->last_pic.f->data[0]);
464 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
466 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
468 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
470 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
476 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
478 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
480 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
482 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
484 if (j == 0 && pass > 1)
489 if (mb_x > 0 && fixed[mb_xy - 1]) {
490 mv_predictor[pred_count][0] =
491 s->cur_pic.motion_val[0][mot_index - mot_step][0];
492 mv_predictor[pred_count][1] =
493 s->cur_pic.motion_val[0][mot_index - mot_step][1];
495 s->cur_pic.ref_index[0][4 * (mb_xy - 1)];
498 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
499 mv_predictor[pred_count][0] =
500 s->cur_pic.motion_val[0][mot_index + mot_step][0];
501 mv_predictor[pred_count][1] =
502 s->cur_pic.motion_val[0][mot_index + mot_step][1];
504 s->cur_pic.ref_index[0][4 * (mb_xy + 1)];
507 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
508 mv_predictor[pred_count][0] =
509 s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][0];
510 mv_predictor[pred_count][1] =
511 s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][1];
513 s->cur_pic.ref_index[0][4 * (mb_xy - s->mb_stride)];
516 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
517 mv_predictor[pred_count][0] =
518 s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][0];
519 mv_predictor[pred_count][1] =
520 s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][1];
522 s->cur_pic.ref_index[0][4 * (mb_xy + s->mb_stride)];
528 if (pred_count > 1) {
529 int sum_x = 0, sum_y = 0, sum_r = 0;
530 int max_x, max_y, min_x, min_y, max_r, min_r;
532 for (j = 0; j < pred_count; j++) {
533 sum_x += mv_predictor[j][0];
534 sum_y += mv_predictor[j][1];
536 if (j && ref[j] != ref[j - 1])
537 goto skip_mean_and_median;
541 mv_predictor[pred_count][0] = sum_x / j;
542 mv_predictor[pred_count][1] = sum_y / j;
543 ref[pred_count] = sum_r / j;
546 if (pred_count >= 3) {
547 min_y = min_x = min_r = 99999;
548 max_y = max_x = max_r = -99999;
550 min_x = min_y = max_x = max_y = min_r = max_r = 0;
552 for (j = 0; j < pred_count; j++) {
553 max_x = FFMAX(max_x, mv_predictor[j][0]);
554 max_y = FFMAX(max_y, mv_predictor[j][1]);
555 max_r = FFMAX(max_r, ref[j]);
556 min_x = FFMIN(min_x, mv_predictor[j][0]);
557 min_y = FFMIN(min_y, mv_predictor[j][1]);
558 min_r = FFMIN(min_r, ref[j]);
560 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
561 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
562 ref[pred_count + 1] = sum_r - max_r - min_r;
564 if (pred_count == 4) {
565 mv_predictor[pred_count + 1][0] /= 2;
566 mv_predictor[pred_count + 1][1] /= 2;
567 ref[pred_count + 1] /= 2;
572 skip_mean_and_median:
576 if (!fixed[mb_xy] && 0) {
577 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
580 ff_thread_await_progress(s->last_pic.tf,
583 if (!s->last_pic.motion_val[0] ||
584 !s->last_pic.ref_index[0])
586 prev_x = s->last_pic.motion_val[0][mot_index][0];
587 prev_y = s->last_pic.motion_val[0][mot_index][1];
588 prev_ref = s->last_pic.ref_index[0][4 * mb_xy];
590 prev_x = s->cur_pic.motion_val[0][mot_index][0];
591 prev_y = s->cur_pic.motion_val[0][mot_index][1];
592 prev_ref = s->cur_pic.ref_index[0][4 * mb_xy];
596 mv_predictor[pred_count][0] = prev_x;
597 mv_predictor[pred_count][1] = prev_y;
598 ref[pred_count] = prev_ref;
603 for (j = 0; j < pred_count; j++) {
604 int *linesize = s->cur_pic.f->linesize;
606 uint8_t *src = s->cur_pic.f->data[0] +
607 mb_x * 16 + mb_y * 16 * linesize[0];
609 s->cur_pic.motion_val[0][mot_index][0] =
610 s->mv[0][0][0] = mv_predictor[j][0];
611 s->cur_pic.motion_val[0][mot_index][1] =
612 s->mv[0][0][1] = mv_predictor[j][1];
614 // predictor intra or otherwise not available
618 s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
619 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
621 if (mb_x > 0 && fixed[mb_xy - 1]) {
623 for (k = 0; k < 16; k++)
624 score += FFABS(src[k * linesize[0] - 1] -
625 src[k * linesize[0]]);
627 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
629 for (k = 0; k < 16; k++)
630 score += FFABS(src[k * linesize[0] + 15] -
631 src[k * linesize[0] + 16]);
633 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
635 for (k = 0; k < 16; k++)
636 score += FFABS(src[k - linesize[0]] - src[k]);
638 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
640 for (k = 0; k < 16; k++)
641 score += FFABS(src[k + linesize[0] * 15] -
642 src[k + linesize[0] * 16]);
645 if (score <= best_score) { // <= will favor the last MV
650 score_sum += best_score;
651 s->mv[0][0][0] = mv_predictor[best_pred][0];
652 s->mv[0][0][1] = mv_predictor[best_pred][1];
654 for (i = 0; i < mot_step; i++)
655 for (j = 0; j < mot_step; j++) {
656 s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
657 s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
660 s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
661 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
664 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
665 fixed[mb_xy] = MV_CHANGED;
668 fixed[mb_xy] = MV_UNCHANGED;
676 for (i = 0; i < s->mb_num; i++) {
677 int mb_xy = s->mb_index2xy[i];
679 fixed[mb_xy] = MV_FROZEN;
684 static int is_intra_more_likely(ERContext *s)
686 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
688 if (!s->last_pic.f || !s->last_pic.f->data[0])
689 return 1; // no previous frame available -> use spatial prediction
691 if (s->avctx->error_concealment & FF_EC_FAVOR_INTER)
695 for (i = 0; i < s->mb_num; i++) {
696 const int mb_xy = s->mb_index2xy[i];
697 const int error = s->error_status_table[mb_xy];
698 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
702 if (undamaged_count < 5)
703 return 0; // almost all MBs damaged -> use temporal prediction
705 // prevent dsp.sad() check, that requires access to the image
707 s->avctx->hwaccel && s->avctx->hwaccel->decode_mb &&
708 s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I)
711 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
715 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
716 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
718 const int mb_xy = mb_x + mb_y * s->mb_stride;
720 error = s->error_status_table[mb_xy];
721 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
722 continue; // skip damaged
725 // skip a few to speed things up
726 if ((j % skip_amount) != 0)
729 if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) {
730 int *linesize = s->cur_pic.f->linesize;
731 uint8_t *mb_ptr = s->cur_pic.f->data[0] +
732 mb_x * 16 + mb_y * 16 * linesize[0];
733 uint8_t *last_mb_ptr = s->last_pic.f->data[0] +
734 mb_x * 16 + mb_y * 16 * linesize[0];
736 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
739 ff_thread_await_progress(s->last_pic.tf, mb_y, 0);
741 is_intra_likely += s->mecc.sad[0](NULL, last_mb_ptr, mb_ptr,
743 // FIXME need await_progress() here
744 is_intra_likely -= s->mecc.sad[0](NULL, last_mb_ptr,
745 last_mb_ptr + linesize[0] * 16,
748 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
755 // av_log(NULL, AV_LOG_ERROR, "is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
756 return is_intra_likely > 0;
759 void ff_er_frame_start(ERContext *s)
761 if (!s->avctx->error_concealment)
764 if (!s->mecc_inited) {
765 ff_me_cmp_init(&s->mecc, s->avctx);
769 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
770 s->mb_stride * s->mb_height * sizeof(uint8_t));
771 s->error_count = 3 * s->mb_num;
772 s->error_occurred = 0;
775 static int er_supported(ERContext *s)
777 if(s->avctx->hwaccel && s->avctx->hwaccel->decode_slice ||
778 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
780 s->cur_pic.field_picture
788 * @param endx x component of the last macroblock, can be -1
789 * for the last of the previous line
790 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
791 * assumed that no earlier end or error of the same type occurred
793 void ff_er_add_slice(ERContext *s, int startx, int starty,
794 int endx, int endy, int status)
796 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
797 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
798 const int start_xy = s->mb_index2xy[start_i];
799 const int end_xy = s->mb_index2xy[end_i];
802 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_slice)
805 if (start_i > end_i || start_xy > end_xy) {
806 av_log(s->avctx, AV_LOG_ERROR,
807 "internal error, slice end before start\n");
811 if (!s->avctx->error_concealment)
815 if (status & (ER_AC_ERROR | ER_AC_END)) {
816 mask &= ~(ER_AC_ERROR | ER_AC_END);
817 avpriv_atomic_int_add_and_fetch(&s->error_count, start_i - end_i - 1);
819 if (status & (ER_DC_ERROR | ER_DC_END)) {
820 mask &= ~(ER_DC_ERROR | ER_DC_END);
821 avpriv_atomic_int_add_and_fetch(&s->error_count, start_i - end_i - 1);
823 if (status & (ER_MV_ERROR | ER_MV_END)) {
824 mask &= ~(ER_MV_ERROR | ER_MV_END);
825 avpriv_atomic_int_add_and_fetch(&s->error_count, start_i - end_i - 1);
828 if (status & ER_MB_ERROR) {
829 s->error_occurred = 1;
830 avpriv_atomic_int_set(&s->error_count, INT_MAX);
834 memset(&s->error_status_table[start_xy], 0,
835 (end_xy - start_xy) * sizeof(uint8_t));
838 for (i = start_xy; i < end_xy; i++)
839 s->error_status_table[i] &= mask;
842 if (end_i == s->mb_num)
843 avpriv_atomic_int_set(&s->error_count, INT_MAX);
845 s->error_status_table[end_xy] &= mask;
846 s->error_status_table[end_xy] |= status;
849 s->error_status_table[start_xy] |= VP_START;
851 if (start_xy > 0 && !(s->avctx->active_thread_type & FF_THREAD_SLICE) &&
852 er_supported(s) && s->avctx->skip_top * s->mb_width < start_i) {
853 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
855 prev_status &= ~ VP_START;
856 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END)) {
857 s->error_occurred = 1;
858 avpriv_atomic_int_set(&s->error_count, INT_MAX);
863 void ff_er_frame_end(ERContext *s)
865 int *linesize = NULL;
866 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
868 int threshold_part[4] = { 100, 100, 100 };
871 int size = s->b8_stride * 2 * s->mb_height;
873 /* We do not support ER of field pictures yet,
874 * though it should not crash if enabled. */
875 if (!s->avctx->error_concealment || s->error_count == 0 ||
878 s->error_count == 3 * s->mb_width *
879 (s->avctx->skip_top + s->avctx->skip_bottom)) {
882 linesize = s->cur_pic.f->linesize;
883 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
884 int status = s->error_status_table[mb_x + (s->mb_height - 1) * s->mb_stride];
889 if ( mb_x == s->mb_width
890 && s->avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO
891 && (s->avctx->height&16)
892 && s->error_count == 3 * s->mb_width * (s->avctx->skip_top + s->avctx->skip_bottom + 1)
894 av_log(s->avctx, AV_LOG_DEBUG, "ignoring last missing slice\n");
899 if (s->last_pic.f->width != s->cur_pic.f->width ||
900 s->last_pic.f->height != s->cur_pic.f->height ||
901 s->last_pic.f->format != s->cur_pic.f->format) {
902 av_log(s->avctx, AV_LOG_WARNING, "Cannot use previous picture in error concealment\n");
903 memset(&s->last_pic, 0, sizeof(s->last_pic));
907 if (s->next_pic.f->width != s->cur_pic.f->width ||
908 s->next_pic.f->height != s->cur_pic.f->height ||
909 s->next_pic.f->format != s->cur_pic.f->format) {
910 av_log(s->avctx, AV_LOG_WARNING, "Cannot use next picture in error concealment\n");
911 memset(&s->next_pic, 0, sizeof(s->next_pic));
915 if (!s->cur_pic.motion_val[0] || !s->cur_pic.ref_index[0]) {
916 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
918 for (i = 0; i < 2; i++) {
919 s->ref_index_buf[i] = av_buffer_allocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
920 s->motion_val_buf[i] = av_buffer_allocz((size + 4) * 2 * sizeof(uint16_t));
921 if (!s->ref_index_buf[i] || !s->motion_val_buf[i])
923 s->cur_pic.ref_index[i] = s->ref_index_buf[i]->data;
924 s->cur_pic.motion_val[i] = (int16_t (*)[2])s->motion_val_buf[i]->data + 4;
927 for (i = 0; i < 2; i++) {
928 av_buffer_unref(&s->ref_index_buf[i]);
929 av_buffer_unref(&s->motion_val_buf[i]);
930 s->cur_pic.ref_index[i] = NULL;
931 s->cur_pic.motion_val[i] = NULL;
937 if (s->avctx->debug & FF_DEBUG_ER) {
938 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
939 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
940 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
942 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
944 av_log(s->avctx, AV_LOG_DEBUG, "\n");
949 /* handle overlapping slices */
950 for (error_type = 1; error_type <= 3; error_type++) {
953 for (i = s->mb_num - 1; i >= 0; i--) {
954 const int mb_xy = s->mb_index2xy[i];
955 int error = s->error_status_table[mb_xy];
957 if (error & (1 << error_type))
959 if (error & (8 << error_type))
963 s->error_status_table[mb_xy] |= 1 << error_type;
965 if (error & VP_START)
971 /* handle slices with partitions of different length */
972 if (s->partitioned_frame) {
975 for (i = s->mb_num - 1; i >= 0; i--) {
976 const int mb_xy = s->mb_index2xy[i];
977 int error = s->error_status_table[mb_xy];
979 if (error & ER_AC_END)
981 if ((error & ER_MV_END) ||
982 (error & ER_DC_END) ||
983 (error & ER_AC_ERROR))
987 s->error_status_table[mb_xy]|= ER_AC_ERROR;
989 if (error & VP_START)
994 /* handle missing slices */
995 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
999 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
1000 const int mb_xy = s->mb_index2xy[i];
1001 int error1 = s->error_status_table[mb_xy];
1002 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
1004 if (error1 & VP_START)
1007 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
1008 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
1009 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
1010 (error1 & ER_MV_END))) {
1016 s->error_status_table[mb_xy] |= ER_MB_ERROR;
1021 /* backward mark errors */
1023 for (error_type = 1; error_type <= 3; error_type++) {
1024 for (i = s->mb_num - 1; i >= 0; i--) {
1025 const int mb_xy = s->mb_index2xy[i];
1026 int error = s->error_status_table[mb_xy];
1028 if (!s->mbskip_table || !s->mbskip_table[mb_xy]) // FIXME partition specific
1030 if (error & (1 << error_type))
1033 if (s->partitioned_frame) {
1034 if (distance < threshold_part[error_type - 1])
1035 s->error_status_table[mb_xy] |= 1 << error_type;
1037 if (distance < threshold)
1038 s->error_status_table[mb_xy] |= 1 << error_type;
1041 if (error & VP_START)
1047 /* forward mark errors */
1049 for (i = 0; i < s->mb_num; i++) {
1050 const int mb_xy = s->mb_index2xy[i];
1051 int old_error = s->error_status_table[mb_xy];
1053 if (old_error & VP_START) {
1054 error = old_error & ER_MB_ERROR;
1056 error |= old_error & ER_MB_ERROR;
1057 s->error_status_table[mb_xy] |= error;
1061 /* handle not partitioned case */
1062 if (!s->partitioned_frame) {
1063 for (i = 0; i < s->mb_num; i++) {
1064 const int mb_xy = s->mb_index2xy[i];
1065 int error = s->error_status_table[mb_xy];
1066 if (error & ER_MB_ERROR)
1067 error |= ER_MB_ERROR;
1068 s->error_status_table[mb_xy] = error;
1073 dc_error = ac_error = mv_error = 0;
1074 for (i = 0; i < s->mb_num; i++) {
1075 const int mb_xy = s->mb_index2xy[i];
1076 int error = s->error_status_table[mb_xy];
1077 if (error & ER_DC_ERROR)
1079 if (error & ER_AC_ERROR)
1081 if (error & ER_MV_ERROR)
1084 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
1085 dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic.f->pict_type));
1087 is_intra_likely = is_intra_more_likely(s);
1089 /* set unknown mb-type to most likely */
1090 for (i = 0; i < s->mb_num; i++) {
1091 const int mb_xy = s->mb_index2xy[i];
1092 int error = s->error_status_table[mb_xy];
1093 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1096 if (is_intra_likely)
1097 s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1099 s->cur_pic.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1102 // change inter to intra blocks if no reference frames are available
1103 if (!(s->last_pic.f && s->last_pic.f->data[0]) &&
1104 !(s->next_pic.f && s->next_pic.f->data[0]))
1105 for (i = 0; i < s->mb_num; i++) {
1106 const int mb_xy = s->mb_index2xy[i];
1107 if (!IS_INTRA(s->cur_pic.mb_type[mb_xy]))
1108 s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1111 /* handle inter blocks with damaged AC */
1112 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1113 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1114 const int mb_xy = mb_x + mb_y * s->mb_stride;
1115 const int mb_type = s->cur_pic.mb_type[mb_xy];
1116 const int dir = !(s->last_pic.f && s->last_pic.f->data[0]);
1117 const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1120 int error = s->error_status_table[mb_xy];
1122 if (IS_INTRA(mb_type))
1124 if (error & ER_MV_ERROR)
1125 continue; // inter with damaged MV
1126 if (!(error & ER_AC_ERROR))
1127 continue; // undamaged inter
1129 if (IS_8X8(mb_type)) {
1130 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1132 mv_type = MV_TYPE_8X8;
1133 for (j = 0; j < 4; j++) {
1134 s->mv[0][j][0] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1135 s->mv[0][j][1] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1138 mv_type = MV_TYPE_16X16;
1139 s->mv[0][0][0] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1140 s->mv[0][0][1] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1143 s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
1144 mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
1149 if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_B) {
1150 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1151 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1152 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1153 const int mb_xy = mb_x + mb_y * s->mb_stride;
1154 const int mb_type = s->cur_pic.mb_type[mb_xy];
1155 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1157 int error = s->error_status_table[mb_xy];
1159 if (IS_INTRA(mb_type))
1161 if (!(error & ER_MV_ERROR))
1162 continue; // inter with undamaged MV
1163 if (!(error & ER_AC_ERROR))
1164 continue; // undamaged inter
1166 if (!(s->last_pic.f && s->last_pic.f->data[0]))
1167 mv_dir &= ~MV_DIR_FORWARD;
1168 if (!(s->next_pic.f && s->next_pic.f->data[0]))
1169 mv_dir &= ~MV_DIR_BACKWARD;
1172 int time_pp = s->pp_time;
1173 int time_pb = s->pb_time;
1175 av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
1176 ff_thread_await_progress(s->next_pic.tf, mb_y, 0);
1178 s->mv[0][0][0] = s->next_pic.motion_val[0][xy][0] * time_pb / time_pp;
1179 s->mv[0][0][1] = s->next_pic.motion_val[0][xy][1] * time_pb / time_pp;
1180 s->mv[1][0][0] = s->next_pic.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1181 s->mv[1][0][1] = s->next_pic.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1189 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
1196 /* the filters below manipulate raw image, skip them */
1197 if (CONFIG_XVMC && s->avctx->hwaccel && s->avctx->hwaccel->decode_mb)
1199 /* fill DC for inter blocks */
1200 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1201 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1202 int dc, dcu, dcv, y, n;
1204 uint8_t *dest_y, *dest_cb, *dest_cr;
1205 const int mb_xy = mb_x + mb_y * s->mb_stride;
1206 const int mb_type = s->cur_pic.mb_type[mb_xy];
1208 // error = s->error_status_table[mb_xy];
1210 if (IS_INTRA(mb_type) && s->partitioned_frame)
1212 // if (error & ER_MV_ERROR)
1213 // continue; // inter data damaged FIXME is this good?
1215 dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1216 dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1217 dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1219 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1220 for (n = 0; n < 4; n++) {
1222 for (y = 0; y < 8; y++) {
1224 for (x = 0; x < 8; x++)
1225 dc += dest_y[x + (n & 1) * 8 +
1226 (y + (n >> 1) * 8) * linesize[0]];
1228 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1232 for (y = 0; y < 8; y++) {
1234 for (x = 0; x < 8; x++) {
1235 dcu += dest_cb[x + y * linesize[1]];
1236 dcv += dest_cr[x + y * linesize[2]];
1239 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1240 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1244 /* guess DC for damaged blocks */
1245 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1246 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
1247 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
1250 /* filter luma DC */
1251 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1254 /* render DC only intra */
1255 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1256 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1257 uint8_t *dest_y, *dest_cb, *dest_cr;
1258 const int mb_xy = mb_x + mb_y * s->mb_stride;
1259 const int mb_type = s->cur_pic.mb_type[mb_xy];
1261 int error = s->error_status_table[mb_xy];
1263 if (IS_INTER(mb_type))
1265 if (!(error & ER_AC_ERROR))
1266 continue; // undamaged
1268 dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1269 dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1270 dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1272 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1277 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1278 /* filter horizontal block boundaries */
1279 h_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
1280 s->mb_height * 2, linesize[0], 1);
1281 h_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
1282 s->mb_height, linesize[1], 0);
1283 h_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
1284 s->mb_height, linesize[2], 0);
1286 /* filter vertical block boundaries */
1287 v_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
1288 s->mb_height * 2, linesize[0], 1);
1289 v_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
1290 s->mb_height, linesize[1], 0);
1291 v_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
1292 s->mb_height, linesize[2], 0);
1296 /* clean a few tables */
1297 for (i = 0; i < s->mb_num; i++) {
1298 const int mb_xy = s->mb_index2xy[i];
1299 int error = s->error_status_table[mb_xy];
1301 if (s->mbskip_table && s->cur_pic.f->pict_type != AV_PICTURE_TYPE_B &&
1302 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1303 s->mbskip_table[mb_xy] = 0;
1305 if (s->mbintra_table)
1306 s->mbintra_table[mb_xy] = 1;
1309 for (i = 0; i < 2; i++) {
1310 av_buffer_unref(&s->ref_index_buf[i]);
1311 av_buffer_unref(&s->motion_val_buf[i]);
1312 s->cur_pic.ref_index[i] = NULL;
1313 s->cur_pic.motion_val[i] = NULL;
1316 memset(&s->cur_pic, 0, sizeof(ERPicture));
1317 memset(&s->last_pic, 0, sizeof(ERPicture));
1318 memset(&s->next_pic, 0, sizeof(ERPicture));