]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
h264: (trivial) make ff_h264_lps_state static
[ffmpeg] / libavfilter / vf_scale.c
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
23  * scale video filter
24  */
25
26 #include "avfilter.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/avassert.h"
34 #include "libswscale/swscale.h"
35
36 static const char *const var_names[] = {
37     "in_w",   "iw",
38     "in_h",   "ih",
39     "out_w",  "ow",
40     "out_h",  "oh",
41     "a",
42     "sar",
43     "dar",
44     "hsub",
45     "vsub",
46     NULL
47 };
48
49 enum var_name {
50     VAR_IN_W,   VAR_IW,
51     VAR_IN_H,   VAR_IH,
52     VAR_OUT_W,  VAR_OW,
53     VAR_OUT_H,  VAR_OH,
54     VAR_A,
55     VAR_SAR,
56     VAR_DAR,
57     VAR_HSUB,
58     VAR_VSUB,
59     VARS_NB
60 };
61
62 typedef struct {
63     struct SwsContext *sws;     ///< software scaler context
64     struct SwsContext *isws[2]; ///< software scaler context for interlaced material
65
66     /**
67      * New dimensions. Special values are:
68      *   0 = original width/height
69      *  -1 = keep original aspect
70      */
71     int w, h;
72     unsigned int flags;         ///sws flags
73
74     int hsub, vsub;             ///< chroma subsampling
75     int slice_y;                ///< top of current output slice
76     int input_is_pal;           ///< set to 1 if the input format is paletted
77     int output_is_pal;          ///< set to 1 if the output format is paletted
78     int interlaced;
79
80     char w_expr[256];           ///< width  expression string
81     char h_expr[256];           ///< height expression string
82 } ScaleContext;
83
84 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
85 {
86     ScaleContext *scale = ctx->priv;
87     const char *p;
88
89     av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
90     av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
91
92     scale->flags = SWS_BILINEAR;
93     if (args) {
94         sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
95         p = strstr(args,"flags=");
96         if (p) {
97             const AVClass *class = sws_get_class();
98             const AVOption    *o = av_opt_find(&class, "sws_flags", NULL, 0,
99                                                AV_OPT_SEARCH_FAKE_OBJ);
100             int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
101
102             if (ret < 0)
103                 return ret;
104         }
105         if(strstr(args,"interl=1")){
106             scale->interlaced=1;
107         }else if(strstr(args,"interl=-1"))
108             scale->interlaced=-1;
109     }
110
111     return 0;
112 }
113
114 static av_cold void uninit(AVFilterContext *ctx)
115 {
116     ScaleContext *scale = ctx->priv;
117     sws_freeContext(scale->sws);
118     sws_freeContext(scale->isws[0]);
119     sws_freeContext(scale->isws[1]);
120     scale->sws = NULL;
121 }
122
123 static int query_formats(AVFilterContext *ctx)
124 {
125     AVFilterFormats *formats;
126     enum PixelFormat pix_fmt;
127     int ret;
128
129     if (ctx->inputs[0]) {
130         formats = NULL;
131         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
132             if (   sws_isSupportedInput(pix_fmt)
133                 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
134                 avfilter_formats_unref(&formats);
135                 return ret;
136             }
137         avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
138     }
139     if (ctx->outputs[0]) {
140         formats = NULL;
141         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
142             if (   (sws_isSupportedOutput(pix_fmt) || pix_fmt == PIX_FMT_PAL8)
143                 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
144                 avfilter_formats_unref(&formats);
145                 return ret;
146             }
147         avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
148     }
149
150     return 0;
151 }
152
153 static int config_props(AVFilterLink *outlink)
154 {
155     AVFilterContext *ctx = outlink->src;
156     AVFilterLink *inlink = outlink->src->inputs[0];
157     enum PixelFormat outfmt = outlink->format;
158     ScaleContext *scale = ctx->priv;
159     int64_t w, h;
160     double var_values[VARS_NB], res;
161     char *expr;
162     int ret;
163
164     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
165     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
166     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
167     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
168     var_values[VAR_A]     = (float) inlink->w / inlink->h;
169     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
170         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
171     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
172     var_values[VAR_HSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
173     var_values[VAR_VSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
174
175     /* evaluate width and height */
176     av_expr_parse_and_eval(&res, (expr = scale->w_expr),
177                            var_names, var_values,
178                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
179     scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
180     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
181                                       var_names, var_values,
182                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
183         goto fail;
184     scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
185     /* evaluate again the width, as it may depend on the output height */
186     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
187                                       var_names, var_values,
188                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189         goto fail;
190     scale->w = res;
191
192     w = scale->w;
193     h = scale->h;
194
195     /* sanity check params */
196     if (w <  -1 || h <  -1) {
197         av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
198         return AVERROR(EINVAL);
199     }
200     if (w == -1 && h == -1)
201         scale->w = scale->h = 0;
202
203     if (!(w = scale->w))
204         w = inlink->w;
205     if (!(h = scale->h))
206         h = inlink->h;
207     if (w == -1)
208         w = av_rescale(h, inlink->w, inlink->h);
209     if (h == -1)
210         h = av_rescale(w, inlink->h, inlink->w);
211
212     if (w > INT_MAX || h > INT_MAX ||
213         (h * inlink->w) > INT_MAX  ||
214         (w * inlink->h) > INT_MAX)
215         av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
216
217     outlink->w = w;
218     outlink->h = h;
219
220     /* TODO: make algorithm configurable */
221
222     scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
223                           av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
224     if (outfmt == PIX_FMT_PAL8) outfmt = PIX_FMT_BGR8;
225     scale->output_is_pal = av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PAL ||
226                            av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PSEUDOPAL;
227
228     if (scale->sws)
229         sws_freeContext(scale->sws);
230     if (inlink->w == outlink->w && inlink->h == outlink->h &&
231         inlink->format == outlink->format)
232         scale->sws = NULL;
233     else {
234         scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
235                                     outlink->w, outlink->h, outfmt,
236                                     scale->flags, NULL, NULL, NULL);
237         if (scale->isws[0])
238             sws_freeContext(scale->isws[0]);
239         scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
240                                         outlink->w, outlink->h/2, outfmt,
241                                         scale->flags, NULL, NULL, NULL);
242         if (scale->isws[1])
243             sws_freeContext(scale->isws[1]);
244         scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
245                                         outlink->w, outlink->h/2, outfmt,
246                                         scale->flags, NULL, NULL, NULL);
247         if (!scale->sws || !scale->isws[0] || !scale->isws[1])
248             return AVERROR(EINVAL);
249     }
250
251     if (inlink->sample_aspect_ratio.num){
252         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
253     } else
254         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
255
256     av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d flags:0x%0x\n",
257            inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
258            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
259            outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
260            outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
261            scale->flags);
262     return 0;
263
264 fail:
265     av_log(NULL, AV_LOG_ERROR,
266            "Error when evaluating the expression '%s'.\n"
267            "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
268            expr, scale->w_expr, scale->h_expr);
269     return ret;
270 }
271
272 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
273 {
274     ScaleContext *scale = link->dst->priv;
275     AVFilterLink *outlink = link->dst->outputs[0];
276     AVFilterBufferRef *outpicref;
277
278     if (!scale->sws) {
279         avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
280         return;
281     }
282
283     scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
284     scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
285
286     outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
287     avfilter_copy_buffer_ref_props(outpicref, picref);
288     outpicref->video->w = outlink->w;
289     outpicref->video->h = outlink->h;
290
291     outlink->out_buf = outpicref;
292     if(scale->output_is_pal)
293         ff_set_systematic_pal2(outpicref->data[1], outlink->format == PIX_FMT_PAL8 ? PIX_FMT_BGR8 : outlink->format);
294
295     av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
296               (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
297               (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
298               INT_MAX);
299
300     scale->slice_y = 0;
301     avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
302 }
303
304 static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
305 {
306     ScaleContext *scale = link->dst->priv;
307     AVFilterBufferRef *cur_pic = link->cur_buf;
308     AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
309     const uint8_t *in[4];
310     uint8_t *out[4];
311     int in_stride[4],out_stride[4];
312     int i;
313
314     for(i=0; i<4; i++){
315         int vsub= ((i+1)&2) ? scale->vsub : 0;
316          in_stride[i] = cur_pic->linesize[i] * mul;
317         out_stride[i] = out_buf->linesize[i] * mul;
318          in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
319         out[i] = out_buf->data[i] +            field  * out_buf->linesize[i];
320     }
321     if(scale->input_is_pal)
322          in[1] = cur_pic->data[1];
323     if(scale->output_is_pal)
324         out[1] = out_buf->data[1];
325
326     return sws_scale(sws, in, in_stride, y/mul, h,
327                          out,out_stride);
328 }
329
330 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
331 {
332     ScaleContext *scale = link->dst->priv;
333     int out_h;
334
335     if (!scale->sws) {
336         avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
337         return;
338     }
339
340     if (scale->slice_y == 0 && slice_dir == -1)
341         scale->slice_y = link->dst->outputs[0]->h;
342
343     if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
344         av_assert0(y%(2<<scale->vsub) == 0);
345         out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
346         out_h+= scale_slice(link, scale->isws[1], y,  h   /2, 2, 1);
347     }else{
348         out_h = scale_slice(link, scale->sws, y, h, 1, 0);
349     }
350
351     if (slice_dir == -1)
352         scale->slice_y -= out_h;
353     avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
354     if (slice_dir == 1)
355         scale->slice_y += out_h;
356 }
357
358 AVFilter avfilter_vf_scale = {
359     .name      = "scale",
360     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
361
362     .init      = init,
363     .uninit    = uninit,
364
365     .query_formats = query_formats,
366
367     .priv_size = sizeof(ScaleContext),
368
369     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
370                                     .type             = AVMEDIA_TYPE_VIDEO,
371                                     .start_frame      = start_frame,
372                                     .draw_slice       = draw_slice,
373                                     .min_perms        = AV_PERM_READ, },
374                                   { .name = NULL}},
375     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
376                                     .type             = AVMEDIA_TYPE_VIDEO,
377                                     .config_props     = config_props, },
378                                   { .name = NULL}},
379 };