package com.tencent.mm.plugin.mmsight.model.b;

import android.graphics.Point;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaCodecInfo;
import android.media.MediaCodecInfo.CodecCapabilities;
import android.media.MediaCodecList;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ttpic.baseutils.ApiHelper$VERSION_CODES;
import java.nio.ByteBuffer;

public final class e implements a {
    protected long bqO;
    protected MediaExtractor clX;
    protected long eTA;
    protected int eTJ;
    protected MediaCodec eTq;
    protected BufferInfo eTy = new BufferInfo();
    protected int eWc = -1;
    protected String fUM = null;
    protected MediaFormat fYc;
    private byte[] fYd;
    private int frameCount = 0;
    protected h oxp;
    private boolean oxs = false;
    protected int sampleSize;

    public e() {
        AppMethodBeat.i(76705);
        AppMethodBeat.o(76705);
    }

    public final int e(String str, long j, long j2, int i) {
        AppMethodBeat.i(76706);
        if (bo.isNullOrNil(str)) {
            AppMethodBeat.o(76706);
            return -1;
        }
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "initDecoder, srcFilePath: %s, startTime: %s, endTime: %s, videoFps: %s", new Object[]{str, Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(i)});
        try {
            int i2;
            this.clX = new MediaExtractor();
            this.clX.setDataSource(str);
            for (i2 = 0; i2 < this.clX.getTrackCount(); i2++) {
                MediaFormat trackFormat = this.clX.getTrackFormat(i2);
                if (trackFormat.getString("mime").toLowerCase().startsWith("video/")) {
                    this.eWc = i2;
                    this.fYc = trackFormat;
                    break;
                }
            }
            if (this.eWc < 0) {
                AppMethodBeat.o(76706);
                return -1;
            }
            this.fUM = str;
            this.bqO = j;
            this.eTA = j2;
            this.clX.selectTrack(this.eWc);
            String string = this.fYc.getString("mime");
            this.eTq = MediaCodec.createDecoderByType(string);
            MediaCodecInfo selectCodec = selectCodec(string);
            ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "found codec: %s", new Object[]{selectCodec});
            if (selectCodec != null) {
                ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "codec name: %s", new Object[]{selectCodec.getName()});
                ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "found colorFormat: %s", new Object[]{Integer.valueOf(a(selectCodec, string))});
                this.fYc.setInteger("color-format", i2);
            }
            this.eTq.configure(this.fYc, null, null, 0);
            this.eTq.start();
            AppMethodBeat.o(76706);
            return 0;
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.MMSightRemuxMediaCodecDecoder", e, "Init decoder failed : %s", new Object[]{e.getMessage()});
            AppMethodBeat.o(76706);
            return -1;
        }
    }

    private static MediaCodecInfo selectCodec(String str) {
        AppMethodBeat.i(76707);
        int codecCount = MediaCodecList.getCodecCount();
        for (int i = 0; i < codecCount; i++) {
            MediaCodecInfo codecInfoAt = MediaCodecList.getCodecInfoAt(i);
            if (!codecInfoAt.isEncoder()) {
                String[] supportedTypes = codecInfoAt.getSupportedTypes();
                for (String equalsIgnoreCase : supportedTypes) {
                    if (equalsIgnoreCase.equalsIgnoreCase(str)) {
                        AppMethodBeat.o(76707);
                        return codecInfoAt;
                    }
                }
                continue;
            }
        }
        AppMethodBeat.o(76707);
        return null;
    }

    private static int a(MediaCodecInfo mediaCodecInfo, String str) {
        AppMethodBeat.i(76708);
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "selectColorFormat, mimeType: %s, codecInfo: %s", new Object[]{str, mediaCodecInfo});
        long yz = bo.yz();
        CodecCapabilities capabilitiesForType = mediaCodecInfo.getCapabilitiesForType(str);
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "getCapabilitiesForType used %sms", new Object[]{Long.valueOf(bo.az(yz))});
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "color format length: %s", new Object[]{Integer.valueOf(capabilitiesForType.colorFormats.length)});
        int i = 0;
        for (int i2 : capabilitiesForType.colorFormats) {
            int i3;
            ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "capabilities colorFormat: %s", new Object[]{Integer.valueOf(i2)});
            switch (i2) {
                case ApiHelper$VERSION_CODES.KITKAT /*19*/:
                case ApiHelper$VERSION_CODES.LOLLIPOP /*21*/:
                    i3 = 1;
                    break;
                default:
                    i3 = 0;
                    break;
            }
            if (i3 != 0 && (i2 > i || i2 == 21)) {
                i = i2;
            }
        }
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "codec: %s, colorFormat: %s", new Object[]{mediaCodecInfo.getName(), Integer.valueOf(i)});
        AppMethodBeat.o(76708);
        return i;
    }

    private boolean UF() {
        AppMethodBeat.i(76709);
        if (this.eTq == null) {
            ab.e("MicroMsg.MMSightRemuxMediaCodecDecoder", "drainDecoder, decoder is null");
            AppMethodBeat.o(76709);
            return true;
        }
        ByteBuffer[] outputBuffers = this.eTq.getOutputBuffers();
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "decoderOutputByteBuffers length: %s", new Object[]{Integer.valueOf(outputBuffers.length)});
        int dequeueOutputBuffer = this.eTq.dequeueOutputBuffer(this.eTy, 60000);
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "outputBufferIndex-->".concat(String.valueOf(dequeueOutputBuffer)));
        while (dequeueOutputBuffer != -1) {
            if (dequeueOutputBuffer != -3) {
                if (dequeueOutputBuffer != -2) {
                    if (dequeueOutputBuffer >= 0) {
                        ab.v("MicroMsg.MMSightRemuxMediaCodecDecoder", "perform decoding");
                        ByteBuffer byteBuffer = outputBuffers[dequeueOutputBuffer];
                        if (byteBuffer == null) {
                            break;
                        }
                        long j = this.eTy.presentationTimeUs;
                        if (j < this.bqO * 1000 && (this.eTy.flags & 4) == 0) {
                            this.eTq.releaseOutputBuffer(dequeueOutputBuffer, false);
                            ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "decoder pts: %s, not reach start: %s", new Object[]{Long.valueOf(j), Long.valueOf(this.bqO * 1000)});
                            AppMethodBeat.o(76709);
                            return false;
                        } else if (this.eTy.size != 0) {
                            byteBuffer.position(this.eTy.offset);
                            byteBuffer.limit(this.eTy.offset + this.eTy.size);
                            long yz = bo.yz();
                            BufferInfo bufferInfo = this.eTy;
                            if (byteBuffer == null) {
                                ab.e("MicroMsg.MMSightRemuxMediaCodecDecoder", "processDecodeOutputBuffer error! byteBuffer is null");
                            } else {
                                this.frameCount++;
                                if (this.eTJ <= 1 || this.frameCount % this.eTJ != 0) {
                                    if (this.fYd == null) {
                                        this.fYd = new byte[byteBuffer.remaining()];
                                    }
                                    long yz2 = bo.yz();
                                    try {
                                        this.fYc = this.eTq.getOutputFormat();
                                    } catch (Exception e) {
                                        ab.e("MicroMsg.MMSightRemuxMediaCodecDecoder", "get output format error");
                                    }
                                    byteBuffer.get(this.fYd, 0, byteBuffer.remaining());
                                    ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "processDecodeOutputBuffer, byteBuffer: %s, bufferInfo: %s, size: %d cost %d", new Object[]{byteBuffer, bufferInfo, Integer.valueOf(bufferInfo.size), Long.valueOf(bo.az(yz2))});
                                    if (this.oxp != null) {
                                        boolean z;
                                        Object obj = (this.eTA == 1 || bufferInfo.presentationTimeUs < this.eTA * 1000) ? null : 1;
                                        h hVar = this.oxp;
                                        byte[] bArr = this.fYd;
                                        if (this.fYd == null || this.oxs || obj != null) {
                                            z = true;
                                        } else {
                                            z = false;
                                        }
                                        hVar.a(bArr, z, bufferInfo.presentationTimeUs / 1000);
                                    }
                                }
                            }
                            ab.v("MicroMsg.MMSightRemuxMediaCodecDecoder", "processDecodeOutputBuffer %s", new Object[]{Long.valueOf(bo.az(yz))});
                            this.eTq.releaseOutputBuffer(dequeueOutputBuffer, false);
                            if (this.eTA != 1 && j >= this.eTA * 1000) {
                                ab.e("MicroMsg.MMSightRemuxMediaCodecDecoder", "exceed endTimeMs");
                                this.oxs = true;
                                AppMethodBeat.o(76709);
                                return true;
                            } else if ((this.eTy.flags & 4) != 0) {
                                ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "receive end of stream");
                                try {
                                    this.eTq.stop();
                                    this.eTq.release();
                                    this.oxs = true;
                                    this.eTq = null;
                                } catch (Exception e2) {
                                    ab.e("MicroMsg.MMSightRemuxMediaCodecDecoder", "stop and release decoder error: %s", new Object[]{e2.getMessage()});
                                }
                                AppMethodBeat.o(76709);
                                return true;
                            } else {
                                AppMethodBeat.o(76709);
                                return false;
                            }
                        } else {
                            this.eTq.releaseOutputBuffer(dequeueOutputBuffer, false);
                        }
                    } else {
                        ab.w("MicroMsg.MMSightRemuxMediaCodecDecoder", "unexpected result from decoder.dequeueOutputBuffer: ".concat(String.valueOf(dequeueOutputBuffer)));
                    }
                } else {
                    this.fYc = this.eTq.getOutputFormat();
                    ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "decoder output format changed: " + this.fYc);
                }
            } else {
                outputBuffers = this.eTq.getOutputBuffers();
                ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "decoder output buffers changed");
            }
            dequeueOutputBuffer = this.eTq.dequeueOutputBuffer(this.eTy, 60000);
            if (dequeueOutputBuffer < 0) {
                break;
            }
        }
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "no output from decoder available, break");
        AppMethodBeat.o(76709);
        return false;
    }

    /* JADX WARNING: Missing block: B:11:0x0071, code skipped:
            if (UF() == false) goto L_0x0073;
     */
    public final void bPZ() {
        /*
        r10 = this;
        r0 = 76710; // 0x12ba6 float:1.07494E-40 double:3.79E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r0);
        r0 = 0;
        r10.frameCount = r0;
        r0 = 0;
        r10.oxs = r0;
    L_0x000c:
        r2 = r10.clX;
        r0 = r10.eTq;
        if (r0 != 0) goto L_0x00c5;
    L_0x0012:
        r0 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r1 = "input decoder is null";
        com.tencent.mm.sdk.platformtools.ab.i(r0, r1);
        r7 = 1;
    L_0x001c:
        if (r7 != 0) goto L_0x0036;
    L_0x001e:
        r0 = r10.clX;
        r0.advance();
        r0 = r10.clX;
        r0 = r0.getSampleTrackIndex();
        r1 = r10.eWc;
        if (r0 == r1) goto L_0x000c;
    L_0x002d:
        r0 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r1 = "track index not match, break";
        com.tencent.mm.sdk.platformtools.ab.i(r0, r1);
    L_0x0036:
        r0 = r10.clX;
        r1 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r2 = "sendDecoderEOS";
        com.tencent.mm.sdk.platformtools.ab.i(r1, r2);
        r1 = r10.eTq;
        r2 = r1.getInputBuffers();
        r1 = r10.eTq;
        r4 = 60000; // 0xea60 float:8.4078E-41 double:2.9644E-319;
        r1 = r1.dequeueInputBuffer(r4);
        if (r1 >= 0) goto L_0x0073;
    L_0x0052:
        r3 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r4 = "check decoder input buffer index = %d count = %d";
        r5 = 2;
        r5 = new java.lang.Object[r5];
        r6 = 0;
        r7 = java.lang.Integer.valueOf(r1);
        r5[r6] = r7;
        r6 = 1;
        r7 = 0;
        r7 = java.lang.Integer.valueOf(r7);
        r5[r6] = r7;
        com.tencent.mm.sdk.platformtools.ab.i(r3, r4, r5);
        r3 = r10.UF();
        if (r3 != 0) goto L_0x00af;
    L_0x0073:
        if (r1 < 0) goto L_0x00a9;
    L_0x0075:
        r2 = r2[r1];
        r2.clear();
        r3 = 0;
        r3 = r0.readSampleData(r2, r3);
        r10.sampleSize = r3;
        r3 = 0;
        r2.position(r3);
        r4 = r0.getSampleTime();
        if (r1 < 0) goto L_0x01a3;
    L_0x008b:
        r0 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r2 = "send EOS, decoderInputBufferIndex: %s";
        r3 = 1;
        r3 = new java.lang.Object[r3];
        r6 = 0;
        r7 = java.lang.Integer.valueOf(r1);
        r3[r6] = r7;
        com.tencent.mm.sdk.platformtools.ab.i(r0, r2, r3);
        r0 = r10.eTq;
        r2 = 0;
        r3 = 0;
        r6 = 1000; // 0x3e8 float:1.401E-42 double:4.94E-321;
        r4 = r4 * r6;
        r6 = 4;
        r0.queueInputBuffer(r1, r2, r3, r4, r6);
    L_0x00a9:
        r0 = 1;
        r10.oxs = r0;
        r10.UF();
    L_0x00af:
        r0 = new com.tencent.mm.plugin.mmsight.model.b.e$1;
        r0.<init>(r10);
        r2 = 500; // 0x1f4 float:7.0E-43 double:2.47E-321;
        com.tencent.mm.sdk.platformtools.al.n(r0, r2);
        r0 = r10.clX;
        r0.release();
        r0 = 76710; // 0x12ba6 float:1.07494E-40 double:3.79E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r0);
        return;
    L_0x00c5:
        r0 = r10.eTq;
        r3 = r0.getInputBuffers();
        if (r3 != 0) goto L_0x00d0;
    L_0x00cd:
        r7 = 0;
        goto L_0x001c;
    L_0x00d0:
        r0 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r1 = "decoderInputByteBuffers size: %d";
        r4 = 1;
        r4 = new java.lang.Object[r4];
        r5 = 0;
        r6 = r3.length;
        r6 = java.lang.Integer.valueOf(r6);
        r4[r5] = r6;
        com.tencent.mm.sdk.platformtools.ab.i(r0, r1, r4);
        r0 = 0;
    L_0x00e5:
        r1 = r10.eTq;
        r4 = 60000; // 0xea60 float:8.4078E-41 double:2.9644E-319;
        r1 = r1.dequeueInputBuffer(r4);
        if (r1 >= 0) goto L_0x011b;
    L_0x00f0:
        r4 = 15;
        if (r0 >= r4) goto L_0x011b;
    L_0x00f4:
        r4 = com.tencent.mm.sdk.platformtools.bo.yz();
        r1 = r10.UF();
        r6 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r7 = "drain cost1 %d";
        r8 = 1;
        r8 = new java.lang.Object[r8];
        r9 = 0;
        r4 = com.tencent.mm.sdk.platformtools.bo.az(r4);
        r4 = java.lang.Long.valueOf(r4);
        r8[r9] = r4;
        com.tencent.mm.sdk.platformtools.ab.i(r6, r7, r8);
        if (r1 == 0) goto L_0x0118;
    L_0x0115:
        r7 = 1;
        goto L_0x001c;
    L_0x0118:
        r0 = r0 + 1;
        goto L_0x00e5;
    L_0x011b:
        r0 = 0;
        if (r1 < 0) goto L_0x0198;
    L_0x011e:
        r3 = r3[r1];
        r3.clear();
        r4 = 0;
        r4 = r2.readSampleData(r3, r4);
        r10.sampleSize = r4;
        r4 = 0;
        r3.position(r4);
        r4 = r2.getSampleTime();
        r2 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r3 = "sampleTime: %s";
        r6 = 1;
        r6 = new java.lang.Object[r6];
        r7 = 0;
        r8 = java.lang.Long.valueOf(r4);
        r6[r7] = r8;
        com.tencent.mm.sdk.platformtools.ab.i(r2, r3, r6);
        r2 = r10.sampleSize;
        if (r2 < 0) goto L_0x0152;
    L_0x0149:
        r2 = r10.eTA;
        r6 = 1000; // 0x3e8 float:1.401E-42 double:4.94E-321;
        r2 = r2 * r6;
        r2 = (r4 > r2 ? 1 : (r4 == r2 ? 0 : -1));
        if (r2 < 0) goto L_0x01ae;
    L_0x0152:
        r0 = 1;
        r7 = r0;
    L_0x0154:
        r0 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r2 = "sawInputEOS: %s";
        r3 = 1;
        r3 = new java.lang.Object[r3];
        r6 = 0;
        r8 = java.lang.Boolean.valueOf(r7);
        r3[r6] = r8;
        com.tencent.mm.sdk.platformtools.ab.i(r0, r2, r3);
        r0 = r10.eTq;
        r2 = 0;
        r3 = r10.sampleSize;
        if (r7 == 0) goto L_0x0196;
    L_0x016e:
        r6 = 4;
    L_0x016f:
        r0.queueInputBuffer(r1, r2, r3, r4, r6);
    L_0x0172:
        r0 = com.tencent.mm.sdk.platformtools.bo.yz();
        r2 = r10.UF();
        r3 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r4 = "drain cost2 %d";
        r5 = 1;
        r5 = new java.lang.Object[r5];
        r6 = 0;
        r0 = com.tencent.mm.sdk.platformtools.bo.az(r0);
        r0 = java.lang.Long.valueOf(r0);
        r5[r6] = r0;
        com.tencent.mm.sdk.platformtools.ab.i(r3, r4, r5);
        if (r2 == 0) goto L_0x001c;
    L_0x0193:
        r7 = 1;
        goto L_0x001c;
    L_0x0196:
        r6 = 0;
        goto L_0x016f;
    L_0x0198:
        r1 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r2 = "input buffer not available";
        com.tencent.mm.sdk.platformtools.ab.w(r1, r2);
        r7 = r0;
        goto L_0x0172;
    L_0x01a3:
        r0 = "MicroMsg.MMSightRemuxMediaCodecDecoder";
        r1 = "input buffer not available";
        com.tencent.mm.sdk.platformtools.ab.w(r0, r1);
        goto L_0x00a9;
    L_0x01ae:
        r7 = r0;
        goto L_0x0154;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.plugin.mmsight.model.b.e.bPZ():void");
    }

    public final void a(h hVar) {
        this.oxp = hVar;
    }

    public final int alK() {
        AppMethodBeat.i(76711);
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "src color format: %s", new Object[]{Integer.valueOf(this.fYc.getInteger("color-format"))});
        switch (this.fYc.getInteger("color-format")) {
            case ApiHelper$VERSION_CODES.KITKAT /*19*/:
                AppMethodBeat.o(76711);
                return 2;
            case ApiHelper$VERSION_CODES.LOLLIPOP /*21*/:
            case 2130706688:
                AppMethodBeat.o(76711);
                return 1;
            default:
                AppMethodBeat.o(76711);
                return 1;
        }
    }

    public final Point alJ() {
        AppMethodBeat.i(76712);
        Point point = new Point(this.fYc.getInteger("width"), this.fYc.getInteger("height"));
        AppMethodBeat.o(76712);
        return point;
    }

    public final void mp(int i) {
        AppMethodBeat.i(76713);
        ab.i("MicroMsg.MMSightRemuxMediaCodecDecoder", "setFrameDropInterval: %s", new Object[]{Integer.valueOf(i)});
        this.eTJ = i;
        AppMethodBeat.o(76713);
    }
}
