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);
52 if (CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264) {
53 H264Context *h = (void*)s;
54 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
55 memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
57 /* FIXME: It is possible albeit uncommon that slice references
58 * differ between slices. We take the easy approach and ignore
59 * it for now. If this turns out to have any relevance in
60 * practice then correct remapping should be added. */
61 if (ref >= h->ref_count[0])
63 fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],
65 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
66 fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
67 pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);
69 ff_h264_hl_decode_mb(h);
72 ff_MPV_decode_mb(s, s->block);
77 * @param stride the number of MVs to get to the next row
78 * @param mv_step the number of MVs per row or column in a macroblock
80 static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
82 if (s->codec_id == CODEC_ID_H264) {
83 H264Context *h = (void*)s;
84 assert(s->quarter_sample);
86 *stride = h->b_stride;
89 *stride = s->b8_stride;
94 * Replace the current MB with a flat dc-only version.
96 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
97 uint8_t *dest_cr, int mb_x, int mb_y)
99 int dc, dcu, dcv, y, i;
100 for (i = 0; i < 4; i++) {
101 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
106 for (y = 0; y < 8; y++) {
108 for (x = 0; x < 8; x++)
109 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * s->linesize] = dc / 8;
112 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
113 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
122 for (y = 0; y < 8; y++) {
124 for (x = 0; x < 8; x++) {
125 dest_cb[x + y * s->uvlinesize] = dcu / 8;
126 dest_cr[x + y * s->uvlinesize] = dcv / 8;
131 static void filter181(int16_t *data, int width, int height, int stride)
135 /* horizontal filter */
136 for (y = 1; y < height - 1; y++) {
137 int prev_dc = data[0 + y * stride];
139 for (x = 1; x < width - 1; x++) {
142 data[x + y * stride] * 8 -
143 data[x + 1 + y * stride];
144 dc = (dc * 10923 + 32768) >> 16;
145 prev_dc = data[x + y * stride];
146 data[x + y * stride] = dc;
150 /* vertical filter */
151 for (x = 1; x < width - 1; x++) {
152 int prev_dc = data[x];
154 for (y = 1; y < height - 1; y++) {
158 data[x + y * stride] * 8 -
159 data[x + (y + 1) * stride];
160 dc = (dc * 10923 + 32768) >> 16;
161 prev_dc = data[x + y * stride];
162 data[x + y * stride] = dc;
168 * guess the dc of blocks which do not have an undamaged dc
169 * @param w width in 8 pixel blocks
170 * @param h height in 8 pixel blocks
172 static void guess_dc(MpegEncContext *s, int16_t *dc, int w,
173 int h, int stride, int is_luma)
176 int16_t (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
177 uint16_t (*dist)[4] = av_malloc(stride*h*sizeof(uint16_t)*4);
179 for(b_y=0; b_y<h; b_y++){
182 for(b_x=0; b_x<w; b_x++){
183 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
184 int error_j= s->error_status_table[mb_index_j];
185 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
186 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
187 color= dc[b_x + b_y*stride];
190 col [b_x + b_y*stride][1]= color;
191 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
195 for(b_x=w-1; b_x>=0; b_x--){
196 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
197 int error_j= s->error_status_table[mb_index_j];
198 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
199 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
200 color= dc[b_x + b_y*stride];
203 col [b_x + b_y*stride][0]= color;
204 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
207 for(b_x=0; b_x<w; b_x++){
210 for(b_y=0; b_y<h; b_y++){
211 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
212 int error_j= s->error_status_table[mb_index_j];
213 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
214 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
215 color= dc[b_x + b_y*stride];
218 col [b_x + b_y*stride][3]= color;
219 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
223 for(b_y=h-1; b_y>=0; b_y--){
224 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
225 int error_j= s->error_status_table[mb_index_j];
226 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
227 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
228 color= dc[b_x + b_y*stride];
231 col [b_x + b_y*stride][2]= color;
232 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
236 for (b_y = 0; b_y < h; b_y++) {
237 for (b_x = 0; b_x < w; b_x++) {
238 int mb_index, error, j;
239 int64_t guess, weight_sum;
240 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
241 error = s->error_status_table[mb_index];
243 if (IS_INTER(s->current_picture.f.mb_type[mb_index]))
245 if (!(error & ER_DC_ERROR))
250 for (j = 0; j < 4; j++) {
251 int64_t weight = 256 * 256 * 256 * 16 / dist[b_x + b_y*stride][j];
252 guess += weight*(int64_t)col[b_x + b_y*stride][j];
253 weight_sum += weight;
255 guess = (guess + weight_sum / 2) / weight_sum;
256 dc[b_x + b_y * stride] = guess;
264 * simple horizontal deblocking filter used for error resilience
265 * @param w width in 8 pixel blocks
266 * @param h height in 8 pixel blocks
268 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w,
269 int h, int stride, int is_luma)
271 int b_x, b_y, mvx_stride, mvy_stride;
272 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
273 set_mv_strides(s, &mvx_stride, &mvy_stride);
274 mvx_stride >>= is_luma;
275 mvy_stride *= mvx_stride;
277 for (b_y = 0; b_y < h; b_y++) {
278 for (b_x = 0; b_x < w - 1; b_x++) {
280 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
281 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
282 int left_intra = IS_INTRA(s->current_picture.f.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
283 int right_intra = IS_INTRA(s->current_picture.f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
284 int left_damage = left_status & ER_MB_ERROR;
285 int right_damage = right_status & ER_MB_ERROR;
286 int offset = b_x * 8 + b_y * stride * 8;
287 int16_t *left_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
288 int16_t *right_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
289 if (!(left_damage || right_damage))
290 continue; // both undamaged
291 if ((!left_intra) && (!right_intra) &&
292 FFABS(left_mv[0] - right_mv[0]) +
293 FFABS(left_mv[1] + right_mv[1]) < 2)
296 for (y = 0; y < 8; y++) {
299 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
300 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
301 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
303 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
311 if (!(left_damage && right_damage))
315 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
316 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
317 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
318 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
321 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
322 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
323 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
324 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
332 * simple vertical deblocking filter used for error resilience
333 * @param w width in 8 pixel blocks
334 * @param h height in 8 pixel blocks
336 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h,
337 int stride, int is_luma)
339 int b_x, b_y, mvx_stride, mvy_stride;
340 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
341 set_mv_strides(s, &mvx_stride, &mvy_stride);
342 mvx_stride >>= is_luma;
343 mvy_stride *= mvx_stride;
345 for (b_y = 0; b_y < h - 1; b_y++) {
346 for (b_x = 0; b_x < w; b_x++) {
348 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
349 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
350 int top_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
351 int bottom_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
352 int top_damage = top_status & ER_MB_ERROR;
353 int bottom_damage = bottom_status & ER_MB_ERROR;
354 int offset = b_x * 8 + b_y * stride * 8;
356 int16_t *top_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
357 int16_t *bottom_mv = s->current_picture.f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
359 if (!(top_damage || bottom_damage))
360 continue; // both undamaged
362 if ((!top_intra) && (!bottom_intra) &&
363 FFABS(top_mv[0] - bottom_mv[0]) +
364 FFABS(top_mv[1] + bottom_mv[1]) < 2)
367 for (x = 0; x < 8; x++) {
370 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
371 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
372 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
374 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
382 if (!(top_damage && bottom_damage))
386 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
387 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
388 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
389 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
392 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
393 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
394 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
395 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
402 static void guess_mv(MpegEncContext *s)
404 uint8_t *fixed = av_malloc(s->mb_stride * s->mb_height);
407 #define MV_UNCHANGED 1
408 const int mb_stride = s->mb_stride;
409 const int mb_width = s->mb_width;
410 const int mb_height = s->mb_height;
411 int i, depth, num_avail;
412 int mb_x, mb_y, mot_step, mot_stride;
414 set_mv_strides(s, &mot_step, &mot_stride);
417 for (i = 0; i < s->mb_num; i++) {
418 const int mb_xy = s->mb_index2xy[i];
420 int error = s->error_status_table[mb_xy];
422 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
423 f = MV_FROZEN; // intra // FIXME check
424 if (!(error & ER_MV_ERROR))
425 f = MV_FROZEN; // inter with undamaged MV
430 else if(s->last_picture.f.data[0] && s->last_picture.f.motion_val[0]){
431 const int mb_y= mb_xy / s->mb_stride;
432 const int mb_x= mb_xy % s->mb_stride;
433 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
434 s->current_picture.f.motion_val[0][mot_index][0]= s->last_picture.f.motion_val[0][mot_index][0];
435 s->current_picture.f.motion_val[0][mot_index][1]= s->last_picture.f.motion_val[0][mot_index][1];
436 s->current_picture.f.ref_index[0][4*mb_xy] = s->last_picture.f.ref_index[0][4*mb_xy];
440 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
441 num_avail <= mb_width / 2) {
442 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
443 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
444 const int mb_xy = mb_x + mb_y * s->mb_stride;
446 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
448 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
451 s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD
454 s->mv_type = MV_TYPE_16X16;
457 s->dsp.clear_blocks(s->block[0]);
469 for (depth = 0; ; depth++) {
470 int changed, pass, none_left;
474 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
479 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
480 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
481 const int mb_xy = mb_x + mb_y * s->mb_stride;
482 int mv_predictor[8][2] = { { 0 } };
486 int best_score = 256 * 256 * 256 * 64;
488 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
489 int prev_x, prev_y, prev_ref;
491 if ((mb_x ^ mb_y ^ pass) & 1)
494 if (fixed[mb_xy] == MV_FROZEN)
496 assert(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
497 assert(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
500 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
502 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
504 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
506 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
512 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
514 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
516 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
518 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
520 if (j == 0 && pass > 1)
525 if (mb_x > 0 && fixed[mb_xy - 1]) {
526 mv_predictor[pred_count][0] =
527 s->current_picture.f.motion_val[0][mot_index - mot_step][0];
528 mv_predictor[pred_count][1] =
529 s->current_picture.f.motion_val[0][mot_index - mot_step][1];
531 s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
534 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
535 mv_predictor[pred_count][0] =
536 s->current_picture.f.motion_val[0][mot_index + mot_step][0];
537 mv_predictor[pred_count][1] =
538 s->current_picture.f.motion_val[0][mot_index + mot_step][1];
540 s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
543 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
544 mv_predictor[pred_count][0] =
545 s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
546 mv_predictor[pred_count][1] =
547 s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
549 s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
552 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
553 mv_predictor[pred_count][0] =
554 s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
555 mv_predictor[pred_count][1] =
556 s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
558 s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
564 if (pred_count > 1) {
565 int sum_x = 0, sum_y = 0, sum_r = 0;
566 int max_x, max_y, min_x, min_y, max_r, min_r;
568 for (j = 0; j < pred_count; j++) {
569 sum_x += mv_predictor[j][0];
570 sum_y += mv_predictor[j][1];
572 if (j && ref[j] != ref[j - 1])
573 goto skip_mean_and_median;
577 mv_predictor[pred_count][0] = sum_x / j;
578 mv_predictor[pred_count][1] = sum_y / j;
579 ref[pred_count] = sum_r / j;
582 if (pred_count >= 3) {
583 min_y = min_x = min_r = 99999;
584 max_y = max_x = max_r = -99999;
586 min_x = min_y = max_x = max_y = min_r = max_r = 0;
588 for (j = 0; j < pred_count; j++) {
589 max_x = FFMAX(max_x, mv_predictor[j][0]);
590 max_y = FFMAX(max_y, mv_predictor[j][1]);
591 max_r = FFMAX(max_r, ref[j]);
592 min_x = FFMIN(min_x, mv_predictor[j][0]);
593 min_y = FFMIN(min_y, mv_predictor[j][1]);
594 min_r = FFMIN(min_r, ref[j]);
596 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
597 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
598 ref[pred_count + 1] = sum_r - max_r - min_r;
600 if (pred_count == 4) {
601 mv_predictor[pred_count + 1][0] /= 2;
602 mv_predictor[pred_count + 1][1] /= 2;
603 ref[pred_count + 1] /= 2;
608 skip_mean_and_median:
612 if (!fixed[mb_xy] && 0) {
613 if (s->avctx->codec_id == CODEC_ID_H264) {
616 ff_thread_await_progress(&s->last_picture_ptr->f,
619 if (!s->last_picture.f.motion_val[0] ||
620 !s->last_picture.f.ref_index[0])
622 prev_x = s->last_picture.f.motion_val[0][mot_index][0];
623 prev_y = s->last_picture.f.motion_val[0][mot_index][1];
624 prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
626 prev_x = s->current_picture.f.motion_val[0][mot_index][0];
627 prev_y = s->current_picture.f.motion_val[0][mot_index][1];
628 prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
632 mv_predictor[pred_count][0] = prev_x;
633 mv_predictor[pred_count][1] = prev_y;
634 ref[pred_count] = prev_ref;
638 s->mv_dir = MV_DIR_FORWARD;
640 s->mv_type = MV_TYPE_16X16;
643 s->dsp.clear_blocks(s->block[0]);
648 for (j = 0; j < pred_count; j++) {
650 uint8_t *src = s->current_picture.f.data[0] +
651 mb_x * 16 + mb_y * 16 * s->linesize;
653 s->current_picture.f.motion_val[0][mot_index][0] =
654 s->mv[0][0][0] = mv_predictor[j][0];
655 s->current_picture.f.motion_val[0][mot_index][1] =
656 s->mv[0][0][1] = mv_predictor[j][1];
658 // predictor intra or otherwise not available
662 decode_mb(s, ref[j]);
664 if (mb_x > 0 && fixed[mb_xy - 1]) {
666 for (k = 0; k < 16; k++)
667 score += FFABS(src[k * s->linesize - 1] -
668 src[k * s->linesize]);
670 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
672 for (k = 0; k < 16; k++)
673 score += FFABS(src[k * s->linesize + 15] -
674 src[k * s->linesize + 16]);
676 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
678 for (k = 0; k < 16; k++)
679 score += FFABS(src[k - s->linesize] - src[k]);
681 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
683 for (k = 0; k < 16; k++)
684 score += FFABS(src[k + s->linesize * 15] -
685 src[k + s->linesize * 16]);
688 if (score <= best_score) { // <= will favor the last MV
693 score_sum += best_score;
694 s->mv[0][0][0] = mv_predictor[best_pred][0];
695 s->mv[0][0][1] = mv_predictor[best_pred][1];
697 for (i = 0; i < mot_step; i++)
698 for (j = 0; j < mot_step; j++) {
699 s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
700 s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
703 decode_mb(s, ref[best_pred]);
706 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
707 fixed[mb_xy] = MV_CHANGED;
710 fixed[mb_xy] = MV_UNCHANGED;
714 // printf(".%d/%d", changed, score_sum); fflush(stdout);
720 for (i = 0; i < s->mb_num; i++) {
721 int mb_xy = s->mb_index2xy[i];
723 fixed[mb_xy] = MV_FROZEN;
725 // printf(":"); fflush(stdout);
731 static int is_intra_more_likely(MpegEncContext *s)
733 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
735 if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
736 return 1; // no previous frame available -> use spatial prediction
739 for (i = 0; i < s->mb_num; i++) {
740 const int mb_xy = s->mb_index2xy[i];
741 const int error = s->error_status_table[mb_xy];
742 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
746 if (s->codec_id == CODEC_ID_H264) {
747 H264Context *h = (void*) s;
748 if (h->list_count <= 0 || h->ref_count[0] <= 0 ||
749 !h->ref_list[0][0].f.data[0])
753 if (undamaged_count < 5)
754 return 0; // almost all MBs damaged -> use temporal prediction
756 // prevent dsp.sad() check, that requires access to the image
757 if (CONFIG_MPEG_XVMC_DECODER &&
758 s->avctx->xvmc_acceleration &&
759 s->pict_type == AV_PICTURE_TYPE_I)
762 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
766 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
767 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
769 const int mb_xy = mb_x + mb_y * s->mb_stride;
771 error = s->error_status_table[mb_xy];
772 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
773 continue; // skip damaged
776 // skip a few to speed things up
777 if ((j % skip_amount) != 0)
780 if (s->pict_type == AV_PICTURE_TYPE_I) {
781 uint8_t *mb_ptr = s->current_picture.f.data[0] +
782 mb_x * 16 + mb_y * 16 * s->linesize;
783 uint8_t *last_mb_ptr = s->last_picture.f.data[0] +
784 mb_x * 16 + mb_y * 16 * s->linesize;
786 if (s->avctx->codec_id == CODEC_ID_H264) {
789 ff_thread_await_progress(&s->last_picture_ptr->f,
792 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);
793 // FIXME need await_progress() here
794 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
796 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
803 // printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
804 return is_intra_likely > 0;
807 void ff_er_frame_start(MpegEncContext *s)
809 if (!s->err_recognition)
812 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
813 s->mb_stride * s->mb_height * sizeof(uint8_t));
814 s->error_count = 3 * s->mb_num;
815 s->error_occurred = 0;
820 * @param endx x component of the last macroblock, can be -1
821 * for the last of the previous line
822 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
823 * assumed that no earlier end or error of the same type occurred
825 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
826 int endx, int endy, int status)
828 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
829 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
830 const int start_xy = s->mb_index2xy[start_i];
831 const int end_xy = s->mb_index2xy[end_i];
834 if (s->avctx->hwaccel)
837 if (start_i > end_i || start_xy > end_xy) {
838 av_log(s->avctx, AV_LOG_ERROR,
839 "internal error, slice end before start\n");
843 if (!s->err_recognition)
847 if (status & (ER_AC_ERROR | ER_AC_END)) {
848 mask &= ~(ER_AC_ERROR | ER_AC_END);
849 s->error_count -= end_i - start_i + 1;
851 if (status & (ER_DC_ERROR | ER_DC_END)) {
852 mask &= ~(ER_DC_ERROR | ER_DC_END);
853 s->error_count -= end_i - start_i + 1;
855 if (status & (ER_MV_ERROR | ER_MV_END)) {
856 mask &= ~(ER_MV_ERROR | ER_MV_END);
857 s->error_count -= end_i - start_i + 1;
860 if (status & ER_MB_ERROR) {
861 s->error_occurred = 1;
862 s->error_count = INT_MAX;
866 memset(&s->error_status_table[start_xy], 0,
867 (end_xy - start_xy) * sizeof(uint8_t));
870 for (i = start_xy; i < end_xy; i++)
871 s->error_status_table[i] &= mask;
874 if (end_i == s->mb_num)
875 s->error_count = INT_MAX;
877 s->error_status_table[end_xy] &= mask;
878 s->error_status_table[end_xy] |= status;
881 s->error_status_table[start_xy] |= VP_START;
883 if (start_xy > 0 && s->avctx->thread_count <= 1 &&
884 s->avctx->skip_top * s->mb_width < start_i) {
885 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
887 prev_status &= ~ VP_START;
888 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
889 s->error_count = INT_MAX;
893 void ff_er_frame_end(MpegEncContext *s)
895 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
897 int threshold_part[4] = { 100, 100, 100 };
900 int size = s->b8_stride * 2 * s->mb_height;
901 Picture *pic = s->current_picture_ptr;
903 /* We do not support ER of field pictures yet,
904 * though it should not crash if enabled. */
905 if (!s->err_recognition || s->error_count == 0 || s->avctx->lowres ||
907 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
908 s->picture_structure != PICT_FRAME ||
909 s->error_count == 3 * s->mb_width *
910 (s->avctx->skip_top + s->avctx->skip_bottom)) {
914 if (s->current_picture.f.motion_val[0] == NULL) {
915 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
917 for (i = 0; i < 2; i++) {
918 pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
919 pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
920 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
922 pic->f.motion_subsample_log2 = 3;
923 s->current_picture = *s->current_picture_ptr;
926 if (s->avctx->debug & FF_DEBUG_ER) {
927 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
928 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
929 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
931 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
933 av_log(s->avctx, AV_LOG_DEBUG, "\n");
938 /* handle overlapping slices */
939 for (error_type = 1; error_type <= 3; error_type++) {
942 for (i = s->mb_num - 1; i >= 0; i--) {
943 const int mb_xy = s->mb_index2xy[i];
944 int error = s->error_status_table[mb_xy];
946 if (error & (1 << error_type))
948 if (error & (8 << error_type))
952 s->error_status_table[mb_xy] |= 1 << error_type;
954 if (error & VP_START)
960 /* handle slices with partitions of different length */
961 if (s->partitioned_frame) {
964 for (i = s->mb_num - 1; i >= 0; i--) {
965 const int mb_xy = s->mb_index2xy[i];
966 int error = s->error_status_table[mb_xy];
968 if (error & ER_AC_END)
970 if ((error & ER_MV_END) ||
971 (error & ER_DC_END) ||
972 (error & ER_AC_ERROR))
976 s->error_status_table[mb_xy]|= ER_AC_ERROR;
978 if (error & VP_START)
983 /* handle missing slices */
984 if (s->err_recognition & AV_EF_EXPLODE) {
988 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
989 const int mb_xy = s->mb_index2xy[i];
990 int error1 = s->error_status_table[mb_xy];
991 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
993 if (error1 & VP_START)
996 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
997 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
998 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
999 (error1 & ER_MV_END))) {
1005 s->error_status_table[mb_xy] |= ER_MB_ERROR;
1010 /* backward mark errors */
1012 for (error_type = 1; error_type <= 3; error_type++) {
1013 for (i = s->mb_num - 1; i >= 0; i--) {
1014 const int mb_xy = s->mb_index2xy[i];
1015 int error = s->error_status_table[mb_xy];
1017 if (!s->mbskip_table[mb_xy]) // FIXME partition specific
1019 if (error & (1 << error_type))
1022 if (s->partitioned_frame) {
1023 if (distance < threshold_part[error_type - 1])
1024 s->error_status_table[mb_xy] |= 1 << error_type;
1026 if (distance < threshold)
1027 s->error_status_table[mb_xy] |= 1 << error_type;
1030 if (error & VP_START)
1036 /* forward mark errors */
1038 for (i = 0; i < s->mb_num; i++) {
1039 const int mb_xy = s->mb_index2xy[i];
1040 int old_error = s->error_status_table[mb_xy];
1042 if (old_error & VP_START) {
1043 error = old_error & ER_MB_ERROR;
1045 error |= old_error & ER_MB_ERROR;
1046 s->error_status_table[mb_xy] |= error;
1050 /* handle not partitioned case */
1051 if (!s->partitioned_frame) {
1052 for (i = 0; i < s->mb_num; i++) {
1053 const int mb_xy = s->mb_index2xy[i];
1054 error = s->error_status_table[mb_xy];
1055 if (error & ER_MB_ERROR)
1056 error |= ER_MB_ERROR;
1057 s->error_status_table[mb_xy] = error;
1062 dc_error = ac_error = mv_error = 0;
1063 for (i = 0; i < s->mb_num; i++) {
1064 const int mb_xy = s->mb_index2xy[i];
1065 error = s->error_status_table[mb_xy];
1066 if (error & ER_DC_ERROR)
1068 if (error & ER_AC_ERROR)
1070 if (error & ER_MV_ERROR)
1073 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
1074 dc_error, ac_error, mv_error);
1076 is_intra_likely = is_intra_more_likely(s);
1078 /* set unknown mb-type to most likely */
1079 for (i = 0; i < s->mb_num; i++) {
1080 const int mb_xy = s->mb_index2xy[i];
1081 error = s->error_status_table[mb_xy];
1082 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1085 if (is_intra_likely)
1086 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1088 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1091 // change inter to intra blocks if no reference frames are available
1092 if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
1093 for (i = 0; i < s->mb_num; i++) {
1094 const int mb_xy = s->mb_index2xy[i];
1095 if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
1096 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1099 /* handle inter blocks with damaged AC */
1100 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1101 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1102 const int mb_xy = mb_x + mb_y * s->mb_stride;
1103 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1104 int dir = !s->last_picture.f.data[0];
1106 error = s->error_status_table[mb_xy];
1108 if (IS_INTRA(mb_type))
1110 if (error & ER_MV_ERROR)
1111 continue; // inter with damaged MV
1112 if (!(error & ER_AC_ERROR))
1113 continue; // undamaged inter
1115 s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1118 if (IS_8X8(mb_type)) {
1119 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1121 s->mv_type = MV_TYPE_8X8;
1122 for (j = 0; j < 4; j++) {
1123 s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1124 s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1127 s->mv_type = MV_TYPE_16X16;
1128 s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1129 s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1132 s->dsp.clear_blocks(s->block[0]);
1136 decode_mb(s, 0 /* FIXME h264 partitioned slices need this set */);
1141 if (s->pict_type == AV_PICTURE_TYPE_B) {
1142 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1143 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1144 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1145 const int mb_xy = mb_x + mb_y * s->mb_stride;
1146 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1148 error = s->error_status_table[mb_xy];
1150 if (IS_INTRA(mb_type))
1152 if (!(error & ER_MV_ERROR))
1153 continue; // inter with undamaged MV
1154 if (!(error & ER_AC_ERROR))
1155 continue; // undamaged inter
1157 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1158 if (!s->last_picture.f.data[0])
1159 s->mv_dir &= ~MV_DIR_FORWARD;
1160 if (!s->next_picture.f.data[0])
1161 s->mv_dir &= ~MV_DIR_BACKWARD;
1163 s->mv_type = MV_TYPE_16X16;
1167 int time_pp = s->pp_time;
1168 int time_pb = s->pb_time;
1170 if (s->avctx->codec_id == CODEC_ID_H264) {
1173 ff_thread_await_progress(&s->next_picture_ptr->f, mb_y, 0);
1175 s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;
1176 s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;
1177 s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1178 s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1186 s->dsp.clear_blocks(s->block[0]);
1195 /* the filters below are not XvMC compatible, skip them */
1196 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1198 /* fill DC for inter blocks */
1199 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1200 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1201 int dc, dcu, dcv, y, n;
1203 uint8_t *dest_y, *dest_cb, *dest_cr;
1204 const int mb_xy = mb_x + mb_y * s->mb_stride;
1205 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1207 error = s->error_status_table[mb_xy];
1209 if (IS_INTRA(mb_type) && s->partitioned_frame)
1211 // if (error & ER_MV_ERROR)
1212 // continue; // inter data damaged FIXME is this good?
1214 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1215 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1216 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1218 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1219 for (n = 0; n < 4; n++) {
1221 for (y = 0; y < 8; y++) {
1223 for (x = 0; x < 8; x++)
1224 dc += dest_y[x + (n & 1) * 8 +
1225 (y + (n >> 1) * 8) * s->linesize];
1227 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1231 for (y = 0; y < 8; y++) {
1233 for (x = 0; x < 8; x++) {
1234 dcu += dest_cb[x + y * s->uvlinesize];
1235 dcv += dest_cr[x + y * s->uvlinesize];
1238 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1239 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1243 /* guess DC for damaged blocks */
1244 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1245 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
1246 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
1249 /* filter luma DC */
1250 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1253 /* render DC only intra */
1254 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1255 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1256 uint8_t *dest_y, *dest_cb, *dest_cr;
1257 const int mb_xy = mb_x + mb_y * s->mb_stride;
1258 const int mb_type = s->current_picture.f.mb_type[mb_xy];
1260 error = s->error_status_table[mb_xy];
1262 if (IS_INTER(mb_type))
1264 if (!(error & ER_AC_ERROR))
1265 continue; // undamaged
1267 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
1268 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1269 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
1271 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1276 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1277 /* filter horizontal block boundaries */
1278 h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1279 s->mb_height * 2, s->linesize, 1);
1280 h_block_filter(s, s->current_picture.f.data[1], s->mb_width,
1281 s->mb_height , s->uvlinesize, 0);
1282 h_block_filter(s, s->current_picture.f.data[2], s->mb_width,
1283 s->mb_height , s->uvlinesize, 0);
1285 /* filter vertical block boundaries */
1286 v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
1287 s->mb_height * 2, s->linesize, 1);
1288 v_block_filter(s, s->current_picture.f.data[1], s->mb_width,
1289 s->mb_height , s->uvlinesize, 0);
1290 v_block_filter(s, s->current_picture.f.data[2], s->mb_width,
1291 s->mb_height , s->uvlinesize, 0);
1295 /* clean a few tables */
1296 for (i = 0; i < s->mb_num; i++) {
1297 const int mb_xy = s->mb_index2xy[i];
1298 int error = s->error_status_table[mb_xy];
1300 if (s->pict_type != AV_PICTURE_TYPE_B &&
1301 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1302 s->mbskip_table[mb_xy] = 0;
1304 s->mbintra_table[mb_xy] = 1;