X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fbitstream.c;h=a04c4a0ddf92dacab7637fac7512d546d84ece98;hb=86decad65240ea9e1bea177f1091f80d08b62402;hp=cb8037609379a8d9bc7712f48159b60ca9e2df7b;hpb=b613bacca9c256f1483c46847f713e47a0e9a5f6;p=ffmpeg diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c index cb803760937..a04c4a0ddf9 100644 --- a/libavcodec/bitstream.c +++ b/libavcodec/bitstream.c @@ -1,8 +1,10 @@ /* * Common bit i/o utils - * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2000, 2001 Fabrice Bellard * Copyright (c) 2002-2004 Michael Niedermayer * + * alternative bitstream reader & writer by Michael Niedermayer + * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or @@ -18,28 +20,23 @@ * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * alternative bitstream reader & writer by Michael Niedermayer */ /** - * @file bitstream.c + * @file libavcodec/bitstream.c * bitstream api. */ #include "avcodec.h" -#include "bitstream.h" +#include "get_bits.h" +#include "put_bits.h" -/** - * Same as av_mallocz_static(), but does a realloc. - * - * @param[in] ptr The block of memory to reallocate. - * @param[in] size The requested size. - * @return Block of memory of requested size. - * @deprecated. Code which uses ff_realloc_static is broken/missdesigned - * and should correctly use static arrays - */ -attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size); +const uint8_t ff_log2_run[32]={ + 0, 0, 0, 0, 1, 1, 1, 1, + 2, 2, 2, 2, 3, 3, 3, 3, + 4, 4, 5, 5, 6, 6, 7, 7, + 8, 9,10,11,12,13,14,15 +}; void align_put_bits(PutBitContext *s) { @@ -50,14 +47,35 @@ void align_put_bits(PutBitContext *s) #endif } -void ff_put_string(PutBitContext * pbc, char *s, int put_zero) +void ff_put_string(PutBitContext *pb, const char *string, int terminate_string) { - while(*s){ - put_bits(pbc, 8, *s); - s++; + while(*string){ + put_bits(pb, 8, *string); + string++; } - if(put_zero) - put_bits(pbc, 8, 0); + if(terminate_string) + put_bits(pb, 8, 0); +} + +void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length) +{ + int words= length>>4; + int bits= length&15; + int i; + + if(length==0) return; + + if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){ + for(i=0; i>(16-bits)); } /* VLC decoding */ @@ -87,13 +105,11 @@ static int alloc_table(VLC *vlc, int size, int use_static) index = vlc->table_size; vlc->table_size += size; if (vlc->table_size > vlc->table_allocated) { - vlc->table_allocated += (1 << vlc->bits); if(use_static) - vlc->table = ff_realloc_static(vlc->table, - sizeof(VLC_TYPE) * 2 * vlc->table_allocated); - else - vlc->table = av_realloc(vlc->table, - sizeof(VLC_TYPE) * 2 * vlc->table_allocated); + abort(); //cant do anything, init_vlc() is used with too little memory + vlc->table_allocated += (1 << vlc->bits); + vlc->table = av_realloc(vlc->table, + sizeof(VLC_TYPE) * 2 * vlc->table_allocated); if (!vlc->table) return -1; } @@ -112,7 +128,7 @@ static int build_table(VLC *vlc, int table_nb_bits, VLC_TYPE (*table)[2]; table_size = 1 << table_nb_bits; - table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC); + table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC); #ifdef DEBUG_VLC av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n", table_index, table_size, code_prefix, n_prefix); @@ -142,42 +158,44 @@ static int build_table(VLC *vlc, int table_nb_bits, #endif /* if code matches the prefix, it is in the table */ n -= n_prefix; - if(flags & INIT_VLC_LE) - code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1); - else - code_prefix2= code >> n; - if (n > 0 && code_prefix2 == code_prefix) { - if (n <= table_nb_bits) { - /* no need to add another table */ - j = (code << (table_nb_bits - n)) & (table_size - 1); - nb = 1 << (table_nb_bits - n); - for(k=0;k> n_prefix) + (k< 0) { + if(flags & INIT_VLC_LE) + code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1); + else + code_prefix2= code >> n; + if (code_prefix2 == code_prefix) { + if (n <= table_nb_bits) { + /* no need to add another table */ + j = (code << (table_nb_bits - n)) & (table_size - 1); + nb = 1 << (table_nb_bits - n); + for(k=0;k> n_prefix) + (k<> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); + } else { + n -= table_nb_bits; + j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); #ifdef DEBUG_VLC - av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n", - j, n); + av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n", + j, n); #endif - /* compute table size */ - n1 = -table[j][1]; //bits - if (n > n1) - n1 = n; - table[j][1] = -n1; //bits + /* compute table size */ + n1 = -table[j][1]; //bits + if (n > n1) + n1 = n; + table[j][1] = -n1; //bits + } } } } @@ -241,15 +259,16 @@ int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, int flags) { vlc->bits = nb_bits; - if(!(flags & INIT_VLC_USE_STATIC)) { + if(flags & INIT_VLC_USE_NEW_STATIC){ + if(vlc->table_size && vlc->table_size == vlc->table_allocated){ + return 0; + }else if(vlc->table_size){ + abort(); // fatal error, we are called on a partially initialized table + } + }else { vlc->table = NULL; vlc->table_allocated = 0; vlc->table_size = 0; - } else { - /* Static tables are initially always NULL, return - if vlc->table != NULL to avoid double allocation */ - if(vlc->table) - return 0; } #ifdef DEBUG_VLC @@ -261,15 +280,17 @@ int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, 0, 0, flags) < 0) { - av_free(vlc->table); + av_freep(&vlc->table); return -1; } + if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated) + av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated); return 0; } void free_vlc(VLC *vlc) { - av_free(vlc->table); + av_freep(&vlc->table); }