graph.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (C) 2024 Niklas Haas
  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. #include "libavutil/avassert.h"
  21. #include "libavutil/error.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/macros.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/slicethread.h"
  28. #include "libswscale/swscale.h"
  29. #include "libswscale/utils.h"
  30. #include "cms.h"
  31. #include "lut3d.h"
  32. #include "swscale_internal.h"
  33. #include "graph.h"
  34. static int pass_alloc_output(SwsPass *pass)
  35. {
  36. if (!pass || pass->output.fmt != AV_PIX_FMT_NONE)
  37. return 0;
  38. pass->output.fmt = pass->format;
  39. return av_image_alloc(pass->output.data, pass->output.linesize, pass->width,
  40. pass->num_slices * pass->slice_h, pass->format, 64);
  41. }
  42. /* slice_align should be a power of two, or 0 to disable slice threading */
  43. static SwsPass *pass_add(SwsGraph *graph, void *priv, enum AVPixelFormat fmt,
  44. int w, int h, SwsPass *input, int slice_align,
  45. sws_filter_run_t run)
  46. {
  47. int ret;
  48. SwsPass *pass = av_mallocz(sizeof(*pass));
  49. if (!pass)
  50. return NULL;
  51. pass->graph = graph;
  52. pass->run = run;
  53. pass->priv = priv;
  54. pass->format = fmt;
  55. pass->width = w;
  56. pass->height = h;
  57. pass->input = input;
  58. pass->output.fmt = AV_PIX_FMT_NONE;
  59. ret = pass_alloc_output(input);
  60. if (ret < 0) {
  61. av_free(pass);
  62. return NULL;
  63. }
  64. if (!slice_align) {
  65. pass->slice_h = pass->height;
  66. pass->num_slices = 1;
  67. } else {
  68. pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads;
  69. pass->slice_h = FFALIGN(pass->slice_h, slice_align);
  70. pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h;
  71. }
  72. ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
  73. if (ret < 0)
  74. av_freep(&pass);
  75. return pass;
  76. }
  77. /* Wrapper around pass_add that chains a pass "in-place" */
  78. static int pass_append(SwsGraph *graph, void *priv, enum AVPixelFormat fmt,
  79. int w, int h, SwsPass **pass, int slice_align,
  80. sws_filter_run_t run)
  81. {
  82. SwsPass *new = pass_add(graph, priv, fmt, w, h, *pass, slice_align, run);
  83. if (!new)
  84. return AVERROR(ENOMEM);
  85. *pass = new;
  86. return 0;
  87. }
  88. static int vshift(enum AVPixelFormat fmt, int plane)
  89. {
  90. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  91. return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0;
  92. }
  93. /* Shift an image vertically by y lines */
  94. static SwsImg shift_img(const SwsImg *img_base, int y)
  95. {
  96. SwsImg img = *img_base;
  97. for (int i = 0; i < 4 && img.data[i]; i++)
  98. img.data[i] += (y >> vshift(img.fmt, i)) * img.linesize[i];
  99. return img;
  100. }
  101. static void run_copy(const SwsImg *out_base, const SwsImg *in_base,
  102. int y, int h, const SwsPass *pass)
  103. {
  104. SwsImg in = shift_img(in_base, y);
  105. SwsImg out = shift_img(out_base, y);
  106. for (int i = 0; i < FF_ARRAY_ELEMS(in.data) && in.data[i]; i++) {
  107. const int lines = h >> vshift(in.fmt, i);
  108. if (in.linesize[i] == out.linesize[i]) {
  109. memcpy(out.data[i], in.data[i], lines * out.linesize[i]);
  110. } else {
  111. const int linesize = FFMIN(out.linesize[i], in.linesize[i]);
  112. for (int j = 0; j < lines; j++) {
  113. memcpy(out.data[i], in.data[i], linesize);
  114. in.data[i] += in.linesize[i];
  115. out.data[i] += out.linesize[i];
  116. }
  117. }
  118. }
  119. }
  120. static void run_rgb0(const SwsImg *out, const SwsImg *in, int y, int h,
  121. const SwsPass *pass)
  122. {
  123. SwsInternal *c = pass->priv;
  124. const int x0 = c->src0Alpha - 1;
  125. const int w4 = 4 * pass->width;
  126. const int src_stride = in->linesize[0];
  127. const int dst_stride = out->linesize[0];
  128. const uint8_t *src = in->data[0] + y * src_stride;
  129. uint8_t *dst = out->data[0] + y * dst_stride;
  130. for (int y = 0; y < h; y++) {
  131. memcpy(dst, src, w4 * sizeof(*dst));
  132. for (int x = x0; x < w4; x += 4)
  133. dst[x] = 0xFF;
  134. src += src_stride;
  135. dst += dst_stride;
  136. }
  137. }
  138. static void run_xyz2rgb(const SwsImg *out, const SwsImg *in, int y, int h,
  139. const SwsPass *pass)
  140. {
  141. ff_xyz12Torgb48(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
  142. in->data[0] + y * in->linesize[0], in->linesize[0],
  143. pass->width, h);
  144. }
  145. static void run_rgb2xyz(const SwsImg *out, const SwsImg *in, int y, int h,
  146. const SwsPass *pass)
  147. {
  148. ff_rgb48Toxyz12(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
  149. in->data[0] + y * in->linesize[0], in->linesize[0],
  150. pass->width, h);
  151. }
  152. /***********************************************************************
  153. * Internal ff_swscale() wrapper. This re-uses the legacy scaling API. *
  154. * This is considered fully deprecated, and will be replaced by a full *
  155. * reimplementation ASAP. *
  156. ***********************************************************************/
  157. static void free_legacy_swscale(void *priv)
  158. {
  159. SwsContext *sws = priv;
  160. sws_free_context(&sws);
  161. }
  162. static void setup_legacy_swscale(const SwsImg *out, const SwsImg *in,
  163. const SwsPass *pass)
  164. {
  165. SwsContext *sws = pass->priv;
  166. SwsInternal *c = sws_internal(sws);
  167. if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
  168. for (int i = 0; i < 4; i++)
  169. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
  170. }
  171. if (usePal(sws->src_format))
  172. ff_update_palette(c, (const uint32_t *) in->data[1]);
  173. }
  174. static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
  175. {
  176. SwsContext *sws = pass->priv;
  177. SwsInternal *parent = sws_internal(sws);
  178. if (pass->num_slices == 1)
  179. return sws;
  180. av_assert1(parent->nb_slice_ctx == pass->num_slices);
  181. sws = parent->slice_ctx[y / pass->slice_h];
  182. if (usePal(sws->src_format)) {
  183. SwsInternal *sub = sws_internal(sws);
  184. memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
  185. memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
  186. }
  187. return sws;
  188. }
  189. static void run_legacy_unscaled(const SwsImg *out, const SwsImg *in_base,
  190. int y, int h, const SwsPass *pass)
  191. {
  192. SwsContext *sws = slice_ctx(pass, y);
  193. SwsInternal *c = sws_internal(sws);
  194. const SwsImg in = shift_img(in_base, y);
  195. c->convert_unscaled(c, (const uint8_t *const *) in.data, in.linesize, y, h,
  196. out->data, out->linesize);
  197. }
  198. static void run_legacy_swscale(const SwsImg *out_base, const SwsImg *in,
  199. int y, int h, const SwsPass *pass)
  200. {
  201. SwsContext *sws = slice_ctx(pass, y);
  202. SwsInternal *c = sws_internal(sws);
  203. const SwsImg out = shift_img(out_base, y);
  204. ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
  205. sws->src_h, out.data, out.linesize, y, h);
  206. }
  207. static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos,
  208. const SwsFormat *fmt)
  209. {
  210. enum AVChromaLocation chroma_loc = fmt->loc;
  211. const int sub_x = fmt->desc->log2_chroma_w;
  212. const int sub_y = fmt->desc->log2_chroma_h;
  213. int x_pos, y_pos;
  214. /* Explicitly default to center siting for compatibility with swscale */
  215. if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) {
  216. chroma_loc = AVCHROMA_LOC_CENTER;
  217. graph->incomplete |= sub_x || sub_y;
  218. }
  219. /* av_chroma_location_enum_to_pos() always gives us values in the range from
  220. * 0 to 256, but we need to adjust this to the true value range of the
  221. * subsampling grid, which may be larger for h/v_sub > 1 */
  222. av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc);
  223. x_pos *= (1 << sub_x) - 1;
  224. y_pos *= (1 << sub_y) - 1;
  225. /* Fix vertical chroma position for interlaced frames */
  226. if (sub_y && fmt->interlaced) {
  227. /* When vertically subsampling, chroma samples are effectively only
  228. * placed next to even rows. To access them from the odd field, we need
  229. * to account for this shift by offsetting the distance of one luma row.
  230. *
  231. * For 4x vertical subsampling (v_sub == 2), they are only placed
  232. * next to every *other* even row, so we need to shift by three luma
  233. * rows to get to the chroma sample. */
  234. if (graph->field == FIELD_BOTTOM)
  235. y_pos += (256 << sub_y) - 256;
  236. /* Luma row distance is doubled for fields, so halve offsets */
  237. y_pos >>= 1;
  238. }
  239. /* Explicitly strip chroma offsets when not subsampling, because it
  240. * interferes with the operation of flags like SWS_FULL_CHR_H_INP */
  241. *h_chr_pos = sub_x ? x_pos : -513;
  242. *v_chr_pos = sub_y ? y_pos : -513;
  243. }
  244. static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
  245. {
  246. if (override == -513 || override == *chr_pos)
  247. return;
  248. if (!*warned) {
  249. av_log(NULL, AV_LOG_WARNING,
  250. "Setting chroma position directly is deprecated, make sure "
  251. "the frame is tagged with the correct chroma location.\n");
  252. *warned = 1;
  253. }
  254. *chr_pos = override;
  255. }
  256. static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws,
  257. SwsPass *input, SwsPass **output)
  258. {
  259. SwsInternal *c = sws_internal(sws);
  260. const int src_w = sws->src_w, src_h = sws->src_h;
  261. const int dst_w = sws->dst_w, dst_h = sws->dst_h;
  262. const int unscaled = src_w == dst_w && src_h == dst_h;
  263. int align = c->dst_slice_align;
  264. SwsPass *pass = NULL;
  265. int ret;
  266. if (c->cascaded_context[0]) {
  267. const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
  268. for (int i = 0; i < num_cascaded; i++) {
  269. SwsContext *sub = c->cascaded_context[i];
  270. const int is_last = i + 1 == num_cascaded;
  271. ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
  272. if (ret < 0)
  273. return ret;
  274. /* Steal cascaded context, so we can free the parent */
  275. c->cascaded_context[i] = NULL;
  276. }
  277. sws_free_context(&sws);
  278. return 0;
  279. }
  280. if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
  281. align = 0; /* disable slice threading */
  282. if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
  283. ret = pass_append(graph, c, AV_PIX_FMT_RGBA, src_w, src_h, &input, 1, run_rgb0);
  284. if (ret < 0)
  285. return ret;
  286. }
  287. if (c->srcXYZ && !(c->dstXYZ && unscaled)) {
  288. ret = pass_append(graph, c, AV_PIX_FMT_RGB48, src_w, src_h, &input, 1, run_xyz2rgb);
  289. if (ret < 0)
  290. return ret;
  291. }
  292. pass = pass_add(graph, sws, sws->dst_format, dst_w, dst_h, input, align,
  293. c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale);
  294. if (!pass)
  295. return AVERROR(ENOMEM);
  296. pass->setup = setup_legacy_swscale;
  297. pass->free = free_legacy_swscale;
  298. /**
  299. * For slice threading, we need to create sub contexts, similar to how
  300. * swscale normally handles it internally. The most important difference
  301. * is that we handle cascaded contexts before threaded contexts; whereas
  302. * context_init_threaded() does it the other way around.
  303. */
  304. if (pass->num_slices > 1) {
  305. c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
  306. if (!c->slice_ctx)
  307. return AVERROR(ENOMEM);
  308. for (int i = 0; i < pass->num_slices; i++) {
  309. SwsContext *slice;
  310. SwsInternal *c2;
  311. slice = c->slice_ctx[i] = sws_alloc_context();
  312. if (!slice)
  313. return AVERROR(ENOMEM);
  314. c->nb_slice_ctx++;
  315. c2 = sws_internal(slice);
  316. c2->parent = sws;
  317. ret = av_opt_copy(slice, sws);
  318. if (ret < 0)
  319. return ret;
  320. ret = ff_sws_init_single_context(slice, NULL, NULL);
  321. if (ret < 0)
  322. return ret;
  323. sws_setColorspaceDetails(slice, c->srcColorspaceTable,
  324. slice->src_range, c->dstColorspaceTable,
  325. slice->dst_range, c->brightness, c->contrast,
  326. c->saturation);
  327. for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
  328. c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
  329. c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
  330. }
  331. }
  332. }
  333. if (c->dstXYZ && !(c->srcXYZ && unscaled)) {
  334. ret = pass_append(graph, c, AV_PIX_FMT_RGB48, dst_w, dst_h, &pass, 1, run_rgb2xyz);
  335. if (ret < 0)
  336. return ret;
  337. }
  338. *output = pass;
  339. return 0;
  340. }
  341. static int add_legacy_sws_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst,
  342. SwsPass *input, SwsPass **output)
  343. {
  344. int ret, warned = 0;
  345. SwsContext *const ctx = graph->ctx;
  346. SwsContext *sws = sws_alloc_context();
  347. if (!sws)
  348. return AVERROR(ENOMEM);
  349. sws->flags = ctx->flags;
  350. sws->dither = ctx->dither;
  351. sws->alpha_blend = ctx->alpha_blend;
  352. sws->gamma_flag = ctx->gamma_flag;
  353. sws->src_w = src.width;
  354. sws->src_h = src.height;
  355. sws->src_format = src.format;
  356. sws->src_range = src.range == AVCOL_RANGE_JPEG;
  357. sws->dst_w = dst.width;
  358. sws->dst_h = dst.height;
  359. sws->dst_format = dst.format;
  360. sws->dst_range = dst.range == AVCOL_RANGE_JPEG;
  361. get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, &src);
  362. get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, &dst);
  363. graph->incomplete |= src.range == AVCOL_RANGE_UNSPECIFIED;
  364. graph->incomplete |= dst.range == AVCOL_RANGE_UNSPECIFIED;
  365. /* Allow overriding chroma position with the legacy API */
  366. legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
  367. legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
  368. legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
  369. legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
  370. ret = sws_init_context(sws, NULL, NULL);
  371. if (ret < 0) {
  372. sws_free_context(&sws);
  373. return ret;
  374. }
  375. /* Set correct color matrices */
  376. {
  377. int in_full, out_full, brightness, contrast, saturation;
  378. const int *inv_table, *table;
  379. sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
  380. (int **)&table, &out_full,
  381. &brightness, &contrast, &saturation);
  382. inv_table = sws_getCoefficients(src.csp);
  383. table = sws_getCoefficients(dst.csp);
  384. graph->incomplete |= src.csp != dst.csp &&
  385. (src.csp == AVCOL_SPC_UNSPECIFIED ||
  386. dst.csp == AVCOL_SPC_UNSPECIFIED);
  387. sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
  388. brightness, contrast, saturation);
  389. }
  390. ret = init_legacy_subpass(graph, sws, input, output);
  391. if (ret < 0) {
  392. sws_free_context(&sws);
  393. return ret;
  394. }
  395. return 0;
  396. }
  397. /**************************
  398. * Gamut and tone mapping *
  399. **************************/
  400. static void free_lut3d(void *priv)
  401. {
  402. SwsLut3D *lut = priv;
  403. ff_sws_lut3d_free(&lut);
  404. }
  405. static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass)
  406. {
  407. SwsLut3D *lut = pass->priv;
  408. /* Update dynamic frame metadata from the original source frame */
  409. ff_sws_lut3d_update(lut, &pass->graph->src.color);
  410. }
  411. static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base,
  412. int y, int h, const SwsPass *pass)
  413. {
  414. SwsLut3D *lut = pass->priv;
  415. const SwsImg in = shift_img(in_base, y);
  416. const SwsImg out = shift_img(out_base, y);
  417. ff_sws_lut3d_apply(lut, in.data[0], in.linesize[0], out.data[0],
  418. out.linesize[0], pass->width, h);
  419. }
  420. static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst,
  421. SwsPass *input, SwsPass **output)
  422. {
  423. enum AVPixelFormat fmt_in, fmt_out;
  424. SwsColorMap map = {0};
  425. SwsLut3D *lut;
  426. SwsPass *pass;
  427. int ret;
  428. /**
  429. * Grayspace does not really have primaries, so just force the use of
  430. * the equivalent other primary set to avoid a conversion. Technically,
  431. * this does affect the weights used for the Grayscale conversion, but
  432. * in practise, that should give the expected results more often than not.
  433. */
  434. if (isGray(dst.format)) {
  435. dst.color = src.color;
  436. } else if (isGray(src.format)) {
  437. src.color = dst.color;
  438. }
  439. /* Fully infer color spaces before color mapping logic */
  440. graph->incomplete |= ff_infer_colors(&src.color, &dst.color);
  441. map.intent = graph->ctx->intent;
  442. map.src = src.color;
  443. map.dst = dst.color;
  444. if (ff_sws_color_map_noop(&map))
  445. return 0;
  446. lut = ff_sws_lut3d_alloc();
  447. if (!lut)
  448. return AVERROR(ENOMEM);
  449. fmt_in = ff_sws_lut3d_pick_pixfmt(src, 0);
  450. fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1);
  451. if (fmt_in != src.format) {
  452. SwsFormat tmp = src;
  453. tmp.format = fmt_in;
  454. ret = add_legacy_sws_pass(graph, src, tmp, input, &input);
  455. if (ret < 0)
  456. return ret;
  457. }
  458. ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map);
  459. if (ret < 0) {
  460. ff_sws_lut3d_free(&lut);
  461. return ret;
  462. }
  463. pass = pass_add(graph, lut, fmt_out, src.width, src.height,
  464. input, 1, run_lut3d);
  465. if (!pass) {
  466. ff_sws_lut3d_free(&lut);
  467. return AVERROR(ENOMEM);
  468. }
  469. pass->setup = setup_lut3d;
  470. pass->free = free_lut3d;
  471. *output = pass;
  472. return 0;
  473. }
  474. /***************************************
  475. * Main filter graph construction code *
  476. ***************************************/
  477. static int init_passes(SwsGraph *graph)
  478. {
  479. SwsFormat src = graph->src;
  480. SwsFormat dst = graph->dst;
  481. SwsPass *pass = NULL; /* read from main input image */
  482. int ret;
  483. ret = adapt_colors(graph, src, dst, pass, &pass);
  484. if (ret < 0)
  485. return ret;
  486. src.format = pass ? pass->format : src.format;
  487. src.color = dst.color;
  488. if (!ff_fmt_equal(&src, &dst)) {
  489. ret = add_legacy_sws_pass(graph, src, dst, pass, &pass);
  490. if (ret < 0)
  491. return ret;
  492. }
  493. if (!pass) {
  494. /* No passes were added, so no operations were necessary */
  495. graph->noop = 1;
  496. /* Add threaded memcpy pass */
  497. pass = pass_add(graph, NULL, dst.format, dst.width, dst.height, pass, 1, run_copy);
  498. if (!pass)
  499. return AVERROR(ENOMEM);
  500. }
  501. return 0;
  502. }
  503. static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
  504. int nb_threads)
  505. {
  506. SwsGraph *graph = priv;
  507. const SwsPass *pass = graph->exec.pass;
  508. const SwsImg *input = pass->input ? &pass->input->output : &graph->exec.input;
  509. const SwsImg *output = pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : &graph->exec.output;
  510. const int slice_y = jobnr * pass->slice_h;
  511. const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y);
  512. pass->run(output, input, slice_y, slice_h, pass);
  513. }
  514. int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
  515. int field, SwsGraph **out_graph)
  516. {
  517. int ret;
  518. SwsGraph *graph = av_mallocz(sizeof(*graph));
  519. if (!graph)
  520. return AVERROR(ENOMEM);
  521. graph->ctx = ctx;
  522. graph->src = *src;
  523. graph->dst = *dst;
  524. graph->field = field;
  525. graph->opts_copy = *ctx;
  526. graph->exec.input.fmt = src->format;
  527. graph->exec.output.fmt = dst->format;
  528. ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph,
  529. sws_graph_worker, NULL, ctx->threads);
  530. if (ret == AVERROR(ENOSYS))
  531. graph->num_threads = 1;
  532. else if (ret < 0)
  533. goto error;
  534. else
  535. graph->num_threads = ret;
  536. ret = init_passes(graph);
  537. if (ret < 0)
  538. goto error;
  539. *out_graph = graph;
  540. return 0;
  541. error:
  542. ff_sws_graph_free(&graph);
  543. return ret;
  544. }
  545. void ff_sws_graph_free(SwsGraph **pgraph)
  546. {
  547. SwsGraph *graph = *pgraph;
  548. if (!graph)
  549. return;
  550. avpriv_slicethread_free(&graph->slicethread);
  551. for (int i = 0; i < graph->num_passes; i++) {
  552. SwsPass *pass = graph->passes[i];
  553. if (pass->free)
  554. pass->free(pass->priv);
  555. if (pass->output.fmt != AV_PIX_FMT_NONE)
  556. av_free(pass->output.data[0]);
  557. av_free(pass);
  558. }
  559. av_free(graph->passes);
  560. av_free(graph);
  561. *pgraph = NULL;
  562. }
  563. /* Tests only options relevant to SwsGraph */
  564. static int opts_equal(const SwsContext *c1, const SwsContext *c2)
  565. {
  566. return c1->flags == c2->flags &&
  567. c1->threads == c2->threads &&
  568. c1->dither == c2->dither &&
  569. c1->alpha_blend == c2->alpha_blend &&
  570. c1->gamma_flag == c2->gamma_flag &&
  571. c1->src_h_chr_pos == c2->src_h_chr_pos &&
  572. c1->src_v_chr_pos == c2->src_v_chr_pos &&
  573. c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
  574. c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
  575. c1->intent == c2->intent &&
  576. !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
  577. }
  578. int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
  579. int field, SwsGraph **out_graph)
  580. {
  581. SwsGraph *graph = *out_graph;
  582. if (graph && ff_fmt_equal(&graph->src, src) &&
  583. ff_fmt_equal(&graph->dst, dst) &&
  584. opts_equal(ctx, &graph->opts_copy))
  585. {
  586. ff_sws_graph_update_metadata(graph, &src->color);
  587. return 0;
  588. }
  589. ff_sws_graph_free(out_graph);
  590. return ff_sws_graph_create(ctx, dst, src, field, out_graph);
  591. }
  592. void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
  593. {
  594. if (!color)
  595. return;
  596. ff_color_update_dynamic(&graph->src.color, color);
  597. }
  598. void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
  599. const int out_linesize[4],
  600. const uint8_t *const in_data[4],
  601. const int in_linesize[4])
  602. {
  603. SwsImg *out = &graph->exec.output;
  604. SwsImg *in = &graph->exec.input;
  605. memcpy(out->data, out_data, sizeof(out->data));
  606. memcpy(out->linesize, out_linesize, sizeof(out->linesize));
  607. memcpy(in->data, in_data, sizeof(in->data));
  608. memcpy(in->linesize, in_linesize, sizeof(in->linesize));
  609. for (int i = 0; i < graph->num_passes; i++) {
  610. const SwsPass *pass = graph->passes[i];
  611. graph->exec.pass = pass;
  612. if (pass->setup)
  613. pass->setup(out, in, pass);
  614. avpriv_slicethread_execute(graph->slicethread, pass->num_slices, 0);
  615. }
  616. }