📄 ima__adpcm_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>audio/IMA_ADPCM.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.1 --><div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a></div><div class="nav"><a class="el" href="dir_000000.html">audio</a></div><h1>IMA_ADPCM.cpp</h1><a href="IMA__ADPCM_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 00025 <span class="preprocessor">#include "<a class="code" href="common_8h.html">common.h</a>"</span>00026 <span class="preprocessor">#include "<a class="code" href="IMA__ADPCM_8h.html">IMA_ADPCM.h</a>"</span>00027 00028 00029 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="group__integers.html#ga1">uint16</a> IMA_ADPCMStepTable[89] =00030 {00031 7, 8, 9, 10, 11, 12, 13, 14,00032 16, 17, 19, 21, 23, 25, 28, 31,00033 34, 37, 41, 45, 50, 55, 60, 66,00034 73, 80, 88, 97, 107, 118, 130, 143,00035 157, 173, 190, 209, 230, 253, 279, 307,00036 337, 371, 408, 449, 494, 544, 598, 658,00037 724, 796, 876, 963, 1060, 1166, 1282, 1411,00038 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,00039 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,00040 7132, 7845, 8630, 9493,10442,11487,12635,13899,00041 15289,16818,18500,20350,22385,24623,27086,29794,00042 3276700043 };00044 00045 00046 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> IMA_ADPCMIndexTable[8] =00047 {00048 -1, -1, -1, -1, 2, 4, 6, 8,00049 };00050 00051 <a name="l00052"></a><a class="code" href="classIMA__ADPCM.html#a0">00052</a> <a class="code" href="common_8h.html#a2">EXPORT</a> <span class="keywordtype">void</span> <a class="code" href="classIMA__ADPCM.html#a0">IMA_ADPCM::EncodeInit</a>(int16 sample1,int16 sample2)00053 {00054 <a class="code" href="classIMA__ADPCM.html#o0">PredictedValue</a> = sample1;00055 <span class="keywordtype">int</span> delta = sample2-sample1;00056 <span class="keywordflow">if</span>(delta<0)00057 delta = - delta;00058 <span class="keywordflow">if</span>(delta>32767)00059 delta = 32767;00060 <span class="keywordtype">int</span> stepIndex = 0;00061 <span class="keywordflow">while</span>(IMA_ADPCMStepTable[stepIndex]<(<a class="code" href="group__integers.html#ga6">uint</a>)delta)00062 stepIndex++;00063 <a class="code" href="classIMA__ADPCM.html#o1">StepIndex</a> = stepIndex;00064 }00065 00066 <a name="l00067"></a><a class="code" href="classIMA__ADPCM.html#a1">00067</a> <a class="code" href="common_8h.html#a2">EXPORT</a> <a class="code" href="group__integers.html#ga6">uint</a> <a class="code" href="classIMA__ADPCM.html#a1">IMA_ADPCM::Encode</a>(int16 pcm16)00068 {00069 <span class="keywordtype">int</span> predicedValue = <a class="code" href="classIMA__ADPCM.html#o0">PredictedValue</a>;00070 <span class="keywordtype">int</span> stepIndex = <a class="code" href="classIMA__ADPCM.html#o1">StepIndex</a>;00071 00072 <span class="keywordtype">int</span> delta = pcm16-predicedValue;00073 <a class="code" href="group__integers.html#ga6">uint</a> value;00074 <span class="keywordflow">if</span>(delta>=0)00075 value = 0;00076 <span class="keywordflow">else</span>00077 {00078 value = 8;00079 delta = -delta;00080 }00081 00082 <span class="keywordtype">int</span> step = IMA_ADPCMStepTable[stepIndex];00083 <span class="keywordtype">int</span> diff = step>>3;00084 <span class="keywordflow">if</span>(delta>step)00085 {00086 value |= 4;00087 delta -= step;00088 diff += step;00089 }00090 step >>= 1;00091 <span class="keywordflow">if</span>(delta>step)00092 {00093 value |= 2;00094 delta -= step;00095 diff += step;00096 }00097 step >>= 1;00098 <span class="keywordflow">if</span>(delta>step)00099 {00100 value |= 1;00101 diff += step;00102 }00103 00104 <span class="keywordflow">if</span>(value&8)00105 predicedValue -= diff;00106 <span class="keywordflow">else</span>00107 predicedValue += diff;00108 <span class="keywordflow">if</span>(predicedValue<-0x8000)00109 predicedValue = -0x8000;00110 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(predicedValue>0x7fff)00111 predicedValue = 0x7fff;00112 <a class="code" href="classIMA__ADPCM.html#o0">PredictedValue</a> = predicedValue;00113 00114 stepIndex += IMA_ADPCMIndexTable[value&7];00115 <span class="keywordflow">if</span>(stepIndex<0)00116 stepIndex = 0;00117 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(stepIndex>88)00118 stepIndex = 88;00119 <a class="code" href="classIMA__ADPCM.html#o1">StepIndex</a> = stepIndex;00120 00121 <span class="keywordflow">return</span> value;00122 }00123 00124 <a name="l00125"></a><a class="code" href="classIMA__ADPCM.html#a2">00125</a> <a class="code" href="common_8h.html#a2">EXPORT</a> <span class="keywordtype">int</span> <a class="code" href="classIMA__ADPCM.html#a2">IMA_ADPCM::Decode</a>(uint adpcm)00126 {00127 <span class="keywordtype">int</span> stepIndex = <a class="code" href="classIMA__ADPCM.html#o1">StepIndex</a>;00128 <span class="keywordtype">int</span> step = IMA_ADPCMStepTable[stepIndex];00129 00130 stepIndex += IMA_ADPCMIndexTable[adpcm&7];00131 <span class="keywordflow">if</span>(stepIndex<0)00132 stepIndex = 0;00133 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(stepIndex>88)00134 stepIndex = 88;00135 <a class="code" href="classIMA__ADPCM.html#o1">StepIndex</a> = stepIndex;00136 00137 <span class="keywordtype">int</span> diff = step>>3;00138 <span class="keywordflow">if</span>(adpcm&4)00139 diff += step;00140 <span class="keywordflow">if</span>(adpcm&2)00141 diff += step>>1;00142 <span class="keywordflow">if</span>(adpcm&1)00143 diff += step>>2;00144 00145 <span class="keywordtype">int</span> predicedValue = <a class="code" href="classIMA__ADPCM.html#o0">PredictedValue</a>;00146 <span class="keywordflow">if</span>(adpcm&8)00147 predicedValue -= diff;00148 <span class="keywordflow">else</span>00149 predicedValue += diff;00150 <span class="keywordflow">if</span>(predicedValue<-0x8000)00151 predicedValue = -0x8000;00152 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(predicedValue>0x7fff)00153 predicedValue = 0x7fff;00154 <a class="code" href="classIMA__ADPCM.html#o0">PredictedValue</a> = predicedValue;00155 00156 <span class="keywordflow">return</span> predicedValue;00157 }00158 00159 <a name="l00160"></a><a class="code" href="classIMA__ADPCM.html#a3">00160</a> <a class="code" href="common_8h.html#a2">EXPORT</a> <a class="code" href="group__integers.html#ga6">uint</a> <a class="code" href="classIMA__ADPCM.html#a1">IMA_ADPCM::Encode</a>(uint8* dst, <span class="keywordtype">int</span> dstOffset, <span class="keyword">const</span> int16* src, uint srcSize)00161 {00162 <span class="comment">// use given bit offset</span>00163 dst += dstOffset>>3;00164 <a class="code" href="group__integers.html#ga6">uint</a> bitOffset = dstOffset&4;00165 00166 <span class="comment">// make sure srcSize represents a whole number of samples</span>00167 srcSize &= ~1;00168 00169 <span class="comment">// calculate end of input buffer</span>00170 <span class="keyword">const</span> <a class="code" href="group__integers.html#ga4">int16</a>* end = (<span class="keyword">const</span> <a class="code" href="group__integers.html#ga4">int16</a>*)((<span class="keyword">const</span> <a class="code" href="group__integers.html#ga0">uint8</a>*)src+srcSize);00171 00172 <span class="keywordflow">while</span>(src<end)00173 {00174 <span class="comment">// encode a pcm value from input buffer</span>00175 <a class="code" href="group__integers.html#ga6">uint</a> adpcm = <a class="code" href="classIMA__ADPCM.html#a1">Encode</a>(*src++);00176 00177 <span class="comment">// pick which nibble to write adpcm value to...</span>00178 <span class="keywordflow">if</span>(!bitOffset)00179 *dst = adpcm; <span class="comment">// write adpcm value to low nibble</span>00180 <span class="keywordflow">else</span>00181 {00182 <a class="code" href="group__integers.html#ga6">uint</a> b = *dst; <span class="comment">// get byte from ouput</span>00183 b &= 0x0f; <span class="comment">// clear bits of high nibble</span>00184 b |= adpcm<<4; <span class="comment">// or adpcm value into the high nibble</span>00185 *dst++ = (<a class="code" href="group__integers.html#ga0">uint8</a>)b; <span class="comment">// write value back to output and move on to next byte</span>00186 }00187 00188 <span class="comment">// toggle which nibble in byte to write to next</span>00189 bitOffset ^= 4; 00190 }00191 00192 <span class="comment">// return number bits written to dst</span>00193 <span class="keywordflow">return</span> srcSize*2;00194 }00195 00196 <a name="l00197"></a><a class="code" href="classIMA__ADPCM.html#a4">00197</a> <a class="code" href="common_8h.html#a2">EXPORT</a> <a class="code" href="group__integers.html#ga6">uint</a> <a class="code" href="classIMA__ADPCM.html#a2">IMA_ADPCM::Decode</a>(int16* dst, <span class="keyword">const</span> uint8* src, <span class="keywordtype">int</span> srcOffset, uint srcSize)00198 {00199 <span class="comment">// use given bit offset</span>00200 src += srcOffset>>3;00201 00202 <span class="comment">// calculate pointers to iterate output buffer</span>00203 <a class="code" href="group__integers.html#ga4">int16</a>* out = dst;00204 <a class="code" href="group__integers.html#ga4">int16</a>* end = out+(srcSize>>2);00205 00206 <span class="keywordflow">while</span>(out<end)00207 {00208 <span class="comment">// get byte from src</span>00209 <a class="code" href="group__integers.html#ga6">uint</a> adpcm = *src; 00210 00211 <span class="comment">// pick which nibble holds a adpcm value...</span>00212 <span class="keywordflow">if</span>(srcOffset&4)00213 {00214 adpcm >>= 4; <span class="comment">// use high nibble of byte</span>00215 ++src; <span class="comment">// move on a byte for next sample</span>00216 }00217 00218 *out++ = <a class="code" href="classIMA__ADPCM.html#a2">Decode</a>(adpcm); <span class="comment">// decode value and store it</span>00219 00220 <span class="comment">// toggle which nibble in byte to write to next</span>00221 srcOffset ^= 4;00222 }00223 00224 <span class="comment">// return number of bytes written to dst</span>00225 <span class="keywordflow">return</span> (<a class="code" href="group__integers.html#ga6">uint</a>)out-(<a class="code" href="group__integers.html#ga6">uint</a>)dst;00226 }</pre></div><HR><ADDRESS STYLE="align: right;"><SMALL>Generated by <A HREF="http://www.doxygen.org/index.html"><IMG SRC="doxygen.png" ALT="doxygen" ALIGN="middle" BORDER="0"></A> 1.4.1</SMALL></ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -