46 #define ZIMG_ALIGNMENT 32
136 if (
s->size_str && (
s->w_expr ||
s->h_expr)) {
138 "Size and width/height expressions cannot be set at the same time.\n");
142 if (
s->w_expr && !
s->h_expr)
143 FFSWAP(
char *,
s->w_expr,
s->size_str);
149 "Invalid size '%s'\n",
s->size_str);
152 snprintf(buf,
sizeof(buf)-1,
"%d",
s->w);
154 snprintf(buf,
sizeof(buf)-1,
"%d",
s->h);
205 double var_values[
VARS_NB], res;
208 int factor_w, factor_h;
216 (
double)
inlink->sample_aspect_ratio.num /
inlink->sample_aspect_ratio.den : 1;
272 if (
s->force_original_aspect_ratio) {
276 if (
s->force_original_aspect_ratio == 1) {
285 if (
w > INT_MAX ||
h > INT_MAX ||
300 if (
inlink->sample_aspect_ratio.num){
307 inlink->sample_aspect_ratio.num,
inlink->sample_aspect_ratio.den,
314 "Error when evaluating the expression '%s'.\n"
315 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
316 expr,
s->w_expr,
s->h_expr);
323 int err_code = zimg_get_last_error(err_msg,
sizeof(err_msg));
332 switch (chroma_location) {
335 return ZIMG_CHROMA_LEFT;
337 return ZIMG_CHROMA_CENTER;
339 return ZIMG_CHROMA_TOP_LEFT;
341 return ZIMG_CHROMA_TOP;
343 return ZIMG_CHROMA_BOTTOM_LEFT;
345 return ZIMG_CHROMA_BOTTOM;
347 return ZIMG_CHROMA_LEFT;
352 switch (colorspace) {
354 return ZIMG_MATRIX_RGB;
356 return ZIMG_MATRIX_709;
358 return ZIMG_MATRIX_UNSPECIFIED;
360 return ZIMG_MATRIX_FCC;
362 return ZIMG_MATRIX_470BG;
364 return ZIMG_MATRIX_170M;
366 return ZIMG_MATRIX_240M;
368 return ZIMG_MATRIX_YCGCO;
370 return ZIMG_MATRIX_2020_NCL;
372 return ZIMG_MATRIX_2020_CL;
374 return ZIMG_MATRIX_CHROMATICITY_DERIVED_NCL;
376 return ZIMG_MATRIX_CHROMATICITY_DERIVED_CL;
378 return ZIMG_MATRIX_ICTCP;
380 return ZIMG_MATRIX_UNSPECIFIED;
387 return ZIMG_TRANSFER_UNSPECIFIED;
389 return ZIMG_TRANSFER_709;
391 return ZIMG_TRANSFER_470_M;
393 return ZIMG_TRANSFER_470_BG;
395 return ZIMG_TRANSFER_601;
397 return ZIMG_TRANSFER_240M;
399 return ZIMG_TRANSFER_LINEAR;
401 return ZIMG_TRANSFER_LOG_100;
403 return ZIMG_TRANSFER_LOG_316;
405 return ZIMG_TRANSFER_IEC_61966_2_4;
407 return ZIMG_TRANSFER_2020_10;
409 return ZIMG_TRANSFER_2020_12;
411 return ZIMG_TRANSFER_ST2084;
413 return ZIMG_TRANSFER_ARIB_B67;
415 return ZIMG_TRANSFER_IEC_61966_2_1;
417 return ZIMG_TRANSFER_UNSPECIFIED;
424 return ZIMG_PRIMARIES_UNSPECIFIED;
426 return ZIMG_PRIMARIES_709;
428 return ZIMG_PRIMARIES_470_M;
430 return ZIMG_PRIMARIES_470_BG;
432 return ZIMG_PRIMARIES_170M;
434 return ZIMG_PRIMARIES_240M;
436 return ZIMG_PRIMARIES_FILM;
438 return ZIMG_PRIMARIES_2020;
440 return ZIMG_PRIMARIES_ST428;
442 return ZIMG_PRIMARIES_ST431_2;
444 return ZIMG_PRIMARIES_ST432_1;
446 return ZIMG_PRIMARIES_EBU3213_E;
448 return ZIMG_PRIMARIES_UNSPECIFIED;
456 return ZIMG_RANGE_LIMITED;
458 return ZIMG_RANGE_FULL;
460 return ZIMG_RANGE_LIMITED;
466 case ZIMG_RANGE_LIMITED:
468 case ZIMG_RANGE_FULL:
475 int colorspace,
int primaries,
int transfer,
int range,
int location)
492 zimg_image_format *src_format, zimg_image_format *dst_format,
493 void **
tmp,
size_t *tmp_size)
498 zimg_filter_graph_free(*
graph);
499 *
graph = zimg_filter_graph_build(src_format, dst_format, params);
507 if (
size > *tmp_size) {
525 for (plane = 0; plane < 3; plane++) {
526 int p =
desc->comp[plane].plane;
533 aligned->format = (*frame)->format;
534 aligned->width = (*frame)->width;
535 aligned->height = (*frame)->height;
559 if (
s->colorspace != -1)
560 frame->colorspace = (
int)
s->dst_format.matrix_coefficients;
562 if (
s->primaries != -1)
563 frame->color_primaries = (
int)
s->dst_format.color_primaries;
569 frame->color_trc = (
int)
s->dst_format.transfer_characteristics;
571 if (
s->chromal != -1)
572 frame->chroma_location = (
int)
s->dst_format.chroma_location + 1;
581 zimg_image_buffer_const src_buf = { ZIMG_API_VERSION };
582 zimg_image_buffer dst_buf = { ZIMG_API_VERSION };
596 out->width = outlink->
w;
597 out->height = outlink->
h;
606 ||
s->out_colorspace !=
out->colorspace
607 ||
s->out_trc !=
out->color_trc
608 ||
s->out_primaries !=
out->color_primaries
609 ||
s->out_range !=
out->color_range
611 ||
s->out_chromal !=
out->chroma_location) {
612 snprintf(buf,
sizeof(buf)-1,
"%d", outlink->
w);
614 snprintf(buf,
sizeof(buf)-1,
"%d", outlink->
h);
624 zimg_image_format_default(&
s->src_format, ZIMG_API_VERSION);
625 zimg_image_format_default(&
s->dst_format, ZIMG_API_VERSION);
626 zimg_graph_builder_params_default(&
s->params, ZIMG_API_VERSION);
628 s->params.dither_type =
s->dither;
629 s->params.cpu_type = ZIMG_CPU_AUTO;
630 s->params.resample_filter =
s->filter;
631 s->params.resample_filter_uv =
s->filter;
632 s->params.nominal_peak_luminance =
s->nominal_peak_luminance;
633 s->params.allow_approximate_gamma =
s->approximate_gamma;
634 s->params.filter_param_a =
s->params.filter_param_a_uv =
s->param_a;
635 s->params.filter_param_b =
s->params.filter_param_b_uv =
s->param_b;
638 s->primaries_in,
s->trc_in,
s->range_in,
s->chromal_in);
640 s->primaries,
s->trc,
s->range,
s->chromal);
645 &
s->tmp, &
s->tmp_size);
653 s->out_colorspace =
out->colorspace;
654 s->out_trc =
out->color_trc;
655 s->out_primaries =
out->color_primaries;
656 s->out_range =
out->color_range;
659 zimg_image_format_default(&
s->alpha_src_format, ZIMG_API_VERSION);
660 zimg_image_format_default(&
s->alpha_dst_format, ZIMG_API_VERSION);
661 zimg_graph_builder_params_default(&
s->alpha_params, ZIMG_API_VERSION);
663 s->alpha_params.dither_type =
s->dither;
664 s->alpha_params.cpu_type = ZIMG_CPU_AUTO;
665 s->alpha_params.resample_filter =
s->filter;
667 s->alpha_src_format.width = in->
width;
668 s->alpha_src_format.height = in->
height;
669 s->alpha_src_format.depth =
desc->comp[0].depth;
670 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;
671 s->alpha_src_format.color_family = ZIMG_COLOR_GREY;
673 s->alpha_dst_format.width =
out->width;
674 s->alpha_dst_format.height =
out->height;
675 s->alpha_dst_format.depth = odesc->
comp[0].
depth;
677 s->alpha_dst_format.color_family = ZIMG_COLOR_GREY;
679 zimg_filter_graph_free(
s->alpha_graph);
680 s->alpha_graph = zimg_filter_graph_build(&
s->alpha_src_format, &
s->alpha_dst_format, &
s->alpha_params);
681 if (!
s->alpha_graph) {
690 av_reduce(&
out->sample_aspect_ratio.num, &
out->sample_aspect_ratio.den,
695 for (plane = 0; plane < 3; plane++) {
696 int p =
desc->comp[plane].plane;
697 src_buf.plane[plane].data = in->
data[p];
698 src_buf.plane[plane].stride = in->
linesize[p];
699 src_buf.plane[plane].mask = -1;
702 dst_buf.plane[plane].data =
out->data[p];
703 dst_buf.plane[plane].stride =
out->linesize[p];
704 dst_buf.plane[plane].mask = -1;
707 ret = zimg_filter_graph_process(
s->graph, &src_buf, &dst_buf,
s->tmp, 0, 0, 0, 0);
714 src_buf.plane[0].data = in->
data[3];
715 src_buf.plane[0].stride = in->
linesize[3];
716 src_buf.plane[0].mask = -1;
718 dst_buf.plane[0].data =
out->data[3];
719 dst_buf.plane[0].stride =
out->linesize[3];
720 dst_buf.plane[0].mask = -1;
722 ret = zimg_filter_graph_process(
s->alpha_graph, &src_buf, &dst_buf,
s->tmp, 0, 0, 0, 0);
731 for (y = 0; y <
out->height; y++) {
732 for (x = 0; x <
out->width; x++) {
738 for (y = 0; y < outlink->
h; y++)
739 memset(
out->data[3] + y *
out->linesize[3], 0xff, outlink->
w);
757 zimg_filter_graph_free(
s->graph);
758 zimg_filter_graph_free(
s->alpha_graph);
764 char *res,
int res_len,
int flags)
769 if ( !strcmp(cmd,
"width") || !strcmp(cmd,
"w")
770 || !strcmp(cmd,
"height") || !strcmp(cmd,
"h")) {
787 #define OFFSET(x) offsetof(ZScaleContext, x)
788 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
789 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
803 {
"error_diffusion", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_ERROR_DIFFUSION}, 0, 0,
FLAGS,
"dither" },
825 {
"unspecified", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_UNSPECIFIED}, 0, 0,
FLAGS,
"primaries" },
862 {
"iec61966-2-4", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_IEC_61966_2_4},0, 0,
FLAGS,
"transfer" },
863 {
"iec61966-2-1", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_IEC_61966_2_1},0, 0,
FLAGS,
"transfer" },
884 {
"chroma-derived-nc",0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_CHROMATICITY_DERIVED_NCL}, 0, 0,
FLAGS,
"matrix" },
885 {
"chroma-derived-c", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_CHROMATICITY_DERIVED_CL}, 0, 0,
FLAGS,
"matrix" },
890 {
"primariesin",
"set input color primaries",
OFFSET(primaries_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"primaries" },
892 {
"transferin",
"set input transfer characteristic",
OFFSET(trc_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"transfer" },
894 {
"matrixin",
"set input colorspace matrix",
OFFSET(colorspace_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"matrix" },
896 {
"chromal",
"set output chroma location",
OFFSET(chromal),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
897 {
"c",
"set output chroma location",
OFFSET(chromal),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
905 {
"chromalin",
"set input chroma location",
OFFSET(chromal_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
906 {
"cin",
"set input chroma location",
OFFSET(chromal_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
909 {
"param_a",
"parameter A, which is parameter \"b\" for bicubic, "
938 .priv_class = &zscale_class,