src/main/java/org/jcodec/codecs/mpeg12/MPEGUtil.java
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
        if (!buf.hasRemaining())
            return null;

        int from = buf.position();
        ByteBuffer result = buf.slice();
        result.order(ByteOrder.BIG_ENDIAN);

        int val = 0xffffffff;
        while (buf.hasRemaining()) {
            val = (val << 8) | (buf.get() & 0xff);
            if (val >= mmin && val <= mmax) {
                if (n == 0) {
                    buf.position(buf.position() - 4);
                    result.limit(buf.position() - from);
                    break;
                }
                --n;
            }
        }
        return result;
    }
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
        if (!buf.hasRemaining())
            return null;

        int from = buf.position();
        ByteBuffer result = buf.slice();
        result.order(ByteOrder.BIG_ENDIAN);

        int val = 0xffffffff;
        while (buf.hasRemaining()) {
            val = (val << 8) | (buf.get() & 0xff);
            if (val >= mmin && val <= mmax) {
                if (n == 0) {
                    buf.position(buf.position() - 4);
                    result.limit(buf.position() - from);
                    break;
                }
                --n;
            }
        }
        return result;
    }
public static final ByteBuffer gotoMarker ( ByteBuffer buf , int n , int mmin , int mmax ) { if ( ! buf . hasRemaining ( ) ) return null ; int from = buf . position ( ) ; ByteBuffer result = buf . slice ( ) ; result . order ( ByteOrder . BIG_ENDIAN ) ; int val = 0xffffffff ; while ( buf . hasRemaining ( ) ) { val = ( val << 8 ) | ( buf . get ( ) & 0xff ) ; if ( val >= mmin && val <= mmax ) { if ( n == 0 ) { buf . position ( buf . position ( ) - 4 ) ; result . limit ( buf . position ( ) - from ) ; break ; } -- n ; } } return result ; }
Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that
preceeds it as a ByteBuffer slice

Segment byte order is always little endian

@param buf
@return
Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that preceeds it as a ByteBuffer slice