Friday 15 July 2016

G711(μ-Law) to RAW decoding Algorithm

/* ===================================================================
 *  @func     AUDIO_audioDecode
 *
 *  @desc     Function does the following
 *
 *  @inputs   This function takes the following inputs
 *            src - G711 data buffer
 *            bufsize - Size of G711 data
 *
 *  @outputs  dst - RAW Data
 *
 *  @return   SUCCESS
 *  ==================================================================
 */
int AUDIO_audioDecode(char *src, short *dst,
                      int bufsize)
{
    int sizeConsumed = 0, i = 0;

    sizeConsumed = bufsize;

    for (i = 0; i < sizeConsumed; i++)
    {
        *((unsigned short *) (dst) + i) =
            ALG_G711ulawDecode(*((unsigned char *) (src) + i));
    }
   
    return TRUE;
}

/* ===================================================================
 *  @func     ALG_G711ulawDecode
 *
 *  @desc     Decode G711 Audio
 *
 *  @inputs   This function takes the following inputs
 *            input - G711 data
 *
 *  @return   RAW Data
 *  ==================================================================
 */
short ALG_G711ulawDecode(unsigned short input)
{
    unsigned short isNegative;

    short nOut;

    isNegative = ((input & 0x80) == 0);
    if (isNegative)
        nOut = 127 - input;
    else
        nOut = 255 - input;
    if (nOut < 2)
        nOut *= 2;
    else if (nOut < 16)
        nOut = ((nOut - 1) << 1) + 1 + 1;
    else if (nOut < 32)
        nOut = ((nOut - 16) << 2) + 2 + 31;
    else if (nOut < 48)
        nOut = ((nOut - 32) << 3) + 4 + 95;
    else if (nOut < 64)
        nOut = ((nOut - 48) << 4) + 8 + 223;
    else if (nOut < 80)
        nOut = ((nOut - 64) << 5) + 16 + 479;
    else if (nOut < 96)
        nOut = ((nOut - 80) << 6) + 32 + 991;
    else if (nOut < 112)
        nOut = ((nOut - 96) << 7) + 64 + 2015;
    else
        nOut = ((nOut - 112) << 8) + 128 + 4063;
    if (isNegative)
        nOut = -nOut;
    nOut <<= 2;
    return nOut;
}

No comments:

Post a Comment