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.

363 lines
11KB

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