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