[FFmpeg-devel] [PATCH] libavcodec/libwebpenc_animencoder.c: support ICCP when the source material contains the icc profile

1035567130 at qq.com 1035567130 at qq.com
Thu Mar 9 11:31:47 EET 2023


From: Wang Yaqiang <wangyaqiang03 at kuaishou.com>

when the source material contains the icc profile,
the AVFrame have side_data with type AV_FRAME_DATA_ICC_PROFILE,
write the ICCP chunk make sure the colors are displayed correctly.

Signed-off-by: Wang Yaqiang <wangyaqiang03 at kuaishou.com>
---
 libavcodec/libwebpenc_animencoder.c | 39 ++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/libavcodec/libwebpenc_animencoder.c b/libavcodec/libwebpenc_animencoder.c
index 8756231f23..acdefc75a4 100644
--- a/libavcodec/libwebpenc_animencoder.c
+++ b/libavcodec/libwebpenc_animencoder.c
@@ -45,6 +45,8 @@ typedef struct LibWebPAnimContext {
     void           *first_frame_opaque;
     AVBufferRef    *first_frame_opaque_ref;
 
+    WebPData icc_data;
+
     int done;                 // If true, we have assembled the bitstream already
 } LibWebPAnimContext;
 
@@ -62,6 +64,7 @@ static av_cold int libwebp_anim_encode_init(AVCodecContext *avctx)
             return AVERROR(EINVAL);
         s->first_frame_pts = AV_NOPTS_VALUE;
         s->done = 0;
+        WebPDataInit(&s->icc_data);
     }
     return ret;
 }
@@ -79,6 +82,27 @@ static int libwebp_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             WebPData assembled_data = { 0 };
             ret = WebPAnimEncoderAssemble(s->enc, &assembled_data);
             if (ret) {
+                if (s->icc_data.bytes) {
+                    WebPMux *muxer = WebPMuxCreate(&assembled_data,1);
+                    WebPDataClear(&assembled_data);
+                    if (!muxer) {
+                        ret = AVERROR(ENOMEM);
+                        return ret;
+                    }
+                    ret = WebPMuxSetChunk(muxer,"ICCP",&s->icc_data,0);
+                    if (!ret) {
+                        ret = AVERROR_INVALIDDATA;
+                        WebPMuxDelete(muxer);
+                        return ret;
+                    }
+                    ret = WebPMuxAssemble(muxer,&assembled_data);
+                    WebPMuxDelete(muxer);
+                    if (!ret) {
+                        ret = AVERROR_INVALIDDATA;
+                        WebPDataClear(&assembled_data);
+                        return ret;
+                    }
+                }
                 ret = ff_get_encode_buffer(avctx, pkt, assembled_data.size, 0);
                 if (ret < 0) {
                     WebPDataClear(&assembled_data);
@@ -117,6 +141,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         int timestamp_ms;
         WebPPicture *pic = NULL;
         AVFrame *alt_frame = NULL;
+        AVFrameSideData *frame_side_data = NULL;
         ret = ff_libwebp_get_frame(avctx, &s->cc, frame, &alt_frame, &pic);
         if (ret < 0)
             goto end;
@@ -131,7 +156,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
             ret = ff_libwebp_error_to_averror(pic->error_code);
             goto end;
         }
-
+        if (!s->icc_data.bytes) {
+            frame_side_data =  av_frame_get_side_data(frame,AV_FRAME_DATA_ICC_PROFILE);
+            if (frame_side_data) {
+                s->icc_data.bytes = WebPMalloc(frame_side_data->size);
+                if (!s->icc_data.bytes) {
+                    ret = AVERROR(ENOMEM);
+                    goto end;
+                }
+                s->icc_data.size = frame_side_data->size;
+                memcpy(s->icc_data.bytes,frame_side_data->data,frame_side_data->size);
+            }
+        }
         if (!avctx->frame_num) {
             s->first_frame_pts = frame->pts;
 #if FF_API_REORDERED_OPAQUE
@@ -170,6 +206,7 @@ static int libwebp_anim_encode_close(AVCodecContext *avctx)
 
     av_buffer_unref(&s->first_frame_opaque_ref);
 
+    WebPDataClear(&s->icc_data);
     return 0;
 }
 
-- 
2.39.2



More information about the ffmpeg-devel mailing list