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 — Adding AAC/MP4 decodeing sample · codice/jcodec@a3b8745

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • samples/main/java/org/jcodec/samples/transcode
    • TranscodeMain.java
  • src/main/java
    • net/sourceforge/jaad/aac/syntax
      • CPE.java
      • ICSInfo.java
    • org/jcodec
      • codecs
        • aac
          • AACUtils.java
        • mpeg4
          • es
            • DecoderConfig.java
            • DecoderSpecific.java
            • Descriptor.java
            • ES.java
            • NodeDescriptor.java
            • SL.java
          • mp4
            • EsdsBox.java
      • containers/mp4/demuxer
        • AbstractMP4DemuxerTrack.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎samples/main/java/org/jcodec/samples/transcode/TranscodeMain.java‎

Copy file name to clipboardExpand all lines: samples/main/java/org/jcodec/samples/transcode/TranscodeMain.java
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
import static org.jcodec.common.tools.MainUtils.tildeExpand;
1313
import static org.jcodec.containers.mp4.TrackType.SOUND;
1414
import static org.jcodec.containers.mp4.TrackType.VIDEO;
15+
import net.sourceforge.jaad.aac.Decoder;
16+
import net.sourceforge.jaad.aac.SampleBuffer;
1517

1618
import org.jcodec.codecs.aac.AACConts;
19+
import org.jcodec.codecs.aac.AACUtils;
20+
import org.jcodec.codecs.aac.AACUtils.AACMetadata;
1721
import org.jcodec.codecs.aac.ADTSParser;
1822
import org.jcodec.codecs.h264.H264Decoder;
1923
import org.jcodec.codecs.h264.H264Encoder;
@@ -40,7 +44,9 @@
4044
import org.jcodec.codecs.vp8.VP8Decoder;
4145
import org.jcodec.codecs.vpx.IVFMuxer;
4246
import org.jcodec.codecs.vpx.VP8Encoder;
47+
import org.jcodec.codecs.wav.WavOutput;
4348
import org.jcodec.codecs.y4m.Y4MDecoder;
49+
import org.jcodec.common.Codec;
4450
import org.jcodec.common.DemuxerTrack;
4551
import org.jcodec.common.DemuxerTrackMeta;
4652
import org.jcodec.common.DemuxerTrackMeta.Type;
@@ -51,6 +57,7 @@
5157
import org.jcodec.common.io.IOUtils;
5258
import org.jcodec.common.io.NIOUtils;
5359
import org.jcodec.common.io.SeekableByteChannel;
60+
import org.jcodec.common.logging.Logger;
5461
import org.jcodec.common.model.ColorSpace;
5562
import org.jcodec.common.model.Packet;
5663
import org.jcodec.common.model.Picture8Bit;
@@ -69,6 +76,7 @@
6976
import org.jcodec.containers.mp4.boxes.Box;
7077
import org.jcodec.containers.mp4.boxes.Header;
7178
import org.jcodec.containers.mp4.boxes.PixelAspectExt;
79+
import org.jcodec.containers.mp4.boxes.SampleEntry;
7280
import org.jcodec.containers.mp4.boxes.VideoSampleEntry;
7381
import org.jcodec.containers.mp4.demuxer.AbstractMP4DemuxerTrack;
7482
import org.jcodec.containers.mp4.demuxer.FramesMP4DemuxerTrack;
@@ -144,6 +152,7 @@ public class TranscodeMain {
144152
profiles.put("tsavc2png", new TsAvc2Png());
145153
profiles.put("y4m2prores", new Y4m2prores());
146154
profiles.put("webm2png", new Webm2png());
155+
profiles.put("mp42wav", new MP42Wav());
147156
}
148157

