static int mp3_write_xing(AVFormatContext *s)

{

    MP3Context       *mp3 = s->priv_data;

    AVCodecContext *codec = s->streams[mp3->audio_stream_idx]->codec;

    int32_t          header;

    MPADecodeHeader  mpah;

    int srate_idx, i, channels;

    int bitrate_idx;

    int best_bitrate_idx = -1;

    int best_bitrate_error = INT_MAX;

    int xing_offset;

    int ver = 0;

    int bytes_needed, lsf;

    const char *vendor = (codec->flags & CODEC_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT;



    if (!s->pb->seekable || !mp3->write_xing)

        return 0;



    for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {

        const uint16_t base_freq = avpriv_mpa_freq_tab[i];



        if      (codec->sample_rate == base_freq)     ver = 0x3; // MPEG 1

        else if (codec->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2

        else if (codec->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5

        else continue;



        srate_idx = i;

        break;

    }

    if (i == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {

        av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");

        return -1;

    }



    switch (codec->channels) {

    case 1:  channels = MPA_MONO;                                          break;

    case 2:  channels = MPA_STEREO;                                        break;

    default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "

                    "not writing Xing header.\n");

             return -1;

    }



    /* dummy MPEG audio header */

    header  =  0xffU                                 << 24; // sync

    header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/

    header |= (srate_idx << 2) << 8;

    header |= channels << 6;



    for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {

        int bit_rate = 1000 * avpriv_mpa_bitrate_tab[lsf][3 - 1][bitrate_idx];

        int error    = FFABS(bit_rate - codec->bit_rate);



        if (error < best_bitrate_error) {

            best_bitrate_error = error;

            best_bitrate_idx   = bitrate_idx;

        }

    }

    av_assert0(best_bitrate_idx >= 0);



    for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {

        int32_t mask = bitrate_idx << (4 + 8);

        if (15 == bitrate_idx)

            return -1;

        header |= mask;



        avpriv_mpegaudio_decode_header(&mpah, header);

        xing_offset=xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];

        bytes_needed = 4              // header

               + xing_offset

               + 4              // xing tag

               + 4              // frames/size/toc flags

               + 4              // frames

               + 4              // size

               + XING_TOC_SIZE   // toc

               + 24

               ;



        if (bytes_needed <= mpah.frame_size)

            break;



        header &= ~mask;

    }



    avio_wb32(s->pb, header);



    ffio_fill(s->pb, 0, xing_offset);

    mp3->xing_offset = avio_tell(s->pb);

    ffio_wfourcc(s->pb, "Xing");

    avio_wb32(s->pb, 0x01 | 0x02 | 0x04);  // frames / size / TOC



    mp3->size = mpah.frame_size;

    mp3->want=1;

    mp3->seen=0;

    mp3->pos=0;



    avio_wb32(s->pb, 0);  // frames

    avio_wb32(s->pb, 0);  // size



    // toc

    for (i = 0; i < XING_TOC_SIZE; ++i)

        avio_w8(s->pb, (uint8_t)(255 * i / XING_TOC_SIZE));



    for (i = 0; i < strlen(vendor); ++i)

        avio_w8(s->pb, vendor[i]);

    for (; i < 21; ++i)

        avio_w8(s->pb, 0);

    avio_wb24(s->pb, FFMAX(codec->delay - 528 - 1, 0)<<12);



    ffio_fill(s->pb, 0, mpah.frame_size - bytes_needed);



    return 0;

}

static int mp3_write_xing(AVFormatContext *s)

{

    MP3Context       *mp3 = s->priv_data;

    AVCodecContext *codec = s->streams[mp3->audio_stream_idx]->codec;

    int32_t          header;

    MPADecodeHeader  mpah;

    int srate_idx, i, channels;

    int bitrate_idx;

    int best_bitrate_idx = -1;

    int best_bitrate_error = INT_MAX;

    int xing_offset;

    int ver = 0;

    int bytes_needed, lsf;

    const char *vendor = (codec->flags & CODEC_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT;



    if (!s->pb->seekable || !mp3->write_xing)

        return 0;



    for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {

        const uint16_t base_freq = avpriv_mpa_freq_tab[i];



        if      (codec->sample_rate == base_freq)     ver = 0x3; 

        else if (codec->sample_rate == base_freq / 2) ver = 0x2; 

        else if (codec->sample_rate == base_freq / 4) ver = 0x0; .5

        else continue;



        srate_idx = i;

        break;

    }

    if (i == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {

        av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");

        return -1;

    }



    switch (codec->channels) {

    case 1:  channels = MPA_MONO;                                          break;

    case 2:  channels = MPA_STEREO;                                        break;

    default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "

                    "not writing Xing header.\n");

             return -1;

    }



    

    header  =  0xffU                                 << 24; 

    header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; /audio-version/layer 3/no crc*/

    header |= (srate_idx << 2) << 8;

    header |= channels << 6;



    for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {

        int bit_rate = 1000 * avpriv_mpa_bitrate_tab[lsf][3 - 1][bitrate_idx];

        int error    = FFABS(bit_rate - codec->bit_rate);



        if (error < best_bitrate_error) {

            best_bitrate_error = error;

            best_bitrate_idx   = bitrate_idx;

        }

    }

    av_assert0(best_bitrate_idx >= 0);



    for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {

        int32_t mask = bitrate_idx << (4 + 8);

        if (15 == bitrate_idx)

            return -1;

        header |= mask;



        avpriv_mpegaudio_decode_header(&mpah, header);

        xing_offset=xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];

        bytes_needed = 4              

               + xing_offset

               + 4              

               + 4              

               + 4              

               + 4              

               + XING_TOC_SIZE   

               + 24

               ;



        if (bytes_needed <= mpah.frame_size)

            break;



        header &= ~mask;

    }



    avio_wb32(s->pb, header);



    ffio_fill(s->pb, 0, xing_offset);

    mp3->xing_offset = avio_tell(s->pb);

    ffio_wfourcc(s->pb, "Xing");

    avio_wb32(s->pb, 0x01 | 0x02 | 0x04);   / size / TOC



    mp3->size = mpah.frame_size;

    mp3->want=1;

    mp3->seen=0;

    mp3->pos=0;



    avio_wb32(s->pb, 0);  

    avio_wb32(s->pb, 0);  



    

    for (i = 0; i < XING_TOC_SIZE; ++i)

        avio_w8(s->pb, (uint8_t)(255 * i / XING_TOC_SIZE));



    for (i = 0; i < strlen(vendor); ++i)

        avio_w8(s->pb, vendor[i]);

    for (; i < 21; ++i)

        avio_w8(s->pb, 0);

    avio_wb24(s->pb, FFMAX(codec->delay - 528 - 1, 0)<<12);



    ffio_fill(s->pb, 0, mpah.frame_size - bytes_needed);



    return 0;

}

    int bytes_needed, lsf;         int bit_rate = 1000 * avpriv_mpa_bitrate_tab[lsf][3 - 1][bitrate_idx];
static int FUNC_0(AVFormatContext *VAR_0)

{

    MP3Context       *mp3 = VAR_0->priv_data;

    AVCodecContext *codec = VAR_0->streams[mp3->audio_stream_idx]->codec;

    int32_t          header;

    MPADecodeHeader  mpah;

    int VAR_1, VAR_2, VAR_3;

    int VAR_4;

    int VAR_5 = -1;

    int VAR_6 = INT_MAX;

    int VAR_7;

    int VAR_8 = 0;

    int VAR_9, VAR_10;

    const char *VAR_11 = (codec->flags & CODEC_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT;



    if (!VAR_0->pb->seekable || !mp3->write_xing)

        return 0;



    for (VAR_2 = 0; VAR_2 < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); VAR_2++) {

        const uint16_t base_freq = avpriv_mpa_freq_tab[VAR_2];



        if      (codec->sample_rate == base_freq)     VAR_8 = 0x3; 

        else if (codec->sample_rate == base_freq / 2) VAR_8 = 0x2; 

        else if (codec->sample_rate == base_freq / 4) VAR_8 = 0x0; .5

        else continue;



        VAR_1 = VAR_2;

        break;

    }

    if (VAR_2 == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {

        av_log(VAR_0, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");

        return -1;

    }



    switch (codec->VAR_3) {

    case 1:  VAR_3 = MPA_MONO;                                          break;

    case 2:  VAR_3 = MPA_STEREO;                                        break;

    default: av_log(VAR_0, AV_LOG_WARNING, "Unsupported number of VAR_3, "

                    "not writing Xing header.\n");

             return -1;

    }



    

    header  =  0xffU                                 << 24; 

    header |= (0x7 << 5 | VAR_8 << 3 | 0x1 << 1 | 0x1) << 16; /audio-version/layer 3/no crc*/

    header |= (VAR_1 << 2) << 8;

    header |= VAR_3 << 6;



    for (VAR_4 = 1; VAR_4 < 15; VAR_4++) {

        int VAR_12 = 1000 * avpriv_mpa_bitrate_tab[VAR_10][3 - 1][VAR_4];

        int VAR_13    = FFABS(VAR_12 - codec->VAR_12);



        if (VAR_13 < VAR_6) {

            VAR_6 = VAR_13;

            VAR_5   = VAR_4;

        }

    }

    av_assert0(VAR_5 >= 0);



    for (VAR_4 = VAR_5; ; VAR_4++) {

        int32_t mask = VAR_4 << (4 + 8);

        if (15 == VAR_4)

            return -1;

        header |= mask;



        avpriv_mpegaudio_decode_header(&mpah, header);

        VAR_7=xing_offtbl[mpah.VAR_10 == 1][mpah.nb_channels == 1];

        VAR_9 = 4              

               + VAR_7

               + 4              

               + 4              

               + 4              

               + 4              

               + XING_TOC_SIZE   

               + 24

               ;



        if (VAR_9 <= mpah.frame_size)

            break;



        header &= ~mask;

    }



    avio_wb32(VAR_0->pb, header);



    ffio_fill(VAR_0->pb, 0, VAR_7);

    mp3->VAR_7 = avio_tell(VAR_0->pb);

    ffio_wfourcc(VAR_0->pb, "Xing");

    avio_wb32(VAR_0->pb, 0x01 | 0x02 | 0x04);   / size / TOC



    mp3->size = mpah.frame_size;

    mp3->want=1;

    mp3->seen=0;

    mp3->pos=0;



    avio_wb32(VAR_0->pb, 0);  

    avio_wb32(VAR_0->pb, 0);  



    

    for (VAR_2 = 0; VAR_2 < XING_TOC_SIZE; ++VAR_2)

        avio_w8(VAR_0->pb, (uint8_t)(255 * VAR_2 / XING_TOC_SIZE));



    for (VAR_2 = 0; VAR_2 < strlen(VAR_11); ++VAR_2)

        avio_w8(VAR_0->pb, VAR_11[VAR_2]);

    for (; VAR_2 < 21; ++VAR_2)

        avio_w8(VAR_0->pb, 0);

    avio_wb24(VAR_0->pb, FFMAX(codec->delay - 528 - 1, 0)<<12);



    ffio_fill(VAR_0->pb, 0, mpah.frame_size - VAR_9);



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0)
{ MP3Context       *mp3 = VAR_0->priv_data; AVCodecContext *codec = VAR_0->streams[mp3->audio_stream_idx]->codec; int32_t          header; MPADecodeHeader  mpah; int VAR_1, VAR_2, VAR_3; int VAR_4; int VAR_5 = -1; int VAR_6 = INT_MAX; int VAR_7; int VAR_8 = 0; int VAR_9, VAR_10; const char *VAR_11 = (codec->flags & CODEC_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT; if (!VAR_0->pb->seekable || !mp3->write_xing)
return 0; for (VAR_2 = 0; VAR_2 < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); VAR_2++) { const uint16_t base_freq = avpriv_mpa_freq_tab[VAR_2]; if      (codec->sample_rate == base_freq)     VAR_8 = 0x3; else if (codec->sample_rate == base_freq / 2) VAR_8 = 0x2; else if (codec->sample_rate == base_freq / 4) VAR_8 = 0x0; .5 else continue; VAR_1 = VAR_2; break; } if (VAR_2 == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) { av_log(VAR_0, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n"); return -1; } switch (codec->VAR_3) { case 1:  VAR_3 = MPA_MONO;                                          break; case 2:  VAR_3 = MPA_STEREO;                                        break; default: av_log(VAR_0, AV_LOG_WARNING, "Unsupported number of VAR_3, "
"not writing Xing header.\n"); return -1; } header  =  0xffU                                 << 24; header |= (0x7 << 5 | VAR_8 << 3 | 0x1 << 1 | 0x1) << 16; /audio-version/layer 3/no crc*/ header |= (VAR_1 << 2) << 8; header |= VAR_3 << 6; for (VAR_4 = 1; VAR_4 < 15; VAR_4++) { int VAR_12 = 1000 * avpriv_mpa_bitrate_tab[VAR_10][3 - 1][VAR_4]; int VAR_13    = FFABS(VAR_12 - codec->VAR_12); if (VAR_13 < VAR_6) { VAR_6 = VAR_13; VAR_5   = VAR_4; } } av_assert0(VAR_5 >= 0); for (VAR_4 = VAR_5; ; VAR_4++) { int32_t mask = VAR_4 << (4 + 8); if (15 == VAR_4)
return -1; header |= mask; avpriv_mpegaudio_decode_header(&mpah, header); VAR_7=xing_offtbl[mpah.VAR_10 == 1][mpah.nb_channels == 1]; VAR_9 = 4
+ VAR_7
+ 4
+ 4
+ 4
+ 4
+ XING_TOC_SIZE
+ 24
; if (VAR_9 <= mpah.frame_size)
break; header &= ~mask; } avio_wb32(VAR_0->pb, header); ffio_fill(VAR_0->pb, 0, VAR_7); mp3->VAR_7 = avio_tell(VAR_0->pb); ffio_wfourcc(VAR_0->pb, "Xing"); avio_wb32(VAR_0->pb, 0x01 | 0x02 | 0x04);   / size / TOC mp3->size = mpah.frame_size; mp3->want=1; mp3->seen=0; mp3->pos=0; avio_wb32(VAR_0->pb, 0); avio_wb32(VAR_0->pb, 0); for (VAR_2 = 0; VAR_2 < XING_TOC_SIZE; ++VAR_2) avio_w8(VAR_0->pb, (uint8_t)(255 * VAR_2 / XING_TOC_SIZE)); for (VAR_2 = 0; VAR_2 < strlen(VAR_11); ++VAR_2) avio_w8(VAR_0->pb, VAR_11[VAR_2]); for (; VAR_2 < 21; ++VAR_2) avio_w8(VAR_0->pb, 0); avio_wb24(VAR_0->pb, FFMAX(codec->delay - 528 - 1, 0)<<12); ffio_fill(VAR_0->pb, 0, mpah.frame_size - VAR_9); return 0; }