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.

356 lines
9.7KB

  1. /*
  2. * Common bit i/o utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include "avcodec.h"
  22. const UINT8 ff_sqrt_tab[128]={
  23. 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
  24. 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  25. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  26. 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
  27. };
  28. void init_put_bits(PutBitContext *s,
  29. UINT8 *buffer, int buffer_size,
  30. void *opaque,
  31. void (*write_data)(void *, UINT8 *, int))
  32. {
  33. s->buf = buffer;
  34. s->buf_end = s->buf + buffer_size;
  35. s->data_out_size = 0;
  36. if(write_data!=NULL)
  37. {
  38. fprintf(stderr, "write Data callback is not supported\n");
  39. }
  40. #ifdef ALT_BITSTREAM_WRITER
  41. s->index=0;
  42. ((uint32_t*)(s->buf))[0]=0;
  43. // memset(buffer, 0, buffer_size);
  44. #else
  45. s->buf_ptr = s->buf;
  46. s->bit_left=32;
  47. s->bit_buf=0;
  48. #endif
  49. }
  50. /* return the number of bits output */
  51. INT64 get_bit_count(PutBitContext *s)
  52. {
  53. #ifdef ALT_BITSTREAM_WRITER
  54. return s->data_out_size * 8 + s->index;
  55. #else
  56. return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left;
  57. #endif
  58. }
  59. void align_put_bits(PutBitContext *s)
  60. {
  61. #ifdef ALT_BITSTREAM_WRITER
  62. put_bits(s,( - s->index) & 7,0);
  63. #else
  64. put_bits(s,s->bit_left & 7,0);
  65. #endif
  66. }
  67. /* pad the end of the output stream with zeros */
  68. void flush_put_bits(PutBitContext *s)
  69. {
  70. #ifdef ALT_BITSTREAM_WRITER
  71. align_put_bits(s);
  72. #else
  73. s->bit_buf<<= s->bit_left;
  74. while (s->bit_left < 32) {
  75. /* XXX: should test end of buffer */
  76. *s->buf_ptr++=s->bit_buf >> 24;
  77. s->bit_buf<<=8;
  78. s->bit_left+=8;
  79. }
  80. s->bit_left=32;
  81. s->bit_buf=0;
  82. #endif
  83. }
  84. /* pad the end of the output stream with zeros */
  85. #ifndef ALT_BITSTREAM_WRITER
  86. void jflush_put_bits(PutBitContext *s)
  87. {
  88. unsigned int b;
  89. s->bit_buf<<= s->bit_left;
  90. s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */
  91. while (s->bit_left < 32) {
  92. b = s->bit_buf >> 24;
  93. *s->buf_ptr++ = b;
  94. if (b == 0xff)
  95. *s->buf_ptr++ = 0;
  96. s->bit_buf<<=8;
  97. s->bit_left+=8;
  98. }
  99. s->bit_left=32;
  100. s->bit_buf=0;
  101. }
  102. #else
  103. void jflush_put_bits(PutBitContext *s)
  104. {
  105. int num= ( - s->index) & 7;
  106. jput_bits(s, num,0xFF>>(8-num));
  107. }
  108. #endif
  109. void put_string(PutBitContext * pbc, char *s)
  110. {
  111. while(*s){
  112. put_bits(pbc, 8, *s);
  113. s++;
  114. }
  115. put_bits(pbc, 8, 0);
  116. }
  117. /* bit input functions */
  118. void init_get_bits(GetBitContext *s,
  119. UINT8 *buffer, int buffer_size)
  120. {
  121. s->buffer= buffer;
  122. s->size= buffer_size;
  123. s->buffer_end= buffer + buffer_size;
  124. #ifdef ALT_BITSTREAM_READER
  125. s->index=0;
  126. #elif defined LIBMPEG2_BITSTREAM_READER
  127. s->buffer_ptr = buffer;
  128. s->bit_count = 16;
  129. s->cache = 0;
  130. #elif defined A32_BITSTREAM_READER
  131. s->buffer_ptr = (uint32_t*)buffer;
  132. s->bit_count = 32;
  133. s->cache0 = 0;
  134. s->cache1 = 0;
  135. #endif
  136. {
  137. OPEN_READER(re, s)
  138. UPDATE_CACHE(re, s)
  139. // UPDATE_CACHE(re, s)
  140. CLOSE_READER(re, s)
  141. }
  142. #ifdef A32_BITSTREAM_READER
  143. s->cache1 = 0;
  144. #endif
  145. }
  146. void align_get_bits(GetBitContext *s)
  147. {
  148. int n= (-get_bits_count(s)) & 7;
  149. if(n) skip_bits(s, n);
  150. }
  151. int check_marker(GetBitContext *s, char *msg)
  152. {
  153. int bit= get_bits1(s);
  154. if(!bit) printf("Marker bit missing %s\n", msg);
  155. return bit;
  156. }
  157. /* VLC decoding */
  158. //#define DEBUG_VLC
  159. #define GET_DATA(v, table, i, wrap, size) \
  160. {\
  161. UINT8 *ptr = (UINT8 *)table + i * wrap;\
  162. switch(size) {\
  163. case 1:\
  164. v = *(UINT8 *)ptr;\
  165. break;\
  166. case 2:\
  167. v = *(UINT16 *)ptr;\
  168. break;\
  169. default:\
  170. v = *(UINT32 *)ptr;\
  171. break;\
  172. }\
  173. }
  174. static int alloc_table(VLC *vlc, int size)
  175. {
  176. int index;
  177. index = vlc->table_size;
  178. vlc->table_size += size;
  179. if (vlc->table_size > vlc->table_allocated) {
  180. vlc->table_allocated += (1 << vlc->bits);
  181. vlc->table = realloc(vlc->table,
  182. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  183. if (!vlc->table)
  184. return -1;
  185. }
  186. return index;
  187. }
  188. static int build_table(VLC *vlc, int table_nb_bits,
  189. int nb_codes,
  190. const void *bits, int bits_wrap, int bits_size,
  191. const void *codes, int codes_wrap, int codes_size,
  192. UINT32 code_prefix, int n_prefix)
  193. {
  194. int i, j, k, n, table_size, table_index, nb, n1, index;
  195. UINT32 code;
  196. VLC_TYPE (*table)[2];
  197. table_size = 1 << table_nb_bits;
  198. table_index = alloc_table(vlc, table_size);
  199. #ifdef DEBUG_VLC
  200. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  201. table_index, table_size, code_prefix, n_prefix);
  202. #endif
  203. if (table_index < 0)
  204. return -1;
  205. table = &vlc->table[table_index];
  206. for(i=0;i<table_size;i++) {
  207. table[i][1] = 0; //bits
  208. table[i][0] = -1; //codes
  209. }
  210. /* first pass: map codes and compute auxillary table sizes */
  211. for(i=0;i<nb_codes;i++) {
  212. GET_DATA(n, bits, i, bits_wrap, bits_size);
  213. GET_DATA(code, codes, i, codes_wrap, codes_size);
  214. /* we accept tables with holes */
  215. if (n <= 0)
  216. continue;
  217. #if defined(DEBUG_VLC) && 0
  218. printf("i=%d n=%d code=0x%x\n", i, n, code);
  219. #endif
  220. /* if code matches the prefix, it is in the table */
  221. n -= n_prefix;
  222. if (n > 0 && (code >> n) == code_prefix) {
  223. if (n <= table_nb_bits) {
  224. /* no need to add another table */
  225. j = (code << (table_nb_bits - n)) & (table_size - 1);
  226. nb = 1 << (table_nb_bits - n);
  227. for(k=0;k<nb;k++) {
  228. #ifdef DEBUG_VLC
  229. printf("%4x: code=%d n=%d\n",
  230. j, i, n);
  231. #endif
  232. if (table[j][1] /*bits*/ != 0) {
  233. fprintf(stderr, "incorrect codes\n");
  234. exit(1);
  235. }
  236. table[j][1] = n; //bits
  237. table[j][0] = i; //code
  238. j++;
  239. }
  240. } else {
  241. n -= table_nb_bits;
  242. j = (code >> n) & ((1 << table_nb_bits) - 1);
  243. #ifdef DEBUG_VLC
  244. printf("%4x: n=%d (subtable)\n",
  245. j, n);
  246. #endif
  247. /* compute table size */
  248. n1 = -table[j][1]; //bits
  249. if (n > n1)
  250. n1 = n;
  251. table[j][1] = -n1; //bits
  252. }
  253. }
  254. }
  255. /* second pass : fill auxillary tables recursively */
  256. for(i=0;i<table_size;i++) {
  257. n = table[i][1]; //bits
  258. if (n < 0) {
  259. n = -n;
  260. if (n > table_nb_bits) {
  261. n = table_nb_bits;
  262. table[i][1] = -n; //bits
  263. }
  264. index = build_table(vlc, n, nb_codes,
  265. bits, bits_wrap, bits_size,
  266. codes, codes_wrap, codes_size,
  267. (code_prefix << table_nb_bits) | i,
  268. n_prefix + table_nb_bits);
  269. if (index < 0)
  270. return -1;
  271. /* note: realloc has been done, so reload tables */
  272. table = &vlc->table[table_index];
  273. table[i][0] = index; //code
  274. }
  275. }
  276. return table_index;
  277. }
  278. /* Build VLC decoding tables suitable for use with get_vlc().
  279. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  280. bigger it is, the faster is the decoding. But it should not be too
  281. big to save memory and L1 cache. '9' is a good compromise.
  282. 'nb_codes' : number of vlcs codes
  283. 'bits' : table which gives the size (in bits) of each vlc code.
  284. 'codes' : table which gives the bit pattern of of each vlc code.
  285. 'xxx_wrap' : give the number of bytes between each entry of the
  286. 'bits' or 'codes' tables.
  287. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  288. or 'codes' tables.
  289. 'wrap' and 'size' allows to use any memory configuration and types
  290. (byte/word/long) to store the 'bits' and 'codes' tables.
  291. */
  292. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  293. const void *bits, int bits_wrap, int bits_size,
  294. const void *codes, int codes_wrap, int codes_size)
  295. {
  296. vlc->bits = nb_bits;
  297. vlc->table = NULL;
  298. vlc->table_allocated = 0;
  299. vlc->table_size = 0;
  300. #ifdef DEBUG_VLC
  301. printf("build table nb_codes=%d\n", nb_codes);
  302. #endif
  303. if (build_table(vlc, nb_bits, nb_codes,
  304. bits, bits_wrap, bits_size,
  305. codes, codes_wrap, codes_size,
  306. 0, 0) < 0) {
  307. av_free(vlc->table);
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. void free_vlc(VLC *vlc)
  313. {
  314. av_free(vlc->table);
  315. }
  316. int ff_gcd(int a, int b){
  317. if(b) return ff_gcd(b, a%b);
  318. else return a;
  319. }