buffers.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "hlxclib/stdlib.h" /* for malloc, free */
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include "coder.h"
  50. /**************************************************************************************
  51. * Function: ClearBuffer
  52. *
  53. * Description: fill buffer with 0's
  54. *
  55. * Inputs: pointer to buffer
  56. * number of bytes to fill with 0
  57. *
  58. * Outputs: cleared buffer
  59. *
  60. * Return: none
  61. *
  62. * Notes: slow, platform-independent equivalent to memset(buf, 0, nBytes)
  63. **************************************************************************************/
  64. #define ClearBuffer(buf, nBytes) memset(buf, 0, nBytes) //fb
  65. /*
  66. static void ClearBuffer(void *buf, int nBytes)
  67. {
  68. int i;
  69. unsigned char *cbuf = (unsigned char *)buf;
  70. for (i = 0; i < nBytes; i++)
  71. cbuf[i] = 0;
  72. //fb
  73. memset(buf, 0, nBytes)
  74. return;
  75. }
  76. */
  77. /**************************************************************************************
  78. * Function: AllocateBuffers
  79. *
  80. * Description: allocate all the memory needed for the MP3 decoder
  81. *
  82. * Inputs: none
  83. *
  84. * Outputs: none
  85. *
  86. * Return: pointer to MP3DecInfo structure (initialized with pointers to all
  87. * the internal buffers needed for decoding, all other members of
  88. * MP3DecInfo structure set to 0)
  89. *
  90. * Notes: if one or more mallocs fail, function frees any buffers already
  91. * allocated before returning
  92. **************************************************************************************/
  93. MP3DecInfo *AllocateBuffers(void)
  94. {
  95. MP3DecInfo *mp3DecInfo;
  96. FrameHeader *fh;
  97. SideInfo *si;
  98. ScaleFactorInfo *sfi;
  99. HuffmanInfo *hi;
  100. DequantInfo *di;
  101. IMDCTInfo *mi;
  102. SubbandInfo *sbi;
  103. mp3DecInfo = (MP3DecInfo *)malloc(sizeof(MP3DecInfo));
  104. if (!mp3DecInfo)
  105. return 0;
  106. ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
  107. fh = (FrameHeader *) malloc(sizeof(FrameHeader));
  108. si = (SideInfo *) malloc(sizeof(SideInfo));
  109. sfi = (ScaleFactorInfo *) malloc(sizeof(ScaleFactorInfo));
  110. hi = (HuffmanInfo *) malloc(sizeof(HuffmanInfo));
  111. di = (DequantInfo *) malloc(sizeof(DequantInfo));
  112. mi = (IMDCTInfo *) malloc(sizeof(IMDCTInfo));
  113. sbi = (SubbandInfo *) malloc(sizeof(SubbandInfo));
  114. mp3DecInfo->FrameHeaderPS = (void *)fh;
  115. mp3DecInfo->SideInfoPS = (void *)si;
  116. mp3DecInfo->ScaleFactorInfoPS = (void *)sfi;
  117. mp3DecInfo->HuffmanInfoPS = (void *)hi;
  118. mp3DecInfo->DequantInfoPS = (void *)di;
  119. mp3DecInfo->IMDCTInfoPS = (void *)mi;
  120. mp3DecInfo->SubbandInfoPS = (void *)sbi;
  121. if (!fh || !si || !sfi || !hi || !di || !mi || !sbi) {
  122. FreeBuffers(mp3DecInfo); /* safe to call - only frees memory that was successfully allocated */
  123. return 0;
  124. }
  125. /* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
  126. ClearBuffer(fh, sizeof(FrameHeader));
  127. ClearBuffer(si, sizeof(SideInfo));
  128. ClearBuffer(sfi, sizeof(ScaleFactorInfo));
  129. ClearBuffer(hi, sizeof(HuffmanInfo));
  130. ClearBuffer(di, sizeof(DequantInfo));
  131. ClearBuffer(mi, sizeof(IMDCTInfo));
  132. ClearBuffer(sbi, sizeof(SubbandInfo));
  133. return mp3DecInfo;
  134. }
  135. #define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */
  136. /**************************************************************************************
  137. * Function: FreeBuffers
  138. *
  139. * Description: frees all the memory used by the MP3 decoder
  140. *
  141. * Inputs: pointer to initialized MP3DecInfo structure
  142. *
  143. * Outputs: none
  144. *
  145. * Return: none
  146. *
  147. * Notes: safe to call even if some buffers were not allocated (uses SAFE_FREE)
  148. **************************************************************************************/
  149. void FreeBuffers(MP3DecInfo *mp3DecInfo)
  150. {
  151. if (!mp3DecInfo)
  152. return;
  153. SAFE_FREE(mp3DecInfo->FrameHeaderPS);
  154. SAFE_FREE(mp3DecInfo->SideInfoPS);
  155. SAFE_FREE(mp3DecInfo->ScaleFactorInfoPS);
  156. SAFE_FREE(mp3DecInfo->HuffmanInfoPS);
  157. SAFE_FREE(mp3DecInfo->DequantInfoPS);
  158. SAFE_FREE(mp3DecInfo->IMDCTInfoPS);
  159. SAFE_FREE(mp3DecInfo->SubbandInfoPS);
  160. SAFE_FREE(mp3DecInfo);
  161. }