summaryrefslogtreecommitdiff
path: root/audio_codec/libcook/ra_huffman.c (plain)
blob: ca547709fec7bdebcea2d14541de0b615e3f5204
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: huffman.c,v 1.6 2005/04/27 19:20:50 hubbe Exp $
3 *
4 * REALNETWORKS CONFIDENTIAL--NOT FOR DISTRIBUTION IN SOURCE CODE FORM
5 * Portions Copyright (c) 1995-2002 RealNetworks, Inc.
6 * All Rights Reserved.
7 *
8 * The contents of this file, and the files included with this file,
9 * are subject to the current version of the Real Format Source Code
10 * Porting and Optimization License, available at
11 * https://helixcommunity.org/2005/license/realformatsource (unless
12 * RealNetworks otherwise expressly agrees in writing that you are
13 * subject to a different license). You may also obtain the license
14 * terms directly from RealNetworks. You may not use this file except
15 * in compliance with the Real Format Source Code Porting and
16 * Optimization License. There are no redistribution rights for the
17 * source code of this file. Please see the Real Format Source Code
18 * Porting and Optimization License for the rights, obligations and
19 * limitations governing use of the contents of the file.
20 *
21 * RealNetworks is the developer of the Original Code and owns the
22 * copyrights in the portions it created.
23 *
24 * This file, and the files included with this file, is distributed and
25 * made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND,
26 * EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL
27 * SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT
29 * OR NON-INFRINGEMENT.
30 *
31 * Technology Compatibility Kit Test Suite(s) Location:
32 * https://rarvcode-tck.helixcommunity.org
33 *
34 * Contributor(s):
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/**************************************************************************************
39 * Fixed-point RealAudio 8 decoder
40 * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
41 * October 2003
42 *
43 * huffman.c - Wolfgang's implementation of Moffat-Turpin style Huffman decoder
44 **************************************************************************************/
45
46#include "coder.h"
47
48/**************************************************************************************
49 * Function: DecodeHuffmanScalar
50 *
51 * Description: decode one Huffman symbol from bitstream
52 *
53 * Inputs: pointers to table and info
54 * right-aligned bit buffer with MAX_HUFF_BITS bits
55 * pointer to receive decoded symbol
56 *
57 * Outputs: decoded symbol
58 *
59 * Return: number of bits in symbol
60 **************************************************************************************/
61int DecodeHuffmanScalar(const unsigned short *huffTab, const HuffInfo *huffTabInfo, int bitBuf, int *val)
62{
63 const unsigned char *countPtr;
64 unsigned int cache, total, count, t;
65 const unsigned short *map;
66
67 map = huffTab + huffTabInfo->offset;
68 countPtr = huffTabInfo->count;
69 cache = (unsigned int)(bitBuf << (17 - MAX_HUFF_BITS));
70
71 total = 0;
72 count = *countPtr++;
73 t = count << 16;
74
75 while (cache >= t) {
76 cache -= t;
77 cache <<= 1;
78 total += count;
79 count = *countPtr++;
80 t = count << 16;
81 }
82 *val = map[total + (cache >> 16)];
83
84 /* return number of bits in symbol */
85 return (countPtr - huffTabInfo->count);
86}
87