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.

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