From: Anton Khirnov Date: Mon, 23 May 2016 07:37:10 +0000 (+0200) Subject: avconv: factor out initializing stream parameters for streamcopy X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=6ed0f70f97c882813199b3bafd724ceeb43659de;p=ffmpeg avconv: factor out initializing stream parameters for streamcopy --- diff --git a/avconv.c b/avconv.c index 74de3ac479e..385eb2c16b2 100644 --- a/avconv.c +++ b/avconv.c @@ -1663,6 +1663,111 @@ static int init_output_bsfs(OutputStream *ost) return 0; } +static int init_output_stream_streamcopy(OutputStream *ost) +{ + OutputFile *of = output_files[ost->file_index]; + InputStream *ist = get_input_stream(ost); + AVCodecParameters *par_dst = ost->st->codecpar; + AVCodecParameters *par_src = ist->st->codecpar; + AVRational sar; + int i; + uint64_t extra_size; + + extra_size = (uint64_t)par_src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE; + if (extra_size > INT_MAX) { + return AVERROR(EINVAL); + } + + ost->st->disposition = ist->st->disposition; + + /* if stream_copy is selected, no need to decode or encode */ + par_dst->codec_id = par_src->codec_id; + par_dst->codec_type = par_src->codec_type; + + if (!par_dst->codec_tag) { + if (!of->ctx->oformat->codec_tag || + av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_dst->codec_id || + av_codec_get_tag(of->ctx->oformat->codec_tag, par_src->codec_id) <= 0) + par_dst->codec_tag = par_src->codec_tag; + } + + par_dst->bit_rate = par_src->bit_rate; + par_dst->field_order = par_src->field_order; + par_dst->chroma_location = par_src->chroma_location; + + if (par_src->extradata) { + par_dst->extradata = av_mallocz(extra_size); + if (!par_dst->extradata) { + return AVERROR(ENOMEM); + } + memcpy(par_dst->extradata, par_src->extradata, par_src->extradata_size); + par_dst->extradata_size = par_src->extradata_size; + } + + ost->st->time_base = ist->st->time_base; + + if (ist->st->nb_side_data) { + ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data, + sizeof(*ist->st->side_data)); + if (!ost->st->side_data) + return AVERROR(ENOMEM); + + for (i = 0; i < ist->st->nb_side_data; i++) { + const AVPacketSideData *sd_src = &ist->st->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->parser = av_parser_init(par_dst->codec_id); + ost->parser_avctx = avcodec_alloc_context3(NULL); + if (!ost->parser_avctx) + return AVERROR(ENOMEM); + + switch (par_dst->codec_type) { + case AVMEDIA_TYPE_AUDIO: + if (audio_volume != 256) { + av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n"); + exit_program(1); + } + par_dst->channel_layout = par_src->channel_layout; + par_dst->sample_rate = par_src->sample_rate; + par_dst->channels = par_src->channels; + par_dst->block_align = par_src->block_align; + break; + case AVMEDIA_TYPE_VIDEO: + par_dst->format = par_src->format; + par_dst->width = par_src->width; + par_dst->height = par_src->height; + if (ost->frame_aspect_ratio) + sar = av_d2q(ost->frame_aspect_ratio * par_dst->height / par_dst->width, 255); + else if (ist->st->sample_aspect_ratio.num) + sar = ist->st->sample_aspect_ratio; + else + sar = par_src->sample_aspect_ratio; + ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar; + break; + case AVMEDIA_TYPE_SUBTITLE: + par_dst->width = par_src->width; + par_dst->height = par_src->height; + break; + case AVMEDIA_TYPE_DATA: + case AVMEDIA_TYPE_ATTACHMENT: + break; + default: + abort(); + } + + return 0; +} + static int init_output_stream(OutputStream *ost, char *error, int error_len) { int ret = 0; @@ -1735,6 +1840,10 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len) 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. @@ -1843,105 +1952,7 @@ static int transcode_init(void) ost->st->disposition = ist->st->disposition; } - if (ost->stream_copy) { - AVCodecParameters *par_dst = ost->st->codecpar; - AVCodecParameters *par_src = ist->st->codecpar; - AVRational sar; - uint64_t extra_size; - - av_assert0(ist && !ost->filter); - - extra_size = (uint64_t)par_src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE; - - if (extra_size > INT_MAX) { - return AVERROR(EINVAL); - } - - /* if stream_copy is selected, no need to decode or encode */ - par_dst->codec_id = par_src->codec_id; - par_dst->codec_type = par_src->codec_type; - - if (!par_dst->codec_tag) { - if (!oc->oformat->codec_tag || - av_codec_get_id (oc->oformat->codec_tag, par_src->codec_tag) == par_dst->codec_id || - av_codec_get_tag(oc->oformat->codec_tag, par_src->codec_id) <= 0) - par_dst->codec_tag = par_src->codec_tag; - } - - par_dst->bit_rate = par_src->bit_rate; - par_dst->field_order = par_src->field_order; - par_dst->chroma_location = par_src->chroma_location; - if (par_src->extradata != NULL) { - par_dst->extradata = av_mallocz(extra_size); - if (!par_dst->extradata) { - return AVERROR(ENOMEM); - } - memcpy(par_dst->extradata, par_src->extradata, - par_src->extradata_size); - par_dst->extradata_size = par_src->extradata_size; - } - - ost->st->time_base = ist->st->time_base; - - if (ist->st->nb_side_data) { - ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data, - sizeof(*ist->st->side_data)); - if (!ost->st->side_data) - return AVERROR(ENOMEM); - - for (j = 0; j < ist->st->nb_side_data; j++) { - const AVPacketSideData *sd_src = &ist->st->side_data[j]; - AVPacketSideData *sd_dst = &ost->st->side_data[j]; - - 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->parser = av_parser_init(par_dst->codec_id); - ost->parser_avctx = avcodec_alloc_context3(NULL); - if (!ost->parser_avctx) - return AVERROR(ENOMEM); - - switch (par_dst->codec_type) { - case AVMEDIA_TYPE_AUDIO: - if (audio_volume != 256) { - av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n"); - exit_program(1); - } - par_dst->channel_layout = par_src->channel_layout; - par_dst->sample_rate = par_src->sample_rate; - par_dst->channels = par_src->channels; - par_dst->block_align = par_src->block_align; - break; - case AVMEDIA_TYPE_VIDEO: - par_dst->format = par_src->format; - par_dst->width = par_src->width; - par_dst->height = par_src->height; - if (ost->frame_aspect_ratio) - sar = av_d2q(ost->frame_aspect_ratio * par_dst->height / par_dst->width, 255); - else if (ist->st->sample_aspect_ratio.num) - sar = ist->st->sample_aspect_ratio; - else - sar = par_src->sample_aspect_ratio; - ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar; - break; - case AVMEDIA_TYPE_SUBTITLE: - par_dst->width = par_src->width; - par_dst->height = par_src->height; - break; - case AVMEDIA_TYPE_DATA: - case AVMEDIA_TYPE_ATTACHMENT: - break; - default: - abort(); - } - } else { + if (!ost->stream_copy) { AVCodecContext *enc_ctx = ost->enc_ctx; AVCodecContext *dec_ctx = NULL;