androidandroid-mediacodecpcmaac

when i encode pcm to an aac file,the file is not correctly analyzed with aac analysis tool


When the sample-rate is 44100,the aac byte array can be decoded and i can sound voice ,but others can not be decoded correctly.I don't know why?

Before encode,i set the MediaFormat.the csd-0 is copy from other sample,and the MediaFormat param will be show.

private boolean prepare() {
    String codecName = null;
    for (int i = 0; i < MediaCodecList.getCodecCount(); i++) {
        MediaCodecInfo mediaCodecInfo = MediaCodecList.getCodecInfoAt(i);
        for (String type : mediaCodecInfo.getSupportedTypes()) {
            if (TextUtils.equals(type, MIME_TYPE)
                    && mediaCodecInfo.isEncoder()) {
                codecName = mediaCodecInfo.getName();
                break;
            }
        }
        if (null != codecName) {
            break;
        }
    }
    try {
        mBufferInfo = new MediaCodec.BufferInfo();
        mEncoder = MediaCodec.createByCodecName(codecName);
        MediaFormat mediaFormat = MediaFormat.createAudioFormat(MIME_TYPE,
                KEY_SAMPLE_RATE, KEY_CHANNEL_COUNT);
        mediaFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, KEY_BIT_RATE);
        mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE,
                KEY_AAC_PROFILE);
        mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, mFrameSize);
        byte[] data = new byte[]{(byte) 0x11, (byte) 0x90};
        ByteBuffer csd_0 = ByteBuffer.wrap(data);
        mediaFormat.setByteBuffer("csd-0", csd_0);
        mEncoder.configure(mediaFormat, null, null,
                MediaCodec.CONFIGURE_FLAG_ENCODE);
        mEncoder.start();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    mBuffer = new byte[mFrameSize];
    int minBufferSize = AudioRecord.getMinBufferSize(KEY_SAMPLE_RATE, CHANNEL_IN,
            AUDIO_FORMAT);
    mRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
            KEY_SAMPLE_RATE, CHANNEL_IN, AUDIO_FORMAT, minBufferSize * 2);
    mRecord.startRecording();
    return true;
}

Use android MediaCodec api to encode to aac in while.encoderCallback will be call to decode by MediaCodec.

long presentationTimeUs = 0;
    private void encode(byte[] data) {
        //api > 21
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {

            int inputBufferIndex = mEncoder.dequeueInputBuffer(-1);
            if (inputBufferIndex >= 0) {

                ByteBuffer inputBuffer = mEncoder.getInputBuffer(inputBufferIndex);
                inputBuffer.clear();

                inputBuffer.put(data);
                inputBuffer.limit(data.length);

                mEncoder.queueInputBuffer(inputBufferIndex, 0, data.length,
                        computePresentationTime(presentationTimeUs), BUFFER_FLAG_CODEC_CONFIG);
                presentationTimeUs+=1;
            }

            int outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, 0);

            while (outputBufferIndex >= 0) {

                ByteBuffer outputBuffer = mEncoder.getOutputBuffer(outputBufferIndex);

                outputBuffer.position(mBufferInfo.offset);
                outputBuffer.limit(mBufferInfo.offset + mBufferInfo.size);


                int length = mBufferInfo.size + 7;
                if (mFrameByte == null || mFrameByte.length < length) {
                    mFrameByte = new byte[length];
                }
                addADTStoPacket(mFrameByte, length);

                outputBuffer.get(mFrameByte, 7, mBufferInfo.size);

                if (encoderCallback!=null){
                    encoderCallback.encode(mFrameByte);

                    mFrameByte = null;
                }
                mEncoder.releaseOutputBuffer(outputBufferIndex, false);
                outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, 0);
            }
    }

add adts header.

private void addADTStoPacket(byte[] packet, int packetLen) {
        int profile = KEY_AAC_PROFILE;  //AAC LC
        int freqIdx = FREQ_IDX;  //44.1KHz
        int chanCfg = CHAN_CFG;  //CPE
        packet[0] = (byte) 0xFF;
        packet[1] = (byte) 0xF9;
        packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
        packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
        packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
        packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
        packet[6] = (byte) 0xFC;
    }

These are the params about sample-rate.

public interface AudioCodec {
    String MIME_TYPE = MediaFormat.MIMETYPE_AUDIO_AAC;
    int KEY_CHANNEL_COUNT = 2;
    int KEY_BIT_RATE = 64 * 1024;
    int KEY_SAMPLE_RATE = 44100;
    int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
    int CHANNEL_IN = AudioFormat.CHANNEL_IN_STEREO;
    int KEY_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
    int WAIT_TIME = 10000;
    int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
    int BUFFFER_SIZE = 2048;
    int FREQ_IDX = 4;
    int CHAN_CFG = 1;
}

Solution

  • Maybe my English is poor,so someone can not understand my description.Now,I've found the reason which the AAC file does not work or plays at a half speed.AAC's csd-0 configuration decides whether or not successful.

    I set below params to MediaCodec which is used to encode and decode.The sample-rate is 44100.

    int KEY_CHANNEL_COUNT = 2;
    int KEY_BIT_RATE = 64*1024;
    int KEY_SAMPLE_RATE = 44100;
    int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
    int CHANNEL_IN = AudioFormat.CHANNEL_IN_STEREO;
    int KEY_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
    int WAIT_TIME = 10000;
    int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
    int BUFFFER_SIZE = 2048;
    int FREQ_IDX = 4;
    int CHAN_CFG = 2;
    byte CSD_0 = 0x12;
    byte CSD_1 = (byte) 0x12;
    

    csd-0 is be setting for encoding aac from pcm by MediaCodec.

    byte[] data = new byte[]{CSD_0, CSD_1};
    ByteBuffer csd_0 = ByteBuffer.wrap(data);
    mediaFormat.setByteBuffer("csd-0", csd_0);
    

    csd-0 is changeable by your sample-rate.For example your sample-rate is 8000.

    int KEY_CHANNEL_COUNT = 1;
    int KEY_SAMPLE_RATE = 8000;
    int KEY_BIT_RATE = 16000;
    int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
    int CHANNEL_IN = AudioFormat.CHANNEL_IN_STEREO;
    int KEY_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
    int WAIT_TIME = 10000;
    int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
    int BUFFFER_SIZE = 2048;
    int FREQ_IDX = 11;
    int CHAN_CFG = 1;
    byte CSD_0 = 0x15;
    byte CSD_1 = (byte) 0x88;