h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_aspect.c
CommitLineData
3922deb5 1/*
3922deb5
BB
2 * Copyright (c) 2010 Bobby Bingham
3
2912e87a 4 * This file is part of Libav.
3922deb5 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
3922deb5
BB
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
3922deb5
BB
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
3922deb5
BB
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
ba87f080 22 * @file
cef4b74b 23 * aspect ratio modification video filters
3922deb5
BB
24 */
25
2831b307
AK
26#include <float.h>
27
1d9c2dc8 28#include "libavutil/common.h"
34a0ac41 29#include "libavutil/eval.h"
0ebcdf5c 30#include "libavutil/mathematics.h"
2831b307 31#include "libavutil/opt.h"
34a0ac41
RP
32#include "libavutil/parseutils.h"
33#include "libavutil/pixdesc.h"
2831b307 34
3922deb5 35#include "avfilter.h"
9d0bfc50 36#include "internal.h"
c04c533f 37#include "video.h"
3922deb5 38
34a0ac41
RP
39static 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
52enum 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
58400ac1 65typedef struct AspectContext {
2831b307 66 const AVClass *class;
d5c66d9c
AK
67 AVRational dar;
68 AVRational sar;
34a0ac41 69 char *ratio_expr;
3922deb5
BB
70} AspectContext;
71
7e350379 72static int filter_frame(AVFilterLink *link, AVFrame *frame)
3922deb5 73{
b3ea7662 74 AspectContext *s = link->dst->priv;
3922deb5 75
d5c66d9c 76 frame->sample_aspect_ratio = s->sar;
565e4993 77 return ff_filter_frame(link->dst->outputs[0], frame);
3922deb5
BB
78}
79
2831b307
AK
80#define OFFSET(x) offsetof(AspectContext, x)
81#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
82
34a0ac41
RP
83static 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
2fb21bf4
SS
115#if CONFIG_SETDAR_FILTER
116/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
117static int setdar_config_props(AVFilterLink *inlink)
3922deb5 118{
b3ea7662 119 AspectContext *s = inlink->dst->priv;
d5c66d9c 120 AVRational dar;
34a0ac41
RP
121 int ret;
122
34a0ac41
RP
123 if ((ret = get_aspect_ratio(inlink, &s->dar)))
124 return ret;
d5c66d9c
AK
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;
2831b307
AK
132 } else {
133 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
134 dar = (AVRational){ inlink->w, inlink->h };
135 }
3922deb5 136
1a49a169 137 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
2831b307
AK
138 inlink->w, inlink->h, dar.num, dar.den,
139 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
910b5b82 140
3922deb5
BB
141 return 0;
142}
143
2831b307 144static const AVOption setdar_options[] = {
34a0ac41 145 { "dar", "display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
2831b307
AK
146 { NULL },
147};
148
149static 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
568c70e7
MR
156static 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,
565e4993 162 .filter_frame = filter_frame,
568c70e7
MR
163 },
164 { NULL }
165};
166
167static const AVFilterPad avfilter_vf_setdar_outputs[] = {
168 {
169 .name = "default",
170 .type = AVMEDIA_TYPE_VIDEO,
171 },
172 { NULL }
173};
174
cd43ca04 175AVFilter ff_vf_setdar = {
2fb21bf4
SS
176 .name = "setdar",
177 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
3922deb5 178
3922deb5 179 .priv_size = sizeof(AspectContext),
2831b307 180 .priv_class = &setdar_class,
3922deb5 181
568c70e7
MR
182 .inputs = avfilter_vf_setdar_inputs,
183
184 .outputs = avfilter_vf_setdar_outputs,
3922deb5 185};
2fb21bf4 186#endif /* CONFIG_SETDAR_FILTER */
3922deb5 187
2fb21bf4 188#if CONFIG_SETSAR_FILTER
910b5b82
MN
189/* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
190static int setsar_config_props(AVFilterLink *inlink)
191{
b3ea7662 192 AspectContext *s = inlink->dst->priv;
34a0ac41
RP
193 int ret;
194
34a0ac41
RP
195 if ((ret = get_aspect_ratio(inlink, &s->sar)))
196 return ret;
910b5b82 197
d5c66d9c 198 inlink->sample_aspect_ratio = s->sar;
910b5b82
MN
199
200 return 0;
201}
202
2831b307 203static const AVOption setsar_options[] = {
34a0ac41 204 { "sar", "sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
2831b307
AK
205 { NULL },
206};
207
208static 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
568c70e7
MR
215static 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,
565e4993 221 .filter_frame = filter_frame,
568c70e7
MR
222 },
223 { NULL }
224};
225
226static const AVFilterPad avfilter_vf_setsar_outputs[] = {
227 {
228 .name = "default",
229 .type = AVMEDIA_TYPE_VIDEO,
230 },
231 { NULL }
232};
233
cd43ca04 234AVFilter ff_vf_setsar = {
2fb21bf4
SS
235 .name = "setsar",
236 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
3922deb5 237
3922deb5 238 .priv_size = sizeof(AspectContext),
2831b307 239 .priv_class = &setsar_class,
3922deb5 240
568c70e7
MR
241 .inputs = avfilter_vf_setsar_inputs,
242
243 .outputs = avfilter_vf_setsar_outputs,
3922deb5 244};
2fb21bf4 245#endif /* CONFIG_SETSAR_FILTER */