2010-03-17 05:43:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Bobby Bingham
|
|
|
|
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2010-03-17 05:43:14 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-03-17 05:43:14 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-03-17 05:43:14 +02:00
|
|
|
* 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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-03-17 05:43:14 +02:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2011-06-04 14:58:23 +03:00
|
|
|
#include "libavutil/mathematics.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 {
|
|
|
|
AVRational aspect;
|
|
|
|
} AspectContext;
|
|
|
|
|
2012-06-21 08:55:56 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args)
|
2010-03-17 05:43:14 +02:00
|
|
|
{
|
|
|
|
AspectContext *aspect = ctx->priv;
|
|
|
|
double ratio;
|
|
|
|
int64_t gcd;
|
2010-11-23 00:03:30 +02:00
|
|
|
char c = 0;
|
2010-03-17 05:43:14 +02:00
|
|
|
|
2010-11-18 22:37:20 +02:00
|
|
|
if (args) {
|
2010-11-23 00:03:30 +02:00
|
|
|
if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
|
|
|
|
if (sscanf(args, "%lf%c", &ratio, &c) == 1)
|
|
|
|
aspect->aspect = av_d2q(ratio, 100);
|
|
|
|
|
|
|
|
if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid string '%s' for aspect ratio.\n", args);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2010-11-23 00:03:32 +02:00
|
|
|
gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
|
|
|
|
if (gcd) {
|
|
|
|
aspect->aspect.num /= gcd;
|
|
|
|
aspect->aspect.den /= gcd;
|
|
|
|
}
|
2010-03-17 05:43:14 +02:00
|
|
|
}
|
|
|
|
|
2010-11-18 22:37:20 +02:00
|
|
|
if (aspect->aspect.den == 0)
|
2010-03-17 05:43:14 +02:00
|
|
|
aspect->aspect = (AVRational) {0, 1};
|
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
|
2010-03-17 05:43:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-07 04:15:19 +03:00
|
|
|
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
|
2010-03-17 05:43:14 +02:00
|
|
|
{
|
|
|
|
AspectContext *aspect = link->dst->priv;
|
|
|
|
|
2010-08-11 14:06:04 +03:00
|
|
|
picref->video->pixel_aspect = aspect->aspect;
|
2012-05-30 12:20:32 +03:00
|
|
|
ff_start_frame(link->dst->outputs[0], picref);
|
2010-03-17 05:43:14 +02:00
|
|
|
}
|
|
|
|
|
2010-11-23 00:03:24 +02:00
|
|
|
#if CONFIG_SETDAR_FILTER
|
|
|
|
/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
|
|
|
|
static int setdar_config_props(AVFilterLink *inlink)
|
2010-03-17 05:43:14 +02:00
|
|
|
{
|
|
|
|
AspectContext *aspect = inlink->dst->priv;
|
2010-11-23 00:03:27 +02:00
|
|
|
AVRational dar = aspect->aspect;
|
2010-03-17 05:43:14 +02:00
|
|
|
|
|
|
|
av_reduce(&aspect->aspect.num, &aspect->aspect.den,
|
|
|
|
aspect->aspect.num * inlink->h,
|
|
|
|
aspect->aspect.den * inlink->w, 100);
|
|
|
|
|
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",
|
2010-11-24 04:38:55 +02:00
|
|
|
inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
|
2011-02-02 21:39:56 +02:00
|
|
|
|
|
|
|
inlink->sample_aspect_ratio = aspect->aspect;
|
|
|
|
|
2010-03-17 05:43:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
.init = init,
|
|
|
|
|
|
|
|
.priv_size = sizeof(AspectContext),
|
|
|
|
|
|
|
|
.inputs = (AVFilterPad[]) {{ .name = "default",
|
2010-03-31 02:30:55 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2010-11-23 00:03:24 +02:00
|
|
|
.config_props = setdar_config_props,
|
2012-05-19 11:37:56 +03:00
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
2010-03-17 05:43:14 +02:00
|
|
|
.start_frame = start_frame,
|
2012-05-19 11:37:56 +03:00
|
|
|
.end_frame = ff_null_end_frame },
|
2010-03-17 05:43:14 +02:00
|
|
|
{ .name = NULL}},
|
|
|
|
|
|
|
|
.outputs = (AVFilterPad[]) {{ .name = "default",
|
2010-03-31 02:30:55 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO, },
|
2010-03-17 05:43:14 +02:00
|
|
|
{ .name = NULL}},
|
|
|
|
};
|
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
|
2011-02-02 21:39:56 +02:00
|
|
|
/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
|
|
|
|
static int setsar_config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AspectContext *aspect = inlink->dst->priv;
|
|
|
|
|
|
|
|
inlink->sample_aspect_ratio = aspect->aspect;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
.init = init,
|
|
|
|
|
|
|
|
.priv_size = sizeof(AspectContext),
|
|
|
|
|
|
|
|
.inputs = (AVFilterPad[]) {{ .name = "default",
|
2010-03-31 02:30:55 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2011-02-02 21:39:56 +02:00
|
|
|
.config_props = setsar_config_props,
|
2012-05-19 11:37:56 +03:00
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
2010-03-17 05:43:14 +02:00
|
|
|
.start_frame = start_frame,
|
2012-05-19 11:37:56 +03:00
|
|
|
.end_frame = ff_null_end_frame },
|
2010-03-17 05:43:14 +02:00
|
|
|
{ .name = NULL}},
|
|
|
|
|
|
|
|
.outputs = (AVFilterPad[]) {{ .name = "default",
|
2010-03-31 02:30:55 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO, },
|
2010-03-17 05:43:14 +02:00
|
|
|
{ .name = NULL}},
|
|
|
|
};
|
2010-11-23 00:03:24 +02:00
|
|
|
#endif /* CONFIG_SETSAR_FILTER */
|