Commit 3597d32e authored by James Almer's avatar James Almer

Merge commit '6ed0f70f'

* commit '6ed0f70f':
  avconv: factor out initializing stream parameters for streamcopy

Conflicts:
    ffmpeg.c
Merged-by: 's avatarJames Almer <jamrial@gmail.com>
parents df2ae8f3 6ed0f70f
......@@ -2666,6 +2666,159 @@ 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 = ost->ref_par;
AVRational sar;
int i, ret;
uint64_t extra_size;
av_assert0(ist && !ost->filter);
avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
if (ret < 0) {
av_log(NULL, AV_LOG_FATAL,
"Error setting up codec context options.\n");
return ret;
}
avcodec_parameters_from_context(par_src, ost->enc_ctx);
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) {
unsigned int 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_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag))
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_size) {
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;
}
par_dst->bits_per_coded_sample = par_src->bits_per_coded_sample;
par_dst->bits_per_raw_sample = par_src->bits_per_raw_sample;
if (!ost->frame_rate.num)
ost->frame_rate = ist->framerate;
ost->st->avg_frame_rate = ost->frame_rate;
ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, ost->st, ist->st, copy_tb);
if (ret < 0)
return ret;
// copy timebase while removing common factors
ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
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);
ost->st->nb_side_data = 0;
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[ost->st->nb_side_data];
if (ost->rotate_overridden && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
continue;
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->frame_size = par_src->frame_size;
par_dst->block_align = par_src->block_align;
par_dst->initial_padding = par_src->initial_padding;
par_dst->trailing_padding = par_src->trailing_padding;
par_dst->profile = par_src->profile;
if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
par_dst->block_align= 0;
if(par_dst->codec_id == AV_CODEC_ID_AC3)
par_dst->block_align= 0;
break;
case AVMEDIA_TYPE_VIDEO:
par_dst->format = par_src->format;
par_dst->color_space = par_src->color_space;
par_dst->color_range = par_src->color_range;
par_dst->color_primaries = par_src->color_primaries;
par_dst->color_trc = par_src->color_trc;
par_dst->width = par_src->width;
par_dst->height = par_src->height;
par_dst->video_delay = par_src->video_delay;
par_dst->profile = par_src->profile;
if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
sar =
av_mul_q(ost->frame_aspect_ratio,
(AVRational){ par_dst->height, par_dst->width });
av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
"with stream copy may produce invalid files\n");
}
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;
ost->st->avg_frame_rate = ist->st->avg_frame_rate;
ost->st->r_frame_rate = ist->st->r_frame_rate;
break;
case AVMEDIA_TYPE_SUBTITLE:
par_dst->width = par_src->width;
par_dst->height = par_src->height;
break;
case AVMEDIA_TYPE_UNKNOWN:
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;
......@@ -2756,6 +2909,10 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
ost->st->codec->codec= ost->enc_ctx->codec;
} 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.
......@@ -2949,151 +3106,7 @@ static int transcode_init(void)
ost->st->disposition = AV_DISPOSITION_DEFAULT;
}
if (ost->stream_copy) {
AVCodecParameters *par_dst = ost->st->codecpar;
AVCodecParameters *par_src = ost->ref_par;
AVRational sar;
uint64_t extra_size;
av_assert0(ist && !ost->filter);
avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
if (ret < 0) {
av_log(NULL, AV_LOG_FATAL,
"Error setting up codec context options.\n");
return ret;
}
avcodec_parameters_from_context(par_src, ost->enc_ctx);
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) {
unsigned int 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_tag2(oc->oformat->codec_tag, par_src->codec_id, &codec_tag))
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_size) {
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;
par_dst->bits_per_coded_sample = par_src->bits_per_coded_sample;
par_dst->bits_per_raw_sample = par_src->bits_per_raw_sample;
if (!ost->frame_rate.num)
ost->frame_rate = ist->framerate;
ost->st->avg_frame_rate = ost->frame_rate;
ret = avformat_transfer_internal_stream_timing_info(oc->oformat, ost->st, ist->st, copy_tb);
if (ret < 0)
return ret;
// copy timebase while removing common factors
ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
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);
ost->st->nb_side_data = 0;
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[ost->st->nb_side_data];
if (ost->rotate_overridden && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
continue;
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->frame_size = par_src->frame_size;
par_dst->block_align = par_src->block_align;
par_dst->initial_padding = par_src->initial_padding;
par_dst->trailing_padding = par_src->trailing_padding;
par_dst->profile = par_src->profile;
if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
par_dst->block_align= 0;
if(par_dst->codec_id == AV_CODEC_ID_AC3)
par_dst->block_align= 0;
break;
case AVMEDIA_TYPE_VIDEO:
par_dst->format = par_src->format;
par_dst->color_space = par_src->color_space;
par_dst->color_range = par_src->color_range;
par_dst->color_primaries = par_src->color_primaries;
par_dst->color_trc = par_src->color_trc;
par_dst->width = par_src->width;
par_dst->height = par_src->height;
par_dst->video_delay = par_src->video_delay;
par_dst->profile = par_src->profile;
if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
sar =
av_mul_q(ost->frame_aspect_ratio,
(AVRational){ par_dst->height, par_dst->width });
av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
"with stream copy may produce invalid files\n");
}
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;
ost->st->avg_frame_rate = ist->st->avg_frame_rate;
ost->st->r_frame_rate = ist->st->r_frame_rate;
break;
case AVMEDIA_TYPE_SUBTITLE:
par_dst->width = par_src->width;
par_dst->height = par_src->height;
break;
case AVMEDIA_TYPE_UNKNOWN:
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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment