package f2;

import androidx.annotation.Nullable;
import androidx.media3.common.Metadata;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.ParsableBitArray;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.m0;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final androidx.core.view.n f25920a = new androidx.core.view.n(17);

    @Nullable
    private final g framePredicate;

    public i(@Nullable g gVar) {
        this.framePredicate = gVar;
    }

    public static a a(int i10, int i11, ParsableByteArray parsableByteArray) {
        int iL;
        String strConcat;
        int unsignedByte = parsableByteArray.readUnsignedByte();
        Charset charsetI = i(unsignedByte);
        int i12 = i10 - 1;
        byte[] bArr = new byte[i12];
        parsableByteArray.readBytes(bArr, 0, i12);
        if (i11 == 2) {
            String str = "image/" + com.google.common.base.h.q(new String(bArr, 0, 3, StandardCharsets.ISO_8859_1));
            if ("image/jpg".equals(str)) {
                str = MimeTypes.IMAGE_JPEG;
            }
            strConcat = str;
            iL = 2;
        } else {
            iL = l(0, bArr);
            String strQ = com.google.common.base.h.q(new String(bArr, 0, iL, StandardCharsets.ISO_8859_1));
            strConcat = strQ.indexOf(47) == -1 ? "image/".concat(strQ) : strQ;
        }
        int i13 = bArr[iL + 1] & 255;
        int i14 = iL + 2;
        int iK = k(i14, unsignedByte, bArr);
        String str2 = new String(bArr, i14, iK - i14, charsetI);
        int iH = h(unsignedByte) + iK;
        return new a(strConcat, str2, i13, i12 <= iH ? Util.EMPTY_BYTE_ARRAY : Arrays.copyOfRange(bArr, iH, i12));
    }

    public static f b(int i10, ParsableByteArray parsableByteArray) {
        int unsignedByte = parsableByteArray.readUnsignedByte();
        Charset charsetI = i(unsignedByte);
        int i11 = i10 - 1;
        byte[] bArr = new byte[i11];
        parsableByteArray.readBytes(bArr, 0, i11);
        int iL = l(0, bArr);
        String strNormalizeMimeType = MimeTypes.normalizeMimeType(new String(bArr, 0, iL, StandardCharsets.ISO_8859_1));
        int i12 = iL + 1;
        int iK = k(i12, unsignedByte, bArr);
        String strE = e(bArr, i12, iK, charsetI);
        int iH = h(unsignedByte) + iK;
        int iK2 = k(iH, unsignedByte, bArr);
        String strE2 = e(bArr, iH, iK2, charsetI);
        int iH2 = h(unsignedByte) + iK2;
        return new f(strNormalizeMimeType, strE, strE2, i11 <= iH2 ? Util.EMPTY_BYTE_ARRAY : Arrays.copyOfRange(bArr, iH2, i11));
    }

    public static m c(int i10, ParsableByteArray parsableByteArray) {
        int unsignedShort = parsableByteArray.readUnsignedShort();
        int unsignedInt24 = parsableByteArray.readUnsignedInt24();
        int unsignedInt242 = parsableByteArray.readUnsignedInt24();
        int unsignedByte = parsableByteArray.readUnsignedByte();
        int unsignedByte2 = parsableByteArray.readUnsignedByte();
        ParsableBitArray parsableBitArray = new ParsableBitArray();
        parsableBitArray.reset(parsableByteArray);
        int i11 = ((i10 - 10) * 8) / (unsignedByte + unsignedByte2);
        int[] iArr = new int[i11];
        int[] iArr2 = new int[i11];
        for (int i12 = 0; i12 < i11; i12++) {
            int bits = parsableBitArray.readBits(unsignedByte);
            int bits2 = parsableBitArray.readBits(unsignedByte2);
            iArr[i12] = bits;
            iArr2[i12] = bits2;
        }
        return new m(unsignedShort, unsignedInt24, iArr, iArr2, unsignedInt242);
    }

    public static n d(int i10, ParsableByteArray parsableByteArray) {
        byte[] bArr = new byte[i10];
        parsableByteArray.readBytes(bArr, 0, i10);
        int iL = l(0, bArr);
        String str = new String(bArr, 0, iL, StandardCharsets.ISO_8859_1);
        int i11 = iL + 1;
        return new n(str, i10 <= i11 ? Util.EMPTY_BYTE_ARRAY : Arrays.copyOfRange(bArr, i11, i10));
    }

    private static c decodeChapterFrame(ParsableByteArray parsableByteArray, int i10, int i11, boolean z10, int i12, @Nullable g gVar) {
        int position = parsableByteArray.getPosition();
        int iL = l(position, parsableByteArray.getData());
        String str = new String(parsableByteArray.getData(), position, iL - position, StandardCharsets.ISO_8859_1);
        parsableByteArray.setPosition(iL + 1);
        int i13 = parsableByteArray.readInt();
        int i14 = parsableByteArray.readInt();
        long unsignedInt = parsableByteArray.readUnsignedInt();
        long j10 = unsignedInt == 4294967295L ? -1L : unsignedInt;
        long unsignedInt2 = parsableByteArray.readUnsignedInt();
        long j11 = unsignedInt2 == 4294967295L ? -1L : unsignedInt2;
        ArrayList arrayList = new ArrayList();
        int i15 = position + i10;
        while (parsableByteArray.getPosition() < i15) {
            j jVarDecodeFrame = decodeFrame(i11, parsableByteArray, z10, i12, gVar);
            if (jVarDecodeFrame != null) {
                arrayList.add(jVarDecodeFrame);
            }
        }
        return new c(str, i13, i14, j10, j11, (j[]) arrayList.toArray(new j[0]));
    }

    private static d decodeChapterTOCFrame(ParsableByteArray parsableByteArray, int i10, int i11, boolean z10, int i12, @Nullable g gVar) {
        int position = parsableByteArray.getPosition();
        int iL = l(position, parsableByteArray.getData());
        String str = new String(parsableByteArray.getData(), position, iL - position, StandardCharsets.ISO_8859_1);
        parsableByteArray.setPosition(iL + 1);
        int unsignedByte = parsableByteArray.readUnsignedByte();
        boolean z11 = (unsignedByte & 2) != 0;
        boolean z12 = (unsignedByte & 1) != 0;
        int unsignedByte2 = parsableByteArray.readUnsignedByte();
        String[] strArr = new String[unsignedByte2];
        for (int i13 = 0; i13 < unsignedByte2; i13++) {
            int position2 = parsableByteArray.getPosition();
            int iL2 = l(position2, parsableByteArray.getData());
            strArr[i13] = new String(parsableByteArray.getData(), position2, iL2 - position2, StandardCharsets.ISO_8859_1);
            parsableByteArray.setPosition(iL2 + 1);
        }
        ArrayList arrayList = new ArrayList();
        int i14 = position + i10;
        while (parsableByteArray.getPosition() < i14) {
            j jVarDecodeFrame = decodeFrame(i11, parsableByteArray, z10, i12, gVar);
            if (jVarDecodeFrame != null) {
                arrayList.add(jVarDecodeFrame);
            }
        }
        return new d(str, z11, z12, strArr, (j[]) arrayList.toArray(new j[0]));
    }

    @Nullable
    private static e decodeCommentFrame(ParsableByteArray parsableByteArray, int i10) {
        if (i10 < 4) {
            return null;
        }
        int unsignedByte = parsableByteArray.readUnsignedByte();
        Charset charsetI = i(unsignedByte);
        byte[] bArr = new byte[3];
        parsableByteArray.readBytes(bArr, 0, 3);
        String str = new String(bArr, 0, 3);
        int i11 = i10 - 4;
        byte[] bArr2 = new byte[i11];
        parsableByteArray.readBytes(bArr2, 0, i11);
        int iK = k(0, unsignedByte, bArr2);
        String str2 = new String(bArr2, 0, iK, charsetI);
        int iH = h(unsignedByte) + iK;
        return new e(str, str2, e(bArr2, iH, k(iH, unsignedByte, bArr2), charsetI));
    }

    /* JADX WARN: Code restructure failed: missing block: B:44:0x00a6, code lost:
    
        if (r20 == 2) goto L49;
     */
    /* JADX WARN: Finally extract failed */
    /* JADX WARN: Removed duplicated region for block: B:114:0x014b A[Catch: all -> 0x0140, Exception -> 0x0143, OutOfMemoryError -> 0x0147, TryCatch #0 {all -> 0x0140, blocks: (B:105:0x0139, B:114:0x014b, B:121:0x0160, B:123:0x0167, B:131:0x0180, B:140:0x0195, B:151:0x01ad, B:157:0x01bc, B:165:0x01d8, B:175:0x01eb, B:183:0x0209, B:184:0x020e), top: B:194:0x012b }] */
    /* JADX WARN: Removed duplicated region for block: B:115:0x0154  */
    /* JADX WARN: Removed duplicated region for block: B:170:0x01e1  */
    /* JADX WARN: Removed duplicated region for block: B:176:0x01fc  */
    /* JADX WARN: Removed duplicated region for block: B:184:0x020e A[Catch: all -> 0x0140, Exception -> 0x01dd, OutOfMemoryError -> 0x01df, TRY_LEAVE, TryCatch #0 {all -> 0x0140, blocks: (B:105:0x0139, B:114:0x014b, B:121:0x0160, B:123:0x0167, B:131:0x0180, B:140:0x0195, B:151:0x01ad, B:157:0x01bc, B:165:0x01d8, B:175:0x01eb, B:183:0x0209, B:184:0x020e), top: B:194:0x012b }] */
    /* JADX WARN: Removed duplicated region for block: B:191:0x022e  */
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static j decodeFrame(int i10, ParsableByteArray parsableByteArray, boolean z10, int i11, @Nullable g gVar) {
        int unsignedIntToInt;
        boolean z11;
        boolean z12;
        boolean z13;
        boolean z14;
        boolean z15;
        Throwable th;
        j bVar;
        int unsignedByte = parsableByteArray.readUnsignedByte();
        int unsignedByte2 = parsableByteArray.readUnsignedByte();
        int unsignedByte3 = parsableByteArray.readUnsignedByte();
        int unsignedByte4 = i10 >= 3 ? parsableByteArray.readUnsignedByte() : 0;
        if (i10 == 4) {
            unsignedIntToInt = parsableByteArray.readUnsignedIntToInt();
            if (!z10) {
                unsignedIntToInt = (((unsignedIntToInt >> 24) & 255) << 21) | (unsignedIntToInt & 255) | (((unsignedIntToInt >> 8) & 255) << 7) | (((unsignedIntToInt >> 16) & 255) << 14);
            }
        } else {
            unsignedIntToInt = i10 == 3 ? parsableByteArray.readUnsignedIntToInt() : parsableByteArray.readUnsignedInt24();
        }
        int unsignedShort = i10 >= 3 ? parsableByteArray.readUnsignedShort() : 0;
        if (unsignedByte == 0 && unsignedByte2 == 0 && unsignedByte3 == 0 && unsignedByte4 == 0 && unsignedIntToInt == 0 && unsignedShort == 0) {
            parsableByteArray.setPosition(parsableByteArray.limit());
            return null;
        }
        int position = parsableByteArray.getPosition() + unsignedIntToInt;
        if (position > parsableByteArray.limit()) {
            Log.w("Id3Decoder", "Frame size exceeds remaining tag data");
            parsableByteArray.setPosition(parsableByteArray.limit());
            return null;
        }
        int i12 = 67;
        if (gVar != null) {
            switch (((androidx.core.view.n) gVar).f1787a) {
                default:
                    if (unsignedByte == 67) {
                        if (unsignedByte2 != 79 || unsignedByte3 != 77 || (unsignedByte4 != 77 && i10 != 2)) {
                        }
                    }
                    if (unsignedByte == 77) {
                        if (unsignedByte2 == 76) {
                            if (unsignedByte3 == 76) {
                                if (unsignedByte4 != 84) {
                                }
                            }
                        }
                    }
                case 17:
                    parsableByteArray.setPosition(position);
                    return null;
            }
        }
        if (i10 == 3) {
            z12 = (unsignedShort & 128) != 0;
            boolean z16 = (unsignedShort & 64) != 0;
            z11 = (unsignedShort & 32) != 0;
            z14 = z16;
            z15 = false;
            z13 = z12;
        } else if (i10 == 4) {
            boolean z17 = (unsignedShort & 64) != 0;
            boolean z18 = (unsignedShort & 8) != 0;
            z14 = (unsignedShort & 4) != 0;
            z15 = (unsignedShort & 2) != 0;
            boolean z19 = z18;
            z13 = (unsignedShort & 1) != 0;
            z11 = z17;
            z12 = z19;
        } else {
            z11 = false;
            z12 = false;
            z13 = false;
            z14 = false;
            z15 = false;
        }
        if (z12 || z14) {
            Log.w("Id3Decoder", "Skipping unsupported compressed or encrypted frame");
            parsableByteArray.setPosition(position);
            return null;
        }
        if (z11) {
            unsignedIntToInt--;
            parsableByteArray.skipBytes(1);
        }
        if (z13) {
            unsignedIntToInt -= 4;
            parsableByteArray.skipBytes(4);
        }
        if (z15) {
            unsignedIntToInt = m(unsignedIntToInt, parsableByteArray);
        }
        int i13 = unsignedIntToInt;
        int i14 = 84;
        try {
            try {
                if (unsignedByte != 84) {
                    if (unsignedByte != i14) {
                        bVar = decodeTextInformationFrame(parsableByteArray, i13, j(i10, unsignedByte, unsignedByte2, unsignedByte3, unsignedByte4));
                    } else if (unsignedByte == 87 && unsignedByte2 == 88 && unsignedByte3 == 88 && (i10 == 2 || unsignedByte4 == 88)) {
                        bVar = decodeWxxxFrame(parsableByteArray, i13);
                    } else if (unsignedByte == 87) {
                        bVar = g(parsableByteArray, i13, j(i10, unsignedByte, unsignedByte2, unsignedByte3, unsignedByte4));
                    } else if (unsignedByte == 80 && unsignedByte2 == 82 && unsignedByte3 == 73 && unsignedByte4 == 86) {
                        bVar = d(i13, parsableByteArray);
                    } else if (unsignedByte == 71 && unsignedByte2 == 69 && unsignedByte3 == 79 && (unsignedByte4 == 66 || i10 == 2)) {
                        bVar = b(i13, parsableByteArray);
                    } else if (i10 == 2) {
                        if (unsignedByte == 80 && unsignedByte2 == 73 && unsignedByte3 == 67) {
                            bVar = a(i13, i10, parsableByteArray);
                        }
                        if (unsignedByte != 67 && unsignedByte2 == 79 && unsignedByte3 == 77 && (unsignedByte4 == 77 || i10 == 2)) {
                            bVar = decodeCommentFrame(parsableByteArray, i13);
                        } else {
                            try {
                                if (unsignedByte != 67 && unsignedByte2 == 72 && unsignedByte3 == 65 && unsignedByte4 == 80) {
                                    i12 = i13;
                                    bVar = decodeChapterFrame(parsableByteArray, i13, i10, z10, i11, gVar);
                                } else if (unsignedByte != 67 && unsignedByte2 == 84 && unsignedByte3 == 79 && unsignedByte4 == 67) {
                                    i12 = i13;
                                    bVar = decodeChapterTOCFrame(parsableByteArray, i13, i10, z10, i11, gVar);
                                } else {
                                    i12 = i13;
                                    if (unsignedByte != 77 && unsignedByte2 == 76 && unsignedByte3 == 76 && unsignedByte4 == 84) {
                                        bVar = c(i12, parsableByteArray);
                                    } else {
                                        String strJ = j(i10, unsignedByte, unsignedByte2, unsignedByte3, unsignedByte4);
                                        byte[] bArr = new byte[i12];
                                        parsableByteArray.readBytes(bArr, 0, i12);
                                        bVar = new b(strJ, bArr);
                                    }
                                }
                                parsableByteArray.setPosition(position);
                                th = null;
                            } catch (Exception e10) {
                                e = e10;
                                parsableByteArray.setPosition(position);
                                th = e;
                                bVar = null;
                            } catch (OutOfMemoryError e11) {
                                e = e11;
                                parsableByteArray.setPosition(position);
                                th = e;
                                bVar = null;
                            }
                        }
                    } else if (unsignedByte == 65 && unsignedByte2 == 80 && unsignedByte3 == 73 && unsignedByte4 == 67) {
                        bVar = a(i13, i10, parsableByteArray);
                    } else {
                        if (unsignedByte != 67) {
                        }
                        if (unsignedByte != 67) {
                            if (unsignedByte != 67) {
                                i12 = i13;
                                if (unsignedByte != 77) {
                                    String strJ2 = j(i10, unsignedByte, unsignedByte2, unsignedByte3, unsignedByte4);
                                    byte[] bArr2 = new byte[i12];
                                    parsableByteArray.readBytes(bArr2, 0, i12);
                                    bVar = new b(strJ2, bArr2);
                                    parsableByteArray.setPosition(position);
                                    th = null;
                                }
                            }
                        }
                    }
                    i12 = i13;
                    parsableByteArray.setPosition(position);
                    th = null;
                } else if (unsignedByte2 == 88 && unsignedByte3 == 88 && (i10 == 2 || unsignedByte4 == 88)) {
                    bVar = decodeTxxxFrame(parsableByteArray, i13);
                    i12 = i13;
                    parsableByteArray.setPosition(position);
                    th = null;
                } else {
                    i14 = 84;
                    if (unsignedByte != i14) {
                    }
                    i12 = i13;
                    parsableByteArray.setPosition(position);
                    th = null;
                }
            } catch (Throwable th2) {
                parsableByteArray.setPosition(position);
                throw th2;
            }
        } catch (Exception e12) {
            e = e12;
            i12 = i13;
            parsableByteArray.setPosition(position);
            th = e;
            bVar = null;
            if (bVar == null) {
            }
            return bVar;
        } catch (OutOfMemoryError e13) {
            e = e13;
            i12 = i13;
            parsableByteArray.setPosition(position);
            th = e;
            bVar = null;
            if (bVar == null) {
            }
            return bVar;
        }
        if (bVar == null) {
            Log.w("Id3Decoder", "Failed to decode frame: id=" + j(i10, unsignedByte, unsignedByte2, unsignedByte3, unsignedByte4) + ", frameSize=" + i12, th);
        }
        return bVar;
    }

    @Nullable
    private static h decodeHeader(ParsableByteArray parsableByteArray) {
        if (parsableByteArray.bytesLeft() < 10) {
            Log.w("Id3Decoder", "Data too short to be an ID3 tag");
            return null;
        }
        int unsignedInt24 = parsableByteArray.readUnsignedInt24();
        if (unsignedInt24 != 4801587) {
            Log.w("Id3Decoder", "Unexpected first three bytes of ID3 tag header: 0x" + String.format("%06X", Integer.valueOf(unsignedInt24)));
            return null;
        }
        int unsignedByte = parsableByteArray.readUnsignedByte();
        parsableByteArray.skipBytes(1);
        int unsignedByte2 = parsableByteArray.readUnsignedByte();
        int synchSafeInt = parsableByteArray.readSynchSafeInt();
        if (unsignedByte == 2) {
            if ((unsignedByte2 & 64) != 0) {
                Log.w("Id3Decoder", "Skipped ID3 tag with majorVersion=2 and undefined compression scheme");
                return null;
            }
        } else if (unsignedByte == 3) {
            if ((unsignedByte2 & 64) != 0) {
                int i10 = parsableByteArray.readInt();
                parsableByteArray.skipBytes(i10);
                synchSafeInt -= i10 + 4;
            }
        } else {
            if (unsignedByte != 4) {
                defpackage.e.z("Skipped ID3 tag with unsupported majorVersion=", unsignedByte, "Id3Decoder");
                return null;
            }
            if ((unsignedByte2 & 64) != 0) {
                int synchSafeInt2 = parsableByteArray.readSynchSafeInt();
                parsableByteArray.skipBytes(synchSafeInt2 - 4);
                synchSafeInt -= synchSafeInt2;
            }
            if ((unsignedByte2 & 16) != 0) {
                synchSafeInt -= 10;
            }
        }
        return new h(unsignedByte, synchSafeInt, unsignedByte < 4 && (unsignedByte2 & 128) != 0);
    }

    @Nullable
    private static o decodeTextInformationFrame(ParsableByteArray parsableByteArray, int i10, String str) {
        if (i10 < 1) {
            return null;
        }
        int unsignedByte = parsableByteArray.readUnsignedByte();
        int i11 = i10 - 1;
        byte[] bArr = new byte[i11];
        parsableByteArray.readBytes(bArr, 0, i11);
        return new o(str, (String) null, f(unsignedByte, 0, bArr));
    }

    @Nullable
    private static o decodeTxxxFrame(ParsableByteArray parsableByteArray, int i10) {
        if (i10 < 1) {
            return null;
        }
        int unsignedByte = parsableByteArray.readUnsignedByte();
        int i11 = i10 - 1;
        byte[] bArr = new byte[i11];
        parsableByteArray.readBytes(bArr, 0, i11);
        int iK = k(0, unsignedByte, bArr);
        return new o("TXXX", new String(bArr, 0, iK, i(unsignedByte)), f(unsignedByte, h(unsignedByte) + iK, bArr));
    }

    @Nullable
    private static p decodeWxxxFrame(ParsableByteArray parsableByteArray, int i10) {
        if (i10 < 1) {
            return null;
        }
        int unsignedByte = parsableByteArray.readUnsignedByte();
        int i11 = i10 - 1;
        byte[] bArr = new byte[i11];
        parsableByteArray.readBytes(bArr, 0, i11);
        int iK = k(0, unsignedByte, bArr);
        String str = new String(bArr, 0, iK, i(unsignedByte));
        int iH = h(unsignedByte) + iK;
        return new p("WXXX", str, e(bArr, iH, l(iH, bArr), StandardCharsets.ISO_8859_1));
    }

    public static String e(byte[] bArr, int i10, int i11, Charset charset) {
        return (i11 <= i10 || i11 > bArr.length) ? "" : new String(bArr, i10, i11 - i10, charset);
    }

    public static ImmutableList f(int i10, int i11, byte[] bArr) {
        if (i11 >= bArr.length) {
            return ImmutableList.w("");
        }
        m0 m0VarM = ImmutableList.m();
        int iK = k(i11, i10, bArr);
        while (i11 < iK) {
            m0VarM.c(new String(bArr, i11, iK - i11, i(i10)));
            i11 = h(i10) + iK;
            iK = k(i11, i10, bArr);
        }
        ImmutableList immutableListI = m0VarM.i();
        return immutableListI.isEmpty() ? ImmutableList.w("") : immutableListI;
    }

    public static p g(ParsableByteArray parsableByteArray, int i10, String str) {
        byte[] bArr = new byte[i10];
        parsableByteArray.readBytes(bArr, 0, i10);
        return new p(str, null, new String(bArr, 0, l(0, bArr), StandardCharsets.ISO_8859_1));
    }

    public static int h(int i10) {
        return (i10 == 0 || i10 == 3) ? 1 : 2;
    }

    public static Charset i(int i10) {
        return i10 != 1 ? i10 != 2 ? i10 != 3 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8 : StandardCharsets.UTF_16BE : StandardCharsets.UTF_16;
    }

    public static String j(int i10, int i11, int i12, int i13, int i14) {
        return i10 == 2 ? String.format(Locale.US, "%c%c%c", Integer.valueOf(i11), Integer.valueOf(i12), Integer.valueOf(i13)) : String.format(Locale.US, "%c%c%c%c", Integer.valueOf(i11), Integer.valueOf(i12), Integer.valueOf(i13), Integer.valueOf(i14));
    }

    public static int k(int i10, int i11, byte[] bArr) {
        int iL = l(i10, bArr);
        if (i11 == 0 || i11 == 3) {
            return iL;
        }
        while (iL < bArr.length - 1) {
            if ((iL - i10) % 2 == 0 && bArr[iL + 1] == 0) {
                return iL;
            }
            iL = l(iL + 1, bArr);
        }
        return bArr.length;
    }

    public static int l(int i10, byte[] bArr) {
        while (i10 < bArr.length) {
            if (bArr[i10] == 0) {
                return i10;
            }
            i10++;
        }
        return bArr.length;
    }

    public static int m(int i10, ParsableByteArray parsableByteArray) {
        byte[] data = parsableByteArray.getData();
        int position = parsableByteArray.getPosition();
        int i11 = position;
        while (true) {
            int i12 = i11 + 1;
            if (i12 >= position + i10) {
                return i10;
            }
            if ((data[i11] & 255) == 255 && data[i12] == 0) {
                System.arraycopy(data, i11 + 2, data, i12, (i10 - (i11 - position)) - 2);
                i10--;
            }
            i11 = i12;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:35:0x007c A[PHI: r3
      0x007c: PHI (r3v16 int) = (r3v5 int), (r3v19 int) binds: [B:42:0x0089, B:33:0x0079] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static boolean n(ParsableByteArray parsableByteArray, int i10, int i11, boolean z10) {
        int unsignedInt24;
        long unsignedInt242;
        int unsignedShort;
        int i12;
        int position = parsableByteArray.getPosition();
        while (true) {
            try {
                boolean z11 = true;
                if (parsableByteArray.bytesLeft() < i11) {
                    parsableByteArray.setPosition(position);
                    return true;
                }
                if (i10 >= 3) {
                    unsignedInt24 = parsableByteArray.readInt();
                    unsignedInt242 = parsableByteArray.readUnsignedInt();
                    unsignedShort = parsableByteArray.readUnsignedShort();
                } else {
                    unsignedInt24 = parsableByteArray.readUnsignedInt24();
                    unsignedInt242 = parsableByteArray.readUnsignedInt24();
                    unsignedShort = 0;
                }
                if (unsignedInt24 == 0 && unsignedInt242 == 0 && unsignedShort == 0) {
                    parsableByteArray.setPosition(position);
                    return true;
                }
                if (i10 == 4 && !z10) {
                    if ((8421504 & unsignedInt242) != 0) {
                        parsableByteArray.setPosition(position);
                        return false;
                    }
                    unsignedInt242 = (((unsignedInt242 >> 24) & 255) << 21) | (unsignedInt242 & 255) | (((unsignedInt242 >> 8) & 255) << 7) | (((unsignedInt242 >> 16) & 255) << 14);
                }
                if (i10 == 4) {
                    i12 = (unsignedShort & 64) != 0 ? 1 : 0;
                    if ((unsignedShort & 1) == 0) {
                        z11 = false;
                    }
                } else if (i10 == 3) {
                    i12 = (unsignedShort & 32) != 0 ? 1 : 0;
                    if ((unsignedShort & 128) == 0) {
                    }
                } else {
                    i12 = 0;
                    z11 = false;
                }
                if (z11) {
                    i12 += 4;
                }
                if (unsignedInt242 < i12) {
                    parsableByteArray.setPosition(position);
                    return false;
                }
                if (parsableByteArray.bytesLeft() < unsignedInt242) {
                    parsableByteArray.setPosition(position);
                    return false;
                }
                parsableByteArray.skipBytes((int) unsignedInt242);
            } catch (Throwable th) {
                parsableByteArray.setPosition(position);
                throw th;
            }
        }
    }

    @Override // a2.c
    @Nullable
    public Metadata decode(a2.b bVar, ByteBuffer byteBuffer) {
        return decode(byteBuffer.array(), byteBuffer.limit());
    }

    @Nullable
    public Metadata decode(byte[] bArr, int i10) {
        ArrayList arrayList = new ArrayList();
        ParsableByteArray parsableByteArray = new ParsableByteArray(bArr, i10);
        h hVarDecodeHeader = decodeHeader(parsableByteArray);
        if (hVarDecodeHeader == null) {
            return null;
        }
        int position = parsableByteArray.getPosition();
        int i11 = hVarDecodeHeader.f25917a;
        int i12 = i11 == 2 ? 6 : 10;
        int iM = hVarDecodeHeader.f25919c;
        if (hVarDecodeHeader.f25918b) {
            iM = m(iM, parsableByteArray);
        }
        parsableByteArray.setLimit(position + iM);
        boolean z10 = false;
        if (!n(parsableByteArray, i11, i12, false)) {
            if (i11 != 4 || !n(parsableByteArray, 4, i12, true)) {
                Log.w("Id3Decoder", "Failed to validate ID3 tag with majorVersion=" + i11);
                return null;
            }
            z10 = true;
        }
        while (parsableByteArray.bytesLeft() >= i12) {
            j jVarDecodeFrame = decodeFrame(i11, parsableByteArray, z10, i12, this.framePredicate);
            if (jVarDecodeFrame != null) {
                arrayList.add(jVarDecodeFrame);
            }
        }
        return new Metadata(arrayList);
    }
}
