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.

286 lines
8.2KB

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