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.

346 lines
11KB

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