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.

453 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. int check_marker(GetBitContext *s, char *msg)
  191. {
  192. int bit= get_bits1(s);
  193. if(!bit) printf("Marker bit missing %s\n", msg);
  194. return bit;
  195. }
  196. #ifndef ALT_BITSTREAM_READER
  197. /* This function is identical to get_bits_long(), the */
  198. /* only diference is that it doesn't touch the buffer */
  199. /* it is usefull to see the buffer. */
  200. unsigned int show_bits_long(GetBitContext *s, int n)
  201. {
  202. unsigned int val;
  203. int bit_cnt;
  204. unsigned int bit_buf;
  205. UINT8 *buf_ptr;
  206. bit_buf = s->bit_buf;
  207. bit_cnt = s->bit_cnt - n;
  208. val = bit_buf >> (32 - n);
  209. buf_ptr = s->buf_ptr;
  210. buf_ptr += 4;
  211. /* handle common case: we can read everything */
  212. if (buf_ptr <= s->buf_end) {
  213. #ifdef ARCH_X86
  214. bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
  215. #else
  216. bit_buf = (buf_ptr[-4] << 24) |
  217. (buf_ptr[-3] << 16) |
  218. (buf_ptr[-2] << 8) |
  219. (buf_ptr[-1]);
  220. #endif
  221. } else {
  222. buf_ptr -= 4;
  223. bit_buf = 0;
  224. if (buf_ptr < s->buf_end)
  225. bit_buf |= *buf_ptr++ << 24;
  226. if (buf_ptr < s->buf_end)
  227. bit_buf |= *buf_ptr++ << 16;
  228. if (buf_ptr < s->buf_end)
  229. bit_buf |= *buf_ptr++ << 8;
  230. if (buf_ptr < s->buf_end)
  231. bit_buf |= *buf_ptr++;
  232. }
  233. val |= bit_buf >> (32 + bit_cnt);
  234. bit_buf <<= - bit_cnt;
  235. bit_cnt += 32;
  236. return val;
  237. }
  238. #endif
  239. /* VLC decoding */
  240. //#define DEBUG_VLC
  241. #define GET_DATA(v, table, i, wrap, size) \
  242. {\
  243. UINT8 *ptr = (UINT8 *)table + i * wrap;\
  244. switch(size) {\
  245. case 1:\
  246. v = *(UINT8 *)ptr;\
  247. break;\
  248. case 2:\
  249. v = *(UINT16 *)ptr;\
  250. break;\
  251. default:\
  252. v = *(UINT32 *)ptr;\
  253. break;\
  254. }\
  255. }
  256. static int alloc_table(VLC *vlc, int size)
  257. {
  258. int index;
  259. index = vlc->table_size;
  260. vlc->table_size += size;
  261. if (vlc->table_size > vlc->table_allocated) {
  262. vlc->table_allocated += (1 << vlc->bits);
  263. vlc->table_bits = realloc(vlc->table_bits,
  264. sizeof(INT8) * vlc->table_allocated);
  265. vlc->table_codes = realloc(vlc->table_codes,
  266. sizeof(INT16) * vlc->table_allocated);
  267. if (!vlc->table_bits ||
  268. !vlc->table_codes)
  269. return -1;
  270. }
  271. return index;
  272. }
  273. static int build_table(VLC *vlc, int table_nb_bits,
  274. int nb_codes,
  275. const void *bits, int bits_wrap, int bits_size,
  276. const void *codes, int codes_wrap, int codes_size,
  277. UINT32 code_prefix, int n_prefix)
  278. {
  279. int i, j, k, n, table_size, table_index, nb, n1, index;
  280. UINT32 code;
  281. INT8 *table_bits;
  282. INT16 *table_codes;
  283. table_size = 1 << table_nb_bits;
  284. table_index = alloc_table(vlc, table_size);
  285. #ifdef DEBUG_VLC
  286. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  287. table_index, table_size, code_prefix, n_prefix);
  288. #endif
  289. if (table_index < 0)
  290. return -1;
  291. table_bits = &vlc->table_bits[table_index];
  292. table_codes = &vlc->table_codes[table_index];
  293. for(i=0;i<table_size;i++) {
  294. table_bits[i] = 0;
  295. table_codes[i] = -1;
  296. }
  297. /* first pass: map codes and compute auxillary table sizes */
  298. for(i=0;i<nb_codes;i++) {
  299. GET_DATA(n, bits, i, bits_wrap, bits_size);
  300. GET_DATA(code, codes, i, codes_wrap, codes_size);
  301. /* we accept tables with holes */
  302. if (n <= 0)
  303. continue;
  304. #if defined(DEBUG_VLC) && 0
  305. printf("i=%d n=%d code=0x%x\n", i, n, code);
  306. #endif
  307. /* if code matches the prefix, it is in the table */
  308. n -= n_prefix;
  309. if (n > 0 && (code >> n) == code_prefix) {
  310. if (n <= table_nb_bits) {
  311. /* no need to add another table */
  312. j = (code << (table_nb_bits - n)) & (table_size - 1);
  313. nb = 1 << (table_nb_bits - n);
  314. for(k=0;k<nb;k++) {
  315. #ifdef DEBUG_VLC
  316. printf("%4x: code=%d n=%d\n",
  317. j, i, n);
  318. #endif
  319. if (table_bits[j] != 0) {
  320. fprintf(stderr, "incorrect codes\n");
  321. exit(1);
  322. }
  323. table_bits[j] = n;
  324. table_codes[j] = i;
  325. j++;
  326. }
  327. } else {
  328. n -= table_nb_bits;
  329. j = (code >> n) & ((1 << table_nb_bits) - 1);
  330. #ifdef DEBUG_VLC
  331. printf("%4x: n=%d (subtable)\n",
  332. j, n);
  333. #endif
  334. /* compute table size */
  335. n1 = -table_bits[j];
  336. if (n > n1)
  337. n1 = n;
  338. table_bits[j] = -n1;
  339. }
  340. }
  341. }
  342. /* second pass : fill auxillary tables recursively */
  343. for(i=0;i<table_size;i++) {
  344. n = table_bits[i];
  345. if (n < 0) {
  346. n = -n;
  347. if (n > table_nb_bits) {
  348. n = table_nb_bits;
  349. table_bits[i] = -n;
  350. }
  351. index = build_table(vlc, n, nb_codes,
  352. bits, bits_wrap, bits_size,
  353. codes, codes_wrap, codes_size,
  354. (code_prefix << table_nb_bits) | i,
  355. n_prefix + table_nb_bits);
  356. if (index < 0)
  357. return -1;
  358. /* note: realloc has been done, so reload tables */
  359. table_bits = &vlc->table_bits[table_index];
  360. table_codes = &vlc->table_codes[table_index];
  361. table_codes[i] = index;
  362. }
  363. }
  364. return table_index;
  365. }
  366. /* Build VLC decoding tables suitable for use with get_vlc().
  367. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  368. bigger it is, the faster is the decoding. But it should not be too
  369. big to save memory and L1 cache. '9' is a good compromise.
  370. 'nb_codes' : number of vlcs codes
  371. 'bits' : table which gives the size (in bits) of each vlc code.
  372. 'codes' : table which gives the bit pattern of of each vlc code.
  373. 'xxx_wrap' : give the number of bytes between each entry of the
  374. 'bits' or 'codes' tables.
  375. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  376. or 'codes' tables.
  377. 'wrap' and 'size' allows to use any memory configuration and types
  378. (byte/word/long) to store the 'bits' and 'codes' tables.
  379. */
  380. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  381. const void *bits, int bits_wrap, int bits_size,
  382. const void *codes, int codes_wrap, int codes_size)
  383. {
  384. vlc->bits = nb_bits;
  385. vlc->table_bits = NULL;
  386. vlc->table_codes = NULL;
  387. vlc->table_allocated = 0;
  388. vlc->table_size = 0;
  389. #ifdef DEBUG_VLC
  390. printf("build table nb_codes=%d\n", nb_codes);
  391. #endif
  392. if (build_table(vlc, nb_bits, nb_codes,
  393. bits, bits_wrap, bits_size,
  394. codes, codes_wrap, codes_size,
  395. 0, 0) < 0) {
  396. if (vlc->table_bits)
  397. free(vlc->table_bits);
  398. if (vlc->table_codes)
  399. free(vlc->table_codes);
  400. return -1;
  401. }
  402. return 0;
  403. }
  404. void free_vlc(VLC *vlc)
  405. {
  406. free(vlc->table_bits);
  407. free(vlc->table_codes);
  408. }