]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_aspect.c
lavfi: remove avfilter_null_* from public API on next bump.
[ffmpeg] / libavfilter / vf_aspect.c
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
21 /**
22  * @file
23  * aspect ratio modification video filters
24  */
25
26 #include "libavutil/mathematics.h"
27 #include "avfilter.h"
28 #include "video.h"
29
30 typedef struct {
31     AVRational aspect;
32 } AspectContext;
33
34 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
35 {
36     AspectContext *aspect = ctx->priv;
37     double  ratio;
38     int64_t gcd;
39     char c = 0;
40
41     if (args) {
42         if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
43             if (sscanf(args, "%lf%c", &ratio, &c) == 1)
44                 aspect->aspect = av_d2q(ratio, 100);
45
46         if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
47             av_log(ctx, AV_LOG_ERROR,
48                    "Invalid string '%s' for aspect ratio.\n", args);
49             return AVERROR(EINVAL);
50         }
51
52         gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
53         if (gcd) {
54             aspect->aspect.num /= gcd;
55             aspect->aspect.den /= gcd;
56         }
57     }
58
59     if (aspect->aspect.den == 0)
60         aspect->aspect = (AVRational) {0, 1};
61
62     av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
63     return 0;
64 }
65
66 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
67 {
68     AspectContext *aspect = link->dst->priv;
69
70     picref->video->pixel_aspect = aspect->aspect;
71     avfilter_start_frame(link->dst->outputs[0], picref);
72 }
73
74 #if CONFIG_SETDAR_FILTER
75 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
76 static int setdar_config_props(AVFilterLink *inlink)
77 {
78     AspectContext *aspect = inlink->dst->priv;
79     AVRational dar = aspect->aspect;
80
81     av_reduce(&aspect->aspect.num, &aspect->aspect.den,
82                aspect->aspect.num * inlink->h,
83                aspect->aspect.den * inlink->w, 100);
84
85     av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
86            inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
87
88     inlink->sample_aspect_ratio = aspect->aspect;
89
90     return 0;
91 }
92
93 AVFilter avfilter_vf_setdar = {
94     .name      = "setdar",
95     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
96
97     .init      = init,
98
99     .priv_size = sizeof(AspectContext),
100
101     .inputs    = (AVFilterPad[]) {{ .name             = "default",
102                                     .type             = AVMEDIA_TYPE_VIDEO,
103                                     .config_props     = setdar_config_props,
104                                     .get_video_buffer = ff_null_get_video_buffer,
105                                     .start_frame      = start_frame,
106                                     .end_frame        = ff_null_end_frame },
107                                   { .name = NULL}},
108
109     .outputs   = (AVFilterPad[]) {{ .name             = "default",
110                                     .type             = AVMEDIA_TYPE_VIDEO, },
111                                   { .name = NULL}},
112 };
113 #endif /* CONFIG_SETDAR_FILTER */
114
115 #if CONFIG_SETSAR_FILTER
116 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
117 static int setsar_config_props(AVFilterLink *inlink)
118 {
119     AspectContext *aspect = inlink->dst->priv;
120
121     inlink->sample_aspect_ratio = aspect->aspect;
122
123     return 0;
124 }
125
126 AVFilter avfilter_vf_setsar = {
127     .name      = "setsar",
128     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
129
130     .init      = init,
131
132     .priv_size = sizeof(AspectContext),
133
134     .inputs    = (AVFilterPad[]) {{ .name             = "default",
135                                     .type             = AVMEDIA_TYPE_VIDEO,
136                                     .config_props     = setsar_config_props,
137                                     .get_video_buffer = ff_null_get_video_buffer,
138                                     .start_frame      = start_frame,
139                                     .end_frame        = ff_null_end_frame },
140                                   { .name = NULL}},
141
142     .outputs   = (AVFilterPad[]) {{ .name             = "default",
143                                     .type             = AVMEDIA_TYPE_VIDEO, },
144                                   { .name = NULL}},
145 };
146 #endif /* CONFIG_SETSAR_FILTER */