static int init_output_stream(OutputStream *ost, char *error, int error_len)

{

    int ret = 0;



    if (ost->encoding_needed) {

        AVCodec      *codec = ost->enc;

        AVCodecContext *dec = NULL;

        InputStream *ist;



        ret = init_output_stream_encode(ost);

        if (ret < 0)

            return ret;



        if ((ist = get_input_stream(ost)))

            dec = ist->dec_ctx;

        if (dec && dec->subtitle_header) {

            ost->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);

            if (!ost->enc_ctx->subtitle_header)

                return AVERROR(ENOMEM);

            memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);

            ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;

        }

        if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))

            av_dict_set(&ost->encoder_opts, "threads", "auto", 0);



        if (ost->filter && ost->filter->filter->inputs[0]->hw_frames_ctx) {

            ost->enc_ctx->hw_frames_ctx = av_buffer_ref(ost->filter->filter->inputs[0]->hw_frames_ctx);

            if (!ost->enc_ctx->hw_frames_ctx)

                return AVERROR(ENOMEM);

        }



        if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {

            if (ret == AVERROR_EXPERIMENTAL)

                abort_codec_experimental(codec, 1);

            snprintf(error, error_len,

                     "Error while opening encoder for output stream #%d:%d - "

                     "maybe incorrect parameters such as bit_rate, rate, width or height",

                    ost->file_index, ost->index);

            return ret;

        }

        assert_avoptions(ost->encoder_opts);

        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)

            av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."

                                         "It takes bits/s as argument, not kbits/s\n");



        ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);

        if (ret < 0) {

            av_log(NULL, AV_LOG_FATAL,

                   "Error initializing the output stream codec context.\n");

            exit_program(1);

        }



        if (ost->enc_ctx->nb_coded_side_data) {

            int i;



            ost->st->side_data = av_realloc_array(NULL, ost->enc_ctx->nb_coded_side_data,

                                                  sizeof(*ost->st->side_data));

            if (!ost->st->side_data)

                return AVERROR(ENOMEM);



            for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {

                const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];

                AVPacketSideData *sd_dst = &ost->st->side_data[i];



                sd_dst->data = av_malloc(sd_src->size);

                if (!sd_dst->data)

                    return AVERROR(ENOMEM);

                memcpy(sd_dst->data, sd_src->data, sd_src->size);

                sd_dst->size = sd_src->size;

                sd_dst->type = sd_src->type;

                ost->st->nb_side_data++;

            }

        }



        ost->st->time_base = ost->enc_ctx->time_base;

    } else if (ost->stream_copy) {

        ret = init_output_stream_streamcopy(ost);

        if (ret < 0)

            return ret;



        /*

         * FIXME: will the codec context used by the parser during streamcopy

         * This should go away with the new parser API.

         */

        ret = avcodec_parameters_to_context(ost->parser_avctx, ost->st->codecpar);

        if (ret < 0)

            return ret;

    }



    /* initialize bitstream filters for the output stream

     * needs to be done here, because the codec id for streamcopy is not

     * known until now */

    ret = init_output_bsfs(ost);

    if (ret < 0)

        return ret;



    ost->mux_timebase = ost->st->time_base;



    ost->initialized = 1;



    ret = check_init_output_file(output_files[ost->file_index], ost->file_index);

    if (ret < 0)

        return ret;



    return ret;

}

static int init_output_stream(OutputStream *ost, char *error, int error_len)

