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.

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