package k2;

import androidx.annotation.Nullable;
import androidx.core.view.n;
import androidx.media3.common.DataReader;
import androidx.media3.common.Format;
import androidx.media3.common.Metadata;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.LongArray;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor;
import androidx.media3.extractor.b0;
import androidx.media3.extractor.m0;
import androidx.media3.extractor.o0;
import androidx.media3.extractor.p0;
import androidx.media3.extractor.q0;
import androidx.media3.extractor.r0;
import androidx.media3.extractor.v;
import androidx.media3.extractor.y;
import androidx.media3.extractor.z;
import androidx.media3.extractor.z0;
import f2.m;
import f2.o;
import java.io.EOFException;
import java.io.IOException;
import java.math.RoundingMode;

/* JADX INFO: loaded from: classes.dex */
public final class d implements y {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final n f29024u = new n(18);

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ParsableByteArray f29027c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final q0 f29028d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final m0 f29029e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final o0 f29030f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final v f29031g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public b0 f29032h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public z0 f29033i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public z0 f29034j;

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

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

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

    @Nullable
    private Metadata metadata;

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

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

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

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

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

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

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

    public d() {
        this(-9223372036854775807L);
    }

    @Nullable
    private f getConstantBitrateSeeker(long j10, h hVar, long j11) {
        long j12;
        long j13;
        long jA = hVar.a();
        if (jA == -9223372036854775807L) {
            return null;
        }
        long j14 = hVar.f29053c;
        q0 q0Var = hVar.f29051a;
        if (j14 != -1) {
            long j15 = j10 + j14;
            j12 = j14 - ((long) q0Var.f3451b);
            j13 = j15;
        } else {
            if (j11 == -1) {
                return null;
            }
            j12 = (j11 - j10) - ((long) q0Var.f3451b);
            j13 = j11;
        }
        long j16 = j12;
        RoundingMode roundingMode = RoundingMode.HALF_UP;
        return new a(com.google.common.primitives.a.c(Util.scaleLargeValue(j16, 8000000L, jA, roundingMode)), com.google.common.primitives.a.c(com.bumptech.glide.i.b(j16, hVar.f29052b, roundingMode)), j13, j10 + ((long) q0Var.f3451b), false);
    }

    /* JADX WARN: Multi-variable type inference failed */
    private static long getId3TlenUs(@Nullable Metadata metadata) {
        if (metadata == null) {
            return -9223372036854775807L;
        }
        int length = metadata.length();
        for (int i10 = 0; i10 < length; i10++) {
            Metadata.Entry entry = metadata.get(i10);
            if (entry instanceof o) {
                o oVar = (o) entry;
                if (oVar.f25921a.equals("TLEN")) {
                    return Util.msToUs(Long.parseLong((String) oVar.f25933b.get(0)));
                }
            }
        }
        return -9223372036854775807L;
    }

