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.

354 lines
11KB

  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. */
  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. * Table of all supported codec IDs.
  186. *
  187. * Terminated by AV_CODEC_ID_NONE.
  188. */
  189. extern const enum AVCodecID ff_cbs_all_codec_ids[];
  190. /**
  191. * Create and initialise a new context for the given codec.
  192. */
  193. int ff_cbs_init(CodedBitstreamContext **ctx,
  194. enum AVCodecID codec_id, void *log_ctx);
  195. /**
  196. * Close a context and free all internal state.
  197. */
  198. void ff_cbs_close(CodedBitstreamContext **ctx);
  199. /**
  200. * Read the extradata bitstream found in codec parameters into a
  201. * fragment, then split into units and decompose.
  202. *
  203. * This also updates the internal state, so will need to be called for
  204. * codecs with extradata to read parameter sets necessary for further
  205. * parsing even if the fragment itself is not desired.
  206. */
  207. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  208. CodedBitstreamFragment *frag,
  209. const AVCodecParameters *par);
  210. /**
  211. * Read the data bitstream from a packet into a fragment, then
  212. * split into units and decompose.
  213. *
  214. * This also updates the internal state of the coded bitstream context
  215. * with any persistent data from the fragment which may be required to
  216. * read following fragments (e.g. parameter sets).
  217. */
  218. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  219. CodedBitstreamFragment *frag,
  220. const AVPacket *pkt);
  221. /**
  222. * Read a bitstream from a memory region into a fragment, then
  223. * split into units and decompose.
  224. *
  225. * This also updates the internal state of the coded bitstream context
  226. * with any persistent data from the fragment which may be required to
  227. * read following fragments (e.g. parameter sets).
  228. */
  229. int ff_cbs_read(CodedBitstreamContext *ctx,
  230. CodedBitstreamFragment *frag,
  231. const uint8_t *data, size_t size);
  232. /**
  233. * Write the content of the fragment to its own internal buffer.
  234. *
  235. * Writes the content of all units and then assembles them into a new
  236. * data buffer. When modifying the content of decomposed units, this
  237. * can be used to regenerate the bitstream form of units or the whole
  238. * fragment so that it can be extracted for other use.
  239. *
  240. * This also updates the internal state of the coded bitstream context
  241. * with any persistent data from the fragment which may be required to
  242. * write following fragments (e.g. parameter sets).
  243. */
  244. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  245. CodedBitstreamFragment *frag);
  246. /**
  247. * Write the bitstream of a fragment to the extradata in codec parameters.
  248. *
  249. * This replaces any existing extradata in the structure.
  250. */
  251. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  252. AVCodecParameters *par,
  253. CodedBitstreamFragment *frag);
  254. /**
  255. * Write the bitstream of a fragment to a packet.
  256. */
  257. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  258. AVPacket *pkt,
  259. CodedBitstreamFragment *frag);
  260. /**
  261. * Free all allocated memory in a fragment.
  262. */
  263. void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
  264. CodedBitstreamFragment *frag);
  265. /**
  266. * Allocate a new internal content buffer of the given size in the unit.
  267. *
  268. * The content will be zeroed.
  269. */
  270. int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
  271. CodedBitstreamUnit *unit,
  272. size_t size,
  273. void (*free)(void *unit, uint8_t *content));
  274. /**
  275. * Allocate a new internal data buffer of the given size in the unit.
  276. *
  277. * The data buffer will have input padding.
  278. */
  279. int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
  280. CodedBitstreamUnit *unit,
  281. size_t size);
  282. /**
  283. * Insert a new unit into a fragment with the given content.
  284. *
  285. * The content structure continues to be owned by the caller if
  286. * content_buf is not supplied.
  287. */
  288. int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
  289. CodedBitstreamFragment *frag,
  290. int position,
  291. CodedBitstreamUnitType type,
  292. void *content,
  293. AVBufferRef *content_buf);
  294. /**
  295. * Insert a new unit into a fragment with the given data bitstream.
  296. *
  297. * If data_buf is not supplied then data must have been allocated with
  298. * av_malloc() and will become owned by the unit after this call.
  299. */
  300. int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
  301. CodedBitstreamFragment *frag,
  302. int position,
  303. CodedBitstreamUnitType type,
  304. uint8_t *data, size_t data_size,
  305. AVBufferRef *data_buf);
  306. /**
  307. * Delete a unit from a fragment and free all memory it uses.
  308. */
  309. int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
  310. CodedBitstreamFragment *frag,
  311. int position);
  312. #endif /* AVCODEC_CBS_H */