[FFmpeg-devel] [PATCH 11/24] sws: do not reallocate scratch buffers for each slice

Michael Niedermayer michael at niedermayer.cc
Tue Jun 1 15:22:17 EEST 2021


On Mon, May 31, 2021 at 09:55:02AM +0200, Anton Khirnov wrote:
> ---
>  libswscale/swscale.c          | 20 ++++++++++++--------
>  libswscale/swscale_internal.h |  6 ++++++
>  libswscale/utils.c            |  3 +++
>  3 files changed, 21 insertions(+), 8 deletions(-)
> 
> diff --git a/libswscale/swscale.c b/libswscale/swscale.c
> index 37c7cf60dd..2db40a6807 100644
> --- a/libswscale/swscale.c
> +++ b/libswscale/swscale.c
> @@ -871,7 +871,6 @@ int attribute_align_arg sws_scale(struct SwsContext *c,
>      int i, ret;
>      const uint8_t *src2[4];
>      uint8_t *dst2[4];
> -    uint8_t *rgb0_tmp = NULL;
>      int macro_height = isBayer(c->srcFormat) ? 2 : (1 << c->chrSrcVSubSample);
>      // copy strides, so they can safely be modified
>      int srcStride2[4];
> @@ -928,11 +927,14 @@ int attribute_align_arg sws_scale(struct SwsContext *c,
>      if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
>          uint8_t *base;
>          int x,y;
> -        rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
> -        if (!rgb0_tmp)
> +
> +        av_fast_malloc(&c->rgb0_scratch, &c->rgb0_scratch_allocated,
> +                       FFABS(srcStride[0]) * srcSliceH + 32);
> +        if (!c->rgb0_scratch)
>              return AVERROR(ENOMEM);
>  
> -        base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
> +        base = srcStride[0] < 0 ? c->rgb0_scratch - srcStride[0] * (srcSliceH-1) :
> +                                  c->rgb0_scratch;
>          for (y=0; y<srcSliceH; y++){
>              memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
>              for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
> @@ -944,11 +946,14 @@ int attribute_align_arg sws_scale(struct SwsContext *c,
>  
>      if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
>          uint8_t *base;
> -        rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
> -        if (!rgb0_tmp)
> +
> +        av_fast_malloc(&c->xyz_scratch, &c->xyz_scratch_allocated,
> +                       FFABS(srcStride[0]) * srcSliceH + 32);
> +        if (!c->xyz_scratch)
>              return AVERROR(ENOMEM);
>  
> -        base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
> +        base = srcStride[0] < 0 ? c->xyz_scratch - srcStride[0] * (srcSliceH-1) :
> +                                  c->xyz_scratch;
>  
>          xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
>          src2[0] = base;
> @@ -996,6 +1001,5 @@ int attribute_align_arg sws_scale(struct SwsContext *c,
>          rgb48Toxyz12(c, dst16, dst16, dstStride2[0]/2, ret);
>      }
>  
> -    av_free(rgb0_tmp);
>      return ret;
>  }

> diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h
> index a1de95cee0..e8a434427b 100644
> --- a/libswscale/swscale_internal.h
> +++ b/libswscale/swscale_internal.h
> @@ -626,6 +626,12 @@ typedef struct SwsContext {
>      SwsDither dither;
>  
>      SwsAlphaBlend alphablend;
> +
> +    uint8_t     *rgb0_scratch;
> +    unsigned int rgb0_scratch_allocated;
> +
> +    uint8_t     *xyz_scratch;
> +    unsigned int xyz_scratch_allocated;

these should have a few words documenting them. Size comes to mind as a usefull
parameter for a reader to be interrested in
lifetime is another one usefull to know for scratch areas, when can code use
them when does its content need to be preserved

otherwise LGTM

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Nations do behave wisely once they have exhausted all other alternatives. 
-- Abba Eban
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20210601/b9d73925/attachment.sig>


More information about the ffmpeg-devel mailing list