int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)

{

    int frame_size_code;



    memset(hdr, 0, sizeof(*hdr));



    hdr->sync_word = get_bits(gbc, 16);

    if(hdr->sync_word != 0x0B77)

        return AAC_AC3_PARSE_ERROR_SYNC;



    /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */

    hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;

    if(hdr->bitstream_id > 16)

        return AAC_AC3_PARSE_ERROR_BSID;



    hdr->num_blocks = 6;



    /* set default mix levels */

    hdr->center_mix_level   = 1;  // -4.5dB

    hdr->surround_mix_level = 1;  // -6.0dB



    if(hdr->bitstream_id <= 10) {

        /* Normal AC-3 */

        hdr->crc1 = get_bits(gbc, 16);

        hdr->sr_code = get_bits(gbc, 2);

        if(hdr->sr_code == 3)

            return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;



        frame_size_code = get_bits(gbc, 6);

        if(frame_size_code > 37)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;



        skip_bits(gbc, 5); // skip bsid, already got it



        hdr->bitstream_mode = get_bits(gbc, 3);

        hdr->channel_mode = get_bits(gbc, 3);



        if(hdr->channel_mode == AC3_CHMODE_STEREO) {

            skip_bits(gbc, 2); // skip dsurmod

        } else {

            if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)

                hdr->center_mix_level = get_bits(gbc, 2);

            if(hdr->channel_mode & 4)

                hdr->surround_mix_level = get_bits(gbc, 2);

        }

        hdr->lfe_on = get_bits1(gbc);



        hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;

        hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;

        hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;

        hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;

        hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;

        hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;

        hdr->substreamid = 0;

    } else {

        /* Enhanced AC-3 */

        hdr->crc1 = 0;

        hdr->frame_type = get_bits(gbc, 2);

        if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)

            return AAC_AC3_PARSE_ERROR_FRAME_TYPE;



        hdr->substreamid = get_bits(gbc, 3);



        hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;

        if(hdr->frame_size < AC3_HEADER_SIZE)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;



        hdr->sr_code = get_bits(gbc, 2);

        if (hdr->sr_code == 3) {

            int sr_code2 = get_bits(gbc, 2);

            if(sr_code2 == 3)

                return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;

            hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;

            hdr->sr_shift = 1;

        } else {

            hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];

            hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];

            hdr->sr_shift = 0;

        }



        hdr->channel_mode = get_bits(gbc, 3);

        hdr->lfe_on = get_bits1(gbc);



        hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /

                        (hdr->num_blocks * 256.0));

        hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;

    }

    hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];

    if (hdr->lfe_on)

        hdr->channel_layout |= AV_CH_LOW_FREQUENCY;



    return 0;

}

int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)

{

    int frame_size_code;



    memset(hdr, 0, sizeof(*hdr));



    hdr->sync_word = get_bits(gbc, 16);

    if(hdr->sync_word != 0x0B77)

        return AAC_AC3_PARSE_ERROR_SYNC;



    

    hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;

    if(hdr->bitstream_id > 16)

        return AAC_AC3_PARSE_ERROR_BSID;



    hdr->num_blocks = 6;



    

    hdr->center_mix_level   = 1;  

    hdr->surround_mix_level = 1;  



    if(hdr->bitstream_id <= 10) {

        

        hdr->crc1 = get_bits(gbc, 16);

        hdr->sr_code = get_bits(gbc, 2);

        if(hdr->sr_code == 3)

            return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;



        frame_size_code = get_bits(gbc, 6);

        if(frame_size_code > 37)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;



        skip_bits(gbc, 5); 



        hdr->bitstream_mode = get_bits(gbc, 3);

        hdr->channel_mode = get_bits(gbc, 3);



        if(hdr->channel_mode == AC3_CHMODE_STEREO) {

            skip_bits(gbc, 2); 

        } else {

            if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)

                hdr->center_mix_level = get_bits(gbc, 2);

            if(hdr->channel_mode & 4)

                hdr->surround_mix_level = get_bits(gbc, 2);

        }

        hdr->lfe_on = get_bits1(gbc);



        hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;

        hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;

        hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;

        hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;

        hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;

        hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; 

        hdr->substreamid = 0;

    } else {

        

        hdr->crc1 = 0;

        hdr->frame_type = get_bits(gbc, 2);

        if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)

            return AAC_AC3_PARSE_ERROR_FRAME_TYPE;



        hdr->substreamid = get_bits(gbc, 3);



        hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;

        if(hdr->frame_size < AC3_HEADER_SIZE)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;



        hdr->sr_code = get_bits(gbc, 2);

        if (hdr->sr_code == 3) {

            int sr_code2 = get_bits(gbc, 2);

            if(sr_code2 == 3)

                return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;

            hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;

            hdr->sr_shift = 1;

        } else {

            hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];

            hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];

            hdr->sr_shift = 0;

        }



        hdr->channel_mode = get_bits(gbc, 3);

        hdr->lfe_on = get_bits1(gbc);



        hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /

                        (hdr->num_blocks * 256.0));

        hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;

    }

    hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];

    if (hdr->lfe_on)

        hdr->channel_layout |= AV_CH_LOW_FREQUENCY;



    return 0;

}

