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.

266 lines
8.1KB

  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. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  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. /* VLC decoding */
  58. //#define DEBUG_VLC
  59. #define GET_DATA(v, table, i, wrap, size) \
  60. {\
  61. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  62. switch(size) {\
  63. case 1:\
  64. v = *(const uint8_t *)ptr;\
  65. break;\
  66. case 2:\
  67. v = *(const uint16_t *)ptr;\
  68. break;\
  69. default:\
  70. v = *(const uint32_t *)ptr;\
  71. break;\
  72. }\
  73. }
  74. static int alloc_table(VLC *vlc, int size, int use_static)
  75. {
  76. int index;
  77. index = vlc->table_size;
  78. vlc->table_size += size;
  79. if (vlc->table_size > vlc->table_allocated) {
  80. vlc->table_allocated += (1 << vlc->bits);
  81. if(use_static)
  82. vlc->table = ff_realloc_static(vlc->table,
  83. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  84. else
  85. vlc->table = av_realloc(vlc->table,
  86. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  87. if (!vlc->table)
  88. return -1;
  89. }
  90. return index;
  91. }
  92. static int build_table(VLC *vlc, int table_nb_bits,
  93. int nb_codes,
  94. const void *bits, int bits_wrap, int bits_size,
  95. const void *codes, int codes_wrap, int codes_size,
  96. uint32_t code_prefix, int n_prefix, int flags)
  97. {
  98. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
  99. uint32_t code;
  100. VLC_TYPE (*table)[2];
  101. table_size = 1 << table_nb_bits;
  102. table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
  103. #ifdef DEBUG_VLC
  104. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  105. table_index, table_size, code_prefix, n_prefix);
  106. #endif
  107. if (table_index < 0)
  108. return -1;
  109. table = &vlc->table[table_index];
  110. for(i=0;i<table_size;i++) {
  111. table[i][1] = 0; //bits
  112. table[i][0] = -1; //codes
  113. }
  114. /* first pass: map codes and compute auxillary table sizes */
  115. for(i=0;i<nb_codes;i++) {
  116. GET_DATA(n, bits, i, bits_wrap, bits_size);
  117. GET_DATA(code, codes, i, codes_wrap, codes_size);
  118. /* we accept tables with holes */
  119. if (n <= 0)
  120. continue;
  121. #if defined(DEBUG_VLC) && 0
  122. printf("i=%d n=%d code=0x%x\n", i, n, code);
  123. #endif
  124. /* if code matches the prefix, it is in the table */
  125. n -= n_prefix;
  126. if(flags & INIT_VLC_LE)
  127. code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
  128. else
  129. code_prefix2= code >> n;
  130. if (n > 0 && code_prefix2 == code_prefix) {
  131. if (n <= table_nb_bits) {
  132. /* no need to add another table */
  133. j = (code << (table_nb_bits - n)) & (table_size - 1);
  134. nb = 1 << (table_nb_bits - n);
  135. for(k=0;k<nb;k++) {
  136. if(flags & INIT_VLC_LE)
  137. j = (code >> n_prefix) + (k<<n);
  138. #ifdef DEBUG_VLC
  139. av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
  140. j, i, n);
  141. #endif
  142. if (table[j][1] /*bits*/ != 0) {
  143. av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
  144. return -1;
  145. }
  146. table[j][1] = n; //bits
  147. table[j][0] = i; //code
  148. j++;
  149. }
  150. } else {
  151. n -= table_nb_bits;
  152. j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
  153. #ifdef DEBUG_VLC
  154. printf("%4x: n=%d (subtable)\n",
  155. j, n);
  156. #endif
  157. /* compute table size */
  158. n1 = -table[j][1]; //bits
  159. if (n > n1)
  160. n1 = n;
  161. table[j][1] = -n1; //bits
  162. }
  163. }
  164. }
  165. /* second pass : fill auxillary tables recursively */
  166. for(i=0;i<table_size;i++) {
  167. n = table[i][1]; //bits
  168. if (n < 0) {
  169. n = -n;
  170. if (n > table_nb_bits) {
  171. n = table_nb_bits;
  172. table[i][1] = -n; //bits
  173. }
  174. index = build_table(vlc, n, nb_codes,
  175. bits, bits_wrap, bits_size,
  176. codes, codes_wrap, codes_size,
  177. (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
  178. n_prefix + table_nb_bits, flags);
  179. if (index < 0)
  180. return -1;
  181. /* note: realloc has been done, so reload tables */
  182. table = &vlc->table[table_index];
  183. table[i][0] = index; //code
  184. }
  185. }
  186. return table_index;
  187. }
  188. /* Build VLC decoding tables suitable for use with get_vlc().
  189. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  190. bigger it is, the faster is the decoding. But it should not be too
  191. big to save memory and L1 cache. '9' is a good compromise.
  192. 'nb_codes' : number of vlcs codes
  193. 'bits' : table which gives the size (in bits) of each vlc code.
  194. 'codes' : table which gives the bit pattern of of each vlc code.
  195. 'xxx_wrap' : give the number of bytes between each entry of the
  196. 'bits' or 'codes' tables.
  197. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  198. or 'codes' tables.
  199. 'wrap' and 'size' allows to use any memory configuration and types
  200. (byte/word/long) to store the 'bits' and 'codes' tables.
  201. 'use_static' should be set to 1 for tables, which should be freed
  202. with av_free_static(), 0 if free_vlc() will be used.
  203. */
  204. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  205. const void *bits, int bits_wrap, int bits_size,
  206. const void *codes, int codes_wrap, int codes_size,
  207. int flags)
  208. {
  209. vlc->bits = nb_bits;
  210. if(!(flags & INIT_VLC_USE_STATIC)) {
  211. vlc->table = NULL;
  212. vlc->table_allocated = 0;
  213. vlc->table_size = 0;
  214. } else {
  215. /* Static tables are initially always NULL, return
  216. if vlc->table != NULL to avoid double allocation */
  217. if(vlc->table)
  218. return 0;
  219. }
  220. #ifdef DEBUG_VLC
  221. printf("build table nb_codes=%d\n", nb_codes);
  222. #endif
  223. if (build_table(vlc, nb_bits, nb_codes,
  224. bits, bits_wrap, bits_size,
  225. codes, codes_wrap, codes_size,
  226. 0, 0, flags) < 0) {
  227. av_free(vlc->table);
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. void free_vlc(VLC *vlc)
  233. {
  234. av_free(vlc->table);
  235. }