void avformat_free_context(AVFormatContext *s)
{
    int i;
    if (!s)
        return;
    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++)
        free_stream(&s->streams[i]);
    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_freep(&s->internal);
    av_free(s);
}
['static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)\n{\n    SegmentContext *seg = s->priv_data;\n    AVFormatContext *oc = seg->avf;\n    AVStream *st = s->streams[pkt->stream_index];\n    int64_t end_pts = seg->recording_time * seg->number;\n    int ret, can_split = 1;\n    if (!oc)\n        return AVERROR(EINVAL);\n    if (seg->has_video) {\n        can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&\n                    pkt->flags & AV_PKT_FLAG_KEY;\n    }\n    if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,\n                                   AV_TIME_BASE_Q) >= 0) {\n        av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\\n",\n               pkt->stream_index, pkt->pts);\n        ret = segment_end(oc, seg->individual_header_trailer);\n        if (!ret)\n            ret = segment_start(s, seg->individual_header_trailer);\n        if (ret)\n            goto fail;\n        oc = seg->avf;\n        if (seg->list) {\n            if (seg->list_type == LIST_HLS) {\n                if ((ret = segment_hls_window(s, 0)) < 0)\n                    goto fail;\n            } else {\n                avio_printf(seg->pb, "%s\\n", oc->filename);\n                avio_flush(seg->pb);\n                if (seg->size && !(seg->number % seg->size)) {\n                    ff_format_io_close(s, &seg->pb);\n                    if ((ret = s->io_open(s, &seg->pb, seg->list,\n                                          AVIO_FLAG_WRITE, NULL)) < 0)\n                        goto fail;\n                }\n            }\n        }\n    }\n    ret = ff_write_chained(oc, pkt->stream_index, pkt, s);\nfail:\n    if (ret < 0)\n        seg_free_context(seg);\n    return ret;\n}', 'static void seg_free_context(SegmentContext *seg)\n{\n    ff_format_io_close(seg->avf, &seg->pb);\n    avformat_free_context(seg->avf);\n    seg->avf = NULL;\n}', 'void avformat_free_context(AVFormatContext *s)\n{\n    int i;\n    if (!s)\n        return;\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        free_stream(&s->streams[i]);\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_freep(&s->internal);\n    av_free(s);\n}']