Commit | Line | Data |
---|---|---|
c3eabb7d SS |
1 | /* |
2 | * copyright (c) 2007 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_scale.c | |
23 | * scale video filter | |
24 | */ | |
25 | ||
26 | #include "avfilter.h" | |
b4b66456 | 27 | #include "libavutil/pixdesc.h" |
c3eabb7d SS |
28 | #include "libswscale/swscale.h" |
29 | ||
30 | typedef struct { | |
31 | struct SwsContext *sws; ///< software scaler context | |
32 | ||
33 | /** | |
34 | * New dimensions. Special values are: | |
35 | * 0 = original width/height | |
36 | * -1 = keep original aspect | |
37 | */ | |
38 | int w, h; | |
39 | ||
40 | int hsub, vsub; ///< chroma subsampling | |
41 | int slice_y; ///< top of current output slice | |
c3eabb7d SS |
42 | int input_is_pal; ///< set to 1 if the input format is paletted |
43 | } ScaleContext; | |
44 | ||
45 | static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) | |
46 | { | |
47 | ScaleContext *scale = ctx->priv; | |
48 | ||
49 | if (args) | |
50 | sscanf(args, "%d:%d", &scale->w, &scale->h); | |
51 | ||
52 | /* sanity check params */ | |
53 | if (scale->w < -1 || scale->h < -1) { | |
54 | av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n"); | |
55 | return -1; | |
56 | } | |
57 | if (scale->w == -1 && scale->h == -1) | |
58 | scale->w = scale->h = 0; | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
63 | static av_cold void uninit(AVFilterContext *ctx) | |
64 | { | |
65 | ScaleContext *scale = ctx->priv; | |
66 | sws_freeContext(scale->sws); | |
67 | scale->sws = NULL; | |
68 | } | |
69 | ||
70 | static int query_formats(AVFilterContext *ctx) | |
71 | { | |
72 | AVFilterFormats *formats; | |
27d8f6b6 SS |
73 | enum PixelFormat pix_fmt; |
74 | int ret; | |
c3eabb7d SS |
75 | |
76 | if (ctx->inputs[0]) { | |
27d8f6b6 SS |
77 | formats = NULL; |
78 | for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) | |
79 | if ( sws_isSupportedInput(pix_fmt) | |
80 | && (ret = avfilter_add_colorspace(&formats, pix_fmt)) < 0) { | |
81 | avfilter_formats_unref(&formats); | |
82 | return ret; | |
83 | } | |
c3eabb7d SS |
84 | avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats); |
85 | } | |
86 | if (ctx->outputs[0]) { | |
27d8f6b6 SS |
87 | formats = NULL; |
88 | for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) | |
89 | if ( sws_isSupportedOutput(pix_fmt) | |
90 | && (ret = avfilter_add_colorspace(&formats, pix_fmt)) < 0) { | |
91 | avfilter_formats_unref(&formats); | |
92 | return ret; | |
93 | } | |
c3eabb7d SS |
94 | avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats); |
95 | } | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | static int config_props(AVFilterLink *outlink) | |
101 | { | |
102 | AVFilterContext *ctx = outlink->src; | |
103 | AVFilterLink *inlink = outlink->src->inputs[0]; | |
104 | ScaleContext *scale = ctx->priv; | |
105 | int64_t w, h; | |
106 | ||
107 | if (!(w = scale->w)) | |
108 | w = inlink->w; | |
109 | if (!(h = scale->h)) | |
110 | h = inlink->h; | |
111 | if (w == -1) | |
112 | w = av_rescale(h, inlink->w, inlink->h); | |
113 | if (h == -1) | |
114 | h = av_rescale(w, inlink->h, inlink->w); | |
115 | ||
116 | if (w > INT_MAX || h > INT_MAX || | |
117 | (h * inlink->w) > INT_MAX || | |
118 | (w * inlink->h) > INT_MAX) | |
119 | av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n"); | |
120 | ||
121 | outlink->w = w; | |
122 | outlink->h = h; | |
123 | ||
124 | /* TODO: make algorithm configurable */ | |
125 | scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format, | |
126 | outlink->w, outlink->h, outlink->format, | |
127 | SWS_BILINEAR, NULL, NULL, NULL); | |
128 | ||
129 | av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s\n", | |
b4b66456 | 130 | outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name); |
c3eabb7d | 131 | |
d2a2b08c | 132 | scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL; |
c3eabb7d SS |
133 | |
134 | return !scale->sws; | |
135 | } | |
136 | ||
137 | static void start_frame(AVFilterLink *link, AVFilterPicRef *picref) | |
138 | { | |
139 | ScaleContext *scale = link->dst->priv; | |
140 | AVFilterLink *outlink = link->dst->outputs[0]; | |
141 | AVFilterPicRef *outpicref; | |
142 | ||
b4b66456 SS |
143 | scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w; |
144 | scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h; | |
b37aa4de | 145 | |
c3eabb7d SS |
146 | outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h); |
147 | outpicref->pts = picref->pts; | |
89fb9ae9 | 148 | outpicref->pos = picref->pos; |
c3eabb7d SS |
149 | outlink->outpic = outpicref; |
150 | ||
151 | av_reduce(&outpicref->pixel_aspect.num, &outpicref->pixel_aspect.den, | |
152 | (int64_t)picref->pixel_aspect.num * outlink->h * link->w, | |
153 | (int64_t)picref->pixel_aspect.den * outlink->w * link->h, | |
154 | INT_MAX); | |
155 | ||
a13a5437 | 156 | scale->slice_y = 0; |
c3eabb7d SS |
157 | avfilter_start_frame(outlink, avfilter_ref_pic(outpicref, ~0)); |
158 | } | |
159 | ||
a13a5437 | 160 | static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) |
c3eabb7d SS |
161 | { |
162 | ScaleContext *scale = link->dst->priv; | |
163 | int out_h; | |
164 | AVFilterPicRef *cur_pic = link->cur_pic; | |
165 | uint8_t *data[4]; | |
166 | ||
a13a5437 SS |
167 | if (scale->slice_y == 0 && slice_dir == -1) |
168 | scale->slice_y = link->dst->outputs[0]->h; | |
c3eabb7d SS |
169 | |
170 | data[0] = cur_pic->data[0] + y * cur_pic->linesize[0]; | |
171 | data[1] = scale->input_is_pal ? | |
172 | cur_pic->data[1] : | |
173 | cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1]; | |
174 | data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2]; | |
175 | data[3] = cur_pic->data[3] + y * cur_pic->linesize[3]; | |
176 | ||
177 | out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h, | |
178 | link->dst->outputs[0]->outpic->data, | |
179 | link->dst->outputs[0]->outpic->linesize); | |
180 | ||
a13a5437 | 181 | if (slice_dir == -1) |
c3eabb7d | 182 | scale->slice_y -= out_h; |
a13a5437 SS |
183 | avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir); |
184 | if (slice_dir == 1) | |
c3eabb7d SS |
185 | scale->slice_y += out_h; |
186 | } | |
187 | ||
188 | AVFilter avfilter_vf_scale = { | |
189 | .name = "scale", | |
190 | .description = "Scale the input video to width:height size and/or convert the image format.", | |
191 | ||
192 | .init = init, | |
193 | .uninit = uninit, | |
194 | ||
195 | .query_formats = query_formats, | |
196 | ||
197 | .priv_size = sizeof(ScaleContext), | |
198 | ||
199 | .inputs = (AVFilterPad[]) {{ .name = "default", | |
200 | .type = CODEC_TYPE_VIDEO, | |
201 | .start_frame = start_frame, | |
202 | .draw_slice = draw_slice, | |
203 | .min_perms = AV_PERM_READ, }, | |
204 | { .name = NULL}}, | |
205 | .outputs = (AVFilterPad[]) {{ .name = "default", | |
206 | .type = CODEC_TYPE_VIDEO, | |
207 | .config_props = config_props, }, | |
208 | { .name = NULL}}, | |
209 | }; |