graph.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef SWSCALE_GRAPH_H
  21. #define SWSCALE_GRAPH_H
  22. #include "libavutil/slicethread.h"
  23. #include "swscale.h"
  24. #include "utils.h"
  25. /**
  26. * Represents a view into a single field of frame data.
  27. */
  28. typedef struct SwsImg {
  29. enum AVPixelFormat fmt;
  30. uint8_t *data[4]; /* points to y=0 */
  31. int linesize[4];
  32. } SwsImg;
  33. typedef struct SwsPass SwsPass;
  34. typedef struct SwsGraph SwsGraph;
  35. /**
  36. * Output `h` lines of filtered data. `out` and `in` point to the
  37. * start of the image buffer for this pass.
  38. */
  39. typedef void (*sws_filter_run_t)(const SwsImg *out, const SwsImg *in,
  40. int y, int h, const SwsPass *pass);
  41. /**
  42. * Represents a single filter pass in the scaling graph. Each filter will
  43. * read from some previous pass's output, and write to a buffer associated
  44. * with the pass (or into the final output image).
  45. */
  46. struct SwsPass {
  47. const SwsGraph *graph;
  48. /**
  49. * Filter main execution function. Called from multiple threads, with
  50. * the granularity dictated by `slice_h`. Individual slices sent to `run`
  51. * are always equal to (or smaller than, for the last slice) `slice_h`.
  52. */
  53. sws_filter_run_t run;
  54. enum AVPixelFormat format; /* new pixel format */
  55. int width, height; /* new output size */
  56. int slice_h; /* filter granularity */
  57. int num_slices;
  58. /**
  59. * Filter input. This pass's output will be resolved to form this pass's.
  60. * input. If NULL, the original input image is used.
  61. */
  62. const SwsPass *input;
  63. /**
  64. * Filter output buffer. Allocated on demand and freed automatically.
  65. */
  66. SwsImg output;
  67. /**
  68. * Called once from the main thread before running the filter. Optional.
  69. * `out` and `in` always point to the main image input/output, regardless
  70. * of `input` and `output` fields.
  71. */
  72. void (*setup)(const SwsImg *out, const SwsImg *in, const SwsPass *pass);
  73. /**
  74. * Optional private state and associated free() function.
  75. */
  76. void (*free)(void *priv);
  77. void *priv;
  78. };
  79. /**
  80. * Filter graph, which represents a 'baked' pixel format conversion.
  81. */
  82. typedef struct SwsGraph {
  83. SwsContext *ctx;
  84. AVSliceThread *slicethread;
  85. int num_threads; /* resolved at init() time */
  86. int incomplete; /* set during init() if formats had to be inferred */
  87. int noop; /* set during init() if the graph is a no-op */
  88. /** Sorted sequence of filter passes to apply */
  89. SwsPass **passes;
  90. int num_passes;
  91. /**
  92. * Cached copy of the public options that were used to construct this
  93. * SwsGraph. Used only to detect when the graph needs to be reinitialized.
  94. */
  95. SwsContext opts_copy;
  96. /**
  97. * Currently active format and processing parameters.
  98. */
  99. SwsFormat src, dst;
  100. int field;
  101. /** Temporary execution state inside ff_sws_graph_run */
  102. struct {
  103. const SwsPass *pass; /* current filter pass */
  104. SwsImg input;
  105. SwsImg output;
  106. } exec;
  107. } SwsGraph;
  108. /**
  109. * Allocate and initialize the filter graph. Returns 0 or a negative error.
  110. */
  111. int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
  112. int field, SwsGraph **out_graph);
  113. /**
  114. * Uninitialize any state associate with this filter graph and free it.
  115. */
  116. void ff_sws_graph_free(SwsGraph **graph);
  117. /**
  118. * Update dynamic per-frame HDR metadata without requiring a full reinit.
  119. */
  120. void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color);
  121. /**
  122. * Wrapper around ff_sws_graph_create() that reuses the existing graph if the
  123. * format is compatible. This will also update dynamic per-frame metadata.
  124. * Must be called after changing any of the fields in `ctx`, or else they will
  125. * have no effect.
  126. */
  127. int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
  128. int field, SwsGraph **graph);
  129. /**
  130. * Dispatch the filter graph on a single field. Internally threaded.
  131. */
  132. void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
  133. const int out_linesize[4],
  134. const uint8_t *const in_data[4],
  135. const int in_linesize[4]);
  136. #endif /* SWSCALE_GRAPH_H */