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.

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