Use a private codec context instance for decoding.
---
libavformat/internal.h | 8 +++
libavformat/utils.c | 158 ++++++++++++++++++++++++++++++++++++-------------
2 files changed, 126 insertions(+), 40 deletions(-)
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 17c45f6..d4fb448 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -102,6 +102,14 @@ struct AVStreamInternal {
* from dts.
*/
int reorder;
+ /**
+ * The codec context used by find_stream_info, the parser, etc.
+ */
+ AVCodecContext *avctx;
+ /**
+ * 1 if avctx has been initialized with the values from the codec
parameters
+ */
+ int avctx_inited;
};
void ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem);
diff --git a/libavformat/utils.c b/libavformat/utils.c
index f6611f0..50ca1db 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1822,7 +1822,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t
old_offset)
static int has_codec_parameters(AVStream *st)
{
- AVCodecContext *avctx = st->codec;
+ AVCodecContext *avctx = st->internal->avctx;
int val;
switch (avctx->codec_type) {
@@ -1846,14 +1846,15 @@ static int has_codec_parameters(AVStream *st)
static int has_decode_delay_been_guessed(AVStream *st)
{
- return st->codec->codec_id != AV_CODEC_ID_H264 ||
+ return st->internal->avctx->codec_id != AV_CODEC_ID_H264 ||
st->info->nb_decoded_frames >= 6;
}
/* returns 1 or 0 if or if not decoded data was returned, or a negative error
*/
-static int try_decode_frame(AVStream *st, AVPacket *avpkt,
+static int try_decode_frame(AVFormatContext *s, AVStream *st, AVPacket *avpkt,
AVDictionary **options)
{
+ AVCodecContext *avctx = st->internal->avctx;
const AVCodec *codec;
int got_picture = 1, ret = 0;
AVFrame *frame = av_frame_alloc();
@@ -1862,11 +1863,17 @@ static int try_decode_frame(AVStream *st, AVPacket
*avpkt,
if (!frame)
return AVERROR(ENOMEM);
- if (!avcodec_is_open(st->codec) && !st->info->found_decoder) {
+ if (!avcodec_is_open(avctx) && !st->info->found_decoder) {
AVDictionary *thread_opt = NULL;
+#if FF_API_LAVF_AVCTX
+FF_DISABLE_DEPRECATION_WARNINGS
codec = st->codec->codec ? st->codec->codec
- : avcodec_find_decoder(st->codec->codec_id);
+ :
avcodec_find_decoder(st->codecpar->codec_id);
+FF_ENABLE_DEPRECATION_WARNINGS
+#else
+ codec = avcodec_find_decoder(st->codecpar->codec_id);
+#endif
if (!codec) {
st->info->found_decoder = -1;
@@ -1877,7 +1884,7 @@ static int try_decode_frame(AVStream *st, AVPacket *avpkt,
/* Force thread count to 1 since the H.264 decoder will not extract
* SPS and PPS to extradata during multi-threaded decoding. */
av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
- ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
+ ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
if (!options)
av_dict_free(&thread_opt);
if (ret < 0) {
@@ -1897,15 +1904,15 @@ static int try_decode_frame(AVStream *st, AVPacket
*avpkt,
ret >= 0 &&
(!has_codec_parameters(st) || !has_decode_delay_been_guessed(st) ||
(!st->codec_info_nb_frames &&
- (st->codec->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
+ (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
got_picture = 0;
- switch (st->codec->codec_type) {
+ switch (avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO:
- ret = avcodec_decode_video2(st->codec, frame,
+ ret = avcodec_decode_video2(avctx, frame,
&got_picture, &pkt);
break;
case AVMEDIA_TYPE_AUDIO:
- ret = avcodec_decode_audio4(st->codec, frame, &got_picture, &pkt);
+ ret = avcodec_decode_audio4(avctx, frame, &got_picture, &pkt);
break;
default:
break;
@@ -2047,6 +2054,7 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
{
int i, count, ret, read_size, j;
AVStream *st;
+ AVCodecContext *avctx;
AVPacket pkt1, *pkt;
int64_t old_offset = avio_tell(ic->pb);
// new streams might appear, no options for those
@@ -2056,30 +2064,55 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
const AVCodec *codec;
AVDictionary *thread_opt = NULL;
st = ic->streams[i];
+ avctx = st->internal->avctx;
// only for the split stuff
if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
- st->parser = av_parser_init(st->codec->codec_id);
+ st->parser = av_parser_init(st->codecpar->codec_id);
if (st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser)
st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
}
+
+#if FF_API_LAVF_AVCTX
+FF_DISABLE_DEPRECATION_WARNINGS
+ /* check if the caller has overridden the codec id */
+ if (st->codec->codec_id != st->codecpar->codec_id) {
+ st->codecpar->codec_id = st->codec->codec_id;
+ st->codecpar->codec_type = st->codec->codec_type;
+ }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+ ret = avcodec_parameters_to_context(avctx, st->codecpar);
+ if (ret < 0)
+ goto find_stream_info_err;
+ if (st->codecpar->codec_id != AV_CODEC_ID_PROBE &&
+ st->codecpar->codec_id != AV_CODEC_ID_NONE)
+ st->internal->avctx_inited = 1;
+
+#if FF_API_LAVF_AVCTX
+FF_DISABLE_DEPRECATION_WARNINGS
codec = st->codec->codec ? st->codec->codec
- : avcodec_find_decoder(st->codec->codec_id);
+ :
avcodec_find_decoder(st->codecpar->codec_id);
+FF_ENABLE_DEPRECATION_WARNINGS
+#else
+ codec = avcodec_find_decoder(st->codecpar->codec_id);
+#endif
/* Force thread count to 1 since the H.264 decoder will not extract
* SPS and PPS to extradata during multi-threaded decoding. */
av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
/* Ensure that subtitle_header is properly set. */
- if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
- && codec && !st->codec->codec)
- avcodec_open2(st->codec, codec,
+ if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE
+ && codec && !avctx->codec)
+ avcodec_open2(avctx, codec,
options ? &options[i] : &thread_opt);
// Try to just open decoders, in case this is enough to get parameters.
if (!has_codec_parameters(st)) {
- if (codec && !st->codec->codec)
- avcodec_open2(st->codec, codec,
+ if (codec && !avctx->codec)
+ avcodec_open2(avctx, codec,
options ? &options[i] : &thread_opt);
}
if (!options)
@@ -2117,15 +2150,15 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
/* variable fps and no guess at the real fps */
if (!st->avg_frame_rate.num &&
st->codec_info_nb_frames < fps_analyze_framecount &&
- st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
+ st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
break;
if (st->parser && st->parser->parser->split &&
- !st->codec->extradata)
+ !st->codecpar->extradata)
break;
if (st->first_dts == AV_NOPTS_VALUE &&
st->codec_info_nb_frames < ic->max_ts_probe &&
- (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
- st->codec->codec_type == AVMEDIA_TYPE_AUDIO))
+ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
+ st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
break;
}
if (i == ic->nb_streams) {
@@ -2166,7 +2199,7 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
/* flush the decoders */
if (st->info->found_decoder == 1) {
do {
- err = try_decode_frame(st, &empty_pkt,
+ err = try_decode_frame(ic, st, &empty_pkt,
(options && i < orig_nb_streams)
? &options[i] : NULL);
} while (err > 0 && !has_codec_parameters(st));
@@ -2177,7 +2210,7 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
"decoding for stream %d failed\n", st->index);
} else if (!has_codec_parameters(st)) {
char buf[256];
- avcodec_string(buf, sizeof(buf), st->codec, 0);
+ avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
av_log(ic, AV_LOG_WARNING,
"Could not find codec parameters (%s)\n", buf);
} else {
@@ -2199,6 +2232,14 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
read_size += pkt->size;
st = ic->streams[pkt->stream_index];
+ avctx = st->internal->avctx;
+ if (!st->internal->avctx_inited) {
+ ret = avcodec_parameters_to_context(avctx, st->codecpar);
+ if (ret < 0)
+ goto find_stream_info_err;
+ st->internal->avctx_inited = 1;
+ }
+
if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
/* check for non-increasing dts */
if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
@@ -2248,16 +2289,16 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
break;
}
}
- if (st->parser && st->parser->parser->split && !st->codec->extradata) {
- int i = st->parser->parser->split(st->codec, pkt->data, pkt->size);
+ if (st->parser && st->parser->parser->split && !avctx->extradata) {
+ int i = st->parser->parser->split(avctx, pkt->data, pkt->size);
if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
- st->codec->extradata_size = i;
- st->codec->extradata = av_mallocz(st->codec->extradata_size +
-
AV_INPUT_BUFFER_PADDING_SIZE);
- if (!st->codec->extradata)
+ avctx->extradata_size = i;
+ avctx->extradata = av_mallocz(avctx->extradata_size +
+
AV_INPUT_BUFFER_PADDING_SIZE);
+ if (!avctx->extradata)
return AVERROR(ENOMEM);
- memcpy(st->codec->extradata, pkt->data,
- st->codec->extradata_size);
+ memcpy(avctx->extradata, pkt->data,
+ avctx->extradata_size);
}
}
@@ -2270,7 +2311,7 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
* least one frame of codec data, this makes sure the codec initializes
* the channel configuration and does not only trust the values from
* the container. */
- try_decode_frame(st, pkt,
+ try_decode_frame(ic, st, pkt,
(options && i < orig_nb_streams) ? &options[i] :
NULL);
if (ic->flags & AVFMT_FLAG_NOBUFFER)
@@ -2283,11 +2324,12 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
// close codecs which were opened in try_decode_frame()
for (i = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
- avcodec_close(st->codec);
+ avcodec_close(st->internal->avctx);
}
for (i = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
- if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+ avctx = st->internal->avctx;
+ if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
/* estimate average framerate if not set by demuxer */
if (!st->avg_frame_rate.num &&
st->info->fps_last_dts != st->info->fps_first_dts) {
@@ -2322,12 +2364,12 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
best_fps, 12 * 1001, INT_MAX);
}
- } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
- if (!st->codec->bits_per_coded_sample)
- st->codec->bits_per_coded_sample =
- av_get_bits_per_sample(st->codec->codec_id);
+ } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
+ if (!avctx->bits_per_coded_sample)
+ avctx->bits_per_coded_sample =
+ av_get_bits_per_sample(avctx->codec_id);
// set stream disposition based on audio service type
- switch (st->codec->audio_service_type) {
+ switch (avctx->audio_service_type) {
case AV_AUDIO_SERVICE_TYPE_EFFECTS:
st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
break;
@@ -2351,9 +2393,38 @@ int avformat_find_stream_info(AVFormatContext *ic,
AVDictionary **options)
compute_chapters_end(ic);
+ /* update the stream parameters from the internal codec contexts */
+ for (i = 0; i < ic->nb_streams; i++) {
+ st = ic->streams[i];
+ if (!st->internal->avctx_inited)
+ continue;
+
+ ret = avcodec_parameters_from_context(st->codecpar,
st->internal->avctx);
+ if (ret < 0)
+ goto find_stream_info_err;
+
+#if FF_API_LAVF_AVCTX
+FF_DISABLE_DEPRECATION_WARNINGS
+ ret = avcodec_parameters_to_context(st->codec, st->codecpar);
+ if (ret < 0)
+ goto find_stream_info_err;
+
+ if (st->internal->avctx->subtitle_header) {
+ st->codec->subtitle_header =
av_malloc(st->internal->avctx->subtitle_header_size);
+ if (!st->codec->subtitle_header)
+ goto find_stream_info_err;
+ st->codec->subtitle_header_size =
st->internal->avctx->subtitle_header_size;
+ memcpy(st->codec->subtitle_header,
st->internal->avctx->subtitle_header,
+ st->codec->subtitle_header_size);
+ }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+ st->internal->avctx_inited = 0;
+ }
+
find_stream_info_err:
for (i = 0; i < ic->nb_streams; i++) {
- ic->streams[i]->codec->thread_count = 0;
av_freep(&ic->streams[i]->info);
}
return ret;
@@ -2460,6 +2531,9 @@ static void free_stream(AVStream **pst)
if (st->attached_pic.data)
av_packet_unref(&st->attached_pic);
+ if (st->internal) {
+ avcodec_free_context(&st->internal->avctx);
+ }
av_freep(&st->internal);
av_dict_free(&st->metadata);
@@ -2578,6 +2652,10 @@ AVStream *avformat_new_stream(AVFormatContext *s, const
AVCodec *c)
if (!st->codecpar)
goto fail;
+ st->internal->avctx = avcodec_alloc_context3(NULL);
+ if (!st->internal->avctx)
+ goto fail;
+
st->index = s->nb_streams;
st->start_time = AV_NOPTS_VALUE;
st->duration = AV_NOPTS_VALUE;
--
2.0.0
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel