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.

74 lines
2.1KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "detection_bbox.h"
  19. AVDetectionBBoxHeader *av_detection_bbox_alloc(uint32_t nb_bboxes, size_t *out_size)
  20. {
  21. size_t size;
  22. struct {
  23. AVDetectionBBoxHeader header;
  24. AVDetectionBBox boxes[1];
  25. } *ret;
  26. size = sizeof(*ret);
  27. if (nb_bboxes - 1 > (SIZE_MAX - size) / sizeof(*ret->boxes))
  28. return NULL;
  29. size += sizeof(*ret->boxes) * (nb_bboxes - 1);
  30. ret = av_mallocz(size);
  31. if (!ret)
  32. return NULL;
  33. ret->header.nb_bboxes = nb_bboxes;
  34. ret->header.bbox_size = sizeof(*ret->boxes);
  35. ret->header.bboxes_offset = (char *)&ret->boxes - (char *)&ret->header;
  36. if (out_size)
  37. *out_size = size;
  38. return &ret->header;
  39. }
  40. AVDetectionBBoxHeader *av_detection_bbox_create_side_data(AVFrame *frame, uint32_t nb_bboxes)
  41. {
  42. AVBufferRef *buf;
  43. AVDetectionBBoxHeader *header;
  44. size_t size;
  45. header = av_detection_bbox_alloc(nb_bboxes, &size);
  46. if (!header)
  47. return NULL;
  48. if (size > INT_MAX) {
  49. av_freep(&header);
  50. return NULL;
  51. }
  52. buf = av_buffer_create((uint8_t *)header, size, NULL, NULL, 0);
  53. if (!buf) {
  54. av_freep(&header);
  55. return NULL;
  56. }
  57. if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DETECTION_BBOXES, buf)) {
  58. av_buffer_unref(&buf);
  59. return NULL;
  60. }
  61. return header;
  62. }