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.

108 lines
3.1KB

  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. #include "config.h"
  21. #include "libavutil/mem.h"
  22. #include "av1.h"
  23. #include "av1_parse.h"
  24. #include "bytestream.h"
  25. int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
  26. {
  27. int64_t obu_size;
  28. int start_pos, type, temporal_id, spatial_id;
  29. int len;
  30. len = parse_obu_header(buf, length, &obu_size, &start_pos,
  31. &type, &temporal_id, &spatial_id);
  32. if (len < 0)
  33. return len;
  34. obu->type = type;
  35. obu->temporal_id = temporal_id;
  36. obu->spatial_id = spatial_id;
  37. obu->data = buf + start_pos;
  38. obu->size = obu_size;
  39. obu->raw_data = buf;
  40. obu->raw_size = len;
  41. av_log(logctx, AV_LOG_DEBUG,
  42. "obu_type: %d, temporal_id: %d, spatial_id: %d, payload size: %d\n",
  43. obu->type, obu->temporal_id, obu->spatial_id, obu->size);
  44. return len;
  45. }
  46. int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, void *logctx)
  47. {
  48. GetByteContext bc;
  49. int ret, consumed;
  50. bytestream2_init(&bc, buf, length);
  51. pkt->nb_obus = 0;
  52. while (bytestream2_get_bytes_left(&bc) > 0) {
  53. AV1OBU *obu;
  54. if (pkt->obus_allocated < pkt->nb_obus + 1) {
  55. int new_size = pkt->obus_allocated + 1;
  56. AV1OBU *tmp = av_realloc_array(pkt->obus, new_size, sizeof(*tmp));
  57. if (!tmp)
  58. return AVERROR(ENOMEM);
  59. pkt->obus = tmp;
  60. memset(pkt->obus + pkt->obus_allocated, 0,
  61. (new_size - pkt->obus_allocated) * sizeof(*tmp));
  62. pkt->obus_allocated = new_size;
  63. }
  64. obu = &pkt->obus[pkt->nb_obus];
  65. consumed = ff_av1_extract_obu(obu, bc.buffer, bytestream2_get_bytes_left(&bc), logctx);
  66. if (consumed < 0)
  67. return consumed;
  68. bytestream2_skip(&bc, consumed);
  69. obu->size_bits = get_obu_bit_length(obu->data, obu->size, obu->type);
  70. if (obu->size_bits < 0 || (!obu->size_bits && obu->type != AV1_OBU_TEMPORAL_DELIMITER)) {
  71. av_log(logctx, AV_LOG_ERROR, "Invalid OBU of type %d, skipping.\n", obu->type);
  72. continue;
  73. }
  74. pkt->nb_obus++;
  75. ret = init_get_bits(&obu->gb, obu->data, obu->size_bits);
  76. if (ret < 0)
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. void ff_av1_packet_uninit(AV1Packet *pkt)
  82. {
  83. av_freep(&pkt->obus);
  84. pkt->obus_allocated = 0;
  85. }