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.

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