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.

607 lines
18KB

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