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 463fb61672..74d387f437 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -117,7 +117,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m,
         * recommended way of embedding cover art within VorbisComments."
         */
     if (!av_strcasecmp(t, "METADATA_BLOCK_PICTURE") && parse_picture) {
-        int ret, len = AV_BASE64_DECODE_SIZE(vl);
+        int ret, len = AV_BASE64_DECODE_SIZE(vl) + AV_INPUT_BUFFER_PADDING_SIZE;
         uint8_t *pict = av_malloc(len);
 
         if (!pict) {
@@ -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");
