⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decoder.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************* MPEG-2 NBC Audio Decoder **************************
 *                                                                           *
"This software module was originally developed by
AT&T, Dolby Laboratories, Fraunhofer Gesellschaft IIS
and edited by
Yoshiaki Oikawa (Sony Corporation),
Mitsuyuki Hatanaka (Sony Corporation)
in the course of development of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7,
14496-1,2 and 3. This software module is an implementation of a part of one or more
MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
standards free license to this software module or modifications thereof for use in
hardware or software products claiming conformance to the MPEG-2 NBC/MPEG-4
Audio  standards. Those intending to use this software module in hardware or
software products are advised that this use may infringe existing patents.
The original developer of this software module and his/her company, the subsequent
editors and their companies, and ISO/IEC have no liability for use of this software
module or modifications thereof in an implementation. Copyright is not released for
non MPEG-2 NBC/MPEG-4 Audio conforming products.The original developer
retains full right to use the code for his/her  own purpose, assign or donate the
code to a third party and to inhibit third party from using the code for non
MPEG-2 NBC/MPEG-4 Audio conforming products. This copyright notice must
be included in all copies or derivative works."
Copyright(c)1996.
 *                                                                           *
 ****************************************************************************/

#ifdef WIN32
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#endif
#include "all.h"
#include "block.h"
#include "nok_lt_prediction.h"
#include "transfo.h"
#include "bits.h"
#include "util.h"


/* D E F I N E S */
#define ftol(A,B) {tmp = *(int*) & A - 0x4B7F8000; B = (short)( (tmp==(short)tmp) ? tmp : (tmp>>31)^0x7FFF);}
#define BYTE_NUMBIT 8       /* bits in byte (char) */
#define bit2byte(a) ((a)/BYTE_NUMBIT) /* (((a)+BYTE_NUMBIT-1)/BYTE_NUMBIT) */

static unsigned long ObjectTypesTable[32] = {0, 1, 1, 1, 1, };


int parse_audio_decoder_specific_info(faacDecHandle hDecoder,unsigned long *samplerate,unsigned long *channels)
{
    unsigned long ObjectTypeIndex, SamplingFrequencyIndex, ChannelsConfiguration;

    faad_byte_align(&hDecoder->ld);
    ObjectTypeIndex = faad_getbits(&hDecoder->ld, 5);
    SamplingFrequencyIndex = faad_getbits(&hDecoder->ld, 4);
    ChannelsConfiguration = faad_getbits(&hDecoder->ld, 4);

    if(ObjectTypesTable[ObjectTypeIndex] != 1){
        return -1;
    }

    *samplerate = SampleRates[SamplingFrequencyIndex];
    if(*samplerate == 0){
        return -2;
    }

    hDecoder->numChannels = *channels = ChannelsConfiguration;

    hDecoder->mc_info.object_type = ObjectTypeIndex - 1;
    hDecoder->mc_info.sampling_rate_idx = SamplingFrequencyIndex;

    if((ChannelsConfiguration != 1) && (ChannelsConfiguration != 2)){

        return -3;

    }

    return 0;

}

int FAADAPI faacDecInit2(faacDecHandle hDecoder,
                         unsigned char* pBuffer,unsigned long SizeOfDecoderSpecificInfo,
                         unsigned long *samplerate, unsigned long *channels)
{
    int rc;

    hDecoder->adif_header_present = 0;
    hDecoder->adts_header_present = 0;

    if((hDecoder == NULL)
        || (pBuffer == NULL)
        || (SizeOfDecoderSpecificInfo < 2)
        || (samplerate == NULL)
        || (channels == NULL)){
        return -1;
    }

    /* get adif header */

    faad_initbits(&hDecoder->ld, pBuffer, 0);

    rc = parse_audio_decoder_specific_info(hDecoder,samplerate,channels);

    if(rc != 0){
        return rc;
    }

    huffbookinit(hDecoder);
    nok_init_lt_pred(hDecoder->nok_lt_status, Chans);
    init_pred(hDecoder, hDecoder->sp_status, Chans);
    MakeFFTOrder(hDecoder);
    InitBlock();  /* calculate windows */

    hDecoder->winmap[0] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
    hDecoder->winmap[1] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
    hDecoder->winmap[2] = hDecoder->win_seq_info[EIGHT_SHORT_WINDOW];
    hDecoder->winmap[3] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];

    return 0;
}


