1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 /* 19 20 Filename: ps_read_data.c 21 22 23 ------------------------------------------------------------------------------ 24 REVISION HISTORY 25 26 27 Who: Date: MM/DD/YYYY 28 Description: 29 30 ------------------------------------------------------------------------------ 31 INPUT AND OUTPUT DEFINITIONS 32 33 34 35 ------------------------------------------------------------------------------ 36 FUNCTION DESCRIPTION 37 38 Decodes parametric stereo 39 40 ------------------------------------------------------------------------------ 41 REQUIREMENTS 42 43 44 ------------------------------------------------------------------------------ 45 REFERENCES 46 47 SC 29 Software Copyright Licencing Disclaimer: 48 49 This software module was originally developed by 50 Coding Technologies 51 52 and edited by 53 - 54 55 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3 56 standards for reference purposes and its performance may not have been 57 optimized. This software module is an implementation of one or more tools as 58 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards. 59 ISO/IEC gives users free license to this software module or modifications 60 thereof for use in products claiming conformance to audiovisual and 61 image-coding related ITU Recommendations and/or ISO/IEC International 62 Standards. ISO/IEC gives users the same free license to this software module or 63 modifications thereof for research purposes and further ISO/IEC standardisation. 64 Those intending to use this software module in products are advised that its 65 use may infringe existing patents. ISO/IEC have no liability for use of this 66 software module or modifications thereof. Copyright is not released for 67 products that do not conform to audiovisual and image-coding related ITU 68 Recommendations and/or ISO/IEC International Standards. 69 The original developer retains full right to modify and use the code for its 70 own purpose, assign or donate the code to a third party and to inhibit third 71 parties from using the code for products that do not conform to audiovisual and 72 image-coding related ITU Recommendations and/or ISO/IEC International Standards. 73 This copyright notice must be included in all copies or derivative works. 74 Copyright (c) ISO/IEC 2003. 75 76 ------------------------------------------------------------------------------ 77 PSEUDO-CODE 78 79 ------------------------------------------------------------------------------ 80 */ 81 82 83 /*---------------------------------------------------------------------------- 84 ; INCLUDES 85 ----------------------------------------------------------------------------*/ 86 87 #ifdef AAC_PLUS 88 89 #ifdef PARAMETRICSTEREO 90 91 #include "pv_audio_type_defs.h" 92 #include "buf_getbits.h" 93 #include "s_bit_buffer.h" 94 #include "s_huffman.h" 95 #include "aac_mem_funcs.h" 96 #include "s_ps_dec.h" 97 #include "sbr_decode_huff_cw.h" 98 #include "ps_decode_bs_utils.h" 99 #include "ps_bstr_decoding.h" 100 #include "ps_read_data.h" 101 /*---------------------------------------------------------------------------- 102 ; MACROS 103 ; Define module specific macros here 104 ----------------------------------------------------------------------------*/ 105 106 107 /*---------------------------------------------------------------------------- 108 ; DEFINES 109 ; Include all pre-processor statements here. Include conditional 110 ; compile variables also. 111 ----------------------------------------------------------------------------*/ 112 113 /*---------------------------------------------------------------------------- 114 ; LOCAL FUNCTION DEFINITIONS 115 ; Function Prototype declaration 116 ----------------------------------------------------------------------------*/ 117 118 /*---------------------------------------------------------------------------- 119 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS 120 ; Variable declaration - defined here and used outside this module 121 ----------------------------------------------------------------------------*/ 122 123 124 /*---------------------------------------------------------------------------- 125 ; EXTERNAL FUNCTION REFERENCES 126 ; Declare functions defined elsewhere and referenced in this module 127 ----------------------------------------------------------------------------*/ 128 129 130 /*---------------------------------------------------------------------------- 131 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES 132 ; Declare variables used in this module but defined elsewhere 133 ----------------------------------------------------------------------------*/ 134 135 /* IID & ICC Huffman codebooks */ 136 const Char aBookPsIidTimeDecode[28][2] = 137 { 138 { -64, 1 }, { -65, 2 }, { -63, 3 }, { -66, 4 }, 139 { -62, 5 }, { -67, 6 }, { -61, 7 }, { -68, 8 }, 140 { -60, 9 }, { -69, 10 }, { -59, 11 }, { -70, 12 }, 141 { -58, 13 }, { -57, 14 }, { -71, 15 }, { 16, 17 }, 142 { -56, -72 }, { 18, 21 }, { 19, 20 }, { -55, -78 }, 143 { -77, -76 }, { 22, 25 }, { 23, 24 }, { -75, -74 }, 144 { -73, -54 }, { 26, 27 }, { -53, -52 }, { -51, -50 } 145 }; 146 147 const Char aBookPsIidFreqDecode[28][2] = 148 { 149 { -64, 1 }, { 2, 3 }, { -63, -65 }, { 4, 5 }, 150 { -62, -66 }, { 6, 7 }, { -61, -67 }, { 8, 9 }, 151 { -68, -60 }, { -59, 10 }, { -69, 11 }, { -58, 12 }, 152 { -70, 13 }, { -71, 14 }, { -57, 15 }, { 16, 17 }, 153 { -56, -72 }, { 18, 19 }, { -55, -54 }, { 20, 21 }, 154 { -73, -53 }, { 22, 24 }, { -74, 23 }, { -75, -78 }, 155 { 25, 26 }, { -77, -76 }, { -52, 27 }, { -51, -50 } 156 }; 157 158 const Char aBookPsIccTimeDecode[14][2] = 159 { 160 { -64, 1 }, { -63, 2 }, { -65, 3 }, { -62, 4 }, 161 { -66, 5 }, { -61, 6 }, { -67, 7 }, { -60, 8 }, 162 { -68, 9 }, { -59, 10 }, { -69, 11 }, { -58, 12 }, 163 { -70, 13 }, { -71, -57 } 164 }; 165 166 const Char aBookPsIccFreqDecode[14][2] = 167 { 168 { -64, 1 }, { -63, 2 }, { -65, 3 }, { -62, 4 }, 169 { -66, 5 }, { -61, 6 }, { -67, 7 }, { -60, 8 }, 170 { -59, 9 }, { -68, 10 }, { -58, 11 }, { -69, 12 }, 171 { -57, 13 }, { -70, -71 } 172 }; 173 174 const Char aBookPsIidFineTimeDecode[60][2] = 175 { 176 { 1, -64 }, { -63, 2 }, { 3, -65 }, { 4, 59 }, 177 { 5, 7 }, { 6, -67 }, { -68, -60 }, { -61, 8 }, 178 { 9, 11 }, { -59, 10 }, { -70, -58 }, { 12, 41 }, 179 { 13, 20 }, { 14, -71 }, { -55, 15 }, { -53, 16 }, 180 { 17, -77 }, { 18, 19 }, { -85, -84 }, { -46, -45 }, 181 { -57, 21 }, { 22, 40 }, { 23, 29 }, { -51, 24 }, 182 { 25, 26 }, { -83, -82 }, { 27, 28 }, { -90, -38 }, 183 { -92, -91 }, { 30, 37 }, { 31, 34 }, { 32, 33 }, 184 { -35, -34 }, { -37, -36 }, { 35, 36 }, { -94, -93 }, 185 { -89, -39 }, { 38, -79 }, { 39, -81 }, { -88, -40 }, 186 { -74, -54 }, { 42, -69 }, { 43, 44 }, { -72, -56 }, 187 { 45, 52 }, { 46, 50 }, { 47, -76 }, { -49, 48 }, 188 { -47, 49 }, { -87, -41 }, { -52, 51 }, { -78, -50 }, 189 { 53, -73 }, { 54, -75 }, { 55, 57 }, { 56, -80 }, 190 { -86, -42 }, { -48, 58 }, { -44, -43 }, { -66, -62 } 191 }; 192 193 const Char aBookPsIidFineFreqDecode[60][2] = 194 { 195 { 1, -64 }, { 2, 4 }, { 3, -65 }, { -66, -62 }, 196 { -63, 5 }, { 6, 7 }, { -67, -61 }, { 8, 9 }, 197 { -68, -60 }, { 10, 11 }, { -69, -59 }, { 12, 13 }, 198 { -70, -58 }, { 14, 18 }, { -57, 15 }, { 16, -72 }, 199 { -54, 17 }, { -75, -53 }, { 19, 37 }, { -56, 20 }, 200 { 21, -73 }, { 22, 29 }, { 23, -76 }, { 24, -78 }, 201 { 25, 28 }, { 26, 27 }, { -85, -43 }, { -83, -45 }, 202 { -81, -47 }, { -52, 30 }, { -50, 31 }, { 32, -79 }, 203 { 33, 34 }, { -82, -46 }, { 35, 36 }, { -90, -89 }, 204 { -92, -91 }, { 38, -71 }, { -55, 39 }, { 40, -74 }, 205 { 41, 50 }, { 42, -77 }, { -49, 43 }, { 44, 47 }, 206 { 45, 46 }, { -86, -42 }, { -88, -87 }, { 48, 49 }, 207 { -39, -38 }, { -41, -40 }, { -51, 51 }, { 52, 59 }, 208 { 53, 56 }, { 54, 55 }, { -35, -34 }, { -37, -36 }, 209 { 57, 58 }, { -94, -93 }, { -84, -44 }, { -80, -48 } 210 }; 211 212 213 214 215 216 217 /*---------------------------------------------------------------------------- 218 ; FUNCTION CODE 219 ----------------------------------------------------------------------------*/ 220 221 Int32 ps_read_data(STRUCT_PS_DEC *ps_dec, 222 BIT_BUFFER * hBitBuf, 223 Int32 nBitsLeft) 224 225 { 226 Int gr; 227 UInt32 env; 228 UInt32 dtFlag; 229 Int32 startbits; 230 SbrHuffman CurrentTable; 231 232 if (!ps_dec) 233 { 234 return 0; 235 } 236 237 startbits = GetNrBitsAvailable(hBitBuf); 238 239 if (buf_get_1bit(hBitBuf)) /* Enable Header */ 240 { 241 ps_dec->bEnableIid = buf_get_1bit(hBitBuf); 242 243 if (ps_dec->bEnableIid) 244 { 245 ps_dec->freqResIid = buf_getbits(hBitBuf, 3); 246 247 if (ps_dec->freqResIid > 2) 248 { 249 ps_dec->bFineIidQ = 1; 250 ps_dec->freqResIid -= 3; 251 } 252 else 253 { 254 ps_dec->bFineIidQ = 0; 255 } 256 } 257 258 ps_dec->bEnableIcc = buf_get_1bit(hBitBuf); 259 if (ps_dec->bEnableIcc) 260 { 261 ps_dec->freqResIcc = buf_getbits(hBitBuf, 3); 262 263 if (ps_dec->freqResIcc > 2) 264 { 265 ps_dec->freqResIcc -= 3; 266 } 267 } 268 ps_dec->bEnableExt = buf_get_1bit(hBitBuf); 269 } 270 271 ps_dec->bFrameClass = buf_get_1bit(hBitBuf); 272 if (ps_dec->bFrameClass == 0) 273 { 274 ps_dec->noEnv = aFixNoEnvDecode[ buf_getbits(hBitBuf, 2)]; 275 } 276 else 277 { 278 ps_dec->noEnv = 1 + buf_getbits(hBitBuf, 2); 279 for (env = 1; env < ps_dec->noEnv + 1; env++) 280 { 281 ps_dec->aEnvStartStop[env] = (buf_getbits(hBitBuf, 5)) + 1; 282 } 283 } 284 285 if ((ps_dec->freqResIid > 2) || (ps_dec->freqResIcc > 2)) 286 { 287 288 ps_dec->bPsDataAvail = 0; 289 290 nBitsLeft -= startbits - GetNrBitsAvailable(hBitBuf); 291 while (nBitsLeft) 292 { 293 int i = nBitsLeft; 294 if (i > 8) 295 { 296 i = 8; 297 } 298 buf_getbits(hBitBuf, i); 299 nBitsLeft -= i; 300 } 301 return (startbits - GetNrBitsAvailable(hBitBuf)); 302 } 303 304 if (ps_dec->bEnableIid) 305 { 306 for (env = 0; env < ps_dec->noEnv; env++) 307 { 308 dtFlag = buf_get_1bit(hBitBuf); 309 310 if (!dtFlag) 311 { 312 if (ps_dec->bFineIidQ) 313 { 314 CurrentTable = aBookPsIidFineFreqDecode; 315 } 316 else 317 { 318 CurrentTable = aBookPsIidFreqDecode; 319 } 320 } 321 else 322 { 323 if (ps_dec->bFineIidQ) 324 { 325 CurrentTable = aBookPsIidFineTimeDecode; 326 } 327 else 328 { 329 CurrentTable = aBookPsIidTimeDecode; 330 } 331 } 332 333 for (gr = 0; gr < aNoIidBins[ps_dec->freqResIid]; gr++) 334 { 335 ps_dec->aaIidIndex[env][gr] = sbr_decode_huff_cw(CurrentTable, hBitBuf); 336 } 337 338 ps_dec->abIidDtFlag[env] = dtFlag; 339 } 340 } 341 342 if (ps_dec->bEnableIcc) 343 { 344 for (env = 0; env < ps_dec->noEnv; env++) 345 { 346 dtFlag = buf_get_1bit(hBitBuf); 347 if (!dtFlag) 348 { 349 CurrentTable = aBookPsIccFreqDecode; 350 } 351 else 352 { 353 CurrentTable = aBookPsIccTimeDecode; 354 } 355 for (gr = 0; gr < aNoIccBins[ps_dec->freqResIcc]; gr++) 356 { 357 ps_dec->aaIccIndex[env][gr] = sbr_decode_huff_cw(CurrentTable, hBitBuf); 358 } 359 360 ps_dec->abIccDtFlag[env] = dtFlag; 361 } 362 } 363 364 if (ps_dec->bEnableExt) 365 { 366 367 int cnt; 368 369 cnt = (int)buf_getbits(hBitBuf, 4); 370 371 if (cnt == 15) 372 { 373 cnt += (int)buf_getbits(hBitBuf, 8); 374 } 375 376 hBitBuf->nrBitsRead += (cnt << 3); 377 } 378 379 ps_dec->bPsDataAvail = 1; 380 381 return (startbits - GetNrBitsAvailable(hBitBuf)); 382 } 383 384 #endif 385 386 387 #endif 388 389