| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2010 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: March 2001 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Minor fixes to make it properly ansi and fit into cst */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
/* |
| 41 |
* This source code is a product of Sun Microsystems, Inc. and is provided |
| 42 |
* for unrestricted use. Users may copy or modify this source code without |
| 43 |
* charge. |
| 44 |
* |
| 45 |
* SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING |
| 46 |
* THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
| 47 |
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
| 48 |
* |
| 49 |
* Sun source code is provided with no support and without any obligation on |
| 50 |
* the part of Sun Microsystems, Inc. to assist in its use, correction, |
| 51 |
* modification or enhancement. |
| 52 |
* |
| 53 |
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
| 54 |
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE |
| 55 |
* OR ANY PART THEREOF. |
| 56 |
* |
| 57 |
* In no event will Sun Microsystems, Inc. be liable for any lost revenue |
| 58 |
* or profits or other special, indirect and consequential damages, even if |
| 59 |
* Sun has been advised of the possibility of such damages. |
| 60 |
* |
| 61 |
* Sun Microsystems, Inc. |
| 62 |
* 2550 Garcia Avenue |
| 63 |
* Mountain View, California 94043 |
| 64 |
*/ |
| 65 |
|
| 66 |
/* |
| 67 |
* g721.c |
| 68 |
* |
| 69 |
* Description: |
| 70 |
* |
| 71 |
* g721_encoder(), g721_decoder() |
| 72 |
* |
| 73 |
* These routines comprise an implementation of the CCITT G.721 ADPCM |
| 74 |
* coding algorithm. Essentially, this implementation is identical to |
| 75 |
* the bit level description except for a few deviations which |
| 76 |
* take advantage of work station attributes, such as hardware 2's |
| 77 |
* complement arithmetic and large memory. Specifically, certain time |
| 78 |
* consuming operations such as multiplications are replaced |
| 79 |
* with lookup tables and software 2's complement operations are |
| 80 |
* replaced with hardware 2's complement. |
| 81 |
* |
| 82 |
* The deviation from the bit level specification (lookup tables) |
| 83 |
* preserves the bit level performance specifications. |
| 84 |
* |
| 85 |
* As outlined in the G.721 Recommendation, the algorithm is broken |
| 86 |
* down into modules. Each section of code below is preceded by |
| 87 |
* the name of the module which it is implementing. |
| 88 |
* |
| 89 |
*/ |
| 90 |
#include "g72x.h" |
| 91 |
|
| 92 |
static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400}; |
| 93 |
/* |
| 94 |
* Maps G.721 code word to reconstructed scale factor normalized log |
| 95 |
* magnitude values. |
| 96 |
*/ |
| 97 |
static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425, |
| 98 |
425, 373, 323, 273, 213, 135, 4, -2048}; |
| 99 |
|
| 100 |
/* Maps G.721 code word to log of scale factor multiplier. */ |
| 101 |
static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122, |
| 102 |
1122, 355, 198, 112, 64, 41, 18, -12}; |
| 103 |
/* |
| 104 |
* Maps G.721 code words to a set of values whose long and short |
| 105 |
* term averages are computed and then compared to give an indication |
| 106 |
* how stationary (steady state) the signal is. |
| 107 |
*/ |
| 108 |
static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00, |
| 109 |
0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0}; |
| 110 |
|
| 111 |
/* |
| 112 |
* g721_encoder() |
| 113 |
* |
| 114 |
* Encodes the input vale of linear PCM, A-law or u-law data sl and returns |
| 115 |
* the resulting code. -1 is returned for unknown input coding value. |
| 116 |
*/ |
| 117 |
int |
| 118 |
g721_encoder( |
| 119 |
int sl, |
| 120 |
int in_coding, |
| 121 |
struct g72x_state *state_ptr) |
| 122 |
{ |
| 123 |
short sezi, se, sez; /* ACCUM */ |
| 124 |
short d; /* SUBTA */ |
| 125 |
short sr; /* ADDB */ |
| 126 |
short y; /* MIX */ |
| 127 |
short dqsez; /* ADDC */ |
| 128 |
short dq, i; |
| 129 |
|
| 130 |
switch (in_coding) { /* linearize input sample to 14-bit PCM */ |
| 131 |
case AUDIO_ENCODING_LINEAR: |
| 132 |
sl >>= 2; /* 14-bit dynamic range */ |
| 133 |
break; |
| 134 |
default: |
| 135 |
return (-1); |
| 136 |
} |
| 137 |
|
| 138 |
sezi = g72x_predictor_zero(state_ptr); |
| 139 |
sez = sezi >> 1; |
| 140 |
se = (sezi + g72x_predictor_pole(state_ptr)) >> 1; /* estimated signal */ |
| 141 |
|
| 142 |
d = sl - se; /* estimation difference */ |
| 143 |
|
| 144 |
/* quantize the prediction difference */ |
| 145 |
y = g72x_step_size(state_ptr); /* quantizer step size */ |
| 146 |
i = g72x_quantize(d, y, qtab_721, 7); /* i = ADPCM code */ |
| 147 |
|
| 148 |
dq = g72x_reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */ |
| 149 |
|
| 150 |
sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */ |
| 151 |
|
| 152 |
dqsez = sr + sez - se; /* pole prediction diff. */ |
| 153 |
|
| 154 |
g72x_update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr); |
| 155 |
|
| 156 |
return (i); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* g721_decoder() |
| 161 |
* |
| 162 |
* Description: |
| 163 |
* |
| 164 |
* Decodes a 4-bit code of G.721 encoded data of i and |
| 165 |
* returns the resulting linear PCM, A-law or u-law value. |
| 166 |
* return -1 for unknown out_coding value. |
| 167 |
*/ |
| 168 |
int |
| 169 |
g721_decoder( |
| 170 |
int i, |
| 171 |
int out_coding, |
| 172 |
struct g72x_state *state_ptr) |
| 173 |
{ |
| 174 |
short sezi, sei, sez, se; /* ACCUM */ |
| 175 |
short y; /* MIX */ |
| 176 |
short sr; /* ADDB */ |
| 177 |
short dq; |
| 178 |
short dqsez; |
| 179 |
|
| 180 |
i &= 0x0f; /* mask to get proper bits */ |
| 181 |
sezi = g72x_predictor_zero(state_ptr); |
| 182 |
sez = sezi >> 1; |
| 183 |
sei = sezi + g72x_predictor_pole(state_ptr); |
| 184 |
se = sei >> 1; /* se = estimated signal */ |
| 185 |
|
| 186 |
y = g72x_step_size(state_ptr); /* dynamic quantizer step size */ |
| 187 |
|
| 188 |
dq = g72x_reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */ |
| 189 |
|
| 190 |
sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */ |
| 191 |
|
| 192 |
dqsez = sr - se + sez; /* pole prediction diff. */ |
| 193 |
|
| 194 |
g72x_update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr); |
| 195 |
|
| 196 |
switch (out_coding) { |
| 197 |
case AUDIO_ENCODING_LINEAR: |
| 198 |
return (sr << 2); /* sr was 14-bit dynamic range */ |
| 199 |
default: |
| 200 |
return (-1); |
| 201 |
} |
| 202 |
} |