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 <float.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <zimg.h>
31
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/eval.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/imgutils.h"
45 #include "libavutil/avassert.h"
46
47 #define ZIMG_ALIGNMENT 32
48
49 static const char *const var_names[] = {
50 "in_w", "iw",
51 "in_h", "ih",
52 "out_w", "ow",
53 "out_h", "oh",
54 "a",
55 "sar",
56 "dar",
57 "hsub",
58 "vsub",
59 "ohsub",
60 "ovsub",
61 NULL
62 };
63
64 enum var_name {
65 VAR_IN_W, VAR_IW,
66 VAR_IN_H, VAR_IH,
67 VAR_OUT_W, VAR_OW,
68 VAR_OUT_H, VAR_OH,
69 VAR_A,
70 VAR_SAR,
71 VAR_DAR,
72 VAR_HSUB,
73 VAR_VSUB,
74 VAR_OHSUB,
75 VAR_OVSUB,
76 VARS_NB
77 };
78
79 typedef struct ZScaleContext {
80 const AVClass *class;
81
82 /**
83 * New dimensions. Special values are:
84 * 0 = original width/height
85 * -1 = keep original aspect
86 * -N = try to keep aspect but make sure it is divisible by N
87 */
88 int w, h;
89 int dither;
90 int filter;
91 int colorspace;
92 int trc;
93 int primaries;
94 int range;
95 int chromal;
96 int colorspace_in;
97 int trc_in;
98 int primaries_in;
99 int range_in;
100 int chromal_in;
101 char *size_str;
102 double nominal_peak_luminance;
103 int approximate_gamma;
104
105 char *w_expr; ///< width expression string
106 char *h_expr; ///< height expression string
107
108 int out_h_chr_pos;
109 int out_v_chr_pos;
110 int in_h_chr_pos;
111 int in_v_chr_pos;
112
113 int force_original_aspect_ratio;
114
115 void *tmp;
116 size_t tmp_size;
117
118 zimg_image_format src_format, dst_format;
119 zimg_image_format alpha_src_format, alpha_dst_format;
120 zimg_graph_builder_params alpha_params, params;
121 zimg_filter_graph *alpha_graph, *graph;
122
123 enum AVColorSpace in_colorspace, out_colorspace;
124 enum AVColorTransferCharacteristic in_trc, out_trc;
125 enum AVColorPrimaries in_primaries, out_primaries;
126 enum AVColorRange in_range, out_range;
127 enum AVChromaLocation in_chromal, out_chromal;
128 } ZScaleContext;
129
init_dict(AVFilterContext * ctx,AVDictionary ** opts)130 static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
131 {
132 ZScaleContext *s = ctx->priv;
133 int ret;
134
135 if (s->size_str && (s->w_expr || s->h_expr)) {
136 av_log(ctx, AV_LOG_ERROR,
137 "Size and width/height expressions cannot be set at the same time.\n");
138 return AVERROR(EINVAL);
139 }
140
141 if (s->w_expr && !s->h_expr)
142 FFSWAP(char *, s->w_expr, s->size_str);
143
144 if (s->size_str) {
145 char buf[32];
146 if ((ret = av_parse_video_size(&s->w, &s->h, s->size_str)) < 0) {
147 av_log(ctx, AV_LOG_ERROR,
148 "Invalid size '%s'\n", s->size_str);
149 return ret;
150 }
151 snprintf(buf, sizeof(buf)-1, "%d", s->w);
152 av_opt_set(s, "w", buf, 0);
153 snprintf(buf, sizeof(buf)-1, "%d", s->h);
154 av_opt_set(s, "h", buf, 0);
155 }
156 if (!s->w_expr)
157 av_opt_set(s, "w", "iw", 0);
158 if (!s->h_expr)
159 av_opt_set(s, "h", "ih", 0);
160
161 return 0;
162 }
163
query_formats(AVFilterContext * ctx)164 static int query_formats(AVFilterContext *ctx)
165 {
166 static const enum AVPixelFormat pixel_fmts[] = {
167 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
168 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
169 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
170 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
171 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
172 AV_PIX_FMT_YUVJ411P,
173 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
174 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
175 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
176 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
177 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
178 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
179 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
180 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
181 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
182 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
183 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
184 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
185 AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
186 AV_PIX_FMT_NONE
187 };
188 int ret;
189
190 ret = ff_formats_ref(ff_make_format_list(pixel_fmts), &ctx->inputs[0]->out_formats);
191 if (ret < 0)
192 return ret;
193 return ff_formats_ref(ff_make_format_list(pixel_fmts), &ctx->outputs[0]->in_formats);
194 }
195
config_props(AVFilterLink * outlink)196 static int config_props(AVFilterLink *outlink)
197 {
198 AVFilterContext *ctx = outlink->src;
199 AVFilterLink *inlink = outlink->src->inputs[0];
200 ZScaleContext *s = ctx->priv;
201 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
202 const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
203 int64_t w, h;
204 double var_values[VARS_NB], res;
205 char *expr;
206 int ret;
207 int factor_w, factor_h;
208
209 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
210 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
211 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
212 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
213 var_values[VAR_A] = (double) inlink->w / inlink->h;
214 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
215 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
216 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
217 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
218 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
219 var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
220 var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
221
222 /* evaluate width and height */
223 av_expr_parse_and_eval(&res, (expr = s->w_expr),
224 var_names, var_values,
225 NULL, NULL, NULL, NULL, NULL, 0, ctx);
226 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
227 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
228 var_names, var_values,
229 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
230 goto fail;
231 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
232 /* evaluate again the width, as it may depend on the output height */
233 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
234 var_names, var_values,
235 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
236 goto fail;
237 s->w = res;
238
239 w = s->w;
240 h = s->h;
241
242 /* Check if it is requested that the result has to be divisible by a some
243 * factor (w or h = -n with n being the factor). */
244 factor_w = 1;
245 factor_h = 1;
246 if (w < -1) {
247 factor_w = -w;
248 }
249 if (h < -1) {
250 factor_h = -h;
251 }
252
253 if (w < 0 && h < 0)
254 s->w = s->h = 0;
255
256 if (!(w = s->w))
257 w = inlink->w;
258 if (!(h = s->h))
259 h = inlink->h;
260
261 /* Make sure that the result is divisible by the factor we determined
262 * earlier. If no factor was set, it is nothing will happen as the default
263 * factor is 1 */
264 if (w < 0)
265 w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
266 if (h < 0)
267 h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
268
269 /* Note that force_original_aspect_ratio may overwrite the previous set
270 * dimensions so that it is not divisible by the set factors anymore. */
271 if (s->force_original_aspect_ratio) {
272 int tmp_w = av_rescale(h, inlink->w, inlink->h);
273 int tmp_h = av_rescale(w, inlink->h, inlink->w);
274
275 if (s->force_original_aspect_ratio == 1) {
276 w = FFMIN(tmp_w, w);
277 h = FFMIN(tmp_h, h);
278 } else {
279 w = FFMAX(tmp_w, w);
280 h = FFMAX(tmp_h, h);
281 }
282 }
283
284 if (w > INT_MAX || h > INT_MAX ||
285 (h * inlink->w) > INT_MAX ||
286 (w * inlink->h) > INT_MAX)
287 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
288
289 outlink->w = w;
290 outlink->h = h;
291
292 if (inlink->w == outlink->w &&
293 inlink->h == outlink->h &&
294 inlink->format == outlink->format)
295 ;
296 else {
297 }
298
299 if (inlink->sample_aspect_ratio.num){
300 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
301 } else
302 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
303
304 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",
305 inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
306 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
307 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
308 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
309 return 0;
310
311 fail:
312 av_log(ctx, AV_LOG_ERROR,
313 "Error when evaluating the expression '%s'.\n"
314 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
315 expr, s->w_expr, s->h_expr);
316 return ret;
317 }
318
print_zimg_error(AVFilterContext * ctx)319 static int print_zimg_error(AVFilterContext *ctx)
320 {
321 char err_msg[1024];
322 int err_code = zimg_get_last_error(err_msg, sizeof(err_msg));
323
324 av_log(ctx, AV_LOG_ERROR, "code %d: %s\n", err_code, err_msg);
325
326 return AVERROR_EXTERNAL;
327 }
328
convert_chroma_location(enum AVChromaLocation chroma_location)329 static int convert_chroma_location(enum AVChromaLocation chroma_location)
330 {
331 switch (chroma_location) {
332 case AVCHROMA_LOC_UNSPECIFIED:
333 case AVCHROMA_LOC_LEFT:
334 return ZIMG_CHROMA_LEFT;
335 case AVCHROMA_LOC_CENTER:
336 return ZIMG_CHROMA_CENTER;
337 case AVCHROMA_LOC_TOPLEFT:
338 return ZIMG_CHROMA_TOP_LEFT;
339 case AVCHROMA_LOC_TOP:
340 return ZIMG_CHROMA_TOP;
341 case AVCHROMA_LOC_BOTTOMLEFT:
342 return ZIMG_CHROMA_BOTTOM_LEFT;
343 case AVCHROMA_LOC_BOTTOM:
344 return ZIMG_CHROMA_BOTTOM;
345 }
346 return ZIMG_CHROMA_LEFT;
347 }
348
convert_matrix(enum AVColorSpace colorspace)349 static int convert_matrix(enum AVColorSpace colorspace)
350 {
351 switch (colorspace) {
352 case AVCOL_SPC_RGB:
353 return ZIMG_MATRIX_RGB;
354 case AVCOL_SPC_BT709:
355 return ZIMG_MATRIX_709;
356 case AVCOL_SPC_UNSPECIFIED:
357 return ZIMG_MATRIX_UNSPECIFIED;
358 case AVCOL_SPC_FCC:
359 return ZIMG_MATRIX_FCC;
360 case AVCOL_SPC_BT470BG:
361 return ZIMG_MATRIX_470BG;
362 case AVCOL_SPC_SMPTE170M:
363 return ZIMG_MATRIX_170M;
364 case AVCOL_SPC_SMPTE240M:
365 return ZIMG_MATRIX_240M;
366 case AVCOL_SPC_YCGCO:
367 return ZIMG_MATRIX_YCGCO;
368 case AVCOL_SPC_BT2020_NCL:
369 return ZIMG_MATRIX_2020_NCL;
370 case AVCOL_SPC_BT2020_CL:
371 return ZIMG_MATRIX_2020_CL;
372 case AVCOL_SPC_CHROMA_DERIVED_NCL:
373 return ZIMG_MATRIX_CHROMATICITY_DERIVED_NCL;
374 case AVCOL_SPC_CHROMA_DERIVED_CL:
375 return ZIMG_MATRIX_CHROMATICITY_DERIVED_CL;
376 case AVCOL_SPC_ICTCP:
377 return ZIMG_MATRIX_ICTCP;
378 }
379 return ZIMG_MATRIX_UNSPECIFIED;
380 }
381
convert_trc(enum AVColorTransferCharacteristic color_trc)382 static int convert_trc(enum AVColorTransferCharacteristic color_trc)
383 {
384 switch (color_trc) {
385 case AVCOL_TRC_UNSPECIFIED:
386 return ZIMG_TRANSFER_UNSPECIFIED;
387 case AVCOL_TRC_BT709:
388 return ZIMG_TRANSFER_709;
389 case AVCOL_TRC_GAMMA22:
390 return ZIMG_TRANSFER_470_M;
391 case AVCOL_TRC_GAMMA28:
392 return ZIMG_TRANSFER_470_BG;
393 case AVCOL_TRC_SMPTE170M:
394 return ZIMG_TRANSFER_601;
395 case AVCOL_TRC_SMPTE240M:
396 return ZIMG_TRANSFER_240M;
397 case AVCOL_TRC_LINEAR:
398 return ZIMG_TRANSFER_LINEAR;
399 case AVCOL_TRC_LOG:
400 return ZIMG_TRANSFER_LOG_100;
401 case AVCOL_TRC_LOG_SQRT:
402 return ZIMG_TRANSFER_LOG_316;
403 case AVCOL_TRC_IEC61966_2_4:
404 return ZIMG_TRANSFER_IEC_61966_2_4;
405 case AVCOL_TRC_BT2020_10:
406 return ZIMG_TRANSFER_2020_10;
407 case AVCOL_TRC_BT2020_12:
408 return ZIMG_TRANSFER_2020_12;
409 case AVCOL_TRC_SMPTE2084:
410 return ZIMG_TRANSFER_ST2084;
411 case AVCOL_TRC_ARIB_STD_B67:
412 return ZIMG_TRANSFER_ARIB_B67;
413 case AVCOL_TRC_IEC61966_2_1:
414 return ZIMG_TRANSFER_IEC_61966_2_1;
415 }
416 return ZIMG_TRANSFER_UNSPECIFIED;
417 }
418
convert_primaries(enum AVColorPrimaries color_primaries)419 static int convert_primaries(enum AVColorPrimaries color_primaries)
420 {
421 switch (color_primaries) {
422 case AVCOL_PRI_UNSPECIFIED:
423 return ZIMG_PRIMARIES_UNSPECIFIED;
424 case AVCOL_PRI_BT709:
425 return ZIMG_PRIMARIES_709;
426 case AVCOL_PRI_BT470M:
427 return ZIMG_PRIMARIES_470_M;
428 case AVCOL_PRI_BT470BG:
429 return ZIMG_PRIMARIES_470_BG;
430 case AVCOL_PRI_SMPTE170M:
431 return ZIMG_PRIMARIES_170M;
432 case AVCOL_PRI_SMPTE240M:
433 return ZIMG_PRIMARIES_240M;
434 case AVCOL_PRI_FILM:
435 return ZIMG_PRIMARIES_FILM;
436 case AVCOL_PRI_BT2020:
437 return ZIMG_PRIMARIES_2020;
438 case AVCOL_PRI_SMPTE428:
439 return ZIMG_PRIMARIES_ST428;
440 case AVCOL_PRI_SMPTE431:
441 return ZIMG_PRIMARIES_ST431_2;
442 case AVCOL_PRI_SMPTE432:
443 return ZIMG_PRIMARIES_ST432_1;
444 case AVCOL_PRI_JEDEC_P22:
445 return ZIMG_PRIMARIES_EBU3213_E;
446 }
447 return ZIMG_PRIMARIES_UNSPECIFIED;
448 }
449
convert_range(enum AVColorRange color_range)450 static int convert_range(enum AVColorRange color_range)
451 {
452 switch (color_range) {
453 case AVCOL_RANGE_UNSPECIFIED:
454 case AVCOL_RANGE_MPEG:
455 return ZIMG_RANGE_LIMITED;
456 case AVCOL_RANGE_JPEG:
457 return ZIMG_RANGE_FULL;
458 }
459 return ZIMG_RANGE_LIMITED;
460 }
461
format_init(zimg_image_format * format,AVFrame * frame,const AVPixFmtDescriptor * desc,int colorspace,int primaries,int transfer,int range,int location)462 static void format_init(zimg_image_format *format, AVFrame *frame, const AVPixFmtDescriptor *desc,
463 int colorspace, int primaries, int transfer, int range, int location)
464 {
465 format->width = frame->width;
466 format->height = frame->height;
467 format->subsample_w = desc->log2_chroma_w;
468 format->subsample_h = desc->log2_chroma_h;
469 format->depth = desc->comp[0].depth;
470 format->pixel_type = (desc->flags & AV_PIX_FMT_FLAG_FLOAT) ? ZIMG_PIXEL_FLOAT : desc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
471 format->color_family = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_COLOR_RGB : ZIMG_COLOR_YUV;
472 format->matrix_coefficients = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_MATRIX_RGB : colorspace == -1 ? convert_matrix(frame->colorspace) : colorspace;
473 format->color_primaries = primaries == -1 ? convert_primaries(frame->color_primaries) : primaries;
474 format->transfer_characteristics = transfer == - 1 ? convert_trc(frame->color_trc) : transfer;
475 format->pixel_range = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? ZIMG_RANGE_FULL : range == -1 ? convert_range(frame->color_range) : range;
476 format->chroma_location = location == -1 ? convert_chroma_location(frame->chroma_location) : location;
477 }
478
graph_build(zimg_filter_graph ** graph,zimg_graph_builder_params * params,zimg_image_format * src_format,zimg_image_format * dst_format,void ** tmp,size_t * tmp_size)479 static int graph_build(zimg_filter_graph **graph, zimg_graph_builder_params *params,
480 zimg_image_format *src_format, zimg_image_format *dst_format,
481 void **tmp, size_t *tmp_size)
482 {
483 int ret;
484 size_t size;
485
486 zimg_filter_graph_free(*graph);
487 *graph = zimg_filter_graph_build(src_format, dst_format, params);
488 if (!*graph)
489 return print_zimg_error(NULL);
490
491 ret = zimg_filter_graph_get_tmp_size(*graph, &size);
492 if (ret)
493 return print_zimg_error(NULL);
494
495 if (size > *tmp_size) {
496 av_freep(tmp);
497 *tmp = av_malloc(size);
498 if (!*tmp)
499 return AVERROR(ENOMEM);
500
501 *tmp_size = size;
502 }
503
504 return 0;
505 }
506
realign_frame(const AVPixFmtDescriptor * desc,AVFrame ** frame)507 static int realign_frame(const AVPixFmtDescriptor *desc, AVFrame **frame)
508 {
509 AVFrame *aligned = NULL;
510 int ret = 0, plane;
511
512 /* Realign any unaligned input frame. */
513 for (plane = 0; plane < 3; plane++) {
514 int p = desc->comp[plane].plane;
515 if ((uintptr_t)(*frame)->data[p] % ZIMG_ALIGNMENT || (*frame)->linesize[p] % ZIMG_ALIGNMENT) {
516 if (!(aligned = av_frame_alloc())) {
517 ret = AVERROR(ENOMEM);
518 goto fail;
519 }
520
521 aligned->format = (*frame)->format;
522 aligned->width = (*frame)->width;
523 aligned->height = (*frame)->height;
524
525 if ((ret = av_frame_get_buffer(aligned, ZIMG_ALIGNMENT)) < 0)
526 goto fail;
527
528 if ((ret = av_frame_copy(aligned, *frame)) < 0)
529 goto fail;
530
531 if ((ret = av_frame_copy_props(aligned, *frame)) < 0)
532 goto fail;
533
534 av_frame_free(frame);
535 *frame = aligned;
536 return 0;
537 }
538 }
539
540 fail:
541 av_frame_free(&aligned);
542 return ret;
543 }
544
filter_frame(AVFilterLink * link,AVFrame * in)545 static int filter_frame(AVFilterLink *link, AVFrame *in)
546 {
547 ZScaleContext *s = link->dst->priv;
548 AVFilterLink *outlink = link->dst->outputs[0];
549 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
550 const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
551 zimg_image_buffer_const src_buf = { ZIMG_API_VERSION };
552 zimg_image_buffer dst_buf = { ZIMG_API_VERSION };
553 char buf[32];
554 int ret = 0, plane;
555 AVFrame *out = NULL;
556
557 if ((ret = realign_frame(desc, &in)) < 0)
558 goto fail;
559
560 if (!(out = ff_get_video_buffer(outlink, outlink->w, outlink->h))) {
561 ret = AVERROR(ENOMEM);
562 goto fail;
563 }
564
565 av_frame_copy_props(out, in);
566 out->width = outlink->w;
567 out->height = outlink->h;
568
569 if( in->width != link->w
570 || in->height != link->h
571 || in->format != link->format
572 || s->in_colorspace != in->colorspace
573 || s->in_trc != in->color_trc
574 || s->in_primaries != in->color_primaries
575 || s->in_range != in->color_range
576 || s->out_colorspace != out->colorspace
577 || s->out_trc != out->color_trc
578 || s->out_primaries != out->color_primaries
579 || s->out_range != out->color_range
580 || s->in_chromal != in->chroma_location
581 || s->out_chromal != out->chroma_location) {
582 snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
583 av_opt_set(s, "w", buf, 0);
584 snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
585 av_opt_set(s, "h", buf, 0);
586
587 link->dst->inputs[0]->format = in->format;
588 link->dst->inputs[0]->w = in->width;
589 link->dst->inputs[0]->h = in->height;
590
591 if ((ret = config_props(outlink)) < 0)
592 goto fail;
593
594 zimg_image_format_default(&s->src_format, ZIMG_API_VERSION);
595 zimg_image_format_default(&s->dst_format, ZIMG_API_VERSION);
596 zimg_graph_builder_params_default(&s->params, ZIMG_API_VERSION);
597
598 s->params.dither_type = s->dither;
599 s->params.cpu_type = ZIMG_CPU_AUTO;
600 s->params.resample_filter = s->filter;
601 s->params.resample_filter_uv = s->filter;
602 s->params.nominal_peak_luminance = s->nominal_peak_luminance;
603 s->params.allow_approximate_gamma = s->approximate_gamma;
604
605 format_init(&s->src_format, in, desc, s->colorspace_in,
606 s->primaries_in, s->trc_in, s->range_in, s->chromal_in);
607 format_init(&s->dst_format, out, odesc, s->colorspace,
608 s->primaries, s->trc, s->range, s->chromal);
609
610 if (s->colorspace != -1)
611 out->colorspace = (int)s->dst_format.matrix_coefficients;
612
613 if (s->primaries != -1)
614 out->color_primaries = (int)s->dst_format.color_primaries;
615
616 if (s->range != -1)
617 out->color_range = (int)s->dst_format.pixel_range + 1;
618
619 if (s->trc != -1)
620 out->color_trc = (int)s->dst_format.transfer_characteristics;
621
622 if (s->chromal != -1)
623 out->chroma_location = (int)s->dst_format.chroma_location - 1;
624
625 ret = graph_build(&s->graph, &s->params, &s->src_format, &s->dst_format,
626 &s->tmp, &s->tmp_size);
627 if (ret < 0)
628 goto fail;
629
630 s->in_colorspace = in->colorspace;
631 s->in_trc = in->color_trc;
632 s->in_primaries = in->color_primaries;
633 s->in_range = in->color_range;
634 s->out_colorspace = out->colorspace;
635 s->out_trc = out->color_trc;
636 s->out_primaries = out->color_primaries;
637 s->out_range = out->color_range;
638
639 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
640 zimg_image_format_default(&s->alpha_src_format, ZIMG_API_VERSION);
641 zimg_image_format_default(&s->alpha_dst_format, ZIMG_API_VERSION);
642 zimg_graph_builder_params_default(&s->alpha_params, ZIMG_API_VERSION);
643
644 s->alpha_params.dither_type = s->dither;
645 s->alpha_params.cpu_type = ZIMG_CPU_AUTO;
646 s->alpha_params.resample_filter = s->filter;
647
648 s->alpha_src_format.width = in->width;
649 s->alpha_src_format.height = in->height;
650 s->alpha_src_format.depth = desc->comp[0].depth;
651 s->alpha_src_format.pixel_type = (desc->flags & AV_PIX_FMT_FLAG_FLOAT) ? ZIMG_PIXEL_FLOAT : desc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
652 s->alpha_src_format.color_family = ZIMG_COLOR_GREY;
653
654 s->alpha_dst_format.width = out->width;
655 s->alpha_dst_format.height = out->height;
656 s->alpha_dst_format.depth = odesc->comp[0].depth;
657 s->alpha_dst_format.pixel_type = (odesc->flags & AV_PIX_FMT_FLAG_FLOAT) ? ZIMG_PIXEL_FLOAT : odesc->comp[0].depth > 8 ? ZIMG_PIXEL_WORD : ZIMG_PIXEL_BYTE;
658 s->alpha_dst_format.color_family = ZIMG_COLOR_GREY;
659
660 zimg_filter_graph_free(s->alpha_graph);
661 s->alpha_graph = zimg_filter_graph_build(&s->alpha_src_format, &s->alpha_dst_format, &s->alpha_params);
662 if (!s->alpha_graph) {
663 ret = print_zimg_error(link->dst);
664 goto fail;
665 }
666 }
667 }
668
669 if (s->colorspace != -1)
670 out->colorspace = (int)s->dst_format.matrix_coefficients;
671
672 if (s->primaries != -1)
673 out->color_primaries = (int)s->dst_format.color_primaries;
674
675 if (s->range != -1)
676 out->color_range = (int)s->dst_format.pixel_range;
677
678 if (s->trc != -1)
679 out->color_trc = (int)s->dst_format.transfer_characteristics;
680
681 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
682 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
683 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
684 INT_MAX);
685
686 for (plane = 0; plane < 3; plane++) {
687 int p = desc->comp[plane].plane;
688 src_buf.plane[plane].data = in->data[p];
689 src_buf.plane[plane].stride = in->linesize[p];
690 src_buf.plane[plane].mask = -1;
691
692 p = odesc->comp[plane].plane;
693 dst_buf.plane[plane].data = out->data[p];
694 dst_buf.plane[plane].stride = out->linesize[p];
695 dst_buf.plane[plane].mask = -1;
696 }
697
698 ret = zimg_filter_graph_process(s->graph, &src_buf, &dst_buf, s->tmp, 0, 0, 0, 0);
699 if (ret) {
700 ret = print_zimg_error(link->dst);
701 goto fail;
702 }
703
704 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
705 src_buf.plane[0].data = in->data[3];
706 src_buf.plane[0].stride = in->linesize[3];
707 src_buf.plane[0].mask = -1;
708
709 dst_buf.plane[0].data = out->data[3];
710 dst_buf.plane[0].stride = out->linesize[3];
711 dst_buf.plane[0].mask = -1;
712
713 ret = zimg_filter_graph_process(s->alpha_graph, &src_buf, &dst_buf, s->tmp, 0, 0, 0, 0);
714 if (ret) {
715 ret = print_zimg_error(link->dst);
716 goto fail;
717 }
718 } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
719 int x, y;
720
721 if (odesc->flags & AV_PIX_FMT_FLAG_FLOAT) {
722 for (y = 0; y < out->height; y++) {
723 for (x = 0; x < out->width; x++) {
724 AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3],
725 av_float2int(1.0f));
726 }
727 }
728 } else {
729 for (y = 0; y < outlink->h; y++)
730 memset(out->data[3] + y * out->linesize[3], 0xff, outlink->w);
731 }
732 }
733
734 fail:
735 av_frame_free(&in);
736 if (ret) {
737 av_frame_free(&out);
738 return ret;
739 }
740
741 return ff_filter_frame(outlink, out);
742 }
743
uninit(AVFilterContext * ctx)744 static av_cold void uninit(AVFilterContext *ctx)
745 {
746 ZScaleContext *s = ctx->priv;
747
748 zimg_filter_graph_free(s->graph);
749 zimg_filter_graph_free(s->alpha_graph);
750 av_freep(&s->tmp);
751 s->tmp_size = 0;
752 }
753
process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)754 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
755 char *res, int res_len, int flags)
756 {
757 ZScaleContext *s = ctx->priv;
758 int ret;
759
760 if ( !strcmp(cmd, "width") || !strcmp(cmd, "w")
761 || !strcmp(cmd, "height") || !strcmp(cmd, "h")) {
762
763 int old_w = s->w;
764 int old_h = s->h;
765 AVFilterLink *outlink = ctx->outputs[0];
766
767 av_opt_set(s, cmd, args, 0);
768 if ((ret = config_props(outlink)) < 0) {
769 s->w = old_w;
770 s->h = old_h;
771 }
772 } else
773 ret = AVERROR(ENOSYS);
774
775 return ret;
776 }
777
778 #define OFFSET(x) offsetof(ZScaleContext, x)
779 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
780 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
781
782 static const AVOption zscale_options[] = {
783 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
784 { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
785 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
786 { "height", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = TFLAGS },
787 { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
788 { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
789 { "dither", "set dither type", OFFSET(dither), AV_OPT_TYPE_INT, {.i64 = 0}, 0, ZIMG_DITHER_ERROR_DIFFUSION, FLAGS, "dither" },
790 { "d", "set dither type", OFFSET(dither), AV_OPT_TYPE_INT, {.i64 = 0}, 0, ZIMG_DITHER_ERROR_DIFFUSION, FLAGS, "dither" },
791 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_NONE}, 0, 0, FLAGS, "dither" },
792 { "ordered", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_ORDERED}, 0, 0, FLAGS, "dither" },
793 { "random", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_RANDOM}, 0, 0, FLAGS, "dither" },
794 { "error_diffusion", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_ERROR_DIFFUSION}, 0, 0, FLAGS, "dither" },
795 { "filter", "set filter type", OFFSET(filter), AV_OPT_TYPE_INT, {.i64 = ZIMG_RESIZE_BILINEAR}, 0, ZIMG_RESIZE_LANCZOS, FLAGS, "filter" },
796 { "f", "set filter type", OFFSET(filter), AV_OPT_TYPE_INT, {.i64 = ZIMG_RESIZE_BILINEAR}, 0, ZIMG_RESIZE_LANCZOS, FLAGS, "filter" },
797 { "point", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_POINT}, 0, 0, FLAGS, "filter" },
798 { "bilinear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_BILINEAR}, 0, 0, FLAGS, "filter" },
799 { "bicubic", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_BICUBIC}, 0, 0, FLAGS, "filter" },
800 { "spline16", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_SPLINE16}, 0, 0, FLAGS, "filter" },
801 { "spline36", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_SPLINE36}, 0, 0, FLAGS, "filter" },
802 { "lanczos", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RESIZE_LANCZOS}, 0, 0, FLAGS, "filter" },
803 { "out_range", "set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
804 { "range", "set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
805 { "r", "set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
806 { "input", 0, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, "range" },
807 { "limited", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RANGE_LIMITED}, 0, 0, FLAGS, "range" },
808 { "full", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RANGE_FULL}, 0, 0, FLAGS, "range" },
809 { "unknown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, "range" },
810 { "tv", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RANGE_LIMITED}, 0, 0, FLAGS, "range" },
811 { "pc", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_RANGE_FULL}, 0, 0, FLAGS, "range" },
812 { "primaries", "set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "primaries" },
813 { "p", "set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "primaries" },
814 { "input", 0, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, "primaries" },
815 { "709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_709}, 0, 0, FLAGS, "primaries" },
816 { "unspecified", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_UNSPECIFIED}, 0, 0, FLAGS, "primaries" },
817 { "170m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_170M}, 0, 0, FLAGS, "primaries" },
818 { "240m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_240M}, 0, 0, FLAGS, "primaries" },
819 { "2020", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_2020}, 0, 0, FLAGS, "primaries" },
820 { "unknown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_UNSPECIFIED}, 0, 0, FLAGS, "primaries" },
821 { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_709}, 0, 0, FLAGS, "primaries" },
822 { "bt470m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_470_M}, 0, 0, FLAGS, "primaries" },
823 { "bt470bg", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_470_BG}, 0, 0, FLAGS, "primaries" },
824 { "smpte170m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_170M}, 0, 0, FLAGS, "primaries" },
825 { "smpte240m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_240M}, 0, 0, FLAGS, "primaries" },
826 { "film", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_FILM}, 0, 0, FLAGS, "primaries" },
827 { "bt2020", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_2020}, 0, 0, FLAGS, "primaries" },
828 { "smpte428", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_ST428}, 0, 0, FLAGS, "primaries" },
829 { "smpte431", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_ST431_2}, 0, 0, FLAGS, "primaries" },
830 { "smpte432", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_ST432_1}, 0, 0, FLAGS, "primaries" },
831 { "jedec-p22", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_EBU3213_E}, 0, 0, FLAGS, "primaries" },
832 { "ebu3213", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_EBU3213_E}, 0, 0, FLAGS, "primaries" },
833 { "transfer", "set transfer characteristic", OFFSET(trc), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "transfer" },
834 { "t", "set transfer characteristic", OFFSET(trc), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "transfer" },
835 { "input", 0, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, "transfer" },
836 { "709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_709}, 0, 0, FLAGS, "transfer" },
837 { "unspecified", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_UNSPECIFIED}, 0, 0, FLAGS, "transfer" },
838 { "601", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_601}, 0, 0, FLAGS, "transfer" },
839 { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_LINEAR}, 0, 0, FLAGS, "transfer" },
840 { "2020_10", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_2020_10}, 0, 0, FLAGS, "transfer" },
841 { "2020_12", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_2020_12}, 0, 0, FLAGS, "transfer" },
842 { "unknown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_UNSPECIFIED}, 0, 0, FLAGS, "transfer" },
843 { "bt470m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_470_M}, 0, 0, FLAGS, "transfer" },
844 { "bt470bg", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_470_BG}, 0, 0, FLAGS, "transfer" },
845 { "smpte170m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_601}, 0, 0, FLAGS, "transfer" },
846 { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_709}, 0, 0, FLAGS, "transfer" },
847 { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_LINEAR}, 0, 0, FLAGS, "transfer" },
848 { "log100", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_LOG_100}, 0, 0, FLAGS, "transfer" },
849 { "log316", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_LOG_316}, 0, 0, FLAGS, "transfer" },
850 { "bt2020-10", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_2020_10}, 0, 0, FLAGS, "transfer" },
851 { "bt2020-12", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_2020_12}, 0, 0, FLAGS, "transfer" },
852 { "smpte2084", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_ST2084}, 0, 0, FLAGS, "transfer" },
853 { "iec61966-2-4", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_IEC_61966_2_4},0, 0, FLAGS, "transfer" },
854 { "iec61966-2-1", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_IEC_61966_2_1},0, 0, FLAGS, "transfer" },
855 { "arib-std-b67", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_ARIB_B67}, 0, 0, FLAGS, "transfer" },
856 { "matrix", "set colorspace matrix", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "matrix" },
857 { "m", "set colorspace matrix", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "matrix" },
858 { "input", 0, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, "matrix" },
859 { "709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_709}, 0, 0, FLAGS, "matrix" },
860 { "unspecified", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_UNSPECIFIED}, 0, 0, FLAGS, "matrix" },
861 { "470bg", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_470BG}, 0, 0, FLAGS, "matrix" },
862 { "170m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_170M}, 0, 0, FLAGS, "matrix" },
863 { "2020_ncl", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_2020_NCL}, 0, 0, FLAGS, "matrix" },
864 { "2020_cl", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_2020_CL}, 0, 0, FLAGS, "matrix" },
865 { "unknown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_UNSPECIFIED}, 0, 0, FLAGS, "matrix" },
866 { "gbr", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_RGB}, 0, 0, FLAGS, "matrix" },
867 { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_709}, 0, 0, FLAGS, "matrix" },
868 { "fcc", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_FCC}, 0, 0, FLAGS, "matrix" },
869 { "bt470bg", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_470BG}, 0, 0, FLAGS, "matrix" },
870 { "smpte170m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_170M}, 0, 0, FLAGS, "matrix" },
871 { "smpte2400m", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_240M}, 0, 0, FLAGS, "matrix" },
872 { "ycgco", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_YCGCO}, 0, 0, FLAGS, "matrix" },
873 { "bt2020nc", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_2020_NCL}, 0, 0, FLAGS, "matrix" },
874 { "bt2020c", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_2020_CL}, 0, 0, FLAGS, "matrix" },
875 { "chroma-derived-nc",0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_CHROMATICITY_DERIVED_NCL}, 0, 0, FLAGS, "matrix" },
876 { "chroma-derived-c", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_CHROMATICITY_DERIVED_CL}, 0, 0, FLAGS, "matrix" },
877 { "ictcp", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_ICTCP}, 0, 0, FLAGS, "matrix" },
878 { "in_range", "set input color range", OFFSET(range_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
879 { "rangein", "set input color range", OFFSET(range_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
880 { "rin", "set input color range", OFFSET(range_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL, FLAGS, "range" },
881 { "primariesin", "set input color primaries", OFFSET(primaries_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "primaries" },
882 { "pin", "set input color primaries", OFFSET(primaries_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "primaries" },
883 { "transferin", "set input transfer characteristic", OFFSET(trc_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "transfer" },
884 { "tin", "set input transfer characteristic", OFFSET(trc_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "transfer" },
885 { "matrixin", "set input colorspace matrix", OFFSET(colorspace_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "matrix" },
886 { "min", "set input colorspace matrix", OFFSET(colorspace_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, "matrix" },
887 { "chromal", "set output chroma location", OFFSET(chromal), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM, FLAGS, "chroma" },
888 { "c", "set output chroma location", OFFSET(chromal), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM, FLAGS, "chroma" },
889 { "input", 0, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, "chroma" },
890 { "left", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_CHROMA_LEFT}, 0, 0, FLAGS, "chroma" },
891 { "center", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_CHROMA_CENTER}, 0, 0, FLAGS, "chroma" },
892 { "topleft", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_CHROMA_TOP_LEFT}, 0, 0, FLAGS, "chroma" },
893 { "top", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_CHROMA_TOP}, 0, 0, FLAGS, "chroma" },
894 { "bottomleft",0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_CHROMA_BOTTOM_LEFT}, 0, 0, FLAGS, "chroma" },
895 { "bottom", 0, 0, AV_OPT_TYPE_CONST, {.i64 = ZIMG_CHROMA_BOTTOM}, 0, 0, FLAGS, "chroma" },
896 { "chromalin", "set input chroma location", OFFSET(chromal_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM, FLAGS, "chroma" },
897 { "cin", "set input chroma location", OFFSET(chromal_in), AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM, FLAGS, "chroma" },
898 { "npl", "set nominal peak luminance", OFFSET(nominal_peak_luminance), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, 0, DBL_MAX, FLAGS },
899 { "agamma", "allow approximate gamma", OFFSET(approximate_gamma), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
900 { NULL }
901 };
902
903 AVFILTER_DEFINE_CLASS(zscale);
904
905 static const AVFilterPad avfilter_vf_zscale_inputs[] = {
906 {
907 .name = "default",
908 .type = AVMEDIA_TYPE_VIDEO,
909 .filter_frame = filter_frame,
910 },
911 { NULL }
912 };
913
914 static const AVFilterPad avfilter_vf_zscale_outputs[] = {
915 {
916 .name = "default",
917 .type = AVMEDIA_TYPE_VIDEO,
918 .config_props = config_props,
919 },
920 { NULL }
921 };
922
923 AVFilter ff_vf_zscale = {
924 .name = "zscale",
925 .description = NULL_IF_CONFIG_SMALL("Apply resizing, colorspace and bit depth conversion."),
926 .init_dict = init_dict,
927 .query_formats = query_formats,
928 .priv_size = sizeof(ZScaleContext),
929 .priv_class = &zscale_class,
930 .uninit = uninit,
931 .inputs = avfilter_vf_zscale_inputs,
932 .outputs = avfilter_vf_zscale_outputs,
933 .process_command = process_command,
934 };
935