int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
                               enum AVSampleFormat sample_fmt, int align)
{
    int line_size;
    int sample_size = av_get_bytes_per_sample(sample_fmt);
    int planar      = av_sample_fmt_is_planar(sample_fmt);
    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
        return AVERROR(EINVAL);
    if (!align) {
        if (nb_samples > INT_MAX - 31)
            return AVERROR(EINVAL);
        align = 1;
        nb_samples = FFALIGN(nb_samples, 32);
    }
    if (nb_channels > INT_MAX / align ||
        (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
        return AVERROR(EINVAL);
    line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :
                         FFALIGN(nb_samples * sample_size * nb_channels, align);
    if (linesize)
        *linesize = line_size;
    return planar ? line_size * nb_channels : line_size;
}
['static int transcode(void)\n{\n    int ret, i, need_input = 1;\n    AVFormatContext *os;\n    OutputStream *ost;\n    InputStream *ist;\n    int64_t timer_start;\n    ret = transcode_init();\n    if (ret < 0)\n        goto fail;\n    av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\\n");\n    term_init();\n    timer_start = av_gettime_relative();\n#if HAVE_PTHREADS\n    if ((ret = init_input_threads()) < 0)\n        goto fail;\n#endif\n    while (!received_sigterm) {\n        if (!need_output()) {\n            av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\\n");\n            break;\n        }\n        if (need_input) {\n            ret = process_input();\n            if (ret == AVERROR_EOF)\n                need_input = 0;\n        }\n        ret = poll_filters();\n        if (ret < 0 && ret != AVERROR_EOF) {\n            char errbuf[128];\n            av_strerror(ret, errbuf, sizeof(errbuf));\n            av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\\n", errbuf);\n            break;\n        }\n        print_report(0, timer_start);\n    }\n#if HAVE_PTHREADS\n    free_input_threads();\n#endif\n    for (i = 0; i < nb_input_streams; i++) {\n        ist = input_streams[i];\n        if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {\n            process_input_packet(ist, NULL, 0);\n        }\n    }\n    poll_filters();\n    flush_encoders();\n    term_exit();\n    for (i = 0; i < nb_output_files; i++) {\n        os = output_files[i]->ctx;\n        av_write_trailer(os);\n    }\n    print_report(1, timer_start);\n    for (i = 0; i < nb_output_streams; i++) {\n        ost = output_streams[i];\n        if (ost->encoding_needed) {\n            av_freep(&ost->enc_ctx->stats_in);\n        }\n    }\n    for (i = 0; i < nb_input_streams; i++) {\n        ist = input_streams[i];\n        if (ist->decoding_needed) {\n            avcodec_close(ist->dec_ctx);\n            if (ist->hwaccel_uninit)\n                ist->hwaccel_uninit(ist->dec_ctx);\n        }\n    }\n    ret = 0;\n fail:\n#if HAVE_PTHREADS\n    free_input_threads();\n#endif\n    if (output_streams) {\n        for (i = 0; i < nb_output_streams; i++) {\n            ost = output_streams[i];\n            if (ost) {\n                if (ost->logfile) {\n                    fclose(ost->logfile);\n                    ost->logfile = NULL;\n                }\n                av_free(ost->forced_kf_pts);\n                av_dict_free(&ost->encoder_opts);\n                av_dict_free(&ost->resample_opts);\n            }\n        }\n    }\n    return ret;\n}', 'static void process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)\n{\n    int i;\n    int repeating = 0;\n    AVPacket avpkt;\n    if (ist->next_dts == AV_NOPTS_VALUE)\n        ist->next_dts = ist->last_dts;\n    if (!pkt) {\n        av_init_packet(&avpkt);\n        avpkt.data = NULL;\n        avpkt.size = 0;\n    } else {\n        avpkt = *pkt;\n    }\n    if (pkt && pkt->dts != AV_NOPTS_VALUE)\n        ist->next_dts = ist->last_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);\n    while (ist->decoding_needed && (!pkt || avpkt.size > 0)) {\n        int ret = 0;\n        int got_output = 0;\n        if (!repeating)\n            ist->last_dts = ist->next_dts;\n        switch (ist->dec_ctx->codec_type) {\n        case AVMEDIA_TYPE_AUDIO:\n            ret = decode_audio    (ist, repeating ? NULL : &avpkt, &got_output);\n            break;\n        case AVMEDIA_TYPE_VIDEO:\n            ret = decode_video    (ist, repeating ? NULL : &avpkt, &got_output);\n            if (repeating && !got_output)\n                ;\n            else if (pkt && pkt->duration)\n                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);\n            else if (ist->st->avg_frame_rate.num)\n                ist->next_dts += av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),\n                                              AV_TIME_BASE_Q);\n            else if (ist->dec_ctx->framerate.num != 0) {\n                int ticks      = ist->st->parser ? ist->st->parser->repeat_pict + 1 :\n                                                   ist->dec_ctx->ticks_per_frame;\n                ist->next_dts += av_rescale_q(ticks, ist->dec_ctx->framerate, AV_TIME_BASE_Q);\n            }\n            break;\n        case AVMEDIA_TYPE_SUBTITLE:\n            if (repeating)\n                break;\n            ret = transcode_subtitles(ist, &avpkt, &got_output);\n            break;\n        default:\n            return;\n        }\n        if (ret < 0) {\n            av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\\n",\n                   ist->file_index, ist->st->index);\n            if (exit_on_error)\n                exit_program(1);\n            break;\n        }\n        if (!got_output)\n            break;\n        repeating = 1;\n    }\n    if (!pkt && ist->decoding_needed && !no_eof) {\n        int ret = send_filter_eof(ist);\n        if (ret < 0) {\n            av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\\n");\n            exit_program(1);\n        }\n    }\n    if (!ist->decoding_needed) {\n        ist->last_dts = ist->next_dts;\n        switch (ist->dec_ctx->codec_type) {\n        case AVMEDIA_TYPE_AUDIO:\n            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /\n                             ist->dec_ctx->sample_rate;\n            break;\n        case AVMEDIA_TYPE_VIDEO:\n            if (ist->dec_ctx->framerate.num != 0) {\n                int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;\n                ist->next_dts += ((int64_t)AV_TIME_BASE *\n                                  ist->dec_ctx->framerate.den * ticks) /\n                                  ist->dec_ctx->framerate.num;\n            }\n            break;\n        }\n    }\n    for (i = 0; pkt && i < nb_output_streams; i++) {\n        OutputStream *ost = output_streams[i];\n        if (!check_output_constraints(ist, ost) || ost->encoding_needed)\n            continue;\n        do_streamcopy(ist, ost, pkt);\n    }\n    return;\n}', 'static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)\n{\n    AVFrame *decoded_frame, *f;\n    AVCodecContext *avctx = ist->dec_ctx;\n    int i, ret, err = 0, resample_changed;\n    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))\n        return AVERROR(ENOMEM);\n    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))\n        return AVERROR(ENOMEM);\n    decoded_frame = ist->decoded_frame;\n    ret = decode(avctx, decoded_frame, got_output, pkt);\n    if (!*got_output || ret < 0)\n        return ret;\n    ist->samples_decoded += decoded_frame->nb_samples;\n    ist->frames_decoded++;\n    if (decoded_frame->pts != AV_NOPTS_VALUE)\n        ist->next_dts = decoded_frame->pts;\n    else if (pkt && pkt->pts != AV_NOPTS_VALUE) {\n        decoded_frame->pts = pkt->pts;\n    }\n    resample_changed = ist->resample_sample_fmt     != decoded_frame->format         ||\n                       ist->resample_channels       != avctx->channels               ||\n                       ist->resample_channel_layout != decoded_frame->channel_layout ||\n                       ist->resample_sample_rate    != decoded_frame->sample_rate;\n    if (resample_changed) {\n        char layout1[64], layout2[64];\n        if (!guess_input_channel_layout(ist)) {\n            av_log(NULL, AV_LOG_FATAL, "Unable to find default channel "\n                   "layout for Input Stream #%d.%d\\n", ist->file_index,\n                   ist->st->index);\n            exit_program(1);\n        }\n        decoded_frame->channel_layout = avctx->channel_layout;\n        av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels,\n                                     ist->resample_channel_layout);\n        av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels,\n                                     decoded_frame->channel_layout);\n        av_log(NULL, AV_LOG_INFO,\n               "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\\n",\n               ist->file_index, ist->st->index,\n               ist->resample_sample_rate,  av_get_sample_fmt_name(ist->resample_sample_fmt),\n               ist->resample_channels, layout1,\n               decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format),\n               avctx->channels, layout2);\n        ist->resample_sample_fmt     = decoded_frame->format;\n        ist->resample_sample_rate    = decoded_frame->sample_rate;\n        ist->resample_channel_layout = decoded_frame->channel_layout;\n        ist->resample_channels       = avctx->channels;\n        for (i = 0; i < nb_filtergraphs; i++)\n            if (ist_in_filtergraph(filtergraphs[i], ist) &&\n                configure_filtergraph(filtergraphs[i]) < 0) {\n                av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\\n");\n                exit_program(1);\n            }\n    }\n    if (decoded_frame->pts != AV_NOPTS_VALUE)\n        decoded_frame->pts = av_rescale_q(decoded_frame->pts,\n                                          ist->st->time_base,\n                                          (AVRational){1, avctx->sample_rate});\n    ist->nb_samples = decoded_frame->nb_samples;\n    for (i = 0; i < ist->nb_filters; i++) {\n        if (i < ist->nb_filters - 1) {\n            f = ist->filter_frame;\n            err = av_frame_ref(f, decoded_frame);\n            if (err < 0)\n                break;\n        } else\n            f = decoded_frame;\n        err = av_buffersrc_add_frame(ist->filters[i]->filter, f);\n        if (err < 0)\n            break;\n    }\n    av_frame_unref(ist->filter_frame);\n    av_frame_unref(decoded_frame);\n    return err < 0 ? err : ret;\n}', 'int av_frame_ref(AVFrame *dst, const AVFrame *src)\n{\n    int i, ret = 0;\n    dst->format         = src->format;\n    dst->width          = src->width;\n    dst->height         = src->height;\n    dst->channel_layout = src->channel_layout;\n    dst->nb_samples     = src->nb_samples;\n    ret = av_frame_copy_props(dst, src);\n    if (ret < 0)\n        return ret;\n    if (!src->buf[0]) {\n        ret = av_frame_get_buffer(dst, 32);\n        if (ret < 0)\n            return ret;\n        ret = av_frame_copy(dst, src);\n        if (ret < 0)\n            av_frame_unref(dst);\n        return ret;\n    }\n    for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {\n        dst->buf[i] = av_buffer_ref(src->buf[i]);\n        if (!dst->buf[i]) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n    }\n    if (src->extended_buf) {\n        dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *\n                                       src->nb_extended_buf);\n        if (!dst->extended_buf) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        dst->nb_extended_buf = src->nb_extended_buf;\n        for (i = 0; i < src->nb_extended_buf; i++) {\n            dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);\n            if (!dst->extended_buf[i]) {\n                ret = AVERROR(ENOMEM);\n                goto fail;\n            }\n        }\n    }\n    if (src->hw_frames_ctx) {\n        dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);\n        if (!dst->hw_frames_ctx) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n    }\n    if (src->extended_data != src->data) {\n        int ch = av_get_channel_layout_nb_channels(src->channel_layout);\n        if (!ch) {\n            ret = AVERROR(EINVAL);\n            goto fail;\n        }\n        dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);\n        if (!dst->extended_data) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);\n    } else\n        dst->extended_data = dst->data;\n    memcpy(dst->data,     src->data,     sizeof(src->data));\n    memcpy(dst->linesize, src->linesize, sizeof(src->linesize));\n    return 0;\nfail:\n    av_frame_unref(dst);\n    return ret;\n}', 'int av_frame_get_buffer(AVFrame *frame, int align)\n{\n    if (frame->format < 0)\n        return AVERROR(EINVAL);\n    if (frame->width > 0 && frame->height > 0)\n        return get_video_buffer(frame, align);\n    else if (frame->nb_samples > 0 && frame->channel_layout)\n        return get_audio_buffer(frame, align);\n    return AVERROR(EINVAL);\n}', 'static int get_audio_buffer(AVFrame *frame, int align)\n{\n    int channels = av_get_channel_layout_nb_channels(frame->channel_layout);\n    int planar   = av_sample_fmt_is_planar(frame->format);\n    int planes   = planar ? channels : 1;\n    int ret, i;\n    if (!frame->linesize[0]) {\n        ret = av_samples_get_buffer_size(&frame->linesize[0], channels,\n                                         frame->nb_samples, frame->format,\n                                         align);\n        if (ret < 0)\n            return ret;\n    }\n    if (planes > AV_NUM_DATA_POINTERS) {\n        frame->extended_data = av_mallocz(planes *\n                                          sizeof(*frame->extended_data));\n        frame->extended_buf  = av_mallocz((planes - AV_NUM_DATA_POINTERS) *\n                                          sizeof(*frame->extended_buf));\n        if (!frame->extended_data || !frame->extended_buf) {\n            av_freep(&frame->extended_data);\n            av_freep(&frame->extended_buf);\n            return AVERROR(ENOMEM);\n        }\n        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;\n    } else\n        frame->extended_data = frame->data;\n    for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {\n        frame->buf[i] = av_buffer_alloc(frame->linesize[0]);\n        if (!frame->buf[i]) {\n            av_frame_unref(frame);\n            return AVERROR(ENOMEM);\n        }\n        frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;\n    }\n    for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {\n        frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);\n        if (!frame->extended_buf[i]) {\n            av_frame_unref(frame);\n            return AVERROR(ENOMEM);\n        }\n        frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;\n    }\n    return 0;\n}', 'int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,\n                               enum AVSampleFormat sample_fmt, int align)\n{\n    int line_size;\n    int sample_size = av_get_bytes_per_sample(sample_fmt);\n    int planar      = av_sample_fmt_is_planar(sample_fmt);\n    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)\n        return AVERROR(EINVAL);\n    if (!align) {\n        if (nb_samples > INT_MAX - 31)\n            return AVERROR(EINVAL);\n        align = 1;\n        nb_samples = FFALIGN(nb_samples, 32);\n    }\n    if (nb_channels > INT_MAX / align ||\n        (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)\n        return AVERROR(EINVAL);\n    line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :\n                         FFALIGN(nb_samples * sample_size * nb_channels, align);\n    if (linesize)\n        *linesize = line_size;\n    return planar ? line_size * nb_channels : line_size;\n}']