Error when create AAC encoder with MediaCodec

Matt  picture Matt · Mar 20, 2014 · Viewed 11.8k times · Source

The question is simple, But I have no any clue to solve it:

I write a single line code to create an AAC encoder on my Nexus 4 (Android 4.4.2)

MediaCodec codec = MediaCodec.createEncoderByType("audio/mp4a-latm");

The return value saved in "codec" is not null, but I get a red error message in Logcat:

03-20 15:25:08.985: E/OMXMaster(24517): A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.

I have also tried another line:

MediaCodec codec = MediaCodec.createByCodecName("OMX.google.aac.encoder");

And get the same error result.

Did I miss any initialization steps before using MediaCodec? I did not find any information about this in the official document.

Did anyone run into this problem?

Actually I am trying to encode PCM to AAC file. And I have read this post @hubeir. It seems that he has made it work. I did the same thing:(1)setup the mediacodec and feed in PCM data to get encoded frame. To do that, I read the code from cts . Every encoded frame length is about 371-379. (2)Add adts header to the frame, then save to file. I have checked the head bit by bit, it is correct. But the file is still not playable. So I think maybe the error log is the problem.

The following is my whole code, for reference:

MediaCodec codec = MediaCodec.createByCodecName("OMX.google.aac.encoder");      
MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, 
                      MediaCodecInfo.CodecProfileLevel.AACObjectELD);
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, nSamplerate);
    format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, nChannels);

    codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    codec.start();

    ByteBuffer[] inputBuffers = codec.getInputBuffers();
    ByteBuffer[] outputBuffers = codec.getOutputBuffers();

    boolean bEndInput = false;
    boolean bEndOutput = false;

while(true)
{
    if (!bEndInput)
    {
        int inputBufferIndex = codec.dequeueInputBuffer(0);
        if (inputBufferIndex >= 0)
        {
            int nLen = app.readPCM(nHandle,inputBuffers[inputBufferIndex]);//This line read PCM, return 0 if end of data.
            int nBufLen = inputBuffers[inputBufferIndex].capacity(); 
            if (nLen == nBufLen)
                codec.queueInputBuffer(inputBufferIndex, 0, nLen, 0,  MediaCodec.BUFFER_FLAG_SYNC_FRAME);
            else if (nLen < nBufLen)
            {
                codec.queueInputBuffer(inputBufferIndex, 0, nLen, 0,  MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                bEndInput = true;
                break;
            }   
        }
    }

    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    if (!bEndOutput)
    {
            int outputBufferIndex = codec.dequeueOutputBuffer(info, 0);
            if (outputBufferIndex  >= 0)
            {
                int outBitsSize   = info.size;
                Log.d("test", "Offset:"+info.offset);
                Log.d("test", "Size:"+info.size);
                Log.d("test", "Time:"+info.presentationTimeUs);
                Log.d("test", "Flags:"+info.flags);
                if (outBitsSize <= 10)
                {
                    codec.releaseOutputBuffer(outputBufferIndex, false /* render */);
                    continue;
                }

                int outPacketSize = outBitsSize + 7;    // 7 is ADTS size
                ByteBuffer outBuf = outputBuffers[outputBufferIndex];

                outBuf.position(info.offset);
                outBuf.limit(info.offset + outBitsSize);
                try {
                    byte[] data = new byte[outPacketSize];  //space for ADTS header included
                    addADTStoPacket(data, outPacketSize);
                    outBuf.get(data, 7, outBitsSize);
                    outBuf.position(info.offset);
                    outputStream.write(data, 0, outPacketSize);  //open FileOutputStream beforehand
                } catch (IOException e) {
                    Log.e("test", "failed writing bitstream data to file");
                    e.printStackTrace();
                }

                outBuf.clear();
                codec.releaseOutputBuffer(outputBufferIndex, false /* render */);
                Log.d("test", "  dequeued " + outBitsSize + " bytes of output data.");
                Log.d("test", "  wrote " + outPacketSize + " bytes into output file.");

                if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM)
                {
                    bEndOutput = true;
                    //break;
                }   
            }
            else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) 
            {
                 outputBuffers = codec.getOutputBuffers();
            } 
            else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) 
            {

            }
    }

    if (bEndInput && bEndOutput)
        break;
}

