vf_aspect.c
Go to the documentation of this file.
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 
26 #include "libavutil/common.h"
27 #include "libavutil/mathematics.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "video.h"
31 
32 typedef struct {
35 
36 static av_cold int init(AVFilterContext *ctx, const char *args)
37 {
38  AspectContext *aspect = ctx->priv;
39  double ratio;
40  int64_t gcd;
41  char c = 0;
42 
43  if (args) {
44  if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
45  if (sscanf(args, "%lf%c", &ratio, &c) == 1)
46  aspect->aspect = av_d2q(ratio, 100);
47 
48  if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
49  av_log(ctx, AV_LOG_ERROR,
50  "Invalid string '%s' for aspect ratio.\n", args);
51  return AVERROR(EINVAL);
52  }
53 
54  gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
55  if (gcd) {
56  aspect->aspect.num /= gcd;
57  aspect->aspect.den /= gcd;
58  }
59  }
60 
61  if (aspect->aspect.den == 0)
62  aspect->aspect = (AVRational) {0, 1};
63 
64  av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
65  return 0;
66 }
67 
68 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
69 {
70  AspectContext *aspect = link->dst->priv;
71 
72  frame->video->pixel_aspect = aspect->aspect;
73  return ff_filter_frame(link->dst->outputs[0], frame);
74 }
75 
76 #if CONFIG_SETDAR_FILTER
77 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
78 static int setdar_config_props(AVFilterLink *inlink)
79 {
80  AspectContext *aspect = inlink->dst->priv;
81  AVRational dar = aspect->aspect;
82 
83  av_reduce(&aspect->aspect.num, &aspect->aspect.den,
84  aspect->aspect.num * inlink->h,
85  aspect->aspect.den * inlink->w, 100);
86 
87  av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
88  inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
89 
90  inlink->sample_aspect_ratio = aspect->aspect;
91 
92  return 0;
93 }
94 
95 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
96  {
97  .name = "default",
98  .type = AVMEDIA_TYPE_VIDEO,
99  .config_props = setdar_config_props,
100  .get_video_buffer = ff_null_get_video_buffer,
101  .filter_frame = filter_frame,
102  },
103  { NULL }
104 };
105 
106 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
107  {
108  .name = "default",
109  .type = AVMEDIA_TYPE_VIDEO,
110  },
111  { NULL }
112 };
113 
114 AVFilter avfilter_vf_setdar = {
115  .name = "setdar",
116  .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
117 
118  .init = init,
119 
120  .priv_size = sizeof(AspectContext),
121 
122  .inputs = avfilter_vf_setdar_inputs,
123 
124  .outputs = avfilter_vf_setdar_outputs,
125 };
126 #endif /* CONFIG_SETDAR_FILTER */
127 
128 #if CONFIG_SETSAR_FILTER
129 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
130 static int setsar_config_props(AVFilterLink *inlink)
131 {
132  AspectContext *aspect = inlink->dst->priv;
133 
134  inlink->sample_aspect_ratio = aspect->aspect;
135 
136  return 0;
137 }
138 
139 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
140  {
141  .name = "default",
142  .type = AVMEDIA_TYPE_VIDEO,
143  .config_props = setsar_config_props,
144  .get_video_buffer = ff_null_get_video_buffer,
145  .filter_frame = filter_frame,
146  },
147  { NULL }
148 };
149 
150 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
151  {
152  .name = "default",
153  .type = AVMEDIA_TYPE_VIDEO,
154  },
155  { NULL }
156 };
157 
158 AVFilter avfilter_vf_setsar = {
159  .name = "setsar",
160  .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
161 
162  .init = init,
163 
164  .priv_size = sizeof(AspectContext),
165 
166  .inputs = avfilter_vf_setsar_inputs,
167 
168  .outputs = avfilter_vf_setsar_outputs,
169 };
170 #endif /* CONFIG_SETSAR_FILTER */
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
int num
numerator
Definition: rational.h:44
const char * name
Pad name.
Definition: internal.h:39
static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Definition: vf_aspect.c:68
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:124
AVRational aspect
Definition: vf_aspect.c:33
A filter pad used for either input or output.
Definition: internal.h:33
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:53
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
struct AVRational AVRational
rational number numerator/denominator
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:372
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_aspect.c:36
common internal and external API header
int den
denominator
Definition: rational.h:45
An instance of a filter.
Definition: avfilter.h:418
internal API functions
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72