package com.applovin.impl; import android.support.v4.media.session.PlaybackStateCompat; import android.util.Pair; import com.applovin.exoplayer2.common.base.Function; import com.applovin.impl.f9; import com.applovin.impl.ij; import com.applovin.impl.j1; import com.mbridge.msdk.playercommon.exoplayer2.C; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; /* loaded from: classes.dex */ public final class pf implements k8, ij { public static final o8 y = new o8() { // from class: com.applovin.impl.pf$$ExternalSyntheticLambda1 @Override // com.applovin.impl.o8 public final k8[] a() { k8[] f; f = pf.f(); return f; } }; private final int a; private final bh b; private final bh c; private final bh d; private final bh e; private final ArrayDeque f; private final mj g; private final List h; private int i; private int j; private long k; private int l; private bh m; private int n; private int o; private int p; private int q; private m8 r; private a[] s; private long[][] t; private int u; private long v; private int w; private nf x; private static int a(int i) { if (i != 1751476579) { return i != 1903435808 ? 0 : 1; } return 2; } /* JADX INFO: Access modifiers changed from: private */ public static /* synthetic */ lo a(lo loVar) { return loVar; } private static boolean b(int i) { return i == 1836019574 || i == 1953653099 || i == 1835297121 || i == 1835626086 || i == 1937007212 || i == 1701082227 || i == 1835365473; } private static boolean c(int i) { return i == 1835296868 || i == 1836476516 || i == 1751411826 || i == 1937011556 || i == 1937011827 || i == 1937011571 || i == 1668576371 || i == 1701606260 || i == 1937011555 || i == 1937011578 || i == 1937013298 || i == 1937007471 || i == 1668232756 || i == 1953196132 || i == 1718909296 || i == 1969517665 || i == 1801812339 || i == 1768715124; } private void e() { this.i = 0; this.l = 0; } @Override // com.applovin.impl.k8 public void a() { } @Override // com.applovin.impl.k8 public void a(m8 m8Var) { this.r = m8Var; } @Override // com.applovin.impl.ij public boolean b() { return true; } @Override // com.applovin.impl.ij public long d() { return this.v; } public pf() { this(0); } @Override // com.applovin.impl.ij public ij.a b(long j) { long j2; long j3; long j4; long j5; int b; if (((a[]) b1.a(this.s)).length == 0) { return new ij.a(kj.c); } int i = this.u; if (i != -1) { ro roVar = this.s[i].b; int a2 = a(roVar, j); if (a2 == -1) { return new ij.a(kj.c); } long j6 = roVar.f[a2]; j2 = roVar.c[a2]; if (j6 >= j || a2 >= roVar.b - 1 || (b = roVar.b(j)) == -1 || b == a2) { j5 = -1; j4 = -9223372036854775807L; } else { j4 = roVar.f[b]; j5 = roVar.c[b]; } j3 = j5; j = j6; } else { j2 = Long.MAX_VALUE; j3 = -1; j4 = -9223372036854775807L; } int i2 = 0; while (true) { a[] aVarArr = this.s; if (i2 >= aVarArr.length) { break; } if (i2 != this.u) { ro roVar2 = aVarArr[i2].b; long a3 = a(roVar2, j, j2); if (j4 != C.TIME_UNSET) { j3 = a(roVar2, j4, j3); } j2 = a3; } i2++; } kj kjVar = new kj(j, j2); if (j4 == C.TIME_UNSET) { return new ij.a(kjVar); } return new ij.a(kjVar, new kj(j4, j3)); } public pf(int i) { this.a = i; this.i = (i & 4) != 0 ? 3 : 0; this.g = new mj(); this.h = new ArrayList(); this.e = new bh(16); this.f = new ArrayDeque(); this.b = new bh(zf.a); this.c = new bh(4); this.d = new bh(); this.n = -1; } private int c(long j) { int i = -1; int i2 = -1; long j2 = Long.MAX_VALUE; boolean z = true; long j3 = Long.MAX_VALUE; boolean z2 = true; long j4 = Long.MAX_VALUE; for (int i3 = 0; i3 < ((a[]) xp.a((Object) this.s)).length; i3++) { a aVar = this.s[i3]; int i4 = aVar.d; ro roVar = aVar.b; if (i4 != roVar.b) { long j5 = roVar.c[i4]; long j6 = ((long[][]) xp.a((Object) this.t))[i3][i4]; long j7 = j5 - j; boolean z3 = j7 < 0 || j7 >= PlaybackStateCompat.ACTION_SET_REPEAT_MODE; if ((!z3 && z2) || (z3 == z2 && j7 < j4)) { z2 = z3; j4 = j7; i2 = i3; j3 = j6; } if (j6 < j2) { z = z3; i = i3; j2 = j6; } } } return (j2 == Long.MAX_VALUE || !z || j3 < j2 + 10485760) ? i2 : i; } private void d(long j) { while (!this.f.isEmpty() && ((j1.a) this.f.peek()).b == j) { j1.a aVar = (j1.a) this.f.pop(); if (aVar.a == 1836019574) { a(aVar); this.f.clear(); this.i = 2; } else if (!this.f.isEmpty()) { ((j1.a) this.f.peek()).a(aVar); } } if (this.i != 2) { e(); } } private void f(long j) { for (a aVar : this.s) { ro roVar = aVar.b; int a2 = roVar.a(j); if (a2 == -1) { a2 = roVar.b(j); } aVar.d = a2; } } /* JADX INFO: Access modifiers changed from: private */ public static /* synthetic */ k8[] f() { return new k8[]{new pf()}; } private void g() { if (this.w != 2 || (this.a & 2) == 0) { return; } m8 m8Var = (m8) b1.a(this.r); m8Var.a(0, 4).a(new f9.b().a(this.x == null ? null : new bf(this.x)).a()); m8Var.c(); m8Var.a(new ij.b(C.TIME_UNSET)); } private static long[][] a(a[] aVarArr) { long[][] jArr = new long[aVarArr.length][]; int[] iArr = new int[aVarArr.length]; long[] jArr2 = new long[aVarArr.length]; boolean[] zArr = new boolean[aVarArr.length]; for (int i = 0; i < aVarArr.length; i++) { jArr[i] = new long[aVarArr[i].b.b]; jArr2[i] = aVarArr[i].b.f[0]; } long j = 0; int i2 = 0; while (i2 < aVarArr.length) { long j2 = Long.MAX_VALUE; int i3 = -1; for (int i4 = 0; i4 < aVarArr.length; i4++) { if (!zArr[i4]) { long j3 = jArr2[i4]; if (j3 <= j2) { i3 = i4; j2 = j3; } } } int i5 = iArr[i3]; long[] jArr3 = jArr[i3]; jArr3[i5] = j; ro roVar = aVarArr[i3].b; j += roVar.d[i5]; int i6 = i5 + 1; iArr[i3] = i6; if (i6 < jArr3.length) { jArr2[i3] = roVar.f[i6]; } else { zArr[i3] = true; i2++; } } return jArr; } public static final class a { public final lo a; public final ro b; public final qo c; public int d; public a(lo loVar, ro roVar, qo qoVar) { this.a = loVar; this.b = roVar; this.c = qoVar; } } private boolean c(l8 l8Var) { j1.a aVar; if (this.l == 0) { if (!l8Var.a(this.e.c(), 0, 8, true)) { g(); return false; } this.l = 8; this.e.f(0); this.k = this.e.y(); this.j = this.e.j(); } long j = this.k; if (j == 1) { l8Var.d(this.e.c(), 8, 8); this.l += 8; this.k = this.e.B(); } else if (j == 0) { long a2 = l8Var.a(); if (a2 == -1 && (aVar = (j1.a) this.f.peek()) != null) { a2 = aVar.b; } if (a2 != -1) { this.k = (a2 - l8Var.f()) + this.l; } } if (this.k >= this.l) { if (b(this.j)) { long f = l8Var.f(); long j2 = this.k; long j3 = this.l; long j4 = (f + j2) - j3; if (j2 != j3 && this.j == 1835365473) { b(l8Var); } this.f.push(new j1.a(this.j, j4)); if (this.k == this.l) { d(j4); } else { e(); } } else if (c(this.j)) { b1.b(this.l == 8); b1.b(this.k <= 2147483647L); bh bhVar = new bh((int) this.k); System.arraycopy(this.e.c(), 0, bhVar.c(), 0, 8); this.m = bhVar; this.i = 1; } else { e(l8Var.f() - this.l); this.m = null; this.i = 1; } return true; } throw dh.a("Atom size less than header length (unsupported)."); } private void b(l8 l8Var) { this.d.d(8); l8Var.c(this.d.c(), 0, 8); k1.a(this.d); l8Var.a(this.d.d()); l8Var.b(); } private void e(long j) { if (this.j == 1836086884) { long j2 = this.l; this.x = new nf(0L, j, C.TIME_UNSET, j + j2, this.k - j2); } } private int d(l8 l8Var, th thVar) { int a2 = this.g.a(l8Var, thVar, this.h); if (a2 == 1 && thVar.a == 0) { e(); } return a2; } private boolean b(l8 l8Var, th thVar) { boolean z; long j = this.k - this.l; long f = l8Var.f() + j; bh bhVar = this.m; if (bhVar != null) { l8Var.d(bhVar.c(), this.l, (int) j); if (this.j == 1718909296) { this.w = a(bhVar); } else if (!this.f.isEmpty()) { ((j1.a) this.f.peek()).a(new j1.b(this.j, bhVar)); } } else if (j < PlaybackStateCompat.ACTION_SET_REPEAT_MODE) { l8Var.a((int) j); } else { thVar.a = l8Var.f() + j; z = true; d(f); return (z || this.i == 2) ? false : true; } z = false; d(f); if (z) { } } private static int a(ro roVar, long j) { int a2 = roVar.a(j); return a2 == -1 ? roVar.b(j) : a2; } private int c(l8 l8Var, th thVar) { long f = l8Var.f(); if (this.n == -1) { int c = c(f); this.n = c; if (c == -1) { return -1; } } a aVar = ((a[]) xp.a((Object) this.s))[this.n]; qo qoVar = aVar.c; int i = aVar.d; ro roVar = aVar.b; long j = roVar.c[i]; int i2 = roVar.d[i]; long j2 = (j - f) + this.o; if (j2 >= 0 && j2 < PlaybackStateCompat.ACTION_SET_REPEAT_MODE) { if (aVar.a.g == 1) { j2 += 8; i2 -= 8; } l8Var.a((int) j2); lo loVar = aVar.a; if (loVar.j != 0) { byte[] c2 = this.c.c(); c2[0] = 0; c2[1] = 0; c2[2] = 0; int i3 = aVar.a.j; int i4 = 4 - i3; while (this.p < i2) { int i5 = this.q; if (i5 == 0) { l8Var.d(c2, i4, i3); this.o += i3; this.c.f(0); int j3 = this.c.j(); if (j3 >= 0) { this.q = j3; this.b.f(0); qoVar.a(this.b, 4); this.p += 4; i2 += i4; } else { throw dh.a("Invalid NAL length", null); } } else { int a2 = qoVar.a((g5) l8Var, i5, false); this.o += a2; this.p += a2; this.q -= a2; } } } else { if ("audio/ac4".equals(loVar.f.m)) { if (this.p == 0) { n.a(i2, this.d); qoVar.a(this.d, 7); this.p += 7; } i2 += 7; } while (true) { int i6 = this.p; if (i6 >= i2) { break; } int a3 = qoVar.a((g5) l8Var, i2 - i6, false); this.o += a3; this.p += a3; this.q -= a3; } } int i7 = i2; ro roVar2 = aVar.b; qoVar.a(roVar2.f[i], roVar2.g[i], i7, 0, null); aVar.d++; this.n = -1; this.o = 0; this.p = 0; this.q = 0; return 0; } thVar.a = j; return 1; } private static long a(ro roVar, long j, long j2) { int a2 = a(roVar, j); return a2 == -1 ? j2 : Math.min(roVar.c[a2], j2); } private static int a(bh bhVar) { bhVar.f(8); int a2 = a(bhVar.j()); if (a2 != 0) { return a2; } bhVar.g(4); while (bhVar.a() > 0) { int a3 = a(bhVar.j()); if (a3 != 0) { return a3; } } return 0; } private void a(j1.a aVar) { bf bfVar; bf bfVar2; ArrayList arrayList; List list; int i; bf bfVar3; char c; int i2; int i3; ArrayList arrayList2 = new ArrayList(); boolean z = this.w == 1; z9 z9Var = new z9(); j1.b e = aVar.e(1969517665); if (e != null) { Pair a2 = k1.a(e); bf bfVar4 = (bf) a2.first; bf bfVar5 = (bf) a2.second; if (bfVar4 != null) { z9Var.a(bfVar4); } bfVar = bfVar5; bfVar2 = bfVar4; } else { bfVar = null; bfVar2 = null; } j1.a d = aVar.d(1835365473); bf b = d != null ? k1.b(d) : null; List a3 = k1.a(aVar, z9Var, C.TIME_UNSET, (y6) null, (this.a & 1) != 0, z, new Function() { // from class: com.applovin.impl.pf$$ExternalSyntheticLambda0 @Override // com.applovin.exoplayer2.common.base.Function public final Object apply(Object obj) { lo a4; a4 = pf.a((lo) obj); return a4; } }); m8 m8Var = (m8) b1.a(this.r); int size = a3.size(); int i4 = 0; int i5 = -1; long j = C.TIME_UNSET; while (i4 < size) { ro roVar = (ro) a3.get(i4); if (roVar.b == 0) { list = a3; i = size; i2 = i5; arrayList = arrayList2; } else { lo loVar = roVar.a; int i6 = i5; arrayList = arrayList2; long j2 = loVar.e; if (j2 == C.TIME_UNSET) { j2 = roVar.h; } long max = Math.max(j, j2); list = a3; i = size; a aVar2 = new a(loVar, roVar, m8Var.a(i4, loVar.b)); int i7 = roVar.e + 30; f9.b a4 = loVar.f.a(); a4.i(i7); if (loVar.b == 2 && j2 > 0 && (i3 = roVar.b) > 1) { a4.a(i3 / (j2 / 1000000.0f)); } hf.a(loVar.b, z9Var, a4); int i8 = loVar.b; bf[] bfVarArr = new bf[2]; bfVarArr[0] = bfVar; if (this.h.isEmpty()) { c = 1; bfVar3 = null; } else { bfVar3 = new bf(this.h); c = 1; } bfVarArr[c] = bfVar3; hf.a(i8, bfVar2, b, a4, bfVarArr); aVar2.c.a(a4.a()); i2 = i6; if (loVar.b == 2 && i2 == -1) { i2 = arrayList.size(); } arrayList.add(aVar2); j = max; } i4++; i5 = i2; arrayList2 = arrayList; a3 = list; size = i; } this.u = i5; this.v = j; a[] aVarArr = (a[]) arrayList2.toArray(new a[0]); this.s = aVarArr; this.t = a(aVarArr); m8Var.c(); m8Var.a(this); } @Override // com.applovin.impl.k8 public int a(l8 l8Var, th thVar) { while (true) { int i = this.i; if (i != 0) { if (i != 1) { if (i == 2) { return c(l8Var, thVar); } if (i != 3) { throw new IllegalStateException(); } return d(l8Var, thVar); } if (b(l8Var, thVar)) { return 1; } } else if (!c(l8Var)) { return -1; } } } @Override // com.applovin.impl.k8 public void a(long j, long j2) { this.f.clear(); this.l = 0; this.n = -1; this.o = 0; this.p = 0; this.q = 0; if (j != 0) { if (this.s != null) { f(j2); } } else if (this.i != 3) { e(); } else { this.g.a(); this.h.clear(); } } @Override // com.applovin.impl.k8 public boolean a(l8 l8Var) { return lk.a(l8Var, (this.a & 2) != 0); } }