00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include <limits.h>
00029
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 #include "h264.h"
00034 #include "rectangle.h"
00035 #include "thread.h"
00036
00037
00038
00039
00040
00041 #undef mb_intra
00042
00043 static void decode_mb(MpegEncContext *s, int ref)
00044 {
00045 s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
00046 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);
00047 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);
00048
00049 ff_init_block_index(s);
00050 ff_update_block_index(s);
00051 s->dest[1] += (16 >> s->chroma_x_shift) - 8;
00052 s->dest[2] += (16 >> s->chroma_x_shift) - 8;
00053
00054 if (CONFIG_H264_DECODER && s->codec_id == AV_CODEC_ID_H264) {
00055 H264Context *h = (void*)s;
00056 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
00057 memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
00058 av_assert1(ref >= 0);
00059
00060
00061
00062
00063 if (ref >= h->ref_count[0])
00064 ref = 0;
00065 if (!h->ref_list[0][ref].f.data[0]) {
00066 av_log(s->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
00067 ref = 0;
00068 }
00069 if ((h->ref_list[0][ref].f.reference&3) != 3) {
00070 av_log(s->avctx, AV_LOG_DEBUG, "Reference invalid\n");
00071 return;
00072 }
00073 fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],
00074 2, 2, 2, ref, 1);
00075 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
00076 fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
00077 pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);
00078 h->mb_mbaff =
00079 h->mb_field_decoding_flag = 0;
00080 ff_h264_hl_decode_mb(h);
00081 } else {
00082 assert(ref == 0);
00083 ff_MPV_decode_mb(s, s->block);
00084 }
00085 }
00086
00091 static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride)
00092 {
00093 if (s->codec_id == AV_CODEC_ID_H264) {
00094 H264Context *h = (void*)s;
00095 av_assert0(s->quarter_sample);
00096 *mv_step = 4;
00097 *stride = h->b_stride;
00098 } else {
00099 *mv_step = 2;
00100 *stride = s->b8_stride;
00101 }
00102 }
00103
00107 static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
00108 uint8_t *dest_cr, int mb_x, int mb_y)
00109 {
00110 int dc, dcu, dcv, y, i;
00111 for (i = 0; i < 4; i++) {
00112 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
00113 if (dc < 0)
00114 dc = 0;
00115 else if (dc > 2040)
00116 dc = 2040;
00117 for (y = 0; y < 8; y++) {
00118 int x;
00119 for (x = 0; x < 8; x++)
00120 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * s->linesize] = dc / 8;
00121 }
00122 }
00123 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
00124 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
00125 if (dcu < 0)
00126 dcu = 0;
00127 else if (dcu > 2040)
00128 dcu = 2040;
00129 if (dcv < 0)
00130 dcv = 0;
00131 else if (dcv > 2040)
00132 dcv = 2040;
00133 for (y = 0; y < 8; y++) {
00134 int x;
00135 for (x = 0; x < 8; x++) {
00136 dest_cb[x + y * s->uvlinesize] = dcu / 8;
00137 dest_cr[x + y * s->uvlinesize] = dcv / 8;
00138 }
00139 }
00140 }
00141
00142 static void filter181(int16_t *data, int width, int height, int stride)
00143 {
00144 int x, y;
00145
00146
00147 for (y = 1; y < height - 1; y++) {
00148 int prev_dc = data[0 + y * stride];
00149
00150 for (x = 1; x < width - 1; x++) {
00151 int dc;
00152 dc = -prev_dc +
00153 data[x + y * stride] * 8 -
00154 data[x + 1 + y * stride];
00155 dc = (dc * 10923 + 32768) >> 16;
00156 prev_dc = data[x + y * stride];
00157 data[x + y * stride] = dc;
00158 }
00159 }
00160
00161
00162 for (x = 1; x < width - 1; x++) {
00163 int prev_dc = data[x];
00164
00165 for (y = 1; y < height - 1; y++) {
00166 int dc;
00167
00168 dc = -prev_dc +
00169 data[x + y * stride] * 8 -
00170 data[x + (y + 1) * stride];
00171 dc = (dc * 10923 + 32768) >> 16;
00172 prev_dc = data[x + y * stride];
00173 data[x + y * stride] = dc;
00174 }
00175 }
00176 }
00177
00183 static void guess_dc(MpegEncContext *s, int16_t *dc, int w,
00184 int h, int stride, int is_luma)
00185 {
00186 int b_x, b_y;
00187 int16_t (*col )[4] = av_malloc(stride*h*sizeof( int16_t)*4);
00188 uint32_t (*dist)[4] = av_malloc(stride*h*sizeof(uint32_t)*4);
00189
00190 if(!col || !dist) {
00191 av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
00192 goto fail;
00193 }
00194
00195 for(b_y=0; b_y<h; b_y++){
00196 int color= 1024;
00197 int distance= -1;
00198 for(b_x=0; b_x<w; b_x++){
00199 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00200 int error_j= s->error_status_table[mb_index_j];
00201 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00202 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00203 color= dc[b_x + b_y*stride];
00204 distance= b_x;
00205 }
00206 col [b_x + b_y*stride][1]= color;
00207 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
00208 }
00209 color= 1024;
00210 distance= -1;
00211 for(b_x=w-1; b_x>=0; b_x--){
00212 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00213 int error_j= s->error_status_table[mb_index_j];
00214 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00215 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00216 color= dc[b_x + b_y*stride];
00217 distance= b_x;
00218 }
00219 col [b_x + b_y*stride][0]= color;
00220 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
00221 }
00222 }
00223 for(b_x=0; b_x<w; b_x++){
00224 int color= 1024;
00225 int distance= -1;
00226 for(b_y=0; b_y<h; b_y++){
00227 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00228 int error_j= s->error_status_table[mb_index_j];
00229 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00230 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00231 color= dc[b_x + b_y*stride];
00232 distance= b_y;
00233 }
00234 col [b_x + b_y*stride][3]= color;
00235 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
00236 }
00237 color= 1024;
00238 distance= -1;
00239 for(b_y=h-1; b_y>=0; b_y--){
00240 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
00241 int error_j= s->error_status_table[mb_index_j];
00242 int intra_j = IS_INTRA(s->current_picture.f.mb_type[mb_index_j]);
00243 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
00244 color= dc[b_x + b_y*stride];
00245 distance= b_y;
00246 }
00247 col [b_x + b_y*stride][2]= color;
00248 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
00249 }
00250 }
00251
00252 for (b_y = 0; b_y < h; b_y++) {
00253 for (b_x = 0; b_x < w; b_x++) {
00254 int mb_index, error, j;
00255 int64_t guess, weight_sum;
00256 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
00257 error = s->error_status_table[mb_index];
00258
00259 if (IS_INTER(s->current_picture.f.mb_type[mb_index]))
00260 continue;
00261 if (!(error & ER_DC_ERROR))
00262 continue;
00263
00264 weight_sum = 0;
00265 guess = 0;
00266 for (j = 0; j < 4; j++) {
00267 int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
00268 guess += weight*(int64_t)col[b_x + b_y*stride][j];
00269 weight_sum += weight;
00270 }
00271 guess = (guess + weight_sum / 2) / weight_sum;
00272 dc[b_x + b_y * stride] = guess;
00273 }
00274 }
00275
00276 fail:
00277 av_freep(&col);
00278 av_freep(&dist);
00279 }
00280
00286 static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w,
00287 int h, int stride, int is_luma)
00288 {
00289 int b_x, b_y, mvx_stride, mvy_stride;
00290 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00291 set_mv_strides(s, &mvx_stride, &mvy_stride);
00292 mvx_stride >>= is_luma;
00293 mvy_stride *= mvx_stride;
00294
00295 for (b_y = 0; b_y < h; b_y++) {
00296 for (b_x = 0; b_x < w - 1; b_x++) {
00297 int y;
00298 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
00299 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
00300 int left_intra = IS_INTRA(s->current_picture.f.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
00301 int right_intra = IS_INTRA(s->current_picture.f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
00302 int left_damage = left_status & ER_MB_ERROR;
00303 int right_damage = right_status & ER_MB_ERROR;
00304 int offset = b_x * 8 + b_y * stride * 8;
00305 int16_t *left_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
00306 int16_t *right_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
00307 if (!(left_damage || right_damage))
00308 continue;
00309 if ((!left_intra) && (!right_intra) &&
00310 FFABS(left_mv[0] - right_mv[0]) +
00311 FFABS(left_mv[1] + right_mv[1]) < 2)
00312 continue;
00313
00314 for (y = 0; y < 8; y++) {
00315 int a, b, c, d;
00316
00317 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
00318 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
00319 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
00320
00321 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
00322 d = FFMAX(d, 0);
00323 if (b < 0)
00324 d = -d;
00325
00326 if (d == 0)
00327 continue;
00328
00329 if (!(left_damage && right_damage))
00330 d = d * 16 / 9;
00331
00332 if (left_damage) {
00333 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
00334 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
00335 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
00336 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
00337 }
00338 if (right_damage) {
00339 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
00340 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
00341 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
00342 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
00343 }
00344 }
00345 }
00346 }
00347 }
00348
00354 static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h,
00355 int stride, int is_luma)
00356 {
00357 int b_x, b_y, mvx_stride, mvy_stride;
00358 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00359 set_mv_strides(s, &mvx_stride, &mvy_stride);
00360 mvx_stride >>= is_luma;
00361 mvy_stride *= mvx_stride;
00362
00363 for (b_y = 0; b_y < h - 1; b_y++) {
00364 for (b_x = 0; b_x < w; b_x++) {
00365 int x;
00366 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
00367 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
00368 int top_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
00369 int bottom_intra = IS_INTRA(s->current_picture.f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
00370 int top_damage = top_status & ER_MB_ERROR;
00371 int bottom_damage = bottom_status & ER_MB_ERROR;
00372 int offset = b_x * 8 + b_y * stride * 8;
00373
00374 int16_t *top_mv = s->current_picture.f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
00375 int16_t *bottom_mv = s->current_picture.f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
00376
00377 if (!(top_damage || bottom_damage))
00378 continue;
00379
00380 if ((!top_intra) && (!bottom_intra) &&
00381 FFABS(top_mv[0] - bottom_mv[0]) +
00382 FFABS(top_mv[1] + bottom_mv[1]) < 2)
00383 continue;
00384
00385 for (x = 0; x < 8; x++) {
00386 int a, b, c, d;
00387
00388 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
00389 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
00390 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
00391
00392 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
00393 d = FFMAX(d, 0);
00394 if (b < 0)
00395 d = -d;
00396
00397 if (d == 0)
00398 continue;
00399
00400 if (!(top_damage && bottom_damage))
00401 d = d * 16 / 9;
00402
00403 if (top_damage) {
00404 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
00405 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
00406 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
00407 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
00408 }
00409 if (bottom_damage) {
00410 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
00411 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
00412 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
00413 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
00414 }
00415 }
00416 }
00417 }
00418 }
00419
00420 static void guess_mv(MpegEncContext *s)
00421 {
00422 uint8_t *fixed = s->er_temp_buffer;
00423 #define MV_FROZEN 3
00424 #define MV_CHANGED 2
00425 #define MV_UNCHANGED 1
00426 const int mb_stride = s->mb_stride;
00427 const int mb_width = s->mb_width;
00428 const int mb_height = s->mb_height;
00429 int i, depth, num_avail;
00430 int mb_x, mb_y, mot_step, mot_stride;
00431
00432 set_mv_strides(s, &mot_step, &mot_stride);
00433
00434 num_avail = 0;
00435 for (i = 0; i < s->mb_num; i++) {
00436 const int mb_xy = s->mb_index2xy[i];
00437 int f = 0;
00438 int error = s->error_status_table[mb_xy];
00439
00440 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00441 f = MV_FROZEN;
00442 if (!(error & ER_MV_ERROR))
00443 f = MV_FROZEN;
00444
00445 fixed[mb_xy] = f;
00446 if (f == MV_FROZEN)
00447 num_avail++;
00448 else if(s->last_picture.f.data[0] && s->last_picture.f.motion_val[0]){
00449 const int mb_y= mb_xy / s->mb_stride;
00450 const int mb_x= mb_xy % s->mb_stride;
00451 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
00452 s->current_picture.f.motion_val[0][mot_index][0]= s->last_picture.f.motion_val[0][mot_index][0];
00453 s->current_picture.f.motion_val[0][mot_index][1]= s->last_picture.f.motion_val[0][mot_index][1];
00454 s->current_picture.f.ref_index[0][4*mb_xy] = s->last_picture.f.ref_index[0][4*mb_xy];
00455 }
00456 }
00457
00458 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
00459 num_avail <= mb_width / 2) {
00460 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00461 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00462 const int mb_xy = mb_x + mb_y * s->mb_stride;
00463
00464 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00465 continue;
00466 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
00467 continue;
00468
00469 s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD
00470 : MV_DIR_BACKWARD;
00471 s->mb_intra = 0;
00472 s->mv_type = MV_TYPE_16X16;
00473 s->mb_skipped = 0;
00474
00475 s->dsp.clear_blocks(s->block[0]);
00476
00477 s->mb_x = mb_x;
00478 s->mb_y = mb_y;
00479 s->mv[0][0][0] = 0;
00480 s->mv[0][0][1] = 0;
00481 decode_mb(s, 0);
00482 }
00483 }
00484 return;
00485 }
00486
00487 for (depth = 0; ; depth++) {
00488 int changed, pass, none_left;
00489
00490 none_left = 1;
00491 changed = 1;
00492 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
00493 int mb_x, mb_y;
00494 int score_sum = 0;
00495
00496 changed = 0;
00497 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00498 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00499 const int mb_xy = mb_x + mb_y * s->mb_stride;
00500 int mv_predictor[8][2] = { { 0 } };
00501 int ref[8] = { 0 };
00502 int pred_count = 0;
00503 int j;
00504 int best_score = 256 * 256 * 256 * 64;
00505 int best_pred = 0;
00506 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
00507 int prev_x, prev_y, prev_ref;
00508
00509 if ((mb_x ^ mb_y ^ pass) & 1)
00510 continue;
00511
00512 if (fixed[mb_xy] == MV_FROZEN)
00513 continue;
00514 av_assert1(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
00515 av_assert1(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);
00516
00517 j = 0;
00518 if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
00519 j = 1;
00520 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
00521 j = 1;
00522 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
00523 j = 1;
00524 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
00525 j = 1;
00526 if (j == 0)
00527 continue;
00528
00529 j = 0;
00530 if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
00531 j = 1;
00532 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
00533 j = 1;
00534 if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
00535 j = 1;
00536 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
00537 j = 1;
00538 if (j == 0 && pass > 1)
00539 continue;
00540
00541 none_left = 0;
00542
00543 if (mb_x > 0 && fixed[mb_xy - 1]) {
00544 mv_predictor[pred_count][0] =
00545 s->current_picture.f.motion_val[0][mot_index - mot_step][0];
00546 mv_predictor[pred_count][1] =
00547 s->current_picture.f.motion_val[0][mot_index - mot_step][1];
00548 ref[pred_count] =
00549 s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
00550 pred_count++;
00551 }
00552 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
00553 mv_predictor[pred_count][0] =
00554 s->current_picture.f.motion_val[0][mot_index + mot_step][0];
00555 mv_predictor[pred_count][1] =
00556 s->current_picture.f.motion_val[0][mot_index + mot_step][1];
00557 ref[pred_count] =
00558 s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
00559 pred_count++;
00560 }
00561 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
00562 mv_predictor[pred_count][0] =
00563 s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
00564 mv_predictor[pred_count][1] =
00565 s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
00566 ref[pred_count] =
00567 s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
00568 pred_count++;
00569 }
00570 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
00571 mv_predictor[pred_count][0] =
00572 s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
00573 mv_predictor[pred_count][1] =
00574 s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
00575 ref[pred_count] =
00576 s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
00577 pred_count++;
00578 }
00579 if (pred_count == 0)
00580 continue;
00581
00582 if (pred_count > 1) {
00583 int sum_x = 0, sum_y = 0, sum_r = 0;
00584 int max_x, max_y, min_x, min_y, max_r, min_r;
00585
00586 for (j = 0; j < pred_count; j++) {
00587 sum_x += mv_predictor[j][0];
00588 sum_y += mv_predictor[j][1];
00589 sum_r += ref[j];
00590 if (j && ref[j] != ref[j - 1])
00591 goto skip_mean_and_median;
00592 }
00593
00594
00595 mv_predictor[pred_count][0] = sum_x / j;
00596 mv_predictor[pred_count][1] = sum_y / j;
00597 ref[pred_count] = sum_r / j;
00598
00599
00600 if (pred_count >= 3) {
00601 min_y = min_x = min_r = 99999;
00602 max_y = max_x = max_r = -99999;
00603 } else {
00604 min_x = min_y = max_x = max_y = min_r = max_r = 0;
00605 }
00606 for (j = 0; j < pred_count; j++) {
00607 max_x = FFMAX(max_x, mv_predictor[j][0]);
00608 max_y = FFMAX(max_y, mv_predictor[j][1]);
00609 max_r = FFMAX(max_r, ref[j]);
00610 min_x = FFMIN(min_x, mv_predictor[j][0]);
00611 min_y = FFMIN(min_y, mv_predictor[j][1]);
00612 min_r = FFMIN(min_r, ref[j]);
00613 }
00614 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
00615 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
00616 ref[pred_count + 1] = sum_r - max_r - min_r;
00617
00618 if (pred_count == 4) {
00619 mv_predictor[pred_count + 1][0] /= 2;
00620 mv_predictor[pred_count + 1][1] /= 2;
00621 ref[pred_count + 1] /= 2;
00622 }
00623 pred_count += 2;
00624 }
00625
00626 skip_mean_and_median:
00627
00628 pred_count++;
00629
00630 if (!fixed[mb_xy] && 0) {
00631 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
00632
00633 } else {
00634 ff_thread_await_progress(&s->last_picture_ptr->f,
00635 mb_y, 0);
00636 }
00637 if (!s->last_picture.f.motion_val[0] ||
00638 !s->last_picture.f.ref_index[0])
00639 goto skip_last_mv;
00640 prev_x = s->last_picture.f.motion_val[0][mot_index][0];
00641 prev_y = s->last_picture.f.motion_val[0][mot_index][1];
00642 prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
00643 } else {
00644 prev_x = s->current_picture.f.motion_val[0][mot_index][0];
00645 prev_y = s->current_picture.f.motion_val[0][mot_index][1];
00646 prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
00647 }
00648
00649
00650 mv_predictor[pred_count][0] = prev_x;
00651 mv_predictor[pred_count][1] = prev_y;
00652 ref[pred_count] = prev_ref;
00653 pred_count++;
00654
00655 skip_last_mv:
00656 s->mv_dir = MV_DIR_FORWARD;
00657 s->mb_intra = 0;
00658 s->mv_type = MV_TYPE_16X16;
00659 s->mb_skipped = 0;
00660
00661 s->dsp.clear_blocks(s->block[0]);
00662
00663 s->mb_x = mb_x;
00664 s->mb_y = mb_y;
00665
00666 for (j = 0; j < pred_count; j++) {
00667 int score = 0;
00668 uint8_t *src = s->current_picture.f.data[0] +
00669 mb_x * 16 + mb_y * 16 * s->linesize;
00670
00671 s->current_picture.f.motion_val[0][mot_index][0] =
00672 s->mv[0][0][0] = mv_predictor[j][0];
00673 s->current_picture.f.motion_val[0][mot_index][1] =
00674 s->mv[0][0][1] = mv_predictor[j][1];
00675
00676
00677 if (ref[j] < 0)
00678 continue;
00679
00680 decode_mb(s, ref[j]);
00681
00682 if (mb_x > 0 && fixed[mb_xy - 1]) {
00683 int k;
00684 for (k = 0; k < 16; k++)
00685 score += FFABS(src[k * s->linesize - 1] -
00686 src[k * s->linesize]);
00687 }
00688 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
00689 int k;
00690 for (k = 0; k < 16; k++)
00691 score += FFABS(src[k * s->linesize + 15] -
00692 src[k * s->linesize + 16]);
00693 }
00694 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
00695 int k;
00696 for (k = 0; k < 16; k++)
00697 score += FFABS(src[k - s->linesize] - src[k]);
00698 }
00699 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
00700 int k;
00701 for (k = 0; k < 16; k++)
00702 score += FFABS(src[k + s->linesize * 15] -
00703 src[k + s->linesize * 16]);
00704 }
00705
00706 if (score <= best_score) {
00707 best_score = score;
00708 best_pred = j;
00709 }
00710 }
00711 score_sum += best_score;
00712 s->mv[0][0][0] = mv_predictor[best_pred][0];
00713 s->mv[0][0][1] = mv_predictor[best_pred][1];
00714
00715 for (i = 0; i < mot_step; i++)
00716 for (j = 0; j < mot_step; j++) {
00717 s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
00718 s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
00719 }
00720
00721 decode_mb(s, ref[best_pred]);
00722
00723
00724 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
00725 fixed[mb_xy] = MV_CHANGED;
00726 changed++;
00727 } else
00728 fixed[mb_xy] = MV_UNCHANGED;
00729 }
00730 }
00731 }
00732
00733 if (none_left)
00734 return;
00735
00736 for (i = 0; i < s->mb_num; i++) {
00737 int mb_xy = s->mb_index2xy[i];
00738 if (fixed[mb_xy])
00739 fixed[mb_xy] = MV_FROZEN;
00740 }
00741 }
00742 }
00743
00744 static int is_intra_more_likely(MpegEncContext *s)
00745 {
00746 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
00747
00748 if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])
00749 return 1;
00750
00751 undamaged_count = 0;
00752 for (i = 0; i < s->mb_num; i++) {
00753 const int mb_xy = s->mb_index2xy[i];
00754 const int error = s->error_status_table[mb_xy];
00755 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
00756 undamaged_count++;
00757 }
00758
00759 if (s->codec_id == AV_CODEC_ID_H264) {
00760 H264Context *h = (void*) s;
00761 if (h->list_count <= 0 || h->ref_count[0] <= 0 ||
00762 !h->ref_list[0][0].f.data[0])
00763 return 1;
00764 }
00765
00766 if (undamaged_count < 5)
00767 return 0;
00768
00769
00770 if (CONFIG_MPEG_XVMC_DECODER &&
00771 s->avctx->xvmc_acceleration &&
00772 s->pict_type == AV_PICTURE_TYPE_I)
00773 return 1;
00774
00775 skip_amount = FFMAX(undamaged_count / 50, 1);
00776 is_intra_likely = 0;
00777
00778 j = 0;
00779 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
00780 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00781 int error;
00782 const int mb_xy = mb_x + mb_y * s->mb_stride;
00783
00784 error = s->error_status_table[mb_xy];
00785 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
00786 continue;
00787
00788 j++;
00789
00790 if ((j % skip_amount) != 0)
00791 continue;
00792
00793 if (s->pict_type == AV_PICTURE_TYPE_I) {
00794 uint8_t *mb_ptr = s->current_picture.f.data[0] +
00795 mb_x * 16 + mb_y * 16 * s->linesize;
00796 uint8_t *last_mb_ptr = s->last_picture.f.data[0] +
00797 mb_x * 16 + mb_y * 16 * s->linesize;
00798
00799 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
00800
00801 } else {
00802 ff_thread_await_progress(&s->last_picture_ptr->f,
00803 mb_y, 0);
00804 }
00805 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);
00806
00807 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
00808 } else {
00809 if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
00810 is_intra_likely++;
00811 else
00812 is_intra_likely--;
00813 }
00814 }
00815 }
00816
00817 return is_intra_likely > 0;
00818 }
00819
00820 void ff_er_frame_start(MpegEncContext *s)
00821 {
00822 if (!s->err_recognition)
00823 return;
00824
00825 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
00826 s->mb_stride * s->mb_height * sizeof(uint8_t));
00827 s->error_count = 3 * s->mb_num;
00828 s->error_occurred = 0;
00829 }
00830
00838 void ff_er_add_slice(MpegEncContext *s, int startx, int starty,
00839 int endx, int endy, int status)
00840 {
00841 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
00842 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
00843 const int start_xy = s->mb_index2xy[start_i];
00844 const int end_xy = s->mb_index2xy[end_i];
00845 int mask = -1;
00846
00847 if (s->avctx->hwaccel)
00848 return;
00849
00850 if (start_i > end_i || start_xy > end_xy) {
00851 av_log(s->avctx, AV_LOG_ERROR,
00852 "internal error, slice end before start\n");
00853 return;
00854 }
00855
00856 if (!s->err_recognition)
00857 return;
00858
00859 mask &= ~VP_START;
00860 if (status & (ER_AC_ERROR | ER_AC_END)) {
00861 mask &= ~(ER_AC_ERROR | ER_AC_END);
00862 s->error_count -= end_i - start_i + 1;
00863 }
00864 if (status & (ER_DC_ERROR | ER_DC_END)) {
00865 mask &= ~(ER_DC_ERROR | ER_DC_END);
00866 s->error_count -= end_i - start_i + 1;
00867 }
00868 if (status & (ER_MV_ERROR | ER_MV_END)) {
00869 mask &= ~(ER_MV_ERROR | ER_MV_END);
00870 s->error_count -= end_i - start_i + 1;
00871 }
00872
00873 if (status & ER_MB_ERROR) {
00874 s->error_occurred = 1;
00875 s->error_count = INT_MAX;
00876 }
00877
00878 if (mask == ~0x7F) {
00879 memset(&s->error_status_table[start_xy], 0,
00880 (end_xy - start_xy) * sizeof(uint8_t));
00881 } else {
00882 int i;
00883 for (i = start_xy; i < end_xy; i++)
00884 s->error_status_table[i] &= mask;
00885 }
00886
00887 if (end_i == s->mb_num)
00888 s->error_count = INT_MAX;
00889 else {
00890 s->error_status_table[end_xy] &= mask;
00891 s->error_status_table[end_xy] |= status;
00892 }
00893
00894 s->error_status_table[start_xy] |= VP_START;
00895
00896 if (start_xy > 0 && s->avctx->thread_count <= 1 &&
00897 s->avctx->skip_top * s->mb_width < start_i) {
00898 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
00899
00900 prev_status &= ~ VP_START;
00901 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
00902 s->error_count = INT_MAX;
00903 }
00904 }
00905
00906 void ff_er_frame_end(MpegEncContext *s)
00907 {
00908 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
00909 int distance;
00910 int threshold_part[4] = { 100, 100, 100 };
00911 int threshold = 50;
00912 int is_intra_likely;
00913 int size = s->b8_stride * 2 * s->mb_height;
00914 Picture *pic = s->current_picture_ptr;
00915
00916
00917
00918 if (!s->err_recognition || s->error_count == 0 || s->avctx->lowres ||
00919 s->avctx->hwaccel ||
00920 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
00921 s->picture_structure != PICT_FRAME ||
00922 s->error_count == 3 * s->mb_width *
00923 (s->avctx->skip_top + s->avctx->skip_bottom)) {
00924 return;
00925 };
00926
00927 if (s->current_picture.f.motion_val[0] == NULL) {
00928 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
00929
00930 for (i = 0; i < 2; i++) {
00931 pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
00932 pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
00933 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
00934 }
00935 pic->f.motion_subsample_log2 = 3;
00936 s->current_picture = *s->current_picture_ptr;
00937 }
00938
00939 if (s->avctx->debug & FF_DEBUG_ER) {
00940 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
00941 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
00942 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
00943
00944 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
00945 }
00946 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00947 }
00948 }
00949
00950 #if 1
00951
00952 for (error_type = 1; error_type <= 3; error_type++) {
00953 int end_ok = 0;
00954
00955 for (i = s->mb_num - 1; i >= 0; i--) {
00956 const int mb_xy = s->mb_index2xy[i];
00957 int error = s->error_status_table[mb_xy];
00958
00959 if (error & (1 << error_type))
00960 end_ok = 1;
00961 if (error & (8 << error_type))
00962 end_ok = 1;
00963
00964 if (!end_ok)
00965 s->error_status_table[mb_xy] |= 1 << error_type;
00966
00967 if (error & VP_START)
00968 end_ok = 0;
00969 }
00970 }
00971 #endif
00972 #if 1
00973
00974 if (s->partitioned_frame) {
00975 int end_ok = 0;
00976
00977 for (i = s->mb_num - 1; i >= 0; i--) {
00978 const int mb_xy = s->mb_index2xy[i];
00979 int error = s->error_status_table[mb_xy];
00980
00981 if (error & ER_AC_END)
00982 end_ok = 0;
00983 if ((error & ER_MV_END) ||
00984 (error & ER_DC_END) ||
00985 (error & ER_AC_ERROR))
00986 end_ok = 1;
00987
00988 if (!end_ok)
00989 s->error_status_table[mb_xy]|= ER_AC_ERROR;
00990
00991 if (error & VP_START)
00992 end_ok = 0;
00993 }
00994 }
00995 #endif
00996
00997 if (s->err_recognition & AV_EF_EXPLODE) {
00998 int end_ok = 1;
00999
01000
01001 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
01002 const int mb_xy = s->mb_index2xy[i];
01003 int error1 = s->error_status_table[mb_xy];
01004 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
01005
01006 if (error1 & VP_START)
01007 end_ok = 1;
01008
01009 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
01010 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
01011 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
01012 (error1 & ER_MV_END))) {
01013
01014 end_ok = 0;
01015 }
01016
01017 if (!end_ok)
01018 s->error_status_table[mb_xy] |= ER_MB_ERROR;
01019 }
01020 }
01021
01022 #if 1
01023
01024 distance = 9999999;
01025 for (error_type = 1; error_type <= 3; error_type++) {
01026 for (i = s->mb_num - 1; i >= 0; i--) {
01027 const int mb_xy = s->mb_index2xy[i];
01028 int error = s->error_status_table[mb_xy];
01029
01030 if (!s->mbskip_table[mb_xy])
01031 distance++;
01032 if (error & (1 << error_type))
01033 distance = 0;
01034
01035 if (s->partitioned_frame) {
01036 if (distance < threshold_part[error_type - 1])
01037 s->error_status_table[mb_xy] |= 1 << error_type;
01038 } else {
01039 if (distance < threshold)
01040 s->error_status_table[mb_xy] |= 1 << error_type;
01041 }
01042
01043 if (error & VP_START)
01044 distance = 9999999;
01045 }
01046 }
01047 #endif
01048
01049
01050 error = 0;
01051 for (i = 0; i < s->mb_num; i++) {
01052 const int mb_xy = s->mb_index2xy[i];
01053 int old_error = s->error_status_table[mb_xy];
01054
01055 if (old_error & VP_START) {
01056 error = old_error & ER_MB_ERROR;
01057 } else {
01058 error |= old_error & ER_MB_ERROR;
01059 s->error_status_table[mb_xy] |= error;
01060 }
01061 }
01062 #if 1
01063
01064 if (!s->partitioned_frame) {
01065 for (i = 0; i < s->mb_num; i++) {
01066 const int mb_xy = s->mb_index2xy[i];
01067 error = s->error_status_table[mb_xy];
01068 if (error & ER_MB_ERROR)
01069 error |= ER_MB_ERROR;
01070 s->error_status_table[mb_xy] = error;
01071 }
01072 }
01073 #endif
01074
01075 dc_error = ac_error = mv_error = 0;
01076 for (i = 0; i < s->mb_num; i++) {
01077 const int mb_xy = s->mb_index2xy[i];
01078 error = s->error_status_table[mb_xy];
01079 if (error & ER_DC_ERROR)
01080 dc_error++;
01081 if (error & ER_AC_ERROR)
01082 ac_error++;
01083 if (error & ER_MV_ERROR)
01084 mv_error++;
01085 }
01086 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
01087 dc_error, ac_error, mv_error, av_get_picture_type_char(s->pict_type));
01088
01089 is_intra_likely = is_intra_more_likely(s);
01090
01091
01092 for (i = 0; i < s->mb_num; i++) {
01093 const int mb_xy = s->mb_index2xy[i];
01094 error = s->error_status_table[mb_xy];
01095 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
01096 continue;
01097
01098 if (is_intra_likely)
01099 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
01100 else
01101 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
01102 }
01103
01104
01105 if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])
01106 for (i = 0; i < s->mb_num; i++) {
01107 const int mb_xy = s->mb_index2xy[i];
01108 if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))
01109 s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
01110 }
01111
01112
01113 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01114 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01115 const int mb_xy = mb_x + mb_y * s->mb_stride;
01116 const int mb_type = s->current_picture.f.mb_type[mb_xy];
01117 int dir = !s->last_picture.f.data[0];
01118
01119 error = s->error_status_table[mb_xy];
01120
01121 if (IS_INTRA(mb_type))
01122 continue;
01123 if (error & ER_MV_ERROR)
01124 continue;
01125 if (!(error & ER_AC_ERROR))
01126 continue;
01127
01128 s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
01129 s->mb_intra = 0;
01130 s->mb_skipped = 0;
01131 if (IS_8X8(mb_type)) {
01132 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
01133 int j;
01134 s->mv_type = MV_TYPE_8X8;
01135 for (j = 0; j < 4; j++) {
01136 s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
01137 s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
01138 }
01139 } else {
01140 s->mv_type = MV_TYPE_16X16;
01141 s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
01142 s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
01143 }
01144
01145 s->dsp.clear_blocks(s->block[0]);
01146
01147 s->mb_x = mb_x;
01148 s->mb_y = mb_y;
01149 decode_mb(s, 0 );
01150 }
01151 }
01152
01153
01154 if (s->pict_type == AV_PICTURE_TYPE_B) {
01155 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01156 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01157 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
01158 const int mb_xy = mb_x + mb_y * s->mb_stride;
01159 const int mb_type = s->current_picture.f.mb_type[mb_xy];
01160
01161 error = s->error_status_table[mb_xy];
01162
01163 if (IS_INTRA(mb_type))
01164 continue;
01165 if (!(error & ER_MV_ERROR))
01166 continue;
01167 if (!(error & ER_AC_ERROR))
01168 continue;
01169
01170 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
01171 if (!s->last_picture.f.data[0])
01172 s->mv_dir &= ~MV_DIR_FORWARD;
01173 if (!s->next_picture.f.data[0])
01174 s->mv_dir &= ~MV_DIR_BACKWARD;
01175 s->mb_intra = 0;
01176 s->mv_type = MV_TYPE_16X16;
01177 s->mb_skipped = 0;
01178
01179 if (s->pp_time) {
01180 int time_pp = s->pp_time;
01181 int time_pb = s->pb_time;
01182
01183 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
01184
01185 } else {
01186 ff_thread_await_progress(&s->next_picture_ptr->f, mb_y, 0);
01187 }
01188 s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;
01189 s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;
01190 s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
01191 s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
01192 } else {
01193 s->mv[0][0][0] = 0;
01194 s->mv[0][0][1] = 0;
01195 s->mv[1][0][0] = 0;
01196 s->mv[1][0][1] = 0;
01197 }
01198
01199 s->dsp.clear_blocks(s->block[0]);
01200 s->mb_x = mb_x;
01201 s->mb_y = mb_y;
01202 decode_mb(s, 0);
01203 }
01204 }
01205 } else
01206 guess_mv(s);
01207
01208
01209 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01210 goto ec_clean;
01211
01212 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01213 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01214 int dc, dcu, dcv, y, n;
01215 int16_t *dc_ptr;
01216 uint8_t *dest_y, *dest_cb, *dest_cr;
01217 const int mb_xy = mb_x + mb_y * s->mb_stride;
01218 const int mb_type = s->current_picture.f.mb_type[mb_xy];
01219
01220 error = s->error_status_table[mb_xy];
01221
01222 if (IS_INTRA(mb_type) && s->partitioned_frame)
01223 continue;
01224
01225
01226
01227 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
01228 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
01229 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
01230
01231 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
01232 for (n = 0; n < 4; n++) {
01233 dc = 0;
01234 for (y = 0; y < 8; y++) {
01235 int x;
01236 for (x = 0; x < 8; x++)
01237 dc += dest_y[x + (n & 1) * 8 +
01238 (y + (n >> 1) * 8) * s->linesize];
01239 }
01240 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
01241 }
01242
01243 dcu = dcv = 0;
01244 for (y = 0; y < 8; y++) {
01245 int x;
01246 for (x = 0; x < 8; x++) {
01247 dcu += dest_cb[x + y * s->uvlinesize];
01248 dcv += dest_cr[x + y * s->uvlinesize];
01249 }
01250 }
01251 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
01252 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
01253 }
01254 }
01255 #if 1
01256
01257 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
01258 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
01259 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
01260 #endif
01261
01262
01263 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
01264
01265 #if 1
01266
01267 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01268 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01269 uint8_t *dest_y, *dest_cb, *dest_cr;
01270 const int mb_xy = mb_x + mb_y * s->mb_stride;
01271 const int mb_type = s->current_picture.f.mb_type[mb_xy];
01272
01273 error = s->error_status_table[mb_xy];
01274
01275 if (IS_INTER(mb_type))
01276 continue;
01277 if (!(error & ER_AC_ERROR))
01278 continue;
01279
01280 dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;
01281 dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
01282 dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;
01283
01284 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
01285 }
01286 }
01287 #endif
01288
01289 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
01290
01291 h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
01292 s->mb_height * 2, s->linesize, 1);
01293 h_block_filter(s, s->current_picture.f.data[1], s->mb_width,
01294 s->mb_height , s->uvlinesize, 0);
01295 h_block_filter(s, s->current_picture.f.data[2], s->mb_width,
01296 s->mb_height , s->uvlinesize, 0);
01297
01298
01299 v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,
01300 s->mb_height * 2, s->linesize, 1);
01301 v_block_filter(s, s->current_picture.f.data[1], s->mb_width,
01302 s->mb_height , s->uvlinesize, 0);
01303 v_block_filter(s, s->current_picture.f.data[2], s->mb_width,
01304 s->mb_height , s->uvlinesize, 0);
01305 }
01306
01307 ec_clean:
01308
01309 for (i = 0; i < s->mb_num; i++) {
01310 const int mb_xy = s->mb_index2xy[i];
01311 int error = s->error_status_table[mb_xy];
01312
01313 if (s->pict_type != AV_PICTURE_TYPE_B &&
01314 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
01315 s->mbskip_table[mb_xy] = 0;
01316 }
01317 s->mbintra_table[mb_xy] = 1;
01318 }
01319 }