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.

630 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 "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 bit (FIXME 64bit).
  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. #if UNCHECKED_BITSTREAM_READER
  115. #define OPEN_READER(name, gb) \
  116. unsigned int name ## _index = (gb)->index; \
  117. unsigned int av_unused name ## _cache = 0
  118. #define HAVE_BITS_REMAINING(name, gb) 1
  119. #else
  120. #define OPEN_READER(name, gb) \
  121. unsigned int name ## _index = (gb)->index; \
  122. unsigned int av_unused name ## _cache = 0; \
  123. unsigned int av_unused name ## _size_plus8 = (gb)->size_in_bits_plus8
  124. #define HAVE_BITS_REMAINING(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 mpeg1 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. * Show 1-25 bits.
  220. */
  221. static inline unsigned int show_bits(GetBitContext *s, int n)
  222. {
  223. register int tmp;
  224. OPEN_READER(re, s);
  225. UPDATE_CACHE(re, s);
  226. tmp = SHOW_UBITS(re, s, n);
  227. return tmp;
  228. }
  229. static inline void skip_bits(GetBitContext *s, int n)
  230. {
  231. OPEN_READER(re, s);
  232. UPDATE_CACHE(re, s);
  233. LAST_SKIP_BITS(re, s, n);
  234. CLOSE_READER(re, s);
  235. }
  236. static inline unsigned int get_bits1(GetBitContext *s)
  237. {
  238. unsigned int index = s->index;
  239. uint8_t result = s->buffer[index >> 3];
  240. #ifdef BITSTREAM_READER_LE
  241. result >>= index & 7;
  242. result &= 1;
  243. #else
  244. result <<= index & 7;
  245. result >>= 8 - 1;
  246. #endif
  247. #if !UNCHECKED_BITSTREAM_READER
  248. if (s->index < s->size_in_bits_plus8)
  249. #endif
  250. index++;
  251. s->index = index;
  252. return result;
  253. }
  254. static inline unsigned int show_bits1(GetBitContext *s)
  255. {
  256. return show_bits(s, 1);
  257. }
  258. static inline void skip_bits1(GetBitContext *s)
  259. {
  260. skip_bits(s, 1);
  261. }
  262. /**
  263. * Read 0-32 bits.
  264. */
  265. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  266. {
  267. if (n <= MIN_CACHE_BITS) {
  268. return get_bits(s, n);
  269. } else {
  270. #ifdef BITSTREAM_READER_LE
  271. int ret = get_bits(s, 16);
  272. return ret | (get_bits(s, n - 16) << 16);
  273. #else
  274. int ret = get_bits(s, 16) << (n - 16);
  275. return ret | get_bits(s, n - 16);
  276. #endif
  277. }
  278. }
  279. /*
  280. * Read 0-64 bits.
  281. */
  282. static inline uint64_t get_bits64(GetBitContext *s, int n)
  283. {
  284. if (n <= 32) {
  285. return get_bits_long(s, n);
  286. } else {
  287. #ifdef BITSTREAM_READER_LE
  288. uint64_t ret = get_bits_long(s, 32);
  289. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  290. #else
  291. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  292. return ret | get_bits_long(s, 32);
  293. #endif
  294. }
  295. }
  296. /**
  297. * Read 0-32 bits as a signed integer.
  298. */
  299. static inline int get_sbits_long(GetBitContext *s, int n)
  300. {
  301. return sign_extend(get_bits_long(s, n), n);
  302. }
  303. /**
  304. * Show 0-32 bits.
  305. */
  306. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  307. {
  308. if (n <= MIN_CACHE_BITS) {
  309. return show_bits(s, n);
  310. } else {
  311. GetBitContext gb = *s;
  312. return get_bits_long(&gb, n);
  313. }
  314. }
  315. static inline int check_marker(GetBitContext *s, const char *msg)
  316. {
  317. int bit = get_bits1(s);
  318. if (!bit)
  319. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  320. return bit;
  321. }
  322. /**
  323. * Initialize GetBitContext.
  324. * @param buffer bitstream buffer, must be FF_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. buffer_size = 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 FF_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. } \
  453. run = table[index].run; \
  454. SKIP_BITS(name, gb, n); \
  455. } while (0)
  456. /**
  457. * Parse a vlc code.
  458. * @param bits is the number of bits which will be read at once, must be
  459. * identical to nb_bits in init_vlc()
  460. * @param max_depth is the number of times bits bits must be read to completely
  461. * read the longest vlc code
  462. * = (max_vlc_length + bits - 1) / bits
  463. */
  464. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  465. int bits, int max_depth)
  466. {
  467. int code;
  468. OPEN_READER(re, s);
  469. UPDATE_CACHE(re, s);
  470. GET_VLC(code, re, s, table, bits, max_depth);
  471. CLOSE_READER(re, s);
  472. return code;
  473. }
  474. static inline int decode012(GetBitContext *gb)
  475. {
  476. int n;
  477. n = get_bits1(gb);
  478. if (n == 0)
  479. return 0;
  480. else
  481. return get_bits1(gb) + 1;
  482. }
  483. static inline int decode210(GetBitContext *gb)
  484. {
  485. if (get_bits1(gb))
  486. return 0;
  487. else
  488. return 2 - get_bits1(gb);
  489. }
  490. static inline int get_bits_left(GetBitContext *gb)
  491. {
  492. return gb->size_in_bits - get_bits_count(gb);
  493. }
  494. //#define TRACE
  495. #ifdef TRACE
  496. static inline void print_bin(int bits, int n)
  497. {
  498. int i;
  499. for (i = n - 1; i >= 0; i--)
  500. av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
  501. for (i = n; i < 24; i++)
  502. av_log(NULL, AV_LOG_DEBUG, " ");
  503. }
  504. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  505. const char *func, int line)
  506. {
  507. int r = get_bits(s, n);
  508. print_bin(r, n);
  509. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  510. r, n, r, get_bits_count(s) - n, file, func, line);
  511. return r;
  512. }
  513. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  514. int bits, int max_depth, const char *file,
  515. const char *func, int line)
  516. {
  517. int show = show_bits(s, 24);
  518. int pos = get_bits_count(s);
  519. int r = get_vlc2(s, table, bits, max_depth);
  520. int len = get_bits_count(s) - pos;
  521. int bits2 = show >> (24 - len);
  522. print_bin(bits2, len);
  523. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  524. bits2, len, r, pos, file, func, line);
  525. return r;
  526. }
  527. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  528. const char *func, int line)
  529. {
  530. int show = show_bits(s, n);
  531. int r = get_xbits(s, n);
  532. print_bin(show, n);
  533. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  534. show, n, r, get_bits_count(s) - n, file, func, line);
  535. return r;
  536. }
  537. #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  538. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  539. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  540. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  541. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  542. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  543. #else //TRACE
  544. #define tprintf(p, ...) { }
  545. #endif
  546. #endif /* AVCODEC_GET_BITS_H */