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.

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