xref: /third_party/ffmpeg/libavcodec/huffman.c (revision cabdff1a)
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
41typedef struct HeapElem {
42    uint64_t val;
43    int name;
44} HeapElem;
45
46static void heap_sift(HeapElem *h, int root, int size)
47{
48    while (root * 2 + 1 < size) {
49        int child = root * 2 + 1;
50        if (child < size - 1 && h[child].val > h[child+1].val)
51            child++;
52        if (h[root].val > h[child].val) {
53            FFSWAP(HeapElem, h[root], h[child]);
54            root = child;
55        } else
56            break;
57    }
58}
59
60int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)
61{
62    HeapElem *h  = av_malloc_array(sizeof(*h), stats_size);
63    int *up      = av_malloc_array(sizeof(*up) * 2, stats_size);
64    uint8_t *len = av_malloc_array(sizeof(*len) * 2, stats_size);
65    uint16_t *map= av_malloc_array(sizeof(*map), stats_size);
66    int offset, i, next;
67    int size = 0;
68    int ret = 0;
69
70    if (!h || !up || !len || !map) {
71        ret = AVERROR(ENOMEM);
72        goto end;
73    }
74
75    for (i = 0; i<stats_size; i++) {
76        dst[i] = 255;
77        if (stats[i] || !skip0)
78            map[size++] = i;
79    }
80
81    for (offset = 1; ; offset <<= 1) {
82        for (i=0; i < size; i++) {
83            h[i].name = i;
84            h[i].val = (stats[map[i]] << 14) + offset;
85        }
86        for (i = size / 2 - 1; i >= 0; i--)
87            heap_sift(h, i, size);
88
89        for (next = size; next < size * 2 - 1; next++) {
90            // merge the two smallest entries, and put it back in the heap
91            uint64_t min1v = h[0].val;
92            up[h[0].name] = next;
93            h[0].val = INT64_MAX;
94            heap_sift(h, 0, size);
95            up[h[0].name] = next;
96            h[0].name = next;
97            h[0].val += min1v;
98            heap_sift(h, 0, size);
99        }
100
101        len[2 * size - 2] = 0;
102        for (i = 2 * size - 3; i >= size; i--)
103            len[i] = len[up[i]] + 1;
104        for (i = 0; i < size; i++) {
105            dst[map[i]] = len[up[i]] + 1;
106            if (dst[map[i]] >= 32) break;
107        }
108        if (i==size) break;
109    }
110end:
111    av_free(h);
112    av_free(up);
113    av_free(len);
114    av_free(map);
115    return ret;
116}
117
118static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
119                           Node *nodes, int node,
120                           uint32_t pfx, int pl, int *pos, int no_zero_count)
121{
122    int s;
123
124    s = nodes[node].sym;
125    if (s != HNODE || (no_zero_count && !nodes[node].count)) {
126        bits[*pos] = pfx;
127        lens[*pos] = pl;
128        xlat[*pos] = s;
129        (*pos)++;
130    } else {
131        pfx <<= 1;
132        pl++;
133        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
134                       pos, no_zero_count);
135        pfx |= 1;
136        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
137                       pos, no_zero_count);
138    }
139}
140
141static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags, int nb_bits)
142{
143    int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
144    uint32_t bits[256];
145    int16_t lens[256];
146    uint8_t xlat[256];
147    int pos = 0;
148
149    get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
150                   &pos, no_zero_count);
151    return ff_init_vlc_sparse(vlc, nb_bits, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
152}
153
154
155/**
156 * nodes size must be 2*nb_codes
157 * first nb_codes nodes.count must be set
158 */
159int ff_huff_build_tree(void *logctx, VLC *vlc, int nb_codes, int nb_bits,
160                       Node *nodes, HuffCmp cmp, int flags)
161{
162    int i, j;
163    int cur_node;
164    int64_t sum = 0;
165
166    for (i = 0; i < nb_codes; i++) {
167        nodes[i].sym = i;
168        nodes[i].n0 = -2;
169        sum += nodes[i].count;
170    }
171
172    if (sum >> 31) {
173        av_log(logctx, AV_LOG_ERROR,
174               "Too high symbol frequencies. "
175               "Tree construction is not possible\n");
176        return -1;
177    }
178    AV_QSORT(nodes, nb_codes, Node, cmp);
179    cur_node = nb_codes;
180    nodes[nb_codes*2-1].count = 0;
181    for (i = 0; i < nb_codes * 2 - 1; i += 2) {
182        uint32_t cur_count = nodes[i].count + nodes[i+1].count;
183        // find correct place to insert new node, and
184        // make space for the new node while at it
185        for(j = cur_node; j > i + 2; j--){
186            if(cur_count > nodes[j-1].count ||
187               (cur_count == nodes[j-1].count &&
188                !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
189                break;
190            nodes[j] = nodes[j - 1];
191        }
192        nodes[j].sym = HNODE;
193        nodes[j].count = cur_count;
194        nodes[j].n0 = i;
195        cur_node++;
196    }
197    if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags, nb_bits) < 0) {
198        av_log(logctx, AV_LOG_ERROR, "Error building tree\n");
199        return -1;
200    }
201    return 0;
202}
203