Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 614046
Collapse All | Expand All

(-)a/libaudiofile/modules/IMA.cpp (-3 / +2 lines)
Lines 169-175 int IMA::decodeBlockWAVE(const uint8_t *encoded, int16_t *decoded) Link Here
169
		if (encoded[1] & 0x80)
169
		if (encoded[1] & 0x80)
170
			m_adpcmState[c].previousValue -= 0x10000;
170
			m_adpcmState[c].previousValue -= 0x10000;
171
171
172
		m_adpcmState[c].index = encoded[2];
172
		m_adpcmState[c].index = clamp(encoded[2], 0, 88);
173
173
174
		*decoded++ = m_adpcmState[c].previousValue;
174
		*decoded++ = m_adpcmState[c].previousValue;
175
175
Lines 210-216 int IMA::decodeBlockQT(const uint8_t *encoded, int16_t *decoded) Link Here
210
			predictor -= 0x10000;
210
			predictor -= 0x10000;
211
211
212
		state.previousValue = clamp(predictor, MIN_INT16, MAX_INT16);
212
		state.previousValue = clamp(predictor, MIN_INT16, MAX_INT16);
213
		state.index = encoded[1] & 0x7f;
213
		state.index = clamp(encoded[1] & 0x7f, 0, 88);
214
		encoded += 2;
214
		encoded += 2;
215
215
216
		for (int n=0; n<m_framesPerPacket; n+=2)
216
		for (int n=0; n<m_framesPerPacket; n+=2)
217
--
218
sfcommands/sfconvert.c | 34 ++++++++++++++++++++++++++++++++--
217
sfcommands/sfconvert.c | 34 ++++++++++++++++++++++++++++++++--
219
1 file changed, 32 insertions(+), 2 deletions(-)
218
1 file changed, 32 insertions(+), 2 deletions(-)
(-)a/sfcommands/sfconvert.c (-3 / +32 lines)
Lines 45-50 void printusage (void); Link Here
45
void usageerror (void);
45
void usageerror (void);
46
bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid);
46
bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid);
47
47
48
int firstBitSet(int x)
49
{
50
        int position=0;
51
        while (x!=0)
52
        {
53
                x>>=1;
54
                ++position;
55
        }
56
        return position;
57
}
58
59
#ifndef __has_builtin
60
#define __has_builtin(x) 0
61
#endif
62
63
int multiplyCheckOverflow(int a, int b, int *result)
64
{
65
#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
66
	return __builtin_mul_overflow(a, b, result);
67
#else
68
	if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
69
		return true;
70
	*result = a * b;
71
	return false;
72
#endif
73
}
74
48
int main (int argc, char **argv)
75
int main (int argc, char **argv)
49
{
76
{
50
	if (argc == 2)
77
	if (argc == 2)
Lines 323-330 bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid) Link Here
323
{
350
{
324
	int frameSize = afGetVirtualFrameSize(infile, trackid, 1);
351
	int frameSize = afGetVirtualFrameSize(infile, trackid, 1);
325
352
326
	const int kBufferFrameCount = 65536;
353
	int kBufferFrameCount = 65536;
327
	void *buffer = malloc(kBufferFrameCount * frameSize);
354
	int bufferSize;
355
	while (multiplyCheckOverflow(kBufferFrameCount, frameSize, &bufferSize))
356
		kBufferFrameCount /= 2;
357
	void *buffer = malloc(bufferSize);
328
358
329
	AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
359
	AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
330
	AFframecount totalFramesWritten = 0;
360
	AFframecount totalFramesWritten = 0;
331
--
332
libaudiofile/WAVE.cpp | 2 ++
361
libaudiofile/WAVE.cpp | 2 ++
333
1 file changed, 2 insertions(+)
362
1 file changed, 2 insertions(+)
(-)a/libaudiofile/WAVE.cpp (-1 / +2 lines)
Lines 326-331 status WAVEFile::parseFormat(const Tag &id, uint32_t size) Link Here
326
			{
326
			{
327
				_af_error(AF_BAD_NOT_IMPLEMENTED,
327
				_af_error(AF_BAD_NOT_IMPLEMENTED,
328
					"IMA ADPCM compression supports only 4 bits per sample");
328
					"IMA ADPCM compression supports only 4 bits per sample");
329
				return AF_FAIL;
329
			}
330
			}
330
331
331
			int bytesPerBlock = (samplesPerBlock + 14) / 8 * 4 * channelCount;
332
			int bytesPerBlock = (samplesPerBlock + 14) / 8 * 4 * channelCount;
Lines 333-338 status WAVEFile::parseFormat(const Tag &id, uint32_t size) Link Here
333
			{
334
			{
334
				_af_error(AF_BAD_CODEC_CONFIG,
335
				_af_error(AF_BAD_CODEC_CONFIG,
335
					"Invalid samples per block for IMA ADPCM compression");
336
					"Invalid samples per block for IMA ADPCM compression");
337
				return AF_FAIL;
336
			}
338
			}
337
339
338
			track->f.sampleWidth = 16;
340
			track->f.sampleWidth = 16;
339
--
340
libaudiofile/modules/BlockCodec.cpp |  5 +--
341
libaudiofile/modules/BlockCodec.cpp |  5 +--
341
libaudiofile/modules/MSADPCM.cpp    | 47 ++++++++++++++++++++++++++---
342
libaudiofile/modules/MSADPCM.cpp    | 47 ++++++++++++++++++++++++++---
342
2 files changed, 46 insertions(+), 6 deletions(-)
343
2 files changed, 46 insertions(+), 6 deletions(-)
(-)a/libaudiofile/modules/BlockCodec.cpp (-2 / +3 lines)
Lines 52-59 void BlockCodec::runPull() Link Here
52
	// Decompress into m_outChunk.
52
	// Decompress into m_outChunk.
53
	for (int i=0; i<blocksRead; i++)
53
	for (int i=0; i<blocksRead; i++)
54
	{
54
	{
55
		decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
55
		if (decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
56
			static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount);
56
			static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount)==0)
57
			break;
57
58
58
		framesRead += m_framesPerPacket;
59
		framesRead += m_framesPerPacket;
59
	}
60
	}
(-)a/libaudiofile/modules/MSADPCM.cpp (-5 / +43 lines)
Lines 101-124 static const int16_t adaptationTable[] = Link Here
101
	768, 614, 512, 409, 307, 230, 230, 230
101
	768, 614, 512, 409, 307, 230, 230, 230
102
};
102
};
103
103
104
int firstBitSet(int x)
105
{
106
        int position=0;
107
        while (x!=0)
108
        {
109
                x>>=1;
110
                ++position;
111
        }
112
        return position;
113
}
114
115
#ifndef __has_builtin
116
#define __has_builtin(x) 0
117
#endif
118
119
int multiplyCheckOverflow(int a, int b, int *result)
120
{
121
#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
122
	return __builtin_mul_overflow(a, b, result);
123
#else
124
	if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
125
		return true;
126
	*result = a * b;
127
	return false;
128
#endif
129
}
130
131
104
// Compute a linear PCM value from the given differential coded value.
132
// Compute a linear PCM value from the given differential coded value.
105
static int16_t decodeSample(ms_adpcm_state &state,
133
static int16_t decodeSample(ms_adpcm_state &state,
106
	uint8_t code, const int16_t *coefficient)
134
	uint8_t code, const int16_t *coefficient, bool *ok=NULL)
