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.

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