static int avi_write_trailer(AVFormatContext *s)

{

    AVIContext *avi = s->priv_data;

    AVIOContext *pb = s->pb;

    int res = 0;

    int i, j, n, nb_frames;

    int64_t file_size;



    if (pb->seekable) {

        if (avi->riff_id == 1) {

            ff_end_tag(pb, avi->movi_list);

            res = avi_write_idx1(s);

            ff_end_tag(pb, avi->riff_start);

        } else {

            avi_write_ix(s);

            ff_end_tag(pb, avi->movi_list);

            ff_end_tag(pb, avi->riff_start);



            file_size = avio_tell(pb);

            avio_seek(pb, avi->odml_list - 8, SEEK_SET);

            ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */

            avio_skip(pb, 16);



            for (n = nb_frames = 0; n < s->nb_streams; n++) {

                AVCodecParameters *par = s->streams[n]->codecpar;

                AVIStream *avist       = s->streams[n]->priv_data;



                if (par->codec_type == AVMEDIA_TYPE_VIDEO) {

                    if (nb_frames < avist->packet_count)

                        nb_frames = avist->packet_count;

                } else {

                    if (par->codec_id == AV_CODEC_ID_MP2 ||

                        par->codec_id == AV_CODEC_ID_MP3)

                        nb_frames += avist->packet_count;

                }

            }

            avio_wl32(pb, nb_frames);

            avio_seek(pb, file_size, SEEK_SET);



            avi_write_counters(s, avi->riff_id);

        }

    }



    for (i = 0; i < s->nb_streams; i++) {

        AVIStream *avist = s->streams[i]->priv_data;

        for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)

            av_free(avist->indexes.cluster[j]);

        av_freep(&avist->indexes.cluster);

        avist->indexes.ents_allocated = avist->indexes.entry = 0;

    }



    return res;

}

static int avi_write_trailer(AVFormatContext *s)

{

    AVIContext *avi = s->priv_data;

    AVIOContext *pb = s->pb;

    int res = 0;

    int i, j, n, nb_frames;

    int64_t file_size;



    if (pb->seekable) {

        if (avi->riff_id == 1) {

            ff_end_tag(pb, avi->movi_list);

            res = avi_write_idx1(s);

            ff_end_tag(pb, avi->riff_start);

        } else {

            avi_write_ix(s);

            ff_end_tag(pb, avi->movi_list);

            ff_end_tag(pb, avi->riff_start);



            file_size = avio_tell(pb);

            avio_seek(pb, avi->odml_list - 8, SEEK_SET);

            ffio_wfourcc(pb, "LIST"); 

            avio_skip(pb, 16);



            for (n = nb_frames = 0; n < s->nb_streams; n++) {

                AVCodecParameters *par = s->streams[n]->codecpar;

                AVIStream *avist       = s->streams[n]->priv_data;



                if (par->codec_type == AVMEDIA_TYPE_VIDEO) {

                    if (nb_frames < avist->packet_count)

                        nb_frames = avist->packet_count;

                } else {

                    if (par->codec_id == AV_CODEC_ID_MP2 ||

                        par->codec_id == AV_CODEC_ID_MP3)

                        nb_frames += avist->packet_count;

                }

            }

            avio_wl32(pb, nb_frames);

            avio_seek(pb, file_size, SEEK_SET);



            avi_write_counters(s, avi->riff_id);

        }

    }



    for (i = 0; i < s->nb_streams; i++) {

        AVIStream *avist = s->streams[i]->priv_data;

        for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)

            av_free(avist->indexes.cluster[j]);

        av_freep(&avist->indexes.cluster);

        avist->indexes.ents_allocated = avist->indexes.entry = 0;

    }



    return res;

}

static int FUNC_0(AVFormatContext *VAR_0)

