~ubuntu-branches/ubuntu/saucy/gst-libav1.0/saucy-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/huffman.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-07-30 09:00:15 UTC
  • mfrom: (1.1.16) (7.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20130730090015-sc1ou2yssu7q5w4e
Tags: 1.1.3-1
* New upstream development snapshot:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.1.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (c) 2006 Konstantin Shishkov
 
3
 * Copyright (c) 2007 Loren Merritt
3
4
 *
4
5
 * This file is part of Libav.
5
6
 *
30
31
/* symbol for Huffman tree node */
31
32
#define HNODE -1
32
33
 
33
 
 
34
 
static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat, Node *nodes, int node, uint32_t pfx, int pl, int *pos, int no_zero_count)
 
34
typedef struct {
 
35
    uint64_t val;
 
36
    int name;
 
37
} HeapElem;
 
38
 
 
39
static void heap_sift(HeapElem *h, int root, int size)
 
40
{
 
41
    while (root * 2 + 1 < size) {
 
42
        int child = root * 2 + 1;
 
43
        if (child < size - 1 && h[child].val > h[child+1].val)
 
44
            child++;
 
45
        if (h[root].val > h[child].val) {
 
46
            FFSWAP(HeapElem, h[root], h[child]);
 
47
            root = child;
 
48
        } else
 
49
            break;
 
50
    }
 
51
}
 
52
 
 
53
void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
 
54
{
 
55
    HeapElem h[256];
 
56
    int up[2*256];
 
57
    int len[2*256];
 
58
    int offset, i, next;
 
59
    int size = 256;
 
60
 
 
61
    for (offset = 1; ; offset <<= 1) {
 
62
        for (i=0; i < size; i++) {
 
63
            h[i].name = i;
 
64
            h[i].val = (stats[i] << 8) + offset;
 
65
        }
 
66
        for (i = size / 2 - 1; i >= 0; i--)
 
67
            heap_sift(h, i, size);
 
68
 
 
69
        for (next = size; next < size * 2 - 1; next++) {
 
70
            // merge the two smallest entries, and put it back in the heap
 
71
            uint64_t min1v = h[0].val;
 
72
            up[h[0].name] = next;
 
73
            h[0].val = INT64_MAX;
 
74
            heap_sift(h, 0, size);
 
75
            up[h[0].name] = next;
 
76
            h[0].name = next;
 
77
            h[0].val += min1v;
 
78
            heap_sift(h, 0, size);
 
79
        }
 
80
 
 
81
        len[2 * size - 2] = 0;
 
82
        for (i = 2 * size - 3; i >= size; i--)
 
83
            len[i] = len[up[i]] + 1;
 
84
        for (i = 0; i < size; i++) {
 
85
            dst[i] = len[up[i]] + 1;
 
86
            if (dst[i] >= 32) break;
 
87
        }
 
88
        if (i==size) break;
 
89
    }
 
90
}
 
91
 
 
92
static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
 
93
                           Node *nodes, int node,
 
94
                           uint32_t pfx, int pl, int *pos, int no_zero_count)
35
95
{
36
96
    int s;
37
97
 
38
98
    s = nodes[node].sym;
39
 
    if(s != HNODE || (no_zero_count && !nodes[node].count)){
 
99
    if (s != HNODE || (no_zero_count && !nodes[node].count)) {
40
100
        bits[*pos] = pfx;
41
101
        lens[*pos] = pl;
42
102
        xlat[*pos] = s;
43
103
        (*pos)++;
44
 
    }else{
 
104
    } else {
45
105
        pfx <<= 1;
46
106
        pl++;
47
 
        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos,
48
 
                       no_zero_count);
 
107
        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
 
108
                       pos, no_zero_count);
49
109
        pfx |= 1;
50
 
        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos,
51
 
                       no_zero_count);
 
110
        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
 
111
                       pos, no_zero_count);
52
112
    }
53
113
}
54
114
 
60
120
    uint8_t xlat[256];
61
121
    int pos = 0;
62
122
 
63
 
    get_tree_codes(bits, lens, xlat, nodes, head, 0, 0, &pos, no_zero_count);
 
123
    get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
 
124
                   &pos, no_zero_count);
64
125
    return ff_init_vlc_sparse(vlc, 9, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
65
126
}
66
127
 
76
137
    int cur_node;
77
138
    int64_t sum = 0;
78
139
 
79
 
    for(i = 0; i < nb_codes; i++){
 
140
    for (i = 0; i < nb_codes; i++) {
80
141
        nodes[i].sym = i;
81
142
        nodes[i].n0 = -2;
82
143
        sum += nodes[i].count;
83
144
    }
84
145
 
85
 
    if(sum >> 31) {
86
 
        av_log(avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
 
146
    if (sum >> 31) {
 
147
        av_log(avctx, AV_LOG_ERROR,
 
148
               "Too high symbol frequencies. "
 
149
               "Tree construction is not possible\n");
87
150
        return -1;
88
151
    }
89
152
    qsort(nodes, nb_codes, sizeof(Node), cmp);
90
153
    cur_node = nb_codes;
91
154
    nodes[nb_codes*2-1].count = 0;
92
 
    for(i = 0; i < nb_codes*2-1; i += 2){
 
155
    for (i = 0; i < nb_codes * 2 - 1; i += 2) {
93
156
        nodes[cur_node].sym = HNODE;
94
 
        nodes[cur_node].count = nodes[i].count + nodes[i+1].count;
 
157
        nodes[cur_node].count = nodes[i].count + nodes[i + 1].count;
95
158
        nodes[cur_node].n0 = i;
96
 
        for(j = cur_node; j > 0; j--){
97
 
            if(nodes[j].count > nodes[j-1].count ||
98
 
               (nodes[j].count == nodes[j-1].count &&
99
 
                (!(flags & FF_HUFFMAN_FLAG_HNODE_FIRST) ||
100
 
                 nodes[j].n0==j-1 || nodes[j].n0==j-2 ||
101
 
                 (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
 
159
        for (j = cur_node; j > 0; j--) {
 
160
            if (nodes[j].count > nodes[j - 1].count ||
 
161
                (nodes[j].count == nodes[j - 1].count &&
 
162
                 (!(flags & FF_HUFFMAN_FLAG_HNODE_FIRST) ||
 
163
                  nodes[j].n0 == j - 1 || nodes[j].n0 == j - 2 ||
 
164
                  (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
102
165
                break;
103
 
            FFSWAP(Node, nodes[j], nodes[j-1]);
 
166
            FFSWAP(Node, nodes[j], nodes[j - 1]);
104
167
        }
105
168
        cur_node++;
106
169
    }
107
 
    if(build_huff_tree(vlc, nodes, nb_codes*2-2, flags) < 0){
 
170
    if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags) < 0) {
108
171
        av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
109
172
        return -1;
110
173
    }