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.

390 lines
12KB

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