GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge remote-tracking branch 'origin/stjs-prepare' into h264decode_optim · codice/jcodec@30bf2db

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • main/java/org/jcodec
      • api
        • FrameGrab8Bit.java
      • common/io
        • BitReader.java
    • test/java/org/jcodec
      • Utils.java
      • api
        • FrameGrabTest.java
      • codecs/vp8
        • VP8DecoderTest.java
      • containers/mkv
        • AudioTrackTest.java
        • CuesFactoryTest.java
        • MKVMuxerTest.java
        • MKVTestSuite.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/org/jcodec/api/FrameGrab8Bit.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/api/FrameGrab8Bit.java
+110-65Lines changed: 110 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@
66

77
import org.jcodec.api.specific.AVCMP4Adaptor;
88
import org.jcodec.api.specific.ContainerAdaptor;
9+
import org.jcodec.codecs.h264.H264Decoder;
10+
import org.jcodec.codecs.mpeg12.MPEGDecoder;
11+
import org.jcodec.codecs.prores.ProresDecoder;
12+
import org.jcodec.common.Codec;
913
import org.jcodec.common.DemuxerTrack;
1014
import org.jcodec.common.DemuxerTrackMeta;
1115
import org.jcodec.common.JCodecUtil;
1216
import org.jcodec.common.JCodecUtil.Format;
1317
import org.jcodec.common.SeekableDemuxerTrack;
18+
import org.jcodec.common.VideoDecoder;
1419
import org.jcodec.common.io.FileChannelWrapper;
1520
import org.jcodec.common.io.NIOUtils;
1621
import org.jcodec.common.io.SeekableByteChannel;
1722
import org.jcodec.common.model.Packet;
18-
import org.jcodec.common.model.Picture;
1923
import org.jcodec.common.model.Picture8Bit;
24+
import org.jcodec.common.model.Size;
25+
import org.jcodec.containers.mp4.MP4Packet;
26+
import org.jcodec.containers.mp4.boxes.SampleEntry;
27+
import org.jcodec.containers.mp4.demuxer.AbstractMP4DemuxerTrack;
2028
import org.jcodec.containers.mp4.demuxer.MP4Demuxer;
2129

