ソースを参照

avformat/flac_picture: Try to reuse buffer for attached picture

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Andreas Rheinhardt 4 年 前
コミット
c1f4858242

+ 13 - 2
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)

+ 13 - 1
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 */

+ 1 - 1
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");

+ 2 - 2
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");