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.

632 lines
19KB

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