An AC-3 stream, defined in ETSI TS 102 366 (clause 4 and Table 4.13), is a run of sync frames with nothing between them. Each frame stands alone and holds 1536 samples of each channel, as six audio blocks of 256 samples. All values are big-endian, most significant bit first.
Frame layout
0Sync information sync word, CRC, sample rate, frame size5 bytes5Bit stream information version, channels, dialogue level and morevaries…Audio blocks 0 to 5 256 samples per channel eachvaries…Auxiliary data unused space, then its lengthvaries…Error check a reserved bit and the second CRC17 bits
Sync information
| Bits | Width | Name | Meaning |
|---|---|---|---|
| 0–15 | 16 | syncword | 0B 77 |
| 16–31 | 16 | crc1 | the check on the first part of the frame |
| 32–33 | 2 | fscod | the sample rate code |
| 34–39 | 6 | frmsizecod | the frame size code |
Sample rate codes
| fscod | Rate |
|---|---|
| 0 | 48,000 Hz |
| 1 | 44,100 Hz |
| 2 | 32,000 Hz |
| 3 | reserved: the decoder should mute |
TS 102 366 gives bsid 9 and 10 no meaning (decoders must mute), but some
decoders, FFmpeg’s among them, read such frames as the standard syntax at half and a
quarter of these rates (FFmpeg’s ac3_parser.c, read this session: sr_shift = max(bsid, 8) − 8, applied to the sample rate and bit rate); see bit
stream information.
liba52 (a52dec liba52/parse.c, a52_syncinfo) does the same, which makes it a second program that does: a halfrate table of 1, 2 and 3 for bsid 9, 10 and 11 shifts both the sample rate and the bit rate, and it refuses bsid 12 and above (buf[5] >= 0x60). It also reads bsid 11 (one eighth of the rate), and it refuses frmsizecod 38 and above. At 44.1 kHz it gives the frame size in bytes as 2 × (320 × bitrate / 147 + (frmsizecod & 1)), with the bit rate in kbit/s.
Frame size codes
Two codes share each bit rate. At 44,100 Hz, a frame cannot be a whole number of 16-bit words long, so the second code of each pair adds one word to even things out. Lengths are in 16-bit words; double them for bytes.
| frmsizecod | Bit rate | 48,000 Hz | 44,100 Hz | 32,000 Hz |
|---|---|---|---|---|
| 0, 1 | 32 kbit/s | 64 | 69, 70 | 96 |
| 2, 3 | 40 kbit/s | 80 | 87, 88 | 120 |
| 4, 5 | 48 kbit/s | 96 | 104, 105 | 144 |
| 6, 7 | 56 kbit/s | 112 | 121, 122 | 168 |
| 8, 9 | 64 kbit/s | 128 | 139, 140 | 192 |
| 10, 11 | 80 kbit/s | 160 | 174, 175 | 240 |
| 12, 13 | 96 kbit/s | 192 | 208, 209 | 288 |
| 14, 15 | 112 kbit/s | 224 | 243, 244 | 336 |
| 16, 17 | 128 kbit/s | 256 | 278, 279 | 384 |
| 18, 19 | 160 kbit/s | 320 | 348, 349 | 480 |
| 20, 21 | 192 kbit/s | 384 | 417, 418 | 576 |
| 22, 23 | 224 kbit/s | 448 | 487, 488 | 672 |
| 24, 25 | 256 kbit/s | 512 | 557, 558 | 768 |
| 26, 27 | 320 kbit/s | 640 | 696, 697 | 960 |
| 28, 29 | 384 kbit/s | 768 | 835, 836 | 1,152 |
| 30, 31 | 448 kbit/s | 896 | 975, 976 | 1,344 |
| 32, 33 | 512 kbit/s | 1,024 | 1,114, 1,115 | 1,536 |
| 34, 35 | 576 kbit/s | 1,152 | 1,253, 1,254 | 1,728 |
| 36, 37 | 640 kbit/s | 1,280 | 1,393, 1,394 | 1,920 |
| 38 to 63 | not valid | |||
Auxiliary data and the error check
Whatever bits the encoder does not use for audio are left between the last block and the
error check. The encoder sizes this field so that crc2 lands on the frame’s
last 16-bit word. Some or all of it can carry user data, which sits at its end, just
before auxdatal, so a reader that decodes no audio can find it by working
backwards from the end of the frame.
| Width in bits | Name | Meaning |
|---|---|---|
| nauxbits | auxbits | the frame’s bits minus every other field’s (skip fields count as used): padding, then auxdatal bits of user data |
| 14 | auxdatal | the length of the user data in bits; only if auxdatae is 1 |
| 1 | auxdatae | 1 means there is user data and auxdatal precedes this bit |
| 1 | crcrsv | reserved; an encoder may flip it to keep crc2 from equalling the sync word. In Enhanced AC-3 this bit is encinfo, for the encoder’s use |
| 16 | crc2 | the check on the whole frame |
User data may be an EMDF metadata container.
CRC
Both checks use the 16-bit polynomial x16 + x15 + x2 + 1
(0x8005), a shift register starting at zero, fed most significant bit
first; the sync word is left out. crc1 is chosen by the encoder so that the
register is zero after the first 5/8 of the frame; it is not the CRC of that part, so a
decoder should not compute one and compare. The 5/8 point counts the sync word:
5/8 frame size (words) = (frame size >> 1) + (frame size >> 3)
crc1 checks bytes 2 to 2 × (5/8 frame size) − 1; crc2 checks bytes 2 to the end
At 48 kHz this is 40 words for the 64-word frames, 800 for the 1,280-word ones; at
44.1 kHz it is 42 for 69 words and 871 for 1,394. crc2 then brings the
register to zero again at the end of the frame, so it covers the last 3/8 and, run from
byte 2, the whole frame. The encoder keeps the sync information, the bit stream
information and blocks 0 and 1 inside the first 5/8, so a decoder may start on block 0
as soon as crc1 checks. If crc1 fails nothing in the frame can
be decoded; what to do then (mute, repeat a block or frame) is the decoder’s choice.
An Enhanced AC-3 frame has only crc2, which a decoder must check before
decoding any block of the frame.
An example
A frame starting 0B 77 D1 2C 18 at 48,000 Hz:
| Field | Value |
|---|---|
| syncword | 0B 77 |
| crc1 | D1 2C, the frame’s own check |
| fscod | 00: 48,000 Hz |
| frmsizecod | 011000, 24: 256 kbit/s |
The frame is 512 words long, 1,024 bytes.