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.

275 lines
7.9KB

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