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
19 #include "detection_bbox.h"
20
av_detection_bbox_alloc(uint32_t nb_bboxes,size_t * out_size)21 AVDetectionBBoxHeader *av_detection_bbox_alloc(uint32_t nb_bboxes, size_t *out_size)
22 {
23 size_t size;
24 struct BBoxContext {
25 AVDetectionBBoxHeader header;
26 AVDetectionBBox boxes;
27 };
28 const size_t bboxes_offset = offsetof(struct BBoxContext, boxes);
29 const size_t bbox_size = sizeof(AVDetectionBBox);
30 AVDetectionBBoxHeader *header;
31
32 size = bboxes_offset;
33 if (nb_bboxes > (SIZE_MAX - size) / bbox_size)
34 return NULL;
35 size += bbox_size * nb_bboxes;
36
37 header = av_mallocz(size);
38 if (!header)
39 return NULL;
40
41 header->nb_bboxes = nb_bboxes;
42 header->bbox_size = bbox_size;
43 header->bboxes_offset = bboxes_offset;
44
45 if (out_size)
46 *out_size = size;
47
48 return header;
49 }
50
av_detection_bbox_create_side_data(AVFrame * frame,uint32_t nb_bboxes)51 AVDetectionBBoxHeader *av_detection_bbox_create_side_data(AVFrame *frame, uint32_t nb_bboxes)
52 {
53 AVBufferRef *buf;
54 AVDetectionBBoxHeader *header;
55 size_t size;
56
57 header = av_detection_bbox_alloc(nb_bboxes, &size);
58 if (!header)
59 return NULL;
60 buf = av_buffer_create((uint8_t *)header, size, NULL, NULL, 0);
61 if (!buf) {
62 av_freep(&header);
63 return NULL;
64 }
65
66 if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DETECTION_BBOXES, buf)) {
67 av_buffer_unref(&buf);
68 return NULL;
69 }
70
71 return header;
72 }
73