formats.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 format_count; ///< 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. typedef struct AVFilterChannelLayouts {
  68. uint64_t *channel_layouts; ///< list of channel layouts
  69. int nb_channel_layouts; ///< number of channel layouts
  70. unsigned refcount; ///< number of references to this list
  71. struct AVFilterChannelLayouts ***refs; ///< references to this list
  72. } AVFilterChannelLayouts;
  73. /**
  74. * Return a channel layouts/samplerates list which contains the intersection of
  75. * the layouts/samplerates of a and b. Also, all the references of a, all the
  76. * references of b, and a and b themselves will be deallocated.
  77. *
  78. * If a and b do not share any common elements, neither is modified, and NULL
  79. * is returned.
  80. */
  81. AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
  82. AVFilterChannelLayouts *b);
  83. AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
  84. AVFilterFormats *b);
  85. /**
  86. * Construct an empty AVFilterChannelLayouts/AVFilterFormats struct --
  87. * representing any channel layout/sample rate.
  88. */
  89. AVFilterChannelLayouts *ff_all_channel_layouts(void);
  90. AVFilterFormats *ff_all_samplerates(void);
  91. AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts);
  92. /**
  93. * A helper for query_formats() which sets all links to the same list of channel
  94. * layouts/sample rates. If there are no links hooked to this filter, the list
  95. * is freed.
  96. */
  97. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  98. AVFilterChannelLayouts *layouts);
  99. void ff_set_common_samplerates(AVFilterContext *ctx,
  100. AVFilterFormats *samplerates);
  101. /**
  102. * A helper for query_formats() which sets all links to the same list of
  103. * formats. If there are no links hooked to this filter, the list of formats is
  104. * freed.
  105. */
  106. void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats);
  107. int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout);
  108. /**
  109. * Add *ref as a new reference to f.
  110. */
  111. void ff_channel_layouts_ref(AVFilterChannelLayouts *f,
  112. AVFilterChannelLayouts **ref);
  113. /**
  114. * Remove a reference to a channel layouts list.
  115. */
  116. void ff_channel_layouts_unref(AVFilterChannelLayouts **ref);
  117. void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
  118. AVFilterChannelLayouts **newref);
  119. int ff_default_query_formats(AVFilterContext *ctx);
  120. /**
  121. * Create a list of supported formats. This is intended for use in
  122. * AVFilter->query_formats().
  123. *
  124. * @param fmts list of media formats, terminated by -1
  125. * @return the format list, with no existing references
  126. */
  127. AVFilterFormats *ff_make_format_list(const int *fmts);
  128. /**
  129. * Add fmt to the list of media formats contained in *avff.
  130. * If *avff is NULL the function allocates the filter formats struct
  131. * and puts its pointer in *avff.
  132. *
  133. * @return a non negative value in case of success, or a negative
  134. * value corresponding to an AVERROR code in case of error
  135. */
  136. int ff_add_format(AVFilterFormats **avff, int64_t fmt);
  137. /**
  138. * Return a list of all formats supported by FFmpeg for the given media type.
  139. */
  140. AVFilterFormats *ff_all_formats(enum AVMediaType type);
  141. /**
  142. * Construct a formats list containing all planar sample formats.
  143. */
  144. AVFilterFormats *ff_planar_sample_fmts(void);
  145. /**
  146. * Return a format list which contains the intersection of the formats of
  147. * a and b. Also, all the references of a, all the references of b, and
  148. * a and b themselves will be deallocated.
  149. *
  150. * If a and b do not share any common formats, neither is modified, and NULL
  151. * is returned.
  152. */
  153. AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b);
  154. /**
  155. * Add *ref as a new reference to formats.
  156. * That is the pointers will point like in the ascii art below:
  157. * ________
  158. * |formats |<--------.
  159. * | ____ | ____|___________________
  160. * | |refs| | | __|_
  161. * | |* * | | | | | | AVFilterLink
  162. * | |* *--------->|*ref|
  163. * | |____| | | |____|
  164. * |________| |________________________
  165. */
  166. void ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
  167. /**
  168. * If *ref is non-NULL, remove *ref as a reference to the format list
  169. * it currently points to, deallocates that list if this was the last
  170. * reference, and sets *ref to NULL.
  171. *
  172. * Before After
  173. * ________ ________ NULL
  174. * |formats |<--------. |formats | ^
  175. * | ____ | ____|________________ | ____ | ____|________________
  176. * | |refs| | | __|_ | |refs| | | __|_
  177. * | |* * | | | | | | AVFilterLink | |* * | | | | | | AVFilterLink
  178. * | |* *--------->|*ref| | |* | | | |*ref|
  179. * | |____| | | |____| | |____| | | |____|
  180. * |________| |_____________________ |________| |_____________________
  181. */
  182. void ff_formats_unref(AVFilterFormats **ref);
  183. /**
  184. *
  185. * Before After
  186. * ________ ________
  187. * |formats |<---------. |formats |<---------.
  188. * | ____ | ___|___ | ____ | ___|___
  189. * | |refs| | | | | | |refs| | | | | NULL
  190. * | |* *--------->|*oldref| | |* *--------->|*newref| ^
  191. * | |* * | | |_______| | |* * | | |_______| ___|___
  192. * | |____| | | |____| | | | |
  193. * |________| |________| |*oldref|
  194. * |_______|
  195. */
  196. void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref);
  197. #endif /* AVFILTER_FORMATS_H */