b453de70c55bfd9101a739fc87c32a6de59778a3
[platform/upstream/ffmpeg.git] / 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 "config_components.h"
27
28 #include <float.h>
29
30 #include "libavutil/common.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/parseutils.h"
35 #include "libavutil/pixdesc.h"
36
37 #include "avfilter.h"
38 #include "internal.h"
39 #include "video.h"
40
41 static const char *const var_names[] = {
42     "w",
43     "h",
44     "a",
45     "dar",
46     "sar",
47     "hsub",
48     "vsub",
49     NULL
50 };
51
52 enum var_name {
53     VAR_W,
54     VAR_H,
55     VAR_A,
56     VAR_DAR,
57     VAR_SAR,
58     VAR_HSUB,
59     VAR_VSUB,
60     VARS_NB
61 };
62
63 typedef struct AspectContext {
64     const AVClass *class;
65     AVRational dar;
66     AVRational sar;
67     int max;
68     char *ratio_expr;
69 } AspectContext;
70
71 static int filter_frame(AVFilterLink *link, AVFrame *frame)
72 {
73     AspectContext *s = link->dst->priv;
74
75     frame->sample_aspect_ratio = s->sar;
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 * (int64_t)w, sar.den * (int64_t)h, INT_MAX);
86     } else {
87         av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
88     }
89 }
90
91 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
92 {
93     AVFilterContext *ctx = inlink->dst;
94     AspectContext *s = inlink->dst->priv;
95     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
96     double var_values[VARS_NB], res;
97     int ret;
98
99     var_values[VAR_W]     = inlink->w;
100     var_values[VAR_H]     = inlink->h;
101     var_values[VAR_A]     = (double) inlink->w / inlink->h;
102     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
103         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
104     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
105     var_values[VAR_HSUB]  = 1 << desc->log2_chroma_w;
106     var_values[VAR_VSUB]  = 1 << desc->log2_chroma_h;
107
108     /* evaluate new aspect ratio*/
109     ret = av_expr_parse_and_eval(&res, s->ratio_expr,
110                                  var_names, var_values,
111                                  NULL, NULL, NULL, NULL, NULL, 0, ctx);
112     if (ret < 0) {
113         ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
114     } else
115         *aspect_ratio = av_d2q(res, s->max);
116
117     if (ret < 0) {
118         av_log(ctx, AV_LOG_ERROR,
119                "Error when evaluating the expression '%s'\n", s->ratio_expr);
120         return ret;
121     }
122     if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
123         av_log(ctx, AV_LOG_ERROR,
124                 "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
125         return AVERROR(EINVAL);
126     }
127     return 0;
128 }
129
130 #if CONFIG_SETDAR_FILTER
131
132 static int setdar_config_props(AVFilterLink *outlink)
133 {
134     AVFilterContext *ctx = outlink->src;
135     AVFilterLink *inlink = ctx->inputs[0];
136     AspectContext *s = ctx->priv;
137     AVRational dar;
138     AVRational old_dar;
139     AVRational old_sar = inlink->sample_aspect_ratio;
140     int ret;
141
142     if ((ret = get_aspect_ratio(inlink, &s->dar)))
143         return ret;
144
145     if (s->dar.num && s->dar.den) {
146         av_reduce(&s->sar.num, &s->sar.den,
147                    s->dar.num * inlink->h,
148                    s->dar.den * inlink->w, INT_MAX);
149         outlink->sample_aspect_ratio = s->sar;
150         dar = s->dar;
151     } else {
152         outlink->sample_aspect_ratio = (AVRational){ 1, 1 };
153         dar = (AVRational){ inlink->w, inlink->h };
154     }
155
156     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
157     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
158            inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
159            dar.num, dar.den, outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
160
161     return 0;
162 }
163
164 static const AVOption setdar_options[] = {
165     { "dar",   "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
166     { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
167     { "r",     "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = 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(setdar);
173
174 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
175     {
176         .name         = "default",
177         .type         = AVMEDIA_TYPE_VIDEO,
178         .filter_frame = filter_frame,
179     },
180 };
181
182 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
183     {
184         .name = "default",
185         .type = AVMEDIA_TYPE_VIDEO,
186         .config_props = setdar_config_props,
187     },
188 };
189
190 const AVFilter ff_vf_setdar = {
191     .name        = "setdar",
192     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
193     .priv_size   = sizeof(AspectContext),
194     .priv_class  = &setdar_class,
195     .flags       = AVFILTER_FLAG_METADATA_ONLY,
196     FILTER_INPUTS(avfilter_vf_setdar_inputs),
197     FILTER_OUTPUTS(avfilter_vf_setdar_outputs),
198 };
199
200 #endif /* CONFIG_SETDAR_FILTER */
201
202 #if CONFIG_SETSAR_FILTER
203
204 static int setsar_config_props(AVFilterLink *outlink)
205 {
206     AVFilterContext *ctx = outlink->src;
207     AVFilterLink *inlink = ctx->inputs[0];
208     AspectContext *s = ctx->priv;
209     AVRational old_sar = inlink->sample_aspect_ratio;
210     AVRational old_dar, dar;
211     int ret;
212
213     if ((ret = get_aspect_ratio(inlink, &s->sar)))
214         return ret;
215
216     outlink->sample_aspect_ratio = s->sar;
217
218     compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
219     compute_dar(&dar, s->sar, inlink->w, inlink->h);
220     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
221            inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
222            outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den, dar.num, dar.den);
223
224     return 0;
225 }
226
227 static const AVOption setsar_options[] = {
228     { "sar",   "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
229     { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
230     { "r",     "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
231     { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
232     { NULL }
233 };
234
235 AVFILTER_DEFINE_CLASS(setsar);
236
237 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
238     {
239         .name         = "default",
240         .type         = AVMEDIA_TYPE_VIDEO,
241         .filter_frame = filter_frame,
242     },
243 };
244
245 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
246     {
247         .name = "default",
248         .type = AVMEDIA_TYPE_VIDEO,
249         .config_props = setsar_config_props,
250     },
251 };
252
253 const AVFilter ff_vf_setsar = {
254     .name        = "setsar",
255     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
256     .priv_size   = sizeof(AspectContext),
257     .priv_class  = &setsar_class,
258     .flags       = AVFILTER_FLAG_METADATA_ONLY,
259     FILTER_INPUTS(avfilter_vf_setsar_inputs),
260     FILTER_OUTPUTS(avfilter_vf_setsar_outputs),
261 };
262
263 #endif /* CONFIG_SETSAR_FILTER */