FFmpeg
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 "internal.h"
31 
32 #include "vidstabutils.h"
33 
34 typedef struct TransformContext {
35  const AVClass *class;
36 
37  VSTransformData td;
38  VSTransformConfig conf;
39 
40  VSTransformations trans; // transformations
41  char *input; // name of transform file
42  int tripod;
43  int debug;
45 
46 #define OFFSET(x) offsetof(TransformContext, x)
47 #define OFFSETC(x) (offsetof(TransformContext, conf)+offsetof(VSTransformConfig, x))
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 
51  {"input", "set path to the file storing the transforms", OFFSET(input),
52  AV_OPT_TYPE_STRING, {.str = DEFAULT_INPUT_NAME}, .flags = FLAGS },
53  {"smoothing", "set number of frames*2 + 1 used for lowpass filtering", OFFSETC(smoothing),
54  AV_OPT_TYPE_INT, {.i64 = 15}, 0, 1000, FLAGS},
55 
56  {"optalgo", "set camera path optimization algo", OFFSETC(camPathAlgo),
57  AV_OPT_TYPE_INT, {.i64 = VSOptimalL1}, VSOptimalL1, VSAvg, FLAGS, "optalgo"},
58  { "opt", "global optimization", 0, // from version 1.0 on
59  AV_OPT_TYPE_CONST, {.i64 = VSOptimalL1 }, 0, 0, FLAGS, "optalgo"},
60  { "gauss", "gaussian kernel", 0,
61  AV_OPT_TYPE_CONST, {.i64 = VSGaussian }, 0, 0, FLAGS, "optalgo"},
62  { "avg", "simple averaging on motion", 0,
63  AV_OPT_TYPE_CONST, {.i64 = VSAvg }, 0, 0, FLAGS, "optalgo"},
64 
65  {"maxshift", "set maximal number of pixels to translate image", OFFSETC(maxShift),
66  AV_OPT_TYPE_INT, {.i64 = -1}, -1, 500, FLAGS},
67  {"maxangle", "set maximal angle in rad to rotate image", OFFSETC(maxAngle),
68  AV_OPT_TYPE_DOUBLE, {.dbl = -1.0}, -1.0, 3.14, FLAGS},
69 
70  {"crop", "set cropping mode", OFFSETC(crop),
71  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, "crop"},
72  { "keep", "keep border", 0,
73  AV_OPT_TYPE_CONST, {.i64 = VSKeepBorder }, 0, 0, FLAGS, "crop"},
74  { "black", "black border", 0,
75  AV_OPT_TYPE_CONST, {.i64 = VSCropBorder }, 0, 0, FLAGS, "crop"},
76 
77  {"invert", "invert transforms", OFFSETC(invert),
78  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
79  {"relative", "consider transforms as relative", OFFSETC(relative),
80  AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS},
81  {"zoom", "set percentage to zoom (>0: zoom in, <0: zoom out", OFFSETC(zoom),
82  AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -100, 100, FLAGS},
83  {"optzoom", "set optimal zoom (0: nothing, 1: optimal static zoom, 2: optimal dynamic zoom)", OFFSETC(optZoom),
84  AV_OPT_TYPE_INT, {.i64 = 1}, 0, 2, FLAGS},
85  {"zoomspeed", "for adative zoom: percent to zoom maximally each frame", OFFSETC(zoomSpeed),
86  AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0, 5, FLAGS},
87 
88  {"interpol", "set type of interpolation", OFFSETC(interpolType),
89  AV_OPT_TYPE_INT, {.i64 = 2}, 0, 3, FLAGS, "interpol"},
90  { "no", "no interpolation", 0,
91  AV_OPT_TYPE_CONST, {.i64 = VS_Zero }, 0, 0, FLAGS, "interpol"},
92  { "linear", "linear (horizontal)", 0,
93  AV_OPT_TYPE_CONST, {.i64 = VS_Linear }, 0, 0, FLAGS, "interpol"},
94  { "bilinear","bi-linear", 0,
95  AV_OPT_TYPE_CONST, {.i64 = VS_BiLinear},0, 0, FLAGS, "interpol"},
96  { "bicubic", "bi-cubic", 0,
97  AV_OPT_TYPE_CONST, {.i64 = VS_BiCubic },0, 0, FLAGS, "interpol"},
98 
99  {"tripod", "enable virtual tripod mode (same as relative=0:smoothing=0)", OFFSET(tripod),
100  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
101  {"debug", "enable debug mode and writer global motions information to file", OFFSET(debug),
102  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
103  {NULL}
104 };
105 
106 AVFILTER_DEFINE_CLASS(vidstabtransform);
107 
109 {
110  TransformContext *tc = ctx->priv;
111  ff_vs_init();
112  tc->class = &vidstabtransform_class;
113  av_log(ctx, AV_LOG_VERBOSE, "vidstabtransform filter: init %s\n", LIBVIDSTAB_VERSION);
114  return 0;
115 }
116 
118 {
119  TransformContext *tc = ctx->priv;
120 
121  vsTransformDataCleanup(&tc->td);
122  vsTransformationsCleanup(&tc->trans);
123 }
124 
126 {
127  AVFilterContext *ctx = inlink->dst;
128  TransformContext *tc = ctx->priv;
129  FILE *f;
130 
132  int is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
133 
134  VSTransformData *td = &(tc->td);
135 
136  VSFrameInfo fi_src;
137  VSFrameInfo fi_dest;
138 
139  if (!vsFrameInfoInit(&fi_src, inlink->w, inlink->h,
140  ff_av2vs_pixfmt(ctx, inlink->format)) ||
141  !vsFrameInfoInit(&fi_dest, inlink->w, inlink->h,
142  ff_av2vs_pixfmt(ctx, inlink->format))) {
143  av_log(ctx, AV_LOG_ERROR, "unknown pixel format: %i (%s)",
144  inlink->format, desc->name);
145  return AVERROR(EINVAL);
146  }
147 
148  if ((!is_planar && fi_src.bytesPerPixel != av_get_bits_per_pixel(desc)/8) ||
149  fi_src.log2ChromaW != desc->log2_chroma_w ||
150  fi_src.log2ChromaH != desc->log2_chroma_h) {
151  av_log(ctx, AV_LOG_ERROR, "pixel-format error: bpp %i<>%i ",
152  fi_src.bytesPerPixel, av_get_bits_per_pixel(desc)/8);
153  av_log(ctx, AV_LOG_ERROR, "chroma_subsampl: w: %i<>%i h: %i<>%i\n",
154  fi_src.log2ChromaW, desc->log2_chroma_w,
155  fi_src.log2ChromaH, desc->log2_chroma_h);
156  return AVERROR(EINVAL);
157  }
158 
159  // set values that are not initializes by the options
160  tc->conf.modName = "vidstabtransform";
161  tc->conf.verbose = 1 + tc->debug;
162  if (tc->tripod) {
163  av_log(ctx, AV_LOG_INFO, "Virtual tripod mode: relative=0, smoothing=0\n");
164  tc->conf.relative = 0;
165  tc->conf.smoothing = 0;
166  }
167  tc->conf.simpleMotionCalculation = 0;
168  tc->conf.storeTransforms = tc->debug;
169  tc->conf.smoothZoom = 0;
170 
171  if (vsTransformDataInit(td, &tc->conf, &fi_src, &fi_dest) != VS_OK) {
172  av_log(ctx, AV_LOG_ERROR, "initialization of vid.stab transform failed, please report a BUG\n");
173  return AVERROR(EINVAL);
174  }
175 
176  vsTransformGetConfig(&tc->conf, td);
177  av_log(ctx, AV_LOG_INFO, "Video transformation/stabilization settings (pass 2/2):\n");
178  av_log(ctx, AV_LOG_INFO, " input = %s\n", tc->input);
179  av_log(ctx, AV_LOG_INFO, " smoothing = %d\n", tc->conf.smoothing);
180  av_log(ctx, AV_LOG_INFO, " optalgo = %s\n",
181  tc->conf.camPathAlgo == VSOptimalL1 ? "opt" :
182  (tc->conf.camPathAlgo == VSGaussian ? "gauss" : "avg"));
183  av_log(ctx, AV_LOG_INFO, " maxshift = %d\n", tc->conf.maxShift);
184  av_log(ctx, AV_LOG_INFO, " maxangle = %f\n", tc->conf.maxAngle);
185  av_log(ctx, AV_LOG_INFO, " crop = %s\n", tc->conf.crop ? "Black" : "Keep");
186  av_log(ctx, AV_LOG_INFO, " relative = %s\n", tc->conf.relative ? "True": "False");
187  av_log(ctx, AV_LOG_INFO, " invert = %s\n", tc->conf.invert ? "True" : "False");
188  av_log(ctx, AV_LOG_INFO, " zoom = %f\n", tc->conf.zoom);
189  av_log(ctx, AV_LOG_INFO, " optzoom = %s\n",
190  tc->conf.optZoom == 1 ? "Static (1)" : (tc->conf.optZoom == 2 ? "Dynamic (2)" : "Off (0)"));
191  if (tc->conf.optZoom == 2)
192  av_log(ctx, AV_LOG_INFO, " zoomspeed = %g\n", tc->conf.zoomSpeed);
193  av_log(ctx, AV_LOG_INFO, " interpol = %s\n", getInterpolationTypeName(tc->conf.interpolType));
194 
195  f = avpriv_fopen_utf8(tc->input, "r");
196  if (!f) {
197  int ret = AVERROR(errno);
198  av_log(ctx, AV_LOG_ERROR, "cannot open input file %s\n", tc->input);
199  return ret;
200  } else {
201  VSManyLocalMotions mlms;
202  if (vsReadLocalMotionsFile(f, &mlms) == VS_OK) {
203  // calculate the actual transforms from the local motions
204  if (vsLocalmotions2Transforms(td, &mlms, &tc->trans) != VS_OK) {
205  av_log(ctx, AV_LOG_ERROR, "calculating transformations failed\n");
206  return AVERROR(EINVAL);
207  }
208  } else { // try to read old format
209  if (!vsReadOldTransforms(td, f, &tc->trans)) { /* read input file */
210  av_log(ctx, AV_LOG_ERROR, "error parsing input file %s\n", tc->input);
211  return AVERROR(EINVAL);
212  }
213  }
214  }
215  fclose(f);
216 
217  if (vsPreprocessTransforms(td, &tc->trans) != VS_OK) {
218  av_log(ctx, AV_LOG_ERROR, "error while preprocessing transforms\n");
219  return AVERROR(EINVAL);
220  }
221 
222  // TODO: add sharpening, so far the user needs to call the unsharp filter manually
223  return 0;
224 }
225 
226 
228 {
229  AVFilterContext *ctx = inlink->dst;
230  TransformContext *tc = ctx->priv;
231  VSTransformData* td = &(tc->td);
232 
233  AVFilterLink *outlink = inlink->dst->outputs[0];
234  int direct = 0;
235  AVFrame *out;
236  VSFrame inframe;
237  int plane;
238 
239  if (av_frame_is_writable(in)) {
240  direct = 1;
241  out = in;
242  } else {
243  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
244  if (!out) {
245  av_frame_free(&in);
246  return AVERROR(ENOMEM);
247  }
249  }
250 
251  for (plane = 0; plane < vsTransformGetSrcFrameInfo(td)->planes; plane++) {
252  inframe.data[plane] = in->data[plane];
253  inframe.linesize[plane] = in->linesize[plane];
254  }
255  if (direct) {
256  vsTransformPrepare(td, &inframe, &inframe);
257  } else { // separate frames
258  VSFrame outframe;
259  for (plane = 0; plane < vsTransformGetDestFrameInfo(td)->planes; plane++) {
260  outframe.data[plane] = out->data[plane];
261  outframe.linesize[plane] = out->linesize[plane];
262  }
263  vsTransformPrepare(td, &inframe, &outframe);
264  }
265 
266  vsDoTransform(td, vsGetNextTransform(td, &tc->trans));
267 
268  vsTransformFinish(td);
269 
270  if (!direct)
271  av_frame_free(&in);
272 
273  return ff_filter_frame(outlink, out);
274 }
275 
277  {
278  .name = "default",
279  .type = AVMEDIA_TYPE_VIDEO,
280  .filter_frame = filter_frame,
281  .config_props = config_input,
282  },
283 };
284 
286  {
287  .name = "default",
288  .type = AVMEDIA_TYPE_VIDEO,
289  },
290 };
291 
293  .name = "vidstabtransform",
294  .description = NULL_IF_CONFIG_SMALL("Transform the frames, "
295  "pass 2 of 2 for stabilization "
296  "(see vidstabdetect for pass 1)."),
297  .priv_size = sizeof(TransformContext),
298  .init = init,
299  .uninit = uninit,
303  .priv_class = &vidstabtransform_class,
304 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
td
#define td
Definition: regdef.h:70
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_vidstabtransform.c:117
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
vidstabutils.h
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2888
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:174
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
zoom
static void zoom(float *u, float *v, float amount)
Definition: vf_xfade.c:1633
OFFSETC
#define OFFSETC(x)
Definition: vf_vidstabtransform.c:47
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_vidstabtransform.c:125
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
pixdesc.h
AVOption
AVOption.
Definition: opt.h:251
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
TransformContext
Definition: vf_vidstabtransform.c:34
av_get_bits_per_pixel
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:2840
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(vidstabtransform)
TransformContext::input
char * input
Definition: vf_vidstabtransform.c:41
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
avfilter_vf_vidstabtransform_inputs
static const AVFilterPad avfilter_vf_vidstabtransform_inputs[]
Definition: vf_vidstabtransform.c:276
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_vidstabtransform.c:108
DEFAULT_INPUT_NAME
#define DEFAULT_INPUT_NAME
Definition: vf_vidstabtransform.c:21
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_vidstabtransform.c:227
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
avfilter_vf_vidstabtransform_outputs
static const AVFilterPad avfilter_vf_vidstabtransform_outputs[]
Definition: vf_vidstabtransform.c:285
TransformContext::conf
VSTransformConfig conf
Definition: vf_vidstabtransform.c:38
ff_av2vs_pixfmt
VSPixelFormat ff_av2vs_pixfmt(AVFilterContext *ctx, enum AVPixelFormat pf)
convert AV's pixelformat to vid.stab pixelformat
Definition: vidstabutils.c:32
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_vidstab_pix_fmts
enum AVPixelFormat ff_vidstab_pix_fmts[]
Definition: vidstabutils.c:23
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
file_open.h
OFFSET
#define OFFSET(x)
Definition: vf_vidstabtransform.c:46
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_vf_vidstabtransform
const AVFilter ff_vf_vidstabtransform
Definition: vf_vidstabtransform.c:292
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:594
f
f
Definition: af_crystalizer.c:122
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
TransformContext::td
VSTransformData td
Definition: vf_vidstabtransform.c:37
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:524
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
FLAGS
#define FLAGS
Definition: vf_vidstabtransform.c:48
TransformContext::debug
int debug
Definition: vf_vidstabtransform.c:43
invert
static void invert(float *h, int n)
Definition: asrc_sinc.c:198
vidstabtransform_options
static const AVOption vidstabtransform_options[]
Definition: vf_vidstabtransform.c:50
common.h
TransformContext::trans
VSTransformations trans
Definition: vf_vidstabtransform.c:40
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
avpriv_fopen_utf8
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:159
AVFilter
Filter definition.
Definition: avfilter.h:161
ret
ret
Definition: filter_design.txt:187
ff_vs_init
void ff_vs_init(void)
sets the memory allocation function and logging constants to av versions
Definition: vidstabutils.c:77
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AV_PIX_FMT_FLAG_PLANAR
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:132
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
tc
#define tc
Definition: regdef.h:69
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
AVFrame::linesize
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:375
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
TransformContext::tripod
int tripod
Definition: vf_vidstabtransform.c:42