bsf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Bitstream filters public API
  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 AVCODEC_BSF_H
  21. #define AVCODEC_BSF_H
  22. #include "libavutil/dict.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/rational.h"
  25. #include "codec_id.h"
  26. #include "codec_par.h"
  27. #include "packet.h"
  28. /**
  29. * @addtogroup lavc_core
  30. * @{
  31. */
  32. typedef struct AVBSFInternal AVBSFInternal;
  33. /**
  34. * The bitstream filter state.
  35. *
  36. * This struct must be allocated with av_bsf_alloc() and freed with
  37. * av_bsf_free().
  38. *
  39. * The fields in the struct will only be changed (by the caller or by the
  40. * filter) as described in their documentation, and are to be considered
  41. * immutable otherwise.
  42. */
  43. typedef struct AVBSFContext {
  44. /**
  45. * A class for logging and AVOptions
  46. */
  47. const AVClass *av_class;
  48. /**
  49. * The bitstream filter this context is an instance of.
  50. */
  51. const struct AVBitStreamFilter *filter;
  52. /**
  53. * Opaque libavcodec internal data. Must not be touched by the caller in any
  54. * way.
  55. */
  56. AVBSFInternal *internal;
  57. /**
  58. * Opaque filter-specific private data. If filter->priv_class is non-NULL,
  59. * this is an AVOptions-enabled struct.
  60. */
  61. void *priv_data;
  62. /**
  63. * Parameters of the input stream. This field is allocated in
  64. * av_bsf_alloc(), it needs to be filled by the caller before
  65. * av_bsf_init().
  66. */
  67. AVCodecParameters *par_in;
  68. /**
  69. * Parameters of the output stream. This field is allocated in
  70. * av_bsf_alloc(), it is set by the filter in av_bsf_init().
  71. */
  72. AVCodecParameters *par_out;
  73. /**
  74. * The timebase used for the timestamps of the input packets. Set by the
  75. * caller before av_bsf_init().
  76. */
  77. AVRational time_base_in;
  78. /**
  79. * The timebase used for the timestamps of the output packets. Set by the
  80. * filter in av_bsf_init().
  81. */
  82. AVRational time_base_out;
  83. } AVBSFContext;
  84. typedef struct AVBitStreamFilter {
  85. const char *name;
  86. /**
  87. * A list of codec ids supported by the filter, terminated by
  88. * AV_CODEC_ID_NONE.
  89. * May be NULL, in that case the bitstream filter works with any codec id.
  90. */
  91. const enum AVCodecID *codec_ids;
  92. /**
  93. * A class for the private data, used to declare bitstream filter private
  94. * AVOptions. This field is NULL for bitstream filters that do not declare
  95. * any options.
  96. *
  97. * If this field is non-NULL, the first member of the filter private data
  98. * must be a pointer to AVClass, which will be set by libavcodec generic
  99. * code to this class.
  100. */
  101. const AVClass *priv_class;
  102. /*****************************************************************
  103. * No fields below this line are part of the public API. They
  104. * may not be used outside of libavcodec and can be changed and
  105. * removed at will.
  106. * New public fields should be added right above.
  107. *****************************************************************
  108. */
  109. int priv_data_size;
  110. int (*init)(AVBSFContext *ctx);
  111. int (*filter)(AVBSFContext *ctx, AVPacket *pkt);
  112. void (*close)(AVBSFContext *ctx);
  113. void (*flush)(AVBSFContext *ctx);
  114. } AVBitStreamFilter;
  115. /**
  116. * @return a bitstream filter with the specified name or NULL if no such
  117. * bitstream filter exists.
  118. */
  119. const AVBitStreamFilter *av_bsf_get_by_name(const char *name);
  120. /**
  121. * Iterate over all registered bitstream filters.
  122. *
  123. * @param opaque a pointer where libavcodec will store the iteration state. Must
  124. * point to NULL to start the iteration.
  125. *
  126. * @return the next registered bitstream filter or NULL when the iteration is
  127. * finished
  128. */
  129. const AVBitStreamFilter *av_bsf_iterate(void **opaque);
  130. /**
  131. * Allocate a context for a given bitstream filter. The caller must fill in the
  132. * context parameters as described in the documentation and then call
  133. * av_bsf_init() before sending any data to the filter.
  134. *
  135. * @param filter the filter for which to allocate an instance.
  136. * @param ctx a pointer into which the pointer to the newly-allocated context
  137. * will be written. It must be freed with av_bsf_free() after the
  138. * filtering is done.
  139. *
  140. * @return 0 on success, a negative AVERROR code on failure
  141. */
  142. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx);
  143. /**
  144. * Prepare the filter for use, after all the parameters and options have been
  145. * set.
  146. */
  147. int av_bsf_init(AVBSFContext *ctx);
  148. /**
  149. * Submit a packet for filtering.
  150. *
  151. * After sending each packet, the filter must be completely drained by calling
  152. * av_bsf_receive_packet() repeatedly until it returns AVERROR(EAGAIN) or
  153. * AVERROR_EOF.
  154. *
  155. * @param pkt the packet to filter. The bitstream filter will take ownership of
  156. * the packet and reset the contents of pkt. pkt is not touched if an error occurs.
  157. * If pkt is empty (i.e. NULL, or pkt->data is NULL and pkt->side_data_elems zero),
  158. * it signals the end of the stream (i.e. no more non-empty packets will be sent;
  159. * sending more empty packets does nothing) and will cause the filter to output
  160. * any packets it may have buffered internally.
  161. *
  162. * @return 0 on success. AVERROR(EAGAIN) if packets need to be retrieved from the
  163. * filter (using av_bsf_receive_packet()) before new input can be consumed. Another
  164. * negative AVERROR value if an error occurs.
  165. */
  166. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt);
  167. /**
  168. * Retrieve a filtered packet.
  169. *
  170. * @param[out] pkt this struct will be filled with the contents of the filtered
  171. * packet. It is owned by the caller and must be freed using
  172. * av_packet_unref() when it is no longer needed.
  173. * This parameter should be "clean" (i.e. freshly allocated
  174. * with av_packet_alloc() or unreffed with av_packet_unref())
  175. * when this function is called. If this function returns
  176. * successfully, the contents of pkt will be completely
  177. * overwritten by the returned data. On failure, pkt is not
  178. * touched.
  179. *
  180. * @return 0 on success. AVERROR(EAGAIN) if more packets need to be sent to the
  181. * filter (using av_bsf_send_packet()) to get more output. AVERROR_EOF if there
  182. * will be no further output from the filter. Another negative AVERROR value if
  183. * an error occurs.
  184. *
  185. * @note one input packet may result in several output packets, so after sending
  186. * a packet with av_bsf_send_packet(), this function needs to be called
  187. * repeatedly until it stops returning 0. It is also possible for a filter to
  188. * output fewer packets than were sent to it, so this function may return
  189. * AVERROR(EAGAIN) immediately after a successful av_bsf_send_packet() call.
  190. */
  191. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt);
  192. /**
  193. * Reset the internal bitstream filter state. Should be called e.g. when seeking.
  194. */
  195. void av_bsf_flush(AVBSFContext *ctx);
  196. /**
  197. * Free a bitstream filter context and everything associated with it; write NULL
  198. * into the supplied pointer.
  199. */
  200. void av_bsf_free(AVBSFContext **ctx);
  201. /**
  202. * Get the AVClass for AVBSFContext. It can be used in combination with
  203. * AV_OPT_SEARCH_FAKE_OBJ for examining options.
  204. *
  205. * @see av_opt_find().
  206. */
  207. const AVClass *av_bsf_get_class(void);
  208. /**
  209. * Structure for chain/list of bitstream filters.
  210. * Empty list can be allocated by av_bsf_list_alloc().
  211. */
  212. typedef struct AVBSFList AVBSFList;
  213. /**
  214. * Allocate empty list of bitstream filters.
  215. * The list must be later freed by av_bsf_list_free()
  216. * or finalized by av_bsf_list_finalize().
  217. *
  218. * @return Pointer to @ref AVBSFList on success, NULL in case of failure
  219. */
  220. AVBSFList *av_bsf_list_alloc(void);
  221. /**
  222. * Free list of bitstream filters.
  223. *
  224. * @param lst Pointer to pointer returned by av_bsf_list_alloc()
  225. */
  226. void av_bsf_list_free(AVBSFList **lst);
  227. /**
  228. * Append bitstream filter to the list of bitstream filters.
  229. *
  230. * @param lst List to append to
  231. * @param bsf Filter context to be appended
  232. *
  233. * @return >=0 on success, negative AVERROR in case of failure
  234. */
  235. int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf);
  236. /**
  237. * Construct new bitstream filter context given it's name and options
  238. * and append it to the list of bitstream filters.
  239. *
  240. * @param lst List to append to
  241. * @param bsf_name Name of the bitstream filter
  242. * @param options Options for the bitstream filter, can be set to NULL
  243. *
  244. * @return >=0 on success, negative AVERROR in case of failure
  245. */
  246. int av_bsf_list_append2(AVBSFList *lst, const char * bsf_name, AVDictionary **options);
  247. /**
  248. * Finalize list of bitstream filters.
  249. *
  250. * This function will transform @ref AVBSFList to single @ref AVBSFContext,
  251. * so the whole chain of bitstream filters can be treated as single filter
  252. * freshly allocated by av_bsf_alloc().
  253. * If the call is successful, @ref AVBSFList structure is freed and lst
  254. * will be set to NULL. In case of failure, caller is responsible for
  255. * freeing the structure by av_bsf_list_free()
  256. *
  257. * @param lst Filter list structure to be transformed
  258. * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure
  259. * representing the chain of bitstream filters
  260. *
  261. * @return >=0 on success, negative AVERROR in case of failure
  262. */
  263. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf);
  264. /**
  265. * Parse string describing list of bitstream filters and create single
  266. * @ref AVBSFContext describing the whole chain of bitstream filters.
  267. * Resulting @ref AVBSFContext can be treated as any other @ref AVBSFContext freshly
  268. * allocated by av_bsf_alloc().
  269. *
  270. * @param str String describing chain of bitstream filters in format
  271. * `bsf1[=opt1=val1:opt2=val2][,bsf2]`
  272. * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure
  273. * representing the chain of bitstream filters
  274. *
  275. * @return >=0 on success, negative AVERROR in case of failure
  276. */
  277. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf);
  278. /**
  279. * Get null/pass-through bitstream filter.
  280. *
  281. * @param[out] bsf Pointer to be set to new instance of pass-through bitstream filter
  282. *
  283. * @return
  284. */
  285. int av_bsf_get_null_filter(AVBSFContext **bsf);
  286. /**
  287. * @}
  288. */
  289. #endif // AVCODEC_BSF_H