package tv.danmaku.ijk.media.player.ui.subtitleconverter.subtitleFile;

import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.google.android.gms.measurement.internal.u1;
import defpackage.e;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;
import u.a;

/* JADX INFO: loaded from: classes3.dex */
public class FormatSTL implements TimedTextFileFormat {
    private void createSTLStyles(TimedTextObject timedTextObject) {
        Style style = new Style("white");
        style.color = Style.getRGBValue("name", "white");
        Style styleS = u1.s(timedTextObject.styling, style.iD, style, "whiteU", style);
        styleS.underline = true;
        Style styleS2 = u1.s(timedTextObject.styling, styleS.iD, styleS, "whiteUI", styleS);
        styleS2.italic = true;
        Style styleS3 = u1.s(timedTextObject.styling, styleS2.iD, styleS2, "whiteI", styleS2);
        styleS3.underline = false;
        timedTextObject.styling.put(styleS3.iD, styleS3);
        Style style2 = new Style("green");
        style2.color = Style.getRGBValue("name", "green");
        Style styleS4 = u1.s(timedTextObject.styling, style2.iD, style2, "greenU", style2);
        styleS4.underline = true;
        Style styleS5 = u1.s(timedTextObject.styling, styleS4.iD, styleS4, "greenUI", styleS4);
        styleS5.italic = true;
        Style styleS6 = u1.s(timedTextObject.styling, styleS5.iD, styleS5, "greenI", styleS5);
        styleS6.underline = false;
        timedTextObject.styling.put(styleS6.iD, styleS6);
        Style style3 = new Style("blue");
        style3.color = Style.getRGBValue("name", "blue");
        Style styleS7 = u1.s(timedTextObject.styling, style3.iD, style3, "blueU", style3);
        styleS7.underline = true;
        Style styleS8 = u1.s(timedTextObject.styling, styleS7.iD, styleS7, "blueUI", styleS7);
        styleS8.italic = true;
        Style styleS9 = u1.s(timedTextObject.styling, styleS8.iD, styleS8, "blueI", styleS8);
        styleS9.underline = false;
        timedTextObject.styling.put(styleS9.iD, styleS9);
        Style style4 = new Style("cyan");
        style4.color = Style.getRGBValue("name", "cyan");
        Style styleS10 = u1.s(timedTextObject.styling, style4.iD, style4, "cyanU", style4);
        styleS10.underline = true;
        Style styleS11 = u1.s(timedTextObject.styling, styleS10.iD, styleS10, "cyanUI", styleS10);
        styleS11.italic = true;
        Style styleS12 = u1.s(timedTextObject.styling, styleS11.iD, styleS11, "cyanI", styleS11);
        styleS12.underline = false;
        timedTextObject.styling.put(styleS12.iD, styleS12);
        Style style5 = new Style("red");
        style5.color = Style.getRGBValue("name", "red");
        Style styleS13 = u1.s(timedTextObject.styling, style5.iD, style5, "redU", style5);
        styleS13.underline = true;
        Style styleS14 = u1.s(timedTextObject.styling, styleS13.iD, styleS13, "redUI", styleS13);
        styleS14.italic = true;
        Style styleS15 = u1.s(timedTextObject.styling, styleS14.iD, styleS14, "redI", styleS14);
        styleS15.underline = false;
        timedTextObject.styling.put(styleS15.iD, styleS15);
        Style style6 = new Style("yellow");
        style6.color = Style.getRGBValue("name", "yellow");
        Style styleS16 = u1.s(timedTextObject.styling, style6.iD, style6, "yellowU", style6);
        styleS16.underline = true;
        Style styleS17 = u1.s(timedTextObject.styling, styleS16.iD, styleS16, "yellowUI", styleS16);
        styleS17.italic = true;
        Style styleS18 = u1.s(timedTextObject.styling, styleS17.iD, styleS17, "yellowI", styleS17);
        styleS18.underline = false;
        timedTextObject.styling.put(styleS18.iD, styleS18);
        Style style7 = new Style("magenta");
        style7.color = Style.getRGBValue("name", "magenta");
        Style styleS19 = u1.s(timedTextObject.styling, style7.iD, style7, "magentaU", style7);
        styleS19.underline = true;
        Style styleS20 = u1.s(timedTextObject.styling, styleS19.iD, styleS19, "magentaUI", styleS19);
        styleS20.italic = true;
        Style styleS21 = u1.s(timedTextObject.styling, styleS20.iD, styleS20, "magentaI", styleS20);
        styleS21.underline = false;
        timedTextObject.styling.put(styleS21.iD, styleS21);
        Style style8 = new Style("black");
        style8.color = Style.getRGBValue("name", "black");
        Style styleS22 = u1.s(timedTextObject.styling, style8.iD, style8, "blackU", style8);
        styleS22.underline = true;
        Style styleS23 = u1.s(timedTextObject.styling, styleS22.iD, styleS22, "blackUI", styleS22);
        styleS23.italic = true;
        Style styleS24 = u1.s(timedTextObject.styling, styleS23.iD, styleS23, "blackI", styleS23);
        styleS24.underline = false;
        timedTextObject.styling.put(styleS24.iD, styleS24);
    }

