[FFmpeg-devel] [PATCH 1/2] avformat/utils: add an internal getter function to access AVStream.attached_pic

James Almer jamrial at gmail.com
Sun Mar 21 19:08:59 EET 2021


It can be removed once the field becomes a pointer.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/apetag.c       |  9 ++++-----
 libavformat/asfdec_f.c     | 13 +++++++------
 libavformat/asfdec_o.c     | 13 +++++++------
 libavformat/flac_picture.c | 14 ++++++++------
 libavformat/hls.c          |  8 +++++---
 libavformat/id3v2.c        | 14 ++++++++------
 libavformat/internal.h     |  2 ++
 libavformat/matroskadec.c  |  2 +-
 libavformat/mov.c          | 19 +++++++++++--------
 libavformat/options.c      | 12 +++++++++++-
 libavformat/utils.c        | 18 ++++++++++++++++--
 libavformat/wtvdec.c       |  8 +++++---
 12 files changed, 85 insertions(+), 47 deletions(-)

diff --git a/libavformat/apetag.c b/libavformat/apetag.c
index 454c6c688b..fce0d49c01 100644
--- a/libavformat/apetag.c
+++ b/libavformat/apetag.c
@@ -79,10 +79,10 @@ static int ape_tag_read_field(AVFormatContext *s)
         av_dict_set(&st->metadata, key, filename, 0);
 
         if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
-            AVPacket pkt;
+            AVPacket *attached_pic = ff_stream_get_attached_pic(st);
             int ret;
 
-            ret = av_get_packet(s->pb, &pkt, size);
+            ret = av_get_packet(s->pb, attached_pic, size);
             if (ret < 0) {
                 av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
                 return ret;
@@ -92,9 +92,8 @@ static int ape_tag_read_field(AVFormatContext *s)
             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
             st->codecpar->codec_id   = id;
 
-            st->attached_pic              = pkt;
-            st->attached_pic.stream_index = st->index;
-            st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+            attached_pic->stream_index = st->index;
+            attached_pic->flags       |= AV_PKT_FLAG_KEY;
         } else {
             if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
                 return ret;
diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
index 1484b544d9..05b06beb4f 100644
--- a/libavformat/asfdec_f.c
+++ b/libavformat/asfdec_f.c
@@ -223,7 +223,7 @@ static int get_value(AVIOContext *pb, int type, int type2_size)
  * but in reality this is only loosely similar */
 static int asf_read_picture(AVFormatContext *s, int len)
 {
-    AVPacket pkt          = { 0 };
+    AVPacket *attached_pic, *pkt = s->internal->parse_pkt;
     const CodecMime *mime = ff_id3v2_mime_tags;
     enum  AVCodecID id    = AV_CODEC_ID_NONE;
     char mimetype[64];
@@ -277,7 +277,7 @@ static int asf_read_picture(AVFormatContext *s, int len)
         return AVERROR(ENOMEM);
     len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
 
-    ret = av_get_packet(s->pb, &pkt, picsize);
+    ret = av_get_packet(s->pb, pkt, picsize);
     if (ret < 0)
         goto fail;
 
@@ -289,9 +289,10 @@ static int asf_read_picture(AVFormatContext *s, int len)
     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
     st->codecpar->codec_type      = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id        = id;
-    st->attached_pic              = pkt;
-    st->attached_pic.stream_index = st->index;
-    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+    attached_pic = ff_stream_get_attached_pic(st);
+    av_packet_move_ref(attached_pic, pkt);
+    attached_pic->stream_index = st->index;
+    attached_pic->flags       |= AV_PKT_FLAG_KEY;
 
     if (*desc)
         av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
@@ -304,7 +305,7 @@ static int asf_read_picture(AVFormatContext *s, int len)
 
 fail:
     av_freep(&desc);
-    av_packet_unref(&pkt);
+    av_packet_unref(pkt);
     return ret;
 }
 
diff --git a/libavformat/asfdec_o.c b/libavformat/asfdec_o.c
index 34ae541934..ac032f98fc 100644
--- a/libavformat/asfdec_o.c
+++ b/libavformat/asfdec_o.c
@@ -360,7 +360,7 @@ static int asf_set_metadata(AVFormatContext *s, const uint8_t *name,
  * but in reality this is only loosely similar */
 static int asf_read_picture(AVFormatContext *s, int len)
 {
-    AVPacket pkt          = { 0 };
+    AVPacket *attached_pic, *pkt = s->internal->parse_pkt;
     const CodecMime *mime = ff_id3v2_mime_tags;
     enum  AVCodecID id    = AV_CODEC_ID_NONE;
     char mimetype[64];
@@ -414,7 +414,7 @@ static int asf_read_picture(AVFormatContext *s, int len)
         return AVERROR(ENOMEM);
     len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
 
-    ret = av_get_packet(s->pb, &pkt, picsize);
+    ret = av_get_packet(s->pb, pkt, picsize);
     if (ret < 0)
         goto fail;
 
@@ -427,9 +427,10 @@ static int asf_read_picture(AVFormatContext *s, int len)
     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
     st->codecpar->codec_type      = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id        = id;
-    st->attached_pic              = pkt;
-    st->attached_pic.stream_index = st->index;
-    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+    attached_pic = ff_stream_get_attached_pic(st);
+    av_packet_move_ref(attached_pic, pkt);
+    attached_pic->stream_index = st->index;
+    attached_pic->flags       |= AV_PKT_FLAG_KEY;
 
     if (*desc) {
         if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
@@ -444,7 +445,7 @@ static int asf_read_picture(AVFormatContext *s, int len)
 
 fail:
     av_freep(&desc);
-    av_packet_unref(&pkt);
+    av_packet_unref(pkt);
     return ret;
 }
 
diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c
index f15cfa877a..52a2b0f1fb 100644
--- a/libavformat/flac_picture.c
+++ b/libavformat/flac_picture.c
@@ -37,6 +37,7 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr
     uint8_t mimetype[64], *desc = NULL;
     GetByteContext g;
     AVStream *st;
+    AVPacket *attached_pic;
     int width, height, ret = 0;
     unsigned int type;
     uint32_t len, left, trunclen = 0;
@@ -165,12 +166,13 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr
         RETURN_ERROR(AVERROR(ENOMEM));
     }
 
-    av_packet_unref(&st->attached_pic);
-    st->attached_pic.buf          = data;
-    st->attached_pic.data         = data->data;
-    st->attached_pic.size         = len;
-    st->attached_pic.stream_index = st->index;
-    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+    attached_pic = ff_stream_get_attached_pic(st);
+    av_packet_unref(attached_pic);
+    attached_pic->buf          = data;
+    attached_pic->data         = data->data;
+    attached_pic->size         = len;
+    attached_pic->stream_index = st->index;
+    attached_pic->flags       |= AV_PKT_FLAG_KEY;
 
     st->disposition      |= AV_DISPOSITION_ATTACHED_PIC;
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 597bea7f25..d75b129e45 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1068,15 +1068,17 @@ 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 ||
+                 !ff_stream_get_attached_pic(pls->ctx->streams[1])->data))
         return 1;
 
     if (apic) {
-        int size = pls->ctx->streams[1]->attached_pic.size;
+        AVPacket *attached_pic = ff_stream_get_attached_pic(pls->ctx->streams[1]);
+        int size = attached_pic->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, attached_pic->data, size) != 0)
             return 1;
     }
 
diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c
index f33b7ba93a..c3df59af4f 100644
--- a/libavformat/id3v2.c
+++ b/libavformat/id3v2.c
@@ -1141,6 +1141,7 @@ int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
 
     for (cur = extra_meta; cur; cur = cur->next) {
         ID3v2ExtraMetaAPIC *apic;
+        AVPacket *attached_pic;
         AVStream *st;
 
         if (strcmp(cur->tag, "APIC"))
@@ -1162,12 +1163,13 @@ int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
 
         av_dict_set(&st->metadata, "comment", apic->type, 0);
 
-        av_packet_unref(&st->attached_pic);
-        st->attached_pic.buf          = apic->buf;
-        st->attached_pic.data         = apic->buf->data;
-        st->attached_pic.size         = apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE;
-        st->attached_pic.stream_index = st->index;
-        st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+        attached_pic = ff_stream_get_attached_pic(st);
+        av_packet_unref(attached_pic);
+        attached_pic->buf          = apic->buf;
+        attached_pic->data         = apic->buf->data;
+        attached_pic->size         = apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE;
+        attached_pic->stream_index = st->index;
+        attached_pic->flags       |= AV_PKT_FLAG_KEY;
 
         apic->buf = NULL;
     }
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 3c6b2921c1..aad2e80cc0 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -892,6 +892,8 @@ int ff_unlock_avformat(void);
  */
 void ff_format_set_url(AVFormatContext *s, char *url);
 
+AVPacket *ff_stream_get_attached_pic(AVStream *st);
+
 void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]);
 
 #endif /* AVFORMAT_INTERNAL_H */
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 9acfdf5b32..c05fb35be7 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -3007,7 +3007,7 @@ static int matroska_read_header(AVFormatContext *s)
             attachments[j].stream = st;
 
             if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
