]> git.sesse.net Git - x264/blob - decoder/vlc.c
* all: re-import of the CVS.
[x264] / decoder / vlc.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <inttypes.h>
27
28 #include "../core/common.h"
29 #include "../core/vlc.h"
30 #include "vlc.h"
31
32
33 static int  vlc_table_realloc( x264_vlc_table_t *table, int i_size )
34 {
35     int i_index;
36
37     i_index = table->i_lookup;
38
39     table->i_lookup += i_size;
40     table->lookup = x264_realloc( table->lookup, sizeof( vlc_lookup_t ) * table->i_lookup );
41
42     return( i_index );
43 }
44
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 )
46 {
47     int i;
48     int i_nb_lookup;
49     vlc_lookup_t *lookup;
50     int i_table_index;
51
52     i_nb_lookup = 1 << i_lookup_bits;
53
54     i_table_index = vlc_table_realloc( table, i_nb_lookup );
55     lookup = &table->lookup[i_table_index];
56
57     for( i = 0; i < i_nb_lookup; i++ )
58     {
59         lookup[i].i_value  = -1;
60         lookup[i].i_size = 0;
61     }
62
63     for( i = 0; i < i_nb_vlc; i++ )
64     {
65         int i_bits;
66         if( vlc[i].i_size <= 0 )
67         {
68             continue;
69         }
70
71         i_bits = vlc[i].i_size - i_prefix_length;
72         if( i_bits > 0 && ( vlc[i].i_bits >> i_bits ) == i_prefix_code )
73         {
74             if( i_bits <= i_lookup_bits )
75             {
76                 int i_lookup_index;
77                 int nb;
78
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++ )
82                 {
83                     lookup[i_lookup_index].i_value = i; /* vlc[i].i_value; */
84                     lookup[i_lookup_index].i_size = i_bits;
85                     i_lookup_index++;
86                 }
87             }
88             else
89             {
90                 int i_bits_max;
91                 int i_lookup_index;
92                 /* need another table */
93                 i_lookup_index = ( vlc[i].i_bits >> (i_bits - i_lookup_bits ) )%i_nb_lookup;
94
95                 i_bits_max =  -lookup[i_lookup_index].i_size;
96                 if( i_bits_max < i_bits - i_lookup_bits )
97                 {
98                     i_bits_max = i_bits - i_lookup_bits;
99                 }
100                 lookup[i_lookup_index].i_size = -i_bits_max;
101             }
102         }
103     }
104
105     /* create other level table */
106     for( i = 0; i < i_nb_lookup; i++ )
107     {
108         if( lookup[i].i_size < 0 )
109         {
110             int i_bits;
111             int i_index;
112             i_bits = -lookup[i].i_size;
113             if( i_bits > i_lookup_bits )
114             {
115                 lookup[i].i_size = -i_lookup_bits;
116                 i_bits = i_lookup_bits;
117             }
118
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;
124         }
125     }
126
127     return( i_table_index );
128 }
129
130
131 x264_vlc_table_t *x264_vlc_table_lookup_new( const vlc_t *vlc, int i_vlc, int i_lookup_bits )
132 {
133     x264_vlc_table_t *table = x264_malloc( sizeof( x264_vlc_table_t ) );
134
135     table->i_lookup_bits = i_lookup_bits;
136     table->i_lookup = 0;
137     table->lookup   = NULL;
138
139     vlc_table_create_part( table, vlc, i_lookup_bits, i_vlc, 0, 0 );
140
141     return table;
142 }
143
144 void x264_vlc_table_lookup_delete( x264_vlc_table_t *table )
145 {
146     x264_free( table->lookup );
147     x264_free( table );
148 }
149
150 #if 0
151 void x264_vlc_table_lookup_print( x264_vlc_table_t *table )
152 {
153     int idx;
154
155     fprintf( stderr, "       " );
156     for( idx = 0; idx < table->i_lookup; idx++ )
157     {
158         if( table->lookup[idx].i_value == -1 )
159         {
160             fprintf( stderr, " MKVLCLU(    -1,  0 )," );
161         }
162         else
163         {
164             fprintf( stderr, " MKVLCLU( 0x%.3x, % 2d ),", table->lookup[idx].i_value, table->lookup[idx].i_size );
165         }
166         if( (idx+1)%4 == 0 && idx < table->i_lookup - 1)
167         {
168             fprintf( stderr, "\n       " );
169         }
170     }
171     fprintf( stderr, "\n" );
172 }
173
174 int main(void)
175 {
176     int i;
177     x264_vlc_table_t *table;
178
179
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}" );
181
182     /* create vlc  entry table and then vlc_lookup_t table */
183
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++ )
187     {
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" );
193     }
194     fprintf( stderr, "};\n" );
195
196 #if 0
197
198     vlce = convert_vlc_to_vlce( x264_level_prefix, 16 );
199     do_vlc_table_create( vlce, 16, "x264_level_prefix_lookup", 8 );
200     free( vlce );
201
202     for( i_table = 0; i_table < 15; i_table++ )
203     {
204         char name[512];
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 );
208
209         free( vlce );
210     }
211
212     for( i_table = 0; i_table < 3; i_table++ )
213     {
214         char name[512];
215
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 );
219
220         free( vlce );
221     }
222
223     for( i_table = 0; i_table < 7; i_table++ )
224     {
225         char name[512];
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 );
229
230         free( vlce );
231     }
232 #endif
233     return 0;
234 }
235
236 #endif