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.

658 lines
20KB

  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 LONG_BITSTREAM_READER
  127. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  128. AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  129. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  130. AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
  131. #else
  132. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  133. AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  134. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  135. AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
  136. #endif
  137. #ifdef BITSTREAM_READER_LE
  138. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
  139. # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
  140. #else
  141. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
  142. # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
  143. #endif
  144. #if UNCHECKED_BITSTREAM_READER
  145. # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
  146. #else
  147. # define SKIP_COUNTER(name, gb, num) \
  148. name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
  149. #endif
  150. #define SKIP_BITS(name, gb, num) \
  151. do { \
  152. SKIP_CACHE(name, gb, num); \
  153. SKIP_COUNTER(name, gb, num); \
  154. } while (0)
  155. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  156. #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
  157. #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
  158. #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
  159. #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
  160. #ifdef BITSTREAM_READER_LE
  161. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
  162. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
  163. #else
  164. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
  165. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
  166. #endif
  167. #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
  168. static inline int get_bits_count(const GetBitContext *s)
  169. {
  170. return s->index;
  171. }
  172. static inline void skip_bits_long(GetBitContext *s, int n)
  173. {
  174. #if UNCHECKED_BITSTREAM_READER
  175. s->index += n;
  176. #else
  177. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  178. #endif
  179. }
  180. /**
  181. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  182. * if MSB not set it is negative
  183. * @param n length in bits
  184. */
  185. static inline int get_xbits(GetBitContext *s, int n)
  186. {
  187. register int sign;
  188. register int32_t cache;
  189. OPEN_READER(re, s);
  190. av_assert2(n>0 && n<=25);
  191. UPDATE_CACHE(re, s);
  192. cache = GET_CACHE(re, s);
  193. sign = ~cache >> 31;
  194. LAST_SKIP_BITS(re, s, n);
  195. CLOSE_READER(re, s);
  196. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  197. }
  198. static inline int get_sbits(GetBitContext *s, int n)
  199. {
  200. register int tmp;
  201. OPEN_READER(re, s);
  202. av_assert2(n>0 && n<=25);
  203. UPDATE_CACHE(re, s);
  204. tmp = SHOW_SBITS(re, s, n);
  205. LAST_SKIP_BITS(re, s, n);
  206. CLOSE_READER(re, s);
  207. return tmp;
  208. }
  209. /**
  210. * Read 1-25 bits.
  211. */
  212. static inline unsigned int get_bits(GetBitContext *s, int n)
  213. {
  214. register int tmp;
  215. OPEN_READER(re, s);
  216. av_assert2(n>0 && n<=25);
  217. UPDATE_CACHE(re, s);
  218. tmp = SHOW_UBITS(re, s, n);
  219. LAST_SKIP_BITS(re, s, n);
  220. CLOSE_READER(re, s);
  221. return tmp;
  222. }
  223. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  224. {
  225. register int tmp;
  226. OPEN_READER(re, s);
  227. av_assert2(n>0 && n<=25);
  228. UPDATE_CACHE_LE(re, s);
  229. tmp = SHOW_UBITS_LE(re, s, n);
  230. LAST_SKIP_BITS(re, s, n);
  231. CLOSE_READER(re, s);
  232. return tmp;
  233. }
  234. /**
  235. * Show 1-25 bits.
  236. */
  237. static inline unsigned int show_bits(GetBitContext *s, int n)
  238. {
  239. register int tmp;
  240. OPEN_READER(re, s);
  241. av_assert2(n>0 && n<=25);
  242. UPDATE_CACHE(re, s);
  243. tmp = SHOW_UBITS(re, s, n);
  244. return tmp;
  245. }
  246. static inline void skip_bits(GetBitContext *s, int n)
  247. {
  248. OPEN_READER(re, s);
  249. UPDATE_CACHE(re, s);
  250. LAST_SKIP_BITS(re, s, n);
  251. CLOSE_READER(re, s);
  252. }
  253. static inline unsigned int get_bits1(GetBitContext *s)
  254. {
  255. unsigned int index = s->index;
  256. uint8_t result = s->buffer[index >> 3];
  257. #ifdef BITSTREAM_READER_LE
  258. result >>= index & 7;
  259. result &= 1;
  260. #else
  261. result <<= index & 7;
  262. result >>= 8 - 1;
  263. #endif
  264. #if !UNCHECKED_BITSTREAM_READER
  265. if (s->index < s->size_in_bits_plus8)
  266. #endif
  267. index++;
  268. s->index = index;
  269. return result;
  270. }
  271. static inline unsigned int show_bits1(GetBitContext *s)
  272. {
  273. return show_bits(s, 1);
  274. }
  275. static inline void skip_bits1(GetBitContext *s)
  276. {
  277. skip_bits(s, 1);
  278. }
  279. /**
  280. * Read 0-32 bits.
  281. */
  282. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  283. {
  284. if (!n) {
  285. return 0;
  286. } else if (n <= MIN_CACHE_BITS) {
  287. return get_bits(s, n);
  288. } else {
  289. #ifdef BITSTREAM_READER_LE
  290. unsigned ret = get_bits(s, 16);
  291. return ret | (get_bits(s, n - 16) << 16);
  292. #else
  293. unsigned ret = get_bits(s, 16) << (n - 16);
  294. return ret | get_bits(s, n - 16);
  295. #endif
  296. }
  297. }
  298. /**
  299. * Read 0-64 bits.
  300. */
  301. static inline uint64_t get_bits64(GetBitContext *s, int n)
  302. {
  303. if (n <= 32) {
  304. return get_bits_long(s, n);
  305. } else {
  306. #ifdef BITSTREAM_READER_LE
  307. uint64_t ret = get_bits_long(s, 32);
  308. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  309. #else
  310. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  311. return ret | get_bits_long(s, 32);
  312. #endif
  313. }
  314. }
  315. /**
  316. * Read 0-32 bits as a signed integer.
  317. */
  318. static inline int get_sbits_long(GetBitContext *s, int n)
  319. {
  320. return sign_extend(get_bits_long(s, n), n);
  321. }
  322. /**
  323. * Show 0-32 bits.
  324. */
  325. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  326. {
  327. if (n <= MIN_CACHE_BITS) {
  328. return show_bits(s, n);
  329. } else {
  330. GetBitContext gb = *s;
  331. return get_bits_long(&gb, n);
  332. }
  333. }
  334. static inline int check_marker(GetBitContext *s, const char *msg)
  335. {
  336. int bit = get_bits1(s);
  337. if (!bit)
  338. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  339. return bit;
  340. }
  341. /**
  342. * Initialize GetBitContext.
  343. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
  344. * larger than the actual read bits because some optimized bitstream
  345. * readers read 32 or 64 bit at once and could read over the end
  346. * @param bit_size the size of the buffer in bits
  347. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  348. */
  349. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  350. int bit_size)
  351. {
  352. int buffer_size;
  353. int ret = 0;
  354. if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
  355. buffer_size = bit_size = 0;
  356. buffer = NULL;
  357. ret = AVERROR_INVALIDDATA;
  358. }
  359. buffer_size = (bit_size + 7) >> 3;
  360. s->buffer = buffer;
  361. s->size_in_bits = bit_size;
  362. s->size_in_bits_plus8 = bit_size + 8;
  363. s->buffer_end = buffer + buffer_size;
  364. s->index = 0;
  365. return ret;
  366. }
  367. /**
  368. * Initialize GetBitContext.
  369. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
  370. * larger than the actual read bits because some optimized bitstream
  371. * readers read 32 or 64 bit at once and could read over the end
  372. * @param byte_size the size of the buffer in bytes
  373. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  374. */
  375. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  376. int byte_size)
  377. {
  378. if (byte_size > INT_MAX / 8 || byte_size < 0)
  379. byte_size = -1;
  380. return init_get_bits(s, buffer, byte_size * 8);
  381. }
  382. static inline const uint8_t *align_get_bits(GetBitContext *s)
  383. {
  384. int n = -get_bits_count(s) & 7;
  385. if (n)
  386. skip_bits(s, n);
  387. return s->buffer + (s->index >> 3);
  388. }
  389. #define init_vlc(vlc, nb_bits, nb_codes, \
  390. bits, bits_wrap, bits_size, \
  391. codes, codes_wrap, codes_size, \
  392. flags) \
  393. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  394. bits, bits_wrap, bits_size, \
  395. codes, codes_wrap, codes_size, \
  396. NULL, 0, 0, flags)
  397. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  398. const void *bits, int bits_wrap, int bits_size,
  399. const void *codes, int codes_wrap, int codes_size,
  400. const void *symbols, int symbols_wrap, int symbols_size,
  401. int flags);
  402. void ff_free_vlc(VLC *vlc);
  403. #define INIT_VLC_LE 2
  404. #define INIT_VLC_USE_NEW_STATIC 4
  405. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  406. do { \
  407. static VLC_TYPE table[static_size][2]; \
  408. (vlc)->table = table; \
  409. (vlc)->table_allocated = static_size; \
  410. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  411. } while (0)
  412. /**
  413. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  414. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  415. * is undefined.
  416. */
  417. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  418. do { \
  419. int n, nb_bits; \
  420. unsigned int index; \
  421. \
  422. index = SHOW_UBITS(name, gb, bits); \
  423. code = table[index][0]; \
  424. n = table[index][1]; \
  425. \
  426. if (max_depth > 1 && n < 0) { \
  427. LAST_SKIP_BITS(name, gb, bits); \
  428. UPDATE_CACHE(name, gb); \
  429. \
  430. nb_bits = -n; \
  431. \
  432. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  433. code = table[index][0]; \
  434. n = table[index][1]; \
  435. if (max_depth > 2 && n < 0) { \
  436. LAST_SKIP_BITS(name, gb, nb_bits); \
  437. UPDATE_CACHE(name, gb); \
  438. \
  439. nb_bits = -n; \
  440. \
  441. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  442. code = table[index][0]; \
  443. n = table[index][1]; \
  444. } \
  445. } \
  446. SKIP_BITS(name, gb, n); \
  447. } while (0)
  448. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  449. max_depth, need_update) \
  450. do { \
  451. int n, nb_bits; \
  452. unsigned int index; \
  453. \
  454. index = SHOW_UBITS(name, gb, bits); \
  455. level = table[index].level; \
  456. n = table[index].len; \
  457. \
  458. if (max_depth > 1 && n < 0) { \
  459. SKIP_BITS(name, gb, bits); \
  460. if (need_update) { \
  461. UPDATE_CACHE(name, gb); \
  462. } \
  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. run = table[index].run; \
  471. SKIP_BITS(name, gb, n); \
  472. } while (0)
  473. /**
  474. * Parse a vlc code.
  475. * @param bits is the number of bits which will be read at once, must be
  476. * identical to nb_bits in init_vlc()
  477. * @param max_depth is the number of times bits bits must be read to completely
  478. * read the longest vlc code
  479. * = (max_vlc_length + bits - 1) / bits
  480. */
  481. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  482. int bits, int max_depth)
  483. {
  484. int code;
  485. OPEN_READER(re, s);
  486. UPDATE_CACHE(re, s);
  487. GET_VLC(code, re, s, table, bits, max_depth);
  488. CLOSE_READER(re, s);
  489. return code;
  490. }
  491. static inline int decode012(GetBitContext *gb)
  492. {
  493. int n;
  494. n = get_bits1(gb);
  495. if (n == 0)
  496. return 0;
  497. else
  498. return get_bits1(gb) + 1;
  499. }
  500. static inline int decode210(GetBitContext *gb)
  501. {
  502. if (get_bits1(gb))
  503. return 0;
  504. else
  505. return 2 - get_bits1(gb);
  506. }
  507. static inline int get_bits_left(GetBitContext *gb)
  508. {
  509. return gb->size_in_bits - get_bits_count(gb);
  510. }
  511. //#define TRACE
  512. #ifdef TRACE
  513. static inline void print_bin(int bits, int n)
  514. {
  515. int i;
  516. for (i = n - 1; i >= 0; i--)
  517. av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
  518. for (i = n; i < 24; i++)
  519. av_log(NULL, AV_LOG_DEBUG, " ");
  520. }
  521. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  522. const char *func, int line)
  523. {
  524. int r = get_bits(s, n);
  525. print_bin(r, n);
  526. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  527. r, n, r, get_bits_count(s) - n, file, func, line);
  528. return r;
  529. }
  530. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  531. int bits, int max_depth, const char *file,
  532. const char *func, int line)
  533. {
  534. int show = show_bits(s, 24);
  535. int pos = get_bits_count(s);
  536. int r = get_vlc2(s, table, bits, max_depth);
  537. int len = get_bits_count(s) - pos;
  538. int bits2 = show >> (24 - len);
  539. print_bin(bits2, len);
  540. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  541. bits2, len, r, pos, file, func, line);
  542. return r;
  543. }
  544. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  545. const char *func, int line)
  546. {
  547. int show = show_bits(s, n);
  548. int r = get_xbits(s, n);
  549. print_bin(show, n);
  550. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  551. show, n, r, get_bits_count(s) - n, file, func, line);
  552. return r;
  553. }
  554. #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  555. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  556. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  557. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  558. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  559. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  560. #else //TRACE
  561. #define tprintf(p, ...) { }
  562. #endif
  563. #endif /* AVCODEC_GET_BITS_H */