-                AVPacket *pkt = &st->attached_pic;
+                AVPacket *pkt = ff_stream_get_attached_pic(st);
 
                 st->disposition         |= AV_DISPOSITION_ATTACHED_PIC;
                 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index f9c4dbe5d4..e8de594e25 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -182,6 +182,7 @@ static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
 {
     AVStream *st;
+    AVPacket *attached_pic;
     MOVStreamContext *sc;
     enum AVCodecID id;
     int ret;
@@ -204,12 +205,13 @@ static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
         return AVERROR(ENOMEM);
     st->priv_data = sc;
 
-    ret = av_get_packet(pb, &st->attached_pic, len);
+    attached_pic = ff_stream_get_attached_pic(st);
+    ret = av_get_packet(pb, attached_pic, len);
     if (ret < 0)
         return ret;
 
-    if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
-        if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
+    if (attached_pic->size >= 8 && id != AV_CODEC_ID_BMP) {
+        if (AV_RB64(attached_pic->data) == 0x89504e470d0a1a0a) {
             id = AV_CODEC_ID_PNG;
         } else {
             id = AV_CODEC_ID_MJPEG;
@@ -218,8 +220,8 @@ static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
 
     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
 
-    st->attached_pic.stream_index = st->index;
-    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+    attached_pic->stream_index = st->index;
+    attached_pic->flags       |= AV_PKT_FLAG_KEY;
 
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id   = id;
@@ -7224,16 +7226,17 @@ static void mov_read_chapters(AVFormatContext *s)
             if (st->internal->nb_index_entries) {
                 // Retrieve the first frame, if possible
                 AVIndexEntry *sample = &st->internal->index_entries[0];
+                AVPacket *attached_pic = ff_stream_get_attached_pic(st);
                 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
                     av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
                     goto finish;
                 }
 
-                if (av_get_packet(sc->pb, &st->attached_pic, sample->size) < 0)
+                if (av_get_packet(sc->pb, attached_pic, sample->size) < 0)
                     goto finish;
 
-                st->attached_pic.stream_index = st->index;
-                st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+                attached_pic->stream_index = st->index;
+                attached_pic->flags       |= AV_PKT_FLAG_KEY;
             }
         } else {
             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
diff --git a/libavformat/options.c b/libavformat/options.c
index 07403b533e..bd4056caa1 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -220,11 +220,21 @@ AVFormatContext *avformat_alloc_context(void)
         av_free(ic);
         return NULL;
     }
+#if !FF_API_INIT_PACKET
+    ic->attached_pic = av_packet_alloc();
+#endif
     internal->pkt = av_packet_alloc();
     internal->parse_pkt = av_packet_alloc();
-    if (!internal->pkt || !internal->parse_pkt) {
+    if (!internal->pkt || !internal->parse_pkt
+#if !FF_API_INIT_PACKET
+        || !ic->attached_pic
+#endif
+    ) {
         av_packet_free(&internal->pkt);
         av_packet_free(&internal->parse_pkt);
+#if !FF_API_INIT_PACKET
+        av_packet_free(&ic->attached_pic);
+#endif
         av_free(internal);
         av_free(ic);
         return NULL;
diff --git a/libavformat/utils.c b/libavformat/utils.c
index ee947c195d..3db12af1b6 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -457,7 +457,8 @@ 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) {
+            AVPacket *attached_pic = ff_stream_get_attached_pic(s->streams[i]);
+            if (attached_pic->size <= 0) {
                 av_log(s, AV_LOG_WARNING,
                     "Attached picture on stream %d has invalid size, "
                     "ignoring\n", i);
@@ -466,7 +467,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,
+                                     attached_pic,
                                      av_packet_ref, 0);
             if (ret < 0)
                 return ret;
@@ -4382,8 +4383,12 @@ static void free_stream(AVStream **pst)
     if (st->parser)
         av_parser_close(st->parser);
 
+#if FF_API_INIT_PACKET
     if (st->attached_pic.data)
         av_packet_unref(&st->attached_pic);
+#else
+    av_packet_free(st->attached_pic);
+#endif
 
     if (st->internal) {
         avcodec_free_context(&st->internal->avctx);
@@ -5855,3 +5860,12 @@ FF_DISABLE_DEPRECATION_WARNINGS
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
 }
+
+AVPacket *ff_stream_get_attached_pic(AVStream *st)
+{
+#if FF_API_INIT_PACKET
+    return &st->attached_pic;
+#else
+    return st->attached_pic;
+#endif
+}
diff --git a/libavformat/wtvdec.c b/libavformat/wtvdec.c
index 7def9d2348..9613cfbd41 100644
--- a/libavformat/wtvdec.c
+++ b/libavformat/wtvdec.c
@@ -434,6 +434,7 @@ static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
     char description[1024];
     unsigned int filesize;
     AVStream *st;
+    AVPacket *attached_pic;
     int ret;
     int64_t pos = avio_tell(pb);
 
@@ -454,11 +455,12 @@ static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id   = AV_CODEC_ID_MJPEG;
     st->id = -1;
-    ret = av_get_packet(pb, &st->attached_pic, filesize);
+    attached_pic = ff_stream_get_attached_pic(st);
+    ret = av_get_packet(pb, attached_pic, filesize);
     if (ret < 0)
         goto done;
-    st->attached_pic.stream_index = st->index;
-    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
+    attached_pic->stream_index = st->index;
+    attached_pic->flags       |= AV_PKT_FLAG_KEY;
     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
 done:
     avio_seek(pb, pos + length, SEEK_SET);
-- 
2.30.2



More information about the ffmpeg-devel mailing list