ADPCM decoding

       (1) IMA ADPCM 44.1Khz 4-bit mono is used this time. The header file format of the wav file in ADPCM format has 48 file description bytes, which are 4 bytes more than ordinary PCM file description bytes. 

  1. u16 ByteExtraData; //Additional data bytes,
  2. u16 sampleperblock; / The number of samples in the data block
typedef struct
{
  uint32_t   uChunkID;       /* 0 */
  uint32_t   uFileSize;      /* 4 */
  uint32_t   uFileFormat;    /* 8 */

  uint32_t   uSubChunk1ID;   /* 12 */
  uint32_t   uSubChunk1Size; /* 16*/
  uint16_t   wAudioFormat;   /* 20 */
  uint16_t   wNbrChannels;   /* 22 */
  uint32_t   uSampleRate;    /* 24 */
  uint32_t   uByteRate;      /* 28 */
  uint16_t   wBlockAlign;    /* 32 */
  uint16_t   wBitPerSample;  /* 34 */

  uint16_t   ByteExtraData;     /* 36 */   //附加的数据字节
  uint16_t   sampleperblock;  /* 38 */     //数据块中的采样数量 

  uint32_t   uSubChunk2ID;   /* 40 */
  uint32_t   uSubChunk2Size; /* 44 */

}WAVE_DESC_T;

       (2) There is a big difference between APDCM audio data and PCM audio data. ADPCM is compressed audio data that needs to be decoded, and PCM audio data is original data that does not need to be decoded. Therefore, the audio data in PCM format is based on the original audio stream. The data is stored sequentially. The audio data of ADPCM is accessed in the form of blocks, and has a fixed size and format. Each block contains header and data.

Typedef struct{ 

         short sample0; //The first sample value (uncompressed) in the block is 2 bytes

         BYTE index; //The last index of the previous block, the index of the first block=0; 1 byte

         BYTE reserved; //1 byte not yet used

}MonoBlockHeader the head of the block

        (3) The first 2 bytes are uncompressed original 16bit data, the 3rd byte is the index of the previous block, and the 4th byte is a reserved bit. The decompression of each data block of ADPCM requires the first three The byte data is input to the decompression function as the dec_state structure parameter of the decompression function. When decompressing, the input starts from the fourth byte in the data block, and the decompressed data is stored in the first two bytes of the uncompressed data in the first two bytes of the data block.

struct adpcm_state {
    short	valprev;	/* Previous output value */
    char	index;		/* Index into stepsize table */
};
//解压函数
void adpcm_decoder(char*inbuff, char*outbuff, int len_of_in,struct adpcm_state *state );

 //数据解码
        if(flag)
        {
            dec_state.index =0;  //第一个block的index为 0
            flag=0;
        }
        else
        {
            dec_state.index =EncodeBuf[2];  //其余个block的index为块中的第三个值
        }

        dec_state.valprev =(short)EncodeBuf[0]+((short)(EncodeBuf[1]))*256;   //每一个block里面帧头有一个未压缩的数据 存储时 先低后高
        pCWaveTmp->aBuf[0].aucBuf[0]= EncodeBuf[0];    //存储第一个没有被压缩的数据
        pCWaveTmp->aBuf[0].aucBuf[1]=EncodeBuf[1];     //存储第一个没有被压缩的数据

        pCWaveTmp->wLen = DECODE_SIZE>>1;
        adpcm_decoder(&EncodeBuf[4],&(pCWaveTmp->aBuf[1]),ENCODE_SIZE-4,&dec_state);//解码一个DecodeBuf共4082字节

        (4) The data 0200 (little-endian mode) means 02=2 means that there are additional data of 2 bytes (my personal understanding), and the data F907 (little-endian mode) means 7F9=2041 means that each block of ADPCM has 2040 The number of sampling points plus one 16bit raw data, each 4bit represents a sampling point. Therefore, it can be calculated that the size of a data block in ADPCM is (2041-1)/2+4=1024, which is 1k, and +4 refers to the number of four bytes plus the MonoBlockHeader in front of the data block.

        (5) The amount of data decompressed from a 1k data block is not 4K but 4082 bytes, that is, 2040*2+2=4082, 2040 sampling points and one sampling point can restore 2 bytes of original data, Plus the original 2 bytes in the data block for a total of 4082 bytes.

Reference article:

Microsoft WAVE soundfile format

Zhennan's popular explanation of the principle of ADPCM audio codec [with Zhennan ADPCM decoding source code]

IMA-ADPCM file analysis and codec (refer to the recording example of punctual atom) (amobbs.com Amo Electronics Forum-Dongguan Amo Electronics Website)

ADPCM encoding and WAV analysis and examples_wandersky0822's blog-CSDN blog_adpcm encoding

Zhennan's in-depth analysis of WAV audio format (ADPCM encoding) is useful for VS1003 WAV recording and decoding- MCU Forum, MCU Technology Exchange Forum- 21ic Electronic Technology Development Forum

Audio encoding (PCM, ADPCM, WAVE files) - Brother Jing 0224 - 博客园

Guess you like

Origin blog.csdn.net/qq_45803449/article/details/126665238