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

org.jcodec.containers.mp4.boxes.VideoSampleEntry Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package org.jcodec.containers.mp4.boxes;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import org.jcodec.common.NIOUtils;
import org.jcodec.common.JCodecUtil;
import org.jcodec.common.tools.ToJSON;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * Describes video payload sample
 * 
 * @author The JCodec project
 * 
 */
public class VideoSampleEntry extends SampleEntry {
    private static final MyFactory FACTORY = new MyFactory();
    private short version;
    private short revision;
    private String vendor;
    private int temporalQual;
    private int spacialQual;
    private short width;
    private short height;
    private float hRes;
    private float vRes;
    private short frameCount;
    private String compressorName;
    private short depth;
    private short clrTbl;

    public VideoSampleEntry(Header atom, short version, short revision, String vendor, int temporalQual,
            int spacialQual, short width, short height, long hRes, long vRes, short frameCount, String compressorName,
            short depth, short drefInd, short clrTbl) {
        super(atom, drefInd);
        factory = FACTORY;
        this.version = version;
        this.revision = revision;
        this.vendor = vendor;
        this.temporalQual = temporalQual;
        this.spacialQual = spacialQual;
        this.width = width;
        this.height = height;
        this.hRes = hRes;
        this.vRes = vRes;
        this.frameCount = frameCount;
        this.compressorName = compressorName;
        this.depth = depth;
        this.clrTbl = clrTbl;
    }

    public VideoSampleEntry(Header atom) {
        super(atom);
        factory = FACTORY;
    }

    public void parse(ByteBuffer input) {
        super.parse(input);

        version = input.getShort();
        revision = input.getShort();
        vendor = NIOUtils.readString(input, 4);
        temporalQual = input.getInt();
        spacialQual = input.getInt();

        width = input.getShort();
        height = input.getShort();

        hRes = (float) input.getInt() / 65536f;
        vRes = (float) input.getInt() / 65536f;

        input.getInt(); // Reserved

        frameCount = input.getShort();

        compressorName = NIOUtils.readPascalString(input, 31);

        depth = input.getShort();

        clrTbl = input.getShort();

        parseExtensions(input);
    }

    @Override
    public void doWrite(ByteBuffer out) {

        super.doWrite(out);

        out.putShort(version);
        out.putShort(revision);
        out.put(JCodecUtil.asciiString(vendor), 0, 4);
        out.putInt(temporalQual);
        out.putInt(spacialQual);

        out.putShort((short) width);
        out.putShort((short) height);

        out.putInt((int) (hRes * 65536));
        out.putInt((int) (vRes * 65536));

        out.putInt(0); // data size

        out.putShort(frameCount);

        NIOUtils.writePascalString(out, compressorName, 31);

        out.putShort(depth);

        out.putShort(clrTbl);

        writeExtensions(out);
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public float gethRes() {
        return hRes;
    }

    public float getvRes() {
        return vRes;
    }

    public long getFrameCount() {
        return frameCount;
    }

    public String getCompressorName() {
        return compressorName;
    }

    public long getDepth() {
        return depth;
    }

    public String getVendor() {
        return vendor;
    }

    public static class MyFactory extends BoxFactory {
        private Map> mappings = new HashMap>();

        public MyFactory() {
            mappings.put(PixelAspectExt.fourcc(), PixelAspectExt.class);
            // mappings.put(AvcCBox.fourcc(), AvcCBox.class);
            mappings.put(ColorExtension.fourcc(), ColorExtension.class);
            mappings.put(GamaExtension.fourcc(), GamaExtension.class);
            mappings.put(CleanApertureExtension.fourcc(), CleanApertureExtension.class);
            mappings.put(FielExtension.fourcc(), FielExtension.class);
        }

        public Class toClass(String fourcc) {
            return mappings.get(fourcc);
        }
    }

    @Override
    public void dump(StringBuilder sb) {
        sb.append(header.getFourcc() + ": {\n");
        sb.append("entry: ");
        ToJSON.toJSON(this, sb, "version", "revision", "vendor", "temporalQual", "spacialQual", "width", "height",
                "hRes", "vRes", "frameCount", "compressorName", "depth", "clrTbl");
        sb.append(",\nexts: [\n");
        dumpBoxes(sb);
        sb.append("\n]\n");
        sb.append("}\n");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy