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.

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