void avformat_free_context(AVFormatContext *s)
{
    int i;
    AVStream *st;
    av_opt_free(s);
    if (s->iformat && s->iformat->priv_class && s->priv_data)
        av_opt_free(s->priv_data);
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
        if (st->parser) {
            av_parser_close(st->parser);
        }
        if (st->attached_pic.data)
            av_free_packet(&st->attached_pic);
        av_dict_free(&st->metadata);
        av_free(st->index_entries);
        av_free(st->codec->extradata);
        av_free(st->codec->subtitle_header);
        av_free(st->codec);
        av_free(st->priv_data);
        av_free(st->info);
        av_free(st);
    }
    for(i=s->nb_programs-1; i>=0; i--) {
        av_dict_free(&s->programs[i]->metadata);
        av_freep(&s->programs[i]->stream_index);
        av_freep(&s->programs[i]);
    }
    av_freep(&s->programs);
    av_freep(&s->priv_data);
    while(s->nb_chapters--) {
        av_dict_free(&s->chapters[s->nb_chapters]->metadata);
        av_free(s->chapters[s->nb_chapters]);
    }
    av_freep(&s->chapters);
    av_dict_free(&s->metadata);
    av_freep(&s->streams);
    av_free(s);
}
['static int seg_write_trailer(struct AVFormatContext *s)\n{\n    SegmentContext *seg = s->priv_data;\n    AVFormatContext *oc = seg->avf;\n    int ret;\n    if (!seg->write_header_trailer) {\n        ret = segment_end(oc, 0);\n        open_null_ctx(&oc->pb);\n        av_write_trailer(oc);\n        close_null_ctx(oc->pb);\n    } else {\n        ret = segment_end(oc, 1);\n    }\n    if (seg->list)\n        avio_close(seg->pb);\n    avformat_free_context(oc);\n    return ret;\n}', 'static int segment_end(AVFormatContext *oc, int write_trailer)\n{\n    int ret = 0;\n    av_write_frame(oc, NULL);\n    if (write_trailer)\n        av_write_trailer(oc);\n    avio_close(oc->pb);\n    return ret;\n}', 'int av_write_trailer(AVFormatContext *s)\n{\n    int ret, i;\n    for (;; ) {\n        AVPacket pkt;\n        ret = interleave_packet(s, &pkt, NULL, 1);\n        if (ret < 0)\n            goto fail;\n        if (!ret)\n            break;\n        ret = s->oformat->write_packet(s, &pkt);\n        if (ret >= 0)\n            s->streams[pkt.stream_index]->nb_frames++;\n        av_free_packet(&pkt);\n        if (ret < 0)\n            goto fail;\n    }\n    if (s->oformat->write_trailer)\n        ret = s->oformat->write_trailer(s);\n    if (!(s->oformat->flags & AVFMT_NOFILE))\n        avio_flush(s->pb);\nfail:\n    for (i = 0; i < s->nb_streams; i++) {\n        av_freep(&s->streams[i]->priv_data);\n        av_freep(&s->streams[i]->index_entries);\n    }\n    if (s->oformat->priv_class)\n        av_opt_free(s->priv_data);\n    av_freep(&s->priv_data);\n    return ret;\n}', 'static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)\n{\n    if (s->oformat->interleave_packet) {\n        int ret = s->oformat->interleave_packet(s, out, in, flush);\n        if (in)\n            av_free_packet(in);\n        return ret;\n    } else\n        return ff_interleave_packet_per_dts(s, out, in, flush);\n}', 'void avformat_free_context(AVFormatContext *s)\n{\n    int i;\n    AVStream *st;\n    av_opt_free(s);\n    if (s->iformat && s->iformat->priv_class && s->priv_data)\n        av_opt_free(s->priv_data);\n    for(i=0;i<s->nb_streams;i++) {\n        st = s->streams[i];\n        if (st->parser) {\n            av_parser_close(st->parser);\n        }\n        if (st->attached_pic.data)\n            av_free_packet(&st->attached_pic);\n        av_dict_free(&st->metadata);\n        av_free(st->index_entries);\n        av_free(st->codec->extradata);\n        av_free(st->codec->subtitle_header);\n        av_free(st->codec);\n        av_free(st->priv_data);\n        av_free(st->info);\n        av_free(st);\n    }\n    for(i=s->nb_programs-1; i>=0; i--) {\n        av_dict_free(&s->programs[i]->metadata);\n        av_freep(&s->programs[i]->stream_index);\n        av_freep(&s->programs[i]);\n    }\n    av_freep(&s->programs);\n    av_freep(&s->priv_data);\n    while(s->nb_chapters--) {\n        av_dict_free(&s->chapters[s->nb_chapters]->metadata);\n        av_free(s->chapters[s->nb_chapters]);\n    }\n    av_freep(&s->chapters);\n    av_dict_free(&s->metadata);\n    av_freep(&s->streams);\n    av_free(s);\n}']