[FFmpeg-cvslog] avformat/flac_picture: Try to reuse buffer for attached picture

Andreas Rheinhardt git at videolan.org
Sat Aug 28 17:52:39 EEST 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Mon Aug 23 13:37:04 2021 +0200| [c1f48582428f7d5e7d93d2f0af6d4612559f74ca] | committer: Andreas Rheinhardt

avformat/flac_picture: Try to reuse buffer for attached picture

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=c1f48582428f7d5e7d93d2f0af6d4612559f74ca
---

 libavformat/flac_picture.c   | 15 +++++++++++++--
 libavformat/flac_picture.h   | 14 +++++++++++++-
 libavformat/flacdec.c        |  2 +-
 libavformat/oggparsevorbis.c |  4 ++--
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c
index 96e14f76c9..e7660a7db1 100644
--- a/libavformat/flac_picture.c
+++ b/libavformat/flac_picture.c
@@ -29,12 +29,13 @@
 
 #define MAX_TRUNC_PICTURE_SIZE (500 * 1024 * 1024)
 
-int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround)
+int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
+                          int truncate_workaround)
 {
     const CodecMime *mime = ff_id3v2_mime_tags;
     enum AVCodecID id = AV_CODEC_ID_NONE;
     AVBufferRef *data = NULL;
-    uint8_t mimetype[64], *desc = NULL;
+    uint8_t mimetype[64], *desc = NULL, *buf = *bufp;
     GetByteContext g;
     AVStream *st;
     int width, height, ret = 0;
@@ -142,6 +143,15 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr
             goto fail;
         }
     }
+    if (trunclen == 0 && len >= buf_size - (buf_size >> 4)) {
+        data = av_buffer_create(buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE,
+                                av_buffer_default_free, NULL, 0);
+        if (!data)
+            RETURN_ERROR(AVERROR(ENOMEM));
+        *bufp = NULL;
+        data->data += bytestream2_tell(&g);
+        data->size  = len + AV_INPUT_BUFFER_PADDING_SIZE;
+    } else {
     if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
         RETURN_ERROR(AVERROR(ENOMEM));
     }
@@ -155,6 +165,7 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr
         if (avio_read(s->pb, data->data + len - trunclen, trunclen) < trunclen)
             RETURN_ERROR(AVERROR_INVALIDDATA);
     }
+    }
     memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
     if (AV_RB64(data->data) == PNGSIG)
diff --git a/libavformat/flac_picture.h b/libavformat/flac_picture.h
index 61fd0c8806..db074e531d 100644
--- a/libavformat/flac_picture.h
+++ b/libavformat/flac_picture.h
@@ -26,6 +26,18 @@
 
 #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
 
-int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround);
+/**
+ * Parse a FLAC METADATA_BLOCK_PICTURE
+ *
+ * @param s   AVFormatContext for logging and the attached picture stream.
+ * @param buf `*buf` points to the actual data which must be padded by
+ *            AV_INPUT_BUFFER_PADDING_SIZE bytes not counted in buf_size.
+ *            This function may take ownership of `*buf` and reset it.
+ * @param buf_size size of `*buf` (excluding padding)
+ * @param truncate_workaround If set, additional data may be read from s->pb if
+ *                            truncation has been detected.
+ */
+int ff_flac_parse_picture(AVFormatContext *s, uint8_t **buf, int buf_size,
+                          int truncate_workaround);
 
 #endif /* AVFORMAT_FLAC_PICTURE_H */
diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index bfa464c508..2060b48c69 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -145,7 +145,7 @@ static int flac_read_header(AVFormatContext *s)
             }
             av_freep(&buffer);
         } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
-            ret = ff_flac_parse_picture(s, buffer, metadata_size, 1);
+            ret = ff_flac_parse_picture(s, &buffer, metadata_size, 1);
             av_freep(&buffer);
             if (ret < 0) {
                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index b66167ba62..77e8d301b2 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -118,7 +118,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m,
      */
     if (!av_strcasecmp(t, "METADATA_BLOCK_PICTURE") && parse_picture) {
         int ret, len = AV_BASE64_DECODE_SIZE(vl);
-        uint8_t *pict = av_malloc(len);
+        uint8_t *pict = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
 
         if (!pict) {
             av_log(as, AV_LOG_WARNING, "out-of-memory error. Skipping cover art block.\n");
@@ -126,7 +126,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m,
         }
         ret = av_base64_decode(pict, v, len);
         if (ret > 0)
-            ret = ff_flac_parse_picture(as, pict, ret, 0);
+            ret = ff_flac_parse_picture(as, &pict, ret, 0);
         av_freep(&pict);
         if (ret < 0) {
             av_log(as, AV_LOG_WARNING, "Failed to parse cover art block.\n");



More information about the ffmpeg-cvslog mailing list