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.

532 lines
16KB

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