64 #define CHROMA_WIDTH(link) (-((-(link)->w) >> av_pix_fmt_desc_get((link)->format)->log2_chroma_w))
65 #define CHROMA_HEIGHT(link) (-((-(link)->h) >> av_pix_fmt_desc_get((link)->format)->log2_chroma_h))
67 #define OFFSET(x) offsetof(DeshakeContext, x)
68 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
94 static int cmp(
const double *
a,
const double *
b)
96 return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
108 qsort(values, count,
sizeof(
double), (
void*)
cmp);
110 for (x = cut; x < count - cut; x++) {
114 return mean / (count - cut * 2);
129 int smallest = INT_MAX;
132 #define CMP(i, j) deshake->sad(src1 + cy * stride + cx, stride,\
133 src2 + (j) * stride + (i), stride)
137 for (y = -deshake->
ry; y <= deshake->ry; y++) {
138 for (x = -deshake->
rx; x <= deshake->rx; x++) {
139 diff =
CMP(cx - x, cy - y);
140 if (diff < smallest) {
149 for (y = -deshake->
ry + 1; y < deshake->ry; y += 2) {
150 for (x = -deshake->
rx + 1; x < deshake->rx; x += 2) {
151 diff =
CMP(cx - x, cy - y);
152 if (diff < smallest) {
164 for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
165 for (x = tmp - 1; x <= tmp + 1; x++) {
166 if (x == tmp && y == tmp2)
169 diff =
CMP(cx - x, cy - y);
170 if (diff < smallest) {
179 if (smallest > 512) {
199 for (i = 0; i <= blocksize * 2; i++) {
201 for (j = 0; j <= 15; j++) {
202 pos = (y - i) * stride + (x - j);
203 if (src[pos] < lowest)
205 else if (src[pos] > highest) {
211 return highest - lowest;
221 a1 = atan2(y - cy, x - cx);
222 a2 = atan2(y - cy + shift->
y, x - cx + shift->
x);
226 return (diff >
M_PI) ? diff - 2 *
M_PI :
243 int count_max_value = 0;
247 int center_x = 0, center_y = 0;
253 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
254 for (y = 0; y < deshake->
ry * 2 + 1; y++) {
261 for (y = deshake->
ry; y < height - deshake->ry - (deshake->
blocksize * 2); y += deshake->
blocksize * 2) {
263 for (x = deshake->
rx; x < width - deshake->rx - 16; x += 16) {
270 if (mv.
x != -1 && mv.
y != -1) {
271 deshake->
counts[mv.
x + deshake->
rx][mv.
y + deshake->
ry] += 1;
272 if (x > deshake->
rx && y > deshake->
ry)
286 if (t->
angle < 0.001)
293 for (y = deshake->
ry * 2; y >= 0; y--) {
294 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
296 if (deshake->
counts[x][y] > count_max_value) {
299 count_max_value = deshake->
counts[x][
y];
305 p_x = (center_x - width / 2.0);
306 p_y = (center_y - height / 2.0);
320 const float *matrix_y,
const float *matrix_uv,
325 const float *matrixs[3];
326 int plane_w[3], plane_h[3];
327 matrixs[0] = matrix_y;
328 matrixs[1] = matrixs[2] = matrix_uv;
330 plane_w[1] = plane_w[2] = cw;
332 plane_h[1] = plane_h[2] = ch;
334 for (i = 0; i < 3; i++) {
337 plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
357 if (deshake->
rx % 16) {
365 fwrite(
"Ori x, Avg x, Fin x, Ori y, Avg y, Fin y, Ori angle, Avg angle, Fin angle, Ori zoom, Avg zoom, Fin zoom\n",
sizeof(
char), 104, deshake->
fp);
369 if (deshake->
cx > 0) {
370 deshake->
cw += deshake->
cx - (deshake->
cx & ~15);
374 if (!CONFIG_OPENCL && deshake->
opencl) {
375 av_log(ctx,
AV_LOG_ERROR,
"OpenCL support was not enabled in this build, cannot be selected\n");
379 if (CONFIG_OPENCL && deshake->
opencl) {
385 av_log(ctx,
AV_LOG_VERBOSE,
"cx: %d, cy: %d, cw: %d, ch: %d, rx: %d, ry: %d, edge: %d blocksize: %d contrast: %d search: %d\n",
386 deshake->
cx, deshake->
cy, deshake->
cw, deshake->
ch,
421 if (CONFIG_OPENCL && deshake->
opencl) {
437 float matrix_y[9], matrix_uv[9];
449 if (CONFIG_OPENCL && deshake->
opencl) {
455 if (deshake->
cx < 0 || deshake->
cy < 0 || deshake->
cw < 0 || deshake->
ch < 0) {
465 if ((
unsigned)deshake->
cx + (
unsigned)deshake->
cw > link->
w) deshake->
cw = link->
w - deshake->
cx;
466 if ((
unsigned)deshake->
cy + (
unsigned)deshake->
ch > link->
h) deshake->
ch = link->
h - deshake->
cy;
481 orig.angle = t.
angle;
504 snprintf(tmp, 256,
"%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n", orig.vector.x, deshake->
avg.
vector.
x, t.
vector.
x, orig.vector.y, deshake->
avg.
vector.
y, t.
vector.
y, orig.angle, deshake->
avg.
angle, t.
angle, orig.zoom, deshake->
avg.
zoom, t.
zoom);
505 fwrite(tmp,
sizeof(
char), strlen(tmp), deshake->
fp);
574 .priv_class = &deshake_class,