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

📄 decoder.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 2 页
字号:

    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];

#if 0
    faad_bookmark(&hDecoder->ld, 0);
#endif

    return bit2byte(bits);
}

void FAADAPI faacDecClose(faacDecHandle hDecoder)
{
    int i;

    EndBlock();
    nok_end_lt_pred(hDecoder->nok_lt_status, Chans);

    for(i=0; i < Chans; i++)
    {
        if (hDecoder->coef[i]) FreeMemory(hDecoder->coef[i]);
        if (hDecoder->data[i]) FreeMemory(hDecoder->data[i]);
        if (hDecoder->state[i]) FreeMemory(hDecoder->state[i]);
        if (hDecoder->factors[i]) FreeMemory(hDecoder->factors[i]);
        if (hDecoder->cb_map[i]) FreeMemory(hDecoder->cb_map[i]);
        if (hDecoder->group[i]) FreeMemory(hDecoder->group[i]);
        if (hDecoder->lpflag[i]) FreeMemory(hDecoder->lpflag[i]);
        if (hDecoder->prstflag[i]) FreeMemory(hDecoder->prstflag[i]);
        if (hDecoder->tns[i]) FreeMemory(hDecoder->tns[i]);
        if (hDecoder->nok_lt_status[i]) FreeMemory(hDecoder->nok_lt_status[i]);
        if (hDecoder->sp_status[i]) FreeMemory(hDecoder->sp_status[i]);
    }

    if (hDecoder->mnt_table) FreeMemory(hDecoder->mnt_table);
    if (hDecoder->exp_table) FreeMemory(hDecoder->exp_table);
    if (hDecoder->iq_exp_tbl) FreeMemory(hDecoder->iq_exp_tbl);
    if (hDecoder->exptable) FreeMemory(hDecoder->exptable);
    if (hDecoder->unscambled64) FreeMemory(hDecoder->unscambled64);
    if (hDecoder->unscambled512) FreeMemory(hDecoder->unscambled512);

    for(i=0; i<Winds; i++)
    {
        if (hDecoder->mask[i]) FreeMemory(hDecoder->mask[i]);
    }

    if (hDecoder) FreeMemory(hDecoder);
}

int FAADAPI faacDecGetProgConfig(faacDecHandle hDecoder,
                                 faacProgConfig *progConfig)
{
    return hDecoder->numChannels;
}

