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

📄 parse.c

📁 从FFMPEG转换而来的H264解码程序,VC下编译..
💻 C
📖 第 1 页 / 共 3 页
字号:
                        levels = 17;
                        break;
                    case 7:
                    default:
                        size = 19;
                        levels = 25;
                        break;
                    }

                    block_code1 = bitstream_get (state, size);
                    /* Should test return value */
                    decode_blockcode (block_code1, levels, block);
                    block_code2 = bitstream_get (state, size);
                    decode_blockcode (block_code2, levels, &block[4]);
                    for (m=0; m<8; m++)
                        subband_samples[k][l][m] = block[m];

                }
                break;

            default: /* Undefined */
                fprintf (stderr, "Unknown quantization index codebook");
                return -1;
            }

            /*
             * Account for quantization step and scale factor
             */

            /* Deal with transients */
            if (state->transition_mode[k][l] &&
                subsubframe >= state->transition_mode[k][l])
                rscale = quant_step_size * state->scale_factor[k][l][1];
            else
                rscale = quant_step_size * state->scale_factor[k][l][0];

            /* Adjustment */
            rscale *= state->scalefactor_adj[k][sel];
            for (m=0; m<8; m++) subband_samples[k][l][m] *= rscale;

            /*
             * Inverse ADPCM if in prediction mode
             */
            if (state->prediction_mode[k][l])
            {
                int n;
                for (m=0; m<8; m++)
                {
                    for (n=1; n<=4; n++)
                        if (m-n >= 0)
                            subband_samples[k][l][m] +=
                              (adpcm_vb[state->prediction_vq[k][l]][n-1] *
                                subband_samples[k][l][m-n]/8192);
                        else if (state->predictor_history)
                            subband_samples[k][l][m] +=
                              (adpcm_vb[state->prediction_vq[k][l]][n-1] *
                               state->subband_samples_hist[k][l][m-n+4]/8192);
                }
            }
        }

        /*
         * Decode VQ encoded high frequencies
         */
        for (l = state->vq_start_subband[k];
             l < state->subband_activity[k]; l++)
        {
            /* 1 vector -> 32 samples but we only need the 8 samples
             * for this subsubframe. */
            int m;

            if (!state->debug_flag & 0x01)
            {
                fprintf (stderr, "Stream with high frequencies VQ coding\n");
                state->debug_flag |= 0x01;
            }

            for (m=0; m<8; m++)
            {
                subband_samples[k][l][m] =
                    high_freq_vq[state->high_freq_vq[k][l]][subsubframe*8+m]
                        * (double)state->scale_factor[k][l][0] / 16.0;
            }
        }
    }

    /* Check for DSYNC after subsubframe */
    if (state->aspf || subsubframe == state->subsubframes - 1)
    {
        if (0xFFFF == bitstream_get (state, 16)) /* 0xFFFF */
        {
#ifdef DEBUG
            fprintf( stderr, "Got subframe DSYNC\n" );
#endif
        }
        else
        {
            fprintf( stderr, "Didn't get subframe DSYNC\n" );
        }
    }

    /* Backup predictor history for adpcm */
    for (k = 0; k < state->prim_channels; k++)
    {
        for (l = 0; l < state->vq_start_subband[k] ; l++)
        {
            int m;
            for (m = 0; m < 4; m++)
                state->subband_samples_hist[k][l][m] =
                    subband_samples[k][l][4+m];
        }
    }

    /* 32 subbands QMF */
    for (k = 0; k < state->prim_channels; k++)
    {
        /*static double pcm_to_float[8] =
            {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/

        qmf_32_subbands (state, k,
                         subband_samples[k],
                         &state->samples[256*k],
          /*WTF ???*/    32768.0*3/2/*pcm_to_float[state->source_pcm_res]*/,
                         0/*state->bias*/);
    }

    /* Down/Up mixing */
    if (state->prim_channels < dts_channels[state->output & DTS_CHANNEL_MASK])
    {
        dts_upmix (state->samples, state->amode, state->output);
    } else
    if (state->prim_channels > dts_channels[state->output & DTS_CHANNEL_MASK])
    {
        dts_downmix (state->samples, state->amode, state->output, state->bias,
                     state->clev, state->slev);
    }

    /* Generate LFE samples for this subsubframe FIXME!!! */
    if (state->output & DTS_LFE)
    {
        int lfe_samples = 2 * state->lfe * state->subsubframes;
        int i_channels = dts_channels[state->output & DTS_CHANNEL_MASK];

        lfe_interpolation_fir (state->lfe, 2 * state->lfe,
                               state->lfe_data + lfe_samples +
                               2 * state->lfe * subsubframe,
                               &state->samples[256*i_channels],
                               8388608.0, state->bias);
        /* Outputs 20bits pcm samples */
    }

    return 0;
}

