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