[FFmpeg-devel] [PATCH 3/3 v2] avformat: add apic to AVStream

James Almer jamrial at gmail.com
Fri Apr 2 02:27:38 EEST 2021


Signed-off-by: James Almer <jamrial at gmail.com>
---
Now allocated only on attached_pic disposition streams.

Added muxer usage doxy for the new field as well since Andreas has a patchset
meant for this purpose. But i can make it say unused instead and let the
aforementioned patchset change it as its own API change.

Still using apic since i want other people's opinion on what the name should
be, since Lynne didn't like it.
Another option is simply picture. I don't want it to be too long.

 libavformat/avformat.h | 24 ++++++++++++++++++------
 libavformat/hls.c      |  6 +++---
 libavformat/id3v2.c    |  2 +-
 libavformat/mov.c      |  4 ++--
 libavformat/utils.c    | 27 ++++++++++++++++++++++-----
 libavformat/version.h  |  3 +++
 6 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 11e993eacb..6a9b09160c 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -838,7 +838,7 @@ typedef struct AVIndexEntry {
  * APIC frame in ID3v2). The first (usually only) packet associated with it
  * will be returned among the first few packets read from the file unless
  * seeking takes place. It can also be accessed at any time in
- * AVStream.attached_pic.
+ * AVStream.apic.
  */
 #define AV_DISPOSITION_ATTACHED_PIC      0x0400
 /**
@@ -947,14 +947,19 @@ typedef struct AVStream {
      */
     AVRational avg_frame_rate;
 
+#if FF_API_ATTACHED_PIC
     /**
      * For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet
      * will contain the attached picture.
      *
      * decoding: set by libavformat, must not be modified by the caller.
      * encoding: unused
+     *
+     * @deprecated Use apic instead.
      */
+    attribute_deprecated
     AVPacket attached_pic;
+#endif
 
     /**
      * An array of side data that applies to the whole stream (i.e. the
@@ -1039,6 +1044,18 @@ typedef struct AVStream {
      */
     AVCodecParameters *codecpar;
 
+    /**
+     * Packet containing the attached picture. Used on streams with
+     * AV_DISPOSITION_ATTACHED_PIC disposition.
+     *
+     * - demuxing: Allocated and filled by libavformat on stream creation. Must
+     *             not be modified by the caller. Freed by libavformat in
+     *             avformat_free_context()
+     * - muxing: Allocated and filled by the caller before avformat_write_header()
+     *           Freed by libavformat in avformat_free_context()
+     */
+    AVPacket *apic;
+
     /*****************************************************************
      * All fields below this line are not part of the public API. They
      * may not be used outside of libavformat and can be changed and
@@ -1049,11 +1066,6 @@ typedef struct AVStream {
      *****************************************************************
      */
 
-#if LIBAVFORMAT_VERSION_MAJOR < 59
-    // kept for ABI compatibility only, do not access in any way
-    void *unused;
-#endif
-
     int pts_wrap_bits; /**< number of bits in pts (used for wrapping control) */
 
     // Timestamp generation support:
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 90e2a2b43a..8ac094acbe 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1068,15 +1068,15 @@ static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata,
     }
 
     /* check if apic appeared */
-    if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data))
+    if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->apic))
         return 1;
 
     if (apic) {
-        int size = pls->ctx->streams[1]->attached_pic.size;
+        int size = pls->ctx->streams[1]->apic->size;
         if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE)
             return 1;
 
-        if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0)
+        if (memcmp(apic->buf->data, pls->ctx->streams[1]->apic->data, size) != 0)
             return 1;
     }
 
diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c
index 863709abbf..49de81ba24 100644
--- a/libavformat/id3v2.c
+++ b/libavformat/id3v2.c
@@ -1154,7 +1154,7 @@ int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
         st  = s->streams[s->nb_streams - 1];
         st->codecpar->codec_id   = apic->id;
 
