Introduction
ATRAC3 (codec id 0) codes 1,024 samples of both channels in each packet. The packet size comes from the parameter word. The sound is split into four frequency bands by two mirror filters; each band is turned into 256 spectral coefficients by a modified discrete cosine transform. A frame carries those 1,024 coefficients for each channel, plus gain control that shapes the loudness inside the frame and tonal components that code strong single frequencies separately. Bits are read most significant first.
Sound unit
A sound unit is one channel of one frame. With separate coding (joint stereo bit 0) the packet is cut in two halves of equal size, one unit in each. All fields follow one another with no padding between them.
| Bits | Field | Meaning |
|---|---|---|
| 6 | Unit id | Always 0x28 (101000). Any other value is an error. |
| 2 | Bands coded | The number of bands with data, less one (so 1 to 4 of the four). |
| per band | Gain control | For each coded band: a 3-bit count of gain points (0 to 7), then for each point a 4-bit level and a 5-bit location. Locations must rise. A point at location l changes the gain over the eight samples that start at 8 × l. |
| 5 | Tonal component groups | 0 when there are none. Otherwise a 2-bit coding selector follows (2 is not allowed; 0 and 1 fix the coding mode, 3 lets each group choose), then for each group: a 1-bit flag for each coded band, a 3-bit count of coefficients less one, a 3-bit quantiser index (at least 2), the mode bit when the selector is 3, and for every flagged group of 64 coefficients a 3-bit count of components, each with a 6-bit scale factor, a 6-bit position within the 64, and the coefficients. At most 64 components a unit. |
| 5 | Subbands | The number of spectral subbands coded, less one (up to 32). |
| 1 | Fixed length flag | 1 when the coefficients of the spectrum are fixed-length codes, 0 when they are Huffman codes. |
| 3 each | Quantiser selectors | One for each subband; 0 means the subband is silent. |
| 6 each | Scale factors | One for each subband whose selector is not 0. The factor is 2 to the power of (index − 15) / 3. |
| rest | Coefficients | For each subband with a selector: its coefficients, in the subband table of 0, 8, 16, … up to 1,024. Selector 1 codes pairs of values; selectors 2 and above code one value each. The value is multiplied by the scale factor and by the inverse of the largest quantised value of that selector. |
The tonal components are added to the spectrum after it has been rebuilt.
Joint stereo
With the joint stereo bit set, the packet holds one block for both channels. The first unit starts at the first byte, as above, and takes the sound unit syntax with the 6-bit id. The second unit is stored from the other end: the packet is read last byte first, after skipping the filler bytes 0xF8 at the end of the packet, and starts with this header:
| Bits | Field | Meaning |
|---|---|---|
| 1 | Weighting flag | Swaps the two weights that the index gives. |
| 3 | Weighting index | 7 means no weighting; otherwise a = index / 7 and b = √(2 − a²). |
| 4 × 2 | Matrix | One mode for each of the four bands: 0, 1, 2 or 3, which tells how the two units are the sum and difference, or the mid and side, of left and right. |
| 2 | Unit id | Always 3 (the second unit has a two-bit id instead of six), followed by the rest of a sound unit. |
The decoder undoes the matrixing and applies the weighting in the three upper bands, using the values of the previous frame to blend the first eight coefficients of each band.
Synthesis
- Each band’s 256 coefficients are reversed in every second band (the spectrum of odd bands is mirrored) and transformed by an inverse MDCT to 512 samples, then multiplied by a 512-point sine-derived window.
- The first half is added to the second half kept from the previous frame, scaled by the gain control of this frame and the next frame (gain compensation); the second half is kept.
- Two inverse mirror filters of 48 taps join the bands in pairs, and a third joins the results, giving 1,024 samples a channel.
The decoder in this explorer follows FFmpeg’s and agrees with FFmpeg’s output to about 3×10−8 of full scale, for separately coded and for joint stereo files.
Sources
- FFmpeg
libavcodec/atrac3.c,atrac3data.handatrac.c: the unit syntax, the Huffman tables, the subband table, the matrices, the filters and the windows. - atracdenc (the encoder, commit cb170709…)
src/atrac/atrac3.cppandatrac3_bitstream.cpp: used to make test files in both stereo modes.