FFmpeg  4.3
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 <float.h>
27 
28 #include "libavutil/common.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
34 
35 #include "avfilter.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 static const char *const var_names[] = {
40  "w",
41  "h",
42  "a", "dar",
43  "sar",
44  "hsub",
45  "vsub",
46  NULL
47 };
48 
49 enum var_name {
57 };
58 
59 typedef struct AspectContext {
60  const AVClass *class;
63  int max;
64  char *ratio_expr;
66 
68 {
69  AspectContext *s = link->dst->priv;
70 
71  frame->sample_aspect_ratio = s->sar;
72  return ff_filter_frame(link->dst->outputs[0], frame);
73 }
74 
75 #define OFFSET(x) offsetof(AspectContext, x)
76 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
77 
78 static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
79 {
80  if (sar.num && sar.den) {
81  av_reduce(&dar->num, &dar->den, sar.num * (int64_t)w, sar.den * (int64_t)h, INT_MAX);
82  } else {
83  av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
84  }
85 }
86 
87 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
88 {
89  AVFilterContext *ctx = inlink->dst;
90  AspectContext *s = inlink->dst->priv;
92  double var_values[VARS_NB], res;
93  int ret;
94 
95  var_values[VAR_W] = inlink->w;
96  var_values[VAR_H] = inlink->h;
97  var_values[VAR_A] = (double) inlink->w / inlink->h;
98  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
99  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
100  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
101  var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
102  var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
103 
104  /* evaluate new aspect ratio*/
105  ret = av_expr_parse_and_eval(&res, s->ratio_expr,
106  var_names, var_values,
107  NULL, NULL, NULL, NULL, NULL, 0, ctx);
108  if (ret < 0) {
109  ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
110  } else
111  *aspect_ratio = av_d2q(res, s->max);
112 
113  if (ret < 0) {
115  "Error when evaluating the expression '%s'\n", s->ratio_expr);
116  return ret;
117  }
118  if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
120  "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
121  return AVERROR(EINVAL);
122  }
123  return 0;
124 }
125 
126 #if CONFIG_SETDAR_FILTER
127 
128 static int setdar_config_props(AVFilterLink *outlink)
129 {
130  AVFilterContext *ctx = outlink->src;
131  AVFilterLink *inlink = ctx->inputs[0];
132  AspectContext *s = ctx->priv;
133  AVRational dar;
134  AVRational old_dar;
135  AVRational old_sar = inlink->sample_aspect_ratio;
136  int ret;
137 
138  if ((ret = get_aspect_ratio(inlink, &s->dar)))
139  return ret;
140 
141  if (s->dar.num && s->dar.den) {
142  av_reduce(&s->sar.num, &s->sar.den,
143  s->dar.num * inlink->h,
144  s->dar.den * inlink->w, INT_MAX);
145  outlink->sample_aspect_ratio = s->sar;
146  dar = s->dar;
147  } else {
148  outlink->sample_aspect_ratio = (AVRational){ 1, 1 };
149  dar = (AVRational){ inlink->w, inlink->h };
150  }
151 
152  compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
153  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
154  inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
155  dar.num, dar.den, outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
156 
157  return 0;
158 }
159 
160 static const AVOption setdar_options[] = {
161  { "dar", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
162  { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
163  { "r", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
164  { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
165  { NULL }
166 };
167 
168 AVFILTER_DEFINE_CLASS(setdar);
169 
170 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_VIDEO,
174  .filter_frame = filter_frame,
175  },
176  { NULL }
177 };
178 
179 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
180  {
181  .name = "default",
182  .type = AVMEDIA_TYPE_VIDEO,
183  .config_props = setdar_config_props,
184  },
185  { NULL }
186 };
187 
189  .name = "setdar",
190  .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
191  .priv_size = sizeof(AspectContext),
192  .priv_class = &setdar_class,
193  .inputs = avfilter_vf_setdar_inputs,
194  .outputs = avfilter_vf_setdar_outputs,
195 };
196 
197 #endif /* CONFIG_SETDAR_FILTER */
198 
199 #if CONFIG_SETSAR_FILTER
200 
201 static int setsar_config_props(AVFilterLink *outlink)
202 {
203  AVFilterContext *ctx = outlink->src;
204  AVFilterLink *inlink = ctx->inputs[0];
205  AspectContext *s = ctx->priv;
206  AVRational old_sar = inlink->sample_aspect_ratio;
207  AVRational old_dar, dar;
208  int ret;
209 
210  if ((ret = get_aspect_ratio(inlink, &s->sar)))
211  return ret;
212 
213  outlink->sample_aspect_ratio = s->sar;
214 
215  compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
216  compute_dar(&dar, s->sar, inlink->w, inlink->h);
217  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
218  inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
219  outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den, dar.num, dar.den);
220 
221  return 0;
222 }
223 
224 static const AVOption setsar_options[] = {
225  { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
226  { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
227  { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
228  { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
229  { NULL }
230 };
231 
232 AVFILTER_DEFINE_CLASS(setsar);
233 
234 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
235  {
236  .name = "default",
237  .type = AVMEDIA_TYPE_VIDEO,
238  .filter_frame = filter_frame,
239  },
240  { NULL }
241 };
242 
243 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_VIDEO,
247  .config_props = setsar_config_props,
248  },
249  { NULL }
250 };
251 
253  .name = "setsar",
254  .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
255  .priv_size = sizeof(AspectContext),
256  .priv_class = &setsar_class,
257  .inputs = avfilter_vf_setsar_inputs,
258  .outputs = avfilter_vf_setsar_outputs,
259 };
260 
261 #endif /* CONFIG_SETSAR_FILTER */
FLAGS
#define FLAGS
Definition: vf_aspect.c:76
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
VAR_SAR
@ VAR_SAR
Definition: vf_aspect.c:53
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
pixdesc.h
ff_vf_setsar
AVFilter ff_vf_setsar
AVOption
AVOption.
Definition: opt.h:246
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
float.h
VAR_A
@ VAR_A
Definition: vf_aspect.c:52
max
#define max(a, b)
Definition: cuda_runtime.h:33
mathematics.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
get_aspect_ratio
static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
Definition: vf_aspect.c:87
video.h
AspectContext::dar
AVRational dar
Definition: vf_aspect.c:61
VAR_VSUB
@ VAR_VSUB
Definition: vf_aspect.c:55
av_reduce
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::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
VAR_HSUB
@ VAR_HSUB
Definition: vf_aspect.c:54
s
#define s(width, name)
Definition: cbs_vp9.c:257
VAR_DAR
@ VAR_DAR
Definition: vf_aspect.c:52
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AspectContext::ratio_expr
char * ratio_expr
Definition: vf_aspect.c:64
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
compute_dar
static void compute_dar(AVRational *dar, AVRational sar, int w, int h)
Definition: vf_aspect.c:78
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
parseutils.h
av_parse_ratio
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
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
eval.h
desc
const char * desc
Definition: nvenc.c:79
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:186
av_expr_parse_and_eval
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:776
var_name
var_name
Definition: aeval.c:46
AspectContext
Definition: vf_aspect.c:59
AspectContext::sar
AVRational sar
Definition: vf_aspect.c:62
internal.h
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:314
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_aspect.c:67
common.h
VARS_NB
@ VARS_NB
Definition: vf_aspect.c:56
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
AspectContext::max
int max
Definition: vf_aspect.c:63
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
w
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo ug o o w
Definition: fate.txt:150
OFFSET
#define OFFSET(x)
Definition: vf_aspect.c:75
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
VAR_H
@ VAR_H
Definition: vf_aspect.c:51
var_names
static const char *const var_names[]
Definition: vf_aspect.c:39
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
ff_vf_setdar
AVFilter ff_vf_setdar
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:227
VAR_W
@ VAR_W
Definition: vf_aspect.c:50