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.

298 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 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. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  58. {
  59. const uint16_t *srcw= (uint16_t*)src;
  60. int words= length>>4;
  61. int bits= length&15;
  62. int i;
  63. if(length==0) return;
  64. if(ENABLE_SMALL || words < 16 || put_bits_count(pb)&7){
  65. for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  66. }else{
  67. for(i=0; put_bits_count(pb)&31; i++)
  68. put_bits(pb, 8, src[i]);
  69. flush_put_bits(pb);
  70. memcpy(pbBufPtr(pb), src+i, 2*words-i);
  71. skip_put_bytes(pb, 2*words-i);
  72. }
  73. put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
  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 = ff_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. const void *symbols, int symbols_wrap, int symbols_size,
  115. uint32_t code_prefix, int n_prefix, int flags)
  116. {
  117. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
  118. uint32_t code;
  119. VLC_TYPE (*table)[2];
  120. table_size = 1 << table_nb_bits;
  121. table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
  122. #ifdef DEBUG_VLC
  123. av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
  124. table_index, table_size, code_prefix, n_prefix);
  125. #endif
  126. if (table_index < 0)
  127. return -1;
  128. table = &vlc->table[table_index];
  129. for(i=0;i<table_size;i++) {
  130. table[i][1] = 0; //bits
  131. table[i][0] = -1; //codes
  132. }
  133. /* first pass: map codes and compute auxillary table sizes */
  134. for(i=0;i<nb_codes;i++) {
  135. GET_DATA(n, bits, i, bits_wrap, bits_size);
  136. GET_DATA(code, codes, i, codes_wrap, codes_size);
  137. /* we accept tables with holes */
  138. if (n <= 0)
  139. continue;
  140. if (!symbols)
  141. symbol = i;
  142. else
  143. GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
  144. #if defined(DEBUG_VLC) && 0
  145. av_log(NULL,AV_LOG_DEBUG,"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] = symbol;
  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. av_log(NULL,AV_LOG_DEBUG,"%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. symbols, symbols_wrap, symbols_size,
  201. (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
  202. n_prefix + table_nb_bits, flags);
  203. if (index < 0)
  204. return -1;
  205. /* note: realloc has been done, so reload tables */
  206. table = &vlc->table[table_index];
  207. table[i][0] = index; //code
  208. }
  209. }
  210. return table_index;
  211. }
  212. /* Build VLC decoding tables suitable for use with get_vlc().
  213. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  214. bigger it is, the faster is the decoding. But it should not be too
  215. big to save memory and L1 cache. '9' is a good compromise.
  216. 'nb_codes' : number of vlcs codes
  217. 'bits' : table which gives the size (in bits) of each vlc code.
  218. 'codes' : table which gives the bit pattern of of each vlc code.
  219. 'symbols' : table which gives the values to be returned from get_vlc().
  220. 'xxx_wrap' : give the number of bytes between each entry of the
  221. 'bits' or 'codes' tables.
  222. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  223. or 'codes' tables.
  224. 'wrap' and 'size' allows to use any memory configuration and types
  225. (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
  226. 'use_static' should be set to 1 for tables, which should be freed
  227. with av_free_static(), 0 if free_vlc() will be used.
  228. */
  229. int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  230. const void *bits, int bits_wrap, int bits_size,
  231. const void *codes, int codes_wrap, int codes_size,
  232. const void *symbols, int symbols_wrap, int symbols_size,
  233. int flags)
  234. {
  235. vlc->bits = nb_bits;
  236. if(!(flags & INIT_VLC_USE_STATIC)) {
  237. vlc->table = NULL;
  238. vlc->table_allocated = 0;
  239. vlc->table_size = 0;
  240. } else {
  241. /* Static tables are initially always NULL, return
  242. if vlc->table != NULL to avoid double allocation */
  243. if(vlc->table)
  244. return 0;
  245. }
  246. #ifdef DEBUG_VLC
  247. av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
  248. #endif
  249. if (build_table(vlc, nb_bits, nb_codes,
  250. bits, bits_wrap, bits_size,
  251. codes, codes_wrap, codes_size,
  252. symbols, symbols_wrap, symbols_size,
  253. 0, 0, flags) < 0) {
  254. av_freep(&vlc->table);
  255. return -1;
  256. }
  257. return 0;
  258. }
  259. void free_vlc(VLC *vlc)
  260. {
  261. av_freep(&vlc->table);
  262. }