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.

299 lines
9.3KB

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