diff mbox series

[FFmpeg-devel,2/2] avcodec/tiff: Don't use separate temporary buffer for fax

Message ID HE1PR0301MB215461C3C63AD6FFAA56629F8F7D9@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit b0cd60bd97ec403579bd7d12e6f55d7caa0d616d
Headers show
Series [FFmpeg-devel,1/2] avcodec/tiff: Avoid forward declarations
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt March 30, 2021, 8:03 a.m. UTC
Also don't unnecessarily copy the input data around if it needn't be
reversed; and remove a redundant memset -- av_fast_padded_malloc()
already does this for us.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/tiff.c | 28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)

Comments

Andreas Rheinhardt April 1, 2021, 1:01 p.m. UTC | #1
Andreas Rheinhardt:
> Also don't unnecessarily copy the input data around if it needn't be
> reversed; and remove a redundant memset -- av_fast_padded_malloc()
> already does this for us.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/tiff.c | 28 ++++++----------------------
>  1 file changed, 6 insertions(+), 22 deletions(-)
> 
> diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
> index 1d72fdc720..6129ae1c25 100644
> --- a/libavcodec/tiff.c
> +++ b/libavcodec/tiff.c
> @@ -108,8 +108,6 @@ typedef struct TiffContext {
>      int deinvert_buf_size;
>      uint8_t *yuv_line;
>      unsigned int yuv_line_size;
> -    uint8_t *fax_buffer;
> -    unsigned int fax_buffer_size;
>  
>      int geotag_count;
>      TiffGeoTag *geotags;
> @@ -613,27 +611,15 @@ static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride
>  static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
>                             const uint8_t *src, int size, int width, int lines)
>  {
> -    int i, ret = 0;
>      int line;
> -    uint8_t *src2;
> -
> -    av_fast_padded_malloc(&s->fax_buffer, &s->fax_buffer_size, size);
> -    src2 = s->fax_buffer;
> -
> -    if (!src2) {
> -        av_log(s->avctx, AV_LOG_ERROR,
> -               "Error allocating temporary buffer\n");
> -        return AVERROR(ENOMEM);
> -    }
> +    int ret;
>  
> -    if (!s->fill_order) {
> -        memcpy(src2, src, size);
> -    } else {
> -        for (i = 0; i < size; i++)
> -            src2[i] = ff_reverse[src[i]];
> +    if (s->fill_order) {
> +        if ((ret = deinvert_buffer(s, src, size)) < 0)
> +            return ret;
> +        src = s->deinvert_buf;
>      }
> -    memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
> -    ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
> +    ret = ff_ccitt_unpack(s->avctx, src, size, dst, lines, stride,
>                            s->compr, s->fax_opts);
>      if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
>          for (line = 0; line < lines; line++) {
> @@ -2186,8 +2172,6 @@ static av_cold int tiff_end(AVCodecContext *avctx)
>      s->deinvert_buf_size = 0;
>      av_freep(&s->yuv_line);
>      s->yuv_line_size = 0;
> -    av_freep(&s->fax_buffer);
> -    s->fax_buffer_size = 0;
>      av_frame_free(&s->jpgframe);
>      av_packet_free(&s->jpkt);
>      avcodec_free_context(&s->avctx_mjpeg);
> 
Will apply tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 1d72fdc720..6129ae1c25 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -108,8 +108,6 @@  typedef struct TiffContext {
     int deinvert_buf_size;
     uint8_t *yuv_line;
     unsigned int yuv_line_size;
-    uint8_t *fax_buffer;
-    unsigned int fax_buffer_size;
 
     int geotag_count;
     TiffGeoTag *geotags;
@@ -613,27 +611,15 @@  static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride
 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
                            const uint8_t *src, int size, int width, int lines)
 {
-    int i, ret = 0;
     int line;
-    uint8_t *src2;
-
-    av_fast_padded_malloc(&s->fax_buffer, &s->fax_buffer_size, size);
-    src2 = s->fax_buffer;
-
-    if (!src2) {
-        av_log(s->avctx, AV_LOG_ERROR,
-               "Error allocating temporary buffer\n");
-        return AVERROR(ENOMEM);
-    }
+    int ret;
 
-    if (!s->fill_order) {
-        memcpy(src2, src, size);
-    } else {
-        for (i = 0; i < size; i++)
-            src2[i] = ff_reverse[src[i]];
+    if (s->fill_order) {
+        if ((ret = deinvert_buffer(s, src, size)) < 0)
+            return ret;
+        src = s->deinvert_buf;
     }
-    memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
-    ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
+    ret = ff_ccitt_unpack(s->avctx, src, size, dst, lines, stride,
                           s->compr, s->fax_opts);
     if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
         for (line = 0; line < lines; line++) {
@@ -2186,8 +2172,6 @@  static av_cold int tiff_end(AVCodecContext *avctx)
     s->deinvert_buf_size = 0;
     av_freep(&s->yuv_line);
     s->yuv_line_size = 0;
-    av_freep(&s->fax_buffer);
-    s->fax_buffer_size = 0;
     av_frame_free(&s->jpgframe);
     av_packet_free(&s->jpkt);
     avcodec_free_context(&s->avctx_mjpeg);