]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_aspect.c
lavfi/setdar: fix num/den swapping in log message
[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 static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
83 {
84     if (sar.num && sar.den) {
85         av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
86     } else {
87         av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
88     }
89 }
90
91 #if CONFIG_SETDAR_FILTER
92
93 static int setdar_config_props(AVFilterLink *inlink)
94 {
95     AspectContext *aspect = inlink->dst->priv;
96     AVRational dar = aspect->aspect, old_dar;
97     AVRational old_sar = inlink->sample_aspect_ratio;
98
99     if (aspect->aspect.num && aspect->aspect.den) {
100         av_reduce(&aspect->aspect.num, &aspect->aspect.den,
101                    aspect->aspect.num * inlink->h,
102                    aspect->aspect.den * inlink->w, INT_MAX);
103         inlink->sample_aspect_ratio = aspect->aspect;
104     } else {
105         inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
106         dar = (AVRational){ inlink->w, inlink->h };
107     }
108
109     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
110     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
111            inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
112            dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
113
114     return 0;
115 }
116
117 static const AVOption setdar_options[] = {
118 #if FF_API_OLD_FILTER_OPTS
119     { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
120     { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
121 #endif
122     { "dar",   "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
123     { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
124     { "r",     "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
125     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
126     { NULL }
127 };
128
129 AVFILTER_DEFINE_CLASS(setdar);
130
131 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
132     {
133         .name             = "default",
134         .type             = AVMEDIA_TYPE_VIDEO,
135         .config_props     = setdar_config_props,
136         .get_video_buffer = ff_null_get_video_buffer,
137         .filter_frame     = filter_frame,
138     },
139     { NULL }
140 };
141
142 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
143     {
144         .name = "default",
145         .type = AVMEDIA_TYPE_VIDEO,
146     },
147     { NULL }
148 };
149
150 AVFilter avfilter_vf_setdar = {
151     .name      = "setdar",
152     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
153     .init      = init,
154     .priv_size = sizeof(AspectContext),
155     .priv_class = &setdar_class,
156
157     .inputs    = avfilter_vf_setdar_inputs,
158
159     .outputs   = avfilter_vf_setdar_outputs,
160 };
161
162 #endif /* CONFIG_SETDAR_FILTER */
163
164 #if CONFIG_SETSAR_FILTER
165
166 static int setsar_config_props(AVFilterLink *inlink)
167 {
168     AspectContext *aspect = inlink->dst->priv;
169     AVRational old_sar = inlink->sample_aspect_ratio;
170     AVRational old_dar, dar;
171
172     inlink->sample_aspect_ratio = aspect->aspect;
173
174     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
175     compute_dar(&dar, aspect->aspect, inlink->w, inlink->h);
176     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
177            inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
178            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
179
180     return 0;
181 }
182
183 static const AVOption setsar_options[] = {
184 #if FF_API_OLD_FILTER_OPTS
185     { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
186     { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
187 #endif
188     { "sar",   "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
189     { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
190     { "r",     "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
191     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
192     { NULL }
193 };
194
195 AVFILTER_DEFINE_CLASS(setsar);
196
197 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
198     {
199         .name             = "default",
200         .type             = AVMEDIA_TYPE_VIDEO,
201         .config_props     = setsar_config_props,
202         .get_video_buffer = ff_null_get_video_buffer,
203         .filter_frame     = filter_frame,
204     },
205     { NULL }
206 };
207
208 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
209     {
210         .name = "default",
211         .type = AVMEDIA_TYPE_VIDEO,
212     },
213     { NULL }
214 };
215
216 AVFilter avfilter_vf_setsar = {
217     .name      = "setsar",
218     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
219     .init      = init,
220     .priv_size = sizeof(AspectContext),
221     .priv_class = &setsar_class,
222
223     .inputs    = avfilter_vf_setsar_inputs,
224
225     .outputs   = avfilter_vf_setsar_outputs,
226 };
227
228 #endif /* CONFIG_SETSAR_FILTER */