Answer

Matt  picture Matt · Mar 21, 2014

I figured out it.

(1) The error setting in the media format:

format.setInteger(MediaFormat.KEY_AAC_PROFILE, 
                      MediaCodecInfo.CodecProfileLevel.AACObjectELD);

should be

format.setInteger(MediaFormat.KEY_AAC_PROFILE, 
                      MediaCodecInfo.CodecProfileLevel.AACObjectLC);

(2) The encoded frame could be written to file with adts file only when (info.flags == 0)

(3) And the output file name suffix should be "aac". "mp4" or "m4a" may not work for some application.

MediaCodec codec = MediaCodec.createByCodecName("OMX.google.aac.encoder");      
MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, 
                      MediaCodecInfo.CodecProfileLevel.AACObjectLC); //fixed version
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, nSamplerate);
    format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, nChannels);

    codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    codec.start();

    ByteBuffer[] inputBuffers = codec.getInputBuffers();
    ByteBuffer[] outputBuffers = codec.getOutputBuffers();

    boolean bEndInput = false;
    boolean bEndOutput = false;

while(true)
{
    if (!bEndInput)
    {
        int inputBufferIndex = codec.dequeueInputBuffer(0);
        if (inputBufferIndex >= 0)
        {
            int nLen = app.readPCM(nHandle,inputBuffers[inputBufferIndex]);//This line read PCM, return 0 if end of data.
            int nBufLen = inputBuffers[inputBufferIndex].capacity(); 
            if (nLen == nBufLen)
                codec.queueInputBuffer(inputBufferIndex, 0, nLen, 0,  MediaCodec.BUFFER_FLAG_SYNC_FRAME);
            else if (nLen < nBufLen)
            {
                codec.queueInputBuffer(inputBufferIndex, 0, nLen, 0,  MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                bEndInput = true;
                break;
            }   
        }
    }

    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    if (!bEndOutput)
    {
            int outputBufferIndex = codec.dequeueOutputBuffer(info, 0);
            if (outputBufferIndex  >= 0)
            {
                int outBitsSize   = info.size;
                Log.d("test", "Offset:"+info.offset);
                Log.d("test", "Size:"+info.size);
                Log.d("test", "Time:"+info.presentationTimeUs);
                Log.d("test", "Flags:"+info.flags);
                if (info.flags != 0) //fixed version
                {
                    codec.releaseOutputBuffer(outputBufferIndex, false /* render */);
                    continue;
                }

                int outPacketSize = outBitsSize + 7;    // 7 is ADTS size
                ByteBuffer outBuf = outputBuffers[outputBufferIndex];

                outBuf.position(info.offset);
                outBuf.limit(info.offset + outBitsSize);
                try {
                    byte[] data = new byte[outPacketSize];  //space for ADTS header included
                    addADTStoPacket(data, outPacketSize);
                    outBuf.get(data, 7, outBitsSize);
                    outBuf.position(info.offset);
                    outputStream.write(data, 0, outPacketSize);  //open FileOutputStream beforehand
                } catch (IOException e) {
                    Log.e("test", "failed writing bitstream data to file");
                    e.printStackTrace();
                }

                outBuf.clear();
                codec.releaseOutputBuffer(outputBufferIndex, false /* render */);
                Log.d("test", "  dequeued " + outBitsSize + " bytes of output data.");
                Log.d("test", "  wrote " + outPacketSize + " bytes into output file.");

                if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM)
                {
                    bEndOutput = true;
                    //break;
                }   
            }
            else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) 
            {
                 outputBuffers = codec.getOutputBuffers();
            } 
            else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) 
            {

            }
    }

    if (bEndInput && bEndOutput)
        break;
}