-        if (AV_RB64(st->attached_pic.data) == PNGSIG)
+        if (AV_RB64(st->apic->data) == PNGSIG)
             st->codecpar->codec_id = AV_CODEC_ID_PNG;
 
         if (apic->description[0])
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 08dcb10df4..e9e9a674b7 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -210,8 +210,8 @@ static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
     st = c->fc->streams[c->fc->nb_streams - 1];
     st->priv_data = sc;
 
-    if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
-        if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
+    if (st->apic->size >= 8 && id != AV_CODEC_ID_BMP) {
+        if (AV_RB64(st->apic->data) == 0x89504e470d0a1a0a) {
             id = AV_CODEC_ID_PNG;
         } else {
             id = AV_CODEC_ID_MJPEG;
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 3624678193..580f67c39d 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -457,7 +457,7 @@ int avformat_queue_attached_pictures(AVFormatContext *s)
     for (i = 0; i < s->nb_streams; i++)
         if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
             s->streams[i]->discard < AVDISCARD_ALL) {
-            if (s->streams[i]->attached_pic.size <= 0) {
+            if (s->streams[i]->apic->size <= 0) {
                 av_log(s, AV_LOG_WARNING,
                     "Attached picture on stream %d has invalid size, "
                     "ignoring\n", i);
@@ -466,7 +466,7 @@ int avformat_queue_attached_pictures(AVFormatContext *s)
 
             ret = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
                                      &s->internal->raw_packet_buffer_end,
-                                     &s->streams[i]->attached_pic,
+                                     s->streams[i]->apic,
                                      av_packet_ref, 0);
             if (ret < 0)
                 return ret;
@@ -478,12 +478,13 @@ int ff_add_attached_pic(AVFormatContext *s, AVStream *st0, AVIOContext *pb,
                         AVBufferRef **buf, int size)
 {
     AVStream *st = st0;
-    AVPacket *pkt;
+    AVPacket *pkt = av_packet_alloc();
     int ret;
 
-    if (!st && !(st = avformat_new_stream(s, NULL)))
+    if (!pkt || (!st && !(st = avformat_new_stream(s, NULL)))) {
+        av_packet_free(&pkt);
         return AVERROR(ENOMEM);
-    pkt = &st->attached_pic;
+    }
     if (buf) {
         av_assert1(*buf);
         av_packet_unref(pkt);
@@ -502,10 +503,20 @@ int ff_add_attached_pic(AVFormatContext *s, AVStream *st0, AVIOContext *pb,
     pkt->stream_index = st->index;
     pkt->flags       |= AV_PKT_FLAG_KEY;
 
+#if FF_API_ATTACHED_PIC
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (av_packet_ref(&st->attached_pic, pkt) < 0)
+        goto fail;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    av_assert0(!st0 || !st0->apic);
+    st->apic = pkt;
+
     return 0;
 fail:
     if (!st0)
         ff_free_stream(s, st);
+    av_packet_free(&pkt);
     return ret;
 }
 
@@ -4408,8 +4419,14 @@ static void free_stream(AVStream **pst)
     if (st->parser)
         av_parser_close(st->parser);
 
+#if FF_API_ATTACHED_PIC
+FF_DISABLE_DEPRECATION_WARNINGS
     if (st->attached_pic.data)
         av_packet_unref(&st->attached_pic);
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+    av_packet_free(&st->apic);
 
     if (st->internal) {
         avcodec_free_context(&st->internal->avctx);
diff --git a/libavformat/version.h b/libavformat/version.h
index ced5600034..dce936794a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -115,6 +115,9 @@
 #ifndef FF_API_LAVF_PRIV_OPT
 #define FF_API_LAVF_PRIV_OPT            (LIBAVFORMAT_VERSION_MAJOR < 60)
 #endif
+#ifndef FF_API_ATTACHED_PIC
+#define FF_API_ATTACHED_PIC             (LIBAVFORMAT_VERSION_MAJOR < 60)
+#endif
 
 
 #ifndef FF_API_R_FRAME_RATE
-- 
2.31.0



More information about the ffmpeg-devel mailing list