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.

276 lines
8.7KB

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