FFmpeg
Loading...
Searching...
No Matches
vf_vidstabtransform.c
Go to the documentation of this file.
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
21#define DEFAULT_INPUT_NAME "transforms.trf"
22
23#include <vid.stab/libvidstab.h>
24
25#include "libavutil/common.h"
26#include "libavutil/file_open.h"
27#include "libavutil/opt.h"
28#include "libavutil/pixdesc.h"
29#include "avfilter.h"
30#include "filters.h"
31#include "video.h"
32
33#include "vidstabutils.h"
34
35typedef struct TransformContext {
36 const AVClass *class;
37
38 VSTransformData td;
39 VSTransformConfig conf;
40
41 VSTransformations trans; // transformations
42 char *input; // name of transform file
43 int tripod;
44 int debug;
46
47#define OFFSET(x) offsetof(TransformContext, x)
48#define OFFSETC(x) (offsetof(TransformContext, conf)+offsetof(VSTransformConfig, x))
49#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
50
52 {"input", "set path to the file storing the transforms", OFFSET(input),
53 AV_OPT_TYPE_STRING, {.str = DEFAULT_INPUT_NAME}, .flags = FLAGS },
54 {"smoothing", "set number of frames*2 + 1 used for lowpass filtering", OFFSETC(smoothing),
55 AV_OPT_TYPE_INT, {.i64 = 15}, 0, 1000, FLAGS},
56
57 {"optalgo", "set camera path optimization algo", OFFSETC(camPathAlgo),
58 AV_OPT_TYPE_INT, {.i64 = VSOptimalL1}, VSOptimalL1, VSAvg, FLAGS, .unit = "optalgo"},
59 { "opt", "global optimization", 0, // from version 1.0 on
60 AV_OPT_TYPE_CONST, {.i64 = VSOptimalL1 }, 0, 0, FLAGS, .unit = "optalgo"},
61 { "gauss", "gaussian kernel", 0,
62 AV_OPT_TYPE_CONST, {.i64 = VSGaussian }, 0, 0, FLAGS, .unit = "optalgo"},
63 { "avg", "simple averaging on motion", 0,
64 AV_OPT_TYPE_CONST, {.i64 = VSAvg }, 0, 0, FLAGS, .unit = "optalgo"},
65
66 {"maxshift", "set maximal number of pixels to translate image", OFFSETC(maxShift),
67 AV_OPT_TYPE_INT, {.i64 = -1}, -1, 500, FLAGS},
68 {"maxangle", "set maximal angle in rad to rotate image", OFFSETC(maxAngle),
69 AV_OPT_TYPE_DOUBLE, {.dbl = -1.0}, -1.0, 3.14, FLAGS},
70
71 {"crop", "set cropping mode", OFFSETC(crop),
72 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, .unit = "crop"},
73 { "keep", "keep border", 0,
74 AV_OPT_TYPE_CONST, {.i64 = VSKeepBorder }, 0, 0, FLAGS, .unit = "crop"},
75 { "black", "black border", 0,
76 AV_OPT_TYPE_CONST, {.i64 = VSCropBorder }, 0, 0, FLAGS, .unit = "crop"},
77
78 {"invert", "invert transforms", OFFSETC(invert),
79 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
80 {"relative", "consider transforms as relative", OFFSETC(relative),
81 AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS},
82 {"zoom", "set percentage to zoom (>0: zoom in, <0: zoom out", OFFSETC(zoom),
83 AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -100, 100, FLAGS},
84 {"optzoom", "set optimal zoom (0: nothing, 1: optimal static zoom, 2: optimal dynamic zoom)", OFFSETC(optZoom),
85 AV_OPT_TYPE_INT, {.i64 = 1}, 0, 2, FLAGS},
86 {"zoomspeed", "for adative zoom: percent to zoom maximally each frame", OFFSETC(zoomSpeed),
87 AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0, 5, FLAGS},
88
89 {"interpol", "set type of interpolation", OFFSETC(interpolType),
90 AV_OPT_TYPE_INT, {.i64 = 2}, 0, 3, FLAGS, .unit = "interpol"},
91 { "no", "no interpolation", 0,
92 AV_OPT_TYPE_CONST, {.i64 = VS_Zero }, 0, 0, FLAGS, .unit = "interpol"},
93 { "linear", "linear (horizontal)", 0,
94 AV_OPT_TYPE_CONST, {.i64 = VS_Linear }, 0, 0, FLAGS, .unit = "interpol"},
95 { "bilinear","bi-linear", 0,
96 AV_OPT_TYPE_CONST, {.i64 = VS_BiLinear},0, 0, FLAGS, .unit = "interpol"},
97 { "bicubic", "bi-cubic", 0,
98 AV_OPT_TYPE_CONST, {.i64 = VS_BiCubic },0, 0, FLAGS, .unit = "interpol"},
99
100 {"tripod", "enable virtual tripod mode (same as relative=0:smoothing=0)", OFFSET(tripod),
101 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
102 {"debug", "enable debug mode and writer global motions information to file", OFFSET(debug),
103 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
104 {NULL}
105};
106
107AVFILTER_DEFINE_CLASS(vidstabtransform);
108
110{
111 TransformContext *tc = ctx->priv;
112 ff_vs_init();
113 tc->class = &vidstabtransform_class;
114 av_log(ctx, AV_LOG_VERBOSE, "vidstabtransform filter: init %s\n", LIBVIDSTAB_VERSION);
115 return 0;
116}
117
119{
120 TransformContext *tc = ctx->priv;
121
122 vsTransformDataCleanup(&tc->td);
123 vsTransformationsCleanup(&tc->trans);
124}
125
126static int config_input(AVFilterLink *inlink)
127{
128 AVFilterContext *ctx = inlink->dst;
129 TransformContext *tc = ctx->priv;
130 FILE *f;
131
133 int is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
134
135 VSTransformData *td = &(tc->td);
136
137 VSFrameInfo fi_src;
138 VSFrameInfo fi_dest;
139
140 if (!vsFrameInfoInit(&fi_src, inlink->w, inlink->h,
141 ff_av2vs_pixfmt(ctx, inlink->format)) ||
142 !vsFrameInfoInit(&fi_dest, inlink->w, inlink->h,
143 ff_av2vs_pixfmt(ctx, inlink->format))) {
144 av_log(ctx, AV_LOG_ERROR, "unknown pixel format: %i (%s)",
145 inlink->format, desc->name);
146 return AVERROR(EINVAL);
147 }
148
149 if ((!is_planar && fi_src.bytesPerPixel != av_get_bits_per_pixel(desc)/8) ||
150 fi_src.log2ChromaW != desc->log2_chroma_w ||
151 fi_src.log2ChromaH != desc->log2_chroma_h) {
152 av_log(ctx, AV_LOG_ERROR, "pixel-format error: bpp %i<>%i ",
153 fi_src.bytesPerPixel, av_get_bits_per_pixel(desc)/8);
154 av_log(ctx, AV_LOG_ERROR, "chroma_subsampl: w: %i<>%i h: %i<>%i\n",
155 fi_src.log2ChromaW, desc->log2_chroma_w,
156 fi_src.log2ChromaH, desc->log2_chroma_h);
157 return AVERROR(EINVAL);
158 }
159
160 // set values that are not initializes by the options
161 tc->conf.modName = "vidstabtransform";
162 tc->conf.verbose = 1 + tc->debug;
163 if (tc->tripod) {
164 av_log(ctx, AV_LOG_INFO, "Virtual tripod mode: relative=0, smoothing=0\n");
165 tc->conf.relative = 0;
166 tc->conf.smoothing = 0;
167 }
168 tc->conf.simpleMotionCalculation = 0;
169 tc->conf.storeTransforms = tc->debug;
170 tc->conf.smoothZoom = 0;
171
172 if (vsTransformDataInit(td, &tc->conf, &fi_src, &fi_dest) != VS_OK) {
173 av_log(ctx, AV_LOG_ERROR, "initialization of vid.stab transform failed, please report a BUG\n");
174 return AVERROR(EINVAL);
175 }
176
177 vsTransformGetConfig(&tc->conf, td);
178 av_log(ctx, AV_LOG_INFO, "Video transformation/stabilization settings (pass 2/2):\n");
179 av_log(ctx, AV_LOG_INFO, " input = %s\n", tc->input);
180 av_log(ctx, AV_LOG_INFO, " smoothing = %d\n", tc->conf.smoothing);
181 av_log(ctx, AV_LOG_INFO, " optalgo = %s\n",
182 tc->conf.camPathAlgo == VSOptimalL1 ? "opt" :
183 (tc->conf.camPathAlgo == VSGaussian ? "gauss" : "avg"));
184 av_log(ctx, AV_LOG_INFO, " maxshift = %d\n", tc->conf.maxShift);
185 av_log(ctx, AV_LOG_INFO, " maxangle = %f\n", tc->conf.maxAngle);
186 av_log(ctx, AV_LOG_INFO, " crop = %s\n", tc->conf.crop ? "Black" : "Keep");
187 av_log(ctx, AV_LOG_INFO, " relative = %s\n", tc->conf.relative ? "True": "False");
188 av_log(ctx, AV_LOG_INFO, " invert = %s\n", tc->conf.invert ? "True" : "False");
189 av_log(ctx, AV_LOG_INFO, " zoom = %f\n", tc->conf.zoom);
190 av_log(ctx, AV_LOG_INFO, " optzoom = %s\n",
191 tc->conf.optZoom == 1 ? "Static (1)" : (tc->conf.optZoom == 2 ? "Dynamic (2)" : "Off (0)"));
192 if (tc->conf.optZoom == 2)
193 av_log(ctx, AV_LOG_INFO, " zoomspeed = %g\n", tc->conf.zoomSpeed);
194 av_log(ctx, AV_LOG_INFO, " interpol = %s\n", getInterpolationTypeName(tc->conf.interpolType));
195
196 f = avpriv_fopen_utf8(tc->input, "rb");
197 if (!f) {
198 int ret = AVERROR(errno);
199 av_log(ctx, AV_LOG_ERROR, "cannot open input file %s\n", tc->input);
200 return ret;
201 } else {
202 VSManyLocalMotions mlms;
203 if (vsReadLocalMotionsFile(f, &mlms) == VS_OK) {
204 // calculate the actual transforms from the local motions
205 if (vsLocalmotions2Transforms(td, &mlms, &tc->trans) != VS_OK) {
206 av_log(ctx, AV_LOG_ERROR, "calculating transformations failed\n");
207 return AVERROR(EINVAL);
208 }
209 } else { // try to read old format
210 if (!vsReadOldTransforms(td, f, &tc->trans)) { /* read input file */
211 av_log(ctx, AV_LOG_ERROR, "error parsing input file %s\n", tc->input);
212 return AVERROR(EINVAL);
213 }
214 }
215 }
216 fclose(f);
217
218 if (vsPreprocessTransforms(td, &tc->trans) != VS_OK) {
219 av_log(ctx, AV_LOG_ERROR, "error while preprocessing transforms\n");
220 return AVERROR(EINVAL);
221 }
222
223 // TODO: add sharpening, so far the user needs to call the unsharp filter manually
224 return 0;
225}
226
227
228static int filter_frame(AVFilterLink *inlink, AVFrame *in)
229{
230 AVFilterContext *ctx = inlink->dst;
231 TransformContext *tc = ctx->priv;
232 VSTransformData* td = &(tc->td);
233
234 AVFilterLink *outlink = ctx->outputs[0];
235 VSFrame inframe;
236 int plane;
237
238 for (plane = 0; plane < vsTransformGetSrcFrameInfo(td)->planes; plane++) {
239 inframe.data[plane] = in->data[plane];
240 inframe.linesize[plane] = in->linesize[plane];
241 }
242 vsTransformPrepare(td, &inframe, &inframe);
243
244 vsDoTransform(td, vsGetNextTransform(td, &tc->trans));
245
246 vsTransformFinish(td);
247
248 return ff_filter_frame(outlink, in);
249}
250
252 {
253 .name = "default",
254 .type = AVMEDIA_TYPE_VIDEO,
255 .filter_frame = filter_frame,
256 .config_props = config_input,
257 /* libvidstab's vsTransformPrepare() takes different internal code paths
258 * for in-place (src == dest) vs. separate-buffer operation. The
259 * separate-buffer path stores a shallow copy of the source frame
260 * pointer in td->src without allocating internal memory. When a
261 * subsequent frame takes the in-place path, it skips allocation
262 * (td->src is non-null) and copies into the stale pointer,
263 * corrupting memory that the caller no longer owns.
264 * Whether a frame is writable depends on pipeline scheduling, so
265 * always ensure the frame is writable to consistently take the
266 * in-place path.
267 */
269 },
270};
271
273 .p.name = "vidstabtransform",
274 .p.description = NULL_IF_CONFIG_SMALL("Transform the frames, "
275 "pass 2 of 2 for stabilization "
276 "(see vidstabdetect for pass 1)."),
277 .p.priv_class = &vidstabtransform_class,
278 .priv_size = sizeof(TransformContext),
279 .init = init,
280 .uninit = uninit,
284};
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_vidstabtransform
static void invert(float *h, int n)
Definition asrc_sinc.c:186
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define f(width, name)
Definition cbs_vp8.c:236
#define FLAGS
Definition cmdutils.c:598
static IPT relative(const CmsCtx *ctx, IPT ipt)
Definition cms.c:544
common internal and external API header
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition filters.h:59
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition file_open.c:160
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
AVOptions.
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition pixdesc.c:3412
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition pixdesc.h:132
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
VSTransformations trans
VSTransformConfig conf
const AVClass * class
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
#define OFFSETC(x)
#define DEFAULT_INPUT_NAME
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static const AVOption vidstabtransform_options[]
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static const AVFilterPad avfilter_vf_vidstabtransform_inputs[]
static void zoom(float *u, float *v, float amount)
Definition vf_xfade.c:1695
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
VSPixelFormat ff_av2vs_pixfmt(AVFilterContext *ctx, enum AVPixelFormat pf)
convert AV's pixelformat to vid.stab pixelformat
enum AVPixelFormat ff_vidstab_pix_fmts[]
void ff_vs_init(void)
sets the memory allocation function and logging constants to av versions