Introduction
ATRAC3plus (codec id 1) codes 2,048 samples of every channel in each packet. The packet size is the block align of the parameter word. The sound is split into 16 frequency subbands by a polyphase filter; each subband is turned into 128 spectral coefficients by a modified discrete cosine transform, so a frame holds 2,048 coefficients for each channel. Each of the coefficients is quantised in one of up to 28 quantisation units that share a word length, a scale factor and a code table. On top of the spectrum a frame can carry gain control that shapes loudness inside the frame and sine-wave tones that code strong single frequencies on their own. Bits are read most significant first. A frame is decoded without reference to the spectrum of an earlier frame, but the synthesis overlaps each frame with the one before, so the first frame of a stream has no complete sound of its own.
Mono, stereo, 3, 4, 6, 7 and 8 channel layouts are defined, built from mono units and stereo-pair units (see Frame). The explorer decodes all of them. It plays stereo files; it has been compared with FFmpeg on real stereo files. The mono and multichannel layouts follow the same tables and the channel map below, but no real file with such a layout was available to compare with.
Frame
| Bits | Field | Meaning |
|---|---|---|
| 1 | Start bit | Always 0. |
| 2 each | Channel unit type | 0 mono unit (one channel), 1 stereo unit (a pair), 2 extension (not defined; an error), 3 terminator. A channel unit follows each 0 or 1; the frame ends at the terminator. The sequence of types must match the channel count. |
| Channels | Units | Output order |
|---|---|---|
| 1 | mono | 0 |
| 2 | stereo | 0, 1 |
| 3 | stereo, mono | 0, 1, 2 |
| 4 | stereo, mono, mono | 0, 1, 2, 3 |
| 6 | stereo, mono, stereo, mono | 0, 1, 2, 4, 5, 3 |
| 7 | stereo, mono, stereo, mono, mono | 0, 1, 2, 4, 5, 6, 3 |
| 8 | stereo, mono, stereo, stereo, mono | 0, 1, 2, 4, 5, 6, 7, 3 |
In the last three rows the output order puts the unit that follows the first stereo pair (the centre channel) last, the low-frequency channel; the table shows which output channel each decoded channel goes to, in decoding order.
Channel unit
A channel unit has the fields below in this order. For a stereo unit each of the word length, scale factor, code table and spectrum parts is given for channel 0 and then for channel 1, and the second channel can refer to the first.
| Bits | Field | Meaning |
|---|---|---|
| 5 | Quantisation units | The number of quantisation units, less one (1 to 32). The values 29, 30 and 31 are not allowed. Each unit covers a fixed run of coefficients; the 28 units together cover the 16 subbands. |
| 1 | Mute flag | 1 makes the whole unit silent. |
| varies | Word lengths | One word length (0 to 7) for each quantisation unit. 0 means the unit has no spectrum. Units past the last one with a word length are not coded further. |
| varies | Scale factors | One index (0 to 63) for each unit up to the last one with a word length. |
| 1 + varies | Code table indexes | A flag choosing the restricted (4 tables) or full (8 tables) set, then a table index for each coded unit. |
| varies | Spectrum | The quantised coefficients of every unit that has a word length, then the power levels. |
| varies | Swap and negate flags | Stereo units only: a flag list for the coded subbands telling which have their two channels exchanged, then one telling which have the second channel negated. Each list is a flag that is 0 (none), or 1 followed by a flag that is 1 (all) or 0 (followed by one bit for each subband). |
| varies | Window shapes | For each channel, a flag list of the same form for the subbands: 1 gives the subband the short window shape for the frame (see Synthesis). |
| varies | Gain control | For each channel, gain points for the subbands. |
| varies | Tones | A flag, and when set, the sine-wave components. |
| 1 (+8) | Noise | A flag; when set a 4-bit noise level and a 4-bit table index follow. The values are read and kept. FFmpeg ignores them in the synthesis, and so does this decoder. |
Word lengths
A 2-bit mode chooses how a channel’s word lengths are coded. All sums are taken modulo 8.
| Mode | Channel 0 | Channel 1 |
|---|---|---|
| 0 | 3 bits for each unit. | The same. |
| 1 | A 2-bit weight index, the number of units coded (see below), a 5-bit position, a 2-bit delta width and a 3-bit minimum; the units before the position have 3 bits each, the others the minimum plus a delta of the given width. The weight table selected by the weight index is added afterwards. | The number of units coded, then a 2-bit choice of a Huffman table, then for each unit the word length of channel 0 plus a Huffman-coded difference. |
| 2 | The number of units coded, a flag, a 1-bit table choice, a 3-bit start and a 4-bit shape number; the shape table gives a starting word length for each segment of units and a Huffman-coded difference is added for each unit (the flag makes the differences come in pairs, each pair preceded by a bit that says the pair has none). | The number of units coded and a Huffman table; the first unit is channel 0’s plus a difference, each later unit continues from the previous unit with the difference between the two units of channel 0 added. |
| 3 | A 2-bit weight index, the number of units coded, a Huffman table and a 3-bit first word length; each later unit is the one before plus a Huffman-coded difference. The weight table is added afterwards. | The same. |
The number of units coded is a 2-bit fill mode and, when the mode is not 0, a 5-bit count. Mode 0 codes every unit. For the units beyond the count: fill mode 1 leaves them 0; fill mode 2 gives them word length 1 in channel 0 and a bit each in channel 1; fill mode 3 gives word length 1 to the units up to a split point (a 2-bit value plus 1, plus 2 for channel 1) counted from the end of the units for channel 0 or from the coded count for channel 1. A word length outside 0 to 7 after the weights are added is an error. All Huffman codes in the format are canonical codes up to 12 bits long.
Scale factors
A 2-bit mode chooses the coding for each channel; the sums are taken modulo 64.
| Mode | Channel 0 | Channel 1 |
|---|---|---|
| 0 | 6 bits for each unit. | The same. |
| 1 | A 2-bit weight index. Index 3: a 6-bit start and 6-bit shape number give a starting value for each unit, then a 5-bit count of long values (4 bits each, less 7, added), and for the rest a minimum (4 bits less 7) plus a value of the given 2-bit width. Otherwise: a 5-bit count of units with 6 bits each, a 3-bit delta width and 6-bit minimum for the rest. The weight table is subtracted afterwards when the index is 1 or 2. | A 2-bit table choice, then for each unit the value of channel 0 plus a Huffman-coded difference. |
| 2 | A 2-bit choice of one of four tables, a start and shape as above, and a Huffman-coded difference added for each unit. | A 2-bit table choice; the first unit is channel 0’s plus a difference, each later unit continues from the previous with the step between the two units of channel 0 added. |
| 3 | A 2-bit weight index and a 2-bit table choice; the index 3 form takes the shaped start and a 4-bit first step, then each unit adds a Huffman-coded step to the running step. Otherwise 6 bits for the first unit and each later unit is the previous plus a Huffman-coded difference. | The values of channel 0, copied. |
The scale factor is the table value SF[index], which is 2 to the power of (index / 3 − 15) in the FFmpeg table.
Code table indexes
A 1-bit flag chooses the full set (8 tables a word length) or the restricted set (4 tables; a lookup turns the restricted index to a full one). Then, for each channel, a table type bit (0 or 1; there are two families of spectrum tables) and a 2-bit mode:
- 0: a fixed-width index (2 bits restricted, 3 bits full) for each unit.
- 1: a Huffman-coded index for each unit.
- 2: the first index Huffman-coded, each later one the previous plus a Huffman-coded difference (modulo 4 or 8).
- 3: channel 1 only, the index of channel 0 plus a Huffman-coded difference.
Each mode starts with a flag for the number of indexes sent (a 5-bit count when set; it may not exceed the coded units). In channel 1, a unit that has no word length of its own but has one in channel 0 carries one bit; 0 there means the unit copies channel 0’s spectrum.
Spectrum
For each unit with a word length, the table is chosen by the table type, the table index and the word length. There are 112 spectrum tables (2 types × 8 × 7 word lengths); some are the same as another. Each table says how many values one code carries (1, 2 or 4), how many bits each value takes, how many codes go in a group, and whether the values are signed. A code word is read with the Huffman table of the code table, the values are taken out of the result from the low bits up; a value that is not signed has its sign bit sent after it when the value is not 0. When a table has a group size above 1, a bit before each group says whether the group is coded or all zero. The quantised values go to the coefficient positions of the unit; the units start at coefficients 0, 16, 32 and so on (the table QU_TO_SPEC_POS in the source lists all 32).
When the unit has more than two quantisation units, 4-bit power levels follow for the subbands that are coded, one for each power group (a group is a run of subbands; the table SUBBAND_TO_NUM_POWGRPS counts them). A level of 15 turns power compensation off for the group.
Gain control
For each channel a 1-bit flag says whether gain control is present. When it is, a 4-bit count of subbands coded (less one) follows, then optionally a flag and 4-bit number that extends the count of subbands the last coded one is copied to. Each coded subband then has up to 7 gain points (0 to 7), each with a level (0 to 15) and a location (0 to 31); locations must rise. The number of points, the levels and the locations are each coded in one of four modes (fixed width, Huffman, difference against the previous subband or against channel 0, or a minimum plus a narrow delta), as set out in the decodeGainc functions of the source. A point at location l changes the gain over the four samples that start at 4 × l.
Tones
A flag says whether sine-wave components (“generalised harmonic analysis” components) are present. Only the amplitude mode 1 exists (mode 0 is an error). The subbands they cover are numbered by a Huffman code. A stereo unit first sends three flag lists: tones shared with channel 0, tone data swapped between channels, and phase inverted. Then for each channel and each subband it carries:
- An envelope: optional start and stop positions (5 bits each) that fade the tones in and out.
- The number of components in the subband (4 bits, or Huffman-coded, or a difference against channel 0). At most 48 in all.
- The frequencies: 10 bits each, the later ones in fewer bits when the one before leaves less room (the order can be ascending or descending), or a difference against channel 0.
- The amplitudes, as scale factors of 6 bits or Huffman-coded, or differences against the closest component in channel 0.
- The phases: 5 bits each.
Synthesis
- Dequantise. Each coefficient is multiplied by the scale factor and the mantissa factor for its word length. For a unit that has power levels, noise from a 1,024-value table is added in the units with a word length, scaled by the level, so the energy is the same after quantisation. The noise sequence depends on the sum of the scale factors. The swap and negate flags are applied to the stereo pair.
- Inverse MDCT. Each coded subband’s 128 coefficients (in odd-numbered subbands the order reversed) become 256 samples, then are windowed. The window’s first and second half are each either a 128-point sine, or for a subband flagged for it a window that is 32 samples of zero, a 64-point sine rise and a flat part; the flags of this frame and of the previous frame choose each half.
- Gain compensation. The first half of the windowed output is added to the second half kept from the previous frame, and both are scaled by the gain points of the frame and of the next, which cancels the gain control that the encoder applied.
- Tones. Each subband’s tones are generated for the frame and the one before, faded with a Hann window, and added.
- Inverse polyphase filter. A 16-point inverse MDCT turns the 16 subband samples of each time step into 16 values, and a 12-tap-per-output filter on a ring of the last 24 steps gives 16 output samples. The 128 steps make 2,048 samples.
The decoder in this explorer follows FFmpeg’s and agrees with FFmpeg’s output to about 10−7 of full scale on the sample files.
Divergence from spec
No public specification of ATRAC3plus exists. The tables and the syntax are those of FFmpeg’s decoder, which is the oracle the explorer is tested against. FFmpeg does not use the noise level and table index, and does not support tone amplitude mode 0 or the channel unit extension; neither does this decoder, which stops with an error on them rather than guessing. The sign and the phase offset of the two transforms are not written in the FFmpeg source in a form that can be copied; they were fixed by comparing with FFmpeg’s output.
Sources
- FFmpeg
libavcodec/atrac3plus.c,atrac3plus_data.h,atrac3plusdsp.c,atrac3plusdec.candatrac.c: the frame syntax, every Huffman and weight table, the synthesis and the gain compensation. - atracdenc (the encoder)
src/atrac/atrac3p*: used to make test files. - The FFmpeg sample server (
samples.ffmpeg.org, foldersoma,A-codecs/ATRAC3+and the trac ticket folders): the real files used to check the decoder.