[FFmpeg-devel] [PATCH] Moves yuv2yuvX_sse3 to yasm, unrolls main loop and other small optimizations for ~20% speedup.

2020-11-10 Thread Alan Kelly
---
 yuv2yuvX.asm: Ports yuv2yuvX to asm, unrolls main loop and adds
 other small optimizations for ~20% speed-up. Copyright updated to
 include the original from swscale.c
 swscale.c: Removes yuv2yuvX_sse3 and calls new function ff_yuv2yuvX_sse3.
 Calls yuv2yuvX_mmxext on remainining elements if required.
 Makefile: Compiles yuv2yuvX.asm

 libswscale/x86/Makefile |   1 +
 libswscale/x86/swscale.c|  75 
 libswscale/x86/yuv2yuvX.asm | 110 
 3 files changed, 121 insertions(+), 65 deletions(-)
 create mode 100644 libswscale/x86/yuv2yuvX.asm

diff --git a/libswscale/x86/Makefile b/libswscale/x86/Makefile
index 831d5359aa..bfe383364e 100644
--- a/libswscale/x86/Makefile
+++ b/libswscale/x86/Makefile
@@ -13,3 +13,4 @@ X86ASM-OBJS += x86/input.o
  \
x86/scale.o  \
x86/rgb_2_rgb.o  \
x86/yuv_2_rgb.o  \
+   x86/yuv2yuvX.o   \
diff --git a/libswscale/x86/swscale.c b/libswscale/x86/swscale.c
index 3160fedf04..fec9fa22e0 100644
--- a/libswscale/x86/swscale.c
+++ b/libswscale/x86/swscale.c
@@ -197,80 +197,25 @@ void ff_updateMMXDitherTables(SwsContext *c, int dstY)
 }
 
 #if HAVE_MMXEXT
+void ff_yuv2yuvX_sse3(const int16_t *filter, int filterSize,
+   uint8_t *dest, int dstW,
+   const uint8_t *dither, int offset);
+
 static void yuv2yuvX_sse3(const int16_t *filter, int filterSize,
const int16_t **src, uint8_t *dest, int dstW,
const uint8_t *dither, int offset)
 {
+int remainder = (dstW % 32);
+int pixelsProcessed = dstW - remainder;
 if(((uintptr_t)dest) & 15){
 yuv2yuvX_mmxext(filter, filterSize, src, dest, dstW, dither, offset);
 return;
 }
-filterSize--;
-#define MAIN_FUNCTION \
-"pxor   %%xmm0, %%xmm0 \n\t" \
-"punpcklbw  %%xmm0, %%xmm3 \n\t" \
-"movd   %4, %%xmm1 \n\t" \
-"punpcklwd  %%xmm1, %%xmm1 \n\t" \
-"punpckldq  %%xmm1, %%xmm1 \n\t" \
-"punpcklqdq %%xmm1, %%xmm1 \n\t" \
-"psllw  $3, %%xmm1 \n\t" \
-"paddw  %%xmm1, %%xmm3 \n\t" \
-"psraw  $4, %%xmm3 \n\t" \
-"movdqa %%xmm3, %%xmm4 \n\t" \
-"movdqa %%xmm3, %%xmm7 \n\t" \
-"movl   %3, %%ecx  \n\t" \
-"mov %0, %%"FF_REG_d"\n\t"\
-"mov(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
-".p2align 4 \n\t" /* FIXME 
Unroll? */\
-"1: \n\t"\
-"movddup  8(%%"FF_REG_d"), %%xmm0   \n\t" /* 
filterCoeff */\
-"movdqa  (%%"FF_REG_S", %%"FF_REG_c", 2), %%xmm2 \n\t" /* 
srcData */\
-"movdqa16(%%"FF_REG_S", %%"FF_REG_c", 2), %%xmm5 \n\t" /* 
srcData */\
-"add$16, %%"FF_REG_d"\n\t"\
-"mov(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
-"test %%"FF_REG_S", %%"FF_REG_S" \n\t"\
-"pmulhw   %%xmm0, %%xmm2  \n\t"\
-"pmulhw   %%xmm0, %%xmm5  \n\t"\
-"paddw%%xmm2, %%xmm3  \n\t"\
-"paddw%%xmm5, %%xmm4  \n\t"\
-" jnz1b \n\t"\
-"psraw   $3, %%xmm3  \n\t"\
-"psraw   $3, %%xmm4  \n\t"\
-"packuswb %%xmm4, %%xmm3  \n\t"\
-"movntdq  %%xmm3, (%1, %%"FF_REG_c") \n\t"\
-"add $16, %%"FF_REG_c"\n\t"\
-"cmp  %2, %%"FF_REG_c"\n\t"\
-"movdqa   %%xmm7, %%xmm3\n\t" \
-"movdqa   %%xmm7, %%xmm4\n\t" \
-"mov %0, %%"FF_REG_d"\n\t"\
-"mov(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
-"jb  1b \n\t"
-
-if (offset) {
-__asm__ volatile(
-"movq  %5, %%xmm3  \n\t"
-"movdqa%%xmm3, %%xmm4  \n\t"
-"psrlq$24, %%xmm3  \n\t"
-"psllq$40, %%xmm4  \n\t"
-"por   %%xmm4, %%xmm3  \n\t"
-MAIN_FUNCTION
-  :: "g" (filter),
-  "r" (dest-offset), "g" ((x86_reg)(dstW+offset)), "m" (offset),
-  "m"(filterSize), "m"(((

[FFmpeg-devel] libffmpeg.so with h264 codec support

2020-11-10 Thread Budi
I tried to build libffmpeg.so with h264 and h265 codec so go to compile
as on: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

but cannot find the exact explanation of this dynamic link build as all refer
tostandalone executable  build
How is actually the definitive way to compile/build libffmpeg.so with
h264/5 support? THANKS
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] libffmpeg.so with h264 codec support

2020-11-10 Thread Nicolas George
Budi (12020-11-10):
> I tried to build libffmpeg.so with h264 and h265 codec so go to compile
> as on: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
> 
> but cannot find the exact explanation of this dynamic link build as all refer
> tostandalone executable  build
> How is actually the definitive way to compile/build libffmpeg.so with
> h264/5 support? THANKS

It has already been told to you that this library does not exist in this
project at all. Please read the replies and do not ask again.

Regards,

-- 
  Nicolas George


signature.asc
Description: PGP signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 000/114] VLC, esp. init_vlc patches

2020-11-10 Thread Andreas Rheinhardt
This patchset provides an alternative to ff_init_vlc_sparse() to
initialize VLCs: ff_init_vlc_from_lengths(). It is based upon the
observation that the codes of a complete VLC can be easily computed
from the lengths if the lengths are ordered according to "left elements
in the tree first". This enables one to initialize trees via lengths and
symbol tables, making the codes redundant. This enables space savings if
a VLC is initialized by codes, (explicit) symbols and lengths or if the
symbols fit in a smaller type than the codes (as is not uncommon). It
also means that one can avoid an implicit qsort that is performed in
ff_init_vlc_sparse() in order to ensure that the codes destined for
the same subtable are contiguous, because the created codes
automatically have this property.

But this of course also restricts its applicability to scenarios where
lengths and symbols are naturally ordered from left to right in the
tree or where it is easy to order them in such a way; the first happens
with several codecs (e.g. Smacker, Theora, TrueMotion 2.0), the latter
includes all static tables as they can be sorted once and for all.

All in all, this saves about 130KB from the binary.

Andreas Rheinhardt (114):
  avcodec/bitstream: Add second function to create VLCs
  avcodec/tscc2: Reduce the size of the tables used to initialize VLCs
  avcodec/tscc2: Combine tables for initializing VLCs
  avcodec/bitstream: Allow static VLC tables to be bigger than needed
  avcodec/tscc2: Make VLC tables static
  avcodec/cllc: Improve creating VLCs
  avcodec/smacker: Improve creating Huffman VLC tables
  avcodec/on2avc: Use smaller tables for VLCs
  avcodec/on2avcdata: Combine tables for codebooks
  avcodec/on2avc: Apply offset when initializing VLC
  avcodec/imc: Make Huffman tables smaller
  avcodec/imc: Avoid offsets table when creating VLCs
  avcodec/clearvideo: Avoid code tables for initializing VLCs
  avcodec/clearvideo: Avoid code duplication when initializing VLCs
  avcodec/clearvideo: Avoid huge VLC length tables
  avcodec/clearvideo: Inline constants
  avcodec/clearvideo: Use minimal max_depth in get_vlc2()
  avcodec/clearvideo: Apply VLC offset during init
  avcodec/clearvideo: Make VLC tables static
  avcodec/wnv1: Make array for initializing VLC smaller
  avcodec/wnv1: Apply offset during init, not later every time
  avcodec/cook: Make tables to initialize VLCs smaller
  avcodec/cook: Apply offset when initializing VLC table
  avcodec/cook: Avoid big length tables for VLC initialization
  avcodec/cook: Inline constants
  avcodec/rv10: Reduce the size of the tables used to initialize VLCs
  avcodec/rv10: Reduce number of exceptions when reading VLC value
  avcodec/rv10: Use symbol table more effectively
  avcodec/rv10: Make VLC tables smaller
  avcodec/rv10: Simplify handling of skip VLC entries
  avcodec/mimic: Reduce size of tables used to initialize VLCs
  avcodec/mimic: Make VLC static
  avcodec/motionpixels: Only create VLC iff it is going to be used
  avcodec/motionpixels: Be more strict when parsing Huffman trees
  avcodec/motionpixels: Don't check for complete VLC
  avcodec/motionpixels: Use symbols table
  avcodec/motionpixels: Simplify creating VLC tables
  avcodec/mpc7: Reduce size of tables used to initialize VLCs
  avcodec/mpc7: Apply offsets when creating VLCs
  avcodec/mpc7: Avoid offsets table when creating VLCs
  avcodec/mpc8: Reduce size of tables used to initialize VLCs
  avcodec/mpc8: Apply offsets when initializing VLCs
  avcodec/mpc8: Reduce the size of the length tables to initialize VLCs
  avcodec/mpc8: Reduce the size of some VLCs
  avcodec/mpc8: Avoid code duplication when initializing VLCs
  avcodec/mobiclip: Avoid redundant codes table to initialize VLCs
  avcodec/mobiclip: Avoid code duplication when initializing VLCs
  avcodec/mobiclip: Reindentation
  avcodec/qdm2: Make tables used to initialize VLCs smaller
  avcodec/qdm2_tablegen: Avoid code duplication when creating VLCs
  avcodec/qdm2: Apply offsets when initializing VLCs
  avcodec/qdm2: Initialize array of VLCs in a loop
  avcodec/qdm2: Avoid offsets table when initializing VLCs
  avcodec/rv40vlc2: Make VLC smaller
  avcodec/rv40: Make the tables used to initialize VLCs smaller
  avcodec/rv40: Make better use of VLC symbols table
  avcodec/rv40: Avoid offset table when initializing static VLCs
  avcodec/rv40: Avoid code duplication when initializing VLCs
  avcodec/mss4: Simplify creating VLC tables
  avcodec/mss4: Don't duplicate standard JPEG tables
  avcodec/mss4: Make VLCs static
  avcodec/sheervideo: Add av_cold to build_vlc()
  avcodec/sheervideo: Simplify creating VLC table
  avcodec/g2meet: Use least max_depth in get_vlc2()
  avcodec/mjpegdec: Simplify creating VLC table
  avcodec/indeo2: Make tables used to initialize VLCs smaller
  avcodec/mpeg4video: Make tables used to initialize VLCs smaller
  avcodec/mpeg4videodec: Make studio VLCs static
  avcodec/truemotion2: Simplify creating VLC table
  avcodec/atrac3plus: Simplify cr

[FFmpeg-devel] [PATCH 005/114] avcodec/tscc2: Make VLC tables static

2020-11-10 Thread Andreas Rheinhardt
Also use a named constant for the number of bits of the VLC tables.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/tscc2.c | 73 --
 1 file changed, 32 insertions(+), 41 deletions(-)

diff --git a/libavcodec/tscc2.c b/libavcodec/tscc2.c
index 7a591899c1..c5ce1cb74b 100644
--- a/libavcodec/tscc2.c
+++ b/libavcodec/tscc2.c
@@ -34,6 +34,8 @@
 #include "mathops.h"
 #include "tscc2data.h"
 
+#define TSCC2_VLC_BITS 9
+
 typedef struct TSCC2Context {
 AVCodecContext *avctx;
 AVFrame   *pic;
@@ -43,52 +45,46 @@ typedef struct TSCC2Context {
 intq[2][3];
 GetBitContext  gb;
 
-VLCdc_vlc, nc_vlc[NUM_VLC_SETS], ac_vlc[NUM_VLC_SETS];
 intblock[16];
 } TSCC2Context;
 
-static av_cold void free_vlcs(TSCC2Context *c)
-{
-int i;
+static VLC dc_vlc, nc_vlc[NUM_VLC_SETS], ac_vlc[NUM_VLC_SETS];
 
-ff_free_vlc(&c->dc_vlc);
-for (i = 0; i < NUM_VLC_SETS; i++) {
-ff_free_vlc(c->nc_vlc + i);
-ff_free_vlc(c->ac_vlc + i);
-}
+static av_cold void tscc2_init_vlc(VLC *vlc, int *offset, int nb_codes,
+   const uint8_t *lens, const void *syms,
+   int sym_length)
+{
+static VLC_TYPE vlc_buf[15442][2];
+
+vlc->table   = &vlc_buf[*offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *offset;
+ff_init_vlc_from_lengths(vlc, TSCC2_VLC_BITS, nb_codes,
+ lens, 1, syms, sym_length, sym_length,
+ 0, INIT_VLC_USE_NEW_STATIC | INIT_VLC_OUTPUT_LE);
+*offset += vlc->table_size;
 }
 
-static av_cold int init_vlcs(TSCC2Context *c)
+static av_cold void tscc2_init_vlcs(void)
 {
 const uint16_t *ac_vlc_syms = tscc2_ac_vlc_syms;
 const uint8_t  *ac_vlc_lens = tscc2_ac_vlc_lens;
-int i, ret;
+int i, offset = 0;
 
-ret = ff_init_vlc_from_lengths(&c->dc_vlc, 9, DC_VLC_COUNT,
-   tscc2_dc_vlc_lens, 1,
-   tscc2_dc_vlc_syms, 2, 2,
-   0, INIT_VLC_OUTPUT_LE);
-if (ret)
-return ret;
+if (ac_vlc[NUM_VLC_SETS - 1].table_size)
+return;
+
+tscc2_init_vlc(&dc_vlc, &offset, DC_VLC_COUNT,
+   tscc2_dc_vlc_lens, tscc2_dc_vlc_syms, 2);
 
 for (i = 0; i < NUM_VLC_SETS; i++) {
-ret = ff_init_vlc_from_lengths(c->nc_vlc + i, 9, 16,
-   tscc2_nc_vlc_lens[i], 1,
-   tscc2_nc_vlc_syms[i], 1, 1,
-   0, INIT_VLC_OUTPUT_LE);
-if (ret)
-return ret;
-ret = ff_init_vlc_from_lengths(c->ac_vlc + i, 9, tscc2_ac_vlc_sizes[i],
-   ac_vlc_lens, 1,
-   ac_vlc_syms, 2, 2,
-   0, INIT_VLC_OUTPUT_LE);
-if (ret)
-return ret;
+tscc2_init_vlc(&nc_vlc[i], &offset, 16,
+   tscc2_nc_vlc_lens[i], tscc2_nc_vlc_syms[i], 1);
+
+tscc2_init_vlc(&ac_vlc[i], &offset, tscc2_ac_vlc_sizes[i],
+   ac_vlc_lens, ac_vlc_syms, 2);
 ac_vlc_lens += tscc2_ac_vlc_sizes[i];
 ac_vlc_syms += tscc2_ac_vlc_sizes[i];
 }
-
-return 0;
 }
 
 #define DEQUANT(val, q) (((q) * (val) + 0x80) >> 8)
@@ -154,7 +150,7 @@ static int tscc2_decode_mb(TSCC2Context *c, int *q, int 
vlc_set,
 if (!(j | k)) {
 dc = get_bits(gb, 8);
 } else {
-dc = get_vlc2(gb, c->dc_vlc.table, 9, 2);
+dc = get_vlc2(gb, dc_vlc.table, TSCC2_VLC_BITS, 2);
 if (dc == 0x100)
 dc = get_bits(gb, 8);
 }
@@ -162,12 +158,12 @@ static int tscc2_decode_mb(TSCC2Context *c, int *q, int 
vlc_set,
 prev_dc = dc;
 c->block[0] = dc;
 
-nc = get_vlc2(gb, c->nc_vlc[vlc_set].table, 9, 1);
+nc = get_vlc2(gb, nc_vlc[vlc_set].table, TSCC2_VLC_BITS, 1);
 
 bpos = 1;
 memset(c->block + 1, 0, 15 * sizeof(*c->block));
 for (l = 0; l < nc; l++) {
-ac = get_vlc2(gb, c->ac_vlc[vlc_set].table, 9, 2);
+ac = get_vlc2(gb, ac_vlc[vlc_set].table, TSCC2_VLC_BITS, 2);
 if (ac == 0x1000)
 ac = get_bits(gb, 12);
 bpos += ac & 0xF;
@@ -330,7 +326,6 @@ static av_cold int tscc2_decode_end(AVCodecContext *avctx)
 
 av_frame_free(&c->pic);
 av_freep(&c->slice_quants);
-free_vlcs(c);
 
 return 0;
 }
@@ -338,17 +333,11 @@ static av_cold int tscc2_decode_end(AVCodecContext *avctx)
 static av_cold int tscc2_decode_init(AVCodecContext *avctx)
 {
 TSCC2Context * const c = avctx->priv_data;
-int ret;
 
 c->avctx = avctx;
 
 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
 
-if ((ret = init_vlcs(c)) < 0

[FFmpeg-devel] [PATCH 002/114] avcodec/tscc2: Reduce the size of the tables used to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
After permuting both the codes, lengths and symbols tables so that
the codes tables are ordered from left to right in the tree, the codes
tables can be easily computed from the lengths tables at runtime and
therefore omitted. This saves about 2KB from the binary.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/tscc2.c |   24 +-
 libavcodec/tscc2data.h | 1195 +---
 2 files changed, 393 insertions(+), 826 deletions(-)

diff --git a/libavcodec/tscc2.c b/libavcodec/tscc2.c
index 9fa28fe9fc..0552d775c5 100644
--- a/libavcodec/tscc2.c
+++ b/libavcodec/tscc2.c
@@ -62,24 +62,24 @@ static av_cold int init_vlcs(TSCC2Context *c)
 {
 int i, ret;
 
-ret = ff_init_vlc_sparse(&c->dc_vlc, 9, DC_VLC_COUNT,
- tscc2_dc_vlc_bits,  1, 1,
- tscc2_dc_vlc_codes, 2, 2,
- tscc2_dc_vlc_syms,  2, 2, INIT_VLC_LE);
+ret = ff_init_vlc_from_lengths(&c->dc_vlc, 9, DC_VLC_COUNT,
+   tscc2_dc_vlc_lens, 1,
+   tscc2_dc_vlc_syms, 2, 2,
+   0, INIT_VLC_OUTPUT_LE);
 if (ret)
 return ret;
 
 for (i = 0; i < NUM_VLC_SETS; i++) {
-ret = ff_init_vlc_sparse(c->nc_vlc + i, 9, 16,
- tscc2_nc_vlc_bits[i],  1, 1,
- tscc2_nc_vlc_codes[i], 2, 2,
- tscc2_nc_vlc_syms, 1, 1, INIT_VLC_LE);
+ret = ff_init_vlc_from_lengths(c->nc_vlc + i, 9, 16,
+   tscc2_nc_vlc_lens[i], 1,
+   tscc2_nc_vlc_syms[i], 1, 1,
+   0, INIT_VLC_OUTPUT_LE);
 if (ret)
 return ret;
-ret = ff_init_vlc_sparse(c->ac_vlc + i, 9, tscc2_ac_vlc_sizes[i],
- tscc2_ac_vlc_bits[i],  1, 1,
- tscc2_ac_vlc_codes[i], 2, 2,
- tscc2_ac_vlc_syms[i],  2, 2, INIT_VLC_LE);
+ret = ff_init_vlc_from_lengths(c->ac_vlc + i, 9, tscc2_ac_vlc_sizes[i],
+   tscc2_ac_vlc_bits[i], 1,
+   tscc2_ac_vlc_syms[i], 2, 2,
+   0, INIT_VLC_OUTPUT_LE);
 if (ret)
 return ret;
 }
diff --git a/libavcodec/tscc2data.h b/libavcodec/tscc2data.h
index 5e8d18b3f5..2ccfa3b811 100644
--- a/libavcodec/tscc2data.h
+++ b/libavcodec/tscc2data.h
@@ -37,866 +37,441 @@ static const uint16_t tscc2_quants[NUM_VLC_SETS][3] = {
 #define DC_VLC_COUNT 47
 
 static const uint16_t tscc2_dc_vlc_syms[DC_VLC_COUNT] = {
-   0x100, 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9,
-0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1,
-0xF0, 0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0x17,
-0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0F,
-0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07,
-0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
-};
-
-static const uint16_t tscc2_dc_vlc_codes[DC_VLC_COUNT] = {
-0x000A, 0x, 0x0006, 0x002E, 0x0002, 0x000E, 0x001A, 0x007E,
-0x004E, 0x005A, 0x00E2, 0x01BE, 0x01BA, 0x00BA, 0x0072, 0x0022,
-0x02BE, 0x00BE, 0x033A, 0x03F2, 0x01F2, 0x03A2, 0x0122, 0x0322,
-0x01A2, 0x0172, 0x0372, 0x013A, 0x00CE, 0x02CE, 0x02FE, 0x00FE,
-0x00A2, 0x00F2, 0x003A, 0x01CE, 0x01FE, 0x0062, 0x00DA, 0x003E,
-0x0032, 0x007A, 0x0012, 0x001E, 0x0016, 0x0004, 0x0001,
-};
-
-static const uint8_t tscc2_dc_vlc_bits[DC_VLC_COUNT] = {
- 5,  3,  5,  6,  6,  7,  7,  8,
- 8,  8,  8,  9,  9,  9,  9,  9,
-10, 10, 10, 10, 10, 10, 10, 10,
-10, 10, 10, 10, 10, 10, 10, 10,
- 9,  9,  9,  9,  9,  8,  8,  8,
- 7,  7,  6,  6,  5,  3,  1,
-};
-
-static const uint8_t tscc2_nc_vlc_syms[16] = {
-0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
-0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
-};
-
-static const uint16_t tscc2_nc_vlc_codes[NUM_VLC_SETS][16] = {
-{ 0x0023, 0x0003, 0x0031, 0x0021, 0x0011, 0x0053, 0x0013, 0x0001,
-  0x0009, 0x0029, 0x0033, 0x0019, 0x000B, 0x0005, 0x0007, 0x, },
-{ 0x0030, 0x0022, 0x0028, 0x0020, 0x0008, 0x, 0x0032, 0x0072,
-  0x0010, 0x0002, 0x0012, 0x0018, 0x000A, 0x0004, 0x0006, 0x0001, },
-{ 0x0032, 0x0038, 0x0020, 0x0008, 0x0002, 0x0018, 0x0072, 0x,
-  0x0028, 0x0022, 0x0012, 0x0010, 0x000A, 0x0004, 0x0006, 0x0001, },
-{ 0x0032, 0x0010, 0x0072, 0x0030, 0x0022, 0x0038, 0x0008, 0x0028,
-  0x0018, 0x0002, 0x0012, 0x, 0x000A, 0x0004, 0x0006, 0x0001, },
-{ 0x0012, 0x0052, 0x, 0x0020, 0x0018, 0x0022, 0x0008, 0x0028,
-  0x0038, 0x0002, 0x0032, 0x0010, 0x000A, 0x0004, 0x0006, 0x0001, },
-{ 0x0016, 0x0096, 0x0006, 0x0046, 0x0056, 0x0002, 0x0036, 0x0076,
-  0x0012, 0x0022, 0x0032, 0x0026, 0x000A, 0x000E, 0x, 0x0001, },
-{ 0x001A, 0x009A, 0x0032, 0x0072, 0x005A, 0x007A, 0x003A, 0x0002,
-  0x0012, 0x002

[FFmpeg-devel] [PATCH 001/114] avcodec/bitstream: Add second function to create VLCs

2020-11-10 Thread Andreas Rheinhardt
When using ff_init_vlc_sparse() to create a VLC, three input tables are
used: A table for lengths, one for codes and one for symbols; the latter
one can be omitted, then a default one will be used. These input tables
will be traversed twice, once to get the long codes (which will be put
into subtables) and once for the small codes. The long codes are then
sorted so that entries that should be in the same subtable are
contiguous.

This commit adds an alternative to ff_init_vlc_sparse():
ff_init_vlc_from_lengths(). It is based upon the observation that if
lengths, codes and symbols tables are permuted (in the same way) so that
the codes are ordered from left to right in the corresponding tree and
if said tree is complete (i.e. every non-leaf node has two children),
the codes can be easily computed from the lengths and are therefore
redundant. This means that if one initializes such a VLC with explicitly
coded lengths, codes and symbols, the codes can be avoided; and even if
one has no explicitly coded symbols, it might still be beneficial to
remove the codes even when one has to add a new symbol table, because
codes are typically longer than symbols so that the latter often fit
into a smaller type, saving space.

Furthermore, given that the codes here are by definition ordered from
left to right, it is unnecessary to sort them again; for the same
reason, one does not have to traverse the input twice. This function
proved to be faster than ff_init_vlc_sparse() whenever it has been
benchmarked.

This function is usable for static tables (they can simply be permuted
once) as well as in scenarios where the tables are naturally ordered
from left to right in the tree; the latter e.g. happens with Smacker,
Theora and several other formats.

In order to make it also usable for (static) tables with incomplete trees,
negative lengths are used to indicate that there is an open end of a
certain length.

Finally, ff_init_vlc_from_lengths() has one downside compared to
ff_init_vlc_sparse(): The latter uses tables that can be reused by
encoders. Of course, one could calculate the needed table at runtime
if one so wishes, but it is nevertheless an obstacle.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/bitstream.c | 138 ++---
 libavcodec/vlc.h   |  47 ++
 2 files changed, 149 insertions(+), 36 deletions(-)

diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c
index c7a87734e5..03d39ad88c 100644
--- a/libavcodec/bitstream.c
+++ b/libavcodec/bitstream.c
@@ -132,6 +132,8 @@ static int alloc_table(VLC *vlc, int size, int use_static)
 return index;
 }
 
+#define LOCALBUF_ELEMS 1500 // the maximum currently needed is 1296 by rv34
+
 typedef struct VLCcode {
 uint8_t bits;
 VLC_TYPE symbol;
@@ -140,6 +142,31 @@ typedef struct VLCcode {
 uint32_t code;
 } VLCcode;
 
+static int vlc_common_init(VLC *vlc_arg, int nb_bits, int nb_codes,
+   VLC **vlc, VLC *localvlc, VLCcode **buf,
+   int flags)
+{
+*vlc = vlc_arg;
+(*vlc)->bits = nb_bits;
+if (flags & INIT_VLC_USE_NEW_STATIC) {
+av_assert0(nb_codes <= LOCALBUF_ELEMS);
+*localvlc = *vlc_arg;
+*vlc = localvlc;
+(*vlc)->table_size = 0;
+} else {
+(*vlc)->table   = NULL;
+(*vlc)->table_allocated = 0;
+(*vlc)->table_size  = 0;
+}
+if (nb_codes > LOCALBUF_ELEMS) {
+*buf = av_malloc_array(nb_codes, sizeof(VLCcode));
+if (!*buf)
+return AVERROR(ENOMEM);
+}
+
+return 0;
+}
+
 static int compare_vlcspec(const void *a, const void *b)
 {
 const VLCcode *sa = a, *sb = b;
@@ -248,6 +275,27 @@ static int build_table(VLC *vlc, int table_nb_bits, int 
nb_codes,
 return table_index;
 }
 
+static int vlc_common_end(VLC *vlc, int nb_bits, int nb_codes, VLCcode *codes,
+  int flags, VLC *vlc_arg, VLCcode 
localbuf[LOCALBUF_ELEMS])
+{
+int ret = build_table(vlc, nb_bits, nb_codes, codes, flags);
+
+if (flags & INIT_VLC_USE_NEW_STATIC) {
+if(vlc->table_size != vlc->table_allocated)
+av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, 
vlc->table_allocated);
+
+av_assert0(ret >= 0);
+*vlc_arg = *vlc;
+} else {
+if (codes != localbuf)
+av_free(codes);
+if (ret < 0) {
+av_freep(&vlc->table);
+return ret;
+}
+}
+return 0;
+}
 
 /* Build VLC decoding tables suitable for use with get_vlc().
 
@@ -278,30 +326,14 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int 
nb_codes,
const void *symbols, int symbols_wrap, int symbols_size,
int flags)
 {
-VLCcode *buf;
+VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf;
 int i, j, ret;
-VLCcode localbuf[1500]; // the maximum currently needed is 1296 by rv34
 VLC localvlc, *vlc;
 
-vlc =

[FFmpeg-devel] [PATCH 006/114] avcodec/cllc: Improve creating VLCs

2020-11-10 Thread Andreas Rheinhardt
One can offload the computation of the codes to
ff_init_vlc_from_lengths(); this also improves performance: The number
of decicycles for one call to read_code_table() decreased from 198343
to 148338 with the sample sample-cllc-rgb.avi from the FATE suite; it
has been looped 100 times and the test repeated ten times to test it
sufficiently often.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/cllc.c | 14 +++---
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/libavcodec/cllc.c b/libavcodec/cllc.c
index 8b1bc75faa..24821f8c16 100644
--- a/libavcodec/cllc.c
+++ b/libavcodec/cllc.c
@@ -46,11 +46,9 @@ static int read_code_table(CLLCContext *ctx, GetBitContext 
*gb, VLC *vlc)
 {
 uint8_t symbols[256];
 uint8_t bits[256];
-uint16_t codes[256];
-int num_lens, num_codes, num_codes_sum, prefix;
+int num_lens, num_codes, num_codes_sum;
 int i, j, count;
 
-prefix= 0;
 count = 0;
 num_codes_sum = 0;
 
@@ -74,19 +72,13 @@ static int read_code_table(CLLCContext *ctx, GetBitContext 
*gb, VLC *vlc)
 for (j = 0; j < num_codes; j++) {
 symbols[count] = get_bits(gb, 8);
 bits[count]= i + 1;
-codes[count]   = prefix++;
 
 count++;
 }
-if (prefix > (65535 - 256)/2) {
-return AVERROR_INVALIDDATA;
-}
-
-prefix <<= 1;
 }
 
-return ff_init_vlc_sparse(vlc, VLC_BITS, count, bits, 1, 1,
-  codes, 2, 2, symbols, 1, 1, 0);
+return ff_init_vlc_from_lengths(vlc, VLC_BITS, count, bits, 1,
+symbols, 1, 1, 0, 0);
 }
 
 /*
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 007/114] avcodec/smacker: Improve creating Huffman VLC tables

2020-11-10 Thread Andreas Rheinhardt
The Smacker Huffman tables are already stored in a tree-like structure;
in particular, they are naturally ordered from left to right in the
tree and are therefore suitable to be initialized by
ff_init_vlc_from_lengths() which avoids traversing the data twice in
order to sort only the codes that are so long that they need into a
subtable.

This improves performance (and reduces codesize): For the sample from
ticket #2425 the number of decicycles for parsing and creating the VLCs
in smka_decode_frame() decreased from 412322 to 359152 (tested with
10 runs each looping 20 times over the file).

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/smacker.c | 46 +---
 1 file changed, 22 insertions(+), 24 deletions(-)

diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c
index 6b1faec09e..19429f3b3e 100644
--- a/libavcodec/smacker.c
+++ b/libavcodec/smacker.c
@@ -62,14 +62,17 @@ typedef struct SmackVContext {
 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
 } SmackVContext;
 
+typedef struct HuffEntry {
+uint8_t value;
+uint8_t length;
+} HuffEntry;
+
 /**
  * Context used for code reconstructing
  */
 typedef struct HuffContext {
 int current;
-uint32_t bits[256];
-uint8_t lengths[256];
-uint8_t values[256];
+HuffEntry entries[256];
 } HuffContext;
 
 /* common parameters used for decode_bigtree */
@@ -105,7 +108,7 @@ enum SmkBlockTypes {
  * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
  * does not overread gb on success.
  */
-static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t 
prefix, int length)
+static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, int length)
 {
 if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
 av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
@@ -119,18 +122,15 @@ static int smacker_decode_tree(GetBitContext *gb, 
HuffContext *hc, uint32_t pref
 }
 if (get_bits_left(gb) < 8)
 return AVERROR_INVALIDDATA;
-hc->bits[hc->current]= prefix;
-hc->lengths[hc->current] = length;
-hc->values[hc->current] = get_bits(gb, 8);
-hc->current++;
+hc->entries[hc->current++] = (HuffEntry){ get_bits(gb, 8), length };
 return 0;
 } else { //Node
 int r;
 length++;
-r = smacker_decode_tree(gb, hc, prefix, length);
+r = smacker_decode_tree(gb, hc, length);
 if(r)
 return r;
-return smacker_decode_tree(gb, hc, prefix | (1U << (length - 1)), 
length);
+return smacker_decode_tree(gb, hc, length);
 }
 }
 
@@ -216,22 +216,21 @@ static int smacker_decode_header_tree(SmackVContext *smk, 
GetBitContext *gb, int
i ? "high" : "low");
 continue;
 }
-err = smacker_decode_tree(gb, &h, 0, 0);
+err = smacker_decode_tree(gb, &h, 0);
 if (err < 0)
 goto error;
 skip_bits1(gb);
 if (h.current > 1) {
-err = ff_init_vlc_sparse(&vlc[i], SMKTREE_BITS, h.current,
- h.lengths, sizeof(*h.lengths), 
sizeof(*h.lengths),
- h.bits,sizeof(*h.bits),
sizeof(*h.bits),
- h.values,  sizeof(*h.values),  
sizeof(*h.values),
- INIT_VLC_LE);
+err = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
+   &h.entries[0].length, 
sizeof(*h.entries),
+   &h.entries[0].value,  
sizeof(*h.entries), 1,
+   0, INIT_VLC_OUTPUT_LE);
 if (err < 0) {
 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
 goto error;
 }
 } else
-ctx.vals[i] = h.values[0];
+ctx.vals[i] = h.entries[0].value;
 }
 
 escapes[0]  = get_bits(gb, 16);
@@ -650,21 +649,20 @@ static int smka_decode_frame(AVCodecContext *avctx, void 
*data,
 HuffContext h;
 h.current = 0;
 skip_bits1(&gb);
-if ((ret = smacker_decode_tree(&gb, &h, 0, 0)) < 0)
+if ((ret = smacker_decode_tree(&gb, &h, 0)) < 0)
 goto error;
 skip_bits1(&gb);
 if (h.current > 1) {
-ret = ff_init_vlc_sparse(&vlc[i], SMKTREE_BITS, h.current,
- h.lengths, sizeof(*h.lengths), 
sizeof(*h.lengths),
- h.bits,sizeof(*h.bits),
sizeof(*h.bits),
- h.values,  sizeof(*h.values),  
sizeof(*h.values),
- INIT_VLC_LE);
+ret = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
+   &h.entries[0].length, 
sizeof(*h.entri

[FFmpeg-devel] [PATCH 003/114] avcodec/tscc2: Combine tables for initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Using one big table for the symbols and lengths makes it
possible to remove the pointers to the individual tables.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/tscc2.c |   8 ++-
 libavcodec/tscc2data.h | 116 ++---
 2 files changed, 34 insertions(+), 90 deletions(-)

diff --git a/libavcodec/tscc2.c b/libavcodec/tscc2.c
index 0552d775c5..7a591899c1 100644
--- a/libavcodec/tscc2.c
+++ b/libavcodec/tscc2.c
@@ -60,6 +60,8 @@ static av_cold void free_vlcs(TSCC2Context *c)
 
 static av_cold int init_vlcs(TSCC2Context *c)
 {
+const uint16_t *ac_vlc_syms = tscc2_ac_vlc_syms;
+const uint8_t  *ac_vlc_lens = tscc2_ac_vlc_lens;
 int i, ret;
 
 ret = ff_init_vlc_from_lengths(&c->dc_vlc, 9, DC_VLC_COUNT,
@@ -77,11 +79,13 @@ static av_cold int init_vlcs(TSCC2Context *c)
 if (ret)
 return ret;
 ret = ff_init_vlc_from_lengths(c->ac_vlc + i, 9, tscc2_ac_vlc_sizes[i],
-   tscc2_ac_vlc_bits[i], 1,
-   tscc2_ac_vlc_syms[i], 2, 2,
+   ac_vlc_lens, 1,
+   ac_vlc_syms, 2, 2,
0, INIT_VLC_OUTPUT_LE);
 if (ret)
 return ret;
+ac_vlc_lens += tscc2_ac_vlc_sizes[i];
+ac_vlc_syms += tscc2_ac_vlc_sizes[i];
 }
 
 return 0;
diff --git a/libavcodec/tscc2data.h b/libavcodec/tscc2data.h
index 2ccfa3b811..63f5619141 100644
--- a/libavcodec/tscc2data.h
+++ b/libavcodec/tscc2data.h
@@ -95,7 +95,8 @@ static const uint8_t tscc2_nc_vlc_lens[NUM_VLC_SETS][16] = {
 { 3, 3, 6, 7, 7, 6, 6, 8, 9, 9, 7, 6, 6, 6, 3, 1 },
 };
 
-static const uint16_t ac_vlc_desc0_syms[172] = {
+static const uint16_t tscc2_ac_vlc_syms[] = {
+/* Descriptor 0 - 172 entries */
 0x0FC0, 0x0040, 0x0FF1, 0x0011, 0x0FA0, 0x0FE5, 0x0140, 0x0280, 0x0D60,
 0x0210, 0x0FC6, 0x0FD6, 0x0200, 0x0F95, 0x0101, 0x0190, 0x0FF5, 0x0FF2,
 0x0060, 0x0FE1, 0x0021, 0x0F41, 0x0270, 0x0D80, 0x0055, 0x0FB2, 0x00F1,
@@ -116,9 +117,7 @@ static const uint16_t ac_vlc_desc0_syms[172] = {
 0x01A0, 0x0036, 0x00D1, 0x0FD3, 0x0025, 0x0130, 0x1000, 0x0051, 0x0FF6,
 0x0ED0, 0x0E60, 0x0042, 0x0DF0, 0x0F20, 0x00E0, 0x0033, 0x0F71, 0x0071,
 0x00A0,
-};
-
-static const uint16_t ac_vlc_desc1_syms[169] = {
+/* Descriptor 1 - 169 entries */
 0x00D0, 0x0E50, 0x00D1, 0x0091, 0x0160, 0x0F31, 0x0EE1, 0x0250, 0x0F70,
 0x0051, 0x0F41, 0x0063, 0x0150, 0x0EF0, 0x01A0, 0x0F51, 0x0FD5, 0x0F93,
 0x0DC0, 0x0240, 0x0095, 0x0FD2, 0x00C0, 0x0FC0, 0x0040, 0x0090, 0x0016,
@@ -138,9 +137,7 @@ static const uint16_t ac_vlc_desc1_syms[169] = {
 0x0F60, 0x0014, 0x0070, 0x0F90, 0x0030, 0x0FB1, 0x0075, 0x0E90, 0x0F91,
 0x0FF4, 0x0013, 0x0FF3, 0x0EE0, 0x0FF8, 0x0065, 0x0E40, 0x01B0, 0x0F30,
 0x0071, 0x0FE5, 0x0EA0, 0x0F71, 0x0110, 0x0FF0, 0x0010,
-};
-
-static const uint16_t ac_vlc_desc2_syms[165] = {
+/* Descriptor 2 - 165 entries */
 0x0051, 0x0E61, 0x0E81, 0x0E80, 0x0FF7, 0x0E00, 0x0E30, 0x0F71, 0x0130,
 0x00F0, 0x0170, 0x0E70, 0x0F81, 0x0F40, 0x0FF3, 0x0040, 0x0013, 0x0FA0,
 0x0FC0, 0x0060, 0x0065, 0x0E40, 0x0ED0, 0x0043, 0x0086, 0x0F93, 0x0036,
@@ -160,9 +157,7 @@ static const uint16_t ac_vlc_desc2_syms[165] = {
 0x0FC5, 0x0EC0, 0x0F00, 0x0090, 0x0022, 0x0F06, 0x0F31, 0x0FA5, 0x00D1,
 0x0E60, 0x0075, 0x0F70, 0x0014, 0x0FF4, 0x0025, 0x0FB1, 0x0FE6, 0x0EE0,
 0x00C0, 0x0FF0, 0x0010,
-};
-
-static const uint16_t ac_vlc_desc3_syms[162] = {
+/* Descriptor 3 - 162 entries */
 0x0FC5, 0x0EC1, 0x0033, 0x0FE3, 0x0FD6, 0x0110, 0x00E0, 0x0071, 0x0F36,
 0x0095, 0x01A1, 0x0F71, 0x0060, 0x0FA0, 0x0FE2, 0x0F20, 0x0F21, 0x0085,
 0x0150, 0x0053, 0x0023, 0x0022, 0x0FF4, 0x0043, 0x0E70, 0x0034, 0x0017,
@@ -181,9 +176,7 @@ static const uint16_t ac_vlc_desc3_syms[162] = {
 0x00B1, 0x0163, 0x0055, 0x0024, 0x0F56, 0x0FA3, 0x0FE4, 0x0F46, 0x0FE6,
 0x0025, 0x0026, 0x0EE0, 0x0FA5, 0x01C1, 0x0F70, 0x0FD0, 0x0030, 0x1000,
 0x0045, 0x0F11, 0x0066, 0x0F85, 0x0051, 0x0014, 0x0021, 0x0FF0, 0x0010,
-};
-
-static const uint16_t ac_vlc_desc4_syms[131] = {
+/* Descriptor 4 - 131 entries */
 0x0010, 0x0FB0, 0x0050, 0x0070, 0x0FF5, 0x0FC1, 0x0090, 0x0FD1, 0x00E0,
 0x0161, 0x0085, 0x0F41, 0x0F20, 0x0FD6, 0x0F70, 0x0FD3, 0x0032, 0x0FD2,
 0x0055, 0x0191, 0x0063, 0x0091, 0x0081, 0x0F91, 0x0015, 0x0031, 0x0065,
@@ -199,9 +192,7 @@ static const uint16_t ac_vlc_desc4_syms[131] = {
 0x0F56, 0x0171, 0x0F10, 0x0FA1, 0x0EB0, 0x0056, 0x0026, 0x0035, 0x0FB5,
 0x0FB3, 0x0EF1, 0x0FF3, 0x0013, 0x0F90, 0x0FA5, 0x0FC2, 0x0F11, 0x0F81,
 0x0110, 0x0042, 0x0061, 0x0FE5, 0x0FF0,
-};
-
-static const uint16_t ac_vlc_desc5_syms[132] = {
+/* Descriptor 5 - 132 entries */
 0x0010, 0x0F81, 0x0FC5, 0x0F20, 0x0F76, 0x0061, 0x0F41, 0x00D3, 0x0FB3,
 0x0023, 0x0F31, 0x0EC0, 0x00B1, 0x0045, 0x0F50, 0x0FF5, 0x0042, 0x00C1,
 0x0EC1, 0x00D0, 0x

[FFmpeg-devel] [PATCH 004/114] avcodec/bitstream: Allow static VLC tables to be bigger than needed

2020-11-10 Thread Andreas Rheinhardt
Right now the allocated size of the VLC table of a static VLC has to
exactly match the size actually used for the VLC: If it is not enough,
abort is called; if it is more than enough, an error message is
emitted. This is no problem when one wants to initialize an individual
VLC via one of the INIT_VLC macros as one just hardcodes the needed
size. Yet it is an obstacle when one wants to initialize several VLCs
in a loop as one then needs to add an array for the sizes/offsets of
the VLC tables (unless max_depth of all arrays is one in which case
the sizes are derivable from the number of bits used).

Yet said size array is not necessary if one removes the warning for too
big buffers. The reason is that the amount of entries needed for the
table is of course generated as a byproduct of initializing the VLC.
So one can proceed as follows:

static VLC vlcs[NUM];
static VLC_TYPE vlc_table[BUF_SIZE][2];

for (int i = 0, offset = 0; i < NUM; i++) {
vlcs[i].table   = &vlc_table[offset];
vlcs[i].table_allocated = BUF_SIZE - offset;
init_vlc();
offset += vlcs[i].table_size;
}

Of course, BUF_SIZE should be equal to the number of entries actually
needed.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/bitstream.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c
index 03d39ad88c..4ffec7e17a 100644
--- a/libavcodec/bitstream.c
+++ b/libavcodec/bitstream.c
@@ -281,9 +281,6 @@ static int vlc_common_end(VLC *vlc, int nb_bits, int 
nb_codes, VLCcode *codes,
 int ret = build_table(vlc, nb_bits, nb_codes, codes, flags);
 
 if (flags & INIT_VLC_USE_NEW_STATIC) {
-if(vlc->table_size != vlc->table_allocated)
-av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, 
vlc->table_allocated);
-
 av_assert0(ret >= 0);
 *vlc_arg = *vlc;
 } else {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 010/114] avcodec/on2avc: Apply offset when initializing VLC

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/on2avc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/on2avc.c b/libavcodec/on2avc.c
index 54afe8b13a..c79d3d64fb 100644
--- a/libavcodec/on2avc.c
+++ b/libavcodec/on2avc.c
@@ -169,7 +169,7 @@ static int on2avc_decode_band_scales(On2AVCContext *c, 
GetBitContext *gb)
 scale = get_bits(gb, 7);
 first = 0;
 } else {
-scale += get_vlc2(gb, c->scale_diff.table, 9, 3) - 60;
+scale += get_vlc2(gb, c->scale_diff.table, 9, 3);
 }
 if (scale < 0 || scale > 127) {
 av_log(c->avctx, AV_LOG_ERROR, "Invalid scale value %d\n",
@@ -960,7 +960,7 @@ static av_cold int on2avc_decode_init(AVCodecContext *avctx)
 
 ret = ff_init_vlc_from_lengths(&c->scale_diff, 9, ON2AVC_SCALE_DIFFS,
ff_on2avc_scale_diff_bits, 1,
-   ff_on2avc_scale_diff_syms, 1, 1, 0, 0);
+   ff_on2avc_scale_diff_syms, 1, 1, -60, 0);
 if (ret < 0)
 goto vlc_fail;
 for (i = 1; i < 16; i++) {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 009/114] avcodec/on2avcdata: Combine tables for codebooks

2020-11-10 Thread Andreas Rheinhardt
Using one big table for the codebook symbols and lengths makes it
possible to remove the pointers to the individual tables.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/on2avc.c |   8 ++-
 libavcodec/on2avcdata.c | 132 ++--
 libavcodec/on2avcdata.h |   4 +-
 3 files changed, 40 insertions(+), 104 deletions(-)

diff --git a/libavcodec/on2avc.c b/libavcodec/on2avc.c
index 5e86c4d11a..54afe8b13a 100644
--- a/libavcodec/on2avc.c
+++ b/libavcodec/on2avc.c
@@ -906,6 +906,8 @@ static av_cold void on2avc_free_vlcs(On2AVCContext *c)
 static av_cold int on2avc_decode_init(AVCodecContext *avctx)
 {
 On2AVCContext *c = avctx->priv_data;
+const uint8_t  *lens = ff_on2avc_cb_lens;
+const uint16_t *syms = ff_on2avc_cb_syms;
 int i, ret;
 
 if (avctx->channels > 2U) {
@@ -964,10 +966,12 @@ static av_cold int on2avc_decode_init(AVCodecContext 
*avctx)
 for (i = 1; i < 16; i++) {
 int idx = i - 1;
 ret = ff_init_vlc_from_lengths(&c->cb_vlc[i], 9, 
ff_on2avc_cb_elems[idx],
-   ff_on2avc_cb_bits[idx], 1,
-   ff_on2avc_cb_syms[idx], 2, 2, 0, 0);
+   lens, 1,
+   syms, 2, 2, 0, 0);
 if (ret < 0)
 goto vlc_fail;
+lens += ff_on2avc_cb_elems[idx];
+syms += ff_on2avc_cb_elems[idx];
 }
 
 return 0;
diff --git a/libavcodec/on2avcdata.c b/libavcodec/on2avcdata.c
index 52ede3f4da..bde50615ce 100644
--- a/libavcodec/on2avcdata.c
+++ b/libavcodec/on2avcdata.c
@@ -143,19 +143,16 @@ const uint8_t 
ff_on2avc_scale_diff_bits[ON2AVC_SCALE_DIFFS] = {
 #define ON2AVC_CB_14_CODES729
 #define ON2AVC_CB_15_CODES   1089
 
-static const uint8_t on2avc_cb1_bits[ON2AVC_CB_1_2_CODES] = {
+const uint8_t ff_on2avc_cb_lens[] = {
+/* Codebook 1 - 41 entries */
 1,  6,  6,  6,  6,  6,  8,  8,  9,  9,  9,  9,  6,  6,  6,  6,  6,  6,  6,
 6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  7,  9,  9,  9,  9,  7,  9,
 9,  9,  9,
-};
-
-static const uint8_t on2avc_cb2_bits[ON2AVC_CB_1_2_CODES] = {
+/* Codebook 2 - 41 entries */
 5,  7,  7,  7,  7,  5,  5,  5,  7,  7,  7,  7,  5,  5,  5,  5,  5,  7,  7,
 7,  7,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  7,  7,  7,  7,  3,  5,
 5,  5,  5,
-};
-
-static const uint8_t on2avc_cb3_bits[ON2AVC_CB_3_4_CODES] = {
+/* Codebook 3 - 313 entries */
 1,  7,  7,  7,  9,  9,  10, 11, 12, 14, 14, 14, 15, 15, 10, 10, 7,  9,  9,
 9,  10, 11, 12, 12, 7,  10, 11, 11, 12, 12, 12, 13, 14, 15, 15, 11, 11, 9,
 9,  7,  9,  10, 11, 12, 12, 9,  9,  7,  7,  7,  7,  7,  7,  7,  7,  7,  9,
@@ -173,9 +170,7 @@ static const uint8_t on2avc_cb3_bits[ON2AVC_CB_3_4_CODES] = 
{
 9,  10, 12, 12, 12, 13, 14, 15, 16, 16, 9,  10, 10, 9,  10, 11, 12, 12, 9,
 10, 11, 12, 13, 13, 9,  9,  9,  11, 12, 13, 14, 14, 11, 11, 8,  9,  11, 12,
 13, 13, 11, 12, 13, 15, 15, 15, 15,
-};
-
-static const uint8_t on2avc_cb4_bits[ON2AVC_CB_3_4_CODES] = {
+/* Codebook 4 - 313 entries */
 7,  7,  7,  9,  9,  9,  10, 11, 12, 12, 6,  6,  6,  7,  9,  9,  9,  9,  6,
 6,  6,  7,  9,  9,  9,  9,  6,  6,  6,  7,  7,  6,  6,  9,  9,  9,  11, 12,
 12, 11, 11, 9,  9,  9,  11, 11, 11, 12, 12, 7,  9,  9,  9,  9,  7,  9,  9,
@@ -193,9 +188,7 @@ static const uint8_t on2avc_cb4_bits[ON2AVC_CB_3_4_CODES] = 
{
 11, 11, 10, 10, 8,  8,  8,  10, 10, 10, 10, 8,  10, 11, 11, 10, 10, 8,  9,
 10, 11, 13, 13, 13, 13, 9,  10, 10, 10, 11, 11, 10, 10, 6,  6,  5,  5,  5,
 6,  8,  9,  9,  8,  9,  10, 11, 11,
-};
-
-static const uint8_t on2avc_cb5_bits[ON2AVC_CB_5_6_CODES] = {
+/* Codebook 5 - 1201 entries */
 1,  7,  8,  10, 10, 10, 11, 12, 14, 14, 14, 14, 7,  10, 11, 14, 14, 14, 14,
 13, 13, 10, 12, 13, 15, 15, 15, 15, 12, 12, 10, 10, 15, 15, 15, 15, 15, 15,
 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 12, 14, 14, 14, 14, 11, 12, 12, 7,
@@ -260,9 +253,7 @@ static const uint8_t on2avc_cb5_bits[ON2AVC_CB_5_6_CODES] = 
{
 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
 15, 15, 15, 15, 15, 15, 15, 15, 14, 15, 15, 14, 14, 14, 14, 13, 14, 14, 11,
 13, 13, 13, 13,
-};
-
-static const uint8_t on2avc_cb6_bits[ON2AVC_CB_5_6_CODES] = {
+/* Codebook 6 - 1201 entries */
 8,  10, 12, 13, 13, 12, 13, 14, 16, 17, 17, 16, 16, 10, 11, 11, 8,  10, 11,
 11, 10, 10, 10, 10, 11, 12, 13, 14, 14, 11, 11, 10, 10, 10, 11, 12, 12, 8,
 10, 11, 12, 12, 10, 11, 11, 8,  10, 10, 10, 11, 13, 13, 13, 13, 10, 11, 12,
@@ -327,9 +318,7 @@ static const uint8_t on2avc_cb6_bits[ON2AVC_CB_5_6_CODES] = 
{
 9,  10, 11, 12, 12, 9,  9,  9,  10, 10, 9,  9,  12, 12, 12, 12, 11, 11, 10,
 10, 9,  10, 11, 13, 13, 13, 13, 9,  9,  9,  9,  12, 12, 12, 12, 11, 11, 10,
 10, 9,  10, 10,
-};
-
-static const uint8_t on2avc_cb7_bits[ON2AVC_CB_7_8_CODES] = {
+/* Codebook 7 - 3281 entries */
 1,  7,  11, 12, 12, 12,

[FFmpeg-devel] [PATCH 011/114] avcodec/imc: Make Huffman tables smaller

2020-11-10 Thread Andreas Rheinhardt
The IMC decoder uses Huffman tables which are created at runtime from
length tables of type uint8_t and code tables of type uint16_t together
with an implicit symbols table (i.e. symbol[i] == i). This commit
changes this: All three tables are subjected to the same permutation to
order the codes from left to right in the tree; afterwards the codes can
be omitted because they are easily computable at runtime from the
lengths, whereas the symbols need to be explicitly coded now. But said
symbols fit into an uint8_t, so one saves space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/imc.c |  7 +++--
 libavcodec/imcdata.h | 66 ++--
 2 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/libavcodec/imc.c b/libavcodec/imc.c
index 70ad5b1dbd..19a323e17d 100644
--- a/libavcodec/imc.c
+++ b/libavcodec/imc.c
@@ -239,9 +239,10 @@ static av_cold int imc_decode_init(AVCodecContext *avctx)
 for (j = 0; j < 4; j++) {
 huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
 huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - 
vlc_offsets[i * 4 + j];
-init_vlc(&huffman_vlc[i][j], IMC_VLC_BITS, imc_huffman_sizes[i],
- imc_huffman_lens[i][j], 1, 1,
- imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&huffman_vlc[i][j], IMC_VLC_BITS, 
imc_huffman_sizes[i],
+ imc_huffman_lens[i][j], 1,
+ imc_huffman_syms[i][j], 1, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 }
 }
 
diff --git a/libavcodec/imcdata.h b/libavcodec/imcdata.h
index 64e7c7185e..46037bd555 100644
--- a/libavcodec/imcdata.h
+++ b/libavcodec/imcdata.h
@@ -114,55 +114,55 @@ static const uint8_t imc_huffman_sizes[4] = {
 
 static const uint8_t imc_huffman_lens[4][4][18] = {
 {
-{ 16, 15, 13, 11,  8,  5,  3,  1,  2,  4,  6,  9, 10, 12, 14, 16,  7,  
0 },
-{ 10,  8,  7,  6,  4,  4,  3,  2,  2,  3,  4,  6,  7,  9, 11, 11,  7,  
0 },
-{ 15, 15, 14, 11,  8,  6,  4,  2,  1,  4,  5,  7,  9, 10, 12, 13,  4,  
0 },
-{ 13, 11, 10,  8,  6,  4,  2,  2,  2,  3,  5,  7,  9, 12, 15, 15, 14,  
0 },
+{ 1,  2,  5,  6, 11, 12, 15, 16, 16, 14, 13, 10,  9,  8,  7,  4,  3 },
+{ 2,  3,  3,  4,  4,  4,  6,  6,  7,  7,  7,  8,  9, 10, 11, 11,  2 },
+{ 1,  4,  4,  7,  9, 10, 12, 15, 15, 14, 13, 11,  8,  6,  5,  4,  2 },
+{ 2,  2,  4,  8,  9, 14, 15, 15, 13, 12, 11, 10,  7,  6,  5,  3,  2 },
 },
 {
-{ 14, 12, 10,  8,  7,  4,  2,  2,  2,  3,  5,  7,  9, 11, 13, 14,  7,  
0 },
-{ 14, 13, 11,  8,  6,  4,  3,  2,  2,  3,  5,  7,  9, 10, 12, 14,  3,  
0 },
-{ 13, 12, 10,  7,  5,  4,  3,  2,  2,  3,  4,  6,  8,  9, 11, 13,  4,  
0 },
-{ 13, 12, 10,  7,  5,  4,  3,  2,  2,  3,  4,  6,  8,  9, 11, 13,  4,  
0 },
+{ 2,  2,  3,  4,  7,  7, 10, 11, 12, 14, 14, 13,  9,  8,  7,  5,  2 },
+{ 2,  3, 14, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  3,  2 },
+{ 2,  3,  3,  4,  4,  5,  7,  9, 11, 12, 13, 13, 10,  8,  6,  4,  2 },
+{ 2,  3,  3,  4,  4,  5,  7,  9, 11, 12, 13, 13, 10,  8,  6,  4,  2 },
 },
 {
-{ 16, 14, 12, 10,  8,  5,  3,  1,  2,  4,  7,  9, 11, 13, 15, 17,  6, 
17 },
-{ 15, 13, 11,  8,  6,  4,  2,  2,  2,  3,  5,  7, 10, 12, 14, 16,  9, 
16 },
-{ 14, 12, 11,  9,  8,  6,  3,  1,  2,  5,  7, 10, 13, 15, 16, 17,  4, 
17 },
-{ 16, 14, 12,  9,  7,  5,  2,  2,  2,  3,  4,  6,  8, 11, 13, 15, 10, 
16 },
+{ 3,  5,  8,  9, 12, 15, 17, 17, 16, 14, 13, 11, 10,  7,  6,  4,  2,  
1 },
+{ 4,  7,  8, 10, 11, 12, 15, 16, 16, 14, 13,  9,  6,  5,  3,  2,  2,  
2 },
+{ 4,  5,  6,  7, 12, 13, 17, 17, 16, 15, 14, 11, 10,  9,  8,  3,  2,  
1 },
+{ 2,  5,  7,  8, 14, 15, 16, 16, 13, 12, 11, 10,  9,  6,  4,  3,  2,  
2 },
 },
 {
-{ 13, 11, 10,  8,  7,  5,  2,  2,  2,  4,  6,  9, 12, 14, 15, 16,  3, 
16 },
-{ 11, 11, 10,  9,  8,  7,  5,  4,  3,  3,  3,  3,  3,  3,  4,  5,  6,  
5 },
-{  9,  9,  7,  6,  5,  4,  3,  3,  2,  3,  4,  5,  4,  5,  5,  6,  8,  
6 },
-{ 13, 12, 10,  8,  5,  3,  3,  2,  2,  3,  4,  7,  9, 11, 14, 15,  6, 
15 },
+{ 2,  4,  7,  9, 10, 12, 13, 15, 16, 16, 14, 11,  8,  6,  5,  3,  2,  
2 },
+{ 3,  5,  5,  7, 10, 11, 11,  9,  8,  6,  5,  4,  4,  3,  3,  3,  3,  
3 },
+{ 5,  8,  9,  9,  7,  6,  6,  6,  5,  5,  5,  4,  4,  4,  3,  3,  3,  
2 },
+{ 2,  3,  5,  7,  8, 11, 12, 13, 15, 15, 14, 10,  9,  6,  4,  3,  3,  
2 },
 }
 };
 
-static const uint16_t imc_huffman_bits[4][4][18] = {
+static const uint8_t imc_huffman_syms[4][4][18] = {
 {
-{ 0xCC32, 0x6618, 0x1987, 0x0660, 0x00CD, 0x0018, 0x0007, 0x, 
0x0002, 0x000D, 0x0032, 0x0199, 0x0331, 0x0CC2, 0x330D, 0xCC33, 0x0067, 0x 
},
-{ 

[FFmpeg-devel] [PATCH 012/114] avcodec/imc: Avoid offsets table when creating VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/imc.c | 12 
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/libavcodec/imc.c b/libavcodec/imc.c
index 19a323e17d..a9e8a9c7e3 100644
--- a/libavcodec/imc.c
+++ b/libavcodec/imc.c
@@ -113,11 +113,6 @@ static VLC huffman_vlc[4][4];
 #define IMC_VLC_BITS 9
 #define VLC_TABLES_SIZE 9512
 
-static const int vlc_offsets[17] = {
-0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
-4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE
-};
-
 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
 
 static inline double freq2bark(double freq)
@@ -235,14 +230,15 @@ static av_cold int imc_decode_init(AVCodecContext *avctx)
 q->sqrt_tab[i] = sqrt(i);
 
 /* initialize the VLC tables */
-for (i = 0; i < 4 ; i++) {
+for (int i = 0, offset = 0; i < 4 ; i++) {
 for (j = 0; j < 4; j++) {
-huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
-huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - 
vlc_offsets[i * 4 + j];
+huffman_vlc[i][j].table   = &vlc_tables[offset];
+huffman_vlc[i][j].table_allocated = VLC_TABLES_SIZE - offset;;
 ff_init_vlc_from_lengths(&huffman_vlc[i][j], IMC_VLC_BITS, 
imc_huffman_sizes[i],
  imc_huffman_lens[i][j], 1,
  imc_huffman_syms[i][j], 1, 1,
  0, INIT_VLC_USE_NEW_STATIC);
+offset += huffman_vlc[i][j].table_size;
 }
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 014/114] avcodec/clearvideo: Avoid code duplication when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/clearvideo.c | 202 +++-
 libavcodec/clearvideodata.h | 104 ++-
 2 files changed, 90 insertions(+), 216 deletions(-)

diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index fd11d88b7a..34d24426ba 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -74,7 +74,7 @@ typedef struct CLVContext {
 inttile_size;
 inttile_shift;
 VLCdc_vlc, ac_vlc;
-LevelCodes ylev[4], ulev[3], vlev[3];
+LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V
 intluma_dc_quant, chroma_dc_quant, ac_quant;
 DECLARE_ALIGNED(16, int16_t, block)[64];
 inttop_dc[3], left_dc[4];
@@ -594,7 +594,7 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 TileInfo *tile;
 MV mv, cmv;
 
-tile = decode_tile_info(&c->gb, c->ylev, 0);
+tile = decode_tile_info(&c->gb, &c->lev[0], 0); // Y
 if (!tile)
 return AVERROR(ENOMEM);
 mv = mvi_predict(&c->mvi, i, j, tile->mv);
@@ -609,14 +609,14 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 cmv.x /= 2;
 cmv.y /= 2;
 av_freep(&tile);
-tile = decode_tile_info(&c->gb, c->ulev, 0);
+tile = decode_tile_info(&c->gb, &c->lev[4], 0); // U
 if (!tile)
 return AVERROR(ENOMEM);
 ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, 
tile, cmv);
 if (ret < 0)
 mb_ret = ret;
 av_freep(&tile);
-tile = decode_tile_info(&c->gb, c->vlev, 0);
+tile = decode_tile_info(&c->gb, &c->lev[7], 0); // V
 if (!tile)
 return AVERROR(ENOMEM);
 ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, 
tile, cmv);
@@ -649,6 +649,7 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 static av_cold int clv_decode_init(AVCodecContext *avctx)
 {
 CLVContext *const c = avctx->priv_data;
+static const uint8_t has_flags[] = { 1, 1, 1, 0, 1, 1, 0, 1, 1 };
 int ret, w, h;
 
 if (avctx->extradata_size == 110) {
@@ -704,162 +705,39 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 return ret;
 }
 
-ret = ff_init_vlc_from_lengths(&c->ylev[0].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsy_0_bits),
-   clv_flagsy_0_bits, 1,
-   clv_flagsy_0_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ylev[1].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsy_1_bits),
-   clv_flagsy_1_bits, 1,
-   clv_flagsy_1_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ylev[2].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsy_2_bits),
-   clv_flagsy_2_bits, 1,
-   clv_flagsy_2_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ulev[0].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsu_0_bits),
-   clv_flagsu_0_bits, 1,
-   clv_flagsu_0_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ulev[1].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsu_1_bits),
-   clv_flagsu_1_bits, 1,
-   clv_flagsu_1_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->vlev[0].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsv_0_bits),
-   clv_flagsv_0_bits, 1,
-   clv_flagsv_0_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->vlev[1].flags_cb, 9, 
FF_ARRAY_ELEMS(clv_flagsv_1_bits),
-   clv_flagsv_1_bits, 1,
-   clv_flagsv_1_syms, 1, 1, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ylev[0].mv_cb, 9, 
FF_ARRAY_ELEMS(clv_mvy_0_bits),
-   clv_mvy_0_bits, 1,
-   clv_mvy_0_syms, 2, 2, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ylev[1].mv_cb, 9, 
FF_ARRAY_ELEMS(clv_mvy_1_bits),
-   clv_mvy_1_bits, 1,
-   clv_mvy_1_syms, 2, 2, 0, 0);
-if (ret)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&c->ylev[2].mv_cb, 9, 
FF_ARRAY_ELEMS(clv_mvy_2_bits),
-

[FFmpeg-devel] [PATCH 015/114] avcodec/clearvideo: Avoid huge VLC length tables

2020-11-10 Thread Andreas Rheinhardt
After the motion vector and bias values tables have been reordered so
that the codes are ordered from left to right, it emerged that the
length of these entries are actually ascending for every table.
Therefore it is possible to encode them in a run-length style and create
the actual length tables during runtime. This commit implements this.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/clearvideo.c |  34 ++-
 libavcodec/clearvideodata.h | 413 +++-
 2 files changed, 62 insertions(+), 385 deletions(-)

diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index 34d24426ba..162dc13ce7 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -646,10 +646,32 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 return mb_ret < 0 ? mb_ret : buf_size;
 }
 
+static av_cold int build_vlc(VLC *vlc, const uint8_t counts[16],
+ const uint16_t **syms)
+{
+uint8_t lens[MAX_VLC_ENTRIES];
+unsigned num = 0;
+int ret;
+
+for (int i = 0; i < 16; i++) {
+unsigned count = counts[i];
+if (count == 255) /* Special case for Y_3 table */
+count = 303;
+for (count += num; num < count; num++)
+lens[num] = i + 1;
+}
+ret = ff_init_vlc_from_lengths(vlc, 9, num, lens, 1, *syms, 2, 2, 0, 0);
+if (ret < 0)
+return ret;
+*syms += num;
+return 0;
+}
+
 static av_cold int clv_decode_init(AVCodecContext *avctx)
 {
 CLVContext *const c = avctx->priv_data;
 static const uint8_t has_flags[] = { 1, 1, 1, 0, 1, 1, 0, 1, 1 };
+const uint16_t *mv_syms = clv_mv_syms, *bias_syms = clv_bias_syms;
 int ret, w, h;
 
 if (avctx->extradata_size == 110) {
@@ -705,14 +727,13 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 return ret;
 }
 
-for (int i = 0, j = 0;; i++) {
+for (int i = 0, j = 0, k = 0;; i++) {
 if (clv_mv_escape[i]) {
 c->lev[i].mv_esc = clv_mv_escape[i];
-ret = ff_init_vlc_from_lengths(&c->lev[i].mv_cb, 9, 
clv_mv_sizes[i],
-   clv_mv_bits[i], 1,
-   clv_mv_syms[i], 2, 2, 0, 0);
+ret = build_vlc(&c->lev[i].mv_cb, clv_mv_len_counts[k], &mv_syms);
 if (ret < 0)
 return ret;
+k++;
 }
 if (i == FF_ARRAY_ELEMS(has_flags))
 break;
@@ -724,9 +745,8 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 return ret;
 
 c->lev[i + 1].bias_esc = 0x100;
-ret = ff_init_vlc_from_lengths(&c->lev[i + 1].bias_cb, 9, 
clv_bias_sizes[j],
-   clv_bias_bits[j], 1,
-   clv_bias_syms[j], 2, 2, 0, 0);
+ret = build_vlc(&c->lev[i + 1].bias_cb,
+clv_bias_len_counts[j], &bias_syms);
 if (ret < 0)
 return ret;
 j++;
diff --git a/libavcodec/clearvideodata.h b/libavcodec/clearvideodata.h
index fa0721cb0c..d2c658eb5d 100644
--- a/libavcodec/clearvideodata.h
+++ b/libavcodec/clearvideodata.h
@@ -24,6 +24,7 @@
 
 #include "libavutil/common.h"
 
+#define MAX_VLC_ENTRIES 1370
 #define NUM_DC_CODES 127
 #define NUM_AC_CODES 104
 
@@ -114,26 +115,19 @@ static const uint16_t clv_mv_escape[] = {
 0x0909, 0x0A0A, 0x1010, 0x1313, 0, 0x0808, 0x0B0B, 0, 0x0808, 0x0B0B
 };
 
-static const uint8_t clv_mvy_0_bits[] = {
- 1,  4,  4,  5,  5,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,
- 7,  7,  7,  7,  7,  8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,
- 9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
-10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11,
-11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
-11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
-13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14,
-14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
-14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
-14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15,
-15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16,
-16, 16, 16, 16, 16
+static const uint8_t clv_mv_len_counts[][16] = {
+{  1,  0,  0,  2,  2,  6, 13,  6, 14, 28,  38,  38,  58,  56,  20,   8 },
+{  1,  0,  0,  2,  3,  4,  8, 10, 20, 30,  37,  63,  84,  68,  32,   0 },
+

[FFmpeg-devel] [PATCH 016/114] avcodec/clearvideo: Inline constants

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/clearvideo.c | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index 162dc13ce7..dd5720bda4 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -32,6 +32,8 @@
 #include "mathops.h"
 #include "clearvideodata.h"
 
+#define CLV_VLC_BITS 9
+
 typedef struct LevelCodes {
 uint16_tmv_esc;
 uint16_tbias_esc;
@@ -87,14 +89,14 @@ static inline int decode_block(CLVContext *ctx, int16_t 
*blk, int has_ac,
 int idx = 1, last = 0, val, skip;
 
 memset(blk, 0, sizeof(*blk) * 64);
-blk[0] = get_vlc2(gb, ctx->dc_vlc.table, 9, 3);
+blk[0] = get_vlc2(gb, ctx->dc_vlc.table, CLV_VLC_BITS, 3);
 blk[0] -= 63;
 
 if (!has_ac)
 return 0;
 
 while (idx < 64 && !last) {
-val = get_vlc2(gb, ctx->ac_vlc.table, 9, 2);
+val = get_vlc2(gb, ctx->ac_vlc.table, CLV_VLC_BITS, 2);
 if (val < 0)
 return AVERROR_INVALIDDATA;
 if (val != 0x1BFF) {
@@ -365,11 +367,11 @@ static TileInfo* decode_tile_info(GetBitContext *gb, 
LevelCodes *lc, int level)
 MV mv = { 0 };
 
 if (lc[level].flags_cb.table) {
-flags = get_vlc2(gb, lc[level].flags_cb.table, 
lc[level].flags_cb.bits, 2);
+flags = get_vlc2(gb, lc[level].flags_cb.table, CLV_VLC_BITS, 2);
 }
 
 if (lc[level].mv_cb.table) {
-uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, 
lc[level].mv_cb.bits, 3);
+uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, CLV_VLC_BITS, 
3);
 
 if (mv_code != lc[level].mv_esc) {
 mv.x = (int8_t)(mv_code & 0xff);
@@ -381,7 +383,7 @@ static TileInfo* decode_tile_info(GetBitContext *gb, 
LevelCodes *lc, int level)
 }
 
 if (lc[level].bias_cb.table) {
-uint16_t bias_val = get_vlc2(gb, lc[level].bias_cb.table, 
lc[level].bias_cb.bits, 2);
+uint16_t bias_val = get_vlc2(gb, lc[level].bias_cb.table, 
CLV_VLC_BITS, 2);
 
 if (bias_val != lc[level].bias_esc) {
 bias = (int16_t)(bias_val);
@@ -660,7 +662,8 @@ static av_cold int build_vlc(VLC *vlc, const uint8_t 
counts[16],
 for (count += num; num < count; num++)
 lens[num] = i + 1;
 }
-ret = ff_init_vlc_from_lengths(vlc, 9, num, lens, 1, *syms, 2, 2, 0, 0);
+ret = ff_init_vlc_from_lengths(vlc, CLV_VLC_BITS, num, lens, 1,
+   *syms, 2, 2, 0, 0);
 if (ret < 0)
 return ret;
 *syms += num;
@@ -712,14 +715,14 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 return AVERROR(ENOMEM);
 
 ff_idctdsp_init(&c->idsp, avctx);
-ret = init_vlc(&c->dc_vlc, 9, NUM_DC_CODES,
+ret = init_vlc(&c->dc_vlc, CLV_VLC_BITS, NUM_DC_CODES,
clv_dc_bits,  1, 1,
clv_dc_codes, 1, 1, 0);
 if (ret) {
 av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
 return ret;
 }
-ret = ff_init_vlc_from_lengths(&c->ac_vlc, 9, NUM_AC_CODES,
+ret = ff_init_vlc_from_lengths(&c->ac_vlc, CLV_VLC_BITS, NUM_AC_CODES,
clv_ac_bits, 1,
clv_ac_syms, 2, 2, 0, 0);
 if (ret) {
@@ -738,7 +741,7 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 if (i == FF_ARRAY_ELEMS(has_flags))
 break;
 if (has_flags[i]) {
-ret = ff_init_vlc_from_lengths(&c->lev[i].flags_cb, 9, 16,
+ret = ff_init_vlc_from_lengths(&c->lev[i].flags_cb, CLV_VLC_BITS, 
16,
clv_flags_bits[j], 1,
clv_flags_syms[j], 1, 1, 0, 0);
 if (ret < 0)
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 017/114] avcodec/clearvideo: Use minimal max_depth in get_vlc2()

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/clearvideo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index dd5720bda4..1d88a9a776 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -371,7 +371,7 @@ static TileInfo* decode_tile_info(GetBitContext *gb, 
LevelCodes *lc, int level)
 }
 
 if (lc[level].mv_cb.table) {
-uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, CLV_VLC_BITS, 
3);
+uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, CLV_VLC_BITS, 
2);
 
 if (mv_code != lc[level].mv_esc) {
 mv.x = (int8_t)(mv_code & 0xff);
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 018/114] avcodec/clearvideo: Apply VLC offset during init

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/clearvideo.c |  7 +++--
 libavcodec/clearvideodata.h | 54 ++---
 2 files changed, 23 insertions(+), 38 deletions(-)

diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index 1d88a9a776..9b4f03d15e 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -90,7 +90,6 @@ static inline int decode_block(CLVContext *ctx, int16_t *blk, 
int has_ac,
 
 memset(blk, 0, sizeof(*blk) * 64);
 blk[0] = get_vlc2(gb, ctx->dc_vlc.table, CLV_VLC_BITS, 3);
-blk[0] -= 63;
 
 if (!has_ac)
 return 0;
@@ -715,9 +714,9 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 return AVERROR(ENOMEM);
 
 ff_idctdsp_init(&c->idsp, avctx);
-ret = init_vlc(&c->dc_vlc, CLV_VLC_BITS, NUM_DC_CODES,
-   clv_dc_bits,  1, 1,
-   clv_dc_codes, 1, 1, 0);
+ret = ff_init_vlc_from_lengths(&c->dc_vlc, CLV_VLC_BITS, NUM_DC_CODES,
+   clv_dc_lens, 1,
+   clv_dc_syms, 1, 1, -63, 0);
 if (ret) {
 av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
 return ret;
diff --git a/libavcodec/clearvideodata.h b/libavcodec/clearvideodata.h
index d2c658eb5d..7fd3793dee 100644
--- a/libavcodec/clearvideodata.h
+++ b/libavcodec/clearvideodata.h
@@ -28,42 +28,28 @@
 #define NUM_DC_CODES 127
 #define NUM_AC_CODES 104
 
-static const uint8_t clv_dc_codes[NUM_DC_CODES] = {
-0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
-0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x07, 0x0B,
-0x0C, 0x08, 0x08, 0x09, 0x04, 0x06, 0x07, 0x05,
-0x04, 0x05, 0x04, 0x06, 0x05, 0x06, 0x07, 0x05,
-0x06, 0x07, 0x06, 0x07, 0x08, 0x06, 0x07, 0x08,
-0x09, 0x0A, 0x0B, 0x07, 0x08, 0x09, 0x07, 0x08,
-0x06, 0x07, 0x08, 0x06, 0x04, 0x05, 0x02, 0x01,
-0x03, 0x06, 0x07, 0x07, 0x09, 0x0A, 0x0B, 0x09,
-0x0A, 0x0B, 0x0A, 0x0B, 0x0C, 0x0D, 0x0C, 0x09,
-0x0D, 0x0A, 0x0B, 0x08, 0x09, 0x0A, 0x0B, 0x07,
-0x08, 0x09, 0x0A, 0x0B, 0x06, 0x07, 0x06, 0x08,
-0x07, 0x09, 0x0A, 0x0B, 0x09, 0x0A, 0x0B, 0x0C,
-0x14, 0x0D, 0x0D, 0x0E, 0x0F, 0x15, 0x15, 0x16,
-0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
-0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
+static const uint8_t clv_dc_lens[NUM_DC_CODES] = {
+22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
+22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
+21, 21, 21, 20, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18,
+17, 17, 17, 17, 16, 16, 16, 15, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14,
+13, 13, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 10, 10, 10,
+10, 10, 10, 10, 10,  9,  9,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  7,  7,
+ 7,  7,  7,  7,  6,  6,  5,  5,  5,  5,  3,  3,  1,
 };
 
-static const uint8_t clv_dc_bits[NUM_DC_CODES] = {
-22, 22, 22, 22, 22, 22, 22, 22,
-22, 22, 22, 22, 22, 22, 22, 22,
-22, 22, 22, 21, 22, 22, 19, 20,
-20, 19, 18, 18, 15, 17, 17, 16,
-14, 15, 12, 13, 14, 14, 14, 12,
-12, 12, 11, 11, 11, 10, 10, 10,
-10, 10, 10,  9,  9,  9,  8,  8,
- 7,  7,  7,  6,  5,  5,  3,  1,
- 3,  5,  5,  6,  7,  7,  7,  8,
- 8,  8,  9,  9,  9,  9, 10, 11,
-10, 11, 11, 12, 12, 12, 12, 13,
-14, 14, 14, 14, 15, 15, 16, 17,
-16, 17, 18, 18, 19, 19, 19, 19,
-21, 19, 20, 19, 19, 21, 22, 22,
-22, 22, 22, 22, 22, 22, 22, 22,
-22, 22, 22, 22, 22, 22, 22,
+static const uint8_t clv_dc_syms[NUM_DC_CODES] = {
+0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
+0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x14, 0x15, 0x6E, 0x6F, 0x70,
+0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C,
+0x7D, 0x7E, 0x13, 0x68, 0x6D, 0x17, 0x18, 0x6A, 0x16, 0x19, 0x64, 0x65,
+0x66, 0x67, 0x69, 0x6B, 0x6C, 0x1A, 0x1B, 0x62, 0x63, 0x1D, 0x1E, 0x5F,
+0x61, 0x1F, 0x5E, 0x60, 0x1C, 0x21, 0x5C, 0x5D, 0x20, 0x24, 0x25, 0x26,
+0x58, 0x59, 0x5A, 0x5B, 0x23, 0x57, 0x22, 0x27, 0x28, 0x29, 0x53, 0x54,
+0x55, 0x56, 0x2A, 0x2B, 0x2C, 0x4F, 0x51, 0x52, 0x2D, 0x2E, 0x2F, 0x30,
+0x31, 0x32, 0x4E, 0x50, 0x33, 0x34, 0x35, 0x4A, 0x4B, 0x4C, 0x4D, 0x36,
+0x37, 0x47, 0x48, 0x49, 0x38, 0x39, 0x3A, 0x44, 0x45, 0x46, 0x3B, 0x43,
+0x3C, 0x3D, 0x41, 0x42, 0x3E, 0x40, 0x3F,
 };
 
 static const uint16_t clv_ac_syms[NUM_AC_CODES] = {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 020/114] avcodec/wnv1: Make array for initializing VLC smaller

2020-11-10 Thread Andreas Rheinhardt
This is possible by switching to ff_init_vlc_from_lengths() which allows
to replace the table for the codes (which need an uint16_t) by a table
of symbols which fit into an uint8_t. Also switch to an ordinary
INIT_VLC macro while just at it.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/wnv1.c | 21 -
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/libavcodec/wnv1.c b/libavcodec/wnv1.c
index b5cd0f0f0c..f8a9b94746 100644
--- a/libavcodec/wnv1.c
+++ b/libavcodec/wnv1.c
@@ -29,11 +29,10 @@
 #include "get_bits.h"
 #include "internal.h"
 
-
-static const uint16_t code_tab[16][2] = {
-{ 0x17F, 9 }, { 0xBF, 8 }, { 0x5F, 7 }, { 0x2F, 6 }, { 0x17, 5 }, { 0x0B, 
4 }, { 0x005, 3 },
-{ 0x000, 1 },
-{ 0x01, 3 }, { 0x03, 4 }, { 0x07, 5 }, { 0x0F, 6 }, { 0x1F, 7 }, { 0x3F, 8 
}, { 0x07F, 9 }, { 0xFF, 8 }
+static const uint8_t code_tab[16][2] = {
+{  7, 1 }, {  8, 3 }, {  6, 3 }, { 9, 4 }, {  5, 4 }, { 10, 5 }, {  4, 5 },
+{ 11, 6 }, {  3, 6 }, { 12, 7 }, { 2, 7 }, { 13, 8 }, {  1, 8 }, { 14, 9 },
+{  0, 9 }, { 15, 8 }
 };
 
 #define CODE_VLC_BITS 9
@@ -115,16 +114,12 @@ static int decode_frame(AVCodecContext *avctx,
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
-static VLC_TYPE code_table[1 << CODE_VLC_BITS][2];
-
 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
 
-code_vlc.table   = code_table;
-code_vlc.table_allocated = 1 << CODE_VLC_BITS;
-init_vlc(&code_vlc, CODE_VLC_BITS, 16,
- &code_tab[0][1], 4, 2,
- &code_tab[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
+INIT_VLC_STATIC_FROM_LENGTHS(&code_vlc, CODE_VLC_BITS, 16,
+ &code_tab[0][1], 2,
+ &code_tab[0][0], 2, 1,
+ 0, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
 return 0;
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 021/114] avcodec/wnv1: Apply offset during init, not later every time

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/wnv1.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavcodec/wnv1.c b/libavcodec/wnv1.c
index f8a9b94746..7dd0e72596 100644
--- a/libavcodec/wnv1.c
+++ b/libavcodec/wnv1.c
@@ -43,10 +43,10 @@ static inline int wnv1_get_code(GetBitContext *gb, int 
shift, int base_value)
 {
 int v = get_vlc2(gb, code_vlc.table, CODE_VLC_BITS, 1);
 
-if (v == 15)
+if (v == 8)
 return get_bits(gb, 8 - shift) << shift;
 else
-return base_value + ((v - 7U) << shift);
+return base_value + v * (1 << shift);
 }
 
 static int decode_frame(AVCodecContext *avctx,
@@ -119,7 +119,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
 INIT_VLC_STATIC_FROM_LENGTHS(&code_vlc, CODE_VLC_BITS, 16,
  &code_tab[0][1], 2,
  &code_tab[0][0], 2, 1,
- 0, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
+ -7, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
 return 0;
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 019/114] avcodec/clearvideo: Make VLC tables static

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/clearvideo.c | 94 ++---
 1 file changed, 40 insertions(+), 54 deletions(-)

diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index 9b4f03d15e..33b94bb086 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -75,13 +75,15 @@ typedef struct CLVContext {
 MVInfo mvi;
 inttile_size;
 inttile_shift;
-VLCdc_vlc, ac_vlc;
-LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V
 intluma_dc_quant, chroma_dc_quant, ac_quant;
 DECLARE_ALIGNED(16, int16_t, block)[64];
 inttop_dc[3], left_dc[4];
 } CLVContext;
 
+static VLCdc_vlc, ac_vlc;
+static LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V
+static VLC_TYPE   vlc_buf[16716][2];
+
 static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
int ac_quant)
 {
@@ -89,13 +91,13 @@ static inline int decode_block(CLVContext *ctx, int16_t 
*blk, int has_ac,
 int idx = 1, last = 0, val, skip;
 
 memset(blk, 0, sizeof(*blk) * 64);
-blk[0] = get_vlc2(gb, ctx->dc_vlc.table, CLV_VLC_BITS, 3);
+blk[0] = get_vlc2(gb, dc_vlc.table, CLV_VLC_BITS, 3);
 
 if (!has_ac)
 return 0;
 
 while (idx < 64 && !last) {
-val = get_vlc2(gb, ctx->ac_vlc.table, CLV_VLC_BITS, 2);
+val = get_vlc2(gb, ac_vlc.table, CLV_VLC_BITS, 2);
 if (val < 0)
 return AVERROR_INVALIDDATA;
 if (val != 0x1BFF) {
@@ -358,7 +360,7 @@ static void mvi_update_row(MVInfo *mvi)
 }
 }
 
-static TileInfo* decode_tile_info(GetBitContext *gb, LevelCodes *lc, int level)
+static TileInfo *decode_tile_info(GetBitContext *gb, const LevelCodes *lc, int 
level)
 {
 TileInfo *ti;
 int i, flags = 0;
@@ -595,7 +597,7 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 TileInfo *tile;
 MV mv, cmv;
 
-tile = decode_tile_info(&c->gb, &c->lev[0], 0); // Y
+tile = decode_tile_info(&c->gb, &lev[0], 0); // Y
 if (!tile)
 return AVERROR(ENOMEM);
 mv = mvi_predict(&c->mvi, i, j, tile->mv);
@@ -610,14 +612,14 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 cmv.x /= 2;
 cmv.y /= 2;
 av_freep(&tile);
-tile = decode_tile_info(&c->gb, &c->lev[4], 0); // U
+tile = decode_tile_info(&c->gb, &lev[4], 0); // U
 if (!tile)
 return AVERROR(ENOMEM);
 ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, 
tile, cmv);
 if (ret < 0)
 mb_ret = ret;
 av_freep(&tile);
-tile = decode_tile_info(&c->gb, &c->lev[7], 0); // V
+tile = decode_tile_info(&c->gb, &lev[7], 0); // V
 if (!tile)
 return AVERROR(ENOMEM);
 ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, 
tile, cmv);
@@ -647,12 +649,11 @@ static int clv_decode_frame(AVCodecContext *avctx, void 
*data,
 return mb_ret < 0 ? mb_ret : buf_size;
 }
 
-static av_cold int build_vlc(VLC *vlc, const uint8_t counts[16],
- const uint16_t **syms)
+static av_cold void build_vlc(VLC *vlc, const uint8_t counts[16],
+  const uint16_t **syms, unsigned *offset)
 {
 uint8_t lens[MAX_VLC_ENTRIES];
 unsigned num = 0;
-int ret;
 
 for (int i = 0; i < 16; i++) {
 unsigned count = counts[i];
@@ -661,12 +662,12 @@ static av_cold int build_vlc(VLC *vlc, const uint8_t 
counts[16],
 for (count += num; num < count; num++)
 lens[num] = i + 1;
 }
-ret = ff_init_vlc_from_lengths(vlc, CLV_VLC_BITS, num, lens, 1,
-   *syms, 2, 2, 0, 0);
-if (ret < 0)
-return ret;
+vlc->table   = &vlc_buf[*offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *offset;
+ff_init_vlc_from_lengths(vlc, CLV_VLC_BITS, num, lens, 1,
+ *syms, 2, 2, 0, INIT_VLC_USE_NEW_STATIC);
 *syms += num;
-return 0;
+*offset += vlc->table_size;
 }
 
 static av_cold int clv_decode_init(AVCodecContext *avctx)
@@ -714,46 +715,39 @@ static av_cold int clv_decode_init(AVCodecContext *avctx)
 return AVERROR(ENOMEM);
 
 ff_idctdsp_init(&c->idsp, avctx);
-ret = ff_init_vlc_from_lengths(&c->dc_vlc, CLV_VLC_BITS, NUM_DC_CODES,
-   clv_dc_lens, 1,
-   clv_dc_syms, 1, 1, -63, 0);
-if (ret) {
-av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
-return ret;
-}
-ret = ff_init_vlc_from_len

[FFmpeg-devel] [PATCH 023/114] avcodec/cook: Apply offset when initializing VLC table

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/cook.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/cook.c b/libavcodec/cook.c
index 7b24868f57..9f44df5332 100644
--- a/libavcodec/cook.c
+++ b/libavcodec/cook.c
@@ -200,7 +200,7 @@ static av_cold int init_cook_vlc_tables(COOKContext *q)
 result |= ff_init_vlc_from_lengths(&q->envelope_quant_index[i], 9, 24,
envelope_quant_index_huffbits[i], 1,
envelope_quant_index_huffsyms[i], 
1, 1,
-   0, 0);
+   -12, 0);
 }
 av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
 for (i = 0; i < 7; i++) {
@@ -385,7 +385,7 @@ static int decode_envelope(COOKContext *q, COOKSubpacket *p,
 
 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table,
  q->envelope_quant_index[vlc_index - 1].bits, 2);
-quant_index_table[i] = quant_index_table[i - 1] + j - 12; // 
differential encoding
+quant_index_table[i] = quant_index_table[i - 1] + j; // differential 
encoding
 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
 av_log(q->avctx, AV_LOG_ERROR,
"Invalid quantizer %d at position %d, outside [-63, 63] 
range\n",
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 022/114] avcodec/cook: Make tables to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
Up until now, the Cook decoder used tables for the lengths of codes and
tables of the codes itself to initialize VLCs; the tables for the codes
were of type uint16_t because the codes were so long. It did not use
explicit symbol tables. This commit instead reorders the tables so that
the code tables are sorted from left to right in the tree. Then the
codes can be easily derived from the lengths and therefore be omitted.
This comes at the price of explicitly coding the symbols, but this is
nevertheless a net win because most of the symbols tables can be coded
on one byte. Furthermore, Cook actually does not use a contiguous range
of symbols for its main VLC tables and the old code compensated for that
by adding holes (codes of length zero) to the tables (that are skipped by
ff_init_vlc_sparse()). This is no longer necessary with the new
approach. All in all, this saves about 1.7KB.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/cook.c |  24 +-
 libavcodec/cookdata.h | 643 ++
 2 files changed, 290 insertions(+), 377 deletions(-)

diff --git a/libavcodec/cook.c b/libavcodec/cook.c
index 9582495442..7b24868f57 100644
--- a/libavcodec/cook.c
+++ b/libavcodec/cook.c
@@ -197,23 +197,27 @@ static av_cold int init_cook_vlc_tables(COOKContext *q)
 
 result = 0;
 for (i = 0; i < 13; i++) {
-result |= init_vlc(&q->envelope_quant_index[i], 9, 24,
-   envelope_quant_index_huffbits[i], 1, 1,
-   envelope_quant_index_huffcodes[i], 2, 2, 0);
+result |= ff_init_vlc_from_lengths(&q->envelope_quant_index[i], 9, 24,
+   envelope_quant_index_huffbits[i], 1,
+   envelope_quant_index_huffsyms[i], 
1, 1,
+   0, 0);
 }
 av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
 for (i = 0; i < 7; i++) {
-result |= init_vlc(&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
-   cvh_huffbits[i], 1, 1,
-   cvh_huffcodes[i], 2, 2, 0);
+int sym_size = 1 + (i == 3);
+result |= ff_init_vlc_from_lengths(&q->sqvh[i], vhvlcsize_tab[i], 
vhsize_tab[i],
+   cvh_huffbits[i], 1,
+   cvh_huffsyms[i], sym_size, sym_size,
+   0, 0);
 }
 
 for (i = 0; i < q->num_subpackets; i++) {
 if (q->subpacket[i].joint_stereo == 1) {
-result |= init_vlc(&q->subpacket[i].channel_coupling, 6,
-   (1 << q->subpacket[i].js_vlc_bits) - 1,
-   ccpl_huffbits[q->subpacket[i].js_vlc_bits - 2], 
1, 1,
-   ccpl_huffcodes[q->subpacket[i].js_vlc_bits - 
2], 2, 2, 0);
+result |= 
ff_init_vlc_from_lengths(&q->subpacket[i].channel_coupling, 6,
+   (1 << 
q->subpacket[i].js_vlc_bits) - 1,
+   
ccpl_huffbits[q->subpacket[i].js_vlc_bits - 2], 1,
+   
ccpl_huffsyms[q->subpacket[i].js_vlc_bits - 2], 1, 1,
+   0, 0);
 av_log(q->avctx, AV_LOG_DEBUG, "subpacket %i Joint-stereo VLC 
used.\n", i);
 }
 }
diff --git a/libavcodec/cookdata.h b/libavcodec/cookdata.h
index efb8a53942..05d1eef23b 100644
--- a/libavcodec/cookdata.h
+++ b/libavcodec/cookdata.h
@@ -71,7 +71,7 @@ static const int vpr_tab[7] = {
 /* VLC data */
 
 static const int vhsize_tab[7] = {
-191, 97, 48, 607, 246, 230, 32,
+181, 94, 48, 520, 209, 192, 32,
 };
 
 static const int vhvlcsize_tab[7] = {
@@ -79,352 +79,269 @@ static const int vhvlcsize_tab[7] = {
 };
 
 static const uint8_t envelope_quant_index_huffbits[13][24] = {
-{  4,  6,  5,  5,  4, 4, 4, 4, 4, 4, 3, 3, 3, 4, 5, 7,  8,  9, 11, 11, 12, 
12, 12, 12 },
-{ 10,  8,  6,  5,  5, 4, 3, 3, 3, 3, 3, 3, 4, 5, 7, 9, 11, 12, 13, 15, 15, 
15, 16, 16 },
-{ 12, 10,  8,  6,  5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 4, 4,  5,  5,  7,  9, 11, 
13, 14, 14 },
-{ 13, 10,  9,  9,  7, 7, 5, 5, 4, 3, 3, 3, 3, 3, 4, 4,  4,  5,  7,  9, 11, 
13, 13, 13 },
-{ 12, 13, 10,  8,  6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 3, 4,  5,  5,  6,  7,  9, 
11, 14, 14 },
-{ 12, 11,  9,  8,  8, 7, 5, 4, 4, 3, 3, 3, 3, 3, 4, 4,  5,  5,  7,  8, 10, 
13, 14, 14 },
-{ 15, 16, 15, 12, 10, 8, 6, 5, 4, 3, 3, 3, 2, 3, 4, 5,  5,  7,  9, 11, 13, 
16, 16, 16 },
-{ 14, 14, 11, 10,  9, 7, 7, 5, 5, 4, 3, 3, 2, 3, 3, 4,  5,  7,  9,  9, 12, 
14, 15, 15 },
-{  9,  9,  9,  8,  7, 6, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5,  6,  7,  8, 10, 11, 
12, 13, 13 },
-{ 14, 12, 10,  8,  6, 6, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5,  6,  8,  8,  9, 11, 
14, 14, 14 },
-{ 13, 10,  9,  8,  6, 6, 5, 4, 4, 4, 3, 3, 2, 3, 4, 5,  6,  8,  9,  9, 11, 
12, 14, 14 },
-{ 16, 13, 12, 11,

[FFmpeg-devel] [PATCH 025/114] avcodec/cook: Inline constants

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/cook.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libavcodec/cook.c b/libavcodec/cook.c
index 75422d18aa..0fa42d681d 100644
--- a/libavcodec/cook.c
+++ b/libavcodec/cook.c
@@ -65,6 +65,9 @@
 #define SUBBAND_SIZE20
 #define MAX_SUBPACKETS   5
 
+#define QUANT_VLC_BITS9
+#define COUPLING_VLC_BITS 6
+
 typedef struct cook_gains {
 int *now;
 int *previous;
@@ -210,7 +213,7 @@ static av_cold int init_cook_vlc_tables(COOKContext *q)
 
 result = 0;
 for (i = 0; i < 13; i++) {
-result |= build_vlc(&q->envelope_quant_index[i], 9,
+result |= build_vlc(&q->envelope_quant_index[i], QUANT_VLC_BITS,
 envelope_quant_index_huffcounts[i],
 envelope_quant_index_huffsyms[i], 1, -12);
 }
@@ -224,7 +227,7 @@ static av_cold int init_cook_vlc_tables(COOKContext *q)
 
 for (i = 0; i < q->num_subpackets; i++) {
 if (q->subpacket[i].joint_stereo == 1) {
-result |= build_vlc(&q->subpacket[i].channel_coupling, 6,
+result |= build_vlc(&q->subpacket[i].channel_coupling, 
COUPLING_VLC_BITS,
 ccpl_huffcounts[q->subpacket[i].js_vlc_bits - 
2],
 ccpl_huffsyms[q->subpacket[i].js_vlc_bits - 
2], 1,
 0);
@@ -394,7 +397,7 @@ static int decode_envelope(COOKContext *q, COOKSubpacket *p,
 vlc_index = 13; // the VLC tables >13 are identical to No. 13
 
 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table,
- q->envelope_quant_index[vlc_index - 1].bits, 2);
+ QUANT_VLC_BITS, 2);
 quant_index_table[i] = quant_index_table[i - 1] + j; // differential 
encoding
 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
 av_log(q->avctx, AV_LOG_ERROR,
@@ -773,7 +776,7 @@ static int decouple_info(COOKContext *q, COOKSubpacket *p, 
int *decouple_tab)
 for (i = 0; i < length; i++)
 decouple_tab[start + i] = get_vlc2(&q->gb,
p->channel_coupling.table,
-   p->channel_coupling.bits, 3);
+   COUPLING_VLC_BITS, 3);
 else
 for (i = 0; i < length; i++) {
 int v = get_bits(&q->gb, p->js_vlc_bits);
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 026/114] avcodec/rv10: Reduce the size of the tables used to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
This can be achieved by switching to ff_init_vlc_from_lengths() which
allows to replace two uint16_t tables for codes with uint8_t tables for
the symbols by permuting the tables so that the codes are ordered from
left to right in the tree in which case they can be easily computed from
the lengths at runtime.

And after doing so, it became apparent that the tables for the symbols
are actually the same for luma and chroma, so that one can even omit one
of them.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv10.c | 199 ++
 1 file changed, 59 insertions(+), 140 deletions(-)

diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index e594160fea..03c732e92d 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -53,144 +53,63 @@ typedef struct RVDecContext {
 int orig_width, orig_height;
 } RVDecContext;
 
-static const uint16_t rv_lum_code[256] = {
-0x3e7f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06,
-0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e,
-0x0f0f, 0x0f10, 0x0f11, 0x0f12, 0x0f13, 0x0f14, 0x0f15, 0x0f16,
-0x0f17, 0x0f18, 0x0f19, 0x0f1a, 0x0f1b, 0x0f1c, 0x0f1d, 0x0f1e,
-0x0f1f, 0x0f20, 0x0f21, 0x0f22, 0x0f23, 0x0f24, 0x0f25, 0x0f26,
-0x0f27, 0x0f28, 0x0f29, 0x0f2a, 0x0f2b, 0x0f2c, 0x0f2d, 0x0f2e,
-0x0f2f, 0x0f30, 0x0f31, 0x0f32, 0x0f33, 0x0f34, 0x0f35, 0x0f36,
-0x0f37, 0x0f38, 0x0f39, 0x0f3a, 0x0f3b, 0x0f3c, 0x0f3d, 0x0f3e,
-0x0f3f, 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386,
-0x0387, 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e,
-0x038f, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396,
-0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e,
-0x039f, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6,
-0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
-0x00cf, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056,
-0x0057, 0x0020, 0x0021, 0x0022, 0x0023, 0x000c, 0x000d, 0x0004,
-0x, 0x0005, 0x000e, 0x000f, 0x0024, 0x0025, 0x0026, 0x0027,
-0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
-0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
-0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
-0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7,
-0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
-0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7,
-0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
-0x0f40, 0x0f41, 0x0f42, 0x0f43, 0x0f44, 0x0f45, 0x0f46, 0x0f47,
-0x0f48, 0x0f49, 0x0f4a, 0x0f4b, 0x0f4c, 0x0f4d, 0x0f4e, 0x0f4f,
-0x0f50, 0x0f51, 0x0f52, 0x0f53, 0x0f54, 0x0f55, 0x0f56, 0x0f57,
-0x0f58, 0x0f59, 0x0f5a, 0x0f5b, 0x0f5c, 0x0f5d, 0x0f5e, 0x0f5f,
-0x0f60, 0x0f61, 0x0f62, 0x0f63, 0x0f64, 0x0f65, 0x0f66, 0x0f67,
-0x0f68, 0x0f69, 0x0f6a, 0x0f6b, 0x0f6c, 0x0f6d, 0x0f6e, 0x0f6f,
-0x0f70, 0x0f71, 0x0f72, 0x0f73, 0x0f74, 0x0f75, 0x0f76, 0x0f77,
-0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f,
+/* The elements with negative length in the bits table correspond to
+ * open ends in the respective Huffman tree. */
+static const uint8_t rv_sym[] = {
+128, 127, 129, 125, 126, 130, 131, 121, 122, 123, 124, 132, 133, 134, 135,
+113, 114, 115, 116, 117, 118, 119, 120, 136, 137, 138, 139, 140, 141, 142,
+143,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
+111, 112, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
+157, 158, 159,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,
+ 77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,
+ 92,  93,  94,  95,  96, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
+170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
+185, 186, 187, 188, 189, 190, 191,   1,   2,   3,   4,   5,   6,   7,   8,
+  9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,
+ 24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
+ 39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,
+ 54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64, 192, 193, 194, 195,
+196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
+211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
+226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
+241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
+  0,   0,   0,   0,   0,   0,   0,   0,
 };
 
-static const uint8_t rv_lum_bits[256] = {
-14, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12,
-12, 12, 12, 12, 12, 12, 12, 12,
-12, 12,

[FFmpeg-devel] [PATCH 024/114] avcodec/cook: Avoid big length tables for VLC initialization

2020-11-10 Thread Andreas Rheinhardt
Permuting the tables used to initialize the Cook VLCs so that the code
tables are ordered from left to right in the tree revealed that the
length of the codes are ascending from left to right. Therefore one can
run-length encode them to avoid the big length tables; this saves a bit
more than 1KB.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/cook.c |  36 +---
 libavcodec/cookdata.h | 187 +++---
 2 files changed, 52 insertions(+), 171 deletions(-)

diff --git a/libavcodec/cook.c b/libavcodec/cook.c
index 9f44df5332..75422d18aa 100644
--- a/libavcodec/cook.c
+++ b/libavcodec/cook.c
@@ -190,6 +190,19 @@ static av_cold void init_gain_table(COOKContext *q)
(1.0 / (double) q->gain_size_factor));
 }
 
+static av_cold int build_vlc(VLC *vlc, int nb_bits, const uint8_t counts[16],
+ const void *syms, int symbol_size, int offset)
+{
+uint8_t lens[MAX_COOK_VLC_ENTRIES];
+unsigned num = 0;
+
+for (int i = 0; i < 16; i++)
+for (unsigned count = num + counts[i]; num < count; num++)
+lens[num] = i + 1;
+
+return ff_init_vlc_from_lengths(vlc, nb_bits, num, lens, 1,
+syms, symbol_size, symbol_size, offset, 0);
+}
 
 static av_cold int init_cook_vlc_tables(COOKContext *q)
 {
@@ -197,27 +210,24 @@ static av_cold int init_cook_vlc_tables(COOKContext *q)
 
 result = 0;
 for (i = 0; i < 13; i++) {
-result |= ff_init_vlc_from_lengths(&q->envelope_quant_index[i], 9, 24,
-   envelope_quant_index_huffbits[i], 1,
-   envelope_quant_index_huffsyms[i], 
1, 1,
-   -12, 0);
+result |= build_vlc(&q->envelope_quant_index[i], 9,
+envelope_quant_index_huffcounts[i],
+envelope_quant_index_huffsyms[i], 1, -12);
 }
 av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
 for (i = 0; i < 7; i++) {
 int sym_size = 1 + (i == 3);
-result |= ff_init_vlc_from_lengths(&q->sqvh[i], vhvlcsize_tab[i], 
vhsize_tab[i],
-   cvh_huffbits[i], 1,
-   cvh_huffsyms[i], sym_size, sym_size,
-   0, 0);
+result |= build_vlc(&q->sqvh[i], vhvlcsize_tab[i],
+cvh_huffcounts[i],
+cvh_huffsyms[i], sym_size, 0);
 }
 
 for (i = 0; i < q->num_subpackets; i++) {
 if (q->subpacket[i].joint_stereo == 1) {
-result |= 
ff_init_vlc_from_lengths(&q->subpacket[i].channel_coupling, 6,
-   (1 << 
q->subpacket[i].js_vlc_bits) - 1,
-   
ccpl_huffbits[q->subpacket[i].js_vlc_bits - 2], 1,
-   
ccpl_huffsyms[q->subpacket[i].js_vlc_bits - 2], 1, 1,
-   0, 0);
+result |= build_vlc(&q->subpacket[i].channel_coupling, 6,
+ccpl_huffcounts[q->subpacket[i].js_vlc_bits - 
2],
+ccpl_huffsyms[q->subpacket[i].js_vlc_bits - 
2], 1,
+0);
 av_log(q->avctx, AV_LOG_DEBUG, "subpacket %i Joint-stereo VLC 
used.\n", i);
 }
 }
diff --git a/libavcodec/cookdata.h b/libavcodec/cookdata.h
index 05d1eef23b..a4ca1201bf 100644
--- a/libavcodec/cookdata.h
+++ b/libavcodec/cookdata.h
@@ -70,41 +70,26 @@ static const int vpr_tab[7] = {
 
 /* VLC data */
 
-static const int vhsize_tab[7] = {
-181, 94, 48, 520, 209, 192, 32,
-};
+#define MAX_COOK_VLC_ENTRIES 520
 
 static const int vhvlcsize_tab[7] = {
 8, 7, 7, 10, 9, 9, 6,
 };
 
-static const uint8_t envelope_quant_index_huffbits[13][24] = {
-{ 3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,
-  5,  5,  6,  7,  8,  9, 11, 11, 12, 12, 12, 12 },
-{ 3,  3,  3,  3,  3,  3,  4,  4,  5,  5,  5,  6,
-  7,  8,  9, 10, 11, 12, 13, 15, 15, 15, 16, 16 },
-{ 3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,
-  5,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 14 },
-{ 3,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,
-  7,  7,  7,  9,  9,  9, 10, 11, 13, 13, 13, 13 },
-{ 3,  3,  3,  3,  3,  4,  4,  4,  5,  5,  5,  5,
-  6,  6,  6,  7,  8,  9, 10, 11, 12, 13, 14, 14 },
-{ 3,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,
-  7,  7,  8,  8,  8,  9, 10, 11, 12, 13, 14, 14 },
-{ 2,  3,  3,  3,  3,  4,  4,  5,  5,  5,  6,  7,
-  8,  9, 10, 11, 12, 13, 15, 15, 16, 16, 16, 16 },
-{ 2,  3,  3,  3,  3,  4,  4,  5,  5,  5,  7,  7,
-  7,  9,  9,  9, 10, 11, 12, 14, 14, 14, 15, 15 },
-{ 3,  3,  3,  3,  3,  3,  4,  4,  5,  5,  6,  6,
-  7,  7,  8,  8,  9,  9,  9, 10, 11, 12, 13, 13 },
-{ 3,  3,  3,  3,  3,  3,  4,  4,  5,

[FFmpeg-devel] [PATCH 029/114] avcodec/rv10: Make VLC tables smaller

2020-11-10 Thread Andreas Rheinhardt
These tables were huge (14 bits) because one needed 14 bits in order to
find out whether a code is valid and in the VLC table or a valid code that
required hacky workarounds due to RealVideo 1.0 using multiple codes
for the same symbol and the code predating the introduction of symbols
tables for VLCs.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv10.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index 08887c121b..a7cb8118e5 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -46,7 +46,7 @@
 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
 
 #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
-#define DC_VLC_BITS 14 // FIXME find a better solution
+#define DC_VLC_BITS9
 
 typedef struct RVDecContext {
 MpegEncContext m;
@@ -409,14 +409,14 @@ static av_cold int rv10_decode_init(AVCodecContext *avctx)
 
 /* init rv vlc */
 if (!done) {
-static VLC_TYPE table[16896 + 16640][2];
+static VLC_TYPE table[1472 + 992][2];
 
 rv_dc_lum.table = table;
-rv_dc_lum.table_allocated   = 16896;
+rv_dc_lum.table_allocated   = 1472;
 rv10_build_vlc(&rv_dc_lum, rv_lum_len_count,
rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len));
-rv_dc_chrom.table   = &table[16896];
-rv_dc_chrom.table_allocated = 16640;
+rv_dc_chrom.table   = &table[1472];
+rv_dc_chrom.table_allocated = 992;
 rv10_build_vlc(&rv_dc_chrom, rv_chrom_len_count,
rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len) - 2);
 done = 1;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 027/114] avcodec/rv10: Reduce number of exceptions when reading VLC value

2020-11-10 Thread Andreas Rheinhardt
RealVideo 1.0 uses an insane way to encode DC coefficients: There are
several symbols that (for no good reason whatsoever) have multiple
encodings, leading to longer codes than necessary.

More specifically, the tree for the 256 luma symbols contains 255 codes
belonging to 255 different symbols on the left; going further right,
the tree consists of two blocks of 128 codes each of length 14 encoding
consecutive numbers (including two encodings for the symbol missing among
the 255 codes on the left); this is followed by two blocks of codes of
length 16 each containing 256 elements with consecutive symbols (i.e.
each of the blocks allows to encode all symbols). The rest of the tree
consists of 2^11 codes that all encode the same symbol.

The tree for the 256 chroma symbols is similar, but is missing the
blocks of length 256 and there are only 2^9 consecutive codes that
encode the same symbol; furthermore, the chroma tree is incomplete:
The right-most node has no right child.

All of this caused problems when parsing these codes; the reason is that
the code for this predates commit b613bacca9c256f1483c46847f713e47a0e9a5f6
which added support for explicit symbol tables and thereby removed the
requirement that different codes have different symbols. In order to
address this, the trees used for parsing were incomplete: They contained
the 255 codes on the left and one code for the remaining symbol. Whenever
a code not in these trees was encountered, it was dealt with in
special cases (one for each of the blocks mentioned above).

This commit reduces the number of special cases: Using a symbols table
allows to treat the blocks of consecutive symbols like ordinary codes;
only the blocks encoding a single symbol are still treated specially
(in order not to waste memory on tables for them).

In order to not increment the size of the tables used to initialize the
VLCs both the symbols as well as the lengths are now run-length encoded.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv10.c | 137 ++
 1 file changed, 53 insertions(+), 84 deletions(-)

diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index 03c732e92d..370d2c7dd4 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -45,6 +45,7 @@
 #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
 
+#define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
 #define DC_VLC_BITS 14 // FIXME find a better solution
 
 typedef struct RVDecContext {
@@ -53,63 +54,25 @@ typedef struct RVDecContext {
 int orig_width, orig_height;
 } RVDecContext;
 
-/* The elements with negative length in the bits table correspond to
- * open ends in the respective Huffman tree. */
-static const uint8_t rv_sym[] = {
-128, 127, 129, 125, 126, 130, 131, 121, 122, 123, 124, 132, 133, 134, 135,
-113, 114, 115, 116, 117, 118, 119, 120, 136, 137, 138, 139, 140, 141, 142,
-143,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
-111, 112, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
-157, 158, 159,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,
- 77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,
- 92,  93,  94,  95,  96, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
-170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
-185, 186, 187, 188, 189, 190, 191,   1,   2,   3,   4,   5,   6,   7,   8,
-  9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,
- 24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
- 39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,
- 54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64, 192, 193, 194, 195,
-196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
-211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
-226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
-241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
-  0,   0,   0,   0,   0,   0,   0,   0,
+/* (run, length) encoded value for the symbols table. The actual symbols
+ * are run..run + length (mod 256).
+ * The last two entries in the following table apply to luma only.
+ * The skip values are not included in this list. */
+static const uint8_t rv_sym_run_len[][2] = {
+{ 128,   0 }, { 127,   0 }, { 129,   0 }, { 125,   1 }, { 130,  1 },
+{ 121,   3 }, { 132,   3 }, { 113,   7 }, { 136,   7 }, {  97, 15 },
+{ 144,  15 }, {  65,  31 }, { 160,  31 }, {   1,  63 }, { 192, 63 },
+{ 129, 127 }, {   0, 127 }, {   1, 255 }, {   0, 255 },
 };
 
-static const uint8_t rv_lum_len[] = {
- 2,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,
- 7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,
- 8,  8,  8,  8,  8,  8,  8,  8,  

[FFmpeg-devel] [PATCH 030/114] avcodec/rv10: Simplify handling of skip VLC entries

2020-11-10 Thread Andreas Rheinhardt
The VLC tables to be used for parsing RealVideo 1.0 DC coefficients are
weird: The luma table contains a block of 2^11 codes beginning with the
same prefix and length that all have the same symbol (i.e. value only
depends upon the prefix); the same goes for the chroma block (except
it's only 2^9 codes). Up until now, these entries (which generally could
be parsed like ordinary entries with subtables) have been treated
specially: They have been treated like open ends of the tree, so that
get_vlc2() returned a value < 0 upon encountering them; afterwards it
was checked whether the right prefix was used and if so, the appropriate
number of bytes was skipped.

But there is actually an easy albeit slightly hacky way to support them
directly without pointless subtables: Just modify the VLC table so that
all the entries sharing the right prefix have a length that equals the
length of the whole entry.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv10.c | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index a7cb8118e5..52e862f1b9 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -83,22 +83,11 @@ int ff_rv_decode_dc(MpegEncContext *s, int n)
 
 if (n < 4) {
 code = get_vlc2(&s->gb, rv_dc_lum.table, DC_VLC_BITS, 2);
-if (code < 0) {
-/* Skip entry - no error. */
-skip_bits(&s->gb, 18);
-code = 255;
-}
 } else {
 code = get_vlc2(&s->gb, rv_dc_chrom.table, DC_VLC_BITS, 2);
 if (code < 0) {
-if (show_bits(&s->gb, 9) == 0x1FE) {
-/* Skip entry - no error. */
-skip_bits(&s->gb, 18);
-code = 255;
-} else {
-av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
-return -1;
-}
+av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
+return -1;
 }
 }
 return code;
@@ -415,10 +404,21 @@ static av_cold int rv10_decode_init(AVCodecContext *avctx)
 rv_dc_lum.table_allocated   = 1472;
 rv10_build_vlc(&rv_dc_lum, rv_lum_len_count,
rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len));
+for (int i = 0; i < 1 << (DC_VLC_BITS - 7 /* Length of skip prefix 
*/); i++) {
+/* All codes beginning with 0x7F have the same length and value.
+ * Modifying the table directly saves us the useless subtables. */
+rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i][0] = 255;
+rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i][1] = 18;
+}
 rv_dc_chrom.table   = &table[1472];
 rv_dc_chrom.table_allocated = 992;
 rv10_build_vlc(&rv_dc_chrom, rv_chrom_len_count,
rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len) - 2);
+for (int i = 0; i < 1 << (DC_VLC_BITS - 9 /* Length of skip prefix 
*/); i++) {
+/* Same as above. */
+rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i][0] = 255;
+rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i][1] = 18;
+}
 done = 1;
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 031/114] avcodec/mimic: Reduce size of tables used to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
By switching to ff_init_vlc_from_lengths() one can replace a table of
codes of type uint32_t with a table of symbols of type uint8_t saving
space. The old tables also had holes in it (because of the symbols) which
are now superfluous, saving ever more space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mimic.c | 55 --
 1 file changed, 19 insertions(+), 36 deletions(-)

diff --git a/libavcodec/mimic.c b/libavcodec/mimic.c
index 0f8103ef85..32f02d4b61 100644
--- a/libavcodec/mimic.c
+++ b/libavcodec/mimic.c
@@ -65,43 +65,25 @@ typedef struct MimicContext {
 int next_prev_index;
 } MimicContext;
 
-static const uint32_t huffcodes[] = {
-0x000a, 0x, 0x, 0x, 0x, 0x,
-0x, 0x, 0x, 0x, 0x, 0x,
-0x, 0x, 0x, 0x, 0x, 0x000b,
-0x001b, 0x0038, 0x0078, 0x0079, 0x007a, 0x00f9,
-0x00fa, 0x03fb, 0x07f8, 0x07f9, 0x07fa, 0x07fb,
-0x0ff8, 0x0ff9, 0x0001, 0x0039, 0x007b, 0x00fb,
-0x01f8, 0x01f9, 0x0ffa, 0x0ffb, 0x1ff8, 0x1ff9,
-0x1ffa, 0x1ffb, 0x3ff8, 0x3ff9, 0x3ffa, 0x,
-0x0004, 0x003a, 0x01fa, 0x3ffb, 0x7ff8, 0x7ff9,
-0x7ffa, 0x7ffb, 0xfff8, 0xfff9, 0xfffa, 0xfffb,
-0x0001fff8, 0x0001fff9, 0x0001fffa, 0x, 0x000c, 0x00f8,
-0x01fb, 0x0001fffb, 0x0003fff8, 0x0003fff9, 0x0003fffa, 0x0003fffb,
-0x0007fff8, 0x0007fff9, 0x0007fffa, 0x0007fffb, 0x0008, 0x0009,
-0x000a, 0x, 0x001a, 0x03f8, 0x000b, 0x0018,
-0x0019, 0x001a, 0x001b, 0x0038, 0x0039, 0x003a,
-0x003b, 0x0078, 0x0079, 0x007a, 0x007b, 0x,
-0x003b, 0x03f9, 0x00f8, 0x00f9, 0x00fa, 0x00fb,
-0x01f8, 0x01f9, 0x01fa, 0x01fb, 0x03f8, 0x03f9,
-0x03fa, 0x03fb, 0x07f8, 0x, 0x03fa, 0x07f9,
-0x07fa, 0x07fb, 0x0ff8, 0x0ff9, 0x0ffa, 0x0ffb,
-0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 0x3ff8, 0x3ff9,
-0x3ffa,
+static const uint8_t huffsyms[] = {
+0x10, 0x20, 0x30, 0x00, 0x11, 0x40, 0x50, 0x12, 0x13, 0x21, 0x31, 0x60,
+0x14, 0x15, 0x16, 0x22, 0x41, 0x17, 0x18, 0x23, 0x24, 0x25, 0x32, 0x42,
+0x51, 0x61, 0x70, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x26, 0x27,
+0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x33, 0x34, 0x35, 0x36, 0x37,
+0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x43, 0x44, 0x45, 0x46, 0x47,
+0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x52, 0x53, 0x54, 0x55, 0x56,
+0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x62, 0x63, 0x64, 0x65,
+0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x71, 0x72, 0x73,
+0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E,
 };
 
 static const uint8_t huffbits[] = {
- 4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
- 0,  0,  0,  0,  2,  4,  5,  6,  7,  7,  7,  8,
- 8, 10, 11, 11, 11, 11, 12, 12,  2,  6,  7,  8,
- 9,  9, 12, 12, 13, 13, 13, 13, 14, 14, 14,  0,
- 3,  6,  9, 14, 15, 15, 15, 15, 16, 16, 16, 16,
-17, 17, 17,  0,  4,  8,  9, 17, 18, 18, 18, 18,
-19, 19, 19, 19, 20, 20, 20,  0,  5, 10, 20, 21,
-21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23,  0,
- 6, 10, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26,
-26, 26, 27,  0, 10, 27, 27, 27, 28, 28, 28, 28,
-29, 29, 29, 29, 30, 30, 30,
+ 2,  2,  3,  4,  4,  4,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,
+ 8,  8,  9,  9,  9,  9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12,
+13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17,
+17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
+22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26,
+26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30,
 };
 
 static const uint8_t col_zag[64] = {
@@ -142,8 +124,9 @@ static av_cold int mimic_decode_init(AVCodecContext *avctx)
 ctx->prev_index = 0;
 ctx->cur_index  = 15;
 
-if ((ret = init_vlc(&ctx->vlc, MIMIC_VLC_BITS, FF_ARRAY_ELEMS(huffbits),
-huffbits, 1, 1, huffcodes, 4, 4, 0)) < 0) {
+ret = ff_init_vlc_from_lengths(&ctx->vlc, MIMIC_VLC_BITS, 
FF_ARRAY_ELEMS(huffbits),
+   huffbits, 1, huffsyms, 1, 1, 0, 0);
+if (ret < 0) {
 av_log(avctx, AV_LOG_ERROR, "error initializing vlc table\n");
 return ret;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe"

[FFmpeg-devel] [PATCH 028/114] avcodec/rv10: Use symbol table more effectively

2020-11-10 Thread Andreas Rheinhardt
The RealVideo 1.0 decoder uses VLCs to parse DC coefficients. But the
values returned from get_vlc2() are not directly used; instead
-(val - 128) (which is in the range -127..128) is. This transformation
is unnecessary as it can effectively be done when initializing the VLC
by modifying the symbols table used. There is just one minor
complication: The chroma table is incomplete and in order to distinguish
an error from get_vlc2() (due to an invalid code) the ordinary return
range is modified to 0..255. This is possible because the only caller of
this function is (on success) only interested in the return value modulo
256.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/ituh263dec.c |  2 +-
 libavcodec/rv10.c   | 24 ++--
 2 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/libavcodec/ituh263dec.c b/libavcodec/ituh263dec.c
index c1005b0994..ced7fa52ee 100644
--- a/libavcodec/ituh263dec.c
+++ b/libavcodec/ituh263dec.c
@@ -467,7 +467,7 @@ static int h263_decode_block(MpegEncContext * s, int16_t * 
block,
 level = s->last_dc[component];
 if (s->rv10_first_dc_coded[component]) {
 diff = ff_rv_decode_dc(s, n);
-if (diff == 0x)
+if (diff < 0)
 return -1;
 level += diff;
 level = level & 0xff; /* handle wrap round */
diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index 370d2c7dd4..08887c121b 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -55,14 +55,14 @@ typedef struct RVDecContext {
 } RVDecContext;
 
 /* (run, length) encoded value for the symbols table. The actual symbols
- * are run..run + length (mod 256).
+ * are run..run - length (mod 256).
  * The last two entries in the following table apply to luma only.
  * The skip values are not included in this list. */
 static const uint8_t rv_sym_run_len[][2] = {
-{ 128,   0 }, { 127,   0 }, { 129,   0 }, { 125,   1 }, { 130,  1 },
-{ 121,   3 }, { 132,   3 }, { 113,   7 }, { 136,   7 }, {  97, 15 },
-{ 144,  15 }, {  65,  31 }, { 160,  31 }, {   1,  63 }, { 192, 63 },
-{ 129, 127 }, {   0, 127 }, {   1, 255 }, {   0, 255 },
+{   0,   0 }, {   1,   0 }, { 255,   0 }, {   3,   1 }, { 254,  1 },
+{   7,   3 }, { 252,   3 }, {  15,   7 }, { 248,   7 }, {  31, 15 },
+{ 240,  15 }, {  63,  31 }, { 224,  31 }, { 127,  63 }, { 192, 63 },
+{ 255, 127 }, { 128, 127 }, { 127, 255 }, { 128, 255 },
 };
 
 /* entry[i] of the following tables gives
@@ -86,9 +86,7 @@ int ff_rv_decode_dc(MpegEncContext *s, int n)
 if (code < 0) {
 /* Skip entry - no error. */
 skip_bits(&s->gb, 18);
-code = 1;
-} else {
-code -= 128;
+code = 255;
 }
 } else {
 code = get_vlc2(&s->gb, rv_dc_chrom.table, DC_VLC_BITS, 2);
@@ -96,16 +94,14 @@ int ff_rv_decode_dc(MpegEncContext *s, int n)
 if (show_bits(&s->gb, 9) == 0x1FE) {
 /* Skip entry - no error. */
 skip_bits(&s->gb, 18);
-code = 1;
+code = 255;
 } else {
 av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
-return 0x;
+return -1;
 }
-} else {
-code -= 128;
 }
 }
-return -code;
+return code;
 }
 
 /* read RV 1.0 compatible frame header */
@@ -336,7 +332,7 @@ static av_cold void rv10_build_vlc(VLC *vlc, const uint16_t 
len_count[15],
 for (unsigned i = 0; i < sym_rl_elems; i++) {
 unsigned cur_sym = sym_rl[i][0];
 for (unsigned tmp = nb_syms + sym_rl[i][1]; nb_syms <= tmp; nb_syms++)
-syms[nb_syms] = 0xFF & cur_sym++;
+syms[nb_syms] = 0xFF & cur_sym--;
 }
 
 for (unsigned i = 0; i < 15; i++)
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 033/114] avcodec/motionpixels: Only create VLC iff it is going to be used

2020-11-10 Thread Andreas Rheinhardt
If the Huffman tree consists of only one entry (which has length zero),
no tree is used at all for parsing as the VLC API currently can't handle
this. So it makes no sense to create a VLC in this case.

Commit 41b7389cade702383e59343561776f83bb26e17f added a check for
whether creating the VLC should be skipped, but it also skipped decoding
the packet and it used the wrong check: It checked max_codes_bits,
the maximum length of code; but this value is only updated iff there is
more than one Huffman entry. So if there is only one Huffman entry, and
there was a previous frame with more than one entry, then a VLC was
created unnecessarily; yet if there was no previous frame with more than
one entry, then this frame will be skipped which is probably
spec-incompliant. I have no sample for the latter.

This commit improves the check to create a VLC iff it is going to be
used.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/motionpixels.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index b48200b017..0bf153f883 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -324,8 +324,7 @@ static int mp_decode_frame(AVCodecContext *avctx,
 if (sz == 0)
 goto end;
 
-if (mp->max_codes_bits <= 0)
-goto end;
+if (mp->codes_count > 1)
 if (init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, 
&mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 
4, 0))
 goto end;
 mp_decode_frame_helper(mp, &gb);
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 032/114] avcodec/mimic: Make VLC static

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mimic.c | 18 +++---
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mimic.c b/libavcodec/mimic.c
index 32f02d4b61..3e4d7c2576 100644
--- a/libavcodec/mimic.c
+++ b/libavcodec/mimic.c
@@ -58,13 +58,14 @@ typedef struct MimicContext {
 BswapDSPContext bbdsp;
 HpelDSPContext  hdsp;
 IDCTDSPContext  idsp;
-VLC vlc;
 
 /* Kept in the context so multithreading can have a constant to read from 
*/
 int next_cur_index;
 int next_prev_index;
 } MimicContext;
 
+static VLC block_vlc;
+
 static const uint8_t huffsyms[] = {
 0x10, 0x20, 0x30, 0x00, 0x11, 0x40, 0x50, 0x12, 0x13, 0x21, 0x31, 0x60,
 0x14, 0x15, 0x16, 0x22, 0x41, 0x17, 0x18, 0x23, 0x24, 0x25, 0x32, 0x42,
@@ -111,25 +112,20 @@ static av_cold int mimic_decode_end(AVCodecContext *avctx)
 av_frame_free(&ctx->frames[i].f);
 }
 
-ff_free_vlc(&ctx->vlc);
-
 return 0;
 }
 
 static av_cold int mimic_decode_init(AVCodecContext *avctx)
 {
 MimicContext *ctx = avctx->priv_data;
-int ret, i;
+int i;
 
 ctx->prev_index = 0;
 ctx->cur_index  = 15;
 
-ret = ff_init_vlc_from_lengths(&ctx->vlc, MIMIC_VLC_BITS, 
FF_ARRAY_ELEMS(huffbits),
-   huffbits, 1, huffsyms, 1, 1, 0, 0);
-if (ret < 0) {
-av_log(avctx, AV_LOG_ERROR, "error initializing vlc table\n");
-return ret;
-}
+if (!block_vlc.table_size)
+INIT_VLC_STATIC_FROM_LENGTHS(&block_vlc, MIMIC_VLC_BITS, 
FF_ARRAY_ELEMS(huffbits),
+ huffbits, 1, huffsyms, 1, 1, 0, 0, 4368);
 ff_blockdsp_init(&ctx->bdsp, avctx);
 ff_bswapdsp_init(&ctx->bbdsp);
 ff_hpeldsp_init(&ctx->hdsp, avctx->flags);
@@ -223,7 +219,7 @@ static int vlc_decode_block(MimicContext *ctx, int 
num_coeffs, int qscale)
 int value;
 int coeff;
 
-vlc = get_vlc2(&ctx->gb, ctx->vlc.table, MIMIC_VLC_BITS, 3);
+vlc = get_vlc2(&ctx->gb, block_vlc.table, MIMIC_VLC_BITS, 3);
 if (!vlc) /* end-of-block code */
 return 0;
 if (vlc == -1)
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 035/114] avcodec/motionpixels: Don't check for complete VLC

2020-11-10 Thread Andreas Rheinhardt
The algorithm used here always creates a complete VLC, so it is
unnecessary to check this again.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/motionpixels.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index 4b9830fbed..a65250efe5 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -195,8 +195,6 @@ static av_always_inline int mp_get_vlc(MotionPixelsContext 
*mp, GetBitContext *g
 int i;
 
 i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, 
mp->max_codes_bits, 1);
-if (i < 0)
-return i;
 return mp->codes[i].delta;
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 034/114] avcodec/motionpixels: Be more strict when parsing Huffman trees

2020-11-10 Thread Andreas Rheinhardt
This ensures that the number of leafs in the Huffman tree equals the
number it is supposed to be and therefore ensures that the VLC tree is
complete, allowing us to remove checks.

Signed-off-by: Andreas Rheinhardt 
---
Alternatively one could use mp->current_codes_count when initializing
the VLC.

 libavcodec/motionpixels.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index 0bf153f883..4b9830fbed 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -133,7 +133,7 @@ static int mp_get_code(MotionPixelsContext *mp, 
GetBitContext *gb, int size, int
 if (mp_get_code(mp, gb, size, code + 1) < 0)
 return AVERROR_INVALIDDATA;
 }
-if (mp->current_codes_count >= MAX_HUFF_CODES) {
+if (mp->current_codes_count >= mp->codes_count) {
 av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n");
 return AVERROR_INVALIDDATA;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 038/114] avcodec/mpc7: Reduce size of tables used to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
By switching to ff_init_vlc_from_lengths() one can replace tables of
codes of type uint16_t with tables of symbols of type uint8_t, saving
space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc7.c |  27 
 libavcodec/mpc7data.h | 156 --
 2 files changed, 90 insertions(+), 93 deletions(-)

diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index 6482029efc..532ea113c2 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -54,7 +54,7 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
 
 static VLC_TYPE quant_tables[7224][2];
 VLC_TYPE (*quant_table)[2] = quant_tables;
-const uint16_t *raw_quant_table = mpc7_quant_vlcs;
+const uint8_t *raw_quant_table = mpc7_quant_vlcs;
 
 /* Musepack SV7 is always stereo */
 if (avctx->channels != 2) {
@@ -93,23 +93,24 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
 
 if(vlc_initialized) return 0;
 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
-INIT_VLC_STATIC(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
-&mpc7_scfi[1], 2, 1,
-&mpc7_scfi[0], 2, 1, 1 << MPC7_SCFI_BITS);
-INIT_VLC_STATIC(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
-&mpc7_dscf[1], 2, 1,
-&mpc7_dscf[0], 2, 1, 1 << MPC7_DSCF_BITS);
-INIT_VLC_STATIC(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
-&mpc7_hdr[1], 2, 1,
-&mpc7_hdr[0], 2, 1, 1 << MPC7_HDR_BITS);
+INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
+ &mpc7_scfi[1], 2,
+ &mpc7_scfi[0], 2, 1, 0, 0, 1 << 
MPC7_SCFI_BITS);
+INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
+ &mpc7_dscf[1], 2,
+ &mpc7_dscf[0], 2, 1, 0, 0, 1 << 
MPC7_DSCF_BITS);
+INIT_VLC_STATIC_FROM_LENGTHS(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
+ &mpc7_hdr[1], 2,
+ &mpc7_hdr[0], 2, 1, 0, 0, 1 << MPC7_HDR_BITS);
 for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
 for(j = 0; j < 2; j++){
 quant_vlc[i][j].table = quant_table;
 quant_vlc[i][j].table_allocated = quant_sizes[i * 2 + j];
 quant_table += quant_sizes[i * 2 + j];
-init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
- raw_quant_table + 1, 4, 2,
- raw_quant_table, 4, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&quant_vlc[i][j], 9, 
mpc7_quant_vlc_sizes[i],
+ &raw_quant_table[1], 2,
+ &raw_quant_table[0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 raw_quant_table += 2 * mpc7_quant_vlc_sizes[i];
 }
 }
diff --git a/libavcodec/mpc7data.h b/libavcodec/mpc7data.h
index b9c5f208a4..b578cbdb27 100644
--- a/libavcodec/mpc7data.h
+++ b/libavcodec/mpc7data.h
@@ -33,21 +33,21 @@ static const int8_t mpc7_idx51[] = { 
-2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0,
 #define MPC7_SCFI_SIZE  4
 #define MPC7_SCFI_BITS  3
 static const uint8_t mpc7_scfi[MPC7_SCFI_SIZE * 2] = {
-0x2, 3, 0x1, 1, 0x3, 3, 0x0, 2
+0x03, 2, 0x00, 3, 0x02, 3, 0x01, 1,
 };
 
 #define MPC7_DSCF_SIZE 16
 #define MPC7_DSCF_BITS  6
 static const uint8_t mpc7_dscf[MPC7_DSCF_SIZE * 2] = {
-0x20, 6, 0x04, 5, 0x11, 5, 0x1E, 5, 0x0D, 4, 0x00, 3, 0x03, 3, 0x09, 4,
-0x05, 3, 0x02, 3, 0x0E, 4, 0x03, 4, 0x1F, 5, 0x05, 5, 0x21, 6, 0x0C, 4
+0x05, 3, 0x01, 5, 0x0D, 5, 0x0B, 4, 0x09, 3, 0x06, 3, 0x00, 6, 0x0E, 6,
+0x02, 5, 0x07, 4, 0x08, 3, 0x0F, 4, 0x04, 4, 0x0A, 4, 0x03, 5, 0x0C, 5,
 };
 
 #define MPC7_HDR_SIZE  10
 #define MPC7_HDR_BITS   9
 static const uint8_t mpc7_hdr[MPC7_HDR_SIZE * 2] = {
-0x5C, 8, 0x2F, 7, 0x0A, 5, 0x04, 4, 0x00, 2,
-0x01, 1, 0x03, 3, 0x16, 6, 0xBB, 9, 0xBA, 9
+0x04, 2, 0x03, 4, 0x02, 5, 0x07, 6, 0x00, 8,
+0x09, 9, 0x08, 9, 0x01, 7, 0x06, 3, 0x05, 1,
 };
 
 #define MPC7_QUANT_VLC_TABLES 7
@@ -59,81 +59,77 @@ static const uint8_t 
mpc7_quant_vlc_off[MPC7_QUANT_VLC_TABLES] = {
 0, 0, 3, 4, 7, 15, 31
 };
 
-static const uint16_t mpc7_quant_vlcs[177 * 2 * 2] = {
-0x0036,  6, 0x0009,  5, 0x0020,  6, 0x0005,  5, 0x000A,  4, 0x0007,  5,
-0x0034,  6, 0x,  5, 0x0023,  6, 0x000A,  5, 0x0006,  4, 0x0004,  5,
-0x000B,  4, 0x0007,  3, 0x000C,  4, 0x0003,  5, 0x0007,  4, 0x000B,  5,
-0x0022,  6, 0x0001,  5, 0x0035,  6, 0x0006,  5, 0x0009,  4, 0x0002,  5,
-0x0021,  6, 0x0008,  5, 0x0037,  6,
-0x0067,  8, 0x003E,  7, 0x00E1,  9, 0x0037,  7, 0x0003,  4, 0x0034,  7,
-0x0065,  8, 0x003C,  7, 0x00E3,  9, 0x0018,  6, 0x,  4, 0x003D,  7,
-0x0004,  4, 0x0001,  1, 0x0005,  4, 0x003F,  7, 0x0001,  4, 0x003B,  7,
-0x00E2,  9, 0x0039,  7, 0x0064,  8, 0x0

[FFmpeg-devel] [PATCH 037/114] avcodec/motionpixels: Simplify creating VLC tables

2020-11-10 Thread Andreas Rheinhardt
By using ff_init_vlc_from_lengths(), we do not have to keep track of the
codes themselves, but can offload this to ff_init_vlc_from_lengths().

Furthermore, the old code presumed sizeof(int) == 4; this is no longer
so.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/motionpixels.c | 19 +--
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index 927f9fdc14..3bc31870d9 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -29,7 +29,6 @@
 #include "motionpixels_tablegen.h"
 
 typedef struct HuffCode {
-int code;
 uint8_t size;
 uint8_t delta;
 } HuffCode;
@@ -121,7 +120,7 @@ static void mp_read_changes_map(MotionPixelsContext *mp, 
GetBitContext *gb, int
 }
 }
 
-static int mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, 
int code)
+static int mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size)
 {
 while (get_bits1(gb)) {
 ++size;
@@ -129,8 +128,7 @@ static int mp_get_code(MotionPixelsContext *mp, 
GetBitContext *gb, int size, int
 av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, 
mp->max_codes_bits);
 return AVERROR_INVALIDDATA;
 }
-code <<= 1;
-if (mp_get_code(mp, gb, size, code + 1) < 0)
+if (mp_get_code(mp, gb, size) < 0)
 return AVERROR_INVALIDDATA;
 }
 if (mp->current_codes_count >= mp->codes_count) {
@@ -138,7 +136,6 @@ static int mp_get_code(MotionPixelsContext *mp, 
GetBitContext *gb, int size, int
 return AVERROR_INVALIDDATA;
 }
 
-mp->codes[mp->current_codes_count  ].code = code;
 mp->codes[mp->current_codes_count++].size = size;
 return 0;
 }
@@ -155,7 +152,7 @@ static int mp_read_codes_table(MotionPixelsContext *mp, 
GetBitContext *gb)
 for (i = 0; i < mp->codes_count; ++i)
 mp->codes[i].delta = get_bits(gb, 4);
 mp->current_codes_count = 0;
-if ((ret = mp_get_code(mp, gb, 0, 0)) < 0)
+if ((ret = mp_get_code(mp, gb, 0)) < 0)
 return ret;
 if (mp->current_codes_count < mp->codes_count) {
 av_log(mp->avctx, AV_LOG_ERROR, "too few codes\n");
@@ -321,10 +318,12 @@ static int mp_decode_frame(AVCodecContext *avctx,
 goto end;
 
 if (mp->codes_count > 1) {
-ret = ff_init_vlc_sparse(&mp->vlc, mp->max_codes_bits, mp->codes_count,
- &mp->codes[0].size,  sizeof(HuffCode), 1,
- &mp->codes[0].code,  sizeof(HuffCode), 4,
- &mp->codes[0].delta, sizeof(HuffCode), 1, 0);
+/* The entries of the mp->codes array are sorted from right to left
+ * in the Huffman tree, hence -(int)sizeof(HuffCode). */
+ret = ff_init_vlc_from_lengths(&mp->vlc, mp->max_codes_bits, 
mp->codes_count,
+   &mp->codes[mp->codes_count - 1].size,  
-(int)sizeof(HuffCode),
+   &mp->codes[mp->codes_count - 1].delta, 
-(int)sizeof(HuffCode), 1,
+   0, 0);
 if (ret < 0)
 goto end;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 036/114] avcodec/motionpixels: Use symbols table

2020-11-10 Thread Andreas Rheinhardt
If allows us to directly store the deltas in the VLC table and therefore
avoids a level of indirection.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/motionpixels.c | 17 ++---
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index a65250efe5..927f9fdc14 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -192,10 +192,8 @@ static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, 
int x, int y, const Yuv
 
 static av_always_inline int mp_get_vlc(MotionPixelsContext *mp, GetBitContext 
*gb)
 {
-int i;
-
-i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, 
mp->max_codes_bits, 1);
-return mp->codes[i].delta;
+return mp->vlc.table ? get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1)
+ : mp->codes[0].delta;
 }
 
 static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y)
@@ -322,9 +320,14 @@ static int mp_decode_frame(AVCodecContext *avctx,
 if (sz == 0)
 goto end;
 
-if (mp->codes_count > 1)
-if (init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, 
&mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 
4, 0))
-goto end;
+if (mp->codes_count > 1) {
+ret = ff_init_vlc_sparse(&mp->vlc, mp->max_codes_bits, mp->codes_count,
+ &mp->codes[0].size,  sizeof(HuffCode), 1,
+ &mp->codes[0].code,  sizeof(HuffCode), 4,
+ &mp->codes[0].delta, sizeof(HuffCode), 1, 0);
+if (ret < 0)
+goto end;
+}
 mp_decode_frame_helper(mp, &gb);
 ff_free_vlc(&mp->vlc);
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 042/114] avcodec/mpc8: Apply offsets when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc8.c | 21 +
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index 339ee515b8..31b7ad14ac 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -39,9 +39,6 @@
 static VLC band_vlc, scfi_vlc[2], dscf_vlc[2], res_vlc[2];
 static VLC q1_vlc, q2_vlc[2], q3_vlc[2], quant_vlc[4][2], q9up_vlc;
 
-static const int q3_offsets[2] = { MPC8_Q3_OFFSET, MPC8_Q4_OFFSET };
-static const int quant_offsets[6] = { MPC8_Q5_OFFSET, MPC8_Q6_OFFSET, 
MPC8_Q7_OFFSET, MPC8_Q8_OFFSET };
-
 static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)
 {
 int len = mpc8_cnk_len[k-1][n-1] - 1;
@@ -163,10 +160,10 @@ static av_cold int mpc8_decode_init(AVCodecContext * 
avctx)
 
 INIT_VLC_STATIC_FROM_LENGTHS(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
  mpc8_q3_bits, 1,
- mpc8_q3_syms, 1, 1, 0, 0, 512);
+ mpc8_q3_syms, 1, 1, MPC8_Q3_OFFSET, 0, 512);
 INIT_VLC_STATIC_FROM_LENGTHS(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
  mpc8_q4_bits, 1,
- mpc8_q4_syms, 1, 1, 0, 0, 516);
+ mpc8_q4_syms, 1, 1, MPC8_Q4_OFFSET, 0, 516);
 
 for(i = 0; i < 2; i++){
 res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
@@ -188,25 +185,25 @@ static av_cold int mpc8_decode_init(AVCodecContext * 
avctx)
 ff_init_vlc_from_lengths(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE,
  mpc8_q5_bits[i], 1,
  mpc8_q5_syms[i], 1, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+ MPC8_Q5_OFFSET, INIT_VLC_USE_NEW_STATIC);
 quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
 quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
 ff_init_vlc_from_lengths(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE,
  mpc8_q6_bits[i], 1,
  mpc8_q6_syms[i], 1, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+ MPC8_Q6_OFFSET, INIT_VLC_USE_NEW_STATIC);
 quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
 quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
 ff_init_vlc_from_lengths(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE,
  mpc8_q7_bits[i], 1,
  mpc8_q7_syms[i], 1, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+ MPC8_Q7_OFFSET, INIT_VLC_USE_NEW_STATIC);
 quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
 quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - 
vlc_offsets[10+i];
 ff_init_vlc_from_lengths(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE,
  mpc8_q8_bits[i], 1,
  mpc8_q8_syms[i], 1, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+ MPC8_Q8_OFFSET, INIT_VLC_USE_NEW_STATIC);
 }
 vlc_initialized = 1;
 
@@ -359,7 +356,7 @@ static int mpc8_decode_frame(AVCodecContext * avctx, void 
*data,
 case 3:
 case 4:
 for(j = 0; j < SAMPLES_PER_BAND; j += 2){
-t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2) + 
q3_offsets[res - 3];
+t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2);
 c->Q[ch][off + j + 1] = t >> 4;
 c->Q[ch][off + j + 0] = sign_extend(t, 4);
 }
@@ -370,8 +367,8 @@ static int mpc8_decode_frame(AVCodecContext * avctx, void 
*data,
 case 8:
 cnt = 2 * mpc8_thres[res];
 for(j = 0; j < SAMPLES_PER_BAND; j++){
-t = get_vlc2(gb, quant_vlc[res - 5][cnt > 
mpc8_thres[res]].table, quant_vlc[res - 5][cnt > mpc8_thres[res]].bits, 2) + 
quant_offsets[res - 5];
-c->Q[ch][off + j] = t;
+const VLC *vlc = &quant_vlc[res - 5][cnt > 
mpc8_thres[res]];
+c->Q[ch][off + j] = get_vlc2(gb, vlc->table, vlc->bits, 2);
 cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
 }
 break;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 040/114] avcodec/mpc7: Avoid offsets table when creating VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc7.c | 17 +
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index 3c1a8578dd..7f6a01d444 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -38,22 +38,15 @@
 
 static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
 
-static const uint16_t quant_sizes[MPC7_QUANT_VLC_TABLES*2] =
-{
-512, 512, 512, 516, 512, 512, 512, 512, 512, 512, 512, 528, 512, 548
-};
-
-
 static av_cold int mpc7_decode_init(AVCodecContext * avctx)
 {
-int i, j;
+int j;
 MPCContext *c = avctx->priv_data;
 GetBitContext gb;
 LOCAL_ALIGNED_16(uint8_t, buf, [16]);
 static int vlc_initialized = 0;
 
 static VLC_TYPE quant_tables[7224][2];
-VLC_TYPE (*quant_table)[2] = quant_tables;
 const uint8_t *raw_quant_table = mpc7_quant_vlcs;
 
 /* Musepack SV7 is always stereo */
@@ -102,16 +95,16 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
 INIT_VLC_STATIC_FROM_LENGTHS(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
  &mpc7_hdr[1], 2,
  &mpc7_hdr[0], 2, 1, -5, 0, 1 << 
MPC7_HDR_BITS);
-for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
+for (unsigned i = 0, offset = 0; i < MPC7_QUANT_VLC_TABLES; i++){
 for(j = 0; j < 2; j++){
-quant_vlc[i][j].table = quant_table;
-quant_vlc[i][j].table_allocated = quant_sizes[i * 2 + j];
-quant_table += quant_sizes[i * 2 + j];
+quant_vlc[i][j].table   = &quant_tables[offset];
+quant_vlc[i][j].table_allocated = FF_ARRAY_ELEMS(quant_tables) - 
offset;
 ff_init_vlc_from_lengths(&quant_vlc[i][j], 9, 
mpc7_quant_vlc_sizes[i],
  &raw_quant_table[1], 2,
  &raw_quant_table[0], 2, 1,
  mpc7_quant_vlc_off[i], 
INIT_VLC_USE_NEW_STATIC);
 raw_quant_table += 2 * mpc7_quant_vlc_sizes[i];
+offset  += quant_vlc[i][j].table_size;
 }
 }
 vlc_initialized = 1;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 039/114] avcodec/mpc7: Apply offsets when creating VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc7.c | 12 ++--
 libavcodec/mpc7data.h |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index 532ea113c2..3c1a8578dd 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -98,10 +98,10 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
  &mpc7_scfi[0], 2, 1, 0, 0, 1 << 
MPC7_SCFI_BITS);
 INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
  &mpc7_dscf[1], 2,
- &mpc7_dscf[0], 2, 1, 0, 0, 1 << 
MPC7_DSCF_BITS);
+ &mpc7_dscf[0], 2, 1, -7, 0, 1 << 
MPC7_DSCF_BITS);
 INIT_VLC_STATIC_FROM_LENGTHS(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
  &mpc7_hdr[1], 2,
- &mpc7_hdr[0], 2, 1, 0, 0, 1 << MPC7_HDR_BITS);
+ &mpc7_hdr[0], 2, 1, -5, 0, 1 << 
MPC7_HDR_BITS);
 for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
 for(j = 0; j < 2; j++){
 quant_vlc[i][j].table = quant_table;
@@ -110,7 +110,7 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
 ff_init_vlc_from_lengths(&quant_vlc[i][j], 9, 
mpc7_quant_vlc_sizes[i],
  &raw_quant_table[1], 2,
  &raw_quant_table[0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+ mpc7_quant_vlc_off[i], 
INIT_VLC_USE_NEW_STATIC);
 raw_quant_table += 2 * mpc7_quant_vlc_sizes[i];
 }
 }
@@ -151,7 +151,7 @@ static inline void idx_to_quant(MPCContext *c, 
GetBitContext *gb, int idx, int *
 case  3: case  4: case  5: case  6: case  7:
 i1 = get_bits1(gb);
 for(i = 0; i < SAMPLES_PER_BAND; i++)
-*dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - 
mpc7_quant_vlc_off[idx-1];
+*dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2);
 break;
 case  8: case  9: case 10: case 11: case 12:
 case 13: case 14: case 15: case 16: case 17:
@@ -166,7 +166,7 @@ static inline void idx_to_quant(MPCContext *c, 
GetBitContext *gb, int idx, int *
 
 static int get_scale_idx(GetBitContext *gb, int ref)
 {
-int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
+int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1);
 if (t == 8)
 return get_bits(gb, 6);
 return ref + t;
@@ -222,7 +222,7 @@ static int mpc7_decode_frame(AVCodecContext * avctx, void 
*data,
 for(i = 0; i <= c->maxbands; i++){
 for(ch = 0; ch < 2; ch++){
 int t = 4;
-if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
+if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1);
 if(t == 4) bands[i].res[ch] = get_bits(&gb, 4);
 else bands[i].res[ch] = bands[i-1].res[ch] + t;
 if (bands[i].res[ch] < -1 || bands[i].res[ch] > 17) {
diff --git a/libavcodec/mpc7data.h b/libavcodec/mpc7data.h
index b578cbdb27..ee77393fce 100644
--- a/libavcodec/mpc7data.h
+++ b/libavcodec/mpc7data.h
@@ -55,8 +55,8 @@ static const uint8_t 
mpc7_quant_vlc_sizes[MPC7_QUANT_VLC_TABLES] = {
 27, 25, 7, 9, 15, 31, 63
 };
 
-static const uint8_t mpc7_quant_vlc_off[MPC7_QUANT_VLC_TABLES] = {
-0, 0, 3, 4, 7, 15, 31
+static const int8_t mpc7_quant_vlc_off[MPC7_QUANT_VLC_TABLES] = {
+0, 0, -3, -4, -7, -15, -31
 };
 
 static const uint8_t mpc7_quant_vlcs[177 * 2 * 2] = {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 041/114] avcodec/mpc8: Reduce size of tables used to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
By switching to ff_init_vlc_from_lengths() one can make a table of
codes of type uint8_t superfluous, saving space.

Other VLCs (for which there was no dedicated symbols table and where the
codes where already of type uint8_t) have been made to use
ff_init_vlc_from_lengths(), too, because it reduces codesize
(ff_init_vlc_from_lengths() has three parameters less than
ff_init_vlc_sparse()) and because it allows to use the offset parameter
in future commits.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc8.c | 133 --
 libavcodec/mpc8huff.h | 575 ++
 2 files changed, 240 insertions(+), 468 deletions(-)

diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index 631bac2753..339ee515b8 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -101,15 +101,6 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 static int vlc_initialized = 0;
 int channels;
 
-static VLC_TYPE band_table[542][2];
-static VLC_TYPE q1_table[520][2];
-static VLC_TYPE q9up_table[524][2];
-static VLC_TYPE scfi0_table[1 << MPC8_SCFI0_BITS][2];
-static VLC_TYPE scfi1_table[1 << MPC8_SCFI1_BITS][2];
-static VLC_TYPE dscf0_table[560][2];
-static VLC_TYPE dscf1_table[598][2];
-static VLC_TYPE q3_0_table[512][2];
-static VLC_TYPE q3_1_table[516][2];
 static VLC_TYPE codes_table[5708][2];
 
 if(avctx->extradata_size < 2){
@@ -145,91 +136,77 @@ static av_cold int mpc8_decode_init(AVCodecContext * 
avctx)
 if(vlc_initialized) return 0;
 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
 
-band_vlc.table = band_table;
-band_vlc.table_allocated = 542;
-init_vlc(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
- mpc8_bands_bits,  1, 1,
- mpc8_bands_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-
-q1_vlc.table = q1_table;
-q1_vlc.table_allocated = 520;
-init_vlc(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
- mpc8_q1_bits,  1, 1,
- mpc8_q1_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-q9up_vlc.table = q9up_table;
-q9up_vlc.table_allocated = 524;
-init_vlc(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
- mpc8_q9up_bits,  1, 1,
- mpc8_q9up_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-
-scfi_vlc[0].table = scfi0_table;
-scfi_vlc[0].table_allocated = 1 << MPC8_SCFI0_BITS;
-init_vlc(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE,
- mpc8_scfi0_bits,  1, 1,
- mpc8_scfi0_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-scfi_vlc[1].table = scfi1_table;
-scfi_vlc[1].table_allocated = 1 << MPC8_SCFI1_BITS;
-init_vlc(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE,
- mpc8_scfi1_bits,  1, 1,
- mpc8_scfi1_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-
-dscf_vlc[0].table = dscf0_table;
-dscf_vlc[0].table_allocated = 560;
-init_vlc(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE,
- mpc8_dscf0_bits,  1, 1,
- mpc8_dscf0_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-dscf_vlc[1].table = dscf1_table;
-dscf_vlc[1].table_allocated = 598;
-init_vlc(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE,
- mpc8_dscf1_bits,  1, 1,
- mpc8_dscf1_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
-
-q3_vlc[0].table = q3_0_table;
-q3_vlc[0].table_allocated = 512;
-ff_init_vlc_sparse(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
- mpc8_q3_bits,  1, 1,
- mpc8_q3_codes, 1, 1,
- mpc8_q3_syms,  1, 1, INIT_VLC_USE_NEW_STATIC);
-q3_vlc[1].table = q3_1_table;
-q3_vlc[1].table_allocated = 516;
-ff_init_vlc_sparse(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
- mpc8_q4_bits,  1, 1,
- mpc8_q4_codes, 1, 1,
- mpc8_q4_syms,  1, 1, INIT_VLC_USE_NEW_STATIC);
+INIT_VLC_STATIC_FROM_LENGTHS(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
+ mpc8_bands_bits, 1,
+ mpc8_bands_syms, 1, 1, 0, 0, 542);
+
+INIT_VLC_STATIC_FROM_LENGTHS(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
+ mpc8_q1_bits, 1,
+ mpc8_q1_syms, 1, 1, 0, 0, 520);
+INIT_VLC_STATIC_FROM_LENGTHS(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
+ mpc8_q9up_bits, 1,
+ mpc8_q9up_syms, 1, 1, 0, 0, 524);
+
+INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc[0], MPC8_SCFI0_BITS, 
MPC8_SCFI0_SIZE,
+ mpc8_scfi0_bits, 1,
+ mpc8_scfi0_syms, 1, 1, 0, 0, 1 << 
MPC8_SCFI0_BITS);
+INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc[1], MPC8_SCFI1_BITS, 
MPC8_SCFI1_SIZE,
+ mpc8_scfi1_bits, 1,
+ mpc8_scfi1_syms, 1, 1, 0, 0, 1 << 
MPC8_SCFI1_BITS);
+
+INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc[0], MPC8_DSCF0_BITS, 
MPC8_DSCF0_SIZE,
+ mpc8_dscf0_bits, 1,
+   

[FFmpeg-devel] [PATCH 043/114] avcodec/mpc8: Reduce the size of the length tables to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
After permuting both length, code as well as symbol tables so that
the codes are ordered from left to right in the tree, it became apparent
that the length of the codes decreases from left to right. Therefore one
can run-length encode the lengths to save space. This commit implements
this.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc8.c | 114 +++--
 libavcodec/mpc8huff.h | 166 --
 2 files changed, 104 insertions(+), 176 deletions(-)

diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index 31b7ad14ac..ef0fd6622a 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -90,6 +90,21 @@ static const uint16_t vlc_offsets[13] = {
 0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
 };
 
+static av_cold void build_vlc(VLC *vlc, int nb_bits,
+  const uint8_t codes_counts[16],
+  const uint8_t syms[], int offset)
+{
+uint8_t len[MPC8_MAX_VLC_SIZE];
+unsigned num = 0;
+
+for (int i = 16; i > 0; i--)
+for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
+len[num] = i;
+
+ff_init_vlc_from_lengths(vlc, nb_bits, num, len, 1,
+ syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC);
+}
+
 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 {
 int i;
@@ -97,7 +112,6 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 GetBitContext gb;
 static int vlc_initialized = 0;
 int channels;
-
 static VLC_TYPE codes_table[5708][2];
 
 if(avctx->extradata_size < 2){
@@ -133,77 +147,65 @@ static av_cold int mpc8_decode_init(AVCodecContext * 
avctx)
 if(vlc_initialized) return 0;
 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
 
-INIT_VLC_STATIC_FROM_LENGTHS(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
- mpc8_bands_bits, 1,
- mpc8_bands_syms, 1, 1, 0, 0, 542);
-
-INIT_VLC_STATIC_FROM_LENGTHS(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
- mpc8_q1_bits, 1,
- mpc8_q1_syms, 1, 1, 0, 0, 520);
-INIT_VLC_STATIC_FROM_LENGTHS(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
- mpc8_q9up_bits, 1,
- mpc8_q9up_syms, 1, 1, 0, 0, 524);
-
-INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc[0], MPC8_SCFI0_BITS, 
MPC8_SCFI0_SIZE,
- mpc8_scfi0_bits, 1,
- mpc8_scfi0_syms, 1, 1, 0, 0, 1 << 
MPC8_SCFI0_BITS);
-INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc[1], MPC8_SCFI1_BITS, 
MPC8_SCFI1_SIZE,
- mpc8_scfi1_bits, 1,
- mpc8_scfi1_syms, 1, 1, 0, 0, 1 << 
MPC8_SCFI1_BITS);
-
-INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc[0], MPC8_DSCF0_BITS, 
MPC8_DSCF0_SIZE,
- mpc8_dscf0_bits, 1,
- mpc8_dscf0_syms, 1, 1, 0, 0, 560);
-INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc[1], MPC8_DSCF1_BITS, 
MPC8_DSCF1_SIZE,
- mpc8_dscf1_bits, 1,
- mpc8_dscf1_syms, 1, 1, 0, 0, 598);
-
-INIT_VLC_STATIC_FROM_LENGTHS(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
- mpc8_q3_bits, 1,
- mpc8_q3_syms, 1, 1, MPC8_Q3_OFFSET, 0, 512);
-INIT_VLC_STATIC_FROM_LENGTHS(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
- mpc8_q4_bits, 1,
- mpc8_q4_syms, 1, 1, MPC8_Q4_OFFSET, 0, 516);
+#define INIT_VLC(vlc, bits, len_counts, symbols, offset, static_size) \
+do {   \
+static VLC_TYPE table[static_size][2]; \
+(vlc)->table   = table;\
+(vlc)->table_allocated = static_size;  \
+build_vlc(vlc, bits, len_counts, symbols, offset); \
+} while (0)
+
+
+INIT_VLC(&band_vlc, MPC8_BANDS_BITS,
+ mpc8_bands_len_counts, mpc8_bands_syms, 0, 542);
+
+INIT_VLC(&q1_vlc, MPC8_Q1_BITS,
+ mpc8_q1_len_counts, mpc8_q1_syms, 0, 520);
+INIT_VLC(&q9up_vlc, MPC8_Q9UP_BITS,
+ mpc8_q9up_len_counts, mpc8_q9up_syms, 0, 524);
+
+INIT_VLC(&scfi_vlc[0], MPC8_SCFI0_BITS,
+ mpc8_scfi_len_counts[0], mpc8_scfi0_syms, 0, 1 << 
MPC8_SCFI0_BITS);
+INIT_VLC(&scfi_vlc[1], MPC8_SCFI1_BITS,
+ mpc8_scfi_len_counts[1], mpc8_scfi1_syms, 0, 1 << 
MPC8_SCFI1_BITS);
+
+INIT_VLC(&dscf_vlc[0], MPC8_DSCF0_BITS,
+ mpc8_dscf_len_counts[0], mpc8_dscf0_syms, 0, 560);
+INIT_VLC(&dscf_vlc[1], MPC8_DSCF1_BITS,
+ mpc8_dscf_len_counts[1], mpc8_dscf1_syms, 0, 598);
+
+INIT_VLC(&q3_vlc[0], MPC8_Q3_BITS,
+ mpc8_

[FFmpeg-devel] [PATCH 049/114] avcodec/qdm2: Make tables used to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
After permuting the codes, symbols and lengths tables used to initialize
the VLCs so that the codes are ordered from left to right in the Huffman
tree, the codes become redundant as they can be easily computed from the
lengths at runtime (or at compile time with --enable-hardcoded-tables);
in this case one has to use explicit symbol tables, but all the symbols
used here fit into an uint8_t, whereas some codes needed uint16_t.
Furthermore, the codes had holes because the range of the symbols was not
contiguous; these have also been removed.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/qdm2_tablegen.h | 102 -
 libavcodec/qdm2data.h  | 226 ++---
 2 files changed, 133 insertions(+), 195 deletions(-)

diff --git a/libavcodec/qdm2_tablegen.h b/libavcodec/qdm2_tablegen.h
index 2331ebfbb2..309bb4b53c 100644
--- a/libavcodec/qdm2_tablegen.h
+++ b/libavcodec/qdm2_tablegen.h
@@ -119,140 +119,140 @@ static av_cold void qdm2_init_vlc(void)
 {
 vlc_tab_level.table   = &qdm2_table[qdm2_vlc_offs[0]];
 vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
-init_vlc(&vlc_tab_level, 8, 24,
- vlc_tab_level_huffbits, 1, 1,
- vlc_tab_level_huffcodes, 2, 2,
+ff_init_vlc_from_lengths(&vlc_tab_level, 8, 24,
+ &tab_level[0][1], 2,
+ &tab_level[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 vlc_tab_diff.table   = &qdm2_table[qdm2_vlc_offs[1]];
 vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
-init_vlc(&vlc_tab_diff, 8, 37,
- vlc_tab_diff_huffbits, 1, 1,
- vlc_tab_diff_huffcodes, 2, 2,
+ff_init_vlc_from_lengths(&vlc_tab_diff, 8, 33,
+ &tab_diff[0][1], 2,
+ &tab_diff[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 vlc_tab_run.table   = &qdm2_table[qdm2_vlc_offs[2]];
 vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
-init_vlc(&vlc_tab_run, 5, 6,
- vlc_tab_run_huffbits, 1, 1,
- vlc_tab_run_huffcodes, 1, 1,
+ff_init_vlc_from_lengths(&vlc_tab_run, 5, 6,
+ &tab_run[0][1], 2,
+ &tab_run[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 fft_level_exp_alt_vlc.table   = &qdm2_table[qdm2_vlc_offs[3]];
 fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] -
 qdm2_vlc_offs[3];
-init_vlc(&fft_level_exp_alt_vlc, 8, 28,
- fft_level_exp_alt_huffbits, 1, 1,
- fft_level_exp_alt_huffcodes, 2, 2,
+ff_init_vlc_from_lengths(&fft_level_exp_alt_vlc, 8, 28,
+ &fft_level_exp_alt[0][1], 2,
+ &fft_level_exp_alt[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 fft_level_exp_vlc.table   = &qdm2_table[qdm2_vlc_offs[4]];
 fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
-init_vlc(&fft_level_exp_vlc, 8, 20,
- fft_level_exp_huffbits, 1, 1,
- fft_level_exp_huffcodes, 2, 2,
+ff_init_vlc_from_lengths(&fft_level_exp_vlc, 8, 20,
+ &fft_level_exp[0][1], 2,
+ &fft_level_exp[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 fft_stereo_exp_vlc.table   = &qdm2_table[qdm2_vlc_offs[5]];
 fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] -
  qdm2_vlc_offs[5];
-init_vlc(&fft_stereo_exp_vlc, 6, 7,
- fft_stereo_exp_huffbits, 1, 1,
- fft_stereo_exp_huffcodes, 1, 1,
+ff_init_vlc_from_lengths(&fft_stereo_exp_vlc, 6, 7,
+ &fft_stereo_exp[0][1], 2,
+ &fft_stereo_exp[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 fft_stereo_phase_vlc.table   = &qdm2_table[qdm2_vlc_offs[6]];
 fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] -
qdm2_vlc_offs[6];
-init_vlc(&fft_stereo_phase_vlc, 6, 9,
- fft_stereo_phase_huffbits, 1, 1,
- fft_stereo_phase_huffcodes, 1, 1,
+ff_init_vlc_from_lengths(&fft_stereo_phase_vlc, 6, 9,
+ &fft_stereo_phase[0][1], 2,
+ &fft_stereo_phase[0][0], 2, 1, 0,
  INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
 
 vlc_tab_tone_level_idx_hi1.table =
 &qdm2_table[qdm2_vlc_offs[7]];
 vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] -
  qdm2_vlc_offs[7];
-init_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20,
- vlc_tab_tone_level_idx_hi1_huffbits, 1, 1,
- vl

[FFmpeg-devel] [PATCH 050/114] avcodec/qdm2_tablegen: Avoid code duplication when creating VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/qdm2_tablegen.h | 159 +++--
 1 file changed, 27 insertions(+), 132 deletions(-)

diff --git a/libavcodec/qdm2_tablegen.h b/libavcodec/qdm2_tablegen.h
index 309bb4b53c..0e57902fc9 100644
--- a/libavcodec/qdm2_tablegen.h
+++ b/libavcodec/qdm2_tablegen.h
@@ -115,145 +115,40 @@ static const uint16_t qdm2_vlc_offs[] = {
 
 static VLC_TYPE qdm2_table[3838][2];
 
-static av_cold void qdm2_init_vlc(void)
+static av_cold void build_vlc(VLC *vlc, int nb_bits, int nb_codes, int idx,
+  const uint8_t tab[][2])
 {
-vlc_tab_level.table   = &qdm2_table[qdm2_vlc_offs[0]];
-vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
-ff_init_vlc_from_lengths(&vlc_tab_level, 8, 24,
- &tab_level[0][1], 2,
- &tab_level[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-vlc_tab_diff.table   = &qdm2_table[qdm2_vlc_offs[1]];
-vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
-ff_init_vlc_from_lengths(&vlc_tab_diff, 8, 33,
- &tab_diff[0][1], 2,
- &tab_diff[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-vlc_tab_run.table   = &qdm2_table[qdm2_vlc_offs[2]];
-vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
-ff_init_vlc_from_lengths(&vlc_tab_run, 5, 6,
- &tab_run[0][1], 2,
- &tab_run[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-fft_level_exp_alt_vlc.table   = &qdm2_table[qdm2_vlc_offs[3]];
-fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] -
-qdm2_vlc_offs[3];
-ff_init_vlc_from_lengths(&fft_level_exp_alt_vlc, 8, 28,
- &fft_level_exp_alt[0][1], 2,
- &fft_level_exp_alt[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-fft_level_exp_vlc.table   = &qdm2_table[qdm2_vlc_offs[4]];
-fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
-ff_init_vlc_from_lengths(&fft_level_exp_vlc, 8, 20,
- &fft_level_exp[0][1], 2,
- &fft_level_exp[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-fft_stereo_exp_vlc.table   = &qdm2_table[qdm2_vlc_offs[5]];
-fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] -
- qdm2_vlc_offs[5];
-ff_init_vlc_from_lengths(&fft_stereo_exp_vlc, 6, 7,
- &fft_stereo_exp[0][1], 2,
- &fft_stereo_exp[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-fft_stereo_phase_vlc.table   = &qdm2_table[qdm2_vlc_offs[6]];
-fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] -
-   qdm2_vlc_offs[6];
-ff_init_vlc_from_lengths(&fft_stereo_phase_vlc, 6, 9,
- &fft_stereo_phase[0][1], 2,
- &fft_stereo_phase[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-vlc_tab_tone_level_idx_hi1.table =
-&qdm2_table[qdm2_vlc_offs[7]];
-vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] -
- qdm2_vlc_offs[7];
-ff_init_vlc_from_lengths(&vlc_tab_tone_level_idx_hi1, 8, 20,
- &tab_tone_level_idx_hi1[0][1], 2,
- &tab_tone_level_idx_hi1[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-vlc_tab_tone_level_idx_mid.table =
-&qdm2_table[qdm2_vlc_offs[8]];
-vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] -
- qdm2_vlc_offs[8];
-ff_init_vlc_from_lengths(&vlc_tab_tone_level_idx_mid, 8, 13,
- &tab_tone_level_idx_mid[0][1], 2,
- &tab_tone_level_idx_mid[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-vlc_tab_tone_level_idx_hi2.table =
-&qdm2_table[qdm2_vlc_offs[9]];
-vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] -
- qdm2_vlc_offs[9];
-ff_init_vlc_from_lengths(&vlc_tab_tone_level_idx_hi2, 8, 18,
- &tab_tone_level_idx_hi2[0][1], 2,
- &tab_tone_level_idx_hi2[0][0], 2, 1, 0,
- INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-
-vlc_tab_type30.table   = &qdm2_table[qdm2_vlc_offs[10]];
-vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
-ff_init_vlc_from_lengths(&vlc_tab_type30, 6, 9,
-  

[FFmpeg-devel] [PATCH 048/114] avcodec/mobiclip: Reindentation

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mobiclip.c | 132 +-
 1 file changed, 66 insertions(+), 66 deletions(-)

diff --git a/libavcodec/mobiclip.c b/libavcodec/mobiclip.c
index e4a2594954..2d3225f464 100644
--- a/libavcodec/mobiclip.c
+++ b/libavcodec/mobiclip.c
@@ -160,80 +160,80 @@ static const uint8_t mv_len[16] =
 static const uint8_t mv_bits[2][16][10] =
 {
 {
-{ 2, 3, 3, 5, 5, 4, 4, 5, 5, 2 },
-{ 2, 3, 4, 4, 3, 4, 4, 2 },
-{ 3, 4, 4, 2, 4, 4, 3, 2 },
-{ 1, 3, 4, 5, 5, 3, 3 },
-{ 2, 4, 4, 3, 3, 4, 4, 2 },
-{ 2, 3, 4, 4, 4, 4, 3, 2 },
-{ 2, 3, 4, 4, 4, 4, 3, 2 },
-{ 2, 2, 3, 4, 5, 5, 2 },
-{ 2, 3, 4, 4, 3, 4, 4, 2 },
-{ 2, 4, 4, 3, 4, 4, 3, 2 },
-{ 2, 3, 3, 5, 5, 4, 3, 2 },
-{ 2, 3, 4, 4, 3, 3, 2 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 2, 3, 4, 4, 3, 3, 2 },
-{ 2, 3, 4, 4, 3, 3, 2 },
-{ 3, 3, 2, 2, 3, 3 },
+{ 2, 3, 3, 5, 5, 4, 4, 5, 5, 2 },
+{ 2, 3, 4, 4, 3, 4, 4, 2 },
+{ 3, 4, 4, 2, 4, 4, 3, 2 },
+{ 1, 3, 4, 5, 5, 3, 3 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
+{ 2, 3, 4, 4, 4, 4, 3, 2 },
+{ 2, 3, 4, 4, 4, 4, 3, 2 },
+{ 2, 2, 3, 4, 5, 5, 2 },
+{ 2, 3, 4, 4, 3, 4, 4, 2 },
+{ 2, 4, 4, 3, 4, 4, 3, 2 },
+{ 2, 3, 3, 5, 5, 4, 3, 2 },
+{ 2, 3, 4, 4, 3, 3, 2 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 3, 4, 4, 3, 3, 2 },
+{ 2, 3, 4, 4, 3, 3, 2 },
+{ 3, 3, 2, 2, 3, 3 },
 },
 {
-{ 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
-{ 2, 3, 4, 5, 5, 2, 3, 3 },
-{ 2, 4, 4, 3, 3, 4, 4, 2 },
-{ 1, 4, 4, 3, 4, 4, 3 },
-{ 3, 3, 2, 4, 5, 5, 3, 2 },
-{ 3, 4, 4, 3, 3, 3, 3, 2 },
-{ 1, 3, 3, 4, 4, 4, 5, 5 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 2, 4, 4, 3, 3, 4, 4, 2 },
-{ 1, 3, 3, 4, 4, 4, 5, 5 },
-{ 2, 3, 4, 4, 4, 4, 3, 2 },
-{ 2, 3, 3, 4, 4, 3, 2 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 2, 3, 3, 4, 4, 3, 2 },
-{ 2, 3, 3, 3, 3, 2 },
+{ 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
+{ 2, 3, 4, 5, 5, 2, 3, 3 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
+{ 1, 4, 4, 3, 4, 4, 3 },
+{ 3, 3, 2, 4, 5, 5, 3, 2 },
+{ 3, 4, 4, 3, 3, 3, 3, 2 },
+{ 1, 3, 3, 4, 4, 4, 5, 5 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
+{ 1, 3, 3, 4, 4, 4, 5, 5 },
+{ 2, 3, 4, 4, 4, 4, 3, 2 },
+{ 2, 3, 3, 4, 4, 3, 2 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 3, 3, 4, 4, 3, 2 },
+{ 2, 3, 3, 3, 3, 2 },
 }
 };
 
 static const uint8_t mv_syms[2][16][10] =
 {
 {
-{ 1, 8, 9, 4, 3, 2, 7, 5, 6, 0 },
-{ 0, 9, 5, 4, 2, 3, 8, 1 },
-{ 3, 9, 5, 0, 4, 8, 2, 1 },
-{ 1, 3, 4, 8, 5, 2, 0 },
-{ 0, 5, 4, 8, 2, 3, 9, 1 },
-{ 0, 3, 5, 9, 4, 8, 2, 1 },
-{ 0, 3, 9, 5, 8, 4, 2, 1 },
-{ 0, 2, 3, 4, 8, 5, 1 },
-{ 0, 3, 8, 4, 2, 5, 9, 1 },
-{ 2, 8, 9, 3, 5, 4, 0, 1 },
-{ 0, 4, 3, 8, 9, 5, 2, 1 },
-{ 0, 4, 8, 5, 3, 2, 1 },
-{ 1, 9, 4, 2, 0, 5, 3 },
-{ 2, 4, 9, 5, 3, 0, 1 },
-{ 0, 4, 9, 5, 3, 2, 1 },
-{ 5, 4, 1, 0, 3, 2 },
+{ 1, 8, 9, 4, 3, 2, 7, 5, 6, 0 },
+{ 0, 9, 5, 4, 2, 3, 8, 1 },
+{ 3, 9, 5, 0, 4, 8, 2, 1 },
+{ 1, 3, 4, 8, 5, 2, 0 },
+{ 0, 5, 4, 8, 2, 3, 9, 1 },
+{ 0, 3, 5, 9, 4, 8, 2, 1 },
+{ 0, 3, 9, 5, 8, 4, 2, 1 },
+{ 0, 2, 3, 4, 8, 5, 1 },
+{ 0, 3, 8, 4, 2, 5, 9, 1 },
+{ 2, 8, 9, 3, 5, 4, 0, 1 },
+{ 0, 4, 3, 8, 9, 5, 2, 1 },
+{ 0, 4, 8, 5, 3, 2, 1 },
+{ 1, 9, 4, 2, 0, 5, 3 },
+{ 2, 4, 9, 5, 3, 0, 1 },
+{ 0, 4, 9, 5, 3, 2, 1 },
+{ 5, 4, 1, 0, 3, 2 },
 },
 {
-{ 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
-{ 9, 2, 3, 5, 4, 1, 8, 0 },
-{ 0, 5, 4, 2, 9, 3, 8, 1 },
-{ 1, 5, 4, 2, 8, 3, 0 },
-{ 2, 9, 8, 3, 5, 4, 0, 1 },
-{ 3, 5, 4, 2, 9, 8, 0, 1 },
-{ 1, 2, 0, 9, 8, 3, 5, 4 },
-{ 1, 8, 5, 2, 0, 4, 3 },
-{ 0, 5, 4, 2, 8, 3, 9, 1 },
-{ 1, 2, 0, 9, 8, 3, 5, 4 },
-{ 0, 3, 9, 8, 5, 4, 2, 1 },
-{ 0, 4, 3, 8, 5, 2, 1 },
-{ 1, 5, 4, 2, 0, 9, 3 },
-{ 1, 9, 5, 2, 0, 4, 3 },
-{ 0, 5, 3, 9, 4, 2, 1 },
-{ 0, 4, 5, 3, 2, 1 },
+{ 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
+{ 9, 2, 3, 5, 4, 1, 8, 0 },
+{ 0, 5, 4, 2, 9, 3, 8, 1 },
+{ 1, 5, 4, 2, 8, 3, 0 },
+{ 2, 9, 8, 3, 5, 4, 0, 1 },
+{ 3, 5, 4, 2, 9, 8, 0, 1 },
+{ 1, 2, 0, 9, 8, 3, 5, 4 },
+{ 1, 8, 5, 2, 0, 4, 3 },
+{ 0, 5, 4, 2, 8, 3, 9, 1 },
+{ 1, 2, 0, 9, 8, 3, 5, 4 },
+{ 0, 3, 9, 8, 5, 4, 2, 1 },
+{ 0, 4, 3, 8, 5, 2, 1 },
+{ 1, 5, 4, 2, 0, 9, 3 },
+{ 1, 9, 5, 2, 0, 4, 3 },
+{ 0, 5, 3, 9, 4, 2, 1 },
+{ 0, 4, 5, 3, 2, 1 },
 }
 };
 
@@ -300,11 +300,11 @@ static av_cold int mobiclip_init(AVCodecContext *avctx)
 
 INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[0], MOBI_

[FFmpeg-devel] [PATCH 044/114] avcodec/mpc8: Reduce the size of some VLCs

2020-11-10 Thread Andreas Rheinhardt
Several of the quantisation VLCs come in pairs and up until now the
number of bits used for each VLC was set to the same value for both VLCs
in such a pair even when one of the two required only a lower number.
This is a waste given that the get_vlc2() call is compatible with these
two VLCs using a different number of bits (it uses vlc->bits).

Given that the code lengths are descending it is easily possible to know
the length of the longest code for a given VLC: It is the length of the
first one. With this information one can easily use the least amount of
bits.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc8.c | 22 +++---
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index ef0fd6622a..4863ec0953 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -87,10 +87,10 @@ static int mpc8_get_mask(GetBitContext *gb, int size, int t)
 }
 
 static const uint16_t vlc_offsets[13] = {
-0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
+0, 640, 1184, 1748, 2298, 2426, 2490, 3002, 3258, 3786, 4298, 4876, 5388
 };
 
-static av_cold void build_vlc(VLC *vlc, int nb_bits,
+static av_cold void build_vlc(VLC *vlc,
   const uint8_t codes_counts[16],
   const uint8_t syms[], int offset)
 {
@@ -101,7 +101,7 @@ static av_cold void build_vlc(VLC *vlc, int nb_bits,
 for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
 len[num] = i;
 
-ff_init_vlc_from_lengths(vlc, nb_bits, num, len, 1,
+ff_init_vlc_from_lengths(vlc, FFMIN(len[0], 9), num, len, 1,
  syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC);
 }
 
@@ -112,7 +112,7 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 GetBitContext gb;
 static int vlc_initialized = 0;
 int channels;
-static VLC_TYPE codes_table[5708][2];
+static VLC_TYPE codes_table[5388][2];
 
 if(avctx->extradata_size < 2){
 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", 
avctx->extradata_size);
@@ -152,7 +152,7 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 static VLC_TYPE table[static_size][2]; \
 (vlc)->table   = table;\
 (vlc)->table_allocated = static_size;  \
-build_vlc(vlc, bits, len_counts, symbols, offset); \
+build_vlc(vlc, len_counts, symbols, offset);   \
 } while (0)
 
 
@@ -182,29 +182,29 @@ static av_cold int mpc8_decode_init(AVCodecContext * 
avctx)
 for(i = 0; i < 2; i++){
 res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
 res_vlc[i].table_allocated = vlc_offsets[1+i] - vlc_offsets[0+i];
-build_vlc(&res_vlc[i], MPC8_RES_BITS,
+build_vlc(&res_vlc[i],
   mpc8_res_len_counts[i], mpc8_res_syms[i], 0);
 
 q2_vlc[i].table = &codes_table[vlc_offsets[2+i]];
 q2_vlc[i].table_allocated = vlc_offsets[3+i] - vlc_offsets[2+i];
-build_vlc(&q2_vlc[i], MPC8_Q2_BITS,
+build_vlc(&q2_vlc[i],
   mpc8_q2_len_counts[i], mpc8_q2_syms[i], 0);
 
 quant_vlc[0][i].table = &codes_table[vlc_offsets[4+i]];
 quant_vlc[0][i].table_allocated = vlc_offsets[5+i] - vlc_offsets[4+i];
-build_vlc(&quant_vlc[0][i], MPC8_Q5_BITS,
+build_vlc(&quant_vlc[0][i],
   mpc8_q5_len_counts[i], mpc8_q5_syms[i], MPC8_Q5_OFFSET);
 quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
 quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
-build_vlc(&quant_vlc[1][i], MPC8_Q6_BITS,
+build_vlc(&quant_vlc[1][i],
   mpc8_q6_len_counts[i], mpc8_q6_syms[i], MPC8_Q6_OFFSET);
 quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
 quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
-build_vlc(&quant_vlc[2][i], MPC8_Q7_BITS,
+build_vlc(&quant_vlc[2][i],
   mpc8_q7_len_counts[i], mpc8_q7_syms[i], MPC8_Q7_OFFSET);
 quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
 quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - 
vlc_offsets[10+i];
-build_vlc(&quant_vlc[3][i], MPC8_Q8_BITS,
+build_vlc(&quant_vlc[3][i],
   mpc8_q8_len_counts[i], mpc8_q8_syms[i], MPC8_Q8_OFFSET);
 }
 vlc_initialized = 1;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 045/114] avcodec/mpc8: Avoid code duplication when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Up until now, VLCs that were part of an array of VLCs were often not
initialized in a loop, but separately. The probable reason for this
was that these VLCs differed slightly in the parameters to be used for
them (i.e. the number of codes or the number of bits to be used
differs), so that one would have to provide these parameters e.g. via
arrays.

Yet these problems have actually largely been solved by now: The length
information is contained in a run-length encoded form that is the same
for all VLCs and both the number of codes as well as the number of bits
to use for each VLC can be easily derived from them.

There is just one problem to be solved: When the underlying tables have
a different number of elements, putting them into an array of arrays
would be wasteful; using an array of pointers to the arrays would
also be wasteful. Therefore this commit combines the tables into bigger
tables. (Given that all the length tables have the same layout this
applies only to the symbols tables.)

Finally, the array containing the offset of the VLC's buffer in the big
buffer has also been removed.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpc8.c |  90 +--
 libavcodec/mpc8huff.h | 257 +++---
 2 files changed, 121 insertions(+), 226 deletions(-)

diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index 4863ec0953..e945d3d200 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -86,33 +86,38 @@ static int mpc8_get_mask(GetBitContext *gb, int size, int t)
 return mask;
 }
 
-static const uint16_t vlc_offsets[13] = {
-0, 640, 1184, 1748, 2298, 2426, 2490, 3002, 3258, 3786, 4298, 4876, 5388
-};
-
-static av_cold void build_vlc(VLC *vlc,
+static av_cold void build_vlc(VLC *vlc, unsigned *buf_offset,
   const uint8_t codes_counts[16],
-  const uint8_t syms[], int offset)
+  const uint8_t **syms, int offset)
 {
+static VLC_TYPE vlc_buf[9296][2];
 uint8_t len[MPC8_MAX_VLC_SIZE];
+const uint8_t *old_syms = *syms;
 unsigned num = 0;
 
+vlc->table   = &vlc_buf[*buf_offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
+
 for (int i = 16; i > 0; i--)
 for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
 len[num] = i;
 
+*syms += num;
 ff_init_vlc_from_lengths(vlc, FFMIN(len[0], 9), num, len, 1,
- syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC);
+ old_syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC);
+*buf_offset += vlc->table_size;
 }
 
 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 {
-int i;
+int i, offset = 0;
 MPCContext *c = avctx->priv_data;
 GetBitContext gb;
 static int vlc_initialized = 0;
+const uint8_t *q_syms   = mpc8_q_syms,  *bands_syms = mpc8_bands_syms;
+const uint8_t *res_syms = mpc8_res_syms, *scfi_syms = mpc8_scfi_syms;
+const uint8_t *dscf_syms = mpc8_dscf_syms;
 int channels;
-static VLC_TYPE codes_table[5388][2];
 
 if(avctx->extradata_size < 2){
 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", 
avctx->extradata_size);
@@ -147,65 +152,24 @@ static av_cold int mpc8_decode_init(AVCodecContext * 
avctx)
 if(vlc_initialized) return 0;
 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
 
-#define INIT_VLC(vlc, bits, len_counts, symbols, offset, static_size) \
-do {   \
-static VLC_TYPE table[static_size][2]; \
-(vlc)->table   = table;\
-(vlc)->table_allocated = static_size;  \
-build_vlc(vlc, len_counts, symbols, offset);   \
-} while (0)
-
+build_vlc(&band_vlc, &offset, mpc8_bands_len_counts, &bands_syms, 0);
 
-INIT_VLC(&band_vlc, MPC8_BANDS_BITS,
- mpc8_bands_len_counts, mpc8_bands_syms, 0, 542);
+build_vlc(&q1_vlc,   &offset, mpc8_q1_len_counts,   &q_syms, 0);
+build_vlc(&q9up_vlc, &offset, mpc8_q9up_len_counts, &q_syms, 0);
 
-INIT_VLC(&q1_vlc, MPC8_Q1_BITS,
- mpc8_q1_len_counts, mpc8_q1_syms, 0, 520);
-INIT_VLC(&q9up_vlc, MPC8_Q9UP_BITS,
- mpc8_q9up_len_counts, mpc8_q9up_syms, 0, 524);
-
-INIT_VLC(&scfi_vlc[0], MPC8_SCFI0_BITS,
- mpc8_scfi_len_counts[0], mpc8_scfi0_syms, 0, 1 << 
MPC8_SCFI0_BITS);
-INIT_VLC(&scfi_vlc[1], MPC8_SCFI1_BITS,
- mpc8_scfi_len_counts[1], mpc8_scfi1_syms, 0, 1 << 
MPC8_SCFI1_BITS);
+for(i = 0; i < 2; i++){
+build_vlc(&scfi_vlc[i], &offset, mpc8_scfi_len_counts[i], &scfi_syms, 
0);
 
-INIT_VLC(&dscf_vlc[0], MPC8_DSCF0_BITS,
- mpc8_dscf_len_counts[0], mpc8_dscf0_syms, 0, 560);
-INIT_VLC(&dscf_vlc[1], MPC8_DSCF1_BITS,
- mpc8_dscf_len_counts[1],

[FFmpeg-devel] [PATCH 052/114] avcodec/qdm2: Initialize array of VLCs in a loop

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/qdm2_tablegen.h | 12 +++-
 libavcodec/qdm2data.h  | 28 ++--
 2 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/libavcodec/qdm2_tablegen.h b/libavcodec/qdm2_tablegen.h
index aaa7356caf..b60989e343 100644
--- a/libavcodec/qdm2_tablegen.h
+++ b/libavcodec/qdm2_tablegen.h
@@ -127,6 +127,8 @@ static av_cold void build_vlc(VLC *vlc, int nb_bits, int 
nb_codes, int idx,
 
 static av_cold void qdm2_init_vlc(void)
 {
+const uint8_t (*tab)[2] = tab_fft_tone_offset;
+
 build_vlc(&vlc_tab_level, 8, 24, 0, tab_level);
 build_vlc(&vlc_tab_diff,  8, 33, 1, tab_diff);
 build_vlc(&vlc_tab_run,   5,  6, 2, tab_run);
@@ -144,11 +146,11 @@ static av_cold void qdm2_init_vlc(void)
 build_vlc(&vlc_tab_type30, 6,  9, 10, tab_type30);
 build_vlc(&vlc_tab_type34, 5, 10, 11, tab_type34);
 
-build_vlc(&vlc_tab_fft_tone_offset[0], 8, 23, 12, tab_fft_tone_offset_0);
-build_vlc(&vlc_tab_fft_tone_offset[1], 8, 28, 13, tab_fft_tone_offset_1);
-build_vlc(&vlc_tab_fft_tone_offset[2], 8, 31, 14, tab_fft_tone_offset_2);
-build_vlc(&vlc_tab_fft_tone_offset[3], 8, 34, 15, tab_fft_tone_offset_3);
-build_vlc(&vlc_tab_fft_tone_offset[4], 8, 37, 16, tab_fft_tone_offset_4);
+for (int i = 0; i < 5; i++) {
+build_vlc(&vlc_tab_fft_tone_offset[i], 8, tab_fft_tone_offset_sizes[i],
+  12 + i, tab);
+tab += tab_fft_tone_offset_sizes[i];
+}
 }
 
 #endif /* CONFIG_HARDCODED_TABLES */
diff --git a/libavcodec/qdm2data.h b/libavcodec/qdm2data.h
index 69f66b4571..713f584269 100644
--- a/libavcodec/qdm2data.h
+++ b/libavcodec/qdm2data.h
@@ -91,45 +91,37 @@ static const uint8_t tab_type34[10][2] = {
 { 4, 3 }, { 6, 3 }, { 5, 3 },
 };
 
-/* values in this table range from -1..22; adjust retrieved value by -1 */
-static const uint8_t tab_fft_tone_offset_0[23][2] = {
+static const uint8_t tab_fft_tone_offset_sizes[] = {
+23, 28, 31, 34, 37
+};
+
+static const uint8_t tab_fft_tone_offset[][2] = {
+/* First table - 23 entries with range -1..21 */
 {  2,  2 }, {  7,  7 }, { 15,  8 }, { 21,  8 }, {  3,  6 }, {  6,  6 },
 { 13,  7 }, { 14,  8 }, { 18,  8 }, {  4,  4 }, {  5,  5 }, { 11,  7 },
 { 10,  7 }, { 20,  6 }, { 12,  8 }, { 16,  9 }, { 22, 10 }, {  0, 10 },
 { 17,  7 }, { 19,  6 }, {  8,  6 }, {  9,  6 }, {  1,  1 },
-};
-
-/* values in this table range from -1..27; adjust retrieved value by -1 */
-static const uint8_t tab_fft_tone_offset_1[28][2] = {
+/* Second table - 28 entries with range -1..26 */
 {  8,  6 }, {  2,  6 }, {  7,  6 }, { 23,  7 }, { 12,  7 }, {  5,  4 },
 { 10,  6 }, { 20,  8 }, { 25,  9 }, { 26, 10 }, { 27, 11 }, {  0, 11 },
 { 22,  7 }, {  9,  5 }, { 13,  6 }, { 17,  6 }, {  4,  5 }, { 14,  6 },
 { 19,  7 }, { 24,  7 }, {  3,  6 }, { 11,  6 }, { 21,  6 }, { 18,  6 },
 { 16,  6 }, { 15,  6 }, {  6,  3 }, {  1,  1 },
-};
-
-/* values in this table range from -1..31; adjust retrieved value by -1 */
-static const uint8_t tab_fft_tone_offset_2[31][2] = {
+/* Third table - 31 entries with range -1..30  (1 omitted) */
 { 14,  7 }, { 17,  7 }, { 15,  7 }, { 23,  9 }, { 28, 10 }, { 29, 11 },
 { 30, 13 }, {  0, 13 }, { 31, 12 }, { 25,  8 }, { 10,  5 }, {  8,  4 },
 {  9,  4 }, {  4,  4 }, { 22,  8 }, {  3,  8 }, { 21,  8 }, { 26,  9 },
 { 27,  9 }, { 12,  6 }, { 11,  5 }, { 16,  7 }, { 18,  7 }, { 20,  8 },
 { 24,  8 }, { 19,  7 }, { 13,  5 }, {  5,  3 }, {  1,  2 }, {  6,  3 },
 {  7,  3 },
-};
-
-/* values in this table range from -1..34; adjust retrieved value by -1 */
-static const uint8_t tab_fft_tone_offset_3[34][2] = {
+/* Fourth table - 34 entries with range -1..33 (1 omitted) */
 {  4,  4 }, {  7,  4 }, { 10,  4 }, {  3, 10 }, { 27, 10 }, { 29, 10 },
 { 28, 10 }, { 22,  8 }, { 21,  7 }, { 15,  6 }, { 14,  5 }, {  8,  4 },
 { 16,  6 }, { 19,  7 }, { 23,  8 }, { 26,  9 }, { 30, 10 }, { 33, 13 },
 { 34, 14 }, {  0, 14 }, { 32, 12 }, { 31, 11 }, { 12,  5 }, {  5,  3 },
 {  9,  3 }, {  1,  4 }, { 20,  7 }, { 25,  8 }, { 24,  8 }, { 18,  6 },
 { 17,  5 }, {  6,  3 }, { 11,  4 }, { 13,  4 },
-};
-
-/* values in this table range from -1..37; adjust retrieved value by -1 */
-static const uint8_t tab_fft_tone_offset_4[37][2] = {
+/* Fifth table - 37 entries with range -1..36 (1 omitted) */
 {  5,  3 }, {  4,  3 }, { 19,  8 }, { 33, 12 }, { 31, 12 }, { 28, 11 },
 { 34, 14 }, { 37, 14 }, { 35, 15 }, {  0, 15 }, { 36, 14 }, { 32, 12 },
 { 30, 11 }, { 24,  9 }, { 22,  8 }, { 23,  9 }, { 29, 10 }, { 27, 10 },
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 046/114] avcodec/mobiclip: Avoid redundant codes table to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
If both codes, lengths and symbols tables are ordered so that the codes
are sorted from left to right in the tree, the codes can be easily
derived from the lengths and therefore become redundant. This is
exploited in this commit to remove the codes tables for the mobiclip
decoder; notice that tables for the run-length VLC were already ordered
correctly.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mobiclip.c | 203 +++---
 1 file changed, 74 insertions(+), 129 deletions(-)

diff --git a/libavcodec/mobiclip.c b/libavcodec/mobiclip.c
index 42d33cf6a5..cb17832898 100644
--- a/libavcodec/mobiclip.c
+++ b/libavcodec/mobiclip.c
@@ -128,19 +128,6 @@ static const uint8_t bits0[] = {
  6,  6,  6,  6,  6,  6,  5,  5,  5,  4,  2,  3,  4,  4,
 };
 
-static const uint8_t codes0[] = {
-0x0, 0x4, 0x5, 0x6, 0x7, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
-0xB, 0xC, 0xD, 0xE, 0xF, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
-0x26, 0x27, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
-0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x3, 0x20,
-0x21, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
-0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
-0x24, 0x25, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
-0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x10, 0x11, 0x12, 0x13, 0x14,
-0x15, 0x16, 0x17, 0xC, 0xD, 0xE, 0xF, 0x10, 0x11, 0x12,
-0x13, 0x14, 0x15, 0xB, 0xC, 0xD, 0x7, 0x2, 0x6, 0xE, 0xF,
-};
-
 static const uint16_t syms0[] = {
 0x0, 0x822, 0x803, 0xB, 0xA, 0xB81, 0xB61, 0xB41, 0xB21, 0x122,
 0x102, 0xE2, 0xC2, 0xA2, 0x63, 0x43, 0x24, 0xC, 0x25, 0x2E1, 0x301,
@@ -172,122 +159,82 @@ static const uint8_t mv_len[16] =
 
 static const uint8_t mv_bits[16][10] =
 {
-{ 1, 3, 3, 4, 4, 5, 5, 5, 6, 6 },
-{ 2, 2, 3, 3, 3, 4, 5, 5 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 1, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 3, 4, 5, 5 },
-{ 2, 3, 3, 3, 3, 3, 4, 4 },
+{ 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
+{ 2, 3, 4, 5, 5, 2, 3, 3 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
+{ 1, 4, 4, 3, 4, 4, 3 },
+{ 3, 3, 2, 4, 5, 5, 3, 2 },
+{ 3, 4, 4, 3, 3, 3, 3, 2 },
 { 1, 3, 3, 4, 4, 4, 5, 5 },
-{ 1, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
 { 1, 3, 3, 4, 4, 4, 5, 5 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 3, 4, 4 },
-{ 1, 3, 3, 4, 4, 4, 4 },
-{ 1, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 3, 4, 4 },
-{ 2, 2, 3, 3, 3, 3 },
-};
-
-static const uint8_t mv_codes[16][10] =
-{
-{ 1, 0, 2, 2, 7, 6, 7, 12, 26, 27 },
-{ 0, 2, 2, 6, 7, 6, 14, 15 },
-{ 0, 3, 3, 4, 4, 5, 10, 11 },
-{ 0, 5, 7, 8, 9, 12, 13 },
-{ 1, 3, 0, 1, 5, 8, 18, 19 },
-{ 3, 0, 2, 3, 4, 5, 2, 3 },
-{ 0, 4, 5, 12, 13, 14, 30, 31 },
-{ 0, 5, 6, 8, 9, 14, 15 },
-{ 0, 3, 3, 4, 4, 5, 10, 11 },
-{ 0, 4, 5, 12, 13, 14, 30, 31 },
-{ 0, 3, 2, 5, 6, 7, 8, 9 },
-{ 0, 3, 2, 3, 5, 8, 9 },
-{ 0, 5, 6, 8, 9, 14, 15 },
-{ 0, 5, 6, 8, 9, 14, 15 },
-{ 0, 3, 2, 3, 5, 8, 9 },
-{ 0, 3, 2, 3, 4, 5 },
+{ 2, 3, 4, 4, 4, 4, 3, 2 },
+{ 2, 3, 3, 4, 4, 3, 2 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 3, 3, 4, 4, 3, 2 },
+{ 2, 3, 3, 3, 3, 2 },
 };
 
 static const uint8_t mv_syms[16][10] =
 {
-{ 0, 8, 1, 2, 9, 3, 6, 7, 5, 4 },
-{ 9, 1, 2, 8, 0, 3, 5, 4 },
-{ 0, 1, 2, 9, 5, 4, 3, 8 },
-{ 1, 2, 0, 5, 4, 8, 3 },
-{ 8, 1, 2, 9, 0, 3, 5, 4 },
-{ 1, 3, 2, 9, 8, 0, 5, 4 },
+{ 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
+{ 9, 2, 3, 5, 4, 1, 8, 0 },
+{ 0, 5, 4, 2, 9, 3, 8, 1 },
+{ 1, 5, 4, 2, 8, 3, 0 },
+{ 2, 9, 8, 3, 5, 4, 0, 1 },
+{ 3, 5, 4, 2, 9, 8, 0, 1 },
 { 1, 2, 0, 9, 8, 3, 5, 4 },
-{ 1, 2, 0, 8, 5, 4, 3 },
-{ 0, 1, 2, 8, 5, 4, 3, 9 },
+{ 1, 8, 5, 2, 0, 4, 3 },
+{ 0, 5, 4, 2, 8, 3, 9, 1 },
 { 1, 2, 0, 9, 8, 3, 5, 4 },
-{ 0, 1, 3, 2, 9, 8, 5, 4 },
-{ 0, 1, 4, 3, 2, 8, 5 },
-{ 1, 2, 0, 5, 4, 9, 3 },
-{ 1, 2, 0, 9, 5, 4, 3 },
-{ 0, 1, 5, 3, 2, 9, 4 },
-{ 0, 1, 4, 5, 3, 2 },
+{ 0, 3, 9, 8, 5, 4, 2, 1 },
+{ 0, 4, 3, 8, 5, 2, 1 },
+{ 1, 5, 4, 2, 0, 9, 3 },
+{ 1, 9, 5, 2, 0, 4, 3 },
+{ 0, 5, 3, 9, 4, 2, 1 },
+{ 0, 4, 5, 3, 2, 1 },
 };
 
 static const uint8_t mv_bits_mods[16][10] =
 {
-{ 2, 2, 3, 3, 4, 4, 5, 5, 5, 5 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 1, 3, 3, 3, 4, 5, 5 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 2, 3, 4, 5, 5 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 3, 4, 5, 5 },
-{ 2, 2, 3, 3, 3, 4, 4 },
-{ 1, 3, 3, 4, 4, 4, 4 },
-{ 2, 2, 3, 3, 3, 4, 4 },
-{ 2, 2, 3, 3, 3, 4, 4 },
-{ 2, 2, 3, 3, 3, 3 },
-};
-
-static const uint8_t mv_codes_mods[16][10] =
-{
-{ 0, 3, 2, 3, 9, 10, 16, 17, 22, 23 },
-{ 0, 3, 2, 4, 6, 7, 10, 11 }

[FFmpeg-devel] [PATCH 051/114] avcodec/qdm2: Apply offsets when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/qdm2.c  | 2 +-
 libavcodec/qdm2_tablegen.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c
index 657b2da64d..bdfe710840 100644
--- a/libavcodec/qdm2.c
+++ b/libavcodec/qdm2.c
@@ -205,7 +205,7 @@ static int qdm2_get_vlc(GetBitContext *gb, const VLC *vlc, 
int flag, int depth)
 value = get_vlc2(gb, vlc->table, vlc->bits, depth);
 
 /* stage-2, 3 bits exponent escape sequence */
-if (value-- == 0)
+if (value < 0)
 value = get_bits(gb, get_bits(gb, 3) + 1);
 
 /* stage-3, optional */
diff --git a/libavcodec/qdm2_tablegen.h b/libavcodec/qdm2_tablegen.h
index 0e57902fc9..aaa7356caf 100644
--- a/libavcodec/qdm2_tablegen.h
+++ b/libavcodec/qdm2_tablegen.h
@@ -122,7 +122,7 @@ static av_cold void build_vlc(VLC *vlc, int nb_bits, int 
nb_codes, int idx,
 vlc->table_allocated = qdm2_vlc_offs[idx + 1] - qdm2_vlc_offs[idx];
 ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
  &tab[0][1], 2, &tab[0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC | INIT_VLC_OUTPUT_LE);
+ -1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_OUTPUT_LE);
 }
 
 static av_cold void qdm2_init_vlc(void)
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 047/114] avcodec/mobiclip: Avoid code duplication when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mobiclip.c | 106 --
 1 file changed, 51 insertions(+), 55 deletions(-)

diff --git a/libavcodec/mobiclip.c b/libavcodec/mobiclip.c
index cb17832898..e4a2594954 100644
--- a/libavcodec/mobiclip.c
+++ b/libavcodec/mobiclip.c
@@ -157,48 +157,9 @@ static const uint8_t mv_len[16] =
 10, 8, 8, 7, 8, 8, 8, 7, 8, 8, 8, 7, 7, 7, 7, 6,
 };
 
-static const uint8_t mv_bits[16][10] =
-{
-{ 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
-{ 2, 3, 4, 5, 5, 2, 3, 3 },
-{ 2, 4, 4, 3, 3, 4, 4, 2 },
-{ 1, 4, 4, 3, 4, 4, 3 },
-{ 3, 3, 2, 4, 5, 5, 3, 2 },
-{ 3, 4, 4, 3, 3, 3, 3, 2 },
-{ 1, 3, 3, 4, 4, 4, 5, 5 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 2, 4, 4, 3, 3, 4, 4, 2 },
-{ 1, 3, 3, 4, 4, 4, 5, 5 },
-{ 2, 3, 4, 4, 4, 4, 3, 2 },
-{ 2, 3, 3, 4, 4, 3, 2 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 1, 4, 4, 3, 3, 4, 4 },
-{ 2, 3, 3, 4, 4, 3, 2 },
-{ 2, 3, 3, 3, 3, 2 },
-};
-
-static const uint8_t mv_syms[16][10] =
-{
-{ 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
-{ 9, 2, 3, 5, 4, 1, 8, 0 },
-{ 0, 5, 4, 2, 9, 3, 8, 1 },
-{ 1, 5, 4, 2, 8, 3, 0 },
-{ 2, 9, 8, 3, 5, 4, 0, 1 },
-{ 3, 5, 4, 2, 9, 8, 0, 1 },
-{ 1, 2, 0, 9, 8, 3, 5, 4 },
-{ 1, 8, 5, 2, 0, 4, 3 },
-{ 0, 5, 4, 2, 8, 3, 9, 1 },
-{ 1, 2, 0, 9, 8, 3, 5, 4 },
-{ 0, 3, 9, 8, 5, 4, 2, 1 },
-{ 0, 4, 3, 8, 5, 2, 1 },
-{ 1, 5, 4, 2, 0, 9, 3 },
-{ 1, 9, 5, 2, 0, 4, 3 },
-{ 0, 5, 3, 9, 4, 2, 1 },
-{ 0, 4, 5, 3, 2, 1 },
-};
-
-static const uint8_t mv_bits_mods[16][10] =
+static const uint8_t mv_bits[2][16][10] =
 {
+{
 { 2, 3, 3, 5, 5, 4, 4, 5, 5, 2 },
 { 2, 3, 4, 4, 3, 4, 4, 2 },
 { 3, 4, 4, 2, 4, 4, 3, 2 },
@@ -215,10 +176,30 @@ static const uint8_t mv_bits_mods[16][10] =
 { 2, 3, 4, 4, 3, 3, 2 },
 { 2, 3, 4, 4, 3, 3, 2 },
 { 3, 3, 2, 2, 3, 3 },
+},
+{
+{ 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
+{ 2, 3, 4, 5, 5, 2, 3, 3 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
+{ 1, 4, 4, 3, 4, 4, 3 },
+{ 3, 3, 2, 4, 5, 5, 3, 2 },
+{ 3, 4, 4, 3, 3, 3, 3, 2 },
+{ 1, 3, 3, 4, 4, 4, 5, 5 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 4, 4, 3, 3, 4, 4, 2 },
+{ 1, 3, 3, 4, 4, 4, 5, 5 },
+{ 2, 3, 4, 4, 4, 4, 3, 2 },
+{ 2, 3, 3, 4, 4, 3, 2 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 1, 4, 4, 3, 3, 4, 4 },
+{ 2, 3, 3, 4, 4, 3, 2 },
+{ 2, 3, 3, 3, 3, 2 },
+}
 };
 
-static const uint8_t mv_syms_mods[16][10] =
+static const uint8_t mv_syms[2][16][10] =
 {
+{
 { 1, 8, 9, 4, 3, 2, 7, 5, 6, 0 },
 { 0, 9, 5, 4, 2, 3, 8, 1 },
 { 3, 9, 5, 0, 4, 8, 2, 1 },
@@ -235,6 +216,25 @@ static const uint8_t mv_syms_mods[16][10] =
 { 2, 4, 9, 5, 3, 0, 1 },
 { 0, 4, 9, 5, 3, 2, 1 },
 { 5, 4, 1, 0, 3, 2 },
+},
+{
+{ 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
+{ 9, 2, 3, 5, 4, 1, 8, 0 },
+{ 0, 5, 4, 2, 9, 3, 8, 1 },
+{ 1, 5, 4, 2, 8, 3, 0 },
+{ 2, 9, 8, 3, 5, 4, 0, 1 },
+{ 3, 5, 4, 2, 9, 8, 0, 1 },
+{ 1, 2, 0, 9, 8, 3, 5, 4 },
+{ 1, 8, 5, 2, 0, 4, 3 },
+{ 0, 5, 4, 2, 8, 3, 9, 1 },
+{ 1, 2, 0, 9, 8, 3, 5, 4 },
+{ 0, 3, 9, 8, 5, 4, 2, 1 },
+{ 0, 4, 3, 8, 5, 2, 1 },
+{ 1, 5, 4, 2, 0, 9, 3 },
+{ 1, 9, 5, 2, 0, 4, 3 },
+{ 0, 5, 3, 9, 4, 2, 1 },
+{ 0, 4, 5, 3, 2, 1 },
+}
 };
 
 typedef struct BlockXY {
@@ -306,20 +306,16 @@ static av_cold int mobiclip_init(AVCodecContext *avctx)
  bits0, sizeof(*bits0),
syms1,  sizeof(*syms1),  sizeof(*syms1),
  0, 0, 1 << MOBI_RL_VLC_BITS);
-for (int j = 0; j < 16; j++) {
+for (int i = 0; i < 2; i++) {
 static VLC_TYPE vlc_buf[2 * 16 << MOBI_MV_VLC_BITS][2];
-mv_vlc[0][j].table   = &vlc_buf[2 * j << MOBI_MV_VLC_BITS];
-mv_vlc[0][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
-ff_init_vlc_from_lengths(&mv_vlc[0][j], MOBI_MV_VLC_BITS, mv_len[j],
- mv_bits_mods[j], sizeof(*mv_bits_mods[j]),
- mv_syms_mods[j], sizeof(*mv_syms_mods[j]),
- sizeof(*mv_syms_mods[j]), 0, 
INIT_VLC_USE_NEW_STATIC);
-mv_vlc[1][j].table   = &vlc_buf[(2 * j + 1) << 
MOBI_MV_VLC_BITS];
-mv_vlc[1][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
-ff_init_vlc_from_lengths(&mv_vlc[1][j], MOBI_MV_VLC_BITS, mv_len[j],
- mv_bits[j], sizeof(*mv_bits[j]),
- mv_syms[j], sizeof(*mv_syms[j]), 
sizeof(*mv_syms[j]),
- 0, INIT_VLC_USE_NEW_STATIC);
+for (int j = 0; j < 16; j++) {
+mv_vlc[i][j].table   = &vlc_buf[(16 * i + j) << 
MOBI_MV_VLC_BITS];
+mv_vlc[i][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
+ff_init_vlc_from_lengths(&mv_vlc[i][j], MOBI_MV_VLC_BITS, 
mv_len[j],
+ mv_bits[i][j

[FFmpeg-devel] [PATCH 054/114] avcodec/rv40vlc2: Make VLC smaller

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv40vlc2.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/rv40vlc2.h b/libavcodec/rv40vlc2.h
index 15119a145b..4cdc7a4193 100644
--- a/libavcodec/rv40vlc2.h
+++ b/libavcodec/rv40vlc2.h
@@ -33,7 +33,7 @@
  * codes used for the first four block types
  */
 //@{
-#define AIC_TOP_BITS  8
+#define AIC_TOP_BITS  7
 #define AIC_TOP_SIZE 16
 static const uint8_t rv40_aic_top_vlc_codes[AIC_TOP_SIZE] = {
  0x01, 0x05, 0x01, 0x00, 0x03, 0x3D, 0x1D, 0x02,
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 053/114] avcodec/qdm2: Avoid offsets table when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/qdm2_tablegen.h | 40 ++
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/libavcodec/qdm2_tablegen.h b/libavcodec/qdm2_tablegen.h
index b60989e343..e492f7bb60 100644
--- a/libavcodec/qdm2_tablegen.h
+++ b/libavcodec/qdm2_tablegen.h
@@ -109,46 +109,44 @@ static VLC vlc_tab_type30;
 static VLC vlc_tab_type34;
 static VLC vlc_tab_fft_tone_offset[5];
 
-static const uint16_t qdm2_vlc_offs[] = {
-
0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
-};
-
 static VLC_TYPE qdm2_table[3838][2];
 
-static av_cold void build_vlc(VLC *vlc, int nb_bits, int nb_codes, int idx,
-  const uint8_t tab[][2])
+static av_cold void build_vlc(VLC *vlc, int nb_bits, int nb_codes,
+  unsigned *offset, const uint8_t tab[][2])
 {
-vlc->table   = &qdm2_table[qdm2_vlc_offs[idx]];
-vlc->table_allocated = qdm2_vlc_offs[idx + 1] - qdm2_vlc_offs[idx];
+vlc->table   = &qdm2_table[*offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(qdm2_table) - *offset;
 ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
  &tab[0][1], 2, &tab[0][0], 2, 1,
  -1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_OUTPUT_LE);
+*offset += vlc->table_size;
 }
 
 static av_cold void qdm2_init_vlc(void)
 {
 const uint8_t (*tab)[2] = tab_fft_tone_offset;
+unsigned offset = 0;
 
-build_vlc(&vlc_tab_level, 8, 24, 0, tab_level);
-build_vlc(&vlc_tab_diff,  8, 33, 1, tab_diff);
-build_vlc(&vlc_tab_run,   5,  6, 2, tab_run);
+build_vlc(&vlc_tab_level, 8, 24, &offset, tab_level);
+build_vlc(&vlc_tab_diff,  8, 33, &offset, tab_diff);
+build_vlc(&vlc_tab_run,   5,  6, &offset, tab_run);
 
-build_vlc(&fft_level_exp_alt_vlc, 8, 28, 3, fft_level_exp_alt);
-build_vlc(&fft_level_exp_vlc, 8, 20, 4, fft_level_exp);
+build_vlc(&fft_level_exp_alt_vlc, 8, 28, &offset, fft_level_exp_alt);
+build_vlc(&fft_level_exp_vlc, 8, 20, &offset, fft_level_exp);
 
-build_vlc(&fft_stereo_exp_vlc,   6, 7, 5, fft_stereo_exp);
-build_vlc(&fft_stereo_phase_vlc, 6, 9, 6, fft_stereo_phase);
+build_vlc(&fft_stereo_exp_vlc,   6, 7, &offset, fft_stereo_exp);
+build_vlc(&fft_stereo_phase_vlc, 6, 9, &offset, fft_stereo_phase);
 
-build_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20, 7, tab_tone_level_idx_hi1);
-build_vlc(&vlc_tab_tone_level_idx_mid, 8, 13, 8, tab_tone_level_idx_mid);
-build_vlc(&vlc_tab_tone_level_idx_hi2, 8, 18, 9, tab_tone_level_idx_hi2);
+build_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20, &offset, 
tab_tone_level_idx_hi1);
+build_vlc(&vlc_tab_tone_level_idx_mid, 8, 13, &offset, 
tab_tone_level_idx_mid);
+build_vlc(&vlc_tab_tone_level_idx_hi2, 8, 18, &offset, 
tab_tone_level_idx_hi2);
 
-build_vlc(&vlc_tab_type30, 6,  9, 10, tab_type30);
-build_vlc(&vlc_tab_type34, 5, 10, 11, tab_type34);
+build_vlc(&vlc_tab_type30, 6,  9, &offset, tab_type30);
+build_vlc(&vlc_tab_type34, 5, 10, &offset, tab_type34);
 
 for (int i = 0; i < 5; i++) {
 build_vlc(&vlc_tab_fft_tone_offset[i], 8, tab_fft_tone_offset_sizes[i],
-  12 + i, tab);
+  &offset, tab);
 tab += tab_fft_tone_offset_sizes[i];
 }
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 057/114] avcodec/rv40: Avoid offset table when initializing static VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv40.c | 12 
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index 81e08af5f1..7ed599cd69 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -41,11 +41,6 @@ static VLC aic_top_vlc;
 static VLC aic_mode1_vlc[AIC_MODE1_NUM], aic_mode2_vlc[AIC_MODE2_NUM];
 static VLC ptype_vlc[NUM_PTYPE_VLCS], btype_vlc[NUM_BTYPE_VLCS];
 
-static const int16_t mode2_offs[] = {
-   0,  614, 1222, 1794, 2410,  3014,  3586,  4202,  4792, 5382, 5966, 6542,
-7138, 7716, 8292, 8864, 9444, 10030, 10642, 11212, 11814
-};
-
 /**
  * Initialize all tables.
  */
@@ -72,7 +67,7 @@ static av_cold void rv40_init_tables(void)
  aic_mode1_vlc_bits[i],  1, 1,
  aic_mode1_vlc_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
 }
-for(i = 0; i < AIC_MODE2_NUM; i++){
+for (unsigned i = 0, offset = 0; i < AIC_MODE2_NUM; i++){
 uint16_t syms[AIC_MODE2_SIZE];
 
 for (int j = 0; j < AIC_MODE2_SIZE; j++) {
@@ -83,11 +78,12 @@ static av_cold void rv40_init_tables(void)
 else
 syms[j] = first | (second << 8);
 }
-aic_mode2_vlc[i].table = &aic_mode2_table[mode2_offs[i]];
-aic_mode2_vlc[i].table_allocated = mode2_offs[i + 1] - mode2_offs[i];
+aic_mode2_vlc[i].table   = &aic_mode2_table[offset];
+aic_mode2_vlc[i].table_allocated = FF_ARRAY_ELEMS(aic_mode2_table) - 
offset;
 ff_init_vlc_from_lengths(&aic_mode2_vlc[i], AIC_MODE2_BITS, 
AIC_MODE2_SIZE,
  aic_mode2_vlc_bits[i], 1,
  syms, 2, 2, 0, INIT_VLC_USE_NEW_STATIC);
+offset += aic_mode2_vlc[i].table_size;
 }
 for(i = 0; i < NUM_PTYPE_VLCS; i++){
 ptype_vlc[i].table = &ptype_table[i << PTYPE_VLC_BITS];
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 056/114] avcodec/rv40: Make better use of VLC symbols table

2020-11-10 Thread Andreas Rheinhardt
RealVideo 4.0 has a VLC that encodes two intra types per code; each
intra type is in the range 0..8 (inclusive) and up until now the VLC
used symbols in the range 0..80; one type was encoded as the remainder
when dividing the symbol by 9 whereas the other type was encoded as
symbol / 9. This is suboptimal; a better way would be to use the high
and low nibble to encode each symbol. But an even better way is to use
16bit symbols so that the two intra types can be directly written as
a 16bit value.

This commit implements this; in order to avoid huge tables the symbols
are stored as uint8_t with high and low nibbles encoding one type each;
they are only unpacked to uint16_t during initialization.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv40.c |  19 ++-
 libavcodec/rv40vlc2.h | 356 +-
 2 files changed, 193 insertions(+), 182 deletions(-)

diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index 3334331658..81e08af5f1 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -24,6 +24,8 @@
  * RV40 decoder
  */
 
+#include "config.h"
+
 #include "libavutil/imgutils.h"
 
 #include "avcodec.h"
@@ -71,11 +73,21 @@ static av_cold void rv40_init_tables(void)
  aic_mode1_vlc_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
 }
 for(i = 0; i < AIC_MODE2_NUM; i++){
+uint16_t syms[AIC_MODE2_SIZE];
+
+for (int j = 0; j < AIC_MODE2_SIZE; j++) {
+int first  = aic_mode2_vlc_syms[i][j] >> 4;
+int second = aic_mode2_vlc_syms[i][j] & 0xF;
+if (HAVE_BIGENDIAN)
+syms[j] = (first << 8) | second;
+else
+syms[j] = first | (second << 8);
+}
 aic_mode2_vlc[i].table = &aic_mode2_table[mode2_offs[i]];
 aic_mode2_vlc[i].table_allocated = mode2_offs[i + 1] - mode2_offs[i];
 ff_init_vlc_from_lengths(&aic_mode2_vlc[i], AIC_MODE2_BITS, 
AIC_MODE2_SIZE,
  aic_mode2_vlc_bits[i], 1,
- aic_mode2_vlc_syms[i], 1, 1, 0, 
INIT_VLC_USE_NEW_STATIC);
+ syms, 2, 2, 0, INIT_VLC_USE_NEW_STATIC);
 }
 for(i = 0; i < NUM_PTYPE_VLCS; i++){
 ptype_vlc[i].table = &ptype_table[i << PTYPE_VLC_BITS];
@@ -194,9 +206,8 @@ static int rv40_decode_intra_types(RV34DecContext *r, 
GetBitContext *gb, int8_t
 if(pattern == rv40_aic_table_index[k])
 break;
 if(j < 3 && k < MODE2_PATTERNS_NUM){ //pattern is found, decoding 
2 coefficients
-v = get_vlc2(gb, aic_mode2_vlc[k].table, AIC_MODE2_BITS, 2);
-*ptr++ = v/9;
-*ptr++ = v%9;
+AV_WN16(ptr, get_vlc2(gb, aic_mode2_vlc[k].table, 
AIC_MODE2_BITS, 2));
+ptr += 2;
 j++;
 }else{
 if(B != -1 && C != -1)
diff --git a/libavcodec/rv40vlc2.h b/libavcodec/rv40vlc2.h
index 58974f6451..783b93ba2e 100644
--- a/libavcodec/rv40vlc2.h
+++ b/libavcodec/rv40vlc2.h
@@ -55,184 +55,184 @@ static const uint8_t rv40_aic_top_vlc_bits[AIC_TOP_SIZE] 
= {
 
 static const uint8_t aic_mode2_vlc_syms[AIC_MODE2_NUM][AIC_MODE2_SIZE] = {
 {
-0x06, 0x01, 0x1A, 0x0A, 0x14, 0x12, 0x04, 0x07, 0x05, 0x0F, 0x08, 0x36,
-0x02, 0x3C, 0x37, 0x15, 0x0E, 0x1C, 0x18, 0x46, 0x0B, 0x09, 0x26, 0x13,
-0x41, 0x2D, 0x50, 0x4A, 0x19, 0x16, 0x28, 0x24, 0x38, 0x32, 0x11, 0x0C,
-0x30, 0x2F, 0x3A, 0x2A, 0x25, 0x2E, 0x2B, 0x1E, 0x1D, 0x43, 0x3D, 0x0D,
-0x03, 0x1B, 0x17, 0x21, 0x1F, 0x29, 0x22, 0x47, 0x4D, 0x4C, 0x44, 0x42,
-0x34, 0x4E, 0x3B, 0x35, 0x40, 0x3E, 0x3F, 0x33, 0x31, 0x10, 0x45, 0x39,
-0x4F, 0x49, 0x48, 0x23, 0x20, 0x2C, 0x27, 0x4B, 0x00,
-},
-{
-0x46, 0x38, 0x2A, 0x1E, 0x3E, 0x2F, 0x11, 0x05, 0x1C, 0x17, 0x26, 0x04,
-0x2E, 0x28, 0x24, 0x1D, 0x32, 0x25, 0x18, 0x16, 0x1B, 0x19, 0x34, 0x21,
-0x4C, 0x45, 0x49, 0x4B, 0x47, 0x42, 0x29, 0x4D, 0x44, 0x4E, 0x4F, 0x33,
-0x30, 0x48, 0x3D, 0x20, 0x1F, 0x27, 0x22, 0x4A, 0x3F, 0x3B, 0x43, 0x40,
-0x31, 0x2C, 0x39, 0x35, 0x41, 0x3A, 0x07, 0x03, 0x2B, 0x23, 0x50, 0x12,
-0x01, 0x36, 0x1A, 0x0F, 0x02, 0x37, 0x2D, 0x0C, 0x08, 0x15, 0x10, 0x3C,
-0x0D, 0x06, 0x13, 0x0E, 0x09, 0x00, 0x14, 0x0B, 0x0A,
-},
-{
-0x09, 0x01, 0x16, 0x0B, 0x50, 0x4B, 0x3F, 0x0D, 0x08, 0x2A, 0x17, 0x4F,
-0x28, 0x26, 0x36, 0x2D, 0x37, 0x2E, 0x40, 0x3C, 0x0E, 0x04, 0x25, 0x1E,
-0x10, 0x03, 0x1F, 0x11, 0x4E, 0x3E, 0x3D, 0x39, 0x34, 0x44, 0x3B, 0x29,
-0x21, 0x35, 0x31, 0x27, 0x22, 0x33, 0x30, 0x43, 0x38, 0x4D, 0x47, 0x07,
-0x05, 0x20, 0x0C, 0x4C, 0x3A, 0x32, 0x45, 0x42, 0x2B, 0x23, 0x2F, 0x2C,
-0x0A, 0x00, 0x13, 0x12, 0x4A, 0x1A, 0x49, 0x48, 0x1C, 0x1B, 0x24, 0x1D,
-0x0F, 0x06, 0x18, 0x15, 0x19, 0x02, 0x46, 0x41, 0x14,
-},
-{
-0x50, 0x4A, 0x2D, 0x2B, 0x43, 0x27, 0x45, 0x44, 0x3E, 0x16, 0x00, 0x22

[FFmpeg-devel] [PATCH 058/114] avcodec/rv40: Avoid code duplication when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
Besides removing code duplication the method for determining the offset
of each VLC table in the VLC_TYPE buffer also has the advantage of not
wasting space for skipped AIC mode 1 VLCs.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv40.c |  52 +++---
 libavcodec/rv40vlc2.h | 383 +++---
 2 files changed, 200 insertions(+), 235 deletions(-)

diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index 7ed599cd69..15e00c2694 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -41,31 +41,35 @@ static VLC aic_top_vlc;
 static VLC aic_mode1_vlc[AIC_MODE1_NUM], aic_mode2_vlc[AIC_MODE2_NUM];
 static VLC ptype_vlc[NUM_PTYPE_VLCS], btype_vlc[NUM_BTYPE_VLCS];
 
+static av_cold void rv40_init_table(VLC *vlc, unsigned *offset, int nb_bits,
+int nb_codes, const uint8_t (*tab)[2])
+{
+static VLC_TYPE vlc_buf[11776][2];
+
+vlc->table   = &vlc_buf[*offset];
+vlc->table_allocated = 1 << nb_bits;
+*offset += 1 << nb_bits;
+
+ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
+ &tab[0][1], 2, &tab[0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
+}
+
 /**
  * Initialize all tables.
  */
 static av_cold void rv40_init_tables(void)
 {
-int i;
-static VLC_TYPE aic_table[1 << AIC_TOP_BITS][2];
-static VLC_TYPE aic_mode1_table[AIC_MODE1_NUM << AIC_MODE1_BITS][2];
+int i, offset = 0;
 static VLC_TYPE aic_mode2_table[11814][2];
-static VLC_TYPE ptype_table[NUM_PTYPE_VLCS << PTYPE_VLC_BITS][2];
-static VLC_TYPE btype_table[NUM_BTYPE_VLCS << BTYPE_VLC_BITS][2];
-
-aic_top_vlc.table = aic_table;
-aic_top_vlc.table_allocated = 1 << AIC_TOP_BITS;
-init_vlc(&aic_top_vlc, AIC_TOP_BITS, AIC_TOP_SIZE,
- rv40_aic_top_vlc_bits,  1, 1,
- rv40_aic_top_vlc_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
+
+rv40_init_table(&aic_top_vlc, &offset, AIC_TOP_BITS, AIC_TOP_SIZE,
+rv40_aic_top_vlc_tab);
 for(i = 0; i < AIC_MODE1_NUM; i++){
 // Every tenth VLC table is empty
 if((i % 10) == 9) continue;
-aic_mode1_vlc[i].table = &aic_mode1_table[i << AIC_MODE1_BITS];
-aic_mode1_vlc[i].table_allocated = 1 << AIC_MODE1_BITS;
-init_vlc(&aic_mode1_vlc[i], AIC_MODE1_BITS, AIC_MODE1_SIZE,
- aic_mode1_vlc_bits[i],  1, 1,
- aic_mode1_vlc_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
+rv40_init_table(&aic_mode1_vlc[i], &offset, AIC_MODE1_BITS,
+AIC_MODE1_SIZE, aic_mode1_vlc_tabs[i]);
 }
 for (unsigned i = 0, offset = 0; i < AIC_MODE2_NUM; i++){
 uint16_t syms[AIC_MODE2_SIZE];
@@ -86,20 +90,12 @@ static av_cold void rv40_init_tables(void)
 offset += aic_mode2_vlc[i].table_size;
 }
 for(i = 0; i < NUM_PTYPE_VLCS; i++){
-ptype_vlc[i].table = &ptype_table[i << PTYPE_VLC_BITS];
-ptype_vlc[i].table_allocated = 1 << PTYPE_VLC_BITS;
-ff_init_vlc_from_lengths(&ptype_vlc[i], PTYPE_VLC_BITS, PTYPE_VLC_SIZE,
- &ptype_vlc_tabs[i][0][1], 2,
- &ptype_vlc_tabs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+rv40_init_table(&ptype_vlc[i], &offset, PTYPE_VLC_BITS, PTYPE_VLC_SIZE,
+ptype_vlc_tabs[i]);
 }
 for(i = 0; i < NUM_BTYPE_VLCS; i++){
-btype_vlc[i].table = &btype_table[i << BTYPE_VLC_BITS];
-btype_vlc[i].table_allocated = 1 << BTYPE_VLC_BITS;
-ff_init_vlc_from_lengths(&btype_vlc[i], BTYPE_VLC_BITS, BTYPE_VLC_SIZE,
- &btype_vlc_tabs[i][0][1], 2,
- &btype_vlc_tabs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+rv40_init_table(&btype_vlc[i], &offset, BTYPE_VLC_BITS, BTYPE_VLC_SIZE,
+btype_vlc_tabs[i]);
 }
 }
 
diff --git a/libavcodec/rv40vlc2.h b/libavcodec/rv40vlc2.h
index 783b93ba2e..fec47ff4ad 100644
--- a/libavcodec/rv40vlc2.h
+++ b/libavcodec/rv40vlc2.h
@@ -35,13 +35,10 @@
 //@{
 #define AIC_TOP_BITS  7
 #define AIC_TOP_SIZE 16
-static const uint8_t rv40_aic_top_vlc_codes[AIC_TOP_SIZE] = {
- 0x01, 0x05, 0x01, 0x00, 0x03, 0x3D, 0x1D, 0x02,
- 0x04, 0x3C, 0x3F, 0x1C, 0x0D, 0x3E, 0x0C, 0x01
-};
-
-static const uint8_t rv40_aic_top_vlc_bits[AIC_TOP_SIZE] = {
- 1, 4, 5, 5, 5, 7, 6, 5, 4, 7, 7, 6, 5, 7, 5, 3
+static const uint8_t rv40_aic_top_vlc_tab[AIC_TOP_SIZE][2] = {
+{ 0x3, 5 }, { 0x2, 5 }, { 0x7, 5 }, { 0x4, 5 }, { 0xF, 3 }, { 0x8, 4 },
+{ 0x1, 4 }, { 0xE, 5 }, { 0xC, 5 }, { 0xB, 6 }, { 0x6, 6 }, { 0x9, 7 },
+{ 0x5, 7 }, { 0xD, 7 }, { 0xA, 7 }, { 0x0, 1 },
 };
 //@}
 
@@ -388,206 +385,178 @@ static const uint8_t 
aic_mode2_vlc_bits[AIC_MODE2_NUM][AIC_MODE2_SIZE] = {
 #define AIC_MODE1_SIZE  9
 #define AIC_MODE1_BITS  7
 
-static const uint8_t aic_mode1_vlc_codes[AIC_MODE

[FFmpeg-devel] [PATCH 055/114] avcodec/rv40: Make the tables used to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
After permuting the codes, symbols and lengths tables used to initialize
the VLC so that the codes are ordered from left to right in the Huffman
tree, the codes become redundant as they can be easily computed from the
lengths at runtime; in this case one has to use explicit symbol tables,
but all the symbols used here fit into an uint8_t, whereas some codes
needed uint16_t. This saves about 1.6KB.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv40.c |  22 +-
 libavcodec/rv40vlc2.h | 754 +++---
 2 files changed, 360 insertions(+), 416 deletions(-)

diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index 462024c81e..3334331658 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -73,25 +73,25 @@ static av_cold void rv40_init_tables(void)
 for(i = 0; i < AIC_MODE2_NUM; i++){
 aic_mode2_vlc[i].table = &aic_mode2_table[mode2_offs[i]];
 aic_mode2_vlc[i].table_allocated = mode2_offs[i + 1] - mode2_offs[i];
-init_vlc(&aic_mode2_vlc[i], AIC_MODE2_BITS, AIC_MODE2_SIZE,
- aic_mode2_vlc_bits[i],  1, 1,
- aic_mode2_vlc_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&aic_mode2_vlc[i], AIC_MODE2_BITS, 
AIC_MODE2_SIZE,
+ aic_mode2_vlc_bits[i], 1,
+ aic_mode2_vlc_syms[i], 1, 1, 0, 
INIT_VLC_USE_NEW_STATIC);
 }
 for(i = 0; i < NUM_PTYPE_VLCS; i++){
 ptype_vlc[i].table = &ptype_table[i << PTYPE_VLC_BITS];
 ptype_vlc[i].table_allocated = 1 << PTYPE_VLC_BITS;
-ff_init_vlc_sparse(&ptype_vlc[i], PTYPE_VLC_BITS, PTYPE_VLC_SIZE,
-ptype_vlc_bits[i],  1, 1,
-ptype_vlc_codes[i], 1, 1,
-ptype_vlc_syms, 1, 1, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ptype_vlc[i], PTYPE_VLC_BITS, PTYPE_VLC_SIZE,
+ &ptype_vlc_tabs[i][0][1], 2,
+ &ptype_vlc_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 }
 for(i = 0; i < NUM_BTYPE_VLCS; i++){
 btype_vlc[i].table = &btype_table[i << BTYPE_VLC_BITS];
 btype_vlc[i].table_allocated = 1 << BTYPE_VLC_BITS;
-ff_init_vlc_sparse(&btype_vlc[i], BTYPE_VLC_BITS, BTYPE_VLC_SIZE,
-btype_vlc_bits[i],  1, 1,
-btype_vlc_codes[i], 1, 1,
-btype_vlc_syms, 1, 1, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&btype_vlc[i], BTYPE_VLC_BITS, BTYPE_VLC_SIZE,
+ &btype_vlc_tabs[i][0][1], 2,
+ &btype_vlc_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 }
 }
 
diff --git a/libavcodec/rv40vlc2.h b/libavcodec/rv40vlc2.h
index 4cdc7a4193..58974f6451 100644
--- a/libavcodec/rv40vlc2.h
+++ b/libavcodec/rv40vlc2.h
@@ -53,370 +53,330 @@ static const uint8_t rv40_aic_top_vlc_bits[AIC_TOP_SIZE] 
= {
 #define AIC_MODE2_SIZE 81
 #define AIC_MODE2_BITS  9
 
-static const uint16_t aic_mode2_vlc_codes[AIC_MODE2_NUM][AIC_MODE2_SIZE] = {
-{ 0x0001, 0x0001, 0x0005, 0x01F5, 0x0011, 0x0049, 0x, 0x0048, 0x004B,
-  0x0035, 0x0003, 0x0034, 0x03C9, 0x01F4, 0x00C9, 0x004A, 0x0FD9, 0x03C8,
-  0x0010, 0x0037, 0x0001, 0x00C8, 0x0075, 0x01F7, 0x00CB, 0x0074, 0x0002,
-  0x01F6, 0x00CA, 0x01F1, 0x01F0, 0x1F81, 0x07F9, 0x1F80, 0x1F83, 0x07F8,
-  0x0077, 0x00F5, 0x0036, 0x07FB, 0x0076, 0x1F82, 0x00F4, 0x00F7, 0x07FA,
-  0x0071, 0x00F6, 0x03CB, 0x03CA, 0x0FD8, 0x00F1, 0x03F5, 0x1F8D, 0x07E5,
-  0x0013, 0x0031, 0x00F0, 0x0FDB, 0x00F3, 0x07E4, 0x0030, 0x01F3, 0x07E7,
-  0x03F4, 0x07E6, 0x0070, 0x3F19, 0x01F2, 0x3F18, 0x0FDA, 0x0033, 0x07E1,
-  0x01FD, 0x01FC, 0x0073, 0x01FF, 0x0FC5, 0x0FC4, 0x0FC7, 0x03F7, 0x0072, },
-{ 0x0005, 0x0005, 0x0005, 0x0079, 0x0005, 0x000D, 0x001D, 0x0078, 0x0069,
-  0x0004, 0x0001, 0x0007, 0x0068, 0x001C, 0x001F, 0x0004, 0x006B, 0x000C,
-  0x0004, 0x001E, 0x0006, 0x006A, 0x0015, 0x000F, 0x0014, 0x0017, 0x0007,
-  0x0016, 0x000E, 0x0011, 0x0009, 0x00D1, 0x00D0, 0x0181, 0x00D3, 0x007B,
-  0x0010, 0x0013, 0x0004, 0x00D2, 0x0007, 0x0319, 0x0008, 0x007A, 0x00DD,
-  0x0019, 0x0006, 0x000B, 0x0065, 0x00DC, 0x0012, 0x0064, 0x0180, 0x00DF,
-  0x0006, 0x0018, 0x0001, 0x00DE, 0x001D, 0x00D9, 0x001B, 0x0067, 0x000A,
-  0x00D8, 0x00DB, 0x001C, 0x0318, 0x00DA, 0x0635, 0x0183, 0x, 0x00C5,
-  0x0066, 0x0061, 0x0035, 0x00C4, 0x0182, 0x0634, 0x031B, 0x00C7, 0x001F, },
-{ 0x0005, 0x0001, 0x001D, 0x01C1, 0x0035, 0x00F1, 0x006D, 0x00F0, 0x0049,
-  0x, 0x0004, 0x0003, 0x00F3, 0x0048, 0x0034, 0x006C, 0x01C0, 0x01C3,
-  0x0007, 0x0006, 0x0001, 0x006F, 0x0002, 0x004B, 0x006E, 0x001C, 0x0005,
-  0x0069, 0x0068, 0x006B, 0x0037, 0x01C2, 0x00F2, 0x0395, 0x01CD, 0x00FD,
-  0x006A, 0x0036, 0x0015, 0x01CC, 0x0014, 0x0394, 0x004A, 0x00FC, 0x00FF,
-  0x0017, 0x0031, 0x00FE, 0x01CF, 0x0397, 0

[FFmpeg-devel] [PATCH 060/114] avcodec/mss4: Don't duplicate standard JPEG tables

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 configure |  2 +-
 libavcodec/mss4.c | 58 +--
 2 files changed, 7 insertions(+), 53 deletions(-)

diff --git a/configure b/configure
index 8a9e9b3cd7..4b47832cd7 100755
--- a/configure
+++ b/configure
@@ -2801,7 +2801,7 @@ msmpeg4v2_encoder_select="h263_encoder"
 msmpeg4v3_decoder_select="h263_decoder"
 msmpeg4v3_encoder_select="h263_encoder"
 mss2_decoder_select="mpegvideo qpeldsp vc1_decoder"
-mts2_decoder_select="mss34dsp"
+mts2_decoder_select="jpegtables mss34dsp"
 mv30_decoder_select="aandcttables blockdsp"
 mvha_decoder_deps="zlib"
 mvha_decoder_select="llviddsp"
diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index a459503417..9c257dc873 100644
--- a/libavcodec/mss4.c
+++ b/libavcodec/mss4.c
@@ -29,6 +29,7 @@
 #include "bytestream.h"
 #include "get_bits.h"
 #include "internal.h"
+#include "jpegtables.h"
 #include "mss34dsp.h"
 #include "unary.h"
 
@@ -57,56 +58,6 @@ static const uint8_t mss4_dc_vlc_lens[2][16] = {
 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0 }
 };
 
-static const uint8_t mss4_ac_vlc_lens[2][16] = {
-{ 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125 },
-{ 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119 }
-};
-
-static const uint8_t mss4_ac_vlc_syms[2][162] = {
-  { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
-0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
-0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
-0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
-0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16,
-0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
-0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
-0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
-0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
-0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
-0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
-0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
-0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
-0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
-0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
-0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
-0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4,
-0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
-0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
-0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
-0xF9, 0xFA  },
-  { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
-0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
-0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
-0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
-0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34,
-0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
-0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38,
-0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
-0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
-0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
-0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
-0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
-0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
-0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
-0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
-0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
-0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
-0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
-0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
-0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
-0xF9, 0xFA  }
-};
-
 static const uint8_t vec_len_syms[2][4] = {
 { 4, 2, 3, 1 },
 { 4, 1, 2, 3 }
@@ -168,8 +119,11 @@ static av_cold int mss4_init_vlcs(MSS4Context *ctx)
 ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL);
 if (ret)
 return ret;
-ret = mss4_init_vlc(&ctx->ac_vlc[i], mss4_ac_vlc_lens[i],
-mss4_ac_vlc_syms[i]);
+ret = mss4_init_vlc(&ctx->ac_vlc[i],
+i ? avpriv_mjpeg_bits_ac_chrominance + 1
+  : avpriv_mjpeg_bits_ac_luminance   + 1,
+i ? avpriv_mjpeg_val_ac_chrominance
+  : avpriv_mjpeg_val_ac_luminance);
 if (ret)
 return ret;
 ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 059/114] avcodec/mss4: Simplify creating VLC tables

2020-11-10 Thread Andreas Rheinhardt
The lengths of the codes used by the mss4 decoder are ascending from
left to right and therefore the lengths can be run-length encoded and
the codes can be easily derived from them. And this is how it is indeed
done. Yet some things can nevertheless be improved:

a) The number of entries of the current VLC is implicitly contained in
the run-length table and needn't be externally prescribed.
b) The maximum length of a code is just the length of the last code
(given that the lengths are ascending), so there is no point in setting
max_bits in the loop itself.
c) One can offload the actual calculation of the codes to
ff_init_vlc_from_lengths().

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mss4.c | 18 +++---
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index c421a07c5d..a459503417 100644
--- a/libavcodec/mss4.c
+++ b/libavcodec/mss4.c
@@ -143,25 +143,21 @@ typedef struct MSS4Context {
 } MSS4Context;
 
 static av_cold int mss4_init_vlc(VLC *vlc, const uint8_t *lens,
- const uint8_t *syms, int num_syms)
+ const uint8_t *syms)
 {
 uint8_t  bits[MAX_ENTRIES];
-uint16_t codes[MAX_ENTRIES];
 int i, j;
-int prefix = 0, max_bits = 0, idx = 0;
+int idx = 0;
 
 for (i = 0; i < 16; i++) {
 for (j = 0; j < lens[i]; j++) {
 bits[idx]  = i + 1;
-codes[idx] = prefix++;
-max_bits   = i + 1;
 idx++;
 }
-prefix <<= 1;
 }
 
-return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), num_syms, bits, 1, 1,
-  codes, 2, 2, syms, 1, 1, 0);
+return ff_init_vlc_from_lengths(vlc, FFMIN(bits[idx - 1], 9), idx,
+bits, 1, syms, 1, 1, 0, 0);
 }
 
 static av_cold int mss4_init_vlcs(MSS4Context *ctx)
@@ -169,15 +165,15 @@ static av_cold int mss4_init_vlcs(MSS4Context *ctx)
 int ret, i;
 
 for (i = 0; i < 2; i++) {
-ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL, 12);
+ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL);
 if (ret)
 return ret;
 ret = mss4_init_vlc(&ctx->ac_vlc[i], mss4_ac_vlc_lens[i],
-mss4_ac_vlc_syms[i], 162);
+mss4_ac_vlc_syms[i]);
 if (ret)
 return ret;
 ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
-mss4_vec_entry_vlc_syms[i], 9);
+mss4_vec_entry_vlc_syms[i]);
 if (ret)
 return ret;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 061/114] avcodec/mss4: Make VLCs static

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mss4.c | 73 +++
 1 file changed, 29 insertions(+), 44 deletions(-)

diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index 9c257dc873..9b343c9b2b 100644
--- a/libavcodec/mss4.c
+++ b/libavcodec/mss4.c
@@ -78,8 +78,6 @@ static const uint8_t mss4_vec_entry_vlc_syms[2][9] = {
 typedef struct MSS4Context {
 AVFrame*pic;
 
-VLCdc_vlc[2], ac_vlc[2];
-VLCvec_entry_vlc[2];
 intblock[64];
 uint8_timgbuf[3][16 * 16];
 
@@ -93,9 +91,13 @@ typedef struct MSS4Context {
 intprev_vec[3][4];
 } MSS4Context;
 
-static av_cold int mss4_init_vlc(VLC *vlc, const uint8_t *lens,
- const uint8_t *syms)
+static VLC dc_vlc[2], ac_vlc[2];
+static VLC vec_entry_vlc[2];
+
+static av_cold void mss4_init_vlc(VLC *vlc, unsigned *offset,
+  const uint8_t *lens, const uint8_t *syms)
 {
+static VLC_TYPE vlc_buf[2146][2];
 uint8_t  bits[MAX_ENTRIES];
 int i, j;
 int idx = 0;
@@ -107,41 +109,27 @@ static av_cold int mss4_init_vlc(VLC *vlc, const uint8_t 
*lens,
 }
 }
 
-return ff_init_vlc_from_lengths(vlc, FFMIN(bits[idx - 1], 9), idx,
-bits, 1, syms, 1, 1, 0, 0);
-}
-
-static av_cold int mss4_init_vlcs(MSS4Context *ctx)
-{
-int ret, i;
-
-for (i = 0; i < 2; i++) {
-ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL);
-if (ret)
-return ret;
-ret = mss4_init_vlc(&ctx->ac_vlc[i],
-i ? avpriv_mjpeg_bits_ac_chrominance + 1
-  : avpriv_mjpeg_bits_ac_luminance   + 1,
-i ? avpriv_mjpeg_val_ac_chrominance
-  : avpriv_mjpeg_val_ac_luminance);
-if (ret)
-return ret;
-ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
-mss4_vec_entry_vlc_syms[i]);
-if (ret)
-return ret;
-}
-return 0;
+vlc->table   = &vlc_buf[*offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *offset;
+ff_init_vlc_from_lengths(vlc, FFMIN(bits[idx - 1], 9), idx,
+ bits, 1, syms, 1, 1, 0, INIT_VLC_USE_NEW_STATIC);
+*offset += vlc->table_size;
 }
 
-static av_cold void mss4_free_vlcs(MSS4Context *ctx)
+static av_cold void mss4_init_vlcs(void)
 {
-int i;
-
-for (i = 0; i < 2; i++) {
-ff_free_vlc(&ctx->dc_vlc[i]);
-ff_free_vlc(&ctx->ac_vlc[i]);
-ff_free_vlc(&ctx->vec_entry_vlc[i]);
+if (vec_entry_vlc[1].table_size)
+return;
+
+for (unsigned i = 0, offset = 0; i < 2; i++) {
+mss4_init_vlc(&dc_vlc[i], &offset, mss4_dc_vlc_lens[i], NULL);
+mss4_init_vlc(&ac_vlc[i], &offset,
+  i ? avpriv_mjpeg_bits_ac_chrominance + 1
+: avpriv_mjpeg_bits_ac_luminance   + 1,
+  i ? avpriv_mjpeg_val_ac_chrominance
+: avpriv_mjpeg_val_ac_luminance);
+mss4_init_vlc(&vec_entry_vlc[i], &offset, mss4_vec_entry_vlc_lens[i],
+  mss4_vec_entry_vlc_syms[i]);
 }
 }
 
@@ -239,7 +227,7 @@ static int mss4_decode_dct_block(MSS4Context *c, 
GetBitContext *gb,
 int xpos = mb_x * 2 + i;
 c->dc_cache[j][TOP_LEFT] = c->dc_cache[j][TOP];
 c->dc_cache[j][TOP]  = c->prev_dc[0][mb_x * 2 + i];
-ret = mss4_decode_dct(gb, c->dc_vlc, c->ac_vlc, c->block,
+ret = mss4_decode_dct(gb, &dc_vlc[0], &ac_vlc[0], c->block,
   c->dc_cache[j],
   xpos, mb_y * 2 + j, c->quant_mat[0]);
 if (ret)
@@ -255,7 +243,7 @@ static int mss4_decode_dct_block(MSS4Context *c, 
GetBitContext *gb,
 for (i = 1; i < 3; i++) {
 c->dc_cache[i + 1][TOP_LEFT] = c->dc_cache[i + 1][TOP];
 c->dc_cache[i + 1][TOP]  = c->prev_dc[i][mb_x];
-ret = mss4_decode_dct(gb, c->dc_vlc + 1, c->ac_vlc + 1,
+ret = mss4_decode_dct(gb, &dc_vlc[1], &ac_vlc[1],
   c->block, c->dc_cache[i + 1], mb_x, mb_y,
   c->quant_mat[1]);
 if (ret)
@@ -347,7 +335,7 @@ static int mss4_decode_image_block(MSS4Context *ctx, 
GetBitContext *gb,
 for (i = 0; i < 3; i++) {
 vec_len[i] = vec_len_syms[!!i][get_unary(gb, 0, 3)];
 for (j = 0; j < vec_len[i]; j++) {
-vec[i][j]  = get_coeff(gb, &ctx->vec_entry_vlc[!!i]);
+vec[i][j]  = get_coeff(gb, &vec_entry_vlc[!!i]);
 vec[i][j] += ctx->prev_vec[i][j];
 ctx->prev_vec[i][j] = vec[i][j];
 }
@@ -586,7 +574,6 @@ static av_cold int mss4_decode_end(AVCodecContext *avctx)
 av_frame_free(&c->pic);
 for (i = 0; i < 3; i++)
 av_freep(&c->prev_dc[i]);
- 

[FFmpeg-devel] [PATCH 065/114] avcodec/mjpegdec: Simplify creating VLC table

2020-11-10 Thread Andreas Rheinhardt
ff_init_vlc_from_lengths() can be used to offload the computation
of the codes; it also allows to omit the check whether the codes
are already properly ordered (they are).

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mjpegdec.c | 15 +--
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 147dd819e5..2a226f63d4 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -50,18 +50,14 @@
 #include "bytestream.h"
 
 
-static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
-const uint8_t *bits_table)
+static void build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table)
 {
-for (int i = 1, code = 0, k = 0; i <= 16; i++) {
+for (int i = 1, k = 0; i <= 16; i++) {
 int nb = bits_table[i];
 for (int j = 0; j < nb;j++) {
 huff_size[k] = i;
-huff_code[k] = code;
-code++;
 k++;
 }
-code <<= 1;
 }
 }
 
@@ -70,13 +66,12 @@ static int build_vlc(VLC *vlc, const uint8_t *bits_table,
  int is_ac)
 {
 uint8_t huff_size[256];
-uint16_t huff_code[256];
 uint16_t huff_sym[256];
 int i;
 
 av_assert0(nb_codes <= 256);
 
-build_huffman_codes(huff_size, huff_code, bits_table);
+build_huffman_codes(huff_size, bits_table);
 
 for (i = 0; i < nb_codes; i++) {
 huff_sym[i] = val_table[i] + 16 * is_ac;
@@ -85,8 +80,8 @@ static int build_vlc(VLC *vlc, const uint8_t *bits_table,
 huff_sym[i] = 16 * 256;
 }
 
-return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
-  huff_code, 2, 2, huff_sym, 2, 2, 0);
+return ff_init_vlc_from_lengths(vlc, 9, nb_codes, huff_size, 1,
+huff_sym, 2, 2, 0, 0);
 }
 
 static int init_default_huffman_tables(MJpegDecodeContext *s)
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 063/114] avcodec/sheervideo: Simplify creating VLC table

2020-11-10 Thread Andreas Rheinhardt
ff_init_vlc_from_lengths() can be used to offload the computation
of the codes; it also needn't check whether the codes are already
properly ordered (they are).

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/sheervideo.c | 13 -
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/libavcodec/sheervideo.c b/libavcodec/sheervideo.c
index 3e60ef26a5..98093d500a 100644
--- a/libavcodec/sheervideo.c
+++ b/libavcodec/sheervideo.c
@@ -1784,11 +1784,10 @@ static void decode_rgb(AVCodecContext *avctx, AVFrame 
*p, GetBitContext *gb)
 static av_cold int build_vlc(VLC *vlc, const SheerTable *table)
 {
 const uint8_t *cur = table->lens;
-uint16_t codes[1024];
 uint8_t  lens[1024];
 unsigned count = 0;
 
-for (unsigned step = 1, len = 1, index = 0; len > 0; len += step) {
+for (unsigned step = 1, len = 1; len > 0; len += step) {
 unsigned new_count = count;
 
 if (len == 16) {
@@ -1797,17 +1796,13 @@ static av_cold int build_vlc(VLC *vlc, const SheerTable 
*table)
 } else
 new_count += *cur++;
 
-for (; count < new_count; count++) {
-codes[count] = index >> (32 - len);
-index   += 1U<< (32 - len);
+for (; count < new_count; count++)
 lens[count]  = len;
-}
 }
 
 ff_free_vlc(vlc);
-return init_vlc(vlc, SHEER_VLC_BITS, count,
-lens,  sizeof(*lens),  sizeof(*lens),
-codes, sizeof(*codes), sizeof(*codes), 0);
+return ff_init_vlc_from_lengths(vlc, SHEER_VLC_BITS, count,
+lens, sizeof(*lens), NULL, 0, 0, 0, 0);
 }
 
 static int decode_frame(AVCodecContext *avctx,
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 064/114] avcodec/g2meet: Use least max_depth in get_vlc2()

2020-11-10 Thread Andreas Rheinhardt
The longest AC codes of the standard JPEG tables are 16 bits long; for
the DC tables, the maximum is 11, so using max_depth of two is
sufficient.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/g2meet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/g2meet.c b/libavcodec/g2meet.c
index 6b870ae3d4..f6aa636bde 100644
--- a/libavcodec/g2meet.c
+++ b/libavcodec/g2meet.c
@@ -248,7 +248,7 @@ static int jpg_decode_block(JPGContext *c, GetBitContext 
*gb,
 return AVERROR_INVALIDDATA;
 
 c->bdsp.clear_block(block);
-dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
+dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 2);
 if (dc < 0)
 return AVERROR_INVALIDDATA;
 if (dc)
@@ -259,7 +259,7 @@ static int jpg_decode_block(JPGContext *c, GetBitContext 
*gb,
 
 pos = 0;
 while (pos < 63) {
-val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
+val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 2);
 if (val < 0)
 return AVERROR_INVALIDDATA;
 pos += val >> 4;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 066/114] avcodec/indeo2: Make tables used to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
Switching from ff_init_vlc_sparse() to ff_init_vlc_from_lengths()
allows to replace codes which are so long that they need to be stored
in an uint16_t by symbols which fit into an uint8_t; furthermore, it is
also easily possible to already incorporate the offset (the real range
of Indeo 2 symbols starts at one, not zero) into the symbols.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/indeo2.c |  8 ++---
 libavcodec/indeo2data.h | 67 ++---
 2 files changed, 34 insertions(+), 41 deletions(-)

diff --git a/libavcodec/indeo2.c b/libavcodec/indeo2.c
index 7a568bfbc4..5a9c0e77be 100644
--- a/libavcodec/indeo2.c
+++ b/libavcodec/indeo2.c
@@ -46,7 +46,7 @@ static VLC ir2_vlc;
 /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
 static inline int ir2_get_code(GetBitContext *gb)
 {
-return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
+return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1);
 }
 
 static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t 
*dst,
@@ -237,9 +237,9 @@ static av_cold int ir2_decode_init(AVCodecContext *avctx)
 if (!ic->picture)
 return AVERROR(ENOMEM);
 
-INIT_LE_VLC_STATIC(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
-   &ir2_codes[0][1], 4, 2,
-   &ir2_codes[0][0], 4, 2, 1 << CODE_VLC_BITS);
+INIT_VLC_STATIC_FROM_LENGTHS(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
+ &ir2_tab[0][1], 2, &ir2_tab[0][0], 2, 1,
+ 0, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
 
 return 0;
 }
diff --git a/libavcodec/indeo2data.h b/libavcodec/indeo2data.h
index bfdb0a68a3..9981a3b2c0 100644
--- a/libavcodec/indeo2data.h
+++ b/libavcodec/indeo2data.h
@@ -25,43 +25,36 @@
 #include 
 
 #define IR2_CODES 143
-static const uint16_t ir2_codes[IR2_CODES][2] = {
-{ 0x,  3 }, { 0x0004,  3 }, { 0x0006,  3 }, { 0x0001,  5 },
-{ 0x0009,  5 }, { 0x0019,  5 }, { 0x000D,  5 }, { 0x001D,  5 },
-{ 0x0023,  6 }, { 0x0013,  6 }, { 0x0033,  6 }, { 0x000B,  6 },
-{ 0x002B,  6 }, { 0x001B,  6 }, { 0x0007,  8 }, { 0x0087,  8 },
-{ 0x0027,  8 }, { 0x00A7,  8 }, { 0x0067,  8 }, { 0x00E7,  8 },
-{ 0x0097,  8 }, { 0x0057,  8 }, { 0x0037,  8 }, { 0x00B7,  8 },
-{ 0x00F7,  8 }, { 0x000F,  9 }, { 0x008F,  9 }, { 0x018F,  9 },
-{ 0x014F,  9 }, { 0x00CF,  9 }, { 0x002F,  9 }, { 0x012F,  9 },
-{ 0x01AF,  9 }, { 0x006F,  9 }, { 0x00EF,  9 }, { 0x01EF,  9 },
-{ 0x001F, 10 }, { 0x021F, 10 }, { 0x011F, 10 }, { 0x031F, 10 },
-{ 0x009F, 10 }, { 0x029F, 10 }, { 0x019F, 10 }, { 0x039F, 10 },
-{ 0x005F, 10 }, { 0x025F, 10 }, { 0x015F, 10 }, { 0x035F, 10 },
-{ 0x00DF, 10 }, { 0x02DF, 10 }, { 0x01DF, 10 }, { 0x03DF, 10 },
-{ 0x003F, 13 }, { 0x103F, 13 }, { 0x083F, 13 }, { 0x183F, 13 },
-{ 0x043F, 13 }, { 0x143F, 13 }, { 0x0C3F, 13 }, { 0x1C3F, 13 },
-{ 0x023F, 13 }, { 0x123F, 13 }, { 0x0A3F, 13 }, { 0x1A3F, 13 },
-{ 0x063F, 13 }, { 0x163F, 13 }, { 0x0E3F, 13 }, { 0x1E3F, 13 },
-{ 0x013F, 13 }, { 0x113F, 13 }, { 0x093F, 13 }, { 0x193F, 13 },
-{ 0x053F, 13 }, { 0x153F, 13 }, { 0x0D3F, 13 }, { 0x1D3F, 13 },
-{ 0x033F, 13 }, { 0x133F, 13 }, { 0x0B3F, 13 }, { 0x1B3F, 13 },
-{ 0x073F, 13 }, { 0x173F, 13 }, { 0x0F3F, 13 }, { 0x1F3F, 13 },
-{ 0x00BF, 13 }, { 0x10BF, 13 }, { 0x08BF, 13 }, { 0x18BF, 13 },
-{ 0x04BF, 13 }, { 0x14BF, 13 }, { 0x0CBF, 13 }, { 0x1CBF, 13 },
-{ 0x02BF, 13 }, { 0x12BF, 13 }, { 0x0ABF, 13 }, { 0x1ABF, 13 },
-{ 0x06BF, 13 }, { 0x16BF, 13 }, { 0x0EBF, 13 }, { 0x1EBF, 13 },
-{ 0x01BF, 13 }, { 0x11BF, 13 }, { 0x09BF, 13 }, { 0x19BF, 13 },
-{ 0x05BF, 13 }, { 0x15BF, 13 }, { 0x0DBF, 13 }, { 0x1DBF, 13 },
-{ 0x03BF, 13 }, { 0x13BF, 13 }, { 0x0BBF, 13 }, { 0x1BBF, 13 },
-{ 0x07BF, 13 }, { 0x17BF, 13 }, { 0x0FBF, 13 }, { 0x1FBF, 13 },
-{ 0x007F, 14 }, { 0x207F, 14 }, { 0x107F, 14 }, { 0x307F, 14 },
-{ 0x087F, 14 }, { 0x287F, 14 }, { 0x187F, 14 }, { 0x387F, 14 },
-{ 0x047F, 14 }, { 0x247F, 14 }, { 0x147F, 14 }, { 0x0002,  3 },
-{ 0x0011,  5 }, { 0x0005,  5 }, { 0x0015,  5 }, { 0x0003,  6 },
-{ 0x003B,  6 }, { 0x0047,  8 }, { 0x00C7,  8 }, { 0x0017,  8 },
-{ 0x00D7,  8 }, { 0x0077,  8 }, { 0x010F,  9 }, { 0x004F,  9 },
-{ 0x01CF,  9 }, { 0x00AF,  9 }, { 0x016F,  9 },
+static const uint8_t ir2_tab[IR2_CODES][2] = {
+{ 0x01,  3 }, { 0x02,  3 }, { 0x80,  3 }, { 0x03,  3 }, { 0x04,  5 },
+{ 0x81,  5 }, { 0x05,  5 }, { 0x06,  5 }, { 0x82,  5 }, { 0x83,  5 },
+{ 0x07,  5 }, { 0x08,  5 }, { 0x84,  6 }, { 0x09,  6 }, { 0x0A,  6 },
+{ 0x0B,  6 }, { 0x0C,  6 }, { 0x0D,  6 }, { 0x0E,  6 }, { 0x85,  6 },
+{ 0x0F,  8 }, { 0x10,  8 }, { 0x86,  8 }, { 0x87,  8 }, { 0x11,  8 },
+{ 0x12,  8 }, { 0x13,  8 }, { 0x14,  8 }, { 0x88,  8 }, { 0x15,  8 },
+{ 0x16,  8 }, { 0x89,  8 }, { 0x17,  8 }, { 0x18,  8 }, { 0x8A,  8 },
+{ 0x19,  8 }, { 0x1A,  9 }, { 0x8B,  9 }, { 0x1B,  9 }, { 0x1C,  9 },
+{ 0x8C, 

[FFmpeg-devel] [PATCH 062/114] avcodec/sheervideo: Add av_cold to build_vlc()

2020-11-10 Thread Andreas Rheinhardt
It is an init function even when called from decode_frame().

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/sheervideo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/sheervideo.c b/libavcodec/sheervideo.c
index 976c21c445..3e60ef26a5 100644
--- a/libavcodec/sheervideo.c
+++ b/libavcodec/sheervideo.c
@@ -1781,7 +1781,7 @@ static void decode_rgb(AVCodecContext *avctx, AVFrame *p, 
GetBitContext *gb)
 }
 }
 
-static int build_vlc(VLC *vlc, const SheerTable *table)
+static av_cold int build_vlc(VLC *vlc, const SheerTable *table)
 {
 const uint8_t *cur = table->lens;
 uint16_t codes[1024];
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 070/114] avcodec/atrac3plus: Simplify creating VLCs

2020-11-10 Thread Andreas Rheinhardt
Use ff_init_vlc_from_lengths() to offload the computation of the codes.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac3plus.c | 8 ++--
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index 6b046a887e..8ae5d6b8f9 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -51,9 +51,7 @@ static av_cold void build_canonical_huff(const uint8_t *cb, 
const uint8_t *xlat,
  int *tab_offset, VLC *out_vlc)
 {
 int i, b;
-uint16_t codes[256];
 uint8_t bits[256];
-unsigned code = 0;
 int index = 0;
 int min_len = *cb++; // get shortest codeword length
 int max_len = *cb++; // get longest  codeword length
@@ -62,17 +60,15 @@ static av_cold void build_canonical_huff(const uint8_t *cb, 
const uint8_t *xlat,
 for (i = *cb++; i > 0; i--) {
 av_assert0(index < 256);
 bits[index]  = b;
-codes[index] = code++;
 index++;
 }
-code <<= 1;
 }
 
 out_vlc->table = &tables_data[*tab_offset];
 out_vlc->table_allocated = 1 << max_len;
 
-ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,
-   xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(out_vlc, max_len, index, bits, 1,
+ xlat, 1, 1, 0, INIT_VLC_USE_NEW_STATIC);
 
 *tab_offset += 1 << max_len;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 068/114] avcodec/mpeg4videodec: Make studio VLCs static

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpeg4video.h|  4 --
 libavcodec/mpeg4videodec.c | 87 --
 2 files changed, 36 insertions(+), 55 deletions(-)

diff --git a/libavcodec/mpeg4video.h b/libavcodec/mpeg4video.h
index 3de598465f..e919db87a5 100644
--- a/libavcodec/mpeg4video.h
+++ b/libavcodec/mpeg4video.h
@@ -115,10 +115,6 @@ typedef struct Mpeg4DecContext {
 int cplx_estimation_trash_p;
 int cplx_estimation_trash_b;
 
-VLC studio_intra_tab[12];
-VLC studio_luma_dc;
-VLC studio_chroma_dc;
-
 int rgb;
 } Mpeg4DecContext;
 
diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index a03bd240a8..a94fe16056 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -53,6 +53,9 @@ static int decode_studio_vol_header(Mpeg4DecContext *ctx, 
GetBitContext *gb);
 static VLC dc_lum, dc_chrom;
 static VLC sprite_trajectory;
 static VLC mb_type_b_vlc;
+static VLC studio_intra_tab[12];
+static VLC studio_luma_dc;
+static VLC studio_chroma_dc;
 
 static const int mb_type_b_map[4] = {
 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
@@ -1820,7 +1823,7 @@ static int mpeg4_decode_studio_block(MpegEncContext *s, 
int32_t block[64], int n
 
 int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
 additional_code_len, sign, mismatch;
-VLC *cur_vlc = &ctx->studio_intra_tab[0];
+const VLC *cur_vlc = &studio_intra_tab[0];
 uint8_t *const scantable = s->intra_scantable.permutated;
 const uint16_t *quant_matrix;
 uint32_t flc;
@@ -1834,14 +1837,14 @@ static int mpeg4_decode_studio_block(MpegEncContext *s, 
int32_t block[64], int n
 
 if (n < 4) {
 cc = 0;
-dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, 
STUDIO_INTRA_BITS, 2);
+dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, 
STUDIO_INTRA_BITS, 2);
 quant_matrix = s->intra_matrix;
 } else {
 cc = (n & 1) + 1;
 if (ctx->rgb)
-dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, 
STUDIO_INTRA_BITS, 2);
+dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, 
STUDIO_INTRA_BITS, 2);
 else
-dct_dc_size = get_vlc2(&s->gb, ctx->studio_chroma_dc.table, 
STUDIO_INTRA_BITS, 2);
+dct_dc_size = get_vlc2(&s->gb, studio_chroma_dc.table, 
STUDIO_INTRA_BITS, 2);
 quant_matrix = s->chroma_intra_matrix;
 }
 
@@ -1878,7 +1881,7 @@ static int mpeg4_decode_studio_block(MpegEncContext *s, 
int32_t block[64], int n
 }
 
 additional_code_len = ac_state_tab[group][0];
-cur_vlc = &ctx->studio_intra_tab[ac_state_tab[group][1]];
+cur_vlc = &studio_intra_tab[ac_state_tab[group][1]];
 
 if (group == 0) {
 /* End of Block */
@@ -3501,36 +3504,33 @@ static int mpeg4_update_thread_context(AVCodecContext 
*dst,
 }
 #endif
 
-static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
+static av_cold void init_studio_vlcs(void)
 {
-int i, ret;
-
-for (i = 0; i < 12; i++) {
-ret = ff_init_vlc_from_lengths(&ctx->studio_intra_tab[i],
-   STUDIO_INTRA_BITS, 24,
-   &ff_mpeg4_studio_intra[i][0][1], 2,
-   &ff_mpeg4_studio_intra[i][0][0], 2, 1,
-   0, 0);
-
-if (ret < 0)
-return ret;
-}
-
-ret = ff_init_vlc_from_lengths(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
-   &ff_mpeg4_studio_dc_luma[0][1], 2,
-   &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
-   0, 0);
-if (ret < 0)
-return ret;
-
-ret = ff_init_vlc_from_lengths(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 
19,
-   &ff_mpeg4_studio_dc_chroma[0][1], 2,
-   &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
-   0, 0);
-if (ret < 0)
-return ret;
-
-return 0;
+if (studio_chroma_dc.table_size)
+return;
+
+for (unsigned i = 0, offset = 0; i < 12; i++) {
+static VLC_TYPE vlc_buf[6498][2];
+
+studio_intra_tab[i].table   = &vlc_buf[offset];
+studio_intra_tab[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
+ff_init_vlc_from_lengths(&studio_intra_tab[i],
+ STUDIO_INTRA_BITS, 24,
+ &ff_mpeg4_studio_intra[i][0][1], 2,
+ &ff_mpeg4_studio_intra[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
+offset += studio_intra_tab[i].table_size;
+}
+
+INIT_VLC_STATIC_FROM_LENGTHS(&studio_luma_dc, STUDIO_INTRA_BITS, 19,
+ &ff_mpeg4_studio_dc_luma[0][1], 2,
+ &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
+

[FFmpeg-devel] [PATCH 067/114] avcodec/mpeg4video: Make tables used to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
Switching from ff_init_vlc_sparse() to ff_init_vlc_from_lengths()
allows to replace codes which are so long that they need to be stored
in an uint16_t by symbols which fit into an uint8_t; and even these can
be avoided in case of the sprite trajectory VLC.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mpeg4data.h | 150 +++--
 libavcodec/mpeg4video.h|   8 +-
 libavcodec/mpeg4videodec.c |  31 
 3 files changed, 80 insertions(+), 109 deletions(-)

diff --git a/libavcodec/mpeg4data.h b/libavcodec/mpeg4data.h
index 4756e9ea1d..30179d9801 100644
--- a/libavcodec/mpeg4data.h
+++ b/libavcodec/mpeg4data.h
@@ -323,10 +323,8 @@ RLTable ff_rvlc_rl_intra = {
 intra_rvlc_level,
 };
 
-const uint16_t ff_sprite_trajectory_tab[15][2] = {
- {0x00, 2}, {0x02, 3},  {0x03, 3},  {0x04, 3}, {0x05, 3}, {0x06, 3},
- {0x0E, 4}, {0x1E, 5},  {0x3E, 6},  {0x7E, 7}, {0xFE, 8},
- {0x1FE, 9},{0x3FE, 10},{0x7FE, 11},{0xFFE, 12},
+const uint8_t ff_sprite_trajectory_lens[15] = {
+2,  3,  3,  3,  3,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
 };
 
 const uint8_t ff_mb_type_b_tab[4][2] = {
@@ -374,119 +372,91 @@ const uint8_t ff_mpeg4_dc_threshold[8]={
 };
 
 /* Note these are different in studio mode */
-const uint16_t ff_mpeg4_studio_dc_luma[19][2]={
-{0x0e,  6}, {0x06,  5}, {0x00,  4}, {0x02,  4},
-{0x07,  3}, {0x05,  3}, {0x03,  3}, {0x02,  3},
-{0x04,  3}, {0x06,  3}, {0x01,  4}, {0x1e,  7},
-{0x3e,  8}, {0x7e,  9}, {0xfe, 10}, {0x1fe, 11},
-{0x3fe, 12}, {0x7fe, 13}, {0x7ff, 13}
+const uint8_t ff_mpeg4_studio_dc_luma[19][2] = {
+{  2,  4 }, { 10,  4 }, {  3,  4 }, {  1,  5 }, {  0,  6 }, { 11,  7 },
+{ 12,  8 }, { 13,  9 }, { 14, 10 }, { 15, 11 }, { 16, 12 }, { 17, 13 },
+{ 18, 13 }, {  7,  3 }, {  6,  3 }, {  8,  3 }, {  5,  3 }, {  9,  3 },
+{  4,  3 },
 };
 
-const uint16_t ff_mpeg4_studio_dc_chroma[19][2]={
-{0x00,  4}, {0x02,  4}, {0x07,  3}, {0x05,  3},
-{0x03,  3}, {0x02,  3}, {0x04,  3}, {0x06,  3},
-{0x01,  4}, {0x06,  5}, {0x0e,  6}, {0x1e,  7},
-{0x3e,  8}, {0x7e,  9}, {0xfe, 10}, {0x1fe, 11},
-{0x3fe, 12}, {0x7fe, 13}, {0x7ff, 13}
+const uint8_t ff_mpeg4_studio_dc_chroma[19][2] = {
+{  0,  4 }, {  8,  4 }, {  1,  4 }, {  9,  5 }, { 10,  6 }, { 11,  7 },
+{ 12,  8 }, { 13,  9 }, { 14, 10 }, { 15, 11 }, { 16, 12 }, { 17, 13 },
+{ 18, 13 }, {  5,  3 }, {  4,  3 }, {  6,  3 }, {  3,  3 }, {  7,  3 },
+{  2,  3 },
 };
 
-const uint16_t ff_mpeg4_studio_intra[12][22][2]={
+const uint8_t ff_mpeg4_studio_intra[12][24][2] = {
 {
-{0x05,  4}, {0x04,  4}, {0x05,  7}, {0x09,  9},
-{0x21, 11}, {0x41, 12}, {0x81, 13}, {0x03,  4},
-{0x03,  5}, {0x05,  6}, {0x04,  7}, {0x03,  7},
-{0x05,  8}, {0x03,  2}, {0x05,  3}, {0x04,  3},
-{0x03,  3}, {0x02,  4}, {0x04,  6}, {0x03,  6},
-{0x11, 10}, {0x80, 13}
+{  0, -6 }, { 21, 13 }, {  6, 13 }, {  5, 12 }, {  4, 11 }, { 20, 10 },
+{  3,  9 }, { 12,  8 }, { 11,  7 }, { 10,  7 }, {  2,  7 }, { 19,  6 },
+{ 18,  6 }, {  9,  6 }, {  8,  5 }, { 17,  4 }, {  7,  4 }, {  1,  4 },
+{  0,  4 }, { 16,  3 }, { 15,  3 }, { 14,  3 }, { 13,  2 },
 },
 {
-{0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
-{0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
-{0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
-{0x00,  0}, {0x00,  0}, {0x01,  1}, {0x01,  2},
-{0x01,  3}, {0x01,  4}, {0x01,  5}, {0x03,  7},
-{0x05,  8}, {0x04,  8}
+{  0, -6 }, { 21,  8 }, { 20,  8 }, { 19,  7 }, { 18,  5 }, { 17,  4 },
+{ 16,  3 }, { 15,  2 }, { 14,  1 },
 },
 {
-{0x05,  3},  {0x03,  5},  {0x02,  5},  {0x03,  7},
-{0x09,  9},  {0x103, 14}, {0x102, 14}, {0x04,  3},
-{0x03,  3},  {0x03,  4},  {0x02,  4},  {0x03,  6},
-{0x11, 10},  {0x03,  2},  {0x02,  3},  {0x02,  6},
-{0x05,  8},  {0x21, 11},  {0x83, 13},  {0x101, 14},
-{0x201, 15}, {0x82, 13}
+{  0, -6 }, {  0,-15 }, { 20, 15 }, { 19, 14 }, {  6, 14 }, {  5, 14 },
+{ 21, 13 }, { 18, 13 }, { 17, 11 }, { 12, 10 }, {  4,  9 }, { 16,  8 },
+{  3,  7 }, { 15,  6 }, { 11,  6 }, {  2,  5 }, {  1,  5 }, { 10,  4 },
+{  9,  4 }, { 14,  3 }, {  8,  3 }, {  7,  3 }, {  0,  3 }, { 13,  2 },
 },
 {
-{0x05,  5}, {0x05,  4}, {0x04,  5}, {0x03,  6},
-{0x09,  9}, {0x83, 13}, {0x82, 13}, {0x03,  3},
-{0x04,  4}, {0x03,  4}, {0x03,  5}, {0x05,  8},
-{0x81, 13}, {0x03,  2}, {0x02,  2}, {0x02,  5},
-{0x02,  6}, {0x03,  7}, {0x11, 10}, {0x43, 12},
-{0x80, 13}, {0x42, 12}
+{  0, -6 }, { 20, 13 }, { 12, 13 }, {  6, 13 }, {  5, 13 }, { 21, 12 },
+{ 19, 12 }, { 18, 10 }, {  4,  9 }, { 11,  8 }, { 17,  7 }, { 16,  6 },
+{  3,  6 }, { 15,  5 }, { 10,  5 }, {  2,  5 }, {  0,  5 }, {  9,  4 },
+{  8,  4 }, {  1,  4 }, {  7,  3 }, { 14,  2 }, { 13,  2 },
 },
 {
- 

[FFmpeg-devel] [PATCH 069/114] avcodec/truemotion2: Simplify creating VLC table

2020-11-10 Thread Andreas Rheinhardt
ff_init_vlc_from_lengths() can be used to offload the computation
of the codes; it also allows to omit the check whether the codes
are already properly ordered (they are). In this case, this also allows
to avoid the allocation of the buffer for the codes.

This improves performance: The amount of decicycles for one call to
tm2_build_huff_tables() when decoding tm20.avi from the FATE-suite
decreased from 46239 to 40035. This test consisted of looping 50 times
over the file and iterating the test ten times.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/truemotion2.c | 24 ++--
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c
index 0f4f345a6c..8c476a2285 100644
--- a/libavcodec/truemotion2.c
+++ b/libavcodec/truemotion2.c
@@ -108,15 +108,14 @@ typedef struct TM2Huff {
 int num; ///< current number filled
 int max_num; ///< total number of codes
 int *nums; ///< literals
-uint32_t *bits; ///< codes
-int *lens; ///< codelengths
+uint8_t *lens; ///< codelengths
 } TM2Huff;
 
 /**
  *
  * @returns the length of the longest code or an AVERROR code
  */
-static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff 
*huff)
+static int tm2_read_tree(TM2Context *ctx, int length, TM2Huff *huff)
 {
 int ret, ret2;
 if (length > huff->max_bits) {
@@ -134,14 +133,13 @@ static int tm2_read_tree(TM2Context *ctx, uint32_t 
prefix, int length, TM2Huff *
 return AVERROR_INVALIDDATA;
 }
 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
-huff->bits[huff->num] = prefix;
 huff->lens[huff->num] = length;
 huff->num++;
 return length;
 } else { /* non-terminal node */
-if ((ret2 = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
+if ((ret2 = tm2_read_tree(ctx, length + 1, huff)) < 0)
 return ret2;
-if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 
0)
+if ((ret = tm2_read_tree(ctx, length + 1, huff)) < 0)
 return ret;
 }
 return FFMAX(ret, ret2);
@@ -177,15 +175,14 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes 
*code)
 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
 huff.max_num = (huff.nodes + 1) >> 1;
 huff.nums= av_calloc(huff.max_num, sizeof(int));
-huff.bits= av_calloc(huff.max_num, sizeof(uint32_t));
-huff.lens= av_calloc(huff.max_num, sizeof(int));
+huff.lens= av_mallocz(huff.max_num);
 
-if (!huff.nums || !huff.bits || !huff.lens) {
+if (!huff.nums || !huff.lens) {
 res = AVERROR(ENOMEM);
 goto out;
 }
 
-res = tm2_read_tree(ctx, 0, 0, &huff);
+res = tm2_read_tree(ctx, 0, &huff);
 
 if (res >= 0 && res != huff.max_bits) {
 av_log(ctx->avctx, AV_LOG_ERROR, "Got less bits than expected: %i of 
%i\n",
@@ -200,9 +197,9 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes 
*code)
 
 /* convert codes to vlc_table */
 if (res >= 0) {
-res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
-   huff.lens, sizeof(int), sizeof(int),
-   huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
+res = ff_init_vlc_from_lengths(&code->vlc, huff.max_bits, huff.max_num,
+   huff.lens, sizeof(huff.lens[0]),
+   NULL, 0, 0, 0, 0);
 if (res < 0)
 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
 else {
@@ -216,7 +213,6 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes 
*code)
 out:
 /* free allocated memory */
 av_free(huff.nums);
-av_free(huff.bits);
 av_free(huff.lens);
 
 return res;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 072/114] avcodec/atrac3plus: Simplify getting offset of VLC in VLC_TYPE buf

2020-11-10 Thread Andreas Rheinhardt
The earlier code used several different offset parameters that were
initialized to magic values. This is unnecessary.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac3plus.c | 25 ++---
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index 955c9901a1..889526e338 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -75,7 +75,7 @@ static av_cold void build_canonical_huff(const uint8_t *cb, 
const uint8_t *xlat,
 
 av_cold void ff_atrac3p_init_vlcs(void)
 {
-int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
+int i, tab_offset = 0;
 
 static const uint8_t wl_nb_bits[4]  = { 2, 3, 5, 5 };
 static const uint8_t wl_nb_codes[4] = { 3, 5, 8, 8 };
@@ -127,39 +127,34 @@ av_cold void ff_atrac3p_init_vlcs(void)
 atrac3p_huff_freq_xlat
 };
 
-for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
-wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
+for (int i = 0; i < 4; i++) {
+wl_vlc_tabs[i].table = &tables_data[tab_offset];
 wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
-ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
-ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
-
+tab_offset += wl_vlc_tabs[i].table_allocated;
 ff_init_vlc_from_lengths(&wl_vlc_tabs[i], wl_nb_bits[i], 
wl_nb_codes[i],
  &wl_huffs[i][0][1], 2,
  &wl_huffs[i][0][0], 2, 1,
  0, INIT_VLC_USE_NEW_STATIC);
 
+ct_vlc_tabs[i].table = &tables_data[tab_offset];
+ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
+tab_offset += ct_vlc_tabs[i].table_allocated;
 ff_init_vlc_from_lengths(&ct_vlc_tabs[i], ct_nb_bits[i], 
ct_nb_codes[i],
  &ct_huffs[i][0][1], 2,
  &ct_huffs[i][0][0], 2, 1,
  0, INIT_VLC_USE_NEW_STATIC);
-
-wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
-ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
 }
 
-for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
-sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
+for (int i = 0; i < 8; i++) {
+sf_vlc_tabs[i].table = &tables_data[tab_offset];
 sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
-
+tab_offset += sf_vlc_tabs[i].table_allocated;
 ff_init_vlc_from_lengths(&sf_vlc_tabs[i], sf_nb_bits[i], 
sf_nb_codes[i],
  &sf_huffs[i][0][1], 2,
  &sf_huffs[i][0][0], 2, 1,
  0, INIT_VLC_USE_NEW_STATIC);
-sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
 }
 
-tab_offset = 2564;
-
 /* build huffman tables for spectrum decoding */
 for (i = 0; i < 112; i++) {
 if (atrac3p_spectra_tabs[i].redirect < 0)
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 071/114] avcodec/atrac3plus: Make tables used to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
The ATRAC3+ decoder currently uses ff_init_vlc_sparse() to initialize
several VLCs; sometimes a symbols table is used, sometimes not; some of
the codes tables are uint16_t, some are uint8_t. Because of these two
latter facts it makes sense to switch to ff_init_vlc_from_lengths()
because it allows to remove the codes at the cost of adding symbols
tables of type uint8_t in the cases where there were none before.

Notice that sometimes the same codes and lengths tables were reused with
two different symbols tables; this could have been preserved (meaning
one could use a lengths table twice), but hasn't, because this allows
to use only one pointer to both the symbols and lengths instead of two
pointers.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac3plus.c  |  72 ---
 libavcodec/atrac3plus_data.h | 226 ---
 2 files changed, 124 insertions(+), 174 deletions(-)

diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index 8ae5d6b8f9..955c9901a1 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -79,47 +79,24 @@ av_cold void ff_atrac3p_init_vlcs(void)
 
 static const uint8_t wl_nb_bits[4]  = { 2, 3, 5, 5 };
 static const uint8_t wl_nb_codes[4] = { 3, 5, 8, 8 };
-static const uint8_t * const wl_bits[4] = {
-atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
-atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
-};
-static const uint8_t * const wl_codes[4] = {
-atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
-atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
-};
-static const uint8_t * const wl_xlats[4] = {
-atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
+static const uint8_t (*const wl_huffs[4])[2] = {
+atrac3p_wl_huff1, atrac3p_wl_huff2,
+atrac3p_wl_huff3, atrac3p_wl_huff4
 };
 
 static const uint8_t ct_nb_bits[4]  = { 3, 4, 4, 4 };
 static const uint8_t ct_nb_codes[4] = { 4, 8, 8, 8 };
-static const uint8_t * const ct_bits[4]  = {
-atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
-atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
-};
-static const uint8_t * const ct_codes[4] = {
-atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
-atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
-};
-static const uint8_t * const ct_xlats[4] = {
-NULL, NULL, atrac3p_ct_huff_xlat1, NULL
+static const uint8_t (*const ct_huffs[4])[2]  = {
+atrac3p_ct_huff1, atrac3p_ct_huff2,
+atrac3p_ct_huff3, atrac3p_ct_huff4
 };
 
 static const uint8_t sf_nb_bits[8]  = {  9,  9,  9,  9,  6,  6,  7,  7 };
-static const uint8_t sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
-static const uint8_t  * const sf_bits[8]  = {
-atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
-atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
-atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
-};
-static const uint16_t * const sf_codes[8] = {
-atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
-atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
-atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
-};
-static const uint8_t  * const sf_xlats[8] = {
-atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
-atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
+static const uint8_t sf_nb_codes[8] = { 64, 64, 64, 64, 15, 15, 15, 15 };
+static const uint8_t  (*const sf_huffs[8])[2]  = {
+atrac3p_sf_huff1, atrac3p_sf_huff2, atrac3p_sf_huff3,
+atrac3p_sf_huff4, atrac3p_sf_huff5, atrac3p_sf_huff6,
+atrac3p_sf_huff7, atrac3p_sf_huff8
 };
 
 static const uint8_t * const gain_cbs[11] = {
@@ -156,17 +133,15 @@ av_cold void ff_atrac3p_init_vlcs(void)
 ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
 ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
 
-ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
-   wl_bits[i],  1, 1,
-   wl_codes[i], 1, 1,
-   wl_xlats[i], 1, 1,
-   INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&wl_vlc_tabs[i], wl_nb_bits[i], 
wl_nb_codes[i],
+ &wl_huffs[i][0][1], 2,
+ &wl_huffs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 
-ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
-   ct_bits[i],  1, 1,
-   ct_codes[i], 1, 1,
-   ct_xlats[i], 1, 1,
-   INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ct_vlc_tabs[i], ct_nb_bits[i], 
ct_nb_codes[i],
+ &ct_huffs[i][0][1], 2,
+ &ct_huffs[i][0][0

[FFmpeg-devel] [PATCH 074/114] avcodec/atrac3plus: Combine codebooks into one array

2020-11-10 Thread Andreas Rheinhardt
ATRAC3+ uses VLCs whose code lengths are ascending from left to right in
the tree; ergo it is possible (and done) to run-length encode the
lengths into so-called codebooks. These codebooks were variable-sized:
The first byte contained the minimum length of a code, the second the
maximum length; this was followed by max - min bytes containing the
actual numbers. The minimal min was 1, the maximal max 12.

While one saves a few bytes by only containing the range that is
actually used, this is more than offset by the fact that there needs
to be a pointer to each of these codebooks.

Furthermore, since 5f8de7b74147e2a347481d7bc900ebecba6f340f the content
of the Atrac3pSpecCodeTab structure (containing data for spectrum
decoding) can be cleanly separated into fields that are only used during
initialization and fields used during actual decoding: The pointers to
the codebooks and the field indicating whether an earlier codebook should
be reused constitute the former category. Therefore the new codebooks are
not placed into the Atrac3pSpecCodeTab (which is now unused during
init), but in an array of its own. The information whether an earlier
codebook should be reused is encoded in the first number of each
spectrum codebook: If it is negative, an earlier codebook (given by the
number) should be reused.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac3plus.c  |  35 +-
 libavcodec/atrac3plus_data.h | 645 ++-
 2 files changed, 257 insertions(+), 423 deletions(-)

diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index dbecb7c21e..43600f12bc 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -50,19 +50,18 @@ static VLC tone_vlc_tabs[7];
 static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t 
**xlat,
  int *tab_offset, VLC *out_vlc)
 {
-int i, b;
+int i, max_len;
 uint8_t bits[256];
 int index = 0;
-int min_len = *cb++; // get shortest codeword length
-int max_len = *cb++; // get longest  codeword length
 
-for (b = min_len; b <= max_len; b++) {
+for (int b = 1; b <= 12; b++) {
 for (i = *cb++; i > 0; i--) {
 av_assert0(index < 256);
 bits[index]  = b;
 index++;
 }
 }
+max_len = bits[index - 1];
 
 out_vlc->table = &tables_data[*tab_offset];
 out_vlc->table_allocated = 1 << max_len;
@@ -101,22 +100,6 @@ av_cold void ff_atrac3p_init_vlcs(void)
 atrac3p_sf_huff7, atrac3p_sf_huff8
 };
 
-static const uint8_t * const gain_cbs[11] = {
-atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
-atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
-atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
-atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
-atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
-atrac3p_huff_gain_loc5_cb
-};
-
-static const uint8_t * const tone_cbs[7] = {
-atrac3p_huff_tonebands_cb,  atrac3p_huff_numwavs1_cb,
-atrac3p_huff_numwavs2_cb,   atrac3p_huff_wav_ampsf1_cb,
-atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
-atrac3p_huff_freq_cb
-};
-
 for (int i = 0; i < 4; i++) {
 wl_vlc_tabs[i].table = &tables_data[tab_offset];
 wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
@@ -148,22 +131,24 @@ av_cold void ff_atrac3p_init_vlcs(void)
 /* build huffman tables for spectrum decoding */
 xlats = atrac3p_spectra_xlats;
 for (i = 0; i < 112; i++) {
-if (atrac3p_spectra_tabs[i].redirect < 0)
-build_canonical_huff(atrac3p_spectra_tabs[i].cb,
+if (atrac3p_spectra_cbs[i][0] >= 0)
+build_canonical_huff(atrac3p_spectra_cbs[i],
  &xlats, &tab_offset, &spec_vlc_tabs[i]);
 else /* Reuse already initialized VLC table */
-spec_vlc_tabs[i] = spec_vlc_tabs[atrac3p_spectra_tabs[i].redirect];
+spec_vlc_tabs[i] = spec_vlc_tabs[-atrac3p_spectra_cbs[i][0]];
 }
 
 /* build huffman tables for gain data decoding */
 xlats = atrac3p_gain_xlats;
 for (i = 0; i < 11; i++)
-build_canonical_huff(gain_cbs[i], &xlats, &tab_offset, 
&gain_vlc_tabs[i]);
+build_canonical_huff(atrac3p_gain_cbs[i], &xlats,
+ &tab_offset, &gain_vlc_tabs[i]);
 
 /* build huffman tables for tone decoding */
 xlats = atrac3p_tone_xlats;
 for (i = 0; i < 7; i++)
-build_canonical_huff(tone_cbs[i], &xlats, &tab_offset, 
&tone_vlc_tabs[i]);
+build_canonical_huff(atrac3p_tone_cbs[i], &xlats,
+ &tab_offset, &tone_vlc_tabs[i]);
 }
 
 /**
diff --git a/libavcodec/atrac3plus_data.h b/libavcodec/atrac3plus_data.h
index 61f4b0a038..6b9109cb70 100644
--- a/libavcodec/atrac3plus_data.h
+++ b/libavcodec/atrac3plus_data.h
@@ -413,254 +413,122 @@ static const uint8_t 
at

[FFmpeg-devel] [PATCH 075/114] avcodec/atrac3plus: Run-length encode length tables to make them smaller

2020-11-10 Thread Andreas Rheinhardt
This is very beneficial for the scale factor tables where 4*64+4*15
bytes of length information can be replaced by eight codebooks of 12
bytes each; furthermore the number of codes as well as the maximum
length of a code can be easily derived from said codebooks, making
tables containing said information superfluous. This and combining the
symbols into one big array also made an array of pointers to the tables
redundant.

For the wordlen and code table tables the benefits are not that big
(given these tables don't contain that many elements), but all in all
using codebooks is also advantageouos for them. Therefore it has been
done.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac3plus.c  |  55 ++--
 libavcodec/atrac3plus_data.h | 157 +++
 2 files changed, 76 insertions(+), 136 deletions(-)

diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index 43600f12bc..a5abc37606 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -78,55 +78,18 @@ av_cold void ff_atrac3p_init_vlcs(void)
 int i, tab_offset = 0;
 const uint8_t *xlats;
 
-static const uint8_t wl_nb_bits[4]  = { 2, 3, 5, 5 };
-static const uint8_t wl_nb_codes[4] = { 3, 5, 8, 8 };
-static const uint8_t (*const wl_huffs[4])[2] = {
-atrac3p_wl_huff1, atrac3p_wl_huff2,
-atrac3p_wl_huff3, atrac3p_wl_huff4
-};
-
-static const uint8_t ct_nb_bits[4]  = { 3, 4, 4, 4 };
-static const uint8_t ct_nb_codes[4] = { 4, 8, 8, 8 };
-static const uint8_t (*const ct_huffs[4])[2]  = {
-atrac3p_ct_huff1, atrac3p_ct_huff2,
-atrac3p_ct_huff3, atrac3p_ct_huff4
-};
-
-static const uint8_t sf_nb_bits[8]  = {  9,  9,  9,  9,  6,  6,  7,  7 };
-static const uint8_t sf_nb_codes[8] = { 64, 64, 64, 64, 15, 15, 15, 15 };
-static const uint8_t  (*const sf_huffs[8])[2]  = {
-atrac3p_sf_huff1, atrac3p_sf_huff2, atrac3p_sf_huff3,
-atrac3p_sf_huff4, atrac3p_sf_huff5, atrac3p_sf_huff6,
-atrac3p_sf_huff7, atrac3p_sf_huff8
-};
-
+xlats = atrac3p_wl_ct_xlats;
 for (int i = 0; i < 4; i++) {
-wl_vlc_tabs[i].table = &tables_data[tab_offset];
-wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
-tab_offset += wl_vlc_tabs[i].table_allocated;
-ff_init_vlc_from_lengths(&wl_vlc_tabs[i], wl_nb_bits[i], 
wl_nb_codes[i],
- &wl_huffs[i][0][1], 2,
- &wl_huffs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
-
-ct_vlc_tabs[i].table = &tables_data[tab_offset];
-ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
-tab_offset += ct_vlc_tabs[i].table_allocated;
-ff_init_vlc_from_lengths(&ct_vlc_tabs[i], ct_nb_bits[i], 
ct_nb_codes[i],
- &ct_huffs[i][0][1], 2,
- &ct_huffs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
+build_canonical_huff(atrac3p_wl_cbs[i], &xlats,
+ &tab_offset, &wl_vlc_tabs[i]);
+build_canonical_huff(atrac3p_ct_cbs[i], &xlats,
+ &tab_offset, &ct_vlc_tabs[i]);
 }
 
-for (int i = 0; i < 8; i++) {
-sf_vlc_tabs[i].table = &tables_data[tab_offset];
-sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
-tab_offset += sf_vlc_tabs[i].table_allocated;
-ff_init_vlc_from_lengths(&sf_vlc_tabs[i], sf_nb_bits[i], 
sf_nb_codes[i],
- &sf_huffs[i][0][1], 2,
- &sf_huffs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
-}
+xlats = atrac3p_sf_xlats;
+for (int i = 0; i < 8; i++)
+build_canonical_huff(atrac3p_sf_cbs[i], &xlats,
+ &tab_offset, &sf_vlc_tabs[i]);
 
 /* build huffman tables for spectrum decoding */
 xlats = atrac3p_spectra_xlats;
diff --git a/libavcodec/atrac3plus_data.h b/libavcodec/atrac3plus_data.h
index 6b9109cb70..7039936ba3 100644
--- a/libavcodec/atrac3plus_data.h
+++ b/libavcodec/atrac3plus_data.h
@@ -27,106 +27,83 @@
 #include 
 
 /** VLC tables for wordlen */
-static const uint8_t atrac3p_wl_huff1[3][2] = {
-{ 0, 1 }, { 1, 2 }, { 7, 2 },
+static const uint8_t atrac3p_wl_cbs[][12] = {
+{  1,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 },
+{  1,  0,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0 },
+{  1,  0,  2,  3,  2,  0,  0,  0,  0,  0,  0,  0 },
+{  1,  0,  2,  3,  2,  0,  0,  0,  0,  0,  0,  0 },
 };
 
-static const uint8_t atrac3p_wl_huff2[5][2] = {
-{ 0, 1 }, { 1, 3 }, { 2, 3 }, { 6, 3 }, { 7, 3 },
-};
-
-static const uint8_t atrac3p_wl_huff3[8][2] = {
-{ 0, 1 }, { 1, 3 }, { 7, 3 }, { 2, 4 }, { 5, 4 }, { 6, 4 }, { 3, 5 },
-{ 4, 5 },
+/** VLC tables for code table indexes */
+static const uint8_t atrac3p_ct_cbs[][12] = {
+{  1,  1,  2,  0,  0,  0,  0,  0,

[FFmpeg-devel] [PATCH 076/114] avcodec/vc1: Already offset subblock VLC tables during init

2020-11-10 Thread Andreas Rheinhardt
Offseting by +1 is possible without adding dummy elements to the
beginning of the codes and lengths tables by switching to
ff_init_vlc_from_lengths() as this allows one to set the symbols
arbitrarily without incurring any penalty.

Signed-off-by: Andreas Rheinhardt 
---
I wonder whether the tables used to initialize VC1 VLCs should not be
put into a header of their own that is only included by vc1.c (the only
user of them) instead of vc1data.c. Given that I already touch all of
them, I could do it. What do other think of this?

 libavcodec/vc1.c   |  7 ---
 libavcodec/vc1_block.c |  2 +-
 libavcodec/vc1data.c   | 27 +--
 libavcodec/vc1data.h   |  5 ++---
 4 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 9df778bcab..68180dc797 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1616,9 +1616,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
  ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
 ff_vc1_subblkpat_vlc[i].table   = &vlc_table[vlc_offs[i * 
3 + 2]];
 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i * 3 + 3] - 
vlc_offs[i * 3 + 2];
-init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
- ff_vc1_subblkpat_bits[i], 1, 1,
- ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_subblkpat_vlc[i], 
VC1_SUBBLKPAT_VLC_BITS, 15,
+ &ff_vc1_subblkpat_tabs[i][0][1], 2,
+ &ff_vc1_subblkpat_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 }
 for (i = 0; i < 4; i++) {
 ff_vc1_4mv_block_pattern_vlc[i].table   = 
&vlc_table[vlc_offs[i * 3 + 9]];
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 5c33170933..b8987b0015 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -1143,7 +1143,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
 ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, 
ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
 }
 if (ttblk == TT_4X4) {
-subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, 
VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
+subblkpat = ~get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, 
VC1_SUBBLKPAT_VLC_BITS, 1);
 }
 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
diff --git a/libavcodec/vc1data.c b/libavcodec/vc1data.c
index 19f1cad45f..e92d9b7310 100644
--- a/libavcodec/vc1data.c
+++ b/libavcodec/vc1data.c
@@ -921,16 +921,23 @@ const uint8_t ff_vc1_ttblk_bits[3][8] = {
 {  2,  3,  3,  3,  3,  3,  4,  4 }
 };
 
-/* SUBBLKPAT tables, p93-94, reordered */
-const uint8_t ff_vc1_subblkpat_codes[3][15] = {
-{ 14, 12,  7, 11,  9, 26,  2, 10, 27,  8,  0,  6,  1, 15,  1 },
-{ 14,  0,  8, 15, 10,  4, 23, 13,  5,  9, 25,  3, 24, 22,  1 },
-{  5,  6,  2,  2,  8,  0, 28,  3,  1,  3, 29,  1, 19, 18, 15 }
-};
-const uint8_t ff_vc1_subblkpat_bits[3][15] = {
-{  5,  5,  5,  5,  5,  6,  4,  5,  6,  5,  4,  5,  4,  5,  1},
-{  4,  3,  4,  4,  4,  5,  5,  4,  5,  4,  5,  4,  5,  5,  2},
-{  3,  3,  4,  3,  4,  5,  5,  3,  5,  4,  5,  4,  5,  5,  4}
+/* SUBBLKPAT tables, p93-94, reordered and offset by 1 */
+const uint8_t ff_vc1_subblkpat_tabs[3][15][2] = {
+{
+{ 0x0B, 4 }, { 0x0D, 4 }, { 0x07, 4 }, { 0x0C, 5 }, { 0x03, 5 },
+{ 0x0A, 5 }, { 0x05, 5 }, { 0x08, 5 }, { 0x04, 5 }, { 0x02, 5 },
+{ 0x06, 6 }, { 0x09, 6 }, { 0x01, 5 }, { 0x0E, 5 }, { 0x0F, 1 },
+},
+{
+{ 0x02, 3 }, { 0x06, 5 }, { 0x09, 5 }, { 0x0C, 4 }, { 0x0F, 2 },
+{ 0x03, 4 }, { 0x0A, 4 }, { 0x05, 4 }, { 0x0E, 5 }, { 0x07, 5 },
+{ 0x0D, 5 }, { 0x0B, 5 }, { 0x08, 4 }, { 0x01, 4 }, { 0x04, 4 },
+},
+{
+{ 0x06, 5 }, { 0x09, 5 }, { 0x0C, 4 }, { 0x03, 4 }, { 0x0A, 4 },
+{ 0x04, 3 }, { 0x08, 3 }, { 0x05, 4 }, { 0x0E, 5 }, { 0x0D, 5 },
+{ 0x01, 3 }, { 0x02, 3 }, { 0x07, 5 }, { 0x0B, 5 }, { 0x0F, 4 },
+}
 };
 
 /* MV differential tables, p265 */
diff --git a/libavcodec/vc1data.h b/libavcodec/vc1data.h
index 90dd8baf61..6b141b0f76 100644
--- a/libavcodec/vc1data.h
+++ b/libavcodec/vc1data.h
@@ -152,9 +152,8 @@ extern const uint8_t ff_vc1_ttmb_bits[3][16];
 extern const uint8_t ff_vc1_ttblk_codes[3][8];
 extern const uint8_t ff_vc1_ttblk_bits[3][8];
 
-/* SUBBLKPAT tables, p93-94, reordered */
-extern const uint8_t ff_vc1_subblkpat_codes[3][15];
-extern const uint8_t ff_vc1_subblkpat_bits[3][15];
+/* SUBBLKPAT tables, p93-94, reordered and offset by 1 */
+extern const uint8_t ff_vc1_subblkpat_tabs[3][15][2];
 
 /* MV differential tables, p265 */
 extern const uint16_t ff_vc1_mv_diff_codes[4][73];
-- 
2.25.1

___

[FFmpeg-devel] [PATCH 079/114] avcodec/vc1_block, msmpeg4dec: Don't check for errors for complete VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/msmpeg4dec.c |  5 -
 libavcodec/vc1_block.c  | 12 
 2 files changed, 17 deletions(-)

diff --git a/libavcodec/msmpeg4dec.c b/libavcodec/msmpeg4dec.c
index 49df06a9d7..b3964457d6 100644
--- a/libavcodec/msmpeg4dec.c
+++ b/libavcodec/msmpeg4dec.c
@@ -614,11 +614,6 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, 
int *dir_ptr)
 } else {
 level = get_vlc2(&s->gb, 
ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
 }
-if (level < 0){
-av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
-*dir_ptr = 0;
-return -1;
-}
 
 if (level == DC_MAX) {
 level = get_bits(&s->gb, 8);
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index ed6df541cf..714b3cdea0 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -584,10 +584,6 @@ static int vc1_decode_i_block(VC1Context *v, int16_t 
block[64], int n,
 } else {
 dcdiff = get_vlc2(&s->gb, 
ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
 }
-if (dcdiff < 0) {
-av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
-return -1;
-}
 if (dcdiff) {
 const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
 if (dcdiff == 119 /* ESC index value */) {
@@ -732,10 +728,6 @@ static int vc1_decode_i_block_adv(VC1Context *v, int16_t 
block[64], int n,
 } else {
 dcdiff = get_vlc2(&s->gb, 
ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
 }
-if (dcdiff < 0) {
-av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
-return -1;
-}
 if (dcdiff) {
 const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
 if (dcdiff == 119 /* ESC index value */) {
@@ -936,10 +928,6 @@ static int vc1_decode_intra_block(VC1Context *v, int16_t 
block[64], int n,
 } else {
 dcdiff = get_vlc2(&s->gb, 
ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
 }
-if (dcdiff < 0) {
-av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
-return -1;
-}
 if (dcdiff) {
 const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
 if (dcdiff == 119 /* ESC index value */) {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 073/114] avcodec/atrac3plus: Combine symbols tables

2020-11-10 Thread Andreas Rheinhardt
This allows to remove lots of pointers (130) to small symbol tables;
it has the downside that some of the default tables must now be coded
explicitly, but this costs only 6 + 4 + 8 + 16 + 8 bytes and is therefore
dwarfed by the gains.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac3plus.c  |   30 +-
 libavcodec/atrac3plus_data.h | 1542 +-
 2 files changed, 763 insertions(+), 809 deletions(-)

diff --git a/libavcodec/atrac3plus.c b/libavcodec/atrac3plus.c
index 889526e338..dbecb7c21e 100644
--- a/libavcodec/atrac3plus.c
+++ b/libavcodec/atrac3plus.c
@@ -43,11 +43,11 @@ static VLC tone_vlc_tabs[7];
  * Generate canonical VLC table from given descriptor.
  *
  * @param[in] cb  ptr to codebook descriptor
- * @param[in] xlatptr to translation table or NULL
+ * @param[in,out] xlatptr to ptr to translation table
  * @param[in,out] tab_offset  starting offset to the generated vlc table
  * @param[out]out_vlc ptr to vlc table to be generated
  */
-static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t 
*xlat,
+static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t 
**xlat,
  int *tab_offset, VLC *out_vlc)
 {
 int i, b;
@@ -68,14 +68,16 @@ static av_cold void build_canonical_huff(const uint8_t *cb, 
const uint8_t *xlat,
 out_vlc->table_allocated = 1 << max_len;
 
 ff_init_vlc_from_lengths(out_vlc, max_len, index, bits, 1,
- xlat, 1, 1, 0, INIT_VLC_USE_NEW_STATIC);
+ *xlat, 1, 1, 0, INIT_VLC_USE_NEW_STATIC);
 
 *tab_offset += 1 << max_len;
+*xlat   += index;
 }
 
 av_cold void ff_atrac3p_init_vlcs(void)
 {
 int i, tab_offset = 0;
+const uint8_t *xlats;
 
 static const uint8_t wl_nb_bits[4]  = { 2, 3, 5, 5 };
 static const uint8_t wl_nb_codes[4] = { 3, 5, 8, 8 };
@@ -107,13 +109,6 @@ av_cold void ff_atrac3p_init_vlcs(void)
 atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
 atrac3p_huff_gain_loc5_cb
 };
-static const uint8_t * const gain_xlats[11] = {
-NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
-atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
-atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
-atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
-atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
-};
 
 static const uint8_t * const tone_cbs[7] = {
 atrac3p_huff_tonebands_cb,  atrac3p_huff_numwavs1_cb,
@@ -121,11 +116,6 @@ av_cold void ff_atrac3p_init_vlcs(void)
 atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
 atrac3p_huff_freq_cb
 };
-static const uint8_t * const tone_xlats[7] = {
-NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
-atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
-atrac3p_huff_freq_xlat
-};
 
 for (int i = 0; i < 4; i++) {
 wl_vlc_tabs[i].table = &tables_data[tab_offset];
@@ -156,22 +146,24 @@ av_cold void ff_atrac3p_init_vlcs(void)
 }
 
 /* build huffman tables for spectrum decoding */
+xlats = atrac3p_spectra_xlats;
 for (i = 0; i < 112; i++) {
 if (atrac3p_spectra_tabs[i].redirect < 0)
 build_canonical_huff(atrac3p_spectra_tabs[i].cb,
- atrac3p_spectra_tabs[i].xlat,
- &tab_offset, &spec_vlc_tabs[i]);
+ &xlats, &tab_offset, &spec_vlc_tabs[i]);
 else /* Reuse already initialized VLC table */
 spec_vlc_tabs[i] = spec_vlc_tabs[atrac3p_spectra_tabs[i].redirect];
 }
 
 /* build huffman tables for gain data decoding */
+xlats = atrac3p_gain_xlats;
 for (i = 0; i < 11; i++)
-build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, 
&gain_vlc_tabs[i]);
+build_canonical_huff(gain_cbs[i], &xlats, &tab_offset, 
&gain_vlc_tabs[i]);
 
 /* build huffman tables for tone decoding */
+xlats = atrac3p_tone_xlats;
 for (i = 0; i < 7; i++)
-build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, 
&tone_vlc_tabs[i]);
+build_canonical_huff(tone_cbs[i], &xlats, &tab_offset, 
&tone_vlc_tabs[i]);
 }
 
 /**
diff --git a/libavcodec/atrac3plus_data.h b/libavcodec/atrac3plus_data.h
index 418735abc5..61f4b0a038 100644
--- a/libavcodec/atrac3plus_data.h
+++ b/libavcodec/atrac3plus_data.h
@@ -417,48 +417,278 @@ static const uint8_t huff_a01_cb[14] = {
 1, 12, 1, 0, 0, 1, 7, 0, 19, 5, 13, 21, 6, 8
 };
 
-static const uint8_t huff_a01_xlat[81] = {
+static const uint8_t huff_a02_cb[13] = {
+2, 12, 1, 0, 4, 11, 0, 1, 29, 6, 20, 7, 2
+};
+
+static const uint8_t huff_a03_cb[9] = { 3, 9, 1, 8, 0, 13, 18, 7, 2 };
+
+static const uint8_t huff_a04_cb[4]   = { 2, 3, 2, 4 };
+
+static const uint8_t huff_a05_cb[12] = {
+

[FFmpeg-devel] [PATCH 077/114] avcodec/vc1: Use symbols table for VLC for Block-level Transform Type

2020-11-10 Thread Andreas Rheinhardt
Expressions like array[get_vlc2()] can be optimized by using a symbols
table if the array is always the same for a given VLC. This requirement
is fulfilled for the VLC for the Block-level Transform Type and
therefore this commit implements this; switching from
ff_init_vlc_sparse() to ff_init_vlc_from_lengths() means that we can
also remove the codes table in the process which had the same size as
the new symbols table; the array used to translate the get_vlc2() values
to the desired form has also been removed, saving space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c   |  7 ---
 libavcodec/vc1_block.c |  2 +-
 libavcodec/vc1data.c   | 30 +-
 libavcodec/vc1data.h   |  6 +-
 4 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 68180dc797..68855ae1d5 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1611,9 +1611,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
  ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
 ff_vc1_ttblk_vlc[i].table   = &vlc_table[vlc_offs[i * 3 + 
1]];
 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i * 3 + 2] - 
vlc_offs[i * 3 + 1];
-init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
- ff_vc1_ttblk_bits[i], 1, 1,
- ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 
8,
+ &ff_vc1_ttblk_tabs[i][0][1], 2,
+ &ff_vc1_ttblk_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 ff_vc1_subblkpat_vlc[i].table   = &vlc_table[vlc_offs[i * 
3 + 2]];
 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i * 3 + 3] - 
vlc_offs[i * 3 + 2];
 ff_init_vlc_from_lengths(&ff_vc1_subblkpat_vlc[i], 
VC1_SUBBLKPAT_VLC_BITS, 15,
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index b8987b0015..da58a0738f 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -1140,7 +1140,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
 s->bdsp.clear_block(block);
 
 if (ttmb == -1) {
-ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, 
ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
+ttblk = get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, 
VC1_TTBLK_VLC_BITS, 1);
 }
 if (ttblk == TT_4X4) {
 subblkpat = ~get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, 
VC1_SUBBLKPAT_VLC_BITS, 1);
diff --git a/libavcodec/vc1data.c b/libavcodec/vc1data.c
index e92d9b7310..ed5e680527 100644
--- a/libavcodec/vc1data.c
+++ b/libavcodec/vc1data.c
@@ -30,13 +30,6 @@
 #include "vc1.h"
 #include "vc1data.h"
 
-/** Table for conversion between TTBLK and TTMB */
-const int ff_vc1_ttblk_to_tt[3][8] = {
-{ TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, 
TT_4X8_LEFT },
-{ TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, 
TT_8X4_BOTTOM, TT_8X4_TOP },
-{ TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, 
TT_8X4, TT_8X4_TOP }
-};
-
 const int ff_vc1_ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
 
 /** MV P mode - the 5th element is only used for mode 1 */
@@ -908,17 +901,20 @@ const uint8_t ff_vc1_ttmb_bits[3][16] = {
 11
   }
 };
-
 /* TTBLK (Transform Type per Block) tables */
-const uint8_t ff_vc1_ttblk_codes[3][8] = {
-{  0,  1,  3,  5, 16, 17, 18, 19 },
-{  3,  0,  1,  2,  3,  5,  8,  9 },
-{  1,  0,  1,  4,  6,  7, 10, 11 }
-};
-const uint8_t ff_vc1_ttblk_bits[3][8] = {
-{  2,  2,  2,  3,  5,  5,  5,  5 },
-{  2,  3,  3,  3,  3,  3,  4,  4 },
-{  2,  3,  3,  3,  3,  3,  4,  4 }
+const uint8_t ff_vc1_ttblk_tabs[3][8][2] = {
+{
+{ TT_8X4, 2 }, { TT_4X8, 2 }, { TT_8X4_TOP, 5 }, { TT_8X4_BOTTOM, 5 },
+{ TT_4X8_RIGHT, 5 }, { TT_4X8_LEFT, 5 }, { TT_4X4, 3 }, { TT_8X8, 2 },
+},
+{
+{ TT_4X8_RIGHT, 3 }, { TT_4X8_LEFT, 3 }, { TT_4X4, 3 }, { TT_8X4, 3 },
+{ TT_8X4_BOTTOM, 4 }, { TT_8X4_TOP, 4 }, { TT_4X8, 3 }, { TT_8X8, 2 },
+},
+{
+{ TT_4X8, 3 }, { TT_4X4, 3 }, { TT_8X8, 2 }, { TT_8X4_BOTTOM, 3 },
+{ TT_8X4, 4 }, { TT_8X4_TOP, 4 }, { TT_4X8_RIGHT, 3 }, { TT_4X8_LEFT, 
3 },
+},
 };
 
 /* SUBBLKPAT tables, p93-94, reordered and offset by 1 */
diff --git a/libavcodec/vc1data.h b/libavcodec/vc1data.h
index 6b141b0f76..92d6374cb2 100644
--- a/libavcodec/vc1data.h
+++ b/libavcodec/vc1data.h
@@ -34,9 +34,6 @@
 
 #include "vlc.h"
 
-/** Table for conversion between TTBLK and TTMB */
-extern const int ff_vc1_ttblk_to_tt[3][8];
-
 extern const int ff_vc1_ttfrm_to_tt[4];
 
 /** MV P mode - the 5th element is only used for mode 1 */
@@ -149,8 +146,7 @@ extern const uint16_t ff_vc1_ttmb_codes[3][16];
 extern const uint8_t ff_vc1_ttmb_bits

[FFmpeg-devel] [PATCH 080/114] avcodec/vc1_block: Don't use 0 for NULL

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1_block.c | 20 ++--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 714b3cdea0..3c9d1d4b8d 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -1633,7 +1633,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v)
 for (i = 0; i < 4; i++) {
 dmv_x = dmv_y = 0;
 if (mvbp & (8 >> i))
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, 
v->range_y, v->mb_type[0], 0);
 ff_vc1_mc_4mv_luma(v, i, 0, 0);
 }
@@ -1642,14 +1642,14 @@ static int vc1_decode_p_mb_intfr(VC1Context *v)
 mvbp  = v->twomvbp;
 dmv_x = dmv_y = 0;
 if (mvbp & 2) {
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 }
 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, 
v->range_y, v->mb_type[0], 0);
 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
 dmv_x = dmv_y = 0;
 if (mvbp & 1) {
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 }
 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, 
v->range_y, v->mb_type[0], 0);
 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
@@ -1659,7 +1659,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v)
 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
 dmv_x = dmv_y = 0;
 if (mvbp) {
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 }
 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, 
v->range_y, v->mb_type[0], 0);
 ff_vc1_mc_1mv(v, 0);
@@ -2352,7 +2352,7 @@ static int vc1_decode_b_mb_intfr(VC1Context *v)
 dmv_x = dmv_y = 0;
 val = ((mvbp >> (3 - i)) & 1);
 if (val)
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 j = i > 1 ? 2 : 0;
 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, 
v->range_y, v->mb_type[0], dir);
 ff_vc1_mc_4mv_luma(v, j, dir, dir);
@@ -2365,14 +2365,14 @@ static int vc1_decode_b_mb_intfr(VC1Context *v)
 mvbp = v->twomvbp;
 dmv_x = dmv_y = 0;
 if (mvbp & 2)
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 
 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, 
v->range_y, v->mb_type[0], 0);
 ff_vc1_mc_1mv(v, 0);
 
 dmv_x = dmv_y = 0;
 if (mvbp & 1)
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 
 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, 
v->range_y, v->mb_type[0], 1);
 ff_vc1_interp_mc(v);
@@ -2384,12 +2384,12 @@ static int vc1_decode_b_mb_intfr(VC1Context *v)
 mvbp = v->twomvbp;
 dmv_x = dmv_y = 0;
 if (mvbp & 2)
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, 
v->range_y, v->mb_type[0], dir);
 
 dmv_x = dmv_y = 0;
 if (mvbp & 1)
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, 
v->range_y, v->mb_type[0], dir2);
 
 if (mvsw) {
@@ -2415,7 +2415,7 @@ static int vc1_decode_b_mb_intfr(VC1Context *v)
 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
 dmv_x = dmv_y = 0;
 if (mvbp)
-get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+get_mvdata_interlaced(v, &dmv_x, &dmv_y, NULL);
 
 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, 
v->range_y, v->mb_type[0], dir);
 v->blk_mv_type[s->block_index[0]] = 1;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

[FFmpeg-devel] [PATCH 078/114] avcodec/vc1: Use a symbols table to decode interlaced MV vector VLCs

2020-11-10 Thread Andreas Rheinhardt
The VC1 decoder currently follows the spec very closely when decoding
interlaced MV vector variable-length codes: The spec uses tables with
contiguous indexes. The last of these is an escape code, for every other
two values are encoded via (index + 1) / 9 and (index + 1) % 9. The
decoder did exactly this. This necessitated a branch to find out what
the current escape code was as well as the actual division/modulo.

This commit changes this by using an explicit symbols table for the
VLCs. The symbols are chosen so that the escape code always has the
value 0 (obviating the branch) and so that for every other code the
low nibble of the corresponding symbol contains (index + 1) % 9 whereas
the high nibble contains (index + 1) / 9.

Furthermore, the VLCs are now initialized with ff_init_vlc_from_lengths();
this allows to replace the uint32_t tables of codes by uint8_t tables of
symbols, thereby saving space on top of all the other improvements.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c   |  14 +-
 libavcodec/vc1_block.c |  16 +-
 libavcodec/vc1data.c   | 594 -
 libavcodec/vc1data.h   |  12 +-
 4 files changed, 313 insertions(+), 323 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 68855ae1d5..2e39870057 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1648,9 +1648,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
 /* initialize interlaced MVDATA tables (2-Ref) */
 ff_vc1_2ref_mvdata_vlc[i].table   = &vlc_table[vlc_offs[i 
* 2 + 22]];
 ff_vc1_2ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 2 + 23] - 
vlc_offs[i * 2 + 22];
-init_vlc(&ff_vc1_2ref_mvdata_vlc[i], VC1_2REF_MVDATA_VLC_BITS, 126,
- ff_vc1_2ref_mvdata_bits[i], 1, 1,
- ff_vc1_2ref_mvdata_codes[i], 4, 4, 
INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_2ref_mvdata_vlc[i], 
VC1_2REF_MVDATA_VLC_BITS, 126,
+ &ff_vc1_2ref_mvdata_tabs[i][0][1], 2,
+ &ff_vc1_2ref_mvdata_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 }
 for (i = 0; i < 4; i++) {
 /* initialize 4MV MBMODE VLC tables for interlaced frame P picture 
*/
@@ -1668,9 +1669,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
 /* initialize interlaced MVDATA tables (1-Ref) */
 ff_vc1_1ref_mvdata_vlc[i].table   = &vlc_table[vlc_offs[i 
* 3 + 39]];
 ff_vc1_1ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 3 + 40] - 
vlc_offs[i * 3 + 39];
-init_vlc(&ff_vc1_1ref_mvdata_vlc[i], VC1_1REF_MVDATA_VLC_BITS, 72,
- ff_vc1_1ref_mvdata_bits[i], 1, 1,
- ff_vc1_1ref_mvdata_codes[i], 4, 4, 
INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_1ref_mvdata_vlc[i], 
VC1_1REF_MVDATA_VLC_BITS, 72,
+ &ff_vc1_1ref_mvdata_tabs[i][0][1], 2,
+ &ff_vc1_1ref_mvdata_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 }
 for (i = 0; i < 4; i++) {
 /* Initialize 2MV Block pattern VLC tables */
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index da58a0738f..ed6df541cf 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -265,20 +265,13 @@ static av_always_inline void 
get_mvdata_interlaced(VC1Context *v, int *dmv_x,
 int index, index1;
 int extend_x, extend_y;
 GetBitContext *gb = &v->s.gb;
-int bits, esc;
+int bits = v->numref ? VC1_2REF_MVDATA_VLC_BITS : VC1_1REF_MVDATA_VLC_BITS;
 int val, sign;
 
-if (v->numref) {
-bits = VC1_2REF_MVDATA_VLC_BITS;
-esc  = 125;
-} else {
-bits = VC1_1REF_MVDATA_VLC_BITS;
-esc  = 71;
-}
 extend_x = v->dmvrange & 1;
 extend_y = (v->dmvrange >> 1) & 1;
 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
-if (index == esc) {
+if (!index) {
 *dmv_x = get_bits(gb, v->k_x);
 *dmv_y = get_bits(gb, v->k_y);
 if (v->numref) {
@@ -288,15 +281,14 @@ static av_always_inline void 
get_mvdata_interlaced(VC1Context *v, int *dmv_x,
 }
 }
 else {
-av_assert0(index < esc);
-index1 = (index + 1) % 9;
+index1 = index & 0xF;
 if (index1 != 0) {
 val= get_bits(gb, index1 + extend_x);
 sign   = 0 - (val & 1);
 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - 
sign;
 } else
 *dmv_x = 0;
-index1 = (index + 1) / 9;
+index1 = index >> 4;
 if (index1 > v->numref) {
 val= get_bits(gb, (index1 >> v->numref) + extend_y);
 sign   = 0 - (val & 1);
diff --git a/libavcodec/vc1data.c b/libavcodec/vc1data.c
index ed5e680527..de41af

[FFmpeg-devel] [PATCH 082/114] avcodec/vc1_block: Use local variables inside macro

2020-11-10 Thread Andreas Rheinhardt
This makes it clearer which variables are really just temporary variables
of the macro and which are actually used externally.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1_block.c | 18 --
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 42de71eb25..3ff67d83bf 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -222,7 +222,8 @@ static void vc1_put_blocks_clamped(VC1Context *v, int 
put_signed)
  * @param _dmv_y Vertical differential for decoded MV
  */
 #define GET_MVDATA(_dmv_x, _dmv_y)  \
-index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
+do {\
+int index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
  VC1_MV_DIFF_VLC_BITS, 2);  \
 if (index > 36) {   \
 mb_has_coeffs = 1;  \
@@ -240,7 +241,7 @@ static void vc1_put_blocks_clamped(VC1Context *v, int 
put_signed)
 _dmv_y = 0; \
 s->mb_intra = 1;\
 } else {\
-index1 = index % 6; \
+int index1 = index % 6, sign, val;  \
 _dmv_x = offset_table[1][index1];   \
 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
 if (val > 0) {  \
@@ -257,7 +258,8 @@ static void vc1_put_blocks_clamped(VC1Context *v, int 
put_signed)
 sign = 0 - (val & 1);   \
 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
 }   \
-}
+}   \
+} while (0)
 
 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
int *dmv_y, int *pred_flag)
@@ -1287,8 +1289,6 @@ static int vc1_decode_p_mb(VC1Context *v)
 
 int mb_has_coeffs = 1; /* last_flag */
 int dmv_x, dmv_y; /* Differential MV components */
-int index, index1; /* LUT indexes */
-int val, sign; /* temp values */
 int first_block = 1;
 int dst_idx, off;
 int skipped, fourmv;
@@ -1338,9 +1338,9 @@ static int vc1_decode_p_mb(VC1Context *v)
 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
 dst_idx = 0;
 for (i = 0; i < 6; i++) {
+int val = ((cbp >> (5 - i)) & 1);
 s->dc_val[0][s->block_index[i]] = 0;
 dst_idx += i >> 2;
-val = ((cbp >> (5 - i)) & 1);
 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
 v->mb_type[0][s->block_index[i]] = s->mb_intra;
 if (s->mb_intra) {
@@ -1391,7 +1391,7 @@ static int vc1_decode_p_mb(VC1Context *v)
 /* Get CBPCY */
 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, 
VC1_CBPCY_P_VLC_BITS, 2);
 for (i = 0; i < 6; i++) {
-val = ((cbp >> (5 - i)) & 1);
+int val = ((cbp >> (5 - i)) & 1);
 s->dc_val[0][s->block_index[i]] = 0;
 s->mb_intra = 0;
 if (i < 4) {
@@ -1851,8 +1851,6 @@ static int vc1_decode_b_mb(VC1Context *v)
 int mqdiff, mquant; /* MB quantization */
 int ttmb = v->ttfrm; /* MB Transform type */
 int mb_has_coeffs = 0; /* last_flag */
-int index, index1; /* LUT indexes */
-int val, sign; /* temp values */
 int first_block = 1;
 int dst_idx, off;
 int skipped, direct;
@@ -1957,9 +1955,9 @@ static int vc1_decode_b_mb(VC1Context *v)
 }
 dst_idx = 0;
 for (i = 0; i < 6; i++) {
+int val = ((cbp >> (5 - i)) & 1);
 s->dc_val[0][s->block_index[i]] = 0;
 dst_idx += i >> 2;
-val = ((cbp >> (5 - i)) & 1);
 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
 v->mb_type[0][s->block_index[i]] = s->mb_intra;
 if (s->mb_intra) {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 081/114] avcodec/vc1: Use symbols table to decode AC coefficients

2020-11-10 Thread Andreas Rheinhardt
The VC1 decoder currently follows the spec very closely when decoding
AC coefficients: The spec uses tables with contiguous indexes. The last
of each of these is an escape code; the other codes encode three other
variables via tables: run, length and whether this is the last
coefficient.

This commit changes this: Given that both run and length fit into 6bits
each, all three of these fit into a VLC symbol (without touching the
sign bit which is needed because some of the VLCs used here aren't
complete).

This already makes the tables for the conversion from index to run etc.
superfluous; furthermore while just at it, this commit also switches to
ff_init_vlc_from_lengths(), thereby making the codes tables superfluous,
too (and said code tables were interleaved with the length tables, so
that the latter were using an uint32_t just as the former, wasting lots
of space). Moreover, the number of codes for each table differed and the
old approach padded the shorter ones to the length of the longer ones;
this is no longer done either. All in all, this saves about 11KB of
tables.

Finally, the symbol and length tables have been added to vc1data.c;
the interleaved codes and lengths tables which they replace was directly
in vc1.c since 3c715383ea7012ac69507e6b9189c98675c77461 in order to
make the VC1 parser compilable without decoder, but the data for the
other VLCs which are also only used in the decoder is in vc1data.c, so
moving it improves consistency.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c   | 231 ++---
 libavcodec/vc1_block.c |  28 ++---
 libavcodec/vc1acdata.h | 174 ---
 libavcodec/vc1data.c   | 227 +++-
 libavcodec/vc1data.h   |   5 +
 5 files changed, 251 insertions(+), 414 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 2e39870057..599a77af31 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1345,227 +1345,6 @@ int ff_vc1_parse_frame_header_adv(VC1Context *v, 
GetBitContext* gb)
 return 0;
 }
 
-static const uint32_t vc1_ac_tables[AC_MODES][186][2] = {
-{
-{ 0x0001,  2}, { 0x0005,  3}, { 0x000D,  4}, { 0x0012,  5}, { 0x000E,  6}, { 
0x0015,  7},
-{ 0x0013,  8}, { 0x003F,  8}, { 0x004B,  9}, { 0x011F,  9}, { 0x00B8, 10}, { 
0x03E3, 10},
-{ 0x0172, 11}, { 0x024D, 12}, { 0x03DA, 12}, { 0x02DD, 13}, { 0x1F55, 13}, { 
0x05B9, 14},
-{ 0x3EAE, 14}, { 0x,  4}, { 0x0010,  5}, { 0x0008,  7}, { 0x0020,  8}, { 
0x0029,  9},
-{ 0x01F4,  9}, { 0x0233, 10}, { 0x01E0, 11}, { 0x012A, 12}, { 0x03DD, 12}, { 
0x050A, 13},
-{ 0x1F29, 13}, { 0x0A42, 14}, { 0x1272, 15}, { 0x1737, 15}, { 0x0003,  5}, { 
0x0011,  7},
-{ 0x00C4,  8}, { 0x004B, 10}, { 0x00B4, 11}, { 0x07D4, 11}, { 0x0345, 12}, { 
0x02D7, 13},
-{ 0x07BF, 13}, { 0x0938, 14}, { 0x0BBB, 14}, { 0x095E, 15}, { 0x0013,  5}, { 
0x0078,  7},
-{ 0x0069,  9}, { 0x0232, 10}, { 0x0461, 11}, { 0x03EC, 12}, { 0x0520, 13}, { 
0x1F2A, 13},
-{ 0x3E50, 14}, { 0x3E51, 14}, { 0x1486, 15}, { 0x000C,  6}, { 0x0024,  9}, { 
0x0094, 11},
-{ 0x08C0, 12}, { 0x0F09, 14}, { 0x1EF0, 15}, { 0x003D,  6}, { 0x0053,  9}, { 
0x01A0, 11},
-{ 0x02D6, 13}, { 0x0F08, 14}, { 0x0013,  7}, { 0x007C,  9}, { 0x07C1, 11}, { 
0x04AC, 14},
-{ 0x001B,  7}, { 0x00A0, 10}, { 0x0344, 12}, { 0x0F79, 14}, { 0x0079,  7}, { 
0x03E1, 10},
-{ 0x02D4, 13}, { 0x2306, 14}, { 0x0021,  8}, { 0x023C, 10}, { 0x0FAE, 12}, { 
0x23DE, 14},
-{ 0x0035,  8}, { 0x0175, 11}, { 0x07B3, 13}, { 0x00C5,  8}, { 0x0174, 11}, { 
0x0785, 13},
-{ 0x0048,  9}, { 0x01A3, 11}, { 0x049E, 13}, { 0x002C,  9}, { 0x00FA, 10}, { 
0x07D6, 11},
-{ 0x0092, 10}, { 0x05CC, 13}, { 0x1EF1, 15}, { 0x00A3, 10}, { 0x03ED, 12}, { 
0x093E, 14},
-{ 0x01E2, 11}, { 0x1273, 15}, { 0x07C4, 11}, { 0x1487, 15}, { 0x0291, 12}, { 
0x0293, 12},
-{ 0x0F8A, 12}, { 0x0509, 13}, { 0x0508, 13}, { 0x078D, 13}, { 0x07BE, 13}, { 
0x078C, 13},
-{ 0x04AE, 14}, { 0x0BBA, 14}, { 0x2307, 14}, { 0x0B9A, 14}, { 0x1736, 15}, { 
0x000E,  4},
-{ 0x0045,  7}, { 0x01F3,  9}, { 0x047A, 11}, { 0x05DC, 13}, { 0x23DF, 14}, { 
0x0019,  5},
-{ 0x0028,  9}, { 0x0176, 11}, { 0x049D, 13}, { 0x23DD, 14}, { 0x0030,  6}, { 
0x00A2, 10},
-{ 0x02EF, 12}, { 0x05B8, 14}, { 0x003F,  6}, { 0x00A5, 10}, { 0x03DB, 12}, { 
0x093F, 14},
-{ 0x0044,  7}, { 0x07CB, 11}, { 0x095F, 15}, { 0x0063,  7}, { 0x03C3, 12}, { 
0x0015,  8},
-{ 0x08F6, 12}, { 0x0017,  8}, { 0x0498, 13}, { 0x002C,  8}, { 0x07B2, 13}, { 
0x002F,  8},
-{ 0x1F54, 13}, { 0x008D,  8}, { 0x07BD, 13}, { 0x008E,  8}, { 0x1182, 13}, { 
0x00FB,  8},
-{ 0x050B, 13}, { 0x002D,  8}, { 0x07C0, 11}, { 0x0079,  9}, { 0x1F5F, 13}, { 
0x007A,  9},
-{ 0x1F56, 13}, { 0x0231, 10}, { 0x03E4, 10}, { 0x01A1, 11}, { 0x0143, 11}, { 
0x01F7, 11},
-{ 0x016F, 12}, { 0x0292, 12}, { 0x02E7, 12}, { 0x016C, 12}, { 0x016D, 12}, { 
0x03DC, 12},
-{ 0x0F8B, 12}, { 0x0499, 13}, { 0x03D8, 12}, { 0x078E, 13}, { 0x02D5, 13}, { 
0x1F5E, 13},
-{ 0x1F2B, 13}, { 0x078F, 13}, { 0x04AD, 14}, { 0x3EAF, 14}, { 0x23DC, 14}, { 
0x004A,  9}
-},

[FFmpeg-devel] [PATCH 084/114] avcodec/vc1: Use symbols table to parse macroblock mode

2020-11-10 Thread Andreas Rheinhardt
Expressions like array[get_vlc2()] can be optimized by using a symbols
table if the array is always the same for a given VLC. This requirement
is fulfilled for the VLCs used to parse the macroblock mode (MBMODE)
element for frame P and B pictures. MBMODE consists of four pieces of
information which can easily be encoded on one byte. Doing so not only
avoids a dereference, but also some space as the table containing this
information becomes redundant; furthermore, some of the VLCs used were
initialized with code tables of type uint16_t which are now replaced by
symbol tables of type uint8_t (thanks to ff_init_vlc_from_lengths()),
thereby saving space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c   |  14 ++--
 libavcodec/vc1_block.c |  41 +-
 libavcodec/vc1data.c   | 177 +++--
 libavcodec/vc1data.h   |  18 +++--
 4 files changed, 155 insertions(+), 95 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 8c8d701ef8..9eb02f88b3 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1439,15 +1439,17 @@ av_cold int ff_vc1_init_common(VC1Context *v)
 /* initialize 4MV MBMODE VLC tables for interlaced frame P picture 
*/
 ff_vc1_intfr_4mv_mbmode_vlc[i].table   = 
&vlc_table[vlc_offs[i * 3 + 37]];
 ff_vc1_intfr_4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 
38] - vlc_offs[i * 3 + 37];
-init_vlc(&ff_vc1_intfr_4mv_mbmode_vlc[i], 
VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
- ff_vc1_intfr_4mv_mbmode_bits[i], 1, 1,
- ff_vc1_intfr_4mv_mbmode_codes[i], 2, 2, 
INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_intfr_4mv_mbmode_vlc[i], 
VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
+ &ff_vc1_intfr_4mv_mbmode_tabs[i][0][1], 2,
+ &ff_vc1_intfr_4mv_mbmode_tabs[i][0][0], 
2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 /* initialize NON-4MV MBMODE VLC tables for the same */
 ff_vc1_intfr_non4mv_mbmode_vlc[i].table   = 
&vlc_table[vlc_offs[i * 3 + 38]];
 ff_vc1_intfr_non4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 
+ 39] - vlc_offs[i * 3 + 38];
-init_vlc(&ff_vc1_intfr_non4mv_mbmode_vlc[i], 
VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
- ff_vc1_intfr_non4mv_mbmode_bits[i], 1, 1,
- ff_vc1_intfr_non4mv_mbmode_codes[i], 1, 1, 
INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_intfr_non4mv_mbmode_vlc[i], 
VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
+ 
&ff_vc1_intfr_non4mv_mbmode_tabs[i][0][1], 2,
+ 
&ff_vc1_intfr_non4mv_mbmode_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 /* initialize interlaced MVDATA tables (1-Ref) */
 ff_vc1_1ref_mvdata_vlc[i].table   = &vlc_table[vlc_offs[i 
* 3 + 39]];
 ff_vc1_1ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 3 + 40] - 
vlc_offs[i * 3 + 39];
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 6820ffb0d7..18637e75d8 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -1518,7 +1518,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v)
 int dst_idx, off;
 int skipped, fourmv = 0, twomv = 0;
 int block_cbp = 0, pat, block_tt = 0;
-int idx_mbmode = 0, mvbp;
+int mvbp;
 int fieldtx;
 
 mquant = v->pq; /* Lossy initialization */
@@ -1528,11 +1528,11 @@ static int vc1_decode_p_mb_intfr(VC1Context *v)
 else
 skipped = v->s.mbskip_table[mb_pos];
 if (!skipped) {
-if (v->fourmvswitch)
-idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, 
VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
-else
-idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, 
VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
-switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
+int bits = v->fourmvswitch ? VC1_INTFR_4MV_MBMODE_VLC_BITS :
+ VC1_INTFR_NON4MV_MBMODE_VLC_BITS;
+int mbmode = get_vlc2(gb, v->mbmode_vlc->table, bits, 2);
+
+switch (mbmode & 0x1F) {
 /* store the motion vector type in a flag (useful later) */
 case MV_PMODE_INTFR_4MV:
 fourmv = 1;
@@ -1562,7 +1562,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v)
 v->blk_mv_type[s->block_index[3]] = 0;
 break;
 }
-if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == 
MV_PMODE_INTFR_INTRA) { // intra MB
+if ((mbmode & 0x1F) == MV_PMODE_INTFR_INTRA) { // intra MB
 for (i = 0; i < 4; i++) {
 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
@@

[FFmpeg-devel] [PATCH 083/114] avcodec/vc1: Use symbols table to decode MV differentials

2020-11-10 Thread Andreas Rheinhardt
The VC1 decoder currently follows the spec very closely when decoding
MV differentials: The spec uses tables with contiguous indexes and
derives four values from it using a pseudo-code procedure: Two flags
and two values with a range of 0..5 each; the latter are obtained as the
result resp. the remainder of dividing the index by 6 in the spec and
in the earlier code.

Yet two flags can be encoded on two bits and the two values can be
encoded on three bits each. Therefore the new symbols uses the two
lowest bits for the flags; the two other values are coded separately
on three bits each. There is just one more minor complication:
The two three-bits values don't always exist; there is another mode.
Instead of using an arbitrary six bit value for the higher bits that
is different from all the values used by the ordinary mode this commit
instead uses a negative number for it; this is possible by shifting the
symbols as stored in the table (to make all fit into an uint8_t) and
then shifting the codes downwards when creating the table. The latter
is possible thanks to ff_init_vlc_from_lengths().

Switching to ff_init_vlc_from_lengths() also entails replacing 16bit
codes by 8bit symbols, thereby saving space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c   |   7 +-
 libavcodec/vc1_block.c |  30 -
 libavcodec/vc1data.c   | 149 +++--
 libavcodec/vc1data.h   |   3 +-
 4 files changed, 86 insertions(+), 103 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 599a77af31..8c8d701ef8 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1414,9 +1414,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
  ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
 ff_vc1_mv_diff_vlc[i].table   = &vlc_table[vlc_offs[i * 3 
+ 11]];
 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i * 3 + 12] - 
vlc_offs[i * 3 + 11];
-init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
- ff_vc1_mv_diff_bits[i], 1, 1,
- ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_mv_diff_vlc[i], 
VC1_MV_DIFF_VLC_BITS, 73,
+ &ff_vc1_mv_diff_tabs[i][0][1], 2,
+ &ff_vc1_mv_diff_tabs[i][0][0], 2, 1,
+ -4, INIT_VLC_USE_NEW_STATIC);
 }
 for (int i = 0, ac_offset = 0; i < 8; i++) {
 ff_vc1_ac_coeff_table[i].table   = &vlc_table[vlc_offs[i * 
2 + 21]];
diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 3ff67d83bf..6820ffb0d7 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -223,25 +223,16 @@ static void vc1_put_blocks_clamped(VC1Context *v, int 
put_signed)
  */
 #define GET_MVDATA(_dmv_x, _dmv_y)  \
 do {\
-int index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
+int symbol = get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
  VC1_MV_DIFF_VLC_BITS, 2);  \
-if (index > 36) {   \
-mb_has_coeffs = 1;  \
-index -= 37;\
-} else  \
-mb_has_coeffs = 0;  \
-s->mb_intra = 0;\
-if (!index) {   \
+mb_has_coeffs = symbol & 1; \
+symbol  >>= 1;  \
+s->mb_intra   = symbol & 1; \
+symbol  >>= 1;  \
+if (!symbol) {  \
 _dmv_x = _dmv_y = 0;\
-} else if (index == 35) {   \
-_dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);  \
-_dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);  \
-} else if (index == 36) {   \
-_dmv_x = 0; \
-_dmv_y = 0; \
-s->mb_intra = 1;\
-} else {\
-int index1 = index % 6, sign, val;  \
+} else if (symbol > 0) {\
+int index

[FFmpeg-devel] [PATCH 085/114] avcodec/vc1: Don't check for errors for complete VLC

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 9eb02f88b3..305cc8c075 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -614,7 +614,7 @@ static void rotate_luts(VC1Context *v)
 static int read_bfraction(VC1Context *v, GetBitContext* gb) {
 int bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, 
VC1_BFRACTION_VLC_BITS, 1);
 
-if (bfraction_lut_index == 21 || bfraction_lut_index < 0) {
+if (bfraction_lut_index == 21) {
 av_log(v->s.avctx, AV_LOG_ERROR, "bfraction invalid\n");
 return AVERROR_INVALIDDATA;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 086/114] avcodec/vc1: Make tables used to initialize VLCs smaller

2020-11-10 Thread Andreas Rheinhardt
By switching to ff_init_vlc_from_lengths() from ff_init_vlc_sparse() one
can replace codes of type uint16_t by symbols of type uint8_t, saving
space.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c |  27 +--
 libavcodec/vc1data.c | 488 ---
 libavcodec/vc1data.h |  13 +-
 3 files changed, 245 insertions(+), 283 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 305cc8c075..dedabaea6c 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1376,18 +1376,19 @@ av_cold int ff_vc1_init_common(VC1Context *v)
 INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
 ff_vc1_norm2_bits, 1, 1,
 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
-INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
-ff_vc1_norm6_bits, 1, 1,
-ff_vc1_norm6_codes, 2, 2, 556);
+INIT_VLC_STATIC_FROM_LENGTHS(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
+ &ff_vc1_norm6_tab[0][1], 2,
+ &ff_vc1_norm6_tab[0][0], 2, 1, 0, 0, 556);
 INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
 ff_vc1_imode_bits, 1, 1,
 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
 for (i = 0; i < 3; i++) {
 ff_vc1_ttmb_vlc[i].table   = &vlc_table[vlc_offs[i * 3 + 
0]];
 ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i * 3 + 1] - 
vlc_offs[i * 3 + 0];
-init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
- ff_vc1_ttmb_bits[i], 1, 1,
- ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 
16,
+ &ff_vc1_ttmb_tabs[i][0][1], 2,
+ &ff_vc1_ttmb_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 ff_vc1_ttblk_vlc[i].table   = &vlc_table[vlc_offs[i * 3 + 
1]];
 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i * 3 + 2] - 
vlc_offs[i * 3 + 1];
 ff_init_vlc_from_lengths(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 
8,
@@ -1409,9 +1410,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
  ff_vc1_4mv_block_pattern_codes[i], 1, 1, 
INIT_VLC_USE_NEW_STATIC);
 ff_vc1_cbpcy_p_vlc[i].table   = &vlc_table[vlc_offs[i * 3 
+ 10]];
 ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i * 3 + 11] - 
vlc_offs[i * 3 + 10];
-init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
- ff_vc1_cbpcy_p_bits[i], 1, 1,
- ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_cbpcy_p_vlc[i], 
VC1_CBPCY_P_VLC_BITS, 64,
+ &ff_vc1_cbpcy_p_tabs[i][0][1], 2,
+ &ff_vc1_cbpcy_p_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 ff_vc1_mv_diff_vlc[i].table   = &vlc_table[vlc_offs[i * 3 
+ 11]];
 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i * 3 + 12] - 
vlc_offs[i * 3 + 11];
 ff_init_vlc_from_lengths(&ff_vc1_mv_diff_vlc[i], 
VC1_MV_DIFF_VLC_BITS, 73,
@@ -1470,9 +1472,10 @@ av_cold int ff_vc1_init_common(VC1Context *v)
 /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) 
*/
 ff_vc1_icbpcy_vlc[i].table   = &vlc_table[vlc_offs[i * 3 + 
53]];
 ff_vc1_icbpcy_vlc[i].table_allocated = vlc_offs[i * 3 + 54] - 
vlc_offs[i * 3 + 53];
-init_vlc(&ff_vc1_icbpcy_vlc[i], VC1_ICBPCY_VLC_BITS, 63,
- ff_vc1_icbpcy_p_bits[i], 1, 1,
- ff_vc1_icbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
+ff_init_vlc_from_lengths(&ff_vc1_icbpcy_vlc[i], 
VC1_ICBPCY_VLC_BITS, 63,
+ &ff_vc1_icbpcy_p_tabs[i][0][1], 2,
+ &ff_vc1_icbpcy_p_tabs[i][0][0], 2, 1,
+ 0, INIT_VLC_USE_NEW_STATIC);
 /* Initialize interlaced field picture MBMODE VLC tables */
 ff_vc1_if_mmv_mbmode_vlc[i].table   = 
&vlc_table[vlc_offs[i * 3 + 54]];
 ff_vc1_if_mmv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 55] 
- vlc_offs[i * 3 + 54];
diff --git a/libavcodec/vc1data.c b/libavcodec/vc1data.c
index 30d8585723..ce9948281f 100644
--- a/libavcodec/vc1data.c
+++ b/libavcodec/vc1data.c
@@ -184,18 +184,25 @@ const uint8_t ff_vc1_norm2_bits[4] = {
 1, 3, 3, 2
 };
 
-const uint16_t ff_vc1_norm6_codes[64] = {
-0x001, 0x002, 0x003, 0x000, 0x004, 0x001, 0x002, 0x047, 0x005, 0x003, 
0x004, 0x04B, 0x005, 0x04D, 0x04E, 0x30E,
-0x006, 0x006, 0x007, 0x053, 0

[FFmpeg-devel] [PATCH 087/114] avcodec/vc1: Avoid code duplication when initializing VLCs

2020-11-10 Thread Andreas Rheinhardt
This has been achieved by switching those VLCs that still used
ff_init_vlc_sparse() to ff_init_vlc_lengths() even though the codes
tables used uint8_t in these cases. But it allows to use one auxiliary
function to initialize the VLCs and by using tables that interleave
symbols and lengths said function only needs one parameter for the
tables, not two. The only table that uses an uint16_t symbols table
still has to be treated specially for this reason.

The offsets table has been removed as a byproduct of these changes.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vc1.c | 158 +++---
 libavcodec/vc1data.c | 159 +--
 libavcodec/vc1data.h |  21 ++
 3 files changed, 138 insertions(+), 200 deletions(-)

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index dedabaea6c..9db8063c12 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -1345,15 +1345,20 @@ int ff_vc1_parse_frame_header_adv(VC1Context *v, 
GetBitContext* gb)
 return 0;
 }
 
-static const uint16_t vlc_offs[] = {
-0,   520,   552,   616,  1128,  1160,  1224,  1740,  1772,  1836,  
1900,  2436,
- 2986,  3050,  3610,  4154,  4218,  4746,  5326,  5390,  5902,  6554,  
7658,  8342,
- 9304,  9988, 10630, 11234, 12174, 13006, 13560, 14232, 14786, 15432, 
16350, 17522,
-20372, 21818, 22330, 22394, 23166, 23678, 23742, 24820, 25332, 25396, 
26460, 26980,
-27048, 27592, 27600, 27608, 27616, 27624, 28224, 28258, 28290, 28802, 
28834, 28866,
-29378, 29412, 29444, 29960, 29994, 30026, 30538, 30572, 30604, 31120, 
31154, 31186,
-31714, 31746, 31778, 32306, 32340, 32372
-};
+static VLC_TYPE vlc_buf[33080][2];
+
+static av_cold void vc1_init_vlc(VLC *vlc, int nb_bits, int nb_codes,
+ unsigned *buf_offset, const uint8_t (*tab)[2],
+ int offset)
+{
+vlc->table   = &vlc_buf[*buf_offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
+
+ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
+ &tab[0][1], 2, &tab[0][0], 2, 1,
+ offset, INIT_VLC_USE_NEW_STATIC);
+*buf_offset += vlc->table_size;
+}
 
 /**
  * Init VC-1 specific tables and VC1Context members
@@ -1364,129 +1369,74 @@ av_cold int ff_vc1_init_common(VC1Context *v)
 {
 static int done = 0;
 int i = 0;
-static VLC_TYPE vlc_table[32372][2];
 
 v->hrd_rate = v->hrd_buffer = NULL;
 
 /* VLC tables */
 if (!done) {
-INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
-ff_vc1_bfraction_bits, 1, 1,
-ff_vc1_bfraction_codes, 1, 1, 1 << 
VC1_BFRACTION_VLC_BITS);
-INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
-ff_vc1_norm2_bits, 1, 1,
-ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
-INIT_VLC_STATIC_FROM_LENGTHS(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
- &ff_vc1_norm6_tab[0][1], 2,
- &ff_vc1_norm6_tab[0][0], 2, 1, 0, 0, 556);
-INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
-ff_vc1_imode_bits, 1, 1,
-ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
+unsigned offset = 0;
+vc1_init_vlc(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
+ &offset, ff_vc1_bfraction_tab, 0);
+vc1_init_vlc(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
+ &offset, ff_vc1_norm2_tab, 0);
+vc1_init_vlc(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
+ &offset, ff_vc1_norm6_tab, 0);
+vc1_init_vlc(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
+ &offset, ff_vc1_imode_tab, 0);
 for (i = 0; i < 3; i++) {
-ff_vc1_ttmb_vlc[i].table   = &vlc_table[vlc_offs[i * 3 + 
0]];
-ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i * 3 + 1] - 
vlc_offs[i * 3 + 0];
-ff_init_vlc_from_lengths(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 
16,
- &ff_vc1_ttmb_tabs[i][0][1], 2,
- &ff_vc1_ttmb_tabs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
-ff_vc1_ttblk_vlc[i].table   = &vlc_table[vlc_offs[i * 3 + 
1]];
-ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i * 3 + 2] - 
vlc_offs[i * 3 + 1];
-ff_init_vlc_from_lengths(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 
8,
- &ff_vc1_ttblk_tabs[i][0][1], 2,
- &ff_vc1_ttblk_tabs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
-ff_vc1_subblkpat_vlc[i].table   = &vlc_table[vlc_offs[i * 
3 + 2]];
-ff_vc1_subblkpat_vlc[i

[FFmpeg-devel] [PATCH 091/114] avcodec/atrac9dec: Don't use unnecessarily large VLC tables

2020-11-10 Thread Andreas Rheinhardt
Using more bits for a VLC than the longest code has has no advantage.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c | 15 ++-
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index ecf95f1777..0477b304d2 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -26,6 +26,8 @@
 #include "libavutil/lfg.h"
 #include "libavutil/float_dsp.h"
 
+#define ATRAC9_SF_VLC_BITS 8
+
 typedef struct ATRAC9ChannelData {
 int band_ext;
 int q_unit_cnt;
@@ -272,7 +274,8 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 c->scalefactors[0] = get_bits(gb, len);
 
 for (int i = 1; i < b->band_ext_q_unit; i++) {
-int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 1);
+int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table,
+ATRAC9_SF_VLC_BITS, 1);
 c->scalefactors[i] = val & ((1 << len) - 1);
 }
 
@@ -302,7 +305,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 const VLC *tab = &s->sf_vlc[1][len];
 
 for (int i = 0; i < unit_cnt; i++) {
-int dist = get_vlc2(gb, tab->table, 9, 1);
+int dist = get_vlc2(gb, tab->table, ATRAC9_SF_VLC_BITS, 1);
 c->scalefactors[i] = baseline[i] + dist;
 }
 
@@ -325,7 +328,8 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 c->scalefactors[0] = get_bits(gb, len);
 
 for (int i = 1; i < unit_cnt; i++) {
-int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 1);
+int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table,
+ATRAC9_SF_VLC_BITS, 1);
 c->scalefactors[i] = val & ((1 << len) - 1);
 }
 
@@ -930,7 +934,8 @@ static av_cold int atrac9_decode_init(AVCodecContext *avctx)
 for (int i = 1; i < 7; i++) {
 const HuffmanCodebook *hf = &at9_huffman_sf_unsigned[i];
 
-init_vlc(&s->sf_vlc[0][i], 9, hf->size, hf->bits, 1, 1, hf->codes,
+init_vlc(&s->sf_vlc[0][i], ATRAC9_SF_VLC_BITS, hf->size,
+ hf->bits, 1, 1, hf->codes,
  2, 2, 0);
 }
 
@@ -943,7 +948,7 @@ static av_cold int atrac9_decode_init(AVCodecContext *avctx)
 for (int j = 0; j < nums; j++)
 sym[j] = sign_extend(j, hf->value_bits);
 
-ff_init_vlc_sparse(&s->sf_vlc[1][i], 9, hf->size, hf->bits, 1, 1,
+ff_init_vlc_sparse(&s->sf_vlc[1][i], ATRAC9_SF_VLC_BITS, hf->size, 
hf->bits, 1, 1,
hf->codes, 2, 2, sym, sizeof(*sym), sizeof(*sym), 
0);
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 089/114] avcodec/atrac9dec: Don't confuse max_depth of VLC with max codelength

2020-11-10 Thread Andreas Rheinhardt
The whole point of VLCs with their tables is to read more than one bit
at a time; therefore max_depth, the number of times one has to
(maximally) read further bits is given by ceil(max_code_length / table_bits)
which in the case of ATRAC9's coefficient VLCs gives an upper bound of
two. Instead the maximum length of a code of the given VLC has been used
(which is not even a compile-time constant). Use two instead.

Furthermore, given that this was the only usage of the field containing
the maximum of all the code lengths of a given VLC the field has been
removed from its containing struct.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c |   2 +-
 libavcodec/atrac9tab.h | 105 -
 2 files changed, 53 insertions(+), 54 deletions(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index 075d610e75..a1af3c22ef 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -411,7 +411,7 @@ static inline void read_coeffs_coarse(ATRAC9Context *s, 
ATRAC9BlockData *b,
 const int groups = bands >> huff->value_cnt_pow;
 
 for (int j = 0; j < groups; j++) {
-uint16_t val = get_vlc2(gb, tab->table, 9, huff->max_bit_size);
+uint16_t val = get_vlc2(gb, tab->table, 9, 2);
 
 for (int k = 0; k < huff->value_cnt; k++) {
 coeffs[k] = sign_extend(val, huff->value_bits);
diff --git a/libavcodec/atrac9tab.h b/libavcodec/atrac9tab.h
index 8f290f158c..c25d0e2744 100644
--- a/libavcodec/atrac9tab.h
+++ b/libavcodec/atrac9tab.h
@@ -476,26 +476,25 @@ typedef struct HuffmanCodebook {
 const int value_cnt;
 const int value_cnt_pow;
 const int value_bits;
-const int max_bit_size;
 } HuffmanCodebook;
 
 static const HuffmanCodebook at9_huffman_sf_unsigned[] = {
 { 0 },
-{ huff_sfb_a1_bits, huff_sfb_a1_codes,  2,  1,  0,  1,  1, },
-{ huff_sfb_a2_bits, huff_sfb_a2_codes,  4,  1,  0,  2,  3, },
-{ huff_sfb_a3_bits, huff_sfb_a3_codes,  8,  1,  0,  3,  6, },
-{ huff_sfb_a4_bits, huff_sfb_a4_codes, 16,  1,  0,  4,  8, },
-{ huff_sfb_a5_bits, huff_sfb_a5_codes, 32,  1,  0,  5,  8, },
-{ huff_sfb_a6_bits, huff_sfb_a6_codes, 64,  1,  0,  6,  8, },
+{ huff_sfb_a1_bits, huff_sfb_a1_codes,  2,  1,  0,  1 },
+{ huff_sfb_a2_bits, huff_sfb_a2_codes,  4,  1,  0,  2 },
+{ huff_sfb_a3_bits, huff_sfb_a3_codes,  8,  1,  0,  3 },
+{ huff_sfb_a4_bits, huff_sfb_a4_codes, 16,  1,  0,  4 },
+{ huff_sfb_a5_bits, huff_sfb_a5_codes, 32,  1,  0,  5 },
+{ huff_sfb_a6_bits, huff_sfb_a6_codes, 64,  1,  0,  6 },
 };
 
 static const HuffmanCodebook at9_huffman_sf_signed[] = {
 { 0 },
 { 0 },
-{ huff_sfb_b2_bits, huff_sfb_b2_codes,  4,  1,  0,  2,  2, },
-{ huff_sfb_b3_bits, huff_sfb_b3_codes,  8,  1,  0,  3,  6, },
-{ huff_sfb_b4_bits, huff_sfb_b4_codes, 16,  1,  0,  4,  8, },
-{ huff_sfb_b5_bits, huff_sfb_b5_codes, 32,  1,  0,  5,  8, },
+{ huff_sfb_b2_bits, huff_sfb_b2_codes,  4,  1,  0,  2 },
+{ huff_sfb_b3_bits, huff_sfb_b3_codes,  8,  1,  0,  3 },
+{ huff_sfb_b4_bits, huff_sfb_b4_codes, 16,  1,  0,  4 },
+{ huff_sfb_b5_bits, huff_sfb_b5_codes, 32,  1,  0,  5 },
 };
 
 static const uint8_t huff_spec_a21_bits[] = {
@@ -1539,40 +1538,40 @@ static const HuffmanCodebook at9_huffman_coeffs[][8][4] 
= {
 { { 0 } },
 { { 0 } },
 {
-{ huff_spec_a21_bits, huff_spec_a21_codes,  16,   2,   1,   2,   
3, },
-{ huff_spec_a22_bits, huff_spec_a22_codes, 256,   4,   2,   2,   
8, },
-{ huff_spec_a23_bits, huff_spec_a23_codes, 256,   4,   2,   2,   
9, },
-{ huff_spec_a24_bits, huff_spec_a24_codes, 256,   4,   2,   2,  
10, },
+{ huff_spec_a21_bits, huff_spec_a21_codes,  16,   2,   1,   2 },
+{ huff_spec_a22_bits, huff_spec_a22_codes, 256,   4,   2,   2 },
+{ huff_spec_a23_bits, huff_spec_a23_codes, 256,   4,   2,   2 },
+{ huff_spec_a24_bits, huff_spec_a24_codes, 256,   4,   2,   2 },
 },
 {
-{ huff_spec_a31_bits, huff_spec_a31_codes,  64,   2,   1,   3,   
7, },
-{ huff_spec_a32_bits, huff_spec_a32_codes,  64,   2,   1,   3,   
7, },
-{ huff_spec_a33_bits, huff_spec_a33_codes,  64,   2,   1,   3,   
8, },
-{ huff_spec_a34_bits, huff_spec_a34_codes,  64,   2,   1,   3,  
10, },
+{ huff_spec_a31_bits, huff_spec_a31_codes,  64,   2,   1,   3 },
+{ huff_spec_a32_bits, huff_spec_a32_codes,  64,   2,   1,   3 },
+{ huff_spec_a33_bits, huff_spec_a33_codes,  64,   2,   1,   3 },
+{ huff_spec_a34_bits, huff_spec_a34_codes,  64,   2,   1,   3 },
 },
 {
-{ huff_spec_a41_bits, huff_spec_a41_codes, 256,   2,   1,   4,   
9, },
-{ huff_spec_a42_bits, huff_spec_a42_codes, 256,   2,   1,   4,  
10, },
-{ huff_spec_a43_bits, huff_spec_a43_codes, 256, 

[FFmpeg-devel] [PATCH 088/114] avcodec/rv34: Avoid offsets table for initialization of static VLCs

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/rv34.c | 66 +--
 1 file changed, 29 insertions(+), 37 deletions(-)

diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c
index 09fa962b2f..00fbcef8ec 100644
--- a/libavcodec/rv34.c
+++ b/libavcodec/rv34.c
@@ -76,27 +76,6 @@ static int rv34_decode_mv(RV34DecContext *r, int block_type);
  * @{
  */
 
-static const int table_offs[] = {
-  0,   1818,   3622,   4144,   4698,   5234,   5804,   5868,   5900,   
5932,
-   5996,   6252,   6316,   6348,   6380,   7674,   8944,  10274,  11668,  
12250,
-  14060,  15846,  16372,  16962,  17512,  18148,  18180,  18212,  18244,  
18308,
-  18564,  18628,  18660,  18692,  20036,  21314,  22648,  23968,  24614,  
26384,
-  28190,  28736,  29366,  29938,  30608,  30640,  30672,  30704,  30768,  
31024,
-  31088,  31120,  31184,  32570,  33898,  35236,  36644,  37286,  39020,  
40802,
-  41368,  42052,  42692,  43348,  43380,  43412,  43444,  43476,  43604,  
43668,
-  43700,  43732,  45100,  46430,  47778,  49160,  49802,  51550,  53340,  
53972,
-  54648,  55348,  55994,  56122,  56154,  56186,  56218,  56346,  56410,  
56442,
-  56474,  57878,  59290,  60636,  62036,  62682,  64460,  64524,  64588,  
64716,
-  64844,  66076,  67466,  67978,  68542,  69064,  69648,  70296,  72010,  
72074,
-  72138,  72202,  72330,  73572,  74936,  75454,  76030,  76566,  77176,  
77822,
-  79582,  79646,  79678,  79742,  79870,  81180,  82536,  83064,  83672,  
84242,
-  84934,  85576,  87384,  87448,  87480,  87544,  87672,  88982,  90340,  
90902,
-  91598,  92182,  92846,  93488,  95246,  95278,  95310,  95374,  95502,  
96878,
-  98266,  98848,  99542, 100234, 100884, 101524, 103320, 103352, 103384, 
103416,
- 103480, 104874, 106222, 106910, 107584, 108258, 108902, 109544, 111366, 
111398,
- 111430, 111462, 111494, 112878, 114320, 114988, 115660, 116310, 116950, 117592
-};
-
 static VLC_TYPE table_data[117592][2];
 
 /**
@@ -108,7 +87,7 @@ static VLC_TYPE table_data[117592][2];
  * @param numVLC table number (for static initialization)
  */
 static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const 
uint8_t *syms,
- const int num)
+ int *offset)
 {
 int counts[17] = {0}, codes[17];
 uint16_t cw[MAX_VLC_SIZE];
@@ -128,12 +107,13 @@ static void rv34_gen_vlc(const uint8_t *bits, int size, 
VLC *vlc, const uint8_t
 for (int i = 0; i < size; i++)
 cw[i] = codes[bits[i]]++;
 
-vlc->table = &table_data[table_offs[num]];
-vlc->table_allocated = table_offs[num + 1] - table_offs[num];
+vlc->table   = &table_data[*offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(table_data) - *offset;
 ff_init_vlc_sparse(vlc, FFMIN(maxbits, 9), size,
bits, 1, 1,
cw,2, 2,
syms, !!syms, !!syms, INIT_VLC_USE_NEW_STATIC);
+*offset += vlc->table_size;
 }
 
 /**
@@ -141,34 +121,46 @@ static void rv34_gen_vlc(const uint8_t *bits, int size, 
VLC *vlc, const uint8_t
  */
 static av_cold void rv34_init_tables(void)
 {
-int i, j, k;
+int i, j, k, offset = 0;
 
 for(i = 0; i < NUM_INTRA_TABLES; i++){
 for(j = 0; j < 2; j++){
-rv34_gen_vlc(rv34_table_intra_cbppat   [i][j], CBPPAT_VLC_SIZE,   
&intra_vlcs[i].cbppattern[j], NULL, 19*i + 0 + j);
-rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, 
&intra_vlcs[i].second_pattern[j], NULL, 19*i + 2 + j);
-rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, 
&intra_vlcs[i].third_pattern[j],  NULL, 19*i + 4 + j);
+rv34_gen_vlc(rv34_table_intra_cbppat   [i][j], CBPPAT_VLC_SIZE,
+ &intra_vlcs[i].cbppattern[j], NULL, &offset);
+rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE,
+ &intra_vlcs[i].second_pattern[j], NULL, &offset);
+rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE,
+ &intra_vlcs[i].third_pattern[j],  NULL, &offset);
 for(k = 0; k < 4; k++){
-rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2],  CBP_VLC_SIZE,   
&intra_vlcs[i].cbp[j][k], rv34_cbp_code, 19*i + 6 + j*4 + k);
+rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2],  CBP_VLC_SIZE,
+ &intra_vlcs[i].cbp[j][k], rv34_cbp_code, &offset);
 }
 }
 for(j = 0; j < 4; j++){
-rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, 
&intra_vlcs[i].first_pattern[j], NULL, 19*i + 14 + j);
+rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE,
+ &intra_vlcs[i].first_pattern[j], NULL, &offset);
 }
-rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, 
&intra_vlcs[i].coefficient, NULL, 19*i + 18);
+rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VL

[FFmpeg-devel] [PATCH 090/114] avcodec/atrac9dec: Use least max_depth in call to get_vlc2()

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index a1af3c22ef..ecf95f1777 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -272,7 +272,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 c->scalefactors[0] = get_bits(gb, len);
 
 for (int i = 1; i < b->band_ext_q_unit; i++) {
-int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
+int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 1);
 c->scalefactors[i] = val & ((1 << len) - 1);
 }
 
@@ -302,7 +302,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 const VLC *tab = &s->sf_vlc[1][len];
 
 for (int i = 0; i < unit_cnt; i++) {
-int dist = get_vlc2(gb, tab->table, 9, 2);
+int dist = get_vlc2(gb, tab->table, 9, 1);
 c->scalefactors[i] = baseline[i] + dist;
 }
 
@@ -325,7 +325,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 c->scalefactors[0] = get_bits(gb, len);
 
 for (int i = 1; i < unit_cnt; i++) {
-int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
+int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 1);
 c->scalefactors[i] = val & ((1 << len) - 1);
 }
 
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 093/114] avcodec/atrac9tab: Unify tables used to initialize VLCs

2020-11-10 Thread Andreas Rheinhardt
Using separate tables has the downside that one needs a big number of
pointers to the separate tables (currently 77); unifying them avoids
this.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c |  19 ++-
 libavcodec/atrac9tab.h | 310 ++---
 2 files changed, 120 insertions(+), 209 deletions(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index 3b26b6b486..ddfa02a393 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -848,6 +848,7 @@ static av_cold int atrac9_decode_init(AVCodecContext *avctx)
 GetBitContext gb;
 ATRAC9Context *s = avctx->priv_data;
 int version, block_config_idx, superframe_idx, alloc_c_len;
+const uint8_t (*tab)[2];
 int ret;
 
 s->avctx = avctx;
@@ -932,17 +933,20 @@ static av_cold int atrac9_decode_init(AVCodecContext 
*avctx)
 s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
 
 /* Unsigned scalefactor VLCs */
+tab = at9_sfb_a_tab;
 for (int i = 1; i < 7; i++) {
 const HuffmanCodebook *hf = &at9_huffman_sf_unsigned[i];
 
 ret = ff_init_vlc_from_lengths(&s->sf_vlc[0][i], ATRAC9_SF_VLC_BITS,
-   hf->size, &hf->tab[0][1], 2,
-   &hf->tab[0][0], 2, 1, 0, 0);
+   hf->size, &tab[0][1], 2,
+   &tab[0][0], 2, 1, 0, 0);
 if (ret < 0)
 return ret;
+tab += hf->size;
 }
 
 /* Signed scalefactor VLCs */
+tab = at9_sfb_b_tab;
 for (int i = 2; i < 6; i++) {
 const HuffmanCodebook *hf = &at9_huffman_sf_signed[i];
 
@@ -950,22 +954,25 @@ static av_cold int atrac9_decode_init(AVCodecContext 
*avctx)
  * the values in the source table are offset by 16 to make
  * them fit into an uint8_t; the -16 reverses this shift. */
 ret = ff_init_vlc_from_lengths(&s->sf_vlc[1][i], ATRAC9_SF_VLC_BITS,
-   hf->size, &hf->tab[0][1], 2,
-   &hf->tab[0][0], 2, 1, -16, 0);
+   hf->size, &tab[0][1], 2,
+   &tab[0][0], 2, 1, -16, 0);
 if (ret < 0)
 return ret;
+tab += hf->size;
 }
 
 /* Coefficient VLCs */
+tab = at9_coeffs_tab;
 for (int i = 0; i < 2; i++) {
 for (int j = 0; j < 8; j++) {
 for (int k = 0; k < 4; k++) {
 const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
 ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i][j][k], 9,
-   hf->size, &hf->tab[0][1], 2,
-   &hf->tab[0][0], 2, 1, 0, 0);
+   hf->size, &tab[0][1], 2,
+   &tab[0][0], 2, 1, 0, 0);
 if (ret < 0)
 return ret;
+tab += hf->size;
 }
 }
 }
diff --git a/libavcodec/atrac9tab.h b/libavcodec/atrac9tab.h
index c62b8070d0..1882bf9f43 100644
--- a/libavcodec/atrac9tab.h
+++ b/libavcodec/atrac9tab.h
@@ -373,34 +373,25 @@ static const uint8_t at9_tab_b_dist[] = {
 27, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30,
 };
 
-static const uint8_t huff_sfb_a1_tab[2][2] = {
+static const uint8_t at9_sfb_a_tab[][2] = {
+/* Unsigned scalefactor table 1 - 2 entries */
 {  0, 1 }, {  1, 1 },
-};
-
-static const uint8_t huff_sfb_a2_tab[4][2] = {
+/* Unsigned scalefactor table 2 - 4 entries */
 {  0, 1 }, {  3, 2 }, {  1, 3 }, {  2, 3 },
-};
-
-static const uint8_t huff_sfb_a3_tab[8][2] = {
+/* Unsigned scalefactor table 3 - 8 entries */
 {  0, 2 }, {  1, 2 }, {  7, 2 }, {  6, 3 }, {  2, 4 }, {  5, 5 }, {  3, 6 
},
 {  4, 6 },
-};
-
-static const uint8_t huff_sfb_a4_tab[16][2] = {
+/* Unsigned scalefactor table 4 - 16 entries */
 {  2, 4 }, { 14, 4 }, { 11, 8 }, { 10, 8 }, {  9, 8 }, {  6, 8 }, {  7, 8 
},
 {  8, 8 }, {  5, 7 }, { 13, 5 }, {  3, 5 }, { 12, 6 }, {  4, 6 }, {  0, 2 
},
 {  1, 2 }, { 15, 2 },
-};
-
-static const uint8_t huff_sfb_a5_tab[32][2] = {
+/* Unsigned scalefactor table 5 - 32 entries */
 { 31, 3 }, {  1, 3 }, { 26, 7 }, {  7, 7 }, { 20, 8 }, { 21, 8 }, { 23, 8 
},
 { 22, 8 }, { 29, 5 }, { 30, 4 }, {  4, 5 }, { 11, 8 }, { 24, 8 }, {  9, 8 
},
 { 10, 8 }, {  6, 6 }, { 25, 7 }, { 19, 8 }, { 12, 8 }, { 27, 6 }, { 18, 8 
},
 { 13, 8 }, { 16, 8 }, { 17, 8 }, { 14, 8 }, { 15, 8 }, {  8, 7 }, {  0, 2 
},
 {  5, 5 }, { 28, 5 }, {  3, 4 }, {  2, 3 },
-};
-
-static const uint8_t huff_sfb_a6_tab[64][2] = {
+/* Unsigned scalefactor table 6 - 64 entries */
 {  0, 3 }, {  1, 3 }, {  2, 4 }, {  3, 4 }, { 61, 4 }, { 62, 4 }, { 63, 4 
},
 {  4, 5 }, {  5, 5 }, { 58, 5 }, { 59, 5 }, { 60, 5 }, {  6, 6 }, {  7, 6 
},
 {  8, 6 

[FFmpeg-devel] [PATCH 094/114] avcodec/atrac9dec: Don't create VLCs that are never used

2020-11-10 Thread Andreas Rheinhardt
The ATRAC9 decoder creates VLCs with parameters contained in
HuffmanCodebooks; some of these HuffmanCodebooks are empty and yet
VLCs (that were completely unused*) were created from them. Said VLC
contained a single table with 512 VLC_TYPE[2] entries, each of which
indicated that this is an invalid code. This commit stops creating said
VLCs.

*: read_coeffs_coarse() uses the HuffmanCodebook
at9_huffman_coeffs[cb][prec][cbi]. prec is c->precision_coarse[i] + 1
and every precision_coarse entry is in the 1..15 range after
calc_precision(), so prec is >= 2 (all codebooks with prec < 2 are
empty). The remaining empty codebooks are those with cb == 1 and cbi ==
0, yet this is impossible, too: cb is given by c->codebookset[i] and
this is always 0 if i < 8 (because those are never set to anything else
in calc_codebook_idx()) and cbi is given by at9_q_unit_to_codebookidx[i]
which is never zero if i >= 8.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index ddfa02a393..4715ce42d7 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -964,8 +964,8 @@ static av_cold int atrac9_decode_init(AVCodecContext *avctx)
 /* Coefficient VLCs */
 tab = at9_coeffs_tab;
 for (int i = 0; i < 2; i++) {
-for (int j = 0; j < 8; j++) {
-for (int k = 0; k < 4; k++) {
+for (int j = 2; j < 8; j++) {
+for (int k = i; k < 4; k++) {
 const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
 ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i][j][k], 9,
hf->size, &tab[0][1], 2,
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 095/114] avcodec/atrac9dec: Make VLCs static

2020-11-10 Thread Andreas Rheinhardt
Also remove code duplication and use a named constant for the number of
VLC bits while just at it.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c | 69 +-
 1 file changed, 34 insertions(+), 35 deletions(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index 4715ce42d7..d2dbffe92a 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -27,6 +27,7 @@
 #include "libavutil/float_dsp.h"
 
 #define ATRAC9_SF_VLC_BITS 8
+#define ATRAC9_COEFF_VLC_BITS 9
 
 typedef struct ATRAC9ChannelData {
 int band_ext;
@@ -92,14 +93,15 @@ typedef struct ATRAC9Context {
 const ATRAC9BlockConfig *block_config;
 
 /* Generated on init */
-VLC sf_vlc[2][8];/* Signed/unsigned, length */
-VLC coeff_vlc[2][8][4];  /* Cookbook, precision, cookbook index */
 uint8_t alloc_curve[48][48];
 DECLARE_ALIGNED(32, float, imdct_win)[256];
 
 DECLARE_ALIGNED(32, float, temp)[256];
 } ATRAC9Context;
 
+static VLC sf_vlc[2][8];/* Signed/unsigned, length */
+static VLC coeff_vlc[2][8][4];  /* Cookbook, precision, cookbook index */
+
 static inline int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b,
  GetBitContext *gb)
 {
@@ -269,7 +271,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
 const int base = get_bits(gb, 5);
 const int len = get_bits(gb, 2) + 3;
-const VLC *tab = &s->sf_vlc[0][len];
+const VLC *tab = &sf_vlc[0][len];
 
 c->scalefactors[0] = get_bits(gb, len);
 
@@ -302,7 +304,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 
 const int len = get_bits(gb, 2) + 2;
 const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
-const VLC *tab = &s->sf_vlc[1][len];
+const VLC *tab = &sf_vlc[1][len];
 
 for (int i = 0; i < unit_cnt; i++) {
 int dist = get_vlc2(gb, tab->table, ATRAC9_SF_VLC_BITS, 1);
@@ -323,7 +325,7 @@ static inline int read_scalefactors(ATRAC9Context *s, 
ATRAC9BlockData *b,
 const int base = get_bits(gb, 5) - (1 << (5 - 1));
 const int len = get_bits(gb, 2) + 1;
 const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
-const VLC *tab = &s->sf_vlc[0][len];
+const VLC *tab = &sf_vlc[0][len];
 
 c->scalefactors[0] = get_bits(gb, len);
 
@@ -410,12 +412,12 @@ static inline void read_coeffs_coarse(ATRAC9Context *s, 
ATRAC9BlockData *b,
 if (prec <= max_prec) {
 const int cb = c->codebookset[i];
 const int cbi = at9_q_unit_to_codebookidx[i];
-const VLC *tab = &s->coeff_vlc[cb][prec][cbi];
+const VLC *tab = &coeff_vlc[cb][prec][cbi];
 const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
 const int groups = bands >> huff->value_cnt_pow;
 
 for (int j = 0; j < groups; j++) {
-uint16_t val = get_vlc2(gb, tab->table, 9, 2);
+uint16_t val = get_vlc2(gb, tab->table, ATRAC9_COEFF_VLC_BITS, 
2);
 
 for (int k = 0; k < huff->value_cnt; k++) {
 coeffs[k] = sign_extend(val, huff->value_bits);
@@ -828,28 +830,34 @@ static av_cold int atrac9_decode_close(AVCodecContext 
*avctx)
 {
 ATRAC9Context *s = avctx->priv_data;
 
-for (int i = 1; i < 7; i++)
-ff_free_vlc(&s->sf_vlc[0][i]);
-for (int i = 2; i < 6; i++)
-ff_free_vlc(&s->sf_vlc[1][i]);
-for (int i = 0; i < 2; i++)
-for (int j = 0; j < 8; j++)
-for (int k = 0; k < 4; k++)
-ff_free_vlc(&s->coeff_vlc[i][j][k]);
-
 ff_mdct_end(&s->imdct);
 av_free(s->fdsp);
 
 return 0;
 }
 
+static av_cold void atrac9_init_vlc(VLC *vlc, int nb_bits, int nb_codes,
+const uint8_t (**tab)[2],
+unsigned *buf_offset, int offset)
+{
+static VLC_TYPE vlc_buf[24812][2];
+
+vlc->table   = &vlc_buf[*buf_offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
+ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
+ &(*tab)[0][1], 2, &(*tab)[0][0], 2, 1,
+ offset, INIT_VLC_USE_NEW_STATIC);
+*buf_offset += vlc->table_size;
+*tab+= nb_codes;
+}
+
 static av_cold int atrac9_decode_init(AVCodecContext *avctx)
 {
 GetBitContext gb;
 ATRAC9Context *s = avctx->priv_data;
 int version, block_config_idx, superframe_idx, alloc_c_len;
 const uint8_t (*tab)[2];
-int ret;
+unsigned offset = 0;
 
 s->avctx = avctx;
 
@@ -932,17 +940,16 @@ static av_cold int atrac9_decode_init(AVCodecContext 
*avctx)
 for (int j = 0; j < i; j++)
 s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
 
+if 

[FFmpeg-devel] [PATCH 097/114] avcodec/atrac9tab: Add missing static to internal table

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9tab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/atrac9tab.h b/libavcodec/atrac9tab.h
index 1882bf9f43..b169035aad 100644
--- a/libavcodec/atrac9tab.h
+++ b/libavcodec/atrac9tab.h
@@ -104,7 +104,7 @@ static const int at9_q_unit_to_coeff_idx[] = {
 72, 80, 88, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256
 };
 
-const uint8_t at9_q_unit_to_codebookidx[] = {
+static const uint8_t at9_q_unit_to_codebookidx[] = {
 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
 };
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 096/114] avcodec/atrac9dec: Replace av_free() by av_freep() in close function

2020-11-10 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/atrac9dec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index d2dbffe92a..585c68f33a 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -831,7 +831,7 @@ static av_cold int atrac9_decode_close(AVCodecContext 
*avctx)
 ATRAC9Context *s = avctx->priv_data;
 
 ff_mdct_end(&s->imdct);
-av_free(s->fdsp);
+av_freep(&s->fdsp);
 
 return 0;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 100/114] avcodec/vp3: Use symbols table for VP3 motion vectors

2020-11-10 Thread Andreas Rheinhardt
Expressions like array[get_vlc2()] can be optimized by using a symbols
table if the array is always the same for a given VLC. This requirement
is fulfilled for the VLC used for VP3 motion vectors. The reason it
hasn't been done before is probably that the array in this case
contained entries in the range -31..31; but this is no problem with
ff_init_vlc_from_lengths(): Just apply an offset of 31 to the symbols
before storing them in the table used to initialize VP3 motion vectors
and apply an offset of -31 when initializing the actual VLC.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vp3.c | 20 ---
 libavcodec/vp3data.h | 46 +++-
 2 files changed, 24 insertions(+), 42 deletions(-)

diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index 7037d03a98..f288a53fe1 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -48,6 +48,7 @@
 #include "vp3dsp.h"
 #include "xiph.h"
 
+#define VP3_MV_VLC_BITS 6
 #define VP4_MV_VLC_BITS 6
 #define SUPERBLOCK_VLC_BITS 6
 
@@ -946,8 +947,10 @@ static int unpack_vectors(Vp3DecodeContext *s, 
GetBitContext *gb)
 case MODE_INTER_PLUS_MV:
 /* all 6 fragments use the same motion vector */
 if (coding_mode == 0) {
-motion_x[0] = motion_vector_table[get_vlc2(gb, 
s->motion_vector_vlc.table, 6, 2)];
-motion_y[0] = motion_vector_table[get_vlc2(gb, 
s->motion_vector_vlc.table, 6, 2)];
+motion_x[0] = get_vlc2(gb, s->motion_vector_vlc.table,
+   VP3_MV_VLC_BITS, 2);
+motion_y[0] = get_vlc2(gb, s->motion_vector_vlc.table,
+   VP3_MV_VLC_BITS, 2);
 } else if (coding_mode == 1) {
 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 
6)];
 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 
6)];
@@ -976,8 +979,10 @@ static int unpack_vectors(Vp3DecodeContext *s, 
GetBitContext *gb)
 current_fragment = BLOCK_Y * s->fragment_width[0] + 
BLOCK_X;
 if (s->all_fragments[current_fragment].coding_method 
!= MODE_COPY) {
 if (coding_mode == 0) {
-motion_x[k] = motion_vector_table[get_vlc2(gb, 
s->motion_vector_vlc.table, 6, 2)];
-motion_y[k] = motion_vector_table[get_vlc2(gb, 
s->motion_vector_vlc.table, 6, 2)];
+motion_x[k] = get_vlc2(gb, 
s->motion_vector_vlc.table,
+   VP3_MV_VLC_BITS, 2);
+motion_y[k] = get_vlc2(gb, 
s->motion_vector_vlc.table,
+   VP3_MV_VLC_BITS, 2);
 } else if (coding_mode == 1) {
 motion_x[k] = 
fixed_motion_vector_table[get_bits(gb, 6)];
 motion_y[k] = 
fixed_motion_vector_table[get_bits(gb, 6)];
@@ -2477,9 +2482,10 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
 if (ret < 0)
 return ret;
 
-if ((ret = init_vlc(&s->motion_vector_vlc, 6, 63,
-&motion_vector_vlc_table[0][1], 2, 1,
-&motion_vector_vlc_table[0][0], 2, 1, 0)) < 0)
+ret = ff_init_vlc_from_lengths(&s->motion_vector_vlc, VP3_MV_VLC_BITS, 63,
+   &motion_vector_vlc_table[0][1], 2,
+   &motion_vector_vlc_table[0][0], 2, 1, -31, 
0);
+if (ret < 0)
 return ret;
 
 #if CONFIG_VP4_DECODER
diff --git a/libavcodec/vp3data.h b/libavcodec/vp3data.h
index eb516a0add..901850bd75 100644
--- a/libavcodec/vp3data.h
+++ b/libavcodec/vp3data.h
@@ -112,41 +112,17 @@ static const uint8_t mode_code_vlc_len[8] = {
 };
 
 static const uint8_t motion_vector_vlc_table[63][2] = {
-{0, 3 },
-{1, 3 },
-{2, 3 },
-
-{6, 4 }, {7, 4 },
-
-{8, 4 }, {9, 4 },
-
-{   40, 6 }, {   41, 6 }, {   42, 6 }, {   43, 6 },
-{   44, 6 }, {   45, 6 }, {   46, 6 }, {   47, 6 },
-
-{   96, 7 }, {   97, 7 }, {   98, 7 }, {   99, 7 },
-{  100, 7 }, {  101, 7 }, {  102, 7 }, {  103, 7 },
-{  104, 7 }, {  105, 7 }, {  106, 7 }, {  107, 7 },
-{  108, 7 }, {  109, 7 }, {  110, 7 }, {  111, 7 },
-
-{ 0xE0, 8 }, { 0xE1, 8 }, { 0xE2, 8 }, { 0xE3, 8 },
-{ 0xE4, 8 }, { 0xE5, 8 }, { 0xE6, 8 }, { 0xE7, 8 },
-{ 0xE8, 8 }, { 0xE9, 8 }, { 0xEA, 8 }, { 0xEB, 8 },
-{ 0xEC, 8 }, { 0xED, 8 }, { 0xEE, 8 }, { 0xEF, 8 },
-
-{ 0xF0, 8 }, { 0xF1, 8 }, { 0xF2, 8 }, { 0xF3, 8 },
-{ 0xF4, 8 }, { 0xF5, 8 }, { 0xF6, 8 }, { 0xF7, 8 },
-{ 0xF8, 8 }, { 0xF9, 8 }, { 0xFA, 8 }, { 0xFB, 8 },
-{ 0xFC, 8 }, { 0xFD, 8 }, { 0xFE, 8 }, { 0xFF, 8 }
-};
-
-static const int8_t 

[FFmpeg-devel] [PATCH 101/114] avcodec/vp3: Remove code duplication when initializing Theora VLCs

2020-11-10 Thread Andreas Rheinhardt
theora_init_huffman_tables() does essentially the same as
ff_init_vlcs_from_lengths().

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/vp3.c | 20 +---
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index f288a53fe1..743d00390c 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -2319,20 +2319,6 @@ static av_cold int init_frames(Vp3DecodeContext *s)
 return 0;
 }
 
-static av_cold int theora_init_huffman_tables(VLC *vlc, const HuffTable *huff)
-{
-uint32_t code = 0, codes[32];
-
-for (unsigned i = 0; i < huff->nb_entries; i++) {
-codes[i] = code>> (31 - huff->entries[i].len);
-code+= 0x8000U >> huff->entries[i].len;
-}
-return ff_init_vlc_sparse(vlc, 11, huff->nb_entries,
-  &huff->entries[0].len, sizeof(huff->entries[0]), 
1,
-  codes, 4, 4,
-  &huff->entries[0].sym, sizeof(huff->entries[0]), 
1, 0);
-}
-
 static av_cold int vp3_decode_init(AVCodecContext *avctx)
 {
 Vp3DecodeContext *s = avctx->priv_data;
@@ -2458,7 +2444,11 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
 }
 } else {
 for (i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) {
-ret = theora_init_huffman_tables(&s->coeff_vlc[i], 
&s->huffman_table[i]);
+const HuffTable *tab = &s->huffman_table[i];
+
+ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i], 11, 
tab->nb_entries,
+   &tab->entries[0].len, 
sizeof(*tab->entries),
+   &tab->entries[0].sym, 
sizeof(*tab->entries), 1, 0, 0);
 if (ret < 0)
 return ret;
 }
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

  1   2   >