Ejemplo n.º 1
0
bool _AUDIO_AAC::Init(const uint8_t *pBuffer, uint32_t length) {
	Clear();
	//http://wiki.multimedia.cx/index.php?title=MP4A#Audio_Specific_Config

	if (length < 2) {
		FATAL("Invalid length: %u", length);
		return false;
	}

	//1. Prepare the bit array
	BitArray ba;
	ba.ReadFromBuffer(pBuffer, length);

	//2. Read the audio object type
	_audioObjectType = ba.ReadBits<uint8_t > (5);
	if ((_audioObjectType != 1)
			&& (_audioObjectType != 2)
			&& (_audioObjectType != 3)
			&& (_audioObjectType != 4)
			&& (_audioObjectType != 6)
			&& (_audioObjectType != 17)
			&& (_audioObjectType != 19)
			&& (_audioObjectType != 20)
			&& (_audioObjectType != 23)
			&& (_audioObjectType != 39)) {
		FATAL("Invalid _audioObjectType: %hhu", _audioObjectType);
		return false;
	}

	//3. Read the sample rate index
	_sampleRateIndex = ba.ReadBits<uint8_t > (4);
	if ((_sampleRateIndex == 13)
			|| (_sampleRateIndex == 14)) {
		FATAL("Invalid sample rate: %hhu", _sampleRateIndex);
		return false;
	}
	if (_sampleRateIndex == 15) {
		if (length < 5) {
			FATAL("Invalid length: %u", length);
			return false;
		}
		_sampleRate = ba.ReadBits<uint32_t > (24);
	} else {
		uint32_t rates[] = {
			96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000,
			12000, 11025, 8000, 7350
		};
		_sampleRate = rates[_sampleRateIndex];
	}

	//4. read the channel configuration index
	_channelConfigurationIndex = ba.ReadBits<uint8_t > (4);
	if ((_channelConfigurationIndex == 0)
			|| (_channelConfigurationIndex >= 8)) {
		FATAL("Invalid _channelConfigurationIndex: %hhu", _channelConfigurationIndex);
		return false;
	}

	_pAAC = new uint8_t[length];
	memcpy(_pAAC, pBuffer, length);
	_aacLength = length;


	return true;
}