Esempio n. 1
0
static  int decode_resync(bitstream_t *bs) 
{
	uint_16 sync_word;
	//int i = 0;

	/* Make sure we sync'ed */
	sync_word = bitstream_get(bs,16);
	if(sync_word == 0x0b77 )
	{
		crc_init();
		return 1;
	}
	return 0;
}
Esempio n. 2
0
static int dca_subframe_footer (dca_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;
}
Esempio n. 3
0
static int dca_subsubframe (dca_state_t * state)
{
    int k, l;
    int subsubframe = state->current_subsubframe;

    const double *quant_step_table;

    /* FIXME */
    double subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];

    /*
     * Audio data
     */

    /* Select quantization step size table */
    if (state->bit_rate == 0x1f) 
        quant_step_table = lossless_quant_d;
    else
        quant_step_table = lossy_quant_d;

    for (k = 0; k < state->prim_channels; k++)
    {
        for (l = 0; l < state->vq_start_subband[k] ; l++)
        {
            int m;

            /* Select the mid-tread linear quantizer */
            int abits = state->bitalloc[k][l];

            double quant_step_size = quant_step_table[abits];
            double rscale;

            /*
             * Determine quantization index code book and its type 
             */

            /* Select quantization index code book */
            int sel = state->quant_index_huffman[k][abits]; 

            /* Determine its type */
            int q_type = 1; /* (Assume Huffman type by default) */
            if (abits >= 11 || !bitalloc_select[abits][sel])
            {
                /* Not Huffman type */
                if (abits <= 7) q_type = 3; /* Block code */
                else q_type = 2; /* No further encoding */
            }

            if (abits == 0) q_type = 0; /* No bits allocated */

            /*
             * Extract bits from the bit stream 
             */
            switch (q_type)
            {
            case 0: /* No bits allocated */
                for (m=0; m<8; m++)
                    subband_samples[k][l][m] = 0;
                break;

            case 1: /* Huffman code */
                for (m=0; m<8; m++)
                    subband_samples[k][l][m] =
                        InverseQ (state, bitalloc_select[abits][sel]);
                break;

            case 2: /* No further encoding */
                for (m=0; m<8; m++)
                {
                    /* Extract (signed) quantization index */
                    int q_index = bitstream_get (state, abits - 3);
                    if( q_index & (1 << (abits - 4)) )
                    {
                        q_index = (1 << (abits - 3)) - q_index;
                        q_index = -q_index;
                    }
                    subband_samples[k][l][m] = q_index;
                }
                break;

            case 3: /* Block code */
                {
                    int block_code1, block_code2, size, levels;
                    int block[8];

                    switch (abits)
                    {
                    case 1:
                        size = 7;
                        levels = 3;
                        break;
                    case 2:
                        size = 10;
                        levels = 5;
                        break;
                    case 3:
                        size = 12;
                        levels = 7;
                        break;
                    case 4:
                        size = 13;
                        levels = 9;
                        break;
                    case 5:
                        size = 15;
                        levels = 13;
                        break;
                    case 6:
                        size = 17;
                        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)
            {
                trace ("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
        {
            trace( "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++)
    {
        qmf_32_subbands (state, k, subband_samples[k], &state->samples[256*k]);
    }

    /* Down/Up mixing */
    if (state->prim_channels < dca_channels[state->output & DCA_CHANNEL_MASK])
    {
        dca_upmix (state->samples, state->amode, state->output);
    } else
    if (state->prim_channels > dca_channels[state->output & DCA_CHANNEL_MASK])
    {
        dca_downmix (state->samples, state->amode, state->output, state->bias,
                     state->clev, state->slev);
    } else if (state->bias)
    {
        for ( k = 0; k < 256*state->prim_channels; k++ )
            state->samples[k] += state->bias;
    }

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

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

    return 0;
}
Esempio n. 4
0
static int dca_subframe_header (dca_state_t * state)
{
    /* Primary audio coding side information */
    int j, k;

    /* Subsubframe count */
    state->subsubframes = bitstream_get (state, 2) + 1;
#ifdef DEBUG
    fprintf (stderr, "subsubframes: %i\n", state->subsubframes);
#endif

    /* Partial subsubframe sample count */
    state->partial_samples = bitstream_get (state, 3);
#ifdef DEBUG
    fprintf (stderr, "partial samples: %i\n", state->partial_samples);
#endif

    /* Get prediction mode for each subband */
    for (j = 0; j < state->prim_channels; j++)
    {
        for (k = 0; k < state->subband_activity[j]; k++)
            state->prediction_mode[j][k] = bitstream_get (state, 1);
#ifdef DEBUG
        fprintf (stderr, "prediction mode:");
        for (k = 0; k < state->subband_activity[j]; k++)
            fprintf (stderr, " %i", state->prediction_mode[j][k]);
        fprintf (stderr, "\n");
#endif
    }

    /* Get prediction codebook */
    for (j = 0; j < state->prim_channels; j++)
    {
        for (k = 0; k < state->subband_activity[j]; k++)
        {
            if (state->prediction_mode[j][k] > 0)
            {
                /* (Prediction coefficient VQ address) */
                state->prediction_vq[j][k] = bitstream_get (state, 12);
#ifdef DEBUG
                fprintf (stderr, "prediction coefs: %f, %f, %f, %f\n",
                         (double)adpcm_vb[state->prediction_vq[j][k]][0]/8192,
                         (double)adpcm_vb[state->prediction_vq[j][k]][1]/8192,
                         (double)adpcm_vb[state->prediction_vq[j][k]][2]/8192,
                         (double)adpcm_vb[state->prediction_vq[j][k]][3]/8192);
#endif
            }
        }
    }

    /* Bit allocation index */
    for (j = 0; j < state->prim_channels; j++)
    {
        for (k = 0; k < state->vq_start_subband[j]; k++)
        {
            if (state->bitalloc_huffman[j] == 6)
                state->bitalloc[j][k] = bitstream_get (state, 5);
            else if (state->bitalloc_huffman[j] == 5)
                state->bitalloc[j][k] = bitstream_get (state, 4);
            else
            {
                state->bitalloc[j][k] = InverseQ (state,
                    bitalloc_12[state->bitalloc_huffman[j]]);
            }

            if (state->bitalloc[j][k] > 26)
            {
                trace ("bitalloc index [%i][%i] too big (%i)\n",
                         j, k, state->bitalloc[j][k]);
                return -1;
            }
        }

#ifdef DEBUG
        fprintf (stderr, "bitalloc index: ");
        for (k = 0; k < state->vq_start_subband[j]; k++)
            fprintf (stderr, "%2.2i ", state->bitalloc[j][k]);
        fprintf (stderr, "\n");
#endif
    }

    /* Transition mode */
    for (j = 0; j < state->prim_channels; j++)
    {
        for (k = 0; k < state->subband_activity[j]; k++)
        {
            state->transition_mode[j][k] = 0;
            if (state->subsubframes > 1 &&
                k < state->vq_start_subband[j] &&
                state->bitalloc[j][k] > 0)
            {
                /* tmode cannot overflow since transient_huffman[j] < 4 */
                state->transition_mode[j][k] = InverseQ (state,
                    tmode[state->transient_huffman[j]]);
            }
        }
#ifdef DEBUG
        fprintf (stderr, "Transition mode:");
        for (k = 0; k < state->subband_activity[j]; k++)
            fprintf (stderr, " %i", state->transition_mode[j][k]);
        fprintf (stderr, "\n");
#endif
    }

    /* Scale factors */
    for (j = 0; j < state->prim_channels; j++)
    {
        const int *scale_table;
        int scale_sum;

        for (k = 0; k < state->subband_activity[j]; k++)
        {
            state->scale_factor[j][k][0] = 0;
            state->scale_factor[j][k][1] = 0;
        }

        if (state->scalefactor_huffman[j] == 6)
            scale_table = scale_factor_quant7;
        else
            scale_table = scale_factor_quant6;

        /* When huffman coded, only the difference is encoded */
        scale_sum = 0;

        for (k = 0; k < state->subband_activity[j]; k++)
        {
            if (k >= state->vq_start_subband[j] || state->bitalloc[j][k] > 0)
            {
                if (state->scalefactor_huffman[j] < 5)
                {
                    /* huffman encoded */
                    scale_sum += InverseQ (state,
                        scales_129[state->scalefactor_huffman[j]]);
                }
                else if (state->scalefactor_huffman[j] == 5)
                {
                    scale_sum = bitstream_get (state, 6);
                }
                else if (state->scalefactor_huffman[j] == 6)
                {
                    scale_sum = bitstream_get (state, 7);
                }

                state->scale_factor[j][k][0] = scale_table[scale_sum];
            }

            if (k < state->vq_start_subband[j] && state->transition_mode[j][k])
            {
                /* Get second scale factor */
                if (state->scalefactor_huffman[j] < 5)
                {
                    /* huffman encoded */
                    scale_sum += InverseQ (state,
                        scales_129[state->scalefactor_huffman[j]]);
                }
                else if (state->scalefactor_huffman[j] == 5)
                {
                    scale_sum = bitstream_get (state, 6);
                }
                else if (state->scalefactor_huffman[j] == 6)
                {
                    scale_sum = bitstream_get (state, 7);
                }

                state->scale_factor[j][k][1] = scale_table[scale_sum];
            }
        }

#ifdef DEBUG
        fprintf (stderr, "Scale factor:");
        for (k = 0; k < state->subband_activity[j]; k++)
        {
            if (k >= state->vq_start_subband[j] || state->bitalloc[j][k] > 0)
                fprintf (stderr, " %i", state->scale_factor[j][k][0]);
            if (k < state->vq_start_subband[j] && state->transition_mode[j][k])
                fprintf (stderr, " %i(t)", state->scale_factor[j][k][1]);
        }
        fprintf (stderr, "\n");
#endif
    }

    /* Joint subband scale factor codebook select */
    for (j = 0; j < state->prim_channels; j++)
    {
        /* Transmitted only if joint subband coding enabled */
        if (state->joint_intensity[j] > 0)
            state->joint_huff[j] = bitstream_get (state, 3);
    }

    /* Scale factors for joint subband coding */
    for (j = 0; j < state->prim_channels; j++)
    {
        int source_channel;

        /* Transmitted only if joint subband coding enabled */
        if (state->joint_intensity[j] > 0)
        {
            int scale = 0;
            source_channel = state->joint_intensity[j] - 1;

            /* When huffman coded, only the difference is encoded
             * (is this valid as well for joint scales ???) */

            for (k = state->subband_activity[j];
                 k < state->subband_activity[source_channel]; k++)
            {
                if (state->joint_huff[j] < 5)
                {
                    /* huffman encoded */
                    scale = InverseQ (state,
                        scales_129[state->joint_huff[j]]);
                }
                else if (state->joint_huff[j] == 5)
                {
                    scale = bitstream_get (state, 6);
                }
                else if (state->joint_huff[j] == 6)
                {
                    scale = bitstream_get (state, 7);
                }

                scale += 64; /* bias */
                state->joint_scale_factor[j][k] = scale;/*joint_scale_table[scale];*/
            }

            if (!state->debug_flag & 0x02)
            {
                fprintf (stderr, "Joint stereo coding not supported\n");
                state->debug_flag |= 0x02;
            }

#ifdef DEBUG
            fprintf (stderr, "Joint scale factor index:\n");
            for (k = state->subband_activity[j];
                 k < state->subband_activity[source_channel]; k++)
                fprintf (stderr, " %i", state->joint_scale_factor[j][k]);
            fprintf (stderr, "\n");
#endif
        }
    }

    /* Stereo downmix coefficients */
    if (state->prim_channels > 2 && state->downmix)
    {
        for (j = 0; j < state->prim_channels; j++)
        {
            state->downmix_coef[j][0] = bitstream_get (state, 7);
            state->downmix_coef[j][1] = bitstream_get (state, 7);
        }
    }

    /* Dynamic range coefficient */
    if (state->dynrange) state->dynrange_coef = bitstream_get (state, 8);

    /* Side information CRC check word */
    if (state->crc_present)
    {
        bitstream_get (state, 16);
    }

    /*
     * Primary audio data arrays
     */

    /* VQ encoded high frequency subbands */
    for (j = 0; j < state->prim_channels; j++)
    {
        for (k = state->vq_start_subband[j];
             k < state->subband_activity[j]; k++)
        {
            /* 1 vector -> 32 samples */
            state->high_freq_vq[j][k] = bitstream_get (state, 10);

#ifdef DEBUG
            fprintf( stderr, "VQ index: %i\n", state->high_freq_vq[j][k] );
#endif
        }
    }

    /* Low frequency effect data */
    if (state->lfe)
    {
        /* LFE samples */
        int lfe_samples = 2 * state->lfe * state->subsubframes;
        double lfe_scale;

        for (j = lfe_samples; j < lfe_samples * 2; j++)
        {
            /* Signed 8 bits int */
            state->lfe_data[j] =
                (signed int)(signed char)bitstream_get (state, 8);
        }

        /* Scale factor index */
        state->lfe_scale_factor =
            scale_factor_quant7[bitstream_get (state, 8)];

        /* Quantization step size * scale factor */
        lfe_scale = 0.035 * state->lfe_scale_factor;

        for (j = lfe_samples; j < lfe_samples * 2; j++)
            state->lfe_data[j] *= lfe_scale;

#ifdef DEBUG
        fprintf (stderr, "LFE samples:\n");
        for (j = lfe_samples; j < lfe_samples * 2; j++)
            fprintf (stderr, " %f", state->lfe_data[j]);
        fprintf (stderr, "\n");
#endif

    }

    return 0;
}
Esempio n. 5
0
int dca_frame (dca_state_t * state, uint8_t * buf, int * flags,
               level_t * level, sample_t bias)
{
    int i, j;
    static float adj_table[] = { 1.0, 1.1250, 1.2500, 1.4375 };

    dca_bitstream_init (state, buf, state->word_mode, state->bigendian_mode);

    /* Sync code */
    bitstream_get (state, 32);

    /* Frame header */
    state->frame_type = bitstream_get (state, 1);
    state->samples_deficit = bitstream_get (state, 5) + 1;
    state->crc_present = bitstream_get (state, 1);
    state->sample_blocks = bitstream_get (state, 7) + 1;
    state->frame_size = bitstream_get (state, 14) + 1;
    state->amode = bitstream_get (state, 6);
    state->sample_rate = bitstream_get (state, 4);
    state->bit_rate = bitstream_get (state, 5);

    state->downmix = bitstream_get (state, 1);
    state->dynrange = bitstream_get (state, 1);
    state->timestamp = bitstream_get (state, 1);
    state->aux_data = bitstream_get (state, 1);
    state->hdcd = bitstream_get (state, 1);
    state->ext_descr = bitstream_get (state, 3);
    state->ext_coding = bitstream_get (state, 1);
    state->aspf = bitstream_get (state, 1);
    state->lfe = bitstream_get (state, 2);
    state->predictor_history = bitstream_get (state, 1);

    /* TODO: check CRC */
    if (state->crc_present) state->header_crc = bitstream_get (state, 16);

    state->multirate_inter = bitstream_get (state, 1);
    state->version = bitstream_get (state, 4);
    state->copy_history = bitstream_get (state, 2);
    state->source_pcm_res = bitstream_get (state, 3);
    state->front_sum = bitstream_get (state, 1);
    state->surround_sum = bitstream_get (state, 1);
    state->dialog_norm = bitstream_get (state, 4);

    /* FIME: channels mixing levels */
    state->clev = state->slev = 1;
    state->output = dca_downmix_init (state->amode, *flags, level,
                                      state->clev, state->slev);
    if (state->output < 0)
        return 1;

    if (state->lfe && (*flags & DCA_LFE))
        state->output |= DCA_LFE;

    *flags = state->output;

    state->dynrng = state->level = MUL_C (*level, 2);
    state->bias = bias;
    state->dynrnge = 1;
    state->dynrngcall = NULL;

#ifdef DEBUG
    fprintf (stderr, "frame type: %i\n", state->frame_type);
    fprintf (stderr, "samples deficit: %i\n", state->samples_deficit);
    fprintf (stderr, "crc present: %i\n", state->crc_present);
    fprintf (stderr, "sample blocks: %i (%i samples)\n",
             state->sample_blocks, state->sample_blocks * 32);
    fprintf (stderr, "frame size: %i bytes\n", state->frame_size);
    fprintf (stderr, "amode: %i (%i channels)\n",
             state->amode, dca_channels[state->amode]);
    fprintf (stderr, "sample rate: %i (%i Hz)\n",
             state->sample_rate, dca_sample_rates[state->sample_rate]);
    fprintf (stderr, "bit rate: %i (%i bits/s)\n",
             state->bit_rate, dca_bit_rates[state->bit_rate]);
    fprintf (stderr, "downmix: %i\n", state->downmix);
    fprintf (stderr, "dynrange: %i\n", state->dynrange);
    fprintf (stderr, "timestamp: %i\n", state->timestamp);
    fprintf (stderr, "aux_data: %i\n", state->aux_data);
    fprintf (stderr, "hdcd: %i\n", state->hdcd);
    fprintf (stderr, "ext descr: %i\n", state->ext_descr);
    fprintf (stderr, "ext coding: %i\n", state->ext_coding);
    fprintf (stderr, "aspf: %i\n", state->aspf);
    fprintf (stderr, "lfe: %i\n", state->lfe);
    fprintf (stderr, "predictor history: %i\n", state->predictor_history);
    fprintf (stderr, "header crc: %i\n", state->header_crc);
    fprintf (stderr, "multirate inter: %i\n", state->multirate_inter);
    fprintf (stderr, "version number: %i\n", state->version);
    fprintf (stderr, "copy history: %i\n", state->copy_history);
    fprintf (stderr, "source pcm resolution: %i (%i bits/sample)\n",
             state->source_pcm_res,
             dca_bits_per_sample[state->source_pcm_res]);
    fprintf (stderr, "front sum: %i\n", state->front_sum);
    fprintf (stderr, "surround sum: %i\n", state->surround_sum);
    fprintf (stderr, "dialog norm: %i\n", state->dialog_norm);
    fprintf (stderr, "\n");
#endif

    /* Primary audio coding header */
    state->subframes = bitstream_get (state, 4) + 1;

    if (state->subframes > DCA_SUBFRAMES_MAX)
        state->subframes = DCA_SUBFRAMES_MAX;

    state->prim_channels = bitstream_get (state, 3) + 1;

    if (state->prim_channels > DCA_PRIM_CHANNELS_MAX)
        state->prim_channels = DCA_PRIM_CHANNELS_MAX;

#ifdef DEBUG
    fprintf (stderr, "subframes: %i\n", state->subframes);
    fprintf (stderr, "prim channels: %i\n", state->prim_channels);
#endif

    for (i = 0; i < state->prim_channels; i++)
    {
        state->subband_activity[i] = bitstream_get (state, 5) + 2;
#ifdef DEBUG
        fprintf (stderr, "subband activity: %i\n", state->subband_activity[i]);
#endif
        if (state->subband_activity[i] > DCA_SUBBANDS)
            state->subband_activity[i] = DCA_SUBBANDS;
    }
    for (i = 0; i < state->prim_channels; i++)
    {
        state->vq_start_subband[i] = bitstream_get (state, 5) + 1;
#ifdef DEBUG
        fprintf (stderr, "vq start subband: %i\n", state->vq_start_subband[i]);
#endif
        if (state->vq_start_subband[i] > DCA_SUBBANDS)
            state->vq_start_subband[i] = DCA_SUBBANDS;
    }
    for (i = 0; i < state->prim_channels; i++)
    {
        state->joint_intensity[i] = bitstream_get (state, 3);
#ifdef DEBUG
        fprintf (stderr, "joint intensity: %i\n", state->joint_intensity[i]);
        if (state->joint_intensity[i]) {fprintf (stderr, "JOINTINTENSITY\n");}
#endif
    }
    for (i = 0; i < state->prim_channels; i++)
    {
        state->transient_huffman[i] = bitstream_get (state, 2);
#ifdef DEBUG
        fprintf (stderr, "transient mode codebook: %i\n",
                 state->transient_huffman[i]);
#endif
    }
    for (i = 0; i < state->prim_channels; i++)
    {
        state->scalefactor_huffman[i] = bitstream_get (state, 3);
#ifdef DEBUG
        fprintf (stderr, "scale factor codebook: %i\n",
                 state->scalefactor_huffman[i]);
#endif
    }
    for (i = 0; i < state->prim_channels; i++)
    {
        state->bitalloc_huffman[i] = bitstream_get (state, 3);
        /* There might be a way not to trash the whole frame, but for
         * now we must bail out or we will buffer overflow later. */
        if (state->bitalloc_huffman[i] == 7)
            return 1;
#ifdef DEBUG
        fprintf (stderr, "bit allocation quantizer: %i\n",
                 state->bitalloc_huffman[i]);
#endif
    }

    /* Get codebooks quantization indexes */
    for (i = 0; i < state->prim_channels; i++)
    {
        state->quant_index_huffman[i][0] = 0; /* Not transmitted */
        state->quant_index_huffman[i][1] = bitstream_get (state, 1);
    }
    for (j = 2; j < 6; j++)
        for (i = 0; i < state->prim_channels; i++)
            state->quant_index_huffman[i][j] = bitstream_get (state, 2);
    for (j = 6; j < 11; j++)
        for (i = 0; i < state->prim_channels; i++)
            state->quant_index_huffman[i][j] = bitstream_get (state, 3);
    for (j = 11; j < 27; j++)
        for (i = 0; i < state->prim_channels; i++)
            state->quant_index_huffman[i][j] = 0; /* Not transmitted */

#ifdef DEBUG
    for (i = 0; i < state->prim_channels; i++)
    {
        fprintf( stderr, "quant index huff:" );
        for (j = 0; j < 11; j++)
            fprintf (stderr, " %i", state->quant_index_huffman[i][j]);
        fprintf (stderr, "\n");
    }
#endif

    /* Get scale factor adjustment */
    for (j = 0; j < 11; j++)
    {
        for (i = 0; i < state->prim_channels; i++)
            state->scalefactor_adj[i][j] = 1;
    }
    for (i = 0; i < state->prim_channels; i++)
    {
        if (state->quant_index_huffman[i][1] == 0)
        {
            /* Transmitted only if quant_index_huffman=0 (Huffman code used) */
            state->scalefactor_adj[i][1] = adj_table[bitstream_get (state, 2)];
        }
    }
    for (j = 2; j < 6; j++)
        for (i = 0; i < state->prim_channels; i++)
            if (state->quant_index_huffman[i][j] < 3)
            {
                /* Transmitted only if quant_index_huffman < 3 */
                state->scalefactor_adj[i][j] =
                    adj_table[bitstream_get (state, 2)];
            }
    for (j = 6; j < 11; j++)
        for (i = 0; i < state->prim_channels; i++)
            if (state->quant_index_huffman[i][j] < 7)
            {
                /* Transmitted only if quant_index_huffman < 7 */
                state->scalefactor_adj[i][j] =
                    adj_table[bitstream_get (state, 2)];
            }

#ifdef DEBUG
    for (i = 0; i < state->prim_channels; i++)
    {
        fprintf (stderr, "scalefac adj:");
        for (j = 0; j < 11; j++)
            fprintf (stderr, " %1.3f", state->scalefactor_adj[i][j]);
        fprintf (stderr, "\n");
    }
#endif

    if (state->crc_present)
    {
        /* Audio header CRC check */
        bitstream_get (state, 16);
    }

    state->current_subframe = 0;
    state->current_subsubframe = 0;

    return 0;
}
Esempio n. 6
0
static int syncinfo (dca_state_t * state, int * flags,
                     int * sample_rate, int * bit_rate, int * frame_length)
{
    int frame_size;

    /* Sync code */
    bitstream_get (state, 32);
    /* Frame type */
    bitstream_get (state, 1);
    /* Samples deficit */
    bitstream_get (state, 5);
    /* CRC present */
    bitstream_get (state, 1);

    *frame_length = (bitstream_get (state, 7) + 1) * 32;
    if (*frame_length < 6 * 32) return 0;
    frame_size = bitstream_get (state, 14) + 1;
    if (frame_size < 96) return 0;
    if (!state->word_mode) frame_size = frame_size * 8 / 14 * 2;

    /* Audio channel arrangement */
    *flags = bitstream_get (state, 6);
    if (*flags > 63)
        return 0;

    *sample_rate = bitstream_get (state, 4);
    if ((size_t)*sample_rate >= sizeof (dca_sample_rates) / sizeof (int))
        return 0;
    *sample_rate = dca_sample_rates[ *sample_rate ];
    if (!*sample_rate) return 0;

    *bit_rate = bitstream_get (state, 5);
    if ((size_t)*bit_rate >= sizeof (dca_bit_rates) / sizeof (int))
        return 0;
    *bit_rate = dca_bit_rates[ *bit_rate ];
    if (!*bit_rate) return 0;

    /* LFE */
    bitstream_get (state, 10);
    if (bitstream_get (state, 2)) *flags |= DCA_LFE;

    return frame_size;
}
Esempio n. 7
0
uint_32 decode_buffer_syncframe(syncinfo_t *syncinfo, uint_8 **start, uint_8 *end)
{
	uint_8 *cur = *start;
	uint_16 syncword = syncinfo->syncword;
	uint_32 ret = 0;

	// find an ac3 sync frame
resync:
	while(syncword != 0x0b77)
	{
		if(cur >= end) {
//			ret = -1;
			goto done;
		}
		syncword = (syncword << 8) + *cur++;
	}

	// need the next 3 bytes to decide how big the frame is
	while(buffer_size < 3)
	{
		if(cur >= end)
			goto done;

		buffer[buffer_size++] = *cur++;
	}
	
	parse_syncinfo(syncinfo, buffer);

	if (syncinfo->frame_size==0)		// CRITICAL CONDITION
		goto done;

	while (buffer_size < (syncinfo->frame_size<<1) - 2)
	{
		if(cur >= end)
			goto done;

		buffer[buffer_size++] = *cur++;
	}

	// check the crc over the entire frame
	if (crc_process_frame(buffer, (syncinfo->frame_size<<1) - 2))
	{
#ifdef _WIN32
		Debug(8, "Audio error\n");
		SetDlgItemText(hDlg, IDC_INFO, "A.E.!");
#else
    fprintf(stderr, "\nAudio error, skipping bad input frame\n");
#endif

		*start = cur; // Skip bad input frame

		syncword = 0xffff;
		buffer_size = 0;
		goto resync;
	}

	// if we got to this point, we found a valid ac3 frame to decode
	bitstream_init(buffer);
	// get rid of the syncinfo struct as we already parsed it
	bitstream_get(24);
	ret = 1;
	*start = cur;
done:
	// reset the syncword for next time
	syncword = 0xffff;
	buffer_size = 0;
//done:
	syncinfo->syncword = syncword;
	return ret;
}
Esempio n. 8
0
int gsm0610_unpack_wav49(gsm0610_frame_t *s, const uint8_t code[], int half)
{
    int i;
    int j;
    static bitstream_state_t bs;
    const uint8_t *c;

    c = code;
    if (half)
        bitstream_init(&bs);
    s->LARc[0] = (int16_t) bitstream_get(&bs, &c, 6);
    s->LARc[1] = (int16_t) bitstream_get(&bs, &c, 6);
    s->LARc[2] = (int16_t) bitstream_get(&bs, &c, 5);
    s->LARc[3] = (int16_t) bitstream_get(&bs, &c, 5);
    s->LARc[4] = (int16_t) bitstream_get(&bs, &c, 4);
    s->LARc[5] = (int16_t) bitstream_get(&bs, &c, 4);
    s->LARc[6] = (int16_t) bitstream_get(&bs, &c, 3);
    s->LARc[7] = (int16_t) bitstream_get(&bs, &c, 3);
    for (i = 0;  i < 4;  i++)
    {
        s->Nc[i] = (int16_t) bitstream_get(&bs, &c, 7);
        s->bc[i] = (int16_t) bitstream_get(&bs, &c, 2);
        s->Mc[i] = (int16_t) bitstream_get(&bs, &c, 2);
        s->xmaxc[i] = (int16_t) bitstream_get(&bs, &c, 6);
        for (j = 0;  j < 13;  j++)
            s->xMc[i][j] = (int16_t) bitstream_get(&bs, &c, 3);
    }
    return (half)  ?  33  :  32;
}
Esempio n. 9
0
/* Fetch an unpacked, left justified, and properly biased/dithered mantissa value */
static uint_16
mantissa_get(bitstream_t *bs, uint_16 bap )
{
	uint_16 result, index;
	uint_16 group_code, *fast_m;
static uint_16 *fast_m_1, *fast_m_2, *fast_m_4;

	//If the bap is 0-5 then we have special cases to take care of
	switch(bap)
	{
		case 0:
			//FIXME change to respect the dither flag
			if ( do_dither ){
				result = (rand()*(32767*2)) - 32767;
				result = result / 2;
			} else
				result = 0;
			break;

		case 1:
			if(m_1_pointer > 2)
			{
				group_code = bitstream_get(bs,5);

				fast_m_1 = &fastM_9[group_code][0];
/*				m_1[0] = *fast_m++;//group_code / 9;
				m_1[1] = *fast_m++;//(group_code % 9) / 3;
				m_1[2] = *fast_m++;//(group_code % 9) % 3;
*/
				m_1_pointer = 0;
			}
			index = fast_m_1[m_1_pointer++];
			result = q_1[index];
			break;
		case 2:
			if(m_2_pointer > 2)
			{
				group_code = bitstream_get(bs,7);

				fast_m_2 = &fastM_25[group_code][0];
/*				m_2[0] = *fast_m++;//group_code / 25;
				m_2[1] = *fast_m++;//(group_code % 25) / 5 ;
				m_2[2] = *fast_m++;//(group_code % 25) % 5 ;
*/				m_2_pointer = 0;
			}
			index = fast_m_2[m_2_pointer++];
			result = q_2[index];
			break;

		case 3:
			result = bitstream_get(bs,3);

			result = q_3[result];
			break;

		case 4:
			if(m_4_pointer > 1)
			{
				group_code = bitstream_get(bs,7);

				fast_m_4 = &fastM_11[group_code][0];
/*				m_4[0] = *fast_m++;//group_code / 11;
				m_4[1] = *fast_m++;//group_code % 11;
*/				m_4_pointer = 0;
			}
			index = fast_m_4[m_4_pointer++];
			result = q_4[index];
			break;

		case 5:
			result = bitstream_get(bs,4);

			result = q_5[result];
			break;

		default:
			result = bitstream_get(bs,qnttztab[bap]);
			result <<= 16 - qnttztab[bap];
	}

	return result;
}
static GstFlowReturn gst_dvbvideosink_render(GstBaseSink *sink, GstBuffer *buffer)
{
	GstDVBVideoSink *self = GST_DVBVIDEOSINK(sink);
	unsigned char *data = GST_BUFFER_DATA(buffer);
	size_t data_len = GST_BUFFER_SIZE(buffer);
	unsigned char *pes_header = GST_BUFFER_DATA(self->pesheader_buffer);
	size_t pes_header_len = 0;
	size_t payload_len = 0;
	GstBuffer *tmpbuf = NULL;

#ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
	gboolean commit_prev_frame_data = FALSE, cache_prev_frame = FALSE;
#endif

	if (self->fd < 0) return GST_FLOW_OK;

#ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
	if (self->must_pack_bitstream)
	{
		cache_prev_frame = TRUE;
		unsigned int pos = 0;
		while (pos < data_len)
		{
			if (memcmp(&data[pos], "\x00\x00\x01", 3))
			{
				pos++;
				continue;
			}
			pos += 3;
			if ((data[pos++] & 0xF0) == 0x20)
			{ // we need time_inc_res
				gboolean low_delay=FALSE;
				unsigned int ver_id = 1, shape=0, time_inc_res=0, tmp=0;
				struct bitstream bit;
				bitstream_init(&bit, data+pos, 0);
				bitstream_get(&bit, 9);
				if (bitstream_get(&bit, 1))
				{
					ver_id = bitstream_get(&bit, 4); // ver_id
					bitstream_get(&bit, 3);
				}
				if ((tmp = bitstream_get(&bit, 4)) == 15)
				{ // Custom Aspect Ration
					bitstream_get(&bit, 8); // skip AR width
					bitstream_get(&bit, 8); // skip AR height
				}
				if (bitstream_get(&bit, 1))
				{
					bitstream_get(&bit, 2);
					low_delay = bitstream_get(&bit, 1) ? TRUE : FALSE;
					if (bitstream_get(&bit, 1))
					{
						bitstream_get(&bit, 32);
						bitstream_get(&bit, 32);
						bitstream_get(&bit, 15);
					}
				}
				shape = bitstream_get(&bit, 2);
				if (ver_id != 1 && shape == 3 /* Grayscale */) bitstream_get(&bit, 4);
				bitstream_get(&bit, 1);
				time_inc_res = bitstream_get(&bit, 16);
				self->time_inc_bits = 0;
				while (time_inc_res)
				{ // count bits
					++self->time_inc_bits;
					time_inc_res >>= 1;
				}
			}
		}