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