# MPEG-1 Audio Layers I and II

*226,750*pages on

this wiki

## Decoding Bitstream

### Header

- syncword=0xfff constant 12 bits
- ID=1 constant 1 bit
- layer 2 bits, 00=reserved,01=Layer 3,10=Layer 2, 11=Layer 1
- protection_bit 1 bit
- bitrate_index 4 bits. kbits/s. 0 is variable bit rate, 0xf is forbidden. Otherwise unsigned int value b. Layer 1=b*32 kbits/s, Layer 2={32,48,56,64,80,96,112,128,160,192,224,256,320,384}[b-1],Layer 3={32,40,48,56,64,80,96,112,128,160,192,224,256,320}[b-1]
- sampling_frequency 2 bits. 0=44.1 kiloHertz, 1=48 kHz, 2=32 kHz, 3=reserved
- padding_bit 1 bit
- private_bit 1 bit
- mode 2 bits
- mode_extension 2 bits
- copyright 1 bit
- original 1 bit
- emphasis 2 bits

### Audio Data

#### Layer I Audio Data

Three arrays are stored in the audio data:

- allocation[channel,subband] The allocation array that determines the number of bits allocated for samples in each channel and subband
- scaleindex[channel,subband] The scalefactor array that determines the scaling that is applied to the subband
- sample[channel,subband,sample_n] Is the actual data used for later decoding of the data

The number of subbands is always 32, the number of sample_n is 12, and the number of channels is determined by the mode in the header (?how?). getbits is a procedure that returns the unsigned int value of the number of bits, and advances the file pointer.

- number_channels determined by header information
- bound determined by header information
- for subband in 0..bound-1:
- for channel in 0..number_channels-1:
- allocation[channel,subband] = getbits(4)

- for channel in 0..number_channels-1:
- for subband in bound..31:
- allocation[0,subband] = getbits(4)

- for subband in 0..31:
- for channel in 0..number_channels-1:
- if(allocation[channel,subband]>0):
- scaleindex[channel,subband] = getbits(6)

- if(allocation[channel,subband]>0):

- for channel in 0..number_channels-1:
- for sample_n in 0..11:
- for subband in 0..bound-1:
- for channel in 0..number_channels-1:
- if(allocation[channel,subband] > 0:
- sample[channel,subband,sample_n] = getbits(allocation[channel,subband]+1)

- if(allocation[channel,subband] > 0:

- for channel in 0..number_channels-1:
- for subband in bound..31:
- if(allocation[0,subband]>0):
- sample[0,subband,sample_n) = getbits(allocation[channel,subband]+1)

- if(allocation[0,subband]>0):

- for subband in 0..bound-1:

## Requantization of Samples

### Layer 1

First the numberbits of bits are gotten from the stream based on the number specified in the audio data. These are converted to a fractionalizednumber, which is then converted to a sample number. The numbers are stored as a sign bit (which gives adder=-1 if the bit = 1, and adder=0 if the sign bit = 0), and a integer s which together are used to get the fractionalized number.

In the above, numberbits is the number of bits in s plus the sign bit (and this ranges from 2 to 15), adder is -1 if the sign bit is set, and 0 otherwise, s is the unsigned integer value of the nb-1 bits after the sign bit, and sample is the end result.

## Synthesis Subband Filter

First the 32 new samples are gotten from the previous algorithm and put in the array S.

Then, the vector V is shifted (Note: array slices are in python format array[start:end+1]):

V[64:1023] = V[0:959]

V is initialized to zero at the start of a frame.

Figure out V[0:64]:

for i in 0..63: sum = 0 for k in 0..31: sum = sum + S[k]*cos((16+i)*(2*k+1)*pi/64) V[i] = sum

Figure out the 512 element U array:

for i in 0..7: U[i*64:i*64+32] = V[i*128:i*128+32] U[i*64+32:i*64+64] = V[i*128+96:i*128+128]

Figure out the 512 element W array:

for i in 0..512: W[i] = U[i]*D[i]

D is a table that can be gotten the iso software simulation zip (see references) in audio/tables/dewindow. The equation is something like where l=i-256 and s is either 1 or -1 and alternates sign every 64 numbers.

Figure out the 32 element SF array:

for j in 0..31: sum = 0 for i in 0..15: sum = sum + W[j+32*i] SF[j] = sum

The SF array is the final samples for output.

## References

- ISO/IEC 11172-3-1993 standard ANSI store ISO store
- ISO's sample implementation
- Layer II audio sample implementation
- Sample MPEG-1 Audio Layer I,II,and III files for testing implementations
- Description of Pseudo QMF Cosine Modulation Filter

#!/usr/bin/env python # Program to convert ISO's hex file examples to binary files. import sys if len(sys.argv) != 3: print sys.argv[0]," hex_file binary_file" sys.exit(-1) hex_file = open(sys.argv[1],"rt") binary_file = open(sys.argv[2],"wb") queue = "" char = " " while len(char) > 0: if len(queue) == 2: binary_file.write(chr(int(queue,16))) queue = "" char = hex_file.read(1) if "0" <= char <= "9" or "A" <= char <= "F": queue += char binary_file.close()