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.

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