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.

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