static int dts_subframe_footer (dts_state_t * state)
{
    int aux_data_count = 0, i;
    int lfe_samples;

    /*
     * Unpack optional information
     */

    /* Time code stamp */
    if (state->timestamp) bitstream_get (state, 32);

    /* Auxiliary data byte count */
    if (state->aux_data) aux_data_count = bitstream_get (state, 6);

    /* Auxiliary data bytes */
    for(i = 0; i < aux_data_count; i++)
        bitstream_get (state, 8);

    /* Optional CRC check bytes */
    if (state->crc_present && (state->downmix || state->dynrange))
        bitstream_get (state, 16);

    /* Backup LFE samples history */
    lfe_samples = 2 * state->lfe * state->subsubframes;
    for (i = 0; i < lfe_samples; i++)
    {
        state->lfe_data[i] = state->lfe_data[i+lfe_samples];
    }

#ifdef DEBUG
    fprintf( stderr, "\n" );
#endif

    return 0;
}

int dts_block (dts_state_t * state)
{
    /* Sanity check */
    if (state->current_subframe >= state->subframes)
    {
        fprintf (stderr, "check failed: %i>%i",
                 state->current_subframe, state->subframes);
        return -1;
    }

    if (!state->current_subsubframe)
    {
#ifdef DEBUG
        fprintf (stderr, "DSYNC dts_subframe_header\n");
#endif
        /* Read subframe header */
        if (dts_subframe_header (state)) return -1;
    }

    /* Read subsubframe */
#ifdef DEBUG
    fprintf (stderr, "DSYNC dts_subsubframe\n");
#endif
    if (dts_subsubframe (state)) return -1;

    /* Update state */
    state->current_subsubframe++;
    if (state->current_subsubframe >= state->subsubframes)
    {
        state->current_subsubframe = 0;
        state->current_subframe++;
    }
    if (state->current_subframe >= state->subframes)
    {
#ifdef DEBUG
        fprintf(stderr, "DSYNC dts_subframe_footer\n");
#endif
        /* Read subframe footer */
        if (dts_subframe_footer (state)) return -1;
    }

    return 0;
}

/* Very compact version of the block code decoder that does not use table
 * look-up but is slightly slower */
int decode_blockcode( int code, int levels, int *values )
{
    int i;
    int offset = (levels - 1) >> 1;

    for (i = 0; i < 4; i++)
    {
        values[i] = (code % levels) - offset;
        code /= levels;
    }

    if (code == 0)
        return 1;
    else
    {
        fprintf (stderr, "ERROR: block code look-up failed\n");
        return 0;
    }
}

static void pre_calc_cosmod( dts_state_t * state )
{
    int i, j, k;

    for (j=0,k=0;k<16;k++)
        for (i=0;i<16;i++)
            state->cos_mod[j++] = cos((2*i+1)*(2*k+1)*M_PI/64);

    for (k=0;k<16;k++)
        for (i=0;i<16;i++)
            state->cos_mod[j++] = cos((i)*(2*k+1)*M_PI/32);

    for (k=0;k<16;k++)
        state->cos_mod[j++] = 0.25/(2*cos((2*k+1)*M_PI/128));

    for (k=0;k<16;k++)
        state->cos_mod[j++] = -0.25/(2.0*sin((2*k+1)*M_PI/128));
}

