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.

602 lines
19KB

  1. /*
  2. * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * bitstream reader API header.
  23. */
  24. #ifndef AVCODEC_GET_BITS_H
  25. #define AVCODEC_GET_BITS_H
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include <assert.h>
  29. #include "libavutil/bswap.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/log.h"
  33. #include "mathops.h"
  34. #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
  35. # define ALT_BITSTREAM_READER
  36. #endif
  37. #if !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
  38. # if ARCH_ARM && !HAVE_FAST_UNALIGNED
  39. # define A32_BITSTREAM_READER
  40. # else
  41. # define ALT_BITSTREAM_READER
  42. //#define A32_BITSTREAM_READER
  43. # endif
  44. #endif
  45. /* bit input */
  46. /* buffer, buffer_end and size_in_bits must be present and used by every reader */
  47. typedef struct GetBitContext {
  48. const uint8_t *buffer, *buffer_end;
  49. #ifdef ALT_BITSTREAM_READER
  50. int index;
  51. #elif defined A32_BITSTREAM_READER
  52. uint32_t *buffer_ptr;
  53. uint32_t cache0;
  54. uint32_t cache1;
  55. int bit_count;
  56. #endif
  57. int size_in_bits;
  58. } GetBitContext;
  59. #define VLC_TYPE int16_t
  60. typedef struct VLC {
  61. int bits;
  62. VLC_TYPE (*table)[2]; ///< code, bits
  63. int table_size, table_allocated;
  64. } VLC;
  65. typedef struct RL_VLC_ELEM {
  66. int16_t level;
  67. int8_t len;
  68. uint8_t run;
  69. } RL_VLC_ELEM;
  70. /* Bitstream reader API docs:
  71. name
  72. arbitrary name which is used as prefix for the internal variables
  73. gb
  74. getbitcontext
  75. OPEN_READER(name, gb)
  76. loads gb into local variables
  77. CLOSE_READER(name, gb)
  78. stores local vars in gb
  79. UPDATE_CACHE(name, gb)
  80. refills the internal cache from the bitstream
  81. after this call at least MIN_CACHE_BITS will be available,
  82. GET_CACHE(name, gb)
  83. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  84. SHOW_UBITS(name, gb, num)
  85. will return the next num bits
  86. SHOW_SBITS(name, gb, num)
  87. will return the next num bits and do sign extension
  88. SKIP_BITS(name, gb, num)
  89. will skip over the next num bits
  90. note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
  91. SKIP_CACHE(name, gb, num)
  92. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  93. SKIP_COUNTER(name, gb, num)
  94. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  95. LAST_SKIP_CACHE(name, gb, num)
  96. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  97. LAST_SKIP_BITS(name, gb, num)
  98. is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
  99. for examples see get_bits, show_bits, skip_bits, get_vlc
  100. */
  101. #ifdef ALT_BITSTREAM_READER
  102. # define MIN_CACHE_BITS 25
  103. # define OPEN_READER(name, gb) \
  104. unsigned int name##_index = (gb)->index; \
  105. unsigned int av_unused name##_cache = 0
  106. # define CLOSE_READER(name, gb) (gb)->index = name##_index
  107. # ifdef ALT_BITSTREAM_READER_LE
  108. # define UPDATE_CACHE(name, gb) \
  109. name##_cache = AV_RL32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) >> (name##_index&0x07)
  110. # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
  111. # else
  112. # define UPDATE_CACHE(name, gb) \
  113. name##_cache = AV_RB32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) << (name##_index&0x07)
  114. # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
  115. # endif
  116. // FIXME name?
  117. # define SKIP_COUNTER(name, gb, num) name##_index += (num)
  118. # define SKIP_BITS(name, gb, num) do { \
  119. SKIP_CACHE(name, gb, num); \
  120. SKIP_COUNTER(name, gb, num); \
  121. } while (0)
  122. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  123. # define LAST_SKIP_CACHE(name, gb, num)
  124. # ifdef ALT_BITSTREAM_READER_LE
  125. # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
  126. # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
  127. # else
  128. # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
  129. # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
  130. # endif
  131. # define GET_CACHE(name, gb) ((uint32_t)name##_cache)
  132. static inline int get_bits_count(const GetBitContext *s){
  133. return s->index;
  134. }
  135. static inline void skip_bits_long(GetBitContext *s, int n){
  136. s->index += n;
  137. }
  138. #elif defined A32_BITSTREAM_READER
  139. # define MIN_CACHE_BITS 32
  140. # define OPEN_READER(name, gb) \
  141. int name##_bit_count = (gb)->bit_count; \
  142. uint32_t name##_cache0 = (gb)->cache0; \
  143. uint32_t name##_cache1 = (gb)->cache1; \
  144. uint32_t *name##_buffer_ptr = (gb)->buffer_ptr
  145. # define CLOSE_READER(name, gb) do { \
  146. (gb)->bit_count = name##_bit_count; \
  147. (gb)->cache0 = name##_cache0; \
  148. (gb)->cache1 = name##_cache1; \
  149. (gb)->buffer_ptr = name##_buffer_ptr; \
  150. } while (0)
  151. # define UPDATE_CACHE(name, gb) do { \
  152. if(name##_bit_count > 0){ \
  153. const uint32_t next = av_be2ne32(*name##_buffer_ptr); \
  154. name##_cache0 |= NEG_USR32(next, name##_bit_count); \
  155. name##_cache1 |= next << name##_bit_count; \
  156. name##_buffer_ptr++; \
  157. name##_bit_count -= 32; \
  158. } \
  159. } while (0)
  160. # define SKIP_CACHE(name, gb, num) do { \
  161. name##_cache0 <<= (num); \
  162. name##_cache0 |= NEG_USR32(name##_cache1,num); \
  163. name##_cache1 <<= (num); \
  164. } while (0)
  165. # define SKIP_COUNTER(name, gb, num) name##_bit_count += (num)
  166. # define SKIP_BITS(name, gb, num) do { \
  167. SKIP_CACHE(name, gb, num); \
  168. SKIP_COUNTER(name, gb, num); \
  169. } while (0)
  170. # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
  171. # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
  172. # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache0, num)
  173. # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache0, num)
  174. # define GET_CACHE(name, gb) name##_cache0
  175. static inline int get_bits_count(const GetBitContext *s) {
  176. return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
  177. }
  178. static inline void skip_bits_long(GetBitContext *s, int n){
  179. OPEN_READER(re, s);
  180. re_bit_count += n;
  181. re_buffer_ptr += re_bit_count>>5;
  182. re_bit_count &= 31;
  183. re_cache0 = av_be2ne32(re_buffer_ptr[-1]) << re_bit_count;
  184. re_cache1 = 0;
  185. UPDATE_CACHE(re, s);
  186. CLOSE_READER(re, s);
  187. }
  188. #endif
  189. /**
  190. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  191. * if MSB not set it is negative
  192. * @param n length in bits
  193. * @author BERO
  194. */
  195. static inline int get_xbits(GetBitContext *s, int n){
  196. register int sign;
  197. register int32_t cache;
  198. OPEN_READER(re, s);
  199. UPDATE_CACHE(re, s);
  200. cache = GET_CACHE(re, s);
  201. sign = ~cache >> 31;
  202. LAST_SKIP_BITS(re, s, n);
  203. CLOSE_READER(re, s);
  204. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  205. }
  206. static inline int get_sbits(GetBitContext *s, int n){
  207. register int tmp;
  208. OPEN_READER(re, s);
  209. UPDATE_CACHE(re, s);
  210. tmp = SHOW_SBITS(re, s, n);
  211. LAST_SKIP_BITS(re, s, n);
  212. CLOSE_READER(re, s);
  213. return tmp;
  214. }
  215. /**
  216. * Read 1-25 bits.
  217. */
  218. static inline unsigned int get_bits(GetBitContext *s, int n){
  219. register int tmp;
  220. OPEN_READER(re, s);
  221. UPDATE_CACHE(re, s);
  222. tmp = SHOW_UBITS(re, s, n);
  223. LAST_SKIP_BITS(re, s, n);
  224. CLOSE_READER(re, s);
  225. return tmp;
  226. }
  227. /**
  228. * Shows 1-25 bits.
  229. */
  230. static inline unsigned int show_bits(GetBitContext *s, int n){
  231. register int tmp;
  232. OPEN_READER(re, s);
  233. UPDATE_CACHE(re, s);
  234. tmp = SHOW_UBITS(re, s, n);
  235. return tmp;
  236. }
  237. static inline void skip_bits(GetBitContext *s, int n){
  238. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  239. OPEN_READER(re, s);
  240. UPDATE_CACHE(re, s);
  241. LAST_SKIP_BITS(re, s, n);
  242. CLOSE_READER(re, s);
  243. }
  244. static inline unsigned int get_bits1(GetBitContext *s){
  245. #ifdef ALT_BITSTREAM_READER
  246. unsigned int index = s->index;
  247. uint8_t result = s->buffer[index>>3];
  248. #ifdef ALT_BITSTREAM_READER_LE
  249. result >>= index & 7;
  250. result &= 1;
  251. #else
  252. result <<= index & 7;
  253. result >>= 8 - 1;
  254. #endif
  255. index++;
  256. s->index = index;
  257. return result;
  258. #else
  259. return get_bits(s, 1);
  260. #endif
  261. }
  262. static inline unsigned int show_bits1(GetBitContext *s){
  263. return show_bits(s, 1);
  264. }
  265. static inline void skip_bits1(GetBitContext *s){
  266. skip_bits(s, 1);
  267. }
  268. /**
  269. * reads 0-32 bits.
  270. */
  271. static inline unsigned int get_bits_long(GetBitContext *s, int n){
  272. if (n <= MIN_CACHE_BITS) return get_bits(s, n);
  273. else {
  274. #ifdef ALT_BITSTREAM_READER_LE
  275. int ret = get_bits(s, 16);
  276. return ret | (get_bits(s, n-16) << 16);
  277. #else
  278. int ret = get_bits(s, 16) << (n-16);
  279. return ret | get_bits(s, n-16);
  280. #endif
  281. }
  282. }
  283. /**
  284. * reads 0-32 bits as a signed integer.
  285. */
  286. static inline int get_sbits_long(GetBitContext *s, int n) {
  287. return sign_extend(get_bits_long(s, n), n);
  288. }
  289. /**
  290. * shows 0-32 bits.
  291. */
  292. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  293. if (n <= MIN_CACHE_BITS) return show_bits(s, n);
  294. else {
  295. GetBitContext gb = *s;
  296. return get_bits_long(&gb, n);
  297. }
  298. }
  299. static inline int check_marker(GetBitContext *s, const char *msg)
  300. {
  301. int bit = get_bits1(s);
  302. if (!bit)
  303. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  304. return bit;
  305. }
  306. /**
  307. * init GetBitContext.
  308. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
  309. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  310. * @param bit_size the size of the buffer in bits
  311. *
  312. * While GetBitContext stores the buffer size, for performance reasons you are
  313. * responsible for checking for the buffer end yourself (take advantage of the padding)!
  314. */
  315. static inline void init_get_bits(GetBitContext *s,
  316. const uint8_t *buffer, int bit_size)
  317. {
  318. int buffer_size = (bit_size+7)>>3;
  319. if (buffer_size < 0 || bit_size < 0) {
  320. buffer_size = bit_size = 0;
  321. buffer = NULL;
  322. }
  323. s->buffer = buffer;
  324. s->size_in_bits = bit_size;
  325. s->buffer_end = buffer + buffer_size;
  326. #ifdef ALT_BITSTREAM_READER
  327. s->index = 0;
  328. #elif defined A32_BITSTREAM_READER
  329. s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);
  330. s->bit_count = 32 + 8*((intptr_t)buffer & 3);
  331. skip_bits_long(s, 0);
  332. #endif
  333. }
  334. static inline void align_get_bits(GetBitContext *s)
  335. {
  336. int n = -get_bits_count(s) & 7;
  337. if (n) skip_bits(s, n);
  338. }
  339. #define init_vlc(vlc, nb_bits, nb_codes, \
  340. bits, bits_wrap, bits_size, \
  341. codes, codes_wrap, codes_size, \
  342. flags) \
  343. init_vlc_sparse(vlc, nb_bits, nb_codes, \
  344. bits, bits_wrap, bits_size, \
  345. codes, codes_wrap, codes_size, \
  346. NULL, 0, 0, flags)
  347. int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  348. const void *bits, int bits_wrap, int bits_size,
  349. const void *codes, int codes_wrap, int codes_size,
  350. const void *symbols, int symbols_wrap, int symbols_size,
  351. int flags);
  352. #define INIT_VLC_LE 2
  353. #define INIT_VLC_USE_NEW_STATIC 4
  354. void free_vlc(VLC *vlc);
  355. #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
  356. static VLC_TYPE table[static_size][2]; \
  357. (vlc)->table = table; \
  358. (vlc)->table_allocated = static_size; \
  359. init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
  360. } while (0)
  361. /**
  362. *
  363. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  364. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  365. * is undefined.
  366. */
  367. #define GET_VLC(code, name, gb, table, bits, max_depth) do { \
  368. int n, nb_bits; \
  369. unsigned int index; \
  370. \
  371. index = SHOW_UBITS(name, gb, bits); \
  372. code = table[index][0]; \
  373. n = table[index][1]; \
  374. \
  375. if (max_depth > 1 && n < 0) { \
  376. LAST_SKIP_BITS(name, gb, bits); \
  377. UPDATE_CACHE(name, gb); \
  378. \
  379. nb_bits = -n; \
  380. \
  381. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  382. code = table[index][0]; \
  383. n = table[index][1]; \
  384. if (max_depth > 2 && n < 0) { \
  385. LAST_SKIP_BITS(name, gb, nb_bits); \
  386. UPDATE_CACHE(name, gb); \
  387. \
  388. nb_bits = -n; \
  389. \
  390. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  391. code = table[index][0]; \
  392. n = table[index][1]; \
  393. } \
  394. } \
  395. SKIP_BITS(name, gb, n); \
  396. } while (0)
  397. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) do { \
  398. int n, nb_bits; \
  399. unsigned int index; \
  400. \
  401. index = SHOW_UBITS(name, gb, bits); \
  402. level = table[index].level; \
  403. n = table[index].len; \
  404. \
  405. if (max_depth > 1 && n < 0) { \
  406. SKIP_BITS(name, gb, bits); \
  407. if (need_update) { \
  408. UPDATE_CACHE(name, gb); \
  409. } \
  410. \
  411. nb_bits = -n; \
  412. \
  413. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  414. level = table[index].level; \
  415. n = table[index].len; \
  416. } \
  417. run = table[index].run; \
  418. SKIP_BITS(name, gb, n); \
  419. } while (0)
  420. /**
  421. * parses a vlc code, faster than get_vlc()
  422. * @param bits is the number of bits which will be read at once, must be
  423. * identical to nb_bits in init_vlc()
  424. * @param max_depth is the number of times bits bits must be read to completely
  425. * read the longest vlc code
  426. * = (max_vlc_length + bits - 1) / bits
  427. */
  428. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  429. int bits, int max_depth)
  430. {
  431. int code;
  432. OPEN_READER(re, s);
  433. UPDATE_CACHE(re, s);
  434. GET_VLC(code, re, s, table, bits, max_depth);
  435. CLOSE_READER(re, s);
  436. return code;
  437. }
  438. static inline int decode012(GetBitContext *gb){
  439. int n;
  440. n = get_bits1(gb);
  441. if (n == 0)
  442. return 0;
  443. else
  444. return get_bits1(gb) + 1;
  445. }
  446. static inline int decode210(GetBitContext *gb){
  447. if (get_bits1(gb))
  448. return 0;
  449. else
  450. return 2 - get_bits1(gb);
  451. }
  452. static inline int get_bits_left(GetBitContext *gb)
  453. {
  454. return gb->size_in_bits - get_bits_count(gb);
  455. }
  456. //#define TRACE
  457. #ifdef TRACE
  458. static inline void print_bin(int bits, int n){
  459. int i;
  460. for (i = n-1; i >= 0; i--) {
  461. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  462. }
  463. for (i = n; i < 24; i++)
  464. av_log(NULL, AV_LOG_DEBUG, " ");
  465. }
  466. static inline int get_bits_trace(GetBitContext *s, int n, char *file,
  467. const char *func, int line){
  468. int r = get_bits(s, n);
  469. print_bin(r, n);
  470. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  471. r, n, r, get_bits_count(s)-n, file, func, line);
  472. return r;
  473. }
  474. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  475. int bits, int max_depth, char *file,
  476. const char *func, int line){
  477. int show = show_bits(s, 24);
  478. int pos = get_bits_count(s);
  479. int r = get_vlc2(s, table, bits, max_depth);
  480. int len = get_bits_count(s) - pos;
  481. int bits2 = show >> (24-len);
  482. print_bin(bits2, len);
  483. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  484. bits2, len, r, pos, file, func, line);
  485. return r;
  486. }
  487. static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
  488. const char *func, int line){
  489. int show = show_bits(s, n);
  490. int r = get_xbits(s, n);
  491. print_bin(show, n);
  492. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  493. show, n, r, get_bits_count(s)-n, file, func, line);
  494. return r;
  495. }
  496. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  497. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  498. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  499. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  500. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  501. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  502. #else //TRACE
  503. #define tprintf(p, ...) {}
  504. #endif
  505. #endif /* AVCODEC_GET_BITS_H */