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.

636 lines
20KB

  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 "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/log.h"
  30. #include "mathops.h"
  31. /*
  32. * Safe bitstream reading:
  33. * optionally, the get_bits API can check to ensure that we
  34. * don't read past input buffer boundaries. This is protected
  35. * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
  36. * then below that with UNCHECKED_BITSTREAM_READER at the per-
  37. * decoder level. This means that decoders that check internally
  38. * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
  39. * overread checks.
  40. * Boundary checking causes a minor performance penalty so for
  41. * applications that won't want/need this, it can be disabled
  42. * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
  43. */
  44. #ifndef UNCHECKED_BITSTREAM_READER
  45. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  46. #endif
  47. typedef struct GetBitContext {
  48. const uint8_t *buffer, *buffer_end;
  49. int index;
  50. int size_in_bits;
  51. #if !UNCHECKED_BITSTREAM_READER
  52. int size_in_bits_plus8;
  53. #endif
  54. } GetBitContext;
  55. #define VLC_TYPE int16_t
  56. typedef struct VLC {
  57. int bits;
  58. VLC_TYPE (*table)[2]; ///< code, bits
  59. int table_size, table_allocated;
  60. } VLC;
  61. typedef struct RL_VLC_ELEM {
  62. int16_t level;
  63. int8_t len;
  64. uint8_t run;
  65. } RL_VLC_ELEM;
  66. /* Bitstream reader API docs:
  67. * name
  68. * arbitrary name which is used as prefix for the internal variables
  69. *
  70. * gb
  71. * getbitcontext
  72. *
  73. * OPEN_READER(name, gb)
  74. * load gb into local variables
  75. *
  76. * CLOSE_READER(name, gb)
  77. * store local vars in gb
  78. *
  79. * UPDATE_CACHE(name, gb)
  80. * Refill the internal cache from the bitstream.
  81. * After this call at least MIN_CACHE_BITS will be available.
  82. *
  83. * GET_CACHE(name, gb)
  84. * Will output the contents of the internal cache,
  85. * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
  86. *
  87. * SHOW_UBITS(name, gb, num)
  88. * Will return the next num bits.
  89. *
  90. * SHOW_SBITS(name, gb, num)
  91. * Will return the next num bits and do sign extension.
  92. *
  93. * SKIP_BITS(name, gb, num)
  94. * Will skip over the next num bits.
  95. * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
  96. *
  97. * SKIP_CACHE(name, gb, num)
  98. * Will remove the next num bits from the cache (note SKIP_COUNTER
  99. * MUST be called before UPDATE_CACHE / CLOSE_READER).
  100. *
  101. * SKIP_COUNTER(name, gb, num)
  102. * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
  103. *
  104. * LAST_SKIP_BITS(name, gb, num)
  105. * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
  106. *
  107. * For examples see get_bits, show_bits, skip_bits, get_vlc.
  108. */
  109. #ifdef LONG_BITSTREAM_READER
  110. # define MIN_CACHE_BITS 32
  111. #else
  112. # define MIN_CACHE_BITS 25
  113. #endif
  114. #define OPEN_READER_NOSIZE(name, gb) \
  115. unsigned int name ## _index = (gb)->index; \
  116. unsigned int av_unused name ## _cache = 0
  117. #if UNCHECKED_BITSTREAM_READER
  118. #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
  119. #define BITS_AVAILABLE(name, gb) 1
  120. #else
  121. #define OPEN_READER(name, gb) \
  122. OPEN_READER_NOSIZE(name, gb); \
  123. unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
  124. #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
  125. #endif
  126. #define CLOSE_READER(name, gb) (gb)->index = name ## _index
  127. #ifdef BITSTREAM_READER_LE
  128. # ifdef LONG_BITSTREAM_READER
  129. # define UPDATE_CACHE(name, gb) name ## _cache = \
  130. AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  131. # else
  132. # define UPDATE_CACHE(name, gb) name ## _cache = \
  133. AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  134. # endif
  135. # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
  136. #else
  137. # ifdef LONG_BITSTREAM_READER
  138. # define UPDATE_CACHE(name, gb) name ## _cache = \
  139. AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
  140. # else
  141. # define UPDATE_CACHE(name, gb) name ## _cache = \
  142. AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
  143. # endif
  144. # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
  145. #endif
  146. #if UNCHECKED_BITSTREAM_READER
  147. # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
  148. #else
  149. # define SKIP_COUNTER(name, gb, num) \
  150. name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
  151. #endif
  152. #define SKIP_BITS(name, gb, num) \
  153. do { \
  154. SKIP_CACHE(name, gb, num); \
  155. SKIP_COUNTER(name, gb, num); \
  156. } while (0)
  157. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  158. #ifdef BITSTREAM_READER_LE
  159. # define SHOW_UBITS(name, gb, num) zero_extend(name ## _cache, num)
  160. # define SHOW_SBITS(name, gb, num) sign_extend(name ## _cache, num)
  161. #else
  162. # define SHOW_UBITS(name, gb, num) NEG_USR32(name ## _cache, num)
  163. # define SHOW_SBITS(name, gb, num) NEG_SSR32(name ## _cache, num)
  164. #endif
  165. #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
  166. static inline int get_bits_count(const GetBitContext *s)
  167. {
  168. return s->index;
  169. }
  170. static inline void skip_bits_long(GetBitContext *s, int n)
  171. {
  172. #if UNCHECKED_BITSTREAM_READER
  173. s->index += n;
  174. #else
  175. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  176. #endif
  177. }
  178. /**
  179. * Read MPEG-1 dc-style VLC (sign bit + mantisse with no MSB).
  180. * if MSB not set it is negative
  181. * @param n length in bits
  182. */
  183. static inline int get_xbits(GetBitContext *s, int n)
  184. {
  185. register int sign;
  186. register int32_t cache;
  187. OPEN_READER(re, s);
  188. UPDATE_CACHE(re, s);
  189. cache = GET_CACHE(re, s);
  190. sign = ~cache >> 31;
  191. LAST_SKIP_BITS(re, s, n);
  192. CLOSE_READER(re, s);
  193. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  194. }
  195. static inline int get_sbits(GetBitContext *s, int n)
  196. {
  197. register int tmp;
  198. OPEN_READER(re, s);
  199. UPDATE_CACHE(re, s);
  200. tmp = SHOW_SBITS(re, s, n);
  201. LAST_SKIP_BITS(re, s, n);
  202. CLOSE_READER(re, s);
  203. return tmp;
  204. }
  205. /**
  206. * Read 1-25 bits.
  207. */
  208. static inline unsigned int get_bits(GetBitContext *s, int n)
  209. {
  210. register int tmp;
  211. OPEN_READER(re, s);
  212. UPDATE_CACHE(re, s);
  213. tmp = SHOW_UBITS(re, s, n);
  214. LAST_SKIP_BITS(re, s, n);
  215. CLOSE_READER(re, s);
  216. return tmp;
  217. }
  218. /**
  219. * Read 0-25 bits.
  220. */
  221. static av_always_inline int get_bitsz(GetBitContext *s, int n)
  222. {
  223. return n ? get_bits(s, n) : 0;
  224. }
  225. /**
  226. * Show 1-25 bits.
  227. */
  228. static inline unsigned int show_bits(GetBitContext *s, int n)
  229. {
  230. register int tmp;
  231. OPEN_READER_NOSIZE(re, s);
  232. UPDATE_CACHE(re, s);
  233. tmp = SHOW_UBITS(re, s, n);
  234. return tmp;
  235. }
  236. static inline void skip_bits(GetBitContext *s, int n)
  237. {
  238. OPEN_READER(re, s);
  239. UPDATE_CACHE(re, s);
  240. LAST_SKIP_BITS(re, s, n);
  241. CLOSE_READER(re, s);
  242. }
  243. static inline unsigned int get_bits1(GetBitContext *s)
  244. {
  245. unsigned int index = s->index;
  246. uint8_t result = s->buffer[index >> 3];
  247. #ifdef BITSTREAM_READER_LE
  248. result >>= index & 7;
  249. result &= 1;
  250. #else
  251. result <<= index & 7;
  252. result >>= 8 - 1;
  253. #endif
  254. #if !UNCHECKED_BITSTREAM_READER
  255. if (s->index < s->size_in_bits_plus8)
  256. #endif
  257. index++;
  258. s->index = index;
  259. return result;
  260. }
  261. static inline unsigned int show_bits1(GetBitContext *s)
  262. {
  263. return show_bits(s, 1);
  264. }
  265. static inline void skip_bits1(GetBitContext *s)
  266. {
  267. skip_bits(s, 1);
  268. }
  269. /**
  270. * Read 0-32 bits.
  271. */
  272. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  273. {
  274. if (n <= MIN_CACHE_BITS) {
  275. return get_bits(s, n);
  276. } else {
  277. #ifdef BITSTREAM_READER_LE
  278. int ret = get_bits(s, 16);
  279. return ret | (get_bits(s, n - 16) << 16);
  280. #else
  281. int ret = get_bits(s, 16) << (n - 16);
  282. return ret | get_bits(s, n - 16);
  283. #endif
  284. }
  285. }
  286. /*
  287. * Read 0-64 bits.
  288. */
  289. static inline uint64_t get_bits64(GetBitContext *s, int n)
  290. {
  291. if (n <= 32) {
  292. return get_bits_long(s, n);
  293. } else {
  294. #ifdef BITSTREAM_READER_LE
  295. uint64_t ret = get_bits_long(s, 32);
  296. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  297. #else
  298. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  299. return ret | get_bits_long(s, 32);
  300. #endif
  301. }
  302. }
  303. /**
  304. * Read 0-32 bits as a signed integer.
  305. */
  306. static inline int get_sbits_long(GetBitContext *s, int n)
  307. {
  308. return sign_extend(get_bits_long(s, n), n);
  309. }
  310. /**
  311. * Show 0-32 bits.
  312. */
  313. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  314. {
  315. if (n <= MIN_CACHE_BITS) {
  316. return show_bits(s, n);
  317. } else {
  318. GetBitContext gb = *s;
  319. return get_bits_long(&gb, n);
  320. }
  321. }
  322. /**
  323. * Initialize GetBitContext.
  324. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  325. * larger than the actual read bits because some optimized bitstream
  326. * readers read 32 or 64 bit at once and could read over the end
  327. * @param bit_size the size of the buffer in bits
  328. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  329. */
  330. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  331. int bit_size)
  332. {
  333. int buffer_size;
  334. int ret = 0;
  335. if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {
  336. bit_size = 0;
  337. buffer = NULL;
  338. ret = AVERROR_INVALIDDATA;
  339. }
  340. buffer_size = (bit_size + 7) >> 3;
  341. s->buffer = buffer;
  342. s->size_in_bits = bit_size;
  343. #if !UNCHECKED_BITSTREAM_READER
  344. s->size_in_bits_plus8 = bit_size + 8;
  345. #endif
  346. s->buffer_end = buffer + buffer_size;
  347. s->index = 0;
  348. return ret;
  349. }
  350. /**
  351. * Initialize GetBitContext.
  352. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  353. * larger than the actual read bits because some optimized bitstream
  354. * readers read 32 or 64 bit at once and could read over the end
  355. * @param byte_size the size of the buffer in bytes
  356. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  357. */
  358. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  359. int byte_size)
  360. {
  361. if (byte_size > INT_MAX / 8)
  362. return AVERROR_INVALIDDATA;
  363. return init_get_bits(s, buffer, byte_size * 8);
  364. }
  365. static inline const uint8_t *align_get_bits(GetBitContext *s)
  366. {
  367. int n = -get_bits_count(s) & 7;
  368. if (n)
  369. skip_bits(s, n);
  370. return s->buffer + (s->index >> 3);
  371. }
  372. #define init_vlc(vlc, nb_bits, nb_codes, \
  373. bits, bits_wrap, bits_size, \
  374. codes, codes_wrap, codes_size, \
  375. flags) \
  376. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  377. bits, bits_wrap, bits_size, \
  378. codes, codes_wrap, codes_size, \
  379. NULL, 0, 0, flags)
  380. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  381. const void *bits, int bits_wrap, int bits_size,
  382. const void *codes, int codes_wrap, int codes_size,
  383. const void *symbols, int symbols_wrap, int symbols_size,
  384. int flags);
  385. void ff_free_vlc(VLC *vlc);
  386. #define INIT_VLC_LE 2
  387. #define INIT_VLC_USE_NEW_STATIC 4
  388. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  389. do { \
  390. static VLC_TYPE table[static_size][2]; \
  391. (vlc)->table = table; \
  392. (vlc)->table_allocated = static_size; \
  393. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  394. } while (0)
  395. /**
  396. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  397. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  398. * is undefined.
  399. */
  400. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  401. do { \
  402. int n, nb_bits; \
  403. unsigned int index; \
  404. \
  405. index = SHOW_UBITS(name, gb, bits); \
  406. code = table[index][0]; \
  407. n = table[index][1]; \
  408. \
  409. if (max_depth > 1 && n < 0) { \
  410. LAST_SKIP_BITS(name, gb, bits); \
  411. UPDATE_CACHE(name, gb); \
  412. \
  413. nb_bits = -n; \
  414. \
  415. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  416. code = table[index][0]; \
  417. n = table[index][1]; \
  418. if (max_depth > 2 && n < 0) { \
  419. LAST_SKIP_BITS(name, gb, nb_bits); \
  420. UPDATE_CACHE(name, gb); \
  421. \
  422. nb_bits = -n; \
  423. \
  424. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  425. code = table[index][0]; \
  426. n = table[index][1]; \
  427. } \
  428. } \
  429. SKIP_BITS(name, gb, n); \
  430. } while (0)
  431. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  432. max_depth, need_update) \
  433. do { \
  434. int n, nb_bits; \
  435. unsigned int index; \
  436. \
  437. index = SHOW_UBITS(name, gb, bits); \
  438. level = table[index].level; \
  439. n = table[index].len; \
  440. \
  441. if (max_depth > 1 && n < 0) { \
  442. SKIP_BITS(name, gb, bits); \
  443. if (need_update) { \
  444. UPDATE_CACHE(name, gb); \
  445. } \
  446. \
  447. nb_bits = -n; \
  448. \
  449. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  450. level = table[index].level; \
  451. n = table[index].len; \
  452. if (max_depth > 2 && n < 0) { \
  453. LAST_SKIP_BITS(name, gb, nb_bits); \
  454. if (need_update) { \
  455. UPDATE_CACHE(name, gb); \
  456. } \
  457. nb_bits = -n; \
  458. \
  459. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  460. level = table[index].level; \
  461. n = table[index].len; \
  462. } \
  463. } \
  464. run = table[index].run; \
  465. SKIP_BITS(name, gb, n); \
  466. } while (0)
  467. /**
  468. * Parse a vlc code.
  469. * @param bits is the number of bits which will be read at once, must be
  470. * identical to nb_bits in init_vlc()
  471. * @param max_depth is the number of times bits bits must be read to completely
  472. * read the longest vlc code
  473. * = (max_vlc_length + bits - 1) / bits
  474. */
  475. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  476. int bits, int max_depth)
  477. {
  478. int code;
  479. OPEN_READER(re, s);
  480. UPDATE_CACHE(re, s);
  481. GET_VLC(code, re, s, table, bits, max_depth);
  482. CLOSE_READER(re, s);
  483. return code;
  484. }
  485. static inline int decode012(GetBitContext *gb)
  486. {
  487. int n;
  488. n = get_bits1(gb);
  489. if (n == 0)
  490. return 0;
  491. else
  492. return get_bits1(gb) + 1;
  493. }
  494. static inline int decode210(GetBitContext *gb)
  495. {
  496. if (get_bits1(gb))
  497. return 0;
  498. else
  499. return 2 - get_bits1(gb);
  500. }
  501. static inline int get_bits_left(GetBitContext *gb)
  502. {
  503. return gb->size_in_bits - get_bits_count(gb);
  504. }
  505. //#define TRACE
  506. #ifdef TRACE
  507. static inline void print_bin(int bits, int n)
  508. {
  509. int i;
  510. for (i = n - 1; i >= 0; i--)
  511. av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
  512. for (i = n; i < 24; i++)
  513. av_log(NULL, AV_LOG_DEBUG, " ");
  514. }
  515. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  516. const char *func, int line)
  517. {
  518. int r = get_bits(s, n);
  519. print_bin(r, n);
  520. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  521. r, n, r, get_bits_count(s) - n, file, func, line);
  522. return r;
  523. }
  524. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  525. int bits, int max_depth, const char *file,
  526. const char *func, int line)
  527. {
  528. int show = show_bits(s, 24);
  529. int pos = get_bits_count(s);
  530. int r = get_vlc2(s, table, bits, max_depth);
  531. int len = get_bits_count(s) - pos;
  532. int bits2 = show >> (24 - len);
  533. print_bin(bits2, len);
  534. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  535. bits2, len, r, pos, file, func, line);
  536. return r;
  537. }
  538. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  539. const char *func, int line)
  540. {
  541. int show = show_bits(s, n);
  542. int r = get_xbits(s, n);
  543. print_bin(show, n);
  544. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  545. show, n, r, get_bits_count(s) - n, file, func, line);
  546. return r;
  547. }
  548. #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  549. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  550. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  551. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  552. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  553. #endif
  554. #endif /* AVCODEC_GET_BITS_H */