{

    int ret = 0;



    if (ost->encoding_needed) {

        AVCodec      *codec = ost->enc;

        AVCodecContext *dec = NULL;

        InputStream *ist;



        ret = init_output_stream_encode(ost);

        if (ret < 0)

            return ret;



        if ((ist = get_input_stream(ost)))

            dec = ist->dec_ctx;

        if (dec && dec->subtitle_header) {

            ost->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);

            if (!ost->enc_ctx->subtitle_header)

                return AVERROR(ENOMEM);

            memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);

            ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;

        }

        if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))

            av_dict_set(&ost->encoder_opts, "threads", "auto", 0);



        if (ost->filter && ost->filter->filter->inputs[0]->hw_frames_ctx) {

            ost->enc_ctx->hw_frames_ctx = av_buffer_ref(ost->filter->filter->inputs[0]->hw_frames_ctx);

            if (!ost->enc_ctx->hw_frames_ctx)

                return AVERROR(ENOMEM);

        }



        if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {

            if (ret == AVERROR_EXPERIMENTAL)

                abort_codec_experimental(codec, 1);

            snprintf(error, error_len,

                     "Error while opening encoder for output stream #%d:%d - "

                     "maybe incorrect parameters such as bit_rate, rate, width or height",

                    ost->file_index, ost->index);

            return ret;

        }

        assert_avoptions(ost->encoder_opts);

        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)

            av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."

                                         "It takes bits/s as argument, not kbits/s\n");



        ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);

        if (ret < 0) {

            av_log(NULL, AV_LOG_FATAL,

                   "Error initializing the output stream codec context.\n");

            exit_program(1);

        }



        if (ost->enc_ctx->nb_coded_side_data) {

            int i;



            ost->st->side_data = av_realloc_array(NULL, ost->enc_ctx->nb_coded_side_data,

                                                  sizeof(*ost->st->side_data));

            if (!ost->st->side_data)

                return AVERROR(ENOMEM);



            for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {

                const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];

                AVPacketSideData *sd_dst = &ost->st->side_data[i];



                sd_dst->data = av_malloc(sd_src->size);

                if (!sd_dst->data)

                    return AVERROR(ENOMEM);

                memcpy(sd_dst->data, sd_src->data, sd_src->size);

                sd_dst->size = sd_src->size;

                sd_dst->type = sd_src->type;

                ost->st->nb_side_data++;

            }

        }



        ost->st->time_base = ost->enc_ctx->time_base;

    } else if (ost->stream_copy) {

        ret = init_output_stream_streamcopy(ost);

        if (ret < 0)

            return ret;



        







        ret = avcodec_parameters_to_context(ost->parser_avctx, ost->st->codecpar);

        if (ret < 0)

            return ret;

    }



    





    ret = init_output_bsfs(ost);

    if (ret < 0)

        return ret;



    ost->mux_timebase = ost->st->time_base;



    ost->initialized = 1;



    ret = check_init_output_file(output_files[ost->file_index], ost->file_index);

    if (ret < 0)

        return ret;



    return ret;

}

static int FUNC_0(OutputStream *VAR_0, char *VAR_1, int VAR_2)

{

    int VAR_3 = 0;



    if (VAR_0->encoding_needed) {

        AVCodec      *codec = VAR_0->enc;

        AVCodecContext *dec = NULL;

        InputStream *ist;



        VAR_3 = init_output_stream_encode(VAR_0);

        if (VAR_3 < 0)

            return VAR_3;



        if ((ist = get_input_stream(VAR_0)))

            dec = ist->dec_ctx;

        if (dec && dec->subtitle_header) {

            VAR_0->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);

            if (!VAR_0->enc_ctx->subtitle_header)

                return AVERROR(ENOMEM);

            memcpy(VAR_0->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);

            VAR_0->enc_ctx->subtitle_header_size = dec->subtitle_header_size;

        }

        if (!av_dict_get(VAR_0->encoder_opts, "threads", NULL, 0))

            av_dict_set(&VAR_0->encoder_opts, "threads", "auto", 0);



        if (VAR_0->filter && VAR_0->filter->filter->inputs[0]->hw_frames_ctx) {

            VAR_0->enc_ctx->hw_frames_ctx = av_buffer_ref(VAR_0->filter->filter->inputs[0]->hw_frames_ctx);

            if (!VAR_0->enc_ctx->hw_frames_ctx)

                return AVERROR(ENOMEM);

        }



        if ((VAR_3 = avcodec_open2(VAR_0->enc_ctx, codec, &VAR_0->encoder_opts)) < 0) {

            if (VAR_3 == AVERROR_EXPERIMENTAL)

                abort_codec_experimental(codec, 1);

            snprintf(VAR_1, VAR_2,

                     "Error while opening encoder for output stream #%d:%d - "

                     "maybe incorrect parameters such as bit_rate, rate, width or height",

                    VAR_0->file_index, VAR_0->index);

            return VAR_3;

        }

        assert_avoptions(VAR_0->encoder_opts);

        if (VAR_0->enc_ctx->bit_rate && VAR_0->enc_ctx->bit_rate < 1000)

            av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."

                                         "It takes bits/s as argument, not kbits/s\n");



        VAR_3 = avcodec_parameters_from_context(VAR_0->st->codecpar, VAR_0->enc_ctx);

        if (VAR_3 < 0) {

            av_log(NULL, AV_LOG_FATAL,

                   "Error initializing the output stream codec context.\n");

            exit_program(1);

        }



        if (VAR_0->enc_ctx->nb_coded_side_data) {

            int VAR_4;



            VAR_0->st->side_data = av_realloc_array(NULL, VAR_0->enc_ctx->nb_coded_side_data,

                                                  sizeof(*VAR_0->st->side_data));

            if (!VAR_0->st->side_data)

                return AVERROR(ENOMEM);



            for (VAR_4 = 0; VAR_4 < VAR_0->enc_ctx->nb_coded_side_data; VAR_4++) {

                const AVPacketSideData *sd_src = &VAR_0->enc_ctx->coded_side_data[VAR_4];

                AVPacketSideData *sd_dst = &VAR_0->st->side_data[VAR_4];



                sd_dst->data = av_malloc(sd_src->size);

                if (!sd_dst->data)

                    return AVERROR(ENOMEM);

                memcpy(sd_dst->data, sd_src->data, sd_src->size);

                sd_dst->size = sd_src->size;

                sd_dst->type = sd_src->type;

                VAR_0->st->nb_side_data++;

            }

        }



        VAR_0->st->time_base = VAR_0->enc_ctx->time_base;

    } else if (VAR_0->stream_copy) {

        VAR_3 = init_output_stream_streamcopy(VAR_0);

        if (VAR_3 < 0)

            return VAR_3;



        







        VAR_3 = avcodec_parameters_to_context(VAR_0->parser_avctx, VAR_0->st->codecpar);

        if (VAR_3 < 0)

            return VAR_3;

    }



    





    VAR_3 = init_output_bsfs(VAR_0);

    if (VAR_3 < 0)

        return VAR_3;



    VAR_0->mux_timebase = VAR_0->st->time_base;



    VAR_0->initialized = 1;



    VAR_3 = check_init_output_file(output_files[VAR_0->file_index], VAR_0->file_index);

    if (VAR_3 < 0)

        return VAR_3;



    return VAR_3;

}

