FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/detection_bbox.c
Date: 2026-05-03 23:58:45
Exec Total Coverage
Lines: 62 65 95.4%
Functions: 1 1 100.0%
Branches: 17 26 65.4%

Line Branch Exec Source
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 <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "libavutil/detection_bbox.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/macros.h"
26 #include "libavutil/mem.h"
27
28 1 int main(void)
29 {
30 AVDetectionBBoxHeader *header;
31 AVDetectionBBox *bbox;
32 AVFrame *frame;
33 size_t size;
34
35 static const unsigned int alloc_counts[] = { 0, 1, 4 };
36
37 /* av_detection_bbox_alloc - various counts */
38 1 printf("Testing av_detection_bbox_alloc()\n");
39
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < FF_ARRAY_ELEMS(alloc_counts); i++) {
40 3 unsigned int nb = alloc_counts[i];
41 3 header = av_detection_bbox_alloc(nb, &size);
42
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (header) {
43 3 printf("alloc %u: OK, nb=%u, size>0=%s\n",
44
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 nb, header->nb_bboxes, size > 0 ? "yes" : "no");
45 3 av_free(header);
46 } else {
47 printf("alloc %u: FAIL\n", nb);
48 }
49 }
50
51 /* av_detection_bbox_alloc without size */
52 1 header = av_detection_bbox_alloc(1, NULL);
53
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("alloc (no size): %s\n", header ? "OK" : "FAIL");
54 1 av_free(header);
55
56 /* av_get_detection_bbox - pointer consistency and write/read back */
57 1 printf("\nTesting av_get_detection_bbox()\n");
58 1 header = av_detection_bbox_alloc(3, NULL);
59
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (header) {
60
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < 3; i++) {
61 3 bbox = av_get_detection_bbox(header, i);
62 3 if ((uint8_t *)bbox != (uint8_t *)header + header->bboxes_offset +
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (size_t)i * header->bbox_size)
64 printf("bbox %d: pointer inconsistent with bboxes_offset/bbox_size\n", i);
65 3 bbox->x = i * 100;
66 3 bbox->y = i * 200;
67 3 bbox->w = 50 + i;
68 3 bbox->h = 60 + i;
69 3 snprintf(bbox->detect_label, sizeof(bbox->detect_label),
70 "obj%d", i);
71 3 bbox->detect_confidence = (AVRational){ 90 + i, 100 };
72 }
73
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < 3; i++) {
74 3 bbox = av_get_detection_bbox(header, i);
75 3 printf("bbox %d: x=%d y=%d w=%d h=%d label=%s conf=%d/%d\n",
76 i, bbox->x, bbox->y, bbox->w, bbox->h,
77 3 bbox->detect_label,
78 bbox->detect_confidence.num,
79 bbox->detect_confidence.den);
80 }
81 1 av_free(header);
82 }
83
84 /* classify fields */
85 1 printf("\nTesting classify fields\n");
86 1 header = av_detection_bbox_alloc(1, NULL);
87
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (header) {
88 1 bbox = av_get_detection_bbox(header, 0);
89 1 bbox->classify_count = 2;
90 1 snprintf(bbox->classify_labels[0], sizeof(bbox->classify_labels[0]),
91 "cat");
92 1 bbox->classify_confidences[0] = (AVRational){ 95, 100 };
93 1 snprintf(bbox->classify_labels[1], sizeof(bbox->classify_labels[1]),
94 "animal");
95 1 bbox->classify_confidences[1] = (AVRational){ 80, 100 };
96 1 printf("classify_count=%u\n", bbox->classify_count);
97
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < (int)bbox->classify_count; i++)
98 2 printf("classify %d: %s %d/%d\n", i,
99 2 bbox->classify_labels[i],
100 bbox->classify_confidences[i].num,
101 bbox->classify_confidences[i].den);
102 1 av_free(header);
103 }
104
105 /* header source field */
106 1 printf("\nTesting source field\n");
107 1 header = av_detection_bbox_alloc(1, NULL);
108
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (header) {
109 1 snprintf(header->source, sizeof(header->source), "test_model_v1");
110 1 printf("source: %s\n", header->source);
111 1 av_free(header);
112 }
113
114 /* av_detection_bbox_create_side_data */
115 1 printf("\nTesting av_detection_bbox_create_side_data()\n");
116 1 frame = av_frame_alloc();
117
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (frame) {
118 1 header = av_detection_bbox_create_side_data(frame, 2);
119
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (header) {
120 1 printf("side_data: OK, nb=%u\n", header->nb_bboxes);
121 1 bbox = av_get_detection_bbox(header, 0);
122 1 bbox->x = 10;
123 1 bbox->y = 20;
124 1 printf("side_data bbox 0: x=%d y=%d\n", bbox->x, bbox->y);
125 } else {
126 printf("side_data: FAIL\n");
127 }
128 1 av_frame_free(&frame);
129 }
130
131 1 return 0;
132 }
133