2230
/**
@@ -41,50 +49,18 @@ public class FrameGrab8Bit {
4149
private ContainerAdaptor decoder;
4250
private ThreadLocal<byte[][]> buffers;
4351

44-
private static int _detectKeyFrame(DemuxerTrack videoTrack, int start) throws IOException {
45-
int[] seekFrames = videoTrack.getMeta().getSeekFrames();
46-
if (seekFrames == null)
47-
return start;
48-
int prev = seekFrames[0];
49-
for (int i = 1; i < seekFrames.length; i++) {
50-
if (seekFrames[i] > start)
51-
break;
52-
prev = seekFrames[i];
53-
}
54-
return prev;
55-
}
56-
57-
private static ContainerAdaptor _detectDecoder(SeekableDemuxerTrack sdt, Packet frame) throws JCodecException {
58-
DemuxerTrackMeta meta = sdt.getMeta();
59-
switch (meta.getCodec()) {
60-
case H264:
61-
return new AVCMP4Adaptor(meta);
62-
default:
63-
throw new UnsupportedFormatException("Codec is not supported");
64-
}
65-
}
66-
67-
static ContainerAdaptor detectDecoder(SeekableDemuxerTrack sdt) throws IOException, JCodecException {
68-
int curFrame = (int) sdt.getCurFrame();
69-
int keyFrame = _detectKeyFrame(sdt, curFrame);
70-
sdt.gotoFrame(keyFrame);
71-
72-
Packet frame = sdt.nextFrame();
73-
ContainerAdaptor decoder = _detectDecoder(sdt, frame);
74-
return decoder;
75-
}
76-
77-
public static FrameGrab8Bit createFrameGrab(SeekableByteChannel _in) throws IOException, JCodecException {
52+
public static FrameGrab8Bit createFrameGrab8Bit(SeekableByteChannel _in) throws IOException, JCodecException {
53+
FrameGrab8Bit fg = new FrameGrab8Bit(null, null);
54+
fg.buffers = new ThreadLocal<byte[][]>();
7855
ByteBuffer header = ByteBuffer.allocate(65536);
7956
_in.read(header);
8057
header.flip();
8158
Format detectFormat = JCodecUtil.detectFormatBuffer(header);
82-
SeekableDemuxerTrack videoTrack = null;
8359

8460
switch (detectFormat) {
8561
case MOV:
8662
MP4Demuxer d1 = new MP4Demuxer(_in);
87-
videoTrack = d1.getVideoTrack();
63+
fg.videoTrack = d1.getVideoTrack();
8864
break;
8965
case MPEG_PS:
9066
throw new UnsupportedFormatException("MPEG PS is temporarily unsupported.");
@@ -93,16 +69,11 @@ public static FrameGrab8Bit createFrameGrab(SeekableByteChannel _in) throws IOEx
9369
default:
9470
throw new UnsupportedFormatException("Container format is not supported by JCodec");
9571
}
96-
ContainerAdaptor decoder = detectDecoder(videoTrack);
97-
FrameGrab8Bit fg = new FrameGrab8Bit(videoTrack, decoder);
72+
fg.decodeLeadingFrames();
9873
return fg;
9974
}
10075

10176
public FrameGrab8Bit(SeekableDemuxerTrack videoTrack, ContainerAdaptor decoder) {
102-
if (decoder == null || videoTrack == null) {
103-
throw new NullPointerException();
104-
}
105-
this.buffers = new ThreadLocal<byte[][]>();
10677
this.videoTrack = videoTrack;
10778
this.decoder = decoder;
10879
}
@@ -206,6 +177,8 @@ private void decodeLeadingFrames() throws IOException, JCodecException {
206177
sdt.gotoFrame(keyFrame);
207178

208179
Packet frame = sdt.nextFrame();
180+
if (decoder == null)
181+
decoder = detectDecoder(sdt, frame);
209182

210183
while (frame.getFrameNo() < curFrame) {
211184
decoder.decodeFrame8Bit(frame, getBuffer());
@@ -236,6 +209,16 @@ private int detectKeyFrame(int start) throws IOException {
236209
return prev;
237210
}
238211

212+
private ContainerAdaptor detectDecoder(SeekableDemuxerTrack videoTrack, Packet frame) throws JCodecException {
213+
DemuxerTrackMeta meta = videoTrack.getMeta();
214+
switch (meta.getCodec()) {
215+
case H264:
216+
return new AVCMP4Adaptor(meta);
217+
default:
218+
throw new UnsupportedFormatException("Codec is not supported");
219+
}
220+
}
221+
239222
/**
240223
* Get frame at current position in JCodec native image
241224
*
@@ -266,26 +249,36 @@ public Picture8Bit getNativeFrame() throws IOException {
266249
}
267250

268251
/**
269-
* Gets info about the media
252+
* Get frame at a specified second as JCodec image
270253
*
254+
* @param file
255+
* @param second
271256
* @return
257+
* @throws IOException
258+
* @throws JCodecException
272259
*/
273-
public MediaInfo getMediaInfo() {
274-
return decoder.getMediaInfo();
260+
public static Picture8Bit getFrameAtSec(File file, double second) throws IOException, JCodecException {
261+
FileChannelWrapper ch = null;
262+
try {
263+
ch = NIOUtils.readableChannel(file);
264+
return createFrameGrab8Bit(ch).seekToSecondPrecise(second).getNativeFrame();
265+
} finally {
266+
NIOUtils.closeQuietly(ch);
267+
}
275268
}
276269

277270
/**
278-
* Get frame at a specified frame number as JCodec image
271+
* Get frame at a specified second as JCodec image
279272
*
280273
* @param file
281274
* @param second
282275
* @return
283276
* @throws IOException
284277
* @throws JCodecException
285278
*/
286-
public static Picture8Bit getFrameFromChannel(SeekableByteChannel ch, int frameNumber)
279+
public static Picture8Bit getFrameFromChannelAtSec(SeekableByteChannel file, double second)
287280
throws JCodecException, IOException {
288-
return createFrameGrab(ch).seekToFramePrecise(frameNumber).getNativeFrame();
281+
return createFrameGrab8Bit(file).seekToSecondPrecise(second).getNativeFrame();
289282
}
290283

291284
/**
@@ -301,42 +294,94 @@ public static Picture8Bit getFrameFromFile(File file, int frameNumber) throws IO
301294
FileChannelWrapper ch = null;
302295
try {
303296
ch = NIOUtils.readableChannel(file);
304-
return createFrameGrab(ch).seekToFramePrecise(frameNumber).getNativeFrame();
297+
return createFrameGrab8Bit(ch).seekToFramePrecise(frameNumber).getNativeFrame();
305298
} finally {
306299
NIOUtils.closeQuietly(ch);
307300
}
308301
}
309302

310303
/**
311-
* Get frame at a specified second as JCodec image
304+
* Get frame at a specified frame number as JCodec image
312305
*
313-
* @param ch
306+
* @param file
314307
* @param second
315308
* @return
316309
* @throws IOException
317310
* @throws JCodecException
318311
*/
319-
public static Picture8Bit getFrameAtSecFromChannel(SeekableByteChannel ch, double second)
312+
public static Picture8Bit getFrameFromChannel(SeekableByteChannel file, int frameNumber)
320313
throws JCodecException, IOException {
321-
return createFrameGrab(ch).seekToSecondPrecise(second).getNativeFrame();
314+
return createFrameGrab8Bit(file).seekToFramePrecise(frameNumber).getNativeFrame();
322315
}
323316

324317
/**
325-
* Get frame at a specified second as JCodec image
318+
* Get a specified frame by number from an already open demuxer track
326319
*
327-
* @param file
328-
* @param second
320+
* @param vt
321+
* @param decoder
322+
* @param frameNumber
329323
* @return
330324
* @throws IOException
331325
* @throws JCodecException
332326
*/
333-
public static Picture8Bit getFrameAtSec(File file, double second) throws IOException, JCodecException {
334-
FileChannelWrapper ch = null;
335-
try {
336-
ch = NIOUtils.readableChannel(file);
337-
return createFrameGrab(ch).seekToSecondPrecise(second).getNativeFrame();
338-
} finally {
339-
NIOUtils.closeQuietly(ch);
340-
}
327+
public static Picture8Bit getNativeFrame(SeekableDemuxerTrack vt, ContainerAdaptor decoder, int frameNumber)
328+
throws IOException, JCodecException {
329+
return new FrameGrab8Bit(vt, decoder).seekToFramePrecise(frameNumber).getNativeFrame();
330+
}
331+
332+
/**
333+
* Get a specified frame by second from an already open demuxer track
334+
*
335+
* @param vt
336+
* @param decoder
337+
* @param frameNumber
338+
* @return
339+
* @throws IOException
340+
* @throws JCodecException
341+
*/
342+
public static Picture8Bit getNativeFrameAtSec(SeekableDemuxerTrack vt, ContainerAdaptor decoder, double second)
343+
throws IOException, JCodecException {
344+
return new FrameGrab8Bit(vt, decoder).seekToSecondPrecise(second).getNativeFrame();
345+
}
346+
347+
/**
348+
* Get a specified frame by number from an already open demuxer track (
349+
* sloppy mode, i.e. nearest keyframe )
350+
*
351+
* @param vt
352+
* @param decoder
353+
* @param frameNumber
354+
* @return
355+
* @throws IOException
356+
* @throws JCodecException
357+
*/
358+
public static Picture8Bit getNativeFrameSloppy(SeekableDemuxerTrack vt, ContainerAdaptor decoder, int frameNumber)
359+
throws IOException, JCodecException {
360+
return new FrameGrab8Bit(vt, decoder).seekToFrameSloppy(frameNumber).getNativeFrame();
361+
}
362+
363+
/**
364+
* Get a specified frame by second from an already open demuxer track (
365+
* sloppy mode, i.e. nearest keyframe )
366+
*
367+
* @param vt
368+
* @param decoder
369+
* @param frameNumber
370+
* @return
371+
* @throws IOException
372+
* @throws JCodecException
373+
*/
374+
public static Picture8Bit getNativeFrameAtSecSloppy(SeekableDemuxerTrack vt, ContainerAdaptor decoder, double second)
375+
throws IOException, JCodecException {
376+
return new FrameGrab8Bit(vt, decoder).seekToSecondSloppy(second).getNativeFrame();
377+
}
378+
379+
/**
380+
* Gets info about the media
381+
*
382+
* @return
383+
*/
384+
public MediaInfo getMediaInfo() {
385+
return decoder.getMediaInfo();
341386
}
342387
}
Collapse file

‎src/main/java/org/jcodec/common/io/BitReader.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/common/io/BitReader.java
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ private BitReader(ByteBuffer bb) {
2929

3030
public BitReader fork() {
3131
BitReader fork = new BitReader(this.bb.duplicate());
32+
fork.initPos = 0;
3233
fork.curInt = this.curInt;
3334
fork.deficit = this.deficit;
3435
return fork;
@@ -149,15 +150,15 @@ public int check24Bits() {
149150
deficit -= 16;
150151
curInt |= nextIgnore16() << deficit;
151152
}
152-
153+
153154
if (deficit > 8) {
154155
deficit -= 8;
155156
curInt |= nextIgnore() << deficit;
156157
}
157-
158+
158159
return curInt >>> 8;
159160
}
160-
161+
161162
public int check16Bits() {
162163
if (deficit > 16) {
163164
deficit -= 16;
Collapse file

‎src/test/java/org/jcodec/Utils.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/jcodec/Utils.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public static void compareMP4H264Files(File file, File refFile) throws IOExcepti
9191
try {
9292
ch1 = NIOUtils.readableChannel(file);
9393
ch2 = NIOUtils.readableChannel(refFile);
94-
FrameGrab8Bit frameGrab1 = FrameGrab8Bit.createFrameGrab(ch1);
95-
FrameGrab8Bit frameGrab2 = FrameGrab8Bit.createFrameGrab(ch2);
94+
FrameGrab8Bit frameGrab1 = FrameGrab8Bit.createFrameGrab8Bit(ch1);
95+
FrameGrab8Bit frameGrab2 = FrameGrab8Bit.createFrameGrab8Bit(ch2);
9696

9797
Picture8Bit fr1, fr2;
9898
do {
Collapse file

‎src/test/java/org/jcodec/api/FrameGrabTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/jcodec/api/FrameGrabTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void compareOneSequence(String compressed, String uncompressed) throws F
4343
try {
4444
ch1 = NIOUtils.readableChannel(new File(compressed));
4545
ch2 = NIOUtils.readableChannel(new File(uncompressed));
46-
FrameGrab8Bit frameGrab1 = FrameGrab8Bit.createFrameGrab(ch1);
46+
FrameGrab8Bit frameGrab1 = FrameGrab8Bit.createFrameGrab8Bit(ch1);
4747

4848
PictureWithMetadata8Bit fr1;
4949
List<PictureWithMetadata8Bit> decoded = new ArrayList<PictureWithMetadata8Bit>();
Collapse file

‎src/test/java/org/jcodec/codecs/vp8/VP8DecoderTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/jcodec/codecs/vp8/VP8DecoderTest.java
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import javax.imageio.ImageIO;
88

9+
import org.jcodec.Utils;
910
import org.jcodec.common.io.NIOUtils;
1011
import org.jcodec.containers.mkv.MKVMuxerTest;
1112
import org.jcodec.scale.AWTUtil;
@@ -23,13 +24,13 @@ public class VP8DecoderTest {
2324
public void testKF() throws Exception {
2425
dec.decode(bb.duplicate());
2526

26-
ImageIO.write(AWTUtil.toBufferedImage(dec.getPicture()), "png", MKVMuxerTest.tildeExpand("~/decoded.png"));
27+
ImageIO.write(AWTUtil.toBufferedImage(dec.getPicture()), "png", Utils.tildeExpand("~/decoded.png"));
2728
}
2829

2930
@Ignore @Test
3031
public void testKFToPicture() throws Exception {
3132
dec.decode(bb.duplicate());
32-
ImageIO.write(AWTUtil.toBufferedImage(dec.getPicture()), "png", MKVMuxerTest.tildeExpand("~/decoded.pic.png"));
33+
ImageIO.write(AWTUtil.toBufferedImage(dec.getPicture()), "png", Utils.tildeExpand("~/decoded.pic.png"));
3334
}
3435

3536
public void pysch() throws Exception {
Collapse file

‎src/test/java/org/jcodec/containers/mkv/AudioTrackTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/jcodec/containers/mkv/AudioTrackTest.java
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.jcodec.common.io.IOUtils.closeQuietly;
55
import static org.jcodec.common.io.IOUtils.readFileToByteArray;
66
import static org.jcodec.containers.mkv.MKVMuxerTest.bufferToArray;
7-
import static org.jcodec.containers.mkv.MKVMuxerTest.tildeExpand;
87
import static org.jcodec.containers.mkv.MKVType.Cluster;
98
import static org.jcodec.containers.mkv.MKVType.Segment;
109
import static org.jcodec.containers.mkv.MKVType.SimpleBlock;
@@ -16,6 +15,7 @@
1615
import java.nio.ByteBuffer;
1716
import java.util.List;
1817

18+
import org.jcodec.Utils;
1919
import org.jcodec.common.io.FileChannelWrapper;
2020
import org.jcodec.common.io.NIOUtils;
2121
import org.jcodec.common.model.Packet;
@@ -43,7 +43,7 @@ public void testSoundSamples() throws Exception {
4343
audio.gotoFrame(9);
4444

4545
Packet p = audio.nextFrame();
46-
ByteBuffer audioSample = NIOUtils.fetchFromFile(tildeExpand("./src/test/resources/mkv/test1.audiosample09.mp3"));
46+
ByteBuffer audioSample = NIOUtils.fetchFromFile(Utils.tildeExpand("./src/test/resources/mkv/test1.audiosample09.mp3"));
4747

4848
Assert.assertArrayEquals(audioSample.array(), bufferToArray(p.getData()));
4949
}
@@ -55,8 +55,8 @@ public void testTwoSoundSamples() throws Exception {
5555
audio.gotoFrame(8);
5656

5757
Packet p = audio.getFrames(2);
58-
byte[] sample08 = readFileToByteArray(tildeExpand("./src/test/resources/mkv/test1.audiosample08.mp3"));
59-
byte[] sample09 = readFileToByteArray(tildeExpand("./src/test/resources/mkv/test1.audiosample09.mp3"));
58+
byte[] sample08 = readFileToByteArray(Utils.tildeExpand("./src/test/resources/mkv/test1.audiosample08.mp3"));
59+
byte[] sample09 = readFileToByteArray(Utils.tildeExpand("./src/test/resources/mkv/test1.audiosample09.mp3"));
6060
byte[] twoSamples = new byte[sample08.length+sample09.length];
6161
arraycopy(sample08, 0, twoSamples, 0, sample08.length);
6262
arraycopy(sample09, 0, twoSamples, sample08.length, sample09.length);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.