package com.google.android.exoplayer2.ext.ffmpeg;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import p000.p001.p002.p003.p004.C0567;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p190.C3431;
import p138.p176.p177.p190.p193.AbstractC3512;
import p138.p176.p177.p190.p193.AbstractC3513;
import p138.p176.p177.p190.p193.C3511;
import p138.p176.p177.p190.p193.C3515;
import p138.p176.p177.p190.p239.C4140;
import p138.p176.p177.p190.p239.C4150;

/* JADX INFO: loaded from: classes.dex */
public final class FfmpegDecoder extends AbstractC3513<C3511, C3515, FfmpegDecoderException> {

    /* JADX INFO: renamed from: ދ, reason: contains not printable characters */
    public final String f1657;

    /* JADX INFO: renamed from: ތ, reason: contains not printable characters */
    public final byte[] f1658;

    /* JADX INFO: renamed from: ލ, reason: contains not printable characters */
    public final int f1659;

    /* JADX INFO: renamed from: ގ, reason: contains not printable characters */
    public final int f1660;

    /* JADX INFO: renamed from: ޏ, reason: contains not printable characters */
    public long f1661;

    /* JADX INFO: renamed from: ސ, reason: contains not printable characters */
    public boolean f1662;

    /* JADX INFO: renamed from: ޑ, reason: contains not printable characters */
    public volatile int f1663;

