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.

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