cbs.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 "codec_id.h"
  24. #include "codec_par.h"
  25. #include "defs.h"
  26. #include "packet.h"
  27. #ifndef CBS_PREFIX
  28. #define CBS_PREFIX cbs
  29. #endif
  30. #define CBS_FUNC_PREFIX_NAME(prefix, name) ff_ ## prefix ## _ ## name
  31. #define CBS_FUNC_NAME(prefix, name) CBS_FUNC_PREFIX_NAME(prefix, name)
  32. #define CBS_FUNC(name) CBS_FUNC_NAME(CBS_PREFIX, name)
  33. /*
  34. * This defines a framework for converting between a coded bitstream
  35. * and structures defining all individual syntax elements found in
  36. * such a stream.
  37. *
  38. * Conversion in both directions is possible. Given a coded bitstream
  39. * (any meaningful fragment), it can be parsed and decomposed into
  40. * syntax elements stored in a set of codec-specific structures.
  41. * Similarly, given a set of those same codec-specific structures the
  42. * syntax elements can be serialised and combined to create a coded
  43. * bitstream.
  44. */
  45. struct AVCodecContext;
  46. struct CodedBitstreamType;
  47. /**
  48. * The codec-specific type of a bitstream unit.
  49. *
  50. * AV1: obu_type
  51. * H.264 / AVC: nal_unit_type
  52. * H.265 / HEVC: nal_unit_type
  53. * JPEG: marker value (without 0xff prefix)
  54. * MPEG-2: start code value (without prefix)
  55. * VP9: unused, set to zero (every unit is a frame)
  56. */
  57. typedef uint32_t CodedBitstreamUnitType;
  58. /**
  59. * Coded bitstream unit structure.
  60. *
  61. * A bitstream unit the smallest element of a bitstream which
  62. * is meaningful on its own. For example, an H.264 NAL unit.
  63. *
  64. * See the codec-specific header for the meaning of this for any
  65. * particular codec.
  66. */
  67. typedef struct CodedBitstreamUnit {
  68. /**
  69. * Codec-specific type of this unit.
  70. */
  71. CodedBitstreamUnitType type;
  72. /**
  73. * Pointer to the directly-parsable bitstream form of this unit.
  74. *
  75. * May be NULL if the unit currently only exists in decomposed form.
  76. */
  77. uint8_t *data;
  78. /**
  79. * The number of bytes in the bitstream (including any padding bits
  80. * in the final byte).
  81. */
  82. size_t data_size;
  83. /**
  84. * The number of bits which should be ignored in the final byte.
  85. *
  86. * This supports non-byte-aligned bitstreams.
  87. */
  88. size_t data_bit_padding;
  89. /**
  90. * A reference to the buffer containing data.
  91. *
  92. * Must be set if data is not NULL.
  93. */
  94. AVBufferRef *data_ref;
  95. /**
  96. * Pointer to the decomposed form of this unit.
  97. *
  98. * The type of this structure depends on both the codec and the
  99. * type of this unit. May be NULL if the unit only exists in
  100. * bitstream form.
  101. */
  102. void *content;
  103. /**
  104. * If content is reference counted, a RefStruct reference backing content.
  105. * NULL if content is not reference counted.
  106. */
  107. void *content_ref;
  108. } CodedBitstreamUnit;
  109. /**
  110. * Coded bitstream fragment structure, combining one or more units.
  111. *
  112. * This is any sequence of units. It need not form some greater whole,
  113. * though in many cases it will. For example, an H.264 access unit,
  114. * which is composed of a sequence of H.264 NAL units.
  115. */
  116. typedef struct CodedBitstreamFragment {
  117. /**
  118. * Pointer to the bitstream form of this fragment.
  119. *
  120. * May be NULL if the fragment only exists as component units.
  121. */
  122. uint8_t *data;
  123. /**
  124. * The number of bytes in the bitstream.
  125. *
  126. * The number of bytes in the bitstream (including any padding bits
  127. * in the final byte).
  128. */
  129. size_t data_size;
  130. /**
  131. * The number of bits which should be ignored in the final byte.
  132. */
  133. size_t data_bit_padding;
  134. /**
  135. * A reference to the buffer containing data.
  136. *
  137. * Must be set if data is not NULL.
  138. */
  139. AVBufferRef *data_ref;
  140. /**
  141. * Number of units in this fragment.
  142. *
  143. * This may be zero if the fragment only exists in bitstream form
  144. * and has not been decomposed.
  145. */
  146. int nb_units;
  147. /**
  148. * Number of allocated units.
  149. *
  150. * Must always be >= nb_units; designed for internal use by cbs.
  151. */
  152. int nb_units_allocated;
  153. /**
  154. * Pointer to an array of units of length nb_units_allocated.
  155. * Only the first nb_units are valid.
  156. *
  157. * Must be NULL if nb_units_allocated is zero.
  158. */
  159. CodedBitstreamUnit *units;
  160. } CodedBitstreamFragment;
  161. struct CodedBitstreamContext;
  162. struct GetBitContext;
  163. struct PutBitContext;
  164. /**
  165. * Callback type for read tracing.
  166. *
  167. * @param ctx User-set trace context.
  168. * @param gbc A GetBitContext set at the start of the syntax
  169. * element. This is a copy, the callee does not
  170. * need to preserve it.
  171. * @param length Length in bits of the syntax element.
  172. * @param name String name of the syntax elements.
  173. * @param subscripts If the syntax element is an array, a pointer to
  174. * an array of subscripts into the array.
  175. * @param value Parsed value of the syntax element.
  176. */
  177. typedef void (*CBSTraceReadCallback)(void *trace_context,
  178. struct GetBitContext *gbc,
  179. int start_position,
  180. const char *name,
  181. const int *subscripts,
  182. int64_t value);
  183. /**
  184. * Callback type for write tracing.
  185. *
  186. * @param ctx User-set trace context.
  187. * @param pbc A PutBitContext set at the end of the syntax
  188. * element. The user must not modify this, but may
  189. * inspect it to determine state.
  190. * @param length Length in bits of the syntax element.
  191. * @param name String name of the syntax elements.
  192. * @param subscripts If the syntax element is an array, a pointer to
  193. * an array of subscripts into the array.
  194. * @param value Written value of the syntax element.
  195. */
  196. typedef void (*CBSTraceWriteCallback)(void *trace_context,
  197. struct PutBitContext *pbc,
  198. int start_position,
  199. const char *name,
  200. const int *subscripts,
  201. int64_t value);
  202. /**
  203. * Context structure for coded bitstream operations.
  204. */
  205. typedef struct CodedBitstreamContext {
  206. /**
  207. * Logging context to be passed to all av_log() calls associated
  208. * with this context.
  209. */
  210. void *log_ctx;
  211. /**
  212. * Internal codec-specific hooks.
  213. */
  214. const struct CodedBitstreamType *codec;
  215. /**
  216. * Internal codec-specific data.
  217. *
  218. * This contains any information needed when reading/writing
  219. * bitsteams which will not necessarily be present in a fragment.
  220. * For example, for H.264 it contains all currently visible
  221. * parameter sets - they are required to determine the bitstream
  222. * syntax but need not be present in every access unit.
  223. */
  224. void *priv_data;
  225. /**
  226. * Array of unit types which should be decomposed when reading.
  227. *
  228. * Types not in this list will be available in bitstream form only.
  229. * If NULL, all supported types will be decomposed.
  230. */
  231. const CodedBitstreamUnitType *decompose_unit_types;
  232. /**
  233. * Length of the decompose_unit_types array.
  234. */
  235. int nb_decompose_unit_types;
  236. /**
  237. * Enable trace output during read/write operations.
  238. */
  239. int trace_enable;
  240. /**
  241. * Log level to use for default trace output.
  242. *
  243. * From AV_LOG_*; defaults to AV_LOG_TRACE.
  244. */
  245. int trace_level;
  246. /**
  247. * User context pointer to pass to trace callbacks.
  248. */
  249. void *trace_context;
  250. /**
  251. * Callback for read tracing.
  252. *
  253. * If tracing is enabled then this is called once for each syntax
  254. * element parsed.
  255. */
  256. CBSTraceReadCallback trace_read_callback;
  257. /**
  258. * Callback for write tracing.
  259. *
  260. * If tracing is enabled then this is called once for each syntax
  261. * element written.
  262. */
  263. CBSTraceWriteCallback trace_write_callback;
  264. /**
  265. * Write buffer. Used as intermediate buffer when writing units.
  266. * For internal use of cbs only.
  267. */
  268. uint8_t *write_buffer;
  269. size_t write_buffer_size;
  270. } CodedBitstreamContext;
  271. /**
  272. * Table of all supported codec IDs.
  273. *
  274. * Terminated by AV_CODEC_ID_NONE.
  275. */
  276. extern const enum AVCodecID CBS_FUNC(all_codec_ids)[];
  277. /**
  278. * Create and initialise a new context for the given codec.
  279. */
  280. int CBS_FUNC(init)(CodedBitstreamContext **ctx,
  281. enum AVCodecID codec_id, void *log_ctx);
  282. /**
  283. * Reset all internal state in a context.
  284. */
  285. void CBS_FUNC(flush)(CodedBitstreamContext *ctx);
  286. /**
  287. * Close a context and free all internal state.
  288. */
  289. void CBS_FUNC(close)(CodedBitstreamContext **ctx);
  290. /**
  291. * Read the extradata bitstream found in codec parameters into a
  292. * fragment, then split into units and decompose.
  293. *
  294. * This also updates the internal state, so will need to be called for
  295. * codecs with extradata to read parameter sets necessary for further
  296. * parsing even if the fragment itself is not desired.
  297. *
  298. * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
  299. * before use.
  300. */
  301. int CBS_FUNC(read_extradata)(CodedBitstreamContext *ctx,
  302. CodedBitstreamFragment *frag,
  303. const AVCodecParameters *par);
  304. /**
  305. * Read the extradata bitstream found in a codec context into a
  306. * fragment, then split into units and decompose.
  307. *
  308. * This acts identical to ff_cbs_read_extradata() for the case where
  309. * you already have a codec context.
  310. */
  311. int CBS_FUNC(read_extradata_from_codec)(CodedBitstreamContext *ctx,
  312. CodedBitstreamFragment *frag,
  313. const struct AVCodecContext *avctx);
  314. int CBS_FUNC(read_packet_side_data)(CodedBitstreamContext *ctx,
  315. CodedBitstreamFragment *frag,
  316. const AVPacket *pkt);
  317. /**
  318. * Read the data bitstream from a packet into a fragment, then
  319. * split into units and decompose.
  320. *
  321. * This also updates the internal state of the coded bitstream context
  322. * with any persistent data from the fragment which may be required to
  323. * read following fragments (e.g. parameter sets).
  324. *
  325. * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
  326. * before use.
  327. */
  328. int CBS_FUNC(read_packet)(CodedBitstreamContext *ctx,
  329. CodedBitstreamFragment *frag,
  330. const AVPacket *pkt);
  331. /**
  332. * Read a bitstream from a memory region into a fragment, then
  333. * split into units and decompose.
  334. *
  335. * This also updates the internal state of the coded bitstream context
  336. * with any persistent data from the fragment which may be required to
  337. * read following fragments (e.g. parameter sets).
  338. *
  339. * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
  340. * before use.
  341. */
  342. int CBS_FUNC(read)(CodedBitstreamContext *ctx,
  343. CodedBitstreamFragment *frag,
  344. const AVBufferRef *buf,
  345. const uint8_t *data, size_t size);
  346. /**
  347. * Write the content of the fragment to its own internal buffer.
  348. *
  349. * Writes the content of all units and then assembles them into a new
  350. * data buffer. When modifying the content of decomposed units, this
  351. * can be used to regenerate the bitstream form of units or the whole
  352. * fragment so that it can be extracted for other use.
  353. *
  354. * This also updates the internal state of the coded bitstream context
  355. * with any persistent data from the fragment which may be required to
  356. * write following fragments (e.g. parameter sets).
  357. */
  358. int CBS_FUNC(write_fragment_data)(CodedBitstreamContext *ctx,
  359. CodedBitstreamFragment *frag);
  360. /**
  361. * Write the bitstream of a fragment to the extradata in codec parameters.
  362. *
  363. * Modifies context and fragment as ff_cbs_write_fragment_data does and
  364. * replaces any existing extradata in the structure.
  365. */
  366. int CBS_FUNC(write_extradata)(CodedBitstreamContext *ctx,
  367. AVCodecParameters *par,
  368. CodedBitstreamFragment *frag);
  369. /**
  370. * Write the bitstream of a fragment to a packet.
  371. *
  372. * Modifies context and fragment as ff_cbs_write_fragment_data does.
  373. *
  374. * On success, the packet's buf is unreferenced and its buf, data and
  375. * size fields are set to the corresponding values from the newly updated
  376. * fragment; other fields are not touched. On failure, the packet is not
  377. * touched at all.
  378. */
  379. int CBS_FUNC(write_packet)(CodedBitstreamContext *ctx,
  380. AVPacket *pkt,
  381. CodedBitstreamFragment *frag);
  382. /**
  383. * Free the units contained in a fragment as well as the fragment's
  384. * own data buffer, but not the units array itself.
  385. */
  386. void CBS_FUNC(fragment_reset)(CodedBitstreamFragment *frag);
  387. /**
  388. * Free the units array of a fragment in addition to what
  389. * ff_cbs_fragment_reset does.
  390. */
  391. void CBS_FUNC(fragment_free)(CodedBitstreamFragment *frag);
  392. /**
  393. * Allocate a new internal content buffer matching the type of the unit.
  394. *
  395. * The content will be zeroed.
  396. */
  397. int CBS_FUNC(alloc_unit_content)(CodedBitstreamContext *ctx,
  398. CodedBitstreamUnit *unit);
  399. /**
  400. * Insert a new unit into a fragment with the given content.
  401. *
  402. * If content_ref is supplied, it has to be a RefStruct reference
  403. * backing content; the user keeps ownership of the supplied reference.
  404. * The content structure continues to be owned by the caller if
  405. * content_ref is not supplied.
  406. */
  407. int CBS_FUNC(insert_unit_content)(CodedBitstreamFragment *frag,
  408. int position,
  409. CodedBitstreamUnitType type,
  410. void *content,
  411. void *content_ref);
  412. /**
  413. * Add a new unit to a fragment with the given data bitstream.
  414. *
  415. * If data_buf is not supplied then data must have been allocated with
  416. * av_malloc() and will on success become owned by the unit after this
  417. * call or freed on error.
  418. */
  419. int CBS_FUNC(append_unit_data)(CodedBitstreamFragment *frag,
  420. CodedBitstreamUnitType type,
  421. uint8_t *data, size_t data_size,
  422. AVBufferRef *data_buf);
  423. /**
  424. * Delete a unit from a fragment and free all memory it uses.
  425. *
  426. * Requires position to be >= 0 and < frag->nb_units.
  427. */
  428. void CBS_FUNC(delete_unit)(CodedBitstreamFragment *frag,
  429. int position);
  430. /**
  431. * Make the content of a unit refcounted.
  432. *
  433. * If the unit is not refcounted, this will do a deep copy of the unit
  434. * content to new refcounted buffers.
  435. *
  436. * It is not valid to call this function on a unit which does not have
  437. * decomposed content.
  438. */
  439. int CBS_FUNC(make_unit_refcounted)(CodedBitstreamContext *ctx,
  440. CodedBitstreamUnit *unit);
  441. /**
  442. * Make the content of a unit writable so that internal fields can be
  443. * modified.
  444. *
  445. * If it is known that there are no other references to the content of
  446. * the unit, does nothing and returns success. Otherwise (including the
  447. * case where the unit content is not refcounted), it does a full clone
  448. * of the content (including any internal buffers) to make a new copy,
  449. * and replaces the existing references inside the unit with that.
  450. *
  451. * It is not valid to call this function on a unit which does not have
  452. * decomposed content.
  453. */
  454. int CBS_FUNC(make_unit_writable)(CodedBitstreamContext *ctx,
  455. CodedBitstreamUnit *unit);
  456. enum CbsDiscardFlags {
  457. DISCARD_FLAG_NONE = 0,
  458. /**
  459. * keep non-vcl units even if the picture has been dropped.
  460. */
  461. DISCARD_FLAG_KEEP_NON_VCL = 0x01,
  462. };
  463. /**
  464. * Discard units according to 'skip'.
  465. */
  466. void CBS_FUNC(discard_units)(CodedBitstreamContext *ctx,
  467. CodedBitstreamFragment *frag,
  468. enum AVDiscard skip,
  469. int flags);
  470. /**
  471. * Helper function for read tracing which formats the syntax element
  472. * and logs the result.
  473. *
  474. * Trace context should be set to the CodedBitstreamContext.
  475. */
  476. void CBS_FUNC(trace_read_log)(void *trace_context,
  477. struct GetBitContext *gbc, int length,
  478. const char *str, const int *subscripts,
  479. int64_t value);
  480. /**
  481. * Helper function for write tracing which formats the syntax element
  482. * and logs the result.
  483. *
  484. * Trace context should be set to the CodedBitstreamContext.
  485. */
  486. void CBS_FUNC(trace_write_log)(void *trace_context,
  487. struct PutBitContext *pbc, int length,
  488. const char *str, const int *subscripts,
  489. int64_t value);
  490. #endif /* AVCODEC_CBS_H */