package com.bytedance.bae.codec;

import X.0HH;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.view.Surface;
import com.bytedance.bae.base.BaeLogging;
import com.bytedance.covode.number.Covode;
import java.nio.ByteBuffer;

public class MediaCodecAACDecoder {
    public long input_ts;
    public MediaCodec mAACDecoder;
    public byte[] mAsc;
    public MediaCodec.BufferInfo mBufferInfo;
    public int mChannels = 1;
    public byte[] mDecodedData;
    public int mHasADTS;
    public ByteBuffer[] mInputBuffers;
    public String mMimeType = "audio/mp4a-latm";
    public ByteBuffer[] mOutputBuffers;
    public MediaFormat mOutputFormat;
    public int mProfile = 2;
    public int mSampleRate = 44100;
    public long output_ts;

    static {
        Covode.recordClassIndex(24222);
    }

    public byte[] getDecodedData() {
        return this.mDecodedData;
    }

    public long getOutputTs() {
        return this.output_ts;
    }

    public int getOutputChannels() {
        return this.mOutputFormat.getInteger("channel-count");
    }

    public int getOutputSampleRate() {
        return this.mOutputFormat.getInteger("sample-rate");
    }

    public MediaCodecAACDecoder() {
        BaeLogging.e("MediaCodecAACDecoder", "MediaCodecAAC Decoder Created");
    }

    public void closeDecoder() {
        try {
            MediaCodec mediaCodec = this.mAACDecoder;
            if (mediaCodec != null) {
                mediaCodec.stop();
                this.mAACDecoder.release();
                this.mAACDecoder = null;
            }
        } catch (Exception e) {
            0HH.LIZ(e);
            BaeLogging.e("MediaCodecAACDecoder", "close AAC decoder failed");
        }
    }

    public boolean openEncoder() {
        try {
            MediaCodec createDecoderByType = MediaCodec.createDecoderByType(this.mMimeType);
            this.mAACDecoder = createDecoderByType;
            if (createDecoderByType != null) {
                return true;
            }
            return false;
        } catch (Exception e) {
            0HH.LIZ(e);
            this.mAACDecoder = null;
            BaeLogging.e("MediaCodecAACDecoder", "Open AAC Decoder failed");
            return false;
        }
    }

    public boolean configAndStartDecoder() {
        try {
            MediaFormat createAudioFormat = MediaFormat.createAudioFormat(this.mMimeType, this.mSampleRate, this.mChannels);
            createAudioFormat.setInteger("aac-profile", this.mProfile);
            createAudioFormat.setInteger("is-adts", this.mHasADTS);
            createAudioFormat.setInteger("aac-max-output-channel_count", this.mChannels);
            genAsc();
            createAudioFormat.setByteBuffer("csd-0", ByteBuffer.wrap(this.mAsc));
            this.mAACDecoder.configure(createAudioFormat, (Surface) null, (MediaCrypto) null, 0);
            this.mAACDecoder.start();
            this.mBufferInfo = new MediaCodec.BufferInfo();
            return true;
        } catch (Exception e) {
            0HH.LIZ(e);
            closeDecoder();
            BaeLogging.e("MediaCodecAACDecoder", "Config and Start Decoder Error");
            return false;
        }
    }

    private void genAsc() {
        int i;
        int i2;
        int i3 = this.mProfile;
        int i4 = 4;
        if (i3 == 2) {
            this.mAsc = new byte[2];
        } else {
            this.mAsc = new byte[4];
        }
        int i5 = this.mSampleRate;
        if (48000 == i5) {
            i4 = 3;
            i = 6;
        } else if (44100 == i5) {
            i = 7;
        } else {
            i4 = 0;
            i = 0;
        }
        int i6 = this.mChannels;
        if (1 == i6) {
            i2 = 1;
        } else if (2 == i6) {
            i2 = 2;
        } else {
            i2 = 0;
        }
        byte b = (byte) i3;
        if (2 == i3) {
            byte[] bArr = this.mAsc;
            bArr[0] = (byte) ((b << 3) | (i4 >> 1));
            bArr[1] = (byte) ((i4 << 7) | (i2 << 3));
        } else if (5 == i3) {
            byte[] bArr2 = this.mAsc;
            bArr2[0] = (byte) ((b << 3) | (i >> 1));
            bArr2[1] = (byte) ((i << 7) | (i2 << 3) | (i4 >> 1));
            bArr2[2] = (byte) ((i4 << 7) | 8);
            bArr2[3] = 0;
        } else if (29 == i3) {
            byte[] bArr3 = this.mAsc;
            bArr3[0] = (byte) ((b << 3) | (i >> 1));
            bArr3[1] = (byte) ((i << 7) | 8 | (i4 >> 1));
            bArr3[2] = (byte) ((i4 << 7) | 8);
            bArr3[3] = 0;
        }
    }

    public void setADTSValue(int i) {
        this.mHasADTS = i;
    }

    public void setChannelsValue(int i) {
        this.mChannels = i;
    }

    public void setInputTs(long j) {
        this.input_ts = j;
    }

    public void setSampeRateValue(int i) {
        this.mSampleRate = i;
    }

    public void setProfileValue(String str) {
        if (str.equals("he_aac")) {
            this.mProfile = 5;
        } else if (str.equals("he_aac_v2")) {
            this.mProfile = 29;
        } else {
            this.mProfile = 2;
        }
    }

    public boolean decodeProcess(byte[] bArr) {
        try {
            MediaCodec mediaCodec = this.mAACDecoder;
            if (mediaCodec == null) {
                return false;
            }
            this.mDecodedData = null;
            int dequeueInputBuffer = mediaCodec.dequeueInputBuffer(300);
            if (dequeueInputBuffer >= 0) {
                ByteBuffer inputBuffer = this.mAACDecoder.getInputBuffer(dequeueInputBuffer);
                inputBuffer.clear();
                byte[] bArr2 = bArr;
                inputBuffer.put(bArr2);
                this.mAACDecoder.queueInputBuffer(dequeueInputBuffer, 0, bArr2.length, (this.input_ts * 1000000) / ((long) this.mSampleRate), 0);
            }
            int dequeueOutputBuffer = this.mAACDecoder.dequeueOutputBuffer(this.mBufferInfo, 0);
            int i = this.mBufferInfo.size;
            if (i > 0 && dequeueOutputBuffer >= 0) {
                this.output_ts = (this.mBufferInfo.presentationTimeUs * ((long) this.mSampleRate)) / 1000000;
                ByteBuffer outputBuffer = this.mAACDecoder.getOutputBuffer(dequeueOutputBuffer);
                byte[] bArr3 = new byte[i];
                this.mDecodedData = bArr3;
                outputBuffer.get(bArr3, this.mBufferInfo.offset, this.mBufferInfo.size);
                outputBuffer.clear();
                this.mOutputFormat = this.mAACDecoder.getOutputFormat(dequeueOutputBuffer);
                this.mAACDecoder.releaseOutputBuffer(dequeueOutputBuffer, false);
            }
            return true;
        } catch (Exception e) {
            0HH.LIZ(e);
            BaeLogging.e("MediaCodecAACDecoder", "AAC Decoder Decode failed");
            return false;
        }
    }
}
