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.

392 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. #if 1
  59. /* return the number of bits output */
  60. int put_bits_count(PutBitContext *s)
  61. {
  62. #ifdef ALT_BITSTREAM_WRITER
  63. return s->index;
  64. #else
  65. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  66. #endif
  67. }
  68. void align_put_bits(PutBitContext *s)
  69. {
  70. #ifdef ALT_BITSTREAM_WRITER
  71. put_bits(s,( - s->index) & 7,0);
  72. #else
  73. put_bits(s,s->bit_left & 7,0);
  74. #endif
  75. }
  76. #endif //CONFIG_ENCODERS
  77. /* pad the end of the output stream with zeros */
  78. void flush_put_bits(PutBitContext *s)
  79. {
  80. #ifdef ALT_BITSTREAM_WRITER
  81. align_put_bits(s);
  82. #else
  83. s->bit_buf<<= s->bit_left;
  84. while (s->bit_left < 32) {
  85. /* XXX: should test end of buffer */
  86. *s->buf_ptr++=s->bit_buf >> 24;
  87. s->bit_buf<<=8;
  88. s->bit_left+=8;
  89. }
  90. s->bit_left=32;
  91. s->bit_buf=0;
  92. #endif
  93. }
  94. #ifdef CONFIG_ENCODERS
  95. void put_string(PutBitContext * pbc, char *s, int put_zero)
  96. {
  97. while(*s){
  98. put_bits(pbc, 8, *s);
  99. s++;
  100. }
  101. if(put_zero)
  102. put_bits(pbc, 8, 0);
  103. }
  104. /* bit input functions */
  105. #endif //CONFIG_ENCODERS
  106. /**
  107. * init GetBitContext.
  108. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
  109. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  110. * @param bit_size the size of the buffer in bits
  111. */
  112. void init_get_bits(GetBitContext *s,
  113. const uint8_t *buffer, int bit_size)
  114. {
  115. const int buffer_size= (bit_size+7)>>3;
  116. s->buffer= buffer;
  117. s->size_in_bits= bit_size;
  118. s->buffer_end= buffer + buffer_size;
  119. #ifdef ALT_BITSTREAM_READER
  120. s->index=0;
  121. #elif defined LIBMPEG2_BITSTREAM_READER
  122. #ifdef LIBMPEG2_BITSTREAM_READER_HACK
  123. if ((int)buffer&1) {
  124. /* word alignment */
  125. s->cache = (*buffer++)<<24;
  126. s->buffer_ptr = buffer;
  127. s->bit_count = 16-8;
  128. } else
  129. #endif
  130. {
  131. s->buffer_ptr = buffer;
  132. s->bit_count = 16;
  133. s->cache = 0;
  134. }
  135. #elif defined A32_BITSTREAM_READER
  136. s->buffer_ptr = (uint32_t*)buffer;
  137. s->bit_count = 32;
  138. s->cache0 = 0;
  139. s->cache1 = 0;
  140. #endif
  141. {
  142. OPEN_READER(re, s)
  143. UPDATE_CACHE(re, s)
  144. UPDATE_CACHE(re, s)
  145. CLOSE_READER(re, s)
  146. }
  147. #ifdef A32_BITSTREAM_READER
  148. s->cache1 = 0;
  149. #endif
  150. }
  151. /**
  152. * reads 0-32 bits.
  153. */
  154. unsigned int get_bits_long(GetBitContext *s, int n){
  155. if(n<=17) return get_bits(s, n);
  156. else{
  157. int ret= get_bits(s, 16) << (n-16);
  158. return ret | get_bits(s, n-16);
  159. }
  160. }
  161. /**
  162. * shows 0-32 bits.
  163. */
  164. unsigned int show_bits_long(GetBitContext *s, int n){
  165. if(n<=17) return show_bits(s, n);
  166. else{
  167. GetBitContext gb= *s;
  168. int ret= get_bits_long(s, n);
  169. *s= gb;
  170. return ret;
  171. }
  172. }
  173. void align_get_bits(GetBitContext *s)
  174. {
  175. int n= (-get_bits_count(s)) & 7;
  176. if(n) skip_bits(s, n);
  177. }
  178. int check_marker(GetBitContext *s, const char *msg)
  179. {
  180. int bit= get_bits1(s);
  181. if(!bit)
  182. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  183. return bit;
  184. }
  185. /* VLC decoding */
  186. //#define DEBUG_VLC
  187. #define GET_DATA(v, table, i, wrap, size) \
  188. {\
  189. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  190. switch(size) {\
  191. case 1:\
  192. v = *(const uint8_t *)ptr;\
  193. break;\
  194. case 2:\
  195. v = *(const uint16_t *)ptr;\
  196. break;\
  197. default:\
  198. v = *(const uint32_t *)ptr;\
  199. break;\
  200. }\
  201. }
  202. static int alloc_table(VLC *vlc, int size)
  203. {
  204. int index;
  205. index = vlc->table_size;
  206. vlc->table_size += size;
  207. if (vlc->table_size > vlc->table_allocated) {
  208. vlc->table_allocated += (1 << vlc->bits);
  209. vlc->table = av_realloc(vlc->table,
  210. sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  211. if (!vlc->table)
  212. return -1;
  213. }
  214. return index;
  215. }
  216. static int build_table(VLC *vlc, int table_nb_bits,
  217. int nb_codes,
  218. const void *bits, int bits_wrap, int bits_size,
  219. const void *codes, int codes_wrap, int codes_size,
  220. uint32_t code_prefix, int n_prefix)
  221. {
  222. int i, j, k, n, table_size, table_index, nb, n1, index;
  223. uint32_t code;
  224. VLC_TYPE (*table)[2];
  225. table_size = 1 << table_nb_bits;
  226. table_index = alloc_table(vlc, table_size);
  227. #ifdef DEBUG_VLC
  228. printf("new table index=%d size=%d code_prefix=%x n=%d\n",
  229. table_index, table_size, code_prefix, n_prefix);
  230. #endif
  231. if (table_index < 0)
  232. return -1;
  233. table = &vlc->table[table_index];
  234. for(i=0;i<table_size;i++) {
  235. table[i][1] = 0; //bits
  236. table[i][0] = -1; //codes
  237. }
  238. /* first pass: map codes and compute auxillary table sizes */
  239. for(i=0;i<nb_codes;i++) {
  240. GET_DATA(n, bits, i, bits_wrap, bits_size);
  241. GET_DATA(code, codes, i, codes_wrap, codes_size);
  242. /* we accept tables with holes */
  243. if (n <= 0)
  244. continue;
  245. #if defined(DEBUG_VLC) && 0
  246. printf("i=%d n=%d code=0x%x\n", i, n, code);
  247. #endif
  248. /* if code matches the prefix, it is in the table */
  249. n -= n_prefix;
  250. if (n > 0 && (code >> n) == code_prefix) {
  251. if (n <= table_nb_bits) {
  252. /* no need to add another table */
  253. j = (code << (table_nb_bits - n)) & (table_size - 1);
  254. nb = 1 << (table_nb_bits - n);
  255. for(k=0;k<nb;k++) {
  256. #ifdef DEBUG_VLC
  257. av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
  258. j, i, n);
  259. #endif
  260. if (table[j][1] /*bits*/ != 0) {
  261. av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
  262. av_abort();
  263. }
  264. table[j][1] = n; //bits
  265. table[j][0] = i; //code
  266. j++;
  267. }
  268. } else {
  269. n -= table_nb_bits;
  270. j = (code >> n) & ((1 << table_nb_bits) - 1);
  271. #ifdef DEBUG_VLC
  272. printf("%4x: n=%d (subtable)\n",
  273. j, n);
  274. #endif
  275. /* compute table size */
  276. n1 = -table[j][1]; //bits
  277. if (n > n1)
  278. n1 = n;
  279. table[j][1] = -n1; //bits
  280. }
  281. }
  282. }
  283. /* second pass : fill auxillary tables recursively */
  284. for(i=0;i<table_size;i++) {
  285. n = table[i][1]; //bits
  286. if (n < 0) {
  287. n = -n;
  288. if (n > table_nb_bits) {
  289. n = table_nb_bits;
  290. table[i][1] = -n; //bits
  291. }
  292. index = build_table(vlc, n, nb_codes,
  293. bits, bits_wrap, bits_size,
  294. codes, codes_wrap, codes_size,
  295. (code_prefix << table_nb_bits) | i,
  296. n_prefix + table_nb_bits);
  297. if (index < 0)
  298. return -1;
  299. /* note: realloc has been done, so reload tables */
  300. table = &vlc->table[table_index];
  301. table[i][0] = index; //code
  302. }
  303. }
  304. return table_index;
  305. }
  306. /* Build VLC decoding tables suitable for use with get_vlc().
  307. 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
  308. bigger it is, the faster is the decoding. But it should not be too
  309. big to save memory and L1 cache. '9' is a good compromise.
  310. 'nb_codes' : number of vlcs codes
  311. 'bits' : table which gives the size (in bits) of each vlc code.
  312. 'codes' : table which gives the bit pattern of of each vlc code.
  313. 'xxx_wrap' : give the number of bytes between each entry of the
  314. 'bits' or 'codes' tables.
  315. 'xxx_size' : gives the number of bytes of each entry of the 'bits'
  316. or 'codes' tables.
  317. 'wrap' and 'size' allows to use any memory configuration and types
  318. (byte/word/long) to store the 'bits' and 'codes' tables.
  319. */
  320. int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  321. const void *bits, int bits_wrap, int bits_size,
  322. const void *codes, int codes_wrap, int codes_size)
  323. {
  324. vlc->bits = nb_bits;
  325. vlc->table = NULL;
  326. vlc->table_allocated = 0;
  327. vlc->table_size = 0;
  328. #ifdef DEBUG_VLC
  329. printf("build table nb_codes=%d\n", nb_codes);
  330. #endif
  331. if (build_table(vlc, nb_bits, nb_codes,
  332. bits, bits_wrap, bits_size,
  333. codes, codes_wrap, codes_size,
  334. 0, 0) < 0) {
  335. av_free(vlc->table);
  336. return -1;
  337. }
  338. return 0;
  339. }
  340. void free_vlc(VLC *vlc)
  341. {
  342. av_free(vlc->table);
  343. }
  344. int64_t ff_gcd(int64_t a, int64_t b){
  345. if(b) return ff_gcd(b, a%b);
  346. else return a;
  347. }