107
{
135
{
108
	int linearSample = (state.sample1 * coefficient[0] +
136
	int linearSample = (state.sample1 * coefficient[0] +
109
		state.sample2 * coefficient[1]) >> 8;
137
		state.sample2 * coefficient[1]) >> 8;
138
	int delta;
110
139
111
	linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta;
140
	linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta;
112
141
113
	linearSample = clamp(linearSample, MIN_INT16, MAX_INT16);
142
	linearSample = clamp(linearSample, MIN_INT16, MAX_INT16);
114
143
115
	int delta = (state.delta * adaptationTable[code]) >> 8;
144
	if (multiplyCheckOverflow(state.delta, adaptationTable[code], &delta))
145
	{
146
                if (ok) *ok=false;
147
		_af_error(AF_BAD_COMPRESSION, "Error decoding sample");
148
		return 0;
149
	}
150
	delta >>= 8;
116
	if (delta < 16)
151
	if (delta < 16)
117
		delta = 16;
152
		delta = 16;
118
153
119
	state.delta = delta;
154
	state.delta = delta;
120
	state.sample2 = state.sample1;
155
	state.sample2 = state.sample1;
121
	state.sample1 = linearSample;
156
	state.sample1 = linearSample;
157
	if (ok) *ok=true;
122
158
123
	return static_cast<int16_t>(linearSample);
159
	return static_cast<int16_t>(linearSample);
124
}
160
}
Lines 212-224 int MSADPCM::decodeBlock(const uint8_t *encoded, int16_t *decoded) Link Here
212
	{
248
	{
213
		uint8_t code;
249
		uint8_t code;
214
		int16_t newSample;
250
		int16_t newSample;
251
		bool ok;
215
252
216
		code = *encoded >> 4;
253
		code = *encoded >> 4;
217
		newSample = decodeSample(*state[0], code, coefficient[0]);
254
		newSample = decodeSample(*state[0], code, coefficient[0], &ok);
255
		if (!ok) return 0;
218
		*decoded++ = newSample;
256
		*decoded++ = newSample;
219
257
220
		code = *encoded & 0x0f;
258
		code = *encoded & 0x0f;
221
		newSample = decodeSample(*state[1], code, coefficient[1]);
259
		newSample = decodeSample(*state[1], code, coefficient[1], &ok);
260
		if (!ok) return 0;
222
		*decoded++ = newSample;
261
		*decoded++ = newSample;
223
262
224
		encoded++;
263
		encoded++;
225
--
226
libaudiofile/WAVE.cpp | 6 ++++++
264
libaudiofile/WAVE.cpp | 6 ++++++
227
1 file changed, 6 insertions(+)
265
1 file changed, 6 insertions(+)
(-)a/libaudiofile/WAVE.cpp (+6 lines)
Lines 281-286 status WAVEFile::parseFormat(const Tag &id, uint32_t size) Link Here
281
281
282
			/* numCoefficients should be at least 7. */
282
			/* numCoefficients should be at least 7. */
283
			assert(numCoefficients >= 7 && numCoefficients <= 255);
283
			assert(numCoefficients >= 7 && numCoefficients <= 255);
284
			if (numCoefficients < 7 || numCoefficients > 255)
285
			{
286
				_af_error(AF_BAD_HEADER,
287
						"Bad number of coefficients");
288
				return AF_FAIL;
289
			}
284
290
285
			m_msadpcmNumCoefficients = numCoefficients;
291
			m_msadpcmNumCoefficients = numCoefficients;
286
292

Return to bug 614046