    private void parseTextForSTL(Caption caption, byte[] bArr, int i10, TimedTextObject timedTextObject) {
        byte b10;
        int i11;
        String strJ;
        Style style;
        String strJ2 = "white";
        String strJ3 = "";
        int length = 0;
        byte b11 = 0;
        boolean z10 = false;
        boolean z11 = false;
        while (length < bArr.length) {
            byte b12 = bArr[length];
            if (b12 < 0) {
                if (b12 <= -113) {
                    int i12 = length + 1;
                    if (i12 < bArr.length && b12 == bArr[i12]) {
                        length = i12;
                    }
                    byte b13 = bArr[length];
                    if (b13 == -118) {
                        caption.content = e.p(new StringBuilder(), caption.content, strJ3, "<br />");
                    } else if (b13 != -113) {
                        switch (b13) {
                            case -128:
                                i11 = 1;
                                z11 = true;
                                break;
                            case -127:
                                i11 = 1;
                                z11 = false;
                                break;
                            case -126:
                                i11 = 1;
                                z10 = true;
                                break;
                            case -125:
                                i11 = 1;
                                z10 = false;
                                break;
                        }
                        length += i11;
                    } else {
                        caption.content = e.o(new StringBuilder(), caption.content, strJ3);
                        if (z10) {
                            strJ2 = u1.j(strJ2, "U");
                        }
                        if (z11) {
                            strJ2 = u1.j(strJ2, "I");
                        }
                        Style style2 = timedTextObject.styling.get(strJ2);
                        if (i10 == 1) {
                            strJ = u1.j(strJ2, "L");
                            if (timedTextObject.styling.get(strJ) == null) {
                                style = new Style(strJ, style2);
                                style.textAlign = "bottom-left";
                                timedTextObject.styling.put(strJ, style);
                                strJ2 = strJ;
                                style2 = style;
                            } else {
                                style2 = timedTextObject.styling.get(strJ);
                                strJ2 = strJ;
                            }
                        } else if (i10 == 3) {
                            strJ = u1.j(strJ2, "R");
                            if (timedTextObject.styling.get(strJ) == null) {
                                style = new Style(strJ, style2);
                                style.textAlign = "bottom-rigth";
                                timedTextObject.styling.put(strJ, style);
                                strJ2 = strJ;
                                style2 = style;
                            } else {
                                style2 = timedTextObject.styling.get(strJ);
                                strJ2 = strJ;
                            }
                        }
                        caption.style = style2;
                        int i13 = caption.start.mseconds;
                        while (timedTextObject.captions.containsKey(Integer.valueOf(i13))) {
                            i13++;
                        }
                        timedTextObject.captions.put(Integer.valueOf(i13), caption);
                        length = bArr.length;
                    }
                    strJ3 = "";
                } else if (b12 >= -64 && b12 <= -49) {
                    b11 = b12;
                }
            } else if (b12 < 32) {
                int i14 = length + 1;
                if (i14 < bArr.length && b12 == bArr[i14]) {
                    length = i14;
                }
                switch (bArr[length]) {
                    case 0:
                        strJ2 = "black";
                        break;
                    case 1:
                        strJ2 = "red";
                        break;
                    case 2:
                        strJ2 = "green";
                        break;
                    case 3:
                        strJ2 = "yellow";
                        break;
                    case 4:
                        strJ2 = "blue";
                        break;
                    case 5:
                        strJ2 = "magenta";
                        break;
                    case 6:
                        strJ2 = "cyan";
                        break;
                    case 7:
                        strJ2 = "white";
                        break;
                }
            } else {
                String str = new String(new byte[]{b12});
                if (b11 != 0) {
                    if (b11 == -62 && bArr[length] == 101) {
                        str = "é";
                    } else if (b11 == -56 && bArr[length] == 105) {
                        str = "ï";
                    } else if (b11 == -63 && bArr[length] == 97) {
                        str = "à";
                    } else {
                        if (b11 == -56) {
                            b10 = 101;
                            if (bArr[length] == 101) {
                                str = "ë";
                            }
                        } else {
                            b10 = 101;
                        }
                        if (b11 == -61 && bArr[length] == b10) {
                            str = "ê";
                        } else if (b11 == -63 && bArr[length] == 117) {
                            str = "ù";
                        } else if (b11 == -61 && bArr[length] == 105) {
                            str = "î";
                        } else if (b11 == -63 && bArr[length] == 101) {
                            str = "è";
                        } else if (b11 == -61 && bArr[length] == 97) {
                            str = "â";
                        } else if (b11 == -61 && bArr[length] == 111) {
                            str = "ô";
                        } else if (b11 == -61 && bArr[length] == 117) {
                            str = "û";
                        } else if (b11 == -53 && bArr[length] == 99) {
                            str = "ç";
                        } else if (b11 == -56 && bArr[length] == 97) {
                            str = "ä";
                        } else if (b11 == -56 && bArr[length] == 111) {
                            str = "ö";
                        } else if (b11 == -56 && bArr[length] == 117) {
                            str = "ü";
                        }
                    }
                    b11 = 0;
                }
                strJ3 = u1.j(strJ3, str);
            }
            i11 = 1;
            length += i11;
        }
    }

