Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH 2/7] proresdec2: store precomputed EC parameters
Date: Fri, 8 Sep 2023 10:39:10 +0200
Message-ID: <AS8P250MB0744FD6F67618303F8378D648FEDA@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <20230908081508.510-2-christophe.gisquet@gmail.com>

Christophe Gisquet:
> Having the various orders and offsets stored in a codebook is compact
> but causes additional computations. Using instead a table for the
> precomputed results achieve some speedups at the cost of ~132 bytes.
> 
> Around 5% speedup.
> ---
>  libavcodec/proresdec2.c | 54 +++++++++++++++++++++++++++++++++++------
>  1 file changed, 47 insertions(+), 7 deletions(-)
> 
> diff --git a/libavcodec/proresdec2.c b/libavcodec/proresdec2.c
> index 6e243cfc17..65e8b01755 100644
> --- a/libavcodec/proresdec2.c
> +++ b/libavcodec/proresdec2.c
> @@ -427,6 +427,7 @@ static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, cons
>  # define READ_BITS get_bits
>  #endif
>  
> +/* Kept for reference and because clearer for first DC */
>  #define DECODE_CODEWORD(val, codebook)                                  \
>      do {                                                                \
>          unsigned int rice_order, exp_order, switch_bits;                \
> @@ -454,18 +455,41 @@ static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, cons
>          }                                                               \
>      } while (0)
>  
> +/* number of bits to switch between rice and exp golomb */
> +#define DECODE_CODEWORD2(val, switch_bits, rice_order, diff, offset)    \
> +    do {                                                                \
> +        unsigned int q, buf, bits;                                      \
> +                                                                        \
> +        buf = show_bits(gb, 14);                                        \
> +        q = 13 - av_log2(buf);                                          \
> +                                                                        \
> +        if (q > switch_bits) { /* exp golomb */                         \
> +            bits = (q<<1) + (int)diff;                                  \
> +            val = READ_BITS(gb, bits) + (int)offset;                    \
> +        } else if (rice_order) {                                        \
> +            skip_remaining(gb, q+1);                                    \
> +            val = (q << rice_order) + get_bits(gb, rice_order);         \
> +        } else {                                                        \
> +            val = q;                                                    \
> +            skip_remaining(gb, q+1);                                    \
> +        }                                                               \
> +    } while (0)
> +
> +
>  #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
>  
>  #define FIRST_DC_CB 0xB8
>  
> -static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
> +static const char dc_codebook[7][4] = {
> +    { 0, 0, 1, -1 }, { 0, 1, 2, -2 }, { 0, 1, 2, -2 },
> +    { 1, 2, 2,  0 }, { 1, 2, 2,  0 }, { 0, 3, 4, -8 }, { 0, 3, 4, -8 }
> +};
>  
>  static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
>                                                int blocks_per_slice)
>  {
>      int16_t prev_dc;
>      int code, i, sign;
> -
>      DECODE_CODEWORD(code, FIRST_DC_CB);
>      prev_dc = TOSIGNED(code);
>      out[0] = prev_dc;
> @@ -475,7 +499,9 @@ static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
>      code = 5;
>      sign = 0;
>      for (i = 1; i < blocks_per_slice; i++, out += 64) {
> -        DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)]);
> +        unsigned int dccb = FFMIN(code, 6U);
> +        DECODE_CODEWORD2(code, dc_codebook[dccb][0], dc_codebook[dccb][1],
> +                               dc_codebook[dccb][2], dc_codebook[dccb][3]);
>          if(code) sign ^= -(code & 1);
>          else     sign  = 0;
>          prev_dc += (((code + 1) >> 1) ^ sign) - sign;
> @@ -485,8 +511,18 @@ static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
>  }
>  
>  // adaptive codebook switching lut according to previous run/level values
> -static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
> -static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
> +static const char run_to_cb[16][4] = {
> +    { 2, 0, -1,  1 }, { 2, 0, -1,  1 }, { 1, 0, 0,  0 }, { 1, 0,  0,  0 }, { 0, 0, 1, -1 },
> +    { 1, 1,  1,  0 }, { 1, 1,  1,  0 }, { 1, 1, 1,  0 }, { 1, 1,  1,  0 },
> +    { 0, 1,  2, -2 }, { 0, 1,  2, -2 }, { 0, 1, 2, -2 }, { 0, 1,  2, -2 }, { 0, 1, 2, -2 }, { 0, 1, 2, -2 },
> +    { 0, 2,  3, -4 }
> +};
> +
> +static const char lev_to_cb[10][4] = {

You seem to require signed chars here, but this is not a given on many
platforms. Better use int8_t.

> +    { 0, 0,  1, -1 }, { 2, 0,  0, -1 }, { 1, 0, 0,  0 }, { 2, 0, -1,  1 }, { 0, 0, 1, -1 },
> +    { 0, 1,  2, -2 }, { 0, 1,  2, -2 }, { 0, 1, 2, -2 }, { 0, 1,  2, -2 },
> +    { 0, 2,  3, -4 }
> +};
>  
>  static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
>                                               int16_t *out, int blocks_per_slice)
> @@ -504,18 +540,22 @@ static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContex
>      block_mask = blocks_per_slice - 1;
>  
>      for (pos = block_mask;;) {
> +        unsigned int runcb = FFMIN(run,  15);
> +        unsigned int levcb = FFMIN(level, 9);
>          bits_rem = get_bits_left(gb);
>          if (!bits_rem || (bits_rem < 16 && !show_bits(gb, bits_rem)))
>              break;
>  
> -        DECODE_CODEWORD(run, run_to_cb[FFMIN(run,  15)]);
> +        DECODE_CODEWORD2(run, run_to_cb[runcb][0], run_to_cb[runcb][1],
> +                              run_to_cb[runcb][2], run_to_cb[runcb][3]);
>          pos += run + 1;
>          if (pos >= max_coeffs) {
>              av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
>              return AVERROR_INVALIDDATA;
>          }
>  
> -        DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
> +        DECODE_CODEWORD2(level, lev_to_cb[levcb][0], lev_to_cb[levcb][1],
> +                                lev_to_cb[levcb][2], lev_to_cb[levcb][3]);
>          level += 1;
>  
>          i = pos >> log2_block_count;

_______________________________________________
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".

  reply	other threads:[~2023-09-08  8:38 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08  8:15 [FFmpeg-devel] [PATCH 1/7] proresdec2: port and fix for cached reader Christophe Gisquet
2023-09-08  8:15 ` [FFmpeg-devel] [PATCH 2/7] proresdec2: store precomputed EC parameters Christophe Gisquet
2023-09-08  8:39   ` Andreas Rheinhardt [this message]
2023-09-08  8:15 ` [FFmpeg-devel] [PATCH 3/7] proresdec2: use VLC for level instead of EC switch Christophe Gisquet
2023-09-08  8:44   ` Andreas Rheinhardt
2023-09-08  9:58     ` Andreas Rheinhardt
2023-09-10 15:28       ` Christophe Gisquet
2023-09-10 15:41         ` Andreas Rheinhardt
2023-09-10 15:56           ` Christophe Gisquet
2023-09-08  8:15 ` [FFmpeg-devel] [PATCH 4/7] proresdec2: offset VLCs by 1 to avoid 1 add Christophe Gisquet
2023-09-08  8:15 ` [FFmpeg-devel] [PATCH 5/7] proresdec2: use VLC for small runs and levels Christophe Gisquet
2023-09-08  8:15 ` [FFmpeg-devel] [PATCH 6/7] proresdec2: remove a useless DC codebook entry Christophe Gisquet
2023-09-08  9:08   ` Andreas Rheinhardt
2023-09-08  8:15 ` [FFmpeg-devel] [PATCH 7/7] prores: use VLC LUTs Christophe Gisquet
2023-09-08  9:20   ` Andreas Rheinhardt
2023-09-08  9:58     ` Christophe Gisquet
2023-09-08  8:20 ` [FFmpeg-devel] [PATCH 1/7] proresdec2: port and fix for cached reader Christophe Gisquet
2023-09-08  8:30   ` Andreas Rheinhardt
2023-09-08  8:34     ` Andreas Rheinhardt
2023-09-11 20:54   ` Christophe Gisquet
2023-09-08  8:36 ` Andreas Rheinhardt
2023-09-08 21:00 ` Michael Niedermayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AS8P250MB0744FD6F67618303F8378D648FEDA@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git