faacDecHandle FAADAPI faacDecOpen(void)
{
    int i;
    faacDecHandle hDecoder = NULL;

    hDecoder = (faacDecHandle)AllocMemory(sizeof(faacDecStruct));
#ifndef WIN32
    SetMemory(hDecoder, 0, sizeof(faacDecStruct));
#endif

    hDecoder->frameNum = 0;
    hDecoder->isMpeg4 = 1;

    /* set defaults */
    hDecoder->current_program = -1;
    hDecoder->default_config = 1;
    hDecoder->dolbyShortOffset_f2t = 1;
    hDecoder->dolbyShortOffset_t2f = 1;
    hDecoder->first_cpe = 0;
    hDecoder->warn_flag = 1;

    hDecoder->config.defObjectType = AACMAIN;
    hDecoder->config.defSampleRate = 44100;

    for(i=0; i < Chans; i++)
    {
        hDecoder->coef[i] = (Float*)AllocMemory(LN2*sizeof(Float));
        hDecoder->data[i] = (Float*)AllocMemory(LN2*sizeof(Float));
        hDecoder->state[i] = (Float*)AllocMemory(LN*sizeof(Float));
        hDecoder->factors[i] = (int*)AllocMemory(MAXBANDS*sizeof(int));
        hDecoder->cb_map[i] = (byte*)AllocMemory(MAXBANDS*sizeof(byte));
        hDecoder->group[i] = (byte*)AllocMemory(NSHORT*sizeof(byte));
        hDecoder->lpflag[i] = (int*)AllocMemory(MAXBANDS*sizeof(int));
        hDecoder->prstflag[i] = (int*)AllocMemory((LEN_PRED_RSTGRP+1)*sizeof(int));
        hDecoder->tns[i] = (TNS_frame_info*)AllocMemory(sizeof(TNS_frame_info));
        hDecoder->nok_lt_status[i]  = (NOK_LT_PRED_STATUS*)AllocMemory(sizeof(NOK_LT_PRED_STATUS));
        hDecoder->sp_status[i]  = (PRED_STATUS*)AllocMemory(LN*sizeof(PRED_STATUS));

        hDecoder->last_rstgrp_num[i] = 0;
        hDecoder->wnd_shape[i].prev_bk = 0;

#ifndef WIN32 /* WIN32 API sets memory to 0 in allocation */
        SetMemory(hDecoder->coef[i],0,LN2*sizeof(Float));
        SetMemory(hDecoder->data[i],0,LN2*sizeof(Float));
        SetMemory(hDecoder->state[i],0,LN*sizeof(Float));
        SetMemory(hDecoder->factors[i],0,MAXBANDS*sizeof(int));
        SetMemory(hDecoder->cb_map[i],0,MAXBANDS*sizeof(byte));
        SetMemory(hDecoder->group[i],0,NSHORT*sizeof(byte));
        SetMemory(hDecoder->lpflag[i],0,MAXBANDS*sizeof(int));
        SetMemory(hDecoder->prstflag[i],0,(LEN_PRED_RSTGRP+1)*sizeof(int));
        SetMemory(hDecoder->tns[i],0,sizeof(TNS_frame_info));
        SetMemory(hDecoder->nok_lt_status[i],0,sizeof(NOK_LT_PRED_STATUS));
        SetMemory(hDecoder->sp_status[i],0,LN*sizeof(PRED_STATUS));
#endif
    }

    hDecoder->mnt_table = AllocMemory(128*sizeof(float));
    hDecoder->exp_table = AllocMemory(256*sizeof(float));
    hDecoder->iq_exp_tbl = AllocMemory(MAX_IQ_TBL*sizeof(Float));
    hDecoder->exptable = AllocMemory(TEXP*sizeof(Float));
    hDecoder->unscambled64 = AllocMemory(64*sizeof(int));
    hDecoder->unscambled512 = AllocMemory(512*sizeof(int));

    SetMemory(hDecoder->lp_store, 0, MAXBANDS*sizeof(int));
    SetMemory(hDecoder->noise_state_save, 0, MAXBANDS*sizeof(long));

    for(i=0; i<Winds; i++)
    {
        hDecoder->mask[i] = (byte*)AllocMemory(MAXBANDS*sizeof(byte));
        SetMemory(hDecoder->mask[i],0,MAXBANDS*sizeof(byte));
    }

    for (i = 0; i < NUM_WIN_SEQ; i++)
        hDecoder->win_seq_info[i] = NULL;

    return hDecoder;
}

