vf_vidstabtransform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 TransformContext {
  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_BOOL, {.i64 = 0}, 0, 1, FLAGS},
  85. {"debug", "enable debug mode and writer global motions information to file", OFFSET(debug),
  86. AV_OPT_TYPE_BOOL, {.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. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  115. if (!fmts_list)
  116. return AVERROR(ENOMEM);
  117. return ff_set_common_formats(ctx, fmts_list);
  118. }
  119. static int config_input(AVFilterLink *inlink)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. TransformContext *tc = ctx->priv;
  123. FILE *f;
  124. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  125. int is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
  126. VSTransformData *td = &(tc->td);
  127. VSFrameInfo fi_src;
  128. VSFrameInfo fi_dest;
  129. if (!vsFrameInfoInit(&fi_src, inlink->w, inlink->h,
  130. ff_av2vs_pixfmt(ctx, inlink->format)) ||
  131. !vsFrameInfoInit(&fi_dest, inlink->w, inlink->h,
  132. ff_av2vs_pixfmt(ctx, inlink->format))) {
  133. av_log(ctx, AV_LOG_ERROR, "unknown pixel format: %i (%s)",
  134. inlink->format, desc->name);
  135. return AVERROR(EINVAL);
  136. }
  137. if ((!is_planar && fi_src.bytesPerPixel != av_get_bits_per_pixel(desc)/8) ||
  138. fi_src.log2ChromaW != desc->log2_chroma_w ||
  139. fi_src.log2ChromaH != desc->log2_chroma_h) {
  140. av_log(ctx, AV_LOG_ERROR, "pixel-format error: bpp %i<>%i ",
  141. fi_src.bytesPerPixel, av_get_bits_per_pixel(desc)/8);
  142. av_log(ctx, AV_LOG_ERROR, "chroma_subsampl: w: %i<>%i h: %i<>%i\n",
  143. fi_src.log2ChromaW, desc->log2_chroma_w,
  144. fi_src.log2ChromaH, desc->log2_chroma_h);
  145. return AVERROR(EINVAL);
  146. }
  147. // set values that are not initializes by the options
  148. tc->conf.modName = "vidstabtransform";
  149. tc->conf.verbose = 1 + tc->debug;
  150. if (tc->tripod) {
  151. av_log(ctx, AV_LOG_INFO, "Virtual tripod mode: relative=0, smoothing=0\n");
  152. tc->conf.relative = 0;
  153. tc->conf.smoothing = 0;
  154. }
  155. tc->conf.simpleMotionCalculation = 0;
  156. tc->conf.storeTransforms = tc->debug;
  157. tc->conf.smoothZoom = 0;
  158. if (vsTransformDataInit(td, &tc->conf, &fi_src, &fi_dest) != VS_OK) {
  159. av_log(ctx, AV_LOG_ERROR, "initialization of vid.stab transform failed, please report a BUG\n");
  160. return AVERROR(EINVAL);
  161. }
  162. vsTransformGetConfig(&tc->conf, td);
  163. av_log(ctx, AV_LOG_INFO, "Video transformation/stabilization settings (pass 2/2):\n");
  164. av_log(ctx, AV_LOG_INFO, " input = %s\n", tc->input);
  165. av_log(ctx, AV_LOG_INFO, " smoothing = %d\n", tc->conf.smoothing);
  166. av_log(ctx, AV_LOG_INFO, " optalgo = %s\n",
  167. tc->conf.camPathAlgo == VSOptimalL1 ? "opt" :
  168. (tc->conf.camPathAlgo == VSGaussian ? "gauss" : "avg"));
  169. av_log(ctx, AV_LOG_INFO, " maxshift = %d\n", tc->conf.maxShift);
  170. av_log(ctx, AV_LOG_INFO, " maxangle = %f\n", tc->conf.maxAngle);
  171. av_log(ctx, AV_LOG_INFO, " crop = %s\n", tc->conf.crop ? "Black" : "Keep");
  172. av_log(ctx, AV_LOG_INFO, " relative = %s\n", tc->conf.relative ? "True": "False");
  173. av_log(ctx, AV_LOG_INFO, " invert = %s\n", tc->conf.invert ? "True" : "False");
  174. av_log(ctx, AV_LOG_INFO, " zoom = %f\n", tc->conf.zoom);
  175. av_log(ctx, AV_LOG_INFO, " optzoom = %s\n",
  176. tc->conf.optZoom == 1 ? "Static (1)" : (tc->conf.optZoom == 2 ? "Dynamic (2)" : "Off (0)"));
  177. if (tc->conf.optZoom == 2)
  178. av_log(ctx, AV_LOG_INFO, " zoomspeed = %g\n", tc->conf.zoomSpeed);
  179. av_log(ctx, AV_LOG_INFO, " interpol = %s\n", getInterpolationTypeName(tc->conf.interpolType));
  180. f = fopen(tc->input, "r");
  181. if (!f) {
  182. int ret = AVERROR(errno);
  183. av_log(ctx, AV_LOG_ERROR, "cannot open input file %s\n", tc->input);
  184. return ret;
  185. } else {
  186. VSManyLocalMotions mlms;
  187. if (vsReadLocalMotionsFile(f, &mlms) == VS_OK) {
  188. // calculate the actual transforms from the local motions
  189. if (vsLocalmotions2Transforms(td, &mlms, &tc->trans) != VS_OK) {
  190. av_log(ctx, AV_LOG_ERROR, "calculating transformations failed\n");
  191. return AVERROR(EINVAL);
  192. }
  193. } else { // try to read old format
  194. if (!vsReadOldTransforms(td, f, &tc->trans)) { /* read input file */
  195. av_log(ctx, AV_LOG_ERROR, "error parsing input file %s\n", tc->input);
  196. return AVERROR(EINVAL);
  197. }
  198. }
  199. }
  200. fclose(f);
  201. if (vsPreprocessTransforms(td, &tc->trans) != VS_OK) {
  202. av_log(ctx, AV_LOG_ERROR, "error while preprocessing transforms\n");
  203. return AVERROR(EINVAL);
  204. }
  205. // TODO: add sharpening, so far the user needs to call the unsharp filter manually
  206. return 0;
  207. }
  208. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  209. {
  210. AVFilterContext *ctx = inlink->dst;
  211. TransformContext *tc = ctx->priv;
  212. VSTransformData* td = &(tc->td);
  213. AVFilterLink *outlink = inlink->dst->outputs[0];
  214. int direct = 0;
  215. AVFrame *out;
  216. VSFrame inframe;
  217. int plane;
  218. if (av_frame_is_writable(in)) {
  219. direct = 1;
  220. out = in;
  221. } else {
  222. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  223. if (!out) {
  224. av_frame_free(&in);
  225. return AVERROR(ENOMEM);
  226. }
  227. av_frame_copy_props(out, in);
  228. }
  229. for (plane = 0; plane < vsTransformGetSrcFrameInfo(td)->planes; plane++) {
  230. inframe.data[plane] = in->data[plane];
  231. inframe.linesize[plane] = in->linesize[plane];
  232. }
  233. if (direct) {
  234. vsTransformPrepare(td, &inframe, &inframe);
  235. } else { // separate frames
  236. VSFrame outframe;
  237. for (plane = 0; plane < vsTransformGetDestFrameInfo(td)->planes; plane++) {
  238. outframe.data[plane] = out->data[plane];
  239. outframe.linesize[plane] = out->linesize[plane];
  240. }
  241. vsTransformPrepare(td, &inframe, &outframe);
  242. }
  243. vsDoTransform(td, vsGetNextTransform(td, &tc->trans));
  244. vsTransformFinish(td);
  245. if (!direct)
  246. av_frame_free(&in);
  247. return ff_filter_frame(outlink, out);
  248. }
  249. static const AVFilterPad avfilter_vf_vidstabtransform_inputs[] = {
  250. {
  251. .name = "default",
  252. .type = AVMEDIA_TYPE_VIDEO,
  253. .filter_frame = filter_frame,
  254. .config_props = config_input,
  255. },
  256. { NULL }
  257. };
  258. static const AVFilterPad avfilter_vf_vidstabtransform_outputs[] = {
  259. {
  260. .name = "default",
  261. .type = AVMEDIA_TYPE_VIDEO,
  262. },
  263. { NULL }
  264. };
  265. AVFilter ff_vf_vidstabtransform = {
  266. .name = "vidstabtransform",
  267. .description = NULL_IF_CONFIG_SMALL("Transform the frames, "
  268. "pass 2 of 2 for stabilization "
  269. "(see vidstabdetect for pass 1)."),
  270. .priv_size = sizeof(TransformContext),
  271. .init = init,
  272. .uninit = uninit,
  273. .query_formats = query_formats,
  274. .inputs = avfilter_vf_vidstabtransform_inputs,
  275. .outputs = avfilter_vf_vidstabtransform_outputs,
  276. .priv_class = &vidstabtransform_class,
  277. };