int FUNC_0(GetBitContext *VAR_0, AC3HeaderInfo *VAR_1)

{

    int VAR_2;



    memset(VAR_1, 0, sizeof(*VAR_1));



    VAR_1->sync_word = get_bits(VAR_0, 16);

    if(VAR_1->sync_word != 0x0B77)

        return AAC_AC3_PARSE_ERROR_SYNC;



    

    VAR_1->bitstream_id = show_bits_long(VAR_0, 29) & 0x1F;

    if(VAR_1->bitstream_id > 16)

        return AAC_AC3_PARSE_ERROR_BSID;



    VAR_1->num_blocks = 6;



    

    VAR_1->center_mix_level   = 1;  

    VAR_1->surround_mix_level = 1;  



    if(VAR_1->bitstream_id <= 10) {

        

        VAR_1->crc1 = get_bits(VAR_0, 16);

        VAR_1->sr_code = get_bits(VAR_0, 2);

        if(VAR_1->sr_code == 3)

            return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;



        VAR_2 = get_bits(VAR_0, 6);

        if(VAR_2 > 37)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;



        skip_bits(VAR_0, 5); 



        VAR_1->bitstream_mode = get_bits(VAR_0, 3);

        VAR_1->channel_mode = get_bits(VAR_0, 3);



        if(VAR_1->channel_mode == AC3_CHMODE_STEREO) {

            skip_bits(VAR_0, 2); 

        } else {

            if((VAR_1->channel_mode & 1) && VAR_1->channel_mode != AC3_CHMODE_MONO)

                VAR_1->center_mix_level = get_bits(VAR_0, 2);

            if(VAR_1->channel_mode & 4)

                VAR_1->surround_mix_level = get_bits(VAR_0, 2);

        }

        VAR_1->lfe_on = get_bits1(VAR_0);



        VAR_1->sr_shift = FFMAX(VAR_1->bitstream_id, 8) - 8;

        VAR_1->sample_rate = ff_ac3_sample_rate_tab[VAR_1->sr_code] >> VAR_1->sr_shift;

        VAR_1->bit_rate = (ff_ac3_bitrate_tab[VAR_2>>1] * 1000) >> VAR_1->sr_shift;

        VAR_1->channels = ff_ac3_channels_tab[VAR_1->channel_mode] + VAR_1->lfe_on;

        VAR_1->frame_size = ff_ac3_frame_size_tab[VAR_2][VAR_1->sr_code] * 2;

        VAR_1->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; 

        VAR_1->substreamid = 0;

    } else {

        

        VAR_1->crc1 = 0;

        VAR_1->frame_type = get_bits(VAR_0, 2);

        if(VAR_1->frame_type == EAC3_FRAME_TYPE_RESERVED)

            return AAC_AC3_PARSE_ERROR_FRAME_TYPE;



        VAR_1->substreamid = get_bits(VAR_0, 3);



        VAR_1->frame_size = (get_bits(VAR_0, 11) + 1) << 1;

        if(VAR_1->frame_size < AC3_HEADER_SIZE)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;



        VAR_1->sr_code = get_bits(VAR_0, 2);

        if (VAR_1->sr_code == 3) {

            int VAR_3 = get_bits(VAR_0, 2);

            if(VAR_3 == 3)

                return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;

            VAR_1->sample_rate = ff_ac3_sample_rate_tab[VAR_3] / 2;

            VAR_1->sr_shift = 1;

        } else {

            VAR_1->num_blocks = eac3_blocks[get_bits(VAR_0, 2)];

            VAR_1->sample_rate = ff_ac3_sample_rate_tab[VAR_1->sr_code];

            VAR_1->sr_shift = 0;

        }



        VAR_1->channel_mode = get_bits(VAR_0, 3);

        VAR_1->lfe_on = get_bits1(VAR_0);



        VAR_1->bit_rate = (uint32_t)(8.0 * VAR_1->frame_size * VAR_1->sample_rate /

                        (VAR_1->num_blocks * 256.0));

        VAR_1->channels = ff_ac3_channels_tab[VAR_1->channel_mode] + VAR_1->lfe_on;

    }

    VAR_1->channel_layout = ff_ac3_channel_layout_tab[VAR_1->channel_mode];

    if (VAR_1->lfe_on)

        VAR_1->channel_layout |= AV_CH_LOW_FREQUENCY;



    return 0;

}