    /* JADX INFO: renamed from: ޒ, reason: contains not printable characters */
    public volatile int f1664;

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Failed to restore switch over string. Please report as a decompilation issue */
    /* JADX WARN: Removed duplicated region for block: B:19:0x0057  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public FfmpegDecoder(int i, int i2, int i3, C3431 c3431, boolean z) throws FfmpegDecoderException {
        byte b;
        byte[] bArrArray;
        super(new C3511[i], new C3515[i2]);
        if (!FfmpegLibrary.m1139()) {
            throw new FfmpegDecoderException("Failed to load decoder native libraries.");
        }
        C0567.m1856(c3431.f10090);
        String strM1138 = FfmpegLibrary.m1138(c3431.f10090);
        C0567.m1856(strM1138);
        this.f1657 = strM1138;
        String str = c3431.f10090;
        List<byte[]> list = c3431.f10092;
        switch (str.hashCode()) {
            case -1003765268:
                b = !str.equals("audio/vorbis") ? (byte) -1 : (byte) 3;
                break;
            case -53558318:
                if (str.equals("audio/mp4a-latm")) {
                    b = 0;
                    break;
                }
                break;
            case 1504470054:
                if (str.equals("audio/alac")) {
                    b = 2;
                    break;
                }
                break;
            case 1504891608:
                if (str.equals("audio/opus")) {
                    b = 1;
                    break;
                }
                break;
        }
        if (b == 0 || b == 1) {
            bArrArray = list.get(0);
        } else if (b == 2) {
            byte[] bArr = list.get(0);
            int length = bArr.length + 12;
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(length);
            byteBufferAllocate.putInt(length);
            byteBufferAllocate.putInt(1634492771);
            byteBufferAllocate.putInt(0);
            byteBufferAllocate.put(bArr, 0, bArr.length);
            bArrArray = byteBufferAllocate.array();
        } else if (b != 3) {
            bArrArray = null;
        } else {
            byte[] bArr2 = list.get(0);
            byte[] bArr3 = list.get(1);
            byte[] bArr4 = new byte[bArr2.length + bArr3.length + 6];
            bArr4[0] = (byte) (bArr2.length >> 8);
            bArr4[1] = (byte) (bArr2.length & 255);
            System.arraycopy(bArr2, 0, bArr4, 2, bArr2.length);
            bArr4[bArr2.length + 2] = 0;
            bArr4[bArr2.length + 3] = 0;
            bArr4[bArr2.length + 4] = (byte) (bArr3.length >> 8);
            bArr4[bArr2.length + 5] = (byte) (bArr3.length & 255);
            System.arraycopy(bArr3, 0, bArr4, bArr2.length + 6, bArr3.length);
            bArrArray = bArr4;
        }
        this.f1658 = bArrArray;
        this.f1659 = z ? 4 : 2;
        this.f1660 = z ? 131072 : 65536;
        long jFfmpegInitialize = ffmpegInitialize(this.f1657, this.f1658, z, c3431.f10104, c3431.f10103);
        this.f1661 = jFfmpegInitialize;
        if (jFfmpegInitialize == 0) {
            throw new FfmpegDecoderException("Initialization failed.");
        }
        m5815(i3);
    }

    public final native int ffmpegDecode(long j, ByteBuffer byteBuffer, int i, ByteBuffer byteBuffer2, int i2);

    public final native int ffmpegGetChannelCount(long j);

    public final native int ffmpegGetSampleRate(long j);

    public final native long ffmpegInitialize(String str, byte[] bArr, boolean z, int i, int i2);

    public final native void ffmpegRelease(long j);

    public final native long ffmpegReset(long j, byte[] bArr);

    @Override // p138.p176.p177.p190.p193.AbstractC3513
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public Exception mo1132(Throwable th) {
        return new FfmpegDecoderException("Unexpected decode error", th);
    }

    @Override // p138.p176.p177.p190.p193.AbstractC3513
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public Exception mo1133(C3511 c3511, AbstractC3512 abstractC3512, boolean z) {
        C3515 c3515 = (C3515) abstractC3512;
        if (z) {
            long jFfmpegReset = ffmpegReset(this.f1661, this.f1658);
            this.f1661 = jFfmpegReset;
            if (jFfmpegReset == 0) {
                return new FfmpegDecoderException("Error resetting (see logcat).");
            }
        }
        ByteBuffer byteBuffer = c3511.f10461;
        C4150.m6755(byteBuffer);
        int iLimit = byteBuffer.limit();
        long j = c3511.f10463;
        int i = this.f1660;
        c3515.f10466 = j;
        ByteBuffer byteBuffer2 = c3515.f10483;
        if (byteBuffer2 == null || byteBuffer2.capacity() < i) {
            c3515.f10483 = ByteBuffer.allocateDirect(i).order(ByteOrder.nativeOrder());
        }
        c3515.f10483.position(0);
        c3515.f10483.limit(i);
        ByteBuffer byteBuffer3 = c3515.f10483;
        int iFfmpegDecode = ffmpegDecode(this.f1661, byteBuffer, iLimit, byteBuffer3, this.f1660);
        if (iFfmpegDecode == -1) {
            c3515.f10443 = Integer.MIN_VALUE;
        } else {
            if (iFfmpegDecode == -2) {
                return new FfmpegDecoderException("Error decoding (see logcat).");
            }
            if (!this.f1662) {
                this.f1663 = ffmpegGetChannelCount(this.f1661);
                this.f1664 = ffmpegGetSampleRate(this.f1661);
                if (this.f1664 == 0 && "alac".equals(this.f1657)) {
                    C0567.m1856(this.f1658);
                    C4140 c4140 = new C4140(this.f1658);
                    c4140.m6693(this.f1658.length - 4);
                    this.f1664 = c4140.m6705();
                }
                this.f1662 = true;
            }
            byteBuffer3.position(0);
            byteBuffer3.limit(iFfmpegDecode);
        }
        return null;
    }

    @Override // p138.p176.p177.p190.p193.AbstractC3513, p138.p176.p177.p190.p193.InterfaceC3509
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public void mo1134() {
        super.mo1134();
        ffmpegRelease(this.f1661);
        this.f1661 = 0L;
    }

    @Override // p138.p176.p177.p190.p193.InterfaceC3509
    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public String mo1135() {
        StringBuilder sbM4889 = C2803.m4889("ffmpeg");
        sbM4889.append(FfmpegLibrary.m1139() ? FfmpegLibrary.ffmpegGetVersion() : null);
        sbM4889.append("-");
        sbM4889.append(this.f1657);
        return sbM4889.toString();
    }

    @Override // p138.p176.p177.p190.p193.AbstractC3513
    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public C3511 mo1136() {
        return new C3511(2);
    }

    @Override // p138.p176.p177.p190.p193.AbstractC3513
    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public AbstractC3512 mo1137() {
        return new C3515(this);
    }
}
