• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sheinbergon / jna-aac-encoder / 44710452468

20 Nov 2026 09:24AM UTC coverage: 94.394% (-5.1%) from 99.461%
44710452468

Pull #36

github

sheinbergon
Add AAC decoding support through the Java audio system

Decode ADTS framed AAC (AAC-LC/HE-AAC/HE-AACv2) to 16 bit signed
little-endian PCM using the fdk-aac decoder:

- AACAudioDecoder, a frame-by-frame decoder over an ADTS input stream
- AACFileReader (AudioFileReader SPI) detecting ADTS AAC audio
- AACFormatConversionProvider (FormatConversionProvider SPI) for AAC to
  PCM_SIGNED conversion
- Treat the fdk-aac decoder handle as opaque, replacing the internal
  struct mappings
- Bump version to 2.2.0

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Pull Request #36: Add AAC decoding support through the Java audio system

95 of 108 branches covered (87.96%)

Branch coverage included in aggregate %.

125 of 141 new or added lines in 9 files covered. (88.65%)

427 of 445 relevant lines covered (95.96%)

4.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.44
/src/main/java/org/sheinbergon/aac/decoder/AACAudioDecoder.java
1
package org.sheinbergon.aac.decoder;
2

3
import com.sun.jna.Memory;
4
import jakarta.annotation.Nonnull;
5
import jakarta.annotation.Nullable;
6
import lombok.Getter;
7
import lombok.experimental.Accessors;
8
import lombok.val;
9
import org.sheinbergon.aac.encoder.util.NotThreadSafe;
10
import org.sheinbergon.aac.jna.FdkAACLibFacade;
11
import org.sheinbergon.aac.jna.structure.AACDecoder;
12

13
import java.io.IOException;
14
import java.io.InputStream;
15

16
/**
17
 * Decodes an ADTS framed AAC stream to interleaved, 16 bit signed little-endian PCM audio.
18
 */
19
@NotThreadSafe
20
@Accessors(fluent = true)
21
public final class AACAudioDecoder implements AutoCloseable {
22

23
  private static final int EOS = -1;
24
  private static final int IN_BUFFER_SIZE = 8192;
25
  // Up to 2048 samples per frame (HE-AAC), for up to 8 output channels
26
  private static final int OUT_BUFFER_SIZE = 2048 * 8 * Short.BYTES;
27

28
  private final InputStream input;
29
  private final AACDecoder decoder = FdkAACLibFacade.openDecoder();
3 ✔
30
  private final byte[] chunk = new byte[IN_BUFFER_SIZE];
4 ✔
31
  // Hard references are advised for memory buffers
32
  private final Memory inBuffer = new Memory(IN_BUFFER_SIZE);
6 ✔
33
  private final Memory outBuffer = new Memory(OUT_BUFFER_SIZE);
6 ✔
34

35
  private int offset = 0;
3 ✔
36
  private int pending = 0;
3 ✔
37
  private boolean eos = false;
3 ✔
38
  private boolean closed = false;
3 ✔
39

40
  /**
41
   * The decoded audio sample rate. Only available after the first frame was decoded.
42
   */
43
  @Getter
44
  private int sampleRate;
45
  /**
46
   * The decoded audio channel count. Only available after the first frame was decoded.
47
   */
48
  @Getter
49
  private int channels;
50

51
  /**
52
   * @param adts the ADTS framed AAC stream to decode, closed along with this decoder
53
   */
54
  public AACAudioDecoder(final @Nonnull InputStream adts) {
2 ✔
55
    this.input = adts;
3 ✔
56
  }
1 ✔
57

58
  /**
59
   * Decode the next AAC frame.
60
   *
61
   * @return the decoded frame's interleaved PCM (s16le) bytes, or {@code null} if the input stream is exhausted
62
   * @throws IOException if the underlying stream could not be read
63
   */
64
  @Nullable
65
  public byte[] decode() throws IOException {
66
    if (closed) {
3 !
NEW
67
      throw new IllegalStateException("Decoder instance already closed");
×
68
    }
69
    while (true) {
70
      readInput();
2 ✔
71
      feedDecoder();
2 ✔
72
      if (FdkAACLibFacade.decodeFrame(decoder, outBuffer)) {
6 ✔
73
        return decodedFrame();
3 ✔
74
      } else if (eos && pending == 0) {
6 !
75
        return null;
2 ✔
76
      }
77
    }
78
  }
79

80
  // Read the next chunk off the input stream, once the previous one was fully consumed
81
  private void readInput() throws IOException {
82
    if (pending == 0 && !eos) {
6 ✔
83
      val read = input.read(chunk);
6 ✔
84
      eos = read == EOS;
8 ✔
85
      if (!eos) {
3 ✔
86
        inBuffer.write(0, chunk, 0, read);
8 ✔
87
        offset = 0;
3 ✔
88
        pending = read;
3 ✔
89
      }
90
    }
91
  }
1 ✔
92

93
  // The library's internal bit-buffer might not have room for everything, leftovers are kept for the next round
94
  private void feedDecoder() {
95
    if (pending > 0) {
3 ✔
96
      val remaining = FdkAACLibFacade.fillDecoder(decoder, inBuffer.share(offset), pending);
12 ✔
97
      offset += pending - remaining;
9 ✔
98
      pending = remaining;
3 ✔
99
    }
100
  }
1 ✔
101

102
  private byte[] decodedFrame() {
103
    val info = FdkAACLibFacade.getDecoderStreamInfo(decoder);
4 ✔
104
    sampleRate = info.sampleRate();
4 ✔
105
    channels = info.channels();
4 ✔
106
    // All supported platforms are little-endian, so native 16 bit samples are already s16le
107
    return outBuffer.getByteArray(0, info.frameSize() * info.channels() * Short.BYTES);
12 ✔
108
  }
109

110
  @Override
111
  public void close() throws IOException {
112
    if (!closed) {
3 !
113
      closed = true;
3 ✔
114
      FdkAACLibFacade.closeDecoder(decoder);
3 ✔
115
      input.close();
3 ✔
116
    }
117
  }
1 ✔
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc