buffers.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: RCSL 1.0/RPSL 1.0
  3. *
  4. * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file, are
  7. * subject to the current version of the RealNetworks Public Source License
  8. * Version 1.0 (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the RealNetworks Community Source License Version 1.0
  11. * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
  12. * in which case the RCSL will apply. You may also obtain the license terms
  13. * directly from RealNetworks. You may not use this file except in
  14. * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
  15. * applicable to this file, the RCSL. Please see the applicable RPSL or
  16. * RCSL for the rights, obligations and limitations governing use of the
  17. * contents of the file.
  18. *
  19. * This file is part of the Helix DNA Technology. RealNetworks is the
  20. * developer of the Original Code and owns the copyrights in the portions
  21. * it created.
  22. *
  23. * This file, and the files included with this file, is distributed and made
  24. * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  25. * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  26. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  27. * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  28. *
  29. * Technology Compatibility Kit Test Suite(s) Location:
  30. * http://www.helixcommunity.org/content/tck
  31. *
  32. * Contributor(s):
  33. *
  34. * ***** END LICENSE BLOCK ***** */
  35. /**************************************************************************************
  36. * Fixed-point MP3 decoder
  37. * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
  38. * June 2003
  39. *
  40. * buffers.c - allocation and freeing of internal MP3 decoder buffers
  41. *
  42. * All memory allocation for the codec is done in this file, so if you don't want
  43. * to use other the default system malloc() and free() for heap management this is
  44. * the only file you'll need to change.
  45. **************************************************************************************/
  46. #include <stdlib.h> /* for malloc, free */
  47. #include <memdebug.h>
  48. #include <contrib/hxmp3/coder.h>
  49. /**************************************************************************************
  50. * Function: ClearBuffer
  51. *
  52. * Description: fill buffer with 0's
  53. *
  54. * Inputs: pointer to buffer
  55. * number of bytes to fill with 0
  56. *
  57. * Outputs: cleared buffer
  58. *
  59. * Return: none
  60. *
  61. * Notes: slow, platform-independent equivalent to memset(buf, 0, nBytes)
  62. **************************************************************************************/
  63. static void ClearBuffer(void *buf, int nBytes)
  64. {
  65. int i;
  66. unsigned char *cbuf = (unsigned char *)buf;
  67. for (i = 0; i < nBytes; i++)
  68. cbuf[i] = 0;
  69. return;
  70. }
  71. /**************************************************************************************
  72. * Function: AllocateBuffers
  73. *
  74. * Description: allocate all the memory needed for the MP3 decoder
  75. *
  76. * Inputs: none
  77. *
  78. * Outputs: none
  79. *
  80. * Return: pointer to MP3DecInfo structure (initialized with pointers to all
  81. * the internal buffers needed for decoding, all other members of
  82. * MP3DecInfo structure set to 0)
  83. *
  84. * Notes: if one or more mallocs fail, function frees any buffers already
  85. * allocated before returning
  86. **************************************************************************************/
  87. MP3DecInfo *AllocateBuffers(void)
  88. {
  89. MP3DecInfo *mp3DecInfo;
  90. FrameHeader *fh;
  91. SideInfo *si;
  92. ScaleFactorInfo *sfi;
  93. HuffmanInfo *hi;
  94. DequantInfo *di;
  95. IMDCTInfo *mi;
  96. SubbandInfo *sbi;
  97. mp3DecInfo = (MP3DecInfo *)malloc(sizeof(MP3DecInfo));
  98. if (!mp3DecInfo)
  99. return 0;
  100. ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
  101. fh = (FrameHeader *) malloc(sizeof(FrameHeader));
  102. si = (SideInfo *) malloc(sizeof(SideInfo));
  103. sfi = (ScaleFactorInfo *) malloc(sizeof(ScaleFactorInfo));
  104. hi = (HuffmanInfo *) malloc(sizeof(HuffmanInfo));
  105. di = (DequantInfo *) malloc(sizeof(DequantInfo));
  106. mi = (IMDCTInfo *) malloc(sizeof(IMDCTInfo));
  107. sbi = (SubbandInfo *) malloc(sizeof(SubbandInfo));
  108. mp3DecInfo->FrameHeaderPS = (void *)fh;
  109. mp3DecInfo->SideInfoPS = (void *)si;
  110. mp3DecInfo->ScaleFactorInfoPS = (void *)sfi;
  111. mp3DecInfo->HuffmanInfoPS = (void *)hi;
  112. mp3DecInfo->DequantInfoPS = (void *)di;
  113. mp3DecInfo->IMDCTInfoPS = (void *)mi;
  114. mp3DecInfo->SubbandInfoPS = (void *)sbi;
  115. if (!fh || !si || !sfi || !hi || !di || !mi || !sbi) {
  116. FreeBuffers(mp3DecInfo); /* safe to call - only frees memory that was successfully allocated */
  117. return 0;
  118. }
  119. /* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
  120. ClearBuffer(fh, sizeof(FrameHeader));
  121. ClearBuffer(si, sizeof(SideInfo));
  122. ClearBuffer(sfi, sizeof(ScaleFactorInfo));
  123. ClearBuffer(hi, sizeof(HuffmanInfo));
  124. ClearBuffer(di, sizeof(DequantInfo));
  125. ClearBuffer(mi, sizeof(IMDCTInfo));
  126. ClearBuffer(sbi, sizeof(SubbandInfo));
  127. return mp3DecInfo;
  128. }
  129. #define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */
  130. /**************************************************************************************
  131. * Function: FreeBuffers
  132. *
  133. * Description: frees all the memory used by the MP3 decoder
  134. *
  135. * Inputs: pointer to initialized MP3DecInfo structure
  136. *
  137. * Outputs: none
  138. *
  139. * Return: none
  140. *
  141. * Notes: safe to call even if some buffers were not allocated (uses SAFE_FREE)
  142. **************************************************************************************/
  143. void FreeBuffers(MP3DecInfo *mp3DecInfo)
  144. {
  145. if (!mp3DecInfo)
  146. return;
  147. SAFE_FREE(mp3DecInfo->FrameHeaderPS);
  148. SAFE_FREE(mp3DecInfo->SideInfoPS);
  149. SAFE_FREE(mp3DecInfo->ScaleFactorInfoPS);
  150. SAFE_FREE(mp3DecInfo->HuffmanInfoPS);
  151. SAFE_FREE(mp3DecInfo->DequantInfoPS);
  152. SAFE_FREE(mp3DecInfo->IMDCTInfoPS);
  153. SAFE_FREE(mp3DecInfo->SubbandInfoPS);
  154. SAFE_FREE(mp3DecInfo);
  155. }