static void qmf_32_subbands (dts_state_t * state, int chans,
                             double samples_in[32][8], sample_t *samples_out,
                             double scale, sample_t bias)
{
    double *prCoeff;
    int i, j, k;
    double raXin[32];

    double *subband_fir_hist = state->subband_fir_hist[chans];
    double *subband_fir_hist2 = state->subband_fir_noidea[chans];

    int nChIndex = 0, NumSubband = 32, nStart = 0, nEnd = 8, nSubIndex;

    /* Select filter */
    if (!state->multirate_inter) /* Non-perfect reconstruction */
        prCoeff = fir_32bands_nonperfect;
    else /* Perfect reconstruction */
        prCoeff = fir_32bands_perfect;

    /* Reconstructed channel sample index */
    for (nSubIndex=nStart; nSubIndex<nEnd; nSubIndex++)
    {
        double A[16], B[16], SUM[16], DIFF[16];

        /* Load in one sample from each subband */
        for (i=0; i<state->subband_activity[chans]; i++)
            raXin[i] = samples_in[i][nSubIndex];

        /* Clear inactive subbands */
        for (i=state->subband_activity[chans]; i<NumSubband; i++)
            raXin[i] = 0.0;

        /* Multiply by cosine modulation coefficients and
         * create temporary arrays SUM and DIFF */
        for (j=0,k=0;k<16;k++)
        {
            A[k] = 0.0;
            for (i=0;i<16;i++)
                A[k]+=(raXin[2*i]+raXin[2*i+1])*state->cos_mod[j++];
        }

        for (k=0;k<16;k++)
        {
            B[k] = 0.0;
            for (i=0;i<16;i++)
            {
                if(i>0) B[k]+=(raXin[2*i]+raXin[2*i-1])*state->cos_mod[j++];
                else B[k]+=(raXin[2*i])*state->cos_mod[j++];
            }
            SUM[k]=A[k]+B[k];
            DIFF[k]=A[k]-B[k];
        }

        /* Store history */
        for (k=0;k<16;k++)
            subband_fir_hist[k]=state->cos_mod[j++]*SUM[k];
        for (k=0;k<16;k++)
            subband_fir_hist[32-k-1]=state->cos_mod[j++]*DIFF[k];
        /* Multiply by filter coefficients */
        for (k=31,i=0;i<32;i++,k--)
            for (j=0;j<512;j+=64)
                subband_fir_hist2[i] += prCoeff[i+j]*
                   (subband_fir_hist[i+j] - subband_fir_hist[j+k]);
        for (k=31,i=0;i<32;i++,k--)
            for (j=0;j<512;j+=64)
                subband_fir_hist2[32+i] += prCoeff[32+i+j]*
                    (-subband_fir_hist[i+j] - subband_fir_hist[j+k]);

        /* Create 32 PCM output samples */
        for (i=0;i<32;i++)
            samples_out[nChIndex++] = (sample_t)(subband_fir_hist2[i] / scale + bias);

        /* Update working arrays */
        for (i=511;i>=32;i--)
            subband_fir_hist[i] = subband_fir_hist[i-32];
        for (i=0;i<NumSubband;i++)
            subband_fir_hist2[i] = subband_fir_hist2[i+32];
        for (i=0;i<NumSubband;i++)
            subband_fir_hist2[i+32] = 0.0;
    }
}

static void lfe_interpolation_fir (int nDecimationSelect, int nNumDeciSample,
                                   double *samples_in, sample_t *samples_out,
                                   double scale, sample_t bias)
{
    /* samples_in: An array holding decimated samples.
     *   Samples in current subframe starts from samples_in[0],
     *   while samples_in[-1], samples_in[-2], ..., stores samples
     *   from last subframe as history.
     *
     * samples_out: An array holding interpolated samples
     */

    int nDeciFactor, k, J;
    double *prCoeff;

    int NumFIRCoef = 512; /* Number of FIR coefficients */
    int nInterpIndex = 0; /* Index to the interpolated samples */
    int nDeciIndex;

    /* Select decimation filter */
    if (nDecimationSelect==1)
    {
        /* 128 decimation */
        nDeciFactor = 128;
        /* Pointer to the FIR coefficients array */
        prCoeff = lfe_fir_128;
    } else {
        /* 64 decimation */
        nDeciFactor = 64;
        prCoeff = lfe_fir_64;
    }

    /* Interpolation */
    for (nDeciIndex=0; nDeciIndex<nNumDeciSample; nDeciIndex++)
    {
        /* One decimated sample generates nDeciFactor interpolated ones */
        for (k=0; k<nDeciFactor; k++)
        {
            /* Clear accumulation */
            double rTmp = 0.0;

            /* Accumulate */
            for (J=0; J<NumFIRCoef/nDeciFactor; J++)
                rTmp += samples_in[nDeciIndex-J]*prCoeff[k+J*nDeciFactor];

            /* Save interpolated samples */
            samples_out[nInterpIndex++] = (sample_t)(rTmp / scale + bias);
        }
    }
}

void dts_dynrng (dts_state_t * state,
                 level_t (* call) (level_t, void *), void * data)
{
    state->dynrange = 0;
    if (call) {
        state->dynrange = 1;
        state->dynrngcall = call;
        state->dynrngdata = data;
    }
}

void dts_free (dts_state_t * state)
{
    free (state->samples);
    free (state);
}
//===========================================================================
void __stdcall getVersion(char *ver,const char* *license)
{
 strcpy(ver, VERSION", build date "__DATE__" "__TIME__" ("COMPILER COMPILER_X64")");
 *license="(C) 2004 Gildas Bazin <gbazin@videolan.org>";
}

⌨️ 快捷键说明

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