cbs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 AVCODEC_CBS_H
  19. #define AVCODEC_CBS_H
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "libavutil/buffer.h"
  23. #include "avcodec.h"
  24. /*
  25. * This defines a framework for converting between a coded bitstream
  26. * and structures defining all individual syntax elements found in
  27. * such a stream.
  28. *
  29. * Conversion in both directions is possible. Given a coded bitstream
  30. * (any meaningful fragment), it can be parsed and decomposed into
  31. * syntax elements stored in a set of codec-specific structures.
  32. * Similarly, given a set of those same codec-specific structures the
  33. * syntax elements can be serialised and combined to create a coded
  34. * bitstream.
  35. */
  36. struct CodedBitstreamType;
  37. /**
  38. * The codec-specific type of a bitstream unit.
  39. *
  40. * H.264 / AVC: nal_unit_type
  41. * H.265 / HEVC: nal_unit_type
  42. * MPEG-2: start code value (without prefix)
  43. * VP9: unused, set to zero (every unit is a frame)
  44. */
  45. typedef uint32_t CodedBitstreamUnitType;
  46. /**
  47. * Coded bitstream unit structure.
  48. *
  49. * A bitstream unit the smallest element of a bitstream which
  50. * is meaningful on its own. For example, an H.264 NAL unit.
  51. *
  52. * See the codec-specific header for the meaning of this for any
  53. * particular codec.
  54. */
  55. typedef struct CodedBitstreamUnit {
  56. /**
  57. * Codec-specific type of this unit.
  58. */
  59. CodedBitstreamUnitType type;
  60. /**
  61. * Pointer to the directly-parsable bitstream form of this unit.
  62. *
  63. * May be NULL if the unit currently only exists in decomposed form.
  64. */
  65. uint8_t *data;
  66. /**
  67. * The number of bytes in the bitstream (including any padding bits
  68. * in the final byte).
  69. */
  70. size_t data_size;
  71. /**
  72. * The number of bits which should be ignored in the final byte.
  73. *
  74. * This supports non-byte-aligned bitstreams.
  75. */
  76. size_t data_bit_padding;
  77. /**
  78. * A reference to the buffer containing data.
  79. *
  80. * Must be set if data is not NULL.
  81. */
  82. AVBufferRef *data_ref;
  83. /**
  84. * Pointer to the decomposed form of this unit.
  85. *
  86. * The type of this structure depends on both the codec and the
  87. * type of this unit. May be NULL if the unit only exists in
  88. * bitstream form.
  89. */
  90. void *content;
  91. /**
  92. * If content is reference counted, a reference to the buffer containing
  93. * content. Null if content is not reference counted.
  94. */
  95. AVBufferRef *content_ref;
  96. } CodedBitstreamUnit;
  97. /**
  98. * Coded bitstream fragment structure, combining one or more units.
  99. *
  100. * This is any sequence of units. It need not form some greater whole,
  101. * though in many cases it will. For example, an H.264 access unit,
  102. * which is composed of a sequence of H.264 NAL units.
  103. */
  104. typedef struct CodedBitstreamFragment {
  105. /**
  106. * Pointer to the bitstream form of this fragment.
  107. *
  108. * May be NULL if the fragment only exists as component units.
  109. */
  110. uint8_t *data;
  111. /**
  112. * The number of bytes in the bitstream.
  113. *
  114. * The number of bytes in the bitstream (including any padding bits
  115. * in the final byte).
  116. */
  117. size_t data_size;
  118. /**
  119. * The number of bits which should be ignored in the final byte.
  120. */
  121. size_t data_bit_padding;
  122. /**
  123. * A reference to the buffer containing data.
  124. *
  125. * Must be set if data is not NULL.
  126. */
  127. AVBufferRef *data_ref;
  128. /**
  129. * Number of units in this fragment.
  130. *
  131. * This may be zero if the fragment only exists in bitstream form
  132. * and has not been decomposed.
  133. */
  134. int nb_units;
  135. /**
  136. * Number of allocated units.
  137. *
  138. * Must always be >= nb_units; designed for internal use by cbs.
  139. */
  140. int nb_units_allocated;
  141. /**
  142. * Pointer to an array of units of length nb_units_allocated.
  143. * Only the first nb_units are valid.
  144. *
  145. * Must be NULL if nb_units_allocated is zero.
  146. */
  147. CodedBitstreamUnit *units;
  148. } CodedBitstreamFragment;
  149. /**
  150. * Context structure for coded bitstream operations.
  151. */
  152. typedef struct CodedBitstreamContext {
  153. /**
  154. * Logging context to be passed to all av_log() calls associated
  155. * with this context.
  156. */
  157. void *log_ctx;
  158. /**
  159. * Internal codec-specific hooks.
  160. */
  161. const struct CodedBitstreamType *codec;
  162. /**
  163. * Internal codec-specific data.
  164. *
  165. * This contains any information needed when reading/writing
  166. * bitsteams which will not necessarily be present in a fragment.
  167. * For example, for H.264 it contains all currently visible
  168. * parameter sets - they are required to determine the bitstream
  169. * syntax but need not be present in every access unit.
  170. */
  171. void *priv_data;
  172. /**
  173. * Array of unit types which should be decomposed when reading.
  174. *
  175. * Types not in this list will be available in bitstream form only.
  176. * If NULL, all supported types will be decomposed.
  177. */
  178. CodedBitstreamUnitType *decompose_unit_types;
  179. /**
  180. * Length of the decompose_unit_types array.
  181. */
  182. int nb_decompose_unit_types;
  183. /**
  184. * Enable trace output during read/write operations.
  185. */
  186. int trace_enable;
  187. /**
  188. * Log level to use for trace output.
  189. *
  190. * From AV_LOG_*; defaults to AV_LOG_TRACE.
  191. */
  192. int trace_level;
  193. /**
  194. * Write buffer. Used as intermediate buffer when writing units.
  195. * For internal use of cbs only.
  196. */
  197. uint8_t *write_buffer;
  198. size_t write_buffer_size;
  199. } CodedBitstreamContext;
  200. /**
  201. * Table of all supported codec IDs.
  202. *
  203. * Terminated by AV_CODEC_ID_NONE.
  204. */
  205. extern const enum AVCodecID ff_cbs_all_codec_ids[];
  206. /**
  207. * Create and initialise a new context for the given codec.
  208. */
  209. int ff_cbs_init(CodedBitstreamContext **ctx,
  210. enum AVCodecID codec_id, void *log_ctx);
  211. /**
  212. * Close a context and free all internal state.
  213. */
  214. void ff_cbs_close(CodedBitstreamContext **ctx);
  215. /**
  216. * Read the extradata bitstream found in codec parameters into a
  217. * fragment, then split into units and decompose.
  218. *
  219. * This also updates the internal state, so will need to be called for
  220. * codecs with extradata to read parameter sets necessary for further
  221. * parsing even if the fragment itself is not desired.
  222. *
  223. * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
  224. * before use.
  225. */
  226. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  227. CodedBitstreamFragment *frag,
  228. const AVCodecParameters *par);
  229. /**
  230. * Read the data bitstream from a packet into a fragment, then
  231. * split into units and decompose.
  232. *
  233. * This also updates the internal state of the coded bitstream context
  234. * with any persistent data from the fragment which may be required to
  235. * read following fragments (e.g. parameter sets).
  236. *
  237. * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
  238. * before use.
  239. */
  240. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  241. CodedBitstreamFragment *frag,
  242. const AVPacket *pkt);
  243. /**
  244. * Read a bitstream from a memory region into a fragment, then
  245. * split into units and decompose.
  246. *
  247. * This also updates the internal state of the coded bitstream context
  248. * with any persistent data from the fragment which may be required to
  249. * read following fragments (e.g. parameter sets).
  250. *
  251. * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
  252. * before use.
  253. */
  254. int ff_cbs_read(CodedBitstreamContext *ctx,
  255. CodedBitstreamFragment *frag,
  256. const uint8_t *data, size_t size);
  257. /**
  258. * Write the content of the fragment to its own internal buffer.
  259. *
  260. * Writes the content of all units and then assembles them into a new
  261. * data buffer. When modifying the content of decomposed units, this
  262. * can be used to regenerate the bitstream form of units or the whole
  263. * fragment so that it can be extracted for other use.
  264. *
  265. * This also updates the internal state of the coded bitstream context
  266. * with any persistent data from the fragment which may be required to
  267. * write following fragments (e.g. parameter sets).
  268. */
  269. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  270. CodedBitstreamFragment *frag);
  271. /**
  272. * Write the bitstream of a fragment to the extradata in codec parameters.
  273. *
  274. * Modifies context and fragment as ff_cbs_write_fragment_data does and
  275. * replaces any existing extradata in the structure.
  276. */
  277. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  278. AVCodecParameters *par,
  279. CodedBitstreamFragment *frag);
  280. /**
  281. * Write the bitstream of a fragment to a packet.
  282. *
  283. * Modifies context and fragment as ff_cbs_write_fragment_data does.
  284. *
  285. * On success, the packet's buf is unreferenced and its buf, data and
  286. * size fields are set to the corresponding values from the newly updated
  287. * fragment; other fields are not touched. On failure, the packet is not
  288. * touched at all.
  289. */
  290. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  291. AVPacket *pkt,
  292. CodedBitstreamFragment *frag);
  293. /**
  294. * Free the units contained in a fragment as well as the fragment's
  295. * own data buffer, but not the units array itself.
  296. */
  297. void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
  298. CodedBitstreamFragment *frag);
  299. /**
  300. * Free the units array of a fragment in addition to what
  301. * ff_cbs_fragment_reset does.
  302. */
  303. void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
  304. CodedBitstreamFragment *frag);
  305. /**
  306. * Allocate a new internal content buffer of the given size in the unit.
  307. *
  308. * The content will be zeroed.
  309. */
  310. int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
  311. CodedBitstreamUnit *unit,
  312. size_t size,
  313. void (*free)(void *opaque, uint8_t *content));
  314. /**
  315. * Allocate a new internal data buffer of the given size in the unit.
  316. *
  317. * The data buffer will have input padding.
  318. */
  319. int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
  320. CodedBitstreamUnit *unit,
  321. size_t size);
  322. /**
  323. * Insert a new unit into a fragment with the given content.
  324. *
  325. * The content structure continues to be owned by the caller if
  326. * content_buf is not supplied.
  327. */
  328. int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
  329. CodedBitstreamFragment *frag,
  330. int position,
  331. CodedBitstreamUnitType type,
  332. void *content,
  333. AVBufferRef *content_buf);
  334. /**
  335. * Insert a new unit into a fragment with the given data bitstream.
  336. *
  337. * If data_buf is not supplied then data must have been allocated with
  338. * av_malloc() and will on success become owned by the unit after this
  339. * call or freed on error.
  340. */
  341. int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
  342. CodedBitstreamFragment *frag,
  343. int position,
  344. CodedBitstreamUnitType type,
  345. uint8_t *data, size_t data_size,
  346. AVBufferRef *data_buf);
  347. /**
  348. * Delete a unit from a fragment and free all memory it uses.
  349. *
  350. * Requires position to be >= 0 and < frag->nb_units.
  351. */
  352. void ff_cbs_delete_unit(CodedBitstreamContext *ctx,
  353. CodedBitstreamFragment *frag,
  354. int position);
  355. #endif /* AVCODEC_CBS_H */