cbs_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <stdint.h>
  21. #include "libavutil/buffer.h"
  22. #include "libavutil/log.h"
  23. #include "cbs.h"
  24. #include "codec_id.h"
  25. #include "get_bits.h"
  26. #include "put_bits.h"
  27. enum CBSContentType {
  28. // Unit content may contain some references to other structures, but all
  29. // managed via buffer reference counting. The descriptor defines the
  30. // structure offsets of every buffer reference.
  31. CBS_CONTENT_TYPE_INTERNAL_REFS,
  32. // Unit content is something more complex. The descriptor defines
  33. // special functions to manage the content.
  34. CBS_CONTENT_TYPE_COMPLEX,
  35. };
  36. enum {
  37. // Maximum number of unit types described by the same non-range
  38. // unit type descriptor.
  39. CBS_MAX_LIST_UNIT_TYPES = 3,
  40. // Maximum number of reference buffer offsets in any one unit.
  41. CBS_MAX_REF_OFFSETS = 2,
  42. // Special value used in a unit type descriptor to indicate that it
  43. // applies to a large range of types rather than a set of discrete
  44. // values.
  45. CBS_UNIT_TYPE_RANGE = -1,
  46. };
  47. typedef const struct CodedBitstreamUnitTypeDescriptor {
  48. // Number of entries in the unit_types array, or the special value
  49. // CBS_UNIT_TYPE_RANGE to indicate that the range fields should be
  50. // used instead.
  51. int nb_unit_types;
  52. union {
  53. // Array of unit types that this entry describes.
  54. CodedBitstreamUnitType list[CBS_MAX_LIST_UNIT_TYPES];
  55. // Start and end of unit type range, used if nb_unit_types is
  56. // CBS_UNIT_TYPE_RANGE.
  57. struct {
  58. CodedBitstreamUnitType start;
  59. CodedBitstreamUnitType end;
  60. } range;
  61. } unit_type;
  62. // The type of content described.
  63. enum CBSContentType content_type;
  64. // The size of the structure which should be allocated to contain
  65. // the decomposed content of this type of unit.
  66. size_t content_size;
  67. union {
  68. // This union's state is determined by content_type:
  69. // ref for CBS_CONTENT_TYPE_INTERNAL_REFS,
  70. // complex for CBS_CONTENT_TYPE_COMPLEX.
  71. struct {
  72. // Number of entries in the ref_offsets array.
  73. // May be zero, then the structure is POD-like.
  74. int nb_offsets;
  75. // The structure must contain two adjacent elements:
  76. // type *field;
  77. // AVBufferRef *field_ref;
  78. // where field points to something in the buffer referred to by
  79. // field_ref. This offset is then set to offsetof(struct, field).
  80. size_t offsets[CBS_MAX_REF_OFFSETS];
  81. } ref;
  82. struct {
  83. void (*content_free)(void *opaque, uint8_t *data);
  84. int (*content_clone)(AVBufferRef **ref, CodedBitstreamUnit *unit);
  85. } complex;
  86. } type;
  87. } CodedBitstreamUnitTypeDescriptor;
  88. typedef struct CodedBitstreamType {
  89. enum AVCodecID codec_id;
  90. // A class for the private data, used to declare private AVOptions.
  91. // This field is NULL for types that do not declare any options.
  92. // If this field is non-NULL, the first member of the filter private data
  93. // must be a pointer to AVClass.
  94. const AVClass *priv_class;
  95. size_t priv_data_size;
  96. // List of unit type descriptors for this codec.
  97. // Terminated by a descriptor with nb_unit_types equal to zero.
  98. const CodedBitstreamUnitTypeDescriptor *unit_types;
  99. // Split frag->data into coded bitstream units, creating the
  100. // frag->units array. Fill data but not content on each unit.
  101. // The header argument should be set if the fragment came from
  102. // a header block, which may require different parsing for some
  103. // codecs (e.g. the AVCC header in H.264).
  104. int (*split_fragment)(CodedBitstreamContext *ctx,
  105. CodedBitstreamFragment *frag,
  106. int header);
  107. // Read the unit->data bitstream and decompose it, creating
  108. // unit->content.
  109. int (*read_unit)(CodedBitstreamContext *ctx,
  110. CodedBitstreamUnit *unit);
  111. // Write the data bitstream from unit->content into pbc.
  112. // Return value AVERROR(ENOSPC) indicates that pbc was too small.
  113. int (*write_unit)(CodedBitstreamContext *ctx,
  114. CodedBitstreamUnit *unit,
  115. PutBitContext *pbc);
  116. // Return 1 when the unit should be dropped according to 'skip',
  117. // 0 otherwise.
  118. int (*discarded_unit)(CodedBitstreamContext *ctx,
  119. const CodedBitstreamUnit *unit,
  120. enum AVDiscard skip);
  121. // Read the data from all of frag->units and assemble it into
  122. // a bitstream for the whole fragment.
  123. int (*assemble_fragment)(CodedBitstreamContext *ctx,
  124. CodedBitstreamFragment *frag);
  125. // Reset the codec internal state.
  126. void (*flush)(CodedBitstreamContext *ctx);
  127. // Free the codec internal state.
  128. void (*close)(CodedBitstreamContext *ctx);
  129. } CodedBitstreamType;
  130. // Helper functions for trace output.
  131. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  132. const char *name);
  133. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  134. const char *name, const int *subscripts,
  135. const char *bitstring, int64_t value);
  136. // Helper functions for read/write of common bitstream elements, including
  137. // generation of trace output. The simple functions are equivalent to
  138. // their non-simple counterparts except that their range is unrestricted
  139. // (i.e. only limited by the amount of bits used) and they lack
  140. // the ability to use subscripts.
  141. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  142. int width, const char *name,
  143. const int *subscripts, uint32_t *write_to,
  144. uint32_t range_min, uint32_t range_max);
  145. int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  146. int width, const char *name, uint32_t *write_to);
  147. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  148. int width, const char *name,
  149. const int *subscripts, uint32_t value,
  150. uint32_t range_min, uint32_t range_max);
  151. int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  152. int width, const char *name, uint32_t value);
  153. int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
  154. int width, const char *name,
  155. const int *subscripts, int32_t *write_to,
  156. int32_t range_min, int32_t range_max);
  157. int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
  158. int width, const char *name,
  159. const int *subscripts, int32_t value,
  160. int32_t range_min, int32_t range_max);
  161. // The largest unsigned value representable in N bits, suitable for use as
  162. // range_max in the above functions.
  163. #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
  164. // The largest signed value representable in N bits, suitable for use as
  165. // range_max in the above functions.
  166. #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
  167. // The smallest signed value representable in N bits, suitable for use as
  168. // range_min in the above functions.
  169. #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
  170. #define TYPE_LIST(...) { __VA_ARGS__ }
  171. #define CBS_UNIT_TYPE_POD(type_, structure) { \
  172. .nb_unit_types = 1, \
  173. .unit_type.list = { type_ }, \
  174. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  175. .content_size = sizeof(structure), \
  176. .type.ref = { .nb_offsets = 0 }, \
  177. }
  178. #define CBS_UNIT_RANGE_POD(range_start, range_end, structure) { \
  179. .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
  180. .unit_type.range.start = range_start, \
  181. .unit_type.range.end = range_end, \
  182. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  183. .content_size = sizeof(structure), \
  184. .type.ref = { .nb_offsets = 0 }, \
  185. }
  186. #define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field) { \
  187. .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
  188. .unit_type.list = TYPE_LIST types, \
  189. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  190. .content_size = sizeof(structure), \
  191. .type.ref = { .nb_offsets = 1, \
  192. .offsets = { offsetof(structure, ref_field) } }, \
  193. }
  194. #define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field) \
  195. CBS_UNIT_TYPES_INTERNAL_REF((type), structure, ref_field)
  196. #define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field) { \
  197. .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
  198. .unit_type.range.start = range_start, \
  199. .unit_type.range.end = range_end, \
  200. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
  201. .content_size = sizeof(structure), \
  202. .type.ref = { .nb_offsets = 1, \
  203. .offsets = { offsetof(structure, ref_field) } }, \
  204. }
  205. #define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func) { \
  206. .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
  207. .unit_type.list = TYPE_LIST types, \
  208. .content_type = CBS_CONTENT_TYPE_COMPLEX, \
  209. .content_size = sizeof(structure), \
  210. .type.complex = { .content_free = free_func }, \
  211. }
  212. #define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func) \
  213. CBS_UNIT_TYPES_COMPLEX((type), structure, free_func)
  214. #define CBS_UNIT_TYPE_END_OF_LIST { .nb_unit_types = 0 }
  215. extern const CodedBitstreamType ff_cbs_type_av1;
  216. extern const CodedBitstreamType ff_cbs_type_h264;
  217. extern const CodedBitstreamType ff_cbs_type_h265;
  218. extern const CodedBitstreamType ff_cbs_type_h266;
  219. extern const CodedBitstreamType ff_cbs_type_jpeg;
  220. extern const CodedBitstreamType ff_cbs_type_mpeg2;
  221. extern const CodedBitstreamType ff_cbs_type_vp9;
  222. #endif /* AVCODEC_CBS_INTERNAL_H */