]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_aspect.c
qpeg: fix an off by 1 error in the MV check
[ffmpeg] / libavfilter / vf_aspect.c
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
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 AspectContext {
66     const AVClass *class;
67     AVRational dar;
68     AVRational sar;
69 #if FF_API_OLD_FILTER_OPTS
70     float aspect_num, aspect_den;
71 #endif
72     char *ratio_expr;
73 } AspectContext;
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) {
125         av_log(NULL, AV_LOG_ERROR,
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,
163            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.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 */