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.

264 lines
8.0KB

  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. */
  37. void *ff_realloc_static(void *ptr, unsigned int size);
  38. void align_put_bits(PutBitContext *s)
  39. {
  40. #ifdef ALT_BITSTREAM_WRITER
  41. put_bits(s,( - s->index) & 7,0);
  42. #else
  43. put_bits(s,s->bit_left & 7,0);
  44. #endif
  45. }
  46. void ff_put_string(PutBitContext * pbc, char *s, int put_zero)
  47. {
  48. while(*s){
  49. put_bits(pbc, 8, *s);
  50. s++;
  51. }
  52. if(put_zero)
  53. put_bits(pbc, 8, 0);
  54. }
  55. /* VLC decoding */
  56. //#define DEBUG_VLC
  57. #define GET_DATA(v, table, i, wrap, size) \
  58. {\
  59. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  60. switch(size) {\
  61. case 1:\
  62. v = *(const uint8_t *)ptr;\
  63. break;\
  64. case 2:\
  65. v = *(const uint16_t *)ptr;\
  66. break;\
  67. default:\
  68. v = *(const uint32_t *)ptr;\
  69. break;\
  70. }\
  71. }
  72. static int alloc_table(VLC *vlc, int size, int use_static)
  73. {
  74. int index;
  75. index = vlc->table_size;
  76. vlc->table_size += size;
  77. if (vlc->table_size > vlc->table_allocated) {
  78. vlc->table_allocated += (1 << vlc->bits);
  79. if(use_static)
  80. vlc->table = ff_realloc_static(vlc->table,
  81. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  82. else
  83. vlc->table = av_realloc(vlc->table,
  84. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  85. if (!vlc->table)
  86. return -1;
  87. }
  88. return index;
  89. }
  90. static int build_table(VLC *vlc, int table_nb_bits,
  91. int nb_codes,
  92. const void *bits, int bits_wrap, int bits_size,
  93. const void *codes, int codes_wrap, int codes_size,
  94. uint32_t code_prefix, int n_prefix, int flags)
  95. {
  96. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
  97. uint32_t code;
  98. VLC_TYPE (*table)[2];
  99. table_size = 1 << table_nb_bits;
  100. table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
  101. #ifdef DEBUG_VLC
  102. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  103. table_index, table_size, code_prefix, n_prefix);
  104. #endif
  105. if (table_index < 0)
  106. return -1;
  107. table = &vlc->table[table_index];
  108. for(i=0;i<table_size;i++) {
  109. table[i][1] = 0; //bits
  110. table[i][0] = -1; //codes
  111. }
  112. /* first pass: map codes and compute auxillary table sizes */
  113. for(i=0;i<nb_codes;i++) {
  114. GET_DATA(n, bits, i, bits_wrap, bits_size);
  115. GET_DATA(code, codes, i, codes_wrap, codes_size);
  116. /* we accept tables with holes */
  117. if (n <= 0)
  118. continue;
  119. #if defined(DEBUG_VLC) && 0
  120. printf("i=%d n=%d code=0x%x\n", i, n, code);
  121. #endif
  122. /* if code matches the prefix, it is in the table */
  123. n -= n_prefix;
  124. if(flags & INIT_VLC_LE)
  125. code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
  126. else
  127. code_prefix2= code >> n;
  128. if (n > 0 && code_prefix2 == code_prefix) {
  129. if (n <= table_nb_bits) {
  130. /* no need to add another table */
  131. j = (code << (table_nb_bits - n)) & (table_size - 1);
  132. nb = 1 << (table_nb_bits - n);
  133. for(k=0;k<nb;k++) {
  134. if(flags & INIT_VLC_LE)
  135. j = (code >> n_prefix) + (k<<n);
  136. #ifdef DEBUG_VLC
  137. av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
  138. j, i, n);
  139. #endif
  140. if (table[j][1] /*bits*/ != 0) {
  141. av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
  142. return -1;
  143. }
  144. table[j][1] = n; //bits
  145. table[j][0] = i; //code
  146. j++;
  147. }
  148. } else {
  149. n -= table_nb_bits;
  150. j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
  151. #ifdef DEBUG_VLC
  152. printf("%4x: n=%d (subtable)\n",
  153. j, n);
  154. #endif
  155. /* compute table size */
  156. n1 = -table[j][1]; //bits
  157. if (n > n1)
  158. n1 = n;
  159. table[j][1] = -n1; //bits
  160. }
  161. }
  162. }
  163. /* second pass : fill auxillary tables recursively */
  164. for(i=0;i<table_size;i++) {
  165. n = table[i][1]; //bits
  166. if (n < 0) {
  167. n = -n;
  168. if (n > table_nb_bits) {
  169. n = table_nb_bits;
  170. table[i][1] = -n; //bits
  171. }
  172. index = build_table(vlc, n, nb_codes,
  173. bits, bits_wrap, bits_size,
  174. codes, codes_wrap, codes_size,
  175. (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
  176. n_prefix + table_nb_bits, flags);
  177. if (index < 0)
  178. return -1;
  179. /* note: realloc has been done, so reload tables */
  180. table = &vlc->table[table_index];
  181. table[i][0] = index; //code
  182. }
  183. }
  184. return table_index;
  185. }
  186. /* Build VLC decoding tables suitable for use with get_vlc().
  187. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  188. bigger it is, the faster is the decoding. But it should not be too
  189. big to save memory and L1 cache. '9' is a good compromise.
  190. 'nb_codes' : number of vlcs codes
  191. 'bits' : table which gives the size (in bits) of each vlc code.
  192. 'codes' : table which gives the bit pattern of of each vlc code.
  193. 'xxx_wrap' : give the number of bytes between each entry of the
  194. 'bits' or 'codes' tables.
  195. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  196. or 'codes' tables.
  197. 'wrap' and 'size' allows to use any memory configuration and types
  198. (byte/word/long) to store the 'bits' and 'codes' tables.
  199. 'use_static' should be set to 1 for tables, which should be freed
  200. with av_free_static(), 0 if free_vlc() will be used.
  201. */
  202. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  203. const void *bits, int bits_wrap, int bits_size,
  204. const void *codes, int codes_wrap, int codes_size,
  205. int use_static)
  206. {
  207. vlc->bits = nb_bits;
  208. if(!use_static) {
  209. vlc->table = NULL;
  210. vlc->table_allocated = 0;
  211. vlc->table_size = 0;
  212. } else {
  213. /* Static tables are initially always NULL, return
  214. if vlc->table != NULL to avoid double allocation */
  215. if(vlc->table)
  216. return 0;
  217. }
  218. #ifdef DEBUG_VLC
  219. printf("build table nb_codes=%d\n", nb_codes);
  220. #endif
  221. if (build_table(vlc, nb_bits, nb_codes,
  222. bits, bits_wrap, bits_size,
  223. codes, codes_wrap, codes_size,
  224. 0, 0, use_static) < 0) {
  225. av_free(vlc->table);
  226. return -1;
  227. }
  228. return 0;
  229. }
  230. void free_vlc(VLC *vlc)
  231. {
  232. av_free(vlc->table);
  233. }