]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_aspect.c
Merge remote-tracking branch 'cigaes/master'
[ffmpeg] / libavfilter / vf_aspect.c
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     "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 {
53     VAR_PI,
54     VAR_PHI,
55     VAR_E,
56     VAR_W,
57     VAR_H,
58     VAR_A, VAR_DAR,
59     VAR_SAR,
60     VAR_HSUB,
61     VAR_VSUB,
62     VARS_NB
63 };
64
65 typedef struct {
66     const AVClass *class;
67     AVRational dar;
68     AVRational sar;
69     int max;
70 #if FF_API_OLD_FILTER_OPTS
71     float aspect_den;
72 #endif
73     char *ratio_expr;
74 } AspectContext;
75
76 static av_cold int init(AVFilterContext *ctx)
77 {
78     AspectContext *s = ctx->priv;
79     int ret;
80
81 #if FF_API_OLD_FILTER_OPTS
82     if (s->ratio_expr && s->aspect_den > 0) {
83         double num;
84         av_log(ctx, AV_LOG_WARNING,
85                "num:den syntax is deprecated, please use num/den or named options instead\n");
86         ret = av_expr_parse_and_eval(&num, s->ratio_expr, NULL, NULL,
87                                      NULL, NULL, NULL, NULL, NULL, 0, ctx);
88         if (ret < 0) {
89             av_log(ctx, AV_LOG_ERROR, "Unable to parse ratio numerator \"%s\"\n", s->ratio_expr);
90             return AVERROR(EINVAL);
91         }
92         s->sar = s->dar = av_d2q(num / s->aspect_den, s->max);
93     }
94 #endif
95
96     return 0;
97 }
98
99 static int filter_frame(AVFilterLink *link, AVFrame *frame)
100 {
101     AspectContext *s = link->dst->priv;
102
103     frame->sample_aspect_ratio = s->sar;
104     return ff_filter_frame(link->dst->outputs[0], frame);
105 }
106
107 #define OFFSET(x) offsetof(AspectContext, x)
108 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
109
110 static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
111 {
112     if (sar.num && sar.den) {
113         av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
114     } else {
115         av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
116     }
117 }
118
119 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
120 {
121     AVFilterContext *ctx = inlink->dst;
122     AspectContext *s = inlink->dst->priv;
123     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
124     double var_values[VARS_NB], res;
125     int ret;
126
127     var_values[VAR_PI]    = M_PI;
128     var_values[VAR_PHI]   = M_PHI;
129     var_values[VAR_E]     = M_E;
130     var_values[VAR_W]     = inlink->w;
131     var_values[VAR_H]     = inlink->h;
132     var_values[VAR_A]     = (double) inlink->w / inlink->h;
133     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
134         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
135     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
136     var_values[VAR_HSUB]  = 1 << desc->log2_chroma_w;
137     var_values[VAR_VSUB]  = 1 << desc->log2_chroma_h;
138
139     /* evaluate new aspect ratio*/
140     ret = av_expr_parse_and_eval(&res, s->ratio_expr,
141                                       var_names, var_values,
142                                       NULL, NULL, NULL, NULL, NULL, 0, ctx);
143     if (ret < 0) {
144         ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
145     } else
146         *aspect_ratio = av_d2q(res, s->max);
147
148     if (ret < 0) {
149         av_log(ctx, AV_LOG_ERROR,
150                "Error when evaluating the expression '%s'\n", s->ratio_expr);
151         return ret;
152     }
153     if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
154         av_log(ctx, AV_LOG_ERROR,
155                 "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
156         return AVERROR(EINVAL);
157     }
158     return 0;
159 }
160
161 #if CONFIG_SETDAR_FILTER
162
163 static int setdar_config_props(AVFilterLink *inlink)
164 {
165     AspectContext *s = inlink->dst->priv;
166     AVRational dar;
167     AVRational old_dar;
168     AVRational old_sar = inlink->sample_aspect_ratio;
169     int ret;
170
171 #if FF_API_OLD_FILTER_OPTS
172     if (!(s->ratio_expr && s->aspect_den > 0)) {
173 #endif
174     if ((ret = get_aspect_ratio(inlink, &s->dar)))
175         return ret;
176 #if FF_API_OLD_FILTER_OPTS
177     }
178 #endif
179
180     if (s->dar.num && s->dar.den) {
181         av_reduce(&s->sar.num, &s->sar.den,
182                    s->dar.num * inlink->h,
183                    s->dar.den * inlink->w, INT_MAX);
184         inlink->sample_aspect_ratio = s->sar;
185         dar = s->dar;
186     } else {
187         inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
188         dar = (AVRational){ inlink->w, inlink->h };
189     }
190
191     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
192     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
193            inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
194            dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
195
196     return 0;
197 }
198
199 static const AVOption setdar_options[] = {
200     { "dar",   "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
201     { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
202     { "r",     "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
203 #if FF_API_OLD_FILTER_OPTS
204     { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
205 #endif
206     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
207     { NULL }
208 };
209
210 AVFILTER_DEFINE_CLASS(setdar);
211
212 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
213     {
214         .name         = "default",
215         .type         = AVMEDIA_TYPE_VIDEO,
216         .config_props = setdar_config_props,
217         .filter_frame = filter_frame,
218     },
219     { NULL }
220 };
221
222 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
223     {
224         .name = "default",
225         .type = AVMEDIA_TYPE_VIDEO,
226     },
227     { NULL }
228 };
229
230 AVFilter ff_vf_setdar = {
231     .name        = "setdar",
232     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
233     .init        = init,
234     .priv_size   = sizeof(AspectContext),
235     .priv_class  = &setdar_class,
236     .inputs      = avfilter_vf_setdar_inputs,
237     .outputs     = avfilter_vf_setdar_outputs,
238 };
239
240 #endif /* CONFIG_SETDAR_FILTER */
241
242 #if CONFIG_SETSAR_FILTER
243
244 static int setsar_config_props(AVFilterLink *inlink)
245 {
246     AspectContext *s = inlink->dst->priv;
247     AVRational old_sar = inlink->sample_aspect_ratio;
248     AVRational old_dar, dar;
249     int ret;
250
251 #if FF_API_OLD_FILTER_OPTS
252     if (!(s->ratio_expr && s->aspect_den > 0)) {
253 #endif
254     if ((ret = get_aspect_ratio(inlink, &s->sar)))
255         return ret;
256 #if FF_API_OLD_FILTER_OPTS
257     }
258 #endif
259
260     inlink->sample_aspect_ratio = s->sar;
261
262     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
263     compute_dar(&dar, s->sar, inlink->w, inlink->h);
264     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
265            inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
266            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
267
268     return 0;
269 }
270
271 static const AVOption setsar_options[] = {
272     { "sar",   "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
273     { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
274     { "r",     "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
275 #if FF_API_OLD_FILTER_OPTS
276     { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
277 #endif
278     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
279     { NULL }
280 };
281
282 AVFILTER_DEFINE_CLASS(setsar);
283
284 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
285     {
286         .name         = "default",
287         .type         = AVMEDIA_TYPE_VIDEO,
288         .config_props = setsar_config_props,
289         .filter_frame = filter_frame,
290     },
291     { NULL }
292 };
293
294 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
295     {
296         .name = "default",
297         .type = AVMEDIA_TYPE_VIDEO,
298     },
299     { NULL }
300 };
301
302 AVFilter ff_vf_setsar = {
303     .name        = "setsar",
304     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
305     .init        = init,
306     .priv_size   = sizeof(AspectContext),
307     .priv_class  = &setsar_class,
308     .inputs      = avfilter_vf_setsar_inputs,
309     .outputs     = avfilter_vf_setsar_outputs,
310 };
311
312 #endif /* CONFIG_SETSAR_FILTER */