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.

390 lines
11KB

  1. /*
  2. * Common bit i/o utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  21. */
  22. /**
  23. * @file common.c
  24. * common internal api.
  25. */
  26. #include "avcodec.h"
  27. const uint8_t ff_sqrt_tab[128]={
  28. 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
  29. 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  30. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  31. 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
  32. };
  33. const uint8_t ff_log2_tab[256]={
  34. 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  35. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  36. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  37. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  38. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  39. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  40. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  41. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  42. };
  43. void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
  44. {
  45. s->buf = buffer;
  46. s->buf_end = s->buf + buffer_size;
  47. #ifdef ALT_BITSTREAM_WRITER
  48. s->index=0;
  49. ((uint32_t*)(s->buf))[0]=0;
  50. // memset(buffer, 0, buffer_size);
  51. #else
  52. s->buf_ptr = s->buf;
  53. s->bit_left=32;
  54. s->bit_buf=0;
  55. #endif
  56. }
  57. #ifdef CONFIG_ENCODERS
  58. /* return the number of bits output */
  59. int get_bit_count(PutBitContext *s)
  60. {
  61. #ifdef ALT_BITSTREAM_WRITER
  62. return s->index;
  63. #else
  64. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  65. #endif
  66. }
  67. void align_put_bits(PutBitContext *s)
  68. {
  69. #ifdef ALT_BITSTREAM_WRITER
  70. put_bits(s,( - s->index) & 7,0);
  71. #else
  72. put_bits(s,s->bit_left & 7,0);
  73. #endif
  74. }
  75. #endif //CONFIG_ENCODERS
  76. /* pad the end of the output stream with zeros */
  77. void flush_put_bits(PutBitContext *s)
  78. {
  79. #ifdef ALT_BITSTREAM_WRITER
  80. align_put_bits(s);
  81. #else
  82. s->bit_buf<<= s->bit_left;
  83. while (s->bit_left < 32) {
  84. /* XXX: should test end of buffer */
  85. *s->buf_ptr++=s->bit_buf >> 24;
  86. s->bit_buf<<=8;
  87. s->bit_left+=8;
  88. }
  89. s->bit_left=32;
  90. s->bit_buf=0;
  91. #endif
  92. }
  93. #ifdef CONFIG_ENCODERS
  94. void put_string(PutBitContext * pbc, char *s)
  95. {
  96. while(*s){
  97. put_bits(pbc, 8, *s);
  98. s++;
  99. }
  100. put_bits(pbc, 8, 0);
  101. }
  102. /* bit input functions */
  103. #endif //CONFIG_ENCODERS
  104. /**
  105. * init GetBitContext.
  106. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  107. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  108. * @param bit_size the size of the buffer in bits
  109. */
  110. void init_get_bits(GetBitContext *s,
  111. const uint8_t *buffer, int bit_size)
  112. {
  113. const int buffer_size= (bit_size+7)>>3;
  114. s->buffer= buffer;
  115. s->size_in_bits= bit_size;
  116. s->buffer_end= buffer + buffer_size;
  117. #ifdef ALT_BITSTREAM_READER
  118. s->index=0;
  119. #elif defined LIBMPEG2_BITSTREAM_READER
  120. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  121. if ((int)buffer&1) {
  122. /* word alignment */
  123. s->cache = (*buffer++)<<24;
  124. s->buffer_ptr = buffer;
  125. s->bit_count = 16-8;
  126. } else
  127. #endif
  128. {
  129. s->buffer_ptr = buffer;
  130. s->bit_count = 16;
  131. s->cache = 0;
  132. }
  133. #elif defined A32_BITSTREAM_READER
  134. s->buffer_ptr = (uint32_t*)buffer;
  135. s->bit_count = 32;
  136. s->cache0 = 0;
  137. s->cache1 = 0;
  138. #endif
  139. {
  140. OPEN_READER(re, s)
  141. UPDATE_CACHE(re, s)
  142. UPDATE_CACHE(re, s)
  143. CLOSE_READER(re, s)
  144. }
  145. #ifdef A32_BITSTREAM_READER
  146. s->cache1 = 0;
  147. #endif
  148. }
  149. /**
  150. * reads 0-32 bits.
  151. */
  152. unsigned int get_bits_long(GetBitContext *s, int n){
  153. if(n<=17) return get_bits(s, n);
  154. else{
  155. int ret= get_bits(s, 16) << (n-16);
  156. return ret | get_bits(s, n-16);
  157. }
  158. }
  159. /**
  160. * shows 0-32 bits.
  161. */
  162. unsigned int show_bits_long(GetBitContext *s, int n){
  163. if(n<=17) return show_bits(s, n);
  164. else{
  165. GetBitContext gb= *s;
  166. int ret= get_bits_long(s, n);
  167. *s= gb;
  168. return ret;
  169. }
  170. }
  171. void align_get_bits(GetBitContext *s)
  172. {
  173. int n= (-get_bits_count(s)) & 7;
  174. if(n) skip_bits(s, n);
  175. }
  176. int check_marker(GetBitContext *s, const char *msg)
  177. {
  178. int bit= get_bits1(s);
  179. if(!bit)
  180. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  181. return bit;
  182. }
  183. /* VLC decoding */
  184. //#define DEBUG_VLC
  185. #define GET_DATA(v, table, i, wrap, size) \
  186. {\
  187. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  188. switch(size) {\
  189. case 1:\
  190. v = *(const uint8_t *)ptr;\
  191. break;\
  192. case 2:\
  193. v = *(const uint16_t *)ptr;\
  194. break;\
  195. default:\
  196. v = *(const uint32_t *)ptr;\
  197. break;\
  198. }\
  199. }
  200. static int alloc_table(VLC *vlc, int size)
  201. {
  202. int index;
  203. index = vlc->table_size;
  204. vlc->table_size += size;
  205. if (vlc->table_size > vlc->table_allocated) {
  206. vlc->table_allocated += (1 << vlc->bits);
  207. vlc->table = av_realloc(vlc->table,
  208. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  209. if (!vlc->table)
  210. return -1;
  211. }
  212. return index;
  213. }
  214. static int build_table(VLC *vlc, int table_nb_bits,
  215. int nb_codes,
  216. const void *bits, int bits_wrap, int bits_size,
  217. const void *codes, int codes_wrap, int codes_size,
  218. uint32_t code_prefix, int n_prefix)
  219. {
  220. int i, j, k, n, table_size, table_index, nb, n1, index;
  221. uint32_t code;
  222. VLC_TYPE (*table)[2];
  223. table_size = 1 << table_nb_bits;
  224. table_index = alloc_table(vlc, table_size);
  225. #ifdef DEBUG_VLC
  226. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  227. table_index, table_size, code_prefix, n_prefix);
  228. #endif
  229. if (table_index < 0)
  230. return -1;
  231. table = &vlc->table[table_index];
  232. for(i=0;i<table_size;i++) {
  233. table[i][1] = 0; //bits
  234. table[i][0] = -1; //codes
  235. }
  236. /* first pass: map codes and compute auxillary table sizes */
  237. for(i=0;i<nb_codes;i++) {
  238. GET_DATA(n, bits, i, bits_wrap, bits_size);
  239. GET_DATA(code, codes, i, codes_wrap, codes_size);
  240. /* we accept tables with holes */
  241. if (n <= 0)
  242. continue;
  243. #if defined(DEBUG_VLC) && 0
  244. printf("i=%d n=%d code=0x%x\n", i, n, code);
  245. #endif
  246. /* if code matches the prefix, it is in the table */
  247. n -= n_prefix;
  248. if (n > 0 && (code >> n) == code_prefix) {
  249. if (n <= table_nb_bits) {
  250. /* no need to add another table */
  251. j = (code << (table_nb_bits - n)) & (table_size - 1);
  252. nb = 1 << (table_nb_bits - n);
  253. for(k=0;k<nb;k++) {
  254. #ifdef DEBUG_VLC
  255. av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
  256. j, i, n);
  257. #endif
  258. if (table[j][1] /*bits*/ != 0) {
  259. av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
  260. av_abort();
  261. }
  262. table[j][1] = n; //bits
  263. table[j][0] = i; //code
  264. j++;
  265. }
  266. } else {
  267. n -= table_nb_bits;
  268. j = (code >> n) & ((1 << table_nb_bits) - 1);
  269. #ifdef DEBUG_VLC
  270. printf("%4x: n=%d (subtable)\n",
  271. j, n);
  272. #endif
  273. /* compute table size */
  274. n1 = -table[j][1]; //bits
  275. if (n > n1)
  276. n1 = n;
  277. table[j][1] = -n1; //bits
  278. }
  279. }
  280. }
  281. /* second pass : fill auxillary tables recursively */
  282. for(i=0;i<table_size;i++) {
  283. n = table[i][1]; //bits
  284. if (n < 0) {
  285. n = -n;
  286. if (n > table_nb_bits) {
  287. n = table_nb_bits;
  288. table[i][1] = -n; //bits
  289. }
  290. index = build_table(vlc, n, nb_codes,
  291. bits, bits_wrap, bits_size,
  292. codes, codes_wrap, codes_size,
  293. (code_prefix << table_nb_bits) | i,
  294. n_prefix + table_nb_bits);
  295. if (index < 0)
  296. return -1;
  297. /* note: realloc has been done, so reload tables */
  298. table = &vlc->table[table_index];
  299. table[i][0] = index; //code
  300. }
  301. }
  302. return table_index;
  303. }
  304. /* Build VLC decoding tables suitable for use with get_vlc().
  305. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  306. bigger it is, the faster is the decoding. But it should not be too
  307. big to save memory and L1 cache. '9' is a good compromise.
  308. 'nb_codes' : number of vlcs codes
  309. 'bits' : table which gives the size (in bits) of each vlc code.
  310. 'codes' : table which gives the bit pattern of of each vlc code.
  311. 'xxx_wrap' : give the number of bytes between each entry of the
  312. 'bits' or 'codes' tables.
  313. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  314. or 'codes' tables.
  315. 'wrap' and 'size' allows to use any memory configuration and types
  316. (byte/word/long) to store the 'bits' and 'codes' tables.
  317. */
  318. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  319. const void *bits, int bits_wrap, int bits_size,
  320. const void *codes, int codes_wrap, int codes_size)
  321. {
  322. vlc->bits = nb_bits;
  323. vlc->table = NULL;
  324. vlc->table_allocated = 0;
  325. vlc->table_size = 0;
  326. #ifdef DEBUG_VLC
  327. printf("build table nb_codes=%d\n", nb_codes);
  328. #endif
  329. if (build_table(vlc, nb_bits, nb_codes,
  330. bits, bits_wrap, bits_size,
  331. codes, codes_wrap, codes_size,
  332. 0, 0) < 0) {
  333. av_free(vlc->table);
  334. return -1;
  335. }
  336. return 0;
  337. }
  338. void free_vlc(VLC *vlc)
  339. {
  340. av_free(vlc->table);
  341. }
  342. int64_t ff_gcd(int64_t a, int64_t b){
  343. if(b) return ff_gcd(b, a%b);
  344. else return a;
  345. }