File: crn_huffman_codes.cpp

package info (click to toggle)
crunch-dxtc 0.55.5-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 3,600 kB
  • sloc: cpp: 64,979; ansic: 633; python: 321; makefile: 112
file content (366 lines) | stat: -rw-r--r-- 9,356 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// File: crn_huffman_codes.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_huffman_codes.h"

namespace crnlib {
struct sym_freq {
  uint m_freq;
  uint16 m_left;
  uint16 m_right;

  inline bool operator<(const sym_freq& other) const {
    return m_freq > other.m_freq;
  }
};

static inline sym_freq* radix_sort_syms(uint num_syms, sym_freq* syms0, sym_freq* syms1) {
  const uint cMaxPasses = 2;
  uint hist[256 * cMaxPasses];

  memset(hist, 0, sizeof(hist[0]) * 256 * cMaxPasses);

  sym_freq* p = syms0;
  sym_freq* q = syms0 + (num_syms >> 1) * 2;

  for (; p != q; p += 2) {
    const uint freq0 = p[0].m_freq;
    const uint freq1 = p[1].m_freq;

    hist[freq0 & 0xFF]++;
    hist[256 + ((freq0 >> 8) & 0xFF)]++;

    hist[freq1 & 0xFF]++;
    hist[256 + ((freq1 >> 8) & 0xFF)]++;
  }

  if (num_syms & 1) {
    const uint freq = p->m_freq;

    hist[freq & 0xFF]++;
    hist[256 + ((freq >> 8) & 0xFF)]++;
  }

  sym_freq* pCur_syms = syms0;
  sym_freq* pNew_syms = syms1;

  for (uint pass = 0; pass < cMaxPasses; pass++) {
    const uint* pHist = &hist[pass << 8];

    uint offsets[256];

    uint cur_ofs = 0;
    for (uint i = 0; i < 256; i += 2) {
      offsets[i] = cur_ofs;
      cur_ofs += pHist[i];

      offsets[i + 1] = cur_ofs;
      cur_ofs += pHist[i + 1];
    }

    const uint pass_shift = pass << 3;

    sym_freq* p = pCur_syms;
    sym_freq* q = pCur_syms + (num_syms >> 1) * 2;

    for (; p != q; p += 2) {
      uint c0 = p[0].m_freq;
      uint c1 = p[1].m_freq;

      if (pass) {
        c0 >>= 8;
        c1 >>= 8;
      }

      c0 &= 0xFF;
      c1 &= 0xFF;

      if (c0 == c1) {
        uint dst_offset0 = offsets[c0];

        offsets[c0] = dst_offset0 + 2;

        pNew_syms[dst_offset0] = p[0];
        pNew_syms[dst_offset0 + 1] = p[1];
      } else {
        uint dst_offset0 = offsets[c0]++;
        uint dst_offset1 = offsets[c1]++;

        pNew_syms[dst_offset0] = p[0];
        pNew_syms[dst_offset1] = p[1];
      }
    }

    if (num_syms & 1) {
      uint c = ((p->m_freq) >> pass_shift) & 0xFF;

      uint dst_offset = offsets[c];
      offsets[c] = dst_offset + 1;

      pNew_syms[dst_offset] = *p;
    }

    sym_freq* t = pCur_syms;
    pCur_syms = pNew_syms;
    pNew_syms = t;
  }

#ifdef CRNLIB_ASSERTS_ENABLED
  uint prev_freq = 0;
  for (uint i = 0; i < num_syms; i++) {
    CRNLIB_ASSERT(!(pCur_syms[i].m_freq < prev_freq));
    prev_freq = pCur_syms[i].m_freq;
  }
#endif

  return pCur_syms;
}

struct huffman_work_tables {
  enum { cMaxInternalNodes = cHuffmanMaxSupportedSyms };

  sym_freq syms0[cHuffmanMaxSupportedSyms + 1 + cMaxInternalNodes];
  sym_freq syms1[cHuffmanMaxSupportedSyms + 1 + cMaxInternalNodes];

