You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
21KB

  1. /*
  2. * Copyright (C) 2010 Georg Martius <georg.martius@web.de>
  3. * Copyright (C) 2010 Daniel G. Taylor <dan@programmer-art.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * fast deshake / depan video filter
  24. *
  25. * SAD block-matching motion compensation to fix small changes in
  26. * horizontal and/or vertical shift. This filter helps remove camera shake
  27. * from hand-holding a camera, bumping a tripod, moving on a vehicle, etc.
  28. *
  29. * Algorithm:
  30. * - For each frame with one previous reference frame
  31. * - For each block in the frame
  32. * - If contrast > threshold then find likely motion vector
  33. * - For all found motion vectors
  34. * - Find most common, store as global motion vector
  35. * - Find most likely rotation angle
  36. * - Transform image along global motion
  37. *
  38. * TODO:
  39. * - Fill frame edges based on previous/next reference frames
  40. * - Fill frame edges by stretching image near the edges?
  41. * - Can this be done quickly and look decent?
  42. *
  43. * Dark Shikari links to http://wiki.videolan.org/SoC_x264_2010#GPU_Motion_Estimation_2
  44. * for an algorithm similar to what could be used here to get the gmv
  45. * It requires only a couple diamond searches + fast downscaling
  46. *
  47. * Special thanks to Jason Kotenko for his help with the algorithm and my
  48. * inability to see simple errors in C code.
  49. */
  50. #include "avfilter.h"
  51. #include "formats.h"
  52. #include "internal.h"
  53. #include "video.h"
  54. #include "libavutil/common.h"
  55. #include "libavutil/mem.h"
  56. #include "libavutil/opt.h"
  57. #include "libavutil/pixdesc.h"
  58. #include "libavcodec/dsputil.h"
  59. #include "deshake.h"
  60. #include "deshake_opencl.h"
  61. #define CHROMA_WIDTH(link) -((-link->w) >> av_pix_fmt_desc_get(link->format)->log2_chroma_w)
  62. #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h)
  63. #define OFFSET(x) offsetof(DeshakeContext, x)
  64. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  65. static const AVOption deshake_options[] = {
  66. { "x", "set x for the rectangular search area", OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  67. { "y", "set y for the rectangular search area", OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  68. { "w", "set width for the rectangular search area", OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  69. { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  70. { "rx", "set x for the rectangular search area", OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, 64, .flags = FLAGS },
  71. { "ry", "set y for the rectangular search area", OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, 64, .flags = FLAGS },
  72. { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
  73. { "blank", "fill zeroes at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK}, INT_MIN, INT_MAX, FLAGS, "edge" },
  74. { "original", "original image at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
  75. { "clamp", "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP}, INT_MIN, INT_MAX, FLAGS, "edge" },
  76. { "mirror", "mirrored edge at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR}, INT_MIN, INT_MAX, FLAGS, "edge" },
  77. { "blocksize", "set motion search blocksize", OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8}, 4, 128, .flags = FLAGS },
  78. { "contrast", "set contrast threshold for blocks", OFFSET(contrast), AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
  79. { "search", "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
  80. { "exhaustive", "exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
  81. { "less", "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
  82. { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  83. { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = FLAGS },
  84. { NULL }
  85. };
  86. AVFILTER_DEFINE_CLASS(deshake);
  87. static int cmp(const double *a, const double *b)
  88. {
  89. return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
  90. }
  91. /**
  92. * Cleaned mean (cuts off 20% of values to remove outliers and then averages)
  93. */
  94. static double clean_mean(double *values, int count)
  95. {
  96. double mean = 0;
  97. int cut = count / 5;
  98. int x;
  99. qsort(values, count, sizeof(double), (void*)cmp);
  100. for (x = cut; x < count - cut; x++) {
  101. mean += values[x];
  102. }
  103. return mean / (count - cut * 2);
  104. }
  105. /**
  106. * Find the most likely shift in motion between two frames for a given
  107. * macroblock. Test each block against several shifts given by the rx
  108. * and ry attributes. Searches using a simple matrix of those shifts and
  109. * chooses the most likely shift by the smallest difference in blocks.
  110. */
  111. static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
  112. uint8_t *src2, int cx, int cy, int stride,
  113. IntMotionVector *mv)
  114. {
  115. int x, y;
  116. int diff;
  117. int smallest = INT_MAX;
  118. int tmp, tmp2;
  119. #define CMP(i, j) deshake->c.sad[0](deshake, src1 + cy * stride + cx, \
  120. src2 + (j) * stride + (i), stride, \
  121. deshake->blocksize)
  122. if (deshake->search == EXHAUSTIVE) {
  123. // Compare every possible position - this is sloooow!
  124. for (y = -deshake->ry; y <= deshake->ry; y++) {
  125. for (x = -deshake->rx; x <= deshake->rx; x++) {
  126. diff = CMP(cx - x, cy - y);
  127. if (diff < smallest) {
  128. smallest = diff;
  129. mv->x = x;
  130. mv->y = y;
  131. }
  132. }
  133. }
  134. } else if (deshake->search == SMART_EXHAUSTIVE) {
  135. // Compare every other possible position and find the best match
  136. for (y = -deshake->ry + 1; y < deshake->ry - 2; y += 2) {
  137. for (x = -deshake->rx + 1; x < deshake->rx - 2; x += 2) {
  138. diff = CMP(cx - x, cy - y);
  139. if (diff < smallest) {
  140. smallest = diff;
  141. mv->x = x;
  142. mv->y = y;
  143. }
  144. }
  145. }
  146. // Hone in on the specific best match around the match we found above
  147. tmp = mv->x;
  148. tmp2 = mv->y;
  149. for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
  150. for (x = tmp - 1; x <= tmp + 1; x++) {
  151. if (x == tmp && y == tmp2)
  152. continue;
  153. diff = CMP(cx - x, cy - y);
  154. if (diff < smallest) {
  155. smallest = diff;
  156. mv->x = x;
  157. mv->y = y;
  158. }
  159. }
  160. }
  161. }
  162. if (smallest > 512) {
  163. mv->x = -1;
  164. mv->y = -1;
  165. }
  166. emms_c();
  167. //av_log(NULL, AV_LOG_ERROR, "%d\n", smallest);
  168. //av_log(NULL, AV_LOG_ERROR, "Final: (%d, %d) = %d x %d\n", cx, cy, mv->x, mv->y);
  169. }
  170. /**
  171. * Find the contrast of a given block. When searching for global motion we
  172. * really only care about the high contrast blocks, so using this method we
  173. * can actually skip blocks we don't care much about.
  174. */
  175. static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
  176. {
  177. int highest = 0;
  178. int lowest = 0;
  179. int i, j, pos;
  180. for (i = 0; i <= blocksize * 2; i++) {
  181. // We use a width of 16 here to match the libavcodec sad functions
  182. for (j = 0; i <= 15; i++) {
  183. pos = (y - i) * stride + (x - j);
  184. if (src[pos] < lowest)
  185. lowest = src[pos];
  186. else if (src[pos] > highest) {
  187. highest = src[pos];
  188. }
  189. }
  190. }
  191. return highest - lowest;
  192. }
  193. /**
  194. * Find the rotation for a given block.
  195. */
  196. static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
  197. {
  198. double a1, a2, diff;
  199. a1 = atan2(y - cy, x - cx);
  200. a2 = atan2(y - cy + shift->y, x - cx + shift->x);
  201. diff = a2 - a1;
  202. return (diff > M_PI) ? diff - 2 * M_PI :
  203. (diff < -M_PI) ? diff + 2 * M_PI :
  204. diff;
  205. }
  206. /**
  207. * Find the estimated global motion for a scene given the most likely shift
  208. * for each block in the frame. The global motion is estimated to be the
  209. * same as the motion from most blocks in the frame, so if most blocks
  210. * move one pixel to the right and two pixels down, this would yield a
  211. * motion vector (1, -2).
  212. */
  213. static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
  214. int width, int height, int stride, Transform *t)
  215. {
  216. int x, y;
  217. IntMotionVector mv = {0, 0};
  218. int counts[128][128];
  219. int count_max_value = 0;
  220. int contrast;
  221. int pos;
  222. double *angles = av_malloc(sizeof(*angles) * width * height / (16 * deshake->blocksize));
  223. int center_x = 0, center_y = 0;
  224. double p_x, p_y;
  225. // Reset counts to zero
  226. for (x = 0; x < deshake->rx * 2 + 1; x++) {
  227. for (y = 0; y < deshake->ry * 2 + 1; y++) {
  228. counts[x][y] = 0;
  229. }
  230. }
  231. pos = 0;
  232. // Find motion for every block and store the motion vector in the counts
  233. for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
  234. // We use a width of 16 here to match the libavcodec sad functions
  235. for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
  236. // If the contrast is too low, just skip this block as it probably
  237. // won't be very useful to us.
  238. contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
  239. if (contrast > deshake->contrast) {
  240. //av_log(NULL, AV_LOG_ERROR, "%d\n", contrast);
  241. find_block_motion(deshake, src1, src2, x, y, stride, &mv);
  242. if (mv.x != -1 && mv.y != -1) {
  243. counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
  244. if (x > deshake->rx && y > deshake->ry)
  245. angles[pos++] = block_angle(x, y, 0, 0, &mv);
  246. center_x += mv.x;
  247. center_y += mv.y;
  248. }
  249. }
  250. }
  251. }
  252. if (pos) {
  253. center_x /= pos;
  254. center_y /= pos;
  255. t->angle = clean_mean(angles, pos);
  256. if (t->angle < 0.001)
  257. t->angle = 0;
  258. } else {
  259. t->angle = 0;
  260. }
  261. // Find the most common motion vector in the frame and use it as the gmv
  262. for (y = deshake->ry * 2; y >= 0; y--) {
  263. for (x = 0; x < deshake->rx * 2 + 1; x++) {
  264. //av_log(NULL, AV_LOG_ERROR, "%5d ", counts[x][y]);
  265. if (counts[x][y] > count_max_value) {
  266. t->vector.x = x - deshake->rx;
  267. t->vector.y = y - deshake->ry;
  268. count_max_value = counts[x][y];
  269. }
  270. }
  271. //av_log(NULL, AV_LOG_ERROR, "\n");
  272. }
  273. p_x = (center_x - width / 2);
  274. p_y = (center_y - height / 2);
  275. t->vector.x += (cos(t->angle)-1)*p_x - sin(t->angle)*p_y;
  276. t->vector.y += sin(t->angle)*p_x + (cos(t->angle)-1)*p_y;
  277. // Clamp max shift & rotation?
  278. t->vector.x = av_clipf(t->vector.x, -deshake->rx * 2, deshake->rx * 2);
  279. t->vector.y = av_clipf(t->vector.y, -deshake->ry * 2, deshake->ry * 2);
  280. t->angle = av_clipf(t->angle, -0.1, 0.1);
  281. //av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
  282. av_free(angles);
  283. }
  284. static int deshake_transform_c(AVFilterContext *ctx,
  285. int width, int height, int cw, int ch,
  286. const float *matrix_y, const float *matrix_uv,
  287. enum InterpolateMethod interpolate,
  288. enum FillMethod fill, AVFrame *in, AVFrame *out)
  289. {
  290. int i = 0, ret = 0;
  291. const float *matrixs[3];
  292. int plane_w[3], plane_h[3];
  293. matrixs[0] = matrix_y;
  294. matrixs[1] = matrixs[2] = matrix_uv;
  295. plane_w[0] = width;
  296. plane_w[1] = plane_w[2] = cw;
  297. plane_h[0] = height;
  298. plane_h[1] = plane_h[2] = ch;
  299. for (i = 0; i < 3; i++) {
  300. // Transform the luma and chroma planes
  301. ret = avfilter_transform(in->data[i], out->data[i], in->linesize[i], out->linesize[i],
  302. plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
  303. if (ret < 0)
  304. return ret;
  305. }
  306. return ret;
  307. }
  308. static av_cold int init(AVFilterContext *ctx, const char *args)
  309. {
  310. int ret;
  311. DeshakeContext *deshake = ctx->priv;
  312. deshake->refcount = 20; // XXX: add to options?
  313. deshake->blocksize /= 2;
  314. deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
  315. if (deshake->filename)
  316. deshake->fp = fopen(deshake->filename, "w");
  317. if (deshake->fp)
  318. 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);
  319. // Quadword align left edge of box for MMX code, adjust width if necessary
  320. // to keep right margin
  321. if (deshake->cx > 0) {
  322. deshake->cw += deshake->cx - (deshake->cx & ~15);
  323. deshake->cx &= ~15;
  324. }
  325. deshake->transform = deshake_transform_c;
  326. if (!CONFIG_OPENCL && deshake->opencl) {
  327. av_log(ctx, AV_LOG_ERROR, "OpenCL support was not enabled in this build, cannot be selected\n");
  328. return AVERROR(EINVAL);
  329. }
  330. if (CONFIG_OPENCL && deshake->opencl) {
  331. deshake->transform = ff_opencl_transform;
  332. ret = ff_opencl_deshake_init(ctx);
  333. if (ret < 0)
  334. return ret;
  335. }
  336. 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",
  337. deshake->cx, deshake->cy, deshake->cw, deshake->ch,
  338. deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
  339. return 0;
  340. }
  341. static int query_formats(AVFilterContext *ctx)
  342. {
  343. static const enum AVPixelFormat pix_fmts[] = {
  344. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
  345. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  346. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  347. };
  348. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  349. return 0;
  350. }
  351. static int config_props(AVFilterLink *link)
  352. {
  353. DeshakeContext *deshake = link->dst->priv;
  354. deshake->ref = NULL;
  355. deshake->last.vector.x = 0;
  356. deshake->last.vector.y = 0;
  357. deshake->last.angle = 0;
  358. deshake->last.zoom = 0;
  359. deshake->avctx = avcodec_alloc_context3(NULL);
  360. dsputil_init(&deshake->c, deshake->avctx);
  361. return 0;
  362. }
  363. static av_cold void uninit(AVFilterContext *ctx)
  364. {
  365. DeshakeContext *deshake = ctx->priv;
  366. if (CONFIG_OPENCL && deshake->opencl) {
  367. ff_opencl_deshake_uninit(ctx);
  368. }
  369. av_frame_free(&deshake->ref);
  370. if (deshake->fp)
  371. fclose(deshake->fp);
  372. if (deshake->avctx)
  373. avcodec_close(deshake->avctx);
  374. av_freep(&deshake->avctx);
  375. }
  376. static int filter_frame(AVFilterLink *link, AVFrame *in)
  377. {
  378. DeshakeContext *deshake = link->dst->priv;
  379. AVFilterLink *outlink = link->dst->outputs[0];
  380. AVFrame *out;
  381. Transform t = {{0},0}, orig = {{0},0};
  382. float matrix_y[9], matrix_uv[9];
  383. float alpha = 2.0 / deshake->refcount;
  384. char tmp[256];
  385. int ret = 0;
  386. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  387. if (!out) {
  388. av_frame_free(&in);
  389. return AVERROR(ENOMEM);
  390. }
  391. av_frame_copy_props(out, in);
  392. if (CONFIG_OPENCL && deshake->opencl) {
  393. ret = ff_opencl_deshake_process_inout_buf(link->dst,in, out);
  394. if (ret < 0)
  395. return ret;
  396. }
  397. if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
  398. // Find the most likely global motion for the current frame
  399. find_motion(deshake, (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0], in->data[0], link->w, link->h, in->linesize[0], &t);
  400. } else {
  401. uint8_t *src1 = (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0];
  402. uint8_t *src2 = in->data[0];
  403. deshake->cx = FFMIN(deshake->cx, link->w);
  404. deshake->cy = FFMIN(deshake->cy, link->h);
  405. if ((unsigned)deshake->cx + (unsigned)deshake->cw > link->w) deshake->cw = link->w - deshake->cx;
  406. if ((unsigned)deshake->cy + (unsigned)deshake->ch > link->h) deshake->ch = link->h - deshake->cy;
  407. // Quadword align right margin
  408. deshake->cw &= ~15;
  409. src1 += deshake->cy * in->linesize[0] + deshake->cx;
  410. src2 += deshake->cy * in->linesize[0] + deshake->cx;
  411. find_motion(deshake, src1, src2, deshake->cw, deshake->ch, in->linesize[0], &t);
  412. }
  413. // Copy transform so we can output it later to compare to the smoothed value
  414. orig.vector.x = t.vector.x;
  415. orig.vector.y = t.vector.y;
  416. orig.angle = t.angle;
  417. orig.zoom = t.zoom;
  418. // Generate a one-sided moving exponential average
  419. deshake->avg.vector.x = alpha * t.vector.x + (1.0 - alpha) * deshake->avg.vector.x;
  420. deshake->avg.vector.y = alpha * t.vector.y + (1.0 - alpha) * deshake->avg.vector.y;
  421. deshake->avg.angle = alpha * t.angle + (1.0 - alpha) * deshake->avg.angle;
  422. deshake->avg.zoom = alpha * t.zoom + (1.0 - alpha) * deshake->avg.zoom;
  423. // Remove the average from the current motion to detect the motion that
  424. // is not on purpose, just as jitter from bumping the camera
  425. t.vector.x -= deshake->avg.vector.x;
  426. t.vector.y -= deshake->avg.vector.y;
  427. t.angle -= deshake->avg.angle;
  428. t.zoom -= deshake->avg.zoom;
  429. // Invert the motion to undo it
  430. t.vector.x *= -1;
  431. t.vector.y *= -1;
  432. t.angle *= -1;
  433. // Write statistics to file
  434. if (deshake->fp) {
  435. 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);
  436. fwrite(tmp, sizeof(char), strlen(tmp), deshake->fp);
  437. }
  438. // Turn relative current frame motion into absolute by adding it to the
  439. // last absolute motion
  440. t.vector.x += deshake->last.vector.x;
  441. t.vector.y += deshake->last.vector.y;
  442. t.angle += deshake->last.angle;
  443. t.zoom += deshake->last.zoom;
  444. // Shrink motion by 10% to keep things centered in the camera frame
  445. t.vector.x *= 0.9;
  446. t.vector.y *= 0.9;
  447. t.angle *= 0.9;
  448. // Store the last absolute motion information
  449. deshake->last.vector.x = t.vector.x;
  450. deshake->last.vector.y = t.vector.y;
  451. deshake->last.angle = t.angle;
  452. deshake->last.zoom = t.zoom;
  453. // Generate a luma transformation matrix
  454. avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix_y);
  455. // Generate a chroma transformation matrix
  456. avfilter_get_matrix(t.vector.x / (link->w / CHROMA_WIDTH(link)), t.vector.y / (link->h / CHROMA_HEIGHT(link)), t.angle, 1.0 + t.zoom / 100.0, matrix_uv);
  457. // Transform the luma and chroma planes
  458. ret = deshake->transform(link->dst, link->w, link->h, CHROMA_WIDTH(link), CHROMA_HEIGHT(link),
  459. matrix_y, matrix_uv, INTERPOLATE_BILINEAR, deshake->edge, in, out);
  460. // Cleanup the old reference frame
  461. av_frame_free(&deshake->ref);
  462. if (ret < 0)
  463. return ret;
  464. // Store the current frame as the reference frame for calculating the
  465. // motion of the next frame
  466. deshake->ref = in;
  467. return ff_filter_frame(outlink, out);
  468. }
  469. static const AVFilterPad deshake_inputs[] = {
  470. {
  471. .name = "default",
  472. .type = AVMEDIA_TYPE_VIDEO,
  473. .filter_frame = filter_frame,
  474. .config_props = config_props,
  475. },
  476. { NULL }
  477. };
  478. static const AVFilterPad deshake_outputs[] = {
  479. {
  480. .name = "default",
  481. .type = AVMEDIA_TYPE_VIDEO,
  482. },
  483. { NULL }
  484. };
  485. static const char *const shorthand[] = {
  486. "x", "y", "w", "h", "rx", "ry", "edge",
  487. "blocksize", "contrast", "search", "filename",
  488. NULL
  489. };
  490. AVFilter avfilter_vf_deshake = {
  491. .name = "deshake",
  492. .description = NULL_IF_CONFIG_SMALL("Stabilize shaky video."),
  493. .priv_size = sizeof(DeshakeContext),
  494. .init = init,
  495. .uninit = uninit,
  496. .query_formats = query_formats,
  497. .inputs = deshake_inputs,
  498. .outputs = deshake_outputs,
  499. .priv_class = &deshake_class,
  500. .shorthand = shorthand,
  501. };