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.

333 lines
11KB

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