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.

445 lines
12KB

  1. /*
  2. * Common bit i/o utils
  3. * Copyright (c) 2000, 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include "common.h"
  22. #include <math.h>
  23. void init_put_bits(PutBitContext *s,
  24. UINT8 *buffer, int buffer_size,
  25. void *opaque,
  26. void (*write_data)(void *, UINT8 *, int))
  27. {
  28. s->buf = buffer;
  29. s->buf_end = s->buf + buffer_size;
  30. s->data_out_size = 0;
  31. if(write_data!=NULL)
  32. {
  33. fprintf(stderr, "write Data callback is not supported\n");
  34. }
  35. #ifdef ALT_BITSTREAM_WRITER
  36. s->index=0;
  37. ((uint32_t*)(s->buf))[0]=0;
  38. // memset(buffer, 0, buffer_size);
  39. #else
  40. s->buf_ptr = s->buf;
  41. s->bit_left=32;
  42. s->bit_buf=0;
  43. #endif
  44. }
  45. /* return the number of bits output */
  46. INT64 get_bit_count(PutBitContext *s)
  47. {
  48. #ifdef ALT_BITSTREAM_WRITER
  49. return s->data_out_size * 8 + s->index;
  50. #else
  51. return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left;
  52. #endif
  53. }
  54. void align_put_bits(PutBitContext *s)
  55. {
  56. #ifdef ALT_BITSTREAM_WRITER
  57. put_bits(s,( - s->index) & 7,0);
  58. #else
  59. put_bits(s,s->bit_left & 7,0);
  60. #endif
  61. }
  62. /* pad the end of the output stream with zeros */
  63. void flush_put_bits(PutBitContext *s)
  64. {
  65. #ifdef ALT_BITSTREAM_WRITER
  66. align_put_bits(s);
  67. #else
  68. s->bit_buf<<= s->bit_left;
  69. while (s->bit_left < 32) {
  70. /* XXX: should test end of buffer */
  71. *s->buf_ptr++=s->bit_buf >> 24;
  72. s->bit_buf<<=8;
  73. s->bit_left+=8;
  74. }
  75. s->bit_left=32;
  76. s->bit_buf=0;
  77. #endif
  78. }
  79. /* pad the end of the output stream with zeros */
  80. #ifndef ALT_BITSTREAM_WRITER
  81. void jflush_put_bits(PutBitContext *s)
  82. {
  83. unsigned int b;
  84. s->bit_buf<<= s->bit_left;
  85. s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */
  86. while (s->bit_left < 32) {
  87. b = s->bit_buf >> 24;
  88. *s->buf_ptr++ = b;
  89. if (b == 0xff)
  90. *s->buf_ptr++ = 0;
  91. s->bit_buf<<=8;
  92. s->bit_left+=8;
  93. }
  94. s->bit_left=32;
  95. s->bit_buf=0;
  96. }
  97. #else
  98. void jflush_put_bits(PutBitContext *s)
  99. {
  100. int num= ( - s->index) & 7;
  101. jput_bits(s, num,0xFF>>(8-num));
  102. }
  103. #endif
  104. /* bit input functions */
  105. void init_get_bits(GetBitContext *s,
  106. UINT8 *buffer, int buffer_size)
  107. {
  108. #ifdef ALT_BITSTREAM_READER
  109. s->index=0;
  110. s->buffer= buffer;
  111. #else
  112. s->buf = buffer;
  113. s->buf_ptr = buffer;
  114. s->buf_end = buffer + buffer_size;
  115. s->bit_cnt = 0;
  116. s->bit_buf = 0;
  117. while (s->buf_ptr < s->buf_end &&
  118. s->bit_cnt < 32) {
  119. s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt));
  120. s->bit_cnt += 8;
  121. }
  122. #endif
  123. }
  124. #ifndef ALT_BITSTREAM_READER
  125. /* n must be >= 1 and <= 32 */
  126. /* also true: n > s->bit_cnt */
  127. unsigned int get_bits_long(GetBitContext *s, int n)
  128. {
  129. unsigned int val;
  130. int bit_cnt;
  131. unsigned int bit_buf;
  132. #ifdef STATS
  133. st_bit_counts[st_current_index] += n;
  134. #endif
  135. bit_buf = s->bit_buf;
  136. bit_cnt = s->bit_cnt - n;
  137. // if (bit_cnt >= 0) {
  138. // val = bit_buf >> (32 - n);
  139. // bit_buf <<= n;
  140. // } else
  141. {
  142. UINT8 *buf_ptr;
  143. val = bit_buf >> (32 - n);
  144. buf_ptr = s->buf_ptr;
  145. buf_ptr += 4;
  146. /* handle common case: we can read everything */
  147. if (buf_ptr <= s->buf_end) {
  148. #if ARCH_X86
  149. bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
  150. #else
  151. bit_buf = (buf_ptr[-4] << 24) |
  152. (buf_ptr[-3] << 16) |
  153. (buf_ptr[-2] << 8) |
  154. (buf_ptr[-1]);
  155. #endif
  156. } else {
  157. buf_ptr -= 4;
  158. bit_buf = 0;
  159. if (buf_ptr < s->buf_end)
  160. bit_buf |= *buf_ptr++ << 24;
  161. if (buf_ptr < s->buf_end)
  162. bit_buf |= *buf_ptr++ << 16;
  163. if (buf_ptr < s->buf_end)
  164. bit_buf |= *buf_ptr++ << 8;
  165. if (buf_ptr < s->buf_end)
  166. bit_buf |= *buf_ptr++;
  167. }
  168. s->buf_ptr = buf_ptr;
  169. val |= bit_buf >> (32 + bit_cnt);
  170. bit_buf <<= - bit_cnt;
  171. bit_cnt += 32;
  172. }
  173. s->bit_buf = bit_buf;
  174. s->bit_cnt = bit_cnt;
  175. return val;
  176. }
  177. #endif
  178. void align_get_bits(GetBitContext *s)
  179. {
  180. #ifdef ALT_BITSTREAM_READER
  181. s->index= (s->index + 7) & (~7);
  182. #else
  183. int n;
  184. n = s->bit_cnt & 7;
  185. if (n > 0) {
  186. get_bits(s, n);
  187. }
  188. #endif
  189. }
  190. #ifndef ALT_BITSTREAM_READER
  191. /* This function is identical to get_bits_long(), the */
  192. /* only diference is that it doesn't touch the buffer */
  193. /* it is usefull to see the buffer. */
  194. unsigned int show_bits_long(GetBitContext *s, int n)
  195. {
  196. unsigned int val;
  197. int bit_cnt;
  198. unsigned int bit_buf;
  199. UINT8 *buf_ptr;
  200. bit_buf = s->bit_buf;
  201. bit_cnt = s->bit_cnt - n;
  202. val = bit_buf >> (32 - n);
  203. buf_ptr = s->buf_ptr;
  204. buf_ptr += 4;
  205. /* handle common case: we can read everything */
  206. if (buf_ptr <= s->buf_end) {
  207. #ifdef ARCH_X86
  208. bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
  209. #else
  210. bit_buf = (buf_ptr[-4] << 24) |
  211. (buf_ptr[-3] << 16) |
  212. (buf_ptr[-2] << 8) |
  213. (buf_ptr[-1]);
  214. #endif
  215. } else {
  216. buf_ptr -= 4;
  217. bit_buf = 0;
  218. if (buf_ptr < s->buf_end)
  219. bit_buf |= *buf_ptr++ << 24;
  220. if (buf_ptr < s->buf_end)
  221. bit_buf |= *buf_ptr++ << 16;
  222. if (buf_ptr < s->buf_end)
  223. bit_buf |= *buf_ptr++ << 8;
  224. if (buf_ptr < s->buf_end)
  225. bit_buf |= *buf_ptr++;
  226. }
  227. val |= bit_buf >> (32 + bit_cnt);
  228. bit_buf <<= - bit_cnt;
  229. bit_cnt += 32;
  230. return val;
  231. }
  232. #endif
  233. /* VLC decoding */
  234. //#define DEBUG_VLC
  235. #define GET_DATA(v, table, i, wrap, size) \
  236. {\
  237. UINT8 *ptr = (UINT8 *)table + i * wrap;\
  238. switch(size) {\
  239. case 1:\
  240. v = *(UINT8 *)ptr;\
  241. break;\
  242. case 2:\
  243. v = *(UINT16 *)ptr;\
  244. break;\
  245. default:\
  246. v = *(UINT32 *)ptr;\
  247. break;\
  248. }\
  249. }
  250. static int alloc_table(VLC *vlc, int size)
  251. {
  252. int index;
  253. index = vlc->table_size;
  254. vlc->table_size += size;
  255. if (vlc->table_size > vlc->table_allocated) {
  256. vlc->table_allocated += (1 << vlc->bits);
  257. vlc->table_bits = realloc(vlc->table_bits,
  258. sizeof(INT8) * vlc->table_allocated);
  259. vlc->table_codes = realloc(vlc->table_codes,
  260. sizeof(INT16) * vlc->table_allocated);
  261. if (!vlc->table_bits ||
  262. !vlc->table_codes)
  263. return -1;
  264. }
  265. return index;
  266. }
  267. static int build_table(VLC *vlc, int table_nb_bits,
  268. int nb_codes,
  269. const void *bits, int bits_wrap, int bits_size,
  270. const void *codes, int codes_wrap, int codes_size,
  271. UINT32 code_prefix, int n_prefix)
  272. {
  273. int i, j, k, n, table_size, table_index, nb, n1, index;
  274. UINT32 code;
  275. INT8 *table_bits;
  276. INT16 *table_codes;
  277. table_size = 1 << table_nb_bits;
  278. table_index = alloc_table(vlc, table_size);
  279. #ifdef DEBUG_VLC
  280. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  281. table_index, table_size, code_prefix, n_prefix);
  282. #endif
  283. if (table_index < 0)
  284. return -1;
  285. table_bits = &vlc->table_bits[table_index];
  286. table_codes = &vlc->table_codes[table_index];
  287. for(i=0;i<table_size;i++) {
  288. table_bits[i] = 0;
  289. table_codes[i] = -1;
  290. }
  291. /* first pass: map codes and compute auxillary table sizes */
  292. for(i=0;i<nb_codes;i++) {
  293. GET_DATA(n, bits, i, bits_wrap, bits_size);
  294. GET_DATA(code, codes, i, codes_wrap, codes_size);
  295. /* we accept tables with holes */
  296. if (n <= 0)
  297. continue;
  298. #if defined(DEBUG_VLC) && 0
  299. printf("i=%d n=%d code=0x%x\n", i, n, code);
  300. #endif
  301. /* if code matches the prefix, it is in the table */
  302. n -= n_prefix;
  303. if (n > 0 && (code >> n) == code_prefix) {
  304. if (n <= table_nb_bits) {
  305. /* no need to add another table */
  306. j = (code << (table_nb_bits - n)) & (table_size - 1);
  307. nb = 1 << (table_nb_bits - n);
  308. for(k=0;k<nb;k++) {
  309. #ifdef DEBUG_VLC
  310. printf("%4x: code=%d n=%d\n",
  311. j, i, n);
  312. #endif
  313. if (table_bits[j] != 0) {
  314. fprintf(stderr, "incorrect codes\n");
  315. exit(1);
  316. }
  317. table_bits[j] = n;
  318. table_codes[j] = i;
  319. j++;
  320. }
  321. } else {
  322. n -= table_nb_bits;
  323. j = (code >> n) & ((1 << table_nb_bits) - 1);
  324. #ifdef DEBUG_VLC
  325. printf("%4x: n=%d (subtable)\n",
  326. j, n);
  327. #endif
  328. /* compute table size */
  329. n1 = -table_bits[j];
  330. if (n > n1)
  331. n1 = n;
  332. table_bits[j] = -n1;
  333. }
  334. }
  335. }
  336. /* second pass : fill auxillary tables recursively */
  337. for(i=0;i<table_size;i++) {
  338. n = table_bits[i];
  339. if (n < 0) {
  340. n = -n;
  341. if (n > table_nb_bits) {
  342. n = table_nb_bits;
  343. table_bits[i] = -n;
  344. }
  345. index = build_table(vlc, n, nb_codes,
  346. bits, bits_wrap, bits_size,
  347. codes, codes_wrap, codes_size,
  348. (code_prefix << table_nb_bits) | i,
  349. n_prefix + table_nb_bits);
  350. if (index < 0)
  351. return -1;
  352. /* note: realloc has been done, so reload tables */
  353. table_bits = &vlc->table_bits[table_index];
  354. table_codes = &vlc->table_codes[table_index];
  355. table_codes[i] = index;
  356. }
  357. }
  358. return table_index;
  359. }
  360. /* Build VLC decoding tables suitable for use with get_vlc().
  361. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  362. bigger it is, the faster is the decoding. But it should not be too
  363. big to save memory and L1 cache. '9' is a good compromise.
  364. 'nb_codes' : number of vlcs codes
  365. 'bits' : table which gives the size (in bits) of each vlc code.
  366. 'codes' : table which gives the bit pattern of of each vlc code.
  367. 'xxx_wrap' : give the number of bytes between each entry of the
  368. 'bits' or 'codes' tables.
  369. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  370. or 'codes' tables.
  371. 'wrap' and 'size' allows to use any memory configuration and types
  372. (byte/word/long) to store the 'bits' and 'codes' tables.
  373. */
  374. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  375. const void *bits, int bits_wrap, int bits_size,
  376. const void *codes, int codes_wrap, int codes_size)
  377. {
  378. vlc->bits = nb_bits;
  379. vlc->table_bits = NULL;
  380. vlc->table_codes = NULL;
  381. vlc->table_allocated = 0;
  382. vlc->table_size = 0;
  383. #ifdef DEBUG_VLC
  384. printf("build table nb_codes=%d\n", nb_codes);
  385. #endif
  386. if (build_table(vlc, nb_bits, nb_codes,
  387. bits, bits_wrap, bits_size,
  388. codes, codes_wrap, codes_size,
  389. 0, 0) < 0) {
  390. if (vlc->table_bits)
  391. free(vlc->table_bits);
  392. if (vlc->table_codes)
  393. free(vlc->table_codes);
  394. return -1;
  395. }
  396. return 0;
  397. }
  398. void free_vlc(VLC *vlc)
  399. {
  400. free(vlc->table_bits);
  401. free(vlc->table_codes);
  402. }