summaryrefslogtreecommitdiff
path: root/audio_codec/libcook/ra_envelope.c (plain)
blob: 87a7f33d8c2a35931efbad5bbdb72a18c90f0db4
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: envelope.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 * envelope.c - power envelope reconstruction
44 **************************************************************************************/
45
46#include "coder.h"
47
48/* coding params for rms[0] */
49#define RMS0BITS 6 /* bits for env[0] */
50#define RMS0MIN -6 /* arbitrary! */
51#define CODE2RMS(i) ((i)+(RMS0MIN))
52
53/**************************************************************************************
54 * Function: DecodeEnvelope
55 *
56 * Description: decode the power envelope
57 *
58 * Inputs: pointer to initialized Gecko2Info struct
59 * number of bits remaining in bitstream for this frame
60 * index of current channel
61 *
62 * Outputs: rmsIndex[0, ..., cregsions-1] has power index for each region
63 * updated rmsMax with largest value of rmsImdex for this channel
64 *
65 * Return: number of bits remaining in bitstream, -1 if out-of-bits
66 **************************************************************************************/
67int DecodeEnvelope(Gecko2Info *gi, int availbits, int ch)
68{
69 int r, code, nbits, rprime, cache, rmsMax;
70 int *rmsIndex = gi->db.rmsIndex;
71 BitStreamInfo *bsi = &(gi->bsi);
72
73 if (availbits < RMS0BITS) {
74 return -1;
75 }
76
77 /* unpack first index */
78 code = GetBits(bsi, RMS0BITS, 1);
79 availbits -= RMS0BITS;
80 rmsIndex[0] = CODE2RMS(code);
81
82 /* check for escape code */
83 /* ASSERT(rmsIndex[0] != 0); */
84
85 rmsMax = rmsIndex[0];
86 for (r = 1; r < gi->cRegions; r++) {
87
88 /* for interleaved regions, choose a reasonable table */
89 if (r < 2 * gi->cplStart) {
90 rprime = r >> 1;
91 if (rprime < 1) {
92 rprime = 1;
93 }
94 } else {
95 rprime = r - gi->cplStart;
96 }
97
98 /* above NUM_POWTABLES, always use the same Huffman table */
99 if (rprime > NUM_POWTABLES) {
100 rprime = NUM_POWTABLES;
101 }
102
103 cache = GetBits(bsi, MAX_HUFF_BITS, 0);
104 nbits = DecodeHuffmanScalar(huffTabPower, &huffTabPowerInfo[rprime - 1], cache, &code);
105
106 /* ran out of bits coding power envelope - should not happen (encoder spec) */
107 if (nbits > availbits) {
108 return -1;
109 }
110
111 availbits -= nbits;
112 AdvanceBitstream(bsi, nbits);
113
114 /* encoder uses differential coding with differences constrained to the range [-12, 11] */
115 rmsIndex[r] = rmsIndex[r - 1] + (code - 12);
116 if (rmsIndex[r] > rmsMax) {
117 rmsMax = rmsIndex[r];
118 }
119 }
120 gi->rmsMax[ch] = rmsMax;
121
122 return availbits;
123}
124