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

2020-11-18 Thread Jerome Borsboom
I don't think it is a good idea to change the format of the table to 
something different than the tables in the SMPTE VC-1 documentation. 
People fluent in this documentation have a hard time correlating the new 
tables to the 'official' ones.


---
Regards,
Jerome


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(-)


...
 
 /* 4MV Block pattern VLC tables */

-const uint8_t ff_vc1_4mv_block_pattern_codes[4][16] = {
-{ 14, 58, 59, 25, 12, 26, 15, 15, 13, 24, 27,  0, 28,  1,  2,  2 },
-{  8, 18, 19,  4, 20,  5, 30, 11, 21, 31,  6, 12,  7, 13, 14,  0 },
-{ 15,  6,  7,  2,  8,  3, 28,  9, 10, 29,  4, 11,  5, 12, 13,  0 },
-{  0, 11, 12,  4, 13,  5, 30, 16, 14, 31,  6, 17,  7, 18, 19, 10 }
-};
-const uint8_t ff_vc1_4mv_block_pattern_bits[4][16] = {
-{ 5, 6, 6, 5, 5, 5, 5, 4, 5, 5, 5, 3, 5, 3, 3, 2 },
-{ 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4, 4, 4, 4, 4, 2 },
-{ 4, 4, 4, 4, 4, 4, 5, 4, 4, 5, 4, 4, 4, 4, 4, 3 },
-{ 2, 4, 4, 4, 4, 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4 }
+const uint8_t ff_vc1_4mv_block_pattern_tabs[4][16][2] = {
+{
+{ 0x0B, 3 }, { 0x0D, 3 }, { 0x0E, 3 }, { 0x04, 5 }, { 0x08, 5 },
+{ 0x00, 5 }, { 0x06, 5 }, { 0x0F, 2 }, { 0x09, 5 }, { 0x03, 5 },
+{ 0x05, 5 }, { 0x0A, 5 }, { 0x0C, 5 }, { 0x01, 6 }, { 0x02, 6 },
+{ 0x07, 4 },
+},
+{
+{ 0x0F, 2 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
+{ 0x00, 4 }, { 0x01, 5 }, { 0x02, 5 }, { 0x04, 5 }, { 0x08, 5 },
+{ 0x07, 4 }, { 0x0B, 4 }, { 0x0D, 4 }, { 0x0E, 4 }, { 0x06, 5 },
+{ 0x09, 5 },
+},
+{
+{ 0x0F, 3 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
+{ 0x01, 4 }, { 0x02, 4 }, { 0x04, 4 }, { 0x07, 4 }, { 0x08, 4 },
+{ 0x0B, 4 }, { 0x0D, 4 }, { 0x0E, 4 }, { 0x06, 5 }, { 0x09, 5 },
+{ 0x00, 4 },
+},
+{
+{ 0x00, 2 }, { 0x03, 4 }, { 0x05, 4 }, { 0x0A, 4 }, { 0x0C, 4 },
+{ 0x07, 5 }, { 0x0B, 5 }, { 0x0D, 5 }, { 0x0E, 5 }, { 0x0F, 4 },
+{ 0x01, 4 }, { 0x02, 4 }, { 0x04, 4 }, { 0x08, 4 }, { 0x06, 5 },
+{ 0x09, 5 },
+},
 };
 
 /* 2MV Block pattern VLC tables */

-const uint8_t ff_vc1_2mv_block_pattern_codes[4][4] = {
-{ 2, 1, 0, 3 }, { 1, 0, 2, 3 }, { 2, 0, 3, 1 }, { 1, 3, 2, 0 }
-};
-
-const uint8_t ff_vc1_2mv_block_pattern_bits[4][4] = {
-{ 2, 2, 2, 2 }, { 1, 2, 3, 3 }, { 3, 2, 3, 1 }, { 1, 3, 3, 2 }
+const uint8_t ff_vc1_2mv_block_pattern_tabs[4][4][2] = {
+{ { 0x02, 2 }, { 0x01, 2 }, { 0x00, 2 }, { 0x03, 2 } },
+{ { 0x01, 2 }, { 0x02, 3 }, { 0x03, 3 }, { 0x00, 1 } },
+{ { 0x01, 2 }, { 0x00, 3 }, { 0x02, 3 }, { 0x03, 1 } },
+{ { 0x03, 2 }, { 0x02, 3 }, { 0x01, 3 }, { 0x00, 1 } },
 };
 
 /* Interlaced frame picture 4MV MBMODE VLC tables (tables 160-163) */

@@ -350,46 +349,42 @@ const uint8_t ff_vc1_intfr_non4mv_mbmode_tabs[4][9][2] = {
 
 /* Interlaced field picture MBMODE VLC tables (p. 356 - 11.4.1, 11.4.2) */

 /* mixed-MV */
-const uint8_t ff_vc1_if_mmv_mbmode_codes[8][8] = {
-{ 16, 17,  3,  3,  0,  5,  9,  2 },
-{  8,  9,  3,  6,  7,  0,  5,  2 },
-{ 16, 17,  5,  3,  0,  3,  9,  2 },
-{ 56, 57, 15,  4,  5,  6, 29,  0 },
-{ 52, 53, 27, 14, 15,  2, 12,  0 },
-{ 56, 57, 29,  5,  6,  0, 15,  4 },
-{ 16, 17,  6,  7,  0,  1,  9,  5 },
-{ 56, 57,  0,  5,  6, 29,  4, 15 }
-};
-const uint8_t ff_vc1_if_mmv_mbmode_bits[8][8] = {
-{ 6, 6, 2, 3, 2, 4, 5, 2 },
-{ 5, 5, 3, 3, 3, 2, 4, 2 },
-{ 6, 6, 4, 3, 2, 2, 5, 2 },
-{ 6, 6, 4, 3, 3, 3, 5, 1 },
-{ 6, 6, 5, 4, 4, 2, 4, 1 },
-{ 6, 6, 5, 3, 3, 1, 4, 3 },
-{ 5, 5, 3, 3, 2, 2, 4, 3 },
-{ 6, 6, 1, 3, 3, 5, 3, 4 }
+const uint8_t ff_vc1_if_mmv_mbmode_tabs[8][8][2] = {
+{ { 0x04, 2 }, { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 },
+  { 0x05, 4 }, { 0x03, 3 }, { 0x07, 2 }, { 0x02, 2 } },
+{ { 0x05, 2 }, { 0x00, 5 }, { 0x01, 5 }, { 0x06, 4 },
+  { 0x02, 3 }, { 0x07, 2 }, { 0x03, 3 }, { 0x04, 3 } },
+{ { 0x04, 2 }, { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 },
+  { 0x02, 4 }, { 0x03, 3 }, { 0x07, 2 }, { 0x05, 2 } },
+{ { 0x07, 1 }, { 0x03, 3 }, { 0x04, 3 }, { 0x05, 3 },
+  { 0x00, 6 }, { 0x01, 6 }, { 0x06, 5 }, { 0x02, 4 } },
+{ { 0x07, 1 }, { 0x05, 2 }, { 0x06, 4 }, { 0x00, 6 },
+  { 0x01, 

[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   = _buf[*buf_offset];
+vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
+
+ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
+ [0][1], 2, [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(_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(_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(_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
- _vc1_norm6_tab[0][1], 2,
- _vc1_norm6_tab[0][0], 2, 1, 0, 0, 556);
-INIT_VLC_STATIC(_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(_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
+ , ff_vc1_bfraction_tab, 0);
+vc1_init_vlc(_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
+ , ff_vc1_norm2_tab, 0);
+vc1_init_vlc(_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 77,
+ , ff_vc1_norm6_tab, 0);
+vc1_init_vlc(_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
+ , ff_vc1_imode_tab, 0);
 for (i = 0; i < 3; i++) {
-ff_vc1_ttmb_vlc[i].table   = _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(_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 
16,
- _vc1_ttmb_tabs[i][0][1], 2,
- _vc1_ttmb_tabs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
-ff_vc1_ttblk_vlc[i].table   = _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(_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 
8,
- _vc1_ttblk_tabs[i][0][1], 2,
- _vc1_ttblk_tabs[i][0][0], 2, 1,
- 0, INIT_VLC_USE_NEW_STATIC);
-ff_vc1_subblkpat_vlc[i].table   = _table[vlc_offs[i * 
3 + 2]];
-ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i * 3 + 3] - 
vlc_offs[i * 3 + 2];
-