ac4_syncframe
The sync frame is the optional layer that makes an AC-4 stream self-framing, defined in annex G of ETSI TS 103 190-1. It is what a
.ac4 file holds, and the form of the media type audio/ac4. Fields are read most significant bit first.
| Field | Bits | Meaning |
|---|---|---|
| sync_word | 16 | 0xAC40: no CRC follows the frame. 0xAC41: a crc_word follows it |
| frame_size | 16 | the size of the raw AC-4 frame in bytes. When it is 0xFFFF, a 24-bit frame_size follows and holds the real size |
| raw_ac4_frame() | frame_size bytes | the table of contents, then the substreams |
| crc_word | 16 | only when the sync word is 0xAC41: the CRC of frame_size and the raw frame |
A frame is therefore 6 bytes longer than its raw frame (4 for the header and 2 for the CRC), or 4 bytes longer without a CRC, or 3 bytes more with the 24-bit escape.
The CRC
The crc_word is a 16-bit CRC over the frame_size field and the raw frame (the sync word is not included; with the
escape, both frame_size fields are). The generator polynomial is x16 + x15 + x2 + 1 (0x8005), the register starts at
0x0000, the data are not reflected and the result is not inverted. A decoder checks a frame by running the CRC over the protected bytes and the
crc_word: the result is 0 when the frame is intact.
The raw frame
raw_ac4_frame() is the unit that MP4 and Matroska store as one sample, without the sync word, frame size or CRC. It is:
| Element | Meaning |
|---|---|
| ac4_toc() | the table of contents, ending on a byte boundary |
| fill_area | bytes between the table of contents and the first substream: the payload_base of the table says how many |
| ac4_substream_data() | the substreams, one after another, byte aligned, with the sizes the substream index table gives. Each is a coded audio substream, a high-frequency extension, an EMDF payloads substream, a presentation substream or an object audio metadata substream, as the table says |
| fill_area | unused bytes at the end of the frame (a constant bit rate encoder pads each frame to the same size) |
Finding frames
A reader starts at the first byte, reads the sync word and frame size, and expects the next sync word at the end of the frame (or the end of
the file). After damage it searches for AC 40 or AC 41 and accepts a frame only if it is trusted: a 0xAC41 frame whose CRC
checks, or a 0xAC40 frame followed by another sync word or the end of the file. The explorer also tells a .ac4 file from other data this
way, over the first frame. Frame sizes of 6 to a few thousand bytes are usual; the frame rate follows the table of contents (for example 25 or 23.44
frames a second, so a 48 kHz stereo stream at 128 kbit/s has frames of about 640 bytes at 25 frames a second).