static int FUNC_0(OutputStream *VAR_0, char *VAR_1, int VAR_2)
{ int VAR_3 = 0; if (VAR_0->encoding_needed) { AVCodec      *codec = VAR_0->enc; AVCodecContext *dec = NULL; InputStream *ist; VAR_3 = init_output_stream_encode(VAR_0); if (VAR_3 < 0)
return VAR_3; if ((ist = get_input_stream(VAR_0)))
dec = ist->dec_ctx; if (dec && dec->subtitle_header) { VAR_0->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size); if (!VAR_0->enc_ctx->subtitle_header)
return AVERROR(ENOMEM); memcpy(VAR_0->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size); VAR_0->enc_ctx->subtitle_header_size = dec->subtitle_header_size; } if (!av_dict_get(VAR_0->encoder_opts, "threads", NULL, 0))
av_dict_set(&VAR_0->encoder_opts, "threads", "auto", 0); if (VAR_0->filter && VAR_0->filter->filter->inputs[0]->hw_frames_ctx) { VAR_0->enc_ctx->hw_frames_ctx = av_buffer_ref(VAR_0->filter->filter->inputs[0]->hw_frames_ctx); if (!VAR_0->enc_ctx->hw_frames_ctx)
return AVERROR(ENOMEM); } if ((VAR_3 = avcodec_open2(VAR_0->enc_ctx, codec, &VAR_0->encoder_opts)) < 0) { if (VAR_3 == AVERROR_EXPERIMENTAL)
abort_codec_experimental(codec, 1); snprintf(VAR_1, VAR_2,
"Error while opening encoder for output stream #%d:%d - "
"maybe incorrect parameters such as bit_rate, rate, width or height",
VAR_0->file_index, VAR_0->index); return VAR_3; } assert_avoptions(VAR_0->encoder_opts); if (VAR_0->enc_ctx->bit_rate && VAR_0->enc_ctx->bit_rate < 1000)
av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
"It takes bits/s as argument, not kbits/s\n"); VAR_3 = avcodec_parameters_from_context(VAR_0->st->codecpar, VAR_0->enc_ctx); if (VAR_3 < 0) { av_log(NULL, AV_LOG_FATAL,
"Error initializing the output stream codec context.\n"); exit_program(1); } if (VAR_0->enc_ctx->nb_coded_side_data) { int VAR_4; VAR_0->st->side_data = av_realloc_array(NULL, VAR_0->enc_ctx->nb_coded_side_data,
sizeof(*VAR_0->st->side_data)); if (!VAR_0->st->side_data)
return AVERROR(ENOMEM); for (VAR_4 = 0; VAR_4 < VAR_0->enc_ctx->nb_coded_side_data; VAR_4++) { const AVPacketSideData *sd_src = &VAR_0->enc_ctx->coded_side_data[VAR_4]; AVPacketSideData *sd_dst = &VAR_0->st->side_data[VAR_4]; sd_dst->data = av_malloc(sd_src->size); if (!sd_dst->data)
return AVERROR(ENOMEM); memcpy(sd_dst->data, sd_src->data, sd_src->size); sd_dst->size = sd_src->size; sd_dst->type = sd_src->type; VAR_0->st->nb_side_data++; } } VAR_0->st->time_base = VAR_0->enc_ctx->time_base; } else if (VAR_0->stream_copy) { VAR_3 = init_output_stream_streamcopy(VAR_0); if (VAR_3 < 0)
return VAR_3; VAR_3 = avcodec_parameters_to_context(VAR_0->parser_avctx, VAR_0->st->codecpar); if (VAR_3 < 0)
return VAR_3; } VAR_3 = init_output_bsfs(VAR_0); if (VAR_3 < 0)
return VAR_3; VAR_0->mux_timebase = VAR_0->st->time_base; VAR_0->initialized = 1; VAR_3 = check_init_output_file(output_files[VAR_0->file_index], VAR_0->file_index); if (VAR_3 < 0)
return VAR_3; return VAR_3; }