You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

737 lines
30KB

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