buffers.cpp
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 /************************************************************************************** 37 * Fixed-point MP3 decoder 38 * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com) 39 * June 2003 40 * 41 * buffers.c - allocation and freeing of internal MP3 decoder buffers 42 * 43 * All memory allocation for the codec is done in this file, so if you don't want 44 * to use other the default system malloc() and free() for heap management this is 45 * the only file you'll need to change. 46 **************************************************************************************/ 47 48 #include "coder.h" 49 50 #ifndef _WIN32 51 #ifdef DEMO_HELIX_FOOTPRINT 52 #include "dv_debug_usart.h" 53 #endif 54 #endif 55 56 /************************************************************************************** 57 * Function: ClearBuffer 58 * 59 * Description: fill buffer with 0's 60 * 61 * Inputs: pointer to buffer 62 * number of bytes to fill with 0 63 * 64 * Outputs: cleared buffer 65 * 66 * Return: none 67 * 68 * Notes: slow, platform-independent equivalent to memset(buf, 0, nBytes) 69 **************************************************************************************/ 70 static void ClearBuffer(void *buf, int nBytes) 71 { 72 int i; 73 unsigned char *cbuf = (unsigned char *)buf; 74 75 for (i = 0; i < nBytes; i++) 76 cbuf[i] = 0; 77 78 return; 79 } 80 81 /************************************************************************************** 82 * Function: AllocateBuffers 83 * 84 * Description: allocate all the memory needed for the MP3 decoder 85 * 86 * Inputs: none 87 * 88 * Outputs: none 89 * 90 * Return: pointer to MP3DecInfo structure (initialized with pointers to all 91 * the internal buffers needed for decoding, all other members of 92 * MP3DecInfo structure set to 0) 93 * 94 * Notes: if one or more mallocs fail, function frees any buffers already 95 * allocated before returning 96 **************************************************************************************/ 97 MP3DecInfo *AllocateBuffers(void) 98 { 99 MP3DecInfo *mp3DecInfo; 100 /* 101 FrameHeader *fh; 102 SideInfo *si; 103 ScaleFactorInfo *sfi; 104 HuffmanInfo *hi; 105 DequantInfo *di; 106 IMDCTInfo *mi; 107 SubbandInfo *sbi; 108 */ 109 110 // Buffers: 111 static char s_mp3DecInfo[sizeof(MP3DecInfo)]; 112 static char fh[sizeof(FrameHeader)]; 113 static char si[sizeof(SideInfo)]; 114 static char sfi[sizeof(ScaleFactorInfo)]; 115 static char hi[sizeof(HuffmanInfo)]; 116 static char di[sizeof(DequantInfo)]; 117 static char mi[sizeof(IMDCTInfo)]; 118 static char sbi[sizeof(SubbandInfo)]; 119 120 //mp3DecInfo = (MP3DecInfo *)malloc(sizeof(MP3DecInfo)); 121 mp3DecInfo = (MP3DecInfo *)s_mp3DecInfo; 122 if (!mp3DecInfo) 123 { 124 #ifndef _WIN32 125 #ifdef DEMO_HELIX_FOOTPRINT 126 sprintf(COPY_DEBUG_BUFFER,"mp3DecInfo size: %d\n", (int)sizeof(MP3DecInfo)); 127 DV_DEBUG_USART_Trace( COPY_DEBUG_BUFFER ); 128 #endif 129 #endif 130 return 0; 131 } 132 ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo)); 133 134 /* Switched to static allocations. 135 hi = (HuffmanInfo *) malloc(sizeof(HuffmanInfo)); 136 sbi = (SubbandInfo *) malloc(sizeof(SubbandInfo)); 137 mi = (IMDCTInfo *) malloc(sizeof(IMDCTInfo)); 138 di = (DequantInfo *) malloc(sizeof(DequantInfo)); 139 si = (SideInfo *) malloc(sizeof(SideInfo)); 140 sfi = (ScaleFactorInfo *) malloc(sizeof(ScaleFactorInfo)); 141 fh = (FrameHeader *) malloc(sizeof(FrameHeader)); 142 */ 143 144 mp3DecInfo->FrameHeaderPS = (void *)fh; 145 mp3DecInfo->SideInfoPS = (void *)si; 146 mp3DecInfo->ScaleFactorInfoPS = (void *)sfi; 147 mp3DecInfo->HuffmanInfoPS = (void *)hi; 148 mp3DecInfo->DequantInfoPS = (void *)di; 149 mp3DecInfo->IMDCTInfoPS = (void *)mi; 150 mp3DecInfo->SubbandInfoPS = (void *)sbi; 151 152 /* Removed; static allocation guarantees success. 153 if (!fh || !si || !sfi || !hi || !di || !mi || !sbi) { 154 #ifndef _WIN32 155 #ifdef DEMO_HELIX_FOOTPRINT 156 sprintf(COPY_DEBUG_BUFFER,"mp3DecInfo:%d[%d] | fh:%d[%d] | si:%d[%d] \ 157 | sfi:%d[%d] | hi:%d[%d] | di:%d[%d] | mi:%d[%d] | sbi:%d[%d]\n", 158 (int)mp3DecInfo, (int)sizeof(MP3DecInfo), (int)fh, (int)sizeof(FrameHeader), 159 (int)si, (int)sizeof(SideInfo), (int)sfi, (int)sizeof(ScaleFactorInfo), 160 (int)hi, (int)sizeof(HuffmanInfo), (int)di, (int)sizeof(DequantInfo), 161 (int)mi, (int)sizeof(IMDCTInfo), (int)sbi, (int)sizeof(SubbandInfo) ); 162 DV_DEBUG_USART_Trace( COPY_DEBUG_BUFFER ); 163 #endif 164 #endif 165 FreeBuffers(mp3DecInfo); // safe to call - only frees memory that was successfully allocated 166 return 0; 167 } 168 */ 169 170 #ifndef _WIN32 171 #ifdef DEMO_HELIX_FOOTPRINT 172 sprintf(COPY_DEBUG_BUFFER, "Total decoder malloc size: %d\n", 173 (int)(sizeof(MP3DecInfo) + sizeof(FrameHeader) + sizeof(SideInfo) 174 + sizeof(ScaleFactorInfo) + sizeof(HuffmanInfo) + sizeof(DequantInfo) 175 + sizeof(IMDCTInfo) + sizeof(SubbandInfo))); 176 DV_DEBUG_USART_Trace( COPY_DEBUG_BUFFER ); 177 #endif 178 #endif 179 180 /* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */ 181 ClearBuffer(fh, sizeof(FrameHeader)); 182 ClearBuffer(si, sizeof(SideInfo)); 183 ClearBuffer(sfi, sizeof(ScaleFactorInfo)); 184 ClearBuffer(hi, sizeof(HuffmanInfo)); 185 ClearBuffer(di, sizeof(DequantInfo)); 186 ClearBuffer(mi, sizeof(IMDCTInfo)); 187 ClearBuffer(sbi, sizeof(SubbandInfo)); 188 189 return mp3DecInfo; 190 } 191 192 #define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */ 193 194 /************************************************************************************** 195 * Function: FreeBuffers 196 * 197 * Description: frees all the memory used by the MP3 decoder 198 * 199 * Inputs: pointer to initialized MP3DecInfo structure 200 * 201 * Outputs: none 202 * 203 * Return: none 204 * 205 * Notes: safe to call even if some buffers were not allocated (uses SAFE_FREE) 206 **************************************************************************************/ 207 void FreeBuffers(MP3DecInfo *mp3DecInfo) 208 { 209 if (!mp3DecInfo) 210 return; 211 212 /* Switched to static allocations. 213 SAFE_FREE(mp3DecInfo->FrameHeaderPS); 214 SAFE_FREE(mp3DecInfo->SideInfoPS); 215 SAFE_FREE(mp3DecInfo->ScaleFactorInfoPS); 216 SAFE_FREE(mp3DecInfo->HuffmanInfoPS); 217 SAFE_FREE(mp3DecInfo->DequantInfoPS); 218 SAFE_FREE(mp3DecInfo->IMDCTInfoPS); 219 SAFE_FREE(mp3DecInfo->SubbandInfoPS); 220 221 SAFE_FREE(mp3DecInfo); 222 */ 223 }