vf_vidstabtransform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #define DEFAULT_INPUT_NAME "transforms.trf"
  21. #include <vid.stab/libvidstab.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "vidstabutils.h"
  28. typedef struct {
  29. const AVClass *class;
  30. VSTransformData td;
  31. VSTransformConfig conf;
  32. VSTransformations trans; // transformations
  33. char *input; // name of transform file
  34. int tripod;
  35. int debug;
  36. } TransformContext;
  37. #define OFFSET(x) offsetof(TransformContext, x)
  38. #define OFFSETC(x) (offsetof(TransformContext, conf)+offsetof(VSTransformConfig, x))
  39. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  40. static const AVOption vidstabtransform_options[] = {
  41. {"input", "set path to the file storing the transforms", OFFSET(input),
  42. AV_OPT_TYPE_STRING, {.str = DEFAULT_INPUT_NAME}, .flags = FLAGS },
  43. {"smoothing", "set number of frames*2 + 1 used for lowpass filtering", OFFSETC(smoothing),
  44. AV_OPT_TYPE_INT, {.i64 = 15}, 0, 1000, FLAGS},
  45. {"optalgo", "set camera path optimization algo", OFFSETC(camPathAlgo),
  46. AV_OPT_TYPE_INT, {.i64 = VSOptimalL1}, VSOptimalL1, VSAvg, FLAGS, "optalgo"},
  47. { "opt", "global optimization", 0, // from version 1.0 on
  48. AV_OPT_TYPE_CONST, {.i64 = VSOptimalL1 }, 0, 0, FLAGS, "optalgo"},
  49. { "gauss", "gaussian kernel", 0,
  50. AV_OPT_TYPE_CONST, {.i64 = VSGaussian }, 0, 0, FLAGS, "optalgo"},
  51. { "avg", "simple averaging on motion", 0,
  52. AV_OPT_TYPE_CONST, {.i64 = VSAvg }, 0, 0, FLAGS, "optalgo"},
  53. {"maxshift", "set maximal number of pixels to translate image", OFFSETC(maxShift),
  54. AV_OPT_TYPE_INT, {.i64 = -1}, -1, 500, FLAGS},
  55. {"maxangle", "set maximal angle in rad to rotate image", OFFSETC(maxAngle),
  56. AV_OPT_TYPE_DOUBLE, {.dbl = -1.0}, -1.0, 3.14, FLAGS},
  57. {"crop", "set cropping mode", OFFSETC(crop),
  58. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, "crop"},
  59. { "keep", "keep border", 0,
  60. AV_OPT_TYPE_CONST, {.i64 = VSKeepBorder }, 0, 0, FLAGS, "crop"},
  61. { "black", "black border", 0,
  62. AV_OPT_TYPE_CONST, {.i64 = VSCropBorder }, 0, 0, FLAGS, "crop"},
  63. {"invert", "invert transforms", OFFSETC(invert),
  64. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
  65. {"relative", "consider transforms as relative", OFFSETC(relative),
  66. AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS},
  67. {"zoom", "set percentage to zoom (>0: zoom in, <0: zoom out", OFFSETC(zoom),
  68. AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -100, 100, FLAGS},
  69. {"optzoom", "set optimal zoom (0: nothing, 1: optimal static zoom, 2: optimal dynamic zoom)", OFFSETC(optZoom),
  70. AV_OPT_TYPE_INT, {.i64 = 1}, 0, 2, FLAGS},
  71. {"zoomspeed", "for adative zoom: percent to zoom maximally each frame", OFFSETC(zoomSpeed),
  72. AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0, 5, FLAGS},
  73. {"interpol", "set type of interpolation", OFFSETC(interpolType),
  74. AV_OPT_TYPE_INT, {.i64 = 2}, 0, 3, FLAGS, "interpol"},
  75. { "no", "no interpolation", 0,
  76. AV_OPT_TYPE_CONST, {.i64 = VS_Zero }, 0, 0, FLAGS, "interpol"},
  77. { "linear", "linear (horizontal)", 0,
  78. AV_OPT_TYPE_CONST, {.i64 = VS_Linear }, 0, 0, FLAGS, "interpol"},
  79. { "bilinear","bi-linear", 0,
  80. AV_OPT_TYPE_CONST, {.i64 = VS_BiLinear},0, 0, FLAGS, "interpol"},
  81. { "bicubic", "bi-cubic", 0,
  82. AV_OPT_TYPE_CONST, {.i64 = VS_BiCubic },0, 0, FLAGS, "interpol"},
  83. {"tripod", "enable virtual tripod mode (same as relative=0:smoothing=0)", OFFSET(tripod),
  84. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
  85. {"debug", "enable debug mode and writer global motions information to file", OFFSET(debug),
  86. AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
  87. {NULL}
  88. };
  89. AVFILTER_DEFINE_CLASS(vidstabtransform);
  90. static av_cold int init(AVFilterContext *ctx)
  91. {
  92. TransformContext *tc = ctx->priv;
  93. ff_vs_init();
  94. tc->class = &vidstabtransform_class;
  95. av_log(ctx, AV_LOG_VERBOSE, "vidstabtransform filter: init %s\n", LIBVIDSTAB_VERSION);
  96. return 0;
  97. }
  98. static av_cold void uninit(AVFilterContext *ctx)
  99. {
  100. TransformContext *tc = ctx->priv;
  101. vsTransformDataCleanup(&tc->td);
  102. vsTransformationsCleanup(&tc->trans);
  103. }
  104. static int query_formats(AVFilterContext *ctx)
  105. {
  106. // If you add something here also add it in vidstabutils.c
  107. static const enum AVPixelFormat pix_fmts[] = {
  108. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  109. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  110. AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
  111. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
  112. AV_PIX_FMT_NONE
  113. };
  114. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  115. return 0;
  116. }
  117. static int config_input(AVFilterLink *inlink)
  118. {
  119. AVFilterContext *ctx = inlink->dst;
  120. TransformContext *tc = ctx->priv;
  121. FILE *f;
  122. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  123. VSTransformData *td = &(tc->td);
  124. VSFrameInfo fi_src;
  125. VSFrameInfo fi_dest;
  126. if (!vsFrameInfoInit(&fi_src, inlink->w, inlink->h,
  127. ff_av2vs_pixfmt(ctx, inlink->format)) ||
  128. !vsFrameInfoInit(&fi_dest, inlink->w, inlink->h,
  129. ff_av2vs_pixfmt(ctx, inlink->format))) {
  130. av_log(ctx, AV_LOG_ERROR, "unknown pixel format: %i (%s)",
  131. inlink->format, desc->name);
  132. return AVERROR(EINVAL);
  133. }
  134. if (fi_src.bytesPerPixel != av_get_bits_per_pixel(desc)/8 ||
  135. fi_src.log2ChromaW != desc->log2_chroma_w ||
  136. fi_src.log2ChromaH != desc->log2_chroma_h) {
  137. av_log(ctx, AV_LOG_ERROR, "pixel-format error: bpp %i<>%i ",
  138. fi_src.bytesPerPixel, av_get_bits_per_pixel(desc)/8);
  139. av_log(ctx, AV_LOG_ERROR, "chroma_subsampl: w: %i<>%i h: %i<>%i\n",
  140. fi_src.log2ChromaW, desc->log2_chroma_w,
  141. fi_src.log2ChromaH, desc->log2_chroma_h);
  142. return AVERROR(EINVAL);
  143. }
  144. // set values that are not initializes by the options
  145. tc->conf.modName = "vidstabtransform";
  146. tc->conf.verbose = 1 + tc->debug;
  147. if (tc->tripod) {
  148. av_log(ctx, AV_LOG_INFO, "Virtual tripod mode: relative=0, smoothing=0\n");
  149. tc->conf.relative = 0;
  150. tc->conf.smoothing = 0;
  151. }
  152. tc->conf.simpleMotionCalculation = 0;
  153. tc->conf.storeTransforms = tc->debug;
  154. tc->conf.smoothZoom = 0;
  155. if (vsTransformDataInit(td, &tc->conf, &fi_src, &fi_dest) != VS_OK) {
  156. av_log(ctx, AV_LOG_ERROR, "initialization of vid.stab transform failed, please report a BUG\n");
  157. return AVERROR(EINVAL);
  158. }
  159. vsTransformGetConfig(&tc->conf, td);
  160. av_log(ctx, AV_LOG_INFO, "Video transformation/stabilization settings (pass 2/2):\n");
  161. av_log(ctx, AV_LOG_INFO, " input = %s\n", tc->input);
  162. av_log(ctx, AV_LOG_INFO, " smoothing = %d\n", tc->conf.smoothing);
  163. av_log(ctx, AV_LOG_INFO, " optalgo = %s\n",
  164. tc->conf.camPathAlgo == VSOptimalL1 ? "opt" :
  165. (tc->conf.camPathAlgo == VSGaussian ? "gauss" : "avg"));
  166. av_log(ctx, AV_LOG_INFO, " maxshift = %d\n", tc->conf.maxShift);
  167. av_log(ctx, AV_LOG_INFO, " maxangle = %f\n", tc->conf.maxAngle);
  168. av_log(ctx, AV_LOG_INFO, " crop = %s\n", tc->conf.crop ? "Black" : "Keep");
  169. av_log(ctx, AV_LOG_INFO, " relative = %s\n", tc->conf.relative ? "True": "False");
  170. av_log(ctx, AV_LOG_INFO, " invert = %s\n", tc->conf.invert ? "True" : "False");
  171. av_log(ctx, AV_LOG_INFO, " zoom = %f\n", tc->conf.zoom);
  172. av_log(ctx, AV_LOG_INFO, " optzoom = %s\n",
  173. tc->conf.optZoom == 1 ? "Static (1)" : (tc->conf.optZoom == 2 ? "Dynamic (2)" : "Off (0)"));
  174. if (tc->conf.optZoom == 2)
  175. av_log(ctx, AV_LOG_INFO, " zoomspeed = %g\n", tc->conf.zoomSpeed);
  176. av_log(ctx, AV_LOG_INFO, " interpol = %s\n", getInterpolationTypeName(tc->conf.interpolType));
  177. f = fopen(tc->input, "r");
  178. if (!f) {
  179. av_log(ctx, AV_LOG_ERROR, "cannot open input file %s\n", tc->input);
  180. return AVERROR(errno);
  181. } else {
  182. VSManyLocalMotions mlms;
  183. if (vsReadLocalMotionsFile(f, &mlms) == VS_OK) {
  184. // calculate the actual transforms from the local motions
  185. if (vsLocalmotions2Transforms(td, &mlms, &tc->trans) != VS_OK) {
  186. av_log(ctx, AV_LOG_ERROR, "calculating transformations failed\n");
  187. return AVERROR(EINVAL);
  188. }
  189. } else { // try to read old format
  190. if (!vsReadOldTransforms(td, f, &tc->trans)) { /* read input file */
  191. av_log(ctx, AV_LOG_ERROR, "error parsing input file %s\n", tc->input);
  192. return AVERROR(EINVAL);
  193. }
  194. }
  195. }
  196. fclose(f);
  197. if (vsPreprocessTransforms(td, &tc->trans) != VS_OK) {
  198. av_log(ctx, AV_LOG_ERROR, "error while preprocessing transforms\n");
  199. return AVERROR(EINVAL);
  200. }
  201. // TODO: add sharpening, so far the user needs to call the unsharp filter manually
  202. return 0;
  203. }
  204. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  205. {
  206. AVFilterContext *ctx = inlink->dst;
  207. TransformContext *tc = ctx->priv;
  208. VSTransformData* td = &(tc->td);
  209. AVFilterLink *outlink = inlink->dst->outputs[0];
  210. int direct = 0;
  211. AVFrame *out;
  212. VSFrame inframe;
  213. int plane;
  214. if (av_frame_is_writable(in)) {
  215. direct = 1;
  216. out = in;
  217. } else {
  218. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  219. if (!out) {
  220. av_frame_free(&in);
  221. return AVERROR(ENOMEM);
  222. }
  223. av_frame_copy_props(out, in);
  224. }
  225. for (plane = 0; plane < vsTransformGetSrcFrameInfo(td)->planes; plane++) {
  226. inframe.data[plane] = in->data[plane];
  227. inframe.linesize[plane] = in->linesize[plane];
  228. }
  229. if (direct) {
  230. vsTransformPrepare(td, &inframe, &inframe);
  231. } else { // separate frames
  232. VSFrame outframe;
  233. for (plane = 0; plane < vsTransformGetDestFrameInfo(td)->planes; plane++) {
  234. outframe.data[plane] = out->data[plane];
  235. outframe.linesize[plane] = out->linesize[plane];
  236. }
  237. vsTransformPrepare(td, &inframe, &outframe);
  238. }
  239. vsDoTransform(td, vsGetNextTransform(td, &tc->trans));
  240. vsTransformFinish(td);
  241. if (!direct)
  242. av_frame_free(&in);
  243. return ff_filter_frame(outlink, out);
  244. }
  245. static const AVFilterPad avfilter_vf_vidstabtransform_inputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. .filter_frame = filter_frame,
  250. .config_props = config_input,
  251. },
  252. { NULL }
  253. };
  254. static const AVFilterPad avfilter_vf_vidstabtransform_outputs[] = {
  255. {
  256. .name = "default",
  257. .type = AVMEDIA_TYPE_VIDEO,
  258. },
  259. { NULL }
  260. };
  261. AVFilter ff_vf_vidstabtransform = {
  262. .name = "vidstabtransform",
  263. .description = NULL_IF_CONFIG_SMALL("Transform the frames, "
  264. "pass 2 of 2 for stabilization "
  265. "(see vidstabdetect for pass 1)."),
  266. .priv_size = sizeof(TransformContext),
  267. .init = init,
  268. .uninit = uninit,
  269. .query_formats = query_formats,
  270. .inputs = avfilter_vf_vidstabtransform_inputs,
  271. .outputs = avfilter_vf_vidstabtransform_outputs,
  272. .priv_class = &vidstabtransform_class,
  273. };