1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /**************************** SBR encoder library ******************************
96 
97    Author(s):
98 
99    Description:
100 
101 *******************************************************************************/
102 
103 /*!
104   \file
105   \brief  SBR bit writing routines $Revision: 93300 $
106 */
107 
108 #include "bit_sbr.h"
109 
110 #include "code_env.h"
111 #include "cmondata.h"
112 #include "sbr.h"
113 
114 #include "ps_main.h"
115 
116 typedef enum { SBR_ID_SCE = 1, SBR_ID_CPE } SBR_ELEMENT_TYPE;
117 
118 static INT encodeSbrData(HANDLE_SBR_ENV_DATA sbrEnvDataLeft,
119                          HANDLE_SBR_ENV_DATA sbrEnvDataRight,
120                          HANDLE_PARAMETRIC_STEREO hParametricStereo,
121                          HANDLE_COMMON_DATA cmonData, SBR_ELEMENT_TYPE sbrElem,
122                          INT coupling, UINT sbrSyntaxFlags);
123 
124 static INT encodeSbrHeader(HANDLE_SBR_HEADER_DATA sbrHeaderData,
125                            HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,
126                            HANDLE_COMMON_DATA cmonData);
127 
128 static INT encodeSbrHeaderData(HANDLE_SBR_HEADER_DATA sbrHeaderData,
129                                HANDLE_FDK_BITSTREAM hBitStream);
130 
131 static INT encodeSbrSingleChannelElement(
132     HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream,
133     HANDLE_PARAMETRIC_STEREO hParametricStereo, const UINT sbrSyntaxFlags);
134 
135 static INT encodeSbrChannelPairElement(
136     HANDLE_SBR_ENV_DATA sbrEnvDataLeft, HANDLE_SBR_ENV_DATA sbrEnvDataRight,
137     HANDLE_PARAMETRIC_STEREO hParametricStereo, HANDLE_FDK_BITSTREAM hBitStream,
138     const INT coupling, const UINT sbrSyntaxFlags);
139 
140 static INT encodeSbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData,
141                          HANDLE_FDK_BITSTREAM hBitStream);
142 
143 static int encodeLowDelaySbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData,
144                                  HANDLE_FDK_BITSTREAM hBitStream,
145                                  const int transmitFreqs,
146                                  const UINT sbrSyntaxFlags);
147 
148 static INT encodeSbrDtdf(HANDLE_SBR_ENV_DATA sbrEnvData,
149                          HANDLE_FDK_BITSTREAM hBitStream);
150 
151 static INT writeNoiseLevelData(HANDLE_SBR_ENV_DATA sbrEnvData,
152                                HANDLE_FDK_BITSTREAM hBitStream, INT coupling);
153 
154 static INT writeEnvelopeData(HANDLE_SBR_ENV_DATA sbrEnvData,
155                              HANDLE_FDK_BITSTREAM hBitStream, INT coupling);
156 
157 static INT writeSyntheticCodingData(HANDLE_SBR_ENV_DATA sbrEnvData,
158                                     HANDLE_FDK_BITSTREAM hBitStream);
159 
160 static INT encodeExtendedData(HANDLE_PARAMETRIC_STEREO hParametricStereo,
161                               HANDLE_FDK_BITSTREAM hBitStream);
162 
163 static INT getSbrExtendedDataSize(HANDLE_PARAMETRIC_STEREO hParametricStereo);
164 
165 /*****************************************************************************
166 
167     functionname: FDKsbrEnc_WriteEnvSingleChannelElement
168     description:  writes pure SBR single channel data element
169     returns:      number of bits written
170     input:
171     output:
172 
173 *****************************************************************************/
FDKsbrEnc_WriteEnvSingleChannelElement(HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_COMMON_DATA cmonData,UINT sbrSyntaxFlags)174 INT FDKsbrEnc_WriteEnvSingleChannelElement(
175     HANDLE_SBR_HEADER_DATA sbrHeaderData,
176     HANDLE_PARAMETRIC_STEREO hParametricStereo,
177     HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData, HANDLE_SBR_ENV_DATA sbrEnvData,
178     HANDLE_COMMON_DATA cmonData, UINT sbrSyntaxFlags)
179 
180 {
181   INT payloadBits = 0;
182 
183   cmonData->sbrHdrBits = 0;
184   cmonData->sbrDataBits = 0;
185 
186   /* write pure sbr data */
187   if (sbrEnvData != NULL) {
188     /* write header */
189     payloadBits += encodeSbrHeader(sbrHeaderData, sbrBitstreamData, cmonData);
190 
191     /* write data */
192     payloadBits += encodeSbrData(sbrEnvData, NULL, hParametricStereo, cmonData,
193                                  SBR_ID_SCE, 0, sbrSyntaxFlags);
194   }
195   return payloadBits;
196 }
197 
198 /*****************************************************************************
199 
200     functionname: FDKsbrEnc_WriteEnvChannelPairElement
201     description:  writes pure SBR channel pair data element
202     returns:      number of bits written
203     input:
204     output:
205 
206 *****************************************************************************/
FDKsbrEnc_WriteEnvChannelPairElement(HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,HANDLE_SBR_ENV_DATA sbrEnvDataLeft,HANDLE_SBR_ENV_DATA sbrEnvDataRight,HANDLE_COMMON_DATA cmonData,UINT sbrSyntaxFlags)207 INT FDKsbrEnc_WriteEnvChannelPairElement(
208     HANDLE_SBR_HEADER_DATA sbrHeaderData,
209     HANDLE_PARAMETRIC_STEREO hParametricStereo,
210     HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,
211     HANDLE_SBR_ENV_DATA sbrEnvDataLeft, HANDLE_SBR_ENV_DATA sbrEnvDataRight,
212     HANDLE_COMMON_DATA cmonData, UINT sbrSyntaxFlags)
213 
214 {
215   INT payloadBits = 0;
216   cmonData->sbrHdrBits = 0;
217   cmonData->sbrDataBits = 0;
218 
219   /* write pure sbr data */
220   if ((sbrEnvDataLeft != NULL) && (sbrEnvDataRight != NULL)) {
221     /* write header */
222     payloadBits += encodeSbrHeader(sbrHeaderData, sbrBitstreamData, cmonData);
223 
224     /* write data */
225     payloadBits += encodeSbrData(sbrEnvDataLeft, sbrEnvDataRight,
226                                  hParametricStereo, cmonData, SBR_ID_CPE,
227                                  sbrHeaderData->coupling, sbrSyntaxFlags);
228   }
229   return payloadBits;
230 }
231 
FDKsbrEnc_CountSbrChannelPairElement(HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,HANDLE_SBR_ENV_DATA sbrEnvDataLeft,HANDLE_SBR_ENV_DATA sbrEnvDataRight,HANDLE_COMMON_DATA cmonData,UINT sbrSyntaxFlags)232 INT FDKsbrEnc_CountSbrChannelPairElement(
233     HANDLE_SBR_HEADER_DATA sbrHeaderData,
234     HANDLE_PARAMETRIC_STEREO hParametricStereo,
235     HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,
236     HANDLE_SBR_ENV_DATA sbrEnvDataLeft, HANDLE_SBR_ENV_DATA sbrEnvDataRight,
237     HANDLE_COMMON_DATA cmonData, UINT sbrSyntaxFlags) {
238   INT payloadBits;
239   INT bitPos = FDKgetValidBits(&cmonData->sbrBitbuf);
240 
241   payloadBits = FDKsbrEnc_WriteEnvChannelPairElement(
242       sbrHeaderData, hParametricStereo, sbrBitstreamData, sbrEnvDataLeft,
243       sbrEnvDataRight, cmonData, sbrSyntaxFlags);
244 
245   FDKpushBack(&cmonData->sbrBitbuf,
246               (FDKgetValidBits(&cmonData->sbrBitbuf) - bitPos));
247 
248   return payloadBits;
249 }
250 
sbrEncoder_GetHeader(SBR_ENCODER * sbrEncoder,HANDLE_FDK_BITSTREAM hBs,INT element_index,int fSendHeaders)251 void sbrEncoder_GetHeader(SBR_ENCODER *sbrEncoder, HANDLE_FDK_BITSTREAM hBs,
252                           INT element_index, int fSendHeaders) {
253   encodeSbrHeaderData(&sbrEncoder->sbrElement[element_index]->sbrHeaderData,
254                       hBs);
255 
256   if (fSendHeaders == 0) {
257     /* Prevent header being embedded into the SBR payload. */
258     sbrEncoder->sbrElement[element_index]->sbrBitstreamData.NrSendHeaderData =
259         -1;
260     sbrEncoder->sbrElement[element_index]->sbrBitstreamData.HeaderActive = 0;
261     sbrEncoder->sbrElement[element_index]
262         ->sbrBitstreamData.CountSendHeaderData = -1;
263   }
264 }
265 
266 /*****************************************************************************
267 
268     functionname: encodeSbrHeader
269     description:  encodes SBR Header information
270     returns:      number of bits written
271     input:
272     output:
273 
274 *****************************************************************************/
encodeSbrHeader(HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,HANDLE_COMMON_DATA cmonData)275 static INT encodeSbrHeader(HANDLE_SBR_HEADER_DATA sbrHeaderData,
276                            HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData,
277                            HANDLE_COMMON_DATA cmonData) {
278   INT payloadBits = 0;
279 
280   if (sbrBitstreamData->HeaderActive) {
281     payloadBits += FDKwriteBits(&cmonData->sbrBitbuf, 1, 1);
282     payloadBits += encodeSbrHeaderData(sbrHeaderData, &cmonData->sbrBitbuf);
283   } else {
284     payloadBits += FDKwriteBits(&cmonData->sbrBitbuf, 0, 1);
285   }
286 
287   cmonData->sbrHdrBits = payloadBits;
288 
289   return payloadBits;
290 }
291 
292 /*****************************************************************************
293 
294     functionname: encodeSbrHeaderData
295     description:  writes sbr_header()
296                   bs_protocol_version through bs_header_extra_2
297     returns:      number of bits written
298     input:
299     output:
300 
301 *****************************************************************************/
encodeSbrHeaderData(HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_FDK_BITSTREAM hBitStream)302 static INT encodeSbrHeaderData(HANDLE_SBR_HEADER_DATA sbrHeaderData,
303                                HANDLE_FDK_BITSTREAM hBitStream)
304 
305 {
306   INT payloadBits = 0;
307   if (sbrHeaderData != NULL) {
308     payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_amp_res,
309                                 SI_SBR_AMP_RES_BITS);
310     payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_start_frequency,
311                                 SI_SBR_START_FREQ_BITS);
312     payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_stop_frequency,
313                                 SI_SBR_STOP_FREQ_BITS);
314     payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_xover_band,
315                                 SI_SBR_XOVER_BAND_BITS);
316 
317     payloadBits += FDKwriteBits(hBitStream, 0, SI_SBR_RESERVED_BITS);
318 
319     payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->header_extra_1,
320                                 SI_SBR_HEADER_EXTRA_1_BITS);
321     payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->header_extra_2,
322                                 SI_SBR_HEADER_EXTRA_2_BITS);
323 
324     if (sbrHeaderData->header_extra_1) {
325       payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->freqScale,
326                                   SI_SBR_FREQ_SCALE_BITS);
327       payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->alterScale,
328                                   SI_SBR_ALTER_SCALE_BITS);
329       payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_noise_bands,
330                                   SI_SBR_NOISE_BANDS_BITS);
331     } /* sbrHeaderData->header_extra_1 */
332 
333     if (sbrHeaderData->header_extra_2) {
334       payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_limiter_bands,
335                                   SI_SBR_LIMITER_BANDS_BITS);
336       payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_limiter_gains,
337                                   SI_SBR_LIMITER_GAINS_BITS);
338       payloadBits += FDKwriteBits(hBitStream, sbrHeaderData->sbr_interpol_freq,
339                                   SI_SBR_INTERPOL_FREQ_BITS);
340       payloadBits +=
341           FDKwriteBits(hBitStream, sbrHeaderData->sbr_smoothing_length,
342                        SI_SBR_SMOOTHING_LENGTH_BITS);
343 
344     } /* sbrHeaderData->header_extra_2 */
345   }   /* sbrHeaderData != NULL */
346 
347   return payloadBits;
348 }
349 
350 /*****************************************************************************
351 
352     functionname: encodeSbrData
353     description:  encodes sbr Data information
354     returns:      number of bits written
355     input:
356     output:
357 
358 *****************************************************************************/
encodeSbrData(HANDLE_SBR_ENV_DATA sbrEnvDataLeft,HANDLE_SBR_ENV_DATA sbrEnvDataRight,HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_COMMON_DATA cmonData,SBR_ELEMENT_TYPE sbrElem,INT coupling,UINT sbrSyntaxFlags)359 static INT encodeSbrData(HANDLE_SBR_ENV_DATA sbrEnvDataLeft,
360                          HANDLE_SBR_ENV_DATA sbrEnvDataRight,
361                          HANDLE_PARAMETRIC_STEREO hParametricStereo,
362                          HANDLE_COMMON_DATA cmonData, SBR_ELEMENT_TYPE sbrElem,
363                          INT coupling, UINT sbrSyntaxFlags) {
364   INT payloadBits = 0;
365 
366   switch (sbrElem) {
367     case SBR_ID_SCE:
368       payloadBits +=
369           encodeSbrSingleChannelElement(sbrEnvDataLeft, &cmonData->sbrBitbuf,
370                                         hParametricStereo, sbrSyntaxFlags);
371       break;
372     case SBR_ID_CPE:
373       payloadBits += encodeSbrChannelPairElement(
374           sbrEnvDataLeft, sbrEnvDataRight, hParametricStereo,
375           &cmonData->sbrBitbuf, coupling, sbrSyntaxFlags);
376       break;
377     default:
378       /* we never should apply SBR to any other element type */
379       FDK_ASSERT(0);
380   }
381 
382   cmonData->sbrDataBits = payloadBits;
383 
384   return payloadBits;
385 }
386 
387 #define MODE_FREQ_TANS 1
388 #define MODE_NO_FREQ_TRAN 0
389 #define LD_TRANSMISSION MODE_FREQ_TANS
encodeFreqs(int mode)390 static int encodeFreqs(int mode) { return ((mode & MODE_FREQ_TANS) ? 1 : 0); }
391 
392 /*****************************************************************************
393 
394     functionname: encodeSbrSingleChannelElement
395     description:  encodes sbr SCE information
396     returns:      number of bits written
397     input:
398     output:
399 
400 *****************************************************************************/
encodeSbrSingleChannelElement(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream,HANDLE_PARAMETRIC_STEREO hParametricStereo,const UINT sbrSyntaxFlags)401 static INT encodeSbrSingleChannelElement(
402     HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream,
403     HANDLE_PARAMETRIC_STEREO hParametricStereo, const UINT sbrSyntaxFlags) {
404   INT i, payloadBits = 0;
405 
406   payloadBits += FDKwriteBits(hBitStream, 0,
407                               SI_SBR_DATA_EXTRA_BITS); /* no reserved bits */
408 
409   if (sbrEnvData->ldGrid) {
410     if (sbrEnvData->hSbrBSGrid->frameClass != FIXFIXonly) {
411       /* encode normal SbrGrid */
412       payloadBits += encodeSbrGrid(sbrEnvData, hBitStream);
413     } else {
414       /* use FIXFIXonly frame Grid */
415       payloadBits += encodeLowDelaySbrGrid(
416           sbrEnvData, hBitStream, encodeFreqs(LD_TRANSMISSION), sbrSyntaxFlags);
417     }
418   } else {
419     if (sbrSyntaxFlags & SBR_SYNTAX_SCALABLE) {
420       payloadBits += FDKwriteBits(hBitStream, 1, SI_SBR_COUPLING_BITS);
421     }
422     payloadBits += encodeSbrGrid(sbrEnvData, hBitStream);
423   }
424 
425   payloadBits += encodeSbrDtdf(sbrEnvData, hBitStream);
426 
427   for (i = 0; i < sbrEnvData->noOfnoisebands; i++) {
428     payloadBits += FDKwriteBits(hBitStream, sbrEnvData->sbr_invf_mode_vec[i],
429                                 SI_SBR_INVF_MODE_BITS);
430   }
431 
432   payloadBits += writeEnvelopeData(sbrEnvData, hBitStream, 0);
433   payloadBits += writeNoiseLevelData(sbrEnvData, hBitStream, 0);
434 
435   payloadBits += writeSyntheticCodingData(sbrEnvData, hBitStream);
436 
437   payloadBits += encodeExtendedData(hParametricStereo, hBitStream);
438 
439   return payloadBits;
440 }
441 
442 /*****************************************************************************
443 
444     functionname: encodeSbrChannelPairElement
445     description:  encodes sbr CPE information
446     returns:
447     input:
448     output:
449 
450 *****************************************************************************/
encodeSbrChannelPairElement(HANDLE_SBR_ENV_DATA sbrEnvDataLeft,HANDLE_SBR_ENV_DATA sbrEnvDataRight,HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_FDK_BITSTREAM hBitStream,const INT coupling,const UINT sbrSyntaxFlags)451 static INT encodeSbrChannelPairElement(
452     HANDLE_SBR_ENV_DATA sbrEnvDataLeft, HANDLE_SBR_ENV_DATA sbrEnvDataRight,
453     HANDLE_PARAMETRIC_STEREO hParametricStereo, HANDLE_FDK_BITSTREAM hBitStream,
454     const INT coupling, const UINT sbrSyntaxFlags) {
455   INT payloadBits = 0;
456   INT i = 0;
457 
458   payloadBits += FDKwriteBits(hBitStream, 0,
459                               SI_SBR_DATA_EXTRA_BITS); /* no reserved bits */
460 
461   payloadBits += FDKwriteBits(hBitStream, coupling, SI_SBR_COUPLING_BITS);
462 
463   if (coupling) {
464     if (sbrEnvDataLeft->ldGrid) {
465       if (sbrEnvDataLeft->hSbrBSGrid->frameClass != FIXFIXonly) {
466         /* normal SbrGrid */
467         payloadBits += encodeSbrGrid(sbrEnvDataLeft, hBitStream);
468 
469       } else {
470         /* FIXFIXonly frame Grid */
471         payloadBits +=
472             encodeLowDelaySbrGrid(sbrEnvDataLeft, hBitStream,
473                                   encodeFreqs(LD_TRANSMISSION), sbrSyntaxFlags);
474       }
475     } else
476       payloadBits += encodeSbrGrid(sbrEnvDataLeft, hBitStream);
477 
478     payloadBits += encodeSbrDtdf(sbrEnvDataLeft, hBitStream);
479     payloadBits += encodeSbrDtdf(sbrEnvDataRight, hBitStream);
480 
481     for (i = 0; i < sbrEnvDataLeft->noOfnoisebands; i++) {
482       payloadBits +=
483           FDKwriteBits(hBitStream, sbrEnvDataLeft->sbr_invf_mode_vec[i],
484                        SI_SBR_INVF_MODE_BITS);
485     }
486 
487     payloadBits += writeEnvelopeData(sbrEnvDataLeft, hBitStream, 1);
488     payloadBits += writeNoiseLevelData(sbrEnvDataLeft, hBitStream, 1);
489     payloadBits += writeEnvelopeData(sbrEnvDataRight, hBitStream, 1);
490     payloadBits += writeNoiseLevelData(sbrEnvDataRight, hBitStream, 1);
491 
492     payloadBits += writeSyntheticCodingData(sbrEnvDataLeft, hBitStream);
493     payloadBits += writeSyntheticCodingData(sbrEnvDataRight, hBitStream);
494 
495   } else { /* no coupling */
496     FDK_ASSERT(sbrEnvDataLeft->ldGrid == sbrEnvDataRight->ldGrid);
497 
498     if (sbrEnvDataLeft->ldGrid || sbrEnvDataRight->ldGrid) {
499       /* sbrEnvDataLeft (left channel) */
500       if (sbrEnvDataLeft->hSbrBSGrid->frameClass != FIXFIXonly) {
501         /* no FIXFIXonly Frame so we dont need encodeLowDelaySbrGrid */
502         /* normal SbrGrid */
503         payloadBits += encodeSbrGrid(sbrEnvDataLeft, hBitStream);
504 
505       } else {
506         /* FIXFIXonly frame Grid */
507         payloadBits +=
508             encodeLowDelaySbrGrid(sbrEnvDataLeft, hBitStream,
509                                   encodeFreqs(LD_TRANSMISSION), sbrSyntaxFlags);
510       }
511 
512       /* sbrEnvDataRight (right channel) */
513       if (sbrEnvDataRight->hSbrBSGrid->frameClass != FIXFIXonly) {
514         /* no FIXFIXonly Frame so we dont need encodeLowDelaySbrGrid */
515         /* normal SbrGrid */
516         payloadBits += encodeSbrGrid(sbrEnvDataRight, hBitStream);
517 
518       } else {
519         /* FIXFIXonly frame Grid */
520         payloadBits +=
521             encodeLowDelaySbrGrid(sbrEnvDataRight, hBitStream,
522                                   encodeFreqs(LD_TRANSMISSION), sbrSyntaxFlags);
523       }
524     } else {
525       payloadBits += encodeSbrGrid(sbrEnvDataLeft, hBitStream);
526       payloadBits += encodeSbrGrid(sbrEnvDataRight, hBitStream);
527     }
528     payloadBits += encodeSbrDtdf(sbrEnvDataLeft, hBitStream);
529     payloadBits += encodeSbrDtdf(sbrEnvDataRight, hBitStream);
530 
531     for (i = 0; i < sbrEnvDataLeft->noOfnoisebands; i++) {
532       payloadBits +=
533           FDKwriteBits(hBitStream, sbrEnvDataLeft->sbr_invf_mode_vec[i],
534                        SI_SBR_INVF_MODE_BITS);
535     }
536     for (i = 0; i < sbrEnvDataRight->noOfnoisebands; i++) {
537       payloadBits +=
538           FDKwriteBits(hBitStream, sbrEnvDataRight->sbr_invf_mode_vec[i],
539                        SI_SBR_INVF_MODE_BITS);
540     }
541 
542     payloadBits += writeEnvelopeData(sbrEnvDataLeft, hBitStream, 0);
543     payloadBits += writeEnvelopeData(sbrEnvDataRight, hBitStream, 0);
544     payloadBits += writeNoiseLevelData(sbrEnvDataLeft, hBitStream, 0);
545     payloadBits += writeNoiseLevelData(sbrEnvDataRight, hBitStream, 0);
546 
547     payloadBits += writeSyntheticCodingData(sbrEnvDataLeft, hBitStream);
548     payloadBits += writeSyntheticCodingData(sbrEnvDataRight, hBitStream);
549 
550   } /* coupling */
551 
552   payloadBits += encodeExtendedData(hParametricStereo, hBitStream);
553 
554   return payloadBits;
555 }
556 
ceil_ln2(INT x)557 static INT ceil_ln2(INT x) {
558   INT tmp = -1;
559   while ((1 << ++tmp) < x)
560     ;
561   return (tmp);
562 }
563 
564 /*****************************************************************************
565 
566     functionname: encodeSbrGrid
567     description:  if hBitStream != NULL writes bits that describes the
568                   time/frequency grouping of a frame; else counts them only
569     returns:      number of bits written or counted
570     input:
571     output:
572 
573 *****************************************************************************/
encodeSbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream)574 static INT encodeSbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData,
575                          HANDLE_FDK_BITSTREAM hBitStream) {
576   INT payloadBits = 0;
577   INT i, temp;
578   INT bufferFrameStart = sbrEnvData->hSbrBSGrid->bufferFrameStart;
579   INT numberTimeSlots = sbrEnvData->hSbrBSGrid->numberTimeSlots;
580 
581   if (sbrEnvData->ldGrid)
582     payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->frameClass,
583                                 SBR_CLA_BITS_LD);
584   else
585     payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->frameClass,
586                                 SBR_CLA_BITS);
587 
588   switch (sbrEnvData->hSbrBSGrid->frameClass) {
589     case FIXFIXonly:
590       FDK_ASSERT(0 /* Fatal error in encodeSbrGrid! */);
591       break;
592     case FIXFIX:
593       temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_env);
594       payloadBits += FDKwriteBits(hBitStream, temp, SBR_ENV_BITS);
595       if ((sbrEnvData->ldGrid) && (sbrEnvData->hSbrBSGrid->bs_num_env == 1))
596         payloadBits += FDKwriteBits(hBitStream, sbrEnvData->currentAmpResFF,
597                                     SI_SBR_AMP_RES_BITS);
598       payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->v_f[0],
599                                   SBR_RES_BITS);
600 
601       break;
602 
603     case FIXVAR:
604     case VARFIX:
605       if (sbrEnvData->hSbrBSGrid->frameClass == FIXVAR)
606         temp = sbrEnvData->hSbrBSGrid->bs_abs_bord -
607                (bufferFrameStart + numberTimeSlots);
608       else
609         temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - bufferFrameStart;
610 
611       payloadBits += FDKwriteBits(hBitStream, temp, SBR_ABS_BITS);
612       payloadBits +=
613           FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->n, SBR_NUM_BITS);
614 
615       for (i = 0; i < sbrEnvData->hSbrBSGrid->n; i++) {
616         temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord[i] - 2) >> 1;
617         payloadBits += FDKwriteBits(hBitStream, temp, SBR_REL_BITS);
618       }
619 
620       temp = ceil_ln2(sbrEnvData->hSbrBSGrid->n + 2);
621       payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->p, temp);
622 
623       for (i = 0; i < sbrEnvData->hSbrBSGrid->n + 1; i++) {
624         payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->v_f[i],
625                                     SBR_RES_BITS);
626       }
627       break;
628 
629     case VARVAR:
630       temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_0 - bufferFrameStart;
631       payloadBits += FDKwriteBits(hBitStream, temp, SBR_ABS_BITS);
632       temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_1 -
633              (bufferFrameStart + numberTimeSlots);
634       payloadBits += FDKwriteBits(hBitStream, temp, SBR_ABS_BITS);
635 
636       payloadBits += FDKwriteBits(
637           hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_0, SBR_NUM_BITS);
638       payloadBits += FDKwriteBits(
639           hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_1, SBR_NUM_BITS);
640 
641       for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_0; i++) {
642         temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_0[i] - 2) >> 1;
643         payloadBits += FDKwriteBits(hBitStream, temp, SBR_REL_BITS);
644       }
645 
646       for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_1; i++) {
647         temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_1[i] - 2) >> 1;
648         payloadBits += FDKwriteBits(hBitStream, temp, SBR_REL_BITS);
649       }
650 
651       temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_rel_0 +
652                       sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 2);
653       payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->p, temp);
654 
655       temp = sbrEnvData->hSbrBSGrid->bs_num_rel_0 +
656              sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 1;
657 
658       for (i = 0; i < temp; i++) {
659         payloadBits += FDKwriteBits(
660             hBitStream, sbrEnvData->hSbrBSGrid->v_fLR[i], SBR_RES_BITS);
661       }
662       break;
663   }
664 
665   return payloadBits;
666 }
667 
668 #define SBR_CLA_BITS_LD 1
669 /*****************************************************************************
670 
671     functionname: encodeLowDelaySbrGrid
672     description:  if hBitStream != NULL writes bits that describes the
673                   time/frequency grouping of a frame;
674                   else counts them only
675                   (this function only write the FIXFIXonly Bitstream data)
676     returns:      number of bits written or counted
677     input:
678     output:
679 
680 *****************************************************************************/
encodeLowDelaySbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream,const int transmitFreqs,const UINT sbrSyntaxFlags)681 static int encodeLowDelaySbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData,
682                                  HANDLE_FDK_BITSTREAM hBitStream,
683                                  const int transmitFreqs,
684                                  const UINT sbrSyntaxFlags) {
685   int payloadBits = 0;
686   int i;
687 
688   /* write FIXFIXonly Grid */
689   /* write frameClass [1 bit] for FIXFIXonly Grid */
690   payloadBits += FDKwriteBits(hBitStream, 1, SBR_CLA_BITS_LD);
691 
692   /* absolute Borders are fix: 0,X,X,X,nTimeSlots; so we dont have to transmit
693    * them */
694   /* only transmit the transient position! */
695   /* with this info (b1) we can reconstruct the Frame on Decoder side : */
696   /* border[0] = 0; border[1] = b1; border[2]=b1+2; border[3] = nrTimeSlots */
697 
698   /* use 3 or 4bits for transient border (border) */
699   if (sbrEnvData->hSbrBSGrid->numberTimeSlots == 8)
700     payloadBits +=
701         FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->bs_abs_bord, 3);
702   else
703     payloadBits +=
704         FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->bs_abs_bord, 4);
705 
706   if (transmitFreqs) {
707     /* write FreqRes grid */
708     for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_env; i++) {
709       payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->v_f[i],
710                                   SBR_RES_BITS);
711     }
712   }
713 
714   return payloadBits;
715 }
716 
717 /*****************************************************************************
718 
719     functionname: encodeSbrDtdf
720     description:  writes bits that describes the direction of the envelopes of a
721 frame returns:      number of bits written input: output:
722 
723 *****************************************************************************/
encodeSbrDtdf(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream)724 static INT encodeSbrDtdf(HANDLE_SBR_ENV_DATA sbrEnvData,
725                          HANDLE_FDK_BITSTREAM hBitStream) {
726   INT i, payloadBits = 0, noOfNoiseEnvelopes;
727 
728   noOfNoiseEnvelopes = sbrEnvData->noOfEnvelopes > 1 ? 2 : 1;
729 
730   for (i = 0; i < sbrEnvData->noOfEnvelopes; ++i) {
731     payloadBits +=
732         FDKwriteBits(hBitStream, sbrEnvData->domain_vec[i], SBR_DIR_BITS);
733   }
734   for (i = 0; i < noOfNoiseEnvelopes; ++i) {
735     payloadBits +=
736         FDKwriteBits(hBitStream, sbrEnvData->domain_vec_noise[i], SBR_DIR_BITS);
737   }
738 
739   return payloadBits;
740 }
741 
742 /*****************************************************************************
743 
744     functionname: writeNoiseLevelData
745     description:  writes bits corresponding to the noise-floor-level
746     returns:      number of bits written
747     input:
748     output:
749 
750 *****************************************************************************/
writeNoiseLevelData(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream,INT coupling)751 static INT writeNoiseLevelData(HANDLE_SBR_ENV_DATA sbrEnvData,
752                                HANDLE_FDK_BITSTREAM hBitStream, INT coupling) {
753   INT j, i, payloadBits = 0;
754   INT nNoiseEnvelopes = sbrEnvData->noOfEnvelopes > 1 ? 2 : 1;
755 
756   for (i = 0; i < nNoiseEnvelopes; i++) {
757     switch (sbrEnvData->domain_vec_noise[i]) {
758       case FREQ:
759         if (coupling && sbrEnvData->balance) {
760           payloadBits += FDKwriteBits(
761               hBitStream,
762               sbrEnvData->sbr_noise_levels[i * sbrEnvData->noOfnoisebands],
763               sbrEnvData->si_sbr_start_noise_bits_balance);
764         } else {
765           payloadBits += FDKwriteBits(
766               hBitStream,
767               sbrEnvData->sbr_noise_levels[i * sbrEnvData->noOfnoisebands],
768               sbrEnvData->si_sbr_start_noise_bits);
769         }
770 
771         for (j = 1 + i * sbrEnvData->noOfnoisebands;
772              j < (sbrEnvData->noOfnoisebands * (1 + i)); j++) {
773           if (coupling) {
774             if (sbrEnvData->balance) {
775               /* coupling && balance */
776               payloadBits += FDKwriteBits(hBitStream,
777                                           sbrEnvData->hufftableNoiseBalanceFreqC
778                                               [sbrEnvData->sbr_noise_levels[j] +
779                                                CODE_BOOK_SCF_LAV_BALANCE11],
780                                           sbrEnvData->hufftableNoiseBalanceFreqL
781                                               [sbrEnvData->sbr_noise_levels[j] +
782                                                CODE_BOOK_SCF_LAV_BALANCE11]);
783             } else {
784               /* coupling && !balance */
785               payloadBits += FDKwriteBits(
786                   hBitStream,
787                   sbrEnvData->hufftableNoiseLevelFreqC
788                       [sbrEnvData->sbr_noise_levels[j] + CODE_BOOK_SCF_LAV11],
789                   sbrEnvData->hufftableNoiseLevelFreqL
790                       [sbrEnvData->sbr_noise_levels[j] + CODE_BOOK_SCF_LAV11]);
791             }
792           } else {
793             /* !coupling */
794             payloadBits += FDKwriteBits(
795                 hBitStream,
796                 sbrEnvData
797                     ->hufftableNoiseFreqC[sbrEnvData->sbr_noise_levels[j] +
798                                           CODE_BOOK_SCF_LAV11],
799                 sbrEnvData
800                     ->hufftableNoiseFreqL[sbrEnvData->sbr_noise_levels[j] +
801                                           CODE_BOOK_SCF_LAV11]);
802           }
803         }
804         break;
805 
806       case TIME:
807         for (j = i * sbrEnvData->noOfnoisebands;
808              j < (sbrEnvData->noOfnoisebands * (1 + i)); j++) {
809           if (coupling) {
810             if (sbrEnvData->balance) {
811               /* coupling && balance */
812               payloadBits += FDKwriteBits(hBitStream,
813                                           sbrEnvData->hufftableNoiseBalanceTimeC
814                                               [sbrEnvData->sbr_noise_levels[j] +
815                                                CODE_BOOK_SCF_LAV_BALANCE11],
816                                           sbrEnvData->hufftableNoiseBalanceTimeL
817                                               [sbrEnvData->sbr_noise_levels[j] +
818                                                CODE_BOOK_SCF_LAV_BALANCE11]);
819             } else {
820               /* coupling && !balance */
821               payloadBits += FDKwriteBits(
822                   hBitStream,
823                   sbrEnvData->hufftableNoiseLevelTimeC
824                       [sbrEnvData->sbr_noise_levels[j] + CODE_BOOK_SCF_LAV11],
825                   sbrEnvData->hufftableNoiseLevelTimeL
826                       [sbrEnvData->sbr_noise_levels[j] + CODE_BOOK_SCF_LAV11]);
827             }
828           } else {
829             /* !coupling */
830             payloadBits += FDKwriteBits(
831                 hBitStream,
832                 sbrEnvData
833                     ->hufftableNoiseLevelTimeC[sbrEnvData->sbr_noise_levels[j] +
834                                                CODE_BOOK_SCF_LAV11],
835                 sbrEnvData
836                     ->hufftableNoiseLevelTimeL[sbrEnvData->sbr_noise_levels[j] +
837                                                CODE_BOOK_SCF_LAV11]);
838           }
839         }
840         break;
841     }
842   }
843   return payloadBits;
844 }
845 
846 /*****************************************************************************
847 
848     functionname: writeEnvelopeData
849     description:  writes bits corresponding to the envelope
850     returns:      number of bits written
851     input:
852     output:
853 
854 *****************************************************************************/
writeEnvelopeData(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream,INT coupling)855 static INT writeEnvelopeData(HANDLE_SBR_ENV_DATA sbrEnvData,
856                              HANDLE_FDK_BITSTREAM hBitStream, INT coupling) {
857   INT payloadBits = 0, j, i, delta;
858 
859   for (j = 0; j < sbrEnvData->noOfEnvelopes;
860        j++) { /* loop over all envelopes */
861     if (sbrEnvData->domain_vec[j] == FREQ) {
862       if (coupling && sbrEnvData->balance) {
863         payloadBits += FDKwriteBits(hBitStream, sbrEnvData->ienvelope[j][0],
864                                     sbrEnvData->si_sbr_start_env_bits_balance);
865       } else {
866         payloadBits += FDKwriteBits(hBitStream, sbrEnvData->ienvelope[j][0],
867                                     sbrEnvData->si_sbr_start_env_bits);
868       }
869     }
870 
871     for (i = 1 - sbrEnvData->domain_vec[j]; i < sbrEnvData->noScfBands[j];
872          i++) {
873       delta = sbrEnvData->ienvelope[j][i];
874       if (coupling && sbrEnvData->balance) {
875         FDK_ASSERT(fixp_abs(delta) <= sbrEnvData->codeBookScfLavBalance);
876       } else {
877         FDK_ASSERT(fixp_abs(delta) <= sbrEnvData->codeBookScfLav);
878       }
879       if (coupling) {
880         if (sbrEnvData->balance) {
881           if (sbrEnvData->domain_vec[j]) {
882             /* coupling && balance && TIME */
883             payloadBits += FDKwriteBits(
884                 hBitStream,
885                 sbrEnvData
886                     ->hufftableBalanceTimeC[delta +
887                                             sbrEnvData->codeBookScfLavBalance],
888                 sbrEnvData
889                     ->hufftableBalanceTimeL[delta +
890                                             sbrEnvData->codeBookScfLavBalance]);
891           } else {
892             /* coupling && balance && FREQ */
893             payloadBits += FDKwriteBits(
894                 hBitStream,
895                 sbrEnvData
896                     ->hufftableBalanceFreqC[delta +
897                                             sbrEnvData->codeBookScfLavBalance],
898                 sbrEnvData
899                     ->hufftableBalanceFreqL[delta +
900                                             sbrEnvData->codeBookScfLavBalance]);
901           }
902         } else {
903           if (sbrEnvData->domain_vec[j]) {
904             /* coupling && !balance && TIME */
905             payloadBits += FDKwriteBits(
906                 hBitStream,
907                 sbrEnvData
908                     ->hufftableLevelTimeC[delta + sbrEnvData->codeBookScfLav],
909                 sbrEnvData
910                     ->hufftableLevelTimeL[delta + sbrEnvData->codeBookScfLav]);
911           } else {
912             /* coupling && !balance && FREQ */
913             payloadBits += FDKwriteBits(
914                 hBitStream,
915                 sbrEnvData
916                     ->hufftableLevelFreqC[delta + sbrEnvData->codeBookScfLav],
917                 sbrEnvData
918                     ->hufftableLevelFreqL[delta + sbrEnvData->codeBookScfLav]);
919           }
920         }
921       } else {
922         if (sbrEnvData->domain_vec[j]) {
923           /* !coupling && TIME */
924           payloadBits += FDKwriteBits(
925               hBitStream,
926               sbrEnvData->hufftableTimeC[delta + sbrEnvData->codeBookScfLav],
927               sbrEnvData->hufftableTimeL[delta + sbrEnvData->codeBookScfLav]);
928         } else {
929           /* !coupling && FREQ */
930           payloadBits += FDKwriteBits(
931               hBitStream,
932               sbrEnvData->hufftableFreqC[delta + sbrEnvData->codeBookScfLav],
933               sbrEnvData->hufftableFreqL[delta + sbrEnvData->codeBookScfLav]);
934         }
935       }
936     }
937   }
938   return payloadBits;
939 }
940 
941 /*****************************************************************************
942 
943     functionname: encodeExtendedData
944     description:  writes bits corresponding to the extended data
945     returns:      number of bits written
946     input:
947     output:
948 
949 *****************************************************************************/
encodeExtendedData(HANDLE_PARAMETRIC_STEREO hParametricStereo,HANDLE_FDK_BITSTREAM hBitStream)950 static INT encodeExtendedData(HANDLE_PARAMETRIC_STEREO hParametricStereo,
951                               HANDLE_FDK_BITSTREAM hBitStream) {
952   INT extDataSize;
953   INT payloadBits = 0;
954 
955   extDataSize = getSbrExtendedDataSize(hParametricStereo);
956 
957   if (extDataSize != 0) {
958     INT maxExtSize = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
959     INT writtenNoBits = 0; /* needed to byte align the extended data */
960 
961     payloadBits += FDKwriteBits(hBitStream, 1, SI_SBR_EXTENDED_DATA_BITS);
962     FDK_ASSERT(extDataSize <= SBR_EXTENDED_DATA_MAX_CNT);
963 
964     if (extDataSize < maxExtSize) {
965       payloadBits +=
966           FDKwriteBits(hBitStream, extDataSize, SI_SBR_EXTENSION_SIZE_BITS);
967     } else {
968       payloadBits +=
969           FDKwriteBits(hBitStream, maxExtSize, SI_SBR_EXTENSION_SIZE_BITS);
970       payloadBits += FDKwriteBits(hBitStream, extDataSize - maxExtSize,
971                                   SI_SBR_EXTENSION_ESC_COUNT_BITS);
972     }
973 
974     /* parametric coding signalled here? */
975     if (hParametricStereo) {
976       writtenNoBits += FDKwriteBits(hBitStream, EXTENSION_ID_PS_CODING,
977                                     SI_SBR_EXTENSION_ID_BITS);
978       writtenNoBits +=
979           FDKsbrEnc_PSEnc_WritePSData(hParametricStereo, hBitStream);
980     }
981 
982     payloadBits += writtenNoBits;
983 
984     /* byte alignment */
985     writtenNoBits = writtenNoBits % 8;
986     if (writtenNoBits)
987       payloadBits += FDKwriteBits(hBitStream, 0, (8 - writtenNoBits));
988   } else {
989     payloadBits += FDKwriteBits(hBitStream, 0, SI_SBR_EXTENDED_DATA_BITS);
990   }
991 
992   return payloadBits;
993 }
994 
995 /*****************************************************************************
996 
997     functionname: writeSyntheticCodingData
998     description:  writes bits corresponding to the "synthetic-coding"-extension
999     returns:      number of bits written
1000     input:
1001     output:
1002 
1003 *****************************************************************************/
writeSyntheticCodingData(HANDLE_SBR_ENV_DATA sbrEnvData,HANDLE_FDK_BITSTREAM hBitStream)1004 static INT writeSyntheticCodingData(HANDLE_SBR_ENV_DATA sbrEnvData,
1005                                     HANDLE_FDK_BITSTREAM hBitStream)
1006 
1007 {
1008   INT i;
1009   INT payloadBits = 0;
1010 
1011   payloadBits += FDKwriteBits(hBitStream, sbrEnvData->addHarmonicFlag, 1);
1012 
1013   if (sbrEnvData->addHarmonicFlag) {
1014     for (i = 0; i < sbrEnvData->noHarmonics; i++) {
1015       payloadBits += FDKwriteBits(hBitStream, sbrEnvData->addHarmonic[i], 1);
1016     }
1017   }
1018 
1019   return payloadBits;
1020 }
1021 
1022 /*****************************************************************************
1023 
1024     functionname: getSbrExtendedDataSize
1025     description:  counts the number of bits needed for encoding the
1026                   extended data (including extension id)
1027 
1028     returns:      number of bits needed for the extended data
1029     input:
1030     output:
1031 
1032 *****************************************************************************/
getSbrExtendedDataSize(HANDLE_PARAMETRIC_STEREO hParametricStereo)1033 static INT getSbrExtendedDataSize(HANDLE_PARAMETRIC_STEREO hParametricStereo) {
1034   INT extDataBits = 0;
1035 
1036   /* add your new extended data counting methods here */
1037 
1038   /*
1039     no extended data
1040   */
1041 
1042   if (hParametricStereo) {
1043     /* PS extended data */
1044     extDataBits += SI_SBR_EXTENSION_ID_BITS;
1045     extDataBits += FDKsbrEnc_PSEnc_WritePSData(hParametricStereo, NULL);
1046   }
1047 
1048   return (extDataBits + 7) >> 3;
1049 }
1050