vf_vidstabtransform.c 13 KB

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