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.

389 lines
11KB

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