size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
{
    int len = strlen(dst);
    va_list vl;
    va_start(vl, fmt);
    len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
    va_end(vl);
    return len;
}
['static int transcode_init(void)\n{\n    int ret = 0, i, j, k;\n    AVFormatContext *oc;\n    AVCodecContext *codec, *icodec;\n    OutputStream *ost;\n    InputStream *ist;\n    char error[1024];\n    int want_sdp = 1;\n    for (i = 0; i < nb_input_files; i++) {\n        InputFile *ifile = input_files[i];\n        if (ifile->rate_emu)\n            for (j = 0; j < ifile->nb_streams; j++)\n                input_streams[j + ifile->ist_index]->start = av_gettime();\n    }\n    for (i = 0; i < nb_output_files; i++) {\n        oc = output_files[i]->ctx;\n        if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {\n            av_dump_format(oc, i, oc->filename, 1);\n            av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\\n", i);\n            return AVERROR(EINVAL);\n        }\n    }\n    for (i = 0; i < nb_filtergraphs; i++)\n        if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)\n            return ret;\n    for (i = 0; i < nb_output_streams; i++) {\n        ost = output_streams[i];\n        oc  = output_files[ost->file_index]->ctx;\n        ist = get_input_stream(ost);\n        if (ost->attachment_filename)\n            continue;\n        codec  = ost->st->codec;\n        if (ist) {\n            icodec = ist->st->codec;\n            ost->st->disposition          = ist->st->disposition;\n            codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;\n            codec->chroma_sample_location = icodec->chroma_sample_location;\n        }\n        if (ost->stream_copy) {\n            uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;\n            if (extra_size > INT_MAX) {\n                return AVERROR(EINVAL);\n            }\n            codec->codec_id   = icodec->codec_id;\n            codec->codec_type = icodec->codec_type;\n            if (!codec->codec_tag) {\n                if (!oc->oformat->codec_tag ||\n                     av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||\n                     av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)\n                    codec->codec_tag = icodec->codec_tag;\n            }\n            codec->bit_rate       = icodec->bit_rate;\n            codec->rc_max_rate    = icodec->rc_max_rate;\n            codec->rc_buffer_size = icodec->rc_buffer_size;\n            codec->field_order    = icodec->field_order;\n            codec->extradata      = av_mallocz(extra_size);\n            if (!codec->extradata) {\n                return AVERROR(ENOMEM);\n            }\n            memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);\n            codec->extradata_size = icodec->extradata_size;\n            if (!copy_tb) {\n                codec->time_base      = icodec->time_base;\n                codec->time_base.num *= icodec->ticks_per_frame;\n                av_reduce(&codec->time_base.num, &codec->time_base.den,\n                          codec->time_base.num, codec->time_base.den, INT_MAX);\n            } else\n                codec->time_base = ist->st->time_base;\n            switch (codec->codec_type) {\n            case AVMEDIA_TYPE_AUDIO:\n                if (audio_volume != 256) {\n                    av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\\n");\n                    exit_program(1);\n                }\n                codec->channel_layout     = icodec->channel_layout;\n                codec->sample_rate        = icodec->sample_rate;\n                codec->channels           = icodec->channels;\n                codec->frame_size         = icodec->frame_size;\n                codec->audio_service_type = icodec->audio_service_type;\n                codec->block_align        = icodec->block_align;\n                break;\n            case AVMEDIA_TYPE_VIDEO:\n                codec->pix_fmt            = icodec->pix_fmt;\n                codec->width              = icodec->width;\n                codec->height             = icodec->height;\n                codec->has_b_frames       = icodec->has_b_frames;\n                if (!codec->sample_aspect_ratio.num) {\n                    codec->sample_aspect_ratio   =\n                    ost->st->sample_aspect_ratio =\n                        ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :\n                        ist->st->codec->sample_aspect_ratio.num ?\n                        ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};\n                }\n                break;\n            case AVMEDIA_TYPE_SUBTITLE:\n                codec->width  = icodec->width;\n                codec->height = icodec->height;\n                break;\n            case AVMEDIA_TYPE_DATA:\n            case AVMEDIA_TYPE_ATTACHMENT:\n                break;\n            default:\n                abort();\n            }\n        } else {\n            if (!ost->enc)\n                ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);\n            if (ist)\n                ist->decoding_needed = 1;\n            ost->encoding_needed = 1;\n            switch (codec->codec_type) {\n            case AVMEDIA_TYPE_AUDIO:\n                ost->fifo = av_fifo_alloc(1024);\n                if (!ost->fifo) {\n                    return AVERROR(ENOMEM);\n                }\n                ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);\n                if (!codec->sample_rate)\n                    codec->sample_rate = icodec->sample_rate;\n                choose_sample_rate(ost->st, ost->enc);\n                codec->time_base = (AVRational){ 1, codec->sample_rate };\n                if (codec->sample_fmt == AV_SAMPLE_FMT_NONE)\n                    codec->sample_fmt = icodec->sample_fmt;\n                choose_sample_fmt(ost->st, ost->enc);\n                if (!codec->channels)\n                    codec->channels = icodec->channels;\n                codec->channel_layout = icodec->channel_layout;\n                if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)\n                    codec->channel_layout = 0;\n                ost->audio_resample       = codec-> sample_rate != icodec->sample_rate || audio_sync_method > 1;\n                icodec->request_channels  = codec-> channels;\n                ost->resample_sample_fmt  = icodec->sample_fmt;\n                ost->resample_sample_rate = icodec->sample_rate;\n                ost->resample_channels    = icodec->channels;\n                break;\n            case AVMEDIA_TYPE_VIDEO:\n                if (!ost->filter) {\n                    FilterGraph *fg;\n                    fg = init_simple_filtergraph(ist, ost);\n                    if (configure_video_filters(fg)) {\n                        av_log(NULL, AV_LOG_FATAL, "Error opening filters!\\n");\n                        exit(1);\n                    }\n                }\n                if (!ost->frame_rate.num && ist &&\n                    (video_sync_method ==  VSYNC_CFR ||\n                     (video_sync_method ==  VSYNC_AUTO &&\n                      !(oc->oformat->flags & (AVFMT_NOTIMESTAMPS | AVFMT_VARIABLE_FPS))))) {\n                    ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};\n                    if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {\n                        int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);\n                        ost->frame_rate = ost->enc->supported_framerates[idx];\n                    }\n                }\n                if (ost->frame_rate.num) {\n                    codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};\n                    video_sync_method = VSYNC_CFR;\n                } else if (ist)\n                    codec->time_base = ist->st->time_base;\n                else\n                    codec->time_base = ost->filter->filter->inputs[0]->time_base;\n                codec->width  = ost->filter->filter->inputs[0]->w;\n                codec->height = ost->filter->filter->inputs[0]->h;\n                codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =\n                    ost->frame_aspect_ratio ?\n                    av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :\n                    ost->filter->filter->inputs[0]->sample_aspect_ratio;\n                codec->pix_fmt = ost->filter->filter->inputs[0]->format;\n                if (codec->width   != icodec->width  ||\n                    codec->height  != icodec->height ||\n                    codec->pix_fmt != icodec->pix_fmt) {\n                    codec->bits_per_raw_sample = 0;\n                }\n                break;\n            case AVMEDIA_TYPE_SUBTITLE:\n                codec->time_base = (AVRational){1, 1000};\n                break;\n            default:\n                abort();\n                break;\n            }\n            if ((codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {\n                char logfilename[1024];\n                FILE *f;\n                snprintf(logfilename, sizeof(logfilename), "%s-%d.log",\n                         pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,\n                         i);\n                if (!strcmp(ost->enc->name, "libx264")) {\n                    av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);\n                } else {\n                    if (codec->flags & CODEC_FLAG_PASS1) {\n                        f = fopen(logfilename, "wb");\n                        if (!f) {\n                            av_log(NULL, AV_LOG_FATAL, "Cannot write log file \'%s\' for pass-1 encoding: %s\\n",\n                                   logfilename, strerror(errno));\n                            exit_program(1);\n                        }\n                        ost->logfile = f;\n                    } else {\n                        char  *logbuffer;\n                        size_t logbuffer_size;\n                        if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {\n                            av_log(NULL, AV_LOG_FATAL, "Error reading log file \'%s\' for pass-2 encoding\\n",\n                                   logfilename);\n                            exit_program(1);\n                        }\n                        codec->stats_in = logbuffer;\n                    }\n                }\n            }\n        }\n    }\n    for (i = 0; i < nb_output_streams; i++) {\n        ost = output_streams[i];\n        if (ost->encoding_needed) {\n            AVCodec      *codec = ost->enc;\n            AVCodecContext *dec = NULL;\n            if (!codec) {\n                snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d:%d",\n                         ost->st->codec->codec_id, ost->file_index, ost->index);\n                ret = AVERROR(EINVAL);\n                goto dump_format;\n            }\n            if ((ist = get_input_stream(ost)))\n                dec = ist->st->codec;\n            if (dec && dec->subtitle_header) {\n                ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);\n                if (!ost->st->codec->subtitle_header) {\n                    ret = AVERROR(ENOMEM);\n                    goto dump_format;\n                }\n                memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);\n                ost->st->codec->subtitle_header_size = dec->subtitle_header_size;\n            }\n            if (!av_dict_get(ost->opts, "threads", NULL, 0))\n                av_dict_set(&ost->opts, "threads", "auto", 0);\n            if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {\n                snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",\n                        ost->file_index, ost->index);\n                ret = AVERROR(EINVAL);\n                goto dump_format;\n            }\n            assert_codec_experimental(ost->st->codec, 1);\n            assert_avoptions(ost->opts);\n            if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)\n                av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."\n                                             "It takes bits/s as argument, not kbits/s\\n");\n            extra_size += ost->st->codec->extradata_size;\n            if (ost->st->codec->me_threshold)\n                input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV;\n        }\n    }\n    for (i = 0; i < nb_input_streams; i++)\n        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)\n            goto dump_format;\n    for (i = 0; i < nb_input_files; i++) {\n        InputFile *ifile = input_files[i];\n        for (j = 0; j < ifile->ctx->nb_programs; j++) {\n            AVProgram *p = ifile->ctx->programs[j];\n            int discard  = AVDISCARD_ALL;\n            for (k = 0; k < p->nb_stream_indexes; k++)\n                if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {\n                    discard = AVDISCARD_DEFAULT;\n                    break;\n                }\n            p->discard = discard;\n        }\n    }\n    for (i = 0; i < nb_output_files; i++) {\n        oc = output_files[i]->ctx;\n        oc->interrupt_callback = int_cb;\n        if (avformat_write_header(oc, &output_files[i]->opts) < 0) {\n            snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);\n            ret = AVERROR(EINVAL);\n            goto dump_format;\n        }\n        assert_avoptions(output_files[i]->opts);\n        if (strcmp(oc->oformat->name, "rtp")) {\n            want_sdp = 0;\n        }\n    }\n dump_format:\n    for (i = 0; i < nb_output_files; i++) {\n        av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);\n    }\n    av_log(NULL, AV_LOG_INFO, "Stream mapping:\\n");\n    for (i = 0; i < nb_input_streams; i++) {\n        ist = input_streams[i];\n        for (j = 0; j < ist->nb_filters; j++) {\n            AVFilterLink *link = ist->filters[j]->filter->outputs[0];\n            if (ist->filters[j]->graph->graph_desc) {\n                av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d (%s) -> %s",\n                       ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",\n                       link->dst->filter->name);\n                if (link->dst->input_count > 1)\n                    av_log(NULL, AV_LOG_INFO, ":%s", link->dstpad->name);\n                if (nb_filtergraphs > 1)\n                    av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);\n                av_log(NULL, AV_LOG_INFO, "\\n");\n            }\n        }\n    }\n    for (i = 0; i < nb_output_streams; i++) {\n        ost = output_streams[i];\n        if (ost->attachment_filename) {\n            av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\\n",\n                   ost->attachment_filename, ost->file_index, ost->index);\n            continue;\n        }\n        if (ost->filter && ost->filter->graph->graph_desc) {\n            AVFilterLink *link = ost->filter->filter->inputs[0];\n            av_log(NULL, AV_LOG_INFO, "  %s", link->src->filter->name);\n            if (link->src->output_count > 1)\n                av_log(NULL, AV_LOG_INFO, ":%s", link->srcpad->name);\n            if (nb_filtergraphs > 1)\n                av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);\n            av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\\n", ost->file_index,\n                   ost->index, ost->enc ? ost->enc->name : "?");\n            continue;\n        }\n        av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",\n               input_streams[ost->source_index]->file_index,\n               input_streams[ost->source_index]->st->index,\n               ost->file_index,\n               ost->index);\n        if (ost->sync_ist != input_streams[ost->source_index])\n            av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",\n                   ost->sync_ist->file_index,\n                   ost->sync_ist->st->index);\n        if (ost->stream_copy)\n            av_log(NULL, AV_LOG_INFO, " (copy)");\n        else\n            av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?\n                   input_streams[ost->source_index]->dec->name : "?",\n                   ost->enc ? ost->enc->name : "?");\n        av_log(NULL, AV_LOG_INFO, "\\n");\n    }\n    if (ret) {\n        av_log(NULL, AV_LOG_ERROR, "%s\\n", error);\n        return ret;\n    }\n    if (want_sdp) {\n        print_sdp();\n    }\n    return 0;\n}', 'static void print_sdp(void)\n{\n    char sdp[2048];\n    int i;\n    AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files);\n    if (!avc)\n        exit_program(1);\n    for (i = 0; i < nb_output_files; i++)\n        avc[i] = output_files[i]->ctx;\n    av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));\n    printf("SDP:\\n%s\\n", sdp);\n    fflush(stdout);\n    av_freep(&avc);\n}', 'int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)\n{\n    AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);\n    struct sdp_session_level s = { 0 };\n    int i, j, port, ttl, is_multicast;\n    char dst[32], dst_type[5];\n    memset(buf, 0, size);\n    s.user = "-";\n    s.src_addr = "127.0.0.1";\n    s.src_type = "IP4";\n    s.name = title ? title->value : "No Name";\n    port = 0;\n    ttl = 0;\n    if (n_files == 1) {\n        port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);\n        is_multicast = resolve_destination(dst, sizeof(dst), dst_type,\n                                           sizeof(dst_type));\n        if (!is_multicast)\n            ttl = 0;\n        if (dst[0]) {\n            s.dst_addr = dst;\n            s.dst_type = dst_type;\n            s.ttl = ttl;\n            if (!strcmp(dst_type, "IP6")) {\n                s.src_addr = "::1";\n                s.src_type = "IP6";\n            }\n        }\n    }\n    sdp_write_header(buf, size, &s);\n    dst[0] = 0;\n    for (i = 0; i < n_files; i++) {\n        if (n_files != 1) {\n            port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);\n            is_multicast = resolve_destination(dst, sizeof(dst), dst_type,\n                                               sizeof(dst_type));\n            if (!is_multicast)\n                ttl = 0;\n        }\n        for (j = 0; j < ac[i]->nb_streams; j++) {\n            ff_sdp_write_media(buf, size,\n                                  ac[i]->streams[j]->codec, dst[0] ? dst : NULL,\n                                  dst_type, (port > 0) ? port + j * 2 : 0, ttl,\n                                  ac[i]);\n            if (port <= 0) {\n                av_strlcatf(buf, size,\n                                   "a=control:streamid=%d\\r\\n", i + j);\n            }\n        }\n    }\n    return 0;\n}', 'static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)\n{\n    av_strlcatf(buff, size, "v=%d\\r\\n"\n                            "o=- %d %d IN %s %s\\r\\n"\n                            "s=%s\\r\\n",\n                            s->sdp_version,\n                            s->id, s->version, s->src_type, s->src_addr,\n                            s->name);\n    sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);\n    av_strlcatf(buff, size, "t=%d %d\\r\\n"\n                            "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\\r\\n",\n                            s->start_time, s->end_time);\n}', 'size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)\n{\n    int len = strlen(dst);\n    va_list vl;\n    va_start(vl, fmt);\n    len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);\n    va_end(vl);\n    return len;\n}']