Bug 865234 - Part 7: Change the speaker up-mixing and down-mixing rules according to the Web Audio spec; r=roc

This commit is contained in:
Ehsan Akhgari 2013-04-29 20:15:26 -04:00
Родитель 28ad963570
Коммит d000f982db
2 изменённых файлов: 71 добавлений и 96 удалений

Просмотреть файл

@ -20,17 +20,51 @@ enum {
static const uint32_t CUSTOM_CHANNEL_LAYOUTS = 6;
static const int IGNORE = CUSTOM_CHANNEL_LAYOUTS;
static const float IGNORE_F = 0.0f;
uint32_t
GetAudioChannelsSuperset(uint32_t aChannels1, uint32_t aChannels2)
{
if (aChannels1 == 3 && aChannels2 == 4) {
// quad layout has no center channel, but input has a center channel as well
// as L and R, so we actually need a 5-channel layout here.
return 5;
}
return std::max(aChannels1, aChannels2);
}
/**
* UpMixMatrix represents a conversion matrix by exploiting the fact that
* each output channel comes from at most one input channel.
*/
struct UpMixMatrix {
uint8_t mInputDestination[CUSTOM_CHANNEL_LAYOUTS];
};
static const UpMixMatrix
gUpMixMatrices[CUSTOM_CHANNEL_LAYOUTS*(CUSTOM_CHANNEL_LAYOUTS - 1)/2] =
{
// Upmixes from mono
{ { 0, 0 } },
{ { 0, IGNORE, IGNORE } },
{ { 0, 0, IGNORE, IGNORE } },
{ { 0, IGNORE, IGNORE, IGNORE, IGNORE } },
{ { IGNORE, IGNORE, 0, IGNORE, IGNORE, IGNORE } },
// Upmixes from stereo
{ { 0, 1, IGNORE } },
{ { 0, 1, IGNORE, IGNORE } },
{ { 0, 1, IGNORE, IGNORE, IGNORE } },
{ { 0, 1, IGNORE, IGNORE, IGNORE, IGNORE } },
// Upmixes from 3-channel
{ { 0, 1, 2, IGNORE } },
{ { 0, 1, 2, IGNORE, IGNORE } },
{ { 0, 1, 2, IGNORE, IGNORE, IGNORE } },
// Upmixes from quad
{ { 0, 1, 2, 3, IGNORE } },
{ { 0, 1, IGNORE, IGNORE, 2, 3 } },
// Upmixes from 5-channel
{ { 0, 1, 2, 3, 4, IGNORE } }
};
static const int gMixingMatrixIndexByChannels[CUSTOM_CHANNEL_LAYOUTS - 1] =
{ 0, 5, 9, 12, 14 };
void
AudioChannelsUpMix(nsTArray<const void*>* aChannelArray,
uint32_t aOutputChannelCount,
@ -46,81 +80,26 @@ AudioChannelsUpMix(nsTArray<const void*>* aChannelArray,
aChannelArray->SetLength(outputChannelCount);
if (inputChannelCount < CUSTOM_CHANNEL_LAYOUTS) {
const void* surroundChannels[CUSTOM_CHANNEL_LAYOUTS] =
{ aZeroChannel, aZeroChannel, aZeroChannel,
aZeroChannel, aZeroChannel, aZeroChannel
};
// First just map everything up to 5.1
switch (inputChannelCount) {
case 1:
surroundChannels[SURROUND_C] = aChannelArray->ElementAt(0);
break;
case 2:
surroundChannels[SURROUND_L] = aChannelArray->ElementAt(0);
surroundChannels[SURROUND_R] = aChannelArray->ElementAt(1);
break;
case 3:
surroundChannels[SURROUND_L] = aChannelArray->ElementAt(0);
surroundChannels[SURROUND_R] = aChannelArray->ElementAt(1);
surroundChannels[SURROUND_C] = aChannelArray->ElementAt(2);
break;
case 4:
surroundChannels[SURROUND_L] = aChannelArray->ElementAt(0);
surroundChannels[SURROUND_R] = aChannelArray->ElementAt(1);
surroundChannels[SURROUND_SL] = aChannelArray->ElementAt(2);
surroundChannels[SURROUND_SR] = aChannelArray->ElementAt(3);
break;
case 5:
surroundChannels[SURROUND_L] = aChannelArray->ElementAt(0);
surroundChannels[SURROUND_R] = aChannelArray->ElementAt(1);
surroundChannels[SURROUND_C] = aChannelArray->ElementAt(2);
surroundChannels[SURROUND_SL] = aChannelArray->ElementAt(3);
surroundChannels[SURROUND_SR] = aChannelArray->ElementAt(4);
break;
}
if (inputChannelCount < CUSTOM_CHANNEL_LAYOUTS &&
outputChannelCount <= CUSTOM_CHANNEL_LAYOUTS) {
const UpMixMatrix& m = gUpMixMatrices[
gMixingMatrixIndexByChannels[inputChannelCount - 1] +
outputChannelCount - inputChannelCount - 1];
if (outputChannelCount < CUSTOM_CHANNEL_LAYOUTS) {
// Map back to aOutputChannelCount
switch (outputChannelCount) {
case 2:
// Upmix from mono, so use the center channel.
aChannelArray->ElementAt(0) = surroundChannels[SURROUND_C];
aChannelArray->ElementAt(1) = surroundChannels[SURROUND_C];
break;
case 3:
aChannelArray->ElementAt(0) = surroundChannels[SURROUND_L];
aChannelArray->ElementAt(1) = surroundChannels[SURROUND_R];
aChannelArray->ElementAt(2) = surroundChannels[SURROUND_C];
break;
case 4:
// We avoided this case up above.
NS_ASSERTION(inputChannelCount != 3,
"3->4 upmix not supported directly");
if (inputChannelCount == 1) {
// Output has no center channel, so map the mono to
// L+R channels per Web Audio
aChannelArray->ElementAt(0) = surroundChannels[SURROUND_C];
aChannelArray->ElementAt(1) = surroundChannels[SURROUND_C];
} else {
aChannelArray->ElementAt(0) = surroundChannels[SURROUND_L];
aChannelArray->ElementAt(1) = surroundChannels[SURROUND_R];
}
aChannelArray->ElementAt(2) = surroundChannels[SURROUND_SL];
aChannelArray->ElementAt(3) = surroundChannels[SURROUND_SR];
break;
case 5:
aChannelArray->ElementAt(0) = surroundChannels[SURROUND_L];
aChannelArray->ElementAt(1) = surroundChannels[SURROUND_R];
aChannelArray->ElementAt(2) = surroundChannels[SURROUND_C];
aChannelArray->ElementAt(3) = surroundChannels[SURROUND_SL];
aChannelArray->ElementAt(4) = surroundChannels[SURROUND_SR];
const void* outputChannels[CUSTOM_CHANNEL_LAYOUTS];
for (uint32_t i = 0; i < outputChannelCount; ++i) {
uint8_t channelIndex = m.mInputDestination[i];
if (channelIndex == IGNORE) {
outputChannels[i] = aZeroChannel;
} else {
outputChannels[i] = aChannelArray->ElementAt(channelIndex);
}
return;
}
memcpy(aChannelArray->Elements(), surroundChannels, sizeof(surroundChannels));
inputChannelCount = CUSTOM_CHANNEL_LAYOUTS;
for (uint32_t i = 0; i < outputChannelCount; ++i) {
aChannelArray->ElementAt(i) = outputChannels[i];
}
return;
}
for (uint32_t i = inputChannelCount; i < outputChannelCount; ++i) {
@ -145,37 +124,31 @@ struct DownMixMatrix {
float mInputCoefficient[CUSTOM_CHANNEL_LAYOUTS];
};
static const int IGNORE = CUSTOM_CHANNEL_LAYOUTS;
static const float IGNORE_F = 0.0f;
static const DownMixMatrix
gDownMixMatrices[CUSTOM_CHANNEL_LAYOUTS*(CUSTOM_CHANNEL_LAYOUTS - 1)/2] =
{
// Downmixes to mono
{ { 0, 0 }, IGNORE, { 0.5f, 0.5f } },
{ { 0, 0, 0 }, IGNORE, { 0.3333f, 0.3333f, 0.3333f } },
{ { 0, IGNORE, IGNORE }, IGNORE, { 1.0f, IGNORE_F, IGNORE_F } },
{ { 0, 0, 0, 0 }, IGNORE, { 0.25f, 0.25f, 0.25f, 0.25f } },
{ { 0, 0, 0, 0, 0 }, IGNORE, { 0.7071f, 0.7071f, 1.0f, 0.5f, 0.5f } },
{ { 0, IGNORE, IGNORE, IGNORE, IGNORE }, IGNORE, { 1.0f, IGNORE_F, IGNORE_F, IGNORE_F, IGNORE_F } },
{ { 0, 0, 0, IGNORE, 0, 0 }, IGNORE, { 0.7071f, 0.7071f, 1.0f, IGNORE_F, 0.5f, 0.5f } },
// Downmixes to stereo
{ { 0, 1, 0 }, 1, { 1.0f, 1.0f, 0.7071f } },
{ { 0, 1, IGNORE }, IGNORE, { 1.0f, 1.0f, IGNORE_F } },
{ { 0, 1, 0, 1 }, IGNORE, { 0.5f, 0.5f, 0.5f, 0.5f } },
{ { 0, 1, 0, 0, 1 }, 1, { 1.0f, 1.0f, 0.7071f, 0.7071f, 0.7071f } },
{ { 0, 1, IGNORE, IGNORE, IGNORE }, IGNORE, { 1.0f, 1.0f, IGNORE_F, IGNORE_F, IGNORE_F } },
{ { 0, 1, 0, IGNORE, 0, 1 }, 1, { 1.0f, 1.0f, 0.7071f, IGNORE_F, 0.7071f, 0.7071f } },
// Downmixes to 3-channel
{ { 0, 1, 0, 1 }, IGNORE, { 0.25f, 0.25f, 0.25f, 0.25f } },
{ { 0, 1, 2, 0, 1 }, IGNORE, { 0.5f, 0.5f, 1.0f, 0.5f, 0.5f } },
{ { 0, 1, 2, IGNORE, 0, 1 }, IGNORE, { 0.5f, 0.5f, 1.0f, IGNORE_F, 0.5f, 0.5f } },
{ { 0, 1, 2, IGNORE }, IGNORE, { 1.0f, 1.0f, 1.0f, IGNORE_F } },
{ { 0, 1, 2, IGNORE, IGNORE }, IGNORE, { 1.0f, 1.0f, 1.0f, IGNORE_F, IGNORE_F } },
{ { 0, 1, 2, IGNORE, IGNORE, IGNORE }, IGNORE, { 1.0f, 1.0f, 1.0f, IGNORE_F, IGNORE_F, IGNORE_F } },
// Downmixes to quad
{ { 0, 1, 0, 2, 3 }, 1, { 1.0f, 1.0f, 0.7071f, 1.0f, 1.0f } },
{ { 0, 1, 2, 3, IGNORE }, IGNORE, { 1.0f, 1.0f, 1.0f, 1.0f, IGNORE_F } },
{ { 0, 1, 0, IGNORE, 2, 3 }, 1, { 1.0f, 1.0f, 0.7071f, IGNORE_F, 1.0f, 1.0f } },
// Downmixes to 5-channel
{ { 0, 1, 2, IGNORE, 3, 4 }, IGNORE, { 1.0f, 1.0f, 1.0f, IGNORE_F, 1.0f, 1.0f } }
{ { 0, 1, 2, 3, 4, IGNORE }, IGNORE, { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, IGNORE_F } }
};
static const int gDownMixMatrixIndexByOutputChannels[CUSTOM_CHANNEL_LAYOUTS - 1] =
{ 0, 5, 9, 12, 14 };
void
AudioChannelsDownMix(const nsTArray<const void*>& aChannelArray,
float** aOutputChannels,
@ -186,7 +159,7 @@ AudioChannelsDownMix(const nsTArray<const void*>& aChannelArray,
const void* const* inputChannels = aChannelArray.Elements();
NS_ASSERTION(inputChannelCount > aOutputChannelCount, "Nothing to do");
if (aOutputChannelCount >= 6) {
if (inputChannelCount > 6) {
// Just drop the unknown channels.
for (uint32_t o = 0; o < aOutputChannelCount; ++o) {
memcpy(aOutputChannels[o], inputChannels[o], aDuration*sizeof(float));
@ -198,7 +171,7 @@ AudioChannelsDownMix(const nsTArray<const void*>& aChannelArray,
inputChannelCount = std::min<uint32_t>(6, inputChannelCount);
const DownMixMatrix& m = gDownMixMatrices[
gDownMixMatrixIndexByOutputChannels[aOutputChannelCount - 1] +
gMixingMatrixIndexByChannels[aOutputChannelCount - 1] +
inputChannelCount - aOutputChannelCount - 1];
// This is slow, but general. We can define custom code for special

Просмотреть файл

@ -289,6 +289,8 @@ AudioNodeStream::ObtainInputBlock(AudioChunk* aTmpChunk)
AllocateAudioBlock(outputChannelCount, aTmpChunk);
float silenceChannel[WEBAUDIO_BLOCK_SIZE] = {0.f};
// The static storage here should be 1KB, so it's fine
nsAutoTArray<float, GUESS_AUDIO_CHANNELS*WEBAUDIO_BLOCK_SIZE> downmixBuffer;
for (uint32_t i = 0; i < inputChunkCount; ++i) {
AudioChunk* chunk = inputChunks[i];
@ -309,9 +311,9 @@ AudioNodeStream::ObtainInputBlock(AudioChunk* aTmpChunk)
if (mMixingMode.mChannelInterpretation == ChannelInterpretation::Speakers) {
nsAutoTArray<float*,GUESS_AUDIO_CHANNELS> outputChannels;
outputChannels.SetLength(outputChannelCount);
downmixBuffer.SetLength(outputChannelCount * WEBAUDIO_BLOCK_SIZE);
for (uint32_t i = 0; i < outputChannelCount; ++i) {
outputChannels[i] =
const_cast<float*>(static_cast<const float*>(aTmpChunk->mChannelData[i]));
outputChannels[i] = &downmixBuffer[i * WEBAUDIO_BLOCK_SIZE];
}
AudioChannelsDownMix(channels, outputChannels.Elements(),