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.

324 lines
10KB

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