    @Nullable
    private static c maybeHandleSeekMetadata(@Nullable Metadata metadata, long j10) {
        if (metadata == null) {
            return null;
        }
        int length = metadata.length();
        for (int i10 = 0; i10 < length; i10++) {
            Metadata.Entry entry = metadata.get(i10);
            if (entry instanceof m) {
                m mVar = (m) entry;
                long id3TlenUs = getId3TlenUs(metadata);
                int length2 = mVar.f25929e.length;
                int i11 = length2 + 1;
                long[] jArr = new long[i11];
                long[] jArr2 = new long[i11];
                jArr[0] = j10;
                long j11 = 0;
                jArr2[0] = 0;
                for (int i12 = 1; i12 <= length2; i12++) {
                    int i13 = i12 - 1;
                    j10 += (long) (mVar.f25927c + mVar.f25929e[i13]);
                    j11 += (long) (mVar.f25928d + mVar.f25930f[i13]);
                    jArr[i12] = j10;
                    jArr2[i12] = j11;
                }
                return new c(id3TlenUs, jArr, jArr2);
            }
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0048  */
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private f maybeReadSeekFrame(z zVar) throws IOException {
        int i10;
        int i11;
        q0 q0Var = this.f29028d;
        ParsableByteArray parsableByteArray = new ParsableByteArray(q0Var.f3451b);
        int i12 = 0;
        zVar.a(0, q0Var.f3451b, parsableByteArray.getData());
        int i13 = 21;
        if ((q0Var.f3450a & 1) != 0) {
            if (q0Var.f3453d != 1) {
                i13 = 36;
            }
        } else if (q0Var.f3453d == 1) {
            i13 = 13;
        }
        if (parsableByteArray.limit() >= i13 + 4) {
            parsableByteArray.setPosition(i13);
            int i14 = parsableByteArray.readInt();
            if (i14 == 1483304551 || i14 == 1231971951) {
                i12 = i14;
            } else if (parsableByteArray.limit() >= 40) {
                parsableByteArray.setPosition(36);
                if (parsableByteArray.readInt() == 1447187017) {
                    i12 = 1447187017;
                }
            }
        }
        if (i12 != 1231971951) {
            if (i12 == 1447187017) {
                g gVarCreate = g.create(zVar.getLength(), zVar.getPosition(), this.f29028d, parsableByteArray);
                zVar.k(q0Var.f3451b);
                return gVarCreate;
            }
            if (i12 != 1483304551) {
                zVar.j();
                return null;
            }
        }
        h hVarB = h.b(q0Var, parsableByteArray);
        m0 m0Var = this.f29029e;
        if ((m0Var.f3414a == -1 || m0Var.f3415b == -1) && (i10 = hVarB.f29054d) != -1 && (i11 = hVarB.f29055e) != -1) {
            m0Var.f3414a = i10;
            m0Var.f3415b = i11;
        }
        long position = zVar.getPosition();
        if (zVar.getLength() != -1) {
            long j10 = hVarB.f29053c;
            if (j10 != -1) {
                long j11 = j10 + position;
                if (zVar.getLength() != j11) {
                    Log.i("Mp3Extractor", "Data size mismatch between stream (" + zVar.getLength() + ") and Xing frame (" + j11 + "), using Xing value.");
                }
            }
        }
        zVar.k(q0Var.f3451b);
        return i12 == 1483304551 ? i.create(hVarB, position) : getConstantBitrateSeeker(position, hVarB, zVar.getLength());
    }

    public final void a() {
        f fVar = this.f29041q;
        if ((fVar instanceof a) && fVar.isSeekable()) {
            long j10 = this.f29039o;
            if (j10 == -1 || j10 == this.f29041q.a()) {
                return;
            }
            a aVar = (a) this.f29041q;
            this.f29041q = new a(aVar.f29013i, aVar.f29014j, this.f29039o, aVar.f29012h, aVar.f29015k);
            ((b0) Assertions.checkNotNull(this.f29032h)).seekMap(this.f29041q);
            ((z0) Assertions.checkNotNull(this.f29033i)).durationUs(this.f29041q.getDurationUs());
        }
    }

    public final boolean b(z zVar) {
        f fVar = this.f29041q;
        if (fVar != null) {
            long jA = fVar.a();
            if (jA != -1 && zVar.g() > jA - 4) {
                return true;
            }
        }
        try {
            return !zVar.f(this.f29027c.getData(), 0, 4, true);
        } catch (EOFException unused) {
            return true;
        }
    }

    public final boolean c(z zVar, boolean z10) throws IOException {
        int iG;
        int i10;
        int iB;
        int i11 = z10 ? 32768 : 131072;
        zVar.j();
        if (zVar.getPosition() == 0) {
            Metadata metadataPeekId3Data = this.f29030f.peekId3Data(zVar, (this.f29025a & 8) == 0 ? null : f29024u);
            this.metadata = metadataPeekId3Data;
            if (metadataPeekId3Data != null) {
                this.f29029e.b(metadataPeekId3Data);
            }
            iG = (int) zVar.g();
            if (!z10) {
                zVar.k(iG);
            }
            i10 = 0;
        } else {
            iG = 0;
            i10 = 0;
        }
        int i12 = i10;
        int i13 = i12;
        while (true) {
            if (!b(zVar)) {
                ParsableByteArray parsableByteArray = this.f29027c;
                parsableByteArray.setPosition(0);
                int i14 = parsableByteArray.readInt();
                if ((i10 == 0 || ((-128000) & i14) == (((long) i10) & (-128000))) && (iB = androidx.media3.extractor.b.b(i14)) != -1) {
                    i12++;
                    if (i12 != 1) {
                        if (i12 == 4) {
                            break;
                        }
                    } else {
                        this.f29028d.a(i14);
                        i10 = i14;
                    }
                    zVar.h(iB - 4);
                } else {
                    int i15 = i13 + 1;
                    if (i13 == i11) {
                        if (z10) {
                            return false;
                        }
                        a();
                        throw new EOFException();
                    }
                    if (z10) {
                        zVar.j();
                        zVar.h(iG + i15);
                    } else {
                        zVar.k(1);
                    }
                    i12 = 0;
                    i13 = i15;
                    i10 = 0;
                }
            } else if (i12 <= 0) {
                a();
                throw new EOFException();
            }
        }
        if (z10) {
            zVar.k(iG + i13);
        } else {
            zVar.j();
        }
        this.f29035k = i10;
        return true;
    }

    @Override // androidx.media3.extractor.y
    public final void init(b0 b0Var) {
        this.f29032h = b0Var;
        z0 z0VarTrack = b0Var.track(0, 1);
        this.f29033i = z0VarTrack;
        this.f29034j = z0VarTrack;
        this.f29032h.endTracks();
    }

    /* JADX WARN: Removed duplicated region for block: B:104:0x027b  */
    /* JADX WARN: Removed duplicated region for block: B:107:0x0287  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x0197  */
    /* JADX WARN: Removed duplicated region for block: B:6:0x001a  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x01b0  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x002b  */
    @Override // androidx.media3.extractor.y
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int read(z zVar, r0 r0Var) throws IOException {
        f fVar;
        ParsableByteArray parsableByteArray;
        int iSampleData;
        long id3TlenUs;
        long jA;
        long j10;
        Assertions.checkStateNotNull(this.f29033i);
        Util.castNonNull(this.f29032h);
        int i10 = this.f29035k;
        q0 q0Var = this.f29028d;
        int i11 = 0;
        if (i10 == 0) {
            try {
                c(zVar, false);
                fVar = this.f29041q;
                parsableByteArray = this.f29027c;
                if (fVar != null) {
                    f fVarMaybeReadSeekFrame = maybeReadSeekFrame(zVar);
                    c cVarMaybeHandleSeekMetadata = maybeHandleSeekMetadata(this.metadata, zVar.getPosition());
                    boolean z10 = this.f29042r;
                    int i12 = this.f29025a;
                    if (z10) {
                        fVarMaybeReadSeekFrame = new e(-9223372036854775807L);
                    } else {
                        if ((i12 & 4) != 0) {
                            if (cVarMaybeHandleSeekMetadata != null) {
                                id3TlenUs = cVarMaybeHandleSeekMetadata.f29023c;
                            } else if (fVarMaybeReadSeekFrame != null) {
                                long durationUs = fVarMaybeReadSeekFrame.getDurationUs();
                                jA = fVarMaybeReadSeekFrame.a();
                                j10 = durationUs;
                                fVarMaybeReadSeekFrame = new b(j10, zVar.getPosition(), jA);
                            } else {
                                id3TlenUs = getId3TlenUs(this.metadata);
                            }
                            j10 = id3TlenUs;
                            jA = -1;
                            fVarMaybeReadSeekFrame = new b(j10, zVar.getPosition(), jA);
                        } else if (cVarMaybeHandleSeekMetadata != null) {
                            fVarMaybeReadSeekFrame = cVarMaybeHandleSeekMetadata;
                        } else if (fVarMaybeReadSeekFrame == null) {
                            fVarMaybeReadSeekFrame = null;
                        }
                        if (fVarMaybeReadSeekFrame != null && !fVarMaybeReadSeekFrame.isSeekable() && (i12 & 1) != 0 && fVarMaybeReadSeekFrame.getDurationUs() != -9223372036854775807L && (fVarMaybeReadSeekFrame.a() != -1 || zVar.getLength() != -1)) {
                            long jB = fVarMaybeReadSeekFrame.b() != -1 ? fVarMaybeReadSeekFrame.b() : 0L;
                            long jA2 = fVarMaybeReadSeekFrame.a() != -1 ? fVarMaybeReadSeekFrame.a() : zVar.getLength();
                            fVarMaybeReadSeekFrame = new a(com.google.common.primitives.a.h(Util.scaleLargeValue(jA2 - jB, 8000000L, fVarMaybeReadSeekFrame.getDurationUs(), RoundingMode.HALF_UP)), -1, jA2, jB, false);
                        } else if (fVarMaybeReadSeekFrame == null || (!fVarMaybeReadSeekFrame.isSeekable() && (i12 & 1) != 0)) {
                            boolean z11 = (i12 & 2) != 0;
                            zVar.a(0, 4, parsableByteArray.getData());
                            parsableByteArray.setPosition(0);
                            q0Var.a(parsableByteArray.readInt());
                            fVarMaybeReadSeekFrame = new a(q0Var.f3454e, q0Var.f3451b, zVar.getLength(), zVar.getPosition(), z11);
                        }
                        this.f29033i.durationUs(fVarMaybeReadSeekFrame.getDurationUs());
                    }
                    this.f29041q = fVarMaybeReadSeekFrame;
                    this.f29032h.seekMap(fVarMaybeReadSeekFrame);
                    Format.Builder sampleRate = new Format.Builder().setContainerMimeType(MimeTypes.AUDIO_MPEG).setSampleMimeType(q0Var.mimeType).setMaxInputSize(4096).setChannelCount(q0Var.f3453d).setSampleRate(q0Var.f3452c);
                    m0 m0Var = this.f29029e;
                    Format.Builder metadata = sampleRate.setEncoderDelay(m0Var.f3414a).setEncoderPadding(m0Var.f3415b).setMetadata((i12 & 8) != 0 ? null : this.metadata);
                    if (this.f29041q.c() != -2147483647) {
                        metadata.setAverageBitrate(this.f29041q.c());
                    }
                    this.f29034j.format(metadata.build());
                    this.f29038n = zVar.getPosition();
                } else if (this.f29038n != 0) {
                    long position = zVar.getPosition();
                    long j11 = this.f29038n;
                    if (position < j11) {
                        zVar.k((int) (j11 - position));
                    }
                }
            } catch (EOFException unused) {
            }
            if (this.f29040p != 0) {
                zVar.j();
                if (b(zVar)) {
                    i11 = -1;
                } else {
                    parsableByteArray.setPosition(0);
                    int i13 = parsableByteArray.readInt();
                    if (((-128000) & i13) != (((long) this.f29035k) & (-128000)) || androidx.media3.extractor.b.b(i13) == -1) {
                        zVar.k(1);
                        this.f29035k = 0;
                    } else {
                        q0Var.a(i13);
                        if (this.f29036l == -9223372036854775807L) {
                            this.f29036l = this.f29041q.getTimeUs(zVar.getPosition());
                            long j12 = this.f29026b;
                            if (j12 != -9223372036854775807L) {
                                this.f29036l = (j12 - this.f29041q.getTimeUs(0L)) + this.f29036l;
                            }
                        }
                        this.f29040p = q0Var.f3451b;
                        long position2 = zVar.getPosition() + ((long) q0Var.f3451b);
                        this.f29039o = position2;
                        f fVar2 = this.f29041q;
                        if (fVar2 instanceof b) {
                            b bVar = (b) fVar2;
                            long j13 = (((this.f29037m + ((long) q0Var.f3455f)) * 1000000) / ((long) q0Var.f3452c)) + this.f29036l;
                            LongArray longArray = bVar.f29020d.f3441b;
                            int size = longArray.size();
                            p0 p0Var = bVar.f29020d;
                            if (size == 0 || j13 - longArray.get(longArray.size() - 1) >= SilenceSkippingAudioProcessor.DEFAULT_MINIMUM_SILENCE_DURATION_US) {
                                p0Var.d(j13, position2);
                            }
                            if (this.f29043s) {
                                long j14 = this.f29044t;
                                LongArray longArray2 = p0Var.f3441b;
                                if (longArray2.size() != 0 && j14 - longArray2.get(longArray2.size() - 1) < SilenceSkippingAudioProcessor.DEFAULT_MINIMUM_SILENCE_DURATION_US) {
                                    this.f29043s = false;
                                    this.f29034j = this.f29033i;
                                }
                            }
                        }
                        iSampleData = this.f29034j.sampleData((DataReader) zVar, this.f29040p, true);
                        if (iSampleData == -1) {
                            int i14 = this.f29040p - iSampleData;
                            this.f29040p = i14;
                            if (i14 <= 0) {
                                this.f29034j.sampleMetadata(((this.f29037m * 1000000) / ((long) q0Var.f3452c)) + this.f29036l, 1, q0Var.f3451b, 0, null);
                                this.f29037m += (long) q0Var.f3455f;
                                this.f29040p = 0;
                            }
                        }
                    }
                }
            } else {
                iSampleData = this.f29034j.sampleData((DataReader) zVar, this.f29040p, true);
                if (iSampleData == -1) {
                }
            }
        } else {
            fVar = this.f29041q;
            parsableByteArray = this.f29027c;
            if (fVar != null) {
            }
            if (this.f29040p != 0) {
            }
        }
        if (i11 == -1) {
            f fVar3 = this.f29041q;
            if (fVar3 instanceof b) {
                long j15 = ((this.f29037m * 1000000) / ((long) q0Var.f3452c)) + this.f29036l;
                if (fVar3.getDurationUs() != j15) {
                    f fVar4 = this.f29041q;
                    ((b) fVar4).f29020d.f3442c = j15;
                    this.f29032h.seekMap(fVar4);
                    this.f29033i.durationUs(this.f29041q.getDurationUs());
                }
            }
        }
        return i11;
    }

    @Override // androidx.media3.extractor.y
    public final void release() {
    }

    @Override // androidx.media3.extractor.y
    public final void seek(long j10, long j11) {
        this.f29035k = 0;
        this.f29036l = -9223372036854775807L;
        this.f29037m = 0L;
        this.f29040p = 0;
        this.f29044t = j11;
        f fVar = this.f29041q;
        if (fVar instanceof b) {
            LongArray longArray = ((b) fVar).f29020d.f3441b;
            if (longArray.size() != 0 && j11 - longArray.get(longArray.size() - 1) < SilenceSkippingAudioProcessor.DEFAULT_MINIMUM_SILENCE_DURATION_US) {
                return;
            }
            this.f29043s = true;
            this.f29034j = this.f29031g;
        }
    }

    @Override // androidx.media3.extractor.y
    public final boolean sniff(z zVar) {
        return c(zVar, true);
    }

    public d(long j10) {
        this.f29025a = 0;
        this.f29026b = j10;
        this.f29027c = new ParsableByteArray(10);
        this.f29028d = new q0();
        this.f29029e = new m0();
        this.f29036l = -9223372036854775807L;
        this.f29030f = new o0();
        v vVar = new v();
        this.f29031g = vVar;
        this.f29034j = vVar;
        this.f29039o = -1L;
    }
}
