bitstream.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Common bit i/o utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file libavcodec/bitstream.c
  26. * bitstream api.
  27. */
  28. #include "avcodec.h"
  29. #include "bitstream.h"
  30. const uint8_t ff_log2_run[32]={
  31. 0, 0, 0, 0, 1, 1, 1, 1,
  32. 2, 2, 2, 2, 3, 3, 3, 3,
  33. 4, 4, 5, 5, 6, 6, 7, 7,
  34. 8, 9,10,11,12,13,14,15
  35. };
  36. /**
  37. * Same as av_mallocz_static(), but does a realloc.
  38. *
  39. * @param[in] ptr The block of memory to reallocate.
  40. * @param[in] size The requested size.
  41. * @return Block of memory of requested size.
  42. * @deprecated. Code which uses ff_realloc_static is broken/misdesigned
  43. * and should correctly use static arrays
  44. */
  45. attribute_deprecated av_alloc_size(2)
  46. static void *ff_realloc_static(void *ptr, unsigned int size);
  47. static void *ff_realloc_static(void *ptr, unsigned int size)
  48. {
  49. return av_realloc(ptr, size);
  50. }
  51. void align_put_bits(PutBitContext *s)
  52. {
  53. #ifdef ALT_BITSTREAM_WRITER
  54. put_bits(s,( - s->index) & 7,0);
  55. #else
  56. put_bits(s,s->bit_left & 7,0);
  57. #endif
  58. }
  59. void ff_put_string(PutBitContext * pbc, const char *s, int put_zero)
  60. {
  61. while(*s){
  62. put_bits(pbc, 8, *s);
  63. s++;
  64. }
  65. if(put_zero)
  66. put_bits(pbc, 8, 0);
  67. }
  68. void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
  69. {
  70. const uint16_t *srcw= (const uint16_t*)src;
  71. int words= length>>4;
  72. int bits= length&15;
  73. int i;
  74. if(length==0) return;
  75. if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){
  76. for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  77. }else{
  78. for(i=0; put_bits_count(pb)&31; i++)
  79. put_bits(pb, 8, src[i]);
  80. flush_put_bits(pb);
  81. memcpy(pbBufPtr(pb), src+i, 2*words-i);
  82. skip_put_bytes(pb, 2*words-i);
  83. }
  84. put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
  85. }
  86. /* VLC decoding */
  87. //#define DEBUG_VLC
  88. #define GET_DATA(v, table, i, wrap, size) \
  89. {\
  90. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  91. switch(size) {\
  92. case 1:\
  93. v = *(const uint8_t *)ptr;\
  94. break;\
  95. case 2:\
  96. v = *(const uint16_t *)ptr;\
  97. break;\
  98. default:\
  99. v = *(const uint32_t *)ptr;\
  100. break;\
  101. }\
  102. }
  103. static int alloc_table(VLC *vlc, int size, int use_static)
  104. {
  105. int index;
  106. index = vlc->table_size;
  107. vlc->table_size += size;
  108. if (vlc->table_size > vlc->table_allocated) {
  109. if(use_static>1)
  110. abort(); //cant do anything, init_vlc() is used with too little memory
  111. vlc->table_allocated += (1 << vlc->bits);
  112. if(use_static)
  113. vlc->table = ff_realloc_static(vlc->table,
  114. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  115. else
  116. vlc->table = av_realloc(vlc->table,
  117. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  118. if (!vlc->table)
  119. return -1;
  120. }
  121. return index;
  122. }
  123. static int build_table(VLC *vlc, int table_nb_bits,
  124. int nb_codes,
  125. const void *bits, int bits_wrap, int bits_size,
  126. const void *codes, int codes_wrap, int codes_size,
  127. const void *symbols, int symbols_wrap, int symbols_size,
  128. uint32_t code_prefix, int n_prefix, int flags)
  129. {
  130. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
  131. uint32_t code;
  132. VLC_TYPE (*table)[2];
  133. table_size = 1 << table_nb_bits;
  134. table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC));
  135. #ifdef DEBUG_VLC
  136. av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
  137. table_index, table_size, code_prefix, n_prefix);
  138. #endif
  139. if (table_index < 0)
  140. return -1;
  141. table = &vlc->table[table_index];
  142. for(i=0;i<table_size;i++) {
  143. table[i][1] = 0; //bits
  144. table[i][0] = -1; //codes
  145. }
  146. /* first pass: map codes and compute auxillary table sizes */
  147. for(i=0;i<nb_codes;i++) {
  148. GET_DATA(n, bits, i, bits_wrap, bits_size);
  149. GET_DATA(code, codes, i, codes_wrap, codes_size);
  150. /* we accept tables with holes */
  151. if (n <= 0)
  152. continue;
  153. if (!symbols)
  154. symbol = i;
  155. else
  156. GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
  157. #if defined(DEBUG_VLC) && 0
  158. av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
  159. #endif
  160. /* if code matches the prefix, it is in the table */
  161. n -= n_prefix;
  162. if(flags & INIT_VLC_LE)
  163. code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
  164. else
  165. code_prefix2= code >> n;
  166. if (n > 0 && code_prefix2 == code_prefix) {
  167. if (n <= table_nb_bits) {
  168. /* no need to add another table */
  169. j = (code << (table_nb_bits - n)) & (table_size - 1);
  170. nb = 1 << (table_nb_bits - n);
  171. for(k=0;k<nb;k++) {
  172. if(flags & INIT_VLC_LE)
  173. j = (code >> n_prefix) + (k<<n);
  174. #ifdef DEBUG_VLC
  175. av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
  176. j, i, n);
  177. #endif
  178. if (table[j][1] /*bits*/ != 0) {
  179. av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
  180. return -1;
  181. }
  182. table[j][1] = n; //bits
  183. table[j][0] = symbol;
  184. j++;
  185. }
  186. } else {
  187. n -= table_nb_bits;
  188. j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
  189. #ifdef DEBUG_VLC
  190. av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
  191. j, n);
  192. #endif
  193. /* compute table size */
  194. n1 = -table[j][1]; //bits
  195. if (n > n1)
  196. n1 = n;
  197. table[j][1] = -n1; //bits
  198. }
  199. }
  200. }
  201. /* second pass : fill auxillary tables recursively */
  202. for(i=0;i<table_size;i++) {
  203. n = table[i][1]; //bits
  204. if (n < 0) {
  205. n = -n;
  206. if (n > table_nb_bits) {
  207. n = table_nb_bits;
  208. table[i][1] = -n; //bits
  209. }
  210. index = build_table(vlc, n, nb_codes,
  211. bits, bits_wrap, bits_size,
  212. codes, codes_wrap, codes_size,
  213. symbols, symbols_wrap, symbols_size,
  214. (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
  215. n_prefix + table_nb_bits, flags);
  216. if (index < 0)
  217. return -1;
  218. /* note: realloc has been done, so reload tables */
  219. table = &vlc->table[table_index];
  220. table[i][0] = index; //code
  221. }
  222. }
  223. return table_index;
  224. }
  225. /* Build VLC decoding tables suitable for use with get_vlc().
  226. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  227. bigger it is, the faster is the decoding. But it should not be too
  228. big to save memory and L1 cache. '9' is a good compromise.
  229. 'nb_codes' : number of vlcs codes
  230. 'bits' : table which gives the size (in bits) of each vlc code.
  231. 'codes' : table which gives the bit pattern of of each vlc code.
  232. 'symbols' : table which gives the values to be returned from get_vlc().
  233. 'xxx_wrap' : give the number of bytes between each entry of the
  234. 'bits' or 'codes' tables.
  235. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  236. or 'codes' tables.
  237. 'wrap' and 'size' allows to use any memory configuration and types
  238. (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
  239. 'use_static' should be set to 1 for tables, which should be freed
  240. with av_free_static(), 0 if free_vlc() will be used.
  241. */
  242. int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  243. const void *bits, int bits_wrap, int bits_size,
  244. const void *codes, int codes_wrap, int codes_size,
  245. const void *symbols, int symbols_wrap, int symbols_size,
  246. int flags)
  247. {
  248. vlc->bits = nb_bits;
  249. if(flags & INIT_VLC_USE_NEW_STATIC){
  250. if(vlc->table_size && vlc->table_size == vlc->table_allocated){
  251. return 0;
  252. }else if(vlc->table_size){
  253. abort(); // fatal error, we are called on a partially initialized table
  254. }
  255. }else if(!(flags & INIT_VLC_USE_STATIC)) {
  256. vlc->table = NULL;
  257. vlc->table_allocated = 0;
  258. vlc->table_size = 0;
  259. } else {
  260. /* Static tables are initially always NULL, return
  261. if vlc->table != NULL to avoid double allocation */
  262. if(vlc->table)
  263. return 0;
  264. }
  265. #ifdef DEBUG_VLC
  266. av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
  267. #endif
  268. if (build_table(vlc, nb_bits, nb_codes,
  269. bits, bits_wrap, bits_size,
  270. codes, codes_wrap, codes_size,
  271. symbols, symbols_wrap, symbols_size,
  272. 0, 0, flags) < 0) {
  273. av_freep(&vlc->table);
  274. return -1;
  275. }
  276. if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated)
  277. av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
  278. return 0;
  279. }
  280. void free_vlc(VLC *vlc)
  281. {
  282. av_freep(&vlc->table);
  283. }