1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#include "main.h"
#include "adts_mp2.h"
#include "../winamp/wa_ipc.h"
#include <math.h>
#include "mpegutil.h"
#include "../nsutil/pcm.h"
extern int g_ds;
<<<<<<< HEAD:in_mp3/adts_mp2.cpp
DecoderHooks hooks={mp3GiveVisData, mp2Equalize, mp3Equalize};
=======
>>>>>>> 5058463... fix old-school vis/eq mp3 stuff:mp3/adts_mp2.cpp
ADTS_MP2::ADTS_MP2() : decoder(0), gain(1.f)
{
memset(&hooks, 0, sizeof(hooks));
#ifndef NO_MP3SURROUND
lineFilled=false;
saDecHandle=0;
saMode = SA_DEC_OFF;
#endif
decoderDelay = 529;
endcut=0;
outputFrameSize = 0;
bitsPerSample = 0;
allowRG = false;
useFloat = false;
channels = 0;
sampleRate = 0;
memset(&delayline, 0, sizeof(delayline));
delaylineSize = 0;
}
void ADTS_MP2::SetDecoderHooks(void *layer3_vis, void *layer2_eq, void *layer3_eq)
{
<<<<<<< HEAD:in_mp3/adts_mp2.cpp
//*(void **)&hooks.layer3_vis = layer3_vis;
=======
*(void **)&hooks.layer3_vis = layer3_vis;
>>>>>>> 5058463... fix old-school vis/eq mp3 stuff:mp3/adts_mp2.cpp
*(void **)&hooks.layer2_eq = layer2_eq;
*(void **)&hooks.layer3_eq = layer3_eq;
}
int ADTS_MP2::Initialize(bool forceMono, bool reverseStereo, bool allowSurround, int maxBits, bool _allowRG, bool _useFloat, bool _useCRC)
{
allowRG = _allowRG;
useFloat = _useFloat;
int downmix = 0;
if (reverseStereo)
downmix = 2;
else
downmix = forceMono ? 1 : 0;
bitsPerSample = maxBits;
<<<<<<< HEAD:in_mp3/adts_mp2.cpp
decoder = new CMpgaDecoder(&hooks, g_ds, downmix, !!_useCRC);
=======
decoder = new CMpgaDecoder(hooks.layer3_vis?&hooks:(DecoderHooks *)0, MPEGAUDIO_QUALITY_FULL/*g_ds*/, downmix, _useCRC);
>>>>>>> 5058463... fix old-school vis/eq mp3 stuff:mp3/adts_mp2.cpp
#ifndef NO_MP3SURROUND
if (allowSurround)
IIS_SADec_Init(&saDecHandle, 6);
#endif
return 0;
}
bool ADTS_MP2::Open(ifc_mpeg_stream_reader *file)
{
decoder->Connect((CGioFile *)file);
if (allowRG)
gain = file->MPEGStream_Gain();
return true;
}
void ADTS_MP2::Close()
{
if (decoder)
{
delete decoder;
decoder = 0;
}
#ifndef NO_MP3SURROUND
if (saDecHandle)
IIS_SADec_Free(&saDecHandle);
saDecHandle=0;
#endif
}
void ADTS_MP2::GetOutputParameters(size_t *numBits, int *numChannels, int *sampleRate)
{
*sampleRate = this->sampleRate;
*numChannels = channels;
*numBits = bitsPerSample;
}
void ADTS_MP2::CalculateFrameSize(int *frameSize)
{
*frameSize = outputFrameSize;
if (decoder->GetStreamInfo()->GetLayer() == 1)
*frameSize *= 3;
}
void ADTS_MP2::Flush(ifc_mpeg_stream_reader *file)
{
decoder->Reset();
#ifndef NO_MP3SURROUND
if (saDecHandle)
IIS_SADec_Reset(saDecHandle);
lineFilled=false;
#endif
}
size_t ADTS_MP2::GetCurrentBitrate()
{
return decoder->GetStreamInfo()->GetBitrate() / 1000;
}
size_t ADTS_MP2::GetDecoderDelay()
{
#ifndef NO_MP3SURROUND
if (!saDecHandle || saMode == SA_DEC_OFF || saMode == SA_DEC_BYPASS)
return decoderDelay;
else /* bcc adds 576 delay */
return decoderDelay+576;
#else
return decoderDelay;
#endif
}
static void Decimate(const float *input, void *output, size_t numSamples, size_t *outputWritten, int bitsPerSample, bool useFloat, float gain)
{
if (!useFloat)
{
// TODO seen a few crashes reported where 'output' is 0
nsutil_pcm_FloatToInt_Interleaved_Gain(output, input, bitsPerSample, numSamples, gain);
}
else if (gain != 1.f)
{
float *data = (float *)output;
for (size_t i=0;i<numSamples;i++)
data[i]*=gain;
//data[i]=input[i]*gain;
}
*outputWritten = numSamples * (bitsPerSample / 8);
}
/*
notes for mp3 surround implementations
need to check the first two frames for ancillary data
store first valid in temp
store second valid frame in delay line
decimate first valid into output buffer
ancillary data is stored one frame behind, so PCM data decoded from mp3 frame n combines with anc data from frame n+1
*/
int ADTS_MP2::Sync(ifc_mpeg_stream_reader *_file, unsigned __int8 *output, size_t outputSize, size_t *outputWritten, size_t *bitrate)
{
SSC ssc;
CGioFile *file = (CGioFile *)_file;
unsigned char ancBytes[8192] = {0};
int numAncBytes = 0;
unsigned int delay=0, totalLength=0;
float floatTemp[1152*2] = {0};
float *flData=useFloat?(float *)output:floatTemp;
ssc = decoder->DecodeFrame(flData, sizeof(floatTemp), &outputFrameSize,
<<<<<<< HEAD:in_mp3/adts_mp2.cpp
ancBytes, &numAncBytes, 1, &delay, &totalLength);
// TODO: benski> we should really have CGioFile try to read this stuff
if (delay && !file->prepad)
{
// validate
if (delay >= 529)
{
decoderDelay = delay;
endcut = 1152 - ((totalLength + delay) % 1152); // how many 0 samples had to be added?
endcut += decoderDelay; // also need to cut out the encoder+decoder delay
file->m_vbr_samples = totalLength;
}
}
=======
ancBytes, &numAncBytes);
>>>>>>> fd5c493... have CGioFile read the OFL (from newer fraunhofer encoders):mp3/adts_mp2.cpp
switch (ssc)
{
case SSC_OK:
{
channels = decoder->GetStreamInfo()->GetEffectiveChannels();
sampleRate = decoder->GetStreamInfo()->GetEffectiveSFreq();
#ifndef NO_MP3SURROUND
if (!numAncBytes && saDecHandle)
{
ssc = decoder->DecodeFrame(delayline, sizeof(delayline), &delaylineSize,
ancBytes, &numAncBytes);
if (SSC_SUCCESS(ssc))
{
lineFilled=true;
}
else if (ssc == SSC_W_MPGA_SYNCEOF)
return ENDOFFILE;
else
return NEEDMOREDATA;
}
if (saDecHandle)
{
SA_DEC_ERROR sa_error = IIS_SADec_DecodeAncData(saDecHandle, ancBytes, numAncBytes, 0, 0);
if (sa_error == SA_DEC_NO_ERROR)
{
IIS_SADec_InitInfo(saDecHandle, sampleRate, channels);
SA_DEC_INFO saInfo = IIS_SADec_GetInfo(saDecHandle);
sampleRate = saInfo.SampleRate;
channels = saInfo.nChannelsOut;
saMode = saInfo.configuredMode;
}
else if (saMode == SA_DEC_OFF)
{
IIS_SADec_Free(&saDecHandle);
saDecHandle=0;
}
else
{
lineFilled=false;
return NEEDMOREDATA;
}
}
if (saDecHandle)
{
float surroundFloatTemp[1152*6] = {0};
int outputSamples = 0;
/*SA_DEC_ERROR sa_error = */IIS_SADec_DecodeFrame(saDecHandle,
flData, outputFrameSize/sizeof(float),
(char *)ancBytes, numAncBytes,
surroundFloatTemp, sizeof(surroundFloatTemp),
&outputSamples, saMode, 0, 0,
0, 0);
if (useFloat)
memcpy(output, surroundFloatTemp, sizeof(surroundFloatTemp));
Decimate(surroundFloatTemp, output, outputSamples, outputWritten, bitsPerSample, useFloat, (float)gain);
outputFrameSize = *outputWritten;
}
else
#endif
{
Decimate(floatTemp, output, outputFrameSize / sizeof(float), outputWritten, bitsPerSample, useFloat, (float)gain);
outputFrameSize = *outputWritten;
}
}
return SSC_OK;
case SSC_W_MPGA_SYNCSEARCHED:
return NEEDMOREDATA;
case SSC_W_MPGA_SYNCEOF:
return ENDOFFILE;
case SSC_E_MPGA_WRONGLAYER:
decoder->m_Mbs.Seek(1);
default:
return NEEDMOREDATA;
}
}
int ADTS_MP2::Decode(ifc_mpeg_stream_reader *file, unsigned __int8 *output, size_t outputSize, size_t *outputWritten, size_t *bitrate, size_t *endCut)
{
if (endcut)
{
*endCut = endcut;
endcut=0;
}
#ifndef NO_MP3SURROUND
if (!saDecHandle && lineFilled) // if we don't have surround info, go ahead and flush out the delayline buffer
{
Decimate(delayline, output, delaylineSize / sizeof(float), outputWritten, bitsPerSample, useFloat, (float)gain);
*bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
lineFilled=false;
return adts::SUCCESS;
}
if (saDecHandle && !lineFilled && !decoder->IsEof()) // have surround info, but don't have a previously decoded frame
{
// resync
int ret = Sync(file, output, outputSize, outputWritten, bitrate);
if (ret == SSC_OK && saDecHandle)
{
*bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
return adts::SUCCESS;
}
else if (saDecHandle)
return ret;
else
return adts::FAILURE;
}
#endif
unsigned char ancBytes[8192] = {0};
int numAncBytes = 0;
int newl;
float floatTemp[1152*2] = {0};
float *flData=useFloat?(float *)output:floatTemp;
SSC ssc = decoder->DecodeFrame(flData, sizeof(floatTemp), &newl, ancBytes, &numAncBytes);
if (SSC_SUCCESS(ssc))
{
#ifndef NO_MP3SURROUND
if (saDecHandle && lineFilled)
{
float surroundFloatTemp[1152*6] = {0};
int outputSamples;
/*SA_DEC_ERROR sa_error = */IIS_SADec_DecodeFrame(saDecHandle,
delayline, delaylineSize/sizeof(float),
(char *)ancBytes, numAncBytes,
surroundFloatTemp, sizeof(surroundFloatTemp),
&outputSamples, saMode, 0, 0,
0, 0);
if (useFloat)
memcpy(output, surroundFloatTemp, sizeof(surroundFloatTemp));
Decimate(surroundFloatTemp, output, outputSamples, outputWritten, bitsPerSample, useFloat, (float)gain);
memcpy(delayline, flData, delaylineSize);
}
else
#endif
{
Decimate(floatTemp, output, newl / sizeof(float), outputWritten, bitsPerSample, useFloat, (float)gain);
}
*bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
return adts::SUCCESS;
}
else if (decoder->IsEof())
{
#ifndef NO_MP3SURROUND
/* In case of SA processing one ancillary data
package and maybe fill samples left in the dynamic
buffer of the mp3 decoder. Take care, that the
ancillary data buffer is greater then the dynamic buffer of
the mp3 decoder. */
if (saDecHandle && lineFilled)
{
decoder->GetLastAncData(ancBytes, &numAncBytes);
float surroundFloatTemp[1152*6];
int outputSamples = 0;
/*SA_DEC_ERROR sa_error = */IIS_SADec_DecodeFrame(saDecHandle,
delayline, delaylineSize/sizeof(float),
(char *)ancBytes, numAncBytes,
surroundFloatTemp, sizeof(surroundFloatTemp),
&outputSamples, saMode, 0, 0,
0, 0);
if (useFloat)
memcpy(output, surroundFloatTemp, sizeof(surroundFloatTemp));
Decimate(surroundFloatTemp, output, outputSamples, outputWritten, bitsPerSample, useFloat, (float)gain);
lineFilled=false;
*bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
return adts::SUCCESS;
}
else
#endif
return adts::ENDOFFILE;
}
else if (ssc == SSC_W_MPGA_SYNCNEEDDATA)
{
*bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
return adts::NEEDMOREDATA;
}
else if (ssc==SSC_W_MPGA_SYNCLOST || ssc==SSC_W_MPGA_SYNCSEARCHED)
{
*bitrate = decoder->GetStreamInfo()->GetBitrate() / 1000;
return adts::NEEDSYNC;
}
else
{
if (ssc == SSC_E_MPGA_WRONGLAYER)
decoder->m_Mbs.Seek(1);
return adts::FAILURE;
}
}
int ADTS_MP2::GetLayer()
{
if (decoder)
return decoder->GetStreamInfo()->GetLayer();
else
return 0;
}
void ADTS_MP2::Release()
{
delete this;
}
|