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.

323 lines
12KB

  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 AVFILTER_FORMATS_H
  19. #define AVFILTER_FORMATS_H
  20. #include "avfilter.h"
  21. #include "version.h"
  22. /**
  23. * A list of supported formats for one end of a filter link. This is used
  24. * during the format negotiation process to try to pick the best format to
  25. * use to minimize the number of necessary conversions. Each filter gives a
  26. * list of the formats supported by each input and output pad. The list
  27. * given for each pad need not be distinct - they may be references to the
  28. * same list of formats, as is often the case when a filter supports multiple
  29. * formats, but will always output the same format as it is given in input.
  30. *
  31. * In this way, a list of possible input formats and a list of possible
  32. * output formats are associated with each link. When a set of formats is
  33. * negotiated over a link, the input and output lists are merged to form a
  34. * new list containing only the common elements of each list. In the case
  35. * that there were no common elements, a format conversion is necessary.
  36. * Otherwise, the lists are merged, and all other links which reference
  37. * either of the format lists involved in the merge are also affected.
  38. *
  39. * For example, consider the filter chain:
  40. * filter (a) --> (b) filter (b) --> (c) filter
  41. *
  42. * where the letters in parenthesis indicate a list of formats supported on
  43. * the input or output of the link. Suppose the lists are as follows:
  44. * (a) = {A, B}
  45. * (b) = {A, B, C}
  46. * (c) = {B, C}
  47. *
  48. * First, the first link's lists are merged, yielding:
  49. * filter (a) --> (a) filter (a) --> (c) filter
  50. *
  51. * Notice that format list (b) now refers to the same list as filter list (a).
  52. * Next, the lists for the second link are merged, yielding:
  53. * filter (a) --> (a) filter (a) --> (a) filter
  54. *
  55. * where (a) = {B}.
  56. *
  57. * Unfortunately, when the format lists at the two ends of a link are merged,
  58. * we must ensure that all links which reference either pre-merge format list
  59. * get updated as well. Therefore, we have the format list structure store a
  60. * pointer to each of the pointers to itself.
  61. */
  62. struct AVFilterFormats {
  63. unsigned nb_formats; ///< number of formats
  64. int *formats; ///< list of media formats
  65. unsigned refcount; ///< number of references to this list
  66. struct AVFilterFormats ***refs; ///< references to this list
  67. };
  68. /**
  69. * A list of supported channel layouts.
  70. *
  71. * The list works the same as AVFilterFormats, except for the following
  72. * differences:
  73. * - A list with all_layouts = 1 means all channel layouts with a known
  74. * disposition; nb_channel_layouts must then be 0.
  75. * - A list with all_counts = 1 means all channel counts, with a known or
  76. * unknown disposition; nb_channel_layouts must then be 0 and all_layouts 1.
  77. * - The list must not contain a layout with a known disposition and a
  78. * channel count with unknown disposition with the same number of channels
  79. * (e.g. AV_CH_LAYOUT_STEREO and FF_COUNT2LAYOUT(2).
  80. */
  81. struct AVFilterChannelLayouts {
  82. uint64_t *channel_layouts; ///< list of channel layouts
  83. int nb_channel_layouts; ///< number of channel layouts
  84. char all_layouts; ///< accept any known channel layout
  85. char all_counts; ///< accept any channel layout or count
  86. unsigned refcount; ///< number of references to this list
  87. struct AVFilterChannelLayouts ***refs; ///< references to this list
  88. };
  89. /**
  90. * Encode a channel count as a channel layout.
  91. * FF_COUNT2LAYOUT(c) means any channel layout with c channels, with a known
  92. * or unknown disposition.
  93. * The result is only valid inside AVFilterChannelLayouts and immediately
  94. * related functions.
  95. */
  96. #define FF_COUNT2LAYOUT(c) (0x8000000000000000ULL | (c))
  97. /**
  98. * Decode a channel count encoded as a channel layout.
  99. * Return 0 if the channel layout was a real one.
  100. */
  101. #define FF_LAYOUT2COUNT(l) (((l) & 0x8000000000000000ULL) ? \
  102. (int)((l) & 0x7FFFFFFF) : 0)
  103. /**
  104. * Check the formats/samplerates lists for compatibility for merging
  105. * without actually merging.
  106. *
  107. * @return 1 if they are compatible, 0 if not.
  108. */
  109. int ff_can_merge_formats(const AVFilterFormats *a, const AVFilterFormats *b,
  110. enum AVMediaType type);
  111. int ff_can_merge_samplerates(const AVFilterFormats *a, const AVFilterFormats *b);
  112. /**
  113. * Merge the formats/channel layouts/samplerates lists if they are compatible
  114. * and update all the references of a and b to point to the combined list and
  115. * free the old lists as needed. The combined list usually contains the
  116. * intersection of the lists of a and b.
  117. *
  118. * Both a and b must have owners (i.e. refcount > 0) for these functions.
  119. *
  120. * @return 1 if merging succeeded, 0 if a and b are incompatible
  121. * and negative AVERROR code on failure.
  122. * a and b are unmodified if 0 is returned.
  123. */
  124. int ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  125. AVFilterChannelLayouts *b);
  126. int ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
  127. enum AVMediaType type);
  128. int ff_merge_samplerates(AVFilterFormats *a, AVFilterFormats *b);
  129. /**
  130. * Construct an empty AVFilterChannelLayouts/AVFilterFormats struct --
  131. * representing any channel layout (with known disposition)/sample rate.
  132. */
  133. av_warn_unused_result
  134. AVFilterChannelLayouts *ff_all_channel_layouts(void);
  135. av_warn_unused_result
  136. AVFilterFormats *ff_all_samplerates(void);
  137. /**
  138. * Construct an AVFilterChannelLayouts coding for any channel layout, with
  139. * known or unknown disposition.
  140. */
  141. av_warn_unused_result
  142. AVFilterChannelLayouts *ff_all_channel_counts(void);
  143. av_warn_unused_result
  144. AVFilterChannelLayouts *ff_make_format64_list(const int64_t *fmts);
  145. #if LIBAVFILTER_VERSION_MAJOR < 8
  146. AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts);
  147. #endif
  148. /**
  149. * A helper for query_formats() which sets all links to the same list of channel
  150. * layouts/sample rates. If there are no links hooked to this filter, the list
  151. * is freed.
  152. */
  153. av_warn_unused_result
  154. int ff_set_common_channel_layouts(AVFilterContext *ctx,
  155. AVFilterChannelLayouts *layouts);
  156. av_warn_unused_result
  157. int ff_set_common_samplerates(AVFilterContext *ctx,
  158. AVFilterFormats *samplerates);
  159. /**
  160. * A helper for query_formats() which sets all links to the same list of
  161. * formats. If there are no links hooked to this filter, the list of formats is
  162. * freed.
  163. */
  164. av_warn_unused_result
  165. int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats);
  166. av_warn_unused_result
  167. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout);
  168. /**
  169. * Add *ref as a new reference to f.
  170. */
  171. av_warn_unused_result
  172. int ff_channel_layouts_ref(AVFilterChannelLayouts *f,
  173. AVFilterChannelLayouts **ref);
  174. /**
  175. * Remove a reference to a channel layouts list.
  176. */
  177. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref);
  178. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  179. AVFilterChannelLayouts **newref);
  180. av_warn_unused_result
  181. int ff_default_query_formats(AVFilterContext *ctx);
  182. /**
  183. * Create a list of supported formats. This is intended for use in
  184. * AVFilter->query_formats().
  185. *
  186. * @param fmts list of media formats, terminated by -1
  187. * @return the format list, with no existing references
  188. */
  189. av_warn_unused_result
  190. AVFilterFormats *ff_make_format_list(const int *fmts);
  191. /**
  192. * Add fmt to the list of media formats contained in *avff.
  193. * If *avff is NULL the function allocates the filter formats struct
  194. * and puts its pointer in *avff.
  195. *
  196. * @return a non negative value in case of success, or a negative
  197. * value corresponding to an AVERROR code in case of error
  198. */
  199. av_warn_unused_result
  200. int ff_add_format(AVFilterFormats **avff, int64_t fmt);
  201. /**
  202. * Return a list of all formats supported by FFmpeg for the given media type.
  203. */
  204. av_warn_unused_result
  205. AVFilterFormats *ff_all_formats(enum AVMediaType type);
  206. /**
  207. * Construct a formats list containing all pixel formats with certain
  208. * properties
  209. */
  210. av_warn_unused_result
  211. int ff_formats_pixdesc_filter(AVFilterFormats **rfmts, unsigned want, unsigned rej);
  212. //* format is software, non-planar with sub-sampling
  213. #define FF_PIX_FMT_FLAG_SW_FLAT_SUB (1 << 24)
  214. /**
  215. * Construct a formats list containing all planar sample formats.
  216. */
  217. av_warn_unused_result
  218. AVFilterFormats *ff_planar_sample_fmts(void);
  219. /**
  220. * Add *ref as a new reference to formats.
  221. * That is the pointers will point like in the ascii art below:
  222. * ________
  223. * |formats |<--------.
  224. * | ____ | ____|___________________
  225. * | |refs| | | __|_
  226. * | |* * | | | | | | AVFilterLink
  227. * | |* *--------->|*ref|
  228. * | |____| | | |____|
  229. * |________| |________________________
  230. */
  231. av_warn_unused_result
  232. int ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
  233. /**
  234. * If *ref is non-NULL, remove *ref as a reference to the format list
  235. * it currently points to, deallocates that list if this was the last
  236. * reference, and sets *ref to NULL.
  237. *
  238. * Before After
  239. * ________ ________ NULL
  240. * |formats |<--------. |formats | ^
  241. * | ____ | ____|________________ | ____ | ____|________________
  242. * | |refs| | | __|_ | |refs| | | __|_
  243. * | |* * | | | | | | AVFilterLink | |* * | | | | | | AVFilterLink
  244. * | |* *--------->|*ref| | |* | | | |*ref|
  245. * | |____| | | |____| | |____| | | |____|
  246. * |________| |_____________________ |________| |_____________________
  247. */
  248. void ff_formats_unref(AVFilterFormats **ref);
  249. /**
  250. * Before After
  251. * ________ ________
  252. * |formats |<---------. |formats |<---------.
  253. * | ____ | ___|___ | ____ | ___|___
  254. * | |refs| | | | | | |refs| | | | | NULL
  255. * | |* *--------->|*oldref| | |* *--------->|*newref| ^
  256. * | |* * | | |_______| | |* * | | |_______| ___|___
  257. * | |____| | | |____| | | | |
  258. * |________| |________| |*oldref|
  259. * |_______|
  260. */
  261. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref);
  262. /**
  263. * Check that fmts is a valid pixel formats list.
  264. *
  265. * In particular, check for duplicates.
  266. */
  267. int ff_formats_check_pixel_formats(void *log, const AVFilterFormats *fmts);
  268. /**
  269. * Check that fmts is a valid sample formats list.
  270. *
  271. * In particular, check for duplicates.
  272. */
  273. int ff_formats_check_sample_formats(void *log, const AVFilterFormats *fmts);
  274. /**
  275. * Check that fmts is a valid sample rates list.
  276. *
  277. * In particular, check for duplicates.
  278. */
  279. int ff_formats_check_sample_rates(void *log, const AVFilterFormats *fmts);
  280. /**
  281. * Check that fmts is a valid channel layouts list.
  282. *
  283. * In particular, check for duplicates.
  284. */
  285. int ff_formats_check_channel_layouts(void *log, const AVFilterChannelLayouts *fmts);
  286. #endif /* AVFILTER_FORMATS_H */