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.

586 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 "transform.h"
  60. #define CHROMA_WIDTH(link) -((-link->w) >> av_pix_fmt_desc_get(link->format)->log2_chroma_w)
  61. #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h)
  62. enum SearchMethod {
  63. EXHAUSTIVE, ///< Search all possible positions
  64. SMART_EXHAUSTIVE, ///< Search most possible positions (faster)
  65. SEARCH_COUNT
  66. };
  67. typedef struct {
  68. int x; ///< Horizontal shift
  69. int y; ///< Vertical shift
  70. } IntMotionVector;
  71. typedef struct {
  72. double x; ///< Horizontal shift
  73. double y; ///< Vertical shift
  74. } MotionVector;
  75. typedef struct {
  76. MotionVector vector; ///< Motion vector
  77. double angle; ///< Angle of rotation
  78. double zoom; ///< Zoom percentage
  79. } Transform;
  80. typedef struct {
  81. const AVClass *class;
  82. AVFrame *ref; ///< Previous frame
  83. int rx; ///< Maximum horizontal shift
  84. int ry; ///< Maximum vertical shift
  85. int edge; ///< Edge fill method
  86. int blocksize; ///< Size of blocks to compare
  87. int contrast; ///< Contrast threshold
  88. int search; ///< Motion search method
  89. AVCodecContext *avctx;
  90. DSPContext c; ///< Context providing optimized SAD methods
  91. Transform last; ///< Transform from last frame
  92. int refcount; ///< Number of reference frames (defines averaging window)
  93. FILE *fp;
  94. Transform avg;
  95. int cw; ///< Crop motion search to this box
  96. int ch;
  97. int cx;
  98. int cy;
  99. char *filename; ///< Motion search detailed log filename
  100. } DeshakeContext;
  101. #define OFFSET(x) offsetof(DeshakeContext, x)
  102. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  103. static const AVOption deshake_options[] = {
  104. { "x", "set x for the rectangular search area", OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  105. { "y", "set y for the rectangular search area", OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  106. { "w", "set width for the rectangular search area", OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  107. { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  108. { "rx", "set x for the rectangular search area", OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, 64, .flags = FLAGS },
  109. { "ry", "set y for the rectangular search area", OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, 64, .flags = FLAGS },
  110. { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
  111. { "blank", "fill zeroes at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK}, INT_MIN, INT_MAX, FLAGS, "edge" },
  112. { "original", "original image at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
  113. { "clamp", "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP}, INT_MIN, INT_MAX, FLAGS, "edge" },
  114. { "mirror", "mirrored edge at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR}, INT_MIN, INT_MAX, FLAGS, "edge" },
  115. { "blocksize", "set motion search blocksize", OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8}, 4, 128, .flags = FLAGS },
  116. { "contrast", "set contrast threshold for blocks", OFFSET(contrast), AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
  117. { "search", "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
  118. { "exhaustive", "exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
  119. { "less", "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
  120. { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  121. { NULL }
  122. };
  123. AVFILTER_DEFINE_CLASS(deshake);
  124. static int cmp(const double *a, const double *b)
  125. {
  126. return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
  127. }
  128. /**
  129. * Cleaned mean (cuts off 20% of values to remove outliers and then averages)
  130. */
  131. static double clean_mean(double *values, int count)
  132. {
  133. double mean = 0;
  134. int cut = count / 5;
  135. int x;
  136. qsort(values, count, sizeof(double), (void*)cmp);
  137. for (x = cut; x < count - cut; x++) {
  138. mean += values[x];
  139. }
  140. return mean / (count - cut * 2);
  141. }
  142. /**
  143. * Find the most likely shift in motion between two frames for a given
  144. * macroblock. Test each block against several shifts given by the rx
  145. * and ry attributes. Searches using a simple matrix of those shifts and
  146. * chooses the most likely shift by the smallest difference in blocks.
  147. */
  148. static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
  149. uint8_t *src2, int cx, int cy, int stride,
  150. IntMotionVector *mv)
  151. {
  152. int x, y;
  153. int diff;
  154. int smallest = INT_MAX;
  155. int tmp, tmp2;
  156. #define CMP(i, j) deshake->c.sad[0](deshake, src1 + cy * stride + cx, \
  157. src2 + (j) * stride + (i), stride, \
  158. deshake->blocksize)
  159. if (deshake->search == EXHAUSTIVE) {
  160. // Compare every possible position - this is sloooow!
  161. for (y = -deshake->ry; y <= deshake->ry; y++) {
  162. for (x = -deshake->rx; x <= deshake->rx; x++) {
  163. diff = CMP(cx - x, cy - y);
  164. if (diff < smallest) {
  165. smallest = diff;
  166. mv->x = x;
  167. mv->y = y;
  168. }
  169. }
  170. }
  171. } else if (deshake->search == SMART_EXHAUSTIVE) {
  172. // Compare every other possible position and find the best match
  173. for (y = -deshake->ry + 1; y < deshake->ry - 2; y += 2) {
  174. for (x = -deshake->rx + 1; x < deshake->rx - 2; x += 2) {
  175. diff = CMP(cx - x, cy - y);
  176. if (diff < smallest) {
  177. smallest = diff;
  178. mv->x = x;
  179. mv->y = y;
  180. }
  181. }
  182. }
  183. // Hone in on the specific best match around the match we found above
  184. tmp = mv->x;
  185. tmp2 = mv->y;
  186. for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
  187. for (x = tmp - 1; x <= tmp + 1; x++) {
  188. if (x == tmp && y == tmp2)
  189. continue;
  190. diff = CMP(cx - x, cy - y);
  191. if (diff < smallest) {
  192. smallest = diff;
  193. mv->x = x;
  194. mv->y = y;
  195. }
  196. }
  197. }
  198. }
  199. if (smallest > 512) {
  200. mv->x = -1;
  201. mv->y = -1;
  202. }
  203. emms_c();
  204. //av_log(NULL, AV_LOG_ERROR, "%d\n", smallest);
  205. //av_log(NULL, AV_LOG_ERROR, "Final: (%d, %d) = %d x %d\n", cx, cy, mv->x, mv->y);
  206. }
  207. /**
  208. * Find the contrast of a given block. When searching for global motion we
  209. * really only care about the high contrast blocks, so using this method we
  210. * can actually skip blocks we don't care much about.
  211. */
  212. static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
  213. {
  214. int highest = 0;
  215. int lowest = 0;
  216. int i, j, pos;
  217. for (i = 0; i <= blocksize * 2; i++) {
  218. // We use a width of 16 here to match the libavcodec sad functions
  219. for (j = 0; i <= 15; i++) {
  220. pos = (y - i) * stride + (x - j);
  221. if (src[pos] < lowest)
  222. lowest = src[pos];
  223. else if (src[pos] > highest) {
  224. highest = src[pos];
  225. }
  226. }
  227. }
  228. return highest - lowest;
  229. }
  230. /**
  231. * Find the rotation for a given block.
  232. */
  233. static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
  234. {
  235. double a1, a2, diff;
  236. a1 = atan2(y - cy, x - cx);
  237. a2 = atan2(y - cy + shift->y, x - cx + shift->x);
  238. diff = a2 - a1;
  239. return (diff > M_PI) ? diff - 2 * M_PI :
  240. (diff < -M_PI) ? diff + 2 * M_PI :
  241. diff;
  242. }
  243. /**
  244. * Find the estimated global motion for a scene given the most likely shift
  245. * for each block in the frame. The global motion is estimated to be the
  246. * same as the motion from most blocks in the frame, so if most blocks
  247. * move one pixel to the right and two pixels down, this would yield a
  248. * motion vector (1, -2).
  249. */
  250. static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
  251. int width, int height, int stride, Transform *t)
  252. {
  253. int x, y;
  254. IntMotionVector mv = {0, 0};
  255. int counts[128][128];
  256. int count_max_value = 0;
  257. int contrast;
  258. int pos;
  259. double *angles = av_malloc(sizeof(*angles) * width * height / (16 * deshake->blocksize));
  260. int center_x = 0, center_y = 0;
  261. double p_x, p_y;
  262. // Reset counts to zero
  263. for (x = 0; x < deshake->rx * 2 + 1; x++) {
  264. for (y = 0; y < deshake->ry * 2 + 1; y++) {
  265. counts[x][y] = 0;
  266. }
  267. }
  268. pos = 0;
  269. // Find motion for every block and store the motion vector in the counts
  270. for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
  271. // We use a width of 16 here to match the libavcodec sad functions
  272. for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
  273. // If the contrast is too low, just skip this block as it probably
  274. // won't be very useful to us.
  275. contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
  276. if (contrast > deshake->contrast) {
  277. //av_log(NULL, AV_LOG_ERROR, "%d\n", contrast);
  278. find_block_motion(deshake, src1, src2, x, y, stride, &mv);
  279. if (mv.x != -1 && mv.y != -1) {
  280. counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
  281. if (x > deshake->rx && y > deshake->ry)
  282. angles[pos++] = block_angle(x, y, 0, 0, &mv);
  283. center_x += mv.x;
  284. center_y += mv.y;
  285. }
  286. }
  287. }
  288. }
  289. if (pos) {
  290. center_x /= pos;
  291. center_y /= pos;
  292. t->angle = clean_mean(angles, pos);
  293. if (t->angle < 0.001)
  294. t->angle = 0;
  295. } else {
  296. t->angle = 0;
  297. }
  298. // Find the most common motion vector in the frame and use it as the gmv
  299. for (y = deshake->ry * 2; y >= 0; y--) {
  300. for (x = 0; x < deshake->rx * 2 + 1; x++) {
  301. //av_log(NULL, AV_LOG_ERROR, "%5d ", counts[x][y]);
  302. if (counts[x][y] > count_max_value) {
  303. t->vector.x = x - deshake->rx;
  304. t->vector.y = y - deshake->ry;
  305. count_max_value = counts[x][y];
  306. }
  307. }
  308. //av_log(NULL, AV_LOG_ERROR, "\n");
  309. }
  310. p_x = (center_x - width / 2);
  311. p_y = (center_y - height / 2);
  312. t->vector.x += (cos(t->angle)-1)*p_x - sin(t->angle)*p_y;
  313. t->vector.y += sin(t->angle)*p_x + (cos(t->angle)-1)*p_y;
  314. // Clamp max shift & rotation?
  315. t->vector.x = av_clipf(t->vector.x, -deshake->rx * 2, deshake->rx * 2);
  316. t->vector.y = av_clipf(t->vector.y, -deshake->ry * 2, deshake->ry * 2);
  317. t->angle = av_clipf(t->angle, -0.1, 0.1);
  318. //av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
  319. av_free(angles);
  320. }
  321. static av_cold int init(AVFilterContext *ctx, const char *args)
  322. {
  323. int ret;
  324. DeshakeContext *deshake = ctx->priv;
  325. static const char *shorthand[] = {
  326. "x", "y", "w", "h", "rx", "ry", "edge",
  327. "blocksize", "contrast", "search", "filename",
  328. NULL
  329. };
  330. deshake->refcount = 20; // XXX: add to options?
  331. deshake->class = &deshake_class;
  332. av_opt_set_defaults(deshake);
  333. ret = av_opt_set_from_string(deshake, args, shorthand, "=", ":");
  334. if (ret < 0)
  335. return ret;
  336. deshake->blocksize /= 2;
  337. deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
  338. if (deshake->filename)
  339. deshake->fp = fopen(deshake->filename, "w");
  340. if (deshake->fp)
  341. 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);
  342. // Quadword align left edge of box for MMX code, adjust width if necessary
  343. // to keep right margin
  344. if (deshake->cx > 0) {
  345. deshake->cw += deshake->cx - (deshake->cx & ~15);
  346. deshake->cx &= ~15;
  347. }
  348. 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",
  349. deshake->cx, deshake->cy, deshake->cw, deshake->ch,
  350. deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
  351. return 0;
  352. }
  353. static int query_formats(AVFilterContext *ctx)
  354. {
  355. static const enum AVPixelFormat pix_fmts[] = {
  356. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
  357. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  358. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  359. };
  360. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  361. return 0;
  362. }
  363. static int config_props(AVFilterLink *link)
  364. {
  365. DeshakeContext *deshake = link->dst->priv;
  366. deshake->ref = NULL;
  367. deshake->last.vector.x = 0;
  368. deshake->last.vector.y = 0;
  369. deshake->last.angle = 0;
  370. deshake->last.zoom = 0;
  371. deshake->avctx = avcodec_alloc_context3(NULL);
  372. dsputil_init(&deshake->c, deshake->avctx);
  373. return 0;
  374. }
  375. static av_cold void uninit(AVFilterContext *ctx)
  376. {
  377. DeshakeContext *deshake = ctx->priv;
  378. av_frame_free(&deshake->ref);
  379. if (deshake->fp)
  380. fclose(deshake->fp);
  381. if (deshake->avctx)
  382. avcodec_close(deshake->avctx);
  383. av_freep(&deshake->avctx);
  384. av_opt_free(deshake);
  385. }
  386. static int filter_frame(AVFilterLink *link, AVFrame *in)
  387. {
  388. DeshakeContext *deshake = link->dst->priv;
  389. AVFilterLink *outlink = link->dst->outputs[0];
  390. AVFrame *out;
  391. Transform t = {{0},0}, orig = {{0},0};
  392. float matrix[9];
  393. float alpha = 2.0 / deshake->refcount;
  394. char tmp[256];
  395. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  396. if (!out) {
  397. av_frame_free(&in);
  398. return AVERROR(ENOMEM);
  399. }
  400. av_frame_copy_props(out, in);
  401. if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
  402. // Find the most likely global motion for the current frame
  403. find_motion(deshake, (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0], in->data[0], link->w, link->h, in->linesize[0], &t);
  404. } else {
  405. uint8_t *src1 = (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0];
  406. uint8_t *src2 = in->data[0];
  407. deshake->cx = FFMIN(deshake->cx, link->w);
  408. deshake->cy = FFMIN(deshake->cy, link->h);
  409. if ((unsigned)deshake->cx + (unsigned)deshake->cw > link->w) deshake->cw = link->w - deshake->cx;
  410. if ((unsigned)deshake->cy + (unsigned)deshake->ch > link->h) deshake->ch = link->h - deshake->cy;
  411. // Quadword align right margin
  412. deshake->cw &= ~15;
  413. src1 += deshake->cy * in->linesize[0] + deshake->cx;
  414. src2 += deshake->cy * in->linesize[0] + deshake->cx;
  415. find_motion(deshake, src1, src2, deshake->cw, deshake->ch, in->linesize[0], &t);
  416. }
  417. // Copy transform so we can output it later to compare to the smoothed value
  418. orig.vector.x = t.vector.x;
  419. orig.vector.y = t.vector.y;
  420. orig.angle = t.angle;
  421. orig.zoom = t.zoom;
  422. // Generate a one-sided moving exponential average
  423. deshake->avg.vector.x = alpha * t.vector.x + (1.0 - alpha) * deshake->avg.vector.x;
  424. deshake->avg.vector.y = alpha * t.vector.y + (1.0 - alpha) * deshake->avg.vector.y;
  425. deshake->avg.angle = alpha * t.angle + (1.0 - alpha) * deshake->avg.angle;
  426. deshake->avg.zoom = alpha * t.zoom + (1.0 - alpha) * deshake->avg.zoom;
  427. // Remove the average from the current motion to detect the motion that
  428. // is not on purpose, just as jitter from bumping the camera
  429. t.vector.x -= deshake->avg.vector.x;
  430. t.vector.y -= deshake->avg.vector.y;
  431. t.angle -= deshake->avg.angle;
  432. t.zoom -= deshake->avg.zoom;
  433. // Invert the motion to undo it
  434. t.vector.x *= -1;
  435. t.vector.y *= -1;
  436. t.angle *= -1;
  437. // Write statistics to file
  438. if (deshake->fp) {
  439. 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);
  440. fwrite(tmp, sizeof(char), strlen(tmp), deshake->fp);
  441. }
  442. // Turn relative current frame motion into absolute by adding it to the
  443. // last absolute motion
  444. t.vector.x += deshake->last.vector.x;
  445. t.vector.y += deshake->last.vector.y;
  446. t.angle += deshake->last.angle;
  447. t.zoom += deshake->last.zoom;
  448. // Shrink motion by 10% to keep things centered in the camera frame
  449. t.vector.x *= 0.9;
  450. t.vector.y *= 0.9;
  451. t.angle *= 0.9;
  452. // Store the last absolute motion information
  453. deshake->last.vector.x = t.vector.x;
  454. deshake->last.vector.y = t.vector.y;
  455. deshake->last.angle = t.angle;
  456. deshake->last.zoom = t.zoom;
  457. // Generate a luma transformation matrix
  458. avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix);
  459. // Transform the luma plane
  460. avfilter_transform(in->data[0], out->data[0], in->linesize[0], out->linesize[0], link->w, link->h, matrix, INTERPOLATE_BILINEAR, deshake->edge);
  461. // Generate a chroma transformation matrix
  462. 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);
  463. // Transform the chroma planes
  464. avfilter_transform(in->data[1], out->data[1], in->linesize[1], out->linesize[1], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), matrix, INTERPOLATE_BILINEAR, deshake->edge);
  465. avfilter_transform(in->data[2], out->data[2], in->linesize[2], out->linesize[2], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), matrix, INTERPOLATE_BILINEAR, deshake->edge);
  466. // Cleanup the old reference frame
  467. av_frame_free(&deshake->ref);
  468. // Store the current frame as the reference frame for calculating the
  469. // motion of the next frame
  470. deshake->ref = in;
  471. return ff_filter_frame(outlink, out);
  472. }
  473. static const AVFilterPad deshake_inputs[] = {
  474. {
  475. .name = "default",
  476. .type = AVMEDIA_TYPE_VIDEO,
  477. .filter_frame = filter_frame,
  478. .config_props = config_props,
  479. },
  480. { NULL }
  481. };
  482. static const AVFilterPad deshake_outputs[] = {
  483. {
  484. .name = "default",
  485. .type = AVMEDIA_TYPE_VIDEO,
  486. },
  487. { NULL }
  488. };
  489. AVFilter avfilter_vf_deshake = {
  490. .name = "deshake",
  491. .description = NULL_IF_CONFIG_SMALL("Stabilize shaky video."),
  492. .priv_size = sizeof(DeshakeContext),
  493. .init = init,
  494. .uninit = uninit,
  495. .query_formats = query_formats,
  496. .inputs = deshake_inputs,
  497. .outputs = deshake_outputs,
  498. .priv_class = &deshake_class,
  499. };