2013-04-18 23:49:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFAULT_INPUT_NAME "transforms.trf"
|
|
|
|
|
|
|
|
#include <vid.stab/libvidstab.h>
|
|
|
|
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#include "vidstabutils.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2013-04-24 15:01:26 +00:00
|
|
|
const AVClass *class;
|
2013-04-18 23:49:27 +00:00
|
|
|
|
|
|
|
VSTransformData td;
|
|
|
|
VSTransformConfig conf;
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
VSTransformations trans; // transformations
|
|
|
|
char *input; // name of transform file
|
2013-04-18 23:49:27 +00:00
|
|
|
int tripod;
|
|
|
|
} TransformContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(TransformContext, x)
|
|
|
|
#define OFFSETC(x) (offsetof(TransformContext, conf)+offsetof(VSTransformConfig, x))
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
static const AVOption vidstabtransform_options[] = {
|
2013-04-24 15:29:29 +00:00
|
|
|
{"input", "path to the file storing the transforms", OFFSET(input),
|
2013-04-24 15:13:01 +00:00
|
|
|
AV_OPT_TYPE_STRING, {.str = DEFAULT_INPUT_NAME}, .flags = FLAGS },
|
2013-04-24 15:29:29 +00:00
|
|
|
{"smoothing", "number of frames*2 + 1 used for lowpass filtering", OFFSETC(smoothing),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 10}, 1, 1000, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"maxshift", "maximal number of pixels to translate image", OFFSETC(maxShift),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 500, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"maxangle", "maximal angle in rad to rotate image", OFFSETC(maxAngle),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_DOUBLE, {.dbl = -1.0}, -1.0, 3.14, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"crop", "set cropping mode", OFFSETC(crop),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, "crop"},
|
|
|
|
{ "keep", "keep border", 0,
|
|
|
|
AV_OPT_TYPE_CONST, {.i64 = VSKeepBorder }, 0, 0, FLAGS, "crop"},
|
|
|
|
{ "black", "black border", 0,
|
|
|
|
AV_OPT_TYPE_CONST, {.i64 = VSCropBorder }, 0, 0, FLAGS, "crop"},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"invert", "1: invert transforms", OFFSETC(invert),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"relative", "consider transforms as 0: absolute, 1: relative", OFFSETC(relative),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"zoom", "percentage to zoom >0: zoom in, <0 zoom out", OFFSETC(zoom),
|
2013-09-13 09:31:56 +00:00
|
|
|
AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -100, 100, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"optzoom", "0: nothing, 1: determine optimal zoom (added to 'zoom')", OFFSETC(optZoom),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS},
|
2013-04-24 15:29:29 +00:00
|
|
|
{"interpol", "type of interpolation", OFFSETC(interpolType),
|
2013-04-18 23:49:27 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 2}, 0, 3, FLAGS, "interpol"},
|
|
|
|
{ "no", "no interpolation", 0,
|
|
|
|
AV_OPT_TYPE_CONST, {.i64 = VS_Zero }, 0, 0, FLAGS, "interpol"},
|
|
|
|
{ "linear", "linear (horizontal)", 0,
|
|
|
|
AV_OPT_TYPE_CONST, {.i64 = VS_Linear }, 0, 0, FLAGS, "interpol"},
|
|
|
|
{ "bilinear","bi-linear", 0,
|
|
|
|
AV_OPT_TYPE_CONST, {.i64 = VS_BiLinear},0, 0, FLAGS, "interpol"},
|
|
|
|
{ "bicubic", "bi-cubic", 0,
|
|
|
|
AV_OPT_TYPE_CONST, {.i64 = VS_BiCubic },0, 0, FLAGS, "interpol"},
|
|
|
|
{"tripod", "if 1: virtual tripod mode (equiv. to relative=0:smoothing=0)", OFFSET(tripod),
|
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS},
|
2013-04-24 15:01:26 +00:00
|
|
|
{NULL}
|
2013-04-18 23:49:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(vidstabtransform);
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
2013-04-24 15:01:26 +00:00
|
|
|
TransformContext *tc = ctx->priv;
|
2013-04-18 23:49:27 +00:00
|
|
|
vs_set_mem_and_log_functions();
|
|
|
|
tc->class = &vidstabtransform_class;
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "vidstabtransform filter: init %s\n", LIBVIDSTAB_VERSION);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
TransformContext *tc = ctx->priv;
|
|
|
|
|
|
|
|
vsTransformDataCleanup(&tc->td);
|
|
|
|
vsTransformationsCleanup(&tc->trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
// If you add something here also add it in vidstabutils.c
|
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
|
|
|
|
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
|
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
TransformContext *tc = ctx->priv;
|
2013-04-24 15:01:26 +00:00
|
|
|
FILE *f;
|
2013-04-18 23:49:27 +00:00
|
|
|
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
VSTransformData *td = &(tc->td);
|
2013-04-18 23:49:27 +00:00
|
|
|
|
|
|
|
VSFrameInfo fi_src;
|
|
|
|
VSFrameInfo fi_dest;
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
if (!vsFrameInfoInit(&fi_src, inlink->w, inlink->h,
|
|
|
|
av_2_vs_pixel_format(ctx, inlink->format)) ||
|
|
|
|
!vsFrameInfoInit(&fi_dest, inlink->w, inlink->h,
|
|
|
|
av_2_vs_pixel_format(ctx, inlink->format))) {
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "unknown pixel format: %i (%s)",
|
|
|
|
inlink->format, desc->name);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
if (fi_src.bytesPerPixel != av_get_bits_per_pixel(desc)/8 ||
|
|
|
|
fi_src.log2ChromaW != desc->log2_chroma_w ||
|
|
|
|
fi_src.log2ChromaH != desc->log2_chroma_h) {
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "pixel-format error: bpp %i<>%i ",
|
|
|
|
fi_src.bytesPerPixel, av_get_bits_per_pixel(desc)/8);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "chroma_subsampl: w: %i<>%i h: %i<>%i\n",
|
|
|
|
fi_src.log2ChromaW, desc->log2_chroma_w,
|
|
|
|
fi_src.log2ChromaH, desc->log2_chroma_h);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set values that are not initializes by the options
|
|
|
|
tc->conf.modName = "vidstabtransform";
|
|
|
|
tc->conf.verbose =1;
|
2013-04-24 15:01:26 +00:00
|
|
|
if (tc->tripod) {
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_INFO, "Virtual tripod mode: relative=0, smoothing=0");
|
2013-04-24 15:01:26 +00:00
|
|
|
tc->conf.relative = 0;
|
|
|
|
tc->conf.smoothing = 0;
|
2013-04-18 23:49:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
if (vsTransformDataInit(td, &tc->conf, &fi_src, &fi_dest) != VS_OK) {
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "initialization of vid.stab transform failed, please report a BUG\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
vsTransformGetConfig(&tc->conf, td);
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_INFO, "Video transformation/stabilization settings (pass 2/2):\n");
|
|
|
|
av_log(ctx, AV_LOG_INFO, " input = %s\n", tc->input);
|
|
|
|
av_log(ctx, AV_LOG_INFO, " smoothing = %d\n", tc->conf.smoothing);
|
|
|
|
av_log(ctx, AV_LOG_INFO, " maxshift = %d\n", tc->conf.maxShift);
|
|
|
|
av_log(ctx, AV_LOG_INFO, " maxangle = %f\n", tc->conf.maxAngle);
|
|
|
|
av_log(ctx, AV_LOG_INFO, " crop = %s\n", tc->conf.crop ? "Black" : "Keep");
|
|
|
|
av_log(ctx, AV_LOG_INFO, " relative = %s\n", tc->conf.relative ? "True": "False");
|
|
|
|
av_log(ctx, AV_LOG_INFO, " invert = %s\n", tc->conf.invert ? "True" : "False");
|
|
|
|
av_log(ctx, AV_LOG_INFO, " zoom = %f\n", tc->conf.zoom);
|
|
|
|
av_log(ctx, AV_LOG_INFO, " optzoom = %s\n", tc->conf.optZoom ? "On" : "Off");
|
|
|
|
av_log(ctx, AV_LOG_INFO, " interpol = %s\n", getInterpolationTypeName(tc->conf.interpolType));
|
|
|
|
|
|
|
|
f = fopen(tc->input, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot open input file %s\n", tc->input);
|
|
|
|
return AVERROR(errno);
|
|
|
|
} else {
|
|
|
|
VSManyLocalMotions mlms;
|
2013-04-24 15:01:26 +00:00
|
|
|
if (vsReadLocalMotionsFile(f, &mlms) == VS_OK) {
|
|
|
|
// calculate the actual transforms from the local motions
|
|
|
|
if (vsLocalmotions2TransformsSimple(td, &mlms, &tc->trans) != VS_OK) {
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "calculating transformations failed\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-04-24 15:01:26 +00:00
|
|
|
} else { // try to read old format
|
2013-04-18 23:49:27 +00:00
|
|
|
if (!vsReadOldTransforms(td, f, &tc->trans)) { /* read input file */
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "error parsing input file %s\n", tc->input);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
if (vsPreprocessTransforms(td, &tc->trans) != VS_OK ) {
|
2013-04-18 23:49:27 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "error while preprocessing transforms\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add sharpening, so far the user needs to call the unsharp filter manually
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
2013-04-18 23:49:27 +00:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
TransformContext *tc = ctx->priv;
|
|
|
|
VSTransformData* td = &(tc->td);
|
|
|
|
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
|
|
|
int direct = 0;
|
|
|
|
AVFrame *out;
|
|
|
|
VSFrame inframe;
|
|
|
|
int plane;
|
|
|
|
|
|
|
|
if (av_frame_is_writable(in)) {
|
|
|
|
direct = 1;
|
|
|
|
out = in;
|
|
|
|
} else {
|
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
}
|
|
|
|
|
2013-04-24 15:01:26 +00:00
|
|
|
for (plane = 0; plane < vsTransformGetSrcFrameInfo(td)->planes; plane++) {
|
2013-04-18 23:49:27 +00:00
|
|
|
inframe.data[plane] = in->data[plane];
|
|
|
|
inframe.linesize[plane] = in->linesize[plane];
|
|
|
|
}
|
2013-04-24 15:16:27 +00:00
|
|
|
if (direct) {
|
2013-04-18 23:49:27 +00:00
|
|
|
vsTransformPrepare(td, &inframe, &inframe);
|
2013-04-24 15:01:26 +00:00
|
|
|
} else { // separate frames
|
2013-04-18 23:49:27 +00:00
|
|
|
VSFrame outframe;
|
2013-04-24 15:01:26 +00:00
|
|
|
for (plane = 0; plane < vsTransformGetDestFrameInfo(td)->planes; plane++) {
|
2013-04-18 23:49:27 +00:00
|
|
|
outframe.data[plane] = out->data[plane];
|
|
|
|
outframe.linesize[plane] = out->linesize[plane];
|
|
|
|
}
|
|
|
|
vsTransformPrepare(td, &inframe, &outframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
vsDoTransform(td, vsGetNextTransform(td, &tc->trans));
|
|
|
|
|
|
|
|
vsTransformFinish(td);
|
|
|
|
|
|
|
|
if (!direct)
|
|
|
|
av_frame_free(&in);
|
|
|
|
|
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_vidstabtransform_inputs[] = {
|
|
|
|
{
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_input,
|
2013-04-18 23:49:27 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_vidstabtransform_outputs[] = {
|
|
|
|
{
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2013-04-18 23:49:27 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter avfilter_vf_vidstabtransform = {
|
|
|
|
.name = "vidstabtransform",
|
2013-04-24 15:01:50 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Transform the frames, "
|
|
|
|
"pass 2 of 2 for stabilization "
|
|
|
|
"(see vidstabdetect for pass 1)."),
|
2013-04-18 23:49:27 +00:00
|
|
|
.priv_size = sizeof(TransformContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.inputs = avfilter_vf_vidstabtransform_inputs,
|
|
|
|
.outputs = avfilter_vf_vidstabtransform_outputs,
|
|
|
|
.priv_class = &vidstabtransform_class,
|
|
|
|
};
|