AVIContext *avi = s->priv_data; offset_t file_size, idx_chunk; int n, nb_frames, au_byterate, au_ssize, au_scale; AVCodecContext *stream; AVIIndex *idx;
int res = 0;
static int avi_write_trailer(AVFormatContext *s){    ByteIOContext *pb = &s->pb;    AVIContext *avi = s->priv_data;    offset_t file_size, idx_chunk;    int n, nb_frames, au_byterate, au_ssize, au_scale;    AVCodecContext *stream;    AVIIndex *idx;    if (!url_is_streamed(&s->pb)) {        end_tag(pb, avi->movi_list);        idx_chunk = start_tag(pb, "idx1");        idx = avi->first;        while (idx != NULL) {            put_buffer(pb, idx->tag, 4);            put_le32(pb, idx->flags);            put_le32(pb, idx->pos);            put_le32(pb, idx->len);            idx = idx->next;        }        end_tag(pb, idx_chunk);                /* update file size */        file_size = url_ftell(pb);        url_fseek(pb, 4, SEEK_SET);        put_le32(pb, (uint32_t)(file_size - 8));        /* Fill in frame/sample counters */        nb_frames = 0;        for(n=0;n<s->nb_streams;n++) {            if (avi->frames_hdr_strm[n] != 0) {                stream = &s->streams[n]->codec;                url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);                if (stream->codec_type == CODEC_TYPE_VIDEO) {                    put_le32(pb, stream->frame_number);                     if (nb_frames < stream->frame_number)                        nb_frames = stream->frame_number;                } else {                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {                        put_le32(pb, stream->frame_number);                        nb_frames += stream->frame_number;                    } else {                        parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);                        put_le32(pb, avi->audio_strm_length[n] / au_ssize);                    }                }            }       }       if (avi->frames_hdr_all != 0) {           url_fseek(pb, avi->frames_hdr_all, SEEK_SET);           put_le32(pb, nb_frames);        }        url_fseek(pb, file_size, SEEK_SET);    }    put_flush_packet(pb);    return 0;}