65 #define CHROMA_WIDTH(link) -((-link->w) >> av_pix_fmt_desc_get(link->format)->log2_chroma_w)
66 #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h)
68 #define OFFSET(x) offsetof(DeshakeContext, x)
69 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
97 static int cmp(
const double *
a,
const double *
b)
99 return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
111 qsort(values, count,
sizeof(
double), (
void*)
cmp);
113 for (x = cut; x < count - cut; x++) {
117 return mean / (count - cut * 2);
132 int smallest = INT_MAX;
135 #define CMP(i, j) deshake->c.sad[0](deshake, src1 + cy * stride + cx, \
136 src2 + (j) * stride + (i), stride, \
141 for (y = -deshake->
ry; y <= deshake->ry; y++) {
142 for (x = -deshake->
rx; x <= deshake->rx; x++) {
143 diff =
CMP(cx - x, cy - y);
144 if (diff < smallest) {
153 for (y = -deshake->
ry + 1; y < deshake->ry; y += 2) {
154 for (x = -deshake->
rx + 1; x < deshake->rx; x += 2) {
155 diff =
CMP(cx - x, cy - y);
156 if (diff < smallest) {
168 for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
169 for (x = tmp - 1; x <= tmp + 1; x++) {
170 if (x == tmp && y == tmp2)
173 diff =
CMP(cx - x, cy - y);
174 if (diff < smallest) {
183 if (smallest > 512) {
203 for (i = 0; i <= blocksize * 2; i++) {
205 for (j = 0; i <= 15; i++) {
206 pos = (y - i) * stride + (x - j);
207 if (src[pos] < lowest)
209 else if (src[pos] > highest) {
215 return highest - lowest;
225 a1 = atan2(y - cy, x - cx);
226 a2 = atan2(y - cy + shift->
y, x - cx + shift->
x);
230 return (diff >
M_PI) ? diff - 2 *
M_PI :
248 int count_max_value = 0;
252 double *angles =
av_malloc(
sizeof(*angles) * width * height / (16 * deshake->
blocksize));
253 int center_x = 0, center_y = 0;
257 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
258 for (y = 0; y < deshake->
ry * 2 + 1; y++) {
265 for (y = deshake->
ry; y < height - deshake->ry - (deshake->
blocksize * 2); y += deshake->
blocksize * 2) {
267 for (x = deshake->
rx; x < width - deshake->rx - 16; x += 16) {
274 if (mv.
x != -1 && mv.
y != -1) {
275 counts[mv.
x + deshake->
rx][mv.
y + deshake->
ry] += 1;
276 if (x > deshake->
rx && y > deshake->
ry)
290 if (t->
angle < 0.001)
297 for (y = deshake->
ry * 2; y >= 0; y--) {
298 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
300 if (counts[x][y] > count_max_value) {
303 count_max_value = counts[x][
y];
309 p_x = (center_x - width / 2);
310 p_y = (center_y - height / 2);
325 const float *matrix_y,
const float *matrix_uv,
330 const float *matrixs[3];
331 int plane_w[3], plane_h[3];
332 matrixs[0] = matrix_y;
333 matrixs[1] = matrixs[2] = matrix_uv;
335 plane_w[1] = plane_w[2] = cw;
337 plane_h[1] = plane_h[2] = ch;
339 for (i = 0; i < 3; i++) {
342 plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
358 if (deshake->
rx % 16) {
366 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);
370 if (deshake->
cx > 0) {
371 deshake->
cw += deshake->
cx - (deshake->
cx & ~15);
375 if (!CONFIG_OPENCL && deshake->
opencl) {
376 av_log(ctx,
AV_LOG_ERROR,
"OpenCL support was not enabled in this build, cannot be selected\n");
380 if (CONFIG_OPENCL && deshake->
opencl) {
386 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",
387 deshake->
cx, deshake->
cy, deshake->
cw, deshake->
ch,
425 if (CONFIG_OPENCL && deshake->
opencl) {
442 float matrix_y[9], matrix_uv[9];
454 if (CONFIG_OPENCL && deshake->
opencl) {
460 if (deshake->
cx < 0 || deshake->
cy < 0 || deshake->
cw < 0 || deshake->
ch < 0) {
470 if ((
unsigned)deshake->
cx + (
unsigned)deshake->
cw > link->
w) deshake->
cw = link->
w - deshake->
cx;
471 if ((
unsigned)deshake->
cy + (
unsigned)deshake->
ch > link->
h) deshake->
ch = link->
h - deshake->
cy;
486 orig.angle = t.
angle;
509 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);
510 fwrite(tmp,
sizeof(
char), strlen(tmp), deshake->
fp);
579 .priv_class = &deshake_class,