FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_aspect.c
Go to the documentation of this file.
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 "libavutil/common.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/parseutils.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "video.h"
33 
34 typedef struct {
35  const AVClass *class;
37  char *ratio_str;
38  int max;
40 
41 #define OFFSET(x) offsetof(AspectContext, x)
42 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
43 
44 static const AVOption options[] = {
45  {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
46  {"ratio", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
47  {"r", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
48  {NULL}
49 };
50 
51 static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
52 {
53  AspectContext *aspect = ctx->priv;
54  static const char *shorthand[] = { "ratio", "max", NULL };
55  char c;
56  int ret;
57  AVRational q;
58 
59  aspect->class = class;
60  av_opt_set_defaults(aspect);
61 
62  if (args && sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
63  aspect->ratio_str = av_strdup(args);
65  "num:den syntax is deprecated, please use num/den or named options instead\n");
66  } else if ((ret = av_opt_set_from_string(aspect, args, shorthand, "=", ":")) < 0) {
67  return ret;
68  }
69 
70  if (aspect->ratio_str) {
71  ret = av_parse_ratio(&aspect->ratio, aspect->ratio_str, aspect->max, 0, ctx);
72  if (ret < 0 || aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
73  av_log(ctx, AV_LOG_ERROR,
74  "Invalid string '%s' for aspect ratio\n", args);
75  return AVERROR(EINVAL);
76  }
77  }
78 
79  av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
80  return 0;
81 }
82 
84 {
85  AspectContext *aspect = link->dst->priv;
86 
87  frame->video->sample_aspect_ratio = aspect->ratio;
88  return ff_filter_frame(link->dst->outputs[0], frame);
89 }
90 
91 static av_cold void uninit(AVFilterContext *ctx)
92 {
93  AspectContext *aspect = ctx->priv;
94 
95  av_opt_free(aspect);
96 }
97 
98 #if CONFIG_SETDAR_FILTER
99 
100 #define setdar_options options
101 AVFILTER_DEFINE_CLASS(setdar);
102 
103 static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
104 {
105  return init(ctx, args, &setdar_class);
106 }
107 
108 static int setdar_config_props(AVFilterLink *inlink)
109 {
110  AspectContext *aspect = inlink->dst->priv;
111  AVRational dar = aspect->ratio;
112 
113  av_reduce(&aspect->ratio.num, &aspect->ratio.den,
114  aspect->ratio.num * inlink->h,
115  aspect->ratio.den * inlink->w, 100);
116 
117  av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
118  inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
119 
120  inlink->sample_aspect_ratio = aspect->ratio;
121 
122  return 0;
123 }
124 
125 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
126  {
127  .name = "default",
128  .type = AVMEDIA_TYPE_VIDEO,
129  .config_props = setdar_config_props,
130  .get_video_buffer = ff_null_get_video_buffer,
131  .filter_frame = filter_frame,
132  },
133  { NULL }
134 };
135 
136 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
137  {
138  .name = "default",
139  .type = AVMEDIA_TYPE_VIDEO,
140  },
141  { NULL }
142 };
143 
144 AVFilter avfilter_vf_setdar = {
145  .name = "setdar",
146  .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
147 
148  .init = setdar_init,
149  .uninit = uninit,
150 
151  .priv_size = sizeof(AspectContext),
152 
153  .inputs = avfilter_vf_setdar_inputs,
154 
155  .outputs = avfilter_vf_setdar_outputs,
156  .priv_class = &setdar_class,
157 };
158 
159 #endif /* CONFIG_SETDAR_FILTER */
160 
161 #if CONFIG_SETSAR_FILTER
162 
163 #define setsar_options options
164 AVFILTER_DEFINE_CLASS(setsar);
165 
166 static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
167 {
168  return init(ctx, args, &setsar_class);
169 }
170 
171 static int setsar_config_props(AVFilterLink *inlink)
172 {
173  AspectContext *aspect = inlink->dst->priv;
174 
175  inlink->sample_aspect_ratio = aspect->ratio;
176 
177  return 0;
178 }
179 
180 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
181  {
182  .name = "default",
183  .type = AVMEDIA_TYPE_VIDEO,
184  .config_props = setsar_config_props,
185  .get_video_buffer = ff_null_get_video_buffer,
186  .filter_frame = filter_frame,
187  },
188  { NULL }
189 };
190 
191 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
192  {
193  .name = "default",
194  .type = AVMEDIA_TYPE_VIDEO,
195  },
196  { NULL }
197 };
198 
199 AVFilter avfilter_vf_setsar = {
200  .name = "setsar",
201  .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
202 
203  .init = setsar_init,
204  .uninit = uninit,
205 
206  .priv_size = sizeof(AspectContext),
207 
208  .inputs = avfilter_vf_setsar_inputs,
209 
210  .outputs = avfilter_vf_setsar_outputs,
211  .priv_class = &setsar_class,
212 };
213 
214 #endif /* CONFIG_SETSAR_FILTER */