{

    AVIContext *avi = VAR_0->priv_data;

    AVIOContext *pb = VAR_0->pb;

    int VAR_1 = 0;

    int VAR_2, VAR_3, VAR_4, VAR_5;

    int64_t file_size;



    if (pb->seekable) {

        if (avi->riff_id == 1) {

            ff_end_tag(pb, avi->movi_list);

            VAR_1 = avi_write_idx1(VAR_0);

            ff_end_tag(pb, avi->riff_start);

        } else {

            avi_write_ix(VAR_0);

            ff_end_tag(pb, avi->movi_list);

            ff_end_tag(pb, avi->riff_start);



            file_size = avio_tell(pb);

            avio_seek(pb, avi->odml_list - 8, SEEK_SET);

            ffio_wfourcc(pb, "LIST"); 

            avio_skip(pb, 16);



            for (VAR_4 = VAR_5 = 0; VAR_4 < VAR_0->nb_streams; VAR_4++) {

                AVCodecParameters *par = VAR_0->streams[VAR_4]->codecpar;

                AVIStream *avist       = VAR_0->streams[VAR_4]->priv_data;



                if (par->codec_type == AVMEDIA_TYPE_VIDEO) {

                    if (VAR_5 < avist->packet_count)

                        VAR_5 = avist->packet_count;

                } else {

                    if (par->codec_id == AV_CODEC_ID_MP2 ||

                        par->codec_id == AV_CODEC_ID_MP3)

                        VAR_5 += avist->packet_count;

                }

            }

            avio_wl32(pb, VAR_5);

            avio_seek(pb, file_size, SEEK_SET);



            avi_write_counters(VAR_0, avi->riff_id);

        }

    }



    for (VAR_2 = 0; VAR_2 < VAR_0->nb_streams; VAR_2++) {

        AVIStream *avist = VAR_0->streams[VAR_2]->priv_data;

        for (VAR_3 = 0; VAR_3 < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; VAR_3++)

            av_free(avist->indexes.cluster[VAR_3]);

        av_freep(&avist->indexes.cluster);

        avist->indexes.ents_allocated = avist->indexes.entry = 0;

    }



    return VAR_1;

}

static int FUNC_0(AVFormatContext *VAR_0)
{ AVIContext *avi = VAR_0->priv_data; AVIOContext *pb = VAR_0->pb; int VAR_1 = 0; int VAR_2, VAR_3, VAR_4, VAR_5; int64_t file_size; if (pb->seekable) { if (avi->riff_id == 1) { ff_end_tag(pb, avi->movi_list); VAR_1 = avi_write_idx1(VAR_0); ff_end_tag(pb, avi->riff_start); } else { avi_write_ix(VAR_0); ff_end_tag(pb, avi->movi_list); ff_end_tag(pb, avi->riff_start); file_size = avio_tell(pb); avio_seek(pb, avi->odml_list - 8, SEEK_SET); ffio_wfourcc(pb, "LIST"); avio_skip(pb, 16); for (VAR_4 = VAR_5 = 0; VAR_4 < VAR_0->nb_streams; VAR_4++) { AVCodecParameters *par = VAR_0->streams[VAR_4]->codecpar; AVIStream *avist       = VAR_0->streams[VAR_4]->priv_data; if (par->codec_type == AVMEDIA_TYPE_VIDEO) { if (VAR_5 < avist->packet_count)
VAR_5 = avist->packet_count; } else { if (par->codec_id == AV_CODEC_ID_MP2 ||
par->codec_id == AV_CODEC_ID_MP3)
VAR_5 += avist->packet_count; } } avio_wl32(pb, VAR_5); avio_seek(pb, file_size, SEEK_SET); avi_write_counters(VAR_0, avi->riff_id); } } for (VAR_2 = 0; VAR_2 < VAR_0->nb_streams; VAR_2++) { AVIStream *avist = VAR_0->streams[VAR_2]->priv_data; for (VAR_3 = 0; VAR_3 < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; VAR_3++) av_free(avist->indexes.cluster[VAR_3]); av_freep(&avist->indexes.cluster); avist->indexes.ents_allocated = avist->indexes.entry = 0; } return VAR_1; }