Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1080 lines
31 KiB

  1. /*#define __TEST
  2. #ifdef __TEST
  3. #include <stdio.h>
  4. FILE *d_codage;
  5. FILE *d_test;
  6. #endif*/
  7. /*
  8. * Project: Direct Subband 16000 bps coder
  9. * Workfile: sb_encod.c
  10. * Author: Alfred Wiesen
  11. * Created: 30 August 1995
  12. * Last update: 4 September 1995
  13. * DLL Version: 1.00
  14. * Revision: Single DLL for coder and decoder.
  15. * Comment:
  16. *
  17. * (C) Copyright 1993-95 Lernout & Hauspie Speech Products N.V. (TM)
  18. * All rights reserved. Company confidential.
  19. */
  20. // ------------------------------------------------------------------------
  21. // ------------------------------------------------------------------------
  22. // Included files
  23. // ------------------------------------------------------------------------
  24. // ------------------------------------------------------------------------
  25. #include <math.h>
  26. #include <windows.h>
  27. #include <windowsx.h>
  28. //#define USE_CRT_RAND 1
  29. #ifdef USE_CRT_RAND
  30. #include <stdlib.h> // for rand() function
  31. #endif
  32. #include "fv_x8.h"
  33. #include "data.h"
  34. #include "bib_32.h"
  35. // ------------------------------------------------------------------------
  36. // ------------------------------------------------------------------------
  37. // Function prototypes
  38. // ------------------------------------------------------------------------
  39. // ------------------------------------------------------------------------
  40. //------------------------------------------------------------------------
  41. void InitializeDecoderInstanceData(PVOID p, DWORD dwMaxBitRate);
  42. void interpolation_I(short low_input[],short coef[],short low_part_mem[],short order);
  43. void bruit_I(PD16008DATA p, short vec[],short max, short deb ,short fin);
  44. #if 0
  45. // PhilF: The following is never called!!!
  46. void dec_0a16_I2(short z1, short z2, short vec[], short maxv, short V1[], short V2[],long *code);
  47. #endif
  48. void dec_sous_bandes(PD16008DATA p,short *out,short *codes_max, long *codes_sb, short *indic_br/*, short *code_max_br*/);
  49. #if 0
  50. // PhilF: The following is not defined anywhere!!!
  51. void decodeframe(short codes_max[],long codes_sb[],short d_indic_sp[],char stream[]);
  52. #endif
  53. #ifdef CELP4800
  54. void demux(PD4808DATA p);
  55. void decode_ai(PD4808DATA p);
  56. void dec_ltp(PD4808DATA p,short no),dec_dic(PD4808DATA p);
  57. void post_synt(PD4808DATA p),post_filt(PD4808DATA p,short no);
  58. #endif
  59. /*void iConvert64To8(short *in, short *out, short N, short *mem);
  60. void iConvert8To64(short *in, short *out, short N, short *mem);
  61. void filt_in(short *mem, short *Vin, short *Vout, short lfen);
  62. //void PassHigh(short *vin,short *vout,short *mem,short nech);
  63. void BandPass(short *,short *,short *,short);*/
  64. // ------------------------------------------------------------------------
  65. // ------------------------------------------------------------------------
  66. // Global variables for decoder
  67. // ------------------------------------------------------------------------
  68. // ------------------------------------------------------------------------
  69. /*#define MAXDECODINGHANDLES 10
  70. // Instance data structure
  71. PD16008DATA pDecoderData;
  72. D16008DATA DecoderData[MAXDECODINGHANDLES];
  73. short brol[300];
  74. short DecodingHandles[MAXDECODINGHANDLES];*/
  75. // ROM tables :
  76. //extern long coef_outfil[];
  77. extern short coef_I[]; // QMF filter coefficients
  78. extern short V3_I[];
  79. extern short V4_I[];
  80. extern short V5_I[];
  81. extern short V6_I[];
  82. extern short V7_I[];
  83. extern short V8_I[];
  84. extern short V9_I[];
  85. extern short d_max_level[]; // Quantified maximum sample level
  86. extern long coeffs[];
  87. extern short quantif[];
  88. extern long Mask[];
  89. extern short tabcos[];
  90. extern short LSP_Q[];
  91. extern short TAB_DI[];
  92. extern short GV[];
  93. extern short BV[];
  94. extern long coef_i[];
  95. extern short NBB[],BITDD[];
  96. extern short LSP0ROM[];
  97. //extern short bytes[];
  98. //extern short bits[];
  99. // RAM variables
  100. /*extern char d_stream[];
  101. extern short synth_speech[];
  102. extern short d_DATA_I[]; // Intermediate vector = input and output of QMF
  103. extern short d_codes_max[];
  104. extern long d_codes_sb[];
  105. extern short d_indic_sp[];
  106. extern short d_num_bandes;
  107. //extern float d_vect6[256], d_vect8[256];*/
  108. // ------------------------------------------------------------------------
  109. // ------------------------------------------------------------------------
  110. // Function implementation
  111. // ------------------------------------------------------------------------
  112. // ------------------------------------------------------------------------
  113. // ------------------------------------------------------------------------
  114. void InitializeDecoderInstanceData(PVOID p, DWORD dwMaxBitRate)
  115. // Instance data initializations
  116. {
  117. short i;
  118. #ifdef CELP4800
  119. if (dwMaxBitRate == 4800)
  120. {
  121. ((PD4808DATA)p)->dwMaxBitRate = dwMaxBitRate;
  122. for (i=0;i<10;i++)
  123. ((PD4808DATA)p)->LSP0[i]=LSP0ROM[i];
  124. }
  125. else
  126. #endif
  127. {
  128. ((PD16008DATA)p)->dwMaxBitRate = dwMaxBitRate;
  129. ((PD16008DATA)p)->lRand = 1L;
  130. ((PD16008DATA)p)->quantif[0] = 9;
  131. ((PD16008DATA)p)->quantif[1] = 9;
  132. ((PD16008DATA)p)->quantif[4] = 5;
  133. ((PD16008DATA)p)->quantif[5] = 5;
  134. ((PD16008DATA)p)->quantif[6] = 5;
  135. ((PD16008DATA)p)->quantif[7] = 5;
  136. ((PD16008DATA)p)->quantif[8] = 5;
  137. ((PD16008DATA)p)->quantif[9] = 5;
  138. ((PD16008DATA)p)->bits[0] = 52;
  139. ((PD16008DATA)p)->bits[2] = 38;
  140. ((PD16008DATA)p)->bits[3] = 38;
  141. ((PD16008DATA)p)->bits[4] = 38;
  142. if (dwMaxBitRate == 16000)
  143. {
  144. ((PD16008DATA)p)->quantif[2] = 7;
  145. ((PD16008DATA)p)->quantif[3] = 7;
  146. ((PD16008DATA)p)->bits[1] = 46;
  147. }
  148. else
  149. {
  150. ((PD16008DATA)p)->quantif[2] = 9;
  151. ((PD16008DATA)p)->quantif[3] = 9;
  152. ((PD16008DATA)p)->quantif[10] = 5;
  153. ((PD16008DATA)p)->quantif[11] = 5;
  154. ((PD16008DATA)p)->bits[1] = 52;
  155. ((PD16008DATA)p)->bits[5] = 38;
  156. }
  157. }
  158. return;
  159. }
  160. //------------------------------------------------------------------------
  161. void interpolation_I(short low_input[],short coef[],short low_part_mem[],short order)
  162. // Purpose : from subbands stored in low_input[], create the corresponding signal
  163. // Remark : The reconstruct signal is stored at *(input+N_SB*L_RES)
  164. {
  165. short *output;
  166. short *high_input;
  167. short *buffer,*sa_vec;
  168. short lng,j,i;
  169. buffer = low_part_mem;
  170. for (i = 7-N_SB; i<7; i++)
  171. {
  172. lng = 1<<i;
  173. high_input=low_input+lng;
  174. output=low_input+L_RES;
  175. sa_vec=output;
  176. for (j = L_RES >> (i+1); j>0; j--)
  177. {
  178. low_part_mem=buffer;
  179. QMInverse(low_input,high_input,coef,output,low_part_mem,lng);
  180. output += 2*lng; low_input += lng; high_input += lng;
  181. if (j&1) high_input += 2*lng;
  182. else low_input += 2*lng;
  183. buffer += 2*order;
  184. }
  185. low_input=sa_vec;
  186. }
  187. }
  188. //------------------------------------------------------------------------
  189. void bruit_I(PD16008DATA p, short vec[],short max, short deb ,short fin)
  190. // rand() generates integers from 1 to RAND_MAX (32767)
  191. {
  192. short i;
  193. for (i=deb;i<fin;i++)
  194. {
  195. #ifdef USE_CRT_RAND
  196. *vec++ = (short)(((long)max*(long)(rand()-16384))>>15);
  197. #else
  198. // We provide our own rand() function in order
  199. // to go away from libcmt, msvcrt...
  200. p->lRand = p->lRand * 214013L + 2531011L;
  201. *vec++ = (short)(((long)max*(long)((long)((p->lRand >> 16) & 0x7fff)-16384))>>15);
  202. #endif
  203. } }
  204. /*void bruit_I(int vec[],int max, int deb ,int fin)
  205. {
  206. int i;
  207. for (i=deb;i<fin;i++) *vec++ = (int)(((long)max*(long)(rand()-16384))>>15);
  208. }*/
  209. //------------------------------------------------------------------------
  210. // PhilF: The following is never called!!!
  211. #if 0
  212. void dec_0a16_I2(short z1, short z2, short vec[], short maxv, short V1[], short V2[],long *code)
  213. // Decodes two long codes to retreive the z level quantified subband
  214. {
  215. // short vect1[16];
  216. short i,x;
  217. long result;
  218. // long lp1,lp2;
  219. result=*(code+1);
  220. for (i=15;i>=8;i--) // Decodes the 8 last samples of the subband
  221. {
  222. if (i==2*(short)(i/2))
  223. {
  224. x=result%z1;
  225. result-=x;
  226. result/=z1;
  227. *(vec+i)=(short)(((long)V1[x]*(long)maxv)>>13);
  228. }
  229. else
  230. {
  231. x=result%z2;
  232. result-=x;
  233. result/=z2;
  234. *(vec+i)=(short)(((long)V2[x]*(long)maxv)>>13);
  235. }
  236. }
  237. result=*(code);
  238. for (i=7;i>=0;i--) // Decodes the 8 first samples of the subband
  239. {
  240. if (i==2*(short)(i/2))
  241. {
  242. x=result%z1;
  243. result-=x;
  244. result/=z1;
  245. *(vec+i)=(short)(((long)V1[x]*(long)maxv)>>13);
  246. }
  247. else
  248. {
  249. x=result%z2;
  250. result-=x;
  251. result/=z2;
  252. *(vec+i)=(short)(((long)V2[x]*(long)maxv)>>13);
  253. }
  254. }
  255. }
  256. #endif
  257. void dec_0a16_I3(short z1, short z2, short vec[], short maxv, long *code)
  258. // Decodes two long codes to retreive the z level quantified subband
  259. {
  260. // short vect1[16];
  261. short i,x;
  262. long result;
  263. short *V1,*V2;
  264. // long lp1,lp2;
  265. switch (z1)
  266. {
  267. case 3: V1=V3_I; break;
  268. case 4: V1=V4_I; break;
  269. case 5: V1=V5_I; break;
  270. case 6: V1=V6_I; break;
  271. case 7: V1=V7_I; break;
  272. case 8: V1=V8_I; break;
  273. case 9: V1=V9_I; break;
  274. }
  275. switch (z2)
  276. {
  277. case 3: V2=V3_I; break;
  278. case 4: V2=V4_I; break;
  279. case 5: V2=V5_I; break;
  280. case 6: V2=V6_I; break;
  281. case 7: V2=V7_I; break;
  282. case 8: V2=V8_I; break;
  283. case 9: V2=V9_I; break;
  284. }
  285. result=*(code+1);
  286. if (z1 && z2)
  287. {
  288. for (i=15;i>=8;i--) // Decodes the 8 last samples of the subband
  289. {
  290. if (i==2*(short)(i/2))
  291. {
  292. x=result%z1;
  293. result-=x;
  294. result/=z1;
  295. *(vec+i)=(short)(((long)V1[x]*(long)maxv)>>13);
  296. }
  297. else
  298. {
  299. x=result%z2;
  300. result-=x;
  301. result/=z2;
  302. *(vec+i)=(short)(((long)V2[x]*(long)maxv)>>13);
  303. }
  304. }
  305. result=*(code);
  306. for (i=7;i>=0;i--) // Decodes the 8 first samples of the subband
  307. {
  308. if (i==2*(short)(i/2))
  309. {
  310. x=result%z1;
  311. result-=x;
  312. result/=z1;
  313. *(vec+i)=(short)(((long)V1[x]*(long)maxv)>>13);
  314. }
  315. else
  316. {
  317. x=result%z2;
  318. result-=x;
  319. result/=z2;
  320. *(vec+i)=(short)(((long)V2[x]*(long)maxv)>>13);
  321. }
  322. }
  323. }
  324. }
  325. //------------------------------------------------------------------------
  326. void dec_sous_bandes(PD16008DATA p,short *out,short *codes_max, long *codes_sb, short *d_indic_sp)
  327. // Decodes the 8 subbands
  328. {
  329. short max[8]={0,0,0,0,0,0,0,0};
  330. short max_loc[8]={0,0,0,0,0,0,0,0};
  331. short order[8]={0,0,0,0,0,0,0,0};
  332. short maximum,max_num;
  333. short i,j,nbsb_sp,ord;
  334. #ifdef MAX_SB_ABSOLU
  335. short sb_count;
  336. #endif
  337. for (i=0;i<8;i++) // Decodes the maximums
  338. {
  339. max_loc[i]=2*d_max_level[codes_max[i]];
  340. }
  341. nbsb_sp=0;
  342. j=0;
  343. for (i=0;i<8;i++)
  344. {
  345. if (d_indic_sp[i]==1)
  346. {
  347. max[i]=max_loc[j];
  348. nbsb_sp++;
  349. j++;
  350. }
  351. }
  352. if (p->dwMaxBitRate == 16000)
  353. {
  354. j=0;
  355. #ifdef MAX_SB_ABSOLU
  356. sb_count=nbsb_sp;
  357. if (sb_count>=MAX_SB_ABSOLU) return;
  358. #endif
  359. for (i=0;i<8;i++)
  360. {
  361. if (d_indic_sp[i]==0)
  362. {
  363. max[i]=max_loc[nbsb_sp+j];
  364. j++;
  365. #ifdef MAX_SB_ABSOLU
  366. sb_count++;
  367. quant_0a16_I3(SILENCE_QUANT_LEVEL_16000,SILENCE_QUANT_LEVEL_16000,in+i*16,max[i],codes_sb+2*sb_count);
  368. if (sb_count>=MAX_SB_ABSOLU) break;
  369. #endif
  370. }
  371. }
  372. }
  373. ord=8;
  374. for (i=0;i<8;i++) // Calculates the order of the subbands
  375. { // 1 is higher energy than 2 than 3,..
  376. maximum=32767;
  377. for (j=7;j>=0;j--)
  378. {
  379. if ((order[j]==0)&&(max[j]<maximum))
  380. {
  381. max_num=j; maximum=max[j];
  382. }
  383. }
  384. order[max_num]=ord;
  385. ord--;
  386. }
  387. if (p->dwMaxBitRate == 16000)
  388. {
  389. // On g�n�re les sous-bandes
  390. for (i=7;i>=nbsb_sp;i--)
  391. {
  392. j=0;
  393. while (order[j]!=i+1) j++;
  394. dec_0a16_I3(SILENCE_QUANT_LEVEL_16000,SILENCE_QUANT_LEVEL_16000,out+j*16,max[j],codes_sb+2*i);
  395. }
  396. }
  397. else
  398. {
  399. // On g�n�re du bruit
  400. if (nbsb_sp==0) maximum=20; // qd on ne doit generer que du bruit
  401. else
  402. {
  403. maximum=32767;
  404. for (i=0;i<nbsb_sp;i++) if (max_loc[i]<maximum) maximum=max_loc[i];
  405. maximum>>=2; // le 64eme du plus petit max transmis
  406. }
  407. //en fait il faudrait diminuer le bruit avec l'ordre
  408. for (i=0;i<8;i++) // Replaces the less energetic subbands
  409. { // with white noise
  410. if (d_indic_sp[i]==0)
  411. {
  412. maximum/=order[i];
  413. bruit_I(p,out+i*16,maximum,0,16);
  414. }
  415. }
  416. }
  417. for (i=nbsb_sp-1;i>=0;i--)
  418. {
  419. j=0;
  420. while (order[j]!=i+1) j++;
  421. dec_0a16_I3(p->quantif[2*i],p->quantif[2*i+1],out+j*16,max[j],codes_sb+2*i);
  422. }
  423. }
  424. //------------------------------------------------------------------------
  425. short Demultiplexing(
  426. char *Stream,
  427. long *Codes,
  428. short *CodeSizes,
  429. short NumCodes,
  430. short StreamSize)
  431. {
  432. short B,P; // B=bits � coder, P=bits disponibles
  433. short i,j;
  434. #ifdef __CHECK_FORMAT
  435. long TotalBytes=0;
  436. for (i=0;i<NumCodes;i++) TotalBytes+=CodeSizes[i];
  437. if (TotalBytes>StreamSize*8) return 1;
  438. #endif
  439. i=0;
  440. j=0;
  441. B=CodeSizes[i]; // bits � coder
  442. P=8; // 1 octet libre au d�part
  443. Codes[i]=0;
  444. while (i<NumCodes)
  445. {
  446. if (P>B)
  447. {
  448. Codes[i]|=(Stream[j]>>(P-B))&Mask[B];
  449. P-=B;
  450. i++;
  451. if (i<NumCodes)
  452. {
  453. B=CodeSizes[i];
  454. Codes[i]=0;
  455. }
  456. }
  457. else if (P<B)
  458. {
  459. Codes[i]|=(Stream[j]&Mask[P])<<(B-P);
  460. B-=P;
  461. P=8;
  462. j++;
  463. }
  464. else
  465. {
  466. Codes[i]|=Stream[j]&Mask[P];
  467. i++;
  468. j++;
  469. P=8;
  470. if (i<NumCodes)
  471. {
  472. B=CodeSizes[i];
  473. Codes[i]=0;
  474. }
  475. }
  476. }
  477. return 0;
  478. }
  479. // ------------------------------------------------------------------------
  480. #ifdef CELP4800
  481. void decode_ai(PD4808DATA p)
  482. {
  483. short_to_short(p->code,p->LSP,10);
  484. p->LSP[10]=32767;
  485. dec_lsp(p->LSP,LSP_Q,NBB,BITDD,TAB_DI);
  486. short_to_short(p->LSP,p->A3,10);
  487. teta_to_cos(tabcos,p->A3,10);
  488. lsp_to_ai(p->A3,p->TLSP,10);
  489. interpol(p->LSP0,p->LSP,p->A1,NETAGES);
  490. teta_to_cos(tabcos,p->A1,10);
  491. lsp_to_ai(p->A1,p->TLSP,10);
  492. interpol(p->LSP,p->LSP0,p->A2,NETAGES);
  493. teta_to_cos(tabcos,p->A2,10);
  494. lsp_to_ai(p->A2,p->TLSP,10);
  495. short_to_short(p->LSP,p->LSP0,NETAGES);
  496. }
  497. // ------------------------------------------------------------------------
  498. void dec_ltp(PD4808DATA p,short no)
  499. {
  500. short k;
  501. switch (no)
  502. {
  503. case 0:
  504. break;
  505. case 1:
  506. short_to_short(p->A2,p->A1,11);
  507. break;
  508. case 2:
  509. short_to_short(p->A3,p->A1,11);
  510. break;
  511. }
  512. p->PITCH=p->code[10+p->depl];
  513. k=p->code[11+p->depl];
  514. if (k<10) p->GLTP = BV[k+1]; /* les BV sont multiplies par 16384 */
  515. else p->GLTP = -BV[k-9];
  516. if (p->PITCH<p->SOULONG)
  517. {
  518. short_to_short(p->EE+lngEE-p->PITCH,p->E,p->PITCH);
  519. short_to_short(p->E,p->E+p->PITCH,(short)(p->SOULONG-p->PITCH));
  520. mult_fact(p->E,p->E,p->GLTP,p->SOULONG);
  521. }
  522. else
  523. {
  524. mult_fact(p->EE+lngEE-p->PITCH,p->E,p->GLTP,p->SOULONG);
  525. }
  526. }
  527. // ------------------------------------------------------------------------
  528. void dec_dic(PD4808DATA p)
  529. {
  530. short i,esp_opt,j,position,npopt,phas_opt,cod;
  531. short c[10];
  532. short Gopt;
  533. cod=p->code[13+p->depl];
  534. if (cod<16) Gopt=GV[cod+1];
  535. else Gopt=-GV[cod-15];
  536. cod=p->code[12+p->depl];
  537. if (cod<54) { position=cod; esp_opt=p->PITCH; }
  538. else
  539. {
  540. if (cod<64)
  541. {
  542. position=cod-54;
  543. if (p->PITCH<p->SOULONG) esp_opt=p->SOULONG+5;
  544. else if (p->PITCH/2<p->SOULONG) esp_opt=p->PITCH/2;
  545. else esp_opt=p->PITCH/3;
  546. }
  547. else
  548. {
  549. if (cod<128)
  550. {
  551. npopt=7;
  552. phas_opt=3;
  553. esp_opt=8;
  554. i=cod-64;
  555. c[0]=1;
  556. decode_dic(c,i,npopt);
  557. }
  558. else
  559. {
  560. npopt=8;
  561. phas_opt=0;
  562. esp_opt=7;
  563. i=cod-128;
  564. c[0]=1;
  565. decode_dic(c,i,npopt);
  566. }
  567. }
  568. }
  569. if (cod<64)
  570. {
  571. i=0;
  572. do
  573. {
  574. p->E[position+i] += Gopt;
  575. i += esp_opt;
  576. }
  577. while ((position+i)<p->SOULONG);
  578. }
  579. else
  580. for (j=0;j<npopt;j++)
  581. p->E[esp_opt*j+phas_opt] += c[j]*Gopt;
  582. short_to_short(p->EE+p->SOULONG,p->EE,(short)(lngEE - p->SOULONG));
  583. short_to_short(p->E,p->EE+lngEE-p->SOULONG,p->SOULONG);
  584. }
  585. // ------------------------------------------------------------------------
  586. void post_synt(PD4808DATA p)
  587. {
  588. short GPREF;
  589. if (abs(p->GLTP)<8192) GPREF = (long)p->GLTP*(long)35/100;
  590. if (p->GLTP>=8192) GPREF=2867;
  591. if (p->GLTP<=-8192) GPREF=-2867;
  592. if (p->PITCH>=p->SOULONG) mult_f_acc(p->EEE+lngEE-p->PITCH,p->E,GPREF,p->SOULONG);
  593. else
  594. {
  595. mult_f_acc(p->EEE+lngEE-p->PITCH,p->E,GPREF,p->PITCH);
  596. mult_f_acc(p->E,p->E+p->PITCH,GPREF,(short)(p->SOULONG-p->PITCH));
  597. }
  598. short_to_short(p->EEE+p->SOULONG,p->EEE,(short)(lngEE-p->SOULONG));
  599. short_to_short(p->E,p->EEE+lngEE-p->SOULONG,p->SOULONG);
  600. synthese(p->MSYNTH,p->A1,p->E,p->E,p->SOULONG,NETAGES);
  601. }
  602. // ------------------------------------------------------------------------
  603. void post_filt(PD4808DATA p,short no)
  604. {
  605. short i0;
  606. switch (no)
  607. {
  608. case 0: i0=0;
  609. break;
  610. case 1: i0=SOUDECAL1;
  611. break;
  612. case 2: i0=SOUDECAL1+SOUDECAL;
  613. break;
  614. }
  615. filt_iir(p->memfil,coef_i,p->E,p->ss+i0,p->SOULONG,4);
  616. }
  617. // ------------------------------------------------------------------------
  618. void demux(PD4808DATA p)
  619. // Purpose : deconcatenate the input stream
  620. // Input parameter :
  621. // input_stream[] : input stream
  622. // Output parameter :
  623. // code[] : separate parameter code
  624. //
  625. // Comments: The LTP or Adaptive codebook is also called PITCH.
  626. //
  627. // Stream format :
  628. // input_stream[0] = LSP[0] | LSP[1] | LSP[2] | (Binary gain 2)
  629. // input_stream[1] = LSP[3] | (Binary gain 3) | (Binary codebook 1)
  630. // input_stream[2] = LSP[4] | LSP[5] | LSP[6] | LSP[7] | LSP[8] | LSP[9]
  631. // input_stream[3] = (LTP codebook 1) | (LTP gain 1) | (Binary gain 1)
  632. // input_stream[4] = (LTP codebook 2) | (LTP gain 2) | (Binary codebook 2)
  633. // input_stream[5] = (LTP codebook 3) | (LTP gain 3) | (Binary codebook 3)
  634. //
  635. // Bit allocation : Codebook or gain "i" is the codebook for subframe "i".
  636. // code[0] = LSP(0) : 3bits code[10] = LTP codebook 1 : 7bits
  637. // code[1] = LSP(1) : 4bits code[11] = LTP gain 1 : 4bits
  638. // code[2] = LSP(2) : 4bits code[12] = Binary codebook 1 : 8bits
  639. // code[3] = LSP(3) : 3bits code[13] = Binary gain 1 : 5bits
  640. // code[4] = LSP(4) : 4bits code[14] = LTP codebook 2 : 4bits
  641. // code[5] = LSP(5) : 3bits code[15] = LTP gain 2 : 4bits
  642. // code[6] = LSP(6) : 3bits code[16] = Binary codebook 2 : 8bits
  643. // code[7] = LSP(7) : 2bits code[17] = Binary gain 2 : 5bits
  644. // code[8] = LSP(8) : 3bits code[18] = LTP codebook 3 : 4bits
  645. // code[9] = LSP(9) : 1bits code[19] = LTP gain 3 : 4bits
  646. // code[20] = Binary codebook 3 : 8bits
  647. // code[21] = Binary gain 3 : 5bits
  648. //
  649. {
  650. p->code[0] = (p->frame[0]>>13) & 0x0007;
  651. p->code[1] = (p->frame[0]>>9) & 0x000f;
  652. p->code[2] = (p->frame[0]>>5) & 0x000f;
  653. p->code[17] = p->frame[0] & 0x001f;
  654. p->code[3] = (p->frame[1]>>13) & 0x0007;
  655. p->code[21] = (p->frame[1]>>8) & 0x001f;
  656. p->code[12] = p->frame[1] & 0x00ff;
  657. p->code[4] = (p->frame[2]>>12) & 0x000f;
  658. p->code[5] = (p->frame[2]>>9) & 0x0007;
  659. p->code[6] = (p->frame[2]>>6) & 0x0007;
  660. p->code[7] = (p->frame[2]>>4) & 0x0003;
  661. p->code[8] = (p->frame[2]>>1) & 0x0007;
  662. p->code[9] = p->frame[2] & 0x0001;
  663. p->code[10] = (p->frame[3]>>9) & 0x007f;
  664. p->code[11] = (p->frame[3]>>5) & 0x000f;
  665. p->code[13] = p->frame[3] & 0x001f;
  666. p->code[14] = (p->frame[4]>>12) & 0x000f;
  667. p->code[15] = (p->frame[4]>>8) & 0x000f;
  668. p->code[16] = p->frame[4] & 0x00ff;
  669. p->code[18] = (p->frame[5]>>12) & 0x000f;
  670. p->code[19] = (p->frame[5]>>8) & 0x000f;
  671. p->code[20] = p->frame[5] & 0x00ff;
  672. p->code[10] += LIM_P1;
  673. p->code[14] = p->code[14]+p->code[10]-7;
  674. p->code[18] = p->code[18]+p->code[14]-7;
  675. }
  676. #endif
  677. // ------------------------------------------------------------------------
  678. // ------------------------------------------------------------------------
  679. // DLL entry points
  680. // ------------------------------------------------------------------------
  681. // ------------------------------------------------------------------------
  682. // ------------------------------------------------------------------------
  683. LH_PREFIX HANDLE LH_SUFFIX MSLHSB_Open_Decoder(DWORD dwMaxBitRate)
  684. {
  685. PVOID pDecoderData;
  686. /*short i,flag=0;
  687. // Test if there are free handles
  688. for (i=0;i<MAXDECODINGHANDLES;i++)
  689. if (DecodingHandles[i]==0) {DecodingHandles[i]=1; flag=1; break;}
  690. if (flag==0) return 0;
  691. pDecoderData=&DecoderData[i];*/
  692. // Check the input bit rate param.
  693. if (
  694. #ifdef CELP4800
  695. (dwMaxBitRate != 4800) &&
  696. #endif
  697. (dwMaxBitRate != 8000) &&
  698. (dwMaxBitRate != 12000) &&
  699. (dwMaxBitRate != 16000))
  700. return (HANDLE)0;
  701. // pDecoderData=(PVOID)GlobalAllocPtr(GMEM_MOVEABLE, dwMaxBitRate == 4800 ? sizeof(D4808DATA) : sizeof(D16008DATA));
  702. #ifdef CELP4800
  703. pDecoderData=(PVOID)GlobalAllocPtr(GHND, dwMaxBitRate == 4800 ? sizeof(D4808DATA) : sizeof(D16008DATA));
  704. #else
  705. pDecoderData=(PVOID)GlobalAllocPtr(GHND, sizeof(D16008DATA));
  706. #endif
  707. if (pDecoderData==NULL)
  708. return (HANDLE)0;
  709. InitializeDecoderInstanceData(pDecoderData, dwMaxBitRate);
  710. #ifdef __TEST
  711. d_codage=(FILE*)fopen("codage.dat","rb");
  712. d_test=(FILE*)fopen("codes_dec.dat","wt");
  713. #endif
  714. return((HANDLE)pDecoderData);
  715. }
  716. // ------------------------------------------------------------------------
  717. LH_PREFIX LH_ERRCODE LH_SUFFIX MSLHSB_Decode(
  718. HANDLE hAccess,
  719. LPBYTE lpSrcBuf,
  720. LPWORD lpSrcBufSize,
  721. LPBYTE lpDstBuf,
  722. LPWORD lpDstBufSize)
  723. {
  724. short i,iOutputSize,flag=0;
  725. char *input;
  726. char *int_ptr;
  727. unsigned short *ptr1;
  728. unsigned short *ptr3;
  729. long interm;
  730. short codesizes[24];
  731. long codes[24];
  732. short numcodes,temp;
  733. short bits_count;
  734. PVOID pDecoderData;
  735. if ((!hAccess) || (!lpSrcBuf) || (!lpDstBuf))
  736. return LH_EBADARG;
  737. /*// First check that the handle provided as argument is correct
  738. for (i=0;i<MAXDECODINGHANDLES;i++)
  739. if ((DecodingHandles[i]==1)&&(hAccess==(HANDLE)&DecoderData[i])) {flag=1; break;}
  740. if (flag==0) return LH_BADHANDLE;*/
  741. pDecoderData=(PVOID)hAccess;
  742. // Check the input bit rate param.
  743. if (
  744. #ifdef CELP4800
  745. (((PD4808DATA)pDecoderData)->dwMaxBitRate != 4800) &&
  746. #endif
  747. (((PD16008DATA)pDecoderData)->dwMaxBitRate != 8000) &&
  748. (((PD16008DATA)pDecoderData)->dwMaxBitRate != 12000) &&
  749. (((PD16008DATA)pDecoderData)->dwMaxBitRate != 16000))
  750. return (LH_ERRCODE)LH_EBADARG;
  751. #ifdef CELP4800
  752. if ((((PD4808DATA)pDecoderData)->dwMaxBitRate == 4800))
  753. {
  754. // then check the buffer sizes passed as argument.
  755. if ((*lpDstBufSize<2*NECHDECAL)||(*lpSrcBufSize<12))
  756. return (LH_ERRCODE)LH_EBADARG;
  757. *lpDstBufSize=2*NECHDECAL;
  758. *lpSrcBufSize=12;
  759. ptr1 = (unsigned short *)lpSrcBuf;
  760. ptr3 = (unsigned short *)&(((PD4808DATA)pDecoderData)->frame);
  761. for (i=6 ; i>0 ; i--) *ptr3++ = *ptr1++;
  762. demux(((PD4808DATA)pDecoderData));
  763. decode_ai(((PD4808DATA)pDecoderData));
  764. for (i=0;i<3;i++)
  765. {
  766. if (i==0) ((PD4808DATA)pDecoderData)->SOULONG=SOUDECAL1;
  767. else ((PD4808DATA)pDecoderData)->SOULONG=SOUDECAL;
  768. ((PD4808DATA)pDecoderData)->depl=4*i;
  769. dec_ltp((PD4808DATA)pDecoderData,i);
  770. dec_dic((PD4808DATA)pDecoderData);
  771. post_synt((PD4808DATA)pDecoderData);
  772. post_filt((PD4808DATA)pDecoderData,i);
  773. }
  774. ptr3 = (unsigned short *)&(((PD4808DATA)pDecoderData)->ss);
  775. ptr1 = (unsigned short *)lpDstBuf;
  776. for (i =160; i>0;i--) *ptr1++ = *ptr3++;
  777. }
  778. else
  779. #endif
  780. {
  781. // then check the buffer sizes passed as argument.
  782. switch (((PD16008DATA)pDecoderData)->dwMaxBitRate)
  783. {
  784. case 8000:
  785. if ((*lpSrcBufSize<1)||(*lpDstBufSize<2*160))
  786. return (LH_ERRCODE)LH_EBADARG;
  787. *lpDstBufSize=2*160;
  788. break;
  789. case 12000:
  790. case 16000:
  791. if ((*lpSrcBufSize<1)||(*lpDstBufSize<2*128))
  792. return (LH_ERRCODE)LH_EBADARG;
  793. *lpDstBufSize=2*128;
  794. break;
  795. }
  796. input = (char *)lpSrcBuf;
  797. int_ptr=(char *)(((PD16008DATA)pDecoderData)->d_stream);
  798. /*for (i=0;i<26;i++)
  799. *int_ptr++=*input++;*/
  800. *int_ptr++=*input++; // read d_stream[0]
  801. for (i=0;i<8;i++)
  802. ((PD16008DATA)pDecoderData)->d_indic_sp[i]=(short)((((PD16008DATA)pDecoderData)->d_stream[0]>>i)&0x01);
  803. ((PD16008DATA)pDecoderData)->d_num_bandes=0;
  804. for (i=0;i<8;i++)
  805. if (((PD16008DATA)pDecoderData)->d_indic_sp[i]==1)
  806. ((PD16008DATA)pDecoderData)->d_num_bandes++;
  807. bits_count=8;
  808. for (i=0;i<((PD16008DATA)pDecoderData)->d_num_bandes;i++)
  809. bits_count+=5+((PD16008DATA)pDecoderData)->bits[i];
  810. if (((PD16008DATA)pDecoderData)->dwMaxBitRate == 16000)
  811. {
  812. #ifdef MAX_SB_ABSOLU
  813. for (i=((PD16008DATA)pDecoderData)->d_num_bandes;i<MAX_SB_ABSOLU;i++)
  814. #else
  815. for (i=((PD16008DATA)pDecoderData)->d_num_bandes;i<8;i++)
  816. #endif
  817. bits_count+=5+SILENCE_CODING_BIT_16000;
  818. }
  819. //temp=bytes[d_num_bandes]; //9
  820. #if 0
  821. temp=(short)((float)bits_count/8.0+0.99);
  822. #else
  823. // We want to go away of libcmt, msvcrt... and
  824. // floating point is not really essential here...
  825. if (bits_count)
  826. temp=(short)((bits_count-1)/8+1);
  827. else
  828. temp=0;
  829. #endif
  830. if (*lpSrcBufSize<temp)
  831. return (LH_ERRCODE)LH_EBADARG;
  832. if ((((PD16008DATA)pDecoderData)->dwMaxBitRate == 16000) || ((((PD16008DATA)pDecoderData)->dwMaxBitRate == 8000) && (((PD16008DATA)pDecoderData)->d_num_bandes)) || ((((PD16008DATA)pDecoderData)->dwMaxBitRate == 12000) && (((PD16008DATA)pDecoderData)->d_num_bandes)))
  833. {
  834. for (i=0;i<temp-1;i++) // read 8 last bytes
  835. *int_ptr++=*input++;
  836. numcodes=0;
  837. for (i=0;i<24;i++) codesizes[i]=0;
  838. for (i=0;i<((PD16008DATA)pDecoderData)->d_num_bandes;i++)
  839. {
  840. codesizes[i]=5;
  841. codesizes[((PD16008DATA)pDecoderData)->d_num_bandes+2*i]=((PD16008DATA)pDecoderData)->bits[i]/2;
  842. codesizes[((PD16008DATA)pDecoderData)->d_num_bandes+2*i+1]=((PD16008DATA)pDecoderData)->bits[i]/2;
  843. numcodes+=3;
  844. }
  845. if (((PD16008DATA)pDecoderData)->dwMaxBitRate == 16000)
  846. {
  847. for (i=((PD16008DATA)pDecoderData)->d_num_bandes;i<8;i++)
  848. {
  849. codesizes[2*((PD16008DATA)pDecoderData)->d_num_bandes+i]=5;
  850. codesizes[8+2*i]=SILENCE_CODING_BIT_16000/2;
  851. codesizes[8+2*i+1]=SILENCE_CODING_BIT_16000/2;
  852. numcodes+=3;
  853. }
  854. }
  855. if (Demultiplexing(((PD16008DATA)pDecoderData)->d_stream+1,codes,codesizes,numcodes,(short)(temp-1)))
  856. return (LH_ERRCODE)LH_BADHANDLE;
  857. for (i=0;i<((PD16008DATA)pDecoderData)->d_num_bandes;i++)
  858. {
  859. ((PD16008DATA)pDecoderData)->d_codes_max[i]=(short)codes[i];
  860. ((PD16008DATA)pDecoderData)->d_codes_sb[2*i]=codes[((PD16008DATA)pDecoderData)->d_num_bandes+2*i];
  861. ((PD16008DATA)pDecoderData)->d_codes_sb[2*i+1]=codes[((PD16008DATA)pDecoderData)->d_num_bandes+2*i+1];
  862. }
  863. if (((PD16008DATA)pDecoderData)->dwMaxBitRate == 16000)
  864. {
  865. #ifdef MAX_SB_ABSOLU
  866. for (i=((PD16008DATA)pDecoderData)->d_num_bandes;i<MAX_SB_ABSOLU;i++)
  867. #else
  868. for (i=((PD16008DATA)pDecoderData)->d_num_bandes;i<8;i++)
  869. #endif
  870. {
  871. ((PD16008DATA)pDecoderData)->d_codes_max[i]=(short)codes[2*((PD16008DATA)pDecoderData)->d_num_bandes+i];
  872. ((PD16008DATA)pDecoderData)->d_codes_sb[2*i]=codes[8+2*i];
  873. ((PD16008DATA)pDecoderData)->d_codes_sb[2*i+1]=codes[8+2*i+1];
  874. }
  875. }
  876. }
  877. *lpSrcBufSize=temp;
  878. dec_sous_bandes(((PD16008DATA)pDecoderData),((PD16008DATA)pDecoderData)->d_DATA_I,((PD16008DATA)pDecoderData)->d_codes_max,((PD16008DATA)pDecoderData)->d_codes_sb,((PD16008DATA)pDecoderData)->d_indic_sp);
  879. interpolation_I(((PD16008DATA)pDecoderData)->d_DATA_I,coef_I,((PD16008DATA)pDecoderData)->QMF_MEM_SYNT_I,Fil_Lenght);
  880. for (i=0;i<128;i++) ((PD16008DATA)pDecoderData)->d_DATA_I[3*L_RES+i]*=8; //TEST 16; // Because input divided before coding
  881. switch (((PD16008DATA)pDecoderData)->dwMaxBitRate)
  882. {
  883. case 8000:
  884. iOutputSize = 160;
  885. #ifdef _X86_
  886. iConvert64To8(((PD16008DATA)pDecoderData)->d_DATA_I+3*L_RES, ((PD16008DATA)pDecoderData)->synth_speech, 128, ((PD16008DATA)pDecoderData)->imem2);
  887. PassLow8(((PD16008DATA)pDecoderData)->synth_speech, ((PD16008DATA)pDecoderData)->synth_speech,((PD16008DATA)pDecoderData)->out_mem2,160);
  888. #else
  889. SampleRate6400To8000(((PD16008DATA)pDecoderData)->d_DATA_I+3*L_RES,
  890. ((PD16008DATA)pDecoderData)->synth_speech,
  891. 128,
  892. ((PD16008DATA)pDecoderData)->imem2,
  893. &((PD16008DATA)pDecoderData)->uiDelayPosition,
  894. &((PD16008DATA)pDecoderData)->iInputStreamTime,
  895. &((PD16008DATA)pDecoderData)->iOutputStreamTime );
  896. #endif
  897. break;
  898. case 12000:
  899. case 16000:
  900. iOutputSize = 128;
  901. for (i=0;i<128;i++)
  902. ((PD16008DATA)pDecoderData)->synth_speech[i]=((PD16008DATA)pDecoderData)->d_DATA_I[3*L_RES+i];
  903. break;
  904. }
  905. for (i=0;i<iOutputSize;i++)
  906. {
  907. interm=((long)((PD16008DATA)pDecoderData)->synth_speech[i] * 2L);//VERS 4 + ( ((long)(rand()-16384))>>8 ) ;
  908. if (interm>32700L) interm=32700L;
  909. if (interm<-32700L) interm=-32700L;
  910. ((PD16008DATA)pDecoderData)->synth_speech[i] = (short)interm ;
  911. }
  912. ptr3 = (unsigned short *)&(((PD16008DATA)pDecoderData)->synth_speech);
  913. ptr1 = (unsigned short *)lpDstBuf;
  914. for (i =0;i<iOutputSize;i++) ptr1[i] = ptr3[i];
  915. }
  916. return (LH_SUCCESS);
  917. }
  918. // ------------------------------------------------------------------------
  919. LH_PREFIX LH_ERRCODE LH_SUFFIX MSLHSB_Close_Decoder(HANDLE hAccess)
  920. {
  921. PVOID pDecoderData;
  922. /*short i,flag=0;
  923. // Check if right handle
  924. for (i=0;i<MAXDECODINGHANDLES;i++)
  925. if ((DecodingHandles[i]==1)&&(hAccess==(HANDLE)&DecoderData[i])) {flag=1; break;}
  926. if (flag==0) return LH_BADHANDLE;
  927. // Free handle
  928. DecodingHandles[i]=0;*/
  929. if (!hAccess)
  930. return LH_EBADARG;
  931. pDecoderData=(PVOID)hAccess;
  932. GlobalFreePtr(pDecoderData);
  933. #ifdef __TEST
  934. fclose(d_codage);
  935. fclose(d_test);
  936. #endif
  937. return LH_SUCCESS;
  938. }