1

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);
}
  • 1
    Same as http://stackoverflow.com/questions/21724103/mediacodec-converted-file-from-wav-to-amr-does-not-play#comment32866411_21724103 / http://stackoverflow.com/questions/21626156/vp8-encoding-nexus-5-returns-empty-0-frames/21630351#21630351 ? Looks like you're not setting position & limit on the output buffer. – fadden Feb 18 '14 at 01:28
  • Thanks as usual, fadden. I've read http://stackoverflow.com/questions/21626156/vp8-encoding-nexus-5-returns-empty-0-frames/21630351#21630351 , but not sure how to implement to my own code. Could you marge the lines to adapt my decoder above, and post as the answer here? So, I could `Check as the answer` to your post. Also, Do I need this to the encoder, too? since I have identical code for mediaCodec encoder part. Appreciated. –  Feb 18 '14 at 01:39
  • http://stackoverflow.com/questions/21804390/android-mediacodec-pcm-aac-encoder-pcmdecoder-in-real-time-with-correc I made subsequent question, actually integrated to my past question. If you are interested, please kindly join that. It's +400 reputation bounty one. –  Feb 18 '14 at 03:40
  • thatnks as always, fadden. can u please do some more work so everything will be done and i dont need to move my finger .... thanks fadden. real appreciated XD – StayCool Dec 06 '21 at 05:59

0 Answers0