diff mbox series

[FFmpeg-devel,21/21] avcodec/smacker: Avoid code duplication

Message ID 20200801134704.3647-13-andreas.rheinhardt@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/8] avcodec/smacker: Remove write-only and unused variables
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Aug. 1, 2020, 1:47 p.m. UTC
Besides the obvious advantage of less code this also has a performance
impact: For GCC 9 the time spent on one call to smka_decode_frame() for
the sample from ticket #2425 decreased from 1693619 to 1498127
decicycles. For Clang 9, it decreased from 1369089 to 1366465
decicycles.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
The numbers for GCC surprised me (as did the fact that GCC was so much
worse than Clang).

 libavcodec/smacker.c | 62 ++++++++++++++------------------------------
 1 file changed, 20 insertions(+), 42 deletions(-)

Comments

Paul B Mahol Aug. 1, 2020, 2:01 p.m. UTC | #1
On 8/1/20, Andreas Rheinhardt <andreas.rheinhardt@gmail.com> wrote:
> Besides the obvious advantage of less code this also has a performance
> impact: For GCC 9 the time spent on one call to smka_decode_frame() for
> the sample from ticket #2425 decreased from 1693619 to 1498127
> decicycles. For Clang 9, it decreased from 1369089 to 1366465
> decicycles.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
> The numbers for GCC surprised me (as did the fact that GCC was so much
> worse than Clang).
>
>  libavcodec/smacker.c | 62 ++++++++++++++------------------------------
>  1 file changed, 20 insertions(+), 42 deletions(-)
>
> diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c
> index d2b1c68162..ffd24c11e7 100644
> --- a/libavcodec/smacker.c
> +++ b/libavcodec/smacker.c
> @@ -671,37 +671,23 @@ static int smka_decode_frame(AVCodecContext *avctx,
> void *data,
>          for(i = 0; i <= stereo; i++)
>              *samples++ = pred[i];
>          for(; i < unp_size / 2; i++) {
> +            unsigned idx = 2 * (i & stereo);
>              if (get_bits_left(&gb) < 0) {
>                  ret = AVERROR_INVALIDDATA;
>                  goto error;
>              }
> -            if(i & stereo) {
> -                if(vlc[2].table)
> -                    res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
> -                else
> -                    res = values[2];
> -                val  = res;
> -                if(vlc[3].table)
> -                    res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
> -                else
> -                    res = values[3];
> -                val |= res << 8;
> -                pred[1] += val;
> -                *samples++ = pred[1];
> -            } else {
> -                if(vlc[0].table)
> -                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
> -                else
> -                    res = values[0];
> -                val  = res;
> -                if(vlc[1].table)
> -                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
> -                else
> -                    res = values[1];
> -                val |= res << 8;
> -                pred[0] += val;
> -                *samples++ = pred[0];
> -            }
> +            if (vlc[idx].table)
> +                res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
> +            else
> +                res = values[idx];
> +            val  = res;
> +            if (vlc[++idx].table)
> +                res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
> +            else
> +                res = values[idx];
> +            val |= res << 8;
> +            pred[idx / 2] += val;
> +            *samples++ = pred[idx / 2];
>          }
>      } else { //8-bit data
>          for(i = stereo; i >= 0; i--)
> @@ -709,25 +695,17 @@ static int smka_decode_frame(AVCodecContext *avctx,
> void *data,
>          for(i = 0; i <= stereo; i++)
>              *samples8++ = pred[i];
>          for(; i < unp_size; i++) {
> +            unsigned idx = i & stereo;
>              if (get_bits_left(&gb) < 0) {
>                  ret = AVERROR_INVALIDDATA;
>                  goto error;
>              }
> -            if(i & stereo){
> -                if(vlc[1].table)
> -                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
> -                else
> -                    res = values[1];
> -                pred[1] += res;
> -                *samples8++ = pred[1];
> -            } else {
> -                if(vlc[0].table)
> -                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
> -                else
> -                    res = values[0];
> -                pred[0] += res;
> -                *samples8++ = pred[0];
> -            }
> +            if (vlc[idx].table)
> +                val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
> +            else
> +                val = values[idx];
> +            pred[idx] += val;
> +            *samples8++ = pred[idx];
>          }
>      }
>
> --
> 2.20.1
>

LGTM

> _______________________________________________
> 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".
diff mbox series

Patch

diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c
index d2b1c68162..ffd24c11e7 100644
--- a/libavcodec/smacker.c
+++ b/libavcodec/smacker.c
@@ -671,37 +671,23 @@  static int smka_decode_frame(AVCodecContext *avctx, void *data,
         for(i = 0; i <= stereo; i++)
             *samples++ = pred[i];
         for(; i < unp_size / 2; i++) {
+            unsigned idx = 2 * (i & stereo);
             if (get_bits_left(&gb) < 0) {
                 ret = AVERROR_INVALIDDATA;
                 goto error;
             }
-            if(i & stereo) {
-                if(vlc[2].table)
-                    res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
-                else
-                    res = values[2];
-                val  = res;
-                if(vlc[3].table)
-                    res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
-                else
-                    res = values[3];
-                val |= res << 8;
-                pred[1] += val;
-                *samples++ = pred[1];
-            } else {
-                if(vlc[0].table)
-                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
-                else
-                    res = values[0];
-                val  = res;
-                if(vlc[1].table)
-                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
-                else
-                    res = values[1];
-                val |= res << 8;
-                pred[0] += val;
-                *samples++ = pred[0];
-            }
+            if (vlc[idx].table)
+                res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
+            else
+                res = values[idx];
+            val  = res;
+            if (vlc[++idx].table)
+                res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
+            else
+                res = values[idx];
+            val |= res << 8;
+            pred[idx / 2] += val;
+            *samples++ = pred[idx / 2];
         }
     } else { //8-bit data
         for(i = stereo; i >= 0; i--)
@@ -709,25 +695,17 @@  static int smka_decode_frame(AVCodecContext *avctx, void *data,
         for(i = 0; i <= stereo; i++)
             *samples8++ = pred[i];
         for(; i < unp_size; i++) {
+            unsigned idx = i & stereo;
             if (get_bits_left(&gb) < 0) {
                 ret = AVERROR_INVALIDDATA;
                 goto error;
             }
-            if(i & stereo){
-                if(vlc[1].table)
-                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
-                else
-                    res = values[1];
-                pred[1] += res;
-                *samples8++ = pred[1];
-            } else {
-                if(vlc[0].table)
-                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
-                else
-                    res = values[0];
-                pred[0] += res;
-                *samples8++ = pred[0];
-            }
+            if (vlc[idx].table)
+                val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
+            else
+                val = values[idx];
+            pred[idx] += val;
+            *samples8++ = pred[idx];
         }
     }