aboutsummaryrefslogtreecommitdiff
path: root/Src/mp3-mpg123/mp3_in_mp4.cpp
blob: f4437e7a0341b7e174f6dc1cb8247287b04e9aca (plain) (blame)
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
// used to decode an MPEG-1 audio object in an MPEG-4 ISO Media file
#include "mp3_in_mp4.h"
#include "api__mp3-mpg123.h"
#include "../nsutil/pcm.h"

// {B6CB4A7C-A8D0-4c55-8E60-9F7A7A23DA0F}
static const GUID playbackConfigGroupGUID = 
{ 0xb6cb4a7c, 0xa8d0, 0x4c55, { 0x8e, 0x60, 0x9f, 0x7a, 0x7a, 0x23, 0xda, 0xf } };

#define FHG_DELAY 529
MPEG4_MP3::MPEG4_MP3()
{
	channels = 0;
	gain = 1;
	floatingPoint = false;
	decoder = 0;
	sample_rate = 0;
	bits = 16;
	pregap = FHG_DELAY;
}

MPEG4_MP3::~MPEG4_MP3()
{
	if (decoder) {
		mpg123_delete(decoder);
		decoder = 0;
	}
}

int MPEG4_MP3::OpenEx(size_t _bits, size_t _maxChannels, bool useFloat)
{
	_bits = bits;
	floatingPoint = useFloat;
	if (floatingPoint)
		bits = 32;
	else
		bits = (int)_bits;

	decoder = mpg123_new(NULL, NULL);
	long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
	if (_maxChannels == 1) {
		flags |= MPG123_FORCE_MONO;
	}
	mpg123_param(decoder, MPG123_FLAGS, flags, 0);
	mpg123_param(decoder, MPG123_RVA, MPG123_RVA_OFF, 0);
	mpg123_open_feed(decoder);
	return MP4_SUCCESS;
}

const char *MPEG4_MP3::GetCodecInfoString()
{
	return 0;
}

int MPEG4_MP3::CanHandleCodec(const char *codecName)
{
	if (!lstrcmpA(codecName, "mp4a"))
		return 1;
	else
		return 0;
}

int MPEG4_MP3::CanHandleType(unsigned __int8 type)
{
	switch(type)
	{
	case MP4_MPEG4_LAYER3_AUDIO:
	case MP4_MPEG4_LAYER2_AUDIO:
	case MP4_MPEG4_LAYER1_AUDIO:
	case MP4_TYPE_MPEG1_AUDIO:
	case MP4_TYPE_MPEG2_AUDIO:
	//case MP4_TYPE_MPEG4_AUDIO:
		return 1;
	default:
		return 0;
	}
}

