package com.ss.android.ttve.mediacodec;

import X.06Z;
import X.0HH;
import X.7Vr;
import X.7cx;
import X.9Cv;
import X.C0347IDi;
import X.C0348IDj;
import X.C0453IHk;
import X.C0454IHl;
import X.C0458IHp;
import X.C0460IHr;
import X.C0462IHt;
import X.C0465IHw;
import X.II0;
import android.graphics.Bitmap;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.opengl.EGL14;
import android.opengl.GLES20;
import android.os.Build;
import android.os.Bundle;
import android.view.Surface;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.vesdk.VEConfigCenter;
import com.ss.android.vesdk.VEVideoEncodeSettings$ENCODE_STANDARD;
import com.ss.android.vesdk.runtime.VERuntime;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import org.json.JSONException;
import org.json.JSONObject;

public class TEAvcEncoder {
    public static boolean DEBUG;
    public static int ENCODE_RESOLUTION_ALIGN = 16;
    public static int MAX_FRAME_RATE = 2000;
    public static long MAX_PRODUCT_OF_SIZE_AND_FPS = -1;
    public static int MIN_FRAME_RATE = 7;
    public static int avcqueuesize = 25;
    public static int file_count;
    public ArrayBlockingQueue<C0465IHw> AVCQueue = new ArrayBlockingQueue<>(avcqueuesize);
    public final int ENCODE_COUNT_DEFAULT = 10;
    public long MINIMUM_KEY_FRAME_PTS_DISTANCE_IN_US = 500000;
    public byte[] configByte = null;
    public int configWaitingFrameCounter = 0;
    public boolean isEnableHwEncoderOpt = false;
    public int mBufferIndex = -1;
    public ByteBuffer mByteBuf = null;
    public boolean mEnableOperatingRate = true;
    public long mEncFrameCount = 0;
    public boolean mEndOfStream = false;
    public boolean mFirstFrame = true;
    public long mFirstFrameTimestamp = Long.MIN_VALUE;
    public long mGOPSize = -1;
    public long mLastEncodeFramePTS = -1;
    public long mLastFramePTS = -1;
    public long mLastKeyFramePTS = 0;
    public long mPicBufferCount = 0;
    public long mPicEncodeCount = 0;
    public long mPicSendCount = 0;
    public long mTotalFrameCount = 0;
    public TEMediaCodecDecoder m_MediaCodecDecInstance = null;
    public Queue<Long> m_PTSQueue = new LinkedList();
    public int m_TransitionFrameCounter = 0;
    public double m_TransitionKeyframeRatio = 1.0d;
    public boolean m_bByteVC110BitHWDecoder = false;
    public int m_bColorRangeFull = 0;
    public boolean m_bEncodeOESTexture = false;
    public boolean m_bEncoderBanExtraDataLoop = false;
    public boolean m_bEncoderGLContextReuse = false;
    public boolean m_bNeedSingalEnd = false;
    public boolean m_bPrintExistBFrame = false;
    public boolean m_bSignalEndOfStream = false;
    public boolean m_bSuccessInit = false;
    public int m_bitRate = 0;
    public MediaFormat m_codecFormat = null;
    public int m_codec_type = 0;
    public int m_colorFormat = 0;
    public int m_colorTrc = 0;
    public int m_colorspace = 0;
    public int m_configStatus = 0;
    public double m_dHpBitrateRatio = 0.75d;
    public C0462IHt m_eglStateSaver;
    public long m_encodeStartTime = -1;
    public int m_frameRate = 0;
    public long m_getnerateIndex = 0;
    public int m_height = 0;
    public int m_height_align = 0;
    public int m_iFrameInternal = 0;
    public boolean m_isNeedReconfigure = false;
    public C0465IHw m_lastCodecData = null;
    public long m_lastDts = 0;
    public int m_level = 0;
    public int m_maxBitRate = 0;
    public MediaCodec m_mediaCodec = null;
    public String m_mime_type = "video/avc";
    public int m_pageMode = -1;
    public int m_profile = 0;
    public C0458IHp m_sharedContext;
    public Surface m_surface = null;
    public C0454IHl m_textureDrawer;
    public C0453IHk m_textureOESDrawer = null;
    public boolean m_useInputSurface = true;
    public int m_width = 0;
    public int m_width_align = 0;
    public byte[] pps;
    public byte[] sps;

    public static boolean saveFrameToFile(int i, int i2, int i3, long j) {
        return true;
    }

    public Surface getInputSurface() {
        return this.m_surface;
    }

    public static TEAvcEncoder createEncoderObject() {
        return new TEAvcEncoder();
    }

    public byte[] getExtraData() {
        byte[] bArr = this.configByte;
        if (bArr != null) {
            return bArr;
        }
        return new byte[0];
    }

    public void setSharedEGLContext() {
        if (this.m_eglStateSaver == null) {
            C0462IHt iHt = new C0462IHt();
            this.m_eglStateSaver = iHt;
            iHt.LIZ();
        }
    }

    static {
        Covode.recordClassIndex(49327);
    }

    public void releaseTextureDrawer() {
        if (this.m_bEncodeOESTexture) {
            C0453IHk iHk = this.m_textureOESDrawer;
            if (iHk != null) {
                iHk.LIZ();
                this.m_textureOESDrawer = null;
                return;
            }
            return;
        }
        C0454IHl iHl = this.m_textureDrawer;
        if (iHl != null) {
            iHl.LIZIZ();
            this.m_textureDrawer = null;
        }
    }

    private int forceToPrepareKeyFrame() {
        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE forceToPrepareKeyFrame");
        this.m_mediaCodec.signalEndOfInputStream();
        int i = 30;
        while (!this.mEndOfStream) {
            int drainOutputBuffer = drainOutputBuffer(10000);
            if (drainOutputBuffer == 0) {
                if (this.mBufferIndex < 0) {
                    i--;
                    if (i <= 0) {
                        break;
                    }
                } else {
                    break;
                }
            } else {
                C0347IDi.LIZLLL("TEAvcEncoder", "forceToPrepareKeyFrame failed ret: ".concat(String.valueOf(drainOutputBuffer)));
                return drainOutputBuffer;
            }
        }
        restartEncoder();
        return 0;
    }

    public ByteBuffer getFrameCacheBuf() {
        if (this.mByteBuf == null) {
            ByteBuffer allocateDirect = ByteBuffer.allocateDirect(this.m_width * this.m_height * 4);
            this.mByteBuf = allocateDirect;
            allocateDirect.order(ByteOrder.nativeOrder());
        }
        this.mByteBuf.position(0);
        return this.mByteBuf;
    }

    public int restartEncoder() {
        C0347IDi.LIZ("TEAvcEncoder", "HW_ENCODE restartEncoder...");
        this.m_bNeedSingalEnd = false;
        stopEncoder();
        int configEncode = configEncode();
        if (configEncode >= 0) {
            return startEncoder();
        }
        if (VEConfigCenter.getInstance().getValue("ve_mediacodec_resource_reasonable_distribute", false).booleanValue()) {
            TEMediaCodecResourceManager.freeCodecBlocksSize(this.m_width_align * this.m_height_align * this.m_frameRate, hashCode());
        }
        return configEncode;
    }

    public void stopEncoder() {
        try {
            this.m_bByteVC110BitHWDecoder = false;
            C0458IHp iHp = this.m_sharedContext;
            if (iHp != null) {
                iHp.LIZIZ();
            }
            C0454IHl iHl = this.m_textureDrawer;
            if (iHl != null) {
                iHl.LIZIZ();
                this.m_textureDrawer = null;
            }
            C0453IHk iHk = this.m_textureOESDrawer;
            if (iHk != null) {
                iHk.LIZ();
                this.m_textureOESDrawer = null;
            }
            MediaCodec mediaCodec = this.m_mediaCodec;
            if (mediaCodec != null) {
                mediaCodec.stop();
            }
        } catch (Exception e) {
            0HH.LIZ(e);
        }
    }

