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.

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