1
2 #define VERY_FAST_FILTER 1 /* JMZ */
3
4 #define LOWPASS 20
5
6 #include "common.h"
7 #include "encoder.h"
8 #include "lingual.h"
9
10 /************************************************************************
11 *
12 * read_samples()
13 *
14 * PURPOSE: reads the PCM samples from a file to the buffer
15 *
16 * SEMANTICS:
17 * Reads #samples_read# number of shorts from #musicin# filepointer
18 * into #sample_buffer[]#. Returns the number of samples read.
19 *
20 ************************************************************************/
21
read_samples_ml(FILE * musicin,long int * sample_buffer,long unsigned int num_samples,long unsigned int frame_size,int * byte_per_sample,int * aiff)22 unsigned long read_samples_ml (FILE * musicin, long int *sample_buffer,
23 long unsigned int num_samples,
24 long unsigned int frame_size,
25 int *byte_per_sample, int *aiff)
26 {
27 unsigned long samples_read;
28 static unsigned long samples_to_read;
29 static char init = TRUE;
30 short pcm_sample_buffer[8064]; /*for correct reading of pcm-data */
31 int i;
32
33 if (init) {
34 samples_to_read = num_samples;
35 init = FALSE;
36 }
37 if (samples_to_read >= frame_size)
38 samples_read = frame_size;
39 else
40 samples_read = samples_to_read;
41 if ((*aiff == 1) && (*byte_per_sample != 2)) {
42 if ((samples_read =
43 fread (sample_buffer, *byte_per_sample, (int) samples_read,
44 musicin)) == 0)
45 if (verbosity >= 2)
46 printf ("Hit end of audio data\n");
47 } else {
48 if ((samples_read =
49 fread (pcm_sample_buffer, sizeof (short), (int) samples_read,
50 musicin)) == 0)
51 if (verbosity >= 2)
52 printf ("Hit end of audio data\n");
53 for (i = 0; i < samples_read; ++i) /* replace 5760 by 'samples_read' WtK 7/8/95 */
54 sample_buffer[i] = pcm_sample_buffer[i];
55 }
56
57 samples_to_read -= samples_read;
58 if (samples_read < frame_size && samples_read > 0) {
59 if (verbosity >= 2)
60 printf ("Insufficient PCM input for one frame - fillout with zeros\n");
61 for (; samples_read < frame_size; sample_buffer[samples_read++] = 0);
62 samples_to_read = 0;
63 }
64 return (samples_read);
65 }
66
67
68 /************************************************************************
69 *
70 * get_audio_ml()
71 *
72 * PURPOSE: reads a frame of audio data from a file to the buffer,
73 * aligns the data for future processing, and separates the
74 * left and right channels
75 *
76 * SEMANTICS:
77 * Calls read_samples() to read a frame of audio data from filepointer
78 * #musicin# to #insampl[]#. The data is shifted to make sure the data
79 * is centered for the 1024pt window to be used by the psychoacoustic model,
80 * and to compensate for the 256 sample delay from the filter bank. For
81 * stereo, the channels are also demultiplexed into #buffer[0][]# and
82 * #buffer[1][]#
83 *
84 ************************************************************************/
85 unsigned long
get_audio_ml(FILE * musicin_ml,double (* buffer)[1152],long unsigned int num_samples,IFF_AIFF * aiff_ptr,frame_params * fr_ps,int * aiff,int * byte_per_sample,double (* buffer_matr)[1152])86 get_audio_ml (FILE * musicin_ml,
87 double (*buffer)[1152],
88 long unsigned int num_samples,
89 IFF_AIFF * aiff_ptr,
90 frame_params * fr_ps,
91 int *aiff, int *byte_per_sample, double (*buffer_matr)[1152]
92 )
93 {
94 int j, ch;
95 long insamp[8064];
96 unsigned long samples_read;
97 int n_ml_ch = fr_ps->header->multiling_ch;
98
99 samples_read =
100 read_samples_ml (musicin_ml, insamp, num_samples,
101 (unsigned long) 1152 * n_ml_ch, byte_per_sample, aiff);
102 for (j = 0; j < 1152; j++)
103 for (ch = 0; ch < n_ml_ch; ch++)
104 buffer_matr[7 + ch][j] = buffer[7 + ch][j] = insamp[(n_ml_ch * j) + ch]; /*WtK 7/8/95 */
105
106 return (samples_read);
107 }
108
109 /************************************************************************
110 *
111 * I_encode_scale (Layer I)
112 * II_encode_scale (Layer II)
113 *
114 * PURPOSE:The encoded scalar factor information is arranged and
115 * queued into the output fifo to be transmitted.
116 *
117 * For Layer II, the three scale factors associated with
118 * a given subband and channel are transmitted in accordance
119 * with the scfsi, which is transmitted first.
120 *
121 ************************************************************************/
122
123
II_sample_encoding_ml(unsigned int (* sbband)[3][12][32],unsigned int (* bit_alloc)[32],frame_params * fr_ps,Bit_stream_struc * bs)124 void II_sample_encoding_ml (unsigned int (*sbband)[3][12][32],
125 unsigned int (*bit_alloc)[32], frame_params * fr_ps,
126 Bit_stream_struc * bs)
127 {
128 unsigned int temp;
129 unsigned int i, j, k, s, x, y;
130 int n_ml_ch = fr_ps->header->multiling_ch;
131 int lsf = fr_ps->header->multiling_fs;
132 int sblimit_ml = fr_ps->sblimit_ml;
133 al_table *alloc_ml = fr_ps->alloc_ml;
134
135 for (s = 0; s < 3; s++)
136 for (j = 0; j < ((lsf == 1) ? 6 : 12); j += 3)
137 for (i = 0; i < sblimit_ml; i++)
138 for (k = 7; k < 7 + n_ml_ch; k++)
139 if (bit_alloc[k][i]) {
140 if ((*alloc_ml)[i][bit_alloc[k][i]].group == 3) {
141 for (x = 0; x < 3; x++)
142 putbits (bs, sbband[k][s][j + x][i],
143 (*alloc_ml)[i][bit_alloc[k][i]].bits);
144 } else {
145 y = (*alloc_ml)[i][bit_alloc[k][i]].steps;
146 temp = sbband[k][s][j][i] +
147 sbband[k][s][j + 1][i] * y + sbband[k][s][j + 2][i] * y * y;
148 putbits (bs, temp, (*alloc_ml)[i][bit_alloc[k][i]].bits);
149 }
150 }
151 }
152
II_encode_bit_alloc_ml(unsigned int (* bit_alloc)[32],frame_params * fr_ps,Bit_stream_struc * bs)153 void II_encode_bit_alloc_ml (unsigned int (*bit_alloc)[32],
154 frame_params * fr_ps, Bit_stream_struc * bs)
155 {
156 int i, k;
157 int n_ml_ch = fr_ps->header->multiling_ch;
158 int sblimit_ml = fr_ps->sblimit_ml;
159 al_table *alloc_ml = fr_ps->alloc_ml;
160
161 for (i = 0; i < sblimit_ml; i++) {
162 for (k = 7; k < 7 + n_ml_ch; ++k) {
163 putbits (bs, bit_alloc[k][i], (*alloc_ml)[i][0].bits);
164 }
165 }
166 }
167
168
ml_sb_sample_swap(int ch0,int ch1,double subsample[14][3][12][SBLIMIT])169 void ml_sb_sample_swap (int ch0, int ch1, double subsample[14][3][12][SBLIMIT])
170 /* Function is called if MultiLingual LSF applies. */
171 /* It organizes subband samples from 3 sub frames of 12 samples each */
172 /* into 6 sub frames of 6 samples each. Subframes 3, 4 and 5 are at */
173 /* sample indices 6..11 in subframes 0,1,2 respectively. */
174 /* WtK 7/8/95 */
175 {
176 int ch, sb, ss;
177 double hlp[6];
178
179 for (ch = ch0; ch < ch1; ch++)
180 for (sb = 0; sb < SBLIMIT; sb++)
181 for (ss = 0; ss < 6; ss++) {
182 hlp[ss] = subsample[ch][2][ss][sb];
183 subsample[ch][2][ss][sb] = subsample[ch][1][ss][sb];
184 subsample[ch][1][ss][sb] = subsample[ch][0][6 + ss][sb];
185 subsample[ch][0][6 + ss][sb] = subsample[ch][1][6 + ss][sb];
186 subsample[ch][1][6 + ss][sb] = hlp[ss];
187 }
188 }
189
ml_sb_sample_shift(int ch0,int ch1,double subsample[14][3][12][SBLIMIT])190 void ml_sb_sample_shift (int ch0, int ch1, double subsample[14][3][12][SBLIMIT])
191 /* In case of MultiLingual LSF this function is called. */
192 /* It shifts the second part in the sub frames into the first part. */
193 /* The first part is shifted into the second part to be used by */
194 /* pick_scale_ml_2() */
195 /* WtK 7/8/95 */
196 {
197 int ch, sb, p, ss;
198 double hlp[6];
199
200 for (ch = ch0; ch < ch1; ch++)
201 for (sb = 0; sb < SBLIMIT; sb++)
202 for (p = 0; p < 2; p++)
203 for (ss = 0; ss < 6; ss++) {
204 hlp[ss] = subsample[ch][p][ss][sb];
205 subsample[ch][p][ss][sb] = subsample[ch][p][ss + 6][sb];
206 subsample[ch][p][ss + 6][sb] = hlp[ss];
207 }
208 }
209
pick_scale_ml_2(frame_params * fr_ps,double subsample[14][3][12][SBLIMIT],double (* max_sc)[32])210 void pick_scale_ml_2 (frame_params * fr_ps,
211 double subsample[14][3][12][SBLIMIT],
212 double (*max_sc)[32])
213 /* pick largest max_sc of odd and even half of frame in case of LSF ML. */
214 /* This improves the psychoacoustic result: */
215 /* The masked threshold is calculated over 2 LSF frames; consequently, */
216 /* the signal level should also be determined over those 2 frames in */
217 /* order to obtain a fair estimate of the SMR. */
218 /* WtK , 7/8/95 */
219 {
220 int k, i, p, j;
221 int maxi;
222 double maxs, mods;
223 int n_ml_ch = fr_ps->header->multiling_ch;
224 int sblimit_ml = fr_ps->sblimit_ml;
225 int ml_fs = fr_ps->header->multiling_fs;
226
227 if ((n_ml_ch > 0) && (ml_fs == 1)) {
228 for (k = 7; k < 7 + n_ml_ch; k++) {
229 for (i = 0; i < sblimit_ml; i++) {
230 maxs = subsample[k][0][0][i];
231 if (maxs < 0)
232 maxs = -maxs;
233 for (p = 0; p < 3; p++)
234 for (j = 6; j < 12; j++) {
235 mods = subsample[k][p][j][i];
236 if (mods < 0)
237 mods = -mods;
238 if (mods > maxs)
239 maxs = mods;
240 }
241 for (j = SCALE_RANGE - 1, maxi = 0; j >= 0; j--)
242 if (maxs < multiple[j]) {
243 maxi = j;
244 break;
245 }
246 if (multiple[maxi] > max_sc[k][i])
247 max_sc[k][i] = multiple[maxi];
248 }
249 }
250 }
251 }
252