package o2;

import androidx.annotation.Nullable;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.text.Cue;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.extractor.text.SubtitleDecoderException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import n2.l;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes.dex */
public final class c extends j {

    @Nullable
    private List<Cue> cues;

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

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

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

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

    @Nullable
    private List<Cue> lastCues;

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

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public static final int[] f31303z = {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[] A = {174, 176, 189, 191, 8482, 162, 163, 9834, 224, 32, 232, 226, 234, 238, IjkMediaMeta.FF_PROFILE_H264_HIGH_444_PREDICTIVE, 251};
    public static final int[] B = {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};
    public static final int[] C = {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};
    public static final boolean[] D = {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: g, reason: collision with root package name */
    public final ParsableByteArray f31304g = new ParsableByteArray();

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public b f31310m = new b(0, 4);

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

    public c(int i10, String str) {
        Assertions.checkArgument(true);
        this.f31308k = 16000000L;
        this.f31305h = MimeTypes.APPLICATION_MP4CEA608.equals(str) ? 2 : 3;
        if (i10 == 1) {
            this.f31307j = 0;
            this.f31306i = 0;
        } else if (i10 == 2) {
            this.f31307j = 1;
            this.f31306i = 0;
        } else if (i10 == 3) {
            this.f31307j = 0;
            this.f31306i = 1;
        } else if (i10 != 4) {
            Log.w("Cea608Decoder", "Invalid channel. Defaulting to CC1.");
            this.f31307j = 0;
            this.f31306i = 0;
        } else {
            this.f31307j = 1;
            this.f31306i = 1;
        }
        f(0);
        e();
        this.f31318u = true;
        this.f31319v = -9223372036854775807L;
    }

    @Override // o2.j
    public final k a() {
        List<Cue> list = this.cues;
        this.lastCues = list;
        return new k((List) Assertions.checkNotNull(list));
    }

    /* JADX WARN: Removed duplicated region for block: B:179:0x0088 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:181:0x0081 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:26:0x005c  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x00a9 A[FALL_THROUGH] */
    @Override // o2.j
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void b(h hVar) {
        ByteBuffer byteBuffer = (ByteBuffer) Assertions.checkNotNull(hVar.data);
        byte[] bArrArray = byteBuffer.array();
        int iLimit = byteBuffer.limit();
        ParsableByteArray parsableByteArray = this.f31304g;
        parsableByteArray.reset(bArrArray, iLimit);
        boolean z10 = false;
        while (true) {
            int iBytesLeft = parsableByteArray.bytesLeft();
            int i10 = this.f31305h;
            if (iBytesLeft < i10) {
                if (z10) {
                    int i11 = this.f31311n;
                    if (i11 == 1 || i11 == 3) {
                        this.cues = d();
                        this.f31319v = this.f31365d;
                        return;
                    }
                    return;
                }
                return;
            }
            int unsignedByte = i10 == 2 ? -4 : parsableByteArray.readUnsignedByte();
            int unsignedByte2 = parsableByteArray.readUnsignedByte();
            int unsignedByte3 = parsableByteArray.readUnsignedByte();
            if ((unsignedByte & 2) == 0 && (unsignedByte & 1) == this.f31306i) {
                byte b10 = (byte) (unsignedByte2 & 127);
                byte b11 = (byte) (unsignedByte3 & 127);
                if (b10 != 0 || b11 != 0) {
                    boolean z11 = this.f31313p;
                    if ((unsignedByte & 4) == 4) {
                        boolean[] zArr = D;
                        boolean z12 = zArr[unsignedByte2] && zArr[unsignedByte3];
                        this.f31313p = z12;
                        if (!z12 || (b10 & 240) != 16) {
                            this.f31314q = false;
                            if (!z12) {
                                if (1 <= b10 && b10 <= 15) {
                                    this.f31318u = false;
                                } else if ((b10 & 246) == 20) {
                                    if (b11 != 32 && b11 != 47) {
                                        switch (b11) {
                                            default:
                                                switch (b11) {
                                                    case 42:
                                                    case 43:
                                                        this.f31318u = false;
                                                        break;
                                                }
                                            case 37:
                                            case 38:
                                            case 39:
                                                break;
                                        }
                                    } else {
                                        this.f31318u = true;
                                    }
                                }
                                if (this.f31318u) {
                                    int i12 = b10 & 224;
                                    if (i12 == 0) {
                                        this.f31317t = (b10 >> 3) & 1;
                                    }
                                    if (this.f31317t == this.f31307j) {
                                        if (i12 == 0) {
                                            int i13 = b10 & 247;
                                            if (i13 == 17 && (b11 & 240) == 48) {
                                                this.f31310m.a((char) A[b11 & 15]);
                                            } else {
                                                int i14 = b10 & 246;
                                                if (i14 == 18 && (b11 & 224) == 32) {
                                                    this.f31310m.b();
                                                    this.f31310m.a((char) ((b10 & 1) == 0 ? B[b11 & 31] : C[b11 & 31]));
                                                } else if (i13 == 17 && (b11 & 240) == 32) {
                                                    this.f31310m.a(' ');
                                                    boolean z13 = (b11 & 1) == 1;
                                                    b bVar = this.f31310m;
                                                    bVar.f31292a.add(new a((b11 >> 1) & 7, bVar.f31294c.length(), z13));
                                                } else if ((b10 & 240) == 16 && (b11 & 192) == 64) {
                                                    int i15 = f31300w[b10 & 7];
                                                    if ((b11 & 32) != 0) {
                                                        i15++;
                                                    }
                                                    b bVar2 = this.f31310m;
                                                    if (i15 != bVar2.f31295d) {
                                                        if (this.f31311n != 1 && !bVar2.d()) {
                                                            b bVar3 = new b(this.f31311n, this.f31312o);
                                                            this.f31310m = bVar3;
                                                            this.f31309l.add(bVar3);
                                                        }
                                                        this.f31310m.f31295d = i15;
                                                    }
                                                    boolean z14 = (b11 & 16) == 16;
                                                    boolean z15 = (b11 & 1) == 1;
                                                    int i16 = (b11 >> 1) & 7;
                                                    b bVar4 = this.f31310m;
                                                    bVar4.f31292a.add(new a(z14 ? 8 : i16, bVar4.f31294c.length(), z15));
                                                    if (z14) {
                                                        this.f31310m.f31296e = f31301x[i16];
                                                    }
                                                } else if (i13 == 23 && b11 >= 33 && b11 <= 35) {
                                                    this.f31310m.f31297f = b11 - 32;
                                                } else if (i14 == 20 && (b11 & 240) == 32) {
                                                    if (b11 == 32) {
                                                        f(2);
                                                    } else if (b11 != 41) {
                                                        switch (b11) {
                                                            case 37:
                                                                f(1);
                                                                this.f31312o = 2;
                                                                this.f31310m.f31299h = 2;
                                                                break;
                                                            case 38:
                                                                f(1);
                                                                this.f31312o = 3;
                                                                this.f31310m.f31299h = 3;
                                                                break;
                                                            case 39:
                                                                f(1);
                                                                this.f31312o = 4;
                                                                this.f31310m.f31299h = 4;
                                                                break;
                                                            default:
                                                                int i17 = this.f31311n;
                                                                if (i17 != 0) {
                                                                    if (b11 != 33) {
                                                                        switch (b11) {
                                                                            case 44:
                                                                                this.cues = Collections.emptyList();
                                                                                int i18 = this.f31311n;
                                                                                if (i18 == 1 || i18 == 3) {
                                                                                    e();
                                                                                }
                                                                                break;
                                                                            case 45:
                                                                                if (i17 == 1 && !this.f31310m.d()) {
                                                                                    b bVar5 = this.f31310m;
                                                                                    ArrayList arrayList = bVar5.f31293b;
                                                                                    arrayList.add(bVar5.c());
                                                                                    bVar5.f31294c.setLength(0);
                                                                                    bVar5.f31292a.clear();
                                                                                    int iMin = Math.min(bVar5.f31299h, bVar5.f31295d);
                                                                                    while (arrayList.size() >= iMin) {
                                                                                        arrayList.remove(0);
                                                                                    }
                                                                                }
                                                                                break;
                                                                            case 46:
                                                                                e();
                                                                                break;
                                                                            case 47:
                                                                                this.cues = d();
                                                                                e();
                                                                                break;
                                                                        }
                                                                    } else {
                                                                        this.f31310m.b();
                                                                        break;
                                                                    }
                                                                }
                                                                break;
                                                        }
                                                    } else {
                                                        f(3);
                                                    }
                                                }
                                            }
                                        } else {
                                            b bVar6 = this.f31310m;
                                            int[] iArr = f31303z;
                                            bVar6.a((char) iArr[(b10 & 127) - 32]);
                                            if ((b11 & 224) != 0) {
                                                this.f31310m.a((char) iArr[(b11 & 127) - 32]);
                                            }
                                        }
                                        z10 = true;
                                    }
                                }
                            } else if (z11) {
                                e();
                                z10 = true;
                            }
                        } else if (this.f31314q && this.f31315r == b10 && this.f31316s == b11) {
                            this.f31314q = false;
                        } else {
                            this.f31314q = true;
                            this.f31315r = b10;
                            this.f31316s = b11;
                            if (!z12) {
                            }
                        }
                    }
                }
            }
        }
    }

    @Override // o2.j
    public final boolean c() {
        return this.cues != this.lastCues;
    }

    public final ArrayList d() {
        ArrayList arrayList = this.f31309l;
        int size = arrayList.size();
        ArrayList arrayList2 = new ArrayList(size);
        int iMin = 2;
        for (int i10 = 0; i10 < size; i10++) {
            Cue cueBuild = ((b) arrayList.get(i10)).build(Integer.MIN_VALUE);
            arrayList2.add(cueBuild);
            if (cueBuild != null) {
                iMin = Math.min(iMin, cueBuild.positionAnchor);
            }
        }
        ArrayList arrayList3 = new ArrayList(size);
        for (int i11 = 0; i11 < size; i11++) {
            Cue cue = (Cue) arrayList2.get(i11);
            if (cue != null) {
                if (cue.positionAnchor != iMin) {
                    cue = (Cue) Assertions.checkNotNull(((b) arrayList.get(i11)).build(iMin));
                }
                arrayList3.add(cue);
            }
        }
        return arrayList3;
    }

    @Override // o2.j, n2.h, s1.e
    @Nullable
    public /* bridge */ /* synthetic */ n2.k dequeueInputBuffer() throws SubtitleDecoderException {
        return super.dequeueInputBuffer();
    }

    public final void e() {
        b bVar = this.f31310m;
        bVar.f31298g = this.f31311n;
        bVar.f31292a.clear();
        bVar.f31293b.clear();
        bVar.f31294c.setLength(0);
        bVar.f31295d = 15;
        bVar.f31296e = 0;
        bVar.f31297f = 0;
        ArrayList arrayList = this.f31309l;
        arrayList.clear();
        arrayList.add(this.f31310m);
    }

    public final void f(int i10) {
        int i11 = this.f31311n;
        if (i11 == i10) {
            return;
        }
        this.f31311n = i10;
        if (i10 != 3) {
            e();
            if (i11 == 3 || i10 == 1 || i10 == 0) {
                this.cues = Collections.emptyList();
                return;
            }
            return;
        }
        int i12 = 0;
        while (true) {
            ArrayList arrayList = this.f31309l;
            if (i12 >= arrayList.size()) {
                return;
            }
            ((b) arrayList.get(i12)).f31298g = i10;
            i12++;
        }
    }

    @Override // o2.j, s1.e
    public final void flush() {
        super.flush();
        this.cues = null;
        this.lastCues = null;
        f(0);
        this.f31312o = 4;
        this.f31310m.f31299h = 4;
        e();
        this.f31313p = false;
        this.f31314q = false;
        this.f31315r = (byte) 0;
        this.f31316s = (byte) 0;
        this.f31317t = 0;
        this.f31318u = true;
        this.f31319v = -9223372036854775807L;
    }

    @Override // s1.e, androidx.media3.exoplayer.image.ImageDecoder
    public final String getName() {
        return "Cea608Decoder";
    }

    @Override // o2.j, s1.e
    public final void release() {
    }

    @Override // o2.j, n2.h, s1.e, androidx.media3.exoplayer.image.ImageDecoder
    @Nullable
    public l dequeueOutputBuffer() throws SubtitleDecoderException {
        l availableOutputBuffer;
        l lVarDequeueOutputBuffer = super.dequeueOutputBuffer();
        if (lVarDequeueOutputBuffer != null) {
            return lVarDequeueOutputBuffer;
        }
        long j10 = this.f31308k;
        if (j10 == -9223372036854775807L) {
            return null;
        }
        long j11 = this.f31319v;
        if (j11 == -9223372036854775807L || this.f31365d - j11 < j10 || (availableOutputBuffer = getAvailableOutputBuffer()) == null) {
            return null;
        }
        this.cues = Collections.emptyList();
        this.f31319v = -9223372036854775807L;
        availableOutputBuffer.e(this.f31365d, a(), Long.MAX_VALUE);
        return availableOutputBuffer;
    }
}
