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.

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