-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from qqia/release_1.0.6
Release 1.0.6
- Loading branch information
Showing
13 changed files
with
565 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,4 +74,4 @@ public void visit(com.amazonaws.kinesisvideo.parser.mkv.MkvDataElement dataEleme | |
} | ||
} | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/com/amazonaws/kinesisvideo/parser/utilities/H264FrameDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
A copy of the License is located at | ||
http://aws.amazon.com/apache2.0/ | ||
or in the "license" file accompanying this file. | ||
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.amazonaws.kinesisvideo.parser.utilities; | ||
|
||
import com.amazonaws.kinesisvideo.parser.mkv.Frame; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jcodec.codecs.h264.H264Decoder; | ||
import org.jcodec.codecs.h264.mp4.AvcCBox; | ||
import org.jcodec.common.model.ColorSpace; | ||
import org.jcodec.common.model.Picture; | ||
import org.jcodec.scale.AWTUtil; | ||
import org.jcodec.scale.Transform; | ||
import org.jcodec.scale.Yuv420jToRgb; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.jcodec.codecs.h264.H264Utils.splitMOVPacket; | ||
|
||
@Slf4j | ||
public class H264FrameDecoder implements FrameVisitor.FrameProcessor { | ||
|
||
private final H264Decoder decoder = new H264Decoder(); | ||
private final Transform transform = new Yuv420jToRgb(); | ||
|
||
@Getter | ||
private int frameCount; | ||
|
||
private byte[] codecPrivateData; | ||
|
||
@Override | ||
public void process(Frame frame, MkvTrackMetadata trackMetadata, Optional<FragmentMetadata> fragmentMetadata) { | ||
decodeH264Frame(frame, trackMetadata); | ||
} | ||
|
||
protected BufferedImage decodeH264Frame(Frame frame, MkvTrackMetadata trackMetadata) { | ||
ByteBuffer frameBuffer = frame.getFrameData(); | ||
int pixelWidth = trackMetadata.getPixelWidth().get().intValue(); | ||
int pixelHeight = trackMetadata.getPixelHeight().get().intValue(); | ||
codecPrivateData = trackMetadata.getCodecPrivateData().array(); | ||
log.debug("Decoding frames ... "); | ||
// Read the bytes that appear to comprise the header | ||
// See: https://www.matroska.org/technical/specs/index.html#simpleblock_structure | ||
|
||
Picture rgb = Picture.create(pixelWidth, pixelHeight, ColorSpace.RGB); | ||
BufferedImage bufferedImage = new BufferedImage(pixelWidth, pixelHeight, BufferedImage.TYPE_3BYTE_BGR); | ||
AvcCBox avcC = AvcCBox.parseAvcCBox(ByteBuffer.wrap(codecPrivateData)); | ||
|
||
decoder.addSps(avcC.getSpsList()); | ||
decoder.addPps(avcC.getPpsList()); | ||
|
||
Picture buf = Picture.create(pixelWidth, pixelHeight, ColorSpace.YUV420J); | ||
List<ByteBuffer> byteBuffers = splitMOVPacket(frameBuffer, avcC); | ||
Picture pic = decoder.decodeFrameFromNals(byteBuffers, buf.getData()); | ||
|
||
if (pic != null) { | ||
// Work around for color issues in JCodec | ||
// https://github.com/jcodec/jcodec/issues/59 | ||
// https://github.com/jcodec/jcodec/issues/192 | ||
byte[][] dataTemp = new byte[3][pic.getData().length]; | ||
dataTemp[0] = pic.getPlaneData(0); | ||
dataTemp[1] = pic.getPlaneData(2); | ||
dataTemp[2] = pic.getPlaneData(1); | ||
|
||
Picture tmpBuf = Picture.createPicture(pixelWidth, pixelHeight, dataTemp, ColorSpace.YUV420J); | ||
transform.transform(tmpBuf, rgb); | ||
AWTUtil.toBufferedImage(rgb, bufferedImage); | ||
frameCount++; | ||
} | ||
return bufferedImage; | ||
} | ||
|
||
public ByteBuffer getCodecPrivateData() { | ||
return ByteBuffer.wrap(codecPrivateData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/amazonaws/kinesisvideo/parser/utilities/SimpleFrameVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
A copy of the License is located at | ||
http://aws.amazon.com/apache2.0/ | ||
or in the "license" file accompanying this file. | ||
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.amazonaws.kinesisvideo.parser.utilities; | ||
|
||
import com.amazonaws.kinesisvideo.parser.ebml.MkvTypeInfos; | ||
import com.amazonaws.kinesisvideo.parser.mkv.Frame; | ||
import com.amazonaws.kinesisvideo.parser.mkv.MkvElementVisitException; | ||
import com.amazonaws.kinesisvideo.parser.mkv.MkvElementVisitor; | ||
import com.amazonaws.kinesisvideo.parser.mkv.MkvValue; | ||
import com.amazonaws.kinesisvideo.parser.mkv.visitors.CompositeMkvElementVisitor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.apache.commons.lang3.Validate; | ||
|
||
import java.math.BigInteger; | ||
|
||
/** | ||
* Fragment metdata tags will not be present as there is no tag when the data (mkv/webm) is not | ||
* retrieved from Kinesis video. | ||
* As a result, user cannot get the timestamp from fragment metadata tags. | ||
* This class is used to provide a FrameVisitor to process frame as well as time elements for timestamp. | ||
**/ | ||
|
||
@Slf4j | ||
public class SimpleFrameVisitor extends CompositeMkvElementVisitor { | ||
private final FrameVisitorInternal frameVisitorInternal; | ||
private final FrameProcessor frameProcessor; | ||
|
||
private SimpleFrameVisitor(FrameProcessor frameProcessor) { | ||
this.frameProcessor = frameProcessor; | ||
frameVisitorInternal = new FrameVisitorInternal(); | ||
childVisitors.add(frameVisitorInternal); | ||
|
||
} | ||
public static SimpleFrameVisitor create(FrameProcessor frameProcessor) { | ||
return new SimpleFrameVisitor(frameProcessor); | ||
} | ||
public interface FrameProcessor { | ||
default void process(Frame frame, long clusterTimeCode, long timeCodeScale) { | ||
throw new NotImplementedException("Default FrameVisitor with No Fragement MetaData"); | ||
} | ||
} | ||
|
||
private class FrameVisitorInternal extends MkvElementVisitor { | ||
private long clusterTimeCode = -1; | ||
private long timeCodeScale = -1; | ||
@Override | ||
public void visit(com.amazonaws.kinesisvideo.parser.mkv.MkvStartMasterElement startMasterElement) | ||
throws com.amazonaws.kinesisvideo.parser.mkv.MkvElementVisitException { | ||
} | ||
|
||
@Override | ||
public void visit(com.amazonaws.kinesisvideo.parser.mkv.MkvEndMasterElement endMasterElement) | ||
throws com.amazonaws.kinesisvideo.parser.mkv.MkvElementVisitException { | ||
} | ||
|
||
@Override | ||
public void visit(com.amazonaws.kinesisvideo.parser.mkv.MkvDataElement dataElement) | ||
throws com.amazonaws.kinesisvideo.parser.mkv.MkvElementVisitException { | ||
|
||
if (MkvTypeInfos.TIMECODE.equals(dataElement.getElementMetaData().getTypeInfo())) { | ||
clusterTimeCode = ((BigInteger) dataElement.getValueCopy().getVal()).longValue(); | ||
} | ||
|
||
if (MkvTypeInfos.TIMECODESCALE.equals(dataElement.getElementMetaData().getTypeInfo())) { | ||
timeCodeScale = ((BigInteger) dataElement.getValueCopy().getVal()).longValue(); | ||
} | ||
|
||
if (MkvTypeInfos.SIMPLEBLOCK.equals(dataElement.getElementMetaData().getTypeInfo())) { | ||
if (clusterTimeCode == -1 || timeCodeScale == -1) { | ||
throw new MkvElementVisitException("No timeCodeScale or timeCode found", new RuntimeException()); | ||
} | ||
final MkvValue<Frame> frame = dataElement.getValueCopy(); | ||
Validate.notNull(frame); | ||
frameProcessor.process(frame.getVal(), clusterTimeCode, timeCodeScale); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.