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.

178 lines
4.5KB

  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. typedef struct AV1OBU {
  27. /** Size of payload */
  28. int size;
  29. const uint8_t *data;
  30. /**
  31. * Size, in bits, of just the data, excluding the trailing_one_bit and
  32. * any trailing padding.
  33. */
  34. int size_bits;
  35. /** Size of entire OBU, including header */
  36. int raw_size;
  37. const uint8_t *raw_data;
  38. /** GetBitContext initialized to the start of the payload */
  39. GetBitContext gb;
  40. int type;
  41. int temporal_id;
  42. int spatial_id;
  43. } AV1OBU;
  44. /** An input packet split into OBUs */
  45. typedef struct AV1Packet {
  46. AV1OBU *obus;
  47. int nb_obus;
  48. int obus_allocated;
  49. unsigned obus_allocated_size;
  50. } AV1Packet;
  51. /**
  52. * Extract an OBU from a raw bitstream.
  53. *
  54. * @note This function does not copy or store any bitstream data. All
  55. * the pointers in the AV1OBU structure will be valid as long
  56. * as the input buffer also is.
  57. */
  58. int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
  59. void *logctx);
  60. /**
  61. * Split an input packet into OBUs.
  62. *
  63. * @note This function does not copy or store any bitstream data. All
  64. * the pointers in the AV1Packet structure will be valid as
  65. * long as the input buffer also is.
  66. */
  67. int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
  68. void *logctx);
  69. /**
  70. * Free all the allocated memory in the packet.
  71. */
  72. void ff_av1_packet_uninit(AV1Packet *pkt);
  73. static inline int64_t leb128(GetBitContext *gb) {
  74. int64_t ret = 0;
  75. int i;
  76. for (i = 0; i < 8; i++) {
  77. int byte = get_bits(gb, 8);
  78. ret |= (int64_t)(byte & 0x7f) << (i * 7);
  79. if (!(byte & 0x80))
  80. break;
  81. }
  82. return ret;
  83. }
  84. static inline int parse_obu_header(const uint8_t *buf, int buf_size,
  85. int64_t *obu_size, int *start_pos, int *type,
  86. int *temporal_id, int *spatial_id)
  87. {
  88. GetBitContext gb;
  89. int ret, extension_flag, has_size_flag;
  90. int64_t size;
  91. ret = init_get_bits8(&gb, buf, FFMIN(buf_size, 2 + 8)); // OBU header fields + max leb128 length
  92. if (ret < 0)
  93. return ret;
  94. if (get_bits1(&gb) != 0) // obu_forbidden_bit
  95. return AVERROR_INVALIDDATA;
  96. *type = get_bits(&gb, 4);
  97. extension_flag = get_bits1(&gb);
  98. has_size_flag = get_bits1(&gb);
  99. skip_bits1(&gb); // obu_reserved_1bit
  100. if (extension_flag) {
  101. *temporal_id = get_bits(&gb, 3);
  102. *spatial_id = get_bits(&gb, 2);
  103. skip_bits(&gb, 3); // extension_header_reserved_3bits
  104. } else {
  105. *temporal_id = *spatial_id = 0;
  106. }
  107. *obu_size = has_size_flag ? leb128(&gb)
  108. : buf_size - 1 - extension_flag;
  109. if (get_bits_left(&gb) < 0)
  110. return AVERROR_INVALIDDATA;
  111. *start_pos = get_bits_count(&gb) / 8;
  112. size = *obu_size + *start_pos;
  113. if (size > buf_size)
  114. return AVERROR_INVALIDDATA;
  115. return size;
  116. }
  117. static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
  118. {
  119. int v;
  120. /* There are no trailing bits on these */
  121. if (type == AV1_OBU_TILE_GROUP ||
  122. type == AV1_OBU_TILE_LIST ||
  123. type == AV1_OBU_FRAME) {
  124. if (size > INT_MAX / 8)
  125. return AVERROR(ERANGE);
  126. else
  127. return size * 8;
  128. }
  129. while (size > 0 && buf[size - 1] == 0)
  130. size--;
  131. if (!size)
  132. return 0;
  133. v = buf[size - 1];
  134. if (size > INT_MAX / 8)
  135. return AVERROR(ERANGE);
  136. size *= 8;
  137. /* Remove the trailing_one_bit and following trailing zeros */
  138. if (v)
  139. size -= ff_ctz(v) + 1;
  140. return size;
  141. }
  142. #endif /* AVCODEC_AV1_PARSE_H */