summaryrefslogtreecommitdiff
path: root/audio_codec/libcook/ra_bitpack.c (plain)
blob: d9181dbd3426cf26ac6997d77378f15096a3aac7
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: bitpack.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 * bitpack.c - bitstream parsing functions
44 **************************************************************************************/
45
46#include "coder.h"
47#include "assembly.h"
48
49const unsigned char pkkey[4] = { 0x37, 0xc5, 0x11, 0xf2 };
50
51/**************************************************************************************
52 * Function: GetBits
53 *
54 * Description: get bits from bitstream, optionally advance bitstream pointer
55 *
56 * Inputs: pointer to initialized BitStreamInfo struct
57 * number of bits to get from bitstream
58 * flag to indicate whether to advance bitstream pointer or not
59 *
60 * Outputs: updated bitstream info struct (if advanceFlag set)
61 *
62 * Return: the next nBits bits of data from bitstream buffer (right-justified)
63 *
64 * Notes: nBits must be in range [0, 31], nBits outside this range masked by 0x1f
65 * for speed, does not indicate error if you overrun bit buffer
66 * if nBits = 0, returns 0
67 * applies XOR key when reading (rather than using out-of-place buffer)
68 * reads byte-at-a-time (not cached 32-bit ints) so not designed to be
69 * especially fast (i.e. handy for Huffman decoding of a few symbols,
70 * but not optimal for decoding long sequences of codewords)
71 **************************************************************************************/
72unsigned int GetBits(BitStreamInfo *bsi, int nBits, int advanceFlag)
73{
74 int readBits, off, key, nBytes;
75 unsigned int data;
76 unsigned char *buf;
77
78 nBits &= 0x1f;
79 if (!nBits) {
80 return 0;
81 }
82
83 buf = bsi->buf;
84 off = bsi->off;
85 key = bsi->key;
86
87 /* first, partial byte */
88 data = ((unsigned int)(*buf++ ^ pkkey[key++])) << (24 + off);
89 key &= 0x03;
90 readBits = 8 - off;
91
92 /* whole bytes */
93 while (readBits < nBits && readBits <= 24) {
94 data |= ((unsigned int)(*buf++ ^ pkkey[key++])) << (24 - readBits);
95 key &= 0x03;
96 readBits += 8;
97 }
98
99 /* final, partial byte (no need to update local key and readBits) */
100 if (readBits < nBits) {
101 data |= ((unsigned int)(*buf++ ^ pkkey[key])) >> (readBits - 24);
102 }
103
104 if (advanceFlag) {
105 nBytes = (bsi->off + nBits) >> 3;
106 bsi->buf += nBytes;
107 bsi->off = (bsi->off + nBits) & 0x07;
108 bsi->key = (bsi->key + nBytes) & 0x03;
109 }
110
111 return (data >> (32 - nBits));
112}
113
114/**************************************************************************************
115 * Function: AdvanceBitstream
116 *
117 * Description: move bitstream pointer ahead
118 *
119 * Inputs: pointer to initialized BitStreamInfo struct
120 * number of bits to advance bitstream
121 *
122 * Outputs: updated bitstream info struct
123 *
124 * Return: none
125 *
126 * Notes: generally use following a GetBits(bsi, maxBits, 0) (i.e. no advance)
127 **************************************************************************************/
128void AdvanceBitstream(BitStreamInfo *bsi, int nBits)
129{
130 int nBytes;
131
132 nBytes = (bsi->off + nBits) >> 3;
133 bsi->buf += nBytes;
134 bsi->off = (bsi->off + nBits) & 0x07;
135 bsi->key = (bsi->key + nBytes) & 0x03;
136}
137
138/**************************************************************************************
139 * Function: DecodeSideInfo
140 *
141 * Description: parse bitstream and decode gain info, coupling info, power envelope,
142 * and categorization code
143 *
144 * Inputs: pointer to initialized Gecko2Info struct
145 * pointer to bitstream buffer (byte-aligned)
146 * number of bits available in buf
147 * channel index
148 *
149 * Outputs: filled-in dgainc structs, channel coupling indices (if joint stereo),
150 * power envelope index for each region,
151 * and rate code (selected categorization)
152 *
153 * Return: number of bits remaining in bitstream, -1 if out-of-bits
154 *
155 * Notes: encoder guarantees that gain, couple, envelope, and rateCode do
156 * not run out of bits
157 **************************************************************************************/
158int DecodeSideInfo(Gecko2Info *gi, unsigned char *buf, int availbits, int ch)
159{
160 BitStreamInfo *bsi = &(gi->bsi);
161
162 /* init bitstream reader */
163 bsi->buf = buf;
164 bsi->off = 0;
165 bsi->key = 0;
166
167 /* decode gain info */
168 availbits = DecodeGainInfo(gi, &(gi->dgainc[ch][1]), availbits);
169 if (availbits < 0) {
170 return -1;
171 }
172
173 /* replicate gain control and decode coupling info, if joint stereo */
174 if (gi->jointStereo) {
175 ASSERT(ch == 0);
176 CopyGainInfo(&gi->dgainc[1][1], &gi->dgainc[0][1]);
177 availbits = DecodeCoupleInfo(gi, availbits);
178 if (availbits < 0) {
179 return -1;
180 }
181 }
182
183 /* decode power envelope (return error if runs out of bits) */
184 availbits = DecodeEnvelope(gi, availbits, ch);
185 if (availbits < 0) {
186 return -1;
187 }
188
189 /* decode rate code (return error if runs out of bits) */
190 if (availbits < gi->rateBits) {
191 return -1;
192 }
193 gi->rateCode = GetBits(bsi, gi->rateBits, 1);
194 availbits -= gi->rateBits;
195
196 return availbits;
197}
198