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: sbr_read_data.c 21 22 ------------------------------------------------------------------------------ 23 REVISION HISTORY 24 25 26 Who: Date: MM/DD/YYYY 27 Description: 28 29 ------------------------------------------------------------------------------ 30 INPUT AND OUTPUT DEFINITIONS 31 32 INPUT 33 34 SBRDECODER self, 35 SBRBITSTREAM * stream, 36 float *timeData, 37 int numChannels 38 39 OUTPUT 40 41 errorCode, noError if successful 42 43 ------------------------------------------------------------------------------ 44 FUNCTION DESCRIPTION 45 46 sbr decoder processing, set up SBR decoder phase 2 in case of 47 different cotrol data 48 49 ------------------------------------------------------------------------------ 50 REQUIREMENTS 51 52 53 ------------------------------------------------------------------------------ 54 REFERENCES 55 56 SC 29 Software Copyright Licencing Disclaimer: 57 58 This software module was originally developed by 59 Coding Technologies 60 61 and edited by 62 - 63 64 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3 65 standards for reference purposes and its performance may not have been 66 optimized. This software module is an implementation of one or more tools as 67 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards. 68 ISO/IEC gives users free license to this software module or modifications 69 thereof for use in products claiming conformance to audiovisual and 70 image-coding related ITU Recommendations and/or ISO/IEC International 71 Standards. ISO/IEC gives users the same free license to this software module or 72 modifications thereof for research purposes and further ISO/IEC standardisation. 73 Those intending to use this software module in products are advised that its 74 use may infringe existing patents. ISO/IEC have no liability for use of this 75 software module or modifications thereof. Copyright is not released for 76 products that do not conform to audiovisual and image-coding related ITU 77 Recommendations and/or ISO/IEC International Standards. 78 The original developer retains full right to modify and use the code for its 79 own purpose, assign or donate the code to a third party and to inhibit third 80 parties from using the code for products that do not conform to audiovisual and 81 image-coding related ITU Recommendations and/or ISO/IEC International Standards. 82 This copyright notice must be included in all copies or derivative works. 83 Copyright (c) ISO/IEC 2002. 84 85 ------------------------------------------------------------------------------ 86 PSEUDO-CODE 87 88 ------------------------------------------------------------------------------ 89 */ 90 91 92 /*---------------------------------------------------------------------------- 93 ; INCLUDES 94 ----------------------------------------------------------------------------*/ 95 96 #ifdef AAC_PLUS 97 98 99 #include "sbr_read_data.h" 100 #include "s_bit_buffer.h" 101 #include "buf_getbits.h" 102 #include "sbr_get_sce.h" 103 #include "sbr_get_cpe.h" 104 #include "sbr_reset_dec.h" 105 #include "sbr_get_header_data.h" 106 #include "sbr_crc_check.h" 107 #include "aac_mem_funcs.h" 108 109 110 #include "init_sbr_dec.h" /* !!! */ 111 112 113 /*---------------------------------------------------------------------------- 114 ; MACROS 115 ; Define module specific macros here 116 ----------------------------------------------------------------------------*/ 117 118 119 /*---------------------------------------------------------------------------- 120 ; DEFINES 121 ; Include all pre-processor statements here. Include conditional 122 ; compile variables also. 123 ----------------------------------------------------------------------------*/ 124 125 /*---------------------------------------------------------------------------- 126 ; LOCAL FUNCTION DEFINITIONS 127 ; Function Prototype declaration 128 ----------------------------------------------------------------------------*/ 129 130 /*---------------------------------------------------------------------------- 131 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS 132 ; Variable declaration - defined here and used outside this module 133 ----------------------------------------------------------------------------*/ 134 135 /*---------------------------------------------------------------------------- 136 ; EXTERNAL FUNCTION REFERENCES 137 ; Declare functions defined elsewhere and referenced in this module 138 ----------------------------------------------------------------------------*/ 139 140 /*---------------------------------------------------------------------------- 141 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES 142 ; Declare variables used in this module but defined elsewhere 143 ----------------------------------------------------------------------------*/ 144 145 /*---------------------------------------------------------------------------- 146 ; FUNCTION CODE 147 ----------------------------------------------------------------------------*/ 148 149 SBR_ERROR sbr_read_data(SBRDECODER_DATA * self, 150 SBR_DEC * sbrDec, 151 SBRBITSTREAM *stream) 152 { 153 SBR_ERROR sbr_err = SBRDEC_OK; 154 int32_t SbrFrameOK = 1; 155 int32_t sbrCRCAlwaysOn = 0; 156 157 UInt32 bs_header_flag = 0; 158 159 SBR_HEADER_STATUS headerStatus = HEADER_OK; 160 161 SBR_CHANNEL *SbrChannel = self->SbrChannel; 162 163 int32_t zeropadding_bits; 164 165 BIT_BUFFER bitBuf ; 166 167 /* 168 * evaluate Bitstream 169 */ 170 171 bitBuf.buffer_word = 0; 172 bitBuf.buffered_bits = 0; 173 bitBuf.nrBitsRead = 0; 174 175 bitBuf.char_ptr = stream->sbrElement[0].Data; 176 bitBuf.bufferLen = (stream->sbrElement[0].Payload) << 3; 177 178 179 /* 180 * we have to skip a nibble because the first element of Data only 181 * contains a nibble of data ! 182 */ 183 buf_getbits(&bitBuf, LEN_NIBBLE); 184 185 if ((stream->sbrElement[0].ExtensionType == SBR_EXTENSION_CRC) || 186 sbrCRCAlwaysOn) 187 { 188 int32_t CRCLen = ((stream->sbrElement[0].Payload - 1) << 3) + 4 - SI_SBR_CRC_BITS; 189 SbrFrameOK = sbr_crc_check(&bitBuf, CRCLen); 190 } 191 192 193 if (SbrFrameOK) 194 { 195 /* 196 * The sbr data seems ok, if the header flag is set we read the header 197 * and check if vital parameters have changed since the previous frame. 198 * If the syncState equals UPSAMPLING, the SBR Tool has not been 199 * initialised by SBR header data, and can only do upsampling 200 */ 201 202 bs_header_flag = buf_getbits(&bitBuf, 1); /* read Header flag */ 203 204 if (bs_header_flag) 205 { 206 /* 207 * If syncState == SBR_ACTIVE, it means that we've had a SBR header 208 * before, and we will compare with the previous header to see if a 209 * reset is required. If the syncState equals UPSAMPLING this means 210 * that the SBR-Tool so far is only initialised to do upsampling 211 * and hence we need to do a reset, and initialise the system 212 * according to the present header. 213 */ 214 215 headerStatus = sbr_get_header_data(&(SbrChannel[0].frameData.sbr_header), 216 &bitBuf, 217 SbrChannel[0].syncState); 218 } /* if (bs_header_flag) */ 219 220 221 switch (stream->sbrElement[0].ElementID) 222 { 223 case SBR_ID_SCE : 224 225 /* change of control data, reset decoder */ 226 if (headerStatus == HEADER_RESET) 227 { 228 sbr_err = sbr_reset_dec(&(SbrChannel[0].frameData), 229 sbrDec, 230 self->SbrChannel[0].frameData.sbr_header.sampleRateMode); 231 232 if (sbr_err != SBRDEC_OK) 233 { 234 break; 235 } 236 /* 237 * At this point we have a header and the system has been reset, 238 * hence syncState from now on will be SBR_ACTIVE. 239 */ 240 SbrChannel[0].syncState = SBR_ACTIVE; 241 } 242 243 if ((SbrChannel[0].syncState == SBR_ACTIVE)) 244 { 245 sbr_err = sbr_get_sce(&(SbrChannel[0].frameData), 246 &bitBuf 247 #ifdef PARAMETRICSTEREO 248 , self->hParametricStereoDec 249 #endif 250 ); 251 252 if (sbr_err != SBRDEC_OK) 253 { 254 break; 255 } 256 } 257 258 break; 259 260 case SBR_ID_CPE : 261 262 if (bs_header_flag) 263 { 264 pv_memcpy(&(SbrChannel[1].frameData.sbr_header), 265 &(SbrChannel[0].frameData.sbr_header), 266 sizeof(SBR_HEADER_DATA)); 267 } 268 269 /* change of control data, reset decoder */ 270 if (headerStatus == HEADER_RESET) 271 { 272 for (int32_t lr = 0 ; lr < 2 ; lr++) 273 { 274 sbr_err = sbr_reset_dec(&(SbrChannel[lr].frameData), 275 sbrDec, 276 self->SbrChannel[0].frameData.sbr_header.sampleRateMode); 277 278 if (sbr_err != SBRDEC_OK) 279 { 280 break; 281 } 282 283 SbrChannel[lr].syncState = SBR_ACTIVE; 284 } 285 } 286 287 if (SbrChannel[0].syncState == SBR_ACTIVE) 288 { 289 sbr_err = sbr_get_cpe(&(SbrChannel[0].frameData), 290 &(SbrChannel[1].frameData), 291 &bitBuf); 292 293 if (sbr_err != SBRDEC_OK) 294 { 295 break; 296 } 297 298 } 299 break; 300 301 default: 302 sbr_err = SBRDEC_ILLEGAL_PLUS_ELE_ID; 303 break; 304 } 305 306 } /* if (SbrFrameOK) */ 307 308 /* 309 * Check that the bits read did not go beyond SBR frame boundaries 310 */ 311 312 zeropadding_bits = (8 - (bitBuf.nrBitsRead & 0x7)) & 0x7; 313 314 if ((bitBuf.nrBitsRead + zeropadding_bits) > bitBuf.bufferLen) 315 { 316 sbr_err = SBRDEC_INVALID_BITSTREAM; 317 } 318 319 return sbr_err; 320 } 321 322 323 #endif 324 325