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.

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