package androidx.media3.extractor.text.cea;

import android.text.Layout;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import androidx.compose.runtime.ComposerImplKt;
import androidx.compose.runtime.ComposerKt;
import androidx.media3.common.C;
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.common.util.UnstableApi;
import androidx.media3.extractor.AacUtil;
import androidx.media3.extractor.flac.FlacConstants;
import androidx.media3.extractor.metadata.dvbsi.AppInfoTableDecoder;
import androidx.media3.extractor.text.Subtitle;
import androidx.media3.extractor.text.SubtitleInputBuffer;
import androidx.media3.extractor.text.SubtitleOutputBuffer;
import androidx.media3.extractor.ts.PsExtractor;
import com.google.android.gms.location.LocationRequest;
import com.google.firebase.firestore.index.FirestoreIndexValueWriter;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class Cea608Decoder extends CeaDecoder {
    private static final int CC_FIELD_FLAG = 1;
    private static final byte CC_IMPLICIT_DATA_HEADER = -4;
    private static final int CC_MODE_PAINT_ON = 3;
    private static final int CC_MODE_POP_ON = 2;
    private static final int CC_MODE_ROLL_UP = 1;
    private static final int CC_MODE_UNKNOWN = 0;
    private static final int CC_TYPE_FLAG = 2;
    private static final int CC_VALID_FLAG = 4;
    private static final byte CTRL_BACKSPACE = 33;
    private static final byte CTRL_CARRIAGE_RETURN = 45;
    private static final byte CTRL_DELETE_TO_END_OF_ROW = 36;
    private static final byte CTRL_END_OF_CAPTION = 47;
    private static final byte CTRL_ERASE_DISPLAYED_MEMORY = 44;
    private static final byte CTRL_ERASE_NON_DISPLAYED_MEMORY = 46;
    private static final byte CTRL_RESUME_CAPTION_LOADING = 32;
    private static final byte CTRL_RESUME_DIRECT_CAPTIONING = 41;
    private static final byte CTRL_RESUME_TEXT_DISPLAY = 43;
    private static final byte CTRL_ROLL_UP_CAPTIONS_2_ROWS = 37;
    private static final byte CTRL_ROLL_UP_CAPTIONS_3_ROWS = 38;
    private static final byte CTRL_ROLL_UP_CAPTIONS_4_ROWS = 39;
    private static final byte CTRL_TEXT_RESTART = 42;
    private static final int DEFAULT_CAPTIONS_ROW_COUNT = 4;
    public static final long MIN_DATA_CHANNEL_TIMEOUT_MS = 16000;
    private static final int NTSC_CC_CHANNEL_1 = 0;
    private static final int NTSC_CC_CHANNEL_2 = 1;
    private static final int NTSC_CC_FIELD_1 = 0;
    private static final int NTSC_CC_FIELD_2 = 1;
    private static final int STYLE_ITALICS = 7;
    private static final int STYLE_UNCHANGED = 8;
    private static final String TAG = "Cea608Decoder";
    private int captionMode;
    private int captionRowCount;
    private List<Cue> cues;
    private boolean isCaptionValid;
    private boolean isInCaptionService;
    private long lastCueUpdateUs;
    private List<Cue> lastCues;
    private final int packetLength;
    private byte repeatableControlCc1;
    private byte repeatableControlCc2;
    private boolean repeatableControlSet;
    private final int selectedChannel;
    private final int selectedField;
    private final long validDataChannelTimeoutUs;
    private static final int[] ROW_INDICES = {11, 1, 3, 12, 14, 5, 7, 9};
    private static final int[] COLUMN_INDICES = {0, 4, 8, 12, 16, 20, 24, 28};
    private static final int[] STYLE_COLORS = {-1, -16711936, -16776961, -16711681, -65536, -256, -65281};
    private static final int[] BASIC_CHARACTER_SET = {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, LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, 103, LocationRequest.PRIORITY_LOW_POWER, LocationRequest.PRIORITY_NO_POWER, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, AppInfoTableDecoder.APPLICATION_INFORMATION_TABLE_ID, 117, 118, 119, 120, 121, 122, 231, 247, 209, 241, 9632};
    private static final int[] SPECIAL_CHARACTER_SET = {174, 176, PsExtractor.PRIVATE_STREAM_1, 191, 8482, 162, 163, 9834, 224, 32, 232, 226, 234, 238, 244, 251};
    private static final int[] SPECIAL_ES_FR_CHARACTER_SET = {193, 201, 211, 218, 220, 252, 8216, 161, 42, 39, 8212, 169, 8480, 8226, 8220, 8221, PsExtractor.AUDIO_STREAM, 194, 199, 200, ComposerKt.compositionLocalMapKey, ComposerKt.providerValuesKey, 235, ComposerKt.referenceKey, ComposerKt.reuseKey, 239, 212, 217, 249, 219, 171, 187};
    private static final int[] SPECIAL_PT_DE_CHARACTER_SET = {195, 227, 205, ComposerKt.providerMapsKey, 236, 210, 242, 213, 245, 123, ComposerImplKt.nodeKey, 92, 94, 95, 124, 126, 196, 228, 214, 246, 223, 165, 164, 9474, 197, 229, 216, 248, 9484, 9488, 9492, 9496};
    private static final boolean[] ODD_PARITY_BYTE_TABLE = {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};
    private final ParsableByteArray ccData = new ParsableByteArray();
    private final ArrayList<CueBuilder> cueBuilders = new ArrayList<>();
    private CueBuilder currentCueBuilder = new CueBuilder(0, 4);
    private int currentChannel = 0;

    public static final class CueBuilder {
        private static final int BASE_ROW = 15;
        private static final int SCREEN_CHARWIDTH = 32;
        private int captionMode;
        private int captionRowCount;
        private int indent;
        private int row;
        private int tabOffset;
        private final List<CueStyle> cueStyles = new ArrayList();
        private final List<SpannableString> rolledUpCaptions = new ArrayList();
        private final StringBuilder captionStringBuilder = new StringBuilder();

        public static class CueStyle {
            public int start;
            public final int style;
            public final boolean underline;

            public CueStyle(int i6, boolean z6, int i7) {
                this.style = i6;
                this.underline = z6;
                this.start = i7;
            }
        }

        public CueBuilder(int i6, int i7) {
            reset(i6);
            this.captionRowCount = i7;
        }

        private SpannableString buildCurrentLine() {
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(this.captionStringBuilder);
            int length = spannableStringBuilder.length();
            int i6 = -1;
            int i7 = -1;
            int i8 = -1;
            int i9 = -1;
            int i10 = 0;
            int i11 = 0;
            boolean z6 = false;
            while (i10 < this.cueStyles.size()) {
                CueStyle cueStyle = this.cueStyles.get(i10);
                boolean z7 = cueStyle.underline;
                int i12 = cueStyle.style;
                if (i12 != 8) {
                    boolean z8 = i12 == 7;
                    if (i12 != 7) {
                        i9 = Cea608Decoder.STYLE_COLORS[i12];
                    }
                    z6 = z8;
                }
                int i13 = cueStyle.start;
                i10++;
                if (i13 != (i10 < this.cueStyles.size() ? this.cueStyles.get(i10).start : length)) {
                    if (i6 != -1 && !z7) {
                        setUnderlineSpan(spannableStringBuilder, i6, i13);
                        i6 = -1;
                    } else if (i6 == -1 && z7) {
                        i6 = i13;
                    }
                    if (i7 != -1 && !z6) {
                        setItalicSpan(spannableStringBuilder, i7, i13);
                        i7 = -1;
                    } else if (i7 == -1 && z6) {
                        i7 = i13;
                    }
                    if (i9 != i8) {
                        setColorSpan(spannableStringBuilder, i11, i13, i8);
                        i8 = i9;
                        i11 = i13;
                    }
                }
            }
            if (i6 != -1 && i6 != length) {
                setUnderlineSpan(spannableStringBuilder, i6, length);
            }
            if (i7 != -1 && i7 != length) {
                setItalicSpan(spannableStringBuilder, i7, length);
            }
            if (i11 != length) {
                setColorSpan(spannableStringBuilder, i11, length, i8);
            }
            return new SpannableString(spannableStringBuilder);
        }

        private static void setColorSpan(SpannableStringBuilder spannableStringBuilder, int i6, int i7, int i8) {
            if (i8 == -1) {
                return;
            }
            spannableStringBuilder.setSpan(new ForegroundColorSpan(i8), i6, i7, 33);
        }

        private static void setItalicSpan(SpannableStringBuilder spannableStringBuilder, int i6, int i7) {
            spannableStringBuilder.setSpan(new StyleSpan(2), i6, i7, 33);
        }

        private static void setUnderlineSpan(SpannableStringBuilder spannableStringBuilder, int i6, int i7) {
            spannableStringBuilder.setSpan(new UnderlineSpan(), i6, i7, 33);
        }

        public void append(char c6) {
            if (this.captionStringBuilder.length() < 32) {
                this.captionStringBuilder.append(c6);
            }
        }

        public void backspace() {
            int length = this.captionStringBuilder.length();
            if (length > 0) {
                this.captionStringBuilder.delete(length - 1, length);
                for (int size = this.cueStyles.size() - 1; size >= 0; size--) {
                    CueStyle cueStyle = this.cueStyles.get(size);
                    int i6 = cueStyle.start;
                    if (i6 != length) {
                        return;
                    }
                    cueStyle.start = i6 - 1;
                }
            }
        }

        public Cue build(int i6) {
            float f6;
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
            for (int i7 = 0; i7 < this.rolledUpCaptions.size(); i7++) {
                spannableStringBuilder.append((CharSequence) this.rolledUpCaptions.get(i7));
                spannableStringBuilder.append('\n');
            }
            spannableStringBuilder.append((CharSequence) buildCurrentLine());
            if (spannableStringBuilder.length() == 0) {
                return null;
            }
            int i8 = this.indent + this.tabOffset;
            int length = (32 - i8) - spannableStringBuilder.length();
            int i9 = i8 - length;
            if (i6 == Integer.MIN_VALUE) {
                i6 = (this.captionMode != 2 || (Math.abs(i9) >= 3 && length >= 0)) ? (this.captionMode != 2 || i9 <= 0) ? 0 : 2 : 1;
            }
            if (i6 != 1) {
                if (i6 == 2) {
                    i8 = 32 - length;
                }
                f6 = ((i8 / 32.0f) * 0.8f) + 0.1f;
            } else {
                f6 = 0.5f;
            }
            int i10 = this.row;
            if (i10 > 7) {
                i10 -= 17;
            } else if (this.captionMode == 1) {
                i10 -= this.captionRowCount - 1;
            }
            return new Cue.Builder().setText(spannableStringBuilder).setTextAlignment(Layout.Alignment.ALIGN_NORMAL).setLine(i10, 1).setPosition(f6).setPositionAnchor(i6).build();
        }

        public boolean isEmpty() {
            return this.cueStyles.isEmpty() && this.rolledUpCaptions.isEmpty() && this.captionStringBuilder.length() == 0;
        }

        public void reset(int i6) {
            this.captionMode = i6;
            this.cueStyles.clear();
            this.rolledUpCaptions.clear();
            this.captionStringBuilder.setLength(0);
            this.row = 15;
            this.indent = 0;
            this.tabOffset = 0;
        }

        public void rollUp() {
            this.rolledUpCaptions.add(buildCurrentLine());
            this.captionStringBuilder.setLength(0);
            this.cueStyles.clear();
            int iMin = Math.min(this.captionRowCount, this.row);
            while (this.rolledUpCaptions.size() >= iMin) {
                this.rolledUpCaptions.remove(0);
            }
        }

        public void setCaptionMode(int i6) {
            this.captionMode = i6;
        }

        public void setCaptionRowCount(int i6) {
            this.captionRowCount = i6;
        }

        public void setStyle(int i6, boolean z6) {
            this.cueStyles.add(new CueStyle(i6, z6, this.captionStringBuilder.length()));
        }
    }

    public Cea608Decoder(String str, int i6, long j4) {
        if (j4 != C.TIME_UNSET) {
            Assertions.checkArgument(j4 >= MIN_DATA_CHANNEL_TIMEOUT_MS);
            this.validDataChannelTimeoutUs = j4 * 1000;
        } else {
            this.validDataChannelTimeoutUs = C.TIME_UNSET;
        }
        this.packetLength = MimeTypes.APPLICATION_MP4CEA608.equals(str) ? 2 : 3;
        if (i6 == 1) {
            this.selectedChannel = 0;
            this.selectedField = 0;
        } else if (i6 == 2) {
            this.selectedChannel = 1;
            this.selectedField = 0;
        } else if (i6 == 3) {
            this.selectedChannel = 0;
            this.selectedField = 1;
        } else if (i6 != 4) {
            Log.w(TAG, "Invalid channel. Defaulting to CC1.");
            this.selectedChannel = 0;
            this.selectedField = 0;
        } else {
            this.selectedChannel = 1;
            this.selectedField = 1;
        }
        setCaptionMode(0);
        resetCueBuilders();
        this.isInCaptionService = true;
        this.lastCueUpdateUs = C.TIME_UNSET;
    }

    private static char getBasicChar(byte b5) {
        return (char) BASIC_CHARACTER_SET[(b5 & 127) - 32];
    }

    private static int getChannel(byte b5) {
        return (b5 >> 3) & 1;
    }

    private List<Cue> getDisplayCues() {
        int size = this.cueBuilders.size();
        ArrayList arrayList = new ArrayList(size);
        int iMin = 2;
        for (int i6 = 0; i6 < size; i6++) {
            Cue cueBuild = this.cueBuilders.get(i6).build(Integer.MIN_VALUE);
            arrayList.add(cueBuild);
            if (cueBuild != null) {
                iMin = Math.min(iMin, cueBuild.positionAnchor);
            }
        }
        ArrayList arrayList2 = new ArrayList(size);
        for (int i7 = 0; i7 < size; i7++) {
            Cue cue = (Cue) arrayList.get(i7);
            if (cue != null) {
                if (cue.positionAnchor != iMin) {
                    cue = (Cue) Assertions.checkNotNull(this.cueBuilders.get(i7).build(iMin));
                }
                arrayList2.add(cue);
            }
        }
        return arrayList2;
    }

    private static char getExtendedEsFrChar(byte b5) {
        return (char) SPECIAL_ES_FR_CHARACTER_SET[b5 & 31];
    }

    private static char getExtendedPtDeChar(byte b5) {
        return (char) SPECIAL_PT_DE_CHARACTER_SET[b5 & 31];
    }

    private static char getExtendedWestEuropeanChar(byte b5, byte b6) {
        return (b5 & 1) == 0 ? getExtendedEsFrChar(b6) : getExtendedPtDeChar(b6);
    }

    private static char getSpecialNorthAmericanChar(byte b5) {
        return (char) SPECIAL_CHARACTER_SET[b5 & 15];
    }

    private void handleMidrowCtrl(byte b5) {
        this.currentCueBuilder.append(' ');
        this.currentCueBuilder.setStyle((b5 >> 1) & 7, (b5 & 1) == 1);
    }

    private void handleMiscCode(byte b5) {
        if (b5 == 32) {
            setCaptionMode(2);
            return;
        }
        if (b5 == 41) {
            setCaptionMode(3);
            return;
        }
        switch (b5) {
            case FirestoreIndexValueWriter.INDEX_TYPE_REFERENCE /* 37 */:
                setCaptionMode(1);
                setCaptionRowCount(2);
                break;
            case FlacConstants.STREAM_INFO_BLOCK_SIZE /* 38 */:
                setCaptionMode(1);
                setCaptionRowCount(3);
                break;
            case 39:
                setCaptionMode(1);
                setCaptionRowCount(4);
                break;
            default:
                int i6 = this.captionMode;
                if (i6 != 0) {
                    if (b5 != 33) {
                        switch (b5) {
                            case 44:
                                this.cues = Collections.EMPTY_LIST;
                                if (i6 == 1 || i6 == 3) {
                                    resetCueBuilders();
                                }
                                break;
                            case 45:
                                if (i6 == 1 && !this.currentCueBuilder.isEmpty()) {
                                    this.currentCueBuilder.rollUp();
                                    break;
                                }
                                break;
                            case 46:
                                resetCueBuilders();
                                break;
                            case 47:
                                this.cues = getDisplayCues();
                                resetCueBuilders();
                                break;
                        }
                    } else {
                        this.currentCueBuilder.backspace();
                        break;
                    }
                }
                break;
        }
    }

    private void handlePreambleAddressCode(byte b5, byte b6) {
        int i6 = ROW_INDICES[b5 & 7];
        if ((b6 & CTRL_RESUME_CAPTION_LOADING) != 0) {
            i6++;
        }
        if (i6 != this.currentCueBuilder.row) {
            if (this.captionMode != 1 && !this.currentCueBuilder.isEmpty()) {
                CueBuilder cueBuilder = new CueBuilder(this.captionMode, this.captionRowCount);
                this.currentCueBuilder = cueBuilder;
                this.cueBuilders.add(cueBuilder);
            }
            this.currentCueBuilder.row = i6;
        }
        boolean z6 = (b6 & 16) == 16;
        boolean z7 = (b6 & 1) == 1;
        int i7 = (b6 >> 1) & 7;
        this.currentCueBuilder.setStyle(z6 ? 8 : i7, z7);
        if (z6) {
            this.currentCueBuilder.indent = COLUMN_INDICES[i7];
        }
    }

    private static boolean isCtrlCode(byte b5) {
        return (b5 & 224) == 0;
    }

    private static boolean isExtendedWestEuropeanChar(byte b5, byte b6) {
        return (b5 & 246) == 18 && (b6 & 224) == 32;
    }

    private static boolean isMidrowCtrlCode(byte b5, byte b6) {
        return (b5 & 247) == 17 && (b6 & 240) == 32;
    }

    private static boolean isMiscCode(byte b5, byte b6) {
        return (b5 & 246) == 20 && (b6 & 240) == 32;
    }

    private static boolean isPreambleAddressCode(byte b5, byte b6) {
        return (b5 & 240) == 16 && (b6 & 192) == 64;
    }

    private static boolean isRepeatable(byte b5) {
        return (b5 & 240) == 16;
    }

    private boolean isRepeatedCommand(boolean z6, byte b5, byte b6) {
        if (!z6 || !isRepeatable(b5)) {
            this.repeatableControlSet = false;
        } else {
            if (this.repeatableControlSet && this.repeatableControlCc1 == b5 && this.repeatableControlCc2 == b6) {
                this.repeatableControlSet = false;
                return true;
            }
            this.repeatableControlSet = true;
            this.repeatableControlCc1 = b5;
            this.repeatableControlCc2 = b6;
        }
        return false;
    }

    private static boolean isServiceSwitchCommand(byte b5) {
        return (b5 & 246) == 20;
    }

    private static boolean isSpecialNorthAmericanChar(byte b5, byte b6) {
        return (b5 & 247) == 17 && (b6 & 240) == 48;
    }

    private static boolean isTabCtrlCode(byte b5, byte b6) {
        return (b5 & 247) == 23 && b6 >= 33 && b6 <= 35;
    }

    private static boolean isXdsControlCode(byte b5) {
        return 1 <= b5 && b5 <= 15;
    }

    private void maybeUpdateIsInCaptionService(byte b5, byte b6) {
        if (isXdsControlCode(b5)) {
            this.isInCaptionService = false;
            return;
        }
        if (isServiceSwitchCommand(b5)) {
            if (b6 != 32 && b6 != 47) {
                switch (b6) {
                    case FirestoreIndexValueWriter.INDEX_TYPE_REFERENCE /* 37 */:
                    case FlacConstants.STREAM_INFO_BLOCK_SIZE /* 38 */:
                    case 39:
                        break;
                    default:
                        switch (b6) {
                            case AacUtil.AUDIO_OBJECT_TYPE_AAC_XHE /* 42 */:
                            case 43:
                                this.isInCaptionService = false;
                                break;
                        }
                        return;
                }
            }
            this.isInCaptionService = true;
        }
    }

    private void resetCueBuilders() {
        this.currentCueBuilder.reset(this.captionMode);
        this.cueBuilders.clear();
        this.cueBuilders.add(this.currentCueBuilder);
    }

    private void setCaptionMode(int i6) {
        int i7 = this.captionMode;
        if (i7 == i6) {
            return;
        }
        this.captionMode = i6;
        if (i6 == 3) {
            for (int i8 = 0; i8 < this.cueBuilders.size(); i8++) {
                this.cueBuilders.get(i8).setCaptionMode(i6);
            }
            return;
        }
        resetCueBuilders();
        if (i7 == 3 || i6 == 1 || i6 == 0) {
            this.cues = Collections.EMPTY_LIST;
        }
    }

    private void setCaptionRowCount(int i6) {
        this.captionRowCount = i6;
        this.currentCueBuilder.setCaptionRowCount(i6);
    }

    private boolean shouldClearStuckCaptions() {
        return (this.validDataChannelTimeoutUs == C.TIME_UNSET || this.lastCueUpdateUs == C.TIME_UNSET || getPositionUs() - this.lastCueUpdateUs < this.validDataChannelTimeoutUs) ? false : true;
    }

    private boolean updateAndVerifyCurrentChannel(byte b5) {
        if (isCtrlCode(b5)) {
            this.currentChannel = getChannel(b5);
        }
        return this.currentChannel == this.selectedChannel;
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder
    public Subtitle createSubtitle() {
        List<Cue> list = this.cues;
        this.lastCues = list;
        return new CeaSubtitle((List) Assertions.checkNotNull(list));
    }

    /* JADX WARN: Removed duplicated region for block: B:26:0x0063  */
    @Override // androidx.media3.extractor.text.cea.CeaDecoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void decode(SubtitleInputBuffer subtitleInputBuffer) {
        ByteBuffer byteBuffer = (ByteBuffer) Assertions.checkNotNull(subtitleInputBuffer.data);
        this.ccData.reset(byteBuffer.array(), byteBuffer.limit());
        boolean z6 = false;
        while (true) {
            int iBytesLeft = this.ccData.bytesLeft();
            int i6 = this.packetLength;
            if (iBytesLeft < i6) {
                break;
            }
            int unsignedByte = i6 == 2 ? -4 : this.ccData.readUnsignedByte();
            int unsignedByte2 = this.ccData.readUnsignedByte();
            int unsignedByte3 = this.ccData.readUnsignedByte();
            if ((unsignedByte & 2) == 0 && (unsignedByte & 1) == this.selectedField) {
                byte b5 = (byte) (unsignedByte2 & 127);
                byte b6 = (byte) (unsignedByte3 & 127);
                if (b5 != 0 || b6 != 0) {
                    boolean z7 = this.isCaptionValid;
                    if ((unsignedByte & 4) == 4) {
                        boolean[] zArr = ODD_PARITY_BYTE_TABLE;
                        boolean z8 = zArr[unsignedByte2] && zArr[unsignedByte3];
                        this.isCaptionValid = z8;
                        if (!isRepeatedCommand(z8, b5, b6)) {
                            if (this.isCaptionValid) {
                                maybeUpdateIsInCaptionService(b5, b6);
                                if (this.isInCaptionService && updateAndVerifyCurrentChannel(b5)) {
                                    if (!isCtrlCode(b5)) {
                                        this.currentCueBuilder.append(getBasicChar(b5));
                                        if ((b6 & 224) != 0) {
                                            this.currentCueBuilder.append(getBasicChar(b6));
                                        }
                                    } else if (isSpecialNorthAmericanChar(b5, b6)) {
                                        this.currentCueBuilder.append(getSpecialNorthAmericanChar(b6));
                                    } else if (isExtendedWestEuropeanChar(b5, b6)) {
                                        this.currentCueBuilder.backspace();
                                        this.currentCueBuilder.append(getExtendedWestEuropeanChar(b5, b6));
                                    } else if (isMidrowCtrlCode(b5, b6)) {
                                        handleMidrowCtrl(b6);
                                    } else if (isPreambleAddressCode(b5, b6)) {
                                        handlePreambleAddressCode(b5, b6);
                                    } else if (isTabCtrlCode(b5, b6)) {
                                        this.currentCueBuilder.tabOffset = b6 - 32;
                                    } else if (isMiscCode(b5, b6)) {
                                        handleMiscCode(b6);
                                    }
                                    z6 = true;
                                }
                            } else if (z7) {
                                resetCueBuilders();
                                z6 = true;
                            }
                        }
                    }
                }
            }
        }
        if (z6) {
            int i7 = this.captionMode;
            if (i7 == 1 || i7 == 3) {
                this.cues = getDisplayCues();
                this.lastCueUpdateUs = getPositionUs();
            }
        }
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder, androidx.media3.decoder.Decoder
    public /* bridge */ /* synthetic */ SubtitleInputBuffer dequeueInputBuffer() {
        return super.dequeueInputBuffer();
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder, androidx.media3.decoder.Decoder
    public void flush() {
        super.flush();
        this.cues = null;
        this.lastCues = null;
        setCaptionMode(0);
        setCaptionRowCount(4);
        resetCueBuilders();
        this.isCaptionValid = false;
        this.repeatableControlSet = false;
        this.repeatableControlCc1 = (byte) 0;
        this.repeatableControlCc2 = (byte) 0;
        this.currentChannel = 0;
        this.isInCaptionService = true;
        this.lastCueUpdateUs = C.TIME_UNSET;
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder, androidx.media3.decoder.Decoder
    public String getName() {
        return TAG;
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder
    public boolean isNewSubtitleDataAvailable() {
        return this.cues != this.lastCues;
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder
    public /* bridge */ /* synthetic */ void queueInputBuffer(SubtitleInputBuffer subtitleInputBuffer) {
        super.queueInputBuffer(subtitleInputBuffer);
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder, androidx.media3.decoder.Decoder
    public void release() {
    }

    @Override // androidx.media3.extractor.text.cea.CeaDecoder, androidx.media3.extractor.text.SubtitleDecoder
    public /* bridge */ /* synthetic */ void setPositionUs(long j4) {
        super.setPositionUs(j4);
    }

    /* JADX WARN: Can't rename method to resolve collision */
    @Override // androidx.media3.extractor.text.cea.CeaDecoder, androidx.media3.decoder.Decoder
    public SubtitleOutputBuffer dequeueOutputBuffer() {
        SubtitleOutputBuffer availableOutputBuffer;
        SubtitleOutputBuffer subtitleOutputBufferDequeueOutputBuffer = super.dequeueOutputBuffer();
        if (subtitleOutputBufferDequeueOutputBuffer != null) {
            return subtitleOutputBufferDequeueOutputBuffer;
        }
        if (!shouldClearStuckCaptions() || (availableOutputBuffer = getAvailableOutputBuffer()) == null) {
            return null;
        }
        this.cues = Collections.EMPTY_LIST;
        this.lastCueUpdateUs = C.TIME_UNSET;
        availableOutputBuffer.setContent(getPositionUs(), createSubtitle(), Long.MAX_VALUE);
        return availableOutputBuffer;
    }
}
