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