buffers.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: buffers.c,v 1.1 2005/02/26 01:47:34 jrecker Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * buffers.c - allocation and deallocation of internal AAC decoder buffers
  43. **************************************************************************************/
  44. #if defined(USE_DEFAULT_STDLIB) || defined(ESP_PLATFORM)
  45. #include <stdlib.h>
  46. #else
  47. #include "hlxclib/stdlib.h"
  48. #endif
  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. #include <string.h>
  65. void ClearBuffer(void *buf, int nBytes)
  66. {
  67. /* int i;
  68. unsigned char *cbuf = (unsigned char *)buf;
  69. for (i = 0; i < nBytes; i++)
  70. cbuf[i] = 0;
  71. return;
  72. */
  73. memset(buf, 0, nBytes);
  74. }
  75. /**************************************************************************************
  76. * Function: AllocateBuffers
  77. *
  78. * Description: allocate all the memory needed for the AAC decoder
  79. *
  80. * Inputs: none
  81. *
  82. * Outputs: none
  83. *
  84. * Return: pointer to AACDecInfo structure, cleared to all 0's (except for
  85. * pointer to platform-specific data structure)
  86. *
  87. * Notes: if one or more mallocs fail, function frees any buffers already
  88. * allocated before returning
  89. **************************************************************************************/
  90. AACDecInfo *AllocateBuffers(void)
  91. {
  92. AACDecInfo *aacDecInfo;
  93. aacDecInfo = (AACDecInfo *)malloc(sizeof(AACDecInfo));
  94. if (!aacDecInfo)
  95. return 0;
  96. ClearBuffer(aacDecInfo, sizeof(AACDecInfo));
  97. aacDecInfo->psInfoBase = malloc(sizeof(PSInfoBase));
  98. if (!aacDecInfo->psInfoBase) {
  99. FreeBuffers(aacDecInfo);
  100. return 0;
  101. }
  102. ClearBuffer(aacDecInfo->psInfoBase, sizeof(PSInfoBase));
  103. return aacDecInfo;
  104. }
  105. AACDecInfo *AllocateBuffersPre(void **ptr, int *sz)
  106. {
  107. AACDecInfo *aacDecInfo;
  108. char *p = (char*)*ptr;
  109. aacDecInfo = (AACDecInfo *)p;
  110. p += (sizeof(AACDecInfo) +7 ) & ~7;
  111. *sz -= (sizeof(AACDecInfo) +7 ) & ~7;
  112. if (*sz < 0)
  113. return 0;
  114. ClearBuffer(aacDecInfo, sizeof(AACDecInfo));
  115. aacDecInfo->psInfoBase = (PSInfoBase*)p;
  116. p += (sizeof(PSInfoBase) + 7) & ~7;
  117. *sz -= (sizeof(PSInfoBase) + 7) & ~7;
  118. if (*sz <0) {
  119. return 0;
  120. }
  121. ClearBuffer(aacDecInfo->psInfoBase, sizeof(PSInfoBase));
  122. *ptr = p;
  123. return aacDecInfo;
  124. }
  125. #ifndef SAFE_FREE
  126. #define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */
  127. #endif
  128. /**************************************************************************************
  129. * Function: FreeBuffers
  130. *
  131. * Description: frees all the memory used by the AAC decoder
  132. *
  133. * Inputs: pointer to initialized AACDecInfo structure
  134. *
  135. * Outputs: none
  136. *
  137. * Return: none
  138. *
  139. * Notes: safe to call even if some buffers were not allocated (uses SAFE_FREE)
  140. **************************************************************************************/
  141. void FreeBuffers(AACDecInfo *aacDecInfo)
  142. {
  143. if (!aacDecInfo)
  144. return;
  145. SAFE_FREE(aacDecInfo->psInfoBase);
  146. SAFE_FREE(aacDecInfo);
  147. }