149158
public static void main(String[] args) throws Exception {
@@ -1559,4 +1568,67 @@ private static ProresEncoder.Profile getProfile(String fourcc) {
15591568
return null;
15601569
}
15611570
}
1571+
1572+
protected static class MP42Wav implements Profile {
1573+
@Override
1574+
public void transcode(Cmd cmd) throws IOException {
1575+
SeekableByteChannel source = null;
1576+
SeekableByteChannel sink = null;
1577+
WavOutput wavOutput = null;
1578+
try {
1579+
source = readableChannel(new File(cmd.getArg(0)));
1580+
sink = writableChannel(new File(cmd.getArg(1)));
1581+
1582+
MP4Demuxer demuxer = new MP4Demuxer(source);
1583+
1584+
List<AbstractMP4DemuxerTrack> tracks = demuxer.getAudioTracks();
1585+
AbstractMP4DemuxerTrack selectedTrack = null;
1586+
for (AbstractMP4DemuxerTrack track : tracks) {
1587+
if (track.getCodec() == Codec.AAC) {
1588+
selectedTrack = track;
1589+
break;
1590+
}
1591+
}
1592+
if (selectedTrack == null) {
1593+
Logger.error("Could not find an AAC track");
1594+
return;
1595+
} else {
1596+
Logger.info("Using the AAC track: " + selectedTrack.getNo());
1597+
}
1598+
SampleEntry sampleEntry = selectedTrack.getSampleEntries()[0];
1599+
AACMetadata meta = AACUtils.getMetadata(sampleEntry);
1600+
wavOutput = new WavOutput(sink, meta.getFormat());
1601+
Decoder aacDecoder = new Decoder(NIOUtils.toArray(AACUtils.getCodecPrivate(sampleEntry)));
1602+
SampleBuffer sampleBuffer = new SampleBuffer();
1603+
1604+
Packet packet;
1605+
while ((packet = selectedTrack.nextFrame()) != null) {
1606+
aacDecoder.decodeFrame(NIOUtils.toArray(packet.getData()), sampleBuffer);
1607+
if (sampleBuffer.isBigEndian())
1608+
toLittleEndian(sampleBuffer);
1609+
wavOutput.write(ByteBuffer.wrap(sampleBuffer.getData()));
1610+
}
1611+
} finally {
1612+
NIOUtils.closeQuietly(source);
1613+
NIOUtils.closeQuietly(wavOutput);
1614+
}
1615+
}
1616+
1617+
private void toLittleEndian(SampleBuffer sampleBuffer) {
1618+
byte[] data = sampleBuffer.getData();
1619+
for (int i = 0; i < data.length; i += 2) {
1620+
byte tmp = data[i];
1621+
data[i] = data[i + 1];
1622+
data[i + 1] = tmp;
1623+
}
1624+
}
1625+
1626+
@Override
1627+
public void printHelp(PrintStream err) {
1628+
MainUtils.printHelpVarArgs(new HashMap<String, String>() {
1629+
{
1630+
}
1631+
}, "in file", "out file");
1632+
}
1633+
}
15621634
}
Collapse file

‎src/main/java/net/sourceforge/jaad/aac/syntax/CPE.java‎

