[FFmpeg-devel] [PATCH v13 8/8] avcodec/webp: export XMP metadata

Thilo Borgmann thilo.borgmann at mail.de
Fri Jun 21 13:43:23 EEST 2024


From: Thilo Borgmann via ffmpeg-devel <ffmpeg-devel at ffmpeg.org>

---
 libavcodec/webp.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index bacf605ff2..c8be673060 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -38,8 +38,8 @@
  * @author Josef Zlomek, Pexeso Inc. <josef at pex.com>
  * Animation
  *
- * Unimplemented:
- *   - XMP metadata
+ * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
+ * XMP metadata
  */
 
 #include "libavutil/common.h"
@@ -220,6 +220,7 @@ typedef struct WebPContext {
     int has_exif;                       /* set after an EXIF chunk has been processed */
     int has_iccp;                       /* set after an ICCP chunk has been processed */
     int duration;                       /* frame duration in an animation */
+    int has_xmp;                        /* set after an XMP chunk has been processed */
     int width;                          /* image width */
     int height;                         /* image height */
     int vp8x_flags;                     /* global flags from VP8X chunk */
@@ -1469,6 +1470,7 @@ static int webp_decode_frame_common(AVCodecContext *avctx, uint8_t *data, int si
     // reset metadata bit for each packet
     s->has_exif      = 0;
     s->has_iccp      = 0;
+    s->has_xmp       = 0;
 
     while (bytestream2_get_bytes_left(&gb) > 8) {
         char chunk_str[5] = { 0 };
@@ -1500,6 +1502,7 @@ static int webp_decode_frame_common(AVCodecContext *avctx, uint8_t *data, int si
             s->canvas_height = 0;
             s->has_exif      = 0;
             s->has_iccp      = 0;
+            s->has_xmp       = 0;
             ff_progress_frame_unref(&s->canvas_frame);
             break;
         case MKTAG('V', 'P', '8', ' '):
@@ -1682,12 +1685,39 @@ exif_end:
             }
             s->vp8x_flags |= VP8X_FLAG_ANIMATION;
             break;
-        case MKTAG('X', 'M', 'P', ' '):
-            AV_WL32(chunk_str, chunk_type);
-            av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n",
-                   chunk_str);
+        case MKTAG('X', 'M', 'P', ' '): {
+            GetByteContext xmp_gb;
+            AVDictionary **xmp_metadata = NULL;
+            uint8_t *buffer;
+            int xmp_offset = bytestream2_tell(&gb);
+
+            if (s->has_xmp) {
+                av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra XMP chunk\n");
+                goto xmp_end;
+            }
+            if (!(s->vp8x_flags & VP8X_FLAG_XMP_METADATA))
+                av_log(avctx, AV_LOG_WARNING,
+                       "XMP chunk present, but XMP bit not set in the "
+                       "VP8X header\n");
+
+            // there are at least chunk_size bytes left to read
+            buffer = av_malloc(chunk_size + 1);
+            if (!buffer) {
+                return AVERROR(ENOMEM);
+            }
+
+            s->has_xmp = 1;
+            bytestream2_init(&xmp_gb, data + xmp_offset, size - xmp_offset);
+            bytestream2_get_buffer(&xmp_gb, buffer, chunk_size);
+            buffer[chunk_size] = '\0';
+
+            xmp_metadata = (s->vp8x_flags & VP8X_FLAG_ANIMATION) ? &p->metadata : &s->frame->metadata;
+            av_dict_set(xmp_metadata, "xmp", buffer, AV_DICT_DONT_STRDUP_VAL);
+
+xmp_end:
             bytestream2_skip(&gb, chunk_size);
             break;
+            }
         default:
             AV_WL32(chunk_str, chunk_type);
             av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
-- 
2.39.3 (Apple Git-146)



More information about the ffmpeg-devel mailing list