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 "mpegvideo.h"
34 #include "rectangle.h"
38 * H264 redefines mb_intra so it is not mistakely used (its uninitialized in h264)
39 * but error concealment must support both h264 and h263 thus we must undo this
43 static void decode_mb(MpegEncContext *s, int ref)
45 s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
46 s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
47 s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
49 ff_init_block_index(s);
50 ff_update_block_index(s);
51 s->dest[1] += (16 >> s->chroma_x_shift) - 8;
52 s->dest[2] += (16 >> s->chroma_x_shift) - 8;
54 if (CONFIG_H264_DECODER && s->codec_id == AV_CODEC_ID_H264) {
55 H264Context *h = (void*)s;
56 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
57 memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
59 /* FIXME: It is possible albeit uncommon that slice references
60 * differ between slices. We take the easy approach and ignore
61 * it for now. If this turns out to have any relevance in
62 * practice then correct remapping should be added. */
63 if (ref >= h->ref_count[0])
65 if (!h->ref_list[0][ref].f.data[0]) {
66 av_log(s->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
69 if ((h->ref_list[0][ref].f.reference&3) != 3) {
70 av_log(s->avctx, AV_LOG_DEBUG, "Reference invalid\n");
73 fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],
75 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
76 fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
77 pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);
79 h->mb_field_decoding_flag = 0;
80 ff_h264_hl_decode_mb(h);
83 ff_MPV_decode_mb(s, s->block);
88 * @param stride the number of MVs to get to the next row
89 * @param mv_step the number of MVs per row or column in a macroblock
91 static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
93 if (s->codec_id == AV_CODEC_ID_H264) {
94 H264Context *h = (void*)s;
95 av_assert0(s->quarter_sample);
97 *stride = h->b_stride;
100 *stride = s->b8_stride;
105 * Replace the current MB with a flat dc-only version.
107 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
108 uint8_t *dest_cr, int mb_x, int mb_y)
110 int dc, dcu, dcv, y, i;
111 for (i = 0; i < 4; i++) {
112 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
117 for (y = 0; y < 8; y++) {
119 for (x = 0; x < 8; x++)
120 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * s->linesize] = dc / 8;
123 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
124 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
133 for (y = 0; y < 8; y++) {
135 for (x = 0; x < 8; x++) {
136 dest_cb[x + y * s->uvlinesize] = dcu / 8;
137 dest_cr[x + y * s->uvlinesize] = dcv / 8;
142 static void filter181(int16_t *data, int width, int height, int stride)
146 /* horizontal filter */
147 for (y = 1; y < height - 1; y++) {
148 int prev_dc = data[0 + y * stride];
150 for (x = 1; x < width - 1; x++) {
153 data[x + y * stride] * 8 -
154 data[x + 1 + y * stride];
155 dc = (dc * 10923 + 32768) >> 16;
156 prev_dc = data[x + y * stride];
157 data[x + y * stride] = dc;
161 /* vertical filter */
162 for (x = 1; x < width - 1; x++) {
163 int prev_dc = data[x];
165 for (y = 1; y < height - 1; y++) {
169 data[x + y * stride] * 8 -
170 data[x + (y + 1) * stride];
171 dc = (dc * 10923 + 32768) >> 16;
172 prev_dc = data[x + y * stride];
173 data[x + y * stride] = dc;
179 * guess the dc of blocks which do not have an undamaged dc
180 * @param w width in 8 pixel blocks
181 * @param h height in 8 pixel blocks
183 static void guess_dc(MpegEncContext *s, int16_t *dc, int w,
184 int h, int stride, int is_luma)
187 int16_t (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
188 uint32_t (*dist)[4] = av_malloc(stride*h*sizeof(uint32_t)*4);
191 av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
195 for(b_y=0; b_y<h; b_y++){
198 for(b_x=0; b_x<w; b_x++){
199 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
200 int error_j= s->error_status_table[mb_index_j];
201 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
202 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
203 color= dc[b_x + b_y*stride];
206 col [b_x + b_y*stride][1]= color;
207 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
211 for(b_x=w-1; b_x>=0; b_x--){
212 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
213 int error_j= s->error_status_table[mb_index_j];
214 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
215 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
216 color= dc[b_x + b_y*stride];
219 col [b_x + b_y*stride][0]= color;
220 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
223 for(b_x=0; b_x<w; b_x++){
226 for(b_y=0; b_y<h; b_y++){
227 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
228 int error_j= s->error_status_table[mb_index_j];
229 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
230 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
231 color= dc[b_x + b_y*stride];
234 col [b_x + b_y*stride][3]= color;
235 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
239 for(b_y=h-1; b_y>=0; b_y--){
240 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
241 int error_j= s->error_status_table[mb_index_j];
242 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
243 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
244 color= dc[b_x + b_y*stride];
247 col [b_x + b_y*stride][2]= color;
248 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
252 for (b_y = 0; b_y < h; b_y++) {
253 for (b_x = 0; b_x < w; b_x++) {
254 int mb_index, error, j;
255 int64_t guess, weight_sum;
256 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
257 error = s->error_status_table[mb_index];
259 if (IS_INTER(s->current_picture.f.mb_type[mb_index]))
261 if (!(error & ER_DC_ERROR))
266 for (j = 0; j < 4; j++) {
267 int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
268 guess += weight*(int64_t)col[b_x + b_y*stride][j];
269 weight_sum += weight;
271 guess = (guess + weight_sum / 2) / weight_sum;
272 dc[b_x + b_y * stride] = guess;
282 * simple horizontal deblocking filter used for error resilience
283 * @param w width in 8 pixel blocks
284 * @param h height in 8 pixel blocks
286 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w,
287 int h, int stride, int is_luma)
289 int b_x, b_y, mvx_stride, mvy_stride;
290 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
291 set_mv_strides(s, &mvx_stride, &mvy_stride);
292 mvx_stride >>= is_luma;
293 mvy_stride *= mvx_stride;
295 for (b_y = 0; b_y < h; b_y++) {
296 for (b_x = 0; b_x < w - 1; b_x++) {
298 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
299 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
300 int left_intra = IS_INTRA(s->current_picture.f.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
301 int right_intra = IS_INTRA(s->current_picture.f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
302 int left_damage = left_status & ER_MB_ERROR;
303 int right_damage = right_status & ER_MB_ERROR;
304 int offset = b_x * 8 + b_y * stride * 8;
305 int16_t *left_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
306 int16_t *right_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
307 if (!(left_damage || right_damage))
308 continue; // both undamaged
309 if ((!left_intra) && (!right_intra) &&
310 FFABS(left_mv[0] - right_mv[0]) +
311 FFABS(left_mv[1] + right_mv[1]) < 2)
314 for (y = 0; y < 8; y++) {
317 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
318 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
319 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
321 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
329 if (!(left_damage && right_damage))
333 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
334 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
335 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
336 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
339 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
340 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
341 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
342 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
350 * simple vertical deblocking filter used for error resilience
351 * @param w width in 8 pixel blocks
352 * @param h height in 8 pixel blocks
354 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h,
355 int stride, int is_luma)
357 int b_x, b_y, mvx_stride, mvy_stride;
358 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
359 set_mv_strides(s, &mvx_stride, &mvy_stride);
360 mvx_stride >>= is_luma;
361 mvy_stride *= mvx_stride;
363 for (b_y = 0; b_y < h - 1; b_y++) {
364 for (b_x = 0; b_x < w; b_x++) {
366 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
367 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
368 int top_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
369 int bottom_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
370 int top_damage = top_status & ER_MB_ERROR;
371 int bottom_damage = bottom_status & ER_MB_ERROR;
372 int offset = b_x * 8 + b_y * stride * 8;
374 int16_t *top_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
375 int16_t *bottom_mv = s->current_picture.f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
377 if (!(top_damage || bottom_damage))
378 continue; // both undamaged
380 if ((!top_intra) && (!bottom_intra) &&
381 FFABS(top_mv[0] - bottom_mv[0]) +
382 FFABS(top_mv[1] + bottom_mv[1]) < 2)
385 for (x = 0; x < 8; x++) {
388 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
389 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
390 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
392 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
400 if (!(top_damage && bottom_damage))
404 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
405 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
406 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
407 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
410 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
411 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
412 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
413 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
420 static void guess_mv(MpegEncContext *s)
422 uint8_t *fixed = s->er_temp_buffer;
425 #define MV_UNCHANGED 1
426 const int mb_stride = s->mb_stride;
427 const int mb_width = s->mb_width;
428 const int mb_height = s->mb_height;
429 int i, depth, num_avail;
430 int mb_x, mb_y, mot_step, mot_stride;
432 set_mv_strides(s, &mot_step, &mot_stride);
435 for (i = 0; i < s->mb_num; i++) {
436 const int mb_xy = s->mb_index2xy[i];
438 int error = s->error_status_table[mb_xy];
440 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
441 f = MV_FROZEN; // intra // FIXME check
442 if (!(error & ER_MV_ERROR))
443 f = MV_FROZEN; // inter with undamaged MV
448 else if(s->last_picture.f.data[0] && s->last_picture.f.motion_val[0]){
449 const int mb_y= mb_xy / s->mb_stride;
450 const int mb_x= mb_xy % s->mb_stride;
451 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
452 s->current_picture.f.motion_val[0][mot_index][0]= s->last_picture.f.motion_val[0][mot_index][0];
453 s->current_picture.f.motion_val[0][mot_index][1]= s->last_picture.f.motion_val[0][mot_index][1];
454 s->current_picture.f.ref_index[0][4*mb_xy] = s->last_picture.f.ref_index[0][4*mb_xy];
458 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
459 num_avail <= mb_width / 2) {
460 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
461 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
462 const int mb_xy = mb_x + mb_y * s->mb_stride;
464 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
466 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
469 s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD
472 s->mv_type = MV_TYPE_16X16;
475 s->dsp.clear_blocks(s->block[0]);
487 for (depth = 0; ; depth++) {
488 int changed, pass, none_left;
492 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
497 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
498 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
499 const int mb_xy = mb_x + mb_y * s->mb_stride;
500 int mv_predictor[8][2] = { { 0 } };
504 int best_score = 256 * 256 * 256 * 64;
506 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
507 int prev_x, prev_y, prev_ref;
509 if ((mb_x ^ mb_y ^ pass) & 1)
512 if (fixed[mb_xy] == MV_FROZEN)
514 av_assert1(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
515 av_assert1(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
518 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
520 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
522 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
524 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
530 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
532 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
534 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
536 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
538 if (j == 0 && pass > 1)
543 if (mb_x > 0 && fixed[mb_xy - 1]) {
544 mv_predictor[pred_count][0] =
545 s->current_picture.f.motion_val[0][mot_index - mot_step][0];
546 mv_predictor[pred_count][1] =
547 s->current_picture.f.motion_val[0][mot_index - mot_step][1];
549 s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
552 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
553 mv_predictor[pred_count][0] =
554 s->current_picture.f.motion_val[0][mot_index + mot_step][0];
555 mv_predictor[pred_count][1] =
556 s->current_picture.f.motion_val[0][mot_index + mot_step][1];
558 s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
561 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
562 mv_predictor[pred_count][0] =
563 s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
564 mv_predictor[pred_count][1] =
565 s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
567 s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
570 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
571 mv_predictor[pred_count][0] =
572 s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
573 mv_predictor[pred_count][1] =
574 s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
576 s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
582 if (pred_count > 1) {
583 int sum_x = 0, sum_y = 0, sum_r = 0;
584 int max_x, max_y, min_x, min_y, max_r, min_r;
586 for (j = 0; j < pred_count; j++) {
587 sum_x += mv_predictor[j][0];
588 sum_y += mv_predictor[j][1];
590 if (j && ref[j] != ref[j - 1])
591 goto skip_mean_and_median;
595 mv_predictor[pred_count][0] = sum_x / j;
596 mv_predictor[pred_count][1] = sum_y / j;
597 ref[pred_count] = sum_r / j;
600 if (pred_count >= 3) {
601 min_y = min_x = min_r = 99999;
602 max_y = max_x = max_r = -99999;
604 min_x = min_y = max_x = max_y = min_r = max_r = 0;
606 for (j = 0; j < pred_count; j++) {
607 max_x = FFMAX(max_x, mv_predictor[j][0]);
608 max_y = FFMAX(max_y, mv_predictor[j][1]);
609 max_r = FFMAX(max_r, ref[j]);
610 min_x = FFMIN(min_x, mv_predictor[j][0]);
611 min_y = FFMIN(min_y, mv_predictor[j][1]);
612 min_r = FFMIN(min_r, ref[j]);
614 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
615 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
616 ref[pred_count + 1] = sum_r - max_r - min_r;
618 if (pred_count == 4) {
619 mv_predictor[pred_count + 1][0] /= 2;
620 mv_predictor[pred_count + 1][1] /= 2;
621 ref[pred_count + 1] /= 2;
626 skip_mean_and_median:
630 if (!fixed[mb_xy] && 0) {
631 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
634 ff_thread_await_progress(&s->last_picture_ptr->f,
637 if (!s->last_picture.f.motion_val[0] ||
638 !s->last_picture.f.ref_index[0])
640 prev_x = s->last_picture.f.motion_val[0][mot_index][0];
641 prev_y = s->last_picture.f.motion_val[0][mot_index][1];
642 prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
644 prev_x = s->current_picture.f.motion_val[0][mot_index][0];
645 prev_y = s->current_picture.f.motion_val[0][mot_index][1];
646 prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
650 mv_predictor[pred_count][0] = prev_x;
651 mv_predictor[pred_count][1] = prev_y;
652 ref[pred_count] = prev_ref;
656 s->mv_dir = MV_DIR_FORWARD;
658 s->mv_type = MV_TYPE_16X16;
661 s->dsp.clear_blocks(s->block[0]);
666 for (j = 0; j < pred_count; j++) {
668 uint8_t *src = s->current_picture.f.data[0] +
669 mb_x * 16 + mb_y * 16 * s->linesize;
671 s->current_picture.f.motion_val[0][mot_index][0] =
672 s->mv[0][0][0] = mv_predictor[j][0];
673 s->current_picture.f.motion_val[0][mot_index][1] =
674 s->mv[0][0][1] = mv_predictor[j][1];
676 // predictor intra or otherwise not available
680 decode_mb(s, ref[j]);
682 if (mb_x > 0 && fixed[mb_xy - 1]) {
684 for (k = 0; k < 16; k++)
685 score += FFABS(src[k * s->linesize - 1] -
686 src[k * s->linesize]);
688 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
690 for (k = 0; k < 16; k++)
691 score += FFABS(src[k * s->linesize + 15] -
692 src[k * s->linesize + 16]);
694 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
696 for (k = 0; k < 16; k++)
697 score += FFABS(src[k - s->linesize] - src[k]);
699 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
701 for (k = 0; k < 16; k++)
702 score += FFABS(src[k + s->linesize * 15] -
703 src[k + s->linesize * 16]);
706 if (score <= best_score) { // <= will favor the last MV
711 score_sum += best_score;
712 s->mv[0][0][0] = mv_predictor[best_pred][0];
713 s->mv[0][0][1] = mv_predictor[best_pred][1];
715 for (i = 0; i < mot_step; i++)
716 for (j = 0; j < mot_step; j++) {
717 s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
718 s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
721 decode_mb(s, ref[best_pred]);
724 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
725 fixed[mb_xy] = MV_CHANGED;
728 fixed[mb_xy] = MV_UNCHANGED;
736 for (i = 0; i < s->mb_num; i++) {
737 int mb_xy = s->mb_index2xy[i];
739 fixed[mb_xy] = MV_FROZEN;
744 static int is_intra_more_likely(MpegEncContext *s)
746 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
748 if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
749 return 1; // no previous frame available -> use spatial prediction
752 for (i = 0; i < s->mb_num; i++) {
753 const int mb_xy = s->mb_index2xy[i];
754 const int error = s->error_status_table[mb_xy];
755 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
759 if (s->codec_id == AV_CODEC_ID_H264) {
760 H264Context *h = (void*) s;
761 if (h->list_count <= 0 || h->ref_count[0] <= 0 ||
762 !h->ref_list[0][0].f.data[0])
766 if (undamaged_count < 5)
767 return 0; // almost all MBs damaged -> use temporal prediction
769 // prevent dsp.sad() check, that requires access to the image
770 if (CONFIG_MPEG_XVMC_DECODER &&
771 s->avctx->xvmc_acceleration &&
772 s->pict_type == AV_PICTURE_TYPE_I)
775 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
779 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
780 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
782 const int mb_xy = mb_x + mb_y * s->mb_stride;
784 error = s->error_status_table[mb_xy];
785 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
786 continue; // skip damaged
789 // skip a few to speed things up
790 if ((j % skip_amount) != 0)
793 if (s->pict_type == AV_PICTURE_TYPE_I) {
794 uint8_t *mb_ptr = s->current_picture.f.data[0] +
795 mb_x * 16 + mb_y * 16 * s->linesize;
796 uint8_t *last_mb_ptr = s->last_picture.f.data[0] +
797 mb_x * 16 + mb_y * 16 * s->linesize;
799 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
802 ff_thread_await_progress(&s->last_picture_ptr->f,
805 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);
806 // FIXME need await_progress() here
807 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
809 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
816 // printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
817 return is_intra_likely > 0;
820 void ff_er_frame_start(MpegEncContext *s)
822 if (!s->err_recognition)
825 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
826 s->mb_stride * s->mb_height * sizeof(uint8_t));
827 s->error_count = 3 * s->mb_num;
828 s->error_occurred = 0;
833 * @param endx x component of the last macroblock, can be -1
834 * for the last of the previous line
835 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
836 * assumed that no earlier end or error of the same type occurred
838 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
839 int endx, int endy, int status)
841 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
842 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
843 const int start_xy = s->mb_index2xy[start_i];
844 const int end_xy = s->mb_index2xy[end_i];
847 if (s->avctx->hwaccel)
850 if (start_i > end_i || start_xy > end_xy) {
851 av_log(s->avctx, AV_LOG_ERROR,
852 "internal error, slice end before start\n");
856 if (!s->err_recognition)
860 if (status & (ER_AC_ERROR | ER_AC_END)) {
861 mask &= ~(ER_AC_ERROR | ER_AC_END);
862 s->error_count -= end_i - start_i + 1;
864 if (status & (ER_DC_ERROR | ER_DC_END)) {
865 mask &= ~(ER_DC_ERROR | ER_DC_END);
866 s->error_count -= end_i - start_i + 1;
868 if (status & (ER_MV_ERROR | ER_MV_END)) {
869 mask &= ~(ER_MV_ERROR | ER_MV_END);
870 s->error_count -= end_i - start_i + 1;
873 if (status & ER_MB_ERROR) {
874 s->error_occurred = 1;
875 s->error_count = INT_MAX;
879 memset(&s->error_status_table[start_xy], 0,
880 (end_xy - start_xy) * sizeof(uint8_t));
883 for (i = start_xy; i < end_xy; i++)
884 s->error_status_table[i] &= mask;
887 if (end_i == s->mb_num)
888 s->error_count = INT_MAX;
890 s->error_status_table[end_xy] &= mask;
891 s->error_status_table[end_xy] |= status;
894 s->error_status_table[start_xy] |= VP_START;
896 if (start_xy > 0 && s->avctx->thread_count <= 1 &&
897 s->avctx->skip_top * s->mb_width < start_i) {
898 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
900 prev_status &= ~ VP_START;
901 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
902 s->error_count = INT_MAX;
906 void ff_er_frame_end(MpegEncContext *s)
908 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
910 int threshold_part[4] = { 100, 100, 100 };
913 int size = s->b8_stride * 2 * s->mb_height;
914 Picture *pic = s->current_picture_ptr;
916 /* We do not support ER of field pictures yet,
917 * though it should not crash if enabled. */
918 if (!s->err_recognition || s->error_count == 0 || s->avctx->lowres ||
920 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
921 s->picture_structure != PICT_FRAME ||
922 s->error_count == 3 * s->mb_width *
923 (s->avctx->skip_top + s->avctx->skip_bottom)) {
927 if (s->current_picture.f.motion_val[0] == NULL) {
928 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
930 for (i = 0; i < 2; i++) {
931 pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
932 pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
933 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
935 pic->f.motion_subsample_log2 = 3;
936 s->current_picture = *s->current_picture_ptr;
939 if (s->avctx->debug & FF_DEBUG_ER) {
940 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
941 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
942 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
944 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
946 av_log(s->avctx, AV_LOG_DEBUG, "\n");
951 /* handle overlapping slices */
952 for (error_type = 1; error_type <= 3; error_type++) {
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 & (1 << error_type))
961 if (error & (8 << error_type))
965 s->error_status_table[mb_xy] |= 1 << error_type;
967 if (error & VP_START)
973 /* handle slices with partitions of different length */
974 if (s->partitioned_frame) {
977 for (i = s->mb_num - 1; i >= 0; i--) {
978 const int mb_xy = s->mb_index2xy[i];
979 int error = s->error_status_table[mb_xy];
981 if (error & ER_AC_END)
983 if ((error & ER_MV_END) ||
984 (error & ER_DC_END) ||
985 (error & ER_AC_ERROR))
989 s->error_status_table[mb_xy]|= ER_AC_ERROR;
991 if (error & VP_START)
996 /* handle missing slices */
997 if (s->err_recognition & AV_EF_EXPLODE) {
1001 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
1002 const int mb_xy = s->mb_index2xy[i];
1003 int error1 = s->error_status_table[mb_xy];
1004 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
1006 if (error1 & VP_START)
1009 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
1010 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
1011 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
1012 (error1 & ER_MV_END))) {
1018 s->error_status_table[mb_xy] |= ER_MB_ERROR;
1023 /* backward mark errors */
1025 for (error_type = 1; error_type <= 3; error_type++) {
1026 for (i = s->mb_num - 1; i >= 0; i--) {
1027 const int mb_xy = s->mb_index2xy[i];
1028 int error = s->error_status_table[mb_xy];
1030 if (!s->mbskip_table[mb_xy]) // FIXME partition specific
1032 if (error & (1 << error_type))
1035 if (s->partitioned_frame) {
1036 if (distance < threshold_part[error_type - 1])
1037 s->error_status_table[mb_xy] |= 1 << error_type;
1039 if (distance < threshold)
1040 s->error_status_table[mb_xy] |= 1 << error_type;
1043 if (error & VP_START)
1049 /* forward mark errors */
1051 for (i = 0; i < s->mb_num; i++) {
1052 const int mb_xy = s->mb_index2xy[i];
1053 int old_error = s->error_status_table[mb_xy];
1055 if (old_error & VP_START) {
1056 error = old_error & ER_MB_ERROR;
1058 error |= old_error & ER_MB_ERROR;
1059 s->error_status_table[mb_xy] |= error;
1063 /* handle not partitioned case */
1064 if (!s->partitioned_frame) {
1065 for (i = 0; i < s->mb_num; i++) {
1066 const int mb_xy = s->mb_index2xy[i];
1067 error = s->error_status_table[mb_xy];
1068 if (error & ER_MB_ERROR)
1069 error |= ER_MB_ERROR;
1070 s->error_status_table[mb_xy] = error;
1075 dc_error = ac_error = mv_error = 0;
1076 for (i = 0; i < s->mb_num; i++) {
1077 const int mb_xy = s->mb_index2xy[i];
1078 error = s->error_status_table[mb_xy];
1079 if (error & ER_DC_ERROR)
1081 if (error & ER_AC_ERROR)
1083 if (error & ER_MV_ERROR)
1086 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
1087 dc_error, ac_error, mv_error, av_get_picture_type_char(s->pict_type));
1089 is_intra_likely = is_intra_more_likely(s);
1091 /* set unknown mb-type to most likely */
1092 for (i = 0; i < s->mb_num; i++) {
1093 const int mb_xy = s->mb_index2xy[i];
1094 error = s->error_status_table[mb_xy];
1095 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1098 if (is_intra_likely)
1099 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1101 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1104 // change inter to intra blocks if no reference frames are available
1105 if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
1106 for (i = 0; i < s->mb_num; i++) {
1107 const int mb_xy = s->mb_index2xy[i];
1108 if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
1109 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1112 /* handle inter blocks with damaged AC */
1113 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1114 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1115 const int mb_xy = mb_x + mb_y * s->mb_stride;
1116 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1117 int dir = !s->last_picture.f.data[0];
1119 error = s->error_status_table[mb_xy];
1121 if (IS_INTRA(mb_type))
1123 if (error & ER_MV_ERROR)
1124 continue; // inter with damaged MV
1125 if (!(error & ER_AC_ERROR))
1126 continue; // undamaged inter
1128 s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1131 if (IS_8X8(mb_type)) {
1132 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1134 s->mv_type = MV_TYPE_8X8;
1135 for (j = 0; j < 4; j++) {
1136 s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1137 s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1140 s->mv_type = MV_TYPE_16X16;
1141 s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1142 s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1145 s->dsp.clear_blocks(s->block[0]);
1149 decode_mb(s, 0 /* FIXME h264 partitioned slices need this set */);
1154 if (s->pict_type == AV_PICTURE_TYPE_B) {
1155 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1156 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1157 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1158 const int mb_xy = mb_x + mb_y * s->mb_stride;
1159 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1161 error = s->error_status_table[mb_xy];
1163 if (IS_INTRA(mb_type))
1165 if (!(error & ER_MV_ERROR))
1166 continue; // inter with undamaged MV
1167 if (!(error & ER_AC_ERROR))
1168 continue; // undamaged inter
1170 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1171 if (!s->last_picture.f.data[0])
1172 s->mv_dir &= ~MV_DIR_FORWARD;
1173 if (!s->next_picture.f.data[0])
1174 s->mv_dir &= ~MV_DIR_BACKWARD;
1176 s->mv_type = MV_TYPE_16X16;
1180 int time_pp = s->pp_time;
1181 int time_pb = s->pb_time;
1183 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
1186 ff_thread_await_progress(&s->next_picture_ptr->f, mb_y, 0);
1188 s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;
1189 s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;
1190 s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1191 s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1199 s->dsp.clear_blocks(s->block[0]);
1208 /* the filters below are not XvMC compatible, skip them */
1209 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1211 /* fill DC for inter blocks */
1212 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1213 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1214 int dc, dcu, dcv, y, n;
1216 uint8_t *dest_y, *dest_cb, *dest_cr;
1217 const int mb_xy = mb_x + mb_y * s->mb_stride;
1218 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1220 error = s->error_status_table[mb_xy];
1222 if (IS_INTRA(mb_type) && s->partitioned_frame)
1224 // if (error & ER_MV_ERROR)
1225 // continue; // inter data damaged FIXME is this good?
1227 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1228 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1229 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1231 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1232 for (n = 0; n < 4; n++) {
1234 for (y = 0; y < 8; y++) {
1236 for (x = 0; x < 8; x++)
1237 dc += dest_y[x + (n & 1) * 8 +
1238 (y + (n >> 1) * 8) * s->linesize];
1240 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1244 for (y = 0; y < 8; y++) {
1246 for (x = 0; x < 8; x++) {
1247 dcu += dest_cb[x + y * s->uvlinesize];
1248 dcv += dest_cr[x + y * s->uvlinesize];
1251 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1252 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1256 /* guess DC for damaged blocks */
1257 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1258 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
1259 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
1262 /* filter luma DC */
1263 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1266 /* render DC only intra */
1267 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1268 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1269 uint8_t *dest_y, *dest_cb, *dest_cr;
1270 const int mb_xy = mb_x + mb_y * s->mb_stride;
1271 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1273 error = s->error_status_table[mb_xy];
1275 if (IS_INTER(mb_type))
1277 if (!(error & ER_AC_ERROR))
1278 continue; // undamaged
1280 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1281 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1282 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1284 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1289 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1290 /* filter horizontal block boundaries */
1291 h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1292 s->mb_height * 2, s->linesize, 1);
1293 h_block_filter(s, s->current_picture.f.data[1], s->mb_width,
1294 s->mb_height , s->uvlinesize, 0);
1295 h_block_filter(s, s->current_picture.f.data[2], s->mb_width,
1296 s->mb_height , s->uvlinesize, 0);
1298 /* filter vertical block boundaries */
1299 v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1300 s->mb_height * 2, s->linesize, 1);
1301 v_block_filter(s, s->current_picture.f.data[1], s->mb_width,
1302 s->mb_height , s->uvlinesize, 0);
1303 v_block_filter(s, s->current_picture.f.data[2], s->mb_width,
1304 s->mb_height , s->uvlinesize, 0);
1308 /* clean a few tables */
1309 for (i = 0; i < s->mb_num; i++) {
1310 const int mb_xy = s->mb_index2xy[i];
1311 int error = s->error_status_table[mb_xy];
1313 if (s->pict_type != AV_PICTURE_TYPE_B &&
1314 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1315 s->mbskip_table[mb_xy] = 0;
1317 s->mbintra_table[mb_xy] = 1;