]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_zscale.c
Merge commit '69a68593ce5684409c3c4dd9a901bfd8b16925b1'
[ffmpeg] / libavfilter / vf_zscale.c
1 /*
2  * Copyright (c) 2015 Paul B Mahol
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  * zscale video filter using z.lib library
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <zimg.h>
30
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/eval.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/avassert.h"
44
45 static const char *const var_names[] = {
46     "in_w",   "iw",
47     "in_h",   "ih",
48     "out_w",  "ow",
49     "out_h",  "oh",
50     "a",
51     "sar",
52     "dar",
53     "hsub",
54     "vsub",
55     "ohsub",
56     "ovsub",
57     NULL
58 };
59
60 enum var_name {
61     VAR_IN_W,   VAR_IW,
62     VAR_IN_H,   VAR_IH,
63     VAR_OUT_W,  VAR_OW,
64     VAR_OUT_H,  VAR_OH,
65     VAR_A,
66     VAR_SAR,
67     VAR_DAR,
68     VAR_HSUB,
69     VAR_VSUB,
70     VAR_OHSUB,
71     VAR_OVSUB,
72     VARS_NB
73 };
74
75 typedef struct ZScaleContext {
76     const AVClass *class;
77
78     /**
79      * New dimensions. Special values are:
80      *   0 = original width/height
81      *  -1 = keep original aspect
82      *  -N = try to keep aspect but make sure it is divisible by N
83      */
84     int w, h;
85     int dither;
86     int filter;
87     int colorspace;
88     int trc;
89     int primaries;
90     int range;
91     char *size_str;
92
93     char *w_expr;               ///< width  expression string
94     char *h_expr;               ///< height expression string
95
96     int out_h_chr_pos;
97     int out_v_chr_pos;
98     int in_h_chr_pos;
99     int in_v_chr_pos;
100
101     int force_original_aspect_ratio;
102
103     void *tmp;
104     size_t tmp_size;
105
106     zimg_image_format src_format, dst_format;
107     zimg_image_format alpha_src_format, alpha_dst_format;
108     zimg_graph_builder_params alpha_params, params;
109     zimg_filter_graph *alpha_graph, *graph;
110
111     enum AVColorSpace in_colorspace, out_colorspace;
112     enum AVColorTransferCharacteristic in_trc, out_trc;
113     enum AVColorPrimaries in_primaries, out_primaries;
114     enum AVColorRange in_range, out_range;
115 } ZScaleContext;
116
117 static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
118 {
119     ZScaleContext *s = ctx->priv;
120     int ret;
121
122     if (s->size_str && (s->w_expr || s->h_expr)) {
123         av_log(ctx, AV_LOG_ERROR,
124                "Size and width/height expressions cannot be set at the same time.\n");
125             return AVERROR(EINVAL);
126     }
127
128     if (s->w_expr && !s->h_expr)
129         FFSWAP(char *, s->w_expr, s->size_str);
130
131     if (s->size_str) {
132         char buf[32];
133         if ((ret = av_parse_video_size(&s->w, &s->h, s->size_str)) < 0) {
134             av_log(ctx, AV_LOG_ERROR,
135                    "Invalid size '%s'\n", s->size_str);
136             return ret;
137         }
138         snprintf(buf, sizeof(buf)-1, "%d", s->w);
139         av_opt_set(s, "w", buf, 0);
140         snprintf(buf, sizeof(buf)-1, "%d", s->h);
141         av_opt_set(s, "h", buf, 0);
142     }
143     if (!s->w_expr)
144         av_opt_set(s, "w", "iw", 0);
145     if (!s->h_expr)
146         av_opt_set(s, "h", "ih", 0);
147
148     return 0;
149 }
150
151 static int query_formats(AVFilterContext *ctx)
152 {
153     static const enum AVPixelFormat pixel_fmts[] = {
154         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
155         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
156         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
157         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
158         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
159         AV_PIX_FMT_YUVJ411P,
160         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
161         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
162         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
163         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
164         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
165         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
166         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
167         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
168         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
169         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
170         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
171         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
172         AV_PIX_FMT_NONE
173     };
174     int ret;
175
176     ret = ff_formats_ref(ff_make_format_list(pixel_fmts), &ctx->inputs[0]->out_formats);
177     if (ret < 0)
178         return ret;
179     return ff_formats_ref(ff_make_format_list(pixel_fmts), &ctx->outputs[0]->in_formats);
180 }
181
182 static int config_props(AVFilterLink *outlink)
183 {
184     AVFilterContext *ctx = outlink->src;
185     AVFilterLink *inlink = outlink->src->inputs[0];
186     ZScaleContext *s = ctx->priv;
187     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
188     const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
189     int64_t w, h;
190     double var_values[VARS_NB], res;
191     char *expr;
192     int ret;
193     int factor_w, factor_h;
194
195     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
196     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
197     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
198     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
199     var_values[VAR_A]     = (double) inlink->w / inlink->h;
200     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
201         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
202     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
203     var_values[VAR_HSUB]  = 1 << desc->log2_chroma_w;
204     var_values[VAR_VSUB]  = 1 << desc->log2_chroma_h;
205     var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
206     var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
207
208     /* evaluate width and height */
209     av_expr_parse_and_eval(&res, (expr = s->w_expr),
210                            var_names, var_values,
211                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
212     s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
213     if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
214                                       var_names, var_values,
215                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
216         goto fail;
217     s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
218     /* evaluate again the width, as it may depend on the output height */
219     if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
220                                       var_names, var_values,
221                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
222         goto fail;
223     s->w = res;
224
225     w = s->w;
226     h = s->h;
227
228     /* Check if it is requested that the result has to be divisible by a some
229      * factor (w or h = -n with n being the factor). */
230     factor_w = 1;
231     factor_h = 1;
232     if (w < -1) {
233         factor_w = -w;
234     }
235     if (h < -1) {
236         factor_h = -h;
237     }
238
239     if (w < 0 && h < 0)
240         s->w = s->h = 0;
241
242     if (!(w = s->w))
243         w = inlink->w;
244     if (!(h = s->h))
245         h = inlink->h;
246
247     /* Make sure that the result is divisible by the factor we determined
248      * earlier. If no factor was set, it is nothing will happen as the default
249      * factor is 1 */
250     if (w < 0)
251         w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
252     if (h < 0)
253         h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
254
255     /* Note that force_original_aspect_ratio may overwrite the previous set
256      * dimensions so that it is not divisible by the set factors anymore. */
257     if (s->force_original_aspect_ratio) {
258         int tmp_w = av_rescale(h, inlink->w, inlink->h);
259         int tmp_h = av_rescale(w, inlink->h, inlink->w);
260
261         if (s->force_original_aspect_ratio == 1) {
262              w = FFMIN(tmp_w, w);
263              h = FFMIN(tmp_h, h);
264         } else {
265              w = FFMAX(tmp_w, w);
266              h = FFMAX(tmp_h, h);
267         }
268     }
269
270     if (w > INT_MAX || h > INT_MAX ||
271         (h * inlink->w) > INT_MAX  ||
272         (w * inlink->h) > INT_MAX)
273         av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
274
275     outlink->w = w;
276     outlink->h = h;
277
278     if (inlink->w == outlink->w &&
279         inlink->h == outlink->h &&
280         inlink->format == outlink->format)
281         ;
282     else {
283     }
284
285     if (inlink->sample_aspect_ratio.num){
286         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
287     } else
288         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
289
290     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d\n",
291            inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
292            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
293            outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
294            outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
295     return 0;
296
297 fail:
298     av_log(ctx, AV_LOG_ERROR,
299            "Error when evaluating the expression '%s'.\n"
300            "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
301            expr, s->w_expr, s->h_expr);
302     return ret;
303 }
304
305 static int print_zimg_error(AVFilterContext *ctx)
306 {
307     char err_msg[1024];
308     int err_code = zimg_get_last_error(err_msg, sizeof(err_msg));
309
310     av_log(ctx, AV_LOG_ERROR, "code %d: %s\n", err_code, err_msg);
311
312     return err_code;
313 }
314
315 static int convert_matrix(enum AVColorSpace colorspace)
316 {
317     switch (colorspace) {
318     case AVCOL_SPC_RGB:
319         return ZIMG_MATRIX_RGB;
320     case AVCOL_SPC_BT709:
321         return ZIMG_MATRIX_709;
322     case AVCOL_SPC_UNSPECIFIED:
323         return ZIMG_MATRIX_UNSPECIFIED;
324     case AVCOL_SPC_BT470BG:
325         return ZIMG_MATRIX_470BG;
326     case AVCOL_SPC_SMPTE170M:
327         return ZIMG_MATRIX_170M;
328     case AVCOL_SPC_YCGCO:
329         return ZIMG_MATRIX_YCGCO;
330     case AVCOL_SPC_BT2020_NCL:
331         return ZIMG_MATRIX_2020_NCL;
332     case AVCOL_SPC_BT2020_CL:
333         return ZIMG_MATRIX_2020_CL;
334     }
335     return ZIMG_MATRIX_UNSPECIFIED;
336 }
337
338 static int convert_trc(enum AVColorTransferCharacteristic color_trc)
339 {
340     switch (color_trc) {
341     case AVCOL_TRC_UNSPECIFIED:
342         return ZIMG_TRANSFER_UNSPECIFIED;
343     case AVCOL_TRC_BT709:
344         return ZIMG_TRANSFER_709;
345     case AVCOL_TRC_SMPTE170M:
346         return ZIMG_TRANSFER_601;
347     case AVCOL_TRC_LINEAR:
348         return ZIMG_TRANSFER_LINEAR;
349     case AVCOL_TRC_BT2020_10:
350         return ZIMG_TRANSFER_2020_10;
351     case AVCOL_TRC_BT2020_12:
352         return ZIMG_TRANSFER_2020_12;
353     }
354     return ZIMG_TRANSFER_UNSPECIFIED;
355 }
356
357 static int convert_primaries(enum AVColorPrimaries color_primaries)
358 {
359     switch (color_primaries) {
360     case AVCOL_PRI_UNSPECIFIED:
361         return ZIMG_PRIMARIES_UNSPECIFIED;
362     case AVCOL_PRI_BT709:
363         return ZIMG_PRIMARIES_709;
364     case AVCOL_PRI_SMPTE170M:
365         return ZIMG_PRIMARIES_170M;
366     case AVCOL_PRI_SMPTE240M:
367         return ZIMG_PRIMARIES_240M;
368     case AVCOL_PRI_BT2020:
369         return ZIMG_PRIMARIES_2020;
370     }
371     return ZIMG_PRIMARIES_UNSPECIFIED;
372 }
373
374 static int convert_range(enum AVColorRange color_range)
375 {
376     switch (color_range) {
377     case AVCOL_RANGE_UNSPECIFIED:
378     case AVCOL_RANGE_MPEG:
379         return ZIMG_RANGE_LIMITED;
380     case AVCOL_RANGE_JPEG:
381         return ZIMG_RANGE_FULL;
382     }
383     return ZIMG_RANGE_LIMITED;
384 }
385
386 static int filter_frame(AVFilterLink *link, AVFrame *in)
387 {
388     ZScaleContext *s = link->dst->priv;
389     AVFilterLink *outlink = link->dst->outputs[0];
390     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
391     const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
392     zimg_image_buffer_const src_buf = { ZIMG_API_VERSION };
393     zimg_image_buffer dst_buf = { ZIMG_API_VERSION };
394     char buf[32];
395     size_t tmp_size;
396     int ret = 0, plane;
397     AVFrame *out;
398
399     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
400     if (!out) {
401         av_frame_free(&in);
402         return AVERROR(ENOMEM);
403     }
404
405     av_frame_copy_props(out, in);
406     out->width  = outlink->w;
407     out->height = outlink->h;
408
409     if(   in->width  != link->w
410        || in->height != link->h
411        || in->format != link->format
412        || s->in_colorspace != in->colorspace
413        || s->in_trc  != in->color_trc
414        || s->in_primaries != in->color_primaries
415        || s->in_range != in->color_range
416        || s->out_colorspace != out->colorspace
417        || s->out_trc  != out->color_trc
418        || s->out_primaries != out->color_primaries
419        || s->out_range != out->color_range) {
420         snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
421         av_opt_set(s, "w", buf, 0);
422         snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
423         av_opt_set(s, "h", buf, 0);
424
425         link->dst->inputs[0]->format = in->format;
426         link->dst->inputs[0]->w      = in->width;
427         link->dst->inputs[0]->h      = in->height;
428
429         if ((ret = config_props(outlink)) < 0) {
430             av_frame_free(&in);
431             av_frame_free(&out);
432             return ret;
433         }
434
435         zimg_image_format_default(&s->src_format, ZIMG_API_VERSION);
436         zimg_image_format_default(&s->dst_format, ZIMG_API_VERSION);
437         zimg_graph_builder_params_default(&s->params, ZIMG_API_VERSION);
438
439         s->params.dither_type = s->dither;
440         s->params.cpu_type = ZIMG_CPU_AUTO;
441         s->params.resample_filter = s->filter;
442         s->params.resample_filter_uv = s->filter;
443
444         s->src_format.width = in->width;
445         s->src_format.height = in->height;
446         s->src_format.subsample_w = desc->log2_chroma_w;
447         s->src_format.subsample_h = desc->log2_chroma_h;
448         s->src_format.depth = desc->comp[0].depth;
449         s->src_format.pixel_type = desc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
450         s->src_format.color_family = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_COLOR_RGB : ZIMG_COLOR_YUV;
451         s->src_format.matrix_coefficients = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_MATRIX_RGB : convert_matrix(in->colorspace);
452         s->src_format.transfer_characteristics = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_TRANSFER_UNSPECIFIED : convert_trc(in->color_trc);
453         s->src_format.color_primaries = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_PRIMARIES_UNSPECIFIED : convert_primaries(in->color_primaries);
454         s->src_format.pixel_range = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_RANGE_FULL : convert_range(in->color_range);
455
456         s->dst_format.width = out->width;
457         s->dst_format.height = out->height;
458         s->dst_format.subsample_w = odesc->log2_chroma_w;
459         s->dst_format.subsample_h = odesc->log2_chroma_h;
460         s->dst_format.depth = odesc->comp[0].depth;
461         s->dst_format.pixel_type = odesc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
462         s->dst_format.color_family = (odesc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_COLOR_RGB : ZIMG_COLOR_YUV;
463         s->dst_format.matrix_coefficients = (odesc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_MATRIX_RGB : s->colorspace == -1 ? convert_matrix(out->colorspace) : s->colorspace;
464         s->dst_format.transfer_characteristics = (odesc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_TRANSFER_UNSPECIFIED : s->trc == -1 ? convert_trc(out->color_trc) : s->trc;
465         s->dst_format.color_primaries = (odesc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_PRIMARIES_UNSPECIFIED : s->primaries == -1 ? convert_primaries(out->color_primaries) : s->primaries;
466         s->dst_format.pixel_range = (odesc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_RANGE_FULL : s->range == -1 ? convert_range(out->color_range) : s->range;
467
468         if (s->colorspace != -1)
469             out->colorspace = (int)s->dst_format.matrix_coefficients;
470
471         if (s->primaries != -1)
472             out->color_primaries = (int)s->dst_format.color_primaries;
473
474         if (s->range != -1)
475             out->color_range = (int)s->dst_format.pixel_range + 1;
476
477         if (s->trc != -1)
478             out->color_trc = (int)s->dst_format.transfer_characteristics;
479
480         zimg_filter_graph_free(s->graph);
481         s->graph = zimg_filter_graph_build(&s->src_format, &s->dst_format, &s->params);
482         if (!s->graph) {
483             ret = print_zimg_error(link->dst);
484             goto fail;
485         }
486
487         if ((ret = zimg_filter_graph_get_tmp_size(s->graph, &tmp_size))) {
488             ret = print_zimg_error(link->dst);
489             goto fail;
490         }
491
492         if (tmp_size > s->tmp_size) {
493             av_freep(&s->tmp);
494             s->tmp = av_malloc(tmp_size);
495             if (!s->tmp) {
496                 ret = AVERROR(ENOMEM);
497                 goto fail;
498             }
499             s->tmp_size = tmp_size;
500         }
501
502         s->in_colorspace  = in->colorspace;
503         s->in_trc         = in->color_trc;
504         s->in_primaries   = in->color_primaries;
505         s->in_range       = in->color_range;
506         s->out_colorspace = out->colorspace;
507         s->out_trc        = out->color_trc;
508         s->out_primaries  = out->color_primaries;
509         s->out_range      = out->color_range;
510
511         if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
512             zimg_image_format_default(&s->alpha_src_format, ZIMG_API_VERSION);
513             zimg_image_format_default(&s->alpha_dst_format, ZIMG_API_VERSION);
514             zimg_graph_builder_params_default(&s->alpha_params, ZIMG_API_VERSION);
515
516             s->alpha_params.dither_type = s->dither;
517             s->alpha_params.cpu_type = ZIMG_CPU_AUTO;
518             s->alpha_params.resample_filter = s->filter;
519
520             s->alpha_src_format.width = in->width;
521             s->alpha_src_format.height = in->height;
522             s->alpha_src_format.depth = desc->comp[0].depth;
523             s->alpha_src_format.pixel_type = desc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
524             s->alpha_src_format.color_family = ZIMG_COLOR_GREY;
525
526             s->alpha_dst_format.width = out->width;
527             s->alpha_dst_format.height = out->height;
528             s->alpha_dst_format.depth = odesc->comp[0].depth;
529             s->alpha_dst_format.pixel_type = odesc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
530             s->alpha_dst_format.color_family = ZIMG_COLOR_GREY;
531
532             zimg_filter_graph_free(s->alpha_graph);
533             s->alpha_graph = zimg_filter_graph_build(&s->alpha_src_format, &s->alpha_dst_format, &s->alpha_params);
534             if (!s->alpha_graph) {
535                 ret = print_zimg_error(link->dst);
536                 goto fail;
537             }
538         }
539     }
540
541     if (s->colorspace != -1)
542         out->colorspace = (int)s->dst_format.matrix_coefficients;
543
544     if (s->primaries != -1)
545         out->color_primaries = (int)s->dst_format.color_primaries;
546
547     if (s->range != -1)
548         out->color_range = (int)s->dst_format.pixel_range;
549
550     if (s->trc != -1)
551         out->color_trc = (int)s->dst_format.transfer_characteristics;
552
553     av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
554               (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
555               (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
556               INT_MAX);
557
558     for (plane = 0; plane < 3; plane++) {
559         int p = desc->comp[plane].plane;
560         src_buf.plane[plane].data   = in->data[p];
561         src_buf.plane[plane].stride = in->linesize[p];
562         src_buf.plane[plane].mask   = -1;
563
564         p = odesc->comp[plane].plane;
565         dst_buf.plane[plane].data   = out->data[p];
566         dst_buf.plane[plane].stride = out->linesize[p];
567         dst_buf.plane[plane].mask   = -1;
568     }
569
570     ret = zimg_filter_graph_process(s->graph, &src_buf, &dst_buf, s->tmp, 0, 0, 0, 0);
571     if (ret) {
572         print_zimg_error(link->dst);
573         goto fail;
574     }
575
576     if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
577         src_buf.plane[0].data   = in->data[3];
578         src_buf.plane[0].stride = in->linesize[3];
579         src_buf.plane[0].mask   = -1;
580
581         dst_buf.plane[0].data   = out->data[3];
582         dst_buf.plane[0].stride = out->linesize[3];
583         dst_buf.plane[0].mask   = -1;
584
585         ret = zimg_filter_graph_process(s->alpha_graph, &src_buf, &dst_buf, s->tmp, 0, 0, 0, 0);
586         if (ret) {
587             print_zimg_error(link->dst);
588             goto fail;
589         }
590     } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
591         int y;
592
593         for (y = 0; y < outlink->h; y++)
594             memset(out->data[3] + y * out->linesize[3], 0xff, outlink->w);
595     }
596
597 fail:
598     av_frame_free(&in);
599     if (ret) {
600         av_frame_free(&out);
601         return ret;
602     }
603
604     return ff_filter_frame(outlink, out);
605 }
606
607 static void uninit(AVFilterContext *ctx)
608 {
609     ZScaleContext *s = ctx->priv;
610
611     zimg_filter_graph_free(s->graph);
612     av_freep(&s->tmp);
613     s->tmp_size = 0;
614 }
615
616 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
617                            char *res, int res_len, int flags)
618 {
619     ZScaleContext *s = ctx->priv;
620     int ret;
621
622     if (   !strcmp(cmd, "width")  || !strcmp(cmd, "w")
623         || !strcmp(cmd, "height") || !strcmp(cmd, "h")) {
624
625         int old_w = s->w;
626         int old_h = s->h;
627         AVFilterLink *outlink = ctx->outputs[0];
628
629         av_opt_set(s, cmd, args, 0);
630         if ((ret = config_props(outlink)) < 0) {
631             s->w = old_w;
632             s->h = old_h;
633         }
634     } else
635         ret = AVERROR(ENOSYS);
636
637     return ret;
638 }
639
640 #define OFFSET(x) offsetof(ZScaleContext, x)
641 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
642
643 static const AVOption zscale_options[] = {
644     { "w",      "Output video width",  OFFSET(w_expr),    AV_OPT_TYPE_STRING, .flags = FLAGS },
645     { "width",  "Output video width",  OFFSET(w_expr),    AV_OPT_TYPE_STRING, .flags = FLAGS },
646     { "h",      "Output video height", OFFSET(h_expr),    AV_OPT_TYPE_STRING, .flags = FLAGS },
647     { "height", "Output video height", OFFSET(h_expr),    AV_OPT_TYPE_STRING, .flags = FLAGS },
648     { "size",   "set video size",      OFFSET(size_str),  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
649     { "s",      "set video size",      OFFSET(size_str),  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
650     { "dither", "set dither type",     OFFSET(dither),    AV_OPT_TYPE_INT, {.i64 = 0}, 0, ZIMG_DITHER_ERROR_DIFFUSION, FLAGS, "dither" },
651     { "d",      "set dither type",     OFFSET(dither),    AV_OPT_TYPE_INT, {.i64 = 0}, 0, ZIMG_DITHER_ERROR_DIFFUSION, FLAGS, "dither" },
652     {     "none",             0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_NONE},     0, 0, FLAGS, "dither" },
653     {     "ordered",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_ORDERED},  0, 0, FLAGS, "dither" },
654     {     "random",           0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_RANDOM},   0, 0, FLAGS, "dither" },
655     {     "error_diffusion",  0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_ERROR_DIFFUSION}, 0, 0, FLAGS, "dither" },
656     { "filter", "set filter type",     OFFSET(filter),    AV_OPT_TYPE_INT, {.i64 = ZIMG_RESIZE_BILINEAR}, 0, ZIMG_RESIZE_LANCZOS, FLAGS, "filter" },
657     { "f",      "set filter type",     OFFSET(filter),    AV_OPT_TYPE_INT, {.i64 = ZIMG_RESIZE_BILINEAR}, 0, ZIMG_RESIZE_LANCZOS, FLAGS, "filter" },
658     {     "point",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_POINT},    0, 0, FLAGS, "filter" },
659     {     "bilinear",         0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_BILINEAR}, 0, 0, FLAGS, "filter" },
660     {     "bicubic",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_BICUBIC},  0, 0, FLAGS, "filter" },
661     {     "spline16",         0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_SPLINE16}, 0, 0, FLAGS, "filter" },
662     {     "spline36",         0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_SPLINE36}, 0, 0, FLAGS, "filter" },
663     {     "lanczos",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_LANCZOS},  0, 0, FLAGS, "filter" },
664     { "range", "set color range",      OFFSET(range),     AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
665     { "r",     "set color range",      OFFSET(range),     AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
666     {     "input",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = -1},                 0, 0, FLAGS, "range" },
667     {     "limited",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RANGE_LIMITED}, 0, 0, FLAGS, "range" },
668     {     "full",             0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_RANGE_FULL},    0, 0, FLAGS, "range" },
669     { "primaries", "set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_PRIMARIES_2020, FLAGS, "primaries" },
670     { "p",         "set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_PRIMARIES_2020, FLAGS, "primaries" },
671     {     "input",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = -1},                         0, 0, FLAGS, "primaries" },
672     {     "709",              0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_709},         0, 0, FLAGS, "primaries" },
673     {     "unspecified",      0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_UNSPECIFIED}, 0, 0, FLAGS, "primaries" },
674     {     "170m",             0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_170M},        0, 0, FLAGS, "primaries" },
675     {     "240m",             0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_240M},        0, 0, FLAGS, "primaries" },
676     {     "2020",             0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_2020},        0, 0, FLAGS, "primaries" },
677     { "transfer", "set transfer characteristic", OFFSET(trc), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_TRANSFER_2020_12, FLAGS, "transfer" },
678     { "t",        "set transfer characteristic", OFFSET(trc), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_TRANSFER_2020_12, FLAGS, "transfer" },
679     {     "input",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = -1},                         0, 0, FLAGS, "transfer" },
680     {     "709",              0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_709},         0, 0, FLAGS, "transfer" },
681     {     "unspecified",      0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_UNSPECIFIED}, 0, 0, FLAGS, "transfer" },
682     {     "601",              0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_601},         0, 0, FLAGS, "transfer" },
683     {     "linear",           0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_LINEAR},      0, 0, FLAGS, "transfer" },
684     {     "2020_10",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_2020_10},     0, 0, FLAGS, "transfer" },
685     {     "2020_12",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_2020_12},     0, 0, FLAGS, "transfer" },
686     { "matrix", "set colorspace matrix", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_MATRIX_2020_CL, FLAGS, "matrix" },
687     { "m",      "set colorspace matrix", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_MATRIX_2020_CL, FLAGS, "matrix" },
688     {     "input",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = -1},                      0, 0, FLAGS, "matrix" },
689     {     "709",              0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_709},         0, 0, FLAGS, "matrix" },
690     {     "unspecified",      0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_UNSPECIFIED}, 0, 0, FLAGS, "matrix" },
691     {     "470bg",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_470BG},       0, 0, FLAGS, "matrix" },
692     {     "170m",             0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_170M},        0, 0, FLAGS, "matrix" },
693     {     "ycgco",            0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_YCGCO},       0, 0, FLAGS, "matrix" },
694     {     "2020_ncl",         0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_2020_NCL},    0, 0, FLAGS, "matrix" },
695     {     "2020_cl",          0,       0,                 AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_2020_CL},     0, 0, FLAGS, "matrix" },
696     { NULL }
697 };
698
699 static const AVClass zscale_class = {
700     .class_name       = "zscale",
701     .item_name        = av_default_item_name,
702     .option           = zscale_options,
703     .version          = LIBAVUTIL_VERSION_INT,
704     .category         = AV_CLASS_CATEGORY_FILTER,
705 };
706
707 static const AVFilterPad avfilter_vf_zscale_inputs[] = {
708     {
709         .name         = "default",
710         .type         = AVMEDIA_TYPE_VIDEO,
711         .filter_frame = filter_frame,
712     },
713     { NULL }
714 };
715
716 static const AVFilterPad avfilter_vf_zscale_outputs[] = {
717     {
718         .name         = "default",
719         .type         = AVMEDIA_TYPE_VIDEO,
720         .config_props = config_props,
721     },
722     { NULL }
723 };
724
725 AVFilter ff_vf_zscale = {
726     .name            = "zscale",
727     .description     = NULL_IF_CONFIG_SMALL("Apply resizing, colorspace and bit depth conversion."),
728     .init_dict       = init_dict,
729     .query_formats   = query_formats,
730     .priv_size       = sizeof(ZScaleContext),
731     .priv_class      = &zscale_class,
732     .uninit          = uninit,
733     .inputs          = avfilter_vf_zscale_inputs,
734     .outputs         = avfilter_vf_zscale_outputs,
735     .process_command = process_command,
736 };