diff mbox series

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

Message ID tencent_C9683B594685E951E7E9D008C13585EF7705@qq.com
State New
Headers show
Series [FFmpeg-devel] libavcodec/libwebpenc_animencoder.c: support ICCP when the source material contains the icc profile | expand

Checks

Context Check Description
yinshiyou/commit_msg_loongarch64 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/commit_msg_x86 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

wangyaqiang March 9, 2023, 9:31 a.m. UTC
From: Wang Yaqiang <wangyaqiang03@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@kuaishou.com>
---
 libavcodec/libwebpenc_animencoder.c | 39 ++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

Comments

Steven Liu March 9, 2023, 11:57 a.m. UTC | #1
<1035567130@qq.com> 于2023年3月9日周四 17:32写道:
>
> From: Wang Yaqiang <wangyaqiang03@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@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;
you can return AVERROR(ENOMEM); here;
> +                    }
> +                    ret = WebPMuxSetChunk(muxer,"ICCP",&s->icc_data,0);
> +                    if (!ret) {
> +                        ret = AVERROR_INVALIDDATA;
> +                        WebPMuxDelete(muxer);
> +                        return ret;
ditoo,
> +                    }
> +                    ret = WebPMuxAssemble(muxer,&assembled_data);
> +                    WebPMuxDelete(muxer);
> +                    if (!ret) {
> +                        ret = AVERROR_INVALIDDATA;
> +                        WebPDataClear(&assembled_data);
> +                        return ret;
ditoo,

or maybe you should try use goto for clean operation?
> +                    }
> +                }
>                  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
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".


Thanks
Steven
diff mbox series

Patch

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;
 }