47 #define ZIMG_ALIGNMENT 64
48 #define MIN_TILESIZE 64
49 #define MAX_THREADS 64
152 zimg_image_format_default(&
s->src_format, ZIMG_API_VERSION);
153 zimg_image_format_default(&
s->dst_format, ZIMG_API_VERSION);
154 zimg_image_format_default(&
s->src_format_tmp, ZIMG_API_VERSION);
155 zimg_image_format_default(&
s->dst_format_tmp, ZIMG_API_VERSION);
157 zimg_image_format_default(&
s->alpha_src_format, ZIMG_API_VERSION);
158 zimg_image_format_default(&
s->alpha_dst_format, ZIMG_API_VERSION);
159 zimg_image_format_default(&
s->alpha_src_format_tmp, ZIMG_API_VERSION);
160 zimg_image_format_default(&
s->alpha_dst_format_tmp, ZIMG_API_VERSION);
162 zimg_graph_builder_params_default(&
s->params, ZIMG_API_VERSION);
163 zimg_graph_builder_params_default(&
s->params_tmp, ZIMG_API_VERSION);
164 zimg_graph_builder_params_default(&
s->alpha_params, ZIMG_API_VERSION);
165 zimg_graph_builder_params_default(&
s->alpha_params_tmp, ZIMG_API_VERSION);
167 if (
s->size_str && (
s->w_expr ||
s->h_expr)) {
169 "Size and width/height expressions cannot be set at the same time.\n");
173 if (
s->w_expr && !
s->h_expr)
174 FFSWAP(
char *,
s->w_expr,
s->size_str);
180 "Invalid size '%s'\n",
s->size_str);
183 snprintf(buf,
sizeof(buf)-1,
"%d",
s->w);
185 snprintf(buf,
sizeof(buf)-1,
"%d",
s->h);
231 s->out_slice_start[0] = 0;
232 for (
int i = 1;
i <
s->nb_threads;
i++) {
236 s->out_slice_end[
s->nb_threads - 1] = out_h;
238 for (
int i = 0;
i <
s->nb_threads;
i++) {
239 s->in_slice_start[
i] =
s->out_slice_start[
i] * in_h / (
double)out_h;
240 s->in_slice_end[
i] =
s->out_slice_end[
i] * in_h / (
double)out_h;
252 double var_values[
VARS_NB], res;
255 int factor_w, factor_h;
263 (
double)
inlink->sample_aspect_ratio.num /
inlink->sample_aspect_ratio.den : 1;
319 if (
s->force_original_aspect_ratio) {
323 if (
s->force_original_aspect_ratio == 1) {
332 if (
w > INT_MAX ||
h > INT_MAX ||
342 if (
inlink->sample_aspect_ratio.num){
349 inlink->sample_aspect_ratio.num,
inlink->sample_aspect_ratio.den,
356 "Error when evaluating the expression '%s'.\n"
357 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
358 expr,
s->w_expr,
s->h_expr);
365 int err_code = zimg_get_last_error(err_msg,
sizeof(err_msg));
374 switch (chroma_location) {
377 return ZIMG_CHROMA_LEFT;
379 return ZIMG_CHROMA_CENTER;
381 return ZIMG_CHROMA_TOP_LEFT;
383 return ZIMG_CHROMA_TOP;
385 return ZIMG_CHROMA_BOTTOM_LEFT;
387 return ZIMG_CHROMA_BOTTOM;
389 return ZIMG_CHROMA_LEFT;
394 switch (colorspace) {
396 return ZIMG_MATRIX_RGB;
398 return ZIMG_MATRIX_709;
400 return ZIMG_MATRIX_UNSPECIFIED;
402 return ZIMG_MATRIX_FCC;
404 return ZIMG_MATRIX_470BG;
406 return ZIMG_MATRIX_170M;
408 return ZIMG_MATRIX_240M;
410 return ZIMG_MATRIX_YCGCO;
412 return ZIMG_MATRIX_2020_NCL;
414 return ZIMG_MATRIX_2020_CL;
416 return ZIMG_MATRIX_CHROMATICITY_DERIVED_NCL;
418 return ZIMG_MATRIX_CHROMATICITY_DERIVED_CL;
420 return ZIMG_MATRIX_ICTCP;
422 return ZIMG_MATRIX_UNSPECIFIED;
429 return ZIMG_TRANSFER_UNSPECIFIED;
431 return ZIMG_TRANSFER_709;
433 return ZIMG_TRANSFER_470_M;
435 return ZIMG_TRANSFER_470_BG;
437 return ZIMG_TRANSFER_601;
439 return ZIMG_TRANSFER_240M;
441 return ZIMG_TRANSFER_LINEAR;
443 return ZIMG_TRANSFER_LOG_100;
445 return ZIMG_TRANSFER_LOG_316;
447 return ZIMG_TRANSFER_IEC_61966_2_4;
449 return ZIMG_TRANSFER_2020_10;
451 return ZIMG_TRANSFER_2020_12;
453 return ZIMG_TRANSFER_ST2084;
455 return ZIMG_TRANSFER_ARIB_B67;
457 return ZIMG_TRANSFER_IEC_61966_2_1;
459 return ZIMG_TRANSFER_UNSPECIFIED;
466 return ZIMG_PRIMARIES_UNSPECIFIED;
468 return ZIMG_PRIMARIES_709;
470 return ZIMG_PRIMARIES_470_M;
472 return ZIMG_PRIMARIES_470_BG;
474 return ZIMG_PRIMARIES_170M;
476 return ZIMG_PRIMARIES_240M;
478 return ZIMG_PRIMARIES_FILM;
480 return ZIMG_PRIMARIES_2020;
482 return ZIMG_PRIMARIES_ST428;
484 return ZIMG_PRIMARIES_ST431_2;
486 return ZIMG_PRIMARIES_ST432_1;
488 return ZIMG_PRIMARIES_EBU3213_E;
490 return ZIMG_PRIMARIES_UNSPECIFIED;
498 return ZIMG_RANGE_LIMITED;
500 return ZIMG_RANGE_FULL;
502 return ZIMG_RANGE_LIMITED;
508 case ZIMG_RANGE_LIMITED:
510 case ZIMG_RANGE_FULL:
519 return ((img_fmt0->chroma_location != img_fmt1->chroma_location) ||
520 #
if ZIMG_API_VERSION >= 0x204
521 (img_fmt0->alpha != img_fmt1->alpha) ||
523 (img_fmt0->color_family != img_fmt1->color_family) ||
524 (img_fmt0->color_primaries != img_fmt1->color_primaries) ||
525 (img_fmt0->depth != img_fmt1->depth) ||
526 (img_fmt0->field_parity != img_fmt1->field_parity) ||
527 (img_fmt0->height != img_fmt1->height) ||
528 (img_fmt0->matrix_coefficients != img_fmt1->matrix_coefficients) ||
529 (img_fmt0->pixel_range != img_fmt1->pixel_range) ||
530 (img_fmt0->pixel_type != img_fmt1->pixel_type) ||
531 (img_fmt0->subsample_h != img_fmt1->subsample_h) ||
532 (img_fmt0->subsample_w != img_fmt1->subsample_w) ||
533 (img_fmt0->transfer_characteristics != img_fmt1->transfer_characteristics) ||
534 (img_fmt0->width != img_fmt1->width));
542 int ret = (parm0->allow_approximate_gamma != parm1->allow_approximate_gamma) ||
543 (parm0->dither_type != parm1->dither_type) ||
544 (parm0->resample_filter != parm1->resample_filter) ||
545 (parm0->resample_filter_uv != parm1->resample_filter_uv);
547 if ((
isnan(parm0->nominal_peak_luminance) == 0) || (
isnan(parm1->nominal_peak_luminance) == 0))
548 ret =
ret || (parm0->nominal_peak_luminance != parm1->nominal_peak_luminance);
549 if ((
isnan(parm0->filter_param_a) == 0) || (
isnan(parm1->filter_param_a) == 0))
550 ret =
ret || (parm0->filter_param_a != parm1->filter_param_a);
551 if ((
isnan(parm0->filter_param_a_uv) == 0) || (
isnan(parm1->filter_param_a_uv) == 0))
552 ret =
ret || (parm0->filter_param_a_uv != parm1->filter_param_a_uv);
553 if ((
isnan(parm0->filter_param_b) == 0) || (
isnan(parm1->filter_param_b) == 0))
554 ret =
ret || (parm0->filter_param_b != parm1->filter_param_b);
555 if ((
isnan(parm0->filter_param_b_uv) == 0) || (
isnan(parm1->filter_param_b_uv) == 0))
556 ret =
ret || (parm0->filter_param_b_uv != parm1->filter_param_b_uv);
584 zimg_image_format src_format;
585 zimg_image_format dst_format;
586 zimg_image_format alpha_src_format;
587 zimg_image_format alpha_dst_format;
588 const double in_slice_start =
s->in_slice_start[job_nr];
589 const double in_slice_end =
s->in_slice_end[job_nr];
590 const int out_slice_start =
s->out_slice_start[job_nr];
591 const int out_slice_end =
s->out_slice_end[job_nr];
593 src_format =
s->src_format;
594 dst_format =
s->dst_format;
598 src_format.active_region.width = in->
width;
599 src_format.active_region.height = in_slice_end - in_slice_start;
600 src_format.active_region.left = 0;
601 src_format.active_region.top = in_slice_start;
603 dst_format.width =
out->width;
604 dst_format.height = out_slice_end - out_slice_start;
606 if (
s->graph[job_nr]) {
607 zimg_filter_graph_free(
s->graph[job_nr]);
609 s->graph[job_nr] = zimg_filter_graph_build(&src_format, &dst_format, &
s->params);
610 if (!
s->graph[job_nr])
613 ret = zimg_filter_graph_get_tmp_size(
s->graph[job_nr], &
size);
624 alpha_src_format =
s->alpha_src_format;
625 alpha_dst_format =
s->alpha_dst_format;
628 alpha_src_format.active_region.width = in->
width;
629 alpha_src_format.active_region.height = in_slice_end - in_slice_start;
630 alpha_src_format.active_region.left = 0;
631 alpha_src_format.active_region.top = in_slice_start;
633 alpha_dst_format.width =
out->width;
634 alpha_dst_format.height = out_slice_end - out_slice_start;
636 if (
s->alpha_graph[job_nr]) {
637 zimg_filter_graph_free(
s->alpha_graph[job_nr]);
639 s->alpha_graph[job_nr] = zimg_filter_graph_build(&alpha_src_format, &alpha_dst_format, &
s->alpha_params);
640 if (!
s->alpha_graph[job_nr])
653 for (plane = 0; plane <
planes; plane++) {
654 int p =
desc->comp[plane].plane;
661 aligned->format = (*frame)->format;
662 aligned->width = (*frame)->width;
663 aligned->height = (*frame)->height;
687 if (
s->colorspace != -1)
690 if (
s->primaries != -1)
699 if (
s->chromal != -1)
710 zimg_image_buffer_const src_buf = { ZIMG_API_VERSION };
711 zimg_image_buffer dst_buf = { ZIMG_API_VERSION };
712 const int out_slice_start =
s->out_slice_start[job_nr];
729 for (
int i = 0;
i < 3;
i++) {
730 const int vsamp =
i >= 1 ?
td->odesc->log2_chroma_h : 0;
732 p =
td->desc->comp[
i].plane;
734 src_buf.plane[
i].data =
td->in->data[p];
735 src_buf.plane[
i].stride =
td->in->linesize[p];
736 src_buf.plane[
i].mask = -1;
738 p =
td->odesc->comp[
i].plane;
739 dst_buf.plane[
i].data =
td->out->data[p] +
td->out->linesize[p] * (out_slice_start >> vsamp);
740 dst_buf.plane[
i].stride =
td->out->linesize[p];
741 dst_buf.plane[
i].mask = -1;
743 if (!
s->graph[job_nr])
745 ret = zimg_filter_graph_process(
s->graph[job_nr], &src_buf, &dst_buf,
s->tmp[job_nr], 0, 0, 0, 0);
750 src_buf.plane[0].data =
td->in->data[3];
751 src_buf.plane[0].stride =
td->in->linesize[3];
752 src_buf.plane[0].mask = -1;
754 dst_buf.plane[0].data =
td->out->data[3] +
td->out->linesize[3] * out_slice_start;
755 dst_buf.plane[0].stride =
td->out->linesize[3];
756 dst_buf.plane[0].mask = -1;
758 if (!
s->alpha_graph[job_nr])
760 ret = zimg_filter_graph_process(
s->alpha_graph[job_nr], &src_buf, &dst_buf,
s->tmp[job_nr], 0, 0, 0, 0);
782 (
link->w != outlink->
w) ||
783 (
link->h != outlink->
h) ||
785 (
s->src_format.chroma_location !=
s->dst_format.chroma_location) ||
786 (
s->src_format.color_family !=
s->dst_format.color_family) ||
787 (
s->src_format.color_primaries !=
s->dst_format.color_primaries) ||
788 (
s->src_format.depth !=
s->dst_format.depth) ||
789 (
s->src_format.matrix_coefficients !=
s->dst_format.matrix_coefficients) ||
790 (
s->src_format.field_parity !=
s->dst_format.field_parity) ||
791 (
s->src_format.pixel_range !=
s->dst_format.pixel_range) ||
792 (
s->src_format.pixel_type !=
s->dst_format.pixel_type) ||
793 (
s->src_format.transfer_characteristics !=
s->dst_format.transfer_characteristics)
809 snprintf(buf,
sizeof(buf)-1,
"%d", outlink->
w);
811 snprintf(buf,
sizeof(buf)-1,
"%d", outlink->
h);
823 s->out_colorspace =
out->colorspace;
824 s->out_trc =
out->color_trc;
825 s->out_primaries =
out->color_primaries;
826 s->out_range =
out->color_range;
830 zimg_image_format_default(&
s->src_format, ZIMG_API_VERSION);
831 zimg_image_format_default(&
s->dst_format, ZIMG_API_VERSION);
832 zimg_graph_builder_params_default(&
s->params, ZIMG_API_VERSION);
835 s->primaries_in,
s->trc_in,
s->range_in,
s->chromal_in);
837 s->primaries,
s->trc,
s->range,
s->chromal);
840 s->params.dither_type =
s->dither;
841 s->params.cpu_type = ZIMG_CPU_AUTO_64B;
842 s->params.resample_filter =
s->filter;
843 s->params.resample_filter_uv =
s->filter;
844 s->params.nominal_peak_luminance =
s->nominal_peak_luminance;
845 s->params.allow_approximate_gamma =
s->approximate_gamma;
846 s->params.filter_param_a =
s->params.filter_param_a_uv =
s->param_a;
847 s->params.filter_param_b =
s->params.filter_param_b_uv =
s->param_b;
850 zimg_image_format_default(&
s->alpha_src_format, ZIMG_API_VERSION);
851 zimg_image_format_default(&
s->alpha_dst_format, ZIMG_API_VERSION);
852 zimg_graph_builder_params_default(&
s->alpha_params, ZIMG_API_VERSION);
854 s->alpha_params.dither_type =
s->dither;
855 s->alpha_params.cpu_type = ZIMG_CPU_AUTO_64B;
856 s->alpha_params.resample_filter =
s->filter;
858 s->alpha_src_format.width = in->
width;
859 s->alpha_src_format.height = in->
height;
860 s->alpha_src_format.depth =
desc->comp[0].depth;
861 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;
862 s->alpha_src_format.color_family = ZIMG_COLOR_GREY;
864 s->alpha_dst_format.depth = odesc->
comp[0].
depth;
866 s->alpha_dst_format.color_family = ZIMG_COLOR_GREY;
870 av_reduce(&
out->sample_aspect_ratio.num, &
out->sample_aspect_ratio.den,
880 memset(
s->jobs_ret, 0,
s->nb_threads *
sizeof(*
s->jobs_ret));
882 for (
int i = 0;
ret >= 0 &&
i <
s->nb_threads;
i++)
883 if (
s->jobs_ret[
i] < 0)
884 ret =
s->jobs_ret[
i];
891 s->src_format_tmp =
s->src_format;
892 s->dst_format_tmp =
s->dst_format;
893 s->params_tmp =
s->params;
895 s->alpha_src_format_tmp =
s->alpha_src_format;
896 s->alpha_dst_format_tmp =
s->alpha_dst_format;
897 s->alpha_params_tmp =
s->alpha_params;
903 for (y = 0; y <
out->height; y++) {
904 const ptrdiff_t row = y *
out->linesize[3];
905 for (x = 0; x <
out->width; x++) {
910 }
else if (
s->dst_format.depth == 8) {
911 for (y = 0; y < outlink->
h; y++)
912 memset(
out->data[3] + y *
out->linesize[3], 0xff, outlink->
w);
914 const uint16_t
max = (1 <<
s->dst_format.depth) - 1;
915 for (y = 0; y < outlink->
h; y++) {
916 const ptrdiff_t row = y *
out->linesize[3];
917 for (x = 0; x <
out->width; x++)
940 for (
int i = 0;
i <
s->nb_threads;
i++) {
943 zimg_filter_graph_free(
s->graph[
i]);
946 if (
s->alpha_graph[
i]) {
947 zimg_filter_graph_free(
s->alpha_graph[
i]);
954 char *res,
int res_len,
int flags)
959 if ( !strcmp(cmd,
"width") || !strcmp(cmd,
"w")
960 || !strcmp(cmd,
"height") || !strcmp(cmd,
"h")) {
977 #define OFFSET(x) offsetof(ZScaleContext, x)
978 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
979 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
993 {
"error_diffusion", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_DITHER_ERROR_DIFFUSION}, 0, 0,
FLAGS,
"dither" },
1015 {
"unspecified", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_PRIMARIES_UNSPECIFIED}, 0, 0,
FLAGS,
"primaries" },
1053 {
"iec61966-2-4", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_IEC_61966_2_4},0, 0,
FLAGS,
"transfer" },
1054 {
"iec61966-2-1", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_TRANSFER_IEC_61966_2_1},0, 0,
FLAGS,
"transfer" },
1075 {
"chroma-derived-nc",0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_CHROMATICITY_DERIVED_NCL}, 0, 0,
FLAGS,
"matrix" },
1076 {
"chroma-derived-c", 0, 0,
AV_OPT_TYPE_CONST, {.i64 = ZIMG_MATRIX_CHROMATICITY_DERIVED_CL}, 0, 0,
FLAGS,
"matrix" },
1078 {
"in_range",
"set input color range",
OFFSET(range_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_RANGE_FULL,
FLAGS,
"range" },
1081 {
"primariesin",
"set input color primaries",
OFFSET(primaries_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"primaries" },
1083 {
"transferin",
"set input transfer characteristic",
OFFSET(trc_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"transfer" },
1084 {
"tin",
"set input transfer characteristic",
OFFSET(trc_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"transfer" },
1085 {
"matrixin",
"set input colorspace matrix",
OFFSET(colorspace_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS,
"matrix" },
1087 {
"chromal",
"set output chroma location",
OFFSET(chromal),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
1088 {
"c",
"set output chroma location",
OFFSET(chromal),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
1096 {
"chromalin",
"set input chroma location",
OFFSET(chromal_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
1097 {
"cin",
"set input chroma location",
OFFSET(chromal_in),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, ZIMG_CHROMA_BOTTOM,
FLAGS,
"chroma" },
1100 {
"param_a",
"parameter A, which is parameter \"b\" for bicubic, "
1129 .priv_class = &zscale_class,