]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_aspect.c
lavfi/aspect: restore ratio parsing
[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/opt.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33
34 #include "avfilter.h"
35 #include "internal.h"
36 #include "video.h"
37
38 typedef struct {
39     const AVClass *class;
40     AVRational aspect;
41     int max;
42 #if FF_API_OLD_FILTER_OPTS
43     float aspect_num, aspect_den;
44 #endif
45     char *ratio_str;
46 } AspectContext;
47
48 static av_cold int init(AVFilterContext *ctx)
49 {
50     AspectContext *s = ctx->priv;
51     int ret;
52
53 #if FF_API_OLD_FILTER_OPTS
54     if (s->aspect_num > 0 && s->aspect_den > 0) {
55         av_log(ctx, AV_LOG_WARNING,
56                "num:den syntax is deprecated, please use num/den or named options instead\n");
57         s->aspect = av_d2q(s->aspect_num / s->aspect_den, s->max);
58     }
59 #endif
60     if (s->ratio_str) {
61         ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
62         if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
63             av_log(ctx, AV_LOG_ERROR,
64                    "Invalid string '%s' for aspect ratio\n", s->ratio_str);
65             return AVERROR(EINVAL);
66         }
67     }
68     return 0;
69 }
70
71 static int filter_frame(AVFilterLink *link, AVFrame *frame)
72 {
73     AspectContext *aspect = link->dst->priv;
74
75     frame->sample_aspect_ratio = aspect->aspect;
76     return ff_filter_frame(link->dst->outputs[0], frame);
77 }
78
79 #define OFFSET(x) offsetof(AspectContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81
82 #if CONFIG_SETDAR_FILTER
83
84 static int setdar_config_props(AVFilterLink *inlink)
85 {
86     AspectContext *aspect = inlink->dst->priv;
87     AVRational dar = aspect->aspect;
88
89     if (aspect->aspect.num && aspect->aspect.den) {
90         av_reduce(&aspect->aspect.num, &aspect->aspect.den,
91                    aspect->aspect.num * inlink->h,
92                    aspect->aspect.den * inlink->w, INT_MAX);
93         inlink->sample_aspect_ratio = aspect->aspect;
94     } else {
95         inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
96         dar = (AVRational){ inlink->w, inlink->h };
97     }
98
99     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
100            inlink->w, inlink->h, dar.num, dar.den,
101            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
102
103     return 0;
104 }
105
106 static const AVOption setdar_options[] = {
107 #if FF_API_OLD_FILTER_OPTS
108     { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
109     { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
110 #endif
111     { "dar",   "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
112     { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
113     { "r",     "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
114     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
115     { NULL }
116 };
117
118 AVFILTER_DEFINE_CLASS(setdar);
119
120 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
121     {
122         .name             = "default",
123         .type             = AVMEDIA_TYPE_VIDEO,
124         .config_props     = setdar_config_props,
125         .get_video_buffer = ff_null_get_video_buffer,
126         .filter_frame     = filter_frame,
127     },
128     { NULL }
129 };
130
131 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
132     {
133         .name = "default",
134         .type = AVMEDIA_TYPE_VIDEO,
135     },
136     { NULL }
137 };
138
139 AVFilter avfilter_vf_setdar = {
140     .name      = "setdar",
141     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
142     .init      = init,
143     .priv_size = sizeof(AspectContext),
144     .priv_class = &setdar_class,
145
146     .inputs    = avfilter_vf_setdar_inputs,
147
148     .outputs   = avfilter_vf_setdar_outputs,
149 };
150
151 #endif /* CONFIG_SETDAR_FILTER */
152
153 #if CONFIG_SETSAR_FILTER
154
155
156 static int setsar_config_props(AVFilterLink *inlink)
157 {
158     AspectContext *aspect = inlink->dst->priv;
159
160     inlink->sample_aspect_ratio = aspect->aspect;
161
162     return 0;
163 }
164
165 static const AVOption setsar_options[] = {
166 #if FF_API_OLD_FILTER_OPTS
167     { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
168     { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
169 #endif
170     { "sar",   "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
171     { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
172     { "r",     "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
173     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
174     { NULL }
175 };
176
177 AVFILTER_DEFINE_CLASS(setsar);
178
179 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
180     {
181         .name             = "default",
182         .type             = AVMEDIA_TYPE_VIDEO,
183         .config_props     = setsar_config_props,
184         .get_video_buffer = ff_null_get_video_buffer,
185         .filter_frame     = filter_frame,
186     },
187     { NULL }
188 };
189
190 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
191     {
192         .name = "default",
193         .type = AVMEDIA_TYPE_VIDEO,
194     },
195     { NULL }
196 };
197
198 AVFilter avfilter_vf_setsar = {
199     .name      = "setsar",
200     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
201
202 #if FF_API_OLD_FILTER_OPTS
203     .init      = init,
204 #endif
205
206     .priv_size = sizeof(AspectContext),
207     .priv_class = &setsar_class,
208
209     .inputs    = avfilter_vf_setsar_inputs,
210
211     .outputs   = avfilter_vf_setsar_outputs,
212 };
213
214 #endif /* CONFIG_SETSAR_FILTER */