int FAADAPI faacDecDecode(faacDecHandle hDecoder,
                          unsigned char *buffer,
                          unsigned long *bytesconsumed,
                          short *sample_buffer,
                          unsigned long *samples)
{
    unsigned char d_bytes[MAX_DBYTES];
    int i, j, ch, wn, ele_id;
    int left, right, tmp;
    int d_tag, d_cnt;
    int channels = 0;
    Info *info;
    MC_Info *mip = &(hDecoder->mc_info);
    Ch_Info *cip;
    int retCode = FAAD_OK;

    faad_initbits(&hDecoder->ld, buffer, *bytesconsumed);

    if (hDecoder->adts_header_present)
    {
        if (get_adts_header(hDecoder))
        {
            goto error;
        }
        /* MPEG2 does byte_alignment() here
        * but ADTS header is always multiple of 8 bits in MPEG2
        * so not needed
        */
    }

    reset_mc_info(hDecoder, mip);
    while ((ele_id=faad_getbits(&hDecoder->ld, LEN_SE_ID)) != ID_END)
    {
        /* get audio syntactic element */
        switch (ele_id) {
        case ID_SCE:        /* single channel */
        case ID_CPE:        /* channel pair */
        case ID_LFE:        /* low freq effects channel */
            if (huffdecode(hDecoder, ele_id, mip, hDecoder->wnd, hDecoder->wnd_shape,
                hDecoder->cb_map, hDecoder->factors, hDecoder->group, hDecoder->hasmask,
                hDecoder->mask, hDecoder->max_sfb,
                hDecoder->lpflag, hDecoder->prstflag, hDecoder->nok_lt_status,
                hDecoder->tns, hDecoder->coef) < 0)
                goto error;
                /* CommonExit(1,"2022: Error in huffman decoder"); */
            if (ele_id == ID_CPE)
                channels += 2;
            else
                channels++;
            break;
        case ID_DSE:        /* data element */
            if (getdata(hDecoder, &d_tag, &d_cnt, d_bytes) < 0)
                goto error;
            break;
        case ID_PCE:        /* program config element */
            get_prog_config(hDecoder, &hDecoder->prog_config);
            break;
        case ID_FIL:        /* fill element */
            getfill(hDecoder, d_bytes);
            break;
        default:
            /* CommonWarning("Element not supported"); */
            goto error;
        }
    }

    if ((channels != hDecoder->numChannels) &&
	(hDecoder->chans_inited == 0)) {
        hDecoder->numChannels = channels;
        retCode = FAAD_OK_CHUPDATE;
        *bytesconsumed = 0;

#if 0
	// wmay = fall through...
        /* no errors, but channel update */
        return retCode;
#endif
    }

    if (!check_mc_info(hDecoder, mip, (hDecoder->frameNum==0 && hDecoder->default_config))) {
        goto error;
    }

    /* m/s stereo */
    for (ch=0; ch<Chans; ch++) {
        cip = &mip->ch_info[ch];
        if ((cip->present) && (cip->cpe) && (cip->ch_is_left)) {
            wn = cip->widx;
            if(hDecoder->hasmask[wn]) {
                left = ch;
                right = cip->paired_ch;
                info = hDecoder->winmap[hDecoder->wnd[wn]];
                if (hDecoder->hasmask[wn] == 1)
                    map_mask(info, hDecoder->group[wn], hDecoder->mask[wn],
                    hDecoder->cb_map[right]);
                synt(info, hDecoder->group[wn], hDecoder->mask[wn],
                    hDecoder->coef[right], hDecoder->coef[left]);
            }
        }
    }

    /* intensity stereo and prediction */
    for (ch=0; ch<Chans; ch++) {
        if (!(mip->ch_info[ch].present))
            continue;

        wn = mip->ch_info[ch].widx;
        info = hDecoder->winmap[hDecoder->wnd[wn]];

        pns(hDecoder, mip, info, wn, ch,
            hDecoder->group[wn], hDecoder->cb_map[ch], hDecoder->factors[ch],
            hDecoder->lpflag[wn], hDecoder->coef);

        intensity(mip, info, wn, ch,
            hDecoder->group[wn], hDecoder->cb_map[ch], hDecoder->factors[ch],
            hDecoder->lpflag[wn], hDecoder->coef);

        if (mip->object_type == AACLTP) {
            nok_lt_predict(hDecoder, info, hDecoder->wnd[wn], &hDecoder->wnd_shape[wn],
                hDecoder->nok_lt_status[ch]->sbk_prediction_used,
                hDecoder->nok_lt_status[ch]->sfb_prediction_used,
                hDecoder->nok_lt_status[ch], hDecoder->nok_lt_status[ch]->weight,
                hDecoder->nok_lt_status[ch]->delay, hDecoder->coef[ch],
                BLOCK_LEN_LONG, 0, BLOCK_LEN_SHORT, hDecoder->tns[ch]);
        } else if (mip->object_type == AACMAIN) {
            predict(hDecoder, info, mip->object_type, hDecoder->lpflag[wn],
                hDecoder->sp_status[ch], hDecoder->coef[ch]);
        }
    }

    for (ch = 0; ch < Chans; ch++) {
        if (!(mip->ch_info[ch].present))
            continue;

        wn = mip->ch_info[ch].widx;
        info = hDecoder->winmap[hDecoder->wnd[wn]];

        /* predictor reset */
        if (mip->object_type == AACMAIN) {
            left = ch;
            right = left;
            if ((mip->ch_info[ch].cpe) && (mip->ch_info[ch].common_window))
                /* prstflag's shared by channel pair */
                right = mip->ch_info[ch].paired_ch;
            predict_reset(hDecoder, info, hDecoder->prstflag[wn], hDecoder->sp_status,
                left, right, hDecoder->last_rstgrp_num);

            /* PNS predictor reset */
            predict_pns_reset(info, hDecoder->sp_status[ch], hDecoder->cb_map[ch]);
        }

        /* tns */
        for (i=j=0; i < hDecoder->tns[ch]->n_subblocks; i++) {

            tns_decode_subblock(hDecoder, &hDecoder->coef[ch][j],
                hDecoder->max_sfb[wn],
                info->sbk_sfb_top[i],
                info->islong,
                &(hDecoder->tns[ch]->info[i]) );

            j += info->bins_per_sbk[i];
        }

        /* inverse transform */
        freq2time_adapt(hDecoder, hDecoder->wnd[wn], &hDecoder->wnd_shape[wn],
            hDecoder->coef[ch], hDecoder->state[ch], hDecoder->data[ch]);

        if (mip->object_type == AACLTP) {
            nok_lt_update(hDecoder->nok_lt_status[ch], hDecoder->data[ch],
                hDecoder->state[ch], BLOCK_LEN_LONG);
        }
    }

    /* Copy output to a standard PCM buffer */
    for(i=0; i < 1024; i++) /* prkoat */
    {

        for (ch=0; ch < mip->nch; ch++)
        {
/* much faster FTOL */
#ifndef ENDIAN_SAFE
            float ftemp;
            /* ftemp = truncate(data[ch][i]) + 0xff8000; */
            ftemp = hDecoder->data[ch][i] + 0xff8000;
            ftol(ftemp, sample_buffer[(i*mip->nch)+ch]);
#else
            sample_buffer[(i*mip->nch)+ch] = (short)truncate(data[ch][i]);
#endif

        }
    }

    hDecoder->frameNum++;
    faad_byte_align(&hDecoder->ld);

    *bytesconsumed = bit2byte(faad_get_processed_bits(&hDecoder->ld));
    if (hDecoder->frameNum > 2)
        *samples = 1024*mip->nch;
    else
        *samples = 0;

    /* no errors */
    return retCode;

error:
#if 0
    //
    // wmay - remove this error recovery, and let calling app handle this -
    // with mpeg4ip, we can get better recovery, especially with RTP.
    //
    /* search for next ADTS header first, so decoding can be continued */
    /* ADIF and RAW AAC files will not be able to continue playing */
    if (hDecoder->adts_header_present) {
        int k, sync = 0;

        faad_byte_align(&hDecoder->ld);

        for(k = 0; sync != 4096 - 1; k++)
        {
            sync = faad_getbits(&hDecoder->ld, 12); /* 12 bit SYNCWORD */
            faad_getbits(&hDecoder->ld, 4);

            /* Bail out if no syncword found */
            if(k >= (6144 / 16))
            {
                SetMemory(sample_buffer, 0, sizeof(short)*mip->nch*1024);

                /* unrecoverable error */
                return FAAD_FATAL_ERROR;
            }
        }

        *bytesconsumed = bit2byte(hDecoder->ld.framebits - 16);
        SetMemory(sample_buffer, 0, sizeof(short)*mip->nch*1024);

        /* error, but recoverable */
        return FAAD_ERROR;
    } else {
        SetMemory(sample_buffer, 0, sizeof(short)*mip->nch*1024);

        /* unrecoverable error */
        return FAAD_FATAL_ERROR;
    }
#else
    *bytesconsumed = bit2byte(faad_get_processed_bits(&hDecoder->ld));
    return FAAD_ERROR;
#endif
}

⌨️ 快捷键说明

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