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_header(AVFormatContext *s)\n{\n    SegmentContext *seg = s->priv_data;\n    AVFormatContext *oc = NULL;\n    int ret, i;\n    seg->number = 0;\n    seg->offset_time = 0;\n    seg->recording_time = seg->time * 1000000;\n    if (!seg->write_header_trailer)\n        seg->individual_header_trailer = 0;\n    if (seg->list && seg->list_type != LIST_HLS)\n        if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)\n            goto fail;\n    for (i = 0; i < s->nb_streams; i++)\n        seg->has_video +=\n            (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);\n    if (seg->has_video > 1)\n        av_log(s, AV_LOG_WARNING,\n               "More than a single video stream present, "\n               "expect issues decoding it.\\n");\n    seg->oformat = av_guess_format(seg->format, s->filename, NULL);\n    if (!seg->oformat) {\n        ret = AVERROR_MUXER_NOT_FOUND;\n        goto fail;\n    }\n    if (seg->oformat->flags & AVFMT_NOFILE) {\n        av_log(s, AV_LOG_ERROR, "format %s not supported.\\n",\n               seg->oformat->name);\n        ret = AVERROR(EINVAL);\n        goto fail;\n    }\n    if ((ret = segment_mux_init(s)) < 0)\n        goto fail;\n    oc = seg->avf;\n    if (av_get_frame_filename(oc->filename, sizeof(oc->filename),\n                              s->filename, seg->number++) < 0) {\n        ret = AVERROR(EINVAL);\n        goto fail;\n    }\n    if (seg->write_header_trailer) {\n        if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)\n            goto fail;\n    } else {\n        if ((ret = open_null_ctx(&oc->pb)) < 0)\n            goto fail;\n    }\n    if ((ret = avformat_write_header(oc, NULL)) < 0) {\n        ff_format_io_close(oc, &oc->pb);\n        goto fail;\n    }\n    if (!seg->write_header_trailer) {\n        close_null_ctx(oc->pb);\n        if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)\n            goto fail;\n    }\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        }\n    }\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}']