Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/amp/layer3.c
1074 views
1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2
*/
3
4
/* layer3.c layer3 audio decoding
5
*
6
* Created by: tomislav uzelac Mar 1 97
7
* Last modified by:
8
*/
9
#include "amp.h"
10
#include "audio.h"
11
#include "getbits.h"
12
#include "getdata.h"
13
#include "huffman.h"
14
#include "misc2.h"
15
#include "transform.h"
16
17
#define LAYER3
18
#include "layer3.h"
19
20
/* this function decodes one layer3 audio frame, except for the header decoding
21
* which is done in main() [audio.c]. returns 0 if everything is ok.
22
*/
23
int layer3_frame(struct AUDIO_HEADER *header,int cnt)
24
{
25
static struct SIDE_INFO info;
26
27
int gr,ch,sb,i;
28
int mean_frame_size,bitrate,fs,hsize,ssize;
29
30
/* we need these later, hsize is the size of header+side_info
31
*/
32
if (header->ID)
33
if (header->mode==3) {
34
nch=1;
35
hsize=21;
36
} else {
37
nch=2;
38
hsize=36;
39
}
40
else
41
if (header->mode==3) {
42
nch=1;
43
hsize=13;
44
} else {
45
nch=2;
46
hsize=21;
47
}
48
49
/* crc increases hsize by 2
50
*/
51
if (header->protection_bit==0) hsize+=2;
52
53
54
/* read layer3 specific side_info
55
*/
56
getinfo(header,&info);
57
58
59
/* MPEG2 only has one granule
60
*/
61
bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index];
62
fs=t_sampling_frequency[header->ID][header->sampling_frequency];
63
if (header->ID) mean_frame_size=144000*bitrate/fs;
64
else mean_frame_size=72000*bitrate/fs;
65
66
67
/* check if mdb is too big for the first few frames. this means that
68
* a part of the stream could be missing. We must still fill the buffer
69
*
70
* don't forget to (re)initialise bclean_bytes to 0, and f_bdirty to FALSE!!!
71
*/
72
if (f_bdirty)
73
{
74
if (info.main_data_begin > bclean_bytes)
75
{
76
fillbfr(mean_frame_size + header->padding_bit - hsize);
77
bclean_bytes+=mean_frame_size + header->padding_bit - hsize;
78
/* warn(" frame %d discarded, incomplete main_data\n",cnt); */
79
return 0;
80
}
81
else
82
{
83
/* re-initialise */
84
f_bdirty=FALSE;
85
bclean_bytes=0;
86
}
87
}
88
/* now update the data 'pointer' (counting in bits) according to
89
* the main_data_begin information
90
*/
91
data = 8 * ((append - info.main_data_begin) & (BUFFER_SIZE-1));
92
93
94
/* read into the buffer all bytes up to the start of next header
95
*/
96
fillbfr(mean_frame_size + header->padding_bit - hsize);
97
98
99
/* these two should go away
100
*/
101
t_l=&t_b8_l[header->ID][header->sampling_frequency][0];
102
t_s=&t_b8_s[header->ID][header->sampling_frequency][0];
103
104
/* decode the scalefactors and huffman data
105
* this part needs to be enhanced for error robustness
106
*/
107
for (gr=0;gr < ((header->ID) ? 2 : 1);gr++) {
108
for (ch=0;ch<nch;ch++) {
109
ssize=decode_scalefactors(&info,header,gr,ch);
110
decode_huffman_data(&info,gr,ch,ssize);
111
}
112
113
/* requantization, stereo processing, reordering(shortbl)
114
*/
115
116
if (A_DOWNMIX && nch==2)
117
requantize_downmix(gr,&info,header);
118
else
119
{
120
if (header->mode!=1 || (header->mode==1 && header->mode_extension==0))
121
{
122
for (ch=0;ch<nch;ch++)
123
requantize_mono(gr,ch,&info,header);
124
}
125
else
126
requantize_ms(gr,&info,header);
127
}
128
/* just which window?
129
*/
130
131
/* this is a very temporary, very ugly hack.
132
*/
133
if (A_DOWNMIX) nch=1;
134
135
for (ch=0; ch < (A_DOWNMIX ? 1:nch) ;ch++) {
136
int win_type; /* same as in the standard, long=0, start=1 ,.... */
137
int window_switching_flag = info.window_switching_flag[gr][ch];
138
int block_type = info.block_type[gr][ch];
139
int mixed_block_flag = info.mixed_block_flag[gr][ch];
140
141
/* antialiasing butterflies
142
*/
143
if (!(window_switching_flag &&
144
block_type==2))
145
alias_reduction(ch);
146
147
if (window_switching_flag &&
148
block_type==2 &&
149
mixed_block_flag)
150
win_type=0;
151
else
152
if (!window_switching_flag) win_type=0;
153
else win_type=block_type;
154
155
/* imdct ...
156
*/
157
for (sb=0;sb<2;sb++)
158
imdct(win_type,sb,ch);
159
160
if (window_switching_flag &&
161
block_type==2 &&
162
mixed_block_flag)
163
win_type=2;
164
165
/* no_of_imdcts tells us how many subbands from the top are all zero
166
* it is set by the requantize functions in misc2.c
167
*/
168
for (sb=2;sb<no_of_imdcts[ch];sb++)
169
imdct(win_type,sb,ch);
170
171
for (;sb<32;sb++)
172
for (i=0;i<18;i++) {
173
res[sb][i]=s[ch][sb][i];
174
s[ch][sb][i]=0.0f;
175
}
176
177
/* polyphase filterbank
178
*/
179
/* if (nch == 2) this was a bug, tomislav */
180
for (i=0;i<18;i++)
181
poly(ch,i);
182
}
183
184
printout();
185
186
/* this is part2 of a particularily ugly hack. this should vanish as soon as nch isn't
187
a global variable anymore
188
*/
189
if (A_DOWNMIX && header->mode!=3) nch=2;
190
191
} /* for (gr... */
192
193
/* return status: 0 for ok, errors will be added
194
*/
195
return 0;
196
}
197
198
199