    private int cleanUpMediaCodec() {
        if (this.mEndOfStream) {
            return 0;
        }
        try {
            MediaCodec mediaCodec = this.m_mediaCodec;
            if (mediaCodec != null) {
                mediaCodec.signalEndOfInputStream();
            }
            int i = 30;
            while (!this.mEndOfStream && i > 0) {
                int drainOutputBuffer = drainOutputBuffer(10000);
                if (drainOutputBuffer != 0) {
                    C0347IDi.LIZLLL("TEAvcEncoder", "CleanUpMediaCodec drainOutputBuffer failed ret: ".concat(String.valueOf(drainOutputBuffer)));
                    return drainOutputBuffer;
                }
                i--;
            }
            C0347IDi.LIZIZ("TEAvcEncoder", "CleanUpMediaCodec: cnt= " + i + " mEndOfStream = " + this.mEndOfStream);
            if (!this.mEndOfStream) {
                C0347IDi.LIZLLL("TEAvcEncoder", "CleanUpMediaCodec: Not reaching end of stream but encoder is alive so let's keep it going!");
                return 0;
            } else if (restartEncoder() >= 0) {
                return 0;
            } else {
                C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "CleanupMediaCodec restartEncoder failed", new C0348IDj[0]));
                return -213;
            }
        } catch (Exception e) {
            StringWriter stringWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(stringWriter));
            C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "CleanUpMediaCodec signalEndOfInputStream exception:" + stringWriter.toString(), new C0348IDj[0]));
            return -213;
        }
    }

    public boolean initTextureDrawer() {
        C0453IHk iHk = null;
        if (this.m_bEncodeOESTexture) {
            C0453IHk iHk2 = new C0453IHk();
            iHk2.LJ = iHk2.LIZ(C0453IHk.LIZ);
            iHk2.LJFF = iHk2.LIZ(C0453IHk.LIZIZ);
            iHk2.LIZJ = new C0460IHr();
            if (!iHk2.LIZJ.LIZ("attribute vec4 aPosition;\nattribute vec2 aTextureCoord;\nuniform mat4 uMVPMatrix;\nvarying vec2 vTextureCoord;\nvoid main()\n{\n   gl_Position = uMVPMatrix * aPosition;\n   vTextureCoord = aTextureCoord;\n}", "#extension GL_OES_EGL_image_external : require\nprecision mediump float;\nuniform samplerExternalOES uTextureSampler;\nvarying vec2 vTextureCoord;\nvoid main() \n{\n  gl_FragColor = texture2D(uTextureSampler, vTextureCoord);\n}")) {
                iHk2.LIZJ.LIZ();
                iHk2.LIZJ = null;
                C0347IDi.LIZLLL("TETextureOESDrawer", "TETextureOESDrawer create failed!");
                iHk2.LIZ();
            } else {
                GLES20.glUseProgram(iHk2.LIZJ.LIZ);
                iHk2.LJI = iHk2.LIZJ.LIZ("uMVPMatrix");
                iHk2.LIZLLL = new int[2];
                GLES20.glGenBuffers(2, iHk2.LIZLLL, 0);
                iHk2.LIZJ.LIZ("aPosition", 0);
                GLES20.glBindBuffer(34962, iHk2.LIZLLL[0]);
                iHk2.LJ.position(0);
                GLES20.glBufferData(34962, 32, iHk2.LJ, 35044);
                iHk2.LIZJ.LIZ("aTextureCoord", 1);
                GLES20.glBindBuffer(34962, iHk2.LIZLLL[1]);
                iHk2.LJFF.position(0);
                GLES20.glBufferData(34962, 32, iHk2.LJFF, 35048);
                iHk2.LIZ(0, false, 0, 0, 0, 0);
                C0347IDi.LIZ("TETextureOESDrawer", "init: success.");
                iHk = iHk2;
            }
            this.m_textureOESDrawer = iHk;
            if (iHk == null) {
                return false;
            }
            return true;
        }
        C0454IHl iHl = this.m_textureDrawer;
        if (iHl != null) {
            iHl.LIZIZ();
            this.m_textureDrawer = null;
        }
        C0454IHl LIZ = C0454IHl.LIZ();
        this.m_textureDrawer = LIZ;
        if (LIZ == null) {
            return false;
        }
        LIZ.LIZJ();
        this.m_textureDrawer.LIZ(-1.0f);
        return true;
    }

    public void releaseEncoder() {
        long j;
        boolean z;
        if (this.configByte == null) {
            j = 1;
        } else {
            j = 0;
        }
        9Cv.LIZ(0, "te_record_is_stop_before_extra_data", j);
        StringBuilder sb = new StringBuilder("TE_RECORD_IS_STOP_BEFORE_EXTRA_DATA: ");
        if (this.configByte == null) {
            z = true;
        } else {
            z = false;
        }
        sb.append(z);
        C0347IDi.LIZ("TEAvcEncoder", sb.toString());
        C0347IDi.LIZ("TEAvcEncoder", "HW_ENCODE AVCQueue = " + this.AVCQueue.size() + ", mPicSendCount = " + this.mPicSendCount + ", mPicBufferCount = " + this.mPicBufferCount + ", mPicEncodeCount = " + this.mPicEncodeCount);
        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE releaseEncoder");
        stopEncoder();
        C0347IDi.LIZ("TEAvcEncoder", "releaseEncoder");
        C0458IHp iHp = this.m_sharedContext;
        if (iHp != null) {
            if (!this.m_bEncoderGLContextReuse) {
                iHp.LIZ();
            } else if (!(iHp.LJI == null || iHp.LJII == null)) {
                EGL14.eglDestroySurface(iHp.LJI, iHp.LJII);
            }
            this.m_sharedContext = null;
        }
        if (this.m_surface != null) {
            C0347IDi.LIZIZ("TEAvcEncoder", "release surface");
            this.m_surface.release();
            this.m_surface = null;
        }
        if (this.m_mediaCodec != null) {
            C0347IDi.LIZIZ("TEAvcEncoder", "release mediaCodec");
            this.m_mediaCodec.release();
            this.m_mediaCodec = null;
            if (VEConfigCenter.getInstance().getValue("ve_mediacodec_resource_reasonable_distribute", false).booleanValue()) {
                TEMediaCodecResourceManager.freeCodecBlocksSize(this.m_width_align * this.m_height_align * this.m_frameRate, hashCode());
            }
        }
    }

    private int configEncode() {
        try {
            Surface surface = this.m_surface;
            if (surface != null) {
                surface.release();
                this.m_surface = null;
            }
            MediaCodec mediaCodec = this.m_mediaCodec;
            if (mediaCodec != null) {
                mediaCodec.release();
            }
            if (VEConfigCenter.getInstance().getValue("ve_mediacodec_resource_reasonable_distribute", false).booleanValue()) {
                TEMediaCodecResourceManager.sHWEncoderStateIsBeforeRunning = true;
                if (!TEMediaCodecResourceManager.tryUseCodecBlocksSize(this.m_width_align * this.m_height_align * this.m_frameRate, hashCode())) {
                    C0347IDi.LIZJ("TEAvcEncoder", "ERROR_HW_OVERLOAD, MaxCodecBlocksSize is: " + TEMediaCodecResourceManager.getMaxCodecBlocksSize() + ", UsedCodecBlocksSize is: " + TEMediaCodecResourceManager.getUsedCodecBlocksSize());
                    return -1;
                }
            }
            MediaCodec createEncoderByType = MediaCodec.createEncoderByType(this.m_mime_type);
            this.m_mediaCodec = createEncoderByType;
            MediaCodecInfo codecInfo = createEncoderByType.getCodecInfo();
            String[] supportedTypes = codecInfo.getSupportedTypes();
            C0347IDi.LIZIZ("TEAvcEncoder", "configEncode supports " + Arrays.toString(supportedTypes));
            MediaCodecInfo.CodecCapabilities capabilitiesForType = codecInfo.getCapabilitiesForType(supportedTypes[0]);
            C0347IDi.LIZ("TEAvcEncoder", "configEncode caps " + Arrays.toString(capabilitiesForType.colorFormats));
            if (codecInfo.getName().startsWith("OMX.google.")) {
                C0347IDi.LIZJ("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "TEAvcEncoder mediaCodecInfo Name() startsWith OMX.google.", new C0348IDj[0]));
                return -2;
            } else if (reconfigureMediaFormat(codecInfo) != 0) {
                C0347IDi.LIZJ("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "TEAvcEncoder reconfigureMediaFormat failed", new C0348IDj[0]));
                return -3;
            } else {
                this.m_mediaCodec.configure(this.m_codecFormat, (Surface) null, (MediaCrypto) null, 1);
                if (this.m_useInputSurface) {
                    C0347IDi.LIZLLL("TEAvcEncoder", "m_mediaCodec.createInputSurface()");
                    this.m_surface = this.m_mediaCodec.createInputSurface();
                }
                return 0;
            }
        } catch (Exception e) {
            StringWriter stringWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(stringWriter));
            C0347IDi.LIZJ("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "TEAvcEncoder configEncode Exception" + stringWriter.toString(), new C0348IDj[0]));
            return -4;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:53:0x01cd A[Catch:{ Exception -> 0x01ec }, RETURN] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public int startEncoder() {
        /*
            r25 = this;
            r3 = r25
            java.lang.String r1 = "TEAvcEncoder"
            java.lang.String r0 = "startEncoder..."
            X.C0347IDi.LIZ(r1, r0)
            boolean r0 = r3.m_useInputSurface     // Catch:{ Exception -> 0x01ec }
            r2 = 0
            if (r0 == 0) goto L_0x01dc
            boolean r1 = r3.m_bEncoderGLContextReuse     // Catch:{ Exception -> 0x01ec }
            r8 = 12322(0x3022, float:1.7267E-41)
            r20 = 6
            r19 = 12323(0x3023, float:1.7268E-41)
            r18 = 12324(0x3024, float:1.727E-41)
            r14 = 12352(0x3040, float:1.7309E-41)
            r7 = 0
            r5 = 13
            r0 = 5
            r15 = 10
            r13 = 3
            r17 = 4
            r16 = 12344(0x3038, float:1.7298E-41)
            r10 = 2
            r9 = 1
            if (r1 == 0) goto L_0x00a7
            X.IHt r1 = r3.m_eglStateSaver     // Catch:{ Exception -> 0x01ec }
            if (r1 != 0) goto L_0x0037
            X.IHt r1 = new X.IHt     // Catch:{ Exception -> 0x01ec }
            r1.<init>()     // Catch:{ Exception -> 0x01ec }
            r3.m_eglStateSaver = r1     // Catch:{ Exception -> 0x01ec }
            r1.LIZ()     // Catch:{ Exception -> 0x01ec }
        L_0x0037:
            X.IHp r1 = r3.m_sharedContext     // Catch:{ Exception -> 0x01ec }
            if (r1 != 0) goto L_0x0098
            X.IHt r1 = r3.m_eglStateSaver     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r6 = r1.LIZ     // Catch:{ Exception -> 0x01ec }
            X.IHt r1 = r3.m_eglStateSaver     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r4 = r1.LIZLLL     // Catch:{ Exception -> 0x01ec }
            X.IHp r1 = new X.IHp     // Catch:{ Exception -> 0x01ec }
            r1.<init>()     // Catch:{ Exception -> 0x01ec }
            r1.LJ = r6     // Catch:{ Exception -> 0x01ec }
            r1.LJI = r4     // Catch:{ Exception -> 0x01ec }
            r1.LJII = r7     // Catch:{ Exception -> 0x01ec }
            int[] r6 = new int[r5]     // Catch:{ Exception -> 0x01ec }
            r6[r2] = r14     // Catch:{ Exception -> 0x01ec }
            r6[r9] = r17     // Catch:{ Exception -> 0x01ec }
            r6[r10] = r18     // Catch:{ Exception -> 0x01ec }
            int r4 = X.C0458IHp.LIZ     // Catch:{ Exception -> 0x01ec }
            r6[r13] = r4     // Catch:{ Exception -> 0x01ec }
            r6[r17] = r19     // Catch:{ Exception -> 0x01ec }
            int r4 = X.C0458IHp.LIZIZ     // Catch:{ Exception -> 0x01ec }
            r6[r0] = r4     // Catch:{ Exception -> 0x01ec }
            r6[r20] = r8     // Catch:{ Exception -> 0x01ec }
            int r4 = X.C0458IHp.LIZJ     // Catch:{ Exception -> 0x01ec }
            r0 = 7
            r6[r0] = r4     // Catch:{ Exception -> 0x01ec }
            r4 = 12321(0x3021, float:1.7265E-41)
            r0 = 8
            r6[r0] = r4     // Catch:{ Exception -> 0x01ec }
            r4 = 9
            int r0 = X.C0458IHp.LIZLLL     // Catch:{ Exception -> 0x01ec }
            r6[r4] = r0     // Catch:{ Exception -> 0x01ec }
            r0 = 12339(0x3033, float:1.729E-41)
            r6[r15] = r0     // Catch:{ Exception -> 0x01ec }
            r5 = 12610(0x3142, float:1.767E-41)
            r4 = 11
            r6[r4] = r5     // Catch:{ Exception -> 0x01ec }
            r0 = 12
            r6[r0] = r16     // Catch:{ Exception -> 0x01ec }
            r6[r15] = r5     // Catch:{ Exception -> 0x01ec }
            r6[r4] = r9     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLConfig[] r8 = new android.opengl.EGLConfig[r9]     // Catch:{ Exception -> 0x01ec }
            int[] r11 = new int[r9]     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r5 = r1.LJI     // Catch:{ Exception -> 0x01ec }
            r7 = 0
            r9 = 0
            r10 = 1
            r12 = 0
            android.opengl.EGL14.eglChooseConfig(r5, r6, r7, r8, r9, r10, r11, r12)     // Catch:{ Exception -> 0x01ec }
            r0 = r8[r2]     // Catch:{ Exception -> 0x01ec }
            r1.LJFF = r0     // Catch:{ Exception -> 0x01ec }
            r3.m_sharedContext = r1     // Catch:{ Exception -> 0x01ec }
        L_0x0098:
            X.IHp r1 = r3.m_sharedContext     // Catch:{ Exception -> 0x01ec }
            android.view.Surface r0 = r3.m_surface     // Catch:{ Exception -> 0x01ec }
            r1.LIZ((java.lang.Object) r0)     // Catch:{ Exception -> 0x01ec }
        L_0x009f:
            boolean r0 = r25.initTextureDrawer()     // Catch:{ Exception -> 0x01ec }
            if (r0 != 0) goto L_0x01dc
            goto L_0x01d8
        L_0x00a7:
            X.IHp r1 = r3.m_sharedContext     // Catch:{ Exception -> 0x01ec }
            if (r1 != 0) goto L_0x01cf
            X.IHt r1 = r3.m_eglStateSaver     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r11 = r1.LIZ     // Catch:{ Exception -> 0x01ec }
            android.view.Surface r12 = r3.m_surface     // Catch:{ Exception -> 0x01ec }
            X.IHp r8 = new X.IHp     // Catch:{ Exception -> 0x01ec }
            r8.<init>()     // Catch:{ Exception -> 0x01ec }
            if (r11 != 0) goto L_0x00ba
            android.opengl.EGLContext r11 = android.opengl.EGL14.EGL_NO_CONTEXT     // Catch:{ Exception -> 0x01ec }
        L_0x00ba:
            int[] r7 = new int[r13]     // Catch:{ Exception -> 0x01ec }
            r6 = 12440(0x3098, float:1.7432E-41)
            r7[r2] = r6     // Catch:{ Exception -> 0x01ec }
            r7[r9] = r10     // Catch:{ Exception -> 0x01ec }
            r7[r10] = r16     // Catch:{ Exception -> 0x01ec }
            int[] r5 = new int[r5]     // Catch:{ Exception -> 0x01ec }
            r5[r2] = r14     // Catch:{ Exception -> 0x01ec }
            r5[r9] = r17     // Catch:{ Exception -> 0x01ec }
            r5[r10] = r18     // Catch:{ Exception -> 0x01ec }
            int r1 = X.C0458IHp.LIZ     // Catch:{ Exception -> 0x01ec }
            r5[r13] = r1     // Catch:{ Exception -> 0x01ec }
            r5[r17] = r19     // Catch:{ Exception -> 0x01ec }
            int r1 = X.C0458IHp.LIZIZ     // Catch:{ Exception -> 0x01ec }
            r5[r0] = r1     // Catch:{ Exception -> 0x01ec }
            r1 = 12322(0x3022, float:1.7267E-41)
            r5[r20] = r1     // Catch:{ Exception -> 0x01ec }
            int r4 = X.C0458IHp.LIZJ     // Catch:{ Exception -> 0x01ec }
            r1 = 7
            r5[r1] = r4     // Catch:{ Exception -> 0x01ec }
            r4 = 12321(0x3021, float:1.7265E-41)
            r1 = 8
            r5[r1] = r4     // Catch:{ Exception -> 0x01ec }
            r4 = 9
            int r1 = X.C0458IHp.LIZLLL     // Catch:{ Exception -> 0x01ec }
            r5[r4] = r1     // Catch:{ Exception -> 0x01ec }
            r1 = 12339(0x3033, float:1.729E-41)
            r5[r15] = r1     // Catch:{ Exception -> 0x01ec }
            r14 = 12610(0x3142, float:1.767E-41)
            r4 = 11
            r5[r4] = r14     // Catch:{ Exception -> 0x01ec }
            r1 = 12
            r5[r1] = r16     // Catch:{ Exception -> 0x01ec }
            r5[r15] = r14     // Catch:{ Exception -> 0x01ec }
            r5[r4] = r9     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLConfig[] r4 = new android.opengl.EGLConfig[r9]     // Catch:{ Exception -> 0x01ec }
            int[] r1 = new int[r9]     // Catch:{ Exception -> 0x01ec }
            int[] r14 = new int[r10]     // Catch:{ Exception -> 0x01ec }
            r14[r2] = r2     // Catch:{ Exception -> 0x01ec }
            r14[r9] = r9     // Catch:{ Exception -> 0x01ec }
            int[] r0 = new int[r0]     // Catch:{ Exception -> 0x01ec }
            r15 = 12375(0x3057, float:1.7341E-41)
            r0[r2] = r15     // Catch:{ Exception -> 0x01ec }
            r15 = 64
            r0[r9] = r15     // Catch:{ Exception -> 0x01ec }
            r15 = 12374(0x3056, float:1.734E-41)
            r0[r10] = r15     // Catch:{ Exception -> 0x01ec }
            r15 = 64
            r0[r13] = r15     // Catch:{ Exception -> 0x01ec }
            r0[r17] = r16     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r15 = android.opengl.EGL14.eglGetDisplay(r2)     // Catch:{ Exception -> 0x01ec }
            r8.LJI = r15     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r13 = android.opengl.EGL14.EGL_NO_DISPLAY     // Catch:{ Exception -> 0x01ec }
            if (r15 != r13) goto L_0x012d
            android.opengl.EGL14.eglGetError()     // Catch:{ Exception -> 0x01ec }
        L_0x0128:
            r8.LIZ()     // Catch:{ Exception -> 0x01ec }
            goto L_0x01be
        L_0x012d:
            int[] r15 = new int[r10]     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r13 = android.opengl.EGL14.EGL_NO_CONTEXT     // Catch:{ Exception -> 0x01ec }
            boolean r13 = r11.equals(r13)     // Catch:{ Exception -> 0x01ec }
            if (r13 != 0) goto L_0x0143
            android.opengl.EGLDisplay r13 = r8.LJI     // Catch:{ Exception -> 0x01ec }
            boolean r13 = android.opengl.EGL14.eglQueryContext(r13, r11, r6, r15, r2)     // Catch:{ Exception -> 0x01ec }
            if (r13 == 0) goto L_0x0143
            r13 = r15[r2]     // Catch:{ Exception -> 0x01ec }
            r7[r9] = r13     // Catch:{ Exception -> 0x01ec }
        L_0x0143:
            android.opengl.EGLDisplay r13 = r8.LJI     // Catch:{ Exception -> 0x01ec }
            boolean r13 = android.opengl.EGL14.eglInitialize(r13, r14, r2, r14, r9)     // Catch:{ Exception -> 0x01ec }
            if (r13 != 0) goto L_0x014f
            android.opengl.EGL14.eglGetError()     // Catch:{ Exception -> 0x01ec }
            goto L_0x0128
        L_0x014f:
            android.opengl.EGLDisplay r13 = r8.LJI     // Catch:{ Exception -> 0x01ec }
            r19 = 0
            r21 = 0
            r22 = 1
            r24 = 0
            r17 = r13
            r18 = r5
            r20 = r4
            r23 = r1
            boolean r5 = android.opengl.EGL14.eglChooseConfig(r17, r18, r19, r20, r21, r22, r23, r24)     // Catch:{ Exception -> 0x01ec }
            if (r5 != 0) goto L_0x016b
            android.opengl.EGL14.eglGetError()     // Catch:{ Exception -> 0x01ec }
            goto L_0x0128
        L_0x016b:
            java.lang.String r13 = "Config num: %d, has sharedContext: %s"
            java.lang.Object[] r5 = new java.lang.Object[r10]     // Catch:{ Exception -> 0x01ec }
            r1 = r1[r2]     // Catch:{ Exception -> 0x01ec }
            java.lang.Integer r1 = java.lang.Integer.valueOf(r1)     // Catch:{ Exception -> 0x01ec }
            r5[r2] = r1     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r1 = android.opengl.EGL14.EGL_NO_CONTEXT     // Catch:{ Exception -> 0x01ec }
            if (r11 != r1) goto L_0x019a
            java.lang.String r1 = "NO"
        L_0x017d:
            r5[r9] = r1     // Catch:{ Exception -> 0x01ec }
            X.0HH.LIZ(r13, r5)     // Catch:{ Exception -> 0x01ec }
            r1 = r4[r2]     // Catch:{ Exception -> 0x01ec }
            r8.LJFF = r1     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r4 = r8.LJI     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLConfig r1 = r8.LJFF     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r1 = android.opengl.EGL14.eglCreateContext(r4, r1, r11, r7, r2)     // Catch:{ Exception -> 0x01ec }
            r8.LJ = r1     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r4 = r8.LJ     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r1 = android.opengl.EGL14.EGL_NO_CONTEXT     // Catch:{ Exception -> 0x01ec }
            if (r4 != r1) goto L_0x019d
            android.opengl.EGL14.eglGetError()     // Catch:{ Exception -> 0x01ec }
            goto L_0x0128
        L_0x019a:
            java.lang.String r1 = "YES"
            goto L_0x017d
        L_0x019d:
            r0[r2] = r16     // Catch:{ Exception -> 0x01ec }
            r0[r10] = r16     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r4 = r8.LJI     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLConfig r1 = r8.LJFF     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLSurface r0 = android.opengl.EGL14.eglCreateWindowSurface(r4, r1, r12, r0, r2)     // Catch:{ Exception -> 0x01ec }
            r8.LJII = r0     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLSurface r1 = r8.LJII     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLSurface r0 = android.opengl.EGL14.EGL_NO_SURFACE     // Catch:{ Exception -> 0x01ec }
            if (r1 != r0) goto L_0x01b6
            android.opengl.EGL14.eglGetError()     // Catch:{ Exception -> 0x01ec }
            goto L_0x0128
        L_0x01b6:
            boolean r0 = r8.LIZIZ()     // Catch:{ Exception -> 0x01ec }
            if (r0 != 0) goto L_0x01c0
            goto L_0x0128
        L_0x01be:
            r8 = 0
            goto L_0x01c9
        L_0x01c0:
            int[] r4 = new int[r9]     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLDisplay r1 = r8.LJI     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGLContext r0 = r8.LJ     // Catch:{ Exception -> 0x01ec }
            android.opengl.EGL14.eglQueryContext(r1, r0, r6, r4, r2)     // Catch:{ Exception -> 0x01ec }
        L_0x01c9:
            r3.m_sharedContext = r8     // Catch:{ Exception -> 0x01ec }
            if (r8 != 0) goto L_0x009f
            r0 = -2
            return r0
        L_0x01cf:
            android.view.Surface r0 = r3.m_surface     // Catch:{ Exception -> 0x01ec }
            boolean r0 = r1.LIZ((java.lang.Object) r0)     // Catch:{ Exception -> 0x01ec }
            if (r0 != 0) goto L_0x009f
            goto L_0x01da
        L_0x01d8:
            r0 = -3
            return r0
        L_0x01da:
            r0 = -2
            return r0
        L_0x01dc:
            android.media.MediaCodec r0 = r3.m_mediaCodec     // Catch:{ Exception -> 0x01ec }
            r0.start()     // Catch:{ Exception -> 0x01ec }
            long r0 = java.lang.System.nanoTime()     // Catch:{ Exception -> 0x01ec }
            r3.m_encodeStartTime = r0     // Catch:{ Exception -> 0x01ec }
            r3.m_isNeedReconfigure = r2     // Catch:{ Exception -> 0x01ec }
            r3.mEndOfStream = r2     // Catch:{ Exception -> 0x01ec }
            return r2
        L_0x01ec:
            r0 = move-exception
            X.0HH.LIZ(r0)
            r0 = -1
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ttve.mediacodec.TEAvcEncoder.startEncoder():int");
    }

    private void setByteVC110BitHWDecoderFlag(boolean z) {
        this.m_bByteVC110BitHWDecoder = z;
    }

    public void enableOperatingRate(boolean z) {
        this.mEnableOperatingRate = z;
    }

    private ByteBuffer getInputBufferByIdx(int i) {
        return this.m_mediaCodec.getInputBuffer(i);
    }

    private ByteBuffer getOutputBufferByIdx(int i) {
        return this.m_mediaCodec.getOutputBuffer(i);
    }

    public static void setEncodeResolutionAlign(int i) {
        if (Build.VERSION.SDK_INT >= 24) {
            ENCODE_RESOLUTION_ALIGN = i;
        }
    }

    private void setMediaCodecDecInstance(Object obj) {
        TEMediaCodecDecoder tEMediaCodecDecoder = (TEMediaCodecDecoder) obj;
        this.m_MediaCodecDecInstance = tEMediaCodecDecoder;
        if (tEMediaCodecDecoder == null) {
            C0347IDi.LIZLLL("TEAvcEncoder", "m_MediaCodecDecInstance is null");
        }
    }

    private void setProcessFlag(int i) {
        boolean z = true;
        if ((i & 1) == 0) {
            z = false;
        }
        this.m_bEncodeOESTexture = z;
    }

    public byte[] getCodecData(int i) {
        C0465IHw iHw = this.m_lastCodecData;
        if (iHw != null) {
            return iHw.LIZ;
        }
        return null;
    }

    public static void setEncodeParams(II0 ii0) {
        C0347IDi.LIZ("TEAvcEncoder", "setEncodeParams " + ii0.LIZ);
        MAX_PRODUCT_OF_SIZE_AND_FPS = ii0.LIZ;
    }

    private void setBitRateMode(MediaCodecInfo.CodecCapabilities codecCapabilities) {
        String[] strArr = {"BITRATE_MODE_CQ", "BITRATE_MODE_VBR", "BITRATE_MODE_CBR"};
        MediaCodecInfo.EncoderCapabilities encoderCapabilities = codecCapabilities.getEncoderCapabilities();
        int i = 0;
        do {
            C0347IDi.LIZIZ("TEAvcEncoder", strArr[i] + ": " + encoderCapabilities.isBitrateModeSupported(i));
            i++;
        } while (i < 3);
        this.m_codecFormat.setInteger("bitrate-mode", 1);
    }

    private void setCacMode(MediaCodecInfo mediaCodecInfo) {
        if (Build.VERSION.SDK_INT >= 29) {
            String lowerCase = mediaCodecInfo.getName().toLowerCase();
            if (lowerCase.contains(".qcom.") || lowerCase.contains(".qti.")) {
                int value = VEConfigCenter.getInstance().getValue("ve_content_adaptive_coding_mode", -1);
                C0347IDi.LIZ("TEAvcEncoder", "set cac value = ".concat(String.valueOf(value)));
                if (value >= 0) {
                    this.m_codecFormat.setInteger("vendor.qti-ext-enc-content-adaptive-mode.value", value);
                }
            }
        }
    }

    private int drainOutputBuffer(long j) {
        int i;
        MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
        C0347IDi.LIZIZ("TEAvcEncoder", "drainOutputBuffer before dequeueOutputBuffer");
        this.mBufferIndex = -1;
        try {
            int dequeueOutputBuffer = this.m_mediaCodec.dequeueOutputBuffer(bufferInfo, j);
            this.mBufferIndex = dequeueOutputBuffer;
            if (this.configByte == null && dequeueOutputBuffer == -1) {
                C0347IDi.LIZJ("TEAvcEncoder", "configByte = null and mBufferIndex = MediaCodec.INFO_TRY_AGAIN_LATER, timeoutUs: ".concat(String.valueOf(j)));
            }
            C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE drainOutputBuffer mBufferIndex " + this.mBufferIndex + " buffer-flag= " + bufferInfo.flags);
            while (true) {
                if (this.mBufferIndex < 0) {
                    break;
                }
                int i2 = bufferInfo.size;
                byte[] bArr = new byte[i2];
                try {
                    ByteBuffer outputBufferByIdx = getOutputBufferByIdx(this.mBufferIndex);
                    outputBufferByIdx.position(bufferInfo.offset);
                    outputBufferByIdx.limit(bufferInfo.offset + bufferInfo.size);
                    outputBufferByIdx.get(bArr);
                    if (bufferInfo.flags == 2) {
                        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE drainOutputBuffer BUFFER_FLAG_CODEC_CONFIG");
                        this.configByte = bArr;
                    } else if (bufferInfo.flags == 1) {
                        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE drainOutputBuffer data BUFFER_FLAG_KEY_FRAME");
                        byte[] bArr2 = this.configByte;
                        if (bArr2 == null) {
                            C0347IDi.LIZLLL("TEAvcEncoder", "I can't find configByte!!!! NEED extract from I frame!!!");
                        } else if (bArr[4] == bArr2[4] && (bArr[bArr2.length + 4] & 31) == 5) {
                            int length = i2 - bArr2.length;
                            byte[] bArr3 = new byte[length];
                            System.arraycopy(bArr, bArr2.length, bArr3, 0, length);
                            bArr = bArr3;
                        }
                        addOutputData(bArr, bufferInfo);
                        this.mPicEncodeCount++;
                    } else if (bufferInfo.flags == 4) {
                        C0347IDi.LIZ("TEAvcEncoder", "HW_ENCODE drainOutputBuffer BUFFER_FLAG_END_OF_STREAM");
                        this.mEndOfStream = true;
                        break;
                    } else {
                        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE drainOutputBuffer data");
                        addOutputData(bArr, bufferInfo);
                        this.mPicEncodeCount++;
                    }
                    try {
                        this.m_mediaCodec.releaseOutputBuffer(this.mBufferIndex, false);
                        this.mBufferIndex = this.m_mediaCodec.dequeueOutputBuffer(bufferInfo, j);
                    } catch (Exception e) {
                        StringWriter stringWriter = new StringWriter();
                        e.printStackTrace(new PrintWriter(stringWriter));
                        C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "drainOutputBuffer exception:" + stringWriter.toString(), new C0348IDj[0]));
                        return -214;
                    }
                } catch (Exception e2) {
                    C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "getOutputBufferByIdx exception:" + e2.getMessage(), new C0348IDj[0]));
                    return -214;
                }
            }
            if (this.mBufferIndex == -2) {
                C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE drainOutputBuffer INFO_OUTPUT_FORMAT_CHANGED");
                MediaFormat outputFormat = this.m_mediaCodec.getOutputFormat();
                ByteBuffer byteBuffer = outputFormat.getByteBuffer("csd-0");
                ByteBuffer byteBuffer2 = outputFormat.getByteBuffer("csd-1");
                if (byteBuffer != null) {
                    byte[] bArr4 = (byte[]) byteBuffer.array().clone();
                    this.sps = bArr4;
                    i = bArr4.length + 0;
                } else {
                    i = 0;
                }
                if (byteBuffer2 != null) {
                    byte[] bArr5 = (byte[]) byteBuffer2.array().clone();
                    this.pps = bArr5;
                    i += bArr5.length;
                }
                byte[] bArr6 = new byte[i];
                this.configByte = bArr6;
                if (byteBuffer != null) {
                    byte[] bArr7 = this.sps;
                    System.arraycopy(bArr7, 0, bArr6, 0, bArr7.length);
                }
                if (byteBuffer2 != null) {
                    byte[] bArr8 = this.pps;
                    System.arraycopy(bArr8, 0, this.configByte, this.sps.length, bArr8.length);
                }
            }
            return 0;
        } catch (Exception e3) {
            StringWriter stringWriter2 = new StringWriter();
            e3.printStackTrace(new PrintWriter(stringWriter2));
            C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "drainOutputBuffer exception:" + stringWriter2.toString(), new C0348IDj[0]));
            return -214;
        }
    }

    private int reconfigureMediaFormat(MediaCodecInfo mediaCodecInfo) {
        String[] supportedTypes = mediaCodecInfo.getSupportedTypes();
        C0347IDi.LIZIZ("TEAvcEncoder", "CodecNames:");
        for (String valueOf : supportedTypes) {
            C0347IDi.LIZIZ("TEAvcEncoder", "Codec: ".concat(String.valueOf(valueOf)));
        }
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat(this.m_mime_type, this.m_width_align, this.m_height_align);
        this.m_codecFormat = createVideoFormat;
        createVideoFormat.setInteger("color-format", this.m_colorFormat);
        this.m_codecFormat.setInteger("bitrate", this.m_bitRate);
        int i = this.m_frameRate;
        long j = MAX_PRODUCT_OF_SIZE_AND_FPS;
        if (j > 0) {
            long j2 = j / ((long) (this.m_width_align * this.m_height_align));
            if (((long) i) > j2) {
                i = (int) j2;
            }
        }
        this.m_codecFormat.setInteger("frame-rate", i);
        this.m_codecFormat.setInteger("i-frame-interval", this.m_iFrameInternal);
        this.mGOPSize = (long) (i * this.m_iFrameInternal);
        C0347IDi.LIZJ("TEAvcEncoder", " m_colorspace " + this.m_colorspace + " m_bColorRangeFull " + this.m_bColorRangeFull + " m_colorTrc " + this.m_colorTrc);
        VEConfigCenter.ValuePkt value = VEConfigCenter.getInstance().getValue("ve_color_space_for_2020");
        if ((value != null && value.getValue() != null && (value.getValue() instanceof Boolean) && ((Boolean) value.getValue()).booleanValue()) || !this.m_useInputSurface) {
            this.m_codecFormat.setInteger("color-standard", this.m_colorspace);
            this.m_codecFormat.setInteger("color-range", this.m_bColorRangeFull);
            this.m_codecFormat.setInteger("color-transfer", this.m_colorTrc);
        }
        setProfile(mediaCodecInfo.getCapabilitiesForType(this.m_mime_type));
        setCacMode(mediaCodecInfo);
        setQpParameters(mediaCodecInfo);
        this.mEncFrameCount = 0;
        C0347IDi.LIZ("TEAvcEncoder", 0HH.LIZ("RequestSyncFrame width:[%d] height:[%d] frameRate:[%d] iFrameInternal:[%d] bitRate:[%d] colorFormat:[%d] codec_type:[%d] gop_size: [%d]", new Object[]{Integer.valueOf(this.m_width_align), Integer.valueOf(this.m_height_align), Integer.valueOf(this.m_frameRate), Integer.valueOf(this.m_iFrameInternal), Integer.valueOf(this.m_bitRate), Integer.valueOf(this.m_colorFormat), Integer.valueOf(this.m_codec_type), Long.valueOf(this.mGOPSize)}));
        return 0;
    }

    private void setQpParameters(MediaCodecInfo mediaCodecInfo) {
        boolean z;
        if (this.m_codecFormat != null) {
            String lowerCase = mediaCodecInfo.getName().toLowerCase();
            String value = VEConfigCenter.getInstance().getValue("ve_encoder_qp_range", "");
            String value2 = VEConfigCenter.getInstance().getValue("ve_encoder_initial_qp", "");
            boolean z2 = false;
            if (value == null || value.isEmpty()) {
                z = false;
            } else {
                z = true;
            }
            if (value2 != null && !value2.isEmpty()) {
                z2 = true;
            }
            C0347IDi.LIZIZ("TEAvcEncoder", "setQpParameters codecName:" + lowerCase + ", hasQpRangeJson:" + z + ", hasInitialQpJson:" + z2);
            if (z) {
                try {
                    JSONObject jSONObject = new JSONObject(value);
                    if (lowerCase.contains(".qcom.") || lowerCase.contains(".qti.")) {
                        setQpRange(jSONObject.getJSONObject("qcom"), "qcom");
                    } else if (lowerCase.contains(".exynos.") || lowerCase.contains(".sec.")) {
                        setQpRange(jSONObject.getJSONObject("exynos"), "exynos");
                    } else if (lowerCase.contains(".mtk.")) {
                        setQpRange(jSONObject.getJSONObject("mtk"), "mtk");
                    } else {
                        setQpRange(jSONObject.getJSONObject("others"), "others");
                    }
                } catch (JSONException e) {
                    0HH.LIZ(e);
                    C0347IDi.LIZLLL("TEAvcEncoder", "qp range json str parse error : " + e.getLocalizedMessage());
                }
            }
            if (z2) {
                try {
                    JSONObject jSONObject2 = new JSONObject(value2);
                    if (lowerCase.contains(".qcom.") || lowerCase.contains(".qti.")) {
                        JSONObject jSONObject3 = jSONObject2.getJSONObject("qcom");
                        int i = jSONObject3.getInt("i");
                        int i2 = jSONObject3.getInt("p");
                        int i3 = jSONObject3.getInt("b");
                        C0347IDi.LIZIZ("TEAvcEncoder", "parse initial QP, I:" + i + " P:" + i2 + " B:" + i3);
                        if (i > 0 && i <= 51) {
                            this.m_codecFormat.setInteger("vendor.qti-ext-enc-initial-qp.qp-i-enable", 1);
                            this.m_codecFormat.setInteger("vendor.qti-ext-enc-initial-qp.qp-i", i);
                        }
                        if (i2 > 0 && i2 <= 51) {
                            this.m_codecFormat.setInteger("vendor.qti-ext-enc-initial-qp.qp-p-enable", 1);
                            this.m_codecFormat.setInteger("vendor.qti-ext-enc-initial-qp.qp-p", i2);
                        }
                        if (i3 > 0 && i3 <= 51) {
                            this.m_codecFormat.setInteger("vendor.qti-ext-enc-initial-qp.qp-b-enable", 1);
                            this.m_codecFormat.setInteger("vendor.qti-ext-enc-initial-qp.qp-b", i3);
                        }
                    }
                } catch (JSONException e2) {
                    0HH.LIZ(e2);
                    C0347IDi.LIZLLL("TEAvcEncoder", "initial qp json str parse error : " + e2.getLocalizedMessage());
                }
            }
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:107:0x00e8 A[SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x0162 A[LOOP:1: B:21:0x00a1->B:58:0x0162, LOOP_END] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void setProfile(android.media.MediaCodecInfo.CodecCapabilities r25) {
        /*
            r24 = this;
            r13 = r24
            int r1 = android.os.Build.VERSION.SDK_INT
            java.lang.String r12 = "TEAvcEncoder"
            r0 = 24
            if (r1 >= r0) goto L_0x0010
            java.lang.String r0 = "setProfile return due to version"
            X.C0347IDi.LIZJ(r12, r0)
            return
        L_0x0010:
            boolean r0 = r13.m_useInputSurface
            r14 = 4
            java.lang.String r5 = "operating-rate"
            r11 = 2
            r3 = 0
            r1 = 1
            r4 = r25
            if (r0 != 0) goto L_0x0199
            com.ss.android.vesdk.VEConfigCenter r2 = com.ss.android.vesdk.VEConfigCenter.getInstance()
            java.lang.String r0 = "ve_enable_encode_buffer_operating_rate"
            java.lang.Boolean r0 = r2.getValue((java.lang.String) r0, (boolean) r1)
            boolean r0 = r0.booleanValue()
            if (r0 == 0) goto L_0x0040
            java.lang.String r2 = java.lang.String.valueOf(r0)
            java.lang.String r0 = "isEnableOperatingRateC: "
            java.lang.String r0 = r0.concat(r2)
            X.C0347IDi.LIZ(r12, r0)
            android.media.MediaFormat r2 = r13.m_codecFormat
            r0 = 240(0xf0, float:3.36E-43)
            r2.setInteger(r5, r0)
        L_0x0040:
            android.media.MediaFormat r2 = r13.m_codecFormat
            java.lang.String r0 = "bitrate-mode"
            r2.setInteger(r0, r1)
        L_0x0047:
            int r8 = r13.m_profile
            int r7 = r13.m_codec_type
            r11 = 0
            android.media.MediaCodecInfo$CodecProfileLevel[] r6 = r4.profileLevels
            int r5 = r6.length
            r4 = 0
        L_0x0050:
            if (r4 >= r5) goto L_0x0059
            r2 = r6[r4]
            int r0 = r2.profile
            if (r0 != r8) goto L_0x0184
            r11 = r2
        L_0x0059:
            java.lang.String r1 = r13.m_mime_type
            java.lang.String r0 = "video/avc"
            boolean r0 = r1.equals(r0)
            java.lang.String r21 = "，m_bitRate = "
            java.lang.String r15 = "high"
            java.lang.String r20 = "main"
            java.lang.String r19 = "bitrate"
            java.lang.String r10 = "level"
            java.lang.String r2 = "profile"
            java.lang.String r18 = "te_composition_video_hw_profile"
            if (r0 == 0) goto L_0x0237
            if (r11 == 0) goto L_0x0237
            android.media.MediaFormat r1 = r13.m_codecFormat
            int r0 = r11.profile
            r1.setInteger(r2, r0)
            int r4 = r11.level
            com.ss.android.vesdk.VEConfigCenter r1 = com.ss.android.vesdk.VEConfigCenter.getInstance()
            java.lang.String r0 = "ve_enable_adaptive_encode_level"
            java.lang.Boolean r0 = r1.getValue((java.lang.String) r0, (boolean) r3)
            boolean r0 = r0.booleanValue()
            if (r0 == 0) goto L_0x00ea
            int r3 = r13.m_width_align
            int r1 = r13.m_height_align
            int r2 = r13.m_frameRate
            int r0 = r13.m_bitRate
            r23 = r0
            long r7 = (long) r3
            long r0 = (long) r1
            long r7 = r7 * r0
            long r0 = (long) r2
            long r7 = r7 * r0
            X.IHx[] r14 = X.C0467IHy.LIZ
            int r0 = r14.length
            r22 = r0
            r9 = 0
        L_0x00a1:
            r0 = r22
            if (r9 >= r0) goto L_0x0180
            r6 = r14[r9]
            int r1 = r6.LIZ
            int r0 = r11.level
            if (r1 >= r0) goto L_0x0180
            r1 = 256(0x100, double:1.265E-321)
            int r0 = r6.LIZIZ
            long r4 = (long) r0
            long r4 = r4 * r1
            int r1 = r11.profile
            r16 = 1000(0x3e8, double:4.94E-321)
            r0 = 1
            if (r1 == r0) goto L_0x0179
            r0 = 2
            if (r1 == r0) goto L_0x0179
            r0 = 4
            if (r1 == r0) goto L_0x0179
            r0 = 8
            if (r1 == r0) goto L_0x016c
            r0 = 16
            if (r1 == r0) goto L_0x0167
            r0 = 32
            if (r1 == r0) goto L_0x0171
            r0 = 64
            if (r1 == r0) goto L_0x0171
            r0 = 65536(0x10000, float:9.18355E-41)
            if (r1 == r0) goto L_0x0179
            r0 = 524288(0x80000, float:7.34684E-40)
            if (r1 == r0) goto L_0x016c
            int r0 = r6.LIZJ
            long r2 = (long) r0
            long r2 = r2 * r16
        L_0x00dd:
            int r0 = (r7 > r4 ? 1 : (r7 == r4 ? 0 : -1))
            if (r0 > 0) goto L_0x0162
            r0 = r23
            long r0 = (long) r0
            int r4 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r4 > 0) goto L_0x0162
            int r4 = r6.LIZ
        L_0x00ea:
            android.media.MediaFormat r0 = r13.m_codecFormat
            r0.setInteger(r10, r4)
            int r1 = r11.profile
            r0 = 2
            if (r1 == r0) goto L_0x014c
            r0 = 8
            if (r1 == r0) goto L_0x0138
            java.lang.String r1 = "baseline"
            r0 = r18
            X.C0188I7f.LIZ((java.lang.String) r0, (java.lang.String) r1)
        L_0x00ff:
            android.media.MediaFormat r2 = r13.m_codecFormat
            int r1 = r13.m_bitRate
            r0 = r19
            r2.setInteger(r0, r1)
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "Set Profile for AVC: "
            r1.<init>(r0)
            int r0 = r11.profile
            r1.append(r0)
            java.lang.String r0 = ", Level = "
            r1.append(r0)
            r1.append(r4)
            java.lang.String r0 = ", maxLevel = "
            r1.append(r0)
            int r0 = r11.level
            r1.append(r0)
            r0 = r21
            r1.append(r0)
            int r0 = r13.m_bitRate
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZ(r12, r0)
            return
        L_0x0138:
            java.lang.String r0 = "Set High Profile"
            X.C0347IDi.LIZIZ(r12, r0)
            int r0 = r13.m_maxBitRate
            double r2 = (double) r0
            double r0 = r13.m_dHpBitrateRatio
            double r2 = r2 * r0
            int r0 = (int) r2
            r13.m_bitRate = r0
            r0 = r18
            X.C0188I7f.LIZ((java.lang.String) r0, (java.lang.String) r15)
            goto L_0x00ff
        L_0x014c:
            java.lang.String r0 = "Set Main Profile"
            X.C0347IDi.LIZIZ(r12, r0)
            int r0 = r13.m_maxBitRate
            double r1 = (double) r0
            double r5 = r13.m_dHpBitrateRatio
            double r1 = r1 * r5
            int r0 = (int) r1
            r13.m_bitRate = r0
            r1 = r18
            r0 = r20
            X.C0188I7f.LIZ((java.lang.String) r1, (java.lang.String) r0)
            goto L_0x00ff
        L_0x0162:
            int r9 = r9 + 1
            r0 = 2
            goto L_0x00a1
        L_0x0167:
            r2 = 3000(0xbb8, double:1.482E-320)
            int r0 = r6.LIZJ
            goto L_0x0175
        L_0x016c:
            r2 = 1250(0x4e2, double:6.176E-321)
            int r0 = r6.LIZJ
            goto L_0x0175
        L_0x0171:
            r2 = 4000(0xfa0, double:1.9763E-320)
            int r0 = r6.LIZJ
        L_0x0175:
            long r0 = (long) r0
            long r2 = r2 * r0
            goto L_0x00dd
        L_0x0179:
            int r0 = r6.LIZJ
            long r2 = (long) r0
            long r2 = r2 * r16
            goto L_0x00dd
        L_0x0180:
            int r4 = r11.level
            goto L_0x00ea
        L_0x0184:
            if (r11 == 0) goto L_0x018c
            int r1 = r11.profile
            int r0 = r2.profile
            if (r1 >= r0) goto L_0x0195
        L_0x018c:
            com.ss.android.vesdk.VEVideoEncodeSettings$ENCODE_STANDARD r0 = com.ss.android.vesdk.VEVideoEncodeSettings$ENCODE_STANDARD.ENCODE_STANDARD_ByteVC1
            int r0 = r0.ordinal()
            if (r7 == r0) goto L_0x0195
            r11 = r2
        L_0x0195:
            int r4 = r4 + 1
            goto L_0x0050
        L_0x0199:
            boolean r0 = r13.mEnableOperatingRate
            if (r0 == 0) goto L_0x0047
            android.media.MediaCodecInfo$VideoCapabilities r6 = r4.getVideoCapabilities()
            r10 = 2147483647(0x7fffffff, float:NaN)
            int r2 = r13.m_width_align     // Catch:{ IllegalArgumentException -> 0x01b7 }
            int r0 = r13.m_height_align     // Catch:{ IllegalArgumentException -> 0x01b7 }
            android.util.Range r0 = r6.getSupportedFrameRatesFor(r2, r0)     // Catch:{ IllegalArgumentException -> 0x01b7 }
            java.lang.Comparable r0 = r0.getUpper()     // Catch:{ IllegalArgumentException -> 0x01b7 }
            java.lang.Double r0 = (java.lang.Double) r0     // Catch:{ IllegalArgumentException -> 0x01b7 }
            int r10 = r0.intValue()     // Catch:{ IllegalArgumentException -> 0x01b7 }
            goto L_0x01bc
        L_0x01b7:
            java.lang.String r0 = "getSupportedFrameRate unsupported size"
            X.C0347IDi.LIZJ(r12, r0)
        L_0x01bc:
            com.ss.android.vesdk.VEConfigCenter r2 = com.ss.android.vesdk.VEConfigCenter.getInstance()
            java.lang.String r0 = "ve_enable_encode_operating_rate"
            int r8 = r2.getValue((java.lang.String) r0, (int) r3)
            com.ss.android.vesdk.VEConfigCenter r2 = com.ss.android.vesdk.VEConfigCenter.getInstance()
            java.lang.String r0 = "ve_crop_encode_operating_rate"
            int r9 = r2.getValue((java.lang.String) r0, (int) r3)
            if (r9 <= 0) goto L_0x01d7
            int r0 = r13.m_pageMode
            if (r0 != r1) goto L_0x01d7
            r8 = r9
        L_0x01d7:
            int r0 = r10 / 120
            int r0 = r0 * 120
            int r2 = java.lang.Math.min(r0, r8)
            java.util.Locale r7 = java.util.Locale.getDefault()
            r0 = 7
            java.lang.Object[] r6 = new java.lang.Object[r0]
            int r0 = r13.m_width_align
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)
            r6[r3] = r0
            int r0 = r13.m_height_align
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)
            r6[r1] = r0
            java.lang.Integer r0 = java.lang.Integer.valueOf(r10)
            r6[r11] = r0
            r1 = 3
            java.lang.Integer r0 = java.lang.Integer.valueOf(r8)
            r6[r1] = r0
            java.lang.Integer r0 = java.lang.Integer.valueOf(r9)
            r6[r14] = r0
            r1 = 5
            java.lang.Integer r0 = java.lang.Integer.valueOf(r2)
            r6[r1] = r0
            r1 = 6
            int r0 = r13.m_pageMode
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)
            r6[r1] = r0
            java.lang.String r0 = "device support max_fps for Res(%d * %d) is %d, libra val = %d, crop libra val = %d, final val = %d pageMode val = %d"
            java.lang.String r0 = X.0HH.LIZ(r7, r0, r6)
            X.C0347IDi.LIZ(r12, r0)
            if (r2 == 0) goto L_0x0047
            int r1 = r13.m_width_align
            int r0 = r13.m_height_align
            int r1 = java.lang.Math.max(r1, r0)
            r0 = 1920(0x780, float:2.69E-42)
            if (r1 > r0) goto L_0x0047
            android.media.MediaFormat r0 = r13.m_codecFormat
            r0.setInteger(r5, r2)
            goto L_0x0047
        L_0x0237:
            java.lang.String r1 = r13.m_mime_type
            java.lang.String r0 = "video/mp4v-es"
            boolean r0 = r1.equals(r0)
            if (r0 == 0) goto L_0x0259
            android.media.MediaFormat r0 = r13.m_codecFormat
            r1 = 1
            r0.setInteger(r2, r1)
            android.media.MediaFormat r0 = r13.m_codecFormat
            r0.setInteger(r10, r1)
            android.media.MediaFormat r2 = r13.m_codecFormat
            int r1 = r13.m_bitRate
            r0 = 2
            int r1 = r1 * 2
            r0 = r19
            r2.setInteger(r0, r1)
            return
        L_0x0259:
            java.lang.String r1 = r13.m_mime_type
            java.lang.String r0 = "video/hevc"
            boolean r0 = r1.equals(r0)
            if (r0 == 0) goto L_0x02bb
            if (r11 == 0) goto L_0x02bb
            android.media.MediaFormat r1 = r13.m_codecFormat
            int r0 = r11.profile
            r1.setInteger(r2, r0)
            android.media.MediaFormat r1 = r13.m_codecFormat
            int r0 = r11.level
            r1.setInteger(r10, r0)
            int r0 = r13.m_maxBitRate
            double r1 = (double) r0
            double r3 = r13.m_dHpBitrateRatio
            double r1 = r1 * r3
            int r0 = (int) r1
            r13.m_bitRate = r0
            int r1 = r11.profile
            r0 = 89478485(0x5555555, float:1.0030885E-35)
            r1 = r1 & r0
            if (r1 == 0) goto L_0x02bc
            r1 = r18
            r0 = r20
            X.C0188I7f.LIZ((java.lang.String) r1, (java.lang.String) r0)
        L_0x028b:
            android.media.MediaFormat r2 = r13.m_codecFormat
            int r1 = r13.m_bitRate
            r0 = r19
            r2.setInteger(r0, r1)
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "Set Profile for H E V C: "
            r1.<init>(r0)
            int r0 = r11.profile
            r1.append(r0)
            java.lang.String r0 = ", level = "
            r1.append(r0)
            int r0 = r11.level
            r1.append(r0)
            r0 = r21
            r1.append(r0)
            int r0 = r13.m_bitRate
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.C0347IDi.LIZ(r12, r0)
        L_0x02bb:
            return
        L_0x02bc:
            r0 = r18
            X.C0188I7f.LIZ((java.lang.String) r0, (java.lang.String) r15)
            goto L_0x028b
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ttve.mediacodec.TEAvcEncoder.setProfile(android.media.MediaCodecInfo$CodecCapabilities):void");
    }

    public int getInfoByFlag(long[] jArr, int i) {
        if (i != 1) {
            return -1;
        }
        jArr[0] = this.m_lastCodecData.LIZIZ;
        jArr[1] = this.m_lastCodecData.LIZJ;
        return 4;
    }

    private void addOutputData(byte[] bArr, MediaCodec.BufferInfo bufferInfo) {
        long j;
        C0347IDi.LIZIZ("TEAvcEncoder", "encode: pts queue size = " + this.m_PTSQueue.size() + " avc sdata size= " + this.AVCQueue.size());
        if (this.m_PTSQueue.size() <= 0 && this.m_profile >= 8) {
            C0347IDi.LIZJ("TEAvcEncoder", "encode: no available pts!!! profile " + this.m_profile);
        } else if (this.m_PTSQueue.size() <= 0) {
            C0347IDi.LIZJ("TEAvcEncoder", "encode: no available pts!!!");
        } else {
            C0465IHw iHw = new C0465IHw();
            long j2 = 0;
            if (bufferInfo.presentationTimeUs > 0) {
                j2 = bufferInfo.presentationTimeUs;
            }
            if (VEConfigCenter.getInstance().getValue("ve_enable_opt_hwencode_dts", false).booleanValue()) {
                long longValue = this.m_PTSQueue.poll().longValue();
                if (j2 == longValue) {
                    j = j2;
                } else {
                    j = (long) Math.ceil((double) (((float) (this.m_lastDts + Math.min(longValue, j2))) / 2.0f));
                    if (!this.m_bPrintExistBFrame) {
                        C0347IDi.LIZ("TEAvcEncoder", "exist B frame, pts = " + j2 + ", dts = " + j);
                        this.m_bPrintExistBFrame = true;
                    }
                }
                this.m_lastDts = j;
            } else {
                long longValue2 = this.m_PTSQueue.poll().longValue();
                if (this.m_profile >= 8) {
                    j = longValue2 - 200000;
                } else {
                    j = j2;
                }
            }
            C0347IDi.LIZIZ("TEAvcEncoder", "dts = " + j + ", pts = " + j2 + " add codecdata-encode AVCQueue-size= " + this.AVCQueue.size());
            iHw.LIZ = bArr;
            iHw.LIZIZ = j2;
            iHw.LIZJ = j;
            iHw.LIZLLL = bufferInfo.flags;
            try {
                this.AVCQueue.add(iHw);
            } catch (Exception e) {
                0HH.LIZ(e);
            }
        }
    }

    /*  JADX ERROR: JadxRuntimeException in pass: CodeShrinkVisitor
        jadx.core.utils.exceptions.JadxRuntimeException: Don't wrap MOVE or CONST insns: 0x0006: MOVE  (r14v0 java.lang.String) = (r30v0 java.lang.String)
        	at jadx.core.dex.instructions.args.InsnArg.wrapArg(InsnArg.java:164)
        	at jadx.core.dex.visitors.shrink.CodeShrinkVisitor.assignInline(CodeShrinkVisitor.java:133)
        	at jadx.core.dex.visitors.shrink.CodeShrinkVisitor.checkInline(CodeShrinkVisitor.java:118)
        	at jadx.core.dex.visitors.shrink.CodeShrinkVisitor.shrinkBlock(CodeShrinkVisitor.java:65)
        	at jadx.core.dex.visitors.shrink.CodeShrinkVisitor.shrinkMethod(CodeShrinkVisitor.java:43)
        	at jadx.core.dex.visitors.shrink.CodeShrinkVisitor.visit(CodeShrinkVisitor.java:35)
        */
    /* JADX WARNING: Removed duplicated region for block: B:130:? A[RETURN, SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:18:0x00cc A[ADDED_TO_REGION] */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x00ec A[ADDED_TO_REGION] */
    private void setQpRange(org.json.JSONObject r29, java.lang.String r30) {
        /*
            r28 = this;
            r2 = r28
            r1 = r29
            if (r1 == 0) goto L_0x000a
            r14 = r30
            if (r14 != 0) goto L_0x000b
        L_0x000a:
            return
        L_0x000b:
            java.lang.String r0 = "i_min"
            int r13 = r1.getInt(r0)
            java.lang.String r0 = "i_max"
            int r12 = r1.getInt(r0)
            java.lang.String r0 = "p_min"
            int r10 = r1.getInt(r0)
            java.lang.String r0 = "p_max"
            int r6 = r1.getInt(r0)
            java.lang.String r0 = "b_min"
            int r3 = r1.getInt(r0)
            java.lang.String r0 = "b_max"
            int r1 = r1.getInt(r0)
            int r4 = android.os.Build.VERSION.SDK_INT
            java.lang.StringBuilder r7 = new java.lang.StringBuilder
            r7.<init>()
            r7.append(r14)
            java.lang.String r0 = " "
            r7.append(r0)
            r7.append(r4)
            java.lang.String r0 = " platform parse QP range, I:"
            r7.append(r0)
            r7.append(r13)
            java.lang.String r5 = "-"
            r7.append(r5)
            r7.append(r12)
            java.lang.String r0 = " P:"
            r7.append(r0)
            r7.append(r10)
            r7.append(r5)
            r7.append(r6)
            java.lang.String r0 = " B:"
            r7.append(r0)
            r7.append(r3)
            r7.append(r5)
            r7.append(r1)
            java.lang.String r5 = r7.toString()
            java.lang.String r0 = "TEAvcEncoder"
            X.C0347IDi.LIZIZ(r0, r5)
            java.lang.String r27 = "vendor.sec-ext-enc-qp-range.B-maxQP"
            java.lang.String r26 = "vendor.qti-ext-enc-qp-range.qp-b-max"
            java.lang.String r25 = "vendor.sec-ext-enc-qp-range.P-maxQP"
            java.lang.String r24 = "vendor.sec-ext-enc-qp-range.B-minQP"
            java.lang.String r23 = "vendor.qti-ext-enc-qp-range.qp-p-max"
            java.lang.String r22 = "vendor.qti-ext-enc-qp-range.qp-b-min"
            java.lang.String r21 = "vendor.sec-ext-enc-qp-range.I-maxQP"
            java.lang.String r20 = "vendor.sec-ext-enc-qp-range.P-minQP"
            java.lang.String r11 = "vendor.qti-ext-enc-qp-range.qp-i-max"
            java.lang.String r9 = "vendor.qti-ext-enc-qp-range.qp-p-min"
            java.lang.String r19 = "vendor.sec-ext-enc-qp-range.I-minQP"
            java.lang.String r8 = "vendor.qti-ext-enc-qp-range.qp-i-min"
            java.lang.String r7 = "exynos"
            java.lang.String r5 = "qcom"
            r0 = 31
            if (r4 < r0) goto L_0x01cc
            boolean r18 = r14.equals(r5)
            java.lang.String r5 = "video-qp-p-max"
            java.lang.String r17 = "video-qp-b-max"
            java.lang.String r16 = "video-qp-b-min"
            java.lang.String r15 = "video-qp-i-max"
            java.lang.String r4 = "video-qp-p-min"
            java.lang.String r0 = "video-qp-i-min"
            if (r18 == 0) goto L_0x0114
            if (r13 > r12) goto L_0x0111
            if (r13 <= 0) goto L_0x00ba
            r7 = 51
            if (r13 > r7) goto L_0x00ba
            android.media.MediaFormat r7 = r2.m_codecFormat
            r7.setInteger(r0, r13)
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r8, r13)
        L_0x00ba:
            if (r12 <= 0) goto L_0x0111
            r7 = 51
            if (r12 > r7) goto L_0x00ca
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r15, r12)
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r11, r12)
        L_0x00ca:
            if (r10 > r6) goto L_0x00ea
            if (r10 <= 0) goto L_0x00da
            if (r10 > r7) goto L_0x00da
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r4, r10)
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r9, r10)
        L_0x00da:
            if (r6 <= 0) goto L_0x00ea
            if (r6 > r7) goto L_0x00ea
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r5, r6)
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r23
            r4.setInteger(r0, r6)
        L_0x00ea:
            if (r3 > r1) goto L_0x000a
            if (r3 <= 0) goto L_0x00fe
            if (r3 > r7) goto L_0x00fe
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r16
            r4.setInteger(r0, r3)
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r22
            r4.setInteger(r0, r3)
        L_0x00fe:
            if (r1 <= 0) goto L_0x000a
            if (r1 > r7) goto L_0x000a
            android.media.MediaFormat r3 = r2.m_codecFormat
            r0 = r17
            r3.setInteger(r0, r1)
            android.media.MediaFormat r2 = r2.m_codecFormat
            r0 = r26
            r2.setInteger(r0, r1)
            return
        L_0x0111:
            r7 = 51
            goto L_0x00ca
        L_0x0114:
            boolean r7 = r14.equals(r7)
            if (r7 == 0) goto L_0x0187
            r7 = 51
            if (r13 > r12) goto L_0x013e
            if (r13 < 0) goto L_0x012e
            if (r13 > r7) goto L_0x012e
            android.media.MediaFormat r8 = r2.m_codecFormat
            r8.setInteger(r0, r13)
            android.media.MediaFormat r8 = r2.m_codecFormat
            r0 = r19
            r8.setInteger(r0, r13)
        L_0x012e:
            if (r12 < 0) goto L_0x013e
            if (r12 > r7) goto L_0x013e
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r15, r12)
            android.media.MediaFormat r8 = r2.m_codecFormat
            r0 = r21
            r8.setInteger(r0, r12)
        L_0x013e:
            if (r10 > r6) goto L_0x0160
            if (r10 < 0) goto L_0x0150
            if (r10 > r7) goto L_0x0150
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r4, r10)
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r20
            r4.setInteger(r0, r10)
        L_0x0150:
            if (r6 < 0) goto L_0x0160
            if (r6 > r7) goto L_0x0160
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r5, r6)
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r25
            r4.setInteger(r0, r6)
        L_0x0160:
            if (r3 > r1) goto L_0x000a
            if (r3 < 0) goto L_0x0174
            if (r3 > r7) goto L_0x0174
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r16
            r4.setInteger(r0, r3)
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r24
            r4.setInteger(r0, r3)
        L_0x0174:
            if (r1 < 0) goto L_0x000a
            if (r1 > r7) goto L_0x000a
            android.media.MediaFormat r3 = r2.m_codecFormat
            r0 = r17
            r3.setInteger(r0, r1)
            android.media.MediaFormat r2 = r2.m_codecFormat
            r0 = r27
            r2.setInteger(r0, r1)
            return
        L_0x0187:
            r5 = 51
            if (r13 > r12) goto L_0x019d
            if (r13 < 0) goto L_0x0194
            if (r13 > r5) goto L_0x0194
            android.media.MediaFormat r7 = r2.m_codecFormat
            r7.setInteger(r0, r13)
        L_0x0194:
            if (r12 < 0) goto L_0x019d
            if (r12 > r5) goto L_0x019d
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r15, r12)
        L_0x019d:
            if (r10 > r6) goto L_0x01b3
            if (r10 < 0) goto L_0x01a8
            if (r10 > r5) goto L_0x01a8
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r4, r10)
        L_0x01a8:
            if (r6 < 0) goto L_0x01b3
            if (r6 > r5) goto L_0x01b3
            android.media.MediaFormat r4 = r2.m_codecFormat
            java.lang.String r0 = "video-qp-p-man"
            r4.setInteger(r0, r6)
        L_0x01b3:
            if (r3 > r1) goto L_0x000a
            if (r3 < 0) goto L_0x01c0
            if (r3 > r5) goto L_0x01c0
            android.media.MediaFormat r4 = r2.m_codecFormat
            r0 = r16
            r4.setInteger(r0, r3)
        L_0x01c0:
            if (r1 < 0) goto L_0x000a
            if (r1 > r5) goto L_0x000a
            android.media.MediaFormat r2 = r2.m_codecFormat
            r0 = r17
            r2.setInteger(r0, r1)
            return
        L_0x01cc:
            r4 = 51
            boolean r0 = r14.equals(r5)
            if (r0 == 0) goto L_0x0217
            if (r13 > r12) goto L_0x01e8
            if (r13 <= 0) goto L_0x01df
            if (r13 > r4) goto L_0x01df
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r8, r13)
        L_0x01df:
            if (r12 <= 0) goto L_0x01e8
            if (r12 > r4) goto L_0x01e8
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r11, r12)
        L_0x01e8:
            if (r10 > r6) goto L_0x01fe
            if (r10 <= 0) goto L_0x01f3
            if (r10 > r4) goto L_0x01f3
            android.media.MediaFormat r0 = r2.m_codecFormat
            r0.setInteger(r9, r10)
        L_0x01f3:
            if (r6 <= 0) goto L_0x01fe
            if (r6 > r4) goto L_0x01fe
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r23
            r5.setInteger(r0, r6)
        L_0x01fe:
            if (r3 > r1) goto L_0x000a
            if (r3 <= 0) goto L_0x020b
            if (r3 > r4) goto L_0x020b
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r22
            r5.setInteger(r0, r3)
        L_0x020b:
            if (r1 <= 0) goto L_0x000a
            if (r1 > r4) goto L_0x000a
            android.media.MediaFormat r2 = r2.m_codecFormat
            r0 = r26
            r2.setInteger(r0, r1)
            return
        L_0x0217:
            boolean r0 = r14.equals(r7)
            if (r0 == 0) goto L_0x000a
            if (r13 > r12) goto L_0x0235
            if (r13 < 0) goto L_0x022a
            if (r13 > r4) goto L_0x022a
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r19
            r5.setInteger(r0, r13)
        L_0x022a:
            if (r12 < 0) goto L_0x0235
            if (r12 > r4) goto L_0x0235
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r21
            r5.setInteger(r0, r12)
        L_0x0235:
            if (r10 > r6) goto L_0x024d
            if (r10 < 0) goto L_0x0242
            if (r10 > r4) goto L_0x0242
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r20
            r5.setInteger(r0, r10)
        L_0x0242:
            if (r6 < 0) goto L_0x024d
            if (r6 > r4) goto L_0x024d
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r25
            r5.setInteger(r0, r6)
        L_0x024d:
            if (r3 > r1) goto L_0x000a
            if (r3 < 0) goto L_0x025a
            if (r3 > r4) goto L_0x025a
            android.media.MediaFormat r5 = r2.m_codecFormat
            r0 = r24
            r5.setInteger(r0, r3)
        L_0x025a:
            if (r1 < 0) goto L_0x000a
            if (r1 > r4) goto L_0x000a
            android.media.MediaFormat r2 = r2.m_codecFormat
            r0 = r27
            r2.setInteger(r0, r1)
            goto L_0x000a
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ttve.mediacodec.TEAvcEncoder.setQpRange(org.json.JSONObject, java.lang.String):void");
    }

    public static Bitmap convertTexToBitmap(int i, int i2, int i3) {
        MethodCollector.i(12691);
        ByteBuffer readTextureToByteBuffer = readTextureToByteBuffer(i, i2, i3);
        Bitmap createBitmap = Bitmap.createBitmap(i2, i3, Bitmap.Config.ARGB_8888);
        createBitmap.copyPixelsFromBuffer(readTextureToByteBuffer);
        MethodCollector.o(12691);
        return createBitmap;
    }

    public static ByteBuffer readTextureToByteBuffer(int i, int i2, int i3) {
        int[] iArr = new int[1];
        int[] iArr2 = new int[1];
        GLES20.glGetIntegerv(36006, iArr2, 0);
        GLES20.glGenFramebuffers(1, iArr, 0);
        GLES20.glBindFramebuffer(36160, iArr[0]);
        GLES20.glFramebufferTexture2D(36160, 36064, 3553, i, 0);
        int i4 = i2;
        int i5 = i3;
        ByteBuffer allocate = ByteBuffer.allocate(i4 * i5 * 4);
        GLES20.glReadPixels(0, 0, i4, i5, 6408, 5121, allocate);
        GLES20.glFinish();
        GLES20.glBindFramebuffer(36160, iArr2[0]);
        GLES20.glDeleteFramebuffers(1, iArr, 0);
        return allocate;
    }

    public int encodeYUV420(byte[] bArr, int i, int i2, long j) {
        int i3 = i2;
        long j2 = j;
        if (this.mLastEncodeFramePTS == j2) {
            C0347IDi.LIZIZ("TEAvcEncoder", "inputBuffer repeat");
            return 0;
        }
        try {
            long j3 = 0;
            int dequeueInputBuffer = this.m_mediaCodec.dequeueInputBuffer(0);
            int i4 = 30;
            while (true) {
                if (dequeueInputBuffer < 0) {
                    if (i4 <= 0) {
                        break;
                    }
                    try {
                        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE no inputBuffer maxCount = ".concat(String.valueOf(i4)));
                        int drainOutputBuffer = drainOutputBuffer(0);
                        if (drainOutputBuffer != 0) {
                            return drainOutputBuffer;
                        }
                        dequeueInputBuffer = this.m_mediaCodec.dequeueInputBuffer(10000);
                        i4--;
                    } catch (Exception e) {
                        e = e;
                        0HH.LIZ(e);
                        return 0;
                    }
                } else {
                    ByteBuffer inputBufferByIdx = getInputBufferByIdx(dequeueInputBuffer);
                    inputBufferByIdx.clear();
                    C0347IDi.LIZIZ("TEAvcEncoder", "inputBuffer " + inputBufferByIdx + ", inputBufferIndex = " + dequeueInputBuffer);
                    if (i3 > inputBufferByIdx.capacity()) {
                        i3 = inputBufferByIdx.capacity();
                    }
                    inputBufferByIdx.put(bArr, i, i3);
                    this.m_mediaCodec.queueInputBuffer(dequeueInputBuffer, 0, i3, j2, 0);
                    this.mLastEncodeFramePTS = j2;
                    this.mPicBufferCount++;
                }
            }
            if (this.AVCQueue.size() <= 0) {
                j3 = 10000;
            }
            int drainOutputBuffer2 = drainOutputBuffer(j3);
            if (drainOutputBuffer2 != 0) {
                return drainOutputBuffer2;
            }
            return drainOutputBuffer2;
        } catch (Exception e2) {
            e = e2;
            0HH.LIZ(e);
            return 0;
        }
    }

    public int encodeVideoFromBuffer(int i, long j, boolean z, boolean z2) {
        int i2 = 0;
        if (!this.m_bSuccessInit) {
            return 0;
        }
        if (this.m_mediaCodec == null) {
            return -211;
        }
        StringBuilder sb = new StringBuilder("HW_ENCODE encodeVideoFromBuffer start isSignalEndStream = ");
        boolean z3 = z;
        sb.append(z3);
        sb.append(", mEndOfStream = ");
        sb.append(this.mEndOfStream);
        sb.append(", size= ");
        int i3 = i;
        sb.append(i3);
        C0347IDi.LIZIZ("TEAvcEncoder", sb.toString());
        long j2 = j;
        this.m_PTSQueue.offer(Long.valueOf(j2));
        if (this.m_isNeedReconfigure || (this.m_configStatus & 4) != 0) {
            if (this.m_mediaCodec == null || this.m_configStatus != 1) {
                restartEncoder();
            } else {
                Bundle bundle = new Bundle();
                bundle.putInt("video-bitrate", this.m_bitRate);
                this.m_mediaCodec.setParameters(bundle);
                this.m_configStatus = 0;
            }
            this.m_isNeedReconfigure = false;
        }
        if (z2 && (VERuntime.getInstance().isTransitionKeyFrameEnable() || VERuntime.getInstance().getTransitionKeyFrameVersion() >= 0)) {
            Bundle bundle2 = new Bundle();
            bundle2.putInt("request-sync", 0);
            bundle2.putInt("video-bitrate", (int) (((double) this.m_bitRate) * this.m_TransitionKeyframeRatio));
            this.m_mediaCodec.setParameters(bundle2);
        }
        if (this.m_encodeStartTime == -1) {
            this.m_encodeStartTime = System.nanoTime();
        }
        int drainOutputBuffer = drainOutputBuffer(0);
        if (drainOutputBuffer != 0) {
            return drainOutputBuffer;
        }
        if (!z3) {
            C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE encodeYUV420");
            int encodeYUV420 = encodeYUV420(this.mByteBuf.array(), this.mByteBuf.arrayOffset(), i3, j2);
            if (encodeYUV420 != 0) {
                return encodeYUV420;
            }
        } else {
            int i4 = 30;
            while (!this.mEndOfStream) {
                C0347IDi.LIZ("TEAvcEncoder", "HW_ENCODE signal flush() from buffer");
                try {
                    this.m_mediaCodec.queueInputBuffer(this.m_mediaCodec.dequeueInputBuffer(10000), 0, 0, 0, 4);
                    int drainOutputBuffer2 = drainOutputBuffer(10000);
                    if (drainOutputBuffer2 == 0) {
                        if (this.mBufferIndex < 0) {
                            i4--;
                            if (i4 <= 0) {
                                break;
                            }
                        } else {
                            break;
                        }
                    } else {
                        return drainOutputBuffer2;
                    }
                } catch (Exception e) {
                    0HH.LIZ(e);
                    return -213;
                }
            }
        }
        C0465IHw poll = this.AVCQueue.poll();
        this.m_lastCodecData = poll;
        if (poll != null) {
            i2 = poll.LIZ.length;
            this.mPicSendCount++;
        }
        C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE AVCQueue = " + this.AVCQueue.size() + ", mPicSendCount = " + this.mPicSendCount + ", mPicBufferCount = " + this.mPicBufferCount + ", mPicEncodeCount = " + this.mPicEncodeCount);
        return i2;
    }

    /* JADX INFO: finally extract failed */
    public int encodeVideoFromTexture(int i, long j, boolean z, boolean z2, boolean z3) {
        boolean z4;
        int i2;
        int i3;
        boolean z5;
        TEMediaCodecDecoder tEMediaCodecDecoder;
        MethodCollector.i(14335);
        if (!this.m_bSuccessInit) {
            MethodCollector.o(14335);
            return 0;
        }
        long j2 = j;
        if (j2 != this.mLastFramePTS) {
            long j3 = this.mGOPSize;
            if (j3 <= 0) {
                C0347IDi.LIZLLL("TEAvcEncoder", "RequestSyncFrame: GOPSize incorrect! GOPSize=" + this.mGOPSize);
            } else if (this.mEncFrameCount % j3 == 0) {
                this.mLastKeyFramePTS = j2;
                C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame: KeyFrame from PresetGOP size=" + this.mGOPSize + " ClipFrameNo.=" + this.mEncFrameCount + " TotalFrameCount=" + this.mTotalFrameCount + " lastKeyFramePTS=" + this.mLastKeyFramePTS);
            }
            this.mEncFrameCount++;
            this.mTotalFrameCount++;
            this.mLastFramePTS = j2;
        }
        int i4 = this.m_TransitionFrameCounter;
        if (i4 > 0) {
            int i5 = i4 - 1;
            this.m_TransitionFrameCounter = i5;
            if (i5 == 0) {
                Bundle bundle = new Bundle();
                bundle.putInt("video-bitrate", this.m_bitRate);
                C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame: Reset video_bitrate back to normal as " + this.m_bitRate + " PTS =" + j2 + " ClipFrameNo.=" + this.mEncFrameCount + " TotalFrameCount=" + this.mTotalFrameCount);
                this.m_mediaCodec.setParameters(bundle);
            }
        }
        if (7Vr.LJI == 7cx.VIDEO_ENC_ENCODING_FALLBACK.getValue()) {
            C0347IDi.LIZLLL("TEAvcEncoder", "TESTING! HW VIDEO ENC ENCODING FALLBACK");
            MethodCollector.o(14335);
            return -211;
        }
        if (Long.MIN_VALUE == this.mFirstFrameTimestamp) {
            this.mFirstFrameTimestamp = System.currentTimeMillis();
        }
        this.m_PTSQueue.offer(Long.valueOf(j2));
        if (this.m_eglStateSaver == null) {
            C0462IHt iHt = new C0462IHt();
            this.m_eglStateSaver = iHt;
            iHt.LIZ();
        }
        if (this.m_isNeedReconfigure && this.isEnableHwEncoderOpt) {
            startEncoder();
        }
        if (this.m_isNeedReconfigure || (this.m_configStatus & 4) != 0) {
            if (this.m_mediaCodec != null && this.m_configStatus == 1) {
                Bundle bundle2 = new Bundle();
                bundle2.putInt("video-bitrate", this.m_bitRate);
                this.m_mediaCodec.setParameters(bundle2);
                this.m_configStatus = 0;
            } else if (!this.m_bEncoderBanExtraDataLoop) {
                restartEncoder();
            }
            this.m_isNeedReconfigure = false;
        }
        try {
            int drainOutputBuffer = drainOutputBuffer(0);
            if (drainOutputBuffer != 0) {
                MethodCollector.o(14335);
                return drainOutputBuffer;
            }
            VEConfigCenter.ValuePkt value = VEConfigCenter.getInstance().getValue("ve_smart_trans_detect");
            if (value == null || value.getValue() == null) {
                z4 = false;
            } else {
                z4 = ((Boolean) value.getValue()).booleanValue();
            }
            boolean z6 = z2;
            if (z6 && (VERuntime.getInstance().isTransitionKeyFrameEnable() || z4 || VERuntime.getInstance().getTransitionKeyFrameVersion() >= 0)) {
                C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame: TransitionKeyFrame Enabled");
                long currentTimeMillis = System.currentTimeMillis();
                Bundle bundle3 = new Bundle();
                int i6 = (int) (((double) this.m_bitRate) * this.m_TransitionKeyframeRatio);
                bundle3.putInt("video-bitrate", i6);
                C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame: Boost video_bitrate as " + i6 + " mbitRate=" + this.m_bitRate + " transitionRatio=" + this.m_TransitionKeyframeRatio + " PTS =" + j2 + " FrameNo.=" + this.mEncFrameCount + " TotalFrameCount=" + this.mTotalFrameCount);
                this.mEncFrameCount = 0;
                int transitionKeyFrameMode = VERuntime.getInstance().getTransitionKeyFrameMode();
                if (transitionKeyFrameMode != 1) {
                    if (transitionKeyFrameMode != 2) {
                        C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame_MODE0");
                        bundle3.putInt("request-sync", 0);
                    } else {
                        int transitionFrameCount = VERuntime.getInstance().getTransitionFrameCount();
                        this.m_TransitionFrameCounter = transitionFrameCount;
                        if (transitionFrameCount <= 0) {
                            this.m_TransitionFrameCounter = 1;
                        }
                        C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame_MODE2: set transition frame count = " + this.m_TransitionFrameCounter);
                    }
                } else if (j2 - this.mLastKeyFramePTS < this.MINIMUM_KEY_FRAME_PTS_DISTANCE_IN_US) {
                    C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame: KeyFrames too close. Skipped! CurrFramePTS = " + j2 + " LastKeyFramePTS=" + this.mLastKeyFramePTS + " CurrFrameNo.=" + this.mTotalFrameCount);
                } else {
                    this.mLastKeyFramePTS = j2;
                    C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame_MODE1");
                    if (cleanUpMediaCodec() == 0) {
                        C0347IDi.LIZIZ("TEAvcEncoder", " RequestSyncFrame Success");
                        bundle3.putInt("request-sync", 0);
                    } else {
                        C0347IDi.LIZLLL("TEAvcEncoder", "RequestSyncFrame Fail");
                    }
                }
                this.m_mediaCodec.setParameters(bundle3);
                C0347IDi.LIZIZ("TEAvcEncoder", "RequestSyncFrame:: pts = " + j2 + " isKeyFrame= " + z6 + " bp_ratio " + this.m_TransitionKeyframeRatio + "  SyncOnceTime=" + (System.currentTimeMillis() - currentTimeMillis));
            }
            if (z3) {
                C0347IDi.LIZ("TEAvcEncoder", "isForceIFrame pts = " + j2 + " keyframe isKeyFrame= " + z6 + " bp_ratio " + this.m_TransitionKeyframeRatio);
                forceToPrepareKeyFrame();
            }
            if (this.m_encodeStartTime == -1) {
                this.m_encodeStartTime = System.nanoTime();
            }
            int i7 = i & -1;
            if (i7 != 0) {
                if (!(this.m_textureDrawer == null && this.m_textureOESDrawer == null)) {
                    if (!7Vr.LIZ() || !EGL14.eglGetCurrentContext().equals(this.m_sharedContext.LJ)) {
                        this.m_sharedContext.LIZIZ();
                    }
                    GLES20.glViewport(0, 0, this.m_width, this.m_height);
                    if (this.m_bEncodeOESTexture) {
                        this.m_textureOESDrawer.LIZ(this.m_MediaCodecDecInstance.getRenderParam().LIZ);
                        this.m_textureOESDrawer.LIZ(-this.m_MediaCodecDecInstance.getRenderParam().LIZIZ, true, this.m_MediaCodecDecInstance.getRenderParam().LIZJ, this.m_MediaCodecDecInstance.getRenderParam().LIZLLL, this.m_width_align, this.m_height_align);
                        this.m_textureOESDrawer.LIZ(i7);
                    } else {
                        this.m_textureDrawer.LIZ(i7);
                    }
                    if (this.mFirstFrame) {
                        if (DEBUG) {
                            ByteBuffer allocateDirect = ByteBuffer.allocateDirect(3686400);
                            allocateDirect.order(ByteOrder.nativeOrder());
                            allocateDirect.position(0);
                            GLES20.glReadPixels(0, 0, 1280, 720, 6408, 5121, allocateDirect);
                        }
                        this.mFirstFrame = false;
                    }
                    long j4 = 1000 * j2;
                    this.m_sharedContext.LIZ(j4);
                    GLES20.glFinish();
                    this.m_sharedContext.LIZJ();
                    this.m_bNeedSingalEnd = true;
                    this.mPicBufferCount++;
                    if (!(this.configByte == null || (tEMediaCodecDecoder = this.m_MediaCodecDecInstance) == null)) {
                        tEMediaCodecDecoder.signalReEncodeOptCV();
                    }
                    int drainOutputBuffer2 = drainOutputBuffer(0);
                    if (drainOutputBuffer2 != 0) {
                        MethodCollector.o(14335);
                        return drainOutputBuffer2;
                    }
                    boolean z7 = this.isEnableHwEncoderOpt;
                    if (z7 && this.configByte == null) {
                        int i8 = this.configWaitingFrameCounter + 1;
                        this.configWaitingFrameCounter = i8;
                        if (i8 >= 25) {
                            this.configWaitingFrameCounter = 0;
                            C0347IDi.LIZLLL("TEAvcEncoder", "get config fail, back to soft encode");
                            MethodCollector.o(14335);
                            return -211;
                        }
                    }
                    try {
                        if (this.configByte == null && !this.m_bEncoderBanExtraDataLoop && !z7) {
                            int i9 = 0;
                            do {
                                C0347IDi.LIZIZ("TEAvcEncoder", "Encoder first frame, count = ".concat(String.valueOf(i9)));
                                GLES20.glViewport(0, 0, this.m_width, this.m_height);
                                if (this.m_bEncodeOESTexture) {
                                    this.m_textureOESDrawer.LIZ(this.m_MediaCodecDecInstance.getRenderParam().LIZ);
                                    C0453IHk iHk = this.m_textureOESDrawer;
                                    int i10 = -this.m_MediaCodecDecInstance.getRenderParam().LIZIZ;
                                    C0453IHk iHk2 = iHk;
                                    iHk2.LIZ(i10, true, this.m_MediaCodecDecInstance.getRenderParam().LIZJ, this.m_MediaCodecDecInstance.getRenderParam().LIZLLL, this.m_width_align, this.m_height_align);
                                    this.m_textureOESDrawer.LIZ(i7);
                                } else {
                                    this.m_textureDrawer.LIZ(i7);
                                }
                                this.m_sharedContext.LIZ(j4);
                                GLES20.glFinish();
                                this.m_sharedContext.LIZJ();
                                int drainOutputBuffer3 = drainOutputBuffer(0);
                                if (drainOutputBuffer3 == 0) {
                                    i9++;
                                    if (i9 > 30) {
                                        break;
                                    }
                                    Thread.sleep(10, 0);
                                } else {
                                    MethodCollector.o(14335);
                                    return drainOutputBuffer3;
                                }
                            } while (this.configByte == null);
                            if (this.configByte == null) {
                                C0348IDj[] iDjArr = new C0348IDj[7];
                                iDjArr[0] = new C0348IDj("AVCQueue count", Integer.valueOf(this.AVCQueue.size()), "");
                                iDjArr[1] = new C0348IDj("textureID", Integer.valueOf(i7), "");
                                iDjArr[2] = new C0348IDj("isTexture", Boolean.valueOf(GLES20.glIsTexture(i)), "");
                                iDjArr[3] = new C0348IDj("timestampUs", Long.valueOf(j2), "");
                                iDjArr[4] = new C0348IDj("glCheckError", Integer.valueOf(GLES20.glGetError()), "");
                                iDjArr[5] = new C0348IDj("m_bEncoderGLContextReuse", Boolean.valueOf(this.m_bEncoderGLContextReuse), "");
                                if (this.m_sharedContext.LJ != null) {
                                    z5 = true;
                                } else {
                                    z5 = false;
                                }
                                iDjArr[6] = new C0348IDj("currentContext", Boolean.valueOf(z5), "");
                                C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "TEAvcEncoder Generate configData failed!!!", iDjArr));
                                MethodCollector.o(14335);
                                return -211;
                            }
                            C0347IDi.LIZ("TEAvcEncoder", "Generate configData succeed!!!".concat(String.valueOf(i9)));
                            this.AVCQueue.clear();
                            restartEncoder();
                            this.m_getnerateIndex = 0;
                            this.m_sharedContext.LIZIZ();
                            GLES20.glViewport(0, 0, this.m_width, this.m_height);
                            if (this.m_bEncodeOESTexture) {
                                this.m_textureOESDrawer.LIZ(this.m_MediaCodecDecInstance.getRenderParam().LIZ);
                                C0453IHk iHk3 = this.m_textureOESDrawer;
                                int i11 = -this.m_MediaCodecDecInstance.getRenderParam().LIZIZ;
                                int i12 = this.m_MediaCodecDecInstance.getRenderParam().LIZJ;
                                C0453IHk iHk4 = iHk3;
                                int i13 = i11;
                                iHk4.LIZ(i13, true, i12, this.m_MediaCodecDecInstance.getRenderParam().LIZLLL, this.m_width_align, this.m_height_align);
                                this.m_textureOESDrawer.LIZ(i7);
                            } else {
                                this.m_textureDrawer.LIZ(i7);
                            }
                            this.m_sharedContext.LIZ(j4);
                            GLES20.glFinish();
                            this.m_sharedContext.LIZJ();
                            TEMediaCodecDecoder tEMediaCodecDecoder2 = this.m_MediaCodecDecInstance;
                            if (tEMediaCodecDecoder2 != null) {
                                tEMediaCodecDecoder2.signalReEncodeOptCV();
                            }
                            long j5 = this.mFirstFrameTimestamp;
                            if (j5 > 0 && j5 != Long.MAX_VALUE) {
                                long currentTimeMillis2 = System.currentTimeMillis() - this.mFirstFrameTimestamp;
                                this.mFirstFrameTimestamp = Long.MAX_VALUE;
                                9Cv.LIZ(0, "te_record_generate_extra_data_cost", currentTimeMillis2);
                                C0347IDi.LIZ("TEAvcEncoder", "TE_RECORD_GEN_EXTRA_DATA_COST: ".concat(String.valueOf(currentTimeMillis2)));
                            }
                        }
                        if (VEConfigCenter.getInstance().getValue("ve_mediacodec_resource_reasonable_distribute", true).booleanValue() && this.m_getnerateIndex == 0) {
                            TEMediaCodecResourceManager.sHWEncoderStateIsBeforeRunning = false;
                        }
                    } catch (Exception e) {
                        0HH.LIZ(e);
                        MethodCollector.o(14335);
                        return -211;
                    } catch (Throwable th) {
                        MethodCollector.o(14335);
                        throw th;
                    }
                }
                this.m_getnerateIndex++;
            } else if (this.m_mediaCodec != null && !this.m_bSignalEndOfStream && this.m_bNeedSingalEnd) {
                try {
                    C0347IDi.LIZ("TEAvcEncoder", "HW_ENCODE signal flush() from texture");
                    this.m_bSignalEndOfStream = true;
                    this.mEndOfStream = false;
                    this.m_mediaCodec.signalEndOfInputStream();
                } catch (Exception e2) {
                    0HH.LIZ(e2);
                    MethodCollector.o(14335);
                    return -213;
                }
            }
            if (!7Vr.LIZ()) {
                C0462IHt iHt2 = this.m_eglStateSaver;
                EGL14.eglMakeCurrent(iHt2.LIZLLL, iHt2.LIZIZ, iHt2.LIZJ, iHt2.LIZ);
            }
            if (this.m_bSignalEndOfStream) {
                int i14 = 30;
                while (!this.mEndOfStream) {
                    int drainOutputBuffer4 = drainOutputBuffer(10000);
                    if (drainOutputBuffer4 == 0) {
                        if (this.mBufferIndex < 0) {
                            i14--;
                            if (i14 <= 0) {
                                break;
                            }
                        } else {
                            break;
                        }
                    } else {
                        MethodCollector.o(14335);
                        return drainOutputBuffer4;
                    }
                }
            } else {
                VEConfigCenter.ValuePkt value2 = VEConfigCenter.getInstance().getValue("byteVC1_10bit_hardware_encoder_timeout");
                if (value2 == null || value2.getValue() == null || !this.m_bByteVC110BitHWDecoder || (i3 = ((Integer) value2.getValue()).intValue()) <= 0) {
                    i3 = 0;
                }
                int drainOutputBuffer5 = drainOutputBuffer((long) i3);
                if (drainOutputBuffer5 != 0) {
                    MethodCollector.o(14335);
                    return drainOutputBuffer5;
                } else if (this.m_bEncoderBanExtraDataLoop || this.isEnableHwEncoderOpt) {
                    if (this.configByte == null) {
                        C0347IDi.LIZIZ("TEAvcEncoder", "Skip extra data looper.");
                        MethodCollector.o(14335);
                        return 0;
                    }
                    long j6 = this.mFirstFrameTimestamp;
                    if (j6 > 0 && j6 != Long.MAX_VALUE) {
                        long currentTimeMillis3 = System.currentTimeMillis() - this.mFirstFrameTimestamp;
                        this.mFirstFrameTimestamp = Long.MAX_VALUE;
                        9Cv.LIZ(0, "te_record_generate_extra_data_cost", currentTimeMillis3);
                        C0347IDi.LIZ("TEAvcEncoder", "TE_RECORD_GEN_EXTRA_DATA_COST: ".concat(String.valueOf(currentTimeMillis3)));
                        int size = this.AVCQueue.size();
                        9Cv.LIZ(0, "te_record_packet_count_before_extra_data", (long) size);
                        C0347IDi.LIZ("TEAvcEncoder", "TE_RECORD_PACKET_COUNT_BEFORE_EXTRA_DATA: ".concat(String.valueOf(size)));
                    }
                }
            }
            C0465IHw poll = this.AVCQueue.poll();
            this.m_lastCodecData = poll;
            if (poll != null) {
                i2 = poll.LIZ.length;
                this.mPicSendCount++;
            } else {
                i2 = 0;
            }
            C0347IDi.LIZIZ("TEAvcEncoder", "HW_ENCODE AVCQueue = " + this.AVCQueue.size() + ", mPicSendCount = " + this.mPicSendCount + ", mPicBufferCount = " + this.mPicBufferCount + ", mPicEncodeCount = " + this.mPicEncodeCount);
            MethodCollector.o(14335);
            return i2;
        } catch (Exception e3) {
            C0347IDi.LIZLLL("TEAvcEncoder", C0347IDi.LIZ("TEAvcEncoder", "drainOutputBuffer exception:" + e3.getMessage(), new C0348IDj[0]));
            MethodCollector.o(14335);
            return -214;
        }
    }

    private void setEncoder(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, double d, int i9, int i10, int i11) {
        this.m_configStatus = 0;
        this.m_codec_type = i;
        if (i == VEVideoEncodeSettings$ENCODE_STANDARD.ENCODE_STANDARD_ByteVC1.ordinal()) {
            this.m_mime_type = "video/hevc";
        } else if (i == VEVideoEncodeSettings$ENCODE_STANDARD.ENCODE_STANDARD_MPEG4.ordinal()) {
            this.m_mime_type = "video/mp4v-es";
        } else {
            this.m_mime_type = "video/avc";
        }
        if (i2 > 0) {
            this.m_width = i2;
            this.m_width_align = i2;
        }
        if (i3 > 0) {
            this.m_height = i3;
            this.m_height_align = i3;
        }
        Math.min(this.m_width_align, this.m_height_align);
        if (this.m_useInputSurface) {
            int i12 = this.m_width_align;
            int i13 = ENCODE_RESOLUTION_ALIGN;
            if (i12 % i13 != 0) {
                this.m_width_align = ((i12 / i13) + 1) * i13;
            }
            int i14 = this.m_height_align;
            if (i14 % i13 != 0) {
                this.m_height_align = ((i14 / i13) + 1) * i13;
            }
            C0347IDi.LIZJ("TEAvcEncoder", "Encoder set OutResolution align: " + ENCODE_RESOLUTION_ALIGN + ", dstResolution: " + this.m_width_align + "*" + this.m_height_align + ", srcResolution: " + this.m_width + "*" + this.m_height);
        }
        if (i4 > 0) {
            if (VEConfigCenter.getInstance().getValue("ve_enable_limit_min_encode_fps", true).booleanValue() && i4 < MIN_FRAME_RATE) {
                C0347IDi.LIZJ("TEAvcEncoder", 0HH.LIZ(Locale.getDefault(), "_frameRate:[%d] is too small, change to %d", new Object[]{Integer.valueOf(i4), Integer.valueOf(MIN_FRAME_RATE)}));
                i4 = MIN_FRAME_RATE;
            }
            if (i4 > MAX_FRAME_RATE) {
                C0347IDi.LIZJ("TEAvcEncoder", 0HH.LIZ(Locale.getDefault(), "_frameRate:[%d] is too large, change to %d", new Object[]{Integer.valueOf(i4), Integer.valueOf(MAX_FRAME_RATE)}));
                i4 = MAX_FRAME_RATE;
            }
            if (this.m_frameRate != i4) {
                this.m_frameRate = i4;
                if (i4 < this.m_iFrameInternal) {
                    this.m_iFrameInternal = i4;
                }
                this.m_isNeedReconfigure = true;
                this.m_configStatus |= 2;
            }
        }
        if (i5 > 0) {
            this.m_maxBitRate = i5;
            if (this.m_bitRate != i5) {
                this.m_bitRate = i5;
                this.m_isNeedReconfigure = true;
                this.m_configStatus |= 1;
            }
        }
        if (i6 >= 0) {
            this.m_iFrameInternal = i6;
        }
        if (i7 > 0) {
            this.m_colorFormat = i7;
        }
        if (this.m_codec_type == VEVideoEncodeSettings$ENCODE_STANDARD.ENCODE_STANDARD_ByteVC1.ordinal()) {
            this.m_profile = 1;
        } else {
            this.m_profile = 06Z.LIZ(i8, 1, 64);
        }
        this.m_dHpBitrateRatio = d;
        C0347IDi.LIZ("TEAvcEncoder", "setEncoder, m_mime_type = " + this.m_mime_type + ", bitRate = " + this.m_bitRate + ", maxBitRate = " + this.m_maxBitRate + ", m_dHpBitrateRatio = " + this.m_dHpBitrateRatio);
        this.m_colorspace = i9;
        this.m_bColorRangeFull = i10;
        this.m_colorTrc = i11;
    }

    public int initEncoder(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, double d, boolean z, int i9, int i10, int i11, double d2, int i12, boolean z2, boolean z3, int i13) {
        TEAvcEncoder tEAvcEncoder;
        if (i12 > 0) {
            setEncodeResolutionAlign(i12);
        }
        double d3 = d2;
        C0347IDi.LIZ("TEAvcEncoder", "m_mediaCodec initEncoder == enter transitionRatio ".concat(String.valueOf(d3)));
        this.m_pageMode = i13;
        this.isEnableHwEncoderOpt = VEConfigCenter.getInstance().getValue("ve_enable_compile_hw_encoder_opt", false).booleanValue();
        C0347IDi.LIZ("TEAvcEncoder", "isEnableHwEncoderOpt: " + this.isEnableHwEncoderOpt);
        boolean z4 = z;
        this.m_useInputSurface = z4;
        if (z4) {
            this.m_colorFormat = 2130708361;
        } else {
            this.m_colorFormat = i5;
        }
        int i14 = i2;
        int i15 = i7;
        setEncoder(i, i14, i3, i4, i15, i6, this.m_colorFormat, i8, d, i9, i10, i11);
        if (d3 <= 0.0d || d3 >= 5.0d) {
            tEAvcEncoder = this;
        } else {
            tEAvcEncoder = this;
            tEAvcEncoder.m_TransitionKeyframeRatio = d3;
        }
        tEAvcEncoder.m_isNeedReconfigure = true;
        tEAvcEncoder.m_bSuccessInit = true;
        tEAvcEncoder.m_bSignalEndOfStream = false;
        tEAvcEncoder.m_bEncoderGLContextReuse = z2;
        tEAvcEncoder.m_bEncoderBanExtraDataLoop = z3;
        C0347IDi.LIZ("TEAvcEncoder", "avcencoder glcontext reuse: " + tEAvcEncoder.m_bEncoderGLContextReuse);
        int configEncode = configEncode();
        if (configEncode < 0) {
            if (VEConfigCenter.getInstance().getValue("ve_mediacodec_resource_reasonable_distribute", false).booleanValue()) {
                TEMediaCodecResourceManager.freeCodecBlocksSize(tEAvcEncoder.m_width_align * tEAvcEncoder.m_height_align * tEAvcEncoder.m_frameRate, hashCode());
            }
        } else if (configEncode == 0 && tEAvcEncoder.m_bEncoderBanExtraDataLoop && (configEncode = startEncoder()) != 0) {
            C0347IDi.LIZLLL("TEAvcEncoder", "Start encoder failed on initEncoder: ".concat(String.valueOf(configEncode)));
        }
        if (7Vr.LJI == 7cx.VIDEO_ENC_INIT_FALLBACK.getValue()) {
            C0347IDi.LIZLLL("TEAvcEncoder", "TESTING! HW VIDEO ENC INIT FALLBACK");
            return -1;
        }
        file_count = 0;
        return configEncode;
    }
}
