TheAlgorithms-C/df/d27/alaw_8c.html
2021-10-25 19:34:27 +00:00

520 lines
40 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: audio/alaw.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('df/d27/alaw_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a> </div>
<div class="headertitle"><div class="title">alaw.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>A-law algorithm for encoding and decoding (16bit pcm &lt;=&gt; a-law). This is the implementation of <a href="https://en.wikipedia.org/wiki/G.711" target="_blank">G.711</a> in C.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;inttypes.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for alaw.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d0b/alaw_8c__incl.svg" width="270" height="112"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:a05b49c662c073f89e86804f7856622a0"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>&#160;&#160;&#160;((size_t)8)</td></tr>
<tr class="memdesc:a05b49c662c073f89e86804f7856622a0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Linear input code | Compressed code | Linear output code ---------------&mdash;+--------------&mdash;+----------------&mdash; s0000000abcdx | s000abcd | s0000000abcd1 s0000001abcdx | s001abcd | s0000001abcd1 s000001abcdxx | s010abcd | s000001abcd10 s00001abcdxxx | s011abcd | s00001abcd100 s0001abcdxxxx | s100abcd | s0001abcd1000 s001abcdxxxxx | s101abcd | s001abcd10000 s01abcdxxxxxx | s110abcd | s01abcd100000 s1abcdxxxxxxx | s111abcd | s1abcd1000000. <a href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">More...</a><br /></td></tr>
<tr class="separator:a05b49c662c073f89e86804f7856622a0"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a751d8e2c1975706e4d876316518c54c4"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d27/alaw_8c.html#a751d8e2c1975706e4d876316518c54c4">encode</a> (uint8_t *out, int16_t *in, size_t len)</td></tr>
<tr class="memdesc:a751d8e2c1975706e4d876316518c54c4"><td class="mdescLeft">&#160;</td><td class="mdescRight">16bit pcm to 8bit alaw <a href="../../df/d27/alaw_8c.html#a751d8e2c1975706e4d876316518c54c4">More...</a><br /></td></tr>
<tr class="separator:a751d8e2c1975706e4d876316518c54c4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a093ac72124e3c2268d1150676e63175d"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d27/alaw_8c.html#a093ac72124e3c2268d1150676e63175d">decode</a> (int16_t *out, uint8_t *in, size_t len)</td></tr>
<tr class="memdesc:a093ac72124e3c2268d1150676e63175d"><td class="mdescLeft">&#160;</td><td class="mdescRight">8bit alaw to 16bit pcm <a href="../../df/d27/alaw_8c.html#a093ac72124e3c2268d1150676e63175d">More...</a><br /></td></tr>
<tr class="separator:a093ac72124e3c2268d1150676e63175d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a840bf13d2e4d75d38fb7bf3a655152f5"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d27/alaw_8c.html#a840bf13d2e4d75d38fb7bf3a655152f5">test</a> (int16_t *pcm, uint8_t *coded, int16_t *decoded, size_t len)</td></tr>
<tr class="memdesc:a840bf13d2e4d75d38fb7bf3a655152f5"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <a href="../../df/d27/alaw_8c.html#a840bf13d2e4d75d38fb7bf3a655152f5">More...</a><br /></td></tr>
<tr class="separator:a840bf13d2e4d75d38fb7bf3a655152f5"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0ddf1224851353fc92bfbff6f499fa97"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d27/alaw_8c.html#a0ddf1224851353fc92bfbff6f499fa97">main</a> (int argc, char *argv[])</td></tr>
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../df/d27/alaw_8c.html#a0ddf1224851353fc92bfbff6f499fa97">More...</a><br /></td></tr>
<tr class="separator:a0ddf1224851353fc92bfbff6f499fa97"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:a538629d6e89d6bd943c5853b8aad4546"><td class="memItemLeft" align="right" valign="top"><a id="a538629d6e89d6bd943c5853b8aad4546" name="a538629d6e89d6bd943c5853b8aad4546"></a>
int16_t&#160;</td><td class="memItemRight" valign="bottom"><b>pcm</b> [<a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>] = {1000, -1000, 1234, 3200, -1314, 0, 32767, -32768}</td></tr>
<tr class="separator:a538629d6e89d6bd943c5853b8aad4546"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a462175be15495bb589d91d3b9bfca266"><td class="memItemLeft" align="right" valign="top"><a id="a462175be15495bb589d91d3b9bfca266" name="a462175be15495bb589d91d3b9bfca266"></a>
uint8_t&#160;</td><td class="memItemRight" valign="bottom"><b>r_coded</b> [<a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>] = {250, 122, 230, 156, 97, 213, 170, 42}</td></tr>
<tr class="separator:a462175be15495bb589d91d3b9bfca266"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2df278be294207b289568c46f14d18b3"><td class="memItemLeft" align="right" valign="top"><a id="a2df278be294207b289568c46f14d18b3" name="a2df278be294207b289568c46f14d18b3"></a>
int16_t&#160;</td><td class="memItemRight" valign="bottom"><b>r_decoded</b> [<a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>] = {1008, -1008, 1248, 3264, -1312, 8, 32256, -32256}</td></tr>
<tr class="separator:a2df278be294207b289568c46f14d18b3"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >A-law algorithm for encoding and decoding (16bit pcm &lt;=&gt; a-law). This is the implementation of <a href="https://en.wikipedia.org/wiki/G.711" target="_blank">G.711</a> in C. </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/HiSunzhenliang" target="_blank">sunzhenliang</a> </dd></dl>
</div><h2 class="groupheader">Macro Definition Documentation</h2>
<a id="a05b49c662c073f89e86804f7856622a0" name="a05b49c662c073f89e86804f7856622a0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a05b49c662c073f89e86804f7856622a0">&#9670;&nbsp;</a></span>LEN</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define LEN&#160;&#160;&#160;((size_t)8)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Linear input code | Compressed code | Linear output code ---------------&mdash;+--------------&mdash;+----------------&mdash; s0000000abcdx | s000abcd | s0000000abcd1 s0000001abcdx | s001abcd | s0000001abcd1 s000001abcdxx | s010abcd | s000001abcd10 s00001abcdxxx | s011abcd | s00001abcd100 s0001abcdxxxx | s100abcd | s0001abcd1000 s001abcdxxxxx | s101abcd | s001abcd10000 s01abcdxxxxxx | s110abcd | s01abcd100000 s1abcdxxxxxxx | s111abcd | s1abcd1000000. </p>
<p >Compressed code: (s | eee | abcd) for assert for appropriate size int types for IO operations </p>
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="a093ac72124e3c2268d1150676e63175d" name="a093ac72124e3c2268d1150676e63175d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a093ac72124e3c2268d1150676e63175d">&#9670;&nbsp;</a></span>decode()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void decode </td>
<td>(</td>
<td class="paramtype">int16_t *&#160;</td>
<td class="paramname"><em>out</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint8_t *&#160;</td>
<td class="paramname"><em>in</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>len</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>8bit alaw to 16bit pcm </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">out</td><td>signed 16bit pcm array </td></tr>
<tr><td class="paramname">in</td><td>unsigned 8bit alaw array </td></tr>
<tr><td class="paramname">len</td><td>length of alaw array </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span>{</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span> uint8_t alaw = 0;</div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span> int32_t pcm = 0;</div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> int32_t sign = 0;</div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> int32_t eee = 0;</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; len; i++)</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> {</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> alaw = *in++;</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> </div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> <span class="comment">/* Re-toggle toggled bits */</span></div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> alaw ^= 0xD5;</div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> </div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> <span class="comment">/* Get sign bit */</span></div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> sign = alaw &amp; 0x80;</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> </div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> <span class="comment">/* Get eee bits */</span></div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> eee = (alaw &amp; 0x70) &gt;&gt; 4;</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> </div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> <span class="comment">/* Get abcd bits and add 1/2 quantization step */</span></div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> pcm = (alaw &amp; 0x0f) &lt;&lt; 4 | 8;</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> </div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> <span class="comment">/* If quantization level &gt; 0, there need `1` bit before abcd bits */</span></div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> pcm += eee ? 0x100 : 0x0;</div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span> </div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> <span class="comment">/* Left shift according quantization level */</span></div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> pcm &lt;&lt;= eee &gt; 1 ? (eee - 1) : 0;</div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> </div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> <span class="comment">/* Use the right sign */</span></div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> *out++ = sign ? -pcm : pcm;</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> }</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a751d8e2c1975706e4d876316518c54c4" name="a751d8e2c1975706e4d876316518c54c4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a751d8e2c1975706e4d876316518c54c4">&#9670;&nbsp;</a></span>encode()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void encode </td>
<td>(</td>
<td class="paramtype">uint8_t *&#160;</td>
<td class="paramname"><em>out</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int16_t *&#160;</td>
<td class="paramname"><em>in</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>len</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>16bit pcm to 8bit alaw </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">out</td><td>unsigned 8bit alaw array </td></tr>
<tr><td class="paramname">in</td><td>signed 16bit pcm array </td></tr>
<tr><td class="paramname">len</td><td>length of pcm array </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span>{</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> uint8_t alaw = 0;</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> int16_t pcm = 0;</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> int32_t sign = 0;</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> int32_t abcd = 0;</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> int32_t eee = 0;</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> int32_t mask = 0;</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; len; i++)</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> {</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> pcm = *in++;</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span> <span class="comment">/* 0-7 kinds of quantization level from the table above */</span></div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> eee = 7;</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span> mask = 0x4000; <span class="comment">/* 0x4000: &#39;0b0100 0000 0000 0000&#39; */</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span> </div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> <span class="comment">/* Get sign bit */</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> sign = (pcm &amp; 0x8000) &gt;&gt; 8;</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span> </div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> <span class="comment">/* Turn negative pcm to positive */</span></div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span> <span class="comment">/* The absolute value of a negative number may be larger than the size</span></div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span><span class="comment"> * of the corresponding positive number, so here needs `-pcm -1` after</span></div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span><span class="comment"> * taking the opposite number. */</span></div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> pcm = sign ? (-pcm - 1) : pcm;</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> </div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="comment">/* Get eee and abcd bit */</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> <span class="comment">/* Use mask to locate the first `1` bit and quantization level at the</span></div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span><span class="comment"> * same time */</span></div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> <span class="keywordflow">while</span> ((pcm &amp; mask) == 0 &amp;&amp; eee &gt; 0)</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> {</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> eee--;</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span> mask &gt;&gt;= 1;</div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> }</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span> </div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span> <span class="comment">/* The location of abcd bits is related with quantization level. Check</span></div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span><span class="comment"> * the table above to determine how many bits to `&gt;&gt;` to get abcd */</span></div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> abcd = (pcm &gt;&gt; (eee ? (eee + 3) : 4)) &amp; 0x0f;</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> </div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span> <span class="comment">/* Put the quantization level number at right bit location to get eee</span></div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span><span class="comment"> * bits */</span></div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span> eee &lt;&lt;= 4;</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span> </div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> <span class="comment">/* Splice results */</span></div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> alaw = (sign | eee | abcd);</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> </div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> <span class="comment">/* The standard specifies that all resulting even bits (LSB</span></div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span><span class="comment"> * is even) are inverted before the octet is transmitted. This is to</span></div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span><span class="comment"> * provide plenty of 0/1 transitions to facilitate the clock recovery</span></div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span><span class="comment"> * process in the PCM receivers. Thus, a silent A-law encoded PCM</span></div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span><span class="comment"> * channel has the 8 bit samples coded 0xD5 instead of 0x80 in the</span></div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span><span class="comment"> * octets. (Reference from wiki above) */</span></div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span> *out++ = alaw ^ 0xD5;</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span> }</div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a0ddf1224851353fc92bfbff6f499fa97" name="a0ddf1224851353fc92bfbff6f499fa97"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0ddf1224851353fc92bfbff6f499fa97">&#9670;&nbsp;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>argv</em>[]&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">argc</td><td>commandline argument count (ignored) </td></tr>
<tr><td class="paramname">argv</td><td>commandline array of arguments (ignored) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span>{</div>
<div class="line"><a id="l00177" name="l00177"></a><span class="lineno"> 177</span> <span class="comment">/* output alaw encoded by encode() */</span></div>
<div class="line"><a id="l00178" name="l00178"></a><span class="lineno"> 178</span> uint8_t coded[<a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>];</div>
<div class="line"><a id="l00179" name="l00179"></a><span class="lineno"> 179</span> </div>
<div class="line"><a id="l00180" name="l00180"></a><span class="lineno"> 180</span> <span class="comment">/* output pcm decoded by decode() from coded[LEN] */</span></div>
<div class="line"><a id="l00181" name="l00181"></a><span class="lineno"> 181</span> int16_t decoded[<a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>];</div>
<div class="line"><a id="l00182" name="l00182"></a><span class="lineno"> 182</span> </div>
<div class="line"><a id="l00183" name="l00183"></a><span class="lineno"> 183</span> <a class="code hl_function" href="../../df/d27/alaw_8c.html#a840bf13d2e4d75d38fb7bf3a655152f5">test</a>(pcm, coded, decoded, <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>); <span class="comment">// run self-test implementations</span></div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> </div>
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> <span class="comment">/* print test pcm inputs */</span></div>
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span> printf(<span class="stringliteral">&quot;inputs: &quot;</span>);</div>
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++)</div>
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span> {</div>
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, pcm[i]);</div>
<div class="line"><a id="l00190" name="l00190"></a><span class="lineno"> 190</span> }</div>
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span> </div>
<div class="line"><a id="l00193" name="l00193"></a><span class="lineno"> 193</span> <span class="comment">/* print encoded alaw */</span></div>
<div class="line"><a id="l00194" name="l00194"></a><span class="lineno"> 194</span> printf(<span class="stringliteral">&quot;encode: &quot;</span>);</div>
<div class="line"><a id="l00195" name="l00195"></a><span class="lineno"> 195</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++)</div>
<div class="line"><a id="l00196" name="l00196"></a><span class="lineno"> 196</span> {</div>
<div class="line"><a id="l00197" name="l00197"></a><span class="lineno"> 197</span> printf(<span class="stringliteral">&quot;%u &quot;</span>, coded[i]);</div>
<div class="line"><a id="l00198" name="l00198"></a><span class="lineno"> 198</span> }</div>
<div class="line"><a id="l00199" name="l00199"></a><span class="lineno"> 199</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><a id="l00200" name="l00200"></a><span class="lineno"> 200</span> </div>
<div class="line"><a id="l00201" name="l00201"></a><span class="lineno"> 201</span> <span class="comment">/* print decoded pcm */</span></div>
<div class="line"><a id="l00202" name="l00202"></a><span class="lineno"> 202</span> printf(<span class="stringliteral">&quot;decode: &quot;</span>);</div>
<div class="line"><a id="l00203" name="l00203"></a><span class="lineno"> 203</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++)</div>
<div class="line"><a id="l00204" name="l00204"></a><span class="lineno"> 204</span> {</div>
<div class="line"><a id="l00205" name="l00205"></a><span class="lineno"> 205</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, decoded[i]);</div>
<div class="line"><a id="l00206" name="l00206"></a><span class="lineno"> 206</span> }</div>
<div class="line"><a id="l00207" name="l00207"></a><span class="lineno"> 207</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><a id="l00208" name="l00208"></a><span class="lineno"> 208</span> </div>
<div class="line"><a id="l00209" name="l00209"></a><span class="lineno"> 209</span> <span class="comment">/* It can be seen that the encoded alaw is smaller than the input PCM, so</span></div>
<div class="line"><a id="l00210" name="l00210"></a><span class="lineno"> 210</span><span class="comment"> * the purpose of compression is achieved. And the decoded PCM is almost the</span></div>
<div class="line"><a id="l00211" name="l00211"></a><span class="lineno"> 211</span><span class="comment"> * same as the original input PCM, which verifies the correctness of the</span></div>
<div class="line"><a id="l00212" name="l00212"></a><span class="lineno"> 212</span><span class="comment"> * decoding. The reason why it is not exactly the same is that there is</span></div>
<div class="line"><a id="l00213" name="l00213"></a><span class="lineno"> 213</span><span class="comment"> * precision loss during encode / decode. */</span></div>
<div class="line"><a id="l00214" name="l00214"></a><span class="lineno"> 214</span> </div>
<div class="line"><a id="l00215" name="l00215"></a><span class="lineno"> 215</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00216" name="l00216"></a><span class="lineno"> 216</span>}</div>
<div class="ttc" id="aalaw_8c_html_a05b49c662c073f89e86804f7856622a0"><div class="ttname"><a href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a></div><div class="ttdeci">#define LEN</div><div class="ttdoc">Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...</div><div class="ttdef"><b>Definition:</b> alaw.c:28</div></div>
<div class="ttc" id="aalaw_8c_html_a840bf13d2e4d75d38fb7bf3a655152f5"><div class="ttname"><a href="../../df/d27/alaw_8c.html#a840bf13d2e4d75d38fb7bf3a655152f5">test</a></div><div class="ttdeci">static void test(int16_t *pcm, uint8_t *coded, int16_t *decoded, size_t len)</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition:</b> alaw.c:148</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../df/d27/alaw_8c_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg" width="154" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a840bf13d2e4d75d38fb7bf3a655152f5" name="a840bf13d2e4d75d38fb7bf3a655152f5"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a840bf13d2e4d75d38fb7bf3a655152f5">&#9670;&nbsp;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramtype">int16_t *&#160;</td>
<td class="paramname"><em>pcm</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint8_t *&#160;</td>
<td class="paramname"><em>coded</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int16_t *&#160;</td>
<td class="paramname"><em>decoded</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>len</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementations. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">pcm</td><td>signed 16bit pcm array </td></tr>
<tr><td class="paramname">coded</td><td>unsigned 8bit alaw array </td></tr>
<tr><td class="paramname">decoded</td><td>signed 16bit pcm array </td></tr>
<tr><td class="paramname">len</td><td>length of test array </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span>{</div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> <span class="comment">/* run encode */</span></div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> <a class="code hl_function" href="../../df/d27/alaw_8c.html#a751d8e2c1975706e4d876316518c54c4">encode</a>(coded, pcm, len);</div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> </div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> <span class="comment">/* check encode result */</span></div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; len; i++)</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> {</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> assert(coded[i] == r_coded[i]);</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> }</div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> </div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> <span class="comment">/* run decode */</span></div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> <a class="code hl_function" href="../../df/d27/alaw_8c.html#a093ac72124e3c2268d1150676e63175d">decode</a>(decoded, coded, len);</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> </div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> <span class="comment">/* check decode result */</span></div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; len; i++)</div>
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span> {</div>
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span> assert(decoded[i] == r_decoded[i]);</div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> }</div>
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span>}</div>
<div class="ttc" id="aalaw_8c_html_a093ac72124e3c2268d1150676e63175d"><div class="ttname"><a href="../../df/d27/alaw_8c.html#a093ac72124e3c2268d1150676e63175d">decode</a></div><div class="ttdeci">void decode(int16_t *out, uint8_t *in, size_t len)</div><div class="ttdoc">8bit alaw to 16bit pcm</div><div class="ttdef"><b>Definition:</b> alaw.c:107</div></div>
<div class="ttc" id="aalaw_8c_html_a751d8e2c1975706e4d876316518c54c4"><div class="ttname"><a href="../../df/d27/alaw_8c.html#a751d8e2c1975706e4d876316518c54c4">encode</a></div><div class="ttdeci">void encode(uint8_t *out, int16_t *in, size_t len)</div><div class="ttdoc">16bit pcm to 8bit alaw</div><div class="ttdef"><b>Definition:</b> alaw.c:46</div></div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_a5f32cf82ad289a7dac212f63f40f55b.html">audio</a></li><li class="navelem"><a class="el" href="../../df/d27/alaw_8c.html">alaw.c</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2 </li>
</ul>
</div>
</body>
</html>