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.

349 lines
9.3KB

  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 "avcodec.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. s->buffer= buffer;
  116. s->size= buffer_size;
  117. s->buffer_end= buffer + buffer_size;
  118. #ifdef ALT_BITSTREAM_READER
  119. s->index=0;
  120. #elif defined LIBMPEG2_BITSTREAM_READER
  121. s->buffer_ptr = buffer;
  122. s->bit_count = 16;
  123. s->cache = 0;
  124. #elif defined A32_BITSTREAM_READER
  125. s->buffer_ptr = (uint32_t*)buffer;
  126. s->bit_count = 32;
  127. s->cache0 = 0;
  128. s->cache1 = 0;
  129. #endif
  130. {
  131. OPEN_READER(re, s)
  132. UPDATE_CACHE(re, s)
  133. // UPDATE_CACHE(re, s)
  134. CLOSE_READER(re, s)
  135. }
  136. #ifdef A32_BITSTREAM_READER
  137. s->cache1 = 0;
  138. #endif
  139. }
  140. void align_get_bits(GetBitContext *s)
  141. {
  142. int n= (-get_bits_count(s)) & 7;
  143. if(n) skip_bits(s, n);
  144. }
  145. int check_marker(GetBitContext *s, char *msg)
  146. {
  147. int bit= get_bits1(s);
  148. if(!bit) printf("Marker bit missing %s\n", msg);
  149. return bit;
  150. }
  151. /* VLC decoding */
  152. //#define DEBUG_VLC
  153. #define GET_DATA(v, table, i, wrap, size) \
  154. {\
  155. UINT8 *ptr = (UINT8 *)table + i * wrap;\
  156. switch(size) {\
  157. case 1:\
  158. v = *(UINT8 *)ptr;\
  159. break;\
  160. case 2:\
  161. v = *(UINT16 *)ptr;\
  162. break;\
  163. default:\
  164. v = *(UINT32 *)ptr;\
  165. break;\
  166. }\
  167. }
  168. static int alloc_table(VLC *vlc, int size)
  169. {
  170. int index;
  171. index = vlc->table_size;
  172. vlc->table_size += size;
  173. if (vlc->table_size > vlc->table_allocated) {
  174. vlc->table_allocated += (1 << vlc->bits);
  175. vlc->table = realloc(vlc->table,
  176. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  177. if (!vlc->table)
  178. return -1;
  179. }
  180. return index;
  181. }
  182. static int build_table(VLC *vlc, int table_nb_bits,
  183. int nb_codes,
  184. const void *bits, int bits_wrap, int bits_size,
  185. const void *codes, int codes_wrap, int codes_size,
  186. UINT32 code_prefix, int n_prefix)
  187. {
  188. int i, j, k, n, table_size, table_index, nb, n1, index;
  189. UINT32 code;
  190. VLC_TYPE (*table)[2];
  191. table_size = 1 << table_nb_bits;
  192. table_index = alloc_table(vlc, table_size);
  193. #ifdef DEBUG_VLC
  194. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  195. table_index, table_size, code_prefix, n_prefix);
  196. #endif
  197. if (table_index < 0)
  198. return -1;
  199. table = &vlc->table[table_index];
  200. for(i=0;i<table_size;i++) {
  201. table[i][1] = 0; //bits
  202. table[i][0] = -1; //codes
  203. }
  204. /* first pass: map codes and compute auxillary table sizes */
  205. for(i=0;i<nb_codes;i++) {
  206. GET_DATA(n, bits, i, bits_wrap, bits_size);
  207. GET_DATA(code, codes, i, codes_wrap, codes_size);
  208. /* we accept tables with holes */
  209. if (n <= 0)
  210. continue;
  211. #if defined(DEBUG_VLC) && 0
  212. printf("i=%d n=%d code=0x%x\n", i, n, code);
  213. #endif
  214. /* if code matches the prefix, it is in the table */
  215. n -= n_prefix;
  216. if (n > 0 && (code >> n) == code_prefix) {
  217. if (n <= table_nb_bits) {
  218. /* no need to add another table */
  219. j = (code << (table_nb_bits - n)) & (table_size - 1);
  220. nb = 1 << (table_nb_bits - n);
  221. for(k=0;k<nb;k++) {
  222. #ifdef DEBUG_VLC
  223. printf("%4x: code=%d n=%d\n",
  224. j, i, n);
  225. #endif
  226. if (table[j][1] /*bits*/ != 0) {
  227. fprintf(stderr, "incorrect codes\n");
  228. exit(1);
  229. }
  230. table[j][1] = n; //bits
  231. table[j][0] = i; //code
  232. j++;
  233. }
  234. } else {
  235. n -= table_nb_bits;
  236. j = (code >> n) & ((1 << table_nb_bits) - 1);
  237. #ifdef DEBUG_VLC
  238. printf("%4x: n=%d (subtable)\n",
  239. j, n);
  240. #endif
  241. /* compute table size */
  242. n1 = -table[j][1]; //bits
  243. if (n > n1)
  244. n1 = n;
  245. table[j][1] = -n1; //bits
  246. }
  247. }
  248. }
  249. /* second pass : fill auxillary tables recursively */
  250. for(i=0;i<table_size;i++) {
  251. n = table[i][1]; //bits
  252. if (n < 0) {
  253. n = -n;
  254. if (n > table_nb_bits) {
  255. n = table_nb_bits;
  256. table[i][1] = -n; //bits
  257. }
  258. index = build_table(vlc, n, nb_codes,
  259. bits, bits_wrap, bits_size,
  260. codes, codes_wrap, codes_size,
  261. (code_prefix << table_nb_bits) | i,
  262. n_prefix + table_nb_bits);
  263. if (index < 0)
  264. return -1;
  265. /* note: realloc has been done, so reload tables */
  266. table = &vlc->table[table_index];
  267. table[i][0] = index; //code
  268. }
  269. }
  270. return table_index;
  271. }
  272. /* Build VLC decoding tables suitable for use with get_vlc().
  273. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  274. bigger it is, the faster is the decoding. But it should not be too
  275. big to save memory and L1 cache. '9' is a good compromise.
  276. 'nb_codes' : number of vlcs codes
  277. 'bits' : table which gives the size (in bits) of each vlc code.
  278. 'codes' : table which gives the bit pattern of of each vlc code.
  279. 'xxx_wrap' : give the number of bytes between each entry of the
  280. 'bits' or 'codes' tables.
  281. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  282. or 'codes' tables.
  283. 'wrap' and 'size' allows to use any memory configuration and types
  284. (byte/word/long) to store the 'bits' and 'codes' tables.
  285. */
  286. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  287. const void *bits, int bits_wrap, int bits_size,
  288. const void *codes, int codes_wrap, int codes_size)
  289. {
  290. vlc->bits = nb_bits;
  291. vlc->table = NULL;
  292. vlc->table_allocated = 0;
  293. vlc->table_size = 0;
  294. #ifdef DEBUG_VLC
  295. printf("build table nb_codes=%d\n", nb_codes);
  296. #endif
  297. if (build_table(vlc, nb_bits, nb_codes,
  298. bits, bits_wrap, bits_size,
  299. codes, codes_wrap, codes_size,
  300. 0, 0) < 0) {
  301. av_free(vlc->table);
  302. return -1;
  303. }
  304. return 0;
  305. }
  306. void free_vlc(VLC *vlc)
  307. {
  308. av_free(vlc->table);
  309. }
  310. int ff_gcd(int a, int b){
  311. if(b) return ff_gcd(b, a%b);
  312. else return a;
  313. }