FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/huffman.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 91 97 93.8%
Branches: 92 102 90.2%

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/qsort.h"
30 #include "libavutil/common.h"
31
32 #include "huffman.h"
33 #include "vlc.h"
34
35 /* symbol for Huffman tree node */
36 #define HNODE -1
37
38 typedef struct HeapElem {
39 uint64_t val;
40 int name;
41 } HeapElem;
42
43 1973140 static void heap_sift(HeapElem *h, int root, int size)
44 {
45
2/2
✓ Branch 0 taken 13354908 times.
✓ Branch 1 taken 761161 times.
14116069 while (root * 2 + 1 < size) {
46 13354908 int child = root * 2 + 1;
47
4/4
✓ Branch 0 taken 13346304 times.
✓ Branch 1 taken 8604 times.
✓ Branch 2 taken 6177070 times.
✓ Branch 3 taken 7169234 times.
13354908 if (child < size - 1 && h[child].val > h[child+1].val)
48 6177070 child++;
49
2/2
✓ Branch 0 taken 12142929 times.
✓ Branch 1 taken 1211979 times.
13354908 if (h[root].val > h[child].val) {
50 12142929 FFSWAP(HeapElem, h[root], h[child]);
51 12142929 root = child;
52 } else
53 1211979 break;
54 }
55 1973140 }
56
57 2446 int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)
58 {
59 2446 HeapElem *h = av_malloc_array(sizeof(*h), stats_size);
60 2446 int *up = av_malloc_array(sizeof(*up) * 2, stats_size);
61 2446 uint8_t *len = av_malloc_array(sizeof(*len) * 2, stats_size);
62 2446 uint16_t *map= av_malloc_array(sizeof(*map), stats_size);
63 int offset, i, next;
64 2446 int size = 0;
65 2446 int ret = 0;
66
67
4/8
✓ Branch 0 taken 2446 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2446 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2446 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2446 times.
2446 if (!h || !up || !len || !map) {
68 ret = AVERROR(ENOMEM);
69 goto end;
70 }
71
72
2/2
✓ Branch 0 taken 875008 times.
✓ Branch 1 taken 2446 times.
877454 for (i = 0; i<stats_size; i++) {
73 875008 dst[i] = 255;
74
3/4
✓ Branch 0 taken 83691 times.
✓ Branch 1 taken 791317 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83691 times.
875008 if (stats[i] || !skip0)
75 791317 map[size++] = i;
76 }
77
78 2446 for (offset = 1; ; offset <<= 1) {
79
2/2
✓ Branch 0 taken 791317 times.
✓ Branch 1 taken 2446 times.
793763 for (i=0; i < size; i++) {
80 791317 h[i].name = i;
81 791317 h[i].val = (stats[map[i]] << 14) + offset;
82 }
83
2/2
✓ Branch 0 taken 395398 times.
✓ Branch 1 taken 2446 times.
397844 for (i = size / 2 - 1; i >= 0; i--)
84 395398 heap_sift(h, i, size);
85
86
2/2
✓ Branch 0 taken 788871 times.
✓ Branch 1 taken 2446 times.
791317 for (next = size; next < size * 2 - 1; next++) {
87 // merge the two smallest entries, and put it back in the heap
88 788871 uint64_t min1v = h[0].val;
89 788871 up[h[0].name] = next;
90 788871 h[0].val = INT64_MAX;
91 788871 heap_sift(h, 0, size);
92 788871 up[h[0].name] = next;
93 788871 h[0].name = next;
94 788871 h[0].val += min1v;
95 788871 heap_sift(h, 0, size);
96 }
97
98 2446 len[2 * size - 2] = 0;
99
2/2
✓ Branch 0 taken 786425 times.
✓ Branch 1 taken 2446 times.
788871 for (i = 2 * size - 3; i >= size; i--)
100 786425 len[i] = len[up[i]] + 1;
101
2/2
✓ Branch 0 taken 791317 times.
✓ Branch 1 taken 2446 times.
793763 for (i = 0; i < size; i++) {
102 791317 dst[map[i]] = len[up[i]] + 1;
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 791317 times.
791317 if (dst[map[i]] >= 32) break;
104 }
105
1/2
✓ Branch 0 taken 2446 times.
✗ Branch 1 not taken.
2446 if (i==size) break;
106 }
107 2446 end:
108 2446 av_free(h);
109 2446 av_free(up);
110 2446 av_free(len);
111 2446 av_free(map);
112 2446 return ret;
113 }
114
115 319323 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
116 Node *nodes, int node,
117 uint32_t pfx, int pl, int *pos, int no_zero_count)
118 {
119 int s;
120
121 319323 s = nodes[node].sym;
122
5/6
✓ Branch 0 taken 157113 times.
✓ Branch 1 taken 162210 times.
✓ Branch 2 taken 50778 times.
✓ Branch 3 taken 106335 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 50778 times.
319323 if (s != HNODE || (no_zero_count && !nodes[node].count)) {
123 162210 bits[*pos] = pfx;
124 162210 lens[*pos] = pl;
125 162210 xlat[*pos] = s;
126 162210 (*pos)++;
127 } else {
128 157113 pfx <<= 1;
129 157113 pl++;
130 157113 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
131 pos, no_zero_count);
132 157113 pfx |= 1;
133 157113 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
134 pos, no_zero_count);
135 }
136 319323 }
137
138 5097 static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
139 {
140 5097 int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
141 uint32_t bits[256];
142 int16_t lens[256];
143 uint8_t xlat[256];
144 5097 int pos = 0;
145
146 5097 get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
147 &pos, no_zero_count);
148 5097 return ff_init_vlc_sparse(vlc, nb_bits, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
149 }
150
151
152 /**
153 * nodes size must be 2*nb_codes
154 * first nb_codes nodes.count must be set
155 */
156 5097 int ff_huff_build_tree(void *logctx, VLC *vlc, int nb_codes, int nb_bits,
157 Node *nodes, HuffCmp cmp, int flags)
158 {
159 int i, j;
160 int cur_node;
161 5097 int64_t sum = 0;
162
163
2/2
✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 5097 times.
167307 for (i = 0; i < nb_codes; i++) {
164 162210 nodes[i].sym = i;
165 162210 nodes[i].n0 = -2;
166 162210 sum += nodes[i].count;
167 }
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5097 times.
5097 if (sum >> 31) {
170 av_log(logctx, AV_LOG_ERROR,
171 "Too high symbol frequencies. "
172 "Tree construction is not possible\n");
173 return -1;
174 }
175
44/44
✓ Branch 0 taken 66248 times.
✓ Branch 1 taken 23732 times.
✓ Branch 3 taken 36365 times.
✓ Branch 4 taken 29883 times.
✓ Branch 6 taken 11086 times.
✓ Branch 7 taken 25279 times.
✓ Branch 9 taken 8949 times.
✓ Branch 10 taken 20934 times.
✓ Branch 12 taken 35522 times.
✓ Branch 13 taken 30726 times.
✓ Branch 14 taken 13551 times.
✓ Branch 15 taken 52697 times.
✓ Branch 16 taken 510008 times.
✓ Branch 17 taken 23486 times.
✓ Branch 19 taken 353232 times.
✓ Branch 20 taken 156776 times.
✓ Branch 21 taken 333731 times.
✓ Branch 22 taken 44596 times.
✓ Branch 24 taken 198065 times.
✓ Branch 25 taken 135666 times.
✓ Branch 26 taken 135666 times.
✓ Branch 27 taken 44596 times.
✓ Branch 28 taken 180262 times.
✓ Branch 29 taken 52697 times.
✓ Branch 30 taken 6731 times.
✓ Branch 31 taken 45966 times.
✓ Branch 32 taken 4852 times.
✓ Branch 33 taken 1879 times.
✓ Branch 34 taken 1475 times.
✓ Branch 35 taken 3377 times.
✓ Branch 36 taken 7939 times.
✓ Branch 37 taken 619 times.
✓ Branch 39 taken 5204 times.
✓ Branch 40 taken 2735 times.
✓ Branch 41 taken 619 times.
✓ Branch 42 taken 2735 times.
✓ Branch 43 taken 28167 times.
✓ Branch 44 taken 23911 times.
✓ Branch 46 taken 11240 times.
✓ Branch 47 taken 12492 times.
✓ Branch 48 taken 89980 times.
✓ Branch 49 taken 19273 times.
✓ Branch 50 taken 57175 times.
✓ Branch 51 taken 5097 times.
851113 AV_QSORT(nodes, nb_codes, Node, cmp);
176 5097 cur_node = nb_codes;
177 5097 nodes[nb_codes*2-1].count = 0;
178
2/2
✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 5097 times.
167307 for (i = 0; i < nb_codes * 2 - 1; i += 2) {
179 162210 uint32_t cur_count = nodes[i].count + nodes[i+1].count;
180 // find correct place to insert new node, and
181 // make space for the new node while at it
182
2/2
✓ Branch 0 taken 5685299 times.
✓ Branch 1 taken 23161 times.
5708460 for(j = cur_node; j > i + 2; j--){
183
2/2
✓ Branch 0 taken 5617648 times.
✓ Branch 1 taken 67651 times.
5685299 if(cur_count > nodes[j-1].count ||
184
2/2
✓ Branch 0 taken 101452 times.
✓ Branch 1 taken 5516196 times.
5617648 (cur_count == nodes[j-1].count &&
185
2/2
✓ Branch 0 taken 30054 times.
✓ Branch 1 taken 71398 times.
101452 !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
186 break;
187 5546250 nodes[j] = nodes[j - 1];
188 }
189 162210 nodes[j].sym = HNODE;
190 162210 nodes[j].count = cur_count;
191 162210 nodes[j].n0 = i;
192 162210 cur_node++;
193 }
194
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5097 times.
5097 if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits) < 0) {
195 av_log(logctx, AV_LOG_ERROR, "Error building tree\n");
196 return -1;
197 }
198 5097 return 0;
199 }
200