Java Code Examples for org.jcodec.codecs.h264.H264Utils

The following examples show how to use org.jcodec.codecs.h264.H264Utils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ImageToVideo   Source File: SequenceEncoderMp4.java    License: Apache License 2.0 6 votes vote down vote up
public void encodeNativeFrame(Picture pic) throws IOException {
    if (toEncode == null) {
        toEncode = Picture.create(pic.getWidth() , pic.getHeight() , encoder.getSupportedColorSpaces()[0]);
    }

    // Perform conversion
    transform.transform(pic, toEncode);

    // Encode image into H.264 frame, the result is stored in '_out' buffer
    _out.clear();
    ByteBuffer result = encoder.encodeFrame(toEncode, _out);

    // Based on the frame above form correct MP4 packet
    spsList.clear();
    ppsList.clear();
    H264Utils.wipePS(result, spsList, ppsList);
    H264Utils.encodeMOVPacket(result);

    // Add packet to video track
    outTrack.addFrame(new MP4Packet(result, frameNo, timeScale, 1, frameNo, true, null, frameNo, 0));

    frameNo++;
}
 
Example 2
Source Project: ImageToVideo   Source File: SequenceEncoderMp4.java    License: Apache License 2.0 5 votes vote down vote up
public void finish() throws IOException {
    // Push saved SPS/PPS to a special storage in MP4
    outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList, 4));

    // Write MP4 header and finalize recording
    muxer.writeHeader();
    NIOUtils.closeQuietly(ch);
}
 
Example 3
public H264FrameEncoder(final int width, final int height, final int bitRate) {
    this.encoder = new H264Encoder(new H264FixedRateControl(bitRate));
    this.out = ByteBuffer.allocate(width * height * 6);
    this.frameNumber = 0;

    final Size size = new Size(width, height);
    sps = this.encoder.initSPS(size);
    pps = this.encoder.initPPS();

    final ByteBuffer spsBuffer = ByteBuffer.allocate(512);
    this.sps.write(spsBuffer);
    spsBuffer.flip();

    final ByteBuffer serialSps = ByteBuffer.allocate(512);
    this.sps.write(serialSps);
    serialSps.flip();
    H264Utils.escapeNALinplace(serialSps);

    final ByteBuffer serialPps = ByteBuffer.allocate(512);
    this.pps.write(serialPps);
    serialPps.flip();
    H264Utils.escapeNALinplace(serialPps);

    final ByteBuffer serialAvcc = ByteBuffer.allocate(512);
    final AvcCBox avcC = AvcCBox.createAvcCBox(this.sps.profileIdc, 0, this.sps.levelIdc, 4,
            asList(serialSps), asList(serialPps));
    avcC.doWrite(serialAvcc);
    serialAvcc.flip();
    cpd = new byte[serialAvcc.remaining()];
    serialAvcc.get(cpd);
}
 
Example 4
public void addFrame(Picture pic) throws IOException {
    if (toEncode == null) {
        toEncode = Picture.create(pic.getWidth(), pic.getHeight(), encoder.getSupportedColorSpaces()[0]);
    }

    if (_out == null)
    {
        // Allocate a buffer big enough to hold output frames
        _out = ByteBuffer.allocate(pic.getWidth() * pic.getHeight() * 6);

    }
    
    // Perform conversion
    transform.transform(pic, toEncode);

    // Encode image into H.264 frame, the result is stored in '_out' buffer
    _out.clear();
    ByteBuffer result = encoder.encodeFrame(toEncode, _out);

    // Based on the frame above form correct MP4 packet
    spsList.clear();
    ppsList.clear();
    H264Utils.wipePS(result, spsList, ppsList);
    H264Utils.encodeMOVPacket(result);

    // Add packet to video track
    outTrack.addFrame(new MP4Packet(result, frameNo, 25, 1, frameNo, true, null, frameNo, 0));

    frameNo++;
}
 
Example 5
public void finish() throws IOException {
    // Push saved SPS/PPS to a special storage in MP4
    outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList, 4));

    audioTrack.addSampleEntry(MP4Muxer.audioSampleEntry(af));

    // Write MP4 header and finalize recording
    muxer.writeHeader();
    NIOUtils.closeQuietly(ch);
}