h264_metadata: Add option to delete filler data
[libav.git] / 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/eval.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
34
35 #include "avfilter.h"
36 #include "internal.h"
37 #include "video.h"
38
39 static const char *const var_names[] = {
40 "PI",
41 "PHI",
42 "E",
43 "w",
44 "h",
45 "a", "dar",
46 "sar",
47 "hsub",
48 "vsub",
49 NULL
50 };
51
52 enum var_name {
53 VAR_PI,
54 VAR_PHI,
55 VAR_E,
56 VAR_W,
57 VAR_H,
58 VAR_A, VAR_DAR,
59 VAR_SAR,
60 VAR_HSUB,
61 VAR_VSUB,
62 VARS_NB
63 };
64
65 typedef struct AspectContext {
66 const AVClass *class;
67 AVRational dar;
68 AVRational sar;
69 char *ratio_expr;
70 } AspectContext;
71
72 static int filter_frame(AVFilterLink *link, AVFrame *frame)
73 {
74 AspectContext *s = link->dst->priv;
75
76 frame->sample_aspect_ratio = s->sar;
77 return ff_filter_frame(link->dst->outputs[0], frame);
78 }
79
80 #define OFFSET(x) offsetof(AspectContext, x)
81 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
82
83 static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
84 {
85 AVFilterContext *ctx = inlink->dst;
86 AspectContext *s = inlink->dst->priv;
87 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
88 double var_values[VARS_NB], res;
89 int ret;
90
91 var_values[VAR_PI] = M_PI;
92 var_values[VAR_PHI] = M_PHI;
93 var_values[VAR_E] = M_E;
94 var_values[VAR_W] = inlink->w;
95 var_values[VAR_H] = inlink->h;
96 var_values[VAR_A] = (double) inlink->w / inlink->h;
97 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
98 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
99 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
100 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
101 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
102
103 /* evaluate new aspect ratio*/
104 if ((ret = av_expr_parse_and_eval(&res, s->ratio_expr,
105 var_names, var_values,
106 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
107 av_log(NULL, AV_LOG_ERROR,
108 "Error when evaluating the expression '%s'\n", s->ratio_expr);
109 return ret;
110 }
111 *aspect_ratio = av_d2q(res, INT_MAX);
112 return 0;
113 }
114
115 #if CONFIG_SETDAR_FILTER
116 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
117 static int setdar_config_props(AVFilterLink *inlink)
118 {
119 AspectContext *s = inlink->dst->priv;
120 AVRational dar;
121 int ret;
122
123 if ((ret = get_aspect_ratio(inlink, &s->dar)))
124 return ret;
125
126 if (s->dar.num && s->dar.den) {
127 av_reduce(&s->sar.num, &s->sar.den,
128 s->dar.num * inlink->h,
129 s->dar.den * inlink->w, 100);
130 inlink->sample_aspect_ratio = s->sar;
131 dar = s->dar;
132 } else {
133 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
134 dar = (AVRational){ inlink->w, inlink->h };
135 }
136
137 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
138 inlink->w, inlink->h, dar.num, dar.den,
139 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
140
141 return 0;
142 }
143
144 static const AVOption setdar_options[] = {
145 { "dar", "display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
146 { NULL },
147 };
148
149 static const AVClass setdar_class = {
150 .class_name = "setdar",
151 .item_name = av_default_item_name,
152 .option = setdar_options,
153 .version = LIBAVUTIL_VERSION_INT,
154 };
155
156 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
157 {
158 .name = "default",
159 .type = AVMEDIA_TYPE_VIDEO,
160 .config_props = setdar_config_props,
161 .get_video_buffer = ff_null_get_video_buffer,
162 .filter_frame = filter_frame,
163 },
164 { NULL }
165 };
166
167 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
168 {
169 .name = "default",
170 .type = AVMEDIA_TYPE_VIDEO,
171 },
172 { NULL }
173 };
174
175 AVFilter ff_vf_setdar = {
176 .name = "setdar",
177 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
178
179 .priv_size = sizeof(AspectContext),
180 .priv_class = &setdar_class,
181
182 .inputs = avfilter_vf_setdar_inputs,
183
184 .outputs = avfilter_vf_setdar_outputs,
185 };
186 #endif /* CONFIG_SETDAR_FILTER */
187
188 #if CONFIG_SETSAR_FILTER
189 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
190 static int setsar_config_props(AVFilterLink *inlink)
191 {
192 AspectContext *s = inlink->dst->priv;
193 int ret;
194
195 if ((ret = get_aspect_ratio(inlink, &s->sar)))
196 return ret;
197
198 inlink->sample_aspect_ratio = s->sar;
199
200 return 0;
201 }
202
203 static const AVOption setsar_options[] = {
204 { "sar", "sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
205 { NULL },
206 };
207
208 static const AVClass setsar_class = {
209 .class_name = "setsar",
210 .item_name = av_default_item_name,
211 .option = setsar_options,
212 .version = LIBAVUTIL_VERSION_INT,
213 };
214
215 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
216 {
217 .name = "default",
218 .type = AVMEDIA_TYPE_VIDEO,
219 .config_props = setsar_config_props,
220 .get_video_buffer = ff_null_get_video_buffer,
221 .filter_frame = filter_frame,
222 },
223 { NULL }
224 };
225
226 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
227 {
228 .name = "default",
229 .type = AVMEDIA_TYPE_VIDEO,
230 },
231 { NULL }
232 };
233
234 AVFilter ff_vf_setsar = {
235 .name = "setsar",
236 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
237
238 .priv_size = sizeof(AspectContext),
239 .priv_class = &setsar_class,
240
241 .inputs = avfilter_vf_setsar_inputs,
242
243 .outputs = avfilter_vf_setsar_outputs,
244 };
245 #endif /* CONFIG_SETSAR_FILTER */