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.

314 lines
9.8KB

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