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