av1_parse.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * AV1 common parsing code
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVCODEC_AV1_PARSE_H
  21. #define AVCODEC_AV1_PARSE_H
  22. #include <stdint.h>
  23. #include "av1.h"
  24. #include "avcodec.h"
  25. #include "get_bits.h"
  26. // OBU header fields + max leb128 length
  27. #define MAX_OBU_HEADER_SIZE (2 + 8)
  28. typedef struct AV1OBU {
  29. /** Size of payload */
  30. int size;
  31. const uint8_t *data;
  32. /**
  33. * Size, in bits, of just the data, excluding the trailing_one_bit and
  34. * any trailing padding.
  35. */
  36. int size_bits;
  37. /** Size of entire OBU, including header */
  38. int raw_size;
  39. const uint8_t *raw_data;
  40. /** GetBitContext initialized to the start of the payload */
  41. GetBitContext gb;
  42. int type;
  43. int temporal_id;
  44. int spatial_id;
  45. } AV1OBU;
  46. /** An input packet split into OBUs */
  47. typedef struct AV1Packet {
  48. AV1OBU *obus;
  49. int nb_obus;
  50. int obus_allocated;
  51. unsigned obus_allocated_size;
  52. } AV1Packet;
  53. /**
  54. * Extract an OBU from a raw bitstream.
  55. *
  56. * @note This function does not copy or store any bitstream data. All
  57. * the pointers in the AV1OBU structure will be valid as long
  58. * as the input buffer also is.
  59. */
  60. int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
  61. void *logctx);
  62. /**
  63. * Split an input packet into OBUs.
  64. *
  65. * @note This function does not copy or store any bitstream data. All
  66. * the pointers in the AV1Packet structure will be valid as
  67. * long as the input buffer also is.
  68. */
  69. int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
  70. void *logctx);
  71. /**
  72. * Free all the allocated memory in the packet.
  73. */
  74. void ff_av1_packet_uninit(AV1Packet *pkt);
  75. static inline int64_t leb128(GetBitContext *gb) {
  76. int64_t ret = 0;
  77. int i;
  78. for (i = 0; i < 8; i++) {
  79. int byte = get_bits(gb, 8);
  80. ret |= (int64_t)(byte & 0x7f) << (i * 7);
  81. if (!(byte & 0x80))
  82. break;
  83. }
  84. return ret;
  85. }
  86. static inline int parse_obu_header(const uint8_t *buf, int buf_size,
  87. int64_t *obu_size, int *start_pos, int *type,
  88. int *temporal_id, int *spatial_id)
  89. {
  90. GetBitContext gb;
  91. int ret, extension_flag, has_size_flag;
  92. int64_t size;
  93. ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
  94. if (ret < 0)
  95. return ret;
  96. if (get_bits1(&gb) != 0) // obu_forbidden_bit
  97. return AVERROR_INVALIDDATA;
  98. *type = get_bits(&gb, 4);
  99. extension_flag = get_bits1(&gb);
  100. has_size_flag = get_bits1(&gb);
  101. skip_bits1(&gb); // obu_reserved_1bit
  102. if (extension_flag) {
  103. *temporal_id = get_bits(&gb, 3);
  104. *spatial_id = get_bits(&gb, 2);
  105. skip_bits(&gb, 3); // extension_header_reserved_3bits
  106. } else {
  107. *temporal_id = *spatial_id = 0;
  108. }
  109. *obu_size = has_size_flag ? leb128(&gb)
  110. : buf_size - 1 - extension_flag;
  111. if (get_bits_left(&gb) < 0)
  112. return AVERROR_INVALIDDATA;
  113. *start_pos = get_bits_count(&gb) / 8;
  114. size = *obu_size + *start_pos;
  115. if (size > buf_size)
  116. return AVERROR_INVALIDDATA;
  117. return size;
  118. }
  119. static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
  120. {
  121. int v;
  122. /* There are no trailing bits on these */
  123. if (type == AV1_OBU_TILE_GROUP ||
  124. type == AV1_OBU_TILE_LIST ||
  125. type == AV1_OBU_FRAME) {
  126. if (size > INT_MAX / 8)
  127. return AVERROR(ERANGE);
  128. else
  129. return size * 8;
  130. }
  131. while (size > 0 && buf[size - 1] == 0)
  132. size--;
  133. if (!size)
  134. return 0;
  135. v = buf[size - 1];
  136. if (size > INT_MAX / 8)
  137. return AVERROR(ERANGE);
  138. size *= 8;
  139. /* Remove the trailing_one_bit and following trailing zeros */
  140. if (v)
  141. size -= ff_ctz(v) + 1;
  142. return size;
  143. }
  144. #endif /* AVCODEC_AV1_PARSE_H */