[FFmpeg-devel] [PATCH 22/44] avformat/utils: Move ff_add_attached_pic to demux_utils.c

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat May 7 14:28:08 EEST 2022


It is demuxer-only: It potentially adds an AVStream and it sets
AVStream.attached_pic.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavformat/apetag.c       |  1 +
 libavformat/asf.c          |  1 +
 libavformat/demux.h        | 16 ++++++++++++++++
 libavformat/demux_utils.c  | 36 ++++++++++++++++++++++++++++++++++++
 libavformat/flac_picture.c |  1 +
 libavformat/internal.h     | 16 ----------------
 libavformat/utils.c        | 35 -----------------------------------
 7 files changed, 55 insertions(+), 51 deletions(-)

diff --git a/libavformat/apetag.c b/libavformat/apetag.c
index cc6becc182..e861aac0f8 100644
--- a/libavformat/apetag.c
+++ b/libavformat/apetag.c
@@ -26,6 +26,7 @@
 #include "avformat.h"
 #include "avio_internal.h"
 #include "apetag.h"
+#include "demux.h"
 #include "internal.h"
 #include "mux.h"
 
diff --git a/libavformat/asf.c b/libavformat/asf.c
index 1ac8b5f078..6e3854eceb 100644
--- a/libavformat/asf.c
+++ b/libavformat/asf.c
@@ -19,6 +19,7 @@
  */
 
 #include "asf.h"
+#include "demux.h"
 #include "id3v2.h"
 #include "internal.h"
 
diff --git a/libavformat/demux.h b/libavformat/demux.h
index d857e4dafb..546569ae6a 100644
--- a/libavformat/demux.h
+++ b/libavformat/demux.h
@@ -187,4 +187,20 @@ void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type);
 AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base,
                               int64_t start, int64_t end, const char *title);
 
+/**
+ * Add an attached pic to an AVStream.
+ *
+ * @param st   if set, the stream to add the attached pic to;
+ *             if unset, a new stream will be added to s.
+ * @param pb   AVIOContext to read data from if buf is unset.
+ * @param buf  if set, it contains the data and size information to be used
+ *             for the attached pic; if unset, data is read from pb.
+ * @param size the size of the data to read if buf is unset.
+ *
+ * @return 0 on success, < 0 on error. On error, this function removes
+ *         the stream it has added (if any).
+ */
+int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
+                        AVBufferRef **buf, int size);
+
 #endif /* AVFORMAT_DEMUX_H */
diff --git a/libavformat/demux_utils.c b/libavformat/demux_utils.c
index ca5750d1ef..1fd634d1a2 100644
--- a/libavformat/demux_utils.c
+++ b/libavformat/demux_utils.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/avassert.h"
 #include "libavcodec/packet_internal.h"
 #include "avformat.h"
 #include "demux.h"
@@ -107,3 +108,38 @@ int avformat_queue_attached_pictures(AVFormatContext *s)
         }
     return 0;
 }
+
+int ff_add_attached_pic(AVFormatContext *s, AVStream *st0, AVIOContext *pb,
+                        AVBufferRef **buf, int size)
+{
+    AVStream *st = st0;
+    AVPacket *pkt;
+    int ret;
+
+    if (!st && !(st = avformat_new_stream(s, NULL)))
+        return AVERROR(ENOMEM);
+    pkt = &st->attached_pic;
+    if (buf) {
+        av_assert1(*buf);
+        av_packet_unref(pkt);
+        pkt->buf  = *buf;
+        pkt->data = (*buf)->data;
+        pkt->size = (*buf)->size - AV_INPUT_BUFFER_PADDING_SIZE;
+        *buf = NULL;
+    } else {
+        ret = av_get_packet(pb, pkt, size);
+        if (ret < 0)
+            goto fail;
+    }
+    st->disposition         |= AV_DISPOSITION_ATTACHED_PIC;
+    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+
+    pkt->stream_index = st->index;
+    pkt->flags       |= AV_PKT_FLAG_KEY;
+
+    return 0;
+fail:
+    if (!st0)
+        ff_remove_stream(s, st);
+    return ret;
+}
diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c
index 0effbf04f6..b33fee75b4 100644
--- a/libavformat/flac_picture.c
+++ b/libavformat/flac_picture.c
@@ -23,6 +23,7 @@
 #include "libavcodec/bytestream.h"
 #include "libavcodec/png.h"
 #include "avformat.h"
+#include "demux.h"
 #include "flac_picture.h"
 #include "id3v2.h"
 #include "internal.h"
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 1a51c0dc97..4d80501eac 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -598,22 +598,6 @@ int ff_add_param_change(AVPacket *pkt, int32_t channels,
  */
 int ff_framehash_write_header(AVFormatContext *s);
 
-/**
- * Add an attached pic to an AVStream.
- *
- * @param st   if set, the stream to add the attached pic to;
- *             if unset, a new stream will be added to s.
- * @param pb   AVIOContext to read data from if buf is unset.
- * @param buf  if set, it contains the data and size information to be used
- *             for the attached pic; if unset, data is read from pb.
- * @param size the size of the data to read if buf is unset.
- *
- * @return 0 on success, < 0 on error. On error, this function removes
- *         the stream it has added (if any).
- */
-int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
-                        AVBufferRef **buf, int size);
-
 /**
  * Frees a stream without modifying the corresponding AVFormatContext.
  * Must only be called if the latter doesn't matter or if the stream
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 492fca000a..9e2e3335ac 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -175,41 +175,6 @@ int av_filename_number_test(const char *filename)
            (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
 }
 
-int ff_add_attached_pic(AVFormatContext *s, AVStream *st0, AVIOContext *pb,
-                        AVBufferRef **buf, int size)
-{
-    AVStream *st = st0;
-    AVPacket *pkt;
-    int ret;
-
-    if (!st && !(st = avformat_new_stream(s, NULL)))
-        return AVERROR(ENOMEM);
-    pkt = &st->attached_pic;
-    if (buf) {
-        av_assert1(*buf);
-        av_packet_unref(pkt);
-        pkt->buf  = *buf;
-        pkt->data = (*buf)->data;
-        pkt->size = (*buf)->size - AV_INPUT_BUFFER_PADDING_SIZE;
-        *buf = NULL;
-    } else {
-        ret = av_get_packet(pb, pkt, size);
-        if (ret < 0)
-            goto fail;
-    }
-    st->disposition         |= AV_DISPOSITION_ATTACHED_PIC;
-    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
-
-    pkt->stream_index = st->index;
-    pkt->flags       |= AV_PKT_FLAG_KEY;
-
-    return 0;
-fail:
-    if (!st0)
-        ff_remove_stream(s, st);
-    return ret;
-}
-
 /**********************************************************/
 
 int ff_is_intra_only(enum AVCodecID id)
-- 
2.32.0



More information about the ffmpeg-devel mailing list