00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "get_bits.h"
00029 #include "huffman.h"
00030
00031
00032 #define HNODE -1
00033
00034 typedef struct {
00035 uint64_t val;
00036 int name;
00037 } HeapElem;
00038
00039 static void heap_sift(HeapElem *h, int root, int size)
00040 {
00041 while (root * 2 + 1 < size) {
00042 int child = root * 2 + 1;
00043 if (child < size - 1 && h[child].val > h[child+1].val)
00044 child++;
00045 if (h[root].val > h[child].val) {
00046 FFSWAP(HeapElem, h[root], h[child]);
00047 root = child;
00048 } else
00049 break;
00050 }
00051 }
00052
00053 void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
00054 {
00055 HeapElem h[256];
00056 int up[2*256];
00057 int len[2*256];
00058 int offset, i, next;
00059 int size = 256;
00060
00061 for (offset = 1; ; offset <<= 1) {
00062 for (i=0; i < size; i++) {
00063 h[i].name = i;
00064 h[i].val = (stats[i] << 8) + offset;
00065 }
00066 for (i = size / 2 - 1; i >= 0; i--)
00067 heap_sift(h, i, size);
00068
00069 for (next = size; next < size * 2 - 1; next++) {
00070
00071 uint64_t min1v = h[0].val;
00072 up[h[0].name] = next;
00073 h[0].val = INT64_MAX;
00074 heap_sift(h, 0, size);
00075 up[h[0].name] = next;
00076 h[0].name = next;
00077 h[0].val += min1v;
00078 heap_sift(h, 0, size);
00079 }
00080
00081 len[2 * size - 2] = 0;
00082 for (i = 2 * size - 3; i >= size; i--)
00083 len[i] = len[up[i]] + 1;
00084 for (i = 0; i < size; i++) {
00085 dst[i] = len[up[i]] + 1;
00086 if (dst[i] >= 32) break;
00087 }
00088 if (i==size) break;
00089 }
00090 }
00091
00092 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
00093 Node *nodes, int node,
00094 uint32_t pfx, int pl, int *pos, int no_zero_count)
00095 {
00096 int s;
00097
00098 s = nodes[node].sym;
00099 if (s != HNODE || (no_zero_count && !nodes[node].count)) {
00100 bits[*pos] = pfx;
00101 lens[*pos] = pl;
00102 xlat[*pos] = s;
00103 (*pos)++;
00104 } else {
00105 pfx <<= 1;
00106 pl++;
00107 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl,
00108 pos, no_zero_count);
00109 pfx |= 1;
00110 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0 + 1, pfx, pl,
00111 pos, no_zero_count);
00112 }
00113 }
00114
00115 static int build_huff_tree(VLC *vlc, Node *nodes, int head, int flags)
00116 {
00117 int no_zero_count = !(flags & FF_HUFFMAN_FLAG_ZERO_COUNT);
00118 uint32_t bits[256];
00119 int16_t lens[256];
00120 uint8_t xlat[256];
00121 int pos = 0;
00122
00123 get_tree_codes(bits, lens, xlat, nodes, head, 0, 0,
00124 &pos, no_zero_count);
00125 return ff_init_vlc_sparse(vlc, 9, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
00126 }
00127
00128
00133 int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes,
00134 Node *nodes, HuffCmp cmp, int flags)
00135 {
00136 int i, j;
00137 int cur_node;
00138 int64_t sum = 0;
00139
00140 for (i = 0; i < nb_codes; i++) {
00141 nodes[i].sym = i;
00142 nodes[i].n0 = -2;
00143 sum += nodes[i].count;
00144 }
00145
00146 if (sum >> 31) {
00147 av_log(avctx, AV_LOG_ERROR,
00148 "Too high symbol frequencies. "
00149 "Tree construction is not possible\n");
00150 return -1;
00151 }
00152 qsort(nodes, nb_codes, sizeof(Node), cmp);
00153 cur_node = nb_codes;
00154 nodes[nb_codes*2-1].count = 0;
00155 for (i = 0; i < nb_codes * 2 - 1; i += 2) {
00156 uint32_t cur_count = nodes[i].count + nodes[i+1].count;
00157
00158
00159 for(j = cur_node; j > i + 2; j--){
00160 if(cur_count > nodes[j-1].count ||
00161 (cur_count == nodes[j-1].count &&
00162 !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
00163 break;
00164 nodes[j] = nodes[j - 1];
00165 }
00166 nodes[j].sym = HNODE;
00167 nodes[j].count = cur_count;
00168 nodes[j].n0 = i;
00169 cur_node++;
00170 }
00171 if (build_huff_tree(vlc, nodes, nb_codes * 2 - 2, flags) < 0) {
00172 av_log(avctx, AV_LOG_ERROR, "Error building tree\n");
00173 return -1;
00174 }
00175 return 0;
00176 }