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.

422 lines
12KB

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