All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcodec.movtool.streaming.tracks.ClipTrack Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package org.jcodec.movtool.streaming.tracks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jcodec.movtool.streaming.CodecMeta;
import org.jcodec.movtool.streaming.VirtualPacket;
import org.jcodec.movtool.streaming.VirtualTrack;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * Returns clips track to a certain frame range
 * 
 * @author The JCodec project
 * 
 */
public class ClipTrack implements VirtualTrack {

    private VirtualTrack src;
    private int from;
    private int to;
    private int startFrame;
    private double startPts;
    private List gop;
    private boolean eof;

    public ClipTrack(VirtualTrack src, int frameFrom, int frameTo) {
        if (frameTo <= frameFrom)
            throw new IllegalArgumentException("Clipping negative or zero frames.");
        this.src = src;
        this.from = frameFrom;
        this.to = frameTo;
    }

    @Override
    public VirtualPacket nextPacket() throws IOException {
        if (eof)
            return null;

        VirtualPacket nextPacket;

        if (gop == null) {
            gop = getGop(src, from);
            startPts = gop.get(0).getPts();
            startFrame = gop.get(0).getFrameNo();
        }

        nextPacket = gop.size() > 0 ? gop.remove(0) : src.nextPacket();

        if (nextPacket == null || nextPacket.getFrameNo() >= to) {
            eof = true;
            return null;
        }

        return new ClipPacket(nextPacket);
    }

    protected List getGop(VirtualTrack src, int from) throws IOException {
        List result = new ArrayList();
        VirtualPacket nextPacket;
        do {
            nextPacket = src.nextPacket();
            if (nextPacket != null) {
                if (nextPacket.isKeyframe())
                    result.clear();
                result.add(nextPacket);
            }
        } while (nextPacket != null && nextPacket.getFrameNo() < from);
        return result;
    }

    @Override
    public CodecMeta getCodecMeta() {
        return src.getCodecMeta();
    }

    @Override
    public VirtualEdit[] getEdits() {
        return null;
    }

    @Override
    public int getPreferredTimescale() {
        return src.getPreferredTimescale();
    }

    @Override
    public void close() throws IOException {
        src.close();
    }

    public class ClipPacket extends VirtualPacketWrapper {

        public ClipPacket(VirtualPacket src) {
            super(src);
        }

        @Override
        public double getPts() {
            return super.getPts() - startPts;
        }

        @Override
        public int getFrameNo() {
            return super.getFrameNo() - startFrame;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy