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.

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