[FFmpeg-devel] [PATCH 1/2] avformat/dv: Avoid alloction of DVDemuxContext

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Sun Aug 2 14:47:30 EEST 2020


This commit avoids allocating a DVDemuxContext when demuxing raw DV by
making it part of the demuxer's context. This also allows to remove
dv_read_close().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavformat/dv.c | 73 +++++++++++++++++++++---------------------------
 1 file changed, 32 insertions(+), 41 deletions(-)

diff --git a/libavformat/dv.c b/libavformat/dv.c
index e99422d4b5..9179e6cec6 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -321,6 +321,21 @@ static int dv_extract_timecode(DVDemuxContext* c, const uint8_t* frame, char *tc
 
 /* The following 3 functions constitute our interface to the world */
 
+static int dv_init_demux(AVFormatContext *s, DVDemuxContext *c)
+{
+    c->vst = avformat_new_stream(s, NULL);
+    if (!c->vst)
+        return AVERROR(ENOMEM);
+
+    c->fctx                   = s;
+    c->vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+    c->vst->codecpar->codec_id   = AV_CODEC_ID_DVVIDEO;
+    c->vst->codecpar->bit_rate   = 25000000;
+    c->vst->start_time        = 0;
+
+    return 0;
+}
+
 DVDemuxContext *avpriv_dv_init_demux(AVFormatContext *s)
 {
     DVDemuxContext *c;
@@ -329,18 +344,11 @@ DVDemuxContext *avpriv_dv_init_demux(AVFormatContext *s)
     if (!c)
         return NULL;
 
-    c->vst = avformat_new_stream(s, NULL);
-    if (!c->vst) {
+    if (dv_init_demux(s, c)) {
         av_free(c);
         return NULL;
     }
 
-    c->fctx                   = s;
-    c->vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
-    c->vst->codecpar->codec_id   = AV_CODEC_ID_DVVIDEO;
-    c->vst->codecpar->bit_rate   = 25000000;
-    c->vst->start_time        = 0;
-
     return c;
 }
 
@@ -452,7 +460,7 @@ void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
  ************************************************************/
 
 typedef struct RawDVContext {
-    DVDemuxContext *dv_demux;
+    DVDemuxContext  dv_demux;
     uint8_t         buf[DV_MAX_FRAME_SIZE];
 } RawDVContext;
 
@@ -479,7 +487,7 @@ static int dv_read_timecode(AVFormatContext *s) {
         goto finish;
     }
 
-    ret = dv_extract_timecode(c->dv_demux, partial_frame, timecode);
+    ret = dv_extract_timecode(&c->dv_demux, partial_frame, timecode);
     if (ret)
         av_dict_set(&s->metadata, "timecode", timecode, 0);
     else
@@ -497,16 +505,14 @@ static int dv_read_header(AVFormatContext *s)
     RawDVContext *c = s->priv_data;
     int ret;
 
-    c->dv_demux = avpriv_dv_init_demux(s);
-    if (!c->dv_demux)
-        return AVERROR(ENOMEM);
+    if ((ret = dv_init_demux(s, &c->dv_demux)) < 0)
+        return ret;
 
     state = avio_rb32(s->pb);
     while ((state & 0xffffff7f) != 0x1f07003f) {
         if (avio_feof(s->pb)) {
             av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n");
-            ret = AVERROR_INVALIDDATA;
-            goto fail;
+            return AVERROR_INVALIDDATA;
         }
         if (state == 0x003f0700 || state == 0xff3f0700)
             marker_pos = avio_tell(s->pb);
@@ -521,33 +527,26 @@ static int dv_read_header(AVFormatContext *s)
 
     if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) != DV_PROFILE_BYTES - 4 ||
         avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0) {
-        ret = AVERROR(EIO);
-        goto fail;
+        return AVERROR(EIO);
     }
 
-    c->dv_demux->sys = av_dv_frame_profile(c->dv_demux->sys,
+    c->dv_demux.sys = av_dv_frame_profile(c->dv_demux.sys,
                                            c->buf,
                                            DV_PROFILE_BYTES);
-    if (!c->dv_demux->sys) {
+    if (!c->dv_demux.sys) {
         av_log(s, AV_LOG_ERROR,
                "Can't determine profile of DV input stream.\n");
-        ret = AVERROR_INVALIDDATA;
-        goto fail;
+        return AVERROR_INVALIDDATA;
     }
 
-    s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size,
+    s->bit_rate = av_rescale_q(c->dv_demux.sys->frame_size,
                                (AVRational) { 8, 1 },
-                               c->dv_demux->sys->time_base);
+                               c->dv_demux.sys->time_base);
 
     if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
         dv_read_timecode(s);
 
     return 0;
-
-fail:
-    av_freep(&c->dv_demux);
-
-    return ret;
 }
 
 static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
@@ -555,14 +554,14 @@ static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
     int size;
     RawDVContext *c = s->priv_data;
 
-    size = avpriv_dv_get_packet(c->dv_demux, pkt);
+    size = avpriv_dv_get_packet(&c->dv_demux, pkt);
 
     if (size < 0) {
         int ret;
         int64_t pos = avio_tell(s->pb);
-        if (!c->dv_demux->sys)
+        if (!c->dv_demux.sys)
             return AVERROR(EIO);
-        size = c->dv_demux->sys->frame_size;
+        size = c->dv_demux.sys->frame_size;
         ret = avio_read(s->pb, c->buf, size);
         if (ret < 0) {
             return ret;
@@ -570,7 +569,7 @@ static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
             return AVERROR(EIO);
         }
 
-        size = avpriv_dv_produce_packet(c->dv_demux, pkt, c->buf, size, pos);
+        size = avpriv_dv_produce_packet(&c->dv_demux, pkt, c->buf, size, pos);
     }
 
     return size;
@@ -580,7 +579,7 @@ static int dv_read_seek(AVFormatContext *s, int stream_index,
                         int64_t timestamp, int flags)
 {
     RawDVContext *r   = s->priv_data;
-    DVDemuxContext *c = r->dv_demux;
+    DVDemuxContext *c = &r->dv_demux;
     int64_t offset    = dv_frame_offset(s, c, timestamp, flags);
 
     if (avio_seek(s->pb, offset, SEEK_SET) < 0)
@@ -590,13 +589,6 @@ static int dv_read_seek(AVFormatContext *s, int stream_index,
     return 0;
 }
 
-static int dv_read_close(AVFormatContext *s)
-{
-    RawDVContext *c = s->priv_data;
-    av_freep(&c->dv_demux);
-    return 0;
-}
-
 static int dv_probe(const AVProbeData *p)
 {
     unsigned marker_pos = 0;
@@ -646,7 +638,6 @@ AVInputFormat ff_dv_demuxer = {
     .read_probe     = dv_probe,
     .read_header    = dv_read_header,
     .read_packet    = dv_read_packet,
-    .read_close     = dv_read_close,
     .read_seek      = dv_read_seek,
     .extensions     = "dv,dif",
 };
-- 
2.20.1



More information about the ffmpeg-devel mailing list