    @Override // tv.danmaku.ijk.media.player.ui.subtitleconverter.subtitleFile.TimedTextFileFormat
    public TimedTextObject parseFile(String str, InputStream inputStream) throws FatalParsingException, IOException {
        return parseFile(str, inputStream, Charset.defaultCharset());
    }

    @Override // tv.danmaku.ijk.media.player.ui.subtitleconverter.subtitleFile.TimedTextFileFormat
    public TimedTextObject parseFile(String str, InputStream inputStream, Charset charset) throws FatalParsingException, IOException {
        boolean z10;
        String str2;
        String str3;
        int i10;
        int i11;
        int i12;
        InputStream inputStream2 = inputStream;
        String str4 = "";
        String str5 = ":";
        TimedTextObject timedTextObject = new TimedTextObject();
        timedTextObject.fileName = str;
        byte[] bArr = new byte[1024];
        byte[] bArr2 = new byte[128];
        try {
            createSTLStyles(timedTextObject);
            if (inputStream2.read(bArr) < 1024) {
                throw new FatalParsingException("The file must contain at least a GSI block");
            }
            int i13 = Integer.parseInt(new String(new byte[]{bArr[6], bArr[7]}));
            int i14 = Integer.parseInt(new String(new byte[]{bArr[12], bArr[13]}));
            byte[] bArr3 = new byte[32];
            System.arraycopy(bArr, 16, bArr3, 0, 32);
            String str6 = new String(bArr3);
            byte[] bArr4 = new byte[32];
            System.arraycopy(bArr, 48, bArr4, 0, 32);
            String str7 = new String(bArr4);
            int i15 = Integer.parseInt(new String(new byte[]{bArr[238], bArr[239], bArr[240], bArr[241], bArr[242]}).trim());
            int i16 = Integer.parseInt(new String(new byte[]{bArr[243], bArr[244], bArr[245], bArr[246], bArr[247]}).trim());
            timedTextObject.title = (str6.trim() + " " + str7.trim()).trim();
            if (i14 > 4 || i14 < 0) {
                timedTextObject.warnings += "Invalid Character Code table number, corrupt data? will try to parse anyways assuming it is latin.\n\n";
            } else if (i14 != 0) {
                timedTextObject.warnings += "Only latin alphabet supported for import from STL, other languages may produce unexpected results.\n\n";
            }
            Caption caption = null;
            int i17 = 0;
            boolean z11 = false;
            int i18 = 0;
            while (true) {
                if (i17 >= i15) {
                    break;
                }
                if (inputStream2.read(bArr2) < 128) {
                    timedTextObject.warnings += "Unexpected end of file, " + i17 + " blocks read, expecting " + i15 + " blocks in total.\n\n";
                    break;
                }
                if (!z11) {
                    caption = new Caption();
                }
                if ((bArr2[2] * 256) + bArr2[1] != i18) {
                    timedTextObject.warnings += "Unexpected subtitle number at TTI block " + i17 + ". Parsing proceeds...\n\n";
                }
                byte b10 = bArr2[3];
                if (b10 != -1 && b10 != -2) {
                    z10 = true;
                } else if (b10 == -2) {
                    str2 = str4;
                    str3 = str5;
                    i10 = i16;
                    i11 = i17;
                    i12 = i15;
                    z11 = false;
                    i17 = i11 + 1;
                    i15 = i12;
                    str4 = str2;
                    str5 = str3;
                    i16 = i10;
                    inputStream2 = inputStream;
                } else {
                    z10 = false;
                }
                StringBuilder sb = new StringBuilder();
                sb.append(str4);
                i12 = i15;
                sb.append((int) bArr2[5]);
                sb.append(str5);
                sb.append((int) bArr2[6]);
                sb.append(str5);
                sb.append((int) bArr2[7]);
                sb.append(str5);
                sb.append((int) bArr2[8]);
                String string = sb.toString();
                String str8 = str4 + ((int) bArr2[9]) + str5 + ((int) bArr2[10]) + str5 + ((int) bArr2[11]) + str5 + ((int) bArr2[12]);
                byte b11 = bArr2[14];
                if (bArr2[15] == 0) {
                    str2 = str4;
                    str3 = str5;
                    byte[] bArr5 = new byte[112];
                    i10 = i16;
                    i11 = i17;
                    System.arraycopy(bArr2, 16, bArr5, 0, 112);
                    if (z10) {
                        parseTextForSTL(caption, bArr5, b11, timedTextObject);
                    } else {
                        caption.start = new Time("h:m:s:f/fps", string + "/" + i13);
                        caption.end = new Time("h:m:s:f/fps", str8 + "/" + i13);
                        parseTextForSTL(caption, bArr5, b11, timedTextObject);
                    }
                } else {
                    str2 = str4;
                    str3 = str5;
                    i10 = i16;
                    i11 = i17;
                }
                if (!z10) {
                    i18++;
                }
                z11 = z10;
                i17 = i11 + 1;
                i15 = i12;
                str4 = str2;
                str5 = str3;
                i16 = i10;
                inputStream2 = inputStream;
            }
            int i19 = i16;
            if (i18 != i19) {
                timedTextObject.warnings += "Number of parsed subtitles (" + i18 + ") different from expected number of subtitles (" + i19 + ").\n\n";
            }
            inputStream.close();
            timedTextObject.cleanUnusedStyles();
            timedTextObject.built = true;
            return timedTextObject;
        } catch (Exception e10) {
            e10.printStackTrace();
            throw new FatalParsingException("Format error in the file, migth be due to corrupt data.\n" + e10.getMessage());
        }
    }