int FUNC_0(GetBitContext *VAR_0, AC3HeaderInfo *VAR_1)
{ int VAR_2; memset(VAR_1, 0, sizeof(*VAR_1)); VAR_1->sync_word = get_bits(VAR_0, 16); if(VAR_1->sync_word != 0x0B77)
return AAC_AC3_PARSE_ERROR_SYNC; VAR_1->bitstream_id = show_bits_long(VAR_0, 29) & 0x1F; if(VAR_1->bitstream_id > 16)
return AAC_AC3_PARSE_ERROR_BSID; VAR_1->num_blocks = 6; VAR_1->center_mix_level   = 1; VAR_1->surround_mix_level = 1; if(VAR_1->bitstream_id <= 10) { VAR_1->crc1 = get_bits(VAR_0, 16); VAR_1->sr_code = get_bits(VAR_0, 2); if(VAR_1->sr_code == 3)
return AAC_AC3_PARSE_ERROR_SAMPLE_RATE; VAR_2 = get_bits(VAR_0, 6); if(VAR_2 > 37)
return AAC_AC3_PARSE_ERROR_FRAME_SIZE; skip_bits(VAR_0, 5); VAR_1->bitstream_mode = get_bits(VAR_0, 3); VAR_1->channel_mode = get_bits(VAR_0, 3); if(VAR_1->channel_mode == AC3_CHMODE_STEREO) { skip_bits(VAR_0, 2); } else { if((VAR_1->channel_mode & 1) && VAR_1->channel_mode != AC3_CHMODE_MONO)
VAR_1->center_mix_level = get_bits(VAR_0, 2); if(VAR_1->channel_mode & 4)
VAR_1->surround_mix_level = get_bits(VAR_0, 2); } VAR_1->lfe_on = get_bits1(VAR_0); VAR_1->sr_shift = FFMAX(VAR_1->bitstream_id, 8) - 8; VAR_1->sample_rate = ff_ac3_sample_rate_tab[VAR_1->sr_code] >> VAR_1->sr_shift; VAR_1->bit_rate = (ff_ac3_bitrate_tab[VAR_2>>1] * 1000) >> VAR_1->sr_shift; VAR_1->channels = ff_ac3_channels_tab[VAR_1->channel_mode] + VAR_1->lfe_on; VAR_1->frame_size = ff_ac3_frame_size_tab[VAR_2][VAR_1->sr_code] * 2; VAR_1->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; VAR_1->substreamid = 0; } else { VAR_1->crc1 = 0; VAR_1->frame_type = get_bits(VAR_0, 2); if(VAR_1->frame_type == EAC3_FRAME_TYPE_RESERVED)
return AAC_AC3_PARSE_ERROR_FRAME_TYPE; VAR_1->substreamid = get_bits(VAR_0, 3); VAR_1->frame_size = (get_bits(VAR_0, 11) + 1) << 1; if(VAR_1->frame_size < AC3_HEADER_SIZE)
return AAC_AC3_PARSE_ERROR_FRAME_SIZE; VAR_1->sr_code = get_bits(VAR_0, 2); if (VAR_1->sr_code == 3) { int VAR_3 = get_bits(VAR_0, 2); if(VAR_3 == 3)
return AAC_AC3_PARSE_ERROR_SAMPLE_RATE; VAR_1->sample_rate = ff_ac3_sample_rate_tab[VAR_3] / 2; VAR_1->sr_shift = 1; } else { VAR_1->num_blocks = eac3_blocks[get_bits(VAR_0, 2)]; VAR_1->sample_rate = ff_ac3_sample_rate_tab[VAR_1->sr_code]; VAR_1->sr_shift = 0; } VAR_1->channel_mode = get_bits(VAR_0, 3); VAR_1->lfe_on = get_bits1(VAR_0); VAR_1->bit_rate = (uint32_t)(8.0 * VAR_1->frame_size * VAR_1->sample_rate /
(VAR_1->num_blocks * 256.0)); VAR_1->channels = ff_ac3_channels_tab[VAR_1->channel_mode] + VAR_1->lfe_on; } VAR_1->channel_layout = ff_ac3_channel_layout_tab[VAR_1->channel_mode]; if (VAR_1->lfe_on)
VAR_1->channel_layout |= AV_CH_LOW_FREQUENCY; return 0; }