NalUnitUtil { public static void discardToSps(ByteBuffer data) { int length = data.position(); int consecutiveZeros = 0; int offset = 0; while (offset + 1 < length) { int value = data.get(offset) & 0xFF; if (consecutiveZeros == 3) { if (value == 1 && (data.get(offset + 1) & 0x1F) == H264_NAL_UNIT_TYPE_SPS) { ByteBuffer offsetData = data.duplicate(); offsetData.position(offset - 3); offsetData.limit(length); data.position(0); data.put(offsetData); return; } } else if (value == 0) { consecutiveZeros++; } if (value != 0) { consecutiveZeros = 0; } offset++; } data.clear(); } private  NalUnitUtil(); static int unescapeStream(byte[] data, int limit); static void discardToSps(ByteBuffer data); static boolean isNalUnitSei(String mimeType, byte nalUnitHeaderFirstByte); static int getNalUnitType(byte[] data, int offset); static int getH265NalUnitType(byte[] data, int offset); static SpsData parseSpsNalUnit(byte[] nalData, int nalOffset, int nalLimit); static PpsData parsePpsNalUnit(byte[] nalData, int nalOffset, int nalLimit); static int findNalUnit(byte[] data, int startOffset, int endOffset,
      boolean[] prefixFlags); static void clearPrefixFlags(boolean[] prefixFlags); static final byte[] NAL_START_CODE; static final int EXTENDED_SAR; static final float[] ASPECT_RATIO_IDC_VALUES; }
@Test public void testDiscardToSps() { assertDiscardToSpsMatchesExpected("", ""); assertDiscardToSpsMatchesExpected("00", ""); assertDiscardToSpsMatchesExpected("FFFF000001", ""); assertDiscardToSpsMatchesExpected("00000001", ""); assertDiscardToSpsMatchesExpected("00000001FF67", ""); assertDiscardToSpsMatchesExpected("00000001000167", ""); assertDiscardToSpsMatchesExpected("0000000167", "0000000167"); assertDiscardToSpsMatchesExpected("0000000167FF", "0000000167FF"); assertDiscardToSpsMatchesExpected("0000000167FF", "0000000167FF"); assertDiscardToSpsMatchesExpected("0000000167FF000000016700", "0000000167FF000000016700"); assertDiscardToSpsMatchesExpected("000000000167FF", "0000000167FF"); assertDiscardToSpsMatchesExpected("0001670000000167FF", "0000000167FF"); assertDiscardToSpsMatchesExpected("FF00000001660000000167FF", "0000000167FF"); }