FFmpeg
Loading...
Searching...
No Matches
vf_aspect.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Bobby Bingham
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/**
22 * @file
23 * aspect ratio modification video filters
24 */
25
26#include "config_components.h"
27
28#include <float.h>
29
30#include "libavutil/common.h"
31#include "libavutil/eval.h"
33#include "libavutil/opt.h"
35#include "libavutil/pixdesc.h"
36
37#include "avfilter.h"
38#include "filters.h"
39#include "video.h"
40
41static const char *const var_names[] = {
42 "w",
43 "h",
44 "a",
45 "dar",
46 "sar",
47 "hsub",
48 "vsub",
49 NULL
50};
51
62
70
72{
73 AspectContext *s = link->dst->priv;
74
75 frame->sample_aspect_ratio = s->sar;
76 return ff_filter_frame(link->dst->outputs[0], frame);
77}
78
79#define OFFSET(x) offsetof(AspectContext, x)
80#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81
82static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
83{
84 if (sar.num && sar.den) {
85 av_reduce(&dar->num, &dar->den, sar.num * (int64_t)w, sar.den * (int64_t)h, INT_MAX);
86 } else {
87 av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
88 }
89}
90
91static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
92{
93 AVFilterContext *ctx = inlink->dst;
94 AspectContext *s = inlink->dst->priv;
96 double var_values[VARS_NB], res;
97 int ret;
98
99 var_values[VAR_W] = inlink->w;
100 var_values[VAR_H] = inlink->h;
101 var_values[VAR_A] = (double) inlink->w / inlink->h;
102 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
103 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
104 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
105 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
106 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
107
108 /* evaluate new aspect ratio*/
109 ret = av_expr_parse_and_eval(&res, s->ratio_expr,
110 var_names, var_values,
111 NULL, NULL, NULL, NULL, NULL, 0, ctx);
112 if (ret < 0) {
113 ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
114 } else
115 *aspect_ratio = av_d2q(res, s->max);
116
117 if (ret < 0) {
119 "Error when evaluating the expression '%s'\n", s->ratio_expr);
120 return ret;
121 }
122 if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
124 "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
125 return AVERROR(EINVAL);
126 }
127 return 0;
128}
129
130static const AVFilterPad aspect_inputs[] = {
131 {
132 .name = "default",
133 .type = AVMEDIA_TYPE_VIDEO,
134 .filter_frame = filter_frame,
135 },
136};
137
138#if CONFIG_SETDAR_FILTER
139
140static int setdar_config_props(AVFilterLink *outlink)
141{
142 AVFilterContext *ctx = outlink->src;
143 AVFilterLink *inlink = ctx->inputs[0];
144 AspectContext *s = ctx->priv;
145 AVRational dar;
146 AVRational old_dar;
147 AVRational old_sar = inlink->sample_aspect_ratio;
148 int ret;
149
150 if ((ret = get_aspect_ratio(inlink, &s->dar)))
151 return ret;
152
153 if (s->dar.num && s->dar.den) {
154 av_reduce(&s->sar.num, &s->sar.den,
155 s->dar.num * inlink->h,
156 s->dar.den * inlink->w, INT_MAX);
157 outlink->sample_aspect_ratio = s->sar;
158 dar = s->dar;
159 } else {
160 outlink->sample_aspect_ratio = (AVRational){ 1, 1 };
161 dar = (AVRational){ inlink->w, inlink->h };
162 }
163
164 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
165 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
166 inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
167 dar.num, dar.den, outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
168
169 return 0;
170}
171
172static const AVOption setdar_options[] = {
173 { "dar", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
174 { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
175 { "r", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
176 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
177 { NULL }
178};
179
181
182static const AVFilterPad avfilter_vf_setdar_outputs[] = {
183 {
184 .name = "default",
185 .type = AVMEDIA_TYPE_VIDEO,
186 .config_props = setdar_config_props,
187 },
188};
189
190const FFFilter ff_vf_setdar = {
191 .p.name = "setdar",
192 .p.description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
193 .p.priv_class = &setdar_class,
195 .priv_size = sizeof(AspectContext),
197 FILTER_OUTPUTS(avfilter_vf_setdar_outputs),
198};
199
200#endif /* CONFIG_SETDAR_FILTER */
201
202#if CONFIG_SETSAR_FILTER
203
204static int setsar_config_props(AVFilterLink *outlink)
205{
206 AVFilterContext *ctx = outlink->src;
207 AVFilterLink *inlink = ctx->inputs[0];
208 AspectContext *s = ctx->priv;
209 AVRational old_sar = inlink->sample_aspect_ratio;
210 AVRational old_dar, dar;
211 int ret;
212
213 if ((ret = get_aspect_ratio(inlink, &s->sar)))
214 return ret;
215
216 outlink->sample_aspect_ratio = s->sar;
217
218 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
219 compute_dar(&dar, s->sar, inlink->w, inlink->h);
220 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
221 inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
222 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den, dar.num, dar.den);
223
224 return 0;
225}
226
227static const AVOption setsar_options[] = {
228 { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
229 { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
230 { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
231 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
232 { NULL }
233};
234
236
237static const AVFilterPad avfilter_vf_setsar_outputs[] = {
238 {
239 .name = "default",
240 .type = AVMEDIA_TYPE_VIDEO,
241 .config_props = setsar_config_props,
242 },
243};
244
245const FFFilter ff_vf_setsar = {
246 .p.name = "setsar",
247 .p.description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
248 .p.priv_class = &setsar_class,
250 .priv_size = sizeof(AspectContext),
252 FILTER_OUTPUTS(avfilter_vf_setsar_outputs),
253};
254
255#endif /* CONFIG_SETSAR_FILTER */
const FFFilter ff_vf_setdar
const FFFilter ff_vf_setsar
@ VAR_H
Definition avfilter.c:565
@ VAR_W
Definition avfilter.c:564
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
@ VAR_VSUB
Definition boxblur.c:42
@ VARS_NB
Definition boxblur.c:43
@ VAR_HSUB
Definition boxblur.c:41
#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
long long int64_t
Definition coverity.c:34
#define max(a, b)
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition eval.c:839
simple arithmetic expression evaluator
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ 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(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition rational.c:110
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#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
uint8_t w
Definition llvidencdsp.c:39
var_name
Definition noise.c:46
static const char *const var_names[]
Definition noise.c:30
AVOptions.
int av_parse_ratio(AVRational *q, const char *str, int max, int log_offset, void *log_ctx)
Parse str and store the parsed ratio in q.
Definition parseutils.c:45
misc parsing utilities
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
@ VAR_SAR
Definition scale_eval.c:49
@ VAR_A
Definition scale_eval.c:48
@ VAR_DAR
Definition scale_eval.c:50
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
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
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
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
AVRational dar
Definition vf_aspect.c:65
AVRational sar
Definition vf_aspect.c:66
char * ratio_expr
Definition vf_aspect.c:68
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static void compute_dar(AVRational *dar, AVRational sar, int w, int h)
Definition vf_aspect.c:82
static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
Definition vf_aspect.c:91
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition vf_aspect.c:71
static const AVFilterPad aspect_inputs[]
Definition vf_aspect.c:130
#define OFFSET(x)
Definition vf_aspect.c:79