[go: nahoru, domu]

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:		grp_data.c
18
19	Content:	Short block grouping function
20
21*******************************************************************************/
22
23#include "basic_op.h"
24#include "psy_const.h"
25#include "interface.h"
26#include "grp_data.h"
27
28/*****************************************************************************
29*
30* function name: groupShortData
31* description:  group short data for next quantization and coding
32*
33**********************************************************************************/
34void
35groupShortData(Word32        *mdctSpectrum,
36               Word32        *tmpSpectrum,
37               SFB_THRESHOLD *sfbThreshold,
38               SFB_ENERGY    *sfbEnergy,
39               SFB_ENERGY    *sfbEnergyMS,
40               SFB_ENERGY    *sfbSpreadedEnergy,
41               const Word16   sfbCnt,
42               const Word16  *sfbOffset,
43               const Word16  *sfbMinSnr,
44               Word16        *groupedSfbOffset,
45               Word16        *maxSfbPerGroup,
46               Word16        *groupedSfbMinSnr,
47               const Word16   noOfGroups,
48               const Word16  *groupLen)
49{
50  Word32 i, j;
51  Word32 line;
52  Word32 sfb;
53  Word32 grp;
54  Word32 wnd;
55  Word32 offset;
56  Word32 highestSfb;
57
58  /* for short: regroup and  */
59  /* cumulate energies und thresholds group-wise . */
60
61  /* calculate sfbCnt */
62  highestSfb = 0;
63  for (wnd=0; wnd<TRANS_FAC; wnd++) {
64    for (sfb=sfbCnt - 1; sfb>=highestSfb; sfb--) {
65      for (line=(sfbOffset[sfb + 1] - 1); line>=sfbOffset[sfb]; line--) {
66
67        if (mdctSpectrum[wnd*FRAME_LEN_SHORT+line] != 0) break;
68      }
69
70      if (line >= sfbOffset[sfb]) break;
71    }
72    highestSfb = max(highestSfb, sfb);
73  }
74
75  if (highestSfb < 0) {
76    highestSfb = 0;
77  }
78  *maxSfbPerGroup = highestSfb + 1;
79
80  /* calculate sfbOffset */
81  i = 0;
82  offset = 0;
83  for (grp = 0; grp < noOfGroups; grp++) {
84    for (sfb = 0; sfb < sfbCnt; sfb++) {
85      groupedSfbOffset[i] = offset + sfbOffset[sfb] * groupLen[grp];
86      i += 1;
87    }
88    offset += groupLen[grp] * FRAME_LEN_SHORT;
89  }
90  groupedSfbOffset[i] = FRAME_LEN_LONG;
91  i += 1;
92
93  /* calculate minSnr */
94  i = 0;
95  offset = 0;
96  for (grp = 0; grp < noOfGroups; grp++) {
97    for (sfb = 0; sfb < sfbCnt; sfb++) {
98      groupedSfbMinSnr[i] = sfbMinSnr[sfb];
99      i += 1;
100    }
101    offset += groupLen[grp] * FRAME_LEN_SHORT;
102  }
103
104
105  /* sum up sfbThresholds */
106  wnd = 0;
107  i = 0;
108  for (grp = 0; grp < noOfGroups; grp++) {
109    for (sfb = 0; sfb < sfbCnt; sfb++) {
110      Word32 thresh = sfbThreshold->sfbShort[wnd][sfb];
111      for (j=1; j<groupLen[grp]; j++) {
112        thresh = L_add(thresh, sfbThreshold->sfbShort[wnd+j][sfb]);
113      }
114      sfbThreshold->sfbLong[i] = thresh;
115      i += 1;
116    }
117    wnd += groupLen[grp];
118  }
119
120  /* sum up sfbEnergies left/right */
121  wnd = 0;
122  i = 0;
123  for (grp = 0; grp < noOfGroups; grp++) {
124    for (sfb = 0; sfb < sfbCnt; sfb++) {
125      Word32 energy = sfbEnergy->sfbShort[wnd][sfb];
126      for (j=1; j<groupLen[grp]; j++) {
127        energy = L_add(energy, sfbEnergy->sfbShort[wnd+j][sfb]);
128      }
129      sfbEnergy->sfbLong[i] = energy;
130      i += 1;
131    }
132    wnd += groupLen[grp];
133  }
134
135  /* sum up sfbEnergies mid/side */
136  wnd = 0;
137  i = 0;
138  for (grp = 0; grp < noOfGroups; grp++) {
139    for (sfb = 0; sfb < sfbCnt; sfb++) {
140      Word32 energy = sfbEnergyMS->sfbShort[wnd][sfb];
141      for (j=1; j<groupLen[grp]; j++) {
142        energy = L_add(energy, sfbEnergyMS->sfbShort[wnd+j][sfb]);
143      }
144      sfbEnergyMS->sfbLong[i] = energy;
145      i += 1;
146    }
147    wnd += groupLen[grp];
148  }
149
150  /* sum up sfbSpreadedEnergies */
151  wnd = 0;
152  i = 0;
153  for (grp = 0; grp < noOfGroups; grp++) {
154    for (sfb = 0; sfb < sfbCnt; sfb++) {
155      Word32 energy = sfbSpreadedEnergy->sfbShort[wnd][sfb];
156      for (j=1; j<groupLen[grp]; j++) {
157        energy = L_add(energy, sfbSpreadedEnergy->sfbShort[wnd+j][sfb]);
158      }
159      sfbSpreadedEnergy->sfbLong[i] = energy;
160      i += 1;
161    }
162    wnd += groupLen[grp];
163  }
164
165  /* re-group spectrum */
166  wnd = 0;
167  i = 0;
168  for (grp = 0; grp < noOfGroups; grp++) {
169    for (sfb = 0; sfb < sfbCnt; sfb++) {
170      for (j = 0; j < groupLen[grp]; j++) {
171        Word16 lineOffset = FRAME_LEN_SHORT * (wnd + j);
172        for (line = lineOffset + sfbOffset[sfb]; line < lineOffset + sfbOffset[sfb+1]; line++) {
173          tmpSpectrum[i] = mdctSpectrum[line];
174          i = i + 1;
175        }
176      }
177    }
178    wnd += groupLen[grp];
179  }
180
181  for(i=0;i<FRAME_LEN_LONG;i+=4) {
182    mdctSpectrum[i] = tmpSpectrum[i];
183	mdctSpectrum[i+1] = tmpSpectrum[i+1];
184	mdctSpectrum[i+2] = tmpSpectrum[i+2];
185	mdctSpectrum[i+3] = tmpSpectrum[i+3];
186  }
187}
188
189