Copy file name to clipboardExpand all lines: src/main/java/net/sourceforge/jaad/aac/syntax/CPE.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean isCommonWindow() {
9393

9494
public static MSMask msMaskFromInt(int i) throws AACException {
9595
MSMask[] values = MSMask.values();
96-
if (values.length >= i) {
96+
if (i >= values.length) {
9797
throw new AACException("unknown MS mask type");
9898
}
9999
return values[i];
Collapse file

‎src/main/java/net/sourceforge/jaad/aac/syntax/ICSInfo.java‎

Copy file name to clipboardExpand all lines: src/main/java/net/sourceforge/jaad/aac/syntax/ICSInfo.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static enum WindowSequence {
3434

3535
public static WindowSequence windowSequenceFromInt(int i) throws AACException {
3636
WindowSequence[] values = WindowSequence.values();
37-
if (values.length >= i) {
37+
if (i >= values.length) {
3838
throw new AACException("unknown window sequence type");
3939
}
4040
return values[i];
Collapse file

‎src/main/java/org/jcodec/codecs/aac/AACUtils.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/aac/AACUtils.java
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
*/
2222
public class AACUtils {
2323

24-
public static class AudioInfo {
24+
public static class AACMetadata {
2525
private AudioFormat format;
2626
private ChannelLabel[] labels;
2727

28-
public AudioInfo(AudioFormat format, ChannelLabel[] labels) {
28+
public AACMetadata(AudioFormat format, ChannelLabel[] labels) {
2929
this.format = format;
3030
this.labels = labels;
3131
}
@@ -60,7 +60,7 @@ private static int getObjectType(BitReader reader) {
6060
ChannelLabel.SIDE_RIGHT, ChannelLabel.REAR_LEFT, ChannelLabel.REAR_RIGHT, ChannelLabel.LFE } //
6161
};
6262

63-
public static AudioInfo parseAudioInfo(ByteBuffer privData) {
63+
public static AACMetadata parseAudioInfo(ByteBuffer privData) {
6464
BitReader reader = BitReader.createBitReader(privData);
6565

6666
int objectType = getObjectType(reader);
@@ -72,12 +72,20 @@ public static AudioInfo parseAudioInfo(ByteBuffer privData) {
7272
return null;
7373

7474
ChannelLabel[] channels = AAC_DEFAULT_CONFIGS[channelConfig];
75-
return new AudioInfo(new AudioFormat(sampleRate, 16, channels.length, true, false), channels);
75+
return new AACMetadata(new AudioFormat(sampleRate, 16, channels.length, true, false), channels);
7676
}
7777

78-
public static AudioInfo getChannels(SampleEntry mp4a) {
78+
public static AACMetadata getMetadata(SampleEntry mp4a) {
7979
if (!"mp4a".equals(mp4a.getFourcc()))
8080
throw new IllegalArgumentException("Not mp4a sample entry");
81+
ByteBuffer b = getCodecPrivate(mp4a);
82+
if (b == null)
83+
return null;
84+
85+
return parseAudioInfo(b);
86+
}
87+
88+
public static ByteBuffer getCodecPrivate(SampleEntry mp4a) {
8189
LeafBox b = Box.findFirst(mp4a, LeafBox.class, "esds");
8290
if (b == null) {
8391
b = Box.findFirstPath(mp4a, LeafBox.class, new String[] { null, "esds" });
@@ -86,6 +94,6 @@ public static AudioInfo getChannels(SampleEntry mp4a) {
8694
return null;
8795
EsdsBox esds = EsdsBox.newEsdsBox();
8896
esds.parse(b.getData());
89-
return parseAudioInfo(esds.getStreamInfo());
97+
return esds.getStreamInfo();
9098
}
9199
}
Collapse file

‎src/main/java/org/jcodec/codecs/mpeg4/es/DecoderConfig.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/mpeg4/es/DecoderConfig.java
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jcodec.codecs.mpeg4.es;
22

33
import java.nio.ByteBuffer;
4+
import java.util.Collection;
45

56
/**
67
* This class is part of JCodec ( www.jcodec.org ) This software is distributed
@@ -15,23 +16,24 @@ public class DecoderConfig extends NodeDescriptor {
1516
private int maxBitrate;
1617
private int avgBitrate;
1718

18-
public DecoderConfig(int objectType, int bufSize, int maxBitrate, int avgBitrate, Descriptor[] children) {
19+
public DecoderConfig(int objectType, int bufSize, int maxBitrate, int avgBitrate, Collection<Descriptor> children) {
1920
super(tag(), children);
2021
this.objectType = objectType;
2122
this.bufSize = bufSize;
2223
this.maxBitrate = maxBitrate;
2324
this.avgBitrate = avgBitrate;
2425
}
2526

26-
protected void parse(ByteBuffer input) {
27+
protected static DecoderConfig parse(ByteBuffer input) {
2728

28-
objectType = input.get() & 0xff;
29+
int objectType = input.get() & 0xff;
2930
input.get();
30-
bufSize = ((input.get() & 0xff) << 16) | (input.getShort() & 0xffff);
31-
maxBitrate = input.getInt();
32-
avgBitrate = input.getInt();
31+
int bufSize = ((input.get() & 0xff) << 16) | (input.getShort() & 0xffff);
32+
int maxBitrate = input.getInt();
33+
int avgBitrate = input.getInt();
3334

34-
super.parse(input);
35+
NodeDescriptor node = NodeDescriptor.parse(input);
36+
return new DecoderConfig(objectType, bufSize, maxBitrate, avgBitrate, node.getChildren());
3537
}
3638

3739
protected void doWrite(ByteBuffer out) {
Collapse file

‎src/main/java/org/jcodec/codecs/mpeg4/es/DecoderSpecific.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/mpeg4/es/DecoderSpecific.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public ByteBuffer getData() {
3232
return data;
3333
}
3434

35-
@Override
36-
protected void parse(ByteBuffer input) {
37-
data = NIOUtils.readBuf(input);
35+
protected static DecoderSpecific parse(ByteBuffer input) {
36+
ByteBuffer data = NIOUtils.readBuf(input);
37+
return new DecoderSpecific(data);
3838
}
3939
}
Collapse file

‎src/main/java/org/jcodec/codecs/mpeg4/es/Descriptor.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/mpeg4/es/Descriptor.java
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jcodec.codecs.mpeg4.es;
22

3+
import java.lang.reflect.Method;
34
import java.nio.ByteBuffer;
45

56
import org.jcodec.common.JCodecUtil;
@@ -43,16 +44,14 @@ public static Descriptor read(ByteBuffer input) {
4344
Class<? extends Descriptor> cls = factory.byTag(tag);
4445
Descriptor descriptor;
4546
try {
46-
descriptor = cls.getConstructor(Integer.TYPE, Integer.TYPE).newInstance(tag, (int) size);
47+
Method method = cls.getDeclaredMethod("parse", ByteBuffer.class);
48+
descriptor = (Descriptor)method.invoke(null, NIOUtils.read(input, size));
4749
} catch (Exception e) {
4850
throw new RuntimeException(e);
4951
}
50-
descriptor.parse(NIOUtils.read(input, size));
5152
return descriptor;
5253
}
5354

54-
protected abstract void parse(ByteBuffer input);
55-
5655
public static <T> T find(Descriptor es, Class<T> class1, int tag) {
5756
if (es.getTag() == tag)
5857
return (T) es;
Collapse file

‎src/main/java/org/jcodec/codecs/mpeg4/es/ES.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/mpeg4/es/ES.java
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jcodec.codecs.mpeg4.es;
22

33
import java.nio.ByteBuffer;
4+
import java.util.Collection;
45

56
/**
67
* This class is part of JCodec ( www.jcodec.org ) This software is distributed
@@ -12,7 +13,7 @@
1213
public class ES extends NodeDescriptor {
1314
private int trackId;
1415

15-
public ES(int trackId, Descriptor[] children) {
16+
public ES(int trackId, Collection<Descriptor> children) {
1617
super(tag(), children);
1718
this.trackId = trackId;
1819
}
@@ -27,10 +28,11 @@ protected void doWrite(ByteBuffer out) {
2728
super.doWrite(out);
2829
}
2930

30-
protected void parse(ByteBuffer input) {
31-
trackId = input.getShort();
31+
protected static ES parse(ByteBuffer input) {
32+
int trackId = input.getShort();
3233
input.get();
33-
super.parse(input);
34+
NodeDescriptor node = NodeDescriptor.parse(input);
35+
return new ES(trackId, node.getChildren());
3436
}
3537

3638
public int getTrackId() {
Collapse file

‎src/main/java/org/jcodec/codecs/mpeg4/es/NodeDescriptor.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/mpeg4/es/NodeDescriptor.java
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.jcodec.codecs.mpeg4.es;
22

3-
import static java.util.Arrays.asList;
4-
53
import java.nio.ByteBuffer;
64
import java.util.ArrayList;
75
import java.util.Collection;
@@ -15,11 +13,10 @@
1513
*/
1614
public class NodeDescriptor extends Descriptor {
1715
private Collection<Descriptor> children;
18-
19-
public NodeDescriptor(int tag, Descriptor[] children) {
16+
17+
public NodeDescriptor(int tag, Collection<Descriptor> children) {
2018
super(tag, 0);
21-
this.children = new ArrayList<Descriptor>();
22-
this.children.addAll(asList(children));
19+
this.children = children;
2320
}
2421

2522
protected void doWrite(ByteBuffer out) {
@@ -32,12 +29,14 @@ public Collection<Descriptor> getChildren() {
3229
return children;
3330
}
3431

35-
protected void parse(ByteBuffer input) {
32+
protected static NodeDescriptor parse(ByteBuffer input) {
33+
Collection<Descriptor> children = new ArrayList<Descriptor>();
3634
Descriptor d;
3735
do {
3836
d = Descriptor.read(input);
3937
if (d != null)
4038
children.add(d);
4139
} while (d != null);
40+
return new NodeDescriptor(0, children);
4241
}
4342
}
Collapse file

‎src/main/java/org/jcodec/codecs/mpeg4/es/SL.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/codecs/mpeg4/es/SL.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ protected void doWrite(ByteBuffer out) {
2121
out.put((byte)0x2);
2222
}
2323

24-
protected void parse(ByteBuffer input) {
24+
protected static SL parse(ByteBuffer input) {
2525
Assert.assertEquals(0x2, input.get() & 0xff);
26+
return new SL();
2627
}
2728

2829
public static int tag() {

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.