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/mathematics.h"
27 #include "avfilter.h"
28 
29 typedef struct {
32 
33 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
34 {
35  AspectContext *aspect = ctx->priv;
36  double ratio;
37  int64_t gcd;
38  char c = 0;
39 
40  if (args) {
41  if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
42  if (sscanf(args, "%lf%c", &ratio, &c) == 1)
43  aspect->aspect = av_d2q(ratio, 100);
44 
45  if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
46  av_log(ctx, AV_LOG_ERROR,
47  "Invalid string '%s' for aspect ratio.\n", args);
48  return AVERROR(EINVAL);
49  }
50 
51  gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
52  if (gcd) {
53  aspect->aspect.num /= gcd;
54  aspect->aspect.den /= gcd;
55  }
56  }
57 
58  if (aspect->aspect.den == 0)
59  aspect->aspect = (AVRational) {0, 1};
60 
61  av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
62  return 0;
63 }
64 
65 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
66 {
67  AspectContext *aspect = link->dst->priv;
68 
69  picref->video->pixel_aspect = aspect->aspect;
70  avfilter_start_frame(link->dst->outputs[0], picref);
71 }
72 
73 #if CONFIG_SETDAR_FILTER
74 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
75 static int setdar_config_props(AVFilterLink *inlink)
76 {
77  AspectContext *aspect = inlink->dst->priv;
78  AVRational dar = aspect->aspect;
79 
80  av_reduce(&aspect->aspect.num, &aspect->aspect.den,
81  aspect->aspect.num * inlink->h,
82  aspect->aspect.den * inlink->w, 100);
83 
84  av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
85  inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
86 
87  inlink->sample_aspect_ratio = aspect->aspect;
88 
89  return 0;
90 }
91 
92 AVFilter avfilter_vf_setdar = {
93  .name = "setdar",
94  .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
95 
96  .init = init,
97 
98  .priv_size = sizeof(AspectContext),
99 
100  .inputs = (AVFilterPad[]) {{ .name = "default",
101  .type = AVMEDIA_TYPE_VIDEO,
102  .config_props = setdar_config_props,
103  .get_video_buffer = avfilter_null_get_video_buffer,
104  .start_frame = start_frame,
105  .end_frame = avfilter_null_end_frame },
106  { .name = NULL}},
107 
108  .outputs = (AVFilterPad[]) {{ .name = "default",
109  .type = AVMEDIA_TYPE_VIDEO, },
110  { .name = NULL}},
111 };
112 #endif /* CONFIG_SETDAR_FILTER */
113 
114 #if CONFIG_SETSAR_FILTER
115 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
116 static int setsar_config_props(AVFilterLink *inlink)
117 {
118  AspectContext *aspect = inlink->dst->priv;
119 
120  inlink->sample_aspect_ratio = aspect->aspect;
121 
122  return 0;
123 }
124 
125 AVFilter avfilter_vf_setsar = {
126  .name = "setsar",
127  .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
128 
129  .init = init,
130 
131  .priv_size = sizeof(AspectContext),
132 
133  .inputs = (AVFilterPad[]) {{ .name = "default",
134  .type = AVMEDIA_TYPE_VIDEO,
135  .config_props = setsar_config_props,
136  .get_video_buffer = avfilter_null_get_video_buffer,
137  .start_frame = start_frame,
138  .end_frame = avfilter_null_end_frame },
139  { .name = NULL}},
140 
141  .outputs = (AVFilterPad[]) {{ .name = "default",
142  .type = AVMEDIA_TYPE_VIDEO, },
143  { .name = NULL}},
144 };
145 #endif /* CONFIG_SETSAR_FILTER */
146 
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:141
int num
numerator
Definition: rational.h:44
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Definition: vf_aspect.c:65
void avfilter_null_end_frame(AVFilterLink *link)
end_frame() handler for filters which simply pass video along
Definition: defaults.c:278
#define av_cold
Definition: attributes.h:71
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:109
AVRational aspect
Definition: vf_aspect.c:30
A filter pad used for either input or output.
Definition: avfilter.h:312
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void * priv
private data for use by the filter
Definition: avfilter.h:553
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:71
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
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
#define FFABS(a)
Definition: common.h:50
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
Definition: vf_aspect.c:33
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
Filter definition.
Definition: avfilter.h:497
rational number numerator/denominator
Definition: rational.h:43
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
const char * name
filter name
Definition: avfilter.h:498
AVFilterBufferRef * avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
get_video_buffer() handler for filters which simply pass video along
Definition: defaults.c:288
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
int den
denominator
Definition: rational.h:45
An instance of a filter.
Definition: avfilter.h:538
#define AV_LOG_INFO
Definition: log.h:119