GeezerVison is showing github.com — Adding AAC/MP4 decodeing sample · codice/jcodec@a3b8745
File treeExpand file treeCollapse file tree Open diff view settings samples/main/java/org/jcodec/samples/transcode net/sourceforge/jaad/aac/syntax
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| 12 | 12 | import static org.jcodec.common.tools.MainUtils.tildeExpand; | 13 | 13 | import static org.jcodec.containers.mp4.TrackType.SOUND; | 14 | 14 | import static org.jcodec.containers.mp4.TrackType.VIDEO; | | 15 | +import net.sourceforge.jaad.aac.Decoder; | | 16 | +import net.sourceforge.jaad.aac.SampleBuffer; | 15 | 17 |
| 16 | 18 | import org.jcodec.codecs.aac.AACConts; | | 19 | +import org.jcodec.codecs.aac.AACUtils; | | 20 | +import org.jcodec.codecs.aac.AACUtils.AACMetadata; | 17 | 21 | import org.jcodec.codecs.aac.ADTSParser; | 18 | 22 | import org.jcodec.codecs.h264.H264Decoder; | 19 | 23 | import org.jcodec.codecs.h264.H264Encoder; | | 40 | 44 | import org.jcodec.codecs.vp8.VP8Decoder; | 41 | 45 | import org.jcodec.codecs.vpx.IVFMuxer; | 42 | 46 | import org.jcodec.codecs.vpx.VP8Encoder; | | 47 | +import org.jcodec.codecs.wav.WavOutput; | 43 | 48 | import org.jcodec.codecs.y4m.Y4MDecoder; | | 49 | +import org.jcodec.common.Codec; | 44 | 50 | import org.jcodec.common.DemuxerTrack; | 45 | 51 | import org.jcodec.common.DemuxerTrackMeta; | 46 | 52 | import org.jcodec.common.DemuxerTrackMeta.Type; | | 51 | 57 | import org.jcodec.common.io.IOUtils; | 52 | 58 | import org.jcodec.common.io.NIOUtils; | 53 | 59 | import org.jcodec.common.io.SeekableByteChannel; | | 60 | +import org.jcodec.common.logging.Logger; | 54 | 61 | import org.jcodec.common.model.ColorSpace; | 55 | 62 | import org.jcodec.common.model.Packet; | 56 | 63 | import org.jcodec.common.model.Picture8Bit; | | 69 | 76 | import org.jcodec.containers.mp4.boxes.Box; | 70 | 77 | import org.jcodec.containers.mp4.boxes.Header; | 71 | 78 | import org.jcodec.containers.mp4.boxes.PixelAspectExt; | | 79 | +import org.jcodec.containers.mp4.boxes.SampleEntry; | 72 | 80 | import org.jcodec.containers.mp4.boxes.VideoSampleEntry; | 73 | 81 | import org.jcodec.containers.mp4.demuxer.AbstractMP4DemuxerTrack; | 74 | 82 | import org.jcodec.containers.mp4.demuxer.FramesMP4DemuxerTrack; | @@ -144,6 +152,7 @@ public class TranscodeMain { | 144 | 152 | profiles.put("tsavc2png", new TsAvc2Png()); | 145 | 153 | profiles.put("y4m2prores", new Y4m2prores()); | 146 | 154 | profiles.put("webm2png", new Webm2png()); | | 155 | + profiles.put("mp42wav", new MP42Wav()); | 147 | 156 | } | 148 | 157 |
| 149 | 158 | public static void main(String[] args) throws Exception { | @@ -1559,4 +1568,67 @@ private static ProresEncoder.Profile getProfile(String fourcc) { | 1559 | 1568 | return null; | 1560 | 1569 | } | 1561 | 1570 | } | | 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 | + } | 1562 | 1634 | } |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -93,7 +93,7 @@ public boolean isCommonWindow() { | 93 | 93 |
| 94 | 94 | public static MSMask msMaskFromInt(int i) throws AACException { | 95 | 95 | MSMask[] values = MSMask.values(); | 96 | | - if (values.length >= i) { | | 96 | + if (i >= values.length) { | 97 | 97 | throw new AACException("unknown MS mask type"); | 98 | 98 | } | 99 | 99 | return values[i]; | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,7 @@ public static enum WindowSequence { | 34 | 34 |
| 35 | 35 | public static WindowSequence windowSequenceFromInt(int i) throws AACException { | 36 | 36 | WindowSequence[] values = WindowSequence.values(); | 37 | | - if (values.length >= i) { | | 37 | + if (i >= values.length) { | 38 | 38 | throw new AACException("unknown window sequence type"); | 39 | 39 | } | 40 | 40 | return values[i]; | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 21 | 21 | */ | 22 | 22 | public class AACUtils { | 23 | 23 |
| 24 | | - public static class AudioInfo { | | 24 | + public static class AACMetadata { | 25 | 25 | private AudioFormat format; | 26 | 26 | private ChannelLabel[] labels; | 27 | 27 |
| 28 | | - public AudioInfo(AudioFormat format, ChannelLabel[] labels) { | | 28 | + public AACMetadata(AudioFormat format, ChannelLabel[] labels) { | 29 | 29 | this.format = format; | 30 | 30 | this.labels = labels; | 31 | 31 | } | @@ -60,7 +60,7 @@ private static int getObjectType(BitReader reader) { | 60 | 60 | ChannelLabel.SIDE_RIGHT, ChannelLabel.REAR_LEFT, ChannelLabel.REAR_RIGHT, ChannelLabel.LFE } // | 61 | 61 | }; | 62 | 62 |
| 63 | | - public static AudioInfo parseAudioInfo(ByteBuffer privData) { | | 63 | + public static AACMetadata parseAudioInfo(ByteBuffer privData) { | 64 | 64 | BitReader reader = BitReader.createBitReader(privData); | 65 | 65 |
| 66 | 66 | int objectType = getObjectType(reader); | @@ -72,12 +72,20 @@ public static AudioInfo parseAudioInfo(ByteBuffer privData) { | 72 | 72 | return null; | 73 | 73 |
| 74 | 74 | 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); | 76 | 76 | } | 77 | 77 |
| 78 | | - public static AudioInfo getChannels(SampleEntry mp4a) { | | 78 | + public static AACMetadata getMetadata(SampleEntry mp4a) { | 79 | 79 | if (!"mp4a".equals(mp4a.getFourcc())) | 80 | 80 | 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) { | 81 | 89 | LeafBox b = Box.findFirst(mp4a, LeafBox.class, "esds"); | 82 | 90 | if (b == null) { | 83 | 91 | b = Box.findFirstPath(mp4a, LeafBox.class, new String[] { null, "esds" }); | @@ -86,6 +94,6 @@ public static AudioInfo getChannels(SampleEntry mp4a) { | 86 | 94 | return null; | 87 | 95 | EsdsBox esds = EsdsBox.newEsdsBox(); | 88 | 96 | esds.parse(b.getData()); | 89 | | - return parseAudioInfo(esds.getStreamInfo()); | | 97 | + return esds.getStreamInfo(); | 90 | 98 | } | 91 | 99 | } |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | 1 | package org.jcodec.codecs.mpeg4.es; | 2 | 2 |
| 3 | 3 | import java.nio.ByteBuffer; | | 4 | +import java.util.Collection; | 4 | 5 |
| 5 | 6 | /** | 6 | 7 | * This class is part of JCodec ( www.jcodec.org ) This software is distributed | @@ -15,23 +16,24 @@ public class DecoderConfig extends NodeDescriptor { | 15 | 16 | private int maxBitrate; | 16 | 17 | private int avgBitrate; | 17 | 18 |
| 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) { | 19 | 20 | super(tag(), children); | 20 | 21 | this.objectType = objectType; | 21 | 22 | this.bufSize = bufSize; | 22 | 23 | this.maxBitrate = maxBitrate; | 23 | 24 | this.avgBitrate = avgBitrate; | 24 | 25 | } | 25 | 26 |
| 26 | | - protected void parse(ByteBuffer input) { | | 27 | + protected static DecoderConfig parse(ByteBuffer input) { | 27 | 28 |
| 28 | | - objectType = input.get() & 0xff; | | 29 | + int objectType = input.get() & 0xff; | 29 | 30 | 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(); | 33 | 34 |
| 34 | | - super.parse(input); | | 35 | + NodeDescriptor node = NodeDescriptor.parse(input); | | 36 | + return new DecoderConfig(objectType, bufSize, maxBitrate, avgBitrate, node.getChildren()); | 35 | 37 | } | 36 | 38 |
| 37 | 39 | protected void doWrite(ByteBuffer out) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -32,8 +32,8 @@ public ByteBuffer getData() { | 32 | 32 | return data; | 33 | 33 | } | 34 | 34 |
| 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); | 38 | 38 | } | 39 | 39 | } |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | 1 | package org.jcodec.codecs.mpeg4.es; | 2 | 2 |
| | 3 | +import java.lang.reflect.Method; | 3 | 4 | import java.nio.ByteBuffer; | 4 | 5 |
| 5 | 6 | import org.jcodec.common.JCodecUtil; | @@ -43,16 +44,14 @@ public static Descriptor read(ByteBuffer input) { | 43 | 44 | Class<? extends Descriptor> cls = factory.byTag(tag); | 44 | 45 | Descriptor descriptor; | 45 | 46 | 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)); | 47 | 49 | } catch (Exception e) { | 48 | 50 | throw new RuntimeException(e); | 49 | 51 | } | 50 | | - descriptor.parse(NIOUtils.read(input, size)); | 51 | 52 | return descriptor; | 52 | 53 | } | 53 | 54 |
| 54 | | - protected abstract void parse(ByteBuffer input); | 55 | | - | 56 | 55 | public static <T> T find(Descriptor es, Class<T> class1, int tag) { | 57 | 56 | if (es.getTag() == tag) | 58 | 57 | return (T) es; | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | 1 | package org.jcodec.codecs.mpeg4.es; | 2 | 2 |
| 3 | 3 | import java.nio.ByteBuffer; | | 4 | +import java.util.Collection; | 4 | 5 |
| 5 | 6 | /** | 6 | 7 | * This class is part of JCodec ( www.jcodec.org ) This software is distributed | | 12 | 13 | public class ES extends NodeDescriptor { | 13 | 14 | private int trackId; | 14 | 15 |
| 15 | | - public ES(int trackId, Descriptor[] children) { | | 16 | + public ES(int trackId, Collection<Descriptor> children) { | 16 | 17 | super(tag(), children); | 17 | 18 | this.trackId = trackId; | 18 | 19 | } | @@ -27,10 +28,11 @@ protected void doWrite(ByteBuffer out) { | 27 | 28 | super.doWrite(out); | 28 | 29 | } | 29 | 30 |
| 30 | | - protected void parse(ByteBuffer input) { | 31 | | - trackId = input.getShort(); | | 31 | + protected static ES parse(ByteBuffer input) { | | 32 | + int trackId = input.getShort(); | 32 | 33 | input.get(); | 33 | | - super.parse(input); | | 34 | + NodeDescriptor node = NodeDescriptor.parse(input); | | 35 | + return new ES(trackId, node.getChildren()); | 34 | 36 | } | 35 | 37 |
| 36 | 38 | public int getTrackId() { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | 1 | package org.jcodec.codecs.mpeg4.es; | 2 | 2 |
| 3 | | -import static java.util.Arrays.asList; | 4 | | - | 5 | 3 | import java.nio.ByteBuffer; | 6 | 4 | import java.util.ArrayList; | 7 | 5 | import java.util.Collection; | | 15 | 13 | */ | 16 | 14 | public class NodeDescriptor extends Descriptor { | 17 | 15 | private Collection<Descriptor> children; | 18 | | - | 19 | | - public NodeDescriptor(int tag, Descriptor[] children) { | | 16 | + | | 17 | + public NodeDescriptor(int tag, Collection<Descriptor> children) { | 20 | 18 | super(tag, 0); | 21 | | - this.children = new ArrayList<Descriptor>(); | 22 | | - this.children.addAll(asList(children)); | | 19 | + this.children = children; | 23 | 20 | } | 24 | 21 |
| 25 | 22 | protected void doWrite(ByteBuffer out) { | @@ -32,12 +29,14 @@ public Collection<Descriptor> getChildren() { | 32 | 29 | return children; | 33 | 30 | } | 34 | 31 |
| 35 | | - protected void parse(ByteBuffer input) { | | 32 | + protected static NodeDescriptor parse(ByteBuffer input) { | | 33 | + Collection<Descriptor> children = new ArrayList<Descriptor>(); | 36 | 34 | Descriptor d; | 37 | 35 | do { | 38 | 36 | d = Descriptor.read(input); | 39 | 37 | if (d != null) | 40 | 38 | children.add(d); | 41 | 39 | } while (d != null); | | 40 | + return new NodeDescriptor(0, children); | 42 | 41 | } | 43 | 42 | } |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -21,8 +21,9 @@ protected void doWrite(ByteBuffer out) { | 21 | 21 | out.put((byte)0x2); | 22 | 22 | } | 23 | 23 |
| 24 | | - protected void parse(ByteBuffer input) { | | 24 | + protected static SL parse(ByteBuffer input) { | 25 | 25 | Assert.assertEquals(0x2, input.get() & 0xff); | | 26 | + return new SL(); | 26 | 27 | } | 27 | 28 |
| 28 | 29 | public static int tag() { | |
|