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.

357 lines
9.9KB

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