  uint16 queue[cMaxInternalNodes];
};

void* create_generate_huffman_codes_tables() {
  return crnlib_new<huffman_work_tables>();
}

void free_generate_huffman_codes_tables(void* p) {
  crnlib_delete(static_cast<huffman_work_tables*>(p));
}

#if USE_CALCULATE_MINIMUM_REDUNDANCY
/* calculate_minimum_redundancy() written by
      Alistair Moffat, alistair@cs.mu.oz.au,
      Jyrki Katajainen, jyrki@diku.dk
      November 1996.
   */
static void calculate_minimum_redundancy(int A[], int n) {
  int root; /* next root node to be used */
  int leaf; /* next leaf to be used */
  int next; /* next value to be assigned */
  int avbl; /* number of available nodes */
  int used; /* number of internal nodes */
  int dpth; /* current depth of leaves */

  /* check for pathological cases */
  if (n == 0) {
    return;
  }
  if (n == 1) {
    A[0] = 0;
    return;
  }

  /* first pass, left to right, setting parent pointers */
  A[0] += A[1];
  root = 0;
  leaf = 2;
  for (next = 1; next < n - 1; next++) {
    /* select first item for a pairing */
    if (leaf >= n || A[root] < A[leaf]) {
      A[next] = A[root];
      A[root++] = next;
    } else
      A[next] = A[leaf++];

    /* add on the second item */
    if (leaf >= n || (root < next && A[root] < A[leaf])) {
      A[next] += A[root];
      A[root++] = next;
    } else
      A[next] += A[leaf++];
  }

  /* second pass, right to left, setting internal depths */
  A[n - 2] = 0;
  for (next = n - 3; next >= 0; next--)
    A[next] = A[A[next]] + 1;

  /* third pass, right to left, setting leaf depths */
  avbl = 1;
  used = dpth = 0;
  root = n - 2;
  next = n - 1;
  while (avbl > 0) {
    while (root >= 0 && A[root] == dpth) {
      used++;
      root--;
    }
    while (avbl > used) {
      A[next--] = dpth;
      avbl--;
    }
    avbl = 2 * used;
    dpth++;
    used = 0;
  }
}
#endif

bool generate_huffman_codes(void* pContext, uint num_syms, const uint16* pFreq, uint8* pCodesizes, uint& max_code_size, uint& total_freq_ret) {
  if ((!num_syms) || (num_syms > cHuffmanMaxSupportedSyms))
    return false;

  huffman_work_tables& state = *static_cast<huffman_work_tables*>(pContext);
  ;

  uint max_freq = 0;
  uint total_freq = 0;

  uint num_used_syms = 0;
  for (uint i = 0; i < num_syms; i++) {
    uint freq = pFreq[i];

    if (!freq)
      pCodesizes[i] = 0;
    else {
      total_freq += freq;
      max_freq = math::maximum(max_freq, freq);

      sym_freq& sf = state.syms0[num_used_syms];
      sf.m_left = (uint16)i;
      sf.m_right = cUINT16_MAX;
      sf.m_freq = freq;
      num_used_syms++;
    }
  }

  total_freq_ret = total_freq;

  if (num_used_syms == 1) {
    pCodesizes[state.syms0[0].m_left] = 1;
    return true;
  }

  sym_freq* syms = radix_sort_syms(num_used_syms, state.syms0, state.syms1);

#if USE_CALCULATE_MINIMUM_REDUNDANCY
  int x[cHuffmanMaxSupportedSyms];
  for (uint i = 0; i < num_used_syms; i++)
    x[i] = state.syms0[i].m_freq;

  calculate_minimum_redundancy(x, num_used_syms);

  uint max_len = 0;
  for (uint i = 0; i < num_used_syms; i++) {
    uint len = x[i];
    max_len = math::maximum(len, max_len);
    pCodesizes[state.syms0[i].m_left] = static_cast<uint8>(len);
  }

  return true;
#else
  // Dummy node
  sym_freq& sf = state.syms0[num_used_syms];
  sf.m_left = cUINT16_MAX;
  sf.m_right = cUINT16_MAX;
  sf.m_freq = UINT_MAX;

  uint next_internal_node = num_used_syms + 1;

  uint queue_front = 0;
  uint queue_end = 0;

  uint next_lowest_sym = 0;

  uint num_nodes_remaining = num_used_syms;
  do {
    uint left_freq = syms[next_lowest_sym].m_freq;
    uint left_child = next_lowest_sym;

    if ((queue_end > queue_front) && (syms[state.queue[queue_front]].m_freq < left_freq)) {
      left_child = state.queue[queue_front];
      left_freq = syms[left_child].m_freq;

      queue_front++;
    } else
      next_lowest_sym++;

    uint right_freq = syms[next_lowest_sym].m_freq;
    uint right_child = next_lowest_sym;

    if ((queue_end > queue_front) && (syms[state.queue[queue_front]].m_freq < right_freq)) {
      right_child = state.queue[queue_front];
      right_freq = syms[right_child].m_freq;

      queue_front++;
    } else
      next_lowest_sym++;

    const uint internal_node_index = next_internal_node;
    next_internal_node++;

    CRNLIB_ASSERT(next_internal_node < CRNLIB_ARRAYSIZE(state.syms0));

    syms[internal_node_index].m_freq = left_freq + right_freq;
    syms[internal_node_index].m_left = static_cast<uint16>(left_child);
    syms[internal_node_index].m_right = static_cast<uint16>(right_child);

    CRNLIB_ASSERT(queue_end < huffman_work_tables::cMaxInternalNodes);
    state.queue[queue_end] = static_cast<uint16>(internal_node_index);
    queue_end++;

    num_nodes_remaining--;

  } while (num_nodes_remaining > 1);

  CRNLIB_ASSERT(next_lowest_sym == num_used_syms);
  CRNLIB_ASSERT((queue_end - queue_front) == 1);

  uint cur_node_index = state.queue[queue_front];

  uint32* pStack = (syms == state.syms0) ? (uint32*)state.syms1 : (uint32*)state.syms0;
  uint32* pStack_top = pStack;

  uint max_level = 0;

  for (;;) {
    uint level = cur_node_index >> 16;
    uint node_index = cur_node_index & 0xFFFF;

    uint left_child = syms[node_index].m_left;
    uint right_child = syms[node_index].m_right;

    uint next_level = (cur_node_index + 0x10000) & 0xFFFF0000;

    if (left_child < num_used_syms) {
      max_level = math::maximum(max_level, level);

      pCodesizes[syms[left_child].m_left] = static_cast<uint8>(level + 1);

      if (right_child < num_used_syms) {
        pCodesizes[syms[right_child].m_left] = static_cast<uint8>(level + 1);

        if (pStack == pStack_top)
          break;
        cur_node_index = *--pStack;
      } else {
        cur_node_index = next_level | right_child;
      }
    } else {
      if (right_child < num_used_syms) {
        max_level = math::maximum(max_level, level);

        pCodesizes[syms[right_child].m_left] = static_cast<uint8>(level + 1);

        cur_node_index = next_level | left_child;
      } else {
        *pStack++ = next_level | left_child;

        cur_node_index = next_level | right_child;
      }
    }
  }

  max_code_size = max_level + 1;
#endif

  return true;
}

}  // namespace crnlib