]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_aspect.c
ffserver: Fix off by 1 error in path
[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     "w",
41     "h",
42     "a", "dar",
43     "sar",
44     "hsub",
45     "vsub",
46     NULL
47 };
48
49 enum var_name {
50     VAR_W,
51     VAR_H,
52     VAR_A, VAR_DAR,
53     VAR_SAR,
54     VAR_HSUB,
55     VAR_VSUB,
56     VARS_NB
57 };
58
59 typedef struct AspectContext {
60     const AVClass *class;
61     AVRational dar;
62     AVRational sar;
63     int max;
64     char *ratio_expr;
65 } AspectContext;
66
67 static int filter_frame(AVFilterLink *link, AVFrame *frame)
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 * w, sar.den * 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;
91     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
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) {
114         av_log(ctx, AV_LOG_ERROR,
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) {
119         av_log(ctx, AV_LOG_ERROR,
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 *inlink)
129 {
130     AspectContext *s = inlink->dst->priv;
131     AVRational dar;
132     AVRational old_dar;
133     AVRational old_sar = inlink->sample_aspect_ratio;
134     int ret;
135
136     if ((ret = get_aspect_ratio(inlink, &s->dar)))
137         return ret;
138
139     if (s->dar.num && s->dar.den) {
140         av_reduce(&s->sar.num, &s->sar.den,
141                    s->dar.num * inlink->h,
142                    s->dar.den * inlink->w, INT_MAX);
143         inlink->sample_aspect_ratio = s->sar;
144         dar = s->dar;
145     } else {
146         inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
147         dar = (AVRational){ inlink->w, inlink->h };
148     }
149
150     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
151     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
152            inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
153            dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
154
155     return 0;
156 }
157
158 static const AVOption setdar_options[] = {
159     { "dar",   "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
160     { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
161     { "r",     "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
162     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
163     { NULL }
164 };
165
166 AVFILTER_DEFINE_CLASS(setdar);
167
168 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
169     {
170         .name         = "default",
171         .type         = AVMEDIA_TYPE_VIDEO,
172         .config_props = setdar_config_props,
173         .filter_frame = filter_frame,
174     },
175     { NULL }
176 };
177
178 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
179     {
180         .name = "default",
181         .type = AVMEDIA_TYPE_VIDEO,
182     },
183     { NULL }
184 };
185
186 AVFilter ff_vf_setdar = {
187     .name        = "setdar",
188     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
189     .priv_size   = sizeof(AspectContext),
190     .priv_class  = &setdar_class,
191     .inputs      = avfilter_vf_setdar_inputs,
192     .outputs     = avfilter_vf_setdar_outputs,
193 };
194
195 #endif /* CONFIG_SETDAR_FILTER */
196
197 #if CONFIG_SETSAR_FILTER
198
199 static int setsar_config_props(AVFilterLink *inlink)
200 {
201     AspectContext *s = inlink->dst->priv;
202     AVRational old_sar = inlink->sample_aspect_ratio;
203     AVRational old_dar, dar;
204     int ret;
205
206     if ((ret = get_aspect_ratio(inlink, &s->sar)))
207         return ret;
208
209     inlink->sample_aspect_ratio = s->sar;
210
211     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
212     compute_dar(&dar, s->sar, inlink->w, inlink->h);
213     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
214            inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
215            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
216
217     return 0;
218 }
219
220 static const AVOption setsar_options[] = {
221     { "sar",   "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
222     { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
223     { "r",     "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
224     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
225     { NULL }
226 };
227
228 AVFILTER_DEFINE_CLASS(setsar);
229
230 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
231     {
232         .name         = "default",
233         .type         = AVMEDIA_TYPE_VIDEO,
234         .config_props = setsar_config_props,
235         .filter_frame = filter_frame,
236     },
237     { NULL }
238 };
239
240 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
241     {
242         .name = "default",
243         .type = AVMEDIA_TYPE_VIDEO,
244     },
245     { NULL }
246 };
247
248 AVFilter ff_vf_setsar = {
249     .name        = "setsar",
250     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
251     .priv_size   = sizeof(AspectContext),
252     .priv_class  = &setsar_class,
253     .inputs      = avfilter_vf_setsar_inputs,
254     .outputs     = avfilter_vf_setsar_outputs,
255 };
256
257 #endif /* CONFIG_SETSAR_FILTER */