Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * @author The JCodec project
 * 
 */

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**
     * Returns next segment between two MPEG marker
     * 
     * i.e. searches for the next marker if the stream is not at the marker
     * boundary already
     * 
     * @param buf
     * @return
     */
    public static final ByteBuffer nextSegment(ByteBuffer buf) {
        gotoMarker(buf, 0, 0x100, 0x1ff);
        return gotoMarker(buf, 1, 0x100, 0x1ff);
    }

    /**
     * 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
     */
    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;
    }
}