    @Override // tv.danmaku.ijk.media.player.ui.subtitleconverter.subtitleFile.TimedTextFileFormat
    public byte[] toFile(TimedTextObject timedTextObject) {
        char c6;
        char c10;
        if (!timedTextObject.built) {
            return null;
        }
        byte[] bArr = new byte[1024];
        byte[] bArr2 = new byte[128];
        byte[] bArr3 = new byte[(timedTextObject.captions.size() * 128) + 1024];
        byte[] bytes = "850STL25.0110000".getBytes();
        System.arraycopy(bytes, 0, bArr, 0, bytes.length);
        String str = timedTextObject.title;
        if (str == null) {
            str = timedTextObject.fileName;
        }
        byte[] bytes2 = str.getBytes();
        for (int i10 = 0; i10 < 208; i10++) {
            if (i10 < bytes2.length) {
                bArr[i10 + 16] = bytes2[i10];
            } else {
                bArr[i10 + 16] = 32;
            }
        }
        String str2 = new SimpleDateFormat("yyMMdd").format(new Date());
        String strJ = e.j(str2, str2, "00");
        String strConcat = "" + timedTextObject.captions.size();
        while (true) {
            c6 = 5;
            if (strConcat.length() >= 5) {
                break;
            }
            strConcat = SessionDescription.SUPPORTED_SDP_VERSION.concat(strConcat);
        }
        StringBuilder sbC = a.c(strJ + strConcat + strConcat + "0013216100000000");
        TreeMap<Integer, Caption> treeMap = timedTextObject.captions;
        sbC.append(treeMap.get(treeMap.firstKey()).start.getTime("hhmmssff/25"));
        byte[] bytes3 = (sbC.toString() + "11OOO").getBytes();
        System.arraycopy(bytes3, 0, bArr, 224, bytes3.length);
        for (int i11 = 277; i11 < 1024; i11++) {
            bArr[i11] = 32;
        }
        System.arraycopy(bArr, 0, bArr3, 0, 1024);
        int i12 = 0;
        for (Caption caption : timedTextObject.captions.values()) {
            bArr2[0] = 0;
            bArr2[1] = (byte) (i12 % 256);
            bArr2[2] = (byte) (i12 / 256);
            bArr2[3] = -1;
            bArr2[4] = 0;
            String[] strArrSplit = caption.start.getTime("h:m:s:f/25").split(":");
            bArr2[c6] = Byte.parseByte(strArrSplit[0]);
            bArr2[6] = Byte.parseByte(strArrSplit[1]);
            bArr2[7] = Byte.parseByte(strArrSplit[2]);
            bArr2[8] = Byte.parseByte(strArrSplit[3]);
            String[] strArrSplit2 = caption.end.getTime("h:m:s:f/25").split(":");
            bArr2[9] = Byte.parseByte(strArrSplit2[0]);
            bArr2[10] = Byte.parseByte(strArrSplit2[1]);
            bArr2[11] = Byte.parseByte(strArrSplit2[2]);
            bArr2[12] = Byte.parseByte(strArrSplit2[3]);
            bArr2[13] = 18;
            Style style = caption.style;
            if (style == null) {
                bArr2[14] = 2;
            } else if (style.textAlign.contains("left")) {
                bArr2[14] = 1;
            } else if (caption.style.textAlign.contains("right")) {
                bArr2[14] = 3;
            }
            bArr2[15] = 0;
            String[] strArrSplit3 = caption.content.split("<br />");
            for (int i13 = 0; i13 < strArrSplit3.length; i13++) {
                strArrSplit3[i13] = strArrSplit3[i13].replaceAll("\\<.*?\\>", "");
            }
            Style style2 = caption.style;
            int i14 = 16;
            if (style2 == null) {
                c10 = 5;
            } else {
                if (style2.italic) {
                    bArr2[16] = -128;
                } else {
                    bArr2[16] = -127;
                }
                if (style2.underline) {
                    bArr2[17] = -126;
                } else {
                    bArr2[17] = -125;
                }
                String strSubstring = style2.color.substring(0, 6);
                if (strSubstring.equalsIgnoreCase("000000")) {
                    bArr2[18] = 0;
                } else if (strSubstring.equalsIgnoreCase("0000ff")) {
                    bArr2[18] = 4;
                } else if (strSubstring.equalsIgnoreCase("00ffff")) {
                    bArr2[18] = 6;
                } else if (strSubstring.equalsIgnoreCase("00ff00")) {
                    bArr2[18] = 2;
                } else if (strSubstring.equalsIgnoreCase("ff0000")) {
                    bArr2[18] = 1;
                } else if (strSubstring.equalsIgnoreCase("ffff00")) {
                    bArr2[18] = 3;
                } else {
                    if (strSubstring.equalsIgnoreCase("ff00ff")) {
                        c10 = 5;
                        bArr2[18] = 5;
                    } else {
                        c10 = 5;
                        bArr2[18] = 7;
                    }
                    i14 = 19;
                }
                i14 = 19;
                c10 = 5;
            }
            int i15 = 0;
            while (i15 < strArrSplit3.length) {
                for (char c11 : strArrSplit3[i15].toCharArray()) {
                    if (i14 > 126) {
                        break;
                    }
                    if (c11 >= ' ' && c11 <= 127) {
                        bArr2[i14] = (byte) c11;
                        i14++;
                    }
                }
                i15++;
                if (i15 < strArrSplit3.length) {
                    bArr2[i14] = -118;
                    i14++;
                }
            }
            while (i14 < 128) {
                bArr2[i14] = -113;
                i14++;
            }
            System.arraycopy(bArr2, 0, bArr3, (i12 * 128) + 1024, bArr2.length);
            bArr2 = new byte[128];
            i12++;
            c6 = c10;
        }
        return bArr3;
    }
}
