stat_bits.c revision e2e838afcf03e603a41a0455846eaf9614537c16
1/*
2 ** Copyright 2003-2010, VisualOn, Inc.
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 express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16/*******************************************************************************
17	File:		stat_bits.c
18
19	Content:	Static bit counter functions
20
21*******************************************************************************/
22
23#include "stat_bits.h"
24#include "bitenc.h"
25#include "tns.h"
26
27
28typedef enum {
29  SI_ID_BITS                =(3),
30  SI_FILL_COUNT_BITS        =(4),
31  SI_FILL_ESC_COUNT_BITS    =(8),
32  SI_FILL_EXTENTION_BITS    =(4),
33  SI_FILL_NIBBLE_BITS       =(4),
34  SI_SCE_BITS               =(4),
35  SI_CPE_BITS               =(5),
36  SI_CPE_MS_MASK_BITS       =(2) ,
37  SI_ICS_INFO_BITS_LONG     =(1+2+1+6+1),
38  SI_ICS_INFO_BITS_SHORT    =(1+2+1+4+7),
39  SI_ICS_BITS               =(8+1+1+1)
40} SI_BITS;
41
42
43/*********************************************************************************
44*
45* function name: countMsMaskBits
46* description:   count ms stereo bits demand
47*
48**********************************************************************************/
49static Word16 countMsMaskBits(Word16   sfbCnt,
50                              Word16   sfbPerGroup,
51                              Word16   maxSfbPerGroup,
52                              struct TOOLSINFO *toolsInfo)
53{
54  Word16 msBits, sfbOff, sfb;
55  msBits = 0;
56
57
58  switch(toolsInfo->msDigest) {
59    case MS_NONE:
60    case MS_ALL:
61      break;
62
63    case MS_SOME:
64      for(sfbOff=0; sfbOff<sfbCnt; sfbOff+=sfbPerGroup)
65        for(sfb=0; sfb<maxSfbPerGroup; sfb++)
66          msBits += 1;
67      break;
68  }
69  return(msBits);
70}
71
72/*********************************************************************************
73*
74* function name: tnsCount
75* description:   count tns bit demand  core function
76*
77**********************************************************************************/
78static Word16 tnsCount(TNS_INFO *tnsInfo, Word16 blockType)
79{
80
81  Word32 i, k;
82  Flag tnsPresent;
83  Word32 numOfWindows;
84  Word32 count;
85  Word32 coefBits;
86  Word16 *ptcoef;
87
88  count = 0;
89
90  if (blockType == 2)
91    numOfWindows = 8;
92  else
93    numOfWindows = 1;
94  tnsPresent = 0;
95
96  for (i=0; i<numOfWindows; i++) {
97
98    if (tnsInfo->tnsActive[i]!=0) {
99      tnsPresent = 1;
100    }
101  }
102
103  if (tnsPresent) {
104    /* there is data to be written*/
105    /*count += 1; */
106    for (i=0; i<numOfWindows; i++) {
107
108      if (blockType == 2)
109        count += 1;
110      else
111        count += 2;
112
113      if (tnsInfo->tnsActive[i]) {
114        count += 1;
115
116        if (blockType == 2) {
117          count += 4;
118          count += 3;
119        }
120        else {
121          count += 6;
122          count += 5;
123        }
124
125        if (tnsInfo->order[i]) {
126          count += 1; /*direction*/
127          count += 1; /*coef_compression */
128
129          if (tnsInfo->coefRes[i] == 4) {
130            ptcoef = tnsInfo->coef + i*TNS_MAX_ORDER_SHORT;
131			coefBits = 3;
132            for(k=0; k<tnsInfo->order[i]; k++) {
133
134              if ((ptcoef[k] > 3) || (ptcoef[k] < -4)) {
135                coefBits = 4;
136                break;
137              }
138            }
139          }
140          else {
141            coefBits = 2;
142            ptcoef = tnsInfo->coef + i*TNS_MAX_ORDER_SHORT;
143			for(k=0; k<tnsInfo->order[i]; k++) {
144
145              if ((ptcoef[k] > 1) || (ptcoef[k] < -2)) {
146                coefBits = 3;
147                break;
148              }
149            }
150          }
151          for (k=0; k<tnsInfo->order[i]; k++ ) {
152            count += coefBits;
153          }
154        }
155      }
156    }
157  }
158
159  return count;
160}
161
162/**********************************************************************************
163*
164* function name: countTnsBits
165* description:   count tns bit demand
166*
167**********************************************************************************/
168static Word16 countTnsBits(TNS_INFO *tnsInfo,Word16 blockType)
169{
170  return(tnsCount(tnsInfo, blockType));
171}
172
173/*********************************************************************************
174*
175* function name: countStaticBitdemand
176* description:   count static bit demand include tns
177*
178**********************************************************************************/
179Word16 countStaticBitdemand(PSY_OUT_CHANNEL psyOutChannel[MAX_CHANNELS],
180                            PSY_OUT_ELEMENT *psyOutElement,
181                            Word16 channels,
182							Word16 adtsUsed)
183{
184  Word32 statBits;
185  Word32 ch;
186
187  statBits = 0;
188
189  /* if adts used, add 56 bits */
190  if(adtsUsed) statBits += 56;
191
192
193  switch (channels) {
194    case 1:
195      statBits += SI_ID_BITS+SI_SCE_BITS+SI_ICS_BITS;
196      statBits += countTnsBits(&(psyOutChannel[0].tnsInfo),
197                               psyOutChannel[0].windowSequence);
198
199      switch(psyOutChannel[0].windowSequence){
200        case LONG_WINDOW:
201        case START_WINDOW:
202        case STOP_WINDOW:
203          statBits += SI_ICS_INFO_BITS_LONG;
204          break;
205        case SHORT_WINDOW:
206          statBits += SI_ICS_INFO_BITS_SHORT;
207          break;
208      }
209      break;
210    case 2:
211      statBits += SI_ID_BITS+SI_CPE_BITS+2*SI_ICS_BITS;
212
213      statBits += SI_CPE_MS_MASK_BITS;
214      statBits += countMsMaskBits(psyOutChannel[0].sfbCnt,
215								  psyOutChannel[0].sfbPerGroup,
216								  psyOutChannel[0].maxSfbPerGroup,
217								  &psyOutElement->toolsInfo);
218
219      switch (psyOutChannel[0].windowSequence) {
220        case LONG_WINDOW:
221        case START_WINDOW:
222        case STOP_WINDOW:
223          statBits += SI_ICS_INFO_BITS_LONG;
224          break;
225        case SHORT_WINDOW:
226          statBits += SI_ICS_INFO_BITS_SHORT;
227          break;
228      }
229      for(ch=0; ch<2; ch++)
230        statBits += countTnsBits(&(psyOutChannel[ch].tnsInfo),
231                                 psyOutChannel[ch].windowSequence);
232      break;
233  }
234
235  return statBits;
236}
237
238