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.

313 lines
9.4KB

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