Skip to content

Commit

Permalink
Work on decoding stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sheinbergon committed Jul 9, 2024
1 parent ea4819c commit f55eefa
Show file tree
Hide file tree
Showing 11 changed files with 498 additions and 39 deletions.
79 changes: 47 additions & 32 deletions src/main/java/org/sheinbergon/aac/jna/FdkAACLib.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
package org.sheinbergon.aac.jna;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.ptr.ShortByReference;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import org.sheinbergon.aac.jna.structure.AACEncBufDesc;
import org.sheinbergon.aac.jna.structure.AACEncInArgs;
import org.sheinbergon.aac.jna.structure.AACEncInfo;
import org.sheinbergon.aac.jna.structure.AACEncOutArgs;
import org.sheinbergon.aac.jna.structure.AACEncoder;
import org.sheinbergon.aac.jna.structure.*;

@SuppressWarnings("MethodName")
public final class FdkAACLib {

@Getter
@Accessors(fluent = true)
@RequiredArgsConstructor
enum Functions {
INFO("aacEncInfo"),
SET_PARAM("aacEncoder_SetParam"),
OPEN("accEncOpen"),
CLOSE("accEncClose"),
ENCODE("aacEncEncode");
@Getter
@Accessors(fluent = true)
@RequiredArgsConstructor
enum Functions {
ENCODER_INFO("aacEncInfo"),
ENCODER_SET_PARAM("aacEncoder_SetParam"),
ENCODER_OPEN("accEncOpen"),
ENCODER_CLOSE("accEncClose"),
ENCODER_ENCODE("aacEncEncode");

private final String libraryFunctionName;
}
private final String libraryFunctionName;
}

private static final String FDK_AAC = "fdk-aac";
private static final String FDK_AAC = "fdk-aac";

static {
Native.register(FDK_AAC);
}
static {
Native.register(FDK_AAC);
}

static native int aacEncOpen(PointerByReference handle, int encModules, int maxChannels);
static native int aacEncOpen(PointerByReference handle, int encModules, int maxChannels);

static native int aacEncClose(PointerByReference handle);
static native int aacEncClose(PointerByReference handle);

static native int aacEncEncode(
AACEncoder hAacEncoder,
AACEncBufDesc inBufDesc,
AACEncBufDesc outBufDesc,
AACEncInArgs inargs,
AACEncOutArgs outargs);
static native int aacEncEncode(
AACEncoder hAacEncoder,
AACEncBufDesc inBufDesc,
AACEncBufDesc outBufDesc,
AACEncInArgs inargs,
AACEncOutArgs outargs);

static native int aacEncInfo(AACEncoder hAacEncoder, AACEncInfo pInfo);
static native int aacEncInfo(AACEncoder hAacEncoder, AACEncInfo pInfo);

static native int aacEncoder_SetParam(AACEncoder encoder, int param, int value);
static native int aacEncoder_SetParam(AACEncoder encoder, int param, int value);

private FdkAACLib() {
}
static native Pointer aacDecoder_Open(int transportFmt, int nrOfLayers);

static native void aacDecoder_Close(AACDecoder self);

static native int aacDecoder_Fill(
AACDecoder self,
Pointer[] Buffer,
int[] bufferSize,
IntByReference bytesValid);

static native int aacDecoder_DecodeFrame(
AACDecoder self,
ShortByReference pTimeData,
int timeDataSize,
int flags);

private FdkAACLib() {
}
}
12 changes: 6 additions & 6 deletions src/main/java/org/sheinbergon/aac/jna/FdkAACLibFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static AACEncoder openEncoder(
final int maxChannels) {
val pointerRef = new PointerByReference();
val result = AACEncError.valueOf(FdkAACLib.aacEncOpen(pointerRef, modules, maxChannels));
verifyResult(result, FdkAACLib.Functions.OPEN);
verifyResult(result, FdkAACLib.Functions.ENCODER_OPEN);
return AACEncoder.of(pointerRef);
}

