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