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.

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