The header is the first 1,024 bytes of the file. It is the structure SFHEADER, a union of the fixed items and a
1,024-byte filler, in IRCAM’s sfheader.h and in the BICSF header of the same name (sfheader.7). Only the
first four items are fixed; after them comes the chain of codes, and the rest is zero.
The fixed items
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | bytes | sf_magic | the magic number; it also says the byte order of every number that follows (below) |
| 4 | 4 | float | sf_srate | the sample rate in hertz, as a 32-bit IEEE float, so 22050.0 is 46 AC 44 00 in big-endian order |
| 8 | 4 | int | sf_chans | the number of channels; the samples of the channels are interleaved. SF_MAXCHAN is 4, the most channels a file can carry the maximum amplitudes of and the most the BICSF programs play |
| 12 | 4 | int | sf_packmode | how a sample is stored: the packing modes |
| 16 | 1,008 | codes | sf_codes | the chain of codes, from byte 16 to the code 0 that ends it, then zeros up to byte 1,024 (Codes) |
The sizes add up: 4 + 4 + 4 + 4 + 1,008 = 1,024. The structure declares sf_chans and sf_packmode as int in BICSF’s
header and as long in IRCAM’s, which are both 4 bytes on the 32-bit machines the format comes from; FFmpeg and libsndfile read 4 bytes.
The magic number and the byte order
The magic is a 32-bit number that the writing program stores in its own byte order, and so the order of its bytes tells the
order of all the other numbers in the header and of the samples. IRCAM’s header defines it from three constants: the byte 0x64
(SF_MAGIC1), the byte 0xA3 (SF_MAGIC2) and a machine code (SF_MACHINE): 1 for a VAX, 2 for a Sun,
3 for a MIPS machine (DECstation) and 4 for a NeXT. A little-endian machine builds the number as 0x64 | 0xA3 << 8 | machine << 16
(so the file starts 64 A3 MM 00) and a big-endian machine as 0x64 << 24 | 0xA3 << 16 | machine << 8 (the same bytes in the file, 64 A3 MM 00).
In the Berkeley/IRCAM/CARL header the magic is the plain number 107364 (0x0001A364), which a little-endian machine writes as 64 A3 01 00 (the
same as IRCAM’s VAX magic) and a big-endian machine as 00 01 A3 64.
Reading the headers and FFmpeg’s reader together, the four bytes give the byte order like this:
| Bytes | Machine | Numbers are | Source |
|---|---|---|---|
64 A3 01 00 | 1, VAX | little-endian | IRCAM sfheader.h; FFmpeg |
64 A3 02 00 | 2, Sun | big-endian | IRCAM sfheader.h; FFmpeg; libsndfile writes this form |
64 A3 03 00 | 3, MIPS | little-endian | IRCAM sfheader.h; FFmpeg; libsndfile writes this form |
64 A3 04 00 | 4, NeXT | big-endian | IRCAM sfheader.h only: FFmpeg does not list it |
00 01 A3 64 | 1 | big-endian | FFmpeg; the number 107364 on a big-endian machine |
00 02 A3 64 | 2 | little-endian | FFmpeg |
00 03 A3 64 | 3 | big-endian | FFmpeg |
00 04 A3 64 | 4 | little-endian | FFmpeg’s table |
libsndfile does not use a table: it accepts 64 A3 xx 00 and 00 xx A3 64 for any xx (its comment says the magic number “is weird in that one
byte in the number can have values of 0x1, 0x2, 0x03 or 0x04”), reads the numbers as little-endian, and switches to big-endian when the channel count then comes out above its limit.
The viewer accepts all eight and says which it found. A file whose first four bytes are anything else is not an IRCAM sound file;
IRCAM’s isforeignmagic test is just the first two bytes, 64 A3, and its sf_param byte (the fourth) is 0.
The packing mode
sf_packmode says how a sample is stored. Its low 16 bits are the bytes a sample takes (sfsamplesize); its high 16 bits tell the
kind of coding when the bytes alone do not. IRCAM’s header names six values; FFmpeg adds two more.
sf_packmode | Name | Samples | Source |
|---|---|---|---|
0x00001 | SF_CHAR | 8-bit signed linear | IRCAM sfheader.h; FFmpeg |
0x00002 | SF_SHORT | 16-bit signed linear | IRCAM and BICSF headers; FFmpeg; libsndfile |
0x00003 | 24-bit signed linear | FFmpeg only | |
0x00004 | SF_FLOAT, SF_cbFLOAT | 32-bit IEEE float | IRCAM and BICSF headers; FFmpeg; libsndfile |
0x00008 | 64-bit IEEE float | FFmpeg only | |
0x10001 | SF_ALAW | 8-bit A-law (G.711) | IRCAM sfheader.h; FFmpeg; libsndfile |
0x20001 | SF_ULAW | 8-bit µ-law (G.711) | IRCAM sfheader.h; FFmpeg; libsndfile |
0x40004 | SF_cbLONG | 32-bit signed linear | IRCAM sfheader.h; FFmpeg; libsndfile |
The bytes of each sample use the byte order the magic gives. A packing mode that is not in the table is one no source defines; the viewer flags it and shows the samples as bytes.
An example header
The start of a mono 16-bit sound at 22,050 Hz, as FFmpeg writes it (the rest of the 1,024 bytes is zero):
| Offset | Bytes | Meaning |
|---|---|---|
| 0 | 64 A3 01 00 | sf_magic: machine 1, little-endian numbers |
| 4 | 00 44 AC 46 | sf_srate 22,050.0 |
| 8 | 01 00 00 00 | sf_chans 1 |
| 12 | 02 00 00 00 | sf_packmode 2, 16-bit signed linear |
| 16 | 1,008 zero bytes | sf_codes: the first code is 0, so the chain is empty |
| 1024 | … | the samples |
Sources: IRCAM’s sfheader.h version 1.5 of 28 March 1992 (the structure, SF_MAGIC1, SF_MAGIC2, the machine codes, the packing modes), the
Berkeley/IRCAM/CARL sfheader.h and its manual page sfheader.7 (the 1,024-byte union, SF_MAGIC 107364), FFmpeg’s ircam.c and
ircamdec.c (the magic table and the packing modes 3 and 8) and libsndfile’s ircam.c (the marker forms it writes). The example is a file written by FFmpeg.