diff mbox series

[FFmpeg-devel,v11,8/8] avcodec/webp: export XMP metadata

Message ID 20240328140858.2521-9-thilo.borgmann@mail.de
State New
Headers show
Series webp: add support for animated WebP decoding | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Thilo Borgmann March 28, 2024, 2:08 p.m. UTC
---
 libavcodec/webp.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index 832ede52fc..b9feaa5bf0 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -38,8 +38,8 @@ 
  * @author Josef Zlomek, Pexeso Inc. <josef@pex.com>
  * Animation
  *
- * Unimplemented:
- *   - XMP metadata
+ * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
+ * XMP metadata
  */
 
 #include "libavutil/imgutils.h"
@@ -216,6 +216,7 @@  typedef struct WebPContext {
     int alpha_data_size;                /* alpha chunk data size */
     int has_exif;                       /* set after an EXIF chunk has been processed */
     int has_iccp;                       /* set after an ICCP chunk has been processed */
+    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 */
@@ -1463,6 +1464,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 };
@@ -1494,6 +1496,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_thread_release_ext_buffer(&s->canvas_frame);
             break;
         case MKTAG('V', 'P', '8', ' '):
@@ -1679,12 +1682,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",