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.

213 lines
8.2KB

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