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.

294 lines
8.5KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 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. void align_get_bits(GetBitContext *s)
  69. {
  70. int n= (-get_bits_count(s)) & 7;
  71. if(n) skip_bits(s, n);
  72. }
  73. int check_marker(GetBitContext *s, const char *msg)
  74. {
  75. int bit= get_bits1(s);
  76. if(!bit)
  77. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  78. return bit;
  79. }
  80. /* VLC decoding */
  81. //#define DEBUG_VLC
  82. #define GET_DATA(v, table, i, wrap, size) \
  83. {\
  84. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  85. switch(size) {\
  86. case 1:\
  87. v = *(const uint8_t *)ptr;\
  88. break;\
  89. case 2:\
  90. v = *(const uint16_t *)ptr;\
  91. break;\
  92. default:\
  93. v = *(const uint32_t *)ptr;\
  94. break;\
  95. }\
  96. }
  97. static int alloc_table(VLC *vlc, int size, int use_static)
  98. {
  99. int index;
  100. index = vlc->table_size;
  101. vlc->table_size += size;
  102. if (vlc->table_size > vlc->table_allocated) {
  103. vlc->table_allocated += (1 << vlc->bits);
  104. if(use_static)
  105. vlc->table = av_realloc_static(vlc->table,
  106. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  107. else
  108. vlc->table = av_realloc(vlc->table,
  109. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  110. if (!vlc->table)
  111. return -1;
  112. }
  113. return index;
  114. }
  115. static int build_table(VLC *vlc, int table_nb_bits,
  116. int nb_codes,
  117. const void *bits, int bits_wrap, int bits_size,
  118. const void *codes, int codes_wrap, int codes_size,
  119. uint32_t code_prefix, int n_prefix, int flags)
  120. {
  121. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
  122. uint32_t code;
  123. VLC_TYPE (*table)[2];
  124. table_size = 1 << table_nb_bits;
  125. table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
  126. #ifdef DEBUG_VLC
  127. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  128. table_index, table_size, code_prefix, n_prefix);
  129. #endif
  130. if (table_index < 0)
  131. return -1;
  132. table = &vlc->table[table_index];
  133. for(i=0;i<table_size;i++) {
  134. table[i][1] = 0; //bits
  135. table[i][0] = -1; //codes
  136. }
  137. /* first pass: map codes and compute auxillary table sizes */
  138. for(i=0;i<nb_codes;i++) {
  139. GET_DATA(n, bits, i, bits_wrap, bits_size);
  140. GET_DATA(code, codes, i, codes_wrap, codes_size);
  141. /* we accept tables with holes */
  142. if (n <= 0)
  143. continue;
  144. #if defined(DEBUG_VLC) && 0
  145. printf("i=%d n=%d code=0x%x\n", i, n, code);
  146. #endif
  147. /* if code matches the prefix, it is in the table */
  148. n -= n_prefix;
  149. if(flags & INIT_VLC_LE)
  150. code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
  151. else
  152. code_prefix2= code >> n;
  153. if (n > 0 && code_prefix2 == code_prefix) {
  154. if (n <= table_nb_bits) {
  155. /* no need to add another table */
  156. j = (code << (table_nb_bits - n)) & (table_size - 1);
  157. nb = 1 << (table_nb_bits - n);
  158. for(k=0;k<nb;k++) {
  159. if(flags & INIT_VLC_LE)
  160. j = (code >> n_prefix) + (k<<n);
  161. #ifdef DEBUG_VLC
  162. av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
  163. j, i, n);
  164. #endif
  165. if (table[j][1] /*bits*/ != 0) {
  166. av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
  167. return -1;
  168. }
  169. table[j][1] = n; //bits
  170. table[j][0] = i; //code
  171. j++;
  172. }
  173. } else {
  174. n -= table_nb_bits;
  175. j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
  176. #ifdef DEBUG_VLC
  177. printf("%4x: n=%d (subtable)\n",
  178. j, n);
  179. #endif
  180. /* compute table size */
  181. n1 = -table[j][1]; //bits
  182. if (n > n1)
  183. n1 = n;
  184. table[j][1] = -n1; //bits
  185. }
  186. }
  187. }
  188. /* second pass : fill auxillary tables recursively */
  189. for(i=0;i<table_size;i++) {
  190. n = table[i][1]; //bits
  191. if (n < 0) {
  192. n = -n;
  193. if (n > table_nb_bits) {
  194. n = table_nb_bits;
  195. table[i][1] = -n; //bits
  196. }
  197. index = build_table(vlc, n, nb_codes,
  198. bits, bits_wrap, bits_size,
  199. codes, codes_wrap, codes_size,
  200. (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
  201. n_prefix + table_nb_bits, flags);
  202. if (index < 0)
  203. return -1;
  204. /* note: realloc has been done, so reload tables */
  205. table = &vlc->table[table_index];
  206. table[i][0] = index; //code
  207. }
  208. }
  209. return table_index;
  210. }
  211. /* Build VLC decoding tables suitable for use with get_vlc().
  212. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  213. bigger it is, the faster is the decoding. But it should not be too
  214. big to save memory and L1 cache. '9' is a good compromise.
  215. 'nb_codes' : number of vlcs codes
  216. 'bits' : table which gives the size (in bits) of each vlc code.
  217. 'codes' : table which gives the bit pattern of of each vlc code.
  218. 'xxx_wrap' : give the number of bytes between each entry of the
  219. 'bits' or 'codes' tables.
  220. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  221. or 'codes' tables.
  222. 'wrap' and 'size' allows to use any memory configuration and types
  223. (byte/word/long) to store the 'bits' and 'codes' tables.
  224. 'use_static' should be set to 1 for tables, which should be freed
  225. with av_free_static(), 0 if free_vlc() will be used.
  226. */
  227. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  228. const void *bits, int bits_wrap, int bits_size,
  229. const void *codes, int codes_wrap, int codes_size,
  230. int use_static)
  231. {
  232. vlc->bits = nb_bits;
  233. if(!use_static) {
  234. vlc->table = NULL;
  235. vlc->table_allocated = 0;
  236. vlc->table_size = 0;
  237. } else {
  238. /* Static tables are initially always NULL, return
  239. if vlc->table != NULL to avoid double allocation */
  240. if(vlc->table)
  241. return 0;
  242. }
  243. #ifdef DEBUG_VLC
  244. printf("build table nb_codes=%d\n", nb_codes);
  245. #endif
  246. if (build_table(vlc, nb_bits, nb_codes,
  247. bits, bits_wrap, bits_size,
  248. codes, codes_wrap, codes_size,
  249. 0, 0, use_static) < 0) {
  250. av_free(vlc->table);
  251. return -1;
  252. }
  253. return 0;
  254. }
  255. void free_vlc(VLC *vlc)
  256. {
  257. av_free(vlc->table);
  258. }