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.

469 lines
12KB

  1. /*
  2. * Common bit i/o utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. #ifdef 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. val |= bit_buf >> (32 + bit_cnt);
  165. bit_buf <<= - bit_cnt;
  166. bit_cnt += 32;
  167. } else {
  168. buf_ptr -= 4;
  169. bit_buf = 0;
  170. if (buf_ptr < s->buf_end)
  171. bit_buf |= *buf_ptr++ << 24;
  172. if (buf_ptr < s->buf_end)
  173. bit_buf |= *buf_ptr++ << 16;
  174. if (buf_ptr < s->buf_end)
  175. bit_buf |= *buf_ptr++ << 8;
  176. if (buf_ptr < s->buf_end)
  177. bit_buf |= *buf_ptr++;
  178. val |= bit_buf >> (32 + bit_cnt);
  179. bit_buf <<= - bit_cnt;
  180. bit_cnt += 8*(buf_ptr - s->buf_ptr);
  181. if(bit_cnt<0) bit_cnt=0;
  182. }
  183. s->buf_ptr = buf_ptr;
  184. }
  185. s->bit_buf = bit_buf;
  186. s->bit_cnt = bit_cnt;
  187. return val;
  188. }
  189. #endif
  190. void align_get_bits(GetBitContext *s)
  191. {
  192. #ifdef ALT_BITSTREAM_READER
  193. s->index= (s->index + 7) & (~7);
  194. #else
  195. int n;
  196. n = s->bit_cnt & 7;
  197. if (n > 0) {
  198. get_bits(s, n);
  199. }
  200. #endif
  201. }
  202. int check_marker(GetBitContext *s, char *msg)
  203. {
  204. int bit= get_bits1(s);
  205. if(!bit) printf("Marker bit missing %s\n", msg);
  206. return bit;
  207. }
  208. #ifndef ALT_BITSTREAM_READER
  209. /* This function is identical to get_bits_long(), the */
  210. /* only diference is that it doesn't touch the buffer */
  211. /* it is usefull to see the buffer. */
  212. unsigned int show_bits_long(GetBitContext *s, int n)
  213. {
  214. unsigned int val;
  215. int bit_cnt;
  216. unsigned int bit_buf;
  217. UINT8 *buf_ptr;
  218. bit_buf = s->bit_buf;
  219. bit_cnt = s->bit_cnt - n;
  220. val = bit_buf >> (32 - n);
  221. buf_ptr = s->buf_ptr;
  222. buf_ptr += 4;
  223. /* handle common case: we can read everything */
  224. if (buf_ptr <= s->buf_end) {
  225. #ifdef ARCH_X86
  226. bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
  227. #else
  228. bit_buf = (buf_ptr[-4] << 24) |
  229. (buf_ptr[-3] << 16) |
  230. (buf_ptr[-2] << 8) |
  231. (buf_ptr[-1]);
  232. #endif
  233. } else {
  234. buf_ptr -= 4;
  235. bit_buf = 0;
  236. if (buf_ptr < s->buf_end)
  237. bit_buf |= *buf_ptr++ << 24;
  238. if (buf_ptr < s->buf_end)
  239. bit_buf |= *buf_ptr++ << 16;
  240. if (buf_ptr < s->buf_end)
  241. bit_buf |= *buf_ptr++ << 8;
  242. if (buf_ptr < s->buf_end)
  243. bit_buf |= *buf_ptr++;
  244. }
  245. val |= bit_buf >> (32 + bit_cnt);
  246. bit_buf <<= - bit_cnt;
  247. bit_cnt += 32;
  248. return val;
  249. }
  250. #endif
  251. /* VLC decoding */
  252. //#define DEBUG_VLC
  253. #define GET_DATA(v, table, i, wrap, size) \
  254. {\
  255. UINT8 *ptr = (UINT8 *)table + i * wrap;\
  256. switch(size) {\
  257. case 1:\
  258. v = *(UINT8 *)ptr;\
  259. break;\
  260. case 2:\
  261. v = *(UINT16 *)ptr;\
  262. break;\
  263. default:\
  264. v = *(UINT32 *)ptr;\
  265. break;\
  266. }\
  267. }
  268. static int alloc_table(VLC *vlc, int size)
  269. {
  270. int index;
  271. index = vlc->table_size;
  272. vlc->table_size += size;
  273. if (vlc->table_size > vlc->table_allocated) {
  274. vlc->table_allocated += (1 << vlc->bits);
  275. vlc->table_bits = realloc(vlc->table_bits,
  276. sizeof(INT8) * vlc->table_allocated);
  277. vlc->table_codes = realloc(vlc->table_codes,
  278. sizeof(INT16) * vlc->table_allocated);
  279. if (!vlc->table_bits ||
  280. !vlc->table_codes)
  281. return -1;
  282. }
  283. return index;
  284. }
  285. static int build_table(VLC *vlc, int table_nb_bits,
  286. int nb_codes,
  287. const void *bits, int bits_wrap, int bits_size,
  288. const void *codes, int codes_wrap, int codes_size,
  289. UINT32 code_prefix, int n_prefix)
  290. {
  291. int i, j, k, n, table_size, table_index, nb, n1, index;
  292. UINT32 code;
  293. INT8 *table_bits;
  294. INT16 *table_codes;
  295. table_size = 1 << table_nb_bits;
  296. table_index = alloc_table(vlc, table_size);
  297. #ifdef DEBUG_VLC
  298. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  299. table_index, table_size, code_prefix, n_prefix);
  300. #endif
  301. if (table_index < 0)
  302. return -1;
  303. table_bits = &vlc->table_bits[table_index];
  304. table_codes = &vlc->table_codes[table_index];
  305. for(i=0;i<table_size;i++) {
  306. table_bits[i] = 0;
  307. table_codes[i] = -1;
  308. }
  309. /* first pass: map codes and compute auxillary table sizes */
  310. for(i=0;i<nb_codes;i++) {
  311. GET_DATA(n, bits, i, bits_wrap, bits_size);
  312. GET_DATA(code, codes, i, codes_wrap, codes_size);
  313. /* we accept tables with holes */
  314. if (n <= 0)
  315. continue;
  316. #if defined(DEBUG_VLC) && 0
  317. printf("i=%d n=%d code=0x%x\n", i, n, code);
  318. #endif
  319. /* if code matches the prefix, it is in the table */
  320. n -= n_prefix;
  321. if (n > 0 && (code >> n) == code_prefix) {
  322. if (n <= table_nb_bits) {
  323. /* no need to add another table */
  324. j = (code << (table_nb_bits - n)) & (table_size - 1);
  325. nb = 1 << (table_nb_bits - n);
  326. for(k=0;k<nb;k++) {
  327. #ifdef DEBUG_VLC
  328. printf("%4x: code=%d n=%d\n",
  329. j, i, n);
  330. #endif
  331. if (table_bits[j] != 0) {
  332. fprintf(stderr, "incorrect codes\n");
  333. exit(1);
  334. }
  335. table_bits[j] = n;
  336. table_codes[j] = i;
  337. j++;
  338. }
  339. } else {
  340. n -= table_nb_bits;
  341. j = (code >> n) & ((1 << table_nb_bits) - 1);
  342. #ifdef DEBUG_VLC
  343. printf("%4x: n=%d (subtable)\n",
  344. j, n);
  345. #endif
  346. /* compute table size */
  347. n1 = -table_bits[j];
  348. if (n > n1)
  349. n1 = n;
  350. table_bits[j] = -n1;
  351. }
  352. }
  353. }
  354. /* second pass : fill auxillary tables recursively */
  355. for(i=0;i<table_size;i++) {
  356. n = table_bits[i];
  357. if (n < 0) {
  358. n = -n;
  359. if (n > table_nb_bits) {
  360. n = table_nb_bits;
  361. table_bits[i] = -n;
  362. }
  363. index = build_table(vlc, n, nb_codes,
  364. bits, bits_wrap, bits_size,
  365. codes, codes_wrap, codes_size,
  366. (code_prefix << table_nb_bits) | i,
  367. n_prefix + table_nb_bits);
  368. if (index < 0)
  369. return -1;
  370. /* note: realloc has been done, so reload tables */
  371. table_bits = &vlc->table_bits[table_index];
  372. table_codes = &vlc->table_codes[table_index];
  373. table_codes[i] = index;
  374. }
  375. }
  376. return table_index;
  377. }
  378. /* Build VLC decoding tables suitable for use with get_vlc().
  379. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  380. bigger it is, the faster is the decoding. But it should not be too
  381. big to save memory and L1 cache. '9' is a good compromise.
  382. 'nb_codes' : number of vlcs codes
  383. 'bits' : table which gives the size (in bits) of each vlc code.
  384. 'codes' : table which gives the bit pattern of of each vlc code.
  385. 'xxx_wrap' : give the number of bytes between each entry of the
  386. 'bits' or 'codes' tables.
  387. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  388. or 'codes' tables.
  389. 'wrap' and 'size' allows to use any memory configuration and types
  390. (byte/word/long) to store the 'bits' and 'codes' tables.
  391. */
  392. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  393. const void *bits, int bits_wrap, int bits_size,
  394. const void *codes, int codes_wrap, int codes_size)
  395. {
  396. vlc->bits = nb_bits;
  397. vlc->table_bits = NULL;
  398. vlc->table_codes = NULL;
  399. vlc->table_allocated = 0;
  400. vlc->table_size = 0;
  401. #ifdef DEBUG_VLC
  402. printf("build table nb_codes=%d\n", nb_codes);
  403. #endif
  404. if (build_table(vlc, nb_bits, nb_codes,
  405. bits, bits_wrap, bits_size,
  406. codes, codes_wrap, codes_size,
  407. 0, 0) < 0) {
  408. av_free(vlc->table_bits);
  409. av_free(vlc->table_codes);
  410. return -1;
  411. }
  412. return 0;
  413. }
  414. void free_vlc(VLC *vlc)
  415. {
  416. av_free(vlc->table_bits);
  417. av_free(vlc->table_codes);
  418. }
  419. int ff_gcd(int a, int b){
  420. if(b) return ff_gcd(b, a%b);
  421. else return a;
  422. }