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