avconv: convert to codecpar

The switch is not yet complete because the parsers and the bistream
filters do not have a new AVCodecParam-based API yet.
This commit is contained in:
Anton Khirnov 2016-01-06 09:18:21 +01:00
parent 5b9cdf8cba
commit 15e84ed3f1
4 changed files with 106 additions and 95 deletions

116
avconv.c
View File

@ -257,8 +257,8 @@ static void abort_codec_experimental(AVCodec *c, int encoder)
static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
{
AVStream *st = ost->st;
AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
AVCodecContext *avctx = ost->encoding_needed ? ost->enc_ctx : ost->st->codec;
int ret;
/*
@ -268,14 +268,14 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
* Counting encoded video frames needs to be done separately because of
* reordering, see do_video_out()
*/
if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {
if (ost->frame_number >= ost->max_frames) {
av_packet_unref(pkt);
return;
}
ost->frame_number++;
}
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
NULL);
ost->quality = sd ? *(int *)sd : -1;
@ -287,6 +287,7 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
}
while (bsfc) {
AVCodecContext *avctx = ost->encoding_needed ? ost->enc_ctx : ost->st->codec;
AVPacket new_pkt = *pkt;
int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
&new_pkt.data, &new_pkt.size,
@ -1601,12 +1602,20 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
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_copy_context(ost->st->codec, ost->enc_ctx);
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);
}
/*
* FIXME: this is only so that the bitstream filters and parsers (that still
* work with a codec context) get the parameter values.
* This should go away with the new BSF/parser API.
*/
ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
if (ret < 0)
return ret;
if (ost->enc_ctx->nb_coded_side_data) {
int i;
@ -1635,7 +1644,15 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
if (ret < 0)
return ret;
ost->st->time_base = ost->st->codec->time_base;
/*
* FIXME: this is only so that the bitstream filters and parsers (that still
* work with a codec context) get the parameter values.
* This should go away with the new BSF/parser API.
*/
ret = avcodec_parameters_to_context(ost->st->codec, ost->st->codecpar);
if (ret < 0)
return ret;
}
return ret;
@ -1719,8 +1736,6 @@ static int transcode_init(void)
/* for each output stream, we compute the right encoding parameters */
for (i = 0; i < nb_output_streams; i++) {
AVCodecContext *enc_ctx;
AVCodecContext *dec_ctx = NULL;
ost = output_streams[i];
oc = output_files[ost->file_index]->ctx;
ist = get_input_stream(ost);
@ -1728,56 +1743,46 @@ static int transcode_init(void)
if (ost->attachment_filename)
continue;
enc_ctx = ost->stream_copy ? ost->st->codec : ost->enc_ctx;
if (ist) {
dec_ctx = ist->dec_ctx;
ost->st->disposition = ist->st->disposition;
enc_ctx->bits_per_raw_sample = dec_ctx->bits_per_raw_sample;
enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
}
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)dec_ctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE;
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 */
enc_ctx->codec_id = dec_ctx->codec_id;
enc_ctx->codec_type = dec_ctx->codec_type;
par_dst->codec_id = par_src->codec_id;
par_dst->codec_type = par_src->codec_type;
if (!enc_ctx->codec_tag) {
if (!par_dst->codec_tag) {
if (!oc->oformat->codec_tag ||
av_codec_get_id (oc->oformat->codec_tag, dec_ctx->codec_tag) == enc_ctx->codec_id ||
av_codec_get_tag(oc->oformat->codec_tag, dec_ctx->codec_id) <= 0)
enc_ctx->codec_tag = dec_ctx->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;
}
enc_ctx->bit_rate = dec_ctx->bit_rate;
enc_ctx->rc_max_rate = dec_ctx->rc_max_rate;
enc_ctx->rc_buffer_size = dec_ctx->rc_buffer_size;
enc_ctx->field_order = dec_ctx->field_order;
enc_ctx->extradata = av_mallocz(extra_size);
if (!enc_ctx->extradata) {
par_dst->bit_rate = par_src->bit_rate;
par_dst->field_order = par_src->field_order;
par_dst->chroma_location = par_src->chroma_location;
par_dst->extradata = av_mallocz(extra_size);
if (!par_dst->extradata) {
return AVERROR(ENOMEM);
}
memcpy(enc_ctx->extradata, dec_ctx->extradata, dec_ctx->extradata_size);
enc_ctx->extradata_size = dec_ctx->extradata_size;
if (!copy_tb) {
enc_ctx->time_base = dec_ctx->time_base;
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
} else
enc_ctx->time_base = ist->st->time_base;
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,
@ -1799,37 +1804,34 @@ static int transcode_init(void)
}
}
ost->parser = av_parser_init(enc_ctx->codec_id);
ost->parser = av_parser_init(par_dst->codec_id);
switch (enc_ctx->codec_type) {
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);
}
enc_ctx->channel_layout = dec_ctx->channel_layout;
enc_ctx->sample_rate = dec_ctx->sample_rate;
enc_ctx->channels = dec_ctx->channels;
enc_ctx->frame_size = dec_ctx->frame_size;
enc_ctx->audio_service_type = dec_ctx->audio_service_type;
enc_ctx->block_align = dec_ctx->block_align;
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:
enc_ctx->pix_fmt = dec_ctx->pix_fmt;
enc_ctx->width = dec_ctx->width;
enc_ctx->height = dec_ctx->height;
enc_ctx->has_b_frames = dec_ctx->has_b_frames;
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 * enc_ctx->height / enc_ctx->width, 255);
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 = dec_ctx->sample_aspect_ratio;
ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio = sar;
sar = par_src->sample_aspect_ratio;
ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
break;
case AVMEDIA_TYPE_SUBTITLE:
enc_ctx->width = dec_ctx->width;
enc_ctx->height = dec_ctx->height;
par_dst->width = par_src->width;
par_dst->height = par_src->height;
break;
case AVMEDIA_TYPE_DATA:
case AVMEDIA_TYPE_ATTACHMENT:
@ -1838,6 +1840,9 @@ static int transcode_init(void)
abort();
}
} else {
AVCodecContext *enc_ctx = ost->enc_ctx;
AVCodecContext *dec_ctx = NULL;
if (!ost->enc) {
/* should only happen when a default codec is not present. */
snprintf(error, sizeof(error), "Automatic encoder selection "
@ -1851,6 +1856,13 @@ static int transcode_init(void)
set_encoder_id(output_files[ost->file_index], ost);
if (ist) {
dec_ctx = ist->dec_ctx;
enc_ctx->bits_per_raw_sample = dec_ctx->bits_per_raw_sample;
enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
}
/*
* We want CFR output if and only if one of those is true:
* 1) user specified output framerate with -r

View File

@ -134,7 +134,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
s = input_files[file_idx]->ctx;
for (i = 0; i < s->nb_streams; i++) {
if (s->streams[i]->codec->codec_type != type)
if (s->streams[i]->codecpar->codec_type != type)
continue;
if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
st = s->streams[i];

View File

@ -470,11 +470,11 @@ static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *
MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
if (codec_name) {
AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0);
st->codec->codec_id = codec->id;
AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
st->codecpar->codec_id = codec->id;
return codec;
} else
return avcodec_find_decoder(st->codec->codec_id);
return avcodec_find_decoder(st->codecpar->codec_id);
}
/* Add all the streams from the given input file to the global
@ -485,7 +485,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
for (i = 0; i < ic->nb_streams; i++) {
AVStream *st = ic->streams[i];
AVCodecContext *dec = st->codec;
AVCodecParameters *par = st->codecpar;
InputStream *ist = av_mallocz(sizeof(*ist));
char *framerate = NULL, *hwaccel = NULL, *hwaccel_device = NULL;
char *codec_tag = NULL;
@ -516,11 +516,11 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
uint32_t tag = strtol(codec_tag, &next, 0);
if (*next)
tag = AV_RL32(codec_tag);
st->codec->codec_tag = tag;
st->codecpar->codec_tag = tag;
}
ist->dec = choose_decoder(o, ic, st);
ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codec->codec_id, ic, st, ist->dec);
ist->decoder_opts = filter_codec_opts(o->g->codec_opts, par->codec_id, ic, st, ist->dec);
ist->dec_ctx = avcodec_alloc_context3(ist->dec);
if (!ist->dec_ctx) {
@ -528,13 +528,13 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
exit_program(1);
}
ret = avcodec_copy_context(ist->dec_ctx, dec);
ret = avcodec_parameters_to_context(ist->dec_ctx, par);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
exit_program(1);
}
switch (dec->codec_type) {
switch (par->codec_type) {
case AVMEDIA_TYPE_VIDEO:
ist->resample_height = ist->dec_ctx->height;
ist->resample_width = ist->dec_ctx->width;
@ -637,7 +637,7 @@ static void dump_attachment(AVStream *st, const char *filename)
AVIOContext *out = NULL;
AVDictionaryEntry *e;
if (!st->codec->extradata_size) {
if (!st->codecpar->extradata_size) {
av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
nb_input_files - 1, st->index);
return;
@ -658,7 +658,7 @@ static void dump_attachment(AVStream *st, const char *filename)
exit_program(1);
}
avio_write(out, st->codec->extradata, st->codec->extradata_size);
avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
avio_flush(out);
avio_close(out);
}
@ -895,14 +895,14 @@ static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *
MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
if (!codec_name) {
ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
NULL, ost->st->codec->codec_type);
ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
NULL, ost->st->codecpar->codec_type);
ost->enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
} else if (!strcmp(codec_name, "copy"))
ost->stream_copy = 1;
else {
ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1);
ost->st->codec->codec_id = ost->enc->id;
ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1);
ost->st->codecpar->codec_id = ost->enc->id;
}
}
@ -931,7 +931,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
ost->file_index = nb_output_files - 1;
ost->index = idx;
ost->st = st;
st->codec->codec_type = type;
st->codecpar->codec_type = type;
choose_encoder(o, oc, ost);
ost->enc_ctx = avcodec_alloc_context3(ost->enc);
@ -1088,7 +1088,7 @@ static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc,
else if (filter)
return av_strdup(filter);
return av_strdup(st->codec->codec_type == AVMEDIA_TYPE_VIDEO ?
return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
"null" : "anull");
}
@ -1524,9 +1524,9 @@ static int open_output_file(OptionsContext *o, const char *filename)
int area = 0, idx = -1;
for (i = 0; i < nb_input_streams; i++) {
ist = input_streams[i];
if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
ist->st->codec->width * ist->st->codec->height > area) {
area = ist->st->codec->width * ist->st->codec->height;
if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
ist->st->codecpar->width * ist->st->codecpar->height > area) {
area = ist->st->codecpar->width * ist->st->codecpar->height;
idx = i;
}
}
@ -1538,9 +1538,9 @@ static int open_output_file(OptionsContext *o, const char *filename)
int channels = 0, idx = -1;
for (i = 0; i < nb_input_streams; i++) {
ist = input_streams[i];
if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
ist->st->codec->channels > channels) {
channels = ist->st->codec->channels;
if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
ist->st->codecpar->channels > channels) {
channels = ist->st->codecpar->channels;
idx = i;
}
}
@ -1550,7 +1550,7 @@ static int open_output_file(OptionsContext *o, const char *filename)
/* subtitles: pick first */
if (!o->subtitle_disable && oc->oformat->subtitle_codec != AV_CODEC_ID_NONE) {
for (i = 0; i < nb_input_streams; i++)
if (input_streams[i]->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
NEW_STREAM(subtitle, i);
break;
}
@ -1587,7 +1587,7 @@ loop_end:
init_output_filter(ofilter, o, oc);
} else {
ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
switch (ist->st->codec->codec_type) {
switch (ist->st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break;
case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
@ -1636,8 +1636,8 @@ loop_end:
ost->stream_copy = 0;
ost->source_index = -1;
ost->attachment_filename = o->attachments[i];
ost->st->codec->extradata = attachment;
ost->st->codec->extradata_size = len;
ost->st->codecpar->extradata = attachment;
ost->st->codecpar->extradata_size = len;
p = strrchr(o->attachments[i], '/');
av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
@ -1838,11 +1838,10 @@ static int opt_target(void *optctx, const char *opt, const char *arg)
int i, j, fr;
for (j = 0; j < nb_input_files; j++) {
for (i = 0; i < input_files[j]->nb_streams; i++) {
AVCodecContext *c = input_files[j]->ctx->streams[i]->codec;
if (c->codec_type != AVMEDIA_TYPE_VIDEO ||
!c->time_base.num)
AVStream *st = input_files[j]->ctx->streams[i];
if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
continue;
fr = c->time_base.den * 1000 / c->time_base.num;
fr = st->time_base.den * 1000 / st->time_base.num;
if (fr == 25000) {
norm = PAL;
break;

View File

@ -1504,12 +1504,12 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
default: av_assert0(0);
}
if (type != st->codec->codec_type)
if (type != st->codecpar->codec_type)
return 0;
if (*spec++ == ':') { /* possibly followed by :index */
int i, index = strtol(spec, NULL, 0);
for (i = 0; i < s->nb_streams; i++)
if (s->streams[i]->codec->codec_type == type && index-- == 0)
if (s->streams[i]->codecpar->codec_type == type && index-- == 0)
return i == st->index;
return 0;
}
@ -1565,17 +1565,17 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
av_freep(&key);
return ret;
} else if (*spec == 'u') {
AVCodecContext *avctx = st->codec;
AVCodecParameters *par = st->codecpar;
int val;
switch (avctx->codec_type) {
switch (par->codec_type) {
case AVMEDIA_TYPE_AUDIO:
val = avctx->sample_rate && avctx->channels;
if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
val = par->sample_rate && par->channels;
if (par->format == AV_SAMPLE_FMT_NONE)
return 0;
break;
case AVMEDIA_TYPE_VIDEO:
val = avctx->width && avctx->height;
if (avctx->pix_fmt == AV_PIX_FMT_NONE)
val = par->width && par->height;
if (par->format == AV_PIX_FMT_NONE)
return 0;
break;
case AVMEDIA_TYPE_UNKNOWN:
@ -1585,7 +1585,7 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
val = 1;
break;
}
return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
return par->codec_id != AV_CODEC_ID_NONE && val != 0;
} else if (!*spec) /* empty specifier, matches everything */
return 1;
@ -1607,7 +1607,7 @@ AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
codec = s->oformat ? avcodec_find_encoder(codec_id)
: avcodec_find_decoder(codec_id);
switch (st->codec->codec_type) {
switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO:
prefix = 'v';
flags |= AV_OPT_FLAG_VIDEO_PARAM;
@ -1664,7 +1664,7 @@ AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
return NULL;
}
for (i = 0; i < s->nb_streams; i++)
opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
s, s->streams[i], NULL);
return opts;
}