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