int MPEG4_MP3::DecodeSample(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
{
		if (!decoder)
		return MP4_FAILURE;

	*outputBufferBytes = 0;
	mpg123_feed(decoder, (unsigned char *)inputBuffer, inputBufferBytes);

	for (;;) {
		// get the decoded data out
		size_t pcm_buf_used=0;
		float decodeBuf[1152*2];
		int err = mpg123_read(decoder, (unsigned char *)decodeBuf, sizeof(decodeBuf), &pcm_buf_used);

		if (pcm_buf_used) {
			if (!_UpdateProperties()) {
				return MP4_FAILURE;
			}
			// deal with pregap
			int numSamples = (int)pcm_buf_used / sizeof(float);
			int offset = min(numSamples, pregap * channels);
			numSamples -= offset;
			pregap -= offset / channels;
			float *pcm_buf = decodeBuf + offset;

			// convert to destination sample format
			
			nsutil_pcm_FloatToInt_Interleaved(outputBuffer, pcm_buf, bits, numSamples);

			*outputBufferBytes += numSamples * bits / 8;
			outputBuffer = (char *)outputBuffer + numSamples * bits / 8;

			return MP4_SUCCESS;
		} else if (err == MPG123_NEED_MORE) {
			*outputBufferBytes = 0;
			return MP4_NEED_MORE_INPUT;
		} else if (err == MPG123_NEW_FORMAT) {
			continue;
		} else if (err == MPG123_OK) {
			continue;
		}
		else
			return MP4_FAILURE;
	}
	return MP4_SUCCESS;
}

bool MPEG4_MP3::_UpdateProperties()
{
	if (decoder && (!channels || !sample_rate)) {
		long sample_rate = 44100;
		int channels = 2;
		int encoding = 0;
		if (mpg123_getformat(decoder, &sample_rate, &channels, &encoding) == MPG123_OK) {
			this->channels = channels;
			this->sample_rate = sample_rate;
		}
	}

	return channels && sample_rate;
}
int MPEG4_MP3::GetOutputPropertiesEx(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
{
	if (_UpdateProperties()) {
		*sampleRate = this->sample_rate;
		*channels = this->channels;
		*bitsPerSample = bits;
		*isFloat = floatingPoint;
		return MP4_SUCCESS;
	} else {
		return MP4_FAILURE;
	}
}

int MPEG4_MP3::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample)
{
	bool dummy;
	return GetOutputPropertiesEx(sampleRate, channels, bitsPerSample, &dummy);
}

void MPEG4_MP3::Close()
{
	if (decoder) {
		mpg123_delete(decoder);
		decoder = 0;
	}
}

void MPEG4_MP3::Flush()
{
	mpg123_open_feed(decoder);
	pregap = FHG_DELAY;
}

int MPEG4_MP3::SetGain(float _gain)
{
	gain = _gain;
	return MP4_SUCCESS;
}

int MPEG4_MP3::GetCurrentBitrate(unsigned int *bitrate)
{
	mpg123_frameinfo frameInfo;
	if (mpg123_info(decoder, &frameInfo) == MPG123_OK) {
		*bitrate = frameInfo.bitrate;
		return MP4_SUCCESS;
	} else {
		return MP4_FAILURE;
	}
}

int MPEG4_MP3::OutputFrameSize(size_t *frameSize)
{
	if (_UpdateProperties()) {
		*frameSize = (bits/8) * channels * mpg123_spf(decoder);
		return MP4_SUCCESS;
	} else {
		return MP4_FAILURE;
	}	
}

int MPEG4_MP3::CanHandleMPEG4Type(unsigned __int8 type)
{
	switch (type)
	{
	case MP4_MPEG4_LAYER1_AUDIO:
	case MP4_MPEG4_LAYER2_AUDIO:
	case MP4_MPEG4_LAYER3_AUDIO:
		return 1;
	default:
		return 0;
	}
}

#define CBCLASS MPEG4_MP3
START_DISPATCH;
CB(MPEG4_AUDIO_OPEN_EX, OpenEx)
CB(MPEG4_AUDIO_CODEC_INFO_STRING, GetCodecInfoString)
CB(MPEG4_AUDIO_BITRATE, GetCurrentBitrate)
CB(MPEG4_AUDIO_FRAMESIZE, OutputFrameSize)
CB(MPEG4_AUDIO_OUTPUTINFO, GetOutputProperties)
CB(MPEG4_AUDIO_OUTPUTINFO_EX, GetOutputPropertiesEx)
CB(MPEG4_AUDIO_DECODE, DecodeSample)
VCB(MPEG4_AUDIO_FLUSH, Flush)
VCB(MPEG4_AUDIO_CLOSE, Close)
CB(MPEG4_AUDIO_HANDLES_CODEC, CanHandleCodec)
CB(MPEG4_AUDIO_HANDLES_TYPE, CanHandleType)
CB(MPEG4_AUDIO_HANDLES_MPEG4_TYPE, CanHandleMPEG4Type)
CB(MPEG4_AUDIO_SET_GAIN, SetGain)
END_DISPATCH;