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