History

avcodec/vlc: Add functions to init static VLCElem[] without VLC

For lots of static VLCs, the number of bits is not read from VLC.bits, but rather a compile-constant that is hardcoded at the callsite of get_vlc2(). Only VLC.table is ever used and not using it directly is just an unnecessary indirection. This commit adds helper functions and macros to avoid the VLC structure when initializing VLC tables; there are 2x2 functions: Two choices for init_sparse or from_lengths and two choices for "overlong" initialization (as used when multiple VLCs are initialized that share the same underlying table).

Andreas Rheinhardt committed 3 years ago

Commit edc50651 parent 424c8ce2 files · +170 −0Browse files

Unified · Split

2 files
  1. libavcodec/vlc.c +68 −0
  2. libavcodec/vlc.h +102 −0
libavcodec/vlc.c+68−0
@@ -350,6 +350,74 @@ fail: · 349 unmodified lines
350 return AVERROR_INVALIDDATA;350 return AVERROR_INVALIDDATA;
351}351}
352 352
353av_cold void ff_vlc_init_table_from_lengths(VLCElem table[], int table_size,
354 int nb_bits, int nb_codes,
355 const int8_t *lens, int lens_wrap,
356 const void *symbols, int symbols_wrap, int symbols_size,
357 int offset, int flags)
358{
359 VLC vlc = { .table = table, .table_allocated = table_size };
360
361 ff_vlc_init_from_lengths(&vlc, nb_bits, nb_codes, lens, lens_wrap,
362 symbols, symbols_wrap, symbols_size,
363 offset, flags | VLC_INIT_USE_STATIC, NULL);
364}
365
366av_cold const VLCElem *ff_vlc_init_tables_from_lengths(VLCInitState *state,
367 int nb_bits, int nb_codes,
368 const int8_t *lens, int lens_wrap,
369 const void *symbols, int symbols_wrap, int symbols_size,
370 int offset, int flags)
371{
372 VLC vlc = { .table = state->table, .table_allocated = state->size };
373
374 ff_vlc_init_from_lengths(&vlc, nb_bits, nb_codes, lens, lens_wrap,
375 symbols, symbols_wrap, symbols_size,
376 offset, flags | VLC_INIT_STATIC_OVERLONG, NULL);
377
378 state->table += vlc.table_size;
379 state->size -= vlc.table_size;
380
381 return vlc.table;
382}
383
384av_cold void ff_vlc_init_table_sparse(VLCElem table[], int table_size,
385 int nb_bits, int nb_codes,
386 const void *bits, int bits_wrap, int bits_size,
387 const void *codes, int codes_wrap, int codes_size,
388 const void *symbols, int symbols_wrap, int symbols_size,
389 int flags)
390{
391 VLC vlc = { .table = table, .table_allocated = table_size };
392
393 ff_vlc_init_sparse(&vlc, nb_bits, nb_codes,
394 bits, bits_wrap, bits_size,
395 codes, codes_wrap, codes_size,
396 symbols, symbols_wrap, symbols_size,
397 flags | VLC_INIT_USE_STATIC);
398}
399
400av_cold const VLCElem *ff_vlc_init_tables_sparse(VLCInitState *state,
401 int nb_bits, int nb_codes,
402 const void *bits, int bits_wrap, int bits_size,
403 const void *codes, int codes_wrap, int codes_size,
404 const void *symbols, int symbols_wrap, int symbols_size,
405 int flags)
406{
407 VLC vlc = { .table = state->table, .table_allocated = state->size };
408
409 ff_vlc_init_sparse(&vlc, nb_bits, nb_codes,
410 bits, bits_wrap, bits_size,
411 codes, codes_wrap, codes_size,
412 symbols, symbols_wrap, symbols_size,
413 flags | VLC_INIT_STATIC_OVERLONG);
414
415 state->table += vlc.table_size;
416 state->size -= vlc.table_size;
417
418 return vlc.table;
419}
420
353static void add_level(VLC_MULTI_ELEM *table, const int is16bit,421static void add_level(VLC_MULTI_ELEM *table, const int is16bit,
354 const int num, const int numbits,422 const int num, const int numbits,
355 const VLCcode *buf,423 const VLCcode *buf,
libavcodec/vlc.h+102−0
@@ -19,8 +19,11 @@ · 18 unmodified lines
19#ifndef AVCODEC_VLC_H19#ifndef AVCODEC_VLC_H
20#define AVCODEC_VLC_H20#define AVCODEC_VLC_H
21 21
22#include <stddef.h>
22#include <stdint.h>23#include <stdint.h>
23 24
25#include "libavutil/macros.h"
26
24#define VLC_MULTI_MAX_SYMBOLS 627#define VLC_MULTI_MAX_SYMBOLS 6
25 28
26// When changing this, be sure to also update tableprint_vlc.h accordingly.29// When changing this, be sure to also update tableprint_vlc.h accordingly.
@@ -223,4 +226,103 @@ void ff_vlc_free(VLC *vlc); · 196 unmodified lines
223 NULL); \226 NULL); \
224 } while (0)227 } while (0)
225 228
229/**
230 * For static VLCs, the number of bits can often be hardcoded
231 * at each get_vlc2() callsite. Then using a full VLC would be uneconomical,
232 * because only VLC.table would ever be accessed after initialization.
233 * The following functions provide wrappers around the relevant ff_vlc_init_*
234 * functions suitable for said task.
235 *
236 * The ff_vlc_init_tables_* functions are intended to be used for initializing
237 * a series of VLCs. The user initializes a VLCInitState with the details
238 * about the underlying array of VLCElem; it is automatically updated by
239 * the ff_vlc_init_tables_* functions (i.e. table is incremented and size
240 * decremented by the number of elements of the current table).
241 * The VLC_INIT_STATIC_OVERLONG flag is also automatically added.
242 * These functions return a pointer to the table just initialized,
243 * potentially to be used in arrays of pointer to VLC tables.
244 *
245 * The ff_vlc_init_table_* functions are intended to be used for initializing
246 * a single VLC table, given by table and table_size. The VLC_INIT_USE_STATIC
247 * flag is automatically added.
248 */
249
250typedef struct VLCInitState {
251 VLCElem *table; ///< points to where the next VLC table will be placed
252 unsigned size; ///< remaining number of elements in table
253} VLCInitState;
254
255#define VLC_INIT_STATE(_table) { .table = (_table), .size = FF_ARRAY_ELEMS(_table) }
256
257void ff_vlc_init_table_from_lengths(VLCElem table[], int table_size,
258 int nb_bits, int nb_codes,
259 const int8_t *lens, int lens_wrap,
260 const void *symbols, int symbols_wrap, int symbols_size,
261 int offset, int flags);
262
263const VLCElem *ff_vlc_init_tables_from_lengths(VLCInitState *state,
264 int nb_bits, int nb_codes,
265 const int8_t *lens, int lens_wrap,
266 const void *symbols, int symbols_wrap, int symbols_size,
267 int offset, int flags);
268
269void ff_vlc_init_table_sparse(VLCElem table[], int table_size,
270 int nb_bits, int nb_codes,
271 const void *bits, int bits_wrap, int bits_size,
272 const void *codes, int codes_wrap, int codes_size,
273 const void *symbols, int symbols_wrap, int symbols_size,
274 int flags);
275
276const VLCElem *ff_vlc_init_tables_sparse(VLCInitState *state,
277 int nb_bits, int nb_codes,
278 const void *bits, int bits_wrap, int bits_size,
279 const void *codes, int codes_wrap, int codes_size,
280 const void *symbols, int symbols_wrap, int symbols_size,
281 int flags);
282
283static inline
284const VLCElem *ff_vlc_init_tables(VLCInitState *state,
285 int nb_bits, int nb_codes,
286 const void *bits, int bits_wrap, int bits_size,
287 const void *codes, int codes_wrap, int codes_size,
288 int flags)
289{
290 return ff_vlc_init_tables_sparse(state, nb_bits, nb_codes,
291 bits, bits_wrap, bits_size,
292 codes, codes_wrap, codes_size,
293 NULL, 0, 0, flags);
294}
295
296#define VLC_INIT_STATIC_SPARSE_TABLE(vlc_table, nb_bits, nb_codes, \
297 bits, bits_wrap, bits_size, \
298 codes, codes_wrap, codes_size, \
299 symbols, symbols_wrap, symbols_size, \
300 flags) \
301 ff_vlc_init_table_sparse(vlc_table, FF_ARRAY_ELEMS(vlc_table), \
302 (nb_bits), (nb_codes), \
303 (bits), (bits_wrap), (bits_size), \
304 (codes), (codes_wrap), (codes_size), \
305 (symbols), (symbols_wrap), (symbols_size), \
306 (flags))
307
308#define VLC_INIT_STATIC_TABLE(vlc_table, nb_bits, nb_codes, \
309 bits, bits_wrap, bits_size, \
310 codes, codes_wrap, codes_size, \
311 flags) \
312 ff_vlc_init_table_sparse(vlc_table, FF_ARRAY_ELEMS(vlc_table), \
313 (nb_bits), (nb_codes), \
314 (bits), (bits_wrap), (bits_size), \
315 (codes), (codes_wrap), (codes_size), \
316 NULL, 0, 0, (flags))
317
318#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, \
319 lens, lens_wrap, \
320 syms, syms_wrap, syms_size, \
321 offset, flags) \
322 ff_vlc_init_table_from_lengths(vlc_table, FF_ARRAY_ELEMS(vlc_table), \
323 (nb_bits), (nb_codes), \
324 (lens), (lens_wrap), \
325 (syms), (syms_wrap), (syms_size), \
326 (offset), (flags))
327
226#endif /* AVCODEC_VLC_H */328#endif /* AVCODEC_VLC_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t