package T0;

import O1.C0225a;
import android.util.Base64;
import d5.AbstractC0844a;
import f1.C0910a;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import k1.C1085a;
import x0.AbstractC1709a;

/* JADX INFO: renamed from: T0.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractC0302b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final int[] f6366a = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350};

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final int[] f6367b = {0, 1, 2, 3, 4, 5, 6, 8, -1, -1, -1, 7, 8, -1, 8, -1};

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final int[] f6368c = {1, 2, 3, 6};

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final int[] f6369d = {48000, 44100, 32000};

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final int[] f6370e = {24000, 22050, 16000};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final int[] f6371f = {2, 1, 2, 3, 3, 4, 4, 5};

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final int[] f6372g = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640};

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final int[] f6373h = {69, 87, 104, 121, 139, 174, 208, 243, 278, 348, 417, 487, 557, 696, 835, 975, 1114, 1253, 1393};
    public static final int[] i = {2002, 2000, 1920, 1601, 1600, 1001, 1000, 960, 800, 800, 480, 400, 400, 2048};

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final int[] f6374j = {1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8};

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final int[] f6375k = {-1, 8000, 16000, 32000, -1, -1, 11025, 22050, 44100, -1, -1, 12000, 24000, 48000, -1, -1};

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final int[] f6376l = {64, 112, 128, 192, 224, 256, 384, 448, 512, 640, 768, 896, 1024, 1152, 1280, 1536, 1920, 2048, 2304, 2560, 2688, 2816, 2823, 2944, 3072, 3840, 4096, 6144, 7680};

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final int[] f6377m = {8000, 16000, 32000, 64000, 128000, 22050, 44100, 88200, 176400, 352800, 12000, 24000, 48000, 96000, 192000, 384000};

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public static final int[] f6378n = {5, 8, 10, 12};

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final int[] f6379o = {6, 9, 12, 15};

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public static final int[] f6380p = {2, 4, 6, 8};

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public static final int[] f6381q = {9, 11, 13, 16};

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public static final int[] f6382r = {5, 8, 10, 12};

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final String[] f6383s = {"audio/mpeg-L1", "audio/mpeg-L2", "audio/mpeg"};

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public static final int[] f6384t = {44100, 48000, 32000};

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final int[] f6385u = {32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000};

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public static final int[] f6386v = {32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000};

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public static final int[] f6387w = {32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000};

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public static final int[] f6388x = {32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000};

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public static final int[] f6389y = {8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000};

    public static ArrayList a(byte[] bArr) {
        long j8 = (((long) (((bArr[11] & 255) << 8) | (bArr[10] & 255))) * 1000000000) / 48000;
        ArrayList arrayList = new ArrayList(3);
        arrayList.add(bArr);
        arrayList.add(ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()).putLong(j8).array());
        arrayList.add(ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()).putLong(80000000L).array());
        return arrayList;
    }

    public static boolean b(x0.o oVar, r rVar, int i5, L4.h hVar) {
        int i8 = oVar.f18540b;
        long jV = oVar.v();
        long j8 = jV >>> 16;
        if (j8 != i5) {
            return false;
        }
        boolean z8 = (j8 & 1) == 1;
        int i9 = (int) ((jV >> 12) & 15);
        int i10 = (int) ((jV >> 8) & 15);
        int i11 = (int) ((jV >> 4) & 15);
        int i12 = (int) ((jV >> 1) & 7);
        boolean z9 = (jV & 1) == 1;
        if (i11 <= 7) {
            if (i11 != rVar.f6450g - 1) {
                return false;
            }
        } else if (i11 > 10 || rVar.f6450g != 2) {
            return false;
        }
        if (!(i12 == 0 || i12 == rVar.i) || z9) {
            return false;
        }
        try {
            long jA = oVar.A();
            if (!z8) {
                jA *= (long) rVar.f6445b;
            }
            hVar.f4006a = jA;
            int iS = s(i9, oVar);
            if (iS == -1 || iS > rVar.f6445b) {
                return false;
            }
            if (i10 != 0) {
                if (i10 > 11) {
                    int i13 = rVar.f6448e;
                    if (i10 != 12) {
                        if (i10 > 14) {
                            return false;
                        }
                        int iZ = oVar.z();
                        if (i10 == 14) {
                            iZ *= 10;
                        }
                        if (iZ != i13) {
                            return false;
                        }
                    } else if (oVar.t() * 1000 != i13) {
                        return false;
                    }
                } else if (i10 != rVar.f6449f) {
                    return false;
                }
            }
            int iT = oVar.t();
            int i14 = oVar.f18540b;
            byte[] bArr = oVar.f18539a;
            int i15 = i14 - 1;
            int i16 = x0.v.f18553a;
            int i17 = 0;
            for (int i18 = i8; i18 < i15; i18++) {
                i17 = x0.v.f18563l[i17 ^ (bArr[i18] & 255)];
            }
            return iT == i17;
        } catch (NumberFormatException unused) {
            return false;
        }
    }

    public static void c(String str, boolean z8) throws u0.F {
        if (!z8) {
            throw u0.F.a(null, str);
        }
    }

    public static void d(long j8, x0.o oVar, F[] fArr) {
        int i5;
        while (true) {
            if (oVar.a() <= 1) {
                return;
            }
            int i8 = 0;
            while (true) {
                if (oVar.a() == 0) {
                    i5 = -1;
                    break;
                }
                int iT = oVar.t();
                i8 += iT;
                if (iT != 255) {
                    i5 = i8;
                    break;
                }
            }
            int i9 = 0;
            while (true) {
                if (oVar.a() == 0) {
                    i9 = -1;
                    break;
                }
                int iT2 = oVar.t();
                i9 += iT2;
                if (iT2 != 255) {
                    break;
                }
            }
            int i10 = oVar.f18540b + i9;
            if (i9 == -1 || i9 > oVar.a()) {
                AbstractC1709a.B("CeaUtil", "Skipping remainder of malformed SEI NAL unit.");
                i10 = oVar.f18541c;
            } else if (i5 == 4 && i9 >= 8) {
                int iT3 = oVar.t();
                int iZ = oVar.z();
                int iG = iZ == 49 ? oVar.g() : 0;
                int iT4 = oVar.t();
                if (iZ == 47) {
                    oVar.G(1);
                }
                boolean z8 = iT3 == 181 && (iZ == 49 || iZ == 47) && iT4 == 3;
                if (iZ == 49) {
                    z8 &= iG == 1195456820;
                }
                if (z8) {
                    e(j8, oVar, fArr);
                }
            }
            oVar.F(i10);
        }
    }

    public static void e(long j8, x0.o oVar, F[] fArr) {
        int iT = oVar.t();
        if ((iT & 64) != 0) {
            oVar.G(1);
            int i5 = (iT & 31) * 3;
            int i8 = oVar.f18540b;
            for (F f3 : fArr) {
                oVar.F(i8);
                f3.c(i5, oVar);
                AbstractC1709a.j(j8 != -9223372036854775807L);
                f3.a(j8, 1, i5, 0, null);
            }
        }
    }

    public static int f(int i5, int i8) {
        int i9 = i8 / 2;
        if (i5 < 0 || i5 >= 3 || i8 < 0 || i9 >= 19) {
            return -1;
        }
        int i10 = f6369d[i5];
        if (i10 == 44100) {
            return ((i8 % 2) + f6373h[i9]) * 2;
        }
        int i11 = f6372g[i9];
        return i10 == 32000 ? i11 * 6 : i11 * 4;
    }

    public static void g(int i5, x0.o oVar) {
        oVar.C(7);
        byte[] bArr = oVar.f18539a;
        bArr[0] = -84;
        bArr[1] = 64;
        bArr[2] = -1;
        bArr[3] = -1;
        bArr[4] = (byte) ((i5 >> 16) & 255);
        bArr[5] = (byte) ((i5 >> 8) & 255);
        bArr[6] = (byte) (i5 & 255);
    }

    public static int h(int i5) {
        int i8;
        int i9;
        int i10;
        int i11;
        if (!((i5 & (-2097152)) == -2097152) || (i8 = (i5 >>> 19) & 3) == 1 || (i9 = (i5 >>> 17) & 3) == 0 || (i10 = (i5 >>> 12) & 15) == 0 || i10 == 15 || (i11 = (i5 >>> 10) & 3) == 3) {
            return -1;
        }
        int i12 = f6384t[i11];
        if (i8 == 2) {
            i12 /= 2;
        } else if (i8 == 0) {
            i12 /= 4;
        }
        int i13 = (i5 >>> 9) & 1;
        if (i9 == 3) {
            return ((((i8 == 3 ? f6385u[i10 - 1] : f6386v[i10 - 1]) * 12) / i12) + i13) * 4;
        }
        int i14 = i8 == 3 ? i9 == 2 ? f6387w[i10 - 1] : f6388x[i10 - 1] : f6389y[i10 - 1];
        if (i8 == 3) {
            return ((i14 * 144) / i12) + i13;
        }
        return (((i9 == 1 ? 72 : 144) * i14) / i12) + i13;
    }

    public static H i(byte[] bArr) {
        byte b8 = bArr[0];
        if (b8 == 127 || b8 == 100 || b8 == 64 || b8 == 113) {
            return new H(bArr.length, bArr);
        }
        byte[] bArrCopyOf = Arrays.copyOf(bArr, bArr.length);
        byte b9 = bArrCopyOf[0];
        if (b9 == -2 || b9 == -1 || b9 == 37 || b9 == -14 || b9 == -24) {
            for (int i5 = 0; i5 < bArrCopyOf.length - 1; i5 += 2) {
                byte b10 = bArrCopyOf[i5];
                int i8 = i5 + 1;
                bArrCopyOf[i5] = bArrCopyOf[i8];
                bArrCopyOf[i8] = b10;
            }
        }
        H h8 = new H(bArrCopyOf.length, bArrCopyOf);
        if (bArrCopyOf[0] == 31) {
            H h9 = new H(bArrCopyOf.length, bArrCopyOf);
            while (h9.b() >= 16) {
                h9.t(2);
                int i9 = h9.i(14) & 16383;
                int iMin = Math.min(8 - h8.f6360c, 14);
                int i10 = h8.f6360c;
                int i11 = (8 - i10) - iMin;
                byte[] bArr2 = h8.f6361d;
                int i12 = h8.f6359b;
                byte b11 = (byte) (((65280 >> i10) | ((1 << i11) - 1)) & bArr2[i12]);
                bArr2[i12] = b11;
                int i13 = 14 - iMin;
                bArr2[i12] = (byte) (b11 | ((i9 >>> i13) << i11));
                int i14 = i12 + 1;
                while (i13 > 8) {
                    h8.f6361d[i14] = (byte) (i9 >>> (i13 - 8));
                    i13 -= 8;
                    i14++;
                }
                int i15 = 8 - i13;
                byte[] bArr3 = h8.f6361d;
                byte b12 = (byte) (bArr3[i14] & ((1 << i15) - 1));
                bArr3[i14] = b12;
                bArr3[i14] = (byte) (((i9 & ((1 << i13) - 1)) << i15) | b12);
                h8.t(14);
                h8.a();
            }
        }
        h8.o(bArrCopyOf.length, bArrCopyOf);
        return h8;
    }

    public static long j(byte b8, byte b9) {
        int i5;
        int i8 = b8 & 255;
        int i9 = b8 & 3;
        if (i9 != 0) {
            i5 = 2;
            if (i9 != 1 && i9 != 2) {
                i5 = b9 & 63;
            }
        } else {
            i5 = 1;
        }
        int i10 = i8 >> 3;
        int i11 = i10 & 3;
        return ((long) i5) * ((long) (i10 >= 16 ? 2500 << i11 : i10 >= 12 ? 10000 << (i10 & 1) : i11 == 3 ? 60000 : 10000 << i11));
    }

    public static int k(H h8) throws u0.F {
        int i5 = h8.i(4);
        if (i5 == 15) {
            if (h8.b() >= 24) {
                return h8.i(24);
            }
            throw u0.F.a(null, "AAC header insufficient data");
        }
        if (i5 < 13) {
            return f6366a[i5];
        }
        throw u0.F.a(null, "AAC header wrong Sampling Frequency Index");
    }

    public static int l(int i5) {
        int i8 = 0;
        while (i5 > 0) {
            i8++;
            i5 >>>= 1;
        }
        return i8;
    }

    /* JADX WARN: Removed duplicated region for block: B:40:0x0083  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x008b  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x0090  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static R0.h m(H h8) {
        int i5;
        int i8;
        int i9 = h8.i(16);
        int i10 = h8.i(16);
        if (i10 == 65535) {
            i10 = h8.i(24);
            i5 = 7;
        } else {
            i5 = 4;
        }
        int i11 = i10 + i5;
        if (i9 == 44097) {
            i11 += 2;
        }
        if (h8.i(2) == 3) {
            do {
                h8.i(2);
            } while (h8.h());
        }
        int i12 = h8.i(10);
        if (h8.h() && h8.i(3) > 0) {
            h8.t(2);
        }
        int i13 = h8.h() ? 48000 : 44100;
        int i14 = h8.i(4);
        int[] iArr = i;
        if (i13 == 44100 && i14 == 13) {
            i8 = iArr[i14];
        } else if (i13 != 48000 || i14 >= 14) {
            i8 = 0;
        } else {
            int i15 = iArr[i14];
            int i16 = i12 % 5;
            if (i16 == 1) {
                i8 = (i14 == 3 || i14 == 8) ? i15 + 1 : i15;
            } else if (i16 != 2) {
                if (i16 != 3) {
                    if (i16 == 4 && (i14 == 3 || i14 == 8 || i14 == 11)) {
                    }
                }
            } else if (i14 == 8 || i14 == 11) {
            }
        }
        return new R0.h(i13, i11, i8);
    }

    public static C0301a n(H h8, boolean z8) throws u0.F {
        int i5 = h8.i(5);
        if (i5 == 31) {
            i5 = h8.i(6) + 32;
        }
        int iK = k(h8);
        int i8 = h8.i(4);
        String strJ = AbstractC0844a.j("mp4a.40.", i5);
        if (i5 == 5 || i5 == 29) {
            iK = k(h8);
            int i9 = h8.i(5);
            if (i9 == 31) {
                i9 = h8.i(6) + 32;
            }
            i5 = i9;
            if (i5 == 22) {
                i8 = h8.i(4);
            }
        }
        if (z8) {
            if (i5 != 1 && i5 != 2 && i5 != 3 && i5 != 4 && i5 != 6 && i5 != 7 && i5 != 17) {
                switch (i5) {
                    case 19:
                    case 20:
                    case 21:
                    case 22:
                    case 23:
                        break;
                    default:
                        throw u0.F.c("Unsupported audio object type: " + i5);
                }
            }
            if (h8.h()) {
                AbstractC1709a.B("AacUtil", "Unexpected frameLengthFlag = 1");
            }
            if (h8.h()) {
                h8.t(14);
            }
            boolean zH = h8.h();
            if (i8 == 0) {
                throw new UnsupportedOperationException();
            }
            if (i5 == 6 || i5 == 20) {
                h8.t(3);
            }
            if (zH) {
                if (i5 == 22) {
                    h8.t(16);
                }
                if (i5 == 17 || i5 == 19 || i5 == 20 || i5 == 23) {
                    h8.t(3);
                }
                h8.t(1);
            }
            switch (i5) {
                case 17:
                case 19:
                case 20:
                case 21:
                case 22:
                case 23:
                    int i10 = h8.i(2);
                    if (i10 == 2 || i10 == 3) {
                        throw u0.F.c("Unsupported epConfig: " + i10);
                    }
                    break;
            }
        }
        int i11 = f6367b[i8];
        if (i11 != -1) {
            return new C0301a(iK, i11, strJ);
        }
        throw u0.F.a(null, null);
    }

    public static void o(H h8, D0.n nVar) throws u0.F {
        int i5 = h8.i(5);
        h8.t(2);
        if (h8.h()) {
            h8.t(5);
        }
        if (i5 >= 7 && i5 <= 10) {
            h8.s();
        }
        if (h8.h()) {
            int i8 = h8.i(3);
            if (nVar.f1572b == -1 && i5 >= 0 && i5 <= 15 && (i8 == 0 || i8 == 1)) {
                nVar.f1572b = i5;
            }
            if (h8.h()) {
                v(h8);
            }
        }
    }

    public static void p(H h8, D0.n nVar) throws u0.F {
        h8.t(2);
        boolean zH = h8.h();
        int i5 = h8.i(8);
        for (int i8 = 0; i8 < i5; i8++) {
            h8.t(2);
            if (h8.h()) {
                h8.t(5);
            }
            if (zH) {
                h8.t(24);
            } else {
                if (h8.h()) {
                    if (!h8.h()) {
                        h8.t(4);
                    }
                    nVar.f1573c = h8.i(6) + 1;
                }
                h8.t(4);
            }
        }
        if (h8.h()) {
            h8.t(3);
            if (h8.h()) {
                v(h8);
            }
        }
    }

    public static int q(H h8, int[] iArr) {
        int i5 = 0;
        for (int i8 = 0; i8 < 3 && h8.h(); i8++) {
            i5++;
        }
        int i9 = 0;
        for (int i10 = 0; i10 < i5; i10++) {
            i9 += 1 << iArr[i10];
        }
        return h8.i(iArr[i5]) + i9;
    }

    public static u0.D r(List list) {
        ArrayList arrayList = new ArrayList();
        for (int i5 = 0; i5 < list.size(); i5++) {
            String str = (String) list.get(i5);
            int i8 = x0.v.f18553a;
            String[] strArrSplit = str.split("=", 2);
            if (strArrSplit.length != 2) {
                AbstractC1709a.B("VorbisUtil", "Failed to parse Vorbis comment: ".concat(str));
            } else if (strArrSplit[0].equals("METADATA_BLOCK_PICTURE")) {
                try {
                    arrayList.add(C0910a.d(new x0.o(Base64.decode(strArrSplit[1], 0))));
                } catch (RuntimeException e8) {
                    AbstractC1709a.C("VorbisUtil", "Failed to parse vorbis picture", e8);
                }
            } else {
                arrayList.add(new C1085a(strArrSplit[0], strArrSplit[1]));
            }
        }
        if (arrayList.isEmpty()) {
            return null;
        }
        return new u0.D(arrayList);
    }

    public static int s(int i5, x0.o oVar) {
        switch (i5) {
            case 1:
                return 192;
            case 2:
            case 3:
            case 4:
            case 5:
                return 576 << (i5 - 2);
            case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                return oVar.t() + 1;
            case Z.g.DOUBLE_FIELD_NUMBER /* 7 */:
                return oVar.z() + 1;
            case Z.g.BYTES_FIELD_NUMBER /* 8 */:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
                return 256 << (i5 - 8);
            default:
                return -1;
        }
    }

    public static C0225a t(x0.o oVar) {
        oVar.G(1);
        int iW = oVar.w();
        long j8 = ((long) oVar.f18540b) + ((long) iW);
        int i5 = iW / 18;
        long[] jArrCopyOf = new long[i5];
        long[] jArrCopyOf2 = new long[i5];
        int i8 = 0;
        while (true) {
            if (i8 >= i5) {
                break;
            }
            long jN = oVar.n();
            if (jN == -1) {
                jArrCopyOf = Arrays.copyOf(jArrCopyOf, i8);
                jArrCopyOf2 = Arrays.copyOf(jArrCopyOf2, i8);
                break;
            }
            jArrCopyOf[i8] = jN;
            jArrCopyOf2[i8] = oVar.n();
            oVar.G(2);
            i8++;
        }
        oVar.G((int) (j8 - ((long) oVar.f18540b)));
        return new C0225a(jArrCopyOf, jArrCopyOf2, 13, false);
    }

    public static P.g u(x0.o oVar, boolean z8, boolean z9) throws u0.F {
        if (z8) {
            w(3, oVar, false);
        }
        oVar.r((int) oVar.k(), StandardCharsets.UTF_8);
        long jK = oVar.k();
        String[] strArr = new String[(int) jK];
        for (int i5 = 0; i5 < jK; i5++) {
            strArr[i5] = oVar.r((int) oVar.k(), StandardCharsets.UTF_8);
        }
        if (z9 && (oVar.t() & 1) == 0) {
            throw u0.F.a(null, "framing bit expected to be set");
        }
        return new P.g(8, strArr);
    }

    public static void v(H h8) throws u0.F {
        int i5 = h8.i(6);
        if (i5 < 2 || i5 > 42) {
            throw u0.F.c(String.format("Invalid language tag bytes number: %d. Must be between 2 and 42.", Integer.valueOf(i5)));
        }
        h8.t(i5 * 8);
    }

    public static boolean w(int i5, x0.o oVar, boolean z8) throws u0.F {
        if (oVar.a() < 7) {
            if (z8) {
                return false;
            }
            throw u0.F.a(null, "too short header: " + oVar.a());
        }
        if (oVar.t() != i5) {
            if (z8) {
                return false;
            }
            throw u0.F.a(null, "expected header type " + Integer.toHexString(i5));
        }
        if (oVar.t() == 118 && oVar.t() == 111 && oVar.t() == 114 && oVar.t() == 98 && oVar.t() == 105 && oVar.t() == 115) {
            return true;
        }
        if (z8) {
            return false;
        }
        throw u0.F.a(null, "expected characters 'vorbis'");
    }
}