Expand All @@ -59,7 +59,7 @@ public static AACEncoder openEncoder(
public static void closeEncoder(final @Nonnull AACEncoder encoder) {
val pointerRef = new PointerByReference(encoder.getPointer());
val result = AACEncError.valueOf(FdkAACLib.aacEncClose(pointerRef));
verifyResult(result, FdkAACLib.Functions.CLOSE);
verifyResult(result, FdkAACLib.Functions.ENCODER_CLOSE);
}

/**
Expand All @@ -69,7 +69,7 @@ public static void closeEncoder(final @Nonnull AACEncoder encoder) {
*/
public static void initEncoder(final @Nonnull AACEncoder encoder) {
val result = AACEncError.valueOf(FdkAACLib.aacEncEncode(encoder, null, null, null, null));
verifyResult(result, FdkAACLib.Functions.ENCODE);
verifyResult(result, FdkAACLib.Functions.ENCODER_ENCODE);
}

/**
Expand Down Expand Up @@ -101,7 +101,7 @@ public static Optional<byte[]> encode(
.filter(result -> result != AACEncError.AACENC_ENCODE_EOF)
.map(result -> {
outArgs.readField("numOutBytes");
verifyResult(result, FdkAACLib.Functions.ENCODE);
verifyResult(result, FdkAACLib.Functions.ENCODER_ENCODE);
return outBufferDescriptor.bufs
.getValue().getByteArray(0, outArgs.numOutBytes);
});
Expand All @@ -116,7 +116,7 @@ public static Optional<byte[]> encode(
public static AACEncInfo getEncoderInfo(final @Nonnull AACEncoder encoder) {
val info = new AACEncInfo();
val result = AACEncError.valueOf(FdkAACLib.aacEncInfo(encoder, info));
verifyResult(result, FdkAACLib.Functions.INFO);
verifyResult(result, FdkAACLib.Functions.ENCODER_INFO);
info.read();
return info;
}
Expand All @@ -134,7 +134,7 @@ public static void setEncoderParam(
final @Nonnull AACEncParam param,
final int value) {
val result = AACEncError.valueOf(FdkAACLib.aacEncoder_SetParam(encoder, param.getValue(), value));
verifyResult(result, FdkAACLib.Functions.SET_PARAM);
verifyResult(result, FdkAACLib.Functions.ENCODER_SET_PARAM);
}

/**
Expand Down
142 changes: 142 additions & 0 deletions src/main/java/org/sheinbergon/aac/jna/structure/AACDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package org.sheinbergon.aac.jna.structure;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.PointerByReference;
import org.sheinbergon.aac.jna.util.JNASupport;

import javax.annotation.Nonnull;
import java.util.List;

/**
* Maps to AAC_DECODER_INSTANCE struct.
*
* @see <a href="https://github.com/mstorsjo/fdk-aac/blob/v2.0.2/libAACdec/src/aacdecoder.h">fdk-aac/libAACdec/src/aacdecoder.h</a>
*/
@SuppressWarnings({
"JavadocVariable",
"VisibilityModifier",
"MissingJavadocMethod",
"MagicNumber",
"MemberName",
"PointlessArithmeticExpression",
"unused"})
public final class AACDecoder extends Structure {

private static final int AACDEC_MAX_NUM_PREROLL_AU = 3;
private static final List<String> FIELD_ORDER = JNASupport.structureFieldOrder(AACDecoder.class);

public static AACDecoder of(final @Nonnull PointerByReference pointerReference) {
return new AACDecoder(pointerReference.getValue());
}

private AACDecoder(final @Nonnull Pointer pointer) {
super(pointer);
setAlignType(Structure.ALIGN_NONE); // Make sure field size alignments are as expected
read(); // Read once after initialize from provided pointer
}

public int aacChannels;
public int[] ascChannels = new int[1];
public int blockNumber;
public int nrOfLayers;
public int outputInterleaved;
public int aacOutDataHeadroom;
public Pointer hInput;
public AACDecoderSamplingRateInfo[] samplingRateInfo = new AACDecoderSamplingRateInfo[1];
public byte frameOK;
public int[] flags = new int[1];
public int[] elFlags = new int[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)];

public int[] elements = new int[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)];

public byte[] elTags = new byte[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)];
public byte[] chMapping = new byte[((8) * 2)];
public int[] channelType = new int[(8)];
public byte[] channelIndices = new byte[(8)];
public FDKChannelMapDescription mapDescr;
public byte chMapIndex;
public int sbrDataLen;
public DecoderCProgramConfig pce;
CStreamInfo
streamInfo; /*!< Pointer to StreamInfo data (read from the bitstream) */
CAacDecoderChannelInfo
*pAacDecoderChannelInfo[(8)]; /*!< Temporal channel memory */
CAacDecoderStaticChannelInfo
*pAacDecoderStaticChannelInfo[(8)]; /*!< Persistent channel memory */

public Pointer workBufferCore1;
public Pointer workBufferCore2;
public Pointer pTimeData2;
public int timeData2Size;

CpePersistentData *cpeStaticData[(
3*((8)*2)+(((8)*2))/2+4*(1)+
1)]; /*!< Pointer to persistent data shared by both channels of a CPE.
This structure is allocated once for each CPE. */

CConcealParams concealCommonData;
CConcealmentMethod concealMethodUser;

