59 uint8_t *dest_cr,
int mb_x,
int mb_y)
62 int dc, dcu, dcv,
y, i;
63 for (i = 0; i < 4; i++) {
64 dc = s->
dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->
b8_stride];
69 for (y = 0; y < 8; y++) {
71 for (x = 0; x < 8; x++)
72 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
85 for (y = 0; y < 8; y++) {
87 for (x = 0; x < 8; x++) {
88 dest_cb[x + y * linesize[1]] = dcu / 8;
89 dest_cr[x + y * linesize[2]] = dcv / 8;
99 for (y = 1; y < height - 1; y++) {
100 int prev_dc = data[0 + y * stride];
102 for (x = 1; x < width - 1; x++) {
105 data[x + y * stride] * 8 -
106 data[x + 1 + y * stride];
107 dc = (dc * 10923 + 32768) >> 16;
108 prev_dc = data[x + y * stride];
109 data[x + y * stride] =
dc;
114 for (x = 1; x < width - 1; x++) {
115 int prev_dc = data[x];
117 for (y = 1; y < height - 1; y++) {
121 data[x + y * stride] * 8 -
122 data[x + (y + 1) * stride];
123 dc = (dc * 10923 + 32768) >> 16;
124 prev_dc = data[x + y * stride];
125 data[x + y * stride] =
dc;
136 int h,
int stride,
int is_luma)
147 for(b_y=0; b_y<h; b_y++){
150 for(b_x=0; b_x<w; b_x++){
151 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->
mb_stride;
155 color= dc[b_x + b_y*stride];
158 col [b_x + b_y*stride][1]=
color;
159 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
163 for(b_x=w-1; b_x>=0; b_x--){
164 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->
mb_stride;
168 color= dc[b_x + b_y*stride];
171 col [b_x + b_y*stride][0]=
color;
172 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
175 for(b_x=0; b_x<w; b_x++){
178 for(b_y=0; b_y<h; b_y++){
179 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->
mb_stride;
183 color= dc[b_x + b_y*stride];
186 col [b_x + b_y*stride][3]=
color;
187 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
191 for(b_y=h-1; b_y>=0; b_y--){
192 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->
mb_stride;
196 color= dc[b_x + b_y*stride];
199 col [b_x + b_y*stride][2]=
color;
200 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
204 for (b_y = 0; b_y < h; b_y++) {
205 for (b_x = 0; b_x < w; b_x++) {
206 int mb_index, error, j;
207 int64_t guess, weight_sum;
208 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->
mb_stride;
218 for (j = 0; j < 4; j++) {
219 int64_t
weight = 256 * 256 * 256 * 16 /
FFMAX(dist[b_x + b_y*stride][j], 1);
220 guess += weight*(int64_t)col[b_x + b_y*stride][j];
223 guess = (guess + weight_sum / 2) / weight_sum;
224 dc[b_x + b_y * stride] = guess;
239 int h,
int stride,
int is_luma)
241 int b_x, b_y, mvx_stride, mvy_stride;
244 mvx_stride >>= is_luma;
245 mvy_stride *= mvx_stride;
247 for (b_y = 0; b_y < h; b_y++) {
248 for (b_x = 0; b_x < w - 1; b_x++) {
256 int offset = b_x * 8 + b_y * stride * 8;
257 int16_t *left_mv = s->
cur_pic.
motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
258 int16_t *right_mv = s->
cur_pic.
motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
259 if (!(left_damage || right_damage))
261 if ((!left_intra) && (!right_intra) &&
262 FFABS(left_mv[0] - right_mv[0]) +
263 FFABS(left_mv[1] + right_mv[1]) < 2)
266 for (y = 0; y < 8; y++) {
269 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
270 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
271 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
281 if (!(left_damage && right_damage))
285 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
286 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
287 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
288 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
291 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
292 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
293 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
294 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
309 int b_x, b_y, mvx_stride, mvy_stride;
312 mvx_stride >>= is_luma;
313 mvy_stride *= mvx_stride;
315 for (b_y = 0; b_y < h - 1; b_y++) {
316 for (b_x = 0; b_x < w; b_x++) {
324 int offset = b_x * 8 + b_y * stride * 8;
327 int16_t *bottom_mv = s->
cur_pic.
motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
329 if (!(top_damage || bottom_damage))
332 if ((!top_intra) && (!bottom_intra) &&
333 FFABS(top_mv[0] - bottom_mv[0]) +
334 FFABS(top_mv[1] + bottom_mv[1]) < 2)
337 for (x = 0; x < 8; x++) {
340 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
341 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
342 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
352 if (!(top_damage && bottom_damage))
356 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
357 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
358 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
359 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
362 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
363 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
364 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
365 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
377 #define MV_UNCHANGED 1
381 int i,
depth, num_avail;
382 int mb_x, mb_y, mot_step, mot_stride;
387 for (i = 0; i < s->
mb_num; i++) {
403 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
411 num_avail <= mb_width / 2) {
412 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
413 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
414 const int mb_xy = mb_x + mb_y * s->
mb_stride;
431 for (depth = 0; ; depth++) {
432 int changed,
pass, none_left;
436 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
441 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
442 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
443 const int mb_xy = mb_x + mb_y * s->
mb_stride;
444 int mv_predictor[8][2] = { { 0 } };
448 int best_score = 256 * 256 * 256 * 64;
450 const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
451 int prev_x = 0, prev_y = 0, prev_ref = 0;
453 if ((mb_x ^ mb_y ^ pass) & 1)
462 if (mb_x > 0 && fixed[mb_xy - 1] ==
MV_FROZEN)
464 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] ==
MV_FROZEN)
466 if (mb_y > 0 && fixed[mb_xy - mb_stride] ==
MV_FROZEN)
468 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] ==
MV_FROZEN)
474 if (mb_x > 0 && fixed[mb_xy - 1 ] ==
MV_CHANGED)
476 if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] ==
MV_CHANGED)
478 if (mb_y > 0 && fixed[mb_xy - mb_stride] ==
MV_CHANGED)
480 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] ==
MV_CHANGED)
482 if (j == 0 && pass > 1)
487 if (mb_x > 0 && fixed[mb_xy - 1]) {
488 mv_predictor[pred_count][0] =
490 mv_predictor[pred_count][1] =
496 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
497 mv_predictor[pred_count][0] =
499 mv_predictor[pred_count][1] =
505 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
506 mv_predictor[pred_count][0] =
508 mv_predictor[pred_count][1] =
514 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
515 mv_predictor[pred_count][0] =
517 mv_predictor[pred_count][1] =
526 if (pred_count > 1) {
527 int sum_x = 0, sum_y = 0, sum_r = 0;
528 int max_x, max_y, min_x, min_y, max_r, min_r;
530 for (j = 0; j < pred_count; j++) {
531 sum_x += mv_predictor[j][0];
532 sum_y += mv_predictor[j][1];
534 if (j && ref[j] != ref[j - 1])
535 goto skip_mean_and_median;
539 mv_predictor[pred_count][0] = sum_x / j;
540 mv_predictor[pred_count][1] = sum_y / j;
541 ref[pred_count] = sum_r / j;
544 if (pred_count >= 3) {
545 min_y = min_x = min_r = 99999;
546 max_y = max_x = max_r = -99999;
548 min_x = min_y = max_x = max_y = min_r = max_r = 0;
550 for (j = 0; j < pred_count; j++) {
551 max_x =
FFMAX(max_x, mv_predictor[j][0]);
552 max_y =
FFMAX(max_y, mv_predictor[j][1]);
553 max_r =
FFMAX(max_r, ref[j]);
554 min_x =
FFMIN(min_x, mv_predictor[j][0]);
555 min_y =
FFMIN(min_y, mv_predictor[j][1]);
556 min_r =
FFMIN(min_r, ref[j]);
558 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
559 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
560 ref[pred_count + 1] = sum_r - max_r - min_r;
562 if (pred_count == 4) {
563 mv_predictor[pred_count + 1][0] /= 2;
564 mv_predictor[pred_count + 1][1] /= 2;
565 ref[pred_count + 1] /= 2;
570 skip_mean_and_median:
574 if (!fixed[mb_xy] && 0) {
594 mv_predictor[pred_count][0] = prev_x;
595 mv_predictor[pred_count][1] = prev_y;
596 ref[pred_count] = prev_ref;
601 for (j = 0; j < pred_count; j++) {
605 mb_x * 16 + mb_y * 16 * linesize[0];
608 s->
mv[0][0][0] = mv_predictor[j][0];
610 s->
mv[0][0][1] = mv_predictor[j][1];
619 if (mb_x > 0 && fixed[mb_xy - 1]) {
621 for (k = 0; k < 16; k++)
622 score +=
FFABS(src[k * linesize[0] - 1] -
623 src[k * linesize[0]]);
625 if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
627 for (k = 0; k < 16; k++)
628 score +=
FFABS(src[k * linesize[0] + 15] -
629 src[k * linesize[0] + 16]);
631 if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
633 for (k = 0; k < 16; k++)
634 score +=
FFABS(src[k - linesize[0]] - src[k]);
636 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
638 for (k = 0; k < 16; k++)
639 score +=
FFABS(src[k + linesize[0] * 15] -
640 src[k + linesize[0] * 16]);
643 if (score <= best_score) {
648 score_sum += best_score;
649 s->
mv[0][0][0] = mv_predictor[best_pred][0];
650 s->
mv[0][0][1] = mv_predictor[best_pred][1];
652 for (i = 0; i < mot_step; i++)
653 for (j = 0; j < mot_step; j++) {
662 if (s->
mv[0][0][0] != prev_x || s->
mv[0][0][1] != prev_y) {
674 for (i = 0; i < s->
mb_num; i++) {
684 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
693 for (i = 0; i < s->
mb_num; i++) {
703 if (undamaged_count < 5)
712 skip_amount =
FFMAX(undamaged_count / 50, 1);
716 for (mb_y = 0; mb_y < s->
mb_height - 1; mb_y++) {
717 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
719 const int mb_xy = mb_x + mb_y * s->
mb_stride;
727 if ((j % skip_amount) != 0)
733 mb_x * 16 + mb_y * 16 * linesize[0];
735 mb_x * 16 + mb_y * 16 * linesize[0];
742 is_intra_likely += s->
mecc->
sad[0](NULL, last_mb_ptr, mb_ptr,
745 is_intra_likely -= s->
mecc->
sad[0](NULL, last_mb_ptr,
746 last_mb_ptr + linesize[0] * 16,
757 return is_intra_likely > 0;
790 int endx,
int endy,
int status)
792 const int start_i = av_clip(startx + starty * s->
mb_width, 0, s->
mb_num - 1);
793 const int end_i = av_clip(endx + endy * s->
mb_width, 0, s->
mb_num);
801 if (start_i > end_i || start_xy > end_xy) {
803 "internal error, slice end before start\n");
831 (end_xy - start_xy) *
sizeof(
uint8_t));
834 for (i = start_xy; i < end_xy; i++)
861 int *linesize = NULL;
862 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
864 int threshold_part[4] = { 100, 100, 100 };
879 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
914 for (i = 0; i < 2; i++) {
923 for (i = 0; i < 2; i++) {
934 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
935 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
946 for (error_type = 1; error_type <= 3; error_type++) {
949 for (i = s->
mb_num - 1; i >= 0; i--) {
953 if (error & (1 << error_type))
955 if (error & (8 << error_type))
971 for (i = s->
mb_num - 1; i >= 0; i--) {
1019 for (error_type = 1; error_type <= 3; error_type++) {
1020 for (i = s->
mb_num - 1; i >= 0; i--) {
1026 if (error & (1 << error_type))
1030 if (distance < threshold_part[error_type - 1])
1033 if (distance < threshold)
1045 for (i = 0; i < s->
mb_num; i++) {
1059 for (i = 0; i < s->
mb_num; i++) {
1069 dc_error = ac_error = mv_error = 0;
1070 for (i = 0; i < s->
mb_num; i++) {
1086 for (i = 0; i < s->
mb_num; i++) {
1092 if (is_intra_likely)
1101 for (i = 0; i < s->
mb_num; i++) {
1108 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1109 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1110 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1126 int mb_index = mb_x * 2 + mb_y * 2 * s->
b8_stride;
1129 for (j = 0; j < 4; j++) {
1140 mv_dir, mv_type, &s->
mv, mb_x, mb_y, 0, 0);
1146 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1147 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1148 int xy = mb_x * 2 + mb_y * 2 * s->
b8_stride;
1149 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1196 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1197 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1198 int dc, dcu, dcv,
y,
n;
1200 uint8_t *dest_y, *dest_cb, *dest_cr;
1201 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1211 dest_y = s->
cur_pic.
f->
data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1212 dest_cb = s->
cur_pic.
f->
data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1213 dest_cr = s->
cur_pic.
f->
data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1216 for (n = 0; n < 4; n++) {
1218 for (y = 0; y < 8; y++) {
1220 for (x = 0; x < 8; x++)
1221 dc += dest_y[x + (n & 1) * 8 +
1222 (y + (n >> 1) * 8) * linesize[0]];
1224 dc_ptr[(n & 1) + (n >> 1) * s->
b8_stride] = (dc + 4) >> 3;
1228 for (y = 0; y < 8; y++) {
1230 for (x = 0; x < 8; x++) {
1231 dcu += dest_cb[x + y * linesize[1]];
1232 dcv += dest_cr[x + y * linesize[2]];
1251 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1252 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1253 uint8_t *dest_y, *dest_cb, *dest_cr;
1254 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1264 dest_y = s->
cur_pic.
f->
data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1265 dest_cb = s->
cur_pic.
f->
data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1266 dest_cr = s->
cur_pic.
f->
data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1268 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1293 for (i = 0; i < s->
mb_num; i++) {
1304 for (i = 0; i < 2; i++) {