Introduction
AMR is a speech codec standardised by 3GPP for mobile phone calls. It switches
between bit rates from 4.75 to 12.2 kbit/s to suit the radio conditions. Phones used it
for voice memos and voice messages, so .amr files are common in old phone
backups. The narrowband codec samples at 8 kHz; the wideband version, AMR-WB, samples
at 16 kHz and its files use .awb.
Structure
The storage format is defined in RFC 4867. A narrowband file starts with the six bytes
#!AMR and a line feed; a wideband file starts with #!AMR-WB
and a line feed. Frames follow with nothing between them. Each frame is 20 ms of
speech: a header byte whose frame type field gives the mode, and so the frame’s
length, followed by the speech bits.
0Magic #!AMR and a line feed6 bytes6Frames header byte, then speech bits, 20 ms eachrepeated
The reference pages cover both codecs:
- File layout – magic numbers, channels, frame headers and AMR in 3GP
- Frame types – every mode of AMR and AMR-WB, with sizes
- Speech bits – how each mode divides its bits among its parameters
Decoding AMR and AMR-WB
The explorer turns AMR into sound wherever the format is stored. A bare .amr
or .awb file is read and played by its own viewer, which shows the magic number,
every frame (its header octet and speech bits), the frame types and the waveform. Inside other
files the sound is decoded in a WAVE file (format codes
0x7361 and 0x7A21 for narrowband, 0x7362 for wideband),
in a CAF file (samr and sawb)
and in a 3GP file (samr and sawb
sample entries).
Narrowband
All eight modes (4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2 and 12.2 kbit/s) are decoded, with mode changes from frame to frame, comfort noise (SID frames and frames with no data, as discontinuous transmission sends them), lost and bad frames, and the decoder homing frames. The output is 8,000 Hz, one channel; as in the 3GPP decoder, only the top 13 bits of each 16-bit sample are used. GSM enhanced full rate uses the coder of the 12.2 kbit/s mode, but it has a decoder of its own (below), because the two differ in the last bit.
GSM enhanced full rate
GSM enhanced full rate (EFR, GSM 06.60) codes 8,000 Hz speech at 12.2 kbit/s in frames of 244 bits, 20 ms each. The 57 parameters of a frame are the ones of the AMR 12.2 kbit/s mode, in the same order and widths (7, 8, 9, 8 and 6 bits of line spectral indices, then four subframes of pitch lag, pitch gain, ten pulse fields and codebook gain), so a frame carries the same bits as the core frame of an AMR 12.2 frame in parameter order. The decoder is a separate module that takes the 244 bits, a bad-frame flag, a SID flag (0 speech, 1 invalid SID, 2 valid SID) and a time-alignment flag, and returns 160 samples. A frame is a valid SID frame when fewer than 2 of its 95 marked bits are 0, and an invalid one when fewer than 16 are.
It differs from the AMR decoder in the post-filter and gain-control arithmetic, in having no high-pass filter on the output (the result is doubled and cut to 13 bits), in its own rules for replacing and muting lost frames, and in its own comfort noise: LSF and gain averaging over seven frames, interpolation between SID updates over 24 frames, and a pseudo-noise pulse excitation. A decoder homing frame (57 parameters with a fixed pattern) resets the decoder, and the first such frame is answered with the encoder homing pattern.
The only stored form with a public definition is the RTP payload: 31 bytes, a 4-bit signature
1100 and then the 244 bits, most significant bit first (RFC 3551 section 4.5.9, ETSI
TS 101 318). It has no file form and no magic number beyond that nibble. The decoder reads such
frames in sequence, but no file format the explorer reads is defined to carry EFR frames, so it
is not called from any viewer yet.
Wideband
All nine modes (6.60, 8.85, 12.65, 14.25, 15.85, 18.25, 19.85, 23.05 and 23.85 kbit/s) are decoded, with comfort noise (SID frames, no-data frames and the muting after a long silence), lost and bad frames and the decoder homing frames. A frame is 320 samples at 16,000 Hz; the two low bits of each sample are zero, as in the 3GPP decoder. The coded speech runs at 12,800 Hz (up to 6.4 kHz) and is resampled to 16,000 Hz; the band from 6 to 7 kHz is made from a noise source shaped by the decoded filter (extrapolated from the low-band filter in the modes up to 6.60 kbit/s), with its level set from the low band, so the high band is a synthesis, not a coding of the original. At 23.85 kbit/s each subframe carries a 4-bit gain for that noise, and the result is low-pass filtered at 7 kHz.
How it works
The decoder is the 3GPP fixed-point reference decoder (TS 26.073) written in JavaScript operation for operation, using the same saturating 16-bit and 32-bit arithmetic, so that its output is exactly the reference output. For each 20 ms frame it does the following.
- Puts the stored bits back in parameter order (the bit orders of the 3GPP storage formats differ from the order the parameters are coded in) and splits them into the parameters of the mode.
- Decodes the line spectral frequencies: split-matrix quantisation with moving-average prediction, one set a frame for modes up to 10.2 kbit/s and two for 12.2, interpolated to each 5 ms subframe, then turned into filter coefficients.
- Decodes, for each subframe, the pitch lag (integer and fractional), the algebraic codebook pulses (2 to 10 pulses, by mode) and the two gains, with the gain of the codebook predicted from the previous subframes.
- Builds the excitation from the pitch and codebook parts, applies the phase dispersion of the low modes, and runs it through the synthesis filter.
- Post-filters (formant and tilt shaping, then gain control) and high-pass filters the result.
The state carries from frame to frame, so a frame cannot be decoded without the ones before it. That makes the sound sequential: the viewers decode the track from its start to the length they show.
Tables
The decoder needs 13,699 table values, which are in shared/audio/amr-nb-tables.js
and were converted from the reference C code by a script and checked by a second,
independently written one (the counts, the sum and a hash of every value must agree). They
are the line-spectral-frequency codebooks (three splits for modes up to 10.2 kbit/s, five for
12.2), the pitch and codebook gain tables, the 4.75 kbit/s joint gain table, the window of
the comfort noise and the eight bit orders of the storage formats.
Conformance
All 573 decoder output files of the 3GPP conformance sequences (TS 26.074: 252,063 frames
in every mode, with mode switching, discontinuous transmission, and the A-law and
µ-law variants) are reproduced bit for bit. Stored files were checked the same way:
files written by the reference encoder, in every mode, with mode switching and with
discontinuous transmission, decode to exactly what the reference decoder writes for them.
The IF2 storage format (a 4-bit frame type, no header byte) is read from its description
and was not checked against a file. How a WAVE file with format code 0x7A21
lays out its frames is not published by Microsoft; they are read as one header byte and
speech bits after another, as FFmpeg reads them.
How the wideband decoder works
It follows the same plan with a different codec (3GPP TS 26.173, ITU-T G.722.2), also written operation for operation. A frame is four subframes of 64 samples at 12,800 Hz.
- Puts the stored bits back in parameter order and reads the parameters of the mode; a frame that was damaged or lost is replaced by values predicted from the frames before it.
- Decodes the immittance spectral frequencies of the frame (two-stage split-matrix quantisation with moving-average prediction: seven indices for the modes above 6.60 kbit/s, five for 6.60), interpolates them to each subframe and turns them into filter coefficients.
- For each subframe decodes the pitch lag (a quarter-sample or half-sample fraction, by mode), an optional low-pass filter of the pitch part, the algebraic codebook (from 12 to 88 bits of pulse positions and signs, by mode) and the two gains.
- Builds the excitation, with a noise enhancer and a high-pass pitch enhancer on the codebook part, and phase dispersion in the low modes, and runs it through the synthesis filter at double precision.
- De-emphasises, high-pass filters at 50 Hz, and resamples 12,800 Hz to 16,000 Hz (five samples from four); then adds the high-band noise.
- Comfort noise frames carry five spectrum indices, a level and a dithering bit; between them the decoder interpolates, and after a long silence it fades the noise out.
The tables (12,057 values: the spectrum codebooks of both coders and of the comfort noise,
the gain tables, the resampling and filter coefficients, the stored bit orders and the homing
frames) are in shared/audio/amr-wb-tables.js, converted from the reference C code
by a script and checked by a second one in the same way as the narrowband tables.
Conformance: all 221 decoder output files of the 3GPP test sequences (TS 26.174: 119,660 frames in all nine modes, with mode switching and discontinuous transmission) are reproduced bit for bit. Stored files made from the sequences (every mode, mode switching, discontinuous transmission) decode to exactly the published output through the stored-frame reading, the WAVE, CAF and 3GP routes and the bare-file viewer. The sequences do not contain lost frames of the kind a network makes, so that concealment follows the reference code but was not compared with a published output.
Not described
These pages follow the AMR storage format as common tools write it. The explorer reads a
bare .amr or .awb file (single or multi-channel) and decodes the sound
of AMR and AMR-WB in the WAVE, CAF and 3GP viewers as well.
Known, not yet covered
- Reference: the 3GPP TS 26.244 sample-entry structure on the file layout page: the standard could not be read, so its fields are given only as tools write them.
- Viewer: AMR-WB+ (the
sawpsample entry, WAVE code0x7363) is not decoded: it is a different codec (TS 26.290) that was not ported. - Viewer: the interface format 1 (IF1) frame of TS 26.101 (three header octets: frame type, quality bit and mode indication; mode request; CRC; then the core frame) is read by the narrowband decoder when it is told the format, but its CRC octet is not checked, because neither specification gives a worked value to settle the bit order, and the wideband IF1 frame of TS 26.201 is not read. The GSM-EFR, TDMA-EFR and PDC-EFR comfort noise frames of IF1 (frame types 9 to 11) are not decoded. IF2 (a 4-bit frame type and then the bits) is read by the decoders from its description; neither format has a magic number, so a bare file in IF1 or IF2 is not recognised.
- Viewer: the WAVE format code
0x7362is read as frames in the storage layout, by analogy with0x7361: Microsoft has not published its layout. The codes0xA104and0xA111(VoiceAge and GSM AMR wideband) are named but not decoded. - Viewer: the channel order of a multi-channel file (RFC 4867 refers to RFC 3551 for it) is not named: the channels are numbered. A frame lost or damaged on a real network (quality bit 0, or frame type 14 in wideband) is concealed as the reference decoder does, but the conformance sequences hold no such frames to compare with.
- Viewer: GSM enhanced full rate (GSM 06.60) is decoded by its own module, checked
bit for bit against the 28 ETSI test sequences, but it is not wired to a file reader: its RTP
payload (31 bytes starting with the nibble 1100) has no file form, and the WAVE code
0xA10Fis named without a published frame layout. An AMR file that holds 12.2 kbit/s frames is decoded by the AMR decoder, which differs from the EFR decoder in the last bit.
Links
Software
- VLC media player – free player for almost any audio or video file (Windows, macOS, Linux, mobile)
- FFmpeg – command-line tool that converts AMR to and from other formats (Windows, macOS, Linux)
Developers
- opencore-amr – AMR-NB and AMR-WB codec libraries (C)
Reference
- RFC 4867 – the RTP payload and file storage formats for AMR and AMR-WB
- 3GPP TS 26.101 and TS 26.201 – the frame structure of AMR and AMR-WB
- 3GPP TS 26.244 – the 3GP file format and its AMR sample entries