g726.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227
  1. /* v2.0 24.Jan.2000
  2. =============================================================================
  3. U U GGG SSSS TTTTT
  4. U U G S T
  5. U U G GG SSSS T
  6. U U G G S T
  7. UUU GG SSS T
  8. ========================================
  9. ITU-T - USER'S GROUP ON SOFTWARE TOOLS
  10. ========================================
  11. =============================================================
  12. COPYRIGHT NOTE: This source code, and all of its derivations,
  13. is subject to the "ITU-T General Public License". Please have
  14. it read in the distribution disk, or in the ITU-T
  15. Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO
  16. CODING STANDARDS".
  17. =============================================================
  18. MODULE: G726.C ADPCM AT 40, 32, 24, AND 16 KBIT/S MODULE
  19. ORIGINAL BY:
  20. Jose' Sindi Yamamoto (Fortran version of the G.721)
  21. <tdsindi@venus.cpqd.ansp.br>
  22. Simao Ferraz de Campos Neto (C translation, adaptation&testing of the G.721)
  23. <simao@venus.cpqd.ansp.br>
  24. Fernando Tofolli Queiroz (Extension of the G.721 to the other rates=>G.726)
  25. <tofolli@venus.cpqd.ansp.br>
  26. Simao Ferraz de Campos Neto (Adaptation and testing of the G.726)
  27. <simao@venus.cpqd.ansp.br>
  28. HISTORY:
  29. 28.Feb.1994 v1.0c Version 1.0 in C, by translating Fortran to C (f2c)
  30. 24.Jan.2000 v2.0 Corrected bug G726_compress() that caused incorrect
  31. processing of test vector ri40fa. Corrected code
  32. provided by Jayesh Patel <jayesh@dspse.com>.
  33. Verified by <simao.campos@labs.comsat.com>
  34. FUNCTIONS:
  35. Public:
  36. G726_encode ..... G726 encoder function;
  37. G726_decode ..... G726 decoder function;
  38. Private:
  39. G726_accum ...... addition of predictor outputs to form the partial
  40. signal estimate (from the sixth order predictor) and
  41. the signal estimate.
  42. G726_adda ....... addition of scale factor to logarithmic version of
  43. quantized difference signal.
  44. G726_addb ....... addition of quantized difference signal and signal
  45. estimate to form reconstructed signal.
  46. G726_addc....... obtain sign of addition of quantized difference signal
  47. and partial signal estimate.
  48. G726_antilog .... convert quantized difference signal from the
  49. logarithmic to the linear domain.
  50. G726_compress ... convert from uniform pcm to either a-law or u-law pcm.
  51. G726_delaya ..... memory block.
  52. G726_delayb ..... memory block.
  53. G726_delayc ..... memory block.
  54. G726_delayd ..... memory block.
  55. G726_expand ..... convert either a-law (law=1) or u-law (law=0) to
  56. uniform pcm.
  57. G726_filta ...... update of short term average of f(i).
  58. G726_filtb ...... update of long term average of f(i).
  59. G726_filtc ...... low pass filter of speed control parameter.
  60. G726_filtd ...... update of fast quantizer scale factor.
  61. G726_filte ...... update of slow quantizer scale factor.
  62. G726_floata ..... convert 15-bit signed magnitude to floating point.
  63. G726_floatb ..... convert 16-bit two's complement to floating point.
  64. G726_fmult ...... multiply predictor coefficients with corresponding
  65. quantized difference signal or reconstructed signal.
  66. Multiplication is done in floating point format.
  67. G726_functf ..... map quantizer output into the f(i) function.
  68. G726_functw ..... map quantizer output into logarithmic version of scale
  69. factor multiplier.
  70. G726_lima ....... limit speed control parameter.
  71. G726_limb ....... limit quantizer scale factor.
  72. G726_limc ....... limits on a2 coefficient of second order predictor.
  73. G726_limd ....... limits on a1 coefficient of second order predictor.
  74. G726_log ........ convert difference signal from linear to the
  75. logarithmic domain.
  76. G726_mix ........ form linear combination of fast and slow quantizer
  77. scale factors.
  78. G726_quan ....... quantize difference signal in logarithmic domain.
  79. G726_reconst .... reconstruction of quantized difference signal in the
  80. logarithmic domain.
  81. G726_subta ...... compute difference signal by subtracting signal
  82. estimate from input signal (or quantized reconstructed
  83. signal in decoder).
  84. G726_subtb ...... scale logarithmic version of difference signal by
  85. subtracting scale factor.
  86. G726_subtc ...... compute magnitude of the difference of short and
  87. long-term function of quantizer output sequence and
  88. then perform threshold comparison for quantizing speed
  89. control parameter.
  90. G726_sync ....... re-encode output pcm sample in decoder for synchronous
  91. tandem coding.
  92. G726_tone ....... partial band signal detection.
  93. G726_trans ...... transition detector.
  94. G726_triga ...... speed control trigger block.
  95. G726_trigb ...... predictor trigger block.
  96. G726_upa1 ....... update a1 coefficient of second order predictor.
  97. G726_upa2 ....... update a2 coefficient of second order predictor.
  98. G726_upb ........ update for coefficients of sixth order predictor.
  99. G726_xor ........ one bit "exclusive or" of sign of difference signal
  100. and sign of delayed difference signal.
  101. =============================================================================
  102. */
  103. /*
  104. * .................. INCLUDES ..................
  105. */
  106. #include "g726.h"
  107. /*
  108. * .................. FUNCTIONS ..................
  109. */
  110. /*
  111. ----------------------------------------------------------------------------
  112. void G726_encode (short *inp_buf, short *out_buf, long smpno,
  113. ~~~~~~~~~~~~~~~~ char *law, short rate, short r, G726_state *state);
  114. Description:
  115. ~~~~~~~~~~~~
  116. Simulation of the ITU-T G.726 ADPCM encoder. Takes the A or mu
  117. law input array of shorts `inp_buf' (16 bit, right- justified,
  118. without sign extension) of length `smpno', and saves the
  119. encoded samples in the array of shorts `out_buf', with the
  120. same number of samples and right-justified.
  121. The state variables are saved in the structure `state', and the
  122. reset can be stablished by making r equal to 1. The law is A if
  123. `law'=='1', and mu law if `law'=='0'.
  124. Return value:
  125. ~~~~~~~~~~~~~
  126. None.
  127. Prototype: in file g726.h
  128. ~~~~~~~~~~
  129. History:
  130. ~~~~~~~~
  131. 31.Jan.91 v1.0f Version 1.0 in Fortran
  132. <tdsindi@venus.cpqd.ansp.br>
  133. 05.Feb.92 v1.0c Version 1.0 in C, by translating Fortran to C (f2c)
  134. <tdsimao@venus.cpqd.ansp.br>
  135. ----------------------------------------------------------------------------
  136. */
  137. void G726_encode (short *inp_buf, short *out_buf, long smpno, char *law, short rate, short r, G726_state * state) {
  138. short s;
  139. short d, i;
  140. short y;
  141. short sigpk;
  142. short sr, tr;
  143. short yu;
  144. short al, fi, dl, ap, dq, ds, se, ax, td, sl, wi;
  145. short u1, u2, u3, u4, u5, u6;
  146. short a1, a2, b1, b2, b3, b4, b5, b6;
  147. short dqln;
  148. short a1p, a2p, a1t, a2t, b1p, b2p, b3p, b4p, b5p, b6p, dq6, pk2, sr2, wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6;
  149. short dml, dln, app, dql, dms;
  150. short dqs, tdp;
  151. short sez;
  152. short yut;
  153. long yl;
  154. long j;
  155. /* Invert even bits if A law */
  156. if (*law == '1') {
  157. for (j = 0; j < smpno; j++)
  158. inp_buf[j] ^= 85;
  159. }
  160. /* Process all desired samples in inp_buf to out_buf; The comments about general blocks are given as in G.726, and refer to: 4.1.1 Input PCM format conversion and difference signal computation 4.1.2 Adaptive quantizer 4.1.3 Inverse adaptive quantizer 4.1.4 Quantizer scale factor adaptation 4.1.5 Adaptation speed control 4.1.6 Adaptive predictor and reconstructed signal calculator 4.1.7 Tone and transition detector 4.1.8 (Only in the decoder) */
  161. for (j = 0; j < smpno; j++, r = 0) {
  162. s = inp_buf[j];
  163. /* Process `known-state' part of 4.2.6 */
  164. G726_delayd (&r, &state->sr1, &sr2);
  165. G726_delayd (&r, &state->sr0, &state->sr1);
  166. G726_delaya (&r, &state->a2r, &a2);
  167. G726_delaya (&r, &state->a1r, &a1);
  168. G726_fmult (&a2, &sr2, &wa2);
  169. G726_fmult (&a1, &state->sr1, &wa1);
  170. G726_delayd (&r, &state->dq5, &dq6);
  171. G726_delayd (&r, &state->dq4, &state->dq5);
  172. G726_delayd (&r, &state->dq3, &state->dq4);
  173. G726_delayd (&r, &state->dq2, &state->dq3);
  174. G726_delayd (&r, &state->dq1, &state->dq2);
  175. G726_delayd (&r, &state->dq0, &state->dq1);
  176. G726_delaya (&r, &state->b1r, &b1);
  177. G726_delaya (&r, &state->b2r, &b2);
  178. G726_delaya (&r, &state->b3r, &b3);
  179. G726_delaya (&r, &state->b4r, &b4);
  180. G726_delaya (&r, &state->b5r, &b5);
  181. G726_delaya (&r, &state->b6r, &b6);
  182. G726_fmult (&b1, &state->dq1, &wb1);
  183. G726_fmult (&b2, &state->dq2, &wb2);
  184. G726_fmult (&b3, &state->dq3, &wb3);
  185. G726_fmult (&b4, &state->dq4, &wb4);
  186. G726_fmult (&b5, &state->dq5, &wb5);
  187. G726_fmult (&b6, &dq6, &wb6);
  188. G726_accum (&wa1, &wa2, &wb1, &wb2, &wb3, &wb4, &wb5, &wb6, &se, &sez);
  189. /* Process 4.2.1 */
  190. G726_expand (&s, law, &sl);
  191. G726_subta (&sl, &se, &d);
  192. /* Process delays and `know-state' part of 4.2.5 */
  193. G726_delaya (&r, &state->dmsp, &dms);
  194. G726_delaya (&r, &state->dmlp, &dml);
  195. G726_delaya (&r, &state->apr, &ap);
  196. G726_lima (&ap, &al);
  197. /* Process `know-state' parts of 4.2.4 */
  198. G726_delayb (&r, &state->yup, &yu);
  199. G726_delayc (&r, &state->ylp, &yl);
  200. G726_mix (&al, &yu, &yl, &y);
  201. /* Process 4.2.2 */
  202. G726_log (&d, &dl, &ds);
  203. G726_subtb (&dl, &y, &dln);
  204. G726_quan (rate, &dln, &ds, &i);
  205. /* Save ADPCM quantized sample into output buffer */
  206. out_buf[j] = i;
  207. /* Process 4.2.3 */
  208. G726_reconst (rate, &i, &dqln, &dqs);
  209. G726_adda (&dqln, &y, &dql);
  210. G726_antilog (&dql, &dqs, &dq);
  211. /* Part of 4.2.5 */
  212. G726_functf (rate, &i, &fi);
  213. G726_filta (&fi, &dms, &state->dmsp);
  214. G726_filtb (&fi, &dml, &state->dmlp);
  215. /* Remaining part of 4.2.4 */
  216. G726_functw (rate, &i, &wi);
  217. G726_filtd (&wi, &y, &yut);
  218. G726_limb (&yut, &state->yup);
  219. G726_filte (&state->yup, &yl, &state->ylp);
  220. /* Process `known-state' part of 4.2.7 */
  221. G726_delaya (&r, &state->tdr, &td);
  222. G726_trans (&td, &yl, &dq, &tr);
  223. /* More `known-state' parts of 4.2.6: update of `pk's */
  224. G726_delaya (&r, &state->pk1, &pk2);
  225. G726_delaya (&r, &state->pk0, &state->pk1);
  226. G726_addc (&dq, &sez, &state->pk0, &sigpk);
  227. /* 4.2.6: find sr0 */
  228. G726_addb (&dq, &se, &sr);
  229. G726_floatb (&sr, &state->sr0);
  230. /* 4.2.6: find dq0 */
  231. G726_floata (&dq, &state->dq0);
  232. /* 4.2.6: prepar a2(r) */
  233. G726_upa2 (&state->pk0, &state->pk1, &pk2, &a2, &a1, &sigpk, &a2t);
  234. G726_limc (&a2t, &a2p);
  235. G726_trigb (&tr, &a2p, &state->a2r);
  236. /* 4.2.6: prepar a1(r) */
  237. G726_upa1 (&state->pk0, &state->pk1, &a1, &sigpk, &a1t);
  238. G726_limd (&a1t, &a2p, &a1p);
  239. G726_trigb (&tr, &a1p, &state->a1r);
  240. /* Remaining of 4.2.7 */
  241. G726_tone (&a2p, &tdp);
  242. G726_trigb (&tr, &tdp, &state->tdr);
  243. /* Remaining of 4.2.5 */
  244. G726_subtc (&state->dmsp, &state->dmlp, &tdp, &y, &ax);
  245. G726_filtc (&ax, &ap, &app);
  246. G726_triga (&tr, &app, &state->apr);
  247. /* Remaining of 4.2.6: update of all `b's */
  248. G726_xor (&state->dq1, &dq, &u1); /* Here, b1 */
  249. G726_upb (rate, &u1, &b1, &dq, &b1p);
  250. G726_trigb (&tr, &b1p, &state->b1r);
  251. G726_xor (&state->dq2, &dq, &u2); /* Here, b2 */
  252. G726_upb (rate, &u2, &b2, &dq, &b2p);
  253. G726_trigb (&tr, &b2p, &state->b2r);
  254. G726_xor (&state->dq3, &dq, &u3); /* Here, b3 */
  255. G726_upb (rate, &u3, &b3, &dq, &b3p);
  256. G726_trigb (&tr, &b3p, &state->b3r);
  257. G726_xor (&state->dq4, &dq, &u4); /* Here, b4 */
  258. G726_upb (rate, &u4, &b4, &dq, &b4p);
  259. G726_trigb (&tr, &b4p, &state->b4r);
  260. G726_xor (&state->dq5, &dq, &u5); /* Here, b5 */
  261. G726_upb (rate, &u5, &b5, &dq, &b5p);
  262. G726_trigb (&tr, &b5p, &state->b5r);
  263. G726_xor (&dq6, &dq, &u6); /* At last, b6 */
  264. G726_upb (rate, &u6, &b6, &dq, &b6p);
  265. G726_trigb (&tr, &b6p, &state->b6r);
  266. }
  267. }
  268. /* ........................ end of G726_encode() ....................... */
  269. /*
  270. ----------------------------------------------------------------------------
  271. void G726_decode (short *inp_buf, short *out_buf, long smpno,
  272. ~~~~~~~~~~~~~~~~ char *law, short rate, short r, G726_state *state);
  273. Description:
  274. ~~~~~~~~~~~~
  275. Simulation of the ITU-T G.726 ADPCM decoder. Takes the ADPCM
  276. input array of shorts `inp_buf' (16 bit, right- justified,
  277. without sign extension) of length `smpno', and saves the
  278. decoded samples (A or mu law) in the array of shorts
  279. `out_buf', with the same number of samples and
  280. right-justified.
  281. The state variables are saved in the structure `state', and the
  282. reset can be stablished by making r equal to 1. The law is A if
  283. `law'=='1', and mu law if `law'=='0'.
  284. Return value:
  285. ~~~~~~~~~~~~~
  286. None.
  287. Prototype: in file g726.h
  288. ~~~~~~~~~~
  289. History:
  290. ~~~~~~~~
  291. 31.Jan.91 v1.0f Version 1.0 in Fortran
  292. <tdsindi@venus.cpqd.ansp.br>
  293. 05.Feb.92 v1.0c Version 1.0 in C, by translating Fortran to C (f2c)
  294. <tdsimao@venus.cpqd.ansp.br>
  295. ----------------------------------------------------------------------------
  296. */
  297. void G726_decode (short *inp_buf, short *out_buf, long smpno, char *law, short rate, short r, G726_state * state) {
  298. short i;
  299. short y;
  300. short sigpk;
  301. short sr, tr;
  302. short sp, dlnx, dsx, sd, slx, dlx, dx; /* these are unique to the decoder */
  303. long yl;
  304. short yu;
  305. short al, fi, ap, dq, se, ax, td, wi;
  306. short u1, u2, u3, u4, u5, u6;
  307. short a1, a2, b1, b2, b3, b4, b5, b6;
  308. short dqln;
  309. short a1p, a2p, a1t, a2t, b1p, b2p, b3p, b4p, b5p, b6p, dq6, pk2, sr2, wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6;
  310. short dml, app, dql, dms;
  311. short dqs, tdp;
  312. short sez;
  313. short yut;
  314. long j;
  315. /* Process all desired samples in inp_buf to out_buf; The comments about general blocks are given as in G.726, and refer to: 4.1.1 Input PCM format conversion and difference signal computation 4.1.2 Adaptive quantizer 4.1.3 Inverse adaptive quantizer 4.1.4 Quantizer scale factor adaptation 4.1.5 Adaptation speed control 4.1.6 Adaptive predictor and reconstructed signal calculator 4.1.7 Tone and transition detector 4.1.8 Output PCM format conversion and synchronous coding adjustment */
  316. for (j = 0; j < smpno; j++, r = 0) {
  317. /* Process `known-state' part of 4.2.6 */
  318. G726_delayd (&r, &state->sr1, &sr2);
  319. G726_delayd (&r, &state->sr0, &state->sr1);
  320. G726_delaya (&r, &state->a2r, &a2);
  321. G726_delaya (&r, &state->a1r, &a1);
  322. G726_fmult (&a2, &sr2, &wa2);
  323. G726_fmult (&a1, &state->sr1, &wa1);
  324. G726_delayd (&r, &state->dq5, &dq6);
  325. G726_delayd (&r, &state->dq4, &state->dq5);
  326. G726_delayd (&r, &state->dq3, &state->dq4);
  327. G726_delayd (&r, &state->dq2, &state->dq3);
  328. G726_delayd (&r, &state->dq1, &state->dq2);
  329. G726_delayd (&r, &state->dq0, &state->dq1);
  330. G726_delaya (&r, &state->b1r, &b1);
  331. G726_delaya (&r, &state->b2r, &b2);
  332. G726_delaya (&r, &state->b3r, &b3);
  333. G726_delaya (&r, &state->b4r, &b4);
  334. G726_delaya (&r, &state->b5r, &b5);
  335. G726_delaya (&r, &state->b6r, &b6);
  336. G726_fmult (&b1, &state->dq1, &wb1);
  337. G726_fmult (&b2, &state->dq2, &wb2);
  338. G726_fmult (&b3, &state->dq3, &wb3);
  339. G726_fmult (&b4, &state->dq4, &wb4);
  340. G726_fmult (&b5, &state->dq5, &wb5);
  341. G726_fmult (&b6, &dq6, &wb6);
  342. G726_accum (&wa1, &wa2, &wb1, &wb2, &wb3, &wb4, &wb5, &wb6, &se, &sez);
  343. /* Process delays and `know-state' part of 4.2.5 */
  344. G726_delaya (&r, &state->dmsp, &dms);
  345. G726_delaya (&r, &state->dmlp, &dml);
  346. G726_delaya (&r, &state->apr, &ap);
  347. G726_lima (&ap, &al);
  348. /* Process `know-state' parts of 4.2.4 */
  349. G726_delayb (&r, &state->yup, &yu);
  350. G726_delayc (&r, &state->ylp, &yl);
  351. G726_mix (&al, &yu, &yl, &y);
  352. /* Retrieve ADPCM sample from input buffer */
  353. i = inp_buf[j];
  354. /* Process 4.2.3 */
  355. G726_reconst (rate, &i, &dqln, &dqs);
  356. G726_adda (&dqln, &y, &dql);
  357. G726_antilog (&dql, &dqs, &dq);
  358. /* Process `known-state' part of 4.2.7 */
  359. G726_delaya (&r, &state->tdr, &td);
  360. G726_trans (&td, &yl, &dq, &tr);
  361. /* Part of 4.2.5 */
  362. G726_functf (rate, &i, &fi);
  363. G726_filta (&fi, &dms, &state->dmsp);
  364. G726_filtb (&fi, &dml, &state->dmlp);
  365. /* Remaining part of 4.2.4 */
  366. G726_functw (rate, &i, &wi);
  367. G726_filtd (&wi, &y, &yut);
  368. G726_limb (&yut, &state->yup);
  369. G726_filte (&state->yup, &yl, &state->ylp);
  370. /* More `known-state' parts of 4.2.6: update of `pk's */
  371. G726_delaya (&r, &state->pk1, &pk2);
  372. G726_delaya (&r, &state->pk0, &state->pk1);
  373. G726_addc (&dq, &sez, &state->pk0, &sigpk);
  374. /* 4.2.6: find sr0 */
  375. G726_addb (&dq, &se, &sr);
  376. G726_floatb (&sr, &state->sr0);
  377. /* 4.2.6: find dq0 */
  378. G726_floata (&dq, &state->dq0);
  379. /* Process 4.2.8 */
  380. G726_compress (&sr, law, &sp);
  381. G726_expand (&sp, law, &slx);
  382. G726_subta (&slx, &se, &dx);
  383. G726_log (&dx, &dlx, &dsx);
  384. G726_subtb (&dlx, &y, &dlnx);
  385. G726_sync (rate, &i, &sp, &dlnx, &dsx, law, &sd);
  386. /* Save output PCM word in output buffer */
  387. out_buf[j] = sd;
  388. /* 4.2.6: prepar a2(r) */
  389. G726_upa2 (&state->pk0, &state->pk1, &pk2, &a2, &a1, &sigpk, &a2t);
  390. G726_limc (&a2t, &a2p);
  391. G726_trigb (&tr, &a2p, &state->a2r);
  392. /* 4.2.6: prepar a1(r) */
  393. G726_upa1 (&state->pk0, &state->pk1, &a1, &sigpk, &a1t);
  394. G726_limd (&a1t, &a2p, &a1p);
  395. G726_trigb (&tr, &a1p, &state->a1r);
  396. /* Remaining of 4.2.7 */
  397. G726_tone (&a2p, &tdp);
  398. G726_trigb (&tr, &tdp, &state->tdr);
  399. /* Remaining of 4.2.5 */
  400. G726_subtc (&state->dmsp, &state->dmlp, &tdp, &y, &ax);
  401. G726_filtc (&ax, &ap, &app);
  402. G726_triga (&tr, &app, &state->apr);
  403. /* Remaining of 4.2.6: update of all `b's */
  404. G726_xor (&state->dq1, &dq, &u1); /* Here, b1 */
  405. G726_upb (rate, &u1, &b1, &dq, &b1p);
  406. G726_trigb (&tr, &b1p, &state->b1r);
  407. G726_xor (&state->dq2, &dq, &u2); /* Here, b2 */
  408. G726_upb (rate, &u2, &b2, &dq, &b2p);
  409. G726_trigb (&tr, &b2p, &state->b2r);
  410. G726_xor (&state->dq3, &dq, &u3); /* Here, b3 */
  411. G726_upb (rate, &u3, &b3, &dq, &b3p);
  412. G726_trigb (&tr, &b3p, &state->b3r);
  413. G726_xor (&state->dq4, &dq, &u4); /* Here, b4 */
  414. G726_upb (rate, &u4, &b4, &dq, &b4p);
  415. G726_trigb (&tr, &b4p, &state->b4r);
  416. G726_xor (&state->dq5, &dq, &u5); /* Here, b5 */
  417. G726_upb (rate, &u5, &b5, &dq, &b5p);
  418. G726_trigb (&tr, &b5p, &state->b5r);
  419. G726_xor (&dq6, &dq, &u6); /* At last, b6 */
  420. G726_upb (rate, &u6, &b6, &dq, &b6p);
  421. G726_trigb (&tr, &b6p, &state->b6r);
  422. }
  423. /* Invert even bits if A law */
  424. if (*law == '1') {
  425. for (j = 0; j < smpno; j++)
  426. out_buf[j] ^= 85;
  427. }
  428. }
  429. /* ...................... end of G726_decode() ...................... */
  430. /*
  431. ----------------------------------------------------------------------
  432. void G726_expand (short *s, char *law, short *sl);
  433. ~~~~~~~~~~~~~~~~
  434. Description:
  435. ~~~~~~~~~~~~
  436. Convert either a-law (law=1) or u-law (law=0) to uniform pcm.
  437. Inputs: s (sp in decoder), law
  438. Output: sl (slx in decoder)
  439. Return value: none.
  440. ~~~~~~~~~~~~~
  441. Prototype: in g726.h
  442. ~~~~~~~~~~
  443. History:
  444. ~~~~~~~~
  445. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  446. <tdsindi@venus.cpqd.ansp.br>
  447. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  448. <tdsimao@venus.cpqd.ansp.br>
  449. ----------------------------------------------------------------------
  450. */
  451. void G726_expand (short *s, char *law, short *sl) {
  452. long mant, iexp;
  453. short s1, ss, sig, ssm, ssq, sss;
  454. s1 = *s;
  455. if (*law == '1') {
  456. /* Invert sign bit */
  457. s1 ^= 128;
  458. if (s1 >= 128) {
  459. s1 += -128;
  460. sig = 4096;
  461. } else {
  462. sig = 0;
  463. }
  464. iexp = s1 / 16;
  465. mant = s1 - (iexp << 4);
  466. ss = (iexp == 0) ? ((mant << 1) + 1 + sig) : ((1 << (iexp - 1)) * ((mant << 1) + 33) + sig);
  467. sss = ss / 4096;
  468. ssm = ss & 4095;
  469. ssq = ssm << 1;
  470. } else {
  471. /* Invert sign bit */
  472. s1 ^= 128;
  473. if (s1 >= 128) {
  474. s1 += -128;
  475. s1 ^= 127;
  476. sig = 8192;
  477. } else {
  478. sig = 0;
  479. s1 ^= 127;
  480. }
  481. iexp = s1 / 16;
  482. mant = s1 - (iexp << 4);
  483. ss = (iexp == 0) ? ((mant << 1) + sig) : ((1 << iexp) * ((mant << 1) + 33) - 33 + sig);
  484. sss = ss / 8192;
  485. ssq = ss & 8191;
  486. }
  487. *sl = (sss == 0) ? ssq : ((16384 - ssq) & 16383);
  488. }
  489. /* ...................... end of G726_expand() ...................... */
  490. /*
  491. ----------------------------------------------------------------------
  492. void G726_subta (short *sl, short *se, short *d);
  493. ~~~~~~~~~~~~~~~
  494. Description:
  495. ~~~~~~~~~~~~
  496. Compute difference signal by subtracting signal estimate from
  497. input signal (or quantized reconstructed signal in decoder).
  498. Inputs: sl (slx in decoder), se
  499. Output: d (dx in decoder)
  500. Return value: none.
  501. ~~~~~~~~~~~~~
  502. Prototype: in g726.h
  503. ~~~~~~~~~~
  504. History:
  505. ~~~~~~~~
  506. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  507. <tdsindi@venus.cpqd.ansp.br>
  508. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  509. <tdsimao@venus.cpqd.ansp.br>
  510. ----------------------------------------------------------------------
  511. */
  512. void G726_subta (short *sl, short *se, short *d) {
  513. long se1;
  514. long sl1, sei, sli;
  515. short ses, sls;
  516. sls = (*sl >> 13);
  517. sl1 = *sl;
  518. se1 = *se;
  519. /* Sign extension */
  520. sli = (sls == 0) ? sl1 : (sl1 + 49152);
  521. ses = (*se >> 14);
  522. /* Sign extension */
  523. sei = (ses == 0) ? se1 : (se1 + 32768);
  524. /* 16 bit TC */
  525. *d = (short) ((sli + 65536 - sei) & 65535);
  526. }
  527. /* ......................... end of G726_subta() ......................... */
  528. /*
  529. --------------------------------------------------------------------------
  530. void G726_log (short *d, short *dl, short *ds);
  531. ~~~~~~~~~~~~~
  532. Description:
  533. ~~~~~~~~~~~~
  534. Convert difference signal from linear to the logarithmic domain.
  535. Prototype: in g726.h
  536. ~~~~~~~~~~
  537. History:
  538. ~~~~~~~~
  539. 31.Jan.91 v1.0f 1st release to UGST, in Fortran.
  540. <tdsindi@venus.cpqd.ansp.br>
  541. 13.Feb.92 v1.0c release of 1st version, in C.
  542. <tdsimao@venus.cpqd.ansp.br>
  543. --------------------------------------------------------------------------
  544. */
  545. void G726_log (short *d, short *dl, short *ds) {
  546. long mant;
  547. long d1;
  548. long dqm, exp_;
  549. *ds = (*d >> 15);
  550. d1 = *d;
  551. /* Convert from 2-complement to signed magnitude */
  552. dqm = (*ds) ? ((65536 - d1) & 32767) : d1;
  553. /* Compute exponent */
  554. if (dqm >= 16384)
  555. exp_ = 14;
  556. else if (dqm >= 8192)
  557. exp_ = 13;
  558. else if (dqm >= 4096)
  559. exp_ = 12;
  560. else if (dqm >= 2048)
  561. exp_ = 11;
  562. else if (dqm >= 1024)
  563. exp_ = 10;
  564. else if (dqm >= 512)
  565. exp_ = 9;
  566. else if (dqm >= 256)
  567. exp_ = 8;
  568. else if (dqm >= 128)
  569. exp_ = 7;
  570. else if (dqm >= 64)
  571. exp_ = 6;
  572. else if (dqm >= 32)
  573. exp_ = 5;
  574. else if (dqm >= 16)
  575. exp_ = 4;
  576. else if (dqm >= 8)
  577. exp_ = 3;
  578. else if (dqm >= 4)
  579. exp_ = 2;
  580. else if (dqm >= 2)
  581. exp_ = 1;
  582. else
  583. exp_ = 0;
  584. /* Compute approximation log2(1+x) = x */
  585. mant = ((dqm << 7) >> exp_) & 127;
  586. /* Combine mantissa and exponent (7 and 4) bits into a 11-bit word */
  587. *dl = (short) ((exp_ << 7) + mant);
  588. }
  589. /* ........................ end of G726_log() ....................... */
  590. /*
  591. ----------------------------------------------------------------------
  592. void G726_quan (short rate, short *dln, short *ds, short *i);
  593. ~~~~~~~~~~~~~~
  594. Description:
  595. ~~~~~~~~~~~~
  596. Quantize difference signal in logarithmic
  597. domain.
  598. Inputs: dln, ds, rate
  599. Output: i
  600. Return value: none.
  601. ~~~~~~~~~~~~~
  602. Prototype: in g726.h
  603. ~~~~~~~~~~
  604. History:
  605. ~~~~~~~~
  606. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  607. <tdsindi@venus.cpqd.ansp.br>
  608. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  609. <tdsimao@venus.cpqd.ansp.br>
  610. ----------------------------------------------------------------------
  611. */
  612. void G726_quan (short rate, short *dln, short *ds, short *i) {
  613. if (rate == 4) {
  614. if (*dln >= 3972)
  615. *i = 1;
  616. else if (*dln >= 2048)
  617. *i = 15;
  618. else if (*dln >= 400)
  619. *i = 7;
  620. else if (*dln >= 349)
  621. *i = 6;
  622. else if (*dln >= 300)
  623. *i = 5;
  624. else if (*dln >= 246)
  625. *i = 4;
  626. else if (*dln >= 178)
  627. *i = 3;
  628. else if (*dln >= 80)
  629. *i = 2;
  630. else
  631. *i = 1;
  632. /* Adjust for sign */
  633. if (*ds)
  634. *i = 15 - *i;
  635. if (*i == 0)
  636. *i = 15;
  637. }
  638. /* ......... end of 32 kbit part ........... */
  639. else if (rate == 3) {
  640. if (*dln >= 2048)
  641. *i = 7;
  642. else if (*dln >= 331)
  643. *i = 3;
  644. else if (*dln >= 218)
  645. *i = 2;
  646. else if (*dln >= 8)
  647. *i = 1;
  648. else if (*dln >= 0)
  649. *i = 7;
  650. /* Adjust for sign */
  651. if (*ds)
  652. *i = 7 - *i;
  653. if (*i == 0)
  654. *i = 7;
  655. }
  656. /* ......... end of 24 kbit part ........... */
  657. else if (rate == 2) {
  658. if (*dln >= 2048)
  659. *i = 0;
  660. else if (*dln >= 261)
  661. *i = 1;
  662. else
  663. *i = 0;
  664. /* Adjust for sign */
  665. if (*ds)
  666. *i = 3 - *i;
  667. }
  668. /* ......... end of 16 kbit part ........... */
  669. else {
  670. if (*dln >= 4080)
  671. *i = 2;
  672. else if (*dln >= 3974)
  673. *i = 1;
  674. else if (*dln >= 2048)
  675. *i = 31;
  676. else if (*dln >= 553)
  677. *i = 15;
  678. else if (*dln >= 528)
  679. *i = 14;
  680. else if (*dln >= 502)
  681. *i = 13;
  682. else if (*dln >= 475)
  683. *i = 12;
  684. else if (*dln >= 445)
  685. *i = 11;
  686. else if (*dln >= 413)
  687. *i = 10;
  688. else if (*dln >= 378)
  689. *i = 9;
  690. else if (*dln >= 339)
  691. *i = 8;
  692. else if (*dln >= 298)
  693. *i = 7;
  694. else if (*dln >= 250)
  695. *i = 6;
  696. else if (*dln >= 198)
  697. *i = 5;
  698. else if (*dln >= 139)
  699. *i = 4;
  700. else if (*dln >= 68)
  701. *i = 3;
  702. else if (*dln >= 0)
  703. *i = 2;
  704. if (*ds)
  705. *i = 31 - *i;
  706. if (*i == 0)
  707. *i = 31;
  708. } /* ......... end of 40 kbit part ........... */
  709. }
  710. /* ........................ end of G726_quan() ........................ */
  711. /*
  712. ----------------------------------------------------------------------
  713. void G726_subtb (short *dl, short *y, short *dln);
  714. ~~~~~~~~~~~~~~~
  715. Description:
  716. ~~~~~~~~~~~~
  717. Scale logarithmic version of difference signal by subtracting
  718. scale factor.
  719. Inputs: dl (dlx no decodificador), y
  720. Output: dln (dlnx no decodificador)
  721. Return value: none.
  722. ~~~~~~~~~~~~~
  723. Prototype: in g726.h
  724. ~~~~~~~~~~
  725. History:
  726. ~~~~~~~~
  727. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  728. <tdsindi@venus.cpqd.ansp.br>
  729. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  730. <tdsimao@venus.cpqd.ansp.br>
  731. ----------------------------------------------------------------------
  732. */
  733. void G726_subtb (short *dl, short *y, short *dln) {
  734. *dln = (*dl + 4096 - (*y >> 2)) & 4095;
  735. }
  736. /* ........................ end of G726_subtb() ........................ */
  737. /*
  738. ----------------------------------------------------------------------
  739. void G726_adda (short *dqln, short *y, short *dql);
  740. ~~~~~~~~~~~~~~
  741. Description:
  742. ~~~~~~~~~~~~
  743. Addition of scale factor to logarithmic version of quantized
  744. difference signal.
  745. Inputs: dqln, y
  746. Output: dql
  747. Return value: none.
  748. ~~~~~~~~~~~~~
  749. Prototype: in g726.h
  750. ~~~~~~~~~~
  751. History:
  752. ~~~~~~~~
  753. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  754. <tdsindi@venus.cpqd.ansp.br>
  755. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  756. <tdsimao@venus.cpqd.ansp.br>
  757. ----------------------------------------------------------------------
  758. */
  759. void G726_adda (dqln, y, dql)
  760. short *dqln, *y, *dql;
  761. {
  762. *dql = (*dqln + (*y >> 2)) & 4095;
  763. }
  764. /* ....................... end of G726_adda() ....................... */
  765. /*
  766. ----------------------------------------------------------------------
  767. void G726_antilog (short *dql, short *dqs, short *dq);
  768. ~~~~~~~~~~~~~~~~~
  769. Description:
  770. ~~~~~~~~~~~~
  771. Convert quantized difference signal from the logarithmic to
  772. the linear domain.
  773. Inputs: dql, dqs
  774. Output: dq
  775. Return value: none.
  776. ~~~~~~~~~~~~~
  777. Prototype: in g726.h
  778. ~~~~~~~~~~
  779. History:
  780. ~~~~~~~~
  781. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  782. <tdsindi@venus.cpqd.ansp.br>
  783. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  784. <tdsimao@venus.cpqd.ansp.br>
  785. ----------------------------------------------------------------------
  786. */
  787. void G726_antilog (dql, dqs, dq)
  788. short *dql, *dqs, *dq;
  789. {
  790. long dqmag;
  791. long ds, dmn, dex, dqt;
  792. /* Extract 4-bit exponent */
  793. ds = (*dql >> 11);
  794. dex = (*dql >> 7) & 15;
  795. /* Extract 7-bit mantissa */
  796. dmn = *dql & 127;
  797. dqt = dmn + 128;
  798. /* Convert mantissa to linear using the approx. 2**x = 1+x */
  799. dqmag = ds ? 0 : ((dqt << 7) >> (14 - dex));
  800. /* Attach sign bit to signed mag. word */
  801. *dq = (short) (*dqs << 15) + dqmag;
  802. }
  803. /* ..................... end of G726_antilog() ..................... */
  804. /*
  805. ----------------------------------------------------------------------
  806. void G726_reconst (short rate, short *i, short *dqln, short *dqs);
  807. ~~~~~~~~~~~~~~~~~
  808. Description:
  809. ~~~~~~~~~~~~
  810. Reconstruction of quantized difference signal in the
  811. logarithmic domain.
  812. Inputs: i, rate
  813. Outputs: dqln, dqs
  814. Return value: none.
  815. ~~~~~~~~~~~~~
  816. Prototype: in g726.h
  817. ~~~~~~~~~~
  818. History:
  819. ~~~~~~~~
  820. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  821. <tdsindi@venus.cpqd.ansp.br>
  822. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  823. <tdsimao@venus.cpqd.ansp.br>
  824. ----------------------------------------------------------------------
  825. */
  826. void G726_reconst (short rate, short *i, short *dqln, short *dqs) {
  827. if (rate == 4) {
  828. /* Initialized data */
  829. static short tab[16] = { 2048, 4, 135, 213, 273, 323, 373, 425,
  830. 425, 373, 323, 273, 213, 135, 4, 2048
  831. };
  832. /* Extract sign */
  833. *dqs = (*i >> 3);
  834. /* Table look-up */
  835. *dqln = tab[*i];
  836. }
  837. /* ............... end of 32 kbit part * ................. */
  838. else if (rate == 3) {
  839. /* Initialized data */
  840. static short tab[8] = { 2048, 135, 273, 373, 373, 273, 135, 2048 };
  841. *dqs = (*i >> 2);
  842. /* Table look-up */
  843. *dqln = tab[*i];
  844. }
  845. /* ............... end of 24 kbit part * ................. */
  846. else if (rate == 2) {
  847. /* Initialized data */
  848. static short tab[4] = { 116, 365, 365, 116 };
  849. *dqs = (*i >> 1);
  850. /* Table look-up */
  851. *dqln = tab[*i];
  852. } /* ............... end of 16 kbit part * ................. */
  853. else {
  854. /* Initialized data */
  855. static short tab[32] = { 2048, 4030, 28, 104, 169, 224, 274, 318, 358, 395, 429,
  856. 459, 488, 514, 539, 566, 566, 539, 514, 488, 459, 429, 395, 358, 318, 274, 224,
  857. 169, 104, 28, 4030, 2048
  858. };
  859. *dqs = (*i >> 4);
  860. /* Table look-up */
  861. *dqln = tab[*i];
  862. } /* ................ end of 40 kbit part ................... */
  863. }
  864. /* ....................... end of G726_reconst() ....................... */
  865. /*
  866. ----------------------------------------------------------------------
  867. void G726_delaya (short *r, short *x, short *y);
  868. ~~~~~~~~~~~~~~~~
  869. Description:
  870. ~~~~~~~~~~~~
  871. Memory block.
  872. Inputs: r, x
  873. Output: y
  874. Return value: none.
  875. ~~~~~~~~~~~~~
  876. Prototype: in g726.h
  877. ~~~~~~~~~~
  878. History:
  879. ~~~~~~~~
  880. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  881. <tdsindi@venus.cpqd.ansp.br>
  882. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  883. <tdsimao@venus.cpqd.ansp.br>
  884. ----------------------------------------------------------------------
  885. */
  886. void G726_delaya (short *r, short *x, short *y) {
  887. *y = (*r == 0) ? *x : 0;
  888. }
  889. /* ....................... end of G726_delaya() ....................... */
  890. /*
  891. ----------------------------------------------------------------------
  892. void G726_delayb (short *r, short *x, short *y);
  893. ~~~~~~~~~~~~~~~~
  894. Description:
  895. ~~~~~~~~~~~~
  896. Memory block.
  897. Inputs: r, x
  898. Output: y
  899. Return value: none.
  900. ~~~~~~~~~~~~~
  901. Prototype: in g726.h
  902. ~~~~~~~~~~
  903. History:
  904. ~~~~~~~~
  905. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  906. <tdsindi@venus.cpqd.ansp.br>
  907. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  908. <tdsimao@venus.cpqd.ansp.br>
  909. ----------------------------------------------------------------------
  910. */
  911. void G726_delayb (short *r, short *x, short *y) {
  912. *y = (*r == 0) ? *x : 544;
  913. }
  914. /* ....................... end of G726_delayb() ....................... */
  915. /*
  916. ----------------------------------------------------------------------
  917. void G726_delayc (short *r, long *x, long *y);
  918. ~~~~~~~~~~~~~~~~
  919. Description:
  920. ~~~~~~~~~~~~
  921. Memory block.
  922. Inputs: r, x
  923. Output: y
  924. Return value: none.
  925. ~~~~~~~~~~~~~
  926. Prototype: in g726.h
  927. ~~~~~~~~~~
  928. History:
  929. ~~~~~~~~
  930. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  931. <tdsindi@venus.cpqd.ansp.br>
  932. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  933. <tdsimao@venus.cpqd.ansp.br>
  934. ----------------------------------------------------------------------
  935. */
  936. void G726_delayc (short *r, long *x, long *y) {
  937. *y = (*r == 0) ? *x : 34816;
  938. }
  939. /* ....................... end of G726_delayc() ....................... */
  940. /*
  941. ----------------------------------------------------------------------
  942. void G726_delayd (short *r, short *x, short *y);
  943. ~~~~~~~~~~~~~~~~
  944. Description:
  945. ~~~~~~~~~~~~
  946. Memory block
  947. Inputs: r, x
  948. Output: y
  949. Return value: none.
  950. ~~~~~~~~~~~~~
  951. Prototype: in g726.h
  952. ~~~~~~~~~~
  953. History:
  954. ~~~~~~~~
  955. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  956. <tdsindi@venus.cpqd.ansp.br>
  957. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  958. <tdsimao@venus.cpqd.ansp.br>
  959. ----------------------------------------------------------------------
  960. */
  961. void G726_delayd (short *r, short *x, short *y) {
  962. *y = (*r == 0) ? *x : 32;
  963. }
  964. /* ....................... end of G726_delayd() ....................... */
  965. /*
  966. ----------------------------------------------------------------------
  967. void G726_filtd (short *wi, short *y, short *yut);
  968. ~~~~~~~~~~~~~~~
  969. Description:
  970. ~~~~~~~~~~~~
  971. Update of fast quantizer scale factor.
  972. Inputs: wi, y
  973. Output: yut
  974. Return value: none.
  975. ~~~~~~~~~~~~~
  976. Prototype: in g726.h
  977. ~~~~~~~~~~
  978. History:
  979. ~~~~~~~~
  980. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  981. <tdsindi@venus.cpqd.ansp.br>
  982. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  983. <tdsimao@venus.cpqd.ansp.br>
  984. ----------------------------------------------------------------------
  985. */
  986. void G726_filtd (short *wi, short *y, short *yut) {
  987. long difs, difsx;
  988. long y1;
  989. long wi1, dif;
  990. /* Compute difference */
  991. wi1 = *wi;
  992. y1 = *y;
  993. dif = ((wi1 << 5) + 131072 - y1) & 131071;
  994. difs = (dif >> 16);
  995. /* Time constant is 1/32; sign extension */
  996. difsx = (difs == 0) ? (dif >> 5) : ((dif >> 5) + 4096);
  997. *yut = (short) ((y1 + difsx) & 8191);
  998. }
  999. /* ....................... end of G726_filte() ....................... */
  1000. /*
  1001. ----------------------------------------------------------------------
  1002. void G726_filte (short *yup, long *yl, long *ylp);
  1003. ~~~~~~~~~~~~~~~
  1004. Description:
  1005. ~~~~~~~~~~~~
  1006. Update of slow quantizer scale factor.
  1007. Inputs: yup, yl
  1008. Output: ylp
  1009. Return value: none.
  1010. ~~~~~~~~~~~~~
  1011. Prototype: in g726.h
  1012. ~~~~~~~~~~
  1013. History:
  1014. ~~~~~~~~
  1015. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1016. <tdsindi@venus.cpqd.ansp.br>
  1017. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1018. <tdsimao@venus.cpqd.ansp.br>
  1019. ----------------------------------------------------------------------
  1020. */
  1021. void G726_filte (short *yup, long *yl, long *ylp) {
  1022. long difs, difsx;
  1023. long dif, dif1, yup1;
  1024. /* Compute difference */
  1025. yup1 = *yup;
  1026. dif1 = 1048576 - *yl;
  1027. dif = (yup1 + (dif1 >> 6)) & 16383;
  1028. difs = (dif >> 13);
  1029. /* Sign extension */
  1030. difsx = (difs == 0) ? dif : (dif + 507904);
  1031. *ylp = (*yl + difsx) & 524287;
  1032. }
  1033. /* ....................... end of G726_filte() ....................... */
  1034. /*
  1035. ----------------------------------------------------------------------
  1036. void G726_functw (short rate, short *i, short *wi);
  1037. ~~~~~~~~~~~~~~~~
  1038. Description:
  1039. ~~~~~~~~~~~~
  1040. Map quantizer output into logarithmic version of scale factor
  1041. multiplier.
  1042. Inputs: i, rate
  1043. Output: wi
  1044. Return value: none.
  1045. ~~~~~~~~~~~~~
  1046. Prototype: in g726.h
  1047. ~~~~~~~~~~
  1048. History:
  1049. ~~~~~~~~
  1050. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1051. <tdsindi@venus.cpqd.ansp.br>
  1052. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1053. <tdsimao@venus.cpqd.ansp.br>
  1054. ----------------------------------------------------------------------
  1055. */
  1056. void G726_functw (short rate, short *i, short *wi) {
  1057. if (rate == 4) {
  1058. /* Initialized data */
  1059. static short tab[8] = { 4084, 18, 41, 64, 112, 198, 355, 1122 };
  1060. short im, is;
  1061. is = (*i >> 3);
  1062. im = (is == 0) ? (*i & 7) : ((15 - *i) & 7);
  1063. /* Scale factor multiplier */
  1064. *wi = tab[im];
  1065. }
  1066. /* ................. end of 32 kbit part * .............. */
  1067. else if (rate == 3) {
  1068. /* Initialized data */
  1069. static short tab[4] = { 4092, 30, 137, 582 };
  1070. short im, is;
  1071. is = (*i >> 2);
  1072. im = (is == 0) ? (*i & 3) : ((7 - *i) & 3);
  1073. *wi = tab[im];
  1074. }
  1075. /* ................. end of 24 kbit part * .............. */
  1076. else if (rate == 2) {
  1077. /* Initialized data */
  1078. static short tab[2] = { 4074, 439 };
  1079. short im, is;
  1080. is = (*i >> 1);
  1081. im = (is == 0) ? (*i & 1) : ((3 - *i) & 1);
  1082. *wi = tab[im];
  1083. }
  1084. /* ................. end of 16 kbit part * .............. */
  1085. else {
  1086. /* Initialized data */
  1087. static short tab[16] = { 14, 14, 24, 39, 40, 41, 58, 100, 141, 179, 219, 280, 358,
  1088. 440, 529, 696
  1089. };
  1090. short im, is;
  1091. is = (*i >> 4);
  1092. im = (is == 0) ? (*i & 15) : ((31 - *i) & 15);
  1093. *wi = tab[im];
  1094. } /* ................. end of 40 kbit part .............. */
  1095. }
  1096. /* ....................... end of G726_functw() ....................... */
  1097. /*
  1098. ----------------------------------------------------------------------
  1099. void G726_limb (short *yut, short *yup);
  1100. ~~~~~~~~~~~~~~
  1101. Description:
  1102. ~~~~~~~~~~~~
  1103. Limit quantizer scale factor.
  1104. Inputs: yut
  1105. Output: yup
  1106. Return value: none.
  1107. ~~~~~~~~~~~~~
  1108. Prototype: in g726.h
  1109. ~~~~~~~~~~
  1110. History:
  1111. ~~~~~~~~
  1112. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1113. <tdsindi@venus.cpqd.ansp.br>
  1114. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1115. <tdsimao@venus.cpqd.ansp.br>
  1116. ----------------------------------------------------------------------
  1117. */
  1118. void G726_limb (short *yut, short *yup) {
  1119. short gell, geul;
  1120. geul = ((*yut + 11264) & 16383) >> 13;
  1121. gell = ((*yut + 15840) & 16383) >> 13;
  1122. if (gell == 1)
  1123. *yup = 544; /* Lower limit is 1.06 */
  1124. else if (geul == 0)
  1125. *yup = 5120; /* Upper limit is 10.0 */
  1126. else
  1127. *yup = *yut;
  1128. }
  1129. /* ....................... end of G726_limb() ....................... */
  1130. /*
  1131. ----------------------------------------------------------------------
  1132. void G726_mix (short *al, short *yu, long *yl, short *y);
  1133. ~~~~~~~~~~~~~
  1134. Description:
  1135. ~~~~~~~~~~~~
  1136. Form linear combination of fast and slow quantizer scale
  1137. factors.
  1138. Inputs: al, yu, yl
  1139. Output: y
  1140. Return value: none.
  1141. ~~~~~~~~~~~~~
  1142. Prototype: in g726.h
  1143. ~~~~~~~~~~
  1144. History:
  1145. ~~~~~~~~
  1146. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1147. <tdsindi@venus.cpqd.ansp.br>
  1148. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1149. <tdsimao@venus.cpqd.ansp.br>
  1150. ----------------------------------------------------------------------
  1151. */
  1152. void G726_mix (short *al, short *yu, long *yl, short *y) {
  1153. long difm, difs, prod;
  1154. long prodm, al1;
  1155. long yu1, dif;
  1156. /* Preamble */
  1157. al1 = *al;
  1158. yu1 = *yu;
  1159. /* Compute difference */
  1160. dif = (yu1 + 16384 - (*yl >> 6)) & 16383;
  1161. difs = (dif >> 13);
  1162. /* Compute magnitude of difference */
  1163. difm = (difs == 0) ? dif : ((16384 - dif) & 8191);
  1164. /* Compute magnitude of product */
  1165. prodm = ((difm * al1) >> 6);
  1166. /* Convert magnitude to two's complement */
  1167. prod = (difs == 0) ? prodm : ((16384 - prodm) & 16383);
  1168. *y = (short) (((*yl >> 6) + prod) & 8191);
  1169. }
  1170. /* ....................... end of G726_mix() ....................... */
  1171. /*
  1172. ----------------------------------------------------------------------
  1173. void G726_filta (short *fi, short *dms, short *dmsp);
  1174. ~~~~~~~~~~~~~~~
  1175. Description:
  1176. ~~~~~~~~~~~~
  1177. Update of short term average of f(i).
  1178. Inputs: fi, dms
  1179. Output: dmsp
  1180. Return value: none.
  1181. ~~~~~~~~~~~~~
  1182. Prototype: in g726.h
  1183. ~~~~~~~~~~
  1184. History:
  1185. ~~~~~~~~
  1186. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1187. <tdsindi@venus.cpqd.ansp.br>
  1188. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1189. <tdsimao@venus.cpqd.ansp.br>
  1190. ----------------------------------------------------------------------
  1191. */
  1192. void G726_filta (short *fi, short *dms, short *dmsp) {
  1193. short difs, difsx;
  1194. short dif;
  1195. /* Compute difference */
  1196. dif = ((*fi << 9) + 8192 - *dms) & 8191;
  1197. difs = (dif >> 12);
  1198. /* Time constant is 1/32, sign extension */
  1199. difsx = (difs == 0) ? (dif >> 5) : ((dif >> 5) + 3840);
  1200. *dmsp = (difsx + *dms) & 4095;
  1201. }
  1202. /* ....................... end of G726_filta() ....................... */
  1203. /*
  1204. ----------------------------------------------------------------------
  1205. void G726_filtb (short *fi, short *dml, short *dmlp);
  1206. ~~~~~~~~~~~~~~~
  1207. Description:
  1208. ~~~~~~~~~~~~
  1209. Update of long term average of f(i).
  1210. Inputs: fi, dml
  1211. Output: dmlp
  1212. Return value: none.
  1213. ~~~~~~~~~~~~~
  1214. Prototype: in g726.h
  1215. ~~~~~~~~~~
  1216. History:
  1217. ~~~~~~~~
  1218. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1219. <tdsindi@venus.cpqd.ansp.br>
  1220. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1221. <tdsimao@venus.cpqd.ansp.br>
  1222. ----------------------------------------------------------------------
  1223. */
  1224. void G726_filtb (short *fi, short *dml, short *dmlp) {
  1225. long difs, difsx;
  1226. long fi1;
  1227. long dif, dml1;
  1228. /* Preamble */
  1229. fi1 = *fi;
  1230. dml1 = *dml;
  1231. /* Compute difference */
  1232. dif = ((fi1 << 11) + 32768 - dml1) & 32767;
  1233. difs = (dif >> 14);
  1234. /* Time constant is 1/28, sign extension */
  1235. difsx = (difs == 0) ? (dif >> 7) : ((dif >> 7) + 16128);
  1236. *dmlp = (short) ((difsx + dml1) & 16383);
  1237. }
  1238. /* ....................... end of G726_filtb() ....................... */
  1239. /*
  1240. ----------------------------------------------------------------------
  1241. void G726_filtc (short *ax, short *ap, short *app);
  1242. ~~~~~~~~~~~~~~~
  1243. Description:
  1244. ~~~~~~~~~~~~
  1245. Low pass filter of speed control parameter.
  1246. Inputs: ax, ap
  1247. Output: app
  1248. Return value: none.
  1249. ~~~~~~~~~~~~~
  1250. Prototype: in g726.h
  1251. ~~~~~~~~~~
  1252. History:
  1253. ~~~~~~~~
  1254. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1255. <tdsindi@venus.cpqd.ansp.br>
  1256. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1257. <tdsimao@venus.cpqd.ansp.br>
  1258. ----------------------------------------------------------------------
  1259. */
  1260. void G726_filtc (short *ax, short *ap, short *app) {
  1261. short difs, difsx;
  1262. short dif;
  1263. /* Compute difference */
  1264. dif = ((*ax << 9) + 2048 - *ap) & 2047;
  1265. difs = (dif >> 10);
  1266. /* Time constant is 1/16, sign extension */
  1267. difsx = (difs == 0) ? (dif >> 4) : ((dif >> 4) + 896);
  1268. *app = (difsx + *ap) & 1023;
  1269. }
  1270. /* .................... end of G726_filtc() .................... */
  1271. /*
  1272. ----------------------------------------------------------------------
  1273. void G726_functf (short rate, short *i, short *fi);
  1274. ~~~~~~~~~~~~~~~~
  1275. Description:
  1276. ~~~~~~~~~~~~
  1277. Map quantizer output into the f(i) function.
  1278. Inputs: i, rate
  1279. Output: fi
  1280. Return value: none.
  1281. ~~~~~~~~~~~~~
  1282. Prototype: in g726.h
  1283. ~~~~~~~~~~
  1284. History:
  1285. ~~~~~~~~
  1286. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1287. <tdsindi@venus.cpqd.ansp.br>
  1288. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1289. <tdsimao@venus.cpqd.ansp.br>
  1290. ----------------------------------------------------------------------
  1291. */
  1292. void G726_functf (short rate, short *i, short *fi) {
  1293. short im, is;
  1294. if (rate == 4) {
  1295. /* Initialized data */
  1296. static short tab[8] = { 0, 0, 0, 1, 1, 1, 3, 7 };
  1297. is = (*i >> 3);
  1298. im = (is == 0) ? (*i & 7) : ((15 - *i) & 7);
  1299. *fi = tab[im];
  1300. }
  1301. /* ................ end of 32 kbit part * ................. */
  1302. else if (rate == 3) {
  1303. /* Initialized data */
  1304. static short tab[4] = { 0, 1, 2, 7 };
  1305. is = (*i >> 2);
  1306. im = (is == 0) ? (*i & 3) : ((7 - *i) & 3);
  1307. *fi = tab[im];
  1308. } /* ................ end of 24 kbit part * ................. */
  1309. else if (rate == 2) {
  1310. /* Initialized data */
  1311. static short tab[2] = { 0, 7 };
  1312. is = (*i >> 1);
  1313. im = (is == 0) ? (*i & 1) : ((3 - *i) & 1);
  1314. *fi = tab[im];
  1315. }
  1316. /* ................ end of 16 kbit part * ................. */
  1317. else {
  1318. /* Initialized data */
  1319. static short tab[16] = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6 };
  1320. is = (*i >> 4);
  1321. im = (is == 0) ? (*i & 15) : ((31 - *i) & 15);
  1322. *fi = tab[im];
  1323. } /* ................ end of 40 kbit part ................. */
  1324. }
  1325. /* ...................... end of G726_functf() ...................... */
  1326. /*
  1327. ----------------------------------------------------------------------
  1328. void G726_lima (short *ap, short *al);
  1329. ~~~~~~~~~~~~~~
  1330. Description:
  1331. ~~~~~~~~~~~~
  1332. Limit speed control parameter.
  1333. Inputs: ap
  1334. Output: al
  1335. Return value: none.
  1336. ~~~~~~~~~~~~~
  1337. Prototype: in g726.h
  1338. ~~~~~~~~~~
  1339. History:
  1340. ~~~~~~~~
  1341. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1342. <tdsindi@venus.cpqd.ansp.br>
  1343. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1344. <tdsimao@venus.cpqd.ansp.br>
  1345. ----------------------------------------------------------------------
  1346. */
  1347. void G726_lima (short *ap, short *al) {
  1348. *al = (*ap >= 256) ? 64 : (*ap >> 2);
  1349. }
  1350. /* ....................... end of G726_lima() ....................... */
  1351. /*
  1352. ----------------------------------------------------------------------
  1353. void G726_subtc (short *dmsp, short *dmlp, short *tdp,
  1354. ~~~~~~~~~~~~~~~ short *y, short *ax);
  1355. Description:
  1356. ~~~~~~~~~~~~
  1357. Functions of quantizer output sequence and then perform
  1358. threshold comparison for quantizing speed control parameter.
  1359. compute magnitude of the difference of short and long-term
  1360. Inputs: dmsp, dmlp, tdp, y
  1361. Output: ax
  1362. Return value: none.
  1363. ~~~~~~~~~~~~~
  1364. Prototype: in g726.h
  1365. ~~~~~~~~~~
  1366. History:
  1367. ~~~~~~~~
  1368. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1369. <tdsindi@venus.cpqd.ansp.br>
  1370. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1371. <tdsimao@venus.cpqd.ansp.br>
  1372. ----------------------------------------------------------------------
  1373. */
  1374. void G726_subtc (short *dmsp, short *dmlp, short *tdp, short *y, short *ax) {
  1375. long difm, difs, dthr, dmlp1, dmsp1;
  1376. long dif;
  1377. /* Preamble */
  1378. dmsp1 = *dmsp;
  1379. dmlp1 = *dmlp;
  1380. /* Compute difference */
  1381. dif = ((dmsp1 << 2) + 32768 - dmlp1) & 32767;
  1382. difs = (dif >> 14);
  1383. /* Compute magnitude of difference */
  1384. difm = (difs == 0) ? dif : ((32768 - dif) & 16383);
  1385. /* Compute threshold */
  1386. dthr = (dmlp1 >> 3);
  1387. /* Quantize speed control parameter */
  1388. *ax = (*y >= 1536 && difm < dthr && *tdp == 0) ? 0 : 1;
  1389. }
  1390. /* ....................... end of G726_subtc() ....................... */
  1391. /*
  1392. ----------------------------------------------------------------------
  1393. void G726_triga (short *tr, short *app, short *apr);
  1394. ~~~~~~~~~~~~~~~
  1395. Description:
  1396. ~~~~~~~~~~~~
  1397. Speed control trigger block.
  1398. Inputs: tr, app
  1399. Output: apr
  1400. Return value: none.
  1401. ~~~~~~~~~~~~~
  1402. Prototype: in g726.h
  1403. ~~~~~~~~~~
  1404. History:
  1405. ~~~~~~~~
  1406. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1407. <tdsindi@venus.cpqd.ansp.br>
  1408. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1409. <tdsimao@venus.cpqd.ansp.br>
  1410. ----------------------------------------------------------------------
  1411. */
  1412. void G726_triga (short *tr, short *app, short *apr) {
  1413. *apr = (*tr == 0) ? (*app) : 256;
  1414. }
  1415. /* ....................... end of G726_triga() ....................... */
  1416. /*
  1417. ----------------------------------------------------------------------
  1418. void G726_accum (short *wa1, short *wa2, short *wb1,
  1419. ~~~~~~~~~~~~~~~ short *wb2, short *wb3, short *wb4,
  1420. short *wb5, short *wb6, short *se, short *sez);
  1421. Description:
  1422. ~~~~~~~~~~~~
  1423. Addition of predictor outputs to form the partial signal
  1424. estimate (from the sixth order predictor) and the signal
  1425. estimate.
  1426. Inputs: wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6
  1427. Output: se, sez
  1428. Return value: none.
  1429. ~~~~~~~~~~~~~
  1430. Prototype: in g726.h
  1431. ~~~~~~~~~~
  1432. History:
  1433. ~~~~~~~~
  1434. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1435. <tdsindi@venus.cpqd.ansp.br>
  1436. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1437. <tdsimao@venus.cpqd.ansp.br>
  1438. ----------------------------------------------------------------------
  1439. */
  1440. void G726_accum (short *wa1, short *wa2, short *wb1, short *wb2, short *wb3, short *wb4, short *wb5, short *wb6, short *se, short *sez) {
  1441. unsigned long sezi;
  1442. unsigned long wa11, wa21, wb11, wb21, wb31, wb41, wb51, wb61, sei;
  1443. /* Preamble */
  1444. wa11 = *wa1;
  1445. wa21 = *wa2;
  1446. wb11 = *wb1;
  1447. wb21 = *wb2;
  1448. wb31 = *wb3;
  1449. wb41 = *wb4;
  1450. wb51 = *wb5;
  1451. wb61 = *wb6;
  1452. /* Sum of partial signal estimate */
  1453. sezi = (((((((((wb11 + wb21) & 65535) + wb31) & 65535)
  1454. + wb41) & 65535) + wb51) & 65535) + wb61) & 65535;
  1455. /* Complete sum for signal estimate */
  1456. sei = (((sezi + wa21) & 65535) + wa11) & 65535;
  1457. *sez = (short) (sezi >> 1);
  1458. *se = (short) (sei >> 1);
  1459. }
  1460. /* ....................... end of G726_accum() ....................... */
  1461. /*
  1462. ----------------------------------------------------------------------
  1463. void G726_addb (short *dq, short *se, short *sr);
  1464. ~~~~~~~~~~~~~~
  1465. Description:
  1466. ~~~~~~~~~~~~
  1467. Addition of quantized difference signal and signal estimate
  1468. to form reconstructed signal.
  1469. Inputs: dq, se
  1470. Output: sr
  1471. Return value: none.
  1472. ~~~~~~~~~~~~~
  1473. Prototype: in g726.h
  1474. ~~~~~~~~~~
  1475. History:
  1476. ~~~~~~~~
  1477. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1478. <tdsindi@venus.cpqd.ansp.br>
  1479. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1480. <tdsimao@venus.cpqd.ansp.br>
  1481. ----------------------------------------------------------------------
  1482. */
  1483. void G726_addb (short *dq, short *se, short *sr) {
  1484. unsigned long dq1, se1;
  1485. unsigned long dqi, sei;
  1486. short dqs, ses;
  1487. /* Preamble */
  1488. dq1 = *dq & 65535;
  1489. se1 = *se;
  1490. /* Sign */
  1491. dqs = (*dq >> 15) & 1;
  1492. /* Convert signed magnitude to 2's complement */
  1493. dqi = (dqs == 0) ? dq1 : ((65536 - (dq1 & 32767)) & 65535);
  1494. ses = (*se >> 14);
  1495. /* Sign extension */
  1496. sei = (ses == 0) ? se1 : ((1 << 15) + se1);
  1497. *sr = (short) ((dqi + sei) & 65535);
  1498. }
  1499. /* ....................... end of G726_addb() ....................... */
  1500. /*
  1501. ----------------------------------------------------------------------
  1502. void G726_addc (short *dq, short *sez, short *pk0,
  1503. ~~~~~~~~~~~~~~ short *sigpk);
  1504. Description:
  1505. ~~~~~~~~~~~~
  1506. Obtain sign of addition of quantized difference signal and
  1507. partial signal estimate.
  1508. Inputs: dq, sez
  1509. Outputs: pk0, sigpk
  1510. Return value: none.
  1511. ~~~~~~~~~~~~~
  1512. Prototype: in g726.h
  1513. ~~~~~~~~~~
  1514. History:
  1515. ~~~~~~~~
  1516. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1517. <tdsindi@venus.cpqd.ansp.br>
  1518. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1519. <tdsimao@venus.cpqd.ansp.br>
  1520. ----------------------------------------------------------------------
  1521. */
  1522. void G726_addc (short *dq, short *sez, short *pk0, short *sigpk) {
  1523. unsigned long sezi;
  1524. short sezs;
  1525. unsigned long dqsez, dq1;
  1526. unsigned long dqi;
  1527. short dqs;
  1528. unsigned long sez1;
  1529. /* Preamble */
  1530. dq1 = *dq & 65535;
  1531. sez1 = *sez;
  1532. /* Get sign */
  1533. dqs = (*dq >> 15) & 1;
  1534. /* Convert signed magnitude to 2's compelemnent */
  1535. dqi = (dqs == 0) ? dq1 : ((65536 - (dq1 & 32767)) & 65535);
  1536. sezs = (*sez >> 14);
  1537. /* Sign extension */
  1538. sezi = (sezs == 0) ? sez1 : (sez1 + 32768);
  1539. dqsez = (dqi + sezi) & 65535;
  1540. *pk0 = (short) (dqsez >> 15);
  1541. *sigpk = (dqsez == 0) ? 1 : 0;
  1542. }
  1543. /* ....................... end of G726_addc() ....................... */
  1544. /*
  1545. ----------------------------------------------------------------------
  1546. void G726_floata (short *dq, short *dq0);
  1547. ~~~~~~~~~~~~~~~~
  1548. Description:
  1549. ~~~~~~~~~~~~
  1550. Convert 16-bit signed magnitude to floating point.
  1551. Inputs: dq
  1552. Output: dq0
  1553. Return value: none.
  1554. ~~~~~~~~~~~~~
  1555. Prototype: in g726.h
  1556. ~~~~~~~~~~
  1557. History:
  1558. ~~~~~~~~
  1559. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1560. <tdsindi@venus.cpqd.ansp.br>
  1561. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1562. <tdsimao@venus.cpqd.ansp.br>
  1563. ----------------------------------------------------------------------
  1564. */
  1565. void G726_floata (short *dq, short *dq0) {
  1566. long mant;
  1567. long mag, exp_;
  1568. long dqs;
  1569. dqs = (*dq >> 15) & 1;
  1570. /* Compute magnitude */
  1571. mag = *dq & 32767;
  1572. /* Exponent */
  1573. if (mag >= 16384)
  1574. exp_ = 15;
  1575. else if (mag >= 8192)
  1576. exp_ = 14;
  1577. else if (mag >= 4096)
  1578. exp_ = 13;
  1579. else if (mag >= 2048)
  1580. exp_ = 12;
  1581. else if (mag >= 1024)
  1582. exp_ = 11;
  1583. else if (mag >= 512)
  1584. exp_ = 10;
  1585. else if (mag >= 256)
  1586. exp_ = 9;
  1587. else if (mag >= 128)
  1588. exp_ = 8;
  1589. else if (mag >= 64)
  1590. exp_ = 7;
  1591. else if (mag >= 32)
  1592. exp_ = 6;
  1593. else if (mag >= 16)
  1594. exp_ = 5;
  1595. else if (mag >= 8)
  1596. exp_ = 4;
  1597. else if (mag >= 4)
  1598. exp_ = 3;
  1599. else if (mag >= 2)
  1600. exp_ = 2;
  1601. else if (mag == 1)
  1602. exp_ = 1;
  1603. else
  1604. exp_ = 0;
  1605. /* Compute mantissa w/a 1 in the most sig. bit */
  1606. mant = (mag == 0) ? (1 << 5) : ((mag << 6) >> exp_);
  1607. /* Combine sign, exponent and mantissa (1,4,6) bits in a word */
  1608. *dq0 = (short) ((dqs << 10) + (exp_ << 6) + mant);
  1609. }
  1610. /* ....................... end of G726_floata() ....................... */
  1611. /*
  1612. ----------------------------------------------------------------------
  1613. void G726_floatb (short *sr, short *sr0);
  1614. ~~~~~~~~~~~~~~~~
  1615. Description:
  1616. ~~~~~~~~~~~~
  1617. Convert 16-bit two's complement to floating point.
  1618. Inputs: sr
  1619. Output: sr0
  1620. Return value: none.
  1621. ~~~~~~~~~~~~~
  1622. Prototype: in g726.h
  1623. ~~~~~~~~~~
  1624. History:
  1625. ~~~~~~~~
  1626. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1627. <tdsindi@venus.cpqd.ansp.br>
  1628. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1629. <tdsimao@venus.cpqd.ansp.br>
  1630. ----------------------------------------------------------------------
  1631. */
  1632. void G726_floatb (short *sr, short *sr0) {
  1633. long mant;
  1634. long mag, exp_, srr, srs;
  1635. /* Preamble */
  1636. srr = *sr & 65535;
  1637. /* Sign */
  1638. srs = (srr >> 15);
  1639. /* Compute magnitude */
  1640. mag = (srs == 0) ? srr : ((65536 - srr) & 32767);
  1641. /* Exponent */
  1642. if (mag >= 16384)
  1643. exp_ = 15;
  1644. else if (mag >= 8192)
  1645. exp_ = 14;
  1646. else if (mag >= 4096)
  1647. exp_ = 13;
  1648. else if (mag >= 2048)
  1649. exp_ = 12;
  1650. else if (mag >= 1024)
  1651. exp_ = 11;
  1652. else if (mag >= 512)
  1653. exp_ = 10;
  1654. else if (mag >= 256)
  1655. exp_ = 9;
  1656. else if (mag >= 128)
  1657. exp_ = 8;
  1658. else if (mag >= 64)
  1659. exp_ = 7;
  1660. else if (mag >= 32)
  1661. exp_ = 6;
  1662. else if (mag >= 16)
  1663. exp_ = 5;
  1664. else if (mag >= 8)
  1665. exp_ = 4;
  1666. else if (mag >= 4)
  1667. exp_ = 3;
  1668. else if (mag >= 2)
  1669. exp_ = 2;
  1670. else if (mag == 1)
  1671. exp_ = 1;
  1672. else
  1673. exp_ = 0;
  1674. /* Compute mantissa w/a 1 in the most sig. bit */
  1675. mant = (mag == 0) ? (1 << 5) : ((mag << 6) >> exp_);
  1676. /* Combine sign, exponent and mantissa (1,4,6) bits in a word */
  1677. *sr0 = (short) ((srs << 10) + (exp_ << 6) + mant);
  1678. }
  1679. /* ....................... end of G726_floatb() ....................... */
  1680. /*
  1681. ----------------------------------------------------------------------
  1682. void G726_fmult (short *a, short *srn, short *wa);
  1683. ~~~~~~~~~~~~~~~
  1684. Description:
  1685. ~~~~~~~~~~~~
  1686. Multiply predictor coefficients with corresponding quantized
  1687. difference signal or reconstructed signal. multiplication is
  1688. done in floating piont format
  1689. Inputs: a (or b), srn (or dqn)
  1690. Outputs: wa (or wb)
  1691. Return value: none.
  1692. ~~~~~~~~~~~~~
  1693. Prototype: in g726.h
  1694. ~~~~~~~~~~
  1695. History:
  1696. ~~~~~~~~
  1697. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1698. <tdsindi@venus.cpqd.ansp.br>
  1699. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1700. <tdsimao@venus.cpqd.ansp.br>
  1701. ----------------------------------------------------------------------
  1702. */
  1703. void G726_fmult (short *An, short *SRn, short *WAn) {
  1704. long anmag, anexp, wanmag, anmant;
  1705. long wanexp, srnexp, an, ans, wanmant, srnmant;
  1706. long wan, wans, srns, srn1;
  1707. /* Preamble */
  1708. an = *An & 65535;
  1709. srn1 = *SRn & 65535;
  1710. /* Sign */
  1711. ans = an & 32768;
  1712. ans = (ans >> 15);
  1713. /* Convert 2's complement to signed magnitude */
  1714. anmag = (ans == 0) ? (an >> 2) : ((16384 - (an >> 2)) & 8191);
  1715. /* Exponent */
  1716. if (anmag >= 4096)
  1717. anexp = 13;
  1718. else if (anmag >= 2048)
  1719. anexp = 12;
  1720. else if (anmag >= 1024)
  1721. anexp = 11;
  1722. else if (anmag >= 512)
  1723. anexp = 10;
  1724. else if (anmag >= 256)
  1725. anexp = 9;
  1726. else if (anmag >= 128)
  1727. anexp = 8;
  1728. else if (anmag >= 64)
  1729. anexp = 7;
  1730. else if (anmag >= 32)
  1731. anexp = 6;
  1732. else if (anmag >= 16)
  1733. anexp = 5;
  1734. else if (anmag >= 8)
  1735. anexp = 4;
  1736. else if (anmag >= 4)
  1737. anexp = 3;
  1738. else if (anmag >= 2)
  1739. anexp = 2;
  1740. else if (anmag == 1)
  1741. anexp = 1;
  1742. else
  1743. anexp = 0;
  1744. /* Compute mantissa w/a 1 in the most sig. bit */
  1745. anmant = (anmag == 0) ? (1 << 5) : ((anmag << 6) >> anexp);
  1746. /* Split floating point word into sign, exponent and mantissa */
  1747. srns = (srn1 >> 10);
  1748. srnexp = (srn1 >> 6) & 15;
  1749. srnmant = srn1 & 63;
  1750. /* Floating point multiplication */
  1751. wans = srns ^ ans;
  1752. wanexp = srnexp + anexp;
  1753. wanmant = ((srnmant * anmant) + 48) >> 4;
  1754. /* Convert floating point to magnitude */
  1755. wanmag = (wanexp <= 26) ? (wanmant << 7) >> (26 - wanexp) : ((wanmant << 7) << (wanexp - 26)) & 32767;
  1756. /* Convert mag. to 2's complement */
  1757. wan = (wans == 0) ? wanmag : ((65536 - wanmag) & 65535);
  1758. *WAn = (short) wan;
  1759. }
  1760. /* ....................... end of G726_fmult() ....................... */
  1761. /*
  1762. ----------------------------------------------------------------------
  1763. void G726_limc (short *a2t, short *a2p);
  1764. ~~~~~~~~~~~~~~
  1765. Description:
  1766. ~~~~~~~~~~~~
  1767. Limits on a2 coefficient of second order predictor.
  1768. Inputs: a2t
  1769. Output: a2p
  1770. Return value: none.
  1771. ~~~~~~~~~~~~~
  1772. Prototype: in g726.h
  1773. ~~~~~~~~~~
  1774. History:
  1775. ~~~~~~~~
  1776. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1777. <tdsindi@venus.cpqd.ansp.br>
  1778. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1779. <tdsimao@venus.cpqd.ansp.br>
  1780. ----------------------------------------------------------------------
  1781. */
  1782. void G726_limc (short *a2t, short *a2p) {
  1783. long a2p1, a2t1, a2ll, a2ul;
  1784. a2t1 = *a2t & 65535;
  1785. a2ul = 12288; /* Upper limit of +.75 */
  1786. a2ll = 53248; /* Lower limit of -.75 */
  1787. if (a2t1 >= 32768 && a2t1 <= a2ll)
  1788. a2p1 = a2ll;
  1789. else if (a2t1 >= a2ul && a2t1 <= 32767)
  1790. a2p1 = a2ul;
  1791. else
  1792. a2p1 = a2t1;
  1793. *a2p = (short) a2p1;
  1794. }
  1795. /* ....................... end of G726_limc() ....................... */
  1796. /*
  1797. ----------------------------------------------------------------------
  1798. void G726_limd (short *a1t, short *a2p, short *a1p);
  1799. ~~~~~~~~~~~~~~
  1800. Description:
  1801. ~~~~~~~~~~~~
  1802. Limits on a1 coefficient of second order predictor.
  1803. Inputs: a1t, a2p
  1804. Output: a1p
  1805. Return value: none.
  1806. ~~~~~~~~~~~~~
  1807. Prototype: in g726.h
  1808. ~~~~~~~~~~
  1809. History:
  1810. ~~~~~~~~
  1811. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1812. <tdsindi@venus.cpqd.ansp.br>
  1813. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1814. <tdsimao@venus.cpqd.ansp.br>
  1815. ----------------------------------------------------------------------
  1816. */
  1817. void G726_limd (short *a1t, short *a2p, short *a1p) {
  1818. long a1p1, a2p1, a1t1, ome, a1ll, a1ul;
  1819. /* Preamble */
  1820. a1t1 = *a1t & 65535;
  1821. a2p1 = *a2p & 65535;
  1822. /* (1-epsilon), where epsilon = (1/16) */
  1823. ome = 15360;
  1824. /* Compute upper limit */
  1825. a1ul = (ome + 65536 - a2p1) & 65535;
  1826. /* Compute lower limit */
  1827. a1ll = (a2p1 + 65536 - ome) & 65535;
  1828. if (a1t1 >= 32768 && a1t1 <= a1ll)
  1829. a1p1 = a1ll;
  1830. else if (a1t1 >= a1ul && a1t1 <= 32767)
  1831. a1p1 = a1ul;
  1832. else
  1833. a1p1 = a1t1;
  1834. *a1p = (short) a1p1;
  1835. }
  1836. /* ....................... end of G726_limd() ....................... */
  1837. /*
  1838. ----------------------------------------------------------------------
  1839. void G726_trigb (short *tr, short *ap, short *ar);
  1840. ~~~~~~~~~~~~~~~
  1841. Description:
  1842. ~~~~~~~~~~~~
  1843. Predictor trigger block.
  1844. Inputs: tr, ap (ou bp ou tdp)
  1845. Output: ar (ou br ou tdr)
  1846. Return value: none.
  1847. ~~~~~~~~~~~~~
  1848. Prototype: in g726.h
  1849. ~~~~~~~~~~
  1850. History:
  1851. ~~~~~~~~
  1852. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1853. <tdsindi@venus.cpqd.ansp.br>
  1854. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1855. <tdsimao@venus.cpqd.ansp.br>
  1856. ----------------------------------------------------------------------
  1857. */
  1858. void G726_trigb (short *tr, short *ap, short *ar) {
  1859. *ar = (*tr == 0) ? *ap : 0;
  1860. }
  1861. /* ....................... end of G726_trigb() ....................... */
  1862. /*
  1863. ----------------------------------------------------------------------
  1864. void G726_upa1 (short *pk0, short *pk1, short *a1,
  1865. ~~~~~~~~~~~~~~ short *sigpk, short *a1t);
  1866. Description:
  1867. ~~~~~~~~~~~~
  1868. Update a1 coefficient of second order predictor.
  1869. Inputs: pk0, pk1, a1, sigpk
  1870. Output: a1t
  1871. Return value: none.
  1872. ~~~~~~~~~~~~~
  1873. Prototype: in g726.h
  1874. ~~~~~~~~~~
  1875. History:
  1876. ~~~~~~~~
  1877. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1878. <tdsindi@venus.cpqd.ansp.br>
  1879. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1880. <tdsimao@venus.cpqd.ansp.br>
  1881. ----------------------------------------------------------------------
  1882. */
  1883. void G726_upa1 (short *pk0, short *pk1, short *a1, short *sigpk, short *a1t) {
  1884. long a11, a1s, ua1;
  1885. long ash;
  1886. short pks;
  1887. long uga1, ula1;
  1888. /* Preamble */
  1889. a11 = *a1 & 65535;
  1890. pks = (*pk0) ^ (*pk1);
  1891. /* Gain is +/- (3/256) */
  1892. uga1 = (*sigpk == 1) ? 0 : ((pks == 0) ? 192 : 65344);
  1893. a1s = (a11 >> 15);
  1894. /* Leak factor is (1/256) */
  1895. ash = (a11 >> 8);
  1896. ula1 = ((a1s == 0) ? (65536 - ash) : (65536 - (ash + 65280))) & 65535;
  1897. /* Compute update */
  1898. ua1 = (uga1 + ula1) & 65535;
  1899. *a1t = (short) ((a11 + ua1) & 65535);
  1900. }
  1901. /* ....................... end of G726_upa1() ....................... */
  1902. /*
  1903. ----------------------------------------------------------------------
  1904. void G726_upa2 (short *pk0, short *pk1, short *pk2,
  1905. ~~~~~~~~~~~~~~ short *a2, short *a1, short *sigpk, short *a2t);
  1906. Description:
  1907. ~~~~~~~~~~~~
  1908. Update a2 coefficient of second order predictor.
  1909. Inputs: pk0, pk1, pk2, a2, a1, sigpk
  1910. Output: a2t
  1911. Return value: none.
  1912. ~~~~~~~~~~~~~
  1913. Prototype: in g726.h
  1914. ~~~~~~~~~~
  1915. History:
  1916. ~~~~~~~~
  1917. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1918. <tdsindi@venus.cpqd.ansp.br>
  1919. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1920. <tdsimao@venus.cpqd.ansp.br>
  1921. ----------------------------------------------------------------------
  1922. */
  1923. void G726_upa2 (short *pk0, short *pk1, short *pk2, short *a2, short *a1, short *sigpk, short *a2t) {
  1924. long uga2a, uga2b, uga2s;
  1925. long a11, a21, fa, fa1;
  1926. short a1s, a2s;
  1927. long ua2;
  1928. long uga2, ula2;
  1929. short pks1, pks2;
  1930. /* Preamble */
  1931. a11 = *a1 & 65535;
  1932. a21 = *a2 & 65535;
  1933. /* 1 bit xors */
  1934. pks1 = (*pk0 ^ *pk1);
  1935. pks2 = (*pk0 ^ *pk2);
  1936. uga2a = (pks2 == 0) ? 16384 : 114688;
  1937. a1s = (*a1 >> 15);
  1938. /* Implement f(a1) w/ limiting at +/-(1/2) */
  1939. if (a1s == 0)
  1940. fa1 = (a11 <= 8191) ? (a11 << 2) : (8191 << 2);
  1941. else
  1942. fa1 = (a11 >= 57345) ? ((a11 << 2) & 131071) : (24577 << 2);
  1943. /* Attach sign to the result of f(a1) */
  1944. fa = (pks1) ? fa1 : ((131072 - fa1) & 131071);
  1945. uga2b = (uga2a + fa) & 131071;
  1946. uga2s = (uga2b >> 16);
  1947. uga2 = (*sigpk == 1) ? 0 : ((uga2s) ? ((uga2b >> 7) + 64512) : (uga2b >> 7));
  1948. a2s = (*a2 >> 15);
  1949. ula2 = (a2s == 0) ? (65536 - (a21 >> 7)) & 65535 : (65536 - ((a21 >> 7) + 65024)) & 65535;
  1950. /* Compute update */
  1951. ua2 = (uga2 + ula2) & 65535;
  1952. *a2t = (short) ((a21 + ua2) & 65535);
  1953. }
  1954. /* ....................... end of G726_upa2() ....................... */
  1955. /*
  1956. ----------------------------------------------------------------------
  1957. void G726_upb (short rate, short *u, short *b, short *dq, short *bp);
  1958. ~~~~~~~~~~~~~
  1959. Description:
  1960. ~~~~~~~~~~~~
  1961. Update for coefficients of sixth order predictor.
  1962. Inputs: u, b, dq, rate
  1963. Output: bp
  1964. Return value: none.
  1965. ~~~~~~~~~~~~~
  1966. Prototype: in g726.h
  1967. ~~~~~~~~~~
  1968. History:
  1969. ~~~~~~~~
  1970. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  1971. <tdsindi@venus.cpqd.ansp.br>
  1972. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  1973. <tdsimao@venus.cpqd.ansp.br>
  1974. ----------------------------------------------------------------------
  1975. */
  1976. void G726_upb (short rate, short *u, short *b, short *dq, short *bp) {
  1977. short dqmag;
  1978. long bb, bs, ub;
  1979. long ugb, ulb;
  1980. short param;
  1981. short leak;
  1982. /* Preamble */
  1983. bb = *b & 65535;
  1984. dqmag = *dq & 32767;
  1985. if (rate != 5) {
  1986. leak = 8;
  1987. param = 65280;
  1988. } else {
  1989. leak = 9;
  1990. param = 65408;
  1991. }
  1992. /* gain is 0 or +/- (1/128) */
  1993. ugb = (dqmag == 0) ? 0 : ((*u == 0) ? 128 : 65408);
  1994. bs = (bb >> 15);
  1995. /* Leak factor is (1/256 or 1/512 for 40 kbit/s) */
  1996. ulb = (bs == 0) ? ((65536 - (bb >> leak)) & 65535) : ((65536 - ((bb >> leak) + param)) & 65535);
  1997. /* Compute update */
  1998. ub = (ugb + ulb) & 65535;
  1999. /* aux = bb + ub;*/
  2000. *bp = (short) ((bb + ub) & 65535);
  2001. }
  2002. /* ....................... end of G726_upb() ....................... */
  2003. /*
  2004. ----------------------------------------------------------------------
  2005. void G726_xor (short *dqn, short *dq, short *u);
  2006. ~~~~~~~~~~~~~
  2007. Description:
  2008. ~~~~~~~~~~~~
  2009. One bit "exclusive or" of sign of difference signal and sign
  2010. of delayed difference signal.
  2011. Inputs: dqn, dq
  2012. Output: u
  2013. Return value: none.
  2014. ~~~~~~~~~~~~~
  2015. Prototype: in g726.h
  2016. ~~~~~~~~~~
  2017. History:
  2018. ~~~~~~~~
  2019. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  2020. <tdsindi@venus.cpqd.ansp.br>
  2021. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  2022. <tdsimao@venus.cpqd.ansp.br>
  2023. ----------------------------------------------------------------------
  2024. */
  2025. void G726_xor (short *dqn, short *dq, short *u) {
  2026. short dqns;
  2027. short dqs;
  2028. dqs = (*dq >> 15) & 1;
  2029. dqns = (*dqn >> 10);
  2030. *u = (dqs ^ dqns);
  2031. }
  2032. /* ....................... end of G726_xor() ....................... */
  2033. /*
  2034. ----------------------------------------------------------------------
  2035. void G726_tone (short *a2p, short *tdp);
  2036. ~~~~~~~~~~~~~~
  2037. Description:
  2038. ~~~~~~~~~~~~
  2039. Partial band signal detection.
  2040. Inputs: a2p
  2041. Output: tdp
  2042. Return value: none.
  2043. ~~~~~~~~~~~~~
  2044. Prototype: in g726.h
  2045. ~~~~~~~~~~
  2046. History:
  2047. ~~~~~~~~
  2048. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  2049. <tdsindi@venus.cpqd.ansp.br>
  2050. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  2051. <tdsimao@venus.cpqd.ansp.br>
  2052. ----------------------------------------------------------------------
  2053. */
  2054. void G726_tone (short *a2p, short *tdp) {
  2055. long a2p1;
  2056. a2p1 = *a2p & 65535;
  2057. *tdp = (a2p1 >= 32768 && a2p1 < 53760) ? 1 : 0;
  2058. }
  2059. /* ....................... end of G726_tone() ....................... */
  2060. /*
  2061. ----------------------------------------------------------------------
  2062. void G726_trans (short *td, long *yl, short *dq, short *tr);
  2063. ~~~~~~~~~~~~~~~
  2064. Description:
  2065. ~~~~~~~~~~~~
  2066. Transition detector.
  2067. Inputs: td, yl, dq
  2068. Output: tr
  2069. Return value: none.
  2070. ~~~~~~~~~~~~~
  2071. Prototype: in g726.h
  2072. ~~~~~~~~~~
  2073. History:
  2074. ~~~~~~~~
  2075. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  2076. <tdsindi@venus.cpqd.ansp.br>
  2077. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  2078. <tdsimao@venus.cpqd.ansp.br>
  2079. ----------------------------------------------------------------------
  2080. */
  2081. void G726_trans (short *td, long *yl, short *dq, short *tr) {
  2082. short dqmag;
  2083. long dqthr;
  2084. short ylint;
  2085. long dqmag1;
  2086. short ylfrac;
  2087. long thr1, thr2;
  2088. dqmag = *dq & 32767;
  2089. ylint = (*yl >> 15);
  2090. ylfrac = (*yl >> 10) & 31;
  2091. thr1 = (ylfrac + 32) << ylint;
  2092. thr2 = (ylint > 9) ? 31744 : thr1;
  2093. dqthr = (thr2 + (thr2 >> 1)) >> 1;
  2094. dqmag1 = dqmag;
  2095. *tr = (dqmag1 > dqthr && *td == 1) ? 1 : 0;
  2096. }
  2097. /* ....................... end of G726_trans() ....................... */
  2098. /*
  2099. ----------------------------------------------------------------------
  2100. void G726_compress (short *sr, char *law, short *sp);
  2101. ~~~~~~~~~~~~~~~~~~
  2102. Description:
  2103. ~~~~~~~~~~~~
  2104. Convert from uniform pcm to either a-law or u-law pcm
  2105. Inputs: sr, law
  2106. Output: sp
  2107. Return value: none.
  2108. ~~~~~~~~~~~~~
  2109. Prototype: in g726.h
  2110. ~~~~~~~~~~
  2111. History:
  2112. ~~~~~~~~
  2113. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  2114. <tdsindi@venus.cpqd.ansp.br>
  2115. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  2116. <tdsimao@venus.cpqd.ansp.br>
  2117. 24.Jan.00 v2.0 Corrected im calculation that caused incorrect
  2118. processing of test vector ri40fa. Corrected
  2119. code provided by Jayesh Patel <jayesh@dspse.com>.
  2120. Verified by <simao.campos@labs.comsat.com>
  2121. ----------------------------------------------------------------------
  2122. */
  2123. void G726_compress (short *sr, char *law, short *sp) {
  2124. short imag, iesp, ofst;
  2125. short ofst1;
  2126. long i;
  2127. long im;
  2128. short is;
  2129. long srr;
  2130. is = (*sr >> 15);
  2131. srr = (*sr & 65535);
  2132. /* Convert 2-complement to signed magnitude */
  2133. im = (is == 0) ? srr : ((65536 - srr) & 32767);
  2134. /* Compress ... */
  2135. if (*law == '1') {
  2136. /* Next line added by J.Patel to fix a with test vector ri40fa.o */
  2137. im = (*sr == -32768) ? 2 : im; /* *** */
  2138. imag = (is == 0) ? (im >> 1) : ((im + 1) >> 1);
  2139. if (is)
  2140. --imag;
  2141. /* Saturation */
  2142. if (imag > 4095)
  2143. imag = 4095;
  2144. iesp = 7;
  2145. for (i = 1; i <= 7; ++i) {
  2146. imag += imag;
  2147. if (imag >= 4096)
  2148. break;
  2149. iesp = 7 - i;
  2150. }
  2151. imag &= 4095;
  2152. imag = (imag >> 8);
  2153. *sp = (is == 0) ? imag + (iesp << 4) : imag + (iesp << 4) + 128;
  2154. /* Sign bit inversion */
  2155. *sp ^= 128;
  2156. } else {
  2157. imag = im;
  2158. if (imag > 8158)
  2159. imag = 8158; /* Saturation */
  2160. ++imag;
  2161. iesp = 0;
  2162. ofst = 31;
  2163. if (imag > ofst) {
  2164. for (iesp = 1; iesp <= 8; ++iesp) {
  2165. ofst1 = ofst;
  2166. ofst += (1 << (iesp + 5));
  2167. if (imag <= ofst)
  2168. break;
  2169. }
  2170. imag -= ofst1 + 1;
  2171. }
  2172. imag /= (1 << (iesp + 1));
  2173. *sp = (is == 0) ? (imag + (iesp << 4)) : (imag + (iesp << 4) + 128);
  2174. /* Sign bit inversion */
  2175. *sp ^= 128;
  2176. *sp ^= 127;
  2177. }
  2178. }
  2179. /* ....................... end of G726_compress()....................... */
  2180. /*
  2181. ----------------------------------------------------------------------
  2182. void G726_sync (short rate, short *i, short *sp, short *dlnx,
  2183. ~~~~~~~~~~~~~~ short *dsx, char *law, short *sd);
  2184. Description:
  2185. ~~~~~~~~~~~~
  2186. Re-encode output pcm sample in decoder for synchronous tandem
  2187. coding.
  2188. Inputs: i, sp, dlnx, dsx, law, rate
  2189. Output: sd
  2190. Return value: none.
  2191. ~~~~~~~~~~~~~
  2192. Prototype: in g726.h
  2193. ~~~~~~~~~~
  2194. History:
  2195. ~~~~~~~~
  2196. 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
  2197. <tdsindi@venus.cpqd.ansp.br>
  2198. 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
  2199. <tdsimao@venus.cpqd.ansp.br>
  2200. ----------------------------------------------------------------------
  2201. */
  2202. void G726_sync (short rate, short *i, short *sp, short *dlnx, short *dsx, char *law, short *sd) {
  2203. short mask, id, im, is, ss;
  2204. if (rate == 4) {
  2205. is = (*i >> 3);
  2206. im = (is == 0) ? (*i + 8) : (*i & 7);
  2207. /* Find value of `id' as in Table 17/G.726 */
  2208. if (*dlnx >= 3972)
  2209. id = 9;
  2210. else if (*dlnx >= 2048)
  2211. id = 7;
  2212. else if (*dlnx >= 400)
  2213. id = 15;
  2214. else if (*dlnx >= 349)
  2215. id = 14;
  2216. else if (*dlnx >= 300)
  2217. id = 13;
  2218. else if (*dlnx >= 246)
  2219. id = 12;
  2220. else if (*dlnx >= 178)
  2221. id = 11;
  2222. else if (*dlnx >= 80)
  2223. id = 10;
  2224. else
  2225. id = 9;
  2226. /* Account for the negative part of the table */
  2227. if (*dsx)
  2228. id = 15 - id;
  2229. if (id == 8)
  2230. id = 7;
  2231. } /* ............... end of 32 kbit part * ................. */
  2232. else if (rate == 3) {
  2233. is = (*i >> 2);
  2234. im = (is == 0) ? (*i + 4) : (*i & 3);
  2235. /* Find value of `id' as in the Table 18/G.726 */
  2236. if (*dlnx >= 2048)
  2237. id = 3;
  2238. else if (*dlnx >= 331)
  2239. id = 7;
  2240. else if (*dlnx >= 218)
  2241. id = 6;
  2242. else if (*dlnx >= 8)
  2243. id = 5;
  2244. else if (*dlnx >= 0)
  2245. id = 3;
  2246. if (*dsx)
  2247. id = 7 - id;
  2248. if (id == 4)
  2249. id = 3;
  2250. } /* ............... end of 24 kbit part * ................. */
  2251. else if (rate == 2) {
  2252. is = (*i >> 1);
  2253. im = (is == 0) ? (*i + 2) : (*i & 1);
  2254. /* Find value of `id' as in the Table 19/G.726 */
  2255. if (*dlnx >= 2048)
  2256. id = 2;
  2257. else if (*dlnx >= 261)
  2258. id = 3;
  2259. else if (*dlnx >= 0)
  2260. id = 2;
  2261. if (*dsx)
  2262. id = 3 - id;
  2263. } /* ............... end of 16 kbit part * ................. */
  2264. else {
  2265. is = (*i >> 4);
  2266. im = (is == 0) ? (*i + 16) : (*i & 15);
  2267. /* Find value of `id' as in the Table 16/G.726 */
  2268. if (*dlnx >= 4080)
  2269. id = 18;
  2270. else if (*dlnx >= 3974)
  2271. id = 17;
  2272. else if (*dlnx >= 2048)
  2273. id = 15;
  2274. else if (*dlnx >= 553)
  2275. id = 31;
  2276. else if (*dlnx >= 528)
  2277. id = 30;
  2278. else if (*dlnx >= 502)
  2279. id = 29;
  2280. else if (*dlnx >= 475)
  2281. id = 28;
  2282. else if (*dlnx >= 445)
  2283. id = 27;
  2284. else if (*dlnx >= 413)
  2285. id = 26;
  2286. else if (*dlnx >= 378)
  2287. id = 25;
  2288. else if (*dlnx >= 339)
  2289. id = 24;
  2290. else if (*dlnx >= 298)
  2291. id = 23;
  2292. else if (*dlnx >= 250)
  2293. id = 22;
  2294. else if (*dlnx >= 198)
  2295. id = 21;
  2296. else if (*dlnx >= 139)
  2297. id = 20;
  2298. else if (*dlnx >= 68)
  2299. id = 19;
  2300. else if (*dlnx >= 0)
  2301. id = 18;
  2302. if (*dsx)
  2303. id = 31 - id;
  2304. if (id == 16)
  2305. id = 15;
  2306. } /* ............... end of 40 kbit part ................. */
  2307. /* Choose sd as sp, sp+ or sp- */
  2308. ss = (*sp & 128) >> 7;
  2309. mask = (*sp & 127);
  2310. if (*law == '1') { /* ......... A-law */
  2311. if (id > im && ss == 1 && mask == 0)
  2312. ss = 0;
  2313. else if (id > im && ss == 1 && mask != 0)
  2314. mask--;
  2315. else if (id > im && ss == 0 && mask != 127)
  2316. mask++;
  2317. else if (id < im && ss == 1 && mask != 127)
  2318. mask++;
  2319. else if (id < im && ss == 0 && mask == 0)
  2320. ss = 1;
  2321. else if (id < im && ss == 0 && mask != 0)
  2322. mask--;
  2323. } else { /* ......... u-law */
  2324. if (id > im && ss == 1 && mask == 127) {
  2325. ss = 0;
  2326. mask--;
  2327. } else if (id > im && ss == 1 && mask != 127)
  2328. mask++;
  2329. else if (id > im && ss == 0 && mask != 0)
  2330. mask--;
  2331. else if (id < im && ss == 1 && mask != 0)
  2332. mask--;
  2333. else if (id < im && ss == 0 && mask == 127)
  2334. ss = 1;
  2335. else if (id < im && ss == 0 && mask != 127)
  2336. mask++;
  2337. }
  2338. *sd = mask + (ss << 7);
  2339. }
  2340. /* ........................ end of G726_sync() ........................ */
  2341. /* ************************* END OF G726.C ************************* */