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.

509 lines
14KB

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