FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_aspect.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 53 62 85.5%
Functions: 5 5 100.0%
Branches: 13 22 59.1%

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