diff mbox series

[FFmpeg-devel,1/2] avformat/dv: add avpriv_dv_close_demux

Message ID 20210430180701.63566-1-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/2] avformat/dv: add avpriv_dv_close_demux | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

James Almer April 30, 2021, 6:07 p.m. UTC
This is in preparation for the next commit.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavdevice/iec61883.c |  2 +-
 libavformat/avidec.c   |  2 +-
 libavformat/dv.c       | 56 ++++++++++++++++++++++++++++--------------
 libavformat/dv.h       |  1 +
 libavformat/mov.c      |  2 +-
 5 files changed, 41 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/libavdevice/iec61883.c b/libavdevice/iec61883.c
index e7b71c757a..18ad704066 100644
--- a/libavdevice/iec61883.c
+++ b/libavdevice/iec61883.c
@@ -463,7 +463,7 @@  static int iec61883_close(AVFormatContext *context)
     } else {
         iec61883_dv_fb_stop(dv->iec61883_dv);
         iec61883_dv_fb_close(dv->iec61883_dv);
-        av_freep(&dv->dv_demux);
+        avpriv_dv_close_demux(&dv->dv_demux);
     }
     while (dv->queue_first) {
         DVPacket *packet = dv->queue_first;
diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index d5dcab6193..2d0d2a7389 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -1951,7 +1951,7 @@  static int avi_read_close(AVFormatContext *s)
         }
     }
 
-    av_freep(&avi->dv_demux);
+    avpriv_dv_close_demux(&avi->dv_demux);
 
     return 0;
 }
diff --git a/libavformat/dv.c b/libavformat/dv.c
index cbb38cbd7d..a948fc0b98 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -318,7 +318,17 @@  static int dv_extract_timecode(DVDemuxContext* c, const uint8_t* frame, char *tc
     return 1;
 }
 
-/* The following 3 functions constitute our interface to the world */
+/* The following 4 functions constitute our interface to the world */
+
+void avpriv_dv_close_demux(DVDemuxContext **pc)
+{
+    DVDemuxContext *c = *pc;
+
+    if (!c)
+        return;
+
+    av_freep(pc);
+}
 
 static int dv_init_demux(AVFormatContext *s, DVDemuxContext *c)
 {
@@ -343,10 +353,8 @@  DVDemuxContext *avpriv_dv_init_demux(AVFormatContext *s)
     if (!c)
         return NULL;
 
-    if (dv_init_demux(s, c)) {
-        av_free(c);
-        return NULL;
-    }
+    if (dv_init_demux(s, c))
+        avpriv_dv_close_demux(&c);
 
     return c;
 }
@@ -448,7 +456,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;
 
@@ -471,7 +479,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
@@ -486,10 +494,10 @@  static int dv_read_header(AVFormatContext *s)
 {
     unsigned state, marker_pos = 0;
     RawDVContext *c = s->priv_data;
-    int ret;
 
-    if ((ret = dv_init_demux(s, &c->dv_demux)) < 0)
-        return ret;
+    c->dv_demux = avpriv_dv_init_demux(s);
+    if (!c->dv_demux)
+        return AVERROR(ENOMEM);
 
     state = avio_rb32(s->pb);
     while ((state & 0xffffff7f) != 0x1f07003f) {
@@ -513,18 +521,18 @@  static int dv_read_header(AVFormatContext *s)
         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");
         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);
@@ -537,14 +545,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;
@@ -552,17 +560,26 @@  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;
 }
 
+static int dv_read_close(AVFormatContext *s)
+{
+    RawDVContext *r   = s->priv_data;
+
+    avpriv_dv_close_demux(&r->dv_demux);
+
+    return 0;
+}
+
 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)
@@ -621,6 +638,7 @@  const 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",
 };
diff --git a/libavformat/dv.h b/libavformat/dv.h
index 160c6ab873..c441a23156 100644
--- a/libavformat/dv.h
+++ b/libavformat/dv.h
@@ -32,6 +32,7 @@ 
 
 typedef struct DVDemuxContext DVDemuxContext;
 DVDemuxContext* avpriv_dv_init_demux(AVFormatContext* s);
+void avpriv_dv_close_demux(DVDemuxContext **c);
 int avpriv_dv_get_packet(DVDemuxContext*, AVPacket *);
 int avpriv_dv_produce_packet(DVDemuxContext*, AVPacket*, uint8_t*, int, int64_t);
 void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset);
diff --git a/libavformat/mov.c b/libavformat/mov.c
index ca8f06c4cd..d2d34d574f 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7385,7 +7385,7 @@  static int mov_read_close(AVFormatContext *s)
         av_freep(&sc->coll);
     }
 
-    av_freep(&mov->dv_demux);
+    avpriv_dv_close_demux(&mov->dv_demux);
     avformat_free_context(mov->dv_fctx);
     mov->dv_fctx = NULL;