faacDecConfigurationPtr FAADAPI faacDecGetCurrentConfiguration(faacDecHandle hDecoder)
{
    faacDecConfigurationPtr config = &(hDecoder->config);

    return config;
}

int FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder,
                                    faacDecConfigurationPtr config)
{
    hDecoder->config.defObjectType = config->defObjectType;
    hDecoder->config.defSampleRate = config->defSampleRate;

    /* OK */
    return 1;
}

int FAADAPI faacDecInit(faacDecHandle hDecoder,
			unsigned char *buffer,
                        unsigned long *samplerate,
			unsigned long *channels)
{
    int i, bits = 0;
    char chk_header[4];

    faad_initbits(&hDecoder->ld, buffer, 4);
#if 0
    faad_bookmark(&hDecoder->ld, 1);
#endif
    for (i = 0; i < 4; i++) {
      chk_header[i] = buffer[i];
    }

    /* Check if an ADIF header is present */
    if (stringcmp(chk_header, "ADIF", 4) == 0)
        hDecoder->adif_header_present = 1;
    else
        hDecoder->adif_header_present = 0;

    /* Check if an ADTS header is present */
    if (hDecoder->adif_header_present == 0)
    {
        if (((int) ( chk_header[0] == (char) 0xFF)) &&
            ((int) ( (chk_header[1] & (char) 0xF0) == (char) 0xF0)))
        {
            hDecoder->adts_header_present = 1;
        } else {
            hDecoder->adts_header_present = 0;
        }
    }

    /* get adif header */
    if (hDecoder->adif_header_present) {
        hDecoder->pceChannels = 2;
#if 0
        faad_bookmark(&hDecoder->ld, 0);
        faad_bookmark(&hDecoder->ld, 1);
#else
	faad_initbits(&hDecoder->ld, buffer, 4);
#endif
        get_adif_header(hDecoder);
        /* only MPEG2 ADIF header uses byte_alignment */
        /* but the PCE already byte aligns the data */
        /*
        if (!hDecoder->isMpeg4)
            byte_align(&hDecoder->ld);
        */
        bits = faad_get_processed_bits(&hDecoder->ld);
    } else if (hDecoder->adts_header_present) {
#if 0
        faad_bookmark(&hDecoder->ld, 0);
        faad_bookmark(&hDecoder->ld, 1);
#else
	faad_initbits(&hDecoder->ld, buffer, 4);
#endif
        get_adts_header(hDecoder);
        /* only MPEG2 ADTS header uses byte_alignment */
        /* but it already is a multiple of 8 bits */
        /*
        if (!hDecoder->isMpeg4)
            byte_align(&hDecoder->ld);
        */
        bits = 0;
    } else {
        hDecoder->mc_info.object_type = hDecoder->config.defObjectType;
        hDecoder->mc_info.sampling_rate_idx = get_sr_index(hDecoder->config.defSampleRate);
    }

    *samplerate = hDecoder->config.defSampleRate;
    hDecoder->numChannels = *channels = 2;
    hDecoder->chans_inited = 0;
    if (hDecoder->adif_header_present) {
      hDecoder->chans_inited = 1;
        *samplerate = SampleRates[hDecoder->prog_config.sampling_rate_idx];
        hDecoder->numChannels = *channels = hDecoder->pceChannels;
    } else if (hDecoder->adts_header_present) {
      hDecoder->chans_inited = 1;
        *samplerate = SampleRates[hDecoder->adts_header.fixed.sampling_rate_idx];
        hDecoder->numChannels = *channels = hDecoder->adts_header.fixed.channel_configuration; /* This works up to 6 channels */
    }

    huffbookinit(hDecoder);
    nok_init_lt_pred(hDecoder->nok_lt_status, Chans);
    init_pred(hDecoder, hDecoder->sp_status, Chans);
    MakeFFTOrder(hDecoder);
    InitBlock();  /* calculate windows */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -