You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
8.4KB

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