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.

645 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. static inline int check_marker(GetBitContext *s, const char *msg)
  323. {
  324. int bit = get_bits1(s);
  325. if (!bit)
  326. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  327. return bit;
  328. }
  329. /**
  330. * Initialize GetBitContext.
  331. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  332. * larger than the actual read bits because some optimized bitstream
  333. * readers read 32 or 64 bit at once and could read over the end
  334. * @param bit_size the size of the buffer in bits
  335. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  336. */
  337. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  338. int bit_size)
  339. {
  340. int buffer_size;
  341. int ret = 0;
  342. if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {
  343. bit_size = 0;
  344. buffer = NULL;
  345. ret = AVERROR_INVALIDDATA;
  346. }
  347. buffer_size = (bit_size + 7) >> 3;
  348. s->buffer = buffer;
  349. s->size_in_bits = bit_size;
  350. #if !UNCHECKED_BITSTREAM_READER
  351. s->size_in_bits_plus8 = bit_size + 8;
  352. #endif
  353. s->buffer_end = buffer + buffer_size;
  354. s->index = 0;
  355. return ret;
  356. }
  357. /**
  358. * Initialize GetBitContext.
  359. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  360. * larger than the actual read bits because some optimized bitstream
  361. * readers read 32 or 64 bit at once and could read over the end
  362. * @param byte_size the size of the buffer in bytes
  363. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  364. */
  365. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  366. int byte_size)
  367. {
  368. if (byte_size > INT_MAX / 8)
  369. return AVERROR_INVALIDDATA;
  370. return init_get_bits(s, buffer, byte_size * 8);
  371. }
  372. static inline const uint8_t *align_get_bits(GetBitContext *s)
  373. {
  374. int n = -get_bits_count(s) & 7;
  375. if (n)
  376. skip_bits(s, n);
  377. return s->buffer + (s->index >> 3);
  378. }
  379. #define init_vlc(vlc, nb_bits, nb_codes, \
  380. bits, bits_wrap, bits_size, \
  381. codes, codes_wrap, codes_size, \
  382. flags) \
  383. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  384. bits, bits_wrap, bits_size, \
  385. codes, codes_wrap, codes_size, \
  386. NULL, 0, 0, flags)
  387. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  388. const void *bits, int bits_wrap, int bits_size,
  389. const void *codes, int codes_wrap, int codes_size,
  390. const void *symbols, int symbols_wrap, int symbols_size,
  391. int flags);
  392. void ff_free_vlc(VLC *vlc);
  393. #define INIT_VLC_LE 2
  394. #define INIT_VLC_USE_NEW_STATIC 4
  395. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  396. do { \
  397. static VLC_TYPE table[static_size][2]; \
  398. (vlc)->table = table; \
  399. (vlc)->table_allocated = static_size; \
  400. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  401. } while (0)
  402. /**
  403. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  404. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  405. * is undefined.
  406. */
  407. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  408. do { \
  409. int n, nb_bits; \
  410. unsigned int index; \
  411. \
  412. index = SHOW_UBITS(name, gb, bits); \
  413. code = table[index][0]; \
  414. n = table[index][1]; \
  415. \
  416. if (max_depth > 1 && n < 0) { \
  417. LAST_SKIP_BITS(name, gb, bits); \
  418. UPDATE_CACHE(name, gb); \
  419. \
  420. nb_bits = -n; \
  421. \
  422. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  423. code = table[index][0]; \
  424. n = table[index][1]; \
  425. if (max_depth > 2 && n < 0) { \
  426. LAST_SKIP_BITS(name, gb, nb_bits); \
  427. UPDATE_CACHE(name, gb); \
  428. \
  429. nb_bits = -n; \
  430. \
  431. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  432. code = table[index][0]; \
  433. n = table[index][1]; \
  434. } \
  435. } \
  436. SKIP_BITS(name, gb, n); \
  437. } while (0)
  438. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  439. max_depth, need_update) \
  440. do { \
  441. int n, nb_bits; \
  442. unsigned int index; \
  443. \
  444. index = SHOW_UBITS(name, gb, bits); \
  445. level = table[index].level; \
  446. n = table[index].len; \
  447. \
  448. if (max_depth > 1 && n < 0) { \
  449. SKIP_BITS(name, gb, bits); \
  450. if (need_update) { \
  451. UPDATE_CACHE(name, gb); \
  452. } \
  453. \
  454. nb_bits = -n; \
  455. \
  456. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  457. level = table[index].level; \
  458. n = table[index].len; \
  459. if (max_depth > 2 && n < 0) { \
  460. LAST_SKIP_BITS(name, gb, nb_bits); \
  461. if (need_update) { \
  462. UPDATE_CACHE(name, gb); \
  463. } \
  464. nb_bits = -n; \
  465. \
  466. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  467. level = table[index].level; \
  468. n = table[index].len; \
  469. } \
  470. } \
  471. run = table[index].run; \
  472. SKIP_BITS(name, gb, n); \
  473. } while (0)
  474. /**
  475. * Parse a vlc code.
  476. * @param bits is the number of bits which will be read at once, must be
  477. * identical to nb_bits in init_vlc()
  478. * @param max_depth is the number of times bits bits must be read to completely
  479. * read the longest vlc code
  480. * = (max_vlc_length + bits - 1) / bits
  481. */
  482. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  483. int bits, int max_depth)
  484. {
  485. int code;
  486. OPEN_READER(re, s);
  487. UPDATE_CACHE(re, s);
  488. GET_VLC(code, re, s, table, bits, max_depth);
  489. CLOSE_READER(re, s);
  490. return code;
  491. }
  492. static inline int decode012(GetBitContext *gb)
  493. {
  494. int n;
  495. n = get_bits1(gb);
  496. if (n == 0)
  497. return 0;
  498. else
  499. return get_bits1(gb) + 1;
  500. }
  501. static inline int decode210(GetBitContext *gb)
  502. {
  503. if (get_bits1(gb))
  504. return 0;
  505. else
  506. return 2 - get_bits1(gb);
  507. }
  508. static inline int get_bits_left(GetBitContext *gb)
  509. {
  510. return gb->size_in_bits - get_bits_count(gb);
  511. }
  512. //#define TRACE
  513. #ifdef TRACE
  514. static inline void print_bin(int bits, int n)
  515. {
  516. int i;
  517. for (i = n - 1; i >= 0; i--)
  518. av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
  519. for (i = n; i < 24; i++)
  520. av_log(NULL, AV_LOG_DEBUG, " ");
  521. }
  522. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  523. const char *func, int line)
  524. {
  525. int r = get_bits(s, n);
  526. print_bin(r, n);
  527. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  528. r, n, r, get_bits_count(s) - n, file, func, line);
  529. return r;
  530. }
  531. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  532. int bits, int max_depth, const char *file,
  533. const char *func, int line)
  534. {
  535. int show = show_bits(s, 24);
  536. int pos = get_bits_count(s);
  537. int r = get_vlc2(s, table, bits, max_depth);
  538. int len = get_bits_count(s) - pos;
  539. int bits2 = show >> (24 - len);
  540. print_bin(bits2, len);
  541. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  542. bits2, len, r, pos, file, func, line);
  543. return r;
  544. }
  545. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  546. const char *func, int line)
  547. {
  548. int show = show_bits(s, n);
  549. int r = get_xbits(s, n);
  550. print_bin(show, n);
  551. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  552. show, n, r, get_bits_count(s) - n, file, func, line);
  553. return r;
  554. }
  555. #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  556. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  557. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  558. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  559. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  560. #endif
  561. #endif /* AVCODEC_GET_BITS_H */