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.

179 lines
6.9KB

  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. } CodedBitstreamUnitTypeDescriptor;
  73. typedef struct CodedBitstreamType {
  74. enum AVCodecID codec_id;
  75. size_t priv_data_size;
  76. // List of unit type descriptors for this codec.
  77. // Terminated by a descriptor with nb_unit_types equal to zero.
  78. const CodedBitstreamUnitTypeDescriptor *unit_types;
  79. // Split frag->data into coded bitstream units, creating the
  80. // frag->units array. Fill data but not content on each unit.
  81. // The header argument should be set if the fragment came from
  82. // a header block, which may require different parsing for some
  83. // codecs (e.g. the AVCC header in H.264).
  84. int (*split_fragment)(CodedBitstreamContext *ctx,
  85. CodedBitstreamFragment *frag,
  86. int header);
  87. // Read the unit->data bitstream and decompose it, creating
  88. // unit->content.
  89. int (*read_unit)(CodedBitstreamContext *ctx,
  90. CodedBitstreamUnit *unit);
  91. // Write the data bitstream from unit->content into pbc.
  92. // Return value AVERROR(ENOSPC) indicates that pbc was too small.
  93. int (*write_unit)(CodedBitstreamContext *ctx,
  94. CodedBitstreamUnit *unit,
  95. PutBitContext *pbc);
  96. // Read the data from all of frag->units and assemble it into
  97. // a bitstream for the whole fragment.
  98. int (*assemble_fragment)(CodedBitstreamContext *ctx,
  99. CodedBitstreamFragment *frag);
  100. // Free the codec internal state.
  101. void (*close)(CodedBitstreamContext *ctx);
  102. } CodedBitstreamType;
  103. // Helper functions for trace output.
  104. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  105. const char *name);
  106. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  107. const char *name, const int *subscripts,
  108. const char *bitstring, int64_t value);
  109. // Helper functions for read/write of common bitstream elements, including
  110. // generation of trace output.
  111. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  112. int width, const char *name,
  113. const int *subscripts, uint32_t *write_to,
  114. uint32_t range_min, uint32_t range_max);
  115. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  116. int width, const char *name,
  117. const int *subscripts, uint32_t value,
  118. uint32_t range_min, uint32_t range_max);
  119. int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
  120. int width, const char *name,
  121. const int *subscripts, int32_t *write_to,
  122. int32_t range_min, int32_t range_max);
  123. int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
  124. int width, const char *name,
  125. const int *subscripts, int32_t value,
  126. int32_t range_min, int32_t range_max);
  127. // The largest unsigned value representable in N bits, suitable for use as
  128. // range_max in the above functions.
  129. #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
  130. // The largest signed value representable in N bits, suitable for use as
  131. // range_max in the above functions.
  132. #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
  133. // The smallest signed value representable in N bits, suitable for use as
  134. // range_min in the above functions.
  135. #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
  136. extern const CodedBitstreamType ff_cbs_type_av1;
  137. extern const CodedBitstreamType ff_cbs_type_h264;
  138. extern const CodedBitstreamType ff_cbs_type_h265;
  139. extern const CodedBitstreamType ff_cbs_type_jpeg;
  140. extern const CodedBitstreamType ff_cbs_type_mpeg2;
  141. extern const CodedBitstreamType ff_cbs_type_vp9;
  142. #endif /* AVCODEC_CBS_INTERNAL_H */