package r1;

import g3.j;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import q1.C1446c;
import x0.AbstractC1709a;
import x0.o;

/* JADX INFO: renamed from: r1.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1487c extends h {
    public final int i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final int f16368j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final int f16369k;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public List f16373o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public List f16374p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public int f16375q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public int f16376r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public boolean f16377s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public boolean f16378t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public byte f16379u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public byte f16380v;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public boolean f16382x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public long f16383y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public static final int[] f16366z = {11, 1, 3, 12, 14, 5, 7, 9};

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final int[] f16360A = {0, 4, 8, 12, 16, 20, 24, 28};

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public static final int[] f16361B = {-1, -16711936, -16776961, -16711681, -65536, -256, -65281};

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public static final int[] f16362C = {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 225, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 233, 93, 237, 243, 250, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 231, 247, 209, 241, 9632};
    public static final int[] D = {174, 176, 189, 191, 8482, 162, 163, 9834, 224, 32, 232, 226, 234, 238, 244, 251};

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public static final int[] f16363E = {193, 201, 211, 218, 220, 252, 8216, 161, 42, 39, 8212, 169, 8480, 8226, 8220, 8221, 192, 194, 199, 200, 202, 203, 235, 206, 207, 239, 212, 217, 249, 219, 171, 187};

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public static final int[] f16364F = {195, 227, 205, 204, 236, 210, 242, 213, 245, 123, 125, 92, 94, 95, 124, 126, 196, 228, 214, 246, 223, 165, 164, 9474, 197, 229, 216, 248, 9484, 9488, 9492, 9496};

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public static final boolean[] f16365G = {false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false, false, true, true, false, true, false, false, true, false, true, true, false, true, false, false, true, true, false, false, true, false, true, true, false};

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final o f16367h = new o();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final ArrayList f16371m = new ArrayList();

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public C1486b f16372n = new C1486b(0, 4);

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public int f16381w = 0;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final long f16370l = 16000000;

    public C1487c(int i, String str) {
        this.i = "application/x-mp4-cea-608".equals(str) ? 2 : 3;
        if (i == 1) {
            this.f16369k = 0;
            this.f16368j = 0;
        } else if (i == 2) {
            this.f16369k = 1;
            this.f16368j = 0;
        } else if (i == 3) {
            this.f16369k = 0;
            this.f16368j = 1;
        } else if (i != 4) {
            AbstractC1709a.B("Cea608Decoder", "Invalid channel. Defaulting to CC1.");
            this.f16369k = 0;
            this.f16368j = 0;
        } else {
            this.f16369k = 1;
            this.f16368j = 1;
        }
        m(0);
        l();
        this.f16382x = true;
        this.f16383y = -9223372036854775807L;
    }

    @Override // r1.h, A0.e
    public final void flush() {
        super.flush();
        this.f16373o = null;
        this.f16374p = null;
        m(0);
        this.f16376r = 4;
        this.f16372n.f16359h = 4;
        l();
        this.f16377s = false;
        this.f16378t = false;
        this.f16379u = (byte) 0;
        this.f16380v = (byte) 0;
        this.f16381w = 0;
        this.f16382x = true;
        this.f16383y = -9223372036854775807L;
    }

    @Override // r1.h
    public final j g() {
        List list = this.f16373o;
        this.f16374p = list;
        list.getClass();
        return new j(list);
    }

    /* JADX WARN: Removed duplicated region for block: B:179:0x0085 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:181:0x007e A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:26:0x0059  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x00a6 A[FALL_THROUGH] */
    @Override // r1.h
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void h(C1491g c1491g) {
        ByteBuffer byteBuffer = c1491g.f18u;
        byteBuffer.getClass();
        byte[] bArrArray = byteBuffer.array();
        int iLimit = byteBuffer.limit();
        o oVar = this.f16367h;
        oVar.D(iLimit, bArrArray);
        boolean z8 = false;
        while (true) {
            int iA = oVar.a();
            int i = this.i;
            if (iA < i) {
                if (z8) {
                    int i5 = this.f16375q;
                    if (i5 == 1 || i5 == 3) {
                        this.f16373o = k();
                        this.f16383y = this.f16430e;
                        return;
                    }
                    return;
                }
                return;
            }
            int iT = i == 2 ? -4 : oVar.t();
            int iT2 = oVar.t();
            int iT3 = oVar.t();
            if ((iT & 2) == 0 && (iT & 1) == this.f16368j) {
                byte b8 = (byte) (iT2 & 127);
                byte b9 = (byte) (iT3 & 127);
                if (b8 != 0 || b9 != 0) {
                    boolean z9 = this.f16377s;
                    if ((iT & 4) == 4) {
                        boolean[] zArr = f16365G;
                        boolean z10 = zArr[iT2] && zArr[iT3];
                        this.f16377s = z10;
                        if (!z10 || (b8 & 240) != 16) {
                            this.f16378t = false;
                            if (!z10) {
                                if (1 <= b8 && b8 <= 15) {
                                    this.f16382x = false;
                                } else if ((b8 & 246) == 20) {
                                    if (b9 != 32 && b9 != 47) {
                                        switch (b9) {
                                            default:
                                                switch (b9) {
                                                    case 42:
                                                    case 43:
                                                        this.f16382x = false;
                                                        break;
                                                }
                                            case 37:
                                            case 38:
                                            case 39:
                                                break;
                                        }
                                    } else {
                                        this.f16382x = true;
                                    }
                                }
                                if (this.f16382x) {
                                    int i8 = b8 & 224;
                                    if (i8 == 0) {
                                        this.f16381w = (b8 >> 3) & 1;
                                    }
                                    if (this.f16381w == this.f16369k) {
                                        if (i8 == 0) {
                                            int i9 = b8 & 247;
                                            if (i9 == 17 && (b9 & 240) == 48) {
                                                this.f16372n.a((char) D[b9 & 15]);
                                            } else {
                                                int i10 = b8 & 246;
                                                if (i10 == 18 && (b9 & 224) == 32) {
                                                    this.f16372n.b();
                                                    this.f16372n.a((char) ((b8 & 1) == 0 ? f16363E[b9 & 31] : f16364F[b9 & 31]));
                                                } else if (i9 == 17 && (b9 & 240) == 32) {
                                                    this.f16372n.a(' ');
                                                    boolean z11 = (b9 & 1) == 1;
                                                    C1486b c1486b = this.f16372n;
                                                    c1486b.f16352a.add(new C1485a((b9 >> 1) & 7, c1486b.f16354c.length(), z11));
                                                } else if ((b8 & 240) == 16 && (b9 & 192) == 64) {
                                                    int i11 = f16366z[b8 & 7];
                                                    if ((b9 & 32) != 0) {
                                                        i11++;
                                                    }
                                                    C1486b c1486b2 = this.f16372n;
                                                    if (i11 != c1486b2.f16355d) {
                                                        if (this.f16375q != 1 && !c1486b2.e()) {
                                                            C1486b c1486b3 = new C1486b(this.f16375q, this.f16376r);
                                                            this.f16372n = c1486b3;
                                                            this.f16371m.add(c1486b3);
                                                        }
                                                        this.f16372n.f16355d = i11;
                                                    }
                                                    boolean z12 = (b9 & 16) == 16;
                                                    boolean z13 = (b9 & 1) == 1;
                                                    int i12 = (b9 >> 1) & 7;
                                                    C1486b c1486b4 = this.f16372n;
                                                    c1486b4.f16352a.add(new C1485a(z12 ? 8 : i12, c1486b4.f16354c.length(), z13));
                                                    if (z12) {
                                                        this.f16372n.f16356e = f16360A[i12];
                                                    }
                                                } else if (i9 == 23 && b9 >= 33 && b9 <= 35) {
                                                    this.f16372n.f16357f = b9 - 32;
                                                } else if (i10 == 20 && (b9 & 240) == 32) {
                                                    if (b9 == 32) {
                                                        m(2);
                                                    } else if (b9 != 41) {
                                                        switch (b9) {
                                                            case 37:
                                                                m(1);
                                                                this.f16376r = 2;
                                                                this.f16372n.f16359h = 2;
                                                                break;
                                                            case 38:
                                                                m(1);
                                                                this.f16376r = 3;
                                                                this.f16372n.f16359h = 3;
                                                                break;
                                                            case 39:
                                                                m(1);
                                                                this.f16376r = 4;
                                                                this.f16372n.f16359h = 4;
                                                                break;
                                                            default:
                                                                int i13 = this.f16375q;
                                                                if (i13 != 0) {
                                                                    if (b9 != 33) {
                                                                        switch (b9) {
                                                                            case 44:
                                                                                this.f16373o = Collections.EMPTY_LIST;
                                                                                if (i13 == 1 || i13 == 3) {
                                                                                    l();
                                                                                }
                                                                                break;
                                                                            case 45:
                                                                                if (i13 == 1 && !this.f16372n.e()) {
                                                                                    C1486b c1486b5 = this.f16372n;
                                                                                    ArrayList arrayList = c1486b5.f16353b;
                                                                                    arrayList.add(c1486b5.d());
                                                                                    c1486b5.f16354c.setLength(0);
                                                                                    c1486b5.f16352a.clear();
                                                                                    int iMin = Math.min(c1486b5.f16359h, c1486b5.f16355d);
                                                                                    while (arrayList.size() >= iMin) {
                                                                                        arrayList.remove(0);
                                                                                    }
                                                                                }
                                                                                break;
                                                                            case 46:
                                                                                l();
                                                                                break;
                                                                            case 47:
                                                                                this.f16373o = k();
                                                                                l();
                                                                                break;
                                                                        }
                                                                    } else {
                                                                        this.f16372n.b();
                                                                        break;
                                                                    }
                                                                }
                                                                break;
                                                        }
                                                    } else {
                                                        m(3);
                                                    }
                                                }
                                            }
                                        } else {
                                            C1486b c1486b6 = this.f16372n;
                                            int[] iArr = f16362C;
                                            c1486b6.a((char) iArr[(b8 & 127) - 32]);
                                            if ((b9 & 224) != 0) {
                                                this.f16372n.a((char) iArr[(b9 & 127) - 32]);
                                            }
                                        }
                                        z8 = true;
                                    }
                                }
                            } else if (z9) {
                                l();
                                z8 = true;
                            }
                        } else if (this.f16378t && this.f16379u == b8 && this.f16380v == b9) {
                            this.f16378t = false;
                        } else {
                            this.f16378t = true;
                            this.f16379u = b8;
                            this.f16380v = b9;
                            if (!z10) {
                            }
                        }
                    }
                }
            }
        }
    }

    @Override // r1.h, A0.e
    /* JADX INFO: renamed from: i */
    public final C1446c c() {
        C1446c c1446c;
        C1446c c1446cC = super.c();
        if (c1446cC != null) {
            return c1446cC;
        }
        long j8 = this.f16370l;
        if (j8 == -9223372036854775807L) {
            return null;
        }
        long j9 = this.f16383y;
        if (j9 == -9223372036854775807L || this.f16430e - j9 < j8 || (c1446c = (C1446c) this.f16427b.pollFirst()) == null) {
            return null;
        }
        this.f16373o = Collections.EMPTY_LIST;
        this.f16383y = -9223372036854775807L;
        j jVarG = g();
        long j10 = this.f16430e;
        c1446c.f23s = j10;
        c1446c.f16202u = jVarG;
        c1446c.f16203v = j10;
        return c1446c;
    }

    @Override // r1.h
    public final boolean j() {
        return this.f16373o != this.f16374p;
    }

    public final ArrayList k() {
        ArrayList arrayList = this.f16371m;
        int size = arrayList.size();
        ArrayList arrayList2 = new ArrayList(size);
        int iMin = 2;
        for (int i = 0; i < size; i++) {
            w0.b bVarC = ((C1486b) arrayList.get(i)).c(Integer.MIN_VALUE);
            arrayList2.add(bVarC);
            if (bVarC != null) {
                iMin = Math.min(iMin, bVarC.i);
            }
        }
        ArrayList arrayList3 = new ArrayList(size);
        for (int i5 = 0; i5 < size; i5++) {
            w0.b bVarC2 = (w0.b) arrayList2.get(i5);
            if (bVarC2 != null) {
                if (bVarC2.i != iMin) {
                    bVarC2 = ((C1486b) arrayList.get(i5)).c(iMin);
                    bVarC2.getClass();
                }
                arrayList3.add(bVarC2);
            }
        }
        return arrayList3;
    }

    public final void l() {
        C1486b c1486b = this.f16372n;
        c1486b.f16358g = this.f16375q;
        c1486b.f16352a.clear();
        c1486b.f16353b.clear();
        c1486b.f16354c.setLength(0);
        c1486b.f16355d = 15;
        c1486b.f16356e = 0;
        c1486b.f16357f = 0;
        ArrayList arrayList = this.f16371m;
        arrayList.clear();
        arrayList.add(this.f16372n);
    }

    public final void m(int i) {
        int i5 = this.f16375q;
        if (i5 == i) {
            return;
        }
        this.f16375q = i;
        if (i != 3) {
            l();
            if (i5 == 3 || i == 1 || i == 0) {
                this.f16373o = Collections.EMPTY_LIST;
                return;
            }
            return;
        }
        int i8 = 0;
        while (true) {
            ArrayList arrayList = this.f16371m;
            if (i8 >= arrayList.size()) {
                return;
            }
            ((C1486b) arrayList.get(i8)).f16358g = i;
            i8++;
        }
    }

    @Override // r1.h, A0.e
    public final void a() {
    }
}
