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.

131 lines
3.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 "avcodec.h"
  24. #include "get_bits.h"
  25. typedef struct AV1OBU {
  26. /** Size of payload */
  27. int size;
  28. const uint8_t *data;
  29. /** Size of entire OBU, including header */
  30. int raw_size;
  31. const uint8_t *raw_data;
  32. /** GetBitContext initialized to the start of the payload */
  33. GetBitContext gb;
  34. int type;
  35. int temporal_id;
  36. int spatial_id;
  37. } AV1OBU;
  38. /** An input packet split into OBUs */
  39. typedef struct AV1Packet {
  40. AV1OBU *obus;
  41. int nb_obus;
  42. int obus_allocated;
  43. } AV1Packet;
  44. /**
  45. * Extract an OBU from a raw bitstream.
  46. *
  47. * @note This function does not copy or store any bistream data. All
  48. * the pointers in the AV1OBU structure will be valid as long
  49. * as the input buffer also is.
  50. */
  51. int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
  52. void *logctx);
  53. /**
  54. * Split an input packet into OBUs.
  55. *
  56. * @note This function does not copy or store any bistream data. All
  57. * the pointers in the AV1Packet structure will be valid as
  58. * long as the input buffer also is.
  59. */
  60. int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
  61. void *logctx);
  62. /**
  63. * Free all the allocated memory in the packet.
  64. */
  65. void ff_av1_packet_uninit(AV1Packet *pkt);
  66. static inline int64_t leb128(GetBitContext *gb) {
  67. int64_t ret = 0;
  68. int i;
  69. for (i = 0; i < 8; i++) {
  70. int byte = get_bits(gb, 8);
  71. ret |= (int64_t)(byte & 0x7f) << (i * 7);
  72. if (!(byte & 0x80))
  73. break;
  74. }
  75. return ret;
  76. }
  77. static inline int parse_obu_header(const uint8_t *buf, int buf_size,
  78. int64_t *obu_size, int *start_pos, int *type,
  79. int *temporal_id, int *spatial_id)
  80. {
  81. GetBitContext gb;
  82. int ret, extension_flag, has_size_flag;
  83. ret = init_get_bits8(&gb, buf, FFMIN(buf_size, 2 + 8)); // OBU header fields + max leb128 length
  84. if (ret < 0)
  85. return ret;
  86. if (get_bits1(&gb) != 0) // obu_forbidden_bit
  87. return AVERROR_INVALIDDATA;
  88. *type = get_bits(&gb, 4);
  89. extension_flag = get_bits1(&gb);
  90. has_size_flag = get_bits1(&gb);
  91. skip_bits1(&gb); // obu_reserved_1bit
  92. if (extension_flag) {
  93. *temporal_id = get_bits(&gb, 3);
  94. *spatial_id = get_bits(&gb, 2);
  95. skip_bits(&gb, 3); // extension_header_reserved_3bits
  96. } else {
  97. *temporal_id = *spatial_id = 0;
  98. }
  99. *obu_size = has_size_flag ? leb128(&gb)
  100. : buf_size - 1 - extension_flag;
  101. if (get_bits_left(&gb) < 0)
  102. return AVERROR_INVALIDDATA;
  103. *start_pos = get_bits_count(&gb) / 8;
  104. return 0;
  105. }
  106. #endif /* AVCODEC_AV1_PARSE_H */