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.

1540 lines
43 KiB

  1. /*
  2. * @DEC_COPYRIGHT@
  3. */
  4. /*
  5. * HISTORY
  6. * $Log: sa_api.c,v $
  7. * Revision 1.1.8.6 1996/11/25 18:21:14 Hans_Graves
  8. * Fix compile warnings under unix.
  9. * [1996/11/25 18:21:00 Hans_Graves]
  10. *
  11. * Revision 1.1.8.5 1996/11/14 21:49:21 Hans_Graves
  12. * AC3 buffering fixes.
  13. * [1996/11/14 21:45:14 Hans_Graves]
  14. *
  15. * Revision 1.1.8.4 1996/11/13 16:10:44 Hans_Graves
  16. * AC3 frame size calculation change.
  17. * [1996/11/13 15:53:44 Hans_Graves]
  18. *
  19. * Revision 1.1.8.3 1996/11/08 21:50:27 Hans_Graves
  20. * Added AC3 support.
  21. * [1996/11/08 21:08:35 Hans_Graves]
  22. *
  23. * Revision 1.1.8.2 1996/09/18 23:45:23 Hans_Graves
  24. * Add some some MPEG memory freeing
  25. * [1996/09/18 21:42:12 Hans_Graves]
  26. *
  27. * Revision 1.1.6.8 1996/04/23 21:01:38 Hans_Graves
  28. * Added SaDecompressQuery() and SaCompressQuery()
  29. * [1996/04/23 20:57:47 Hans_Graves]
  30. *
  31. * Revision 1.1.6.7 1996/04/17 16:38:31 Hans_Graves
  32. * Add casts where ScBitBuf_t and ScBitString_t types are used
  33. * [1996/04/17 16:34:14 Hans_Graves]
  34. *
  35. * Revision 1.1.6.6 1996/04/15 14:18:32 Hans_Graves
  36. * Change proto for SaCompress() - returns bytes processed
  37. * [1996/04/15 14:10:27 Hans_Graves]
  38. *
  39. * Revision 1.1.6.5 1996/04/10 21:46:51 Hans_Graves
  40. * Added SaGet/SetParam functions
  41. * [1996/04/10 21:25:16 Hans_Graves]
  42. *
  43. * Revision 1.1.6.4 1996/04/09 16:04:23 Hans_Graves
  44. * Remove warnings under NT
  45. * [1996/04/09 15:55:26 Hans_Graves]
  46. *
  47. * Revision 1.1.6.3 1996/03/29 22:20:48 Hans_Graves
  48. * Added MPEG_SUPPORT and GSM_SUPPORT
  49. * [1996/03/29 21:51:24 Hans_Graves]
  50. *
  51. * Revision 1.1.6.2 1996/03/08 18:46:05 Hans_Graves
  52. * Removed debugging printf
  53. * [1996/03/08 18:42:52 Hans_Graves]
  54. *
  55. * Revision 1.1.4.5 1996/02/22 21:55:04 Bjorn_Engberg
  56. * Removed a compiler warning on NT.
  57. * [1996/02/22 21:54:39 Bjorn_Engberg]
  58. *
  59. * Revision 1.1.4.4 1996/02/06 22:53:51 Hans_Graves
  60. * Moved ScBSReset() from DecompressBegin() to DecompressEnd(). Disabled FRAME callbacks.
  61. * [1996/02/06 22:19:16 Hans_Graves]
  62. *
  63. * Revision 1.1.4.3 1996/01/19 15:29:27 Bjorn_Engberg
  64. * Removed compiler wanrnings for NT.
  65. * [1996/01/19 15:03:46 Bjorn_Engberg]
  66. *
  67. * Revision 1.1.4.2 1996/01/15 16:26:18 Hans_Graves
  68. * Added SaSetBitrate(). SOme MPEG Audio encoding fix-ups
  69. * [1996/01/15 16:07:48 Hans_Graves]
  70. *
  71. * Revision 1.1.2.7 1995/07/21 17:40:57 Hans_Graves
  72. * Renamed Callback related stuff.
  73. * [1995/07/21 17:25:44 Hans_Graves]
  74. *
  75. * Revision 1.1.2.6 1995/06/27 17:40:57 Hans_Graves
  76. * Removed include <mmsystem.h>.
  77. * [1995/06/27 17:32:20 Hans_Graves]
  78. *
  79. * Revision 1.1.2.5 1995/06/27 13:54:14 Hans_Graves
  80. * Added GSM Encoding and Decoding
  81. * [1995/06/26 21:04:12 Hans_Graves]
  82. *
  83. * Revision 1.1.2.4 1995/06/09 18:33:27 Hans_Graves
  84. * Added SaGetInputBitstream().
  85. * [1995/06/09 18:32:35 Hans_Graves]
  86. *
  87. * Revision 1.1.2.3 1995/06/07 19:34:39 Hans_Graves
  88. * Enhanced sa_GetMpegAudioInfo().
  89. * [1995/06/07 19:33:25 Hans_Graves]
  90. *
  91. * Revision 1.1.2.2 1995/05/31 18:07:17 Hans_Graves
  92. * Inclusion in new SLIB location.
  93. * [1995/05/31 17:28:50 Hans_Graves]
  94. *
  95. * Revision 1.1.2.3 1995/04/17 18:47:31 Hans_Graves
  96. * Added MPEG Compression functionality
  97. * [1995/04/17 18:47:00 Hans_Graves]
  98. *
  99. * Revision 1.1.2.2 1995/04/07 19:55:45 Hans_Graves
  100. * Inclusion in SLIB
  101. * [1995/04/07 19:55:15 Hans_Graves]
  102. *
  103. * $EndLog$
  104. */
  105. /*****************************************************************************
  106. ** Copyright (c) Digital Equipment Corporation, 1995 **
  107. ** **
  108. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  109. ** laws of the United States. **
  110. ** **
  111. ** The software contained on this media is proprietary to and embodies **
  112. ** the confidential technology of Digital Equipment Corporation. **
  113. ** Possession, use, duplication or dissemination of the software and **
  114. ** media is authorized only pursuant to a valid written license from **
  115. ** Digital Equipment Corporation. **
  116. ** **
  117. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  118. ** Government is subject to restrictions as set forth in Subparagraph **
  119. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  120. ******************************************************************************/
  121. /*
  122. #define _DEBUG_
  123. #define _VERBOSE_
  124. */
  125. #include <stdio.h>
  126. #include <stdlib.h>
  127. #include <string.h>
  128. #include "SC.h"
  129. #include "SC_err.h"
  130. #include "SA.h"
  131. #ifdef MPEG_SUPPORT
  132. #include "sa_mpeg.h"
  133. #endif /* MPEG_SUPPORT */
  134. #ifdef GSM_SUPPORT
  135. #include "sa_gsm.h"
  136. #endif /* GSM_SUPPORT */
  137. #ifdef AC3_SUPPORT
  138. #include "sa_ac3.h"
  139. #endif /* AC3_SUPPORT */
  140. #include "sa_intrn.h"
  141. #include "sa_proto.h"
  142. #ifdef MPEG_SUPPORT
  143. static int MPEGAudioFilter(ScBitstream_t *bs)
  144. {
  145. int type, stat=NoErrors;
  146. unsigned dword PacketStartCode;
  147. ScBSPosition_t PacketStart, PacketLength=0;
  148. while (!bs->EOI)
  149. {
  150. if ((int)ScBSPeekBits(bs, MPEG_SYNC_WORD_LEN)==MPEG_SYNC_WORD)
  151. {
  152. ScBSSetFilter(bs, NULL);
  153. return(0);
  154. }
  155. PacketStartCode=(unsigned int)ScBSGetBits(bs, PACKET_START_CODE_PREFIX_LEN);
  156. if (PacketStartCode!=PACKET_START_CODE_PREFIX) {
  157. fprintf(stderr,"Packet cannot be located at Byte pos 0x%X; got 0x%X\n",
  158. ScBSBytePosition(bs),PacketStartCode);
  159. bs->EOI=TRUE;
  160. return(-1);
  161. }
  162. type=(int)ScBSGetBits(bs, 8);
  163. switch (type)
  164. {
  165. case AUDIO_STREAM_BASE:
  166. PacketLength=(unsigned int)ScBSGetBits(bs, 16)*8;
  167. PacketStart=ScBSBitPosition(bs);
  168. sc_dprintf("Audio Packet Start=0x%X Length=0x%X (0x%X)\n",
  169. PacketStart/8, PacketLength/8, PacketLength/8);
  170. while (ScBSPeekBits(bs, 8)==0xFF) /* Stuffing bytes */
  171. ScBSSkipBits(bs, 8);
  172. if (ScBSPeekBits(bs, 2)==1) /* STD_buffer stuff */
  173. ScBSSkipBits(bs, 2*8);
  174. if (ScBSPeekBits(bs, 4)==2) /* Time Stamps */
  175. ScBSSkipBits(bs, 5*8);
  176. else if (ScBSPeekBits(bs, 4)==3) /* Time Stamps */
  177. ScBSSkipBits(bs, 10*8);
  178. else if (ScBSGetBits(bs, 8)!=0x0F)
  179. fprintf(stderr, "Last byte before data not 0x0F at pos 0x%X\n",
  180. ScBSBytePosition(bs));
  181. return((int)(PacketStart+PacketLength));
  182. break;
  183. case PACK_START_BASE:
  184. sc_dprintf("Pack Start=0x%X Length=0x%X\n",
  185. ScBSBytePosition(bs), 8);
  186. ScBSSkipBits(bs, 8*8);
  187. break;
  188. default:
  189. PacketLength=(unsigned int)ScBSGetBits(bs, 16)*8;
  190. ScBSSkipBits(bs, (unsigned int)PacketLength);
  191. break;
  192. }
  193. }
  194. return(0);
  195. }
  196. #endif /* MPEG_SUPPORT */
  197. /*
  198. ** Name: SaOpenCodec
  199. ** Purpose: Open the specified codec. Return stat code.
  200. **
  201. ** Args: CodecType = SA_MPEG_ENCODE & SA_MPEG_DECODE are the only
  202. ** recognized codec for now.
  203. ** Sah = handle to software codec's Info structure.
  204. */
  205. SaStatus_t SaOpenCodec (SaCodecType_e CodecType, SaHandle_t *Sah)
  206. {
  207. int stat;
  208. SaCodecInfo_t *Info = NULL;
  209. if ((CodecType != SA_PCM_DECODE)
  210. && (CodecType != SA_PCM_ENCODE)
  211. #ifdef MPEG_SUPPORT
  212. && (CodecType != SA_MPEG_DECODE)
  213. && (CodecType != SA_MPEG_ENCODE)
  214. #endif /* MPEG_SUPPORT */
  215. #ifdef GSM_SUPPORT
  216. && (CodecType != SA_GSM_DECODE)
  217. && (CodecType != SA_GSM_ENCODE)
  218. #endif /* GSM_SUPPORT */
  219. #ifdef AC3_SUPPORT
  220. && (CodecType != SA_AC3_DECODE)
  221. /* && (CodecType != SA_AC3_ENCODE) */
  222. #endif /* AC3_SUPPORT */
  223. #ifdef G723_SUPPORT
  224. && (CodecType != SA_G723_DECODE)
  225. && (CodecType != SA_G723_ENCODE)
  226. #endif /* G723_SUPPORT */
  227. )
  228. return(SaErrorCodecType);
  229. if (!Sah)
  230. return (SaErrorBadPointer);
  231. /*
  232. ** Allocate memory for the Codec Info structure:
  233. */
  234. if ((Info = (SaCodecInfo_t *)ScAlloc(sizeof(SaCodecInfo_t))) == NULL)
  235. return (SaErrorMemory);
  236. Info->Type = CodecType;
  237. Info->CallbackFunction=NULL;
  238. Info->BSIn = NULL;
  239. Info->BSOut = NULL;
  240. stat = ScBufQueueCreate(&Info->Q);
  241. if (stat != NoErrors)
  242. return(stat);
  243. /*
  244. ** Allocate memory for Info structure and clear it
  245. */
  246. switch(CodecType)
  247. {
  248. case SA_PCM_DECODE:
  249. case SA_PCM_ENCODE:
  250. break;
  251. #ifdef MPEG_SUPPORT
  252. case SA_MPEG_DECODE:
  253. {
  254. SaMpegDecompressInfo_t *MDInfo;
  255. if ((MDInfo = (SaMpegDecompressInfo_t *)
  256. ScAlloc (sizeof(SaMpegDecompressInfo_t))) == NULL)
  257. return(SaErrorMemory);
  258. Info->MDInfo = MDInfo;
  259. stat = sa_InitMpegDecoder (Info);
  260. RETURN_ON_ERROR(stat);
  261. }
  262. break;
  263. case SA_MPEG_ENCODE:
  264. {
  265. SaMpegCompressInfo_t *MCInfo;
  266. if ((MCInfo = (SaMpegCompressInfo_t *)
  267. ScAlloc (sizeof(SaMpegCompressInfo_t))) == NULL)
  268. return(SaErrorMemory);
  269. Info->MCInfo = MCInfo;
  270. stat = sa_InitMpegEncoder (Info);
  271. RETURN_ON_ERROR(stat);
  272. }
  273. break;
  274. #endif /* MPEG_SUPPORT */
  275. #ifdef AC3_SUPPORT
  276. case SA_AC3_DECODE:
  277. /* case SA_AC3_ENCODE: */
  278. {
  279. SaAC3DecompressInfo_t *AC3Info;
  280. if ((AC3Info = (SaAC3DecompressInfo_t *)
  281. ScAlloc (sizeof(SaAC3DecompressInfo_t))) == NULL)
  282. return(SaErrorMemory);
  283. Info->AC3Info = AC3Info;
  284. /* Initialize Dolby subroutine */
  285. stat = sa_InitAC3Decoder(Info);
  286. }
  287. break;
  288. #endif /* AC3_SUPPORT */
  289. #ifdef GSM_SUPPORT
  290. case SA_GSM_DECODE:
  291. case SA_GSM_ENCODE:
  292. {
  293. SaGSMInfo_t *GSMInfo;
  294. if ((GSMInfo = (SaGSMInfo_t *)ScAlloc (sizeof(SaGSMInfo_t)))==NULL)
  295. return(SaErrorMemory);
  296. Info->GSMInfo = GSMInfo;
  297. stat = sa_InitGSM(GSMInfo);
  298. RETURN_ON_ERROR(stat);
  299. }
  300. break;
  301. #endif /* GSM_SUPPORT */
  302. #ifdef G723_SUPPORT
  303. case SA_G723_DECODE:
  304. {
  305. SaG723Info_t *pSaG723Info;
  306. if ((pSaG723Info = (SaG723Info_t *)
  307. ScAlloc (sizeof(SaG723Info_t))) == NULL)
  308. return(SaErrorMemory);
  309. Info->pSaG723Info = pSaG723Info;
  310. saG723DecompressInit(pSaG723Info);
  311. }
  312. break;
  313. case SA_G723_ENCODE:
  314. {
  315. SaG723Info_t *pSaG723Info;
  316. if ((pSaG723Info = (SaG723Info_t *)
  317. ScAlloc (sizeof(SaG723Info_t))) == NULL)
  318. return(SaErrorMemory);
  319. Info->pSaG723Info = pSaG723Info;
  320. saG723CompressInit(pSaG723Info);
  321. SaSetParamInt((SaHandle_t)Info, SA_PARAM_BITRATE, 6400);
  322. }
  323. break;
  324. #endif /* G723_SUPPORT */
  325. default:
  326. return(SaErrorCodecType);
  327. }
  328. *Sah = (SaHandle_t) Info; /* Return handle */
  329. Info->wfIn=NULL;
  330. Info->wfOut=NULL;
  331. return(NoErrors);
  332. }
  333. /*
  334. ** Name: SaCloseCodec
  335. ** Purpose: Closes the specified codec. Free the Info structure
  336. **
  337. ** Args: Sah = handle to software codec's Info structure.
  338. **
  339. */
  340. SaStatus_t SaCloseCodec (SaHandle_t Sah)
  341. {
  342. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  343. if (!Info)
  344. return(SaErrorCodecHandle);
  345. ScBufQueueDestroy(Info->Q);
  346. switch (Info->Type)
  347. {
  348. #ifdef MPEG_SUPPORT
  349. case SA_MPEG_DECODE:
  350. if (Info->MDInfo)
  351. {
  352. sa_EndMpegDecoder(Info);
  353. ScFree(Info->MDInfo);
  354. }
  355. break;
  356. case SA_MPEG_ENCODE:
  357. if (Info->MCInfo)
  358. {
  359. sa_EndMpegEncoder(Info);
  360. ScFree(Info->MCInfo);
  361. }
  362. break;
  363. #endif /* MPEG_SUPPORT */
  364. #ifdef AC3_SUPPORT
  365. case SA_AC3_DECODE:
  366. /* case SA_AC3_ENCODE: */
  367. sa_EndAC3Decoder(Info);
  368. if (Info->AC3Info)
  369. ScFree(Info->AC3Info);
  370. break;
  371. #endif /* AC3_SUPPORT */
  372. #ifdef GSM_SUPPORT
  373. case SA_GSM_DECODE:
  374. case SA_GSM_ENCODE:
  375. if (Info->GSMInfo)
  376. ScFree(Info->GSMInfo);
  377. break;
  378. #endif /* GSM_SUPPORT */
  379. #ifdef G723_SUPPORT
  380. case SA_G723_DECODE:
  381. if (Info->pSaG723Info)
  382. {
  383. saG723DecompressFree(Info->pSaG723Info);
  384. ScFree(Info->pSaG723Info);
  385. }
  386. break;
  387. case SA_G723_ENCODE:
  388. if (Info->pSaG723Info)
  389. {
  390. saG723CompressFree(Info->pSaG723Info);
  391. ScFree(Info->pSaG723Info);
  392. }
  393. break;
  394. #endif /* G723_SUPPORT */
  395. }
  396. if (Info->wfIn)
  397. ScFree(Info->wfIn);
  398. if (Info->wfOut)
  399. ScFree(Info->wfOut);
  400. /*
  401. ** Free Info structure
  402. */
  403. if (Info->BSIn)
  404. ScBSDestroy(Info->BSIn);
  405. if (Info->BSOut)
  406. ScBSDestroy(Info->BSOut);
  407. ScFree(Info);
  408. return(NoErrors);
  409. }
  410. /*
  411. ** Name: SaRegisterCallback
  412. ** Purpose: Specify the user-function that will be called during processing
  413. ** to determine if the codec should abort the frame.
  414. ** Args: Sah = handle to software codec's Info structure.
  415. ** Callback = callback function to register
  416. **
  417. */
  418. SaStatus_t SaRegisterCallback (SaHandle_t Sah,
  419. int (*Callback)(SaHandle_t, SaCallbackInfo_t *, SaInfo_t *),
  420. void *UserData)
  421. {
  422. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  423. if (!Info)
  424. return(SaErrorCodecHandle);
  425. if (!Callback)
  426. return(SaErrorBadPointer);
  427. Info->CallbackFunction = Callback;
  428. if (Info->BSIn)
  429. {
  430. Info->BSIn->Callback=(int (*)(ScHandle_t, ScCallbackInfo_t *, void *))Callback;
  431. Info->BSIn->UserData=UserData;
  432. }
  433. if (Info->BSOut)
  434. {
  435. Info->BSOut->Callback=(int (*)(ScHandle_t, ScCallbackInfo_t *, void *))Callback;
  436. Info->BSOut->UserData=UserData;
  437. }
  438. return(NoErrors);
  439. }
  440. /*
  441. ** Name: SaGetInputBitstream
  442. ** Purpose: Returns the current input bitstream being used by
  443. ** the Codec.
  444. ** Return: NULL if there no associated bitstream
  445. */
  446. ScBitstream_t *SaGetInputBitstream (SaHandle_t Sah)
  447. {
  448. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  449. if (Info)
  450. return(Info->BSIn);
  451. return(NULL);
  452. }
  453. /***************************** Decompression *******************************/
  454. /*
  455. ** Name: SaDecompressQuery
  456. ** Purpose: Check if input and output formats are supported.
  457. */
  458. SaStatus_t SaDecompressQuery(SaHandle_t Sah, WAVEFORMATEX *wfIn,
  459. WAVEFORMATEX *wfOut)
  460. {
  461. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  462. /*
  463. * This stuff should really be pushed down to the individual codecs
  464. * unless it has to be here - tfm
  465. */
  466. if (!Info)
  467. return(SaErrorCodecHandle);
  468. if (wfIn)
  469. {
  470. if (wfIn->nChannels!=1 && wfIn->nChannels!=2)
  471. return(SaErrorUnrecognizedFormat);
  472. }
  473. if (wfOut)
  474. {
  475. if (wfOut->wFormatTag != WAVE_FORMAT_PCM)
  476. return(SaErrorUnrecognizedFormat);
  477. if (wfOut->nChannels!=1 && wfOut->nChannels!=2 && wfOut->nChannels!=4)
  478. return(SaErrorUnrecognizedFormat);
  479. }
  480. if (wfIn && wfOut)
  481. {
  482. if (wfIn->nSamplesPerSec != wfOut->nSamplesPerSec)
  483. return(SaErrorUnrecognizedFormat);
  484. if (wfIn->wBitsPerSample !=16 &&
  485. (wfOut->wBitsPerSample !=16 || wfOut->wBitsPerSample !=8))
  486. return(SaErrorUnrecognizedFormat);
  487. }
  488. return(SaErrorNone);
  489. }
  490. /*
  491. ** Name: SaDecompressBegin
  492. ** Purpose: Initialize the Decompression Codec. Call after SaOpenCodec &
  493. ** before SaDecompress (SaDecompress will call SaDecompressBegin
  494. ** on first call to codec after open if user doesn't call it)
  495. **
  496. ** Args: Sah = handle to software codec's Info structure.
  497. ** wfIn = format of input (compressed) audio
  498. ** wfOut = format of output (uncompressed) audio
  499. */
  500. SaStatus_t SaDecompressBegin (SaHandle_t Sah, WAVEFORMATEX *wfIn,
  501. WAVEFORMATEX *wfOut)
  502. {
  503. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  504. SaStatus_t status;
  505. if (!Info)
  506. return(SaErrorCodecHandle);
  507. if (!wfIn || !wfOut)
  508. return(SaErrorBadPointer);
  509. status=SaDecompressQuery(Sah, wfIn, wfOut);
  510. if (status!=SaErrorNone)
  511. return(status);
  512. switch (Info->Type)
  513. {
  514. case SA_PCM_DECODE:
  515. break;
  516. #ifdef MPEG_SUPPORT
  517. case SA_MPEG_DECODE:
  518. if (Info->MDInfo->DecompressStarted = FALSE)
  519. Info->MDInfo->DecompressStarted = TRUE;
  520. break;
  521. #endif /* MPEG_SUPPORT */
  522. #ifdef AC3_SUPPORT
  523. case SA_AC3_DECODE:
  524. if (Info->AC3Info->DecompressStarted = FALSE)
  525. Info->AC3Info->DecompressStarted = TRUE;
  526. break;
  527. #endif /* AC3_SUPPORT */
  528. #ifdef G723_SUPPORT
  529. /*
  530. case SA_G723_DECODE:
  531. if (Info->pSaG723Info->DecompressStarted = FALSE)
  532. Info->pSaG723Info->DecompressStarted = TRUE;
  533. break;
  534. */
  535. #endif /* G723_SUPPORT */
  536. }
  537. if ((Info->wfIn = (WAVEFORMATEX *)ScAlloc(sizeof(WAVEFORMATEX)+
  538. wfIn->cbSize)) == NULL)
  539. return (SaErrorMemory);
  540. if ((Info->wfOut = (WAVEFORMATEX *)ScAlloc(sizeof(WAVEFORMATEX)+
  541. wfOut->cbSize)) == NULL)
  542. return (SaErrorMemory);
  543. memcpy(Info->wfOut, wfOut, sizeof(WAVEFORMATEX)+wfOut->cbSize);
  544. memcpy(Info->wfIn, wfIn, sizeof(WAVEFORMATEX)+wfIn->cbSize);
  545. return(NoErrors);
  546. }
  547. /*
  548. ** Name: SaDecompress
  549. ** Purpose: Decompress a frame CompData -> PCM
  550. **
  551. ** Args: Sah = handle to software codec's Info structure.
  552. ** CompData = Pointer to compressed data (INPUT)
  553. ** CompLen = Length of CompData buffer
  554. ** DcmpData = buffer for decompressed data (OUTPUT)
  555. ** DcmpLen = Size of output buffer
  556. **
  557. */
  558. SaStatus_t SaDecompress (SaHandle_t Sah, u_char *CompData, unsigned int CompLen,
  559. u_char *DcmpData, unsigned int *DcmpLen)
  560. {
  561. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  562. unsigned int MaxDcmpLen = *DcmpLen;
  563. int stat=NoErrors;
  564. if (Sah==NULL)
  565. return(SaErrorCodecHandle);
  566. if (!DcmpData || !DcmpLen)
  567. return(SaErrorBadPointer);
  568. switch (Info->Type)
  569. {
  570. #ifdef MPEG_SUPPORT
  571. case SA_MPEG_DECODE:
  572. stat=sa_DecompressMPEG(Info, DcmpData, MaxDcmpLen, DcmpLen);
  573. Info->Info.NumBytesOut += *DcmpLen;
  574. break;
  575. case SA_PCM_DECODE:
  576. stat=ScBSGetBytes(Info->BSIn, DcmpData, MaxDcmpLen, DcmpLen);
  577. Info->Info.NumBytesIn += *DcmpLen;
  578. Info->Info.NumBytesOut += *DcmpLen;
  579. break;
  580. #endif /* MPEG_SUPPORT */
  581. #ifdef AC3_SUPPORT
  582. case SA_AC3_DECODE:
  583. stat=sa_DecompressAC3(Info, &DcmpData, MaxDcmpLen, DcmpLen);
  584. Info->Info.NumBytesOut += *DcmpLen;
  585. break;
  586. #endif /* AC3_SUPPORT */
  587. #ifdef GSM_SUPPORT
  588. case SA_GSM_DECODE:
  589. stat=sa_GSMDecode(Info->GSMInfo, CompData, (word *)DcmpData);
  590. if (stat==NoErrors)
  591. {
  592. *DcmpLen = 160 * 2;
  593. Info->Info.NumBytesIn += 33;
  594. Info->Info.NumBytesOut += 160 * 2;
  595. }
  596. else
  597. *DcmpLen = 0;
  598. break;
  599. #endif /* GSM_SUPPORT */
  600. #ifdef G723_SUPPORT
  601. case SA_G723_DECODE:
  602. //Can add a Param for to have CRC or not
  603. {
  604. word Crc = 0;
  605. stat = saG723Decompress( Info,(word *)DcmpData,
  606. (char *)CompData, Crc ) ;
  607. if(stat == SaErrorNone)
  608. {
  609. *DcmpLen = 480; //G723 240 samples(16-bit)= 240*2 = 480 bytes
  610. Info->Info.NumBytesOut += *DcmpLen;
  611. }
  612. else
  613. *DcmpLen = 0;
  614. }
  615. break;
  616. /*
  617. case SA_PCM_DECODE:
  618. stat=ScBSGetBytes(Info->BSIn, DcmpData, MaxDcmpLen, DcmpLen);
  619. Info->Info.NumBytesIn += *DcmpLen;
  620. Info->Info.NumBytesOut += *DcmpLen;
  621. break;
  622. */
  623. #endif /* G723_SUPPORT */
  624. default:
  625. *DcmpLen=0;
  626. return(SaErrorUnrecognizedFormat);
  627. }
  628. #if 0
  629. if (*DcmpLen && Info->CallbackFunction)
  630. {
  631. SaCallbackInfo_t CB;
  632. CB.Message = CB_FRAME_READY;
  633. CB.Data = DcmpData;
  634. CB.DataSize = CB_DATA_AUDIO;
  635. CB.DataSize = MaxDcmpLen;
  636. CB.DataUsed = *DcmpLen;
  637. CB.Action = CB_ACTION_CONTINUE;
  638. (*Info->CallbackFunction)(Sah, &CB, &Info->Info);
  639. }
  640. #endif
  641. return(stat);
  642. }
  643. /*
  644. ** Name: SaDecompressEx
  645. ** Purpose: Decompress a frame CompData -> PCM
  646. **
  647. ** Args: Sah = handle to software codec's Info structure.
  648. ** CompData = Pointer to compressed data (INPUT)
  649. ** CompLen = Length of CompData buffer
  650. ** DcmpData = Array of pointers to buffer for decompressed data (OUTPUT)
  651. ** DcmpLen = Size of decompressed buffers (all must be the same size)
  652. **
  653. */
  654. SaStatus_t SaDecompressEx (SaHandle_t Sah, u_char *CompData, unsigned int CompLen,
  655. u_char **DcmpData, unsigned int *DcmpLen)
  656. {
  657. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  658. unsigned int MaxDcmpLen = *DcmpLen;
  659. int stat=NoErrors;
  660. if (Sah==NULL)
  661. return(SaErrorCodecHandle);
  662. if (!DcmpData || !DcmpLen)
  663. return(SaErrorBadPointer);
  664. switch (Info->Type)
  665. {
  666. #ifdef AC3_SUPPORT
  667. case SA_AC3_DECODE:
  668. stat=sa_DecompressAC3(Info, DcmpData, MaxDcmpLen, DcmpLen);
  669. Info->Info.NumBytesOut += *DcmpLen;
  670. break;
  671. #endif /* AC3_SUPPORT */
  672. }
  673. return(stat);
  674. }
  675. /*
  676. ** Name: SaDecompressEnd
  677. ** Purpose: Terminate the Decompression Codec. Call after all calls to
  678. ** SaDecompress are done.
  679. **
  680. ** Args: Sah = handle to software codec's Info structure.
  681. */
  682. SaStatus_t SaDecompressEnd (SaHandle_t Sah)
  683. {
  684. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  685. if (!Info)
  686. return(SaErrorCodecHandle);
  687. switch (Info->Type)
  688. {
  689. #ifdef MPEG_SUPPORT
  690. case SA_MPEG_DECODE:
  691. Info->MDInfo->DecompressStarted = FALSE;
  692. break;
  693. #endif /* MPEG_SUPPORT */
  694. #ifdef AC3_SUPPORT
  695. case SA_AC3_DECODE:
  696. Info->AC3Info->DecompressStarted = FALSE;
  697. break;
  698. #endif /* AC3_SUPPORT */
  699. #ifdef G723_SUPPORT
  700. case SA_G723_DECODE:
  701. //Info->pSaG723Info->DecompressStarted = FALSE;
  702. break;
  703. #endif /* G723_SUPPORT */
  704. default:
  705. break;
  706. }
  707. if (Info->BSIn)
  708. ScBSReset(Info->BSIn); /* frees up any remaining compressed buffers */
  709. return(NoErrors);
  710. }
  711. /****************************** Compression ********************************/
  712. /*
  713. ** Name: SaCompressQuery
  714. ** Purpose: Check if input and output formats are supported.
  715. */
  716. SaStatus_t SaCompressQuery(SaHandle_t Sah, WAVEFORMATEX *wfIn,
  717. WAVEFORMATEX *wfOut)
  718. {
  719. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  720. if (!Info)
  721. return(SaErrorCodecHandle);
  722. if (!wfIn || !wfOut)
  723. return(SaErrorBadPointer);
  724. if (wfIn)
  725. {
  726. if (wfIn->wFormatTag != WAVE_FORMAT_PCM)
  727. return(SaErrorUnrecognizedFormat);
  728. if (wfIn->nChannels!=1 && wfIn->nChannels!=2)
  729. return(SaErrorUnrecognizedFormat);
  730. }
  731. if (wfOut)
  732. {
  733. if (wfOut->nChannels!=1 && wfOut->nChannels!=2)
  734. return(SaErrorUnrecognizedFormat);
  735. }
  736. if (wfIn && wfOut)
  737. {
  738. if (wfIn->nSamplesPerSec != wfOut->nSamplesPerSec)
  739. return(SaErrorUnrecognizedFormat);
  740. if (wfIn->wBitsPerSample!=16 &&
  741. (wfOut->wBitsPerSample !=16 || wfOut->wBitsPerSample !=8))
  742. return(SaErrorUnrecognizedFormat);
  743. }
  744. return(SaErrorNone);
  745. }
  746. /*
  747. ** Name: SaCompressBegin
  748. ** Purpose: Initialize the Compression Codec. Call after SaOpenCodec &
  749. ** before SaCompress (SaCompress will call SaCompressBegin
  750. ** on first call to codec after open if user doesn't call it)
  751. **
  752. ** Args: Sah = handle to software codec's Info structure.
  753. ** wfIn = format of input (uncompressed) audio
  754. ** wfOut = format of output (compressed) audio
  755. */
  756. SaStatus_t SaCompressBegin (SaHandle_t Sah, WAVEFORMATEX *wfIn,
  757. WAVEFORMATEX *wfOut)
  758. {
  759. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  760. SaStatus_t status;
  761. if (!Info)
  762. return(SaErrorCodecHandle);
  763. if (!wfIn || !wfOut)
  764. return(SaErrorBadPointer);
  765. status=SaCompressQuery(Sah, wfIn, wfOut);
  766. if (status!=SaErrorNone)
  767. return(status);
  768. switch (Info->Type)
  769. {
  770. #ifdef MPEG_SUPPORT
  771. case SA_MPEG_ENCODE:
  772. SaSetParamInt(Sah, SA_PARAM_SAMPLESPERSEC, wfIn->nSamplesPerSec);
  773. SaSetParamInt(Sah, SA_PARAM_CHANNELS, wfIn->nChannels);
  774. sa_MpegVerifyEncoderSettings(Sah);
  775. if (Info->MCInfo->CompressStarted = FALSE)
  776. Info->MCInfo->CompressStarted = TRUE;
  777. break;
  778. #endif /* MPEG_SUPPORT */
  779. #ifdef GSM_SUPPORT
  780. case SA_GSM_ENCODE:
  781. break;
  782. #endif /* GSM_SUPPORT */
  783. #ifdef AC3_SUPPORT
  784. #if 0
  785. case SA_AC3_ENCODE:
  786. break;
  787. #endif
  788. #endif /* AC3_SUPPORT */
  789. #ifdef G723_SUPPORT
  790. case SA_G723_ENCODE:
  791. //SaSetParamInt(Sah, SA_PARAM_SAMPLESPERSEC, wfIn->nSamplesPerSec);
  792. //SaSetParamInt(Sah, SA_PARAM_CHANNELS, wfIn->nChannels);
  793. //sa_MpegVerifyEncoderSettings(Sah);
  794. /*
  795. if (Info->pSaG723Info->CompressStarted = FALSE)
  796. Info->pSaG723Info->CompressStarted = TRUE;
  797. */
  798. break;
  799. #endif /* G723_SUPPORT */
  800. case SA_PCM_ENCODE:
  801. break;
  802. default:
  803. return(SaErrorUnrecognizedFormat);
  804. }
  805. if ((Info->wfIn = (WAVEFORMATEX *)ScAlloc(sizeof(WAVEFORMATEX)+
  806. wfIn->cbSize)) == NULL)
  807. return (SaErrorMemory);
  808. if ((Info->wfOut = (WAVEFORMATEX *)ScAlloc(sizeof(WAVEFORMATEX)+
  809. wfOut->cbSize)) == NULL)
  810. return (SaErrorMemory);
  811. memcpy(Info->wfOut, wfOut, sizeof(WAVEFORMATEX)+wfOut->cbSize);
  812. memcpy(Info->wfIn, wfIn, sizeof(WAVEFORMATEX)+wfIn->cbSize);
  813. return(NoErrors);
  814. }
  815. /*
  816. ** Name: SaCompress
  817. ** Purpose: Compress PCM audio ->CompData
  818. **
  819. ** Args: Sah = handle to software codec's Info structure.
  820. ** DcmpData = buffer for decompressed data (INPUT)
  821. ** DcmpLen = Number of Bytes Compressed (return bytes processed)
  822. ** CompData = Pointer to compressed data (OUTPUT)
  823. ** CompLen = Length of CompData buffer
  824. **
  825. */
  826. SaStatus_t SaCompress(SaHandle_t Sah,
  827. u_char *DcmpData, unsigned int *DcmpLen,
  828. u_char *CompData, unsigned int *CompLen)
  829. {
  830. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  831. unsigned int MaxCompLen = *CompLen, NumBytesIn=0;
  832. int stat=NoErrors;
  833. if (Sah==NULL)
  834. return(SaErrorCodecHandle);
  835. if (!DcmpData || !DcmpLen || !CompLen)
  836. return(SaErrorBadPointer);
  837. *CompLen = 0;
  838. switch (Info->Type)
  839. {
  840. #ifdef MPEG_SUPPORT
  841. case SA_MPEG_ENCODE:
  842. {
  843. unsigned int DcmpBytes, CompBytes, Offset;
  844. Offset=0;
  845. *CompLen=0;
  846. do {
  847. DcmpBytes=*DcmpLen-Offset;
  848. if (DcmpBytes<sa_GetMPEGSampleSize(Info)*2)
  849. break;
  850. _SlibDebug(_DEBUG_,
  851. printf("sa_CompressMPEG(Offset=%d) Address=%p Len=%d\n", Offset,
  852. DcmpData, DcmpBytes) );
  853. stat=sa_CompressMPEG(Info, DcmpData+Offset, &DcmpBytes,
  854. &CompBytes);
  855. if (stat==NoErrors)
  856. {
  857. if (CompBytes && Info->CallbackFunction)
  858. {
  859. SaCallbackInfo_t CB;
  860. CB.Message = CB_FRAME_READY;
  861. CB.Data = DcmpData+Offset;
  862. CB.DataType = CB_DATA_COMPRESSED;
  863. CB.DataSize = *DcmpLen-Offset;
  864. CB.DataUsed = CompBytes;
  865. CB.Action = CB_ACTION_CONTINUE;
  866. (*Info->CallbackFunction)(Sah, &CB, &Info->Info);
  867. }
  868. Offset+=DcmpBytes;
  869. NumBytesIn += DcmpBytes;
  870. *CompLen+=CompBytes;
  871. }
  872. } while (stat==NoErrors && DcmpBytes>0 && Offset<*DcmpLen);
  873. }
  874. break;
  875. #endif /* MPEG_SUPPORT */
  876. #ifdef GSM_SUPPORT
  877. case SA_GSM_ENCODE:
  878. {
  879. unsigned int DcmpBytes, CompBytes, Offset;
  880. Offset=0;
  881. *CompLen=0;
  882. if (!Info->BSOut && !CompData)
  883. return(SaErrorBadPointer);
  884. do {
  885. DcmpBytes=*DcmpLen-Offset;
  886. stat=sa_GSMEncode(Info->GSMInfo, (word *)(DcmpData+Offset),
  887. &DcmpBytes, CompData, Info->BSOut);
  888. if (stat==NoErrors)
  889. {
  890. Offset+=DcmpBytes;
  891. NumBytesIn += DcmpBytes;
  892. *CompLen += 33;
  893. if (CompData)
  894. CompData += 33;
  895. }
  896. } while (stat==NoErrors && Offset<*DcmpLen);
  897. }
  898. break;
  899. #endif /* GSM_SUPPORT */
  900. #ifdef AC3_SUPPORT
  901. #if 0 /* no AC-3 Encode yet */
  902. case SA_AC3_ENCODE:
  903. {
  904. unsigned int DcmpBytes, CompBytes, Offset;
  905. Offset=0;
  906. *CompLen=0;
  907. if (!Info->BSOut && !CompData)
  908. return(SaErrorBadPointer);
  909. do {
  910. DcmpBytes=*DcmpLen-Offset;
  911. stat=sa_AC3Encode(Info->AC3Info, (word *)(DcmpData+Offset),
  912. &DcmpBytes, CompData, Info->BSOut);
  913. if (stat==NoErrors)
  914. {
  915. Offset+=DcmpBytes;
  916. NumBytesIn += DcmpBytes;
  917. *CompLen += 33;
  918. if (CompData)
  919. CompData += 33;
  920. }
  921. } while (stat==NoErrors && Offset<*DcmpLen);
  922. }
  923. break;
  924. #endif
  925. #endif /* AC3_SUPPORT */
  926. #ifdef G723_SUPPORT
  927. case SA_G723_ENCODE:
  928. {
  929. /* Call SaG723Compress (audiobufsize/480) times
  930. * Need to store unprocessed stuff in Info->AudiobufUsed.
  931. * (This is done in SlibWriteAudio)
  932. * G723 encodes 240 samples at a time.=240*2 =480
  933. */
  934. unsigned int Offset;
  935. int iTimes = (int)(*DcmpLen / 480);
  936. int iLoop =0;
  937. Offset=0;
  938. *CompLen=0;
  939. while (stat==SaErrorNone && iLoop<iTimes)
  940. {
  941. stat = saG723Compress(Info,(word *)(DcmpData+Offset),
  942. (char *)CompData);
  943. Offset+=480; /* Input :240 samples (240*2 = 480 bytes) */
  944. NumBytesIn += 480;
  945. *CompLen+=24;/* 24 for 6.3 ;20 for 5.3 rate */
  946. iLoop++;
  947. }
  948. }
  949. break;
  950. #endif /* G723_SUPPORT */
  951. case SA_PCM_ENCODE:
  952. ScBSPutBytes(Info->BSOut, DcmpData, *DcmpLen);
  953. *CompLen = *DcmpLen;
  954. NumBytesIn = *DcmpLen;
  955. break;
  956. default:
  957. *CompLen=0;
  958. return(SaErrorUnrecognizedFormat);
  959. }
  960. *DcmpLen = NumBytesIn;
  961. Info->Info.NumBytesIn += NumBytesIn;
  962. Info->Info.NumBytesOut += *CompLen;
  963. return(stat);
  964. }
  965. /*
  966. ** Name: SaCompressEnd
  967. ** Purpose: Terminate the Compression Codec. Call after all calls to
  968. ** SaCompress are done.
  969. **
  970. ** Args: Sah = handle to software codec's Info structure.
  971. */
  972. SaStatus_t SaCompressEnd (SaHandle_t Sah)
  973. {
  974. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  975. if (!Info)
  976. return(SaErrorCodecHandle);
  977. switch (Info->Type)
  978. {
  979. #ifdef MPEG_SUPPORT
  980. case SA_MPEG_ENCODE:
  981. Info->MCInfo->CompressStarted = FALSE;
  982. break;
  983. #endif /* MPEG_SUPPORT */
  984. #ifdef G723_SUPPORT
  985. case SA_G723_ENCODE:
  986. //Info->pSaG723Info->CompressStarted = FALSE;
  987. break;
  988. #endif /* G723_SUPPORT */
  989. default:
  990. break;
  991. }
  992. if (Info->BSOut)
  993. ScBSFlush(Info->BSOut); /* flush out any remaining compressed buffers */
  994. /*
  995. if (Info->CallbackFunction)
  996. {
  997. CB.Message = CB_CODEC_DONE;
  998. CB.Data = NULL;
  999. CB.DataSize = 0;
  1000. CB.DataUsed = 0;
  1001. CB.DataType = CB_DATA_NONE;
  1002. CB.TimeStamp = 0;
  1003. CB.Flags = 0;
  1004. CB.Value = 0;
  1005. CB.Format = NULL;
  1006. CB.Action = CB_ACTION_CONTINUE;
  1007. (*Info->CallbackFunction)(Sah, &CB, NULL);
  1008. _SlibDebug(_DEBUG_,
  1009. printf("SaDecompressEnd Callback: CB_CODEC_DONE Action = %d\n",
  1010. CB.Action) );
  1011. if (CB.Action == CB_ACTION_END)
  1012. return (ScErrorClientEnd);
  1013. }
  1014. */
  1015. return(NoErrors);
  1016. }
  1017. /***************************** Miscellaneous *******************************/
  1018. /*
  1019. ** Name: SaSetDataSource
  1020. ** Purpose: Set the data source used by the MPEG bitstream parsing code
  1021. ** to either the Buffer Queue or File input. The default is
  1022. ** to use the Buffer Queue where data buffers are added by calling
  1023. ** SaAddBuffer. When using file IO, the data is read from a file
  1024. ** descriptor into a buffer supplied by the user.
  1025. **
  1026. ** Args: Sah = handle to software codec's Info structure.
  1027. ** Source = SU_USE_QUEUE or SU_USE_FILE
  1028. ** Fd = File descriptor to use if Source = SV_USE_FILE
  1029. ** Buf = Pointer to buffer to use if Source = SV_USE_FILE
  1030. ** BufSize= Size of buffer when Source = SV_USE_FILE
  1031. */
  1032. SaStatus_t SaSetDataSource (SaHandle_t Sah, int Source, int Fd,
  1033. void *Buffer_UserData, int BufSize)
  1034. {
  1035. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1036. int stat=NoErrors;
  1037. int DataType;
  1038. if (!Info)
  1039. return(SaErrorCodecHandle);
  1040. if (Info->Type==SA_MPEG_DECODE || Info->Type==SA_GSM_DECODE ||
  1041. Info->Type==SA_AC3_DECODE || Info->Type == SA_G723_DECODE )
  1042. DataType=CB_DATA_COMPRESSED;
  1043. else
  1044. DataType=CB_DATA_AUDIO;
  1045. if (Info->BSIn)
  1046. {
  1047. ScBSDestroy(Info->BSIn);
  1048. Info->BSIn=NULL;
  1049. }
  1050. switch (Source)
  1051. {
  1052. #ifdef MPEG_SUPPORT
  1053. case SA_USE_SAME:
  1054. if (Info->Type==SA_MPEG_DECODE)
  1055. ScBSSetFilter(Info->BSIn, MPEGAudioFilter);
  1056. break;
  1057. #endif /* MPEG_SUPPORT */
  1058. case SA_USE_BUFFER:
  1059. stat=ScBSCreateFromBuffer(&Info->BSIn, Buffer_UserData, BufSize);
  1060. #ifdef MPEG_SUPPORT
  1061. if (stat==NoErrors && Info->Type==SA_MPEG_DECODE)
  1062. ScBSSetFilter(Info->BSIn, MPEGAudioFilter);
  1063. #endif /* MPEG_SUPPORT */
  1064. break;
  1065. case SA_USE_BUFFER_QUEUE:
  1066. stat=ScBSCreateFromBufferQueue(&Info->BSIn, Sah, DataType, Info->Q,
  1067. (int (*)(ScHandle_t, ScCallbackInfo_t *, void *))Info->CallbackFunction,
  1068. (void *)Buffer_UserData);
  1069. break;
  1070. case SA_USE_FILE:
  1071. stat=ScBSCreateFromFile(&Info->BSIn, Fd, Buffer_UserData, BufSize);
  1072. #ifdef MPEG_SUPPORT
  1073. if (stat==NoErrors && Info->Type==SA_MPEG_DECODE)
  1074. ScBSSetFilter(Info->BSIn, MPEGAudioFilter);
  1075. #endif /* MPEG_SUPPORT */
  1076. break;
  1077. default:
  1078. stat=SaErrorBadArgument;
  1079. }
  1080. if (stat==NoErrors && Info->BSIn)
  1081. ScBSReset(Info->BSIn);
  1082. return(stat);
  1083. }
  1084. SaStatus_t SaSetDataDestination (SaHandle_t Sah, int Dest, int Fd,
  1085. void *Buffer_UserData, int BufSize)
  1086. {
  1087. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1088. int stat=NoErrors;
  1089. int DataType;
  1090. if (!Info)
  1091. return(SaErrorCodecHandle);
  1092. if (Info->Type==SA_MPEG_ENCODE || Info->Type==SA_GSM_ENCODE
  1093. /* || Info->Type==SA_AC3_ENCODE */ ||Info->Type==SA_G723_ENCODE)
  1094. DataType=CB_DATA_COMPRESSED;
  1095. else
  1096. DataType=CB_DATA_AUDIO;
  1097. if (Info->BSOut)
  1098. {
  1099. ScBSDestroy(Info->BSOut);
  1100. Info->BSOut=NULL;
  1101. }
  1102. switch (Dest)
  1103. {
  1104. case SA_USE_SAME:
  1105. break;
  1106. case SA_USE_BUFFER:
  1107. stat=ScBSCreateFromBuffer(&Info->BSOut, Buffer_UserData, BufSize);
  1108. break;
  1109. case SA_USE_BUFFER_QUEUE:
  1110. stat=ScBSCreateFromBufferQueue(&Info->BSOut, Sah, DataType, Info->Q,
  1111. (int (*)(ScHandle_t, ScCallbackInfo_t *, void *))Info->CallbackFunction,
  1112. (void *)Buffer_UserData);
  1113. break;
  1114. case SA_USE_FILE:
  1115. stat=ScBSCreateFromFile(&Info->BSOut, Fd, Buffer_UserData, BufSize);
  1116. break;
  1117. default:
  1118. stat=SaErrorBadArgument;
  1119. }
  1120. /*
  1121. if (stat==NoErrors && Info->BSOut)
  1122. ScBSReset(Info->BSOut);
  1123. */
  1124. return(stat);
  1125. }
  1126. /*
  1127. ** Name: SaGetDataSource
  1128. ** Purpose: Returns the current input bitstream being used by
  1129. ** the Codec.
  1130. ** Return: NULL if there no associated bitstream
  1131. */
  1132. ScBitstream_t *SaGetDataSource (SaHandle_t Sah)
  1133. {
  1134. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1135. if (!Info)
  1136. return(NULL);
  1137. return(Info->BSIn);
  1138. }
  1139. /*
  1140. ** Name: SaGetDataDestination
  1141. ** Purpose: Returns the current input bitstream being used by
  1142. ** the Codec.
  1143. ** Return: NULL if there no associated bitstream
  1144. */
  1145. ScBitstream_t *SaGetDataDestination(SaHandle_t Sah)
  1146. {
  1147. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1148. if (!Info)
  1149. return(NULL);
  1150. return(Info->BSOut);
  1151. }
  1152. /*
  1153. ** Name: SaAddBuffer
  1154. ** Purpose: Add a buffer of MPEG bitstream data to the CODEC or add an image
  1155. ** buffer to be filled by the CODEC (in streaming mode)
  1156. **
  1157. ** Args: Sah = handle to software codec's Info structure.
  1158. ** BufferInfo = structure describing buffer's address, type & size
  1159. */
  1160. SaStatus_t SaAddBuffer (SaHandle_t Sah, SaCallbackInfo_t *BufferInfo)
  1161. {
  1162. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1163. if (!Info)
  1164. return(SaErrorCodecHandle);
  1165. if (BufferInfo->DataType != CB_DATA_COMPRESSED)
  1166. return(SaErrorBadArgument);
  1167. if (!BufferInfo->Data || (BufferInfo->DataSize <= 0))
  1168. return(SaErrorBadArgument);
  1169. ScBufQueueAdd(Info->Q, BufferInfo->Data, BufferInfo->DataSize);
  1170. return(NoErrors);
  1171. }
  1172. #ifdef MPEG_SUPPORT
  1173. /*
  1174. ** Name: sa_GetMpegAudioInfo()
  1175. ** Purpose: Extract info about audio packets in an MPEG file.
  1176. ** Notes: If an "info" structure is passed to this function,
  1177. ** the entire file will be read for extended info.
  1178. ** Return: Not 0 = error
  1179. */
  1180. SaStatus_t sa_GetMpegAudioInfo(int fd, WAVEFORMATEX *wf, SaInfo_t *info)
  1181. {
  1182. int stat, sync;
  1183. ScBitstream_t *bs;
  1184. SaFrameParams_t fr_ps;
  1185. SaLayer_t layer;
  1186. unsigned long aframes=0, samples=0;
  1187. /* Default info parameters */
  1188. if (info)
  1189. {
  1190. info->Name[0]=0;
  1191. info->Description[0]=0;
  1192. info->Version=0;
  1193. info->CodecStarted=FALSE;
  1194. info->MS=0;
  1195. info->NumBytesIn=0;
  1196. info->NumBytesOut=0;
  1197. info->NumFrames=0;
  1198. info->TotalFrames=0;
  1199. info->TotalMS=0;
  1200. }
  1201. /* Default wave parameters */
  1202. wf->wFormatTag = WAVE_FORMAT_PCM;
  1203. wf->nChannels = 2;
  1204. wf->nSamplesPerSec = 44100;
  1205. wf->wBitsPerSample = 16;
  1206. wf->cbSize = 0;
  1207. stat=ScBSCreateFromFile(&bs, fd, NULL, 1024);
  1208. if (stat!=NoErrors)
  1209. {
  1210. fprintf(stderr, "Error creating bitstream.\n");
  1211. return(-1);
  1212. }
  1213. if (ScBSPeekBits(bs, PACK_START_CODE_LEN)!=PACK_START_CODE_BIN
  1214. && ScBSPeekBits(bs, MPEG_SYNC_WORD_LEN)!=MPEG_SYNC_WORD)
  1215. stat=SaErrorUnrecognizedFormat;
  1216. else
  1217. {
  1218. if (ScBSPeekBits(bs, MPEG_SYNC_WORD_LEN)==MPEG_SYNC_WORD)
  1219. printf("No MPEG packs found in file; assuming Audio stream only.\n");
  1220. else
  1221. ScBSSetFilter(bs, MPEGAudioFilter); /* Use the MPEG audio filter */
  1222. fr_ps.header = &layer;
  1223. fr_ps.tab_num = -1; /* no table loaded */
  1224. fr_ps.alloc = NULL;
  1225. sync = ScBSSeekAlign(bs, MPEG_SYNC_WORD, MPEG_SYNC_WORD_LEN);
  1226. if (!sync) {
  1227. sc_vprintf(stderr,"sa_GetMpegAudioInfo: Frame cannot be located\n");
  1228. return(SaErrorSyncLost);
  1229. }
  1230. /* Decode the first header to see what kind of audio we have */
  1231. sa_DecodeInfo(bs, &fr_ps);
  1232. sa_hdr_to_frps(&fr_ps);
  1233. #ifdef _VERBOSE_
  1234. sa_ShowHeader(&fr_ps);
  1235. #endif
  1236. /* Save no. of channels & sample rate return parameters for caller */
  1237. wf->nChannels = fr_ps.stereo;
  1238. wf->nSamplesPerSec = s_freq_int[fr_ps.header->sampling_frequency];
  1239. wf->wBitsPerSample = 16;
  1240. stat=SaErrorNone;
  1241. if (info) /* Read through all frames if there's a info structure */
  1242. {
  1243. sc_vprintf("Counting frames...\n");
  1244. aframes=0;
  1245. while (!bs->EOI && sync)
  1246. {
  1247. sync = ScBSSeekAlign(bs, MPEG_SYNC_WORD, MPEG_SYNC_WORD_LEN);
  1248. if (sync)
  1249. {
  1250. sc_dprintf("0x%X: Frame found\n",
  1251. ScBSBytePosition(bs)-4);
  1252. aframes++;
  1253. }
  1254. sa_DecodeInfo(bs, &fr_ps);
  1255. if (wf->nChannels<2) /* take the maximum number of channels */
  1256. {
  1257. sa_hdr_to_frps(&fr_ps);
  1258. wf->nChannels = fr_ps.stereo;
  1259. }
  1260. if (layer.lay==1)
  1261. samples+=384;
  1262. else
  1263. samples+=1152;
  1264. }
  1265. info->TotalFrames=aframes;
  1266. info->TotalMS=(samples*1000)/wf->nSamplesPerSec;
  1267. info->NumBytesOut=samples * wf->nChannels * 2;
  1268. sc_vprintf("Total Audio Frames = %u Bytes = %d MS = %d\n",
  1269. info->TotalFrames, info->NumBytesOut, info->TotalMS);
  1270. }
  1271. }
  1272. /* Reset the bitstream back to the beginning */
  1273. ScBSReset(bs);
  1274. /* Close the bit stream */
  1275. ScBSDestroy(bs);
  1276. /* Calculate additional parameters */
  1277. wf->nBlockAlign = (wf->wBitsPerSample>>3) * wf->nChannels;
  1278. wf->nAvgBytesPerSec = wf->nBlockAlign*wf->nSamplesPerSec;
  1279. return(stat);
  1280. }
  1281. #endif /* MPEG_SUPPORT */
  1282. /*
  1283. ** Name: sa_ConvertFormat()
  1284. ** Purpose: Do simple PCM data conversion (i.e. 16 to 8 bit,
  1285. ** Stereo to Mono, etc.)
  1286. */
  1287. static int sa_ConvertPCMFormat(SaCodecInfo_t *Info, u_char *data, int length)
  1288. {
  1289. int skip, rbytes;
  1290. u_char *fromptr, *toptr;
  1291. /* convert 16 bit to 8 bit if necessary */
  1292. if (Info->wfOut->wBitsPerSample == 8)
  1293. {
  1294. if (Info->wfOut->nChannels==1 && Info->wfOut->nChannels==2)
  1295. skip=4;
  1296. else
  1297. skip=2;
  1298. length/=skip;
  1299. toptr = data;
  1300. fromptr = data+1;
  1301. for (rbytes=length; rbytes; rbytes--, toptr++, fromptr+=skip)
  1302. *toptr = *fromptr;
  1303. }
  1304. return(length);
  1305. }
  1306. SaStatus_t SaSetParamBoolean(SaHandle_t Sah, SaParameter_t param,
  1307. ScBoolean_t value)
  1308. {
  1309. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1310. if (!Info)
  1311. return(SaErrorCodecHandle);
  1312. _SlibDebug(_VERBOSE_, printf("SaSetParamBoolean()\n") );
  1313. switch (Info->Type)
  1314. {
  1315. #ifdef MPEG_SUPPORT
  1316. case SA_MPEG_ENCODE:
  1317. saMpegSetParamBoolean(Sah, param, value);
  1318. break;
  1319. #endif
  1320. #ifdef G723_SUPPORT
  1321. case SA_G723_ENCODE:
  1322. saG723SetParamBoolean(Sah, param, value);
  1323. break;
  1324. #endif
  1325. default:
  1326. return(SaErrorCodecType);
  1327. }
  1328. return(NoErrors);
  1329. }
  1330. SaStatus_t SaSetParamInt(SaHandle_t Sah, SaParameter_t param, qword value)
  1331. {
  1332. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1333. if (!Info)
  1334. return(SaErrorCodecHandle);
  1335. _SlibDebug(_VERBOSE_, printf("SaSetParamInt()\n") );
  1336. switch (Info->Type)
  1337. {
  1338. #ifdef MPEG_SUPPORT
  1339. case SA_MPEG_DECODE:
  1340. case SA_MPEG_ENCODE:
  1341. saMpegSetParamInt(Sah, param, value);
  1342. break;
  1343. #endif
  1344. #ifdef AC3_SUPPORT
  1345. case SA_AC3_DECODE:
  1346. /* case SA_AC3_ENCODE: */
  1347. saAC3SetParamInt(Sah, param, value);
  1348. break;
  1349. #endif
  1350. #ifdef G723_SUPPORT
  1351. case SA_G723_DECODE:
  1352. case SA_G723_ENCODE:
  1353. saG723SetParamInt(Sah, param, value);
  1354. break;
  1355. #endif
  1356. default:
  1357. return(SaErrorCodecType);
  1358. }
  1359. return(NoErrors);
  1360. }
  1361. ScBoolean_t SaGetParamBoolean(SaHandle_t Sah, SaParameter_t param)
  1362. {
  1363. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1364. if (!Info)
  1365. return(FALSE);
  1366. switch (Info->Type)
  1367. {
  1368. #ifdef MPEG_SUPPORT
  1369. case SA_MPEG_DECODE:
  1370. case SA_MPEG_ENCODE:
  1371. return(saMpegGetParamBoolean(Sah, param));
  1372. break;
  1373. #endif
  1374. #ifdef G723_SUPPORT
  1375. case SA_G723_DECODE:
  1376. case SA_G723_ENCODE:
  1377. return(saG723GetParamBoolean(Sah, param));
  1378. break;
  1379. #endif
  1380. }
  1381. return(FALSE);
  1382. }
  1383. qword SaGetParamInt(SaHandle_t Sah, SaParameter_t param)
  1384. {
  1385. SaCodecInfo_t *Info = (SaCodecInfo_t *)Sah;
  1386. if (!Info)
  1387. return(0);
  1388. switch (Info->Type)
  1389. {
  1390. #ifdef MPEG_SUPPORT
  1391. case SA_MPEG_DECODE:
  1392. case SA_MPEG_ENCODE:
  1393. return(saMpegGetParamInt(Sah, param));
  1394. break;
  1395. #endif
  1396. #ifdef G723_SUPPORT
  1397. case SA_G723_DECODE:
  1398. case SA_G723_ENCODE:
  1399. return(saG723GetParamInt(Sah, param));
  1400. break;
  1401. #endif
  1402. }
  1403. return(0);
  1404. }