Browse code

Add video filter to manipulate aspect ratio

Originally committed as revision 22573 to svn://svn.ffmpeg.org/ffmpeg/trunk

Bobby Bingham authored on 2010/03/17 12:43:14
Showing 3 changed files
... ...
@@ -14,10 +14,12 @@ OBJS = allfilters.o                                                     \
14 14
        graphparser.o                                                    \
15 15
        parseutils.o                                                     \
16 16
 
17
+OBJS-$(CONFIG_ASPECT_FILTER)                 += vf_aspect.o
17 18
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
18 19
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
19 20
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
20 21
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
22
+OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
21 23
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
22 24
 OBJS-$(CONFIG_SLICIFY_FILTER)                += vf_slicify.o
23 25
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
... ...
@@ -34,15 +34,17 @@ void avfilter_register_all(void)
34 34
         return;
35 35
     initialized = 1;
36 36
 
37
+    REGISTER_FILTER (ASPECT, aspect, vf);
37 38
     REGISTER_FILTER (CROP, crop, vf);
38 39
     REGISTER_FILTER (FORMAT, format, vf);
39 40
     REGISTER_FILTER (NOFORMAT, noformat, vf);
40 41
     REGISTER_FILTER (NULL, null, vf);
42
+    REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
41 43
     REGISTER_FILTER (SCALE, scale, vf);
42 44
     REGISTER_FILTER (SLICIFY, slicify, vf);
43 45
     REGISTER_FILTER (VFLIP, vflip, vf);
44 46
 
45 47
     REGISTER_FILTER (NULLSRC, nullsrc, vsrc);
46 48
 
47
-    REGISTER_FILTER(NULLSINK, nullsink, vsink);
49
+    REGISTER_FILTER (NULLSINK, nullsink, vsink);
48 50
 }
49 51
new file mode 100644
... ...
@@ -0,0 +1,135 @@
0
+/*
1
+ * Aspect ratio modification video filter
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 libavfilter/vf_aspect.c
23
+ * aspect ratio modification video filter
24
+ */
25
+
26
+#include "avfilter.h"
27
+
28
+typedef struct {
29
+    AVRational aspect;
30
+} AspectContext;
31
+
32
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
33
+{
34
+    AspectContext *aspect = ctx->priv;
35
+    double  ratio;
36
+    int64_t gcd;
37
+
38
+    if(args) {
39
+        if(sscanf(args, "%d:%d", &aspect->aspect.num, &aspect->aspect.den) < 2) {
40
+            if(sscanf(args, "%lf", &ratio) < 1)
41
+                return -1;
42
+            aspect->aspect = av_d2q(ratio, 100);
43
+        } else {
44
+            gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
45
+            if(gcd) {
46
+                aspect->aspect.num /= gcd;
47
+                aspect->aspect.den /= gcd;
48
+            }
49
+        }
50
+    }
51
+
52
+    if(aspect->aspect.den == 0)
53
+        aspect->aspect = (AVRational) {0, 1};
54
+
55
+    return 0;
56
+}
57
+
58
+
59
+static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
60
+                                        int w, int h)
61
+{
62
+    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
63
+}
64
+
65
+static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
66
+{
67
+    AspectContext *aspect = link->dst->priv;
68
+
69
+    picref->pixel_aspect = aspect->aspect;
70
+    avfilter_start_frame(link->dst->outputs[0], picref);
71
+}
72
+
73
+static void end_frame(AVFilterLink *link)
74
+{
75
+    avfilter_end_frame(link->dst->outputs[0]);
76
+}
77
+
78
+#if CONFIG_ASPECT_FILTER
79
+/* for aspect filter, convert from frame aspect ratio to pixel aspect ratio */
80
+static int frameaspect_config_props(AVFilterLink *inlink)
81
+{
82
+    AspectContext *aspect = inlink->dst->priv;
83
+
84
+    av_reduce(&aspect->aspect.num, &aspect->aspect.den,
85
+               aspect->aspect.num * inlink->h,
86
+               aspect->aspect.den * inlink->w, 100);
87
+
88
+    return 0;
89
+}
90
+
91
+AVFilter avfilter_vf_aspect = {
92
+    .name      = "aspect",
93
+    .description = NULL_IF_CONFIG_SMALL("Set the frame aspect ratio."),
94
+
95
+    .init      = init,
96
+
97
+    .priv_size = sizeof(AspectContext),
98
+
99
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
100
+                                    .type             = CODEC_TYPE_VIDEO,
101
+                                    .config_props     = frameaspect_config_props,
102
+                                    .get_video_buffer = get_video_buffer,
103
+                                    .start_frame      = start_frame,
104
+                                    .end_frame        = end_frame },
105
+                                  { .name = NULL}},
106
+
107
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
108
+                                    .type             = CODEC_TYPE_VIDEO, },
109
+                                  { .name = NULL}},
110
+};
111
+#endif /* CONFIG_ASPECT_FILTER */
112
+
113
+#if CONFIG_PIXELASPECT_FILTER
114
+AVFilter avfilter_vf_pixelaspect = {
115
+    .name      = "pixelaspect",
116
+    .description = NULL_IF_CONFIG_SMALL("Set the pixel aspect ratio."),
117
+
118
+    .init      = init,
119
+
120
+    .priv_size = sizeof(AspectContext),
121
+
122
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
123
+                                    .type             = CODEC_TYPE_VIDEO,
124
+                                    .get_video_buffer = get_video_buffer,
125
+                                    .start_frame      = start_frame,
126
+                                    .end_frame        = end_frame },
127
+                                  { .name = NULL}},
128
+
129
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
130
+                                    .type             = CODEC_TYPE_VIDEO, },
131
+                                  { .name = NULL}},
132
+};
133
+#endif /* CONFIG_PIXELASPECT_FILTER */
134
+