vf_zscale.c 41 KB

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