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.

484 lines
13KB

  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. /* VLC decoding */
  229. //#define DEBUG_VLC
  230. #define GET_DATA(v, table, i, wrap, size) \
  231. {\
  232. UINT8 *ptr = (UINT8 *)table + i * wrap;\
  233. switch(size) {\
  234. case 1:\
  235. v = *(UINT8 *)ptr;\
  236. break;\
  237. case 2:\
  238. v = *(UINT16 *)ptr;\
  239. break;\
  240. default:\
  241. v = *(UINT32 *)ptr;\
  242. break;\
  243. }\
  244. }
  245. static int alloc_table(VLC *vlc, int size)
  246. {
  247. int index;
  248. index = vlc->table_size;
  249. vlc->table_size += size;
  250. if (vlc->table_size > vlc->table_allocated) {
  251. vlc->table_allocated += (1 << vlc->bits);
  252. vlc->table_bits = realloc(vlc->table_bits,
  253. sizeof(INT8) * vlc->table_allocated);
  254. vlc->table_codes = realloc(vlc->table_codes,
  255. sizeof(INT16) * vlc->table_allocated);
  256. if (!vlc->table_bits ||
  257. !vlc->table_codes)
  258. return -1;
  259. }
  260. return index;
  261. }
  262. static int build_table(VLC *vlc, int table_nb_bits,
  263. int nb_codes,
  264. const void *bits, int bits_wrap, int bits_size,
  265. const void *codes, int codes_wrap, int codes_size,
  266. UINT32 code_prefix, int n_prefix)
  267. {
  268. int i, j, k, n, table_size, table_index, nb, n1, index;
  269. UINT32 code;
  270. INT8 *table_bits;
  271. INT16 *table_codes;
  272. table_size = 1 << table_nb_bits;
  273. table_index = alloc_table(vlc, table_size);
  274. #ifdef DEBUG_VLC
  275. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  276. table_index, table_size, code_prefix, n_prefix);
  277. #endif
  278. if (table_index < 0)
  279. return -1;
  280. table_bits = &vlc->table_bits[table_index];
  281. table_codes = &vlc->table_codes[table_index];
  282. for(i=0;i<table_size;i++) {
  283. table_bits[i] = 0;
  284. table_codes[i] = -1;
  285. }
  286. /* first pass: map codes and compute auxillary table sizes */
  287. for(i=0;i<nb_codes;i++) {
  288. GET_DATA(n, bits, i, bits_wrap, bits_size);
  289. GET_DATA(code, codes, i, codes_wrap, codes_size);
  290. /* we accept tables with holes */
  291. if (n <= 0)
  292. continue;
  293. #if defined(DEBUG_VLC) && 0
  294. printf("i=%d n=%d code=0x%x\n", i, n, code);
  295. #endif
  296. /* if code matches the prefix, it is in the table */
  297. n -= n_prefix;
  298. if (n > 0 && (code >> n) == code_prefix) {
  299. if (n <= table_nb_bits) {
  300. /* no need to add another table */
  301. j = (code << (table_nb_bits - n)) & (table_size - 1);
  302. nb = 1 << (table_nb_bits - n);
  303. for(k=0;k<nb;k++) {
  304. #ifdef DEBUG_VLC
  305. printf("%4x: code=%d n=%d\n",
  306. j, i, n);
  307. #endif
  308. if (table_bits[j] != 0) {
  309. fprintf(stderr, "incorrect codes\n");
  310. exit(1);
  311. }
  312. table_bits[j] = n;
  313. table_codes[j] = i;
  314. j++;
  315. }
  316. } else {
  317. n -= table_nb_bits;
  318. j = (code >> n) & ((1 << table_nb_bits) - 1);
  319. #ifdef DEBUG_VLC
  320. printf("%4x: n=%d (subtable)\n",
  321. j, n);
  322. #endif
  323. /* compute table size */
  324. n1 = -table_bits[j];
  325. if (n > n1)
  326. n1 = n;
  327. table_bits[j] = -n1;
  328. }
  329. }
  330. }
  331. /* second pass : fill auxillary tables recursively */
  332. for(i=0;i<table_size;i++) {
  333. n = table_bits[i];
  334. if (n < 0) {
  335. n = -n;
  336. if (n > table_nb_bits) {
  337. n = table_nb_bits;
  338. table_bits[i] = -n;
  339. }
  340. index = build_table(vlc, n, nb_codes,
  341. bits, bits_wrap, bits_size,
  342. codes, codes_wrap, codes_size,
  343. (code_prefix << table_nb_bits) | i,
  344. n_prefix + table_nb_bits);
  345. if (index < 0)
  346. return -1;
  347. /* note: realloc has been done, so reload tables */
  348. table_bits = &vlc->table_bits[table_index];
  349. table_codes = &vlc->table_codes[table_index];
  350. table_codes[i] = index;
  351. }
  352. }
  353. return table_index;
  354. }
  355. /* Build VLC decoding tables suitable for use with get_vlc().
  356. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  357. bigger it is, the faster is the decoding. But it should not be too
  358. big to save memory and L1 cache. '9' is a good compromise.
  359. 'nb_codes' : number of vlcs codes
  360. 'bits' : table which gives the size (in bits) of each vlc code.
  361. 'codes' : table which gives the bit pattern of of each vlc code.
  362. 'xxx_wrap' : give the number of bytes between each entry of the
  363. 'bits' or 'codes' tables.
  364. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  365. or 'codes' tables.
  366. 'wrap' and 'size' allows to use any memory configuration and types
  367. (byte/word/long) to store the 'bits' and 'codes' tables.
  368. */
  369. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  370. const void *bits, int bits_wrap, int bits_size,
  371. const void *codes, int codes_wrap, int codes_size)
  372. {
  373. vlc->bits = nb_bits;
  374. vlc->table_bits = NULL;
  375. vlc->table_codes = NULL;
  376. vlc->table_allocated = 0;
  377. vlc->table_size = 0;
  378. #ifdef DEBUG_VLC
  379. printf("build table nb_codes=%d\n", nb_codes);
  380. #endif
  381. if (build_table(vlc, nb_bits, nb_codes,
  382. bits, bits_wrap, bits_size,
  383. codes, codes_wrap, codes_size,
  384. 0, 0) < 0) {
  385. if (vlc->table_bits)
  386. free(vlc->table_bits);
  387. if (vlc->table_codes)
  388. free(vlc->table_codes);
  389. return -1;
  390. }
  391. return 0;
  392. }
  393. void free_vlc(VLC *vlc)
  394. {
  395. free(vlc->table_bits);
  396. free(vlc->table_codes);
  397. }
  398. int get_vlc(GetBitContext *s, VLC *vlc)
  399. {
  400. int bit_cnt, code, n, nb_bits, index;
  401. UINT32 bit_buf;
  402. INT16 *table_codes;
  403. INT8 *table_bits;
  404. UINT8 *buf_ptr;
  405. SAVE_BITS(s);
  406. nb_bits = vlc->bits;
  407. table_codes = vlc->table_codes;
  408. table_bits = vlc->table_bits;
  409. for(;;) {
  410. SHOW_BITS(s, index, nb_bits);
  411. code = table_codes[index];
  412. n = table_bits[index];
  413. if (n > 0) {
  414. /* most common case */
  415. FLUSH_BITS(n);
  416. #ifdef STATS
  417. st_bit_counts[st_current_index] += n;
  418. #endif
  419. break;
  420. } else if (n == 0) {
  421. return -1;
  422. } else {
  423. FLUSH_BITS(nb_bits);
  424. #ifdef STATS
  425. st_bit_counts[st_current_index] += nb_bits;
  426. #endif
  427. nb_bits = -n;
  428. table_codes = vlc->table_codes + code;
  429. table_bits = vlc->table_bits + code;
  430. }
  431. }
  432. RESTORE_BITS(s);
  433. return code;
  434. }