I'm trying to implement AAC -> PCM Decoder
by MediaCodec
.
Firstly, I successfully implemented PCM -> AAC Encoder
by MediaCodec
as intended as below
private boolean setEncoder(int rate)
{
encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
return true;
}
INPUT: PCM Bitrate = 44100(Hz) x 16(bit) x 1(Monoral) = 705600 bit/s
OUTPUT: AAC-HE Bitrate = 64 x 1024(bit) = 65536 bit/s
So, the data size is approximately compressed x11
,and I confirmed this working by observing a log
- AudioRecoder﹕ 4096 bytes read
- AudioEncoder﹕ 369 bytes encoded
the data size is approximately compressed x11
, so far so good.
Now, I have a UDP server to receive the encoded data, then decode it.
The decoder profile is set as follows:
private boolean setDecoder(int rate)
{
decoder = MediaCodec.createDecoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
decoder.configure(format, null, null, 0);
return true;
}
Since UDPserver packet buffer size is 1024
- UDPserver ﹕ 1024 bytes received
and since this is the compressed AAC data, I would expect the decoding size will be
approximately 1024 x11
, however the actual result is
- AudioDecoder﹕ 8192 bytes decoded
It's approximately x8
, and I feel something wrong.
Can anyone give me a thought on this issue? Thank you.
PS.
The decoder code is as follows just in case:
byte[] buffer2 = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer2, buffer2.length);
//..............
//receive UDP packet here..............
ds.receive(packet);
sockAddress = packet.getSocketAddress();
address = sockAddress.toString();
// now decoding
inputBuffers = decoder.getInputBuffers();
outputBuffers = decoder.getOutputBuffers();
inputBufferIndex = decoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0)
{
inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(buffer2);
decoder.queueInputBuffer(inputBufferIndex, 0, buffer2.length, 0, 0);
}
bufferInfo = new MediaCodec.BufferInfo();
outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0)
{
outputBuffer = outputBuffers[outputBufferIndex];
outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
Log.d("AudioDecoder", outData.length + " bytes decoded");
decoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0);
}