Libav
vf_aspect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Bobby Bingham
3 
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
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  "PI",
41  "PHI",
42  "E",
43  "w",
44  "h",
45  "a", "dar",
46  "sar",
47  "hsub",
48  "vsub",
49  NULL
50 };
51 
52 enum var_name {
63 };
64 
65 typedef struct AspectContext {
66  const AVClass *class;
69 #if FF_API_OLD_FILTER_OPTS
71 #endif
72  char *ratio_expr;
74 
75 #if FF_API_OLD_FILTER_OPTS
76 static av_cold int init(AVFilterContext *ctx)
77 {
78  AspectContext *s = ctx->priv;
79 
80  if (s->aspect_num > 0 && s->aspect_den > 0) {
81  av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
82  "dar=<number> or dar=num/den.\n");
83  s->sar = s->dar = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
84  }
85 
86  return 0;
87 }
88 #endif
89 
90 static int filter_frame(AVFilterLink *link, AVFrame *frame)
91 {
92  AspectContext *s = link->dst->priv;
93 
94  frame->sample_aspect_ratio = s->sar;
95  return ff_filter_frame(link->dst->outputs[0], frame);
96 }
97 
98 #define OFFSET(x) offsetof(AspectContext, x)
99 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
100 
101 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
102 {
103  AVFilterContext *ctx = inlink->dst;
104  AspectContext *s = inlink->dst->priv;
105  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
106  double var_values[VARS_NB], res;
107  int ret;
108 
109  var_values[VAR_PI] = M_PI;
110  var_values[VAR_PHI] = M_PHI;
111  var_values[VAR_E] = M_E;
112  var_values[VAR_W] = inlink->w;
113  var_values[VAR_H] = inlink->h;
114  var_values[VAR_A] = (double) inlink->w / inlink->h;
115  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
116  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
117  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
118  var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
119  var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
120 
121  /* evaluate new aspect ratio*/
122  if ((ret = av_expr_parse_and_eval(&res, s->ratio_expr,
123  var_names, var_values,
124  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
126  "Error when evaluating the expression '%s'\n", s->ratio_expr);
127  return ret;
128  }
129  *aspect_ratio = av_d2q(res, INT_MAX);
130  return 0;
131 }
132 
133 #if CONFIG_SETDAR_FILTER
134 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
135 static int setdar_config_props(AVFilterLink *inlink)
136 {
137  AspectContext *s = inlink->dst->priv;
138  AVRational dar;
139  int ret;
140 
141 #if FF_API_OLD_FILTER_OPTS
142  if (!(s->aspect_num > 0 && s->aspect_den > 0)) {
143 #endif
144  if ((ret = get_aspect_ratio(inlink, &s->dar)))
145  return ret;
146 #if FF_API_OLD_FILTER_OPTS
147  }
148 #endif
149 
150  if (s->dar.num && s->dar.den) {
151  av_reduce(&s->sar.num, &s->sar.den,
152  s->dar.num * inlink->h,
153  s->dar.den * inlink->w, 100);
154  inlink->sample_aspect_ratio = s->sar;
155  dar = s->dar;
156  } else {
157  inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
158  dar = (AVRational){ inlink->w, inlink->h };
159  }
160 
161  av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
162  inlink->w, inlink->h, dar.num, dar.den,
164 
165  return 0;
166 }
167 
168 static const AVOption setdar_options[] = {
169 #if FF_API_OLD_FILTER_OPTS
170  { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
171  { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
172 #endif
173  { "dar", "display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
174  { NULL },
175 };
176 
177 static const AVClass setdar_class = {
178  .class_name = "setdar",
179  .item_name = av_default_item_name,
180  .option = setdar_options,
181  .version = LIBAVUTIL_VERSION_INT,
182 };
183 
184 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
185  {
186  .name = "default",
187  .type = AVMEDIA_TYPE_VIDEO,
188  .config_props = setdar_config_props,
189  .get_video_buffer = ff_null_get_video_buffer,
190  .filter_frame = filter_frame,
191  },
192  { NULL }
193 };
194 
195 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
196  {
197  .name = "default",
198  .type = AVMEDIA_TYPE_VIDEO,
199  },
200  { NULL }
201 };
202 
203 AVFilter ff_vf_setdar = {
204  .name = "setdar",
205  .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
206 
207 #if FF_API_OLD_FILTER_OPTS
208  .init = init,
209 #endif
210 
211  .priv_size = sizeof(AspectContext),
212  .priv_class = &setdar_class,
213 
214  .inputs = avfilter_vf_setdar_inputs,
215 
216  .outputs = avfilter_vf_setdar_outputs,
217 };
218 #endif /* CONFIG_SETDAR_FILTER */
219 
220 #if CONFIG_SETSAR_FILTER
221 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
222 static int setsar_config_props(AVFilterLink *inlink)
223 {
224  AspectContext *s = inlink->dst->priv;
225  int ret;
226 
227 #if FF_API_OLD_FILTER_OPTS
228  if (!(s->aspect_num > 0 && s->aspect_den > 0)) {
229 #endif
230  if ((ret = get_aspect_ratio(inlink, &s->sar)))
231  return ret;
232 #if FF_API_OLD_FILTER_OPTS
233  }
234 #endif
235 
236  inlink->sample_aspect_ratio = s->sar;
237 
238  return 0;
239 }
240 
241 static const AVOption setsar_options[] = {
242 #if FF_API_OLD_FILTER_OPTS
243  { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
244  { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
245 #endif
246  { "sar", "sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
247  { NULL },
248 };
249 
250 static const AVClass setsar_class = {
251  .class_name = "setsar",
252  .item_name = av_default_item_name,
253  .option = setsar_options,
254  .version = LIBAVUTIL_VERSION_INT,
255 };
256 
257 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
258  {
259  .name = "default",
260  .type = AVMEDIA_TYPE_VIDEO,
261  .config_props = setsar_config_props,
262  .get_video_buffer = ff_null_get_video_buffer,
263  .filter_frame = filter_frame,
264  },
265  { NULL }
266 };
267 
268 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
269  {
270  .name = "default",
271  .type = AVMEDIA_TYPE_VIDEO,
272  },
273  { NULL }
274 };
275 
276 AVFilter ff_vf_setsar = {
277  .name = "setsar",
278  .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
279 
280 #if FF_API_OLD_FILTER_OPTS
281  .init = init,
282 #endif
283 
284  .priv_size = sizeof(AspectContext),
285  .priv_class = &setsar_class,
286 
287  .inputs = avfilter_vf_setsar_inputs,
288 
289  .outputs = avfilter_vf_setsar_outputs,
290 };
291 #endif /* CONFIG_SETSAR_FILTER */
#define OFFSET(x)
Definition: vf_aspect.c:98
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
static const char *const var_names[]
Definition: vf_aspect.c:39
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
int num
numerator
Definition: rational.h:44
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:733
#define av_cold
Definition: attributes.h:66
static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
Definition: vf_aspect.c:101
AVOptions.
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
A filter pad used for either input or output.
Definition: internal.h:36
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:551
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
float aspect_den
Definition: vf_aspect.c:70
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void * priv
private data for use by the filter
Definition: avfilter.h:584
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static av_cold int init(AVFilterContext *ctx)
Definition: vf_aspect.c:76
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:105
#define M_E
Definition: ratecontrol.c:39
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
AVRational dar
Definition: vf_aspect.c:67
NULL
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVRational sar
Definition: vf_aspect.c:68
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:206
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
rational number numerator/denominator
Definition: rational.h:43
#define M_PHI
Definition: mathematics.h:34
const char * name
Filter name.
Definition: avfilter.h:425
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
#define FLAGS
Definition: vf_aspect.c:99
common internal and external API header
int den
denominator
Definition: rational.h:45
float aspect_num
Definition: vf_aspect.c:70
An instance of a filter.
Definition: avfilter.h:563
char * ratio_expr
Definition: vf_aspect.c:72
internal API functions
var_name
Definition: setpts.c:60
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_aspect.c:90
simple arithmetic expression evaluator