void avformat_free_context(AVFormatContext *s)
{
    int i, j;
    AVStream *st;
    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++) {
        st = s->streams[i];
        for (j = 0; j < st->nb_side_data; j++)
            av_freep(&st->side_data[j].data);
        av_freep(&st->side_data);
        st->nb_side_data = 0;
        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_freep(&st->probe_data.buf);
        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_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 = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,\n                              &s->interrupt_callback, 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 = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,\n                              &s->interrupt_callback, 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        avio_close(oc->pb);\n        goto fail;\n    }\n    if (!seg->write_header_trailer) {\n        close_null_ctx(oc->pb);\n        if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,\n                              &s->interrupt_callback, 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}', 'AVOutputFormat *av_guess_format(const char *short_name, const char *filename,\n                                const char *mime_type)\n{\n    AVOutputFormat *fmt = NULL, *fmt_found;\n    int score_max, score;\n#if CONFIG_IMAGE2_MUXER\n    if (!short_name && filename &&\n        av_filename_number_test(filename) &&\n        ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {\n        return av_guess_format("image2", NULL, NULL);\n    }\n#endif\n    fmt_found = NULL;\n    score_max = 0;\n    while ((fmt = av_oformat_next(fmt))) {\n        score = 0;\n        if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))\n            score += 100;\n        if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))\n            score += 10;\n        if (filename && fmt->extensions &&\n            av_match_ext(filename, fmt->extensions)) {\n            score += 5;\n        }\n        if (score > score_max) {\n            score_max = score;\n            fmt_found = fmt;\n        }\n    }\n    return fmt_found;\n}', 'static void seg_free_context(SegmentContext *seg)\n{\n    avio_closep(&seg->pb);\n    avformat_free_context(seg->avf);\n    seg->avf = NULL;\n}', 'void avformat_free_context(AVFormatContext *s)\n{\n    int i, j;\n    AVStream *st;\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        st = s->streams[i];\n        for (j = 0; j < st->nb_side_data; j++)\n            av_freep(&st->side_data[j].data);\n        av_freep(&st->side_data);\n        st->nb_side_data = 0;\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_freep(&st->probe_data.buf);\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_freep(&s->internal);\n    av_free(s);\n}']