cbs.h 12 KB

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