wmabitstream.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  23. */
  24. /**
  25. * @file bitstream.c
  26. * bitstream api.
  27. */
  28. #include "wmabitstream.h"
  29. #include "codeclib.h"
  30. /**
  31. * Same as av_mallocz_static(), but does a realloc.
  32. *
  33. * @param[in] ptr The block of memory to reallocate.
  34. * @param[in] size The requested size.
  35. * @return Block of memory of requested size.
  36. * @deprecated. Code which uses ff_realloc_static is broken/missdesigned
  37. * and should correctly use static arrays
  38. */
  39. attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size);
  40. const uint8_t ff_sqrt_tab[128]={
  41. 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
  42. 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  43. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  44. 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
  45. };
  46. const uint8_t ff_log2_tab[256]={
  47. 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  48. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  49. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  50. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  51. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  52. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  53. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  54. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  55. };
  56. void align_put_bits(PutBitContext *s)
  57. {
  58. #ifdef ALT_BITSTREAM_WRITER
  59. put_bits(s,( - s->index) & 7,0);
  60. #else
  61. put_bits(s,s->bit_left & 7,0);
  62. #endif
  63. }
  64. void ff_put_string(PutBitContext * pbc, char *s, int put_zero)
  65. {
  66. while(*s){
  67. put_bits(pbc, 8, *s);
  68. s++;
  69. }
  70. if(put_zero)
  71. put_bits(pbc, 8, 0);
  72. }
  73. /* VLC decoding */
  74. //#define DEBUG_VLC
  75. #define GET_DATA(v, table, i, wrap, size) \
  76. {\
  77. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  78. switch(size) {\
  79. case 1:\
  80. v = *(const uint8_t *)ptr;\
  81. break;\
  82. case 2:\
  83. v = *(const uint16_t *)ptr;\
  84. break;\
  85. default:\
  86. v = *(const uint32_t *)ptr;\
  87. break;\
  88. }\
  89. }
  90. static int alloc_table(VLC *vlc, int size)
  91. {
  92. int index;
  93. index = vlc->table_size;
  94. vlc->table_size += size;
  95. if (vlc->table_size > vlc->table_allocated) {
  96. DEBUGF("Tried to allocate past the end of a Huffman table: %d/%d\n",
  97. vlc->table_allocated, vlc->table_allocated+(1 << vlc->bits));
  98. vlc->table_allocated += (1 << vlc->bits);
  99. if (!vlc->table)
  100. return -1;
  101. }
  102. return index;
  103. }
  104. static int build_table(VLC *vlc, int table_nb_bits,
  105. int nb_codes,
  106. const void *bits, int bits_wrap, int bits_size,
  107. const void *codes, int codes_wrap, int codes_size,
  108. uint32_t code_prefix, int n_prefix)
  109. {
  110. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
  111. uint32_t code;
  112. int flags = 0;
  113. VLC_TYPE (*table)[2];
  114. table_size = 1 << table_nb_bits;
  115. table_index = alloc_table(vlc, table_size);
  116. #ifdef DEBUG_VLC
  117. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  118. table_index, table_size, code_prefix, n_prefix);
  119. #endif
  120. if (table_index < 0)
  121. return -1;
  122. table = &vlc->table[table_index];
  123. for(i=0;i<table_size;i++) {
  124. table[i][1] = 0; //bits
  125. table[i][0] = -1; //codes
  126. }
  127. /* first pass: map codes and compute auxillary table sizes */
  128. for(i=0;i<nb_codes;i++) {
  129. GET_DATA(n, bits, i, bits_wrap, bits_size);
  130. GET_DATA(code, codes, i, codes_wrap, codes_size);
  131. /* we accept tables with holes */
  132. if (n <= 0)
  133. continue;
  134. #if defined(DEBUG_VLC) && 0
  135. printf("i=%d n=%d code=0x%x\n", i, n, code);
  136. #endif
  137. /* if code matches the prefix, it is in the table */
  138. n -= n_prefix;
  139. if(flags & INIT_VLC_LE)
  140. code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (uint32_t)(1 << n_prefix)-1);
  141. else
  142. code_prefix2= code >> n;
  143. if (n > 0 && (int)code_prefix2 == (int)code_prefix) {
  144. if (n <= table_nb_bits) {
  145. /* no need to add another table */
  146. j = (code << (table_nb_bits - n)) & (table_size - 1);
  147. nb = 1 << (table_nb_bits - n);
  148. for(k=0;k<nb;k++) {
  149. if(flags & INIT_VLC_LE)
  150. j = (code >> n_prefix) + (k<<n);
  151. #ifdef DEBUG_VLC
  152. av_log(NULL, 0, "%4x: code=%d n=%d\n",
  153. j, i, n);
  154. #endif
  155. if (table[j][1] /*bits*/ != 0) {
  156. return -1;
  157. }
  158. table[j][1] = n; //bits
  159. table[j][0] = i; //code
  160. j++;
  161. }
  162. } else {
  163. n -= table_nb_bits;
  164. j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
  165. #ifdef DEBUG_VLC
  166. av_log(NULL, 0,"%4x: n=%d (subtable)\n",
  167. j, n);
  168. #endif
  169. /* compute table size */
  170. n1 = -table[j][1]; //bits
  171. if (n > n1)
  172. n1 = n;
  173. table[j][1] = -n1; //bits
  174. }
  175. }
  176. }
  177. /* second pass : fill auxillary tables recursively */
  178. for(i=0;i<table_size;i++) {
  179. n = table[i][1]; //bits
  180. if (n < 0) {
  181. n = -n;
  182. if (n > table_nb_bits) {
  183. n = table_nb_bits;
  184. table[i][1] = -n; //bits
  185. }
  186. index = build_table(vlc, n, nb_codes,
  187. bits, bits_wrap, bits_size,
  188. codes, codes_wrap, codes_size,
  189. (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
  190. n_prefix + table_nb_bits);
  191. if (index < 0)
  192. return -1;
  193. /* note: realloc has been done, so reload tables */
  194. table = &vlc->table[table_index];
  195. table[i][0] = index; //code
  196. }
  197. }
  198. return table_index;
  199. }
  200. /* Build VLC decoding tables suitable for use with get_vlc().
  201. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  202. bigger it is, the faster is the decoding. But it should not be too
  203. big to save memory and L1 cache. '9' is a good compromise.
  204. 'nb_codes' : number of vlcs codes
  205. 'bits' : table which gives the size (in bits) of each vlc code.
  206. 'codes' : table which gives the bit pattern of of each vlc code.
  207. 'xxx_wrap' : give the number of bytes between each entry of the
  208. 'bits' or 'codes' tables.
  209. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  210. or 'codes' tables.
  211. 'wrap' and 'size' allows to use any memory configuration and types
  212. (byte/word/long) to store the 'bits' and 'codes' tables.
  213. 'use_static' should be set to 1 for tables, which should be freed
  214. with av_free_static(), 0 if free_vlc() will be used.
  215. */
  216. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  217. const void *bits, int bits_wrap, int bits_size,
  218. const void *codes, int codes_wrap, int codes_size,
  219. int flags)
  220. {
  221. vlc->bits = nb_bits;
  222. vlc->table_size = 0;
  223. #ifdef DEBUG_VLC
  224. printf("build table nb_codes=%d\n", nb_codes);
  225. #endif
  226. if (build_table(vlc, nb_bits, nb_codes,
  227. bits, bits_wrap, bits_size,
  228. codes, codes_wrap, codes_size,
  229. 0, 0) < 0) {
  230. //av_free(vlc->table);
  231. return -1;
  232. }
  233. /* return flags to block gcc warning while allowing us to keep
  234. * consistent with ffmpeg's function parameters
  235. */
  236. return flags;
  237. }