cbs_internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_INTERNAL_H
  19. #define AVCODEC_CBS_INTERNAL_H
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "config.h"
  23. #include "libavutil/log.h"
  24. #include "cbs.h"
  25. #include "codec_id.h"
  26. #include "get_bits.h"
  27. #include "put_bits.h"
  28. #include "libavutil/refstruct.h"
  29. #ifndef CBS_READ
  30. #define CBS_READ 1
  31. #endif
  32. #ifndef CBS_WRITE
  33. #define CBS_WRITE 1
  34. #endif
  35. #ifndef CBS_TRACE
  36. #define CBS_TRACE 1
  37. #endif
  38. #ifndef CBS_APV
  39. #define CBS_APV CONFIG_CBS_APV
  40. #endif
  41. #ifndef CBS_AV1
  42. #define CBS_AV1 CONFIG_CBS_AV1
  43. #endif
  44. #ifndef CBS_H264
  45. #define CBS_H264 CONFIG_CBS_H264
  46. #endif
  47. #ifndef CBS_H265
  48. #define CBS_H265 CONFIG_CBS_H265
  49. #endif
  50. #ifndef CBS_H266
  51. #define CBS_H266 CONFIG_CBS_H266
  52. #endif
  53. #ifndef CBS_JPEG
  54. #define CBS_JPEG CONFIG_CBS_JPEG
  55. #endif
  56. #ifndef CBS_MPEG2
  57. #define CBS_MPEG2 CONFIG_CBS_MPEG2
  58. #endif
  59. #ifndef CBS_VP8
  60. #define CBS_VP8 CONFIG_CBS_VP8
  61. #endif
  62. #ifndef CBS_VP9
  63. #define CBS_VP9 CONFIG_CBS_VP9
  64. #endif
  65. enum CBSContentType {
  66. // Unit content may contain some references to other structures, but all
  67. // managed via buffer reference counting. The descriptor defines the
  68. // structure offsets of every buffer reference.
  69. CBS_CONTENT_TYPE_INTERNAL_REFS,
  70. // Unit content is something more complex. The descriptor defines
  71. // special functions to manage the content.
  72. CBS_CONTENT_TYPE_COMPLEX,
  73. };
  74. enum {
  75. // Maximum number of unit types described by the same non-range
  76. // unit type descriptor.
  77. CBS_MAX_LIST_UNIT_TYPES = 3,
  78. // Maximum number of reference buffer offsets in any one unit.
  79. CBS_MAX_REF_OFFSETS = 2,
  80. // Special value used in a unit type descriptor to indicate that it
  81. // applies to a large range of types rather than a set of discrete
  82. // values.
  83. CBS_UNIT_TYPE_RANGE = -1,
  84. };
  85. typedef const struct CodedBitstreamUnitTypeDescriptor {
  86. // Number of entries in the unit_types array, or the special value
  87. // CBS_UNIT_TYPE_RANGE to indicate that the range fields should be
  88. // used instead.
  89. int nb_unit_types;
  90. union {
  91. // Array of unit types that this entry describes.
  92. CodedBitstreamUnitType list[CBS_MAX_LIST_UNIT_TYPES];
  93. // Start and end of unit type range, used if nb_unit_types is
  94. // CBS_UNIT_TYPE_RANGE.
  95. struct {
  96. CodedBitstreamUnitType start;
  97. CodedBitstreamUnitType end;
  98. } range;
  99. } unit_type;
  100. // The type of content described.
  101. enum CBSContentType content_type;
  102. // The size of the structure which should be allocated to contain
  103. // the decomposed content of this type of unit.
  104. size_t content_size;
  105. union {
  106. // This union's state is determined by content_type:
  107. // ref for CBS_CONTENT_TYPE_INTERNAL_REFS,
  108. // complex for CBS_CONTENT_TYPE_COMPLEX.
  109. struct {
  110. // Number of entries in the ref_offsets array.
  111. // May be zero, then the structure is POD-like.
  112. int nb_offsets;
  113. // The structure must contain two adjacent elements:
  114. // type *field;
  115. // AVBufferRef *field_ref;
  116. // where field points to something in the buffer referred to by
  117. // field_ref. This offset is then set to offsetof(struct, field).
  118. size_t offsets[CBS_MAX_REF_OFFSETS];
  119. } ref;
  120. struct {
  121. void (*content_free)(AVRefStructOpaque opaque, void *content);
  122. int (*content_clone)(void **new_content, CodedBitstreamUnit *unit);
  123. } complex;
  124. } type;
  125. } CodedBitstreamUnitTypeDescriptor;
  126. typedef struct CodedBitstreamType {
  127. enum AVCodecID codec_id;
  128. // A class for the private data, used to declare private AVOptions.
  129. // This field is NULL for types that do not declare any options.
  130. // If this field is non-NULL, the first member of the filter private data
  131. // must be a pointer to AVClass.
  132. const AVClass *priv_class;
  133. size_t priv_data_size;
  134. // List of unit type descriptors for this codec.
  135. // Terminated by a descriptor with nb_unit_types equal to zero.
  136. const CodedBitstreamUnitTypeDescriptor *unit_types;
  137. // Split frag->data into coded bitstream units, creating the
  138. // frag->units array. Fill data but not content on each unit.
  139. // The header argument should be set if the fragment came from
  140. // a header block, which may require different parsing for some
  141. // codecs (e.g. the AVCC header in H.264).
  142. int (*split_fragment)(CodedBitstreamContext *ctx,
  143. CodedBitstreamFragment *frag,
  144. int header);
  145. // Read the unit->data bitstream and decompose it, creating
  146. // unit->content.
  147. int (*read_unit)(CodedBitstreamContext *ctx,
  148. CodedBitstreamUnit *unit);
  149. // Write the data bitstream from unit->content into pbc.
  150. // Return value AVERROR(ENOSPC) indicates that pbc was too small.
  151. int (*write_unit)(CodedBitstreamContext *ctx,
  152. CodedBitstreamUnit *unit,
  153. PutBitContext *pbc);
  154. // Return 1 when the unit should be dropped according to 'skip',
  155. // 0 otherwise.
  156. int (*discarded_unit)(CodedBitstreamContext *ctx,
  157. const CodedBitstreamUnit *unit,
  158. enum AVDiscard skip);
  159. // Read the data from all of frag->units and assemble it into
  160. // a bitstream for the whole fragment.
  161. int (*assemble_fragment)(CodedBitstreamContext *ctx,
  162. CodedBitstreamFragment *frag);
  163. // Reset the codec internal state.
  164. void (*flush)(CodedBitstreamContext *ctx);
  165. // Free the codec internal state.
  166. void (*close)(CodedBitstreamContext *ctx);
  167. } CodedBitstreamType;
  168. // Helper functions for trace output.
  169. void CBS_FUNC(trace_header)(CodedBitstreamContext *ctx,
  170. const char *name);
  171. // Helper functions for read/write of common bitstream elements, including
  172. // generation of trace output. The simple functions are equivalent to
  173. // their non-simple counterparts except that their range is unrestricted
  174. // (i.e. only limited by the amount of bits used) and they lack
  175. // the ability to use subscripts.
  176. int CBS_FUNC(read_unsigned)(CodedBitstreamContext *ctx, GetBitContext *gbc,
  177. int width, const char *name,
  178. const int *subscripts, uint32_t *write_to,
  179. uint32_t range_min, uint32_t range_max);
  180. int CBS_FUNC(read_simple_unsigned)(CodedBitstreamContext *ctx, GetBitContext *gbc,
  181. int width, const char *name, uint32_t *write_to);
  182. int CBS_FUNC(write_unsigned)(CodedBitstreamContext *ctx, PutBitContext *pbc,
  183. int width, const char *name,
  184. const int *subscripts, uint32_t value,
  185. uint32_t range_min, uint32_t range_max);
  186. int CBS_FUNC(write_simple_unsigned)(CodedBitstreamContext *ctx, PutBitContext *pbc,
  187. int width, const char *name, uint32_t value);
  188. int CBS_FUNC(read_signed)(CodedBitstreamContext *ctx, GetBitContext *gbc,
  189. int width, const char *name,
  190. const int *subscripts, int32_t *write_to,
  191. int32_t range_min, int32_t range_max);
  192. int CBS_FUNC(write_signed)(CodedBitstreamContext *ctx, PutBitContext *pbc,
  193. int width, const char *name,
  194. const int *subscripts, int32_t value,
  195. int32_t range_min, int32_t range_max);
  196. // The largest unsigned value representable in N bits, suitable for use as
  197. // range_max in the above functions.
  198. #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
  199. // The largest signed value representable in N bits, suitable for use as
  200. // range_max in the above functions.
  201. #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
  202. // The smallest signed value representable in N bits, suitable for use as
  203. // range_min in the above functions.
  204. #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
  205. #if CBS_TRACE
  206. // Start of a syntax element during read tracing.
  207. #define CBS_TRACE_READ_START() \
  208. GetBitContext trace_start; \
  209. do { \
  210. if (ctx->trace_enable) \
  211. trace_start = *gbc; \
  212. } while (0)
  213. // End of a syntax element for tracing, make callback.
  214. #define CBS_TRACE_READ_END() \
  215. do { \
  216. if (ctx->trace_enable) { \
  217. int start_position = get_bits_count(&trace_start); \
  218. int end_position = get_bits_count(gbc); \
  219. av_assert0(start_position <= end_position); \
  220. ctx->trace_read_callback(ctx->trace_context, &trace_start, \
  221. end_position - start_position, \
  222. name, subscripts, value); \
  223. } \
  224. } while (0)
  225. // End of a syntax element with no subscript entries.
  226. #define CBS_TRACE_READ_END_NO_SUBSCRIPTS() \
  227. do { \
  228. const int *subscripts = NULL; \
  229. CBS_TRACE_READ_END(); \
  230. } while (0)
  231. // End of a syntax element which is made up of subelements which
  232. // are already traced, so we are only showing the value.
  233. #define CBS_TRACE_READ_END_VALUE_ONLY() \
  234. do { \
  235. if (ctx->trace_enable) { \
  236. ctx->trace_read_callback(ctx->trace_context, &trace_start, 0, \
  237. name, subscripts, value); \
  238. } \
  239. } while (0)
  240. // Start of a syntax element during write tracing.
  241. #define CBS_TRACE_WRITE_START() \
  242. int start_position; \
  243. do { \
  244. if (ctx->trace_enable) \
  245. start_position = put_bits_count(pbc);; \
  246. } while (0)
  247. // End of a syntax element for tracing, make callback.
  248. #define CBS_TRACE_WRITE_END() \
  249. do { \
  250. if (ctx->trace_enable) { \
  251. int end_position = put_bits_count(pbc); \
  252. av_assert0(start_position <= end_position); \
  253. ctx->trace_write_callback(ctx->trace_context, pbc, \
  254. end_position - start_position, \
  255. name, subscripts, value); \
  256. } \
  257. } while (0)
  258. // End of a syntax element with no subscript entries.
  259. #define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS() \
  260. do { \
  261. const int *subscripts = NULL; \
  262. CBS_TRACE_WRITE_END(); \
  263. } while (0)
  264. // End of a syntax element which is made up of subelements which are
  265. // already traced, so we are only showing the value. This forges a
  266. // PutBitContext to point to the position of the start of the syntax
  267. // element, but the other state doesn't matter because length is zero.
  268. #define CBS_TRACE_WRITE_END_VALUE_ONLY() \
  269. do { \
  270. if (ctx->trace_enable) { \
  271. PutBitContext tmp; \
  272. init_put_bits(&tmp, pbc->buf, start_position); \
  273. skip_put_bits(&tmp, start_position); \
  274. ctx->trace_write_callback(ctx->trace_context, &tmp, 0, \
  275. name, subscripts, value); \
  276. } \
  277. } while (0)
  278. #else // CBS_TRACE
  279. #define CBS_TRACE_READ_START() do { } while (0)
  280. #define CBS_TRACE_READ_END() do { } while (0)
  281. #define CBS_TRACE_READ_END_NO_SUBSCRIPTS() do { } while (0)
  282. #define CBS_TRACE_READ_END_VALUE_ONLY() do { } while (0)
  283. #define CBS_TRACE_WRITE_START() do { } while (0)
  284. #define CBS_TRACE_WRITE_END() do { } while (0)
  285. #define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS() do { } while (0)
  286. #define CBS_TRACE_WRITE_END_VALUE_ONLY() do { } while (0)
  287. #endif // CBS_TRACE
  288. #define TYPE_LIST(...) { __VA_ARGS__ }
  289. #define CBS_UNIT_TYPE_POD(type_, structure) { \
  290. .nb_unit_types = 1, \
  291. .unit_type.list = { type_ }, \
  292. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  293. .content_size = sizeof(structure), \
  294. .type.ref = { .nb_offsets = 0 }, \
  295. }
  296. #define CBS_UNIT_RANGE_POD(range_start, range_end, structure) { \
  297. .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
  298. .unit_type.range.start = range_start, \
  299. .unit_type.range.end = range_end, \
  300. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  301. .content_size = sizeof(structure), \
  302. .type.ref = { .nb_offsets = 0 }, \
  303. }
  304. #define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field) { \
  305. .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
  306. .unit_type.list = TYPE_LIST types, \
  307. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  308. .content_size = sizeof(structure), \
  309. .type.ref = { .nb_offsets = 1, \
  310. .offsets = { offsetof(structure, ref_field) } }, \
  311. }
  312. #define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field) \
  313. CBS_UNIT_TYPES_INTERNAL_REF((type), structure, ref_field)
  314. #define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field) { \
  315. .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
  316. .unit_type.range.start = range_start, \
  317. .unit_type.range.end = range_end, \
  318. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  319. .content_size = sizeof(structure), \
  320. .type.ref = { .nb_offsets = 1, \
  321. .offsets = { offsetof(structure, ref_field) } }, \
  322. }
  323. #define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func) { \
  324. .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
  325. .unit_type.list = TYPE_LIST types, \
  326. .content_type = CBS_CONTENT_TYPE_COMPLEX, \
  327. .content_size = sizeof(structure), \
  328. .type.complex = { .content_free = free_func }, \
  329. }
  330. #define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func) \
  331. CBS_UNIT_TYPES_COMPLEX((type), structure, free_func)
  332. #define CBS_UNIT_TYPE_END_OF_LIST { .nb_unit_types = 0 }
  333. extern const CodedBitstreamType CBS_FUNC(type_apv);
  334. extern const CodedBitstreamType CBS_FUNC(type_av1);
  335. extern const CodedBitstreamType CBS_FUNC(type_h264);
  336. extern const CodedBitstreamType CBS_FUNC(type_h265);
  337. extern const CodedBitstreamType CBS_FUNC(type_h266);
  338. extern const CodedBitstreamType CBS_FUNC(type_jpeg);
  339. extern const CodedBitstreamType CBS_FUNC(type_mpeg2);
  340. extern const CodedBitstreamType CBS_FUNC(type_vp8);
  341. extern const CodedBitstreamType CBS_FUNC(type_vp9);
  342. #endif /* AVCODEC_CBS_INTERNAL_H */