FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/huffman.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 84 89 94.4%
Functions: 5 5 100.0%
Branches: 89 96 92.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2006 Konstantin Shishkov
3 * Copyright (c) 2007 Loren Merritt
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * huffman tree builder and VLC generator
25 */
26
27 #include <stdint.h>
28
29 #include "libavutil/error.h"
30 #include "libavutil/log.h"
31 #include "libavutil/macros.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/qsort.h"
34
35 #include "huffman.h"
36 #include "vlc.h"
37
38 /* symbol for Huffman tree node */
39 #define HNODE -1
40
41 typedef struct HeapElem {
42 union {
43 uint64_t val;
44 uint16_t dummy; // exists solely to ensure alignof(HeapElem) >= alignof(uint16_t)
45 };
46 int name;
47 } HeapElem;
48
49 1973953 static void heap_sift(HeapElem *h, int root, int size)
50 {
51
2/2
✓ Branch 0 taken 13358868 times.
✓ Branch 1 taken 761433 times.
14120301 while (root * 2 + 1 < size) {
52 13358868 int child = root * 2 + 1;
53
4/4
✓ Branch 0 taken 13350259 times.
✓ Branch 1 taken 8609 times.
✓ Branch 2 taken 6178785 times.
✓ Branch 3 taken 7171474 times.
13358868 if (child < size - 1 && h[child].val > h[child+1].val)
54 6178785 child++;
55
2/2
✓ Branch 0 taken 12146348 times.
✓ Branch 1 taken 1212520 times.
13358868 if (h[root].val > h[child].val) {
56 12146348 FFSWAP(HeapElem, h[root], h[child]);
57 12146348 root = child;
58 } else
59 1212520 break;
60 }
61 1973953 }
62
63 2449 int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)
64 {
65 int *up;
66 uint16_t *map;
67 uint8_t *len;
68 2449 HeapElem *h = av_malloc_array(stats_size,
69 sizeof(*h) + 2 * sizeof(up) + 2 * sizeof(len) + sizeof(map));
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2449 times.
2449 if (!h)
71 return AVERROR(ENOMEM);
72 2449 up = (int*)(h + stats_size);
73 // map is suitably aligned because up uses an even number of elements
74 // and alignof(uint16_t) is either 1 or 2.
75 2449 map = (uint16_t*)(up + 2 * stats_size);
76 2449 len = (uint8_t*)(map + stats_size);
77
78 int offset, i, next;
79 2449 int size = 0;
80 2449 int ret = 0;
81
82
2/2
✓ Branch 0 taken 875776 times.
✓ Branch 1 taken 2449 times.
878225 for (i = 0; i<stats_size; i++) {
83 875776 dst[i] = 255;
84
3/4
✓ Branch 0 taken 84131 times.
✓ Branch 1 taken 791645 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84131 times.
875776 if (stats[i] || !skip0)
85 791645 map[size++] = i;
86 }
87
88 2449 for (offset = 1; ; offset <<= 1) {
89
2/2
✓ Branch 0 taken 791645 times.
✓ Branch 1 taken 2449 times.
794094 for (i=0; i < size; i++) {
90 791645 h[i].name = i;
91 791645 h[i].val = (stats[map[i]] << 14) + offset;
92 }
93
2/2
✓ Branch 0 taken 395561 times.
✓ Branch 1 taken 2449 times.
398010 for (i = size / 2 - 1; i >= 0; i--)
94 395561 heap_sift(h, i, size);
95
96
2/2
✓ Branch 0 taken 789196 times.
✓ Branch 1 taken 2449 times.
791645 for (next = size; next < size * 2 - 1; next++) {
97 // merge the two smallest entries, and put it back in the heap
98 789196 uint64_t min1v = h[0].val;
99 789196 up[h[0].name] = next;
100 789196 h[0].val = INT64_MAX;
101 789196 heap_sift(h, 0, size);
102 789196 up[h[0].name] = next;
103 789196 h[0].name = next;
104 789196 h[0].val += min1v;
105 789196 heap_sift(h, 0, size);
106 }
107
108 2449 len[2 * size - 2] = 0;
109
2/2
✓ Branch 0 taken 786747 times.
✓ Branch 1 taken 2449 times.
789196 for (i = 2 * size - 3; i >= size; i--)
110 786747 len[i] = len[up[i]] + 1;
111
2/2
✓ Branch 0 taken 791645 times.
✓ Branch 1 taken 2449 times.
794094 for (i = 0; i < size; i++) {
112 791645 dst[map[i]] = len[up[i]] + 1;
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 791645 times.
791645 if (dst[map[i]] >= 32) break;
114 }
115
1/2
✓ Branch 0 taken 2449 times.
✗ Branch 1 not taken.
2449 if (i==size) break;
116 }
117 2449 av_free(h);
118 2449 return ret;
119 }
120
121 287031 static void get_tree_codes(int8_t *lens, uint8_t *xlat,
122 Node *nodes, int node, int pl, int *pos, int no_zero_count)
123 {
124 int s;
125
126 287031 s = nodes[node].sym;
127
5/6
✓ Branch 0 taken 141669 times.
✓ Branch 1 taken 145362 times.
✓ Branch 2 taken 35334 times.
✓ Branch 3 taken 106335 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 35334 times.
287031 if (s != HNODE || (no_zero_count && !nodes[node].count)) {
128 145362 lens[*pos] = pl;
129 145362 xlat[*pos] = s;
130 145362 (*pos)++;
131 } else {
132 141669 pl++;
133 141669 get_tree_codes(lens, xlat, nodes, nodes[node].n0, pl,
134 pos, no_zero_count);
135 141669 get_tree_codes(lens, xlat, nodes, nodes[node].n0 + 1, pl,
136 pos, no_zero_count);
137 }
138 287031 }
139
140 3693 static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits, void *logctx)
141 {
142 3693 int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
143 int8_t lens[256];
144 uint8_t xlat[256];
145 3693 int pos = 0;
146
147 3693 get_tree_codes(lens, xlat, nodes, head, 0,
148 &pos, no_zero_count);
149 3693 return ff_vlc_init_from_lengths(vlc, nb_bits, pos, lens, 1,
150 xlat, 1, 1, 0, 0, logctx);
151 }
152
153
154 /**
155 * nodes size must be 2*nb_codes
156 * first nb_codes nodes.count must be set
157 */
158 3693 int ff_huff_build_tree(void *logctx, VLC *vlc, int nb_codes, int nb_bits,
159 Node *nodes, HuffCmp cmp, int flags)
160 {
161 int i, j;
162 int cur_node;
163 3693 int64_t sum = 0;
164
165
2/2
✓ Branch 0 taken 145362 times.
✓ Branch 1 taken 3693 times.
149055 for (i = 0; i < nb_codes; i++) {
166 145362 nodes[i].sym = i;
167 145362 nodes[i].n0 = -2;
168 145362 sum += nodes[i].count;
169 }
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3693 times.
3693 if (sum >> 31) {
172 av_log(logctx, AV_LOG_ERROR,
173 "Too high symbol frequencies. "
174 "Tree construction is not possible\n");
175 return -1;
176 }
177
44/44
✓ Branch 0 taken 60121 times.
✓ Branch 1 taken 20542 times.
✓ Branch 3 taken 32073 times.
✓ Branch 4 taken 28048 times.
✓ Branch 6 taken 10242 times.
✓ Branch 7 taken 21831 times.
✓ Branch 9 taken 8683 times.
✓ Branch 10 taken 19365 times.
✓ Branch 12 taken 31653 times.
✓ Branch 13 taken 28468 times.
✓ Branch 14 taken 12435 times.
✓ Branch 15 taken 47686 times.
✓ Branch 16 taken 493365 times.
✓ Branch 17 taken 20114 times.
✓ Branch 19 taken 342802 times.
✓ Branch 20 taken 150563 times.
✓ Branch 21 taken 326481 times.
✓ Branch 22 taken 40480 times.
✓ Branch 24 taken 196284 times.
✓ Branch 25 taken 130197 times.
✓ Branch 26 taken 40480 times.
✓ Branch 27 taken 130197 times.
✓ Branch 28 taken 170677 times.
✓ Branch 29 taken 47686 times.
✓ Branch 30 taken 6709 times.
✓ Branch 31 taken 40977 times.
✓ Branch 32 taken 4852 times.
✓ Branch 33 taken 1857 times.
✓ Branch 34 taken 1475 times.
✓ Branch 35 taken 3377 times.
✓ Branch 36 taken 7894 times.
✓ Branch 37 taken 619 times.
✓ Branch 39 taken 5181 times.
✓ Branch 40 taken 2713 times.
✓ Branch 41 taken 619 times.
✓ Branch 42 taken 2713 times.
✓ Branch 43 taken 24609 times.
✓ Branch 44 taken 22458 times.
✓ Branch 46 taken 9926 times.
✓ Branch 47 taken 10616 times.
✓ Branch 48 taken 80663 times.
✓ Branch 49 taken 17164 times.
✓ Branch 50 taken 50760 times.
✓ Branch 51 taken 3693 times.
864150 AV_QSORT(nodes, nb_codes, Node, cmp);
178 3693 cur_node = nb_codes;
179 3693 nodes[nb_codes*2-1].count = 0;
180
2/2
✓ Branch 0 taken 145362 times.
✓ Branch 1 taken 3693 times.
149055 for (i = 0; i < nb_codes * 2 - 1; i += 2) {
181 145362 uint32_t cur_count = nodes[i].count + nodes[i+1].count;
182 // find correct place to insert new node, and
183 // make space for the new node while at it
184
2/2
✓ Branch 0 taken 5623698 times.
✓ Branch 1 taken 16173 times.
5639871 for(j = cur_node; j > i + 2; j--){
185
2/2
✓ Branch 0 taken 5565907 times.
✓ Branch 1 taken 57791 times.
5623698 if(cur_count > nodes[j-1].count ||
186
2/2
✓ Branch 0 taken 90183 times.
✓ Branch 1 taken 5475724 times.
5565907 (cur_count == nodes[j-1].count &&
187
2/2
✓ Branch 0 taken 18785 times.
✓ Branch 1 taken 71398 times.
90183 !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
188 break;
189 5494509 nodes[j] = nodes[j - 1];
190 }
191 145362 nodes[j].sym = HNODE;
192 145362 nodes[j].count = cur_count;
193 145362 nodes[j].n0 = i;
194 145362 cur_node++;
195 }
196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3693 times.
3693 if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits, logctx) < 0) {
197 av_log(logctx, AV_LOG_ERROR, "Error building tree\n");
198 return -1;
199 }
200 3693 return 0;
201 }
202