diff options
author | Andreas Rheinhardt <andreas.rheinhardt@gmail.com> | 2020-06-25 13:20:19 +0200 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@gmail.com> | 2020-06-27 23:31:34 +0200 |
commit | d7ad70e33b6f3ced421d2be67a067d0940682bb0 (patch) | |
tree | 26f570c586a4d4b3b6ed6265ff227735a182a07d /libavcodec | |
parent | 5e196dac22cc510db104922f99626a03b453ef4a (diff) | |
download | ffmpeg-d7ad70e33b6f3ced421d2be67a067d0940682bb0.tar.gz |
avcodec/bitstream: Avoid allocation when creating VLC tables
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Diffstat (limited to 'libavcodec')
-rw-r--r-- | libavcodec/bitstream.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c index 53a2db7451..d379dbc0e8 100644 --- a/libavcodec/bitstream.c +++ b/libavcodec/bitstream.c @@ -285,7 +285,6 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, vlc->bits = nb_bits; if (flags & INIT_VLC_USE_NEW_STATIC) { av_assert0(nb_codes + 1 <= FF_ARRAY_ELEMS(localbuf)); - buf = localbuf; localvlc = *vlc_arg; vlc = &localvlc; vlc->table_size = 0; @@ -293,11 +292,13 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, vlc->table = NULL; vlc->table_allocated = 0; vlc->table_size = 0; - + } + if (nb_codes + 1 > FF_ARRAY_ELEMS(localbuf)) { buf = av_malloc_array((nb_codes + 1), sizeof(VLCcode)); if (!buf) return AVERROR(ENOMEM); - } + } else + buf = localbuf; av_assert0(symbols_size <= 2 || !symbols); @@ -309,7 +310,7 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, continue; \ if (buf[j].bits > 3*nb_bits || buf[j].bits>32) { \ av_log(NULL, AV_LOG_ERROR, "Too long VLC (%d) in init_vlc\n", buf[j].bits);\ - if (!(flags & INIT_VLC_USE_NEW_STATIC)) \ + if (buf != localbuf) \ av_free(buf); \ return AVERROR(EINVAL); \ } \ @@ -317,7 +318,7 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, if (buf[j].code >= (1LL<<buf[j].bits)) { \ av_log(NULL, AV_LOG_ERROR, "Invalid code %"PRIx32" for %d in " \ "init_vlc\n", buf[j].code, i); \ - if (!(flags & INIT_VLC_USE_NEW_STATIC)) \ + if (buf != localbuf) \ av_free(buf); \ return AVERROR(EINVAL); \ } \ @@ -346,7 +347,8 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, av_assert0(ret >= 0); *vlc_arg = *vlc; } else { - av_free(buf); + if (buf != localbuf) + av_free(buf); if (ret < 0) { av_freep(&vlc->table); return ret; |