vf_deshake.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 "libavutil/qsort.h"
  59. #include "deshake.h"
  60. #include "deshake_opencl.h"
  61. #define OFFSET(x) offsetof(DeshakeContext, x)
  62. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  63. static const AVOption deshake_options[] = {
  64. { "x", "set x for the rectangular search area", OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  65. { "y", "set y for the rectangular search area", OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  66. { "w", "set width for the rectangular search area", OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  67. { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  68. { "rx", "set x for the rectangular search area", OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
  69. { "ry", "set y for the rectangular search area", OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
  70. { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
  71. { "blank", "fill zeroes at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK}, INT_MIN, INT_MAX, FLAGS, "edge" },
  72. { "original", "original image at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
  73. { "clamp", "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP}, INT_MIN, INT_MAX, FLAGS, "edge" },
  74. { "mirror", "mirrored edge at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR}, INT_MIN, INT_MAX, FLAGS, "edge" },
  75. { "blocksize", "set motion search blocksize", OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8}, 4, 128, .flags = FLAGS },
  76. { "contrast", "set contrast threshold for blocks", OFFSET(contrast), AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
  77. { "search", "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
  78. { "exhaustive", "exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
  79. { "less", "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
  80. { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  81. { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  82. { NULL }
  83. };
  84. AVFILTER_DEFINE_CLASS(deshake);
  85. static int cmp(const void *a, const void *b)
  86. {
  87. return FFDIFFSIGN(*(const double *)a, *(const double *)b);
  88. }
  89. /**
  90. * Cleaned mean (cuts off 20% of values to remove outliers and then averages)
  91. */
  92. static double clean_mean(double *values, int count)
  93. {
  94. double mean = 0;
  95. int cut = count / 5;
  96. int x;
  97. AV_QSORT(values, count, double, cmp);
  98. for (x = cut; x < count - cut; x++) {
  99. mean += values[x];
  100. }
  101. return mean / (count - cut * 2);
  102. }
  103. /**
  104. * Find the most likely shift in motion between two frames for a given
  105. * macroblock. Test each block against several shifts given by the rx
  106. * and ry attributes. Searches using a simple matrix of those shifts and
  107. * chooses the most likely shift by the smallest difference in blocks.
  108. */
  109. static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
  110. uint8_t *src2, int cx, int cy, int stride,
  111. IntMotionVector *mv)
  112. {
  113. int x, y;
  114. int diff;
  115. int smallest = INT_MAX;
  116. int tmp, tmp2;
  117. #define CMP(i, j) deshake->sad(src1 + cy * stride + cx, stride,\
  118. src2 + (j) * stride + (i), stride)
  119. if (deshake->search == EXHAUSTIVE) {
  120. // Compare every possible position - this is sloooow!
  121. for (y = -deshake->ry; y <= deshake->ry; y++) {
  122. for (x = -deshake->rx; x <= deshake->rx; x++) {
  123. diff = CMP(cx - x, cy - y);
  124. if (diff < smallest) {
  125. smallest = diff;
  126. mv->x = x;
  127. mv->y = y;
  128. }
  129. }
  130. }
  131. } else if (deshake->search == SMART_EXHAUSTIVE) {
  132. // Compare every other possible position and find the best match
  133. for (y = -deshake->ry + 1; y < deshake->ry; y += 2) {
  134. for (x = -deshake->rx + 1; x < deshake->rx; x += 2) {
  135. diff = CMP(cx - x, cy - y);
  136. if (diff < smallest) {
  137. smallest = diff;
  138. mv->x = x;
  139. mv->y = y;
  140. }
  141. }
  142. }
  143. // Hone in on the specific best match around the match we found above
  144. tmp = mv->x;
  145. tmp2 = mv->y;
  146. for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
  147. for (x = tmp - 1; x <= tmp + 1; x++) {
  148. if (x == tmp && y == tmp2)
  149. continue;
  150. diff = CMP(cx - x, cy - y);
  151. if (diff < smallest) {
  152. smallest = diff;
  153. mv->x = x;
  154. mv->y = y;
  155. }
  156. }
  157. }
  158. }
  159. if (smallest > 512) {
  160. mv->x = -1;
  161. mv->y = -1;
  162. }
  163. emms_c();
  164. //av_log(NULL, AV_LOG_ERROR, "%d\n", smallest);
  165. //av_log(NULL, AV_LOG_ERROR, "Final: (%d, %d) = %d x %d\n", cx, cy, mv->x, mv->y);
  166. }
  167. /**
  168. * Find the contrast of a given block. When searching for global motion we
  169. * really only care about the high contrast blocks, so using this method we
  170. * can actually skip blocks we don't care much about.
  171. */
  172. static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
  173. {
  174. int highest = 0;
  175. int lowest = 255;
  176. int i, j, pos;
  177. for (i = 0; i <= blocksize * 2; i++) {
  178. // We use a width of 16 here to match the sad function
  179. for (j = 0; j <= 15; j++) {
  180. pos = (y - i) * stride + (x - j);
  181. if (src[pos] < lowest)
  182. lowest = src[pos];
  183. else if (src[pos] > highest) {
  184. highest = src[pos];
  185. }
  186. }
  187. }
  188. return highest - lowest;
  189. }
  190. /**
  191. * Find the rotation for a given block.
  192. */
  193. static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
  194. {
  195. double a1, a2, diff;
  196. a1 = atan2(y - cy, x - cx);
  197. a2 = atan2(y - cy + shift->y, x - cx + shift->x);
  198. diff = a2 - a1;
  199. return (diff > M_PI) ? diff - 2 * M_PI :
  200. (diff < -M_PI) ? diff + 2 * M_PI :
  201. diff;
  202. }
  203. /**
  204. * Find the estimated global motion for a scene given the most likely shift
  205. * for each block in the frame. The global motion is estimated to be the
  206. * same as the motion from most blocks in the frame, so if most blocks
  207. * move one pixel to the right and two pixels down, this would yield a
  208. * motion vector (1, -2).
  209. */
  210. static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
  211. int width, int height, int stride, Transform *t)
  212. {
  213. int x, y;
  214. IntMotionVector mv = {0, 0};
  215. int count_max_value = 0;
  216. int contrast;
  217. int pos;
  218. int center_x = 0, center_y = 0;
  219. double p_x, p_y;
  220. av_fast_malloc(&deshake->angles, &deshake->angles_size, width * height / (16 * deshake->blocksize) * sizeof(*deshake->angles));
  221. // Reset counts to zero
  222. for (x = 0; x < deshake->rx * 2 + 1; x++) {
  223. for (y = 0; y < deshake->ry * 2 + 1; y++) {
  224. deshake->counts[x][y] = 0;
  225. }
  226. }
  227. pos = 0;
  228. // Find motion for every block and store the motion vector in the counts
  229. for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
  230. // We use a width of 16 here to match the sad function
  231. for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
  232. // If the contrast is too low, just skip this block as it probably
  233. // won't be very useful to us.
  234. contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
  235. if (contrast > deshake->contrast) {
  236. //av_log(NULL, AV_LOG_ERROR, "%d\n", contrast);
  237. find_block_motion(deshake, src1, src2, x, y, stride, &mv);
  238. if (mv.x != -1 && mv.y != -1) {
  239. deshake->counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
  240. if (x > deshake->rx && y > deshake->ry)
  241. deshake->angles[pos++] = block_angle(x, y, 0, 0, &mv);
  242. center_x += mv.x;
  243. center_y += mv.y;
  244. }
  245. }
  246. }
  247. }
  248. if (pos) {
  249. center_x /= pos;
  250. center_y /= pos;
  251. t->angle = clean_mean(deshake->angles, pos);
  252. if (t->angle < 0.001)
  253. t->angle = 0;
  254. } else {
  255. t->angle = 0;
  256. }
  257. // Find the most common motion vector in the frame and use it as the gmv
  258. for (y = deshake->ry * 2; y >= 0; y--) {
  259. for (x = 0; x < deshake->rx * 2 + 1; x++) {
  260. //av_log(NULL, AV_LOG_ERROR, "%5d ", deshake->counts[x][y]);
  261. if (deshake->counts[x][y] > count_max_value) {
  262. t->vec.x = x - deshake->rx;
  263. t->vec.y = y - deshake->ry;
  264. count_max_value = deshake->counts[x][y];
  265. }
  266. }
  267. //av_log(NULL, AV_LOG_ERROR, "\n");
  268. }
  269. p_x = (center_x - width / 2.0);
  270. p_y = (center_y - height / 2.0);
  271. t->vec.x += (cos(t->angle)-1)*p_x - sin(t->angle)*p_y;
  272. t->vec.y += sin(t->angle)*p_x + (cos(t->angle)-1)*p_y;
  273. // Clamp max shift & rotation?
  274. t->vec.x = av_clipf(t->vec.x, -deshake->rx * 2, deshake->rx * 2);
  275. t->vec.y = av_clipf(t->vec.y, -deshake->ry * 2, deshake->ry * 2);
  276. t->angle = av_clipf(t->angle, -0.1, 0.1);
  277. //av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
  278. }
  279. static int deshake_transform_c(AVFilterContext *ctx,
  280. int width, int height, int cw, int ch,
  281. const float *matrix_y, const float *matrix_uv,
  282. enum InterpolateMethod interpolate,
  283. enum FillMethod fill, AVFrame *in, AVFrame *out)
  284. {
  285. int i = 0, ret = 0;
  286. const float *matrixs[3];
  287. int plane_w[3], plane_h[3];
  288. matrixs[0] = matrix_y;
  289. matrixs[1] = matrixs[2] = matrix_uv;
  290. plane_w[0] = width;
  291. plane_w[1] = plane_w[2] = cw;
  292. plane_h[0] = height;
  293. plane_h[1] = plane_h[2] = ch;
  294. for (i = 0; i < 3; i++) {
  295. // Transform the luma and chroma planes
  296. ret = avfilter_transform(in->data[i], out->data[i], in->linesize[i], out->linesize[i],
  297. plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
  298. if (ret < 0)
  299. return ret;
  300. }
  301. return ret;
  302. }
  303. static av_cold int init(AVFilterContext *ctx)
  304. {
  305. int ret;
  306. DeshakeContext *deshake = ctx->priv;
  307. deshake->sad = av_pixelutils_get_sad_fn(4, 4, 1, deshake); // 16x16, 2nd source unaligned
  308. if (!deshake->sad)
  309. return AVERROR(EINVAL);
  310. deshake->refcount = 20; // XXX: add to options?
  311. deshake->blocksize /= 2;
  312. deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
  313. if (deshake->rx % 16) {
  314. av_log(ctx, AV_LOG_ERROR, "rx must be a multiple of 16\n");
  315. return AVERROR_PATCHWELCOME;
  316. }
  317. if (deshake->filename)
  318. deshake->fp = fopen(deshake->filename, "w");
  319. if (deshake->fp)
  320. 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);
  321. // Quadword align left edge of box for MMX code, adjust width if necessary
  322. // to keep right margin
  323. if (deshake->cx > 0) {
  324. deshake->cw += deshake->cx - (deshake->cx & ~15);
  325. deshake->cx &= ~15;
  326. }
  327. deshake->transform = deshake_transform_c;
  328. if (!CONFIG_OPENCL && deshake->opencl) {
  329. av_log(ctx, AV_LOG_ERROR, "OpenCL support was not enabled in this build, cannot be selected\n");
  330. return AVERROR(EINVAL);
  331. }
  332. if (CONFIG_OPENCL && deshake->opencl) {
  333. deshake->transform = ff_opencl_transform;
  334. ret = ff_opencl_deshake_init(ctx);
  335. if (ret < 0)
  336. return ret;
  337. }
  338. 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",
  339. deshake->cx, deshake->cy, deshake->cw, deshake->ch,
  340. deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
  341. return 0;
  342. }
  343. static int query_formats(AVFilterContext *ctx)
  344. {
  345. static const enum AVPixelFormat pix_fmts[] = {
  346. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
  347. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  348. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  349. };
  350. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  351. if (!fmts_list)
  352. return AVERROR(ENOMEM);
  353. return ff_set_common_formats(ctx, fmts_list);
  354. }
  355. static int config_props(AVFilterLink *link)
  356. {
  357. DeshakeContext *deshake = link->dst->priv;
  358. deshake->ref = NULL;
  359. deshake->last.vec.x = 0;
  360. deshake->last.vec.y = 0;
  361. deshake->last.angle = 0;
  362. deshake->last.zoom = 0;
  363. return 0;
  364. }
  365. static av_cold void uninit(AVFilterContext *ctx)
  366. {
  367. DeshakeContext *deshake = ctx->priv;
  368. if (CONFIG_OPENCL && deshake->opencl) {
  369. ff_opencl_deshake_uninit(ctx);
  370. }
  371. av_frame_free(&deshake->ref);
  372. av_freep(&deshake->angles);
  373. deshake->angles_size = 0;
  374. if (deshake->fp)
  375. fclose(deshake->fp);
  376. }
  377. static int filter_frame(AVFilterLink *link, AVFrame *in)
  378. {
  379. DeshakeContext *deshake = link->dst->priv;
  380. AVFilterLink *outlink = link->dst->outputs[0];
  381. AVFrame *out;
  382. Transform t = {{0},0}, orig = {{0},0};
  383. float matrix_y[9], matrix_uv[9];
  384. float alpha = 2.0 / deshake->refcount;
  385. char tmp[256];
  386. int ret = 0;
  387. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  388. const int chroma_width = AV_CEIL_RSHIFT(link->w, desc->log2_chroma_w);
  389. const int chroma_height = AV_CEIL_RSHIFT(link->h, desc->log2_chroma_h);
  390. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  391. if (!out) {
  392. av_frame_free(&in);
  393. return AVERROR(ENOMEM);
  394. }
  395. av_frame_copy_props(out, in);
  396. if (CONFIG_OPENCL && deshake->opencl) {
  397. ret = ff_opencl_deshake_process_inout_buf(link->dst,in, out);
  398. if (ret < 0)
  399. return ret;
  400. }
  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.vec.x = t.vec.x;
  419. orig.vec.y = t.vec.y;
  420. orig.angle = t.angle;
  421. orig.zoom = t.zoom;
  422. // Generate a one-sided moving exponential average
  423. deshake->avg.vec.x = alpha * t.vec.x + (1.0 - alpha) * deshake->avg.vec.x;
  424. deshake->avg.vec.y = alpha * t.vec.y + (1.0 - alpha) * deshake->avg.vec.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.vec.x -= deshake->avg.vec.x;
  430. t.vec.y -= deshake->avg.vec.y;
  431. t.angle -= deshake->avg.angle;
  432. t.zoom -= deshake->avg.zoom;
  433. // Invert the motion to undo it
  434. t.vec.x *= -1;
  435. t.vec.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.vec.x, deshake->avg.vec.x, t.vec.x, orig.vec.y, deshake->avg.vec.y, t.vec.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.vec.x += deshake->last.vec.x;
  445. t.vec.y += deshake->last.vec.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.vec.x *= 0.9;
  450. t.vec.y *= 0.9;
  451. t.angle *= 0.9;
  452. // Store the last absolute motion information
  453. deshake->last.vec.x = t.vec.x;
  454. deshake->last.vec.y = t.vec.y;
  455. deshake->last.angle = t.angle;
  456. deshake->last.zoom = t.zoom;
  457. // Generate a luma transformation matrix
  458. avfilter_get_matrix(t.vec.x, t.vec.y, t.angle, 1.0 + t.zoom / 100.0, matrix_y);
  459. // Generate a chroma transformation matrix
  460. avfilter_get_matrix(t.vec.x / (link->w / chroma_width), t.vec.y / (link->h / chroma_height), t.angle, 1.0 + t.zoom / 100.0, matrix_uv);
  461. // Transform the luma and chroma planes
  462. ret = deshake->transform(link->dst, link->w, link->h, chroma_width, chroma_height,
  463. matrix_y, matrix_uv, INTERPOLATE_BILINEAR, deshake->edge, in, out);
  464. // Cleanup the old reference frame
  465. av_frame_free(&deshake->ref);
  466. if (ret < 0)
  467. return ret;
  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 ff_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. };