FFmpeg
vf_vidstabdetect.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_RESULT_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 "internal.h"
32 #include "video.h"
33 
34 #include "vidstabutils.h"
35 
36 typedef struct StabData {
37  const AVClass *class;
38 
39  VSMotionDetect md;
40  VSMotionDetectConfig conf;
41 
42  char *result;
44  FILE *f;
45 } StabData;
46 
47 
48 #define OFFSET(x) offsetof(StabData, x)
49 #define OFFSETC(x) (offsetof(StabData, conf)+offsetof(VSMotionDetectConfig, x))
50 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
51 
52 static const AVOption vidstabdetect_options[] = {
53  {"result", "path to the file used to write the transforms", OFFSET(result), AV_OPT_TYPE_STRING, {.str = DEFAULT_RESULT_NAME}, .flags = FLAGS},
54  {"shakiness", "how shaky is the video and how quick is the camera?"
55  " 1: little (fast) 10: very strong/quick (slow)", OFFSETC(shakiness), AV_OPT_TYPE_INT, {.i64 = 5}, 1, 10, FLAGS},
56  {"accuracy", "(>=shakiness) 1: low 15: high (slow)", OFFSETC(accuracy), AV_OPT_TYPE_INT, {.i64 = 15}, 1, 15, FLAGS},
57  {"stepsize", "region around minimum is scanned with 1 pixel resolution", OFFSETC(stepSize), AV_OPT_TYPE_INT, {.i64 = 6}, 1, 32, FLAGS},
58  {"mincontrast", "below this contrast a field is discarded (0-1)", OFFSETC(contrastThreshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0.0, 1.0, FLAGS},
59  {"show", "0: draw nothing; 1,2: show fields and transforms", OFFSETC(show), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, FLAGS},
60  {"tripod", "virtual tripod mode (if >0): motion is compared to a reference"
61  " reference frame (frame # is the value)", OFFSETC(virtualTripod), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
62 #ifdef LIBVIDSTAB_FILE_FORMAT_VERSION
63  { "fileformat", "transforms data file format", OFFSET(fileformat), AV_OPT_TYPE_INT, {.i64 = BINARY_SERIALIZATION_MODE}, ASCII_SERIALIZATION_MODE, BINARY_SERIALIZATION_MODE, FLAGS, .unit = "file_format"},
64  { "ascii", "ASCII text", 0, AV_OPT_TYPE_CONST, {.i64 = ASCII_SERIALIZATION_MODE }, 0, 0, FLAGS, .unit = "file_format"},
65  { "binary", "binary", 0, AV_OPT_TYPE_CONST, {.i64 = BINARY_SERIALIZATION_MODE}, 0, 0, FLAGS, .unit = "file_format"},
66 #endif
67  {NULL}
68 };
69 
70 AVFILTER_DEFINE_CLASS(vidstabdetect);
71 
73 {
74  StabData *s = ctx->priv;
75  ff_vs_init();
76  s->class = &vidstabdetect_class;
77  av_log(ctx, AV_LOG_VERBOSE, "vidstabdetect filter: init %s\n", LIBVIDSTAB_VERSION);
78  return 0;
79 }
80 
82 {
83  StabData *s = ctx->priv;
84  VSMotionDetect *md = &(s->md);
85 
86  if (s->f) {
87  fclose(s->f);
88  s->f = NULL;
89  }
90 
91  vsMotionDetectionCleanup(md);
92 }
93 
95 {
96  AVFilterContext *ctx = inlink->dst;
97  StabData *s = ctx->priv;
98 
99  VSMotionDetect* md = &(s->md);
100  VSFrameInfo fi;
102  int is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
103  const char *file_mode = "w";
104 
105 #ifdef LIBVIDSTAB_FILE_FORMAT_VERSION
106  md->serializationMode = s->fileformat;
107  if (s->fileformat == BINARY_SERIALIZATION_MODE)
108  file_mode = "wb";
109 #endif
110 
111  vsFrameInfoInit(&fi, inlink->w, inlink->h,
112  ff_av2vs_pixfmt(ctx, inlink->format));
113  if (!is_planar && fi.bytesPerPixel != av_get_bits_per_pixel(desc)/8) {
114  av_log(ctx, AV_LOG_ERROR, "pixel-format error: wrong bits/per/pixel, please report a BUG");
115  return AVERROR(EINVAL);
116  }
117  if (fi.log2ChromaW != desc->log2_chroma_w) {
118  av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_w, please report a BUG");
119  return AVERROR(EINVAL);
120  }
121 
122  if (fi.log2ChromaH != desc->log2_chroma_h) {
123  av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_h, please report a BUG");
124  return AVERROR(EINVAL);
125  }
126 
127  // set values that are not initialized by the options
128  s->conf.algo = 1;
129  s->conf.modName = "vidstabdetect";
130  if (vsMotionDetectInit(md, &s->conf, &fi) != VS_OK) {
131  av_log(ctx, AV_LOG_ERROR, "initialization of Motion Detection failed, please report a BUG");
132  return AVERROR(EINVAL);
133  }
134 
135  vsMotionDetectGetConfig(&s->conf, md);
136  av_log(ctx, AV_LOG_INFO, "Video stabilization settings (pass 1/2):\n");
137  av_log(ctx, AV_LOG_INFO, " shakiness = %d\n", s->conf.shakiness);
138  av_log(ctx, AV_LOG_INFO, " accuracy = %d\n", s->conf.accuracy);
139  av_log(ctx, AV_LOG_INFO, " stepsize = %d\n", s->conf.stepSize);
140  av_log(ctx, AV_LOG_INFO, " mincontrast = %f\n", s->conf.contrastThreshold);
141  av_log(ctx, AV_LOG_INFO, " tripod = %d\n", s->conf.virtualTripod);
142  av_log(ctx, AV_LOG_INFO, " show = %d\n", s->conf.show);
143  av_log(ctx, AV_LOG_INFO, " result = %s\n", s->result);
144 
145  s->f = avpriv_fopen_utf8(s->result, file_mode);
146  if (s->f == NULL) {
147  av_log(ctx, AV_LOG_ERROR, "cannot open transform file %s\n", s->result);
148  return AVERROR(EINVAL);
149  } else {
150  if (vsPrepareFile(md, s->f) != VS_OK) {
151  av_log(ctx, AV_LOG_ERROR, "cannot write to transform file %s\n", s->result);
152  return AVERROR(EINVAL);
153  }
154  }
155  return 0;
156 }
157 
159 {
160  AVFilterContext *ctx = inlink->dst;
161  StabData *s = ctx->priv;
162  VSMotionDetect *md = &(s->md);
163  LocalMotions localmotions;
164 
165  AVFilterLink *outlink = inlink->dst->outputs[0];
166  VSFrame frame;
167  int plane, ret;
168 
169  if (s->conf.show > 0 && !av_frame_is_writable(in)) {
171  if (ret < 0) {
172  av_frame_free(&in);
173  return ret;
174  }
175  }
176 
177  for (plane = 0; plane < md->fi.planes; plane++) {
178  frame.data[plane] = in->data[plane];
179  frame.linesize[plane] = in->linesize[plane];
180  }
181  if (vsMotionDetection(md, &localmotions, &frame) != VS_OK) {
182  av_log(ctx, AV_LOG_ERROR, "motion detection failed");
183  return AVERROR_EXTERNAL;
184  } else {
185  if (vsWriteToFile(md, s->f, &localmotions) != VS_OK) {
186  int ret = AVERROR(errno);
187  av_log(ctx, AV_LOG_ERROR, "cannot write to transform file");
188  return ret;
189  }
190  vs_vector_del(&localmotions);
191  }
192 
193  return ff_filter_frame(outlink, in);
194 }
195 
197  {
198  .name = "default",
199  .type = AVMEDIA_TYPE_VIDEO,
200  .filter_frame = filter_frame,
201  .config_props = config_input,
202  },
203 };
204 
206  .name = "vidstabdetect",
207  .description = NULL_IF_CONFIG_SMALL("Extract relative transformations, "
208  "pass 1 of 2 for stabilization "
209  "(see vidstabtransform for pass 2)."),
210  .priv_size = sizeof(StabData),
211  .init = init,
212  .uninit = uninit,
217  .priv_class = &vidstabdetect_class,
218 };
ff_vf_vidstabdetect
const AVFilter ff_vf_vidstabdetect
Definition: vf_vidstabdetect.c:205
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:162
FLAGS
#define FLAGS
Definition: vf_vidstabdetect.c:50
DEFAULT_RESULT_NAME
#define DEFAULT_RESULT_NAME
Definition: vf_vidstabdetect.c:21
StabData::md
VSMotionDetect md
Definition: vf_vidstabdetect.c:39
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
md
#define md
Definition: vf_colormatrix.c:101
StabData::fileformat
int fileformat
Definition: vf_vidstabdetect.c:43
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
pixdesc.h
AVOption
AVOption.
Definition: opt.h:346
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
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:2917
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_vidstabdetect.c:158
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
StabData::f
FILE * f
Definition: vf_vidstabdetect.c:44
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
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
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_av2vs_pixfmt
VSPixelFormat ff_av2vs_pixfmt(AVFilterContext *ctx, enum AVPixelFormat pf)
convert AV's pixelformat to vid.stab pixelformat
Definition: vidstabutils.c:33
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:237
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_vidstab_pix_fmts
enum AVPixelFormat ff_vidstab_pix_fmts[]
Definition: vidstabutils.c:24
OFFSETC
#define OFFSETC(x)
Definition: vf_vidstabdetect.c:49
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_vidstabdetect.c:72
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
file_open.h
ff_inlink_make_frame_writable
int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
Make sure a frame is writable.
Definition: avfilter.c:1489
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
avfilter_vf_vidstabdetect_inputs
static const AVFilterPad avfilter_vf_vidstabdetect_inputs[]
Definition: vf_vidstabdetect.c:196
StabData::conf
VSMotionDetectConfig conf
Definition: vf_vidstabdetect.c:40
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:94
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:645
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_vidstabdetect.c:94
vidstabdetect_options
static const AVOption vidstabdetect_options[]
Definition: vf_vidstabdetect.c:52
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_vidstabdetect.c:81
common.h
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
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:166
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(vidstabdetect)
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:78
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
OFFSET
#define OFFSET(x)
Definition: vf_vidstabdetect.c:48
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:133
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:407
desc
const char * desc
Definition: libsvtav1.c:75
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
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
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:419
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:239
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
StabData
Definition: vf_vidstabdetect.c:36
StabData::result
char * result
Definition: vf_vidstabdetect.c:42