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.
32 #include "error_resilience.h"
33 #include "mpegvideo.h"
34 #include "rectangle.h"
38 * @param stride the number of MVs to get to the next row
39 * @param mv_step the number of MVs per row or column in a macroblock
41 static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
43 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
44 av_assert0(s->quarter_sample);
46 *stride = s->mb_width * 4;
49 *stride = s->b8_stride;
54 * Replace the current MB with a flat dc-only version.
56 static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
57 uint8_t *dest_cr, int mb_x, int mb_y)
59 int *linesize = s->cur_pic->f.linesize;
60 int dc, dcu, dcv, y, i;
61 for (i = 0; i < 4; i++) {
62 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
67 for (y = 0; y < 8; y++) {
69 for (x = 0; x < 8; x++)
70 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
73 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
74 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
83 for (y = 0; y < 8; y++) {
85 for (x = 0; x < 8; x++) {
86 dest_cb[x + y * linesize[1]] = dcu / 8;
87 dest_cr[x + y * linesize[2]] = dcv / 8;
92 static void filter181(int16_t *data, int width, int height, int stride)
96 /* horizontal filter */
97 for (y = 1; y < height - 1; y++) {
98 int prev_dc = data[0 + y * stride];
100 for (x = 1; x < width - 1; x++) {
103 data[x + y * stride] * 8 -
104 data[x + 1 + y * stride];
105 dc = (dc * 10923 + 32768) >> 16;
106 prev_dc = data[x + y * stride];
107 data[x + y * stride] = dc;
111 /* vertical filter */
112 for (x = 1; x < width - 1; x++) {
113 int prev_dc = data[x];
115 for (y = 1; y < height - 1; y++) {
119 data[x + y * stride] * 8 -
120 data[x + (y + 1) * stride];
121 dc = (dc * 10923 + 32768) >> 16;
122 prev_dc = data[x + y * stride];
123 data[x + y * stride] = dc;
129 * guess the dc of blocks which do not have an undamaged dc
130 * @param w width in 8 pixel blocks
131 * @param h height in 8 pixel blocks
133 static void guess_dc(ERContext *s, int16_t *dc, int w,
134 int h, int stride, int is_luma)
137 int16_t (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
138 uint32_t (*dist)[4] = av_malloc(stride*h*sizeof(uint32_t)*4);
141 av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
145 for(b_y=0; b_y<h; b_y++){
148 for(b_x=0; b_x<w; b_x++){
149 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
150 int error_j= s->error_status_table[mb_index_j];
151 int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
152 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
153 color= dc[b_x + b_y*stride];
156 col [b_x + b_y*stride][1]= color;
157 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
161 for(b_x=w-1; b_x>=0; b_x--){
162 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
163 int error_j= s->error_status_table[mb_index_j];
164 int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
165 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
166 color= dc[b_x + b_y*stride];
169 col [b_x + b_y*stride][0]= color;
170 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
173 for(b_x=0; b_x<w; b_x++){
176 for(b_y=0; b_y<h; b_y++){
177 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
178 int error_j= s->error_status_table[mb_index_j];
179 int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
180 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
181 color= dc[b_x + b_y*stride];
184 col [b_x + b_y*stride][3]= color;
185 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
189 for(b_y=h-1; b_y>=0; b_y--){
190 int mb_index_j= (b_x>>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->cur_pic->f.mb_type[mb_index_j]);
193 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
194 color= dc[b_x + b_y*stride];
197 col [b_x + b_y*stride][2]= color;
198 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
202 for (b_y = 0; b_y < h; b_y++) {
203 for (b_x = 0; b_x < w; b_x++) {
204 int mb_index, error, j;
205 int64_t guess, weight_sum;
206 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
207 error = s->error_status_table[mb_index];
209 if (IS_INTER(s->cur_pic->f.mb_type[mb_index]))
211 if (!(error & ER_DC_ERROR))
216 for (j = 0; j < 4; j++) {
217 int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
218 guess += weight*(int64_t)col[b_x + b_y*stride][j];
219 weight_sum += weight;
221 guess = (guess + weight_sum / 2) / weight_sum;
222 dc[b_x + b_y * stride] = guess;
232 * simple horizontal deblocking filter used for error resilience
233 * @param w width in 8 pixel blocks
234 * @param h height in 8 pixel blocks
236 static void h_block_filter(ERContext *s, uint8_t *dst, int w,
237 int h, int stride, int is_luma)
239 int b_x, b_y, mvx_stride, mvy_stride;
240 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
241 set_mv_strides(s, &mvx_stride, &mvy_stride);
242 mvx_stride >>= is_luma;
243 mvy_stride *= mvx_stride;
245 for (b_y = 0; b_y < h; b_y++) {
246 for (b_x = 0; b_x < w - 1; b_x++) {
248 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
249 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
250 int left_intra = IS_INTRA(s->cur_pic->f.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
251 int right_intra = IS_INTRA(s->cur_pic->f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
252 int left_damage = left_status & ER_MB_ERROR;
253 int right_damage = right_status & ER_MB_ERROR;
254 int offset = b_x * 8 + b_y * stride * 8;
255 int16_t *left_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
256 int16_t *right_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
257 if (!(left_damage || right_damage))
258 continue; // both undamaged
259 if ((!left_intra) && (!right_intra) &&
260 FFABS(left_mv[0] - right_mv[0]) +
261 FFABS(left_mv[1] + right_mv[1]) < 2)
264 for (y = 0; y < 8; y++) {
267 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
268 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
269 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
271 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
279 if (!(left_damage && right_damage))
283 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
284 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
285 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
286 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
289 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
290 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
291 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
292 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
300 * simple vertical deblocking filter used for error resilience
301 * @param w width in 8 pixel blocks
302 * @param h height in 8 pixel blocks
304 static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
305 int stride, int is_luma)
307 int b_x, b_y, mvx_stride, mvy_stride;
308 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
309 set_mv_strides(s, &mvx_stride, &mvy_stride);
310 mvx_stride >>= is_luma;
311 mvy_stride *= mvx_stride;
313 for (b_y = 0; b_y < h - 1; b_y++) {
314 for (b_x = 0; b_x < w; b_x++) {
316 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
317 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
318 int top_intra = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
319 int bottom_intra = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
320 int top_damage = top_status & ER_MB_ERROR;
321 int bottom_damage = bottom_status & ER_MB_ERROR;
322 int offset = b_x * 8 + b_y * stride * 8;
324 int16_t *top_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
325 int16_t *bottom_mv = s->cur_pic->f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
327 if (!(top_damage || bottom_damage))
328 continue; // both undamaged
330 if ((!top_intra) && (!bottom_intra) &&
331 FFABS(top_mv[0] - bottom_mv[0]) +
332 FFABS(top_mv[1] + bottom_mv[1]) < 2)
335 for (x = 0; x < 8; x++) {
338 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
339 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
340 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
342 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
350 if (!(top_damage && bottom_damage))
354 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
355 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
356 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
357 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
360 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
361 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
362 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
363 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
370 static void guess_mv(ERContext *s)
372 uint8_t *fixed = s->er_temp_buffer;
375 #define MV_UNCHANGED 1
376 const int mb_stride = s->mb_stride;
377 const int mb_width = s->mb_width;
378 const int mb_height = s->mb_height;
379 int i, depth, num_avail;
380 int mb_x, mb_y, mot_step, mot_stride;
382 set_mv_strides(s, &mot_step, &mot_stride);
385 for (i = 0; i < s->mb_num; i++) {
386 const int mb_xy = s->mb_index2xy[i];
388 int error = s->error_status_table[mb_xy];
390 if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
391 f = MV_FROZEN; // intra // FIXME check
392 if (!(error & ER_MV_ERROR))
393 f = MV_FROZEN; // inter with undamaged MV
398 else if(s->last_pic->f.data[0] && s->last_pic->f.motion_val[0]){
399 const int mb_y= mb_xy / s->mb_stride;
400 const int mb_x= mb_xy % s->mb_stride;
401 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
402 s->cur_pic->f.motion_val[0][mot_index][0]= s->last_pic->f.motion_val[0][mot_index][0];
403 s->cur_pic->f.motion_val[0][mot_index][1]= s->last_pic->f.motion_val[0][mot_index][1];
404 s->cur_pic->f.ref_index[0][4*mb_xy] = s->last_pic->f.ref_index[0][4*mb_xy];
408 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
409 num_avail <= mb_width / 2) {
410 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
411 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
412 const int mb_xy = mb_x + mb_y * s->mb_stride;
413 int mv_dir = (s->last_pic && s->last_pic->f.data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
415 if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
417 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
422 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
429 for (depth = 0; ; depth++) {
430 int changed, pass, none_left;
434 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
439 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
440 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
441 const int mb_xy = mb_x + mb_y * s->mb_stride;
442 int mv_predictor[8][2] = { { 0 } };
446 int best_score = 256 * 256 * 256 * 64;
448 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
449 int prev_x, prev_y, prev_ref;
451 if ((mb_x ^ mb_y ^ pass) & 1)
454 if (fixed[mb_xy] == MV_FROZEN)
456 av_assert1(!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]));
457 av_assert1(s->last_pic && s->last_pic->f.data[0]);
460 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
462 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
464 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
466 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
472 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
474 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
476 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
478 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
480 if (j == 0 && pass > 1)
485 if (mb_x > 0 && fixed[mb_xy - 1]) {
486 mv_predictor[pred_count][0] =
487 s->cur_pic->f.motion_val[0][mot_index - mot_step][0];
488 mv_predictor[pred_count][1] =
489 s->cur_pic->f.motion_val[0][mot_index - mot_step][1];
491 s->cur_pic->f.ref_index[0][4 * (mb_xy - 1)];
494 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
495 mv_predictor[pred_count][0] =
496 s->cur_pic->f.motion_val[0][mot_index + mot_step][0];
497 mv_predictor[pred_count][1] =
498 s->cur_pic->f.motion_val[0][mot_index + mot_step][1];
500 s->cur_pic->f.ref_index[0][4 * (mb_xy + 1)];
503 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
504 mv_predictor[pred_count][0] =
505 s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][0];
506 mv_predictor[pred_count][1] =
507 s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][1];
509 s->cur_pic->f.ref_index[0][4 * (mb_xy - s->mb_stride)];
512 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
513 mv_predictor[pred_count][0] =
514 s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][0];
515 mv_predictor[pred_count][1] =
516 s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][1];
518 s->cur_pic->f.ref_index[0][4 * (mb_xy + s->mb_stride)];
524 if (pred_count > 1) {
525 int sum_x = 0, sum_y = 0, sum_r = 0;
526 int max_x, max_y, min_x, min_y, max_r, min_r;
528 for (j = 0; j < pred_count; j++) {
529 sum_x += mv_predictor[j][0];
530 sum_y += mv_predictor[j][1];
532 if (j && ref[j] != ref[j - 1])
533 goto skip_mean_and_median;
537 mv_predictor[pred_count][0] = sum_x / j;
538 mv_predictor[pred_count][1] = sum_y / j;
539 ref[pred_count] = sum_r / j;
542 if (pred_count >= 3) {
543 min_y = min_x = min_r = 99999;
544 max_y = max_x = max_r = -99999;
546 min_x = min_y = max_x = max_y = min_r = max_r = 0;
548 for (j = 0; j < pred_count; j++) {
549 max_x = FFMAX(max_x, mv_predictor[j][0]);
550 max_y = FFMAX(max_y, mv_predictor[j][1]);
551 max_r = FFMAX(max_r, ref[j]);
552 min_x = FFMIN(min_x, mv_predictor[j][0]);
553 min_y = FFMIN(min_y, mv_predictor[j][1]);
554 min_r = FFMIN(min_r, ref[j]);
556 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
557 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
558 ref[pred_count + 1] = sum_r - max_r - min_r;
560 if (pred_count == 4) {
561 mv_predictor[pred_count + 1][0] /= 2;
562 mv_predictor[pred_count + 1][1] /= 2;
563 ref[pred_count + 1] /= 2;
568 skip_mean_and_median:
572 if (!fixed[mb_xy] && 0) {
573 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
576 ff_thread_await_progress(&s->last_pic->f,
579 if (!s->last_pic->f.motion_val[0] ||
580 !s->last_pic->f.ref_index[0])
582 prev_x = s->last_pic->f.motion_val[0][mot_index][0];
583 prev_y = s->last_pic->f.motion_val[0][mot_index][1];
584 prev_ref = s->last_pic->f.ref_index[0][4 * mb_xy];
586 prev_x = s->cur_pic->f.motion_val[0][mot_index][0];
587 prev_y = s->cur_pic->f.motion_val[0][mot_index][1];
588 prev_ref = s->cur_pic->f.ref_index[0][4 * mb_xy];
592 mv_predictor[pred_count][0] = prev_x;
593 mv_predictor[pred_count][1] = prev_y;
594 ref[pred_count] = prev_ref;
599 for (j = 0; j < pred_count; j++) {
600 int *linesize = s->cur_pic->f.linesize;
602 uint8_t *src = s->cur_pic->f.data[0] +
603 mb_x * 16 + mb_y * 16 * linesize[0];
605 s->cur_pic->f.motion_val[0][mot_index][0] =
606 s->mv[0][0][0] = mv_predictor[j][0];
607 s->cur_pic->f.motion_val[0][mot_index][1] =
608 s->mv[0][0][1] = mv_predictor[j][1];
610 // predictor intra or otherwise not available
614 s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
615 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
617 if (mb_x > 0 && fixed[mb_xy - 1]) {
619 for (k = 0; k < 16; k++)
620 score += FFABS(src[k * linesize[0] - 1] -
621 src[k * linesize[0]]);
623 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
625 for (k = 0; k < 16; k++)
626 score += FFABS(src[k * linesize[0] + 15] -
627 src[k * linesize[0] + 16]);
629 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
631 for (k = 0; k < 16; k++)
632 score += FFABS(src[k - linesize[0]] - src[k]);
634 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
636 for (k = 0; k < 16; k++)
637 score += FFABS(src[k + linesize[0] * 15] -
638 src[k + linesize[0] * 16]);
641 if (score <= best_score) { // <= will favor the last MV
646 score_sum += best_score;
647 s->mv[0][0][0] = mv_predictor[best_pred][0];
648 s->mv[0][0][1] = mv_predictor[best_pred][1];
650 for (i = 0; i < mot_step; i++)
651 for (j = 0; j < mot_step; j++) {
652 s->cur_pic->f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
653 s->cur_pic->f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
656 s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
657 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
660 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
661 fixed[mb_xy] = MV_CHANGED;
664 fixed[mb_xy] = MV_UNCHANGED;
672 for (i = 0; i < s->mb_num; i++) {
673 int mb_xy = s->mb_index2xy[i];
675 fixed[mb_xy] = MV_FROZEN;
680 static int is_intra_more_likely(ERContext *s)
682 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
684 if (!s->last_pic || !s->last_pic->f.data[0])
685 return 1; // no previous frame available -> use spatial prediction
688 for (i = 0; i < s->mb_num; i++) {
689 const int mb_xy = s->mb_index2xy[i];
690 const int error = s->error_status_table[mb_xy];
691 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
695 if (s->avctx->codec_id == AV_CODEC_ID_H264 && s->ref_count <= 0)
698 if (undamaged_count < 5)
699 return 0; // almost all MBs damaged -> use temporal prediction
701 // prevent dsp.sad() check, that requires access to the image
702 if (CONFIG_MPEG_XVMC_DECODER &&
703 s->avctx->xvmc_acceleration &&
704 s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I)
707 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
711 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
712 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
714 const int mb_xy = mb_x + mb_y * s->mb_stride;
716 error = s->error_status_table[mb_xy];
717 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
718 continue; // skip damaged
721 // skip a few to speed things up
722 if ((j % skip_amount) != 0)
725 if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I) {
726 int *linesize = s->cur_pic->f.linesize;
727 uint8_t *mb_ptr = s->cur_pic->f.data[0] +
728 mb_x * 16 + mb_y * 16 * linesize[0];
729 uint8_t *last_mb_ptr = s->last_pic->f.data[0] +
730 mb_x * 16 + mb_y * 16 * linesize[0];
732 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
735 ff_thread_await_progress(&s->last_pic->f, mb_y, 0);
737 is_intra_likely += s->dsp->sad[0](NULL, last_mb_ptr, mb_ptr,
739 // FIXME need await_progress() here
740 is_intra_likely -= s->dsp->sad[0](NULL, last_mb_ptr,
741 last_mb_ptr + linesize[0] * 16,
744 if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
751 // printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
752 return is_intra_likely > 0;
755 void ff_er_frame_start(ERContext *s)
757 if (!s->avctx->err_recognition)
760 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
761 s->mb_stride * s->mb_height * sizeof(uint8_t));
762 s->error_count = 3 * s->mb_num;
763 s->error_occurred = 0;
768 * @param endx x component of the last macroblock, can be -1
769 * for the last of the previous line
770 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
771 * assumed that no earlier end or error of the same type occurred
773 void ff_er_add_slice(ERContext *s, int startx, int starty,
774 int endx, int endy, int status)
776 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
777 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
778 const int start_xy = s->mb_index2xy[start_i];
779 const int end_xy = s->mb_index2xy[end_i];
782 if (s->avctx->hwaccel)
785 if (start_i > end_i || start_xy > end_xy) {
786 av_log(s->avctx, AV_LOG_ERROR,
787 "internal error, slice end before start\n");
791 if (!s->avctx->err_recognition)
795 if (status & (ER_AC_ERROR | ER_AC_END)) {
796 mask &= ~(ER_AC_ERROR | ER_AC_END);
797 s->error_count -= end_i - start_i + 1;
799 if (status & (ER_DC_ERROR | ER_DC_END)) {
800 mask &= ~(ER_DC_ERROR | ER_DC_END);
801 s->error_count -= end_i - start_i + 1;
803 if (status & (ER_MV_ERROR | ER_MV_END)) {
804 mask &= ~(ER_MV_ERROR | ER_MV_END);
805 s->error_count -= end_i - start_i + 1;
808 if (status & ER_MB_ERROR) {
809 s->error_occurred = 1;
810 s->error_count = INT_MAX;
814 memset(&s->error_status_table[start_xy], 0,
815 (end_xy - start_xy) * sizeof(uint8_t));
818 for (i = start_xy; i < end_xy; i++)
819 s->error_status_table[i] &= mask;
822 if (end_i == s->mb_num)
823 s->error_count = INT_MAX;
825 s->error_status_table[end_xy] &= mask;
826 s->error_status_table[end_xy] |= status;
829 s->error_status_table[start_xy] |= VP_START;
831 if (start_xy > 0 && s->avctx->thread_count <= 1 &&
832 s->avctx->skip_top * s->mb_width < start_i) {
833 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
835 prev_status &= ~ VP_START;
836 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
837 s->error_count = INT_MAX;
841 void ff_er_frame_end(ERContext *s)
843 int *linesize = s->cur_pic->f.linesize;
844 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
846 int threshold_part[4] = { 100, 100, 100 };
849 int size = s->b8_stride * 2 * s->mb_height;
851 /* We do not support ER of field pictures yet,
852 * though it should not crash if enabled. */
853 if (!s->avctx->err_recognition || s->error_count == 0 ||
856 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
857 !s->cur_pic || s->cur_pic->field_picture ||
858 s->error_count == 3 * s->mb_width *
859 (s->avctx->skip_top + s->avctx->skip_bottom)) {
863 if (s->cur_pic->f.motion_val[0] == NULL) {
864 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
866 for (i = 0; i < 2; i++) {
867 s->cur_pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
868 s->cur_pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
869 s->cur_pic->f.motion_val[i] = s->cur_pic->motion_val_base[i] + 4;
871 s->cur_pic->f.motion_subsample_log2 = 3;
874 if (s->avctx->debug & FF_DEBUG_ER) {
875 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
876 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
877 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
879 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
881 av_log(s->avctx, AV_LOG_DEBUG, "\n");
886 /* handle overlapping slices */
887 for (error_type = 1; error_type <= 3; error_type++) {
890 for (i = s->mb_num - 1; i >= 0; i--) {
891 const int mb_xy = s->mb_index2xy[i];
892 int error = s->error_status_table[mb_xy];
894 if (error & (1 << error_type))
896 if (error & (8 << error_type))
900 s->error_status_table[mb_xy] |= 1 << error_type;
902 if (error & VP_START)
908 /* handle slices with partitions of different length */
909 if (s->partitioned_frame) {
912 for (i = s->mb_num - 1; i >= 0; i--) {
913 const int mb_xy = s->mb_index2xy[i];
914 int error = s->error_status_table[mb_xy];
916 if (error & ER_AC_END)
918 if ((error & ER_MV_END) ||
919 (error & ER_DC_END) ||
920 (error & ER_AC_ERROR))
924 s->error_status_table[mb_xy]|= ER_AC_ERROR;
926 if (error & VP_START)
931 /* handle missing slices */
932 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
936 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
937 const int mb_xy = s->mb_index2xy[i];
938 int error1 = s->error_status_table[mb_xy];
939 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
941 if (error1 & VP_START)
944 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
945 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
946 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
947 (error1 & ER_MV_END))) {
953 s->error_status_table[mb_xy] |= ER_MB_ERROR;
958 /* backward mark errors */
960 for (error_type = 1; error_type <= 3; error_type++) {
961 for (i = s->mb_num - 1; i >= 0; i--) {
962 const int mb_xy = s->mb_index2xy[i];
963 int error = s->error_status_table[mb_xy];
965 if (!s->mbskip_table[mb_xy]) // FIXME partition specific
967 if (error & (1 << error_type))
970 if (s->partitioned_frame) {
971 if (distance < threshold_part[error_type - 1])
972 s->error_status_table[mb_xy] |= 1 << error_type;
974 if (distance < threshold)
975 s->error_status_table[mb_xy] |= 1 << error_type;
978 if (error & VP_START)
984 /* forward mark errors */
986 for (i = 0; i < s->mb_num; i++) {
987 const int mb_xy = s->mb_index2xy[i];
988 int old_error = s->error_status_table[mb_xy];
990 if (old_error & VP_START) {
991 error = old_error & ER_MB_ERROR;
993 error |= old_error & ER_MB_ERROR;
994 s->error_status_table[mb_xy] |= error;
998 /* handle not partitioned case */
999 if (!s->partitioned_frame) {
1000 for (i = 0; i < s->mb_num; i++) {
1001 const int mb_xy = s->mb_index2xy[i];
1002 error = s->error_status_table[mb_xy];
1003 if (error & ER_MB_ERROR)
1004 error |= ER_MB_ERROR;
1005 s->error_status_table[mb_xy] = error;
1010 dc_error = ac_error = mv_error = 0;
1011 for (i = 0; i < s->mb_num; i++) {
1012 const int mb_xy = s->mb_index2xy[i];
1013 error = s->error_status_table[mb_xy];
1014 if (error & ER_DC_ERROR)
1016 if (error & ER_AC_ERROR)
1018 if (error & ER_MV_ERROR)
1021 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
1022 dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic->f.pict_type));
1024 is_intra_likely = is_intra_more_likely(s);
1026 /* set unknown mb-type to most likely */
1027 for (i = 0; i < s->mb_num; i++) {
1028 const int mb_xy = s->mb_index2xy[i];
1029 error = s->error_status_table[mb_xy];
1030 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1033 if (is_intra_likely)
1034 s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1036 s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1039 // change inter to intra blocks if no reference frames are available
1040 if (!(s->last_pic && s->last_pic->f.data[0]) &&
1041 !(s->next_pic && s->next_pic->f.data[0]))
1042 for (i = 0; i < s->mb_num; i++) {
1043 const int mb_xy = s->mb_index2xy[i];
1044 if (!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
1045 s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1048 /* handle inter blocks with damaged AC */
1049 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1050 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1051 const int mb_xy = mb_x + mb_y * s->mb_stride;
1052 const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1053 const int dir = !(s->last_pic && s->last_pic->f.data[0]);
1054 const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1057 error = s->error_status_table[mb_xy];
1059 if (IS_INTRA(mb_type))
1061 if (error & ER_MV_ERROR)
1062 continue; // inter with damaged MV
1063 if (!(error & ER_AC_ERROR))
1064 continue; // undamaged inter
1066 if (IS_8X8(mb_type)) {
1067 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1069 mv_type = MV_TYPE_8X8;
1070 for (j = 0; j < 4; j++) {
1071 s->mv[0][j][0] = s->cur_pic->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1072 s->mv[0][j][1] = s->cur_pic->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1075 mv_type = MV_TYPE_16X16;
1076 s->mv[0][0][0] = s->cur_pic->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1077 s->mv[0][0][1] = s->cur_pic->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1080 s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
1081 mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
1086 if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_B) {
1087 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1088 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1089 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1090 const int mb_xy = mb_x + mb_y * s->mb_stride;
1091 const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1092 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1094 error = s->error_status_table[mb_xy];
1096 if (IS_INTRA(mb_type))
1098 if (!(error & ER_MV_ERROR))
1099 continue; // inter with undamaged MV
1100 if (!(error & ER_AC_ERROR))
1101 continue; // undamaged inter
1103 if (!(s->last_pic && s->last_pic->f.data[0]))
1104 mv_dir &= ~MV_DIR_FORWARD;
1105 if (!(s->next_pic && s->next_pic->f.data[0]))
1106 mv_dir &= ~MV_DIR_BACKWARD;
1109 int time_pp = s->pp_time;
1110 int time_pb = s->pb_time;
1112 av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
1113 ff_thread_await_progress(&s->next_pic->f, mb_y, 0);
1115 s->mv[0][0][0] = s->next_pic->f.motion_val[0][xy][0] * time_pb / time_pp;
1116 s->mv[0][0][1] = s->next_pic->f.motion_val[0][xy][1] * time_pb / time_pp;
1117 s->mv[1][0][0] = s->next_pic->f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1118 s->mv[1][0][1] = s->next_pic->f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1126 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
1133 /* the filters below are not XvMC compatible, skip them */
1134 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1136 /* fill DC for inter blocks */
1137 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1138 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1139 int dc, dcu, dcv, y, n;
1141 uint8_t *dest_y, *dest_cb, *dest_cr;
1142 const int mb_xy = mb_x + mb_y * s->mb_stride;
1143 const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1145 error = s->error_status_table[mb_xy];
1147 if (IS_INTRA(mb_type) && s->partitioned_frame)
1149 // if (error & ER_MV_ERROR)
1150 // continue; // inter data damaged FIXME is this good?
1152 dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1153 dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1154 dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1156 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1157 for (n = 0; n < 4; n++) {
1159 for (y = 0; y < 8; y++) {
1161 for (x = 0; x < 8; x++)
1162 dc += dest_y[x + (n & 1) * 8 +
1163 (y + (n >> 1) * 8) * linesize[0]];
1165 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1169 for (y = 0; y < 8; y++) {
1171 for (x = 0; x < 8; x++) {
1172 dcu += dest_cb[x + y * linesize[1]];
1173 dcv += dest_cr[x + y * linesize[2]];
1176 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1177 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1181 /* guess DC for damaged blocks */
1182 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1183 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
1184 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
1187 /* filter luma DC */
1188 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1191 /* render DC only intra */
1192 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1193 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1194 uint8_t *dest_y, *dest_cb, *dest_cr;
1195 const int mb_xy = mb_x + mb_y * s->mb_stride;
1196 const int mb_type = s->cur_pic->f.mb_type[mb_xy];
1198 error = s->error_status_table[mb_xy];
1200 if (IS_INTER(mb_type))
1202 if (!(error & ER_AC_ERROR))
1203 continue; // undamaged
1205 dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1206 dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1207 dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1209 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1214 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1215 /* filter horizontal block boundaries */
1216 h_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
1217 s->mb_height * 2, linesize[0], 1);
1218 h_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
1219 s->mb_height, linesize[1], 0);
1220 h_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
1221 s->mb_height, linesize[2], 0);
1223 /* filter vertical block boundaries */
1224 v_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
1225 s->mb_height * 2, linesize[0], 1);
1226 v_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
1227 s->mb_height, linesize[1], 0);
1228 v_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
1229 s->mb_height, linesize[2], 0);
1233 /* clean a few tables */
1234 for (i = 0; i < s->mb_num; i++) {
1235 const int mb_xy = s->mb_index2xy[i];
1236 int error = s->error_status_table[mb_xy];
1238 if (s->cur_pic->f.pict_type != AV_PICTURE_TYPE_B &&
1239 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1240 s->mbskip_table[mb_xy] = 0;
1242 s->mbintra_table[mb_xy] = 1;