Use a private codec context instance for decoding.
---
 libavformat/avformat.h |   8 ++++
 libavformat/internal.h |  11 +++++
 libavformat/utils.c    | 117 ++++++++++++++++++++++++++++++++-----------------
 3 files changed, 97 insertions(+), 39 deletions(-)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index a05403c..7c6c8c9 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -682,6 +682,8 @@ typedef struct AVIndexEntry {
  */
 #define AV_DISPOSITION_ATTACHED_PIC      0x0400
 
+typedef struct AVStreamInternal AVStreamInternal;
+
 /**
  * Stream structure.
  * New fields can be added to the end with minor version bumps.
@@ -883,6 +885,12 @@ typedef struct AVStream {
                                     support seeking natively. */
     int nb_index_entries;
     unsigned int index_entries_allocated_size;
+
+    /**
+     * An opaque field for libavformat internal usage.
+     * Must not be accessed in any way by callers.
+     */
+    AVStreamInternal *internal;
 } AVStream;
 
 #define AV_PROGRAM_RUNNING 1
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 9921ce1..5b06cea 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -50,6 +50,17 @@ struct AVFormatInternal {
     int nb_interleaved_streams;
 };
 
+struct AVStreamInternal {
+    /**
+     * 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);
 
 #ifdef __GNUC__
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 2691000..43f700d 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1807,7 +1807,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) {
@@ -1831,7 +1831,7 @@ 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;
 }
 
@@ -1839,6 +1839,7 @@ static int has_decode_delay_been_guessed(AVStream *st)
 static int try_decode_frame(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();
@@ -1847,11 +1848,11 @@ 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;
 
-        codec = st->codec->codec ? st->codec->codec
-                                 : avcodec_find_decoder(st->codec->codec_id);
+        codec = avctx->codec ? avctx->codec :
+                               avcodec_find_decoder(avctx->codec_id);
 
         if (!codec) {
             st->info->found_decoder = -1;
@@ -1862,7 +1863,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) {
@@ -1882,15 +1883,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 & CODEC_CAP_CHANNEL_CONF))) {
+             avctx->codec->capabilities & 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;
@@ -2032,6 +2033,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
@@ -2041,30 +2043,37 @@ 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;
         }
-        codec = st->codec->codec ? st->codec->codec
-                                 : avcodec_find_decoder(st->codec->codec_id);
+
+        ret = avcodec_parameters_to_context(avctx, st->codecpar);
+        if (ret < 0)
+            goto find_stream_info_err;
+        st->internal->avctx_inited = 1;
+
+        codec = avctx->codec ? avctx->codec :
+                               avcodec_find_decoder(st->codecpar->codec_id);
 
         /* 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)
@@ -2102,14 +2111,14 @@ 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->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) {
@@ -2161,7 +2170,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 {
@@ -2183,6 +2192,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 &&
@@ -2230,16 +2247,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 +
-                                                  
FF_INPUT_BUFFER_PADDING_SIZE);
-                if (!st->codec->extradata)
+                avctx->extradata_size = i;
+                avctx->extradata      = av_mallocz(avctx->extradata_size +
+                                                   
FF_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);
             }
         }
 
@@ -2262,11 +2279,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) {
@@ -2301,12 +2319,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;
@@ -2330,9 +2348,17 @@ 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];
+        ret = avcodec_parameters_from_context(st->codecpar, 
st->internal->avctx);
+        if (ret < 0)
+            goto find_stream_info_err;
+        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;
@@ -2439,6 +2465,11 @@ static void free_stream(AVStream **pst)
     if (st->attached_pic.data)
         av_free_packet(&st->attached_pic);
 
+    if (st->internal) {
+        avcodec_free_context(&st->internal->avctx);
+    }
+    av_freep(&st->internal);
+
     av_dict_free(&st->metadata);
     avcodec_parameters_free(&st->codecpar);
     av_freep(&st->probe_data.buf);
@@ -2535,6 +2566,14 @@ AVStream *avformat_new_stream(AVFormatContext *s, const 
AVCodec *c)
     if (!st->codecpar)
         goto fail;
 
+    st->internal = av_mallocz(sizeof(*st->internal));
+    if (!st->internal)
+        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

Reply via email to