FFmpeg
Loading...
Searching...
No Matches
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 "video.h"
32
33#include "vidstabutils.h"
34
35typedef struct StabData {
36 const AVClass *class;
37
38 VSMotionDetect md;
39 VSMotionDetectConfig conf;
40
41 char *result;
43 FILE *f;
44} StabData;
45
46
47#define OFFSET(x) offsetof(StabData, x)
48#define OFFSETC(x) (offsetof(StabData, conf)+offsetof(VSMotionDetectConfig, x))
49#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
50
52 {"result", "path to the file used to write the transforms", OFFSET(result), AV_OPT_TYPE_STRING, {.str = DEFAULT_RESULT_NAME}, .flags = FLAGS},
53 {"shakiness", "how shaky is the video and how quick is the camera?"
54 " 1: little (fast) 10: very strong/quick (slow)", OFFSETC(shakiness), AV_OPT_TYPE_INT, {.i64 = 5}, 1, 10, FLAGS},
55 {"accuracy", "(>=shakiness) 1: low 15: high (slow)", OFFSETC(accuracy), AV_OPT_TYPE_INT, {.i64 = 15}, 1, 15, FLAGS},
56 {"stepsize", "region around minimum is scanned with 1 pixel resolution", OFFSETC(stepSize), AV_OPT_TYPE_INT, {.i64 = 6}, 1, 32, FLAGS},
57 {"mincontrast", "below this contrast a field is discarded (0-1)", OFFSETC(contrastThreshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0.0, 1.0, FLAGS},
58 {"show", "0: draw nothing; 1,2: show fields and transforms", OFFSETC(show), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, FLAGS},
59 {"tripod", "virtual tripod mode (if >0): motion is compared to a reference"
60 " reference frame (frame # is the value)", OFFSETC(virtualTripod), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
61#ifdef LIBVIDSTAB_FILE_FORMAT_VERSION
62 { "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"},
63 { "ascii", "ASCII text", 0, AV_OPT_TYPE_CONST, {.i64 = ASCII_SERIALIZATION_MODE }, 0, 0, FLAGS, .unit = "file_format"},
64 { "binary", "binary", 0, AV_OPT_TYPE_CONST, {.i64 = BINARY_SERIALIZATION_MODE}, 0, 0, FLAGS, .unit = "file_format"},
65#endif
66 {NULL}
67};
68
69AVFILTER_DEFINE_CLASS(vidstabdetect);
70
72{
73 StabData *s = ctx->priv;
74 ff_vs_init();
75 s->class = &vidstabdetect_class;
76 av_log(ctx, AV_LOG_VERBOSE, "vidstabdetect filter: init %s\n", LIBVIDSTAB_VERSION);
77 return 0;
78}
79
81{
82 StabData *s = ctx->priv;
83 VSMotionDetect *md = &(s->md);
84
85 if (s->f) {
86 fclose(s->f);
87 s->f = NULL;
88 }
89
90 vsMotionDetectionCleanup(md);
91}
92
93static int config_input(AVFilterLink *inlink)
94{
95 AVFilterContext *ctx = inlink->dst;
96 StabData *s = ctx->priv;
97
98 VSMotionDetect* md = &(s->md);
99 VSFrameInfo fi;
101 int is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
102 const char *file_mode = "w";
103
104#ifdef LIBVIDSTAB_FILE_FORMAT_VERSION
105 md->serializationMode = s->fileformat;
106 if (s->fileformat == BINARY_SERIALIZATION_MODE)
107 file_mode = "wb";
108#endif
109
110 vsFrameInfoInit(&fi, inlink->w, inlink->h,
111 ff_av2vs_pixfmt(ctx, inlink->format));
112 if (!is_planar && fi.bytesPerPixel != av_get_bits_per_pixel(desc)/8) {
113 av_log(ctx, AV_LOG_ERROR, "pixel-format error: wrong bits/per/pixel, please report a BUG");
114 return AVERROR(EINVAL);
115 }
116 if (fi.log2ChromaW != desc->log2_chroma_w) {
117 av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_w, please report a BUG");
118 return AVERROR(EINVAL);
119 }
120
121 if (fi.log2ChromaH != desc->log2_chroma_h) {
122 av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_h, please report a BUG");
123 return AVERROR(EINVAL);
124 }
125
126 // set values that are not initialized by the options
127 s->conf.algo = 1;
128 s->conf.modName = "vidstabdetect";
129 if (vsMotionDetectInit(md, &s->conf, &fi) != VS_OK) {
130 av_log(ctx, AV_LOG_ERROR, "initialization of Motion Detection failed, please report a BUG");
131 return AVERROR(EINVAL);
132 }
133
134 vsMotionDetectGetConfig(&s->conf, md);
135 av_log(ctx, AV_LOG_INFO, "Video stabilization settings (pass 1/2):\n");
136 av_log(ctx, AV_LOG_INFO, " shakiness = %d\n", s->conf.shakiness);
137 av_log(ctx, AV_LOG_INFO, " accuracy = %d\n", s->conf.accuracy);
138 av_log(ctx, AV_LOG_INFO, " stepsize = %d\n", s->conf.stepSize);
139 av_log(ctx, AV_LOG_INFO, " mincontrast = %f\n", s->conf.contrastThreshold);
140 av_log(ctx, AV_LOG_INFO, " tripod = %d\n", s->conf.virtualTripod);
141 av_log(ctx, AV_LOG_INFO, " show = %d\n", s->conf.show);
142 av_log(ctx, AV_LOG_INFO, " result = %s\n", s->result);
143
144 s->f = avpriv_fopen_utf8(s->result, file_mode);
145 if (s->f == NULL) {
146 av_log(ctx, AV_LOG_ERROR, "cannot open transform file %s\n", s->result);
147 return AVERROR(EINVAL);
148 } else {
149 if (vsPrepareFile(md, s->f) != VS_OK) {
150 av_log(ctx, AV_LOG_ERROR, "cannot write to transform file %s\n", s->result);
151 return AVERROR(EINVAL);
152 }
153 }
154 return 0;
155}
156
157static int filter_frame(AVFilterLink *inlink, AVFrame *in)
158{
159 AVFilterContext *ctx = inlink->dst;
160 StabData *s = ctx->priv;
161 VSMotionDetect *md = &(s->md);
162 LocalMotions localmotions;
163
164 AVFilterLink *outlink = inlink->dst->outputs[0];
165 VSFrame frame;
166 int plane, ret;
167
168 if (s->conf.show > 0 && !av_frame_is_writable(in)) {
169 ret = ff_inlink_make_frame_writable(inlink, &in);
170 if (ret < 0) {
171 av_frame_free(&in);
172 return ret;
173 }
174 }
175
176 for (plane = 0; plane < md->fi.planes; plane++) {
177 frame.data[plane] = in->data[plane];
178 frame.linesize[plane] = in->linesize[plane];
179 }
180 if (vsMotionDetection(md, &localmotions, &frame) != VS_OK) {
181 av_log(ctx, AV_LOG_ERROR, "motion detection failed");
182 return AVERROR_EXTERNAL;
183 } else {
184 if (vsWriteToFile(md, s->f, &localmotions) != VS_OK) {
185 int ret = AVERROR(errno);
186 av_log(ctx, AV_LOG_ERROR, "cannot write to transform file");
187 return ret;
188 }
189 vs_vector_del(&localmotions);
190 }
191
192 return ff_filter_frame(outlink, in);
193}
194
196 {
197 .name = "default",
198 .type = AVMEDIA_TYPE_VIDEO,
199 .filter_frame = filter_frame,
200 .config_props = config_input,
201 },
202};
203
205 .p.name = "vidstabdetect",
206 .p.description = NULL_IF_CONFIG_SMALL("Extract relative transformations, "
207 "pass 1 of 2 for stabilization "
208 "(see vidstabtransform for pass 2)."),
210 .p.priv_class = &vidstabdetect_class,
211 .priv_size = sizeof(StabData),
212 .init = init,
213 .uninit = uninit,
217};
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_vidstabdetect
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
Make sure a frame is writable.
Definition avfilter.c:1567
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define NULL
Definition coverity.c:32
static AVFrame * frame
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_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 AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#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 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
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
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
VSMotionDetectConfig conf
VSMotionDetect md
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
#define md
static const AVOption vidstabdetect_options[]
#define DEFAULT_RESULT_NAME
#define OFFSETC(x)
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static const AVFilterPad avfilter_vf_vidstabdetect_inputs[]
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