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.

631 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 void align_get_bits(GetBitContext *s)
  368. {
  369. int n = -get_bits_count(s) & 7;
  370. if (n)
  371. skip_bits(s, n);
  372. }
  373. #define init_vlc(vlc, nb_bits, nb_codes, \
  374. bits, bits_wrap, bits_size, \
  375. codes, codes_wrap, codes_size, \
  376. flags) \
  377. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  378. bits, bits_wrap, bits_size, \
  379. codes, codes_wrap, codes_size, \
  380. NULL, 0, 0, flags)
  381. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  382. const void *bits, int bits_wrap, int bits_size,
  383. const void *codes, int codes_wrap, int codes_size,
  384. const void *symbols, int symbols_wrap, int symbols_size,
  385. int flags);
  386. void ff_free_vlc(VLC *vlc);
  387. #define INIT_VLC_LE 2
  388. #define INIT_VLC_USE_NEW_STATIC 4
  389. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  390. do { \
  391. static VLC_TYPE table[static_size][2]; \
  392. (vlc)->table = table; \
  393. (vlc)->table_allocated = static_size; \
  394. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  395. } while (0)
  396. /**
  397. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  398. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  399. * is undefined.
  400. */
  401. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  402. do { \
  403. int n, nb_bits; \
  404. unsigned int index; \
  405. \
  406. index = SHOW_UBITS(name, gb, bits); \
  407. code = table[index][0]; \
  408. n = table[index][1]; \
  409. \
  410. if (max_depth > 1 && n < 0) { \
  411. LAST_SKIP_BITS(name, gb, bits); \
  412. UPDATE_CACHE(name, gb); \
  413. \
  414. nb_bits = -n; \
  415. \
  416. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  417. code = table[index][0]; \
  418. n = table[index][1]; \
  419. if (max_depth > 2 && n < 0) { \
  420. LAST_SKIP_BITS(name, gb, nb_bits); \
  421. UPDATE_CACHE(name, gb); \
  422. \
  423. nb_bits = -n; \
  424. \
  425. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  426. code = table[index][0]; \
  427. n = table[index][1]; \
  428. } \
  429. } \
  430. SKIP_BITS(name, gb, n); \
  431. } while (0)
  432. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  433. max_depth, need_update) \
  434. do { \
  435. int n, nb_bits; \
  436. unsigned int index; \
  437. \
  438. index = SHOW_UBITS(name, gb, bits); \
  439. level = table[index].level; \
  440. n = table[index].len; \
  441. \
  442. if (max_depth > 1 && n < 0) { \
  443. SKIP_BITS(name, gb, bits); \
  444. if (need_update) { \
  445. UPDATE_CACHE(name, gb); \
  446. } \
  447. \
  448. nb_bits = -n; \
  449. \
  450. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  451. level = table[index].level; \
  452. n = table[index].len; \
  453. } \
  454. run = table[index].run; \
  455. SKIP_BITS(name, gb, n); \
  456. } while (0)
  457. /**
  458. * Parse a vlc code.
  459. * @param bits is the number of bits which will be read at once, must be
  460. * identical to nb_bits in init_vlc()
  461. * @param max_depth is the number of times bits bits must be read to completely
  462. * read the longest vlc code
  463. * = (max_vlc_length + bits - 1) / bits
  464. */
  465. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  466. int bits, int max_depth)
  467. {
  468. int code;
  469. OPEN_READER(re, s);
  470. UPDATE_CACHE(re, s);
  471. GET_VLC(code, re, s, table, bits, max_depth);
  472. CLOSE_READER(re, s);
  473. return code;
  474. }
  475. static inline int decode012(GetBitContext *gb)
  476. {
  477. int n;
  478. n = get_bits1(gb);
  479. if (n == 0)
  480. return 0;
  481. else
  482. return get_bits1(gb) + 1;
  483. }
  484. static inline int decode210(GetBitContext *gb)
  485. {
  486. if (get_bits1(gb))
  487. return 0;
  488. else
  489. return 2 - get_bits1(gb);
  490. }
  491. static inline int get_bits_left(GetBitContext *gb)
  492. {
  493. return gb->size_in_bits - get_bits_count(gb);
  494. }
  495. //#define TRACE
  496. #ifdef TRACE
  497. static inline void print_bin(int bits, int n)
  498. {
  499. int i;
  500. for (i = n - 1; i >= 0; i--)
  501. av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
  502. for (i = n; i < 24; i++)
  503. av_log(NULL, AV_LOG_DEBUG, " ");
  504. }
  505. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  506. const char *func, int line)
  507. {
  508. int r = get_bits(s, n);
  509. print_bin(r, n);
  510. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  511. r, n, r, get_bits_count(s) - n, file, func, line);
  512. return r;
  513. }
  514. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  515. int bits, int max_depth, const char *file,
  516. const char *func, int line)
  517. {
  518. int show = show_bits(s, 24);
  519. int pos = get_bits_count(s);
  520. int r = get_vlc2(s, table, bits, max_depth);
  521. int len = get_bits_count(s) - pos;
  522. int bits2 = show >> (24 - len);
  523. print_bin(bits2, len);
  524. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  525. bits2, len, r, pos, file, func, line);
  526. return r;
  527. }
  528. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  529. const char *func, int line)
  530. {
  531. int show = show_bits(s, n);
  532. int r = get_xbits(s, n);
  533. print_bin(show, n);
  534. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  535. show, n, r, get_bits_count(s) - n, file, func, line);
  536. return r;
  537. }
  538. #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  539. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  540. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  541. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  542. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  543. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  544. #else //TRACE
  545. #define tprintf(p, ...) { }
  546. #endif
  547. #endif /* AVCODEC_GET_BITS_H */