CUsacCoreExtensions usacCoreExt; /*!< Data and handles to extend USAC FD/LPD
core decoder (SBR, MPS, ...) */
public int[] numUsacElements = new int[(1 * 1)];
public byte[] usacStereoConfigIndex = new byte[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)];
public Pointer[] pUsacConfig = new Pointer[(1 * 1)];
public int nbDiv;
public byte useLdQmfTimeAlign;
public int aacChannelsPrev;
public int[] channelTypePrev = new int[(8)];
public byte[] channelIndicesPrev = new byte[(8)];
public byte downscaleFactor;
public byte downscaleFactorInBS;
public Pointer hSbrDecoder;
public byte sbrEnabled;
public byte sbrEnabledPrev;
public byte psPossible;
public AACDecoderSBRParams sbrParams;
public ByteByReference pDrmBsBuffer;
public short drmBsBufferSize;
FDK_QMF_DOMAIN
qmfDomain; /*!< Instance of module for QMF domain data handling */
public int qmfModeCurr;
public int qmfModeUser;
public Pointer hDrcInfo;
public int metadataExpiry;
public Pointer pMpegSurroundDecoder;
public byte mpsEnableUser;
public byte mpsEnableCurr;
public byte mpsApplicable;
public byte mpsOutputMode;
public int mpsOutChannelsLast;
public int mpsFrameSizeLast;
public AACDecoderCAncData ancData;
public Pointer hPcmUtils;
public Pointer hLimiter;
public byte limiterEnableUser;
public byte limiterEnableCurr;
// TODO - This might need to use smarter size of types, as signed long is 32BIT in windows platforms
public long[] extGain = new long[1];
public int extGainDelay;
public Pointer hUniDrcDecoder;
public byte multibandDrcPresent;
public byte numTimeSlots;
public int[] loudnessInfoSetPosition = new int[3];
public byte defaultTargetLoudness;
public Pointer[] pTimeDataFlush = new Pointer[((8) * 2)];
public byte flushStatus;
public byte flushCnt;
public byte buildUpStatus;
public byte buildUpCnt;
public byte hasAudioPreRoll;
public int[] prerollAULength = new int[AACDEC_MAX_NUM_PREROLL_AU + 1];
public int accessUnit;
public byte applyCrossfade;
public AACDecoderFDKSignalDelay usacResidualDelay;

@Override
protected List<String> getFieldOrder() {
return FIELD_ORDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sheinbergon.aac.jna.structure;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import org.sheinbergon.aac.jna.util.JNASupport;

import java.util.List;

/**
* Maps to CAncData struct.
*
* @see <a href="https://github.com/mstorsjo/fdk-aac/blob/v2.0.2/libAACdec/src/aacdecoder.h">fdk-aac/libAACdec/src/aacdecoder.h</a>
*/
@SuppressWarnings("MagicNumber")
public class AACDecoderCAncData extends Structure {

private static final List<String> FIELD_ORDER = JNASupport.structureFieldOrder(AACDecoderCAncData.class);

public Pointer buffer;
public int bufferSize;
public int[] offset = new int[8];
public int nrElements;

protected List<String> getFieldOrder() {
return FIELD_ORDER;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sheinbergon.aac.jna.structure;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import org.sheinbergon.aac.jna.util.JNASupport;

import java.util.List;

/**
* Maps to FDK_SignalDelay struct.
*
* @see <a href="https://github.com/mstorsjo/fdk-aac/blob/v2.0.2/libAACdec/src/FDK_delay.h">fdk-aac/libAACdec/src/FDK_delay.h</a>
*/
@SuppressWarnings({"JavadocVariable", "VisibilityModifier"})
public class AACDecoderFDKSignalDelay extends Structure {

private static final List<String> FIELD_ORDER = JNASupport.structureFieldOrder(AACDecoderFDKSignalDelay.class);
public Pointer delay_line;
public short delay;
public byte num_channels;

@Override
protected List<String> getFieldOrder() {
return FIELD_ORDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.sheinbergon.aac.jna.structure;

import com.sun.jna.Structure;
import com.sun.jna.ptr.ShortByReference;
import org.sheinbergon.aac.jna.util.JNASupport;

import java.util.List;

/**
* Maps to SamplingRateInfo struct.
*
* @see <a href="https://github.com/mstorsjo/fdk-aac/blob/v2.0.2/libAACdec/src/aacdecoder.h">fdk-aac/libAACdec/src/aacdecoder.h</a>
*/
@SuppressWarnings({"JavadocVariable", "VisibilityModifier"})
public class AACDecoderSBRParams extends Structure {

private static final List<String> FIELD_ORDER = JNASupport.structureFieldOrder(AACDecoderSBRParams.class);
public int nsDelay;

@Override
protected List<String> getFieldOrder() {
return FIELD_ORDER;
}
}
Loading

0 comments on commit f55eefa

Please sign in to comment.