film_grain_params.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_FILM_GRAIN_PARAMS_H
  19. #define AVUTIL_FILM_GRAIN_PARAMS_H
  20. #include "frame.h"
  21. enum AVFilmGrainParamsType {
  22. AV_FILM_GRAIN_PARAMS_NONE = 0,
  23. /**
  24. * The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
  25. */
  26. AV_FILM_GRAIN_PARAMS_AV1,
  27. /**
  28. * The union is valid when interpreted as AVFilmGrainH274Params (codec.h274)
  29. */
  30. AV_FILM_GRAIN_PARAMS_H274,
  31. };
  32. /**
  33. * This structure describes how to handle film grain synthesis for AOM codecs.
  34. *
  35. * @note The struct must be allocated as part of AVFilmGrainParams using
  36. * av_film_grain_params_alloc(). Its size is not a part of the public ABI.
  37. */
  38. typedef struct AVFilmGrainAOMParams {
  39. /**
  40. * Number of points, and the scale and value for each point of the
  41. * piecewise linear scaling function for the uma plane.
  42. */
  43. int num_y_points;
  44. uint8_t y_points[14][2 /* value, scaling */];
  45. /**
  46. * Signals whether to derive the chroma scaling function from the luma.
  47. * Not equivalent to copying the luma values and scales.
  48. */
  49. int chroma_scaling_from_luma;
  50. /**
  51. * If chroma_scaling_from_luma is set to 0, signals the chroma scaling
  52. * function parameters.
  53. */
  54. int num_uv_points[2 /* cb, cr */];
  55. uint8_t uv_points[2 /* cb, cr */][10][2 /* value, scaling */];
  56. /**
  57. * Specifies the shift applied to the chroma components. For AV1, its within
  58. * [8; 11] and determines the range and quantization of the film grain.
  59. */
  60. int scaling_shift;
  61. /**
  62. * Specifies the auto-regression lag.
  63. */
  64. int ar_coeff_lag;
  65. /**
  66. * Luma auto-regression coefficients. The number of coefficients is given by
  67. * 2 * ar_coeff_lag * (ar_coeff_lag + 1).
  68. */
  69. int8_t ar_coeffs_y[24];
  70. /**
  71. * Chroma auto-regression coefficients. The number of coefficients is given by
  72. * 2 * ar_coeff_lag * (ar_coeff_lag + 1) + !!num_y_points.
  73. */
  74. int8_t ar_coeffs_uv[2 /* cb, cr */][25];
  75. /**
  76. * Specifies the range of the auto-regressive coefficients. Values of 6,
  77. * 7, 8 and so on represent a range of [-2, 2), [-1, 1), [-0.5, 0.5) and
  78. * so on. For AV1 must be between 6 and 9.
  79. */
  80. int ar_coeff_shift;
  81. /**
  82. * Signals the down shift applied to the generated gaussian numbers during
  83. * synthesis.
  84. */
  85. int grain_scale_shift;
  86. /**
  87. * Specifies the luma/chroma multipliers for the index to the component
  88. * scaling function.
  89. */
  90. int uv_mult[2 /* cb, cr */];
  91. int uv_mult_luma[2 /* cb, cr */];
  92. /**
  93. * Offset used for component scaling function. For AV1 its a 9-bit value
  94. * with a range [-256, 255]
  95. */
  96. int uv_offset[2 /* cb, cr */];
  97. /**
  98. * Signals whether to overlap film grain blocks.
  99. */
  100. int overlap_flag;
  101. /**
  102. * Signals to clip to limited color levels after film grain application.
  103. */
  104. int limit_output_range;
  105. } AVFilmGrainAOMParams;
  106. /**
  107. * This structure describes how to handle film grain synthesis for codecs using
  108. * the ITU-T H.274 Versatile suplemental enhancement information message.
  109. *
  110. * @note The struct must be allocated as part of AVFilmGrainParams using
  111. * av_film_grain_params_alloc(). Its size is not a part of the public ABI.
  112. */
  113. typedef struct AVFilmGrainH274Params {
  114. /**
  115. * Specifies the film grain simulation mode.
  116. * 0 = Frequency filtering, 1 = Auto-regression
  117. */
  118. int model_id;
  119. #if FF_API_H274_FILM_GRAIN_VCS
  120. /**
  121. * TODO: On this ABI bump, please also re-order the fields in
  122. * AVFilmGrainParams (see below)
  123. */
  124. /**
  125. * Specifies the bit depth used for the luma component.
  126. *
  127. * @deprecated use AVFilmGrainParams.bit_depth_luma.
  128. */
  129. attribute_deprecated
  130. int bit_depth_luma;
  131. /**
  132. * Specifies the bit depth used for the chroma components.
  133. *
  134. * @deprecated use AVFilmGrainParams.bit_depth_chroma.
  135. */
  136. attribute_deprecated
  137. int bit_depth_chroma;
  138. /**
  139. * Specifies the video signal characteristics.
  140. *
  141. * @deprecated use AVFilmGrainParams.color_{range,primaries,trc,space}.
  142. */
  143. attribute_deprecated
  144. enum AVColorRange color_range;
  145. attribute_deprecated
  146. enum AVColorPrimaries color_primaries;
  147. attribute_deprecated
  148. enum AVColorTransferCharacteristic color_trc;
  149. attribute_deprecated
  150. enum AVColorSpace color_space;
  151. #endif
  152. /**
  153. * Specifies the blending mode used to blend the simulated film grain
  154. * with the decoded images.
  155. *
  156. * 0 = Additive, 1 = Multiplicative
  157. */
  158. int blending_mode_id;
  159. /**
  160. * Specifies a scale factor used in the film grain characterization equations.
  161. */
  162. int log2_scale_factor;
  163. /**
  164. * Indicates if the modelling of film grain for a given component is present.
  165. */
  166. int component_model_present[3 /* y, cb, cr */];
  167. /**
  168. * Specifies the number of intensity intervals for which a specific set of
  169. * model values has been estimated, with a range of [1, 256].
  170. */
  171. uint16_t num_intensity_intervals[3 /* y, cb, cr */];
  172. /**
  173. * Specifies the number of model values present for each intensity interval
  174. * in which the film grain has been modelled, with a range of [1, 6].
  175. */
  176. uint8_t num_model_values[3 /* y, cb, cr */];
  177. /**
  178. * Specifies the lower ounds of each intensity interval for whichthe set of
  179. * model values applies for the component.
  180. */
  181. uint8_t intensity_interval_lower_bound[3 /* y, cb, cr */][256 /* intensity interval */];
  182. /**
  183. * Specifies the upper bound of each intensity interval for which the set of
  184. * model values applies for the component.
  185. */
  186. uint8_t intensity_interval_upper_bound[3 /* y, cb, cr */][256 /* intensity interval */];
  187. /**
  188. * Specifies the model values for the component for each intensity interval.
  189. * - When model_id == 0, the following applies:
  190. * For comp_model_value[y], the range of values is [0, 2^bit_depth_luma - 1]
  191. * For comp_model_value[cb..cr], the range of values is [0, 2^bit_depth_chroma - 1]
  192. * - Otherwise, the following applies:
  193. * For comp_model_value[y], the range of values is [-2^(bit_depth_luma - 1), 2^(bit_depth_luma - 1) - 1]
  194. * For comp_model_value[cb..cr], the range of values is [-2^(bit_depth_chroma - 1), 2^(bit_depth_chroma - 1) - 1]
  195. */
  196. int16_t comp_model_value[3 /* y, cb, cr */][256 /* intensity interval */][6 /* model value */];
  197. } AVFilmGrainH274Params;
  198. /**
  199. * This structure describes how to handle film grain synthesis in video
  200. * for specific codecs. Must be present on every frame where film grain is
  201. * meant to be synthesised for correct presentation.
  202. *
  203. * @note The struct must be allocated with av_film_grain_params_alloc() and
  204. * its size is not a part of the public ABI.
  205. */
  206. typedef struct AVFilmGrainParams {
  207. /**
  208. * Specifies the codec for which this structure is valid.
  209. */
  210. enum AVFilmGrainParamsType type;
  211. /**
  212. * Seed to use for the synthesis process, if the codec allows for it.
  213. *
  214. * @note For H.264, this refers to `pic_offset` as defined in
  215. * SMPTE RDD 5-2006.
  216. */
  217. uint64_t seed;
  218. /**
  219. * Additional fields may be added both here and in any structure included.
  220. * If a codec's film grain structure differs slightly over another
  221. * codec's, fields within may change meaning depending on the type.
  222. *
  223. * TODO: Move this to the end of the structure, at the next ABI bump.
  224. */
  225. union {
  226. AVFilmGrainAOMParams aom;
  227. AVFilmGrainH274Params h274;
  228. } codec;
  229. /**
  230. * Intended display resolution. May be 0 if the codec does not specify
  231. * any restrictions.
  232. */
  233. int width, height;
  234. /**
  235. * Intended subsampling ratio, or 0 for luma-only streams.
  236. */
  237. int subsampling_x, subsampling_y;
  238. /**
  239. * Intended video signal characteristics.
  240. */
  241. enum AVColorRange color_range;
  242. enum AVColorPrimaries color_primaries;
  243. enum AVColorTransferCharacteristic color_trc;
  244. enum AVColorSpace color_space;
  245. /**
  246. * Intended bit depth, or 0 for unknown/unspecified.
  247. */
  248. int bit_depth_luma;
  249. int bit_depth_chroma;
  250. } AVFilmGrainParams;
  251. /**
  252. * Allocate an AVFilmGrainParams structure and set its fields to
  253. * default values. The resulting struct can be freed using av_freep().
  254. * If size is not NULL it will be set to the number of bytes allocated.
  255. *
  256. * @return An AVFilmGrainParams filled with default values or NULL
  257. * on failure.
  258. */
  259. AVFilmGrainParams *av_film_grain_params_alloc(size_t *size);
  260. /**
  261. * Allocate a complete AVFilmGrainParams and add it to the frame.
  262. *
  263. * @param frame The frame which side data is added to.
  264. *
  265. * @return The AVFilmGrainParams structure to be filled by caller.
  266. */
  267. AVFilmGrainParams *av_film_grain_params_create_side_data(AVFrame *frame);
  268. /**
  269. * Select the most appropriate film grain parameters set for the frame,
  270. * taking into account the frame's format, resolution and video signal
  271. * characteristics.
  272. *
  273. * @note, for H.274, this may select a film grain parameter set with
  274. * greater chroma resolution than the frame. Users should take care to
  275. * correctly adjust the chroma grain frequency to the frame.
  276. */
  277. const AVFilmGrainParams *av_film_grain_params_select(const AVFrame *frame);
  278. #endif /* AVUTIL_FILM_GRAIN_PARAMS_H */