[FFmpeg-cvslog] lavf: use the new bitstream filter for extracting extradata

Anton Khirnov git at videolan.org
Thu Mar 23 22:23:47 EET 2017


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Thu Sep 22 15:24:34 2016 +0200| [8e2ea691351c5079cdab245ff7bfa5c0f3e3bfe4] | committer: Anton Khirnov

lavf: use the new bitstream filter for extracting extradata

This also fixes a minor bug introduced in the codecpar conversion, where
the termination condition for extracting the extradata does not match
the actual extradata setting code. As a result, the packet durations
made up by lavf go back to their values before the codecpar conversion.
That is of little consequence since that code should eventually be
dropped completely.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8e2ea691351c5079cdab245ff7bfa5c0f3e3bfe4
---

 libavformat/internal.h   |   9 ++++
 libavformat/utils.c      | 123 ++++++++++++++++++++++++++++++++++++++++++-----
 tests/ref/fate/iv8-demux |  10 ++--
 tests/ref/fate/wtv-demux |  68 +++++++++++++-------------
 4 files changed, 158 insertions(+), 52 deletions(-)

diff --git a/libavformat/internal.h b/libavformat/internal.h
index de55af5..52cd29b 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -113,6 +113,15 @@ struct AVStreamInternal {
 
     enum AVCodecID orig_codec_id;
 
+    /* the context for extracting extradata in find_stream_info()
+     * inited=1/bsf=NULL signals that extracting is not possible (codec not
+     * supported) */
+    struct {
+        AVBSFContext *bsf;
+        AVPacket     *pkt;
+        int inited;
+    } extract_extradata;
+
 #if FF_API_LAVF_AVCTX
     // whether the deprecated stream codec context needs
     // to be filled from the codec parameters
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 0e94b15..1c93326 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2089,6 +2089,104 @@ static int get_std_framerate(int i)
         return ((const int[]) { 24, 30, 60, 12, 15 })[i - 60 * 12] * 1000 * 12;
 }
 
+static int extract_extradata_init(AVStream *st)
+{
+    AVStreamInternal *i = st->internal;
+    const AVBitStreamFilter *f;
+    int ret;
+
+    f = av_bsf_get_by_name("extract_extradata");
+    if (!f)
+        goto finish;
+
+    i->extract_extradata.pkt = av_packet_alloc();
+    if (!i->extract_extradata.pkt)
+        return AVERROR(ENOMEM);
+
+    ret = av_bsf_alloc(f, &i->extract_extradata.bsf);
+    if (ret < 0)
+        goto fail;
+
+    ret = avcodec_parameters_copy(i->extract_extradata.bsf->par_in,
+                                  st->codecpar);
+    if (ret < 0)
+        goto fail;
+
+    i->extract_extradata.bsf->time_base_in = st->time_base;
+
+    /* if init fails here, we assume extracting extradata is just not
+     * supported for this codec, so we return success */
+    ret = av_bsf_init(i->extract_extradata.bsf);
+    if (ret < 0) {
+        av_bsf_free(&i->extract_extradata.bsf);
+        ret = 0;
+    }
+
+finish:
+    i->extract_extradata.inited = 1;
+
+    return 0;
+fail:
+    av_bsf_free(&i->extract_extradata.bsf);
+    av_packet_free(&i->extract_extradata.pkt);
+    return ret;
+}
+
+static int extract_extradata(AVStream *st, AVPacket *pkt)
+{
+    AVStreamInternal *i = st->internal;
+    AVPacket *pkt_ref;
+    int ret;
+
+    if (!i->extract_extradata.inited) {
+        ret = extract_extradata_init(st);
+        if (ret < 0)
+            return ret;
+    }
+
+    if (i->extract_extradata.inited && !i->extract_extradata.bsf)
+        return 0;
+
+    pkt_ref = i->extract_extradata.pkt;
+    ret = av_packet_ref(pkt_ref, pkt);
+    if (ret < 0)
+        return ret;
+
+    ret = av_bsf_send_packet(i->extract_extradata.bsf, pkt_ref);
+    if (ret < 0) {
+        av_packet_unref(pkt_ref);
+        return ret;
+    }
+
+    while (ret >= 0 && !i->avctx->extradata) {
+        int extradata_size;
+        uint8_t *extradata;
+
+        ret = av_bsf_receive_packet(i->extract_extradata.bsf, pkt_ref);
+        if (ret < 0) {
+            if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
+                return ret;
+            continue;
+        }
+
+        extradata = av_packet_get_side_data(pkt_ref, AV_PKT_DATA_NEW_EXTRADATA,
+                                            &extradata_size);
+
+        if (extradata) {
+            i->avctx->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!i->avctx->extradata) {
+                av_packet_unref(pkt_ref);
+                return AVERROR(ENOMEM);
+            }
+            memcpy(i->avctx->extradata, extradata, extradata_size);
+            i->avctx->extradata_size = extradata_size;
+        }
+        av_packet_unref(pkt_ref);
+    }
+
+    return 0;
+}
+
 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
 {
     int i, count, ret, read_size, j;
@@ -2194,8 +2292,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 st->codec_info_nb_frames < fps_analyze_framecount &&
                 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
                 break;
-            if (st->parser && st->parser->parser->split &&
-                !st->codecpar->extradata)
+            if (!st->codecpar->extradata &&
+                !st->internal->avctx->extradata &&
+                (!st->internal->extract_extradata.inited ||
+                 st->internal->extract_extradata.bsf))
                 break;
             if (st->first_dts == AV_NOPTS_VALUE &&
                 st->codec_info_nb_frames < ic->max_ts_probe &&
@@ -2331,17 +2431,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 break;
             }
         }
-        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) {
-                avctx->extradata_size = i;
-                avctx->extradata      = av_mallocz(avctx->extradata_size +
-                                                   AV_INPUT_BUFFER_PADDING_SIZE);
-                if (!avctx->extradata)
-                    return AVERROR(ENOMEM);
-                memcpy(avctx->extradata, pkt->data,
-                       avctx->extradata_size);
-            }
+        if (!st->internal->avctx->extradata) {
+            ret = extract_extradata(st, pkt);
+            if (ret < 0)
+                goto find_stream_info_err;
         }
 
         /* If still no information, we try to open the codec and to
@@ -2468,6 +2561,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
 find_stream_info_err:
     for (i = 0; i < ic->nb_streams; i++) {
         av_freep(&ic->streams[i]->info);
+        av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
+        av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);
     }
     return ret;
 }
@@ -2575,6 +2670,8 @@ static void free_stream(AVStream **pst)
 
     if (st->internal) {
         avcodec_free_context(&st->internal->avctx);
+        av_bsf_free(&st->internal->extract_extradata.bsf);
+        av_packet_free(&st->internal->extract_extradata.pkt);
     }
     av_freep(&st->internal);
 
diff --git a/tests/ref/fate/iv8-demux b/tests/ref/fate/iv8-demux
index f13e691..56a54a1 100644
--- a/tests/ref/fate/iv8-demux
+++ b/tests/ref/fate/iv8-demux
@@ -19,8 +19,8 @@
 0,      57600,      61200,        0,    20874, 0xed0b91ec
 0,      61200,      64799,        0,    20877, 0xe1623e01
 0,      64799,      68399,        0,    20933, 0x19906564
-0,      68399,      72000,        0,    20891, 0x3d064fd3
-0,      72000,      75600,        0,    20834, 0xcb774dbc
-0,      75600,      79200,        0,    20870, 0xbc536589
-0,      79200,      82800,        0,    21421, 0xc99a68e4
-0,      82800,      86400,        0,    12869, 0x5684e304
+0,      68399,      72000,     3600,    20891, 0x3d064fd3
+0,      72000,      75600,     3600,    20834, 0xcb774dbc
+0,      75600,      79200,     3600,    20870, 0xbc536589
+0,      79200,      82800,     3600,    21421, 0xc99a68e4
+0,      82800,      86400,     3600,    12869, 0x5684e304
diff --git a/tests/ref/fate/wtv-demux b/tests/ref/fate/wtv-demux
index ae44958..bdd6c20 100644
--- a/tests/ref/fate/wtv-demux
+++ b/tests/ref/fate/wtv-demux
@@ -59,83 +59,83 @@
 0,   11486331,   11486331,   400000,     6156, 0xe168394b
 1,   11519998,   11519998,   240000,      576, 0x1fea1448
 1,   11759998,   11759998,   240000,      576, 0x55840a01
-0,   11886331,   13086442,   400000,    23364, 0x53164f1e
+0,   11886331,   13086442,   449438,    23364, 0x53164f1e
 1,   11999998,   11999998,   240000,      576, 0x6c9c24ce
 1,   12239998,   12239998,   240000,      576, 0x955f1e97
-0,   12286442,   12286442,   400000,     6708, 0x89877269
+0,   12286442,   12286442,   449438,     6708, 0x89877269
 1,   12479998,   12479998,   240000,      576, 0x2827134f
-0,   12686442,   12686442,   400000,     6908, 0x8d62a249
+0,   12686442,   12686442,   449438,     6908, 0x8d62a249
 1,   12719998,   12719998,   240000,      576, 0x34a01c29
 1,   12959998,   12959998,   240000,      576, 0x7d351e52
-0,   13086442,   14286442,   400000,    38156, 0xec41f682
+0,   13086442,   14286442,   449438,    38156, 0xec41f682
 1,   13199998,   13199998,   240000,      576, 0x00c91d9e
 1,   13439998,   13439998,   240000,      576, 0x57ea1a97
-0,   13486331,   13486331,   400000,     5764, 0xcc04534b
+0,   13486331,   13486331,   449438,     5764, 0xcc04534b
 1,   13679998,   13679998,   240000,      576, 0xef3a1c74
-0,   13886331,   13886331,   400000,     5388, 0xb8a1c3c5
+0,   13886331,   13886331,   449438,     5388, 0xb8a1c3c5
 1,   13919998,   13919998,   240000,      576, 0x11fc217d
 1,   14159998,   14159998,   240000,      576, 0x59ce20e5
-0,   14286442,   15486331,   400000,    16764, 0x59460d96
+0,   14286442,   15486331,   449438,    16764, 0x59460d96
 1,   14399998,   14399998,   240000,      576, 0xaafc1dbf
 1,   14639998,   14639998,   240000,      576, 0xdd941609
-0,   14686331,   14686331,   400000,     5548, 0x5c91e93d
+0,   14686331,   14686331,   449438,     5548, 0x5c91e93d
 1,   14879998,   14879998,   240000,      576, 0x900420b0
-0,   15086331,   15086331,   400000,     5652, 0x5e321aed
+0,   15086331,   15086331,   449438,     5652, 0x5e321aed
 1,   15119998,   15119998,   240000,      576, 0x5f4f1aa1
 1,   15359998,   15359998,   240000,      576, 0x7d7e18de
-0,   15486331,   16686331,   400000,    15564, 0xefdf5080
+0,   15486331,   16686331,   449438,    15564, 0xefdf5080
 1,   15599998,   15599998,   240000,      576, 0x986c0d9d
 1,   15839998,   15839998,   240000,      576, 0xcb4c21c0
-0,   15886331,   15886331,   400000,     6492, 0xd1d5c5f8
+0,   15886331,   15886331,   449438,     6492, 0xd1d5c5f8
 1,   16079998,   16079998,   240000,      576, 0xbcfb1e8b
-0,   16286331,   16286331,   400000,     5604, 0xf9472b44
+0,   16286331,   16286331,   449438,     5604, 0xf9472b44
 1,   16319998,   16319998,   240000,      576, 0xcb541b4c
 1,   16559998,   16559998,   240000,      576, 0x980426e9
-0,   16686331,   17886331,   400000,    17924, 0x45815b7b
+0,   16686331,   17886331,   449438,    17924, 0x45815b7b
 1,   16799998,   16799998,   240000,      576, 0x09d00aa0
 1,   17039998,   17039998,   240000,      576, 0xad591374
-0,   17086442,   17086442,   400000,     5020, 0x3cc5e554
+0,   17086442,   17086442,   449438,     5020, 0x3cc5e554
 1,   17279998,   17279998,   240000,      576, 0x97bf1461
-0,   17486442,   17486442,   400000,     5276, 0xa0554c12
+0,   17486442,   17486442,   449438,     5276, 0xa0554c12
 1,   17519998,   17519998,   240000,      576, 0xdc871cc4
 1,   17759998,   17759998,   240000,      576, 0x56781896
-0,   17886331,   19086442,   400000,    31460, 0x5765eb5f
+0,   17886331,   19086442,   449438,    31460, 0x5765eb5f
 1,   17999998,   17999998,   240000,      576, 0xc77714e3
 1,   18239998,   18239998,   240000,      576, 0x280e18d4
-0,   18286331,   18286331,   400000,     4972, 0x91adbab7
+0,   18286331,   18286331,   449438,     4972, 0x91adbab7
 1,   18479998,   18479998,   240000,      576, 0xbc0d2302
-0,   18686442,   18686442,   400000,     5580, 0xfea707cb
+0,   18686442,   18686442,   449438,     5580, 0xfea707cb
 1,   18719998,   18719998,   240000,      576, 0x79191384
 1,   18959998,   18959998,   240000,      576, 0x65481c97
-0,   19086442,   20286331,   400000,    17412, 0x0afe4d27
+0,   19086442,   20286331,   449438,    17412, 0x0afe4d27
 1,   19199998,   19199998,   240000,      576, 0xc94d227d
 1,   19439998,   19439998,   240000,      576, 0xa68a1f14
-0,   19486442,   19486442,   400000,     5236, 0x03f55309
+0,   19486442,   19486442,   449438,     5236, 0x03f55309
 1,   19679998,   19679998,   240000,      576, 0x6af11a5c
-0,   19886331,   19886331,   400000,     4924, 0x558e753c
+0,   19886331,   19886331,   449438,     4924, 0x558e753c
 1,   19919998,   19919998,   240000,      576, 0x4d1019ef
 1,   20159998,   20159998,   240000,      576, 0x3b1b17b5
-0,   20286331,   21486331,   400000,    15396, 0xf145d121
+0,   20286331,   21486331,   449438,    15396, 0xf145d121
 1,   20399998,   20399998,   240000,      576, 0xcdd8159f
 1,   20639998,   20639998,   240000,      576, 0x97cd1d06
-0,   20686331,   20686331,   400000,     4708, 0x43066a92
+0,   20686331,   20686331,   449438,     4708, 0x43066a92
 1,   20879998,   20879998,   240000,      576, 0x5d1b1123
-0,   21086442,   21086442,   400000,     4332, 0x9e22bcba
+0,   21086442,   21086442,   449438,     4332, 0x9e22bcba
 1,   21119998,   21119998,   240000,      576, 0x888d0cb0
 1,   21359998,   21359998,   240000,      576, 0x556e1dad
-0,   21486331,   22686442,   400000,    12876, 0x46ff9ef4
+0,   21486331,   22686442,   449438,    12876, 0x46ff9ef4
 1,   21599998,   21599998,   240000,      576, 0xf7af0bce
 1,   21839998,   21839998,   240000,      576, 0xb5da160a
-0,   21886442,   21886442,   400000,     5940, 0x27cba62e
+0,   21886442,   21886442,   449438,     5940, 0x27cba62e
 1,   22079998,   22079998,   240000,      576, 0x4a8d0e98
-0,   22286442,   22286442,   400000,     6124, 0x6bab0a6d
+0,   22286442,   22286442,   449438,     6124, 0x6bab0a6d
 1,   22319998,   22319998,   240000,      576, 0x183b1c7e
 1,   22559998,   22559998,   240000,      576, 0xc47120e6
-0,   22686442,   23886442,   400000,    36428, 0x942f9648
+0,   22686442,   23886442,   449438,    36428, 0x942f9648
 1,   22799998,   22799998,   240000,      576, 0xb1f31346
-0,   23086331,   23086331,   400000,     6660, 0x545a0db7
-0,   23486331,   23486331,   400000,     6780, 0x2d1d4189
-0,   23886442,   25086331,   400000,    16460, 0x7c3b3ca4
-0,   24286442,   24286442,   400000,     6724, 0x8538cc6f
-0,   24686442,   24686442,   400000,     7068, 0x69574fd0
-0,   25086331,   26286331,   400000,    19552, 0xf230e854
+0,   23086331,   23086331,   449438,     6660, 0x545a0db7
+0,   23486331,   23486331,   449438,     6780, 0x2d1d4189
+0,   23886442,   25086331,   449438,    16460, 0x7c3b3ca4
+0,   24286442,   24286442,   449438,     6724, 0x8538cc6f
+0,   24686442,   24686442,   449438,     7068, 0x69574fd0
+0,   25086331,   26286331,   449438,    19552, 0xf230e854



More information about the ffmpeg-cvslog mailing list