2010-03-17 05:43:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Bobby Bingham
|
|
|
|
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2010-11-18 22:37:17 +02:00
|
|
|
* aspect ratio modification video filters
|
2010-03-17 05:43:14 +02:00
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-01-25 01:20:23 +03:00
|
|
|
#include "libavutil/opt.h"
|
2011-06-04 14:58:23 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2012-01-17 17:25:14 +03:00
|
|
|
#include "libavutil/parseutils.h"
|
2010-03-17 05:43:14 +02:00
|
|
|
#include "avfilter.h"
|
2012-06-12 21:12:42 +03:00
|
|
|
#include "internal.h"
|
2012-05-19 11:37:56 +03:00
|
|
|
#include "video.h"
|
2010-03-17 05:43:14 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2012-01-25 01:20:23 +03:00
|
|
|
const AVClass *class;
|
2012-01-25 00:47:32 +03:00
|
|
|
AVRational ratio;
|
2012-01-25 01:20:23 +03:00
|
|
|
char *ratio_str;
|
2012-10-16 19:10:59 +03:00
|
|
|
int max;
|
2010-03-17 05:43:14 +02:00
|
|
|
} AspectContext;
|
|
|
|
|
2012-01-25 01:20:23 +03:00
|
|
|
#define OFFSET(x) offsetof(AspectContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
2012-10-16 19:10:59 +03:00
|
|
|
{"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
|
2012-01-25 01:20:23 +03:00
|
|
|
{"ratio", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
|
|
|
|
{"r", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
|
2010-03-17 05:43:14 +02:00
|
|
|
{
|
|
|
|
AspectContext *aspect = ctx->priv;
|
2012-10-16 19:10:59 +03:00
|
|
|
static const char *shorthand[] = { "ratio", "max", NULL };
|
2012-01-25 01:20:23 +03:00
|
|
|
char c;
|
|
|
|
int ret;
|
|
|
|
AVRational q;
|
|
|
|
|
|
|
|
aspect->class = class;
|
|
|
|
av_opt_set_defaults(aspect);
|
|
|
|
|
2013-02-20 14:34:05 +03:00
|
|
|
if (args && sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
|
2012-01-25 01:20:23 +03:00
|
|
|
aspect->ratio_str = av_strdup(args);
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"num:den syntax is deprecated, please use num/den or named options instead\n");
|
|
|
|
} else if ((ret = av_opt_set_from_string(aspect, args, shorthand, "=", ":")) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2012-01-25 01:20:23 +03:00
|
|
|
if (aspect->ratio_str) {
|
2012-10-16 19:10:59 +03:00
|
|
|
ret = av_parse_ratio(&aspect->ratio, aspect->ratio_str, aspect->max, 0, ctx);
|
2012-01-25 01:20:23 +03:00
|
|
|
if (ret < 0 || aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
|
2010-11-23 00:03:30 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2012-01-25 01:20:23 +03:00
|
|
|
"Invalid string '%s' for aspect ratio\n", args);
|
2012-03-19 22:06:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
2010-11-23 00:03:30 +02:00
|
|
|
}
|
2010-03-17 05:43:14 +02:00
|
|
|
}
|
2012-02-18 13:31:28 +03:00
|
|
|
|
2012-06-27 02:58:09 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
|
2010-03-17 05:43:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *link, AVFrame *frame)
|
2010-03-17 05:43:14 +02:00
|
|
|
{
|
|
|
|
AspectContext *aspect = link->dst->priv;
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
frame->sample_aspect_ratio = aspect->ratio;
|
2012-11-29 02:31:50 +03:00
|
|
|
return ff_filter_frame(link->dst->outputs[0], frame);
|
2010-03-17 05:43:14 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 01:20:23 +03:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AspectContext *aspect = ctx->priv;
|
|
|
|
|
|
|
|
av_opt_free(aspect);
|
|
|
|
}
|
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
#if CONFIG_SETDAR_FILTER
|
2012-01-25 01:20:23 +03:00
|
|
|
|
|
|
|
#define setdar_options options
|
|
|
|
AVFILTER_DEFINE_CLASS(setdar);
|
|
|
|
|
|
|
|
static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
|
|
|
|
{
|
|
|
|
return init(ctx, args, &setdar_class);
|
|
|
|
}
|
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
static int setdar_config_props(AVFilterLink *inlink)
|
2010-03-17 05:43:14 +02:00
|
|
|
{
|
|
|
|
AspectContext *aspect = inlink->dst->priv;
|
2012-01-25 00:47:32 +03:00
|
|
|
AVRational dar = aspect->ratio;
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2012-01-25 00:47:32 +03:00
|
|
|
av_reduce(&aspect->ratio.num, &aspect->ratio.den,
|
|
|
|
aspect->ratio.num * inlink->h,
|
|
|
|
aspect->ratio.den * inlink->w, 100);
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
|
2012-01-25 00:47:32 +03:00
|
|
|
inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
|
2011-01-31 19:51:47 +02:00
|
|
|
|
2012-01-25 00:47:32 +03:00
|
|
|
inlink->sample_aspect_ratio = aspect->ratio;
|
2011-01-31 19:51:47 +02:00
|
|
|
|
2010-03-17 05:43:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_setdar_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = setdar_config_props,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
2012-11-29 02:31:50 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_setdar_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
AVFilter avfilter_vf_setdar = {
|
|
|
|
.name = "setdar",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2012-01-25 01:20:23 +03:00
|
|
|
.init = setdar_init,
|
|
|
|
.uninit = uninit,
|
2010-03-17 05:43:14 +02:00
|
|
|
|
|
|
|
.priv_size = sizeof(AspectContext),
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
.inputs = avfilter_vf_setdar_inputs,
|
|
|
|
|
|
|
|
.outputs = avfilter_vf_setdar_outputs,
|
2012-01-25 01:20:23 +03:00
|
|
|
.priv_class = &setdar_class,
|
2010-03-17 05:43:14 +02:00
|
|
|
};
|
2012-01-25 01:20:23 +03:00
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
#endif /* CONFIG_SETDAR_FILTER */
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
#if CONFIG_SETSAR_FILTER
|
2012-01-25 01:20:23 +03:00
|
|
|
|
|
|
|
#define setsar_options options
|
|
|
|
AVFILTER_DEFINE_CLASS(setsar);
|
|
|
|
|
|
|
|
static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
|
|
|
|
{
|
|
|
|
return init(ctx, args, &setsar_class);
|
|
|
|
}
|
|
|
|
|
2011-01-31 19:51:47 +02:00
|
|
|
static int setsar_config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AspectContext *aspect = inlink->dst->priv;
|
|
|
|
|
2012-01-25 00:47:32 +03:00
|
|
|
inlink->sample_aspect_ratio = aspect->ratio;
|
2011-01-31 19:51:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_setsar_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = setsar_config_props,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
2012-11-29 02:31:50 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_setsar_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
AVFilter avfilter_vf_setsar = {
|
|
|
|
.name = "setsar",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2012-01-25 01:20:23 +03:00
|
|
|
.init = setsar_init,
|
|
|
|
.uninit = uninit,
|
2010-03-17 05:43:14 +02:00
|
|
|
|
|
|
|
.priv_size = sizeof(AspectContext),
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
.inputs = avfilter_vf_setsar_inputs,
|
|
|
|
|
|
|
|
.outputs = avfilter_vf_setsar_outputs,
|
2012-10-22 14:12:15 +03:00
|
|
|
.priv_class = &setsar_class,
|
2010-03-17 05:43:14 +02:00
|
|
|
};
|
2012-01-25 01:20:23 +03:00
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
#endif /* CONFIG_SETSAR_FILTER */
|