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.

175 lines
4.4KB

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