buffers.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // J.Sz. 21/04/2006 #include "hlxclib/stdlib.h" /* for malloc, free */
  47. #include "coder.h"
  48. #define static_buffers
  49. #ifdef static_buffers
  50. MP3DecInfo mp3DecInfo; // 0x7f0 = 2032
  51. SubbandInfo sbi; // 0x2204 = 8708
  52. IMDCTInfo mi; // 0x1b20 = 6944
  53. HuffmanInfo hi; // 0x1210 = 4624
  54. DequantInfo di; // 0x348 = 840
  55. ScaleFactorInfo sfi; // 0x124 = 292
  56. SideInfo si; // 0x148 = 328
  57. FrameHeader fh; // 0x38 = 56
  58. #else
  59. #include <rtthread.h>
  60. #define malloc rt_malloc
  61. #define free rt_free
  62. #endif
  63. /**************************************************************************************
  64. * Function: ClearBuffer
  65. *
  66. * Description: fill buffer with 0's
  67. *
  68. * Inputs: pointer to buffer
  69. * number of bytes to fill with 0
  70. *
  71. * Outputs: cleared buffer
  72. *
  73. * Return: none
  74. *
  75. * Notes: slow, platform-independent equivalent to memset(buf, 0, nBytes)
  76. **************************************************************************************/
  77. void ClearBuffer(void *buf, int nBytes)
  78. {
  79. int i;
  80. unsigned char *cbuf = (unsigned char *)buf;
  81. for (i = 0; i < nBytes; i++)
  82. cbuf[i] = 0;
  83. }
  84. /**************************************************************************************
  85. * Function: AllocateBuffers
  86. *
  87. * Description: allocate all the memory needed for the MP3 decoder
  88. *
  89. * Inputs: none
  90. *
  91. * Outputs: none
  92. *
  93. * Return: pointer to MP3DecInfo structure (initialized with pointers to all
  94. * the internal buffers needed for decoding, all other members of
  95. * MP3DecInfo structure set to 0)
  96. *
  97. * Notes: if one or more mallocs fail, function frees any buffers already
  98. * allocated before returning
  99. *
  100. * Changed by Kasper Jepsen to support static buffers as well.
  101. *
  102. **************************************************************************************/
  103. MP3DecInfo *AllocateBuffers(void)
  104. {
  105. MP3DecInfo *mp3DecInfo_pointer;
  106. #ifdef static_buffers
  107. mp3DecInfo_pointer = (MP3DecInfo*)&mp3DecInfo;
  108. ClearBuffer((void*)&mp3DecInfo, sizeof(MP3DecInfo));
  109. mp3DecInfo.FrameHeaderPS = (void*)&fh;
  110. mp3DecInfo.SideInfoPS = (void*)&si;
  111. mp3DecInfo.ScaleFactorInfoPS = (void*)&sfi;
  112. mp3DecInfo.HuffmanInfoPS = (void*)&hi;
  113. mp3DecInfo.DequantInfoPS = (void*)&di;
  114. mp3DecInfo.IMDCTInfoPS = (void*)&mi;
  115. mp3DecInfo.SubbandInfoPS = (void*)&sbi;
  116. /* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
  117. ClearBuffer((void*)&fh, sizeof(FrameHeader));
  118. ClearBuffer((void*)&si, sizeof(SideInfo));
  119. ClearBuffer((void*)&sfi, sizeof(ScaleFactorInfo));
  120. ClearBuffer((void*)&hi, sizeof(HuffmanInfo));
  121. ClearBuffer((void*)&di, sizeof(DequantInfo));
  122. ClearBuffer((void*)&mi, sizeof(IMDCTInfo));
  123. ClearBuffer((void*)&sbi, sizeof(SubbandInfo));
  124. // return mp3DecInfo_pointer;
  125. #else
  126. FrameHeader *fh;
  127. SideInfo *si;
  128. ScaleFactorInfo *sfi;
  129. HuffmanInfo *hi;
  130. DequantInfo *di;
  131. IMDCTInfo *mi;
  132. SubbandInfo *sbi;
  133. mp3DecInfo_pointer = (MP3DecInfo *)malloc(sizeof(MP3DecInfo));
  134. if (!mp3DecInfo_pointer)
  135. return 0;
  136. ClearBuffer(mp3DecInfo_pointer, sizeof(MP3DecInfo));
  137. fh = (FrameHeader *) malloc(sizeof(FrameHeader));
  138. si = (SideInfo *) malloc(sizeof(SideInfo));
  139. sfi = (ScaleFactorInfo *) malloc(sizeof(ScaleFactorInfo));
  140. hi = (HuffmanInfo *) malloc(sizeof(HuffmanInfo));
  141. di = (DequantInfo *) malloc(sizeof(DequantInfo));
  142. mi = (IMDCTInfo *) malloc(sizeof(IMDCTInfo));
  143. sbi = (SubbandInfo *) malloc(sizeof(SubbandInfo));
  144. mp3DecInfo_pointer->FrameHeaderPS = (void *)fh;
  145. mp3DecInfo_pointer->SideInfoPS = (void *)si;
  146. mp3DecInfo_pointer->ScaleFactorInfoPS = (void *)sfi;
  147. mp3DecInfo_pointer->HuffmanInfoPS = (void *)hi;
  148. mp3DecInfo_pointer->DequantInfoPS = (void *)di;
  149. mp3DecInfo_pointer->IMDCTInfoPS = (void *)mi;
  150. mp3DecInfo_pointer->SubbandInfoPS = (void *)sbi;
  151. if (!fh || !si || !sfi || !hi || !di || !mi || !sbi) {
  152. FreeBuffers(mp3DecInfo_pointer); /* safe to call - only frees memory that was successfully allocated */
  153. return 0;
  154. }
  155. /* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
  156. //Optimized away.. hmm
  157. ClearBuffer(fh, sizeof(FrameHeader));
  158. ClearBuffer(si, sizeof(SideInfo));
  159. ClearBuffer(sfi, sizeof(ScaleFactorInfo));
  160. ClearBuffer(hi, sizeof(HuffmanInfo));
  161. ClearBuffer(di, sizeof(DequantInfo));
  162. ClearBuffer(mi, sizeof(IMDCTInfo));
  163. ClearBuffer(sbi, sizeof(SubbandInfo));
  164. #endif
  165. return mp3DecInfo_pointer;
  166. }
  167. #ifndef static_buffers
  168. #define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */
  169. #endif
  170. /**************************************************************************************
  171. * Function: FreeBuffers
  172. *
  173. * Description: frees all the memory used by the MP3 decoder
  174. *
  175. * Inputs: pointer to initialized MP3DecInfo structure
  176. *
  177. * Outputs: none
  178. *
  179. * Return: none
  180. *
  181. * Notes: safe to call even if some buffers were not allocated (uses SAFE_FREE)
  182. **************************************************************************************/
  183. void FreeBuffers(MP3DecInfo *mp3DecInfo)
  184. {
  185. #ifndef static_buffers
  186. if (!mp3DecInfo)
  187. return;
  188. SAFE_FREE(mp3DecInfo->FrameHeaderPS);
  189. SAFE_FREE(mp3DecInfo->SideInfoPS);
  190. SAFE_FREE(mp3DecInfo->ScaleFactorInfoPS);
  191. SAFE_FREE(mp3DecInfo->HuffmanInfoPS);
  192. SAFE_FREE(mp3DecInfo->DequantInfoPS);
  193. SAFE_FREE(mp3DecInfo->IMDCTInfoPS);
  194. SAFE_FREE(mp3DecInfo->SubbandInfoPS);
  195. SAFE_FREE(mp3DecInfo);
  196. #endif
  197. }