1 /*****************************************************************************
2 * vlc.c: VLC lookup table generation.
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: vlc.c,v 1.1 2004/06/03 19:27:07 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
28 #include "common/common.h"
29 #include "common/vlc.h"
33 static int vlc_table_realloc( x264_vlc_table_t *table, int i_size )
37 i_index = table->i_lookup;
39 table->i_lookup += i_size;
40 table->lookup = x264_realloc( table->lookup, sizeof( vlc_lookup_t ) * table->i_lookup );
45 static int vlc_table_create_part( x264_vlc_table_t *table, const vlc_t *vlc, int i_lookup_bits, int i_nb_vlc, int i_prefix_code, int i_prefix_length )
52 i_nb_lookup = 1 << i_lookup_bits;
54 i_table_index = vlc_table_realloc( table, i_nb_lookup );
55 lookup = &table->lookup[i_table_index];
57 for( i = 0; i < i_nb_lookup; i++ )
59 lookup[i].i_value = -1;
63 for( i = 0; i < i_nb_vlc; i++ )
66 if( vlc[i].i_size <= 0 )
71 i_bits = vlc[i].i_size - i_prefix_length;
72 if( i_bits > 0 && ( vlc[i].i_bits >> i_bits ) == i_prefix_code )
74 if( i_bits <= i_lookup_bits )
79 i_lookup_index = ( vlc[i].i_bits << ( i_lookup_bits - i_bits ) )%i_nb_lookup;
80 nb = 1 << ( i_lookup_bits - i_bits );
81 for( nb = 0; nb < (1 << ( i_lookup_bits - i_bits)); nb++ )
83 lookup[i_lookup_index].i_value = i; /* vlc[i].i_value; */
84 lookup[i_lookup_index].i_size = i_bits;
92 /* need another table */
93 i_lookup_index = ( vlc[i].i_bits >> (i_bits - i_lookup_bits ) )%i_nb_lookup;
95 i_bits_max = -lookup[i_lookup_index].i_size;
96 if( i_bits_max < i_bits - i_lookup_bits )
98 i_bits_max = i_bits - i_lookup_bits;
100 lookup[i_lookup_index].i_size = -i_bits_max;
105 /* create other level table */
106 for( i = 0; i < i_nb_lookup; i++ )
108 if( lookup[i].i_size < 0 )
112 i_bits = -lookup[i].i_size;
113 if( i_bits > i_lookup_bits )
115 lookup[i].i_size = -i_lookup_bits;
116 i_bits = i_lookup_bits;
119 i_index = vlc_table_create_part( table, vlc, i_bits, i_nb_vlc,
120 (i_prefix_code << i_lookup_bits)|i,
121 i_lookup_bits+i_prefix_length );
122 lookup = &table->lookup[i_table_index]; // reallocated
123 lookup[i].i_value = i_index;
127 return( i_table_index );
131 x264_vlc_table_t *x264_vlc_table_lookup_new( const vlc_t *vlc, int i_vlc, int i_lookup_bits )
133 x264_vlc_table_t *table = x264_malloc( sizeof( x264_vlc_table_t ) );
135 table->i_lookup_bits = i_lookup_bits;
137 table->lookup = NULL;
139 vlc_table_create_part( table, vlc, i_lookup_bits, i_vlc, 0, 0 );
144 void x264_vlc_table_lookup_delete( x264_vlc_table_t *table )
146 x264_free( table->lookup );
151 void x264_vlc_table_lookup_print( x264_vlc_table_t *table )
155 fprintf( stderr, " " );
156 for( idx = 0; idx < table->i_lookup; idx++ )
158 if( table->lookup[idx].i_value == -1 )
160 fprintf( stderr, " MKVLCLU( -1, 0 )," );
164 fprintf( stderr, " MKVLCLU( 0x%.3x, % 2d ),", table->lookup[idx].i_value, table->lookup[idx].i_size );
166 if( (idx+1)%4 == 0 && idx < table->i_lookup - 1)
168 fprintf( stderr, "\n " );
171 fprintf( stderr, "\n" );
177 x264_vlc_table_t *table;
180 printf( "typedef struct\n int i_value;\n int i_size;\n} vlc_lookup_t;\n\n#define MKVLCLU(a,b) { .i_value=a, .i_size=b}" );
182 /* create vlc entry table and then vlc_lookup_t table */
184 /* x264_coeff_token */
185 fprintf( stderr, "static const vlc_lookup_t x264_coeff_token_lookup[5][]=\n{\n" );
186 for( i = 0; i < 5; i++ )
188 fprintf( stderr, " {\n" );
189 table = x264_vlc_table_lookup_new( x264_coeff_token[i], 17*4, 6 );
190 x264_vlc_table_lookup_print( table );
191 x264_vlc_table_lookup_delete( table );
192 fprintf( stderr, " },\n" );
194 fprintf( stderr, "};\n" );
198 vlce = convert_vlc_to_vlce( x264_level_prefix, 16 );
199 do_vlc_table_create( vlce, 16, "x264_level_prefix_lookup", 8 );
202 for( i_table = 0; i_table < 15; i_table++ )
205 vlce = convert_vlc_to_vlce( x264_total_zeros[i_table], 16 );
206 sprintf( name, "x264_total_zeros_%d", i_table );
207 do_vlc_table_create( vlce, 16, name, 6 );
212 for( i_table = 0; i_table < 3; i_table++ )
216 vlce = convert_vlc_to_vlce( x264_total_zeros_dc[i_table], 4 );
217 sprintf( name, "x264_total_zeros_dc_%d", i_table );
218 do_vlc_table_create( vlce, 4, name, 3 );
223 for( i_table = 0; i_table < 7; i_table++ )
226 vlce = convert_vlc_to_vlce( x264_run_before[i_table], 15 );
227 sprintf( name, "x264_run_before_%d", i_table );
228 do_vlc_table_create( vlce, 15, name, 6 );