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.

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