formats.h 11 KB

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