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.

659 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. void * volatile init_state;
  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
  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 LONG_BITSTREAM_READER
  128. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  129. AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  130. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  131. AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
  132. #else
  133. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  134. AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  135. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  136. AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
  137. #endif
  138. #ifdef BITSTREAM_READER_LE
  139. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
  140. # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
  141. #else
  142. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
  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. #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
  158. #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
  159. #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
  160. #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
  161. #ifdef BITSTREAM_READER_LE
  162. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
  163. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
  164. #else
  165. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
  166. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
  167. #endif
  168. #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
  169. static inline int get_bits_count(const GetBitContext *s)
  170. {
  171. return s->index;
  172. }
  173. static inline void skip_bits_long(GetBitContext *s, int n)
  174. {
  175. #if UNCHECKED_BITSTREAM_READER
  176. s->index += n;
  177. #else
  178. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  179. #endif
  180. }
  181. /**
  182. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  183. * if MSB not set it is negative
  184. * @param n length in bits
  185. */
  186. static inline int get_xbits(GetBitContext *s, int n)
  187. {
  188. register int sign;
  189. register int32_t cache;
  190. OPEN_READER(re, s);
  191. av_assert2(n>0 && n<=25);
  192. UPDATE_CACHE(re, s);
  193. cache = GET_CACHE(re, s);
  194. sign = ~cache >> 31;
  195. LAST_SKIP_BITS(re, s, n);
  196. CLOSE_READER(re, s);
  197. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  198. }
  199. static inline int get_sbits(GetBitContext *s, int n)
  200. {
  201. register int tmp;
  202. OPEN_READER(re, s);
  203. av_assert2(n>0 && n<=25);
  204. UPDATE_CACHE(re, s);
  205. tmp = SHOW_SBITS(re, s, n);
  206. LAST_SKIP_BITS(re, s, n);
  207. CLOSE_READER(re, s);
  208. return tmp;
  209. }
  210. /**
  211. * Read 1-25 bits.
  212. */
  213. static inline unsigned int get_bits(GetBitContext *s, int n)
  214. {
  215. register int tmp;
  216. OPEN_READER(re, s);
  217. av_assert2(n>0 && n<=25);
  218. UPDATE_CACHE(re, s);
  219. tmp = SHOW_UBITS(re, s, n);
  220. LAST_SKIP_BITS(re, s, n);
  221. CLOSE_READER(re, s);
  222. return tmp;
  223. }
  224. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  225. {
  226. register int tmp;
  227. OPEN_READER(re, s);
  228. av_assert2(n>0 && n<=25);
  229. UPDATE_CACHE_LE(re, s);
  230. tmp = SHOW_UBITS_LE(re, s, n);
  231. LAST_SKIP_BITS(re, s, n);
  232. CLOSE_READER(re, s);
  233. return tmp;
  234. }
  235. /**
  236. * Show 1-25 bits.
  237. */
  238. static inline unsigned int show_bits(GetBitContext *s, int n)
  239. {
  240. register int tmp;
  241. OPEN_READER(re, s);
  242. av_assert2(n>0 && n<=25);
  243. UPDATE_CACHE(re, s);
  244. tmp = SHOW_UBITS(re, s, n);
  245. return tmp;
  246. }
  247. static inline void skip_bits(GetBitContext *s, int n)
  248. {
  249. OPEN_READER(re, s);
  250. UPDATE_CACHE(re, s);
  251. LAST_SKIP_BITS(re, s, n);
  252. CLOSE_READER(re, s);
  253. }
  254. static inline unsigned int get_bits1(GetBitContext *s)
  255. {
  256. unsigned int index = s->index;
  257. uint8_t result = s->buffer[index >> 3];
  258. #ifdef BITSTREAM_READER_LE
  259. result >>= index & 7;
  260. result &= 1;
  261. #else
  262. result <<= index & 7;
  263. result >>= 8 - 1;
  264. #endif
  265. #if !UNCHECKED_BITSTREAM_READER
  266. if (s->index < s->size_in_bits_plus8)
  267. #endif
  268. index++;
  269. s->index = index;
  270. return result;
  271. }
  272. static inline unsigned int show_bits1(GetBitContext *s)
  273. {
  274. return show_bits(s, 1);
  275. }
  276. static inline void skip_bits1(GetBitContext *s)
  277. {
  278. skip_bits(s, 1);
  279. }
  280. /**
  281. * Read 0-32 bits.
  282. */
  283. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  284. {
  285. if (!n) {
  286. return 0;
  287. } else if (n <= MIN_CACHE_BITS) {
  288. return get_bits(s, n);
  289. } else {
  290. #ifdef BITSTREAM_READER_LE
  291. unsigned ret = get_bits(s, 16);
  292. return ret | (get_bits(s, n - 16) << 16);
  293. #else
  294. unsigned ret = get_bits(s, 16) << (n - 16);
  295. return ret | get_bits(s, n - 16);
  296. #endif
  297. }
  298. }
  299. /**
  300. * Read 0-64 bits.
  301. */
  302. static inline uint64_t get_bits64(GetBitContext *s, int n)
  303. {
  304. if (n <= 32) {
  305. return get_bits_long(s, n);
  306. } else {
  307. #ifdef BITSTREAM_READER_LE
  308. uint64_t ret = get_bits_long(s, 32);
  309. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  310. #else
  311. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  312. return ret | get_bits_long(s, 32);
  313. #endif
  314. }
  315. }
  316. /**
  317. * Read 0-32 bits as a signed integer.
  318. */
  319. static inline int get_sbits_long(GetBitContext *s, int n)
  320. {
  321. return sign_extend(get_bits_long(s, n), n);
  322. }
  323. /**
  324. * Show 0-32 bits.
  325. */
  326. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  327. {
  328. if (n <= MIN_CACHE_BITS) {
  329. return show_bits(s, n);
  330. } else {
  331. GetBitContext gb = *s;
  332. return get_bits_long(&gb, n);
  333. }
  334. }
  335. static inline int check_marker(GetBitContext *s, const char *msg)
  336. {
  337. int bit = get_bits1(s);
  338. if (!bit)
  339. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  340. return bit;
  341. }
  342. /**
  343. * Initialize GetBitContext.
  344. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
  345. * larger than the actual read bits because some optimized bitstream
  346. * readers read 32 or 64 bit at once and could read over the end
  347. * @param bit_size the size of the buffer in bits
  348. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  349. */
  350. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  351. int bit_size)
  352. {
  353. int buffer_size;
  354. int ret = 0;
  355. if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
  356. buffer_size = bit_size = 0;
  357. buffer = NULL;
  358. ret = AVERROR_INVALIDDATA;
  359. }
  360. buffer_size = (bit_size + 7) >> 3;
  361. s->buffer = buffer;
  362. s->size_in_bits = bit_size;
  363. s->size_in_bits_plus8 = bit_size + 8;
  364. s->buffer_end = buffer + buffer_size;
  365. s->index = 0;
  366. return ret;
  367. }
  368. /**
  369. * Initialize GetBitContext.
  370. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
  371. * larger than the actual read bits because some optimized bitstream
  372. * readers read 32 or 64 bit at once and could read over the end
  373. * @param byte_size the size of the buffer in bytes
  374. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  375. */
  376. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  377. int byte_size)
  378. {
  379. if (byte_size > INT_MAX / 8 || byte_size < 0)
  380. byte_size = -1;
  381. return init_get_bits(s, buffer, byte_size * 8);
  382. }
  383. static inline const uint8_t *align_get_bits(GetBitContext *s)
  384. {
  385. int n = -get_bits_count(s) & 7;
  386. if (n)
  387. skip_bits(s, n);
  388. return s->buffer + (s->index >> 3);
  389. }
  390. #define init_vlc(vlc, nb_bits, nb_codes, \
  391. bits, bits_wrap, bits_size, \
  392. codes, codes_wrap, codes_size, \
  393. flags) \
  394. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  395. bits, bits_wrap, bits_size, \
  396. codes, codes_wrap, codes_size, \
  397. NULL, 0, 0, flags)
  398. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  399. const void *bits, int bits_wrap, int bits_size,
  400. const void *codes, int codes_wrap, int codes_size,
  401. const void *symbols, int symbols_wrap, int symbols_size,
  402. int flags);
  403. void ff_free_vlc(VLC *vlc);
  404. #define INIT_VLC_LE 2
  405. #define INIT_VLC_USE_NEW_STATIC 4
  406. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  407. do { \
  408. static VLC_TYPE table[static_size][2]; \
  409. (vlc)->table = table; \
  410. (vlc)->table_allocated = static_size; \
  411. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  412. } while (0)
  413. /**
  414. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  415. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  416. * is undefined.
  417. */
  418. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  419. do { \
  420. int n, nb_bits; \
  421. unsigned int index; \
  422. \
  423. index = SHOW_UBITS(name, gb, bits); \
  424. code = table[index][0]; \
  425. n = table[index][1]; \
  426. \
  427. if (max_depth > 1 && n < 0) { \
  428. LAST_SKIP_BITS(name, gb, bits); \
  429. UPDATE_CACHE(name, gb); \
  430. \
  431. nb_bits = -n; \
  432. \
  433. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  434. code = table[index][0]; \
  435. n = table[index][1]; \
  436. if (max_depth > 2 && n < 0) { \
  437. LAST_SKIP_BITS(name, gb, nb_bits); \
  438. UPDATE_CACHE(name, gb); \
  439. \
  440. nb_bits = -n; \
  441. \
  442. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  443. code = table[index][0]; \
  444. n = table[index][1]; \
  445. } \
  446. } \
  447. SKIP_BITS(name, gb, n); \
  448. } while (0)
  449. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  450. max_depth, need_update) \
  451. do { \
  452. int n, nb_bits; \
  453. unsigned int index; \
  454. \
  455. index = SHOW_UBITS(name, gb, bits); \
  456. level = table[index].level; \
  457. n = table[index].len; \
  458. \
  459. if (max_depth > 1 && n < 0) { \
  460. SKIP_BITS(name, gb, bits); \
  461. if (need_update) { \
  462. UPDATE_CACHE(name, gb); \
  463. } \
  464. \
  465. nb_bits = -n; \
  466. \
  467. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  468. level = table[index].level; \
  469. n = table[index].len; \
  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. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  561. #else //TRACE
  562. #define tprintf(p, ...) { }
  563. #endif
  564. #endif /* AVCODEC_GET_BITS_H */