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.

588 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_sbits(GetBitContext *s, int n)
  193. {
  194. register int tmp;
  195. OPEN_READER(re, s);
  196. av_assert2(n>0 && n<=25);
  197. UPDATE_CACHE(re, s);
  198. tmp = SHOW_SBITS(re, s, n);
  199. LAST_SKIP_BITS(re, s, n);
  200. CLOSE_READER(re, s);
  201. return tmp;
  202. }
  203. /**
  204. * Read 1-25 bits.
  205. */
  206. static inline unsigned int get_bits(GetBitContext *s, int n)
  207. {
  208. register int tmp;
  209. OPEN_READER(re, s);
  210. av_assert2(n>0 && n<=25);
  211. UPDATE_CACHE(re, s);
  212. tmp = SHOW_UBITS(re, s, n);
  213. LAST_SKIP_BITS(re, s, n);
  214. CLOSE_READER(re, s);
  215. return tmp;
  216. }
  217. /**
  218. * Read 0-25 bits.
  219. */
  220. static av_always_inline int get_bitsz(GetBitContext *s, int n)
  221. {
  222. return n ? get_bits(s, n) : 0;
  223. }
  224. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  225. {
  226. register int tmp;
  227. OPEN_READER(re, s);
  228. av_assert2(n>0 && n<=25);
  229. UPDATE_CACHE_LE(re, s);
  230. tmp = SHOW_UBITS_LE(re, s, n);
  231. LAST_SKIP_BITS(re, s, n);
  232. CLOSE_READER(re, s);
  233. return tmp;
  234. }
  235. /**
  236. * Show 1-25 bits.
  237. */
  238. static inline unsigned int show_bits(GetBitContext *s, int n)
  239. {
  240. register int tmp;
  241. OPEN_READER_NOSIZE(re, s);
  242. av_assert2(n>0 && n<=25);
  243. UPDATE_CACHE(re, s);
  244. tmp = SHOW_UBITS(re, s, n);
  245. return tmp;
  246. }
  247. static inline void skip_bits(GetBitContext *s, int n)
  248. {
  249. OPEN_READER(re, s);
  250. LAST_SKIP_BITS(re, s, n);
  251. CLOSE_READER(re, s);
  252. }
  253. static inline unsigned int get_bits1(GetBitContext *s)
  254. {
  255. unsigned int index = s->index;
  256. uint8_t result = s->buffer[index >> 3];
  257. #ifdef BITSTREAM_READER_LE
  258. result >>= index & 7;
  259. result &= 1;
  260. #else
  261. result <<= index & 7;
  262. result >>= 8 - 1;
  263. #endif
  264. #if !UNCHECKED_BITSTREAM_READER
  265. if (s->index < s->size_in_bits_plus8)
  266. #endif
  267. index++;
  268. s->index = index;
  269. return result;
  270. }
  271. static inline unsigned int show_bits1(GetBitContext *s)
  272. {
  273. return show_bits(s, 1);
  274. }
  275. static inline void skip_bits1(GetBitContext *s)
  276. {
  277. skip_bits(s, 1);
  278. }
  279. /**
  280. * Read 0-32 bits.
  281. */
  282. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  283. {
  284. if (!n) {
  285. return 0;
  286. } else if (n <= MIN_CACHE_BITS) {
  287. return get_bits(s, n);
  288. } else {
  289. #ifdef BITSTREAM_READER_LE
  290. unsigned ret = get_bits(s, 16);
  291. return ret | (get_bits(s, n - 16) << 16);
  292. #else
  293. unsigned ret = get_bits(s, 16) << (n - 16);
  294. return ret | get_bits(s, n - 16);
  295. #endif
  296. }
  297. }
  298. /**
  299. * Read 0-64 bits.
  300. */
  301. static inline uint64_t get_bits64(GetBitContext *s, int n)
  302. {
  303. if (n <= 32) {
  304. return get_bits_long(s, n);
  305. } else {
  306. #ifdef BITSTREAM_READER_LE
  307. uint64_t ret = get_bits_long(s, 32);
  308. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  309. #else
  310. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  311. return ret | get_bits_long(s, 32);
  312. #endif
  313. }
  314. }
  315. /**
  316. * Read 0-32 bits as a signed integer.
  317. */
  318. static inline int get_sbits_long(GetBitContext *s, int n)
  319. {
  320. return sign_extend(get_bits_long(s, n), n);
  321. }
  322. /**
  323. * Show 0-32 bits.
  324. */
  325. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  326. {
  327. if (n <= MIN_CACHE_BITS) {
  328. return show_bits(s, n);
  329. } else {
  330. GetBitContext gb = *s;
  331. return get_bits_long(&gb, n);
  332. }
  333. }
  334. static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
  335. {
  336. int bit = get_bits1(s);
  337. if (!bit)
  338. av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
  339. get_bits_count(s) - 1, s->size_in_bits, msg);
  340. return bit;
  341. }
  342. /**
  343. * Initialize GetBitContext.
  344. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  345. * larger than the actual read bits because some optimized bitstream
  346. * readers read 32 or 64 bit at once and could read over the end
  347. * @param bit_size the size of the buffer in bits
  348. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  349. */
  350. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  351. int bit_size)
  352. {
  353. int buffer_size;
  354. int ret = 0;
  355. if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
  356. bit_size = 0;
  357. buffer = NULL;
  358. ret = AVERROR_INVALIDDATA;
  359. }
  360. buffer_size = (bit_size + 7) >> 3;
  361. s->buffer = buffer;
  362. s->size_in_bits = bit_size;
  363. s->size_in_bits_plus8 = bit_size + 8;
  364. s->buffer_end = buffer + buffer_size;
  365. s->index = 0;
  366. return ret;
  367. }
  368. /**
  369. * Initialize GetBitContext.
  370. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  371. * larger than the actual read bits because some optimized bitstream
  372. * readers read 32 or 64 bit at once and could read over the end
  373. * @param byte_size the size of the buffer in bytes
  374. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  375. */
  376. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  377. int byte_size)
  378. {
  379. if (byte_size > INT_MAX / 8 || byte_size < 0)
  380. byte_size = -1;
  381. return init_get_bits(s, buffer, byte_size * 8);
  382. }
  383. static inline const uint8_t *align_get_bits(GetBitContext *s)
  384. {
  385. int n = -get_bits_count(s) & 7;
  386. if (n)
  387. skip_bits(s, n);
  388. return s->buffer + (s->index >> 3);
  389. }
  390. /**
  391. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  392. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  393. * is undefined.
  394. */
  395. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  396. do { \
  397. int n, nb_bits; \
  398. unsigned int index; \
  399. \
  400. index = SHOW_UBITS(name, gb, bits); \
  401. code = table[index][0]; \
  402. n = table[index][1]; \
  403. \
  404. if (max_depth > 1 && n < 0) { \
  405. LAST_SKIP_BITS(name, gb, bits); \
  406. UPDATE_CACHE(name, gb); \
  407. \
  408. nb_bits = -n; \
  409. \
  410. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  411. code = table[index][0]; \
  412. n = table[index][1]; \
  413. if (max_depth > 2 && n < 0) { \
  414. LAST_SKIP_BITS(name, gb, nb_bits); \
  415. UPDATE_CACHE(name, gb); \
  416. \
  417. nb_bits = -n; \
  418. \
  419. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  420. code = table[index][0]; \
  421. n = table[index][1]; \
  422. } \
  423. } \
  424. SKIP_BITS(name, gb, n); \
  425. } while (0)
  426. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  427. max_depth, need_update) \
  428. do { \
  429. int n, nb_bits; \
  430. unsigned int index; \
  431. \
  432. index = SHOW_UBITS(name, gb, bits); \
  433. level = table[index].level; \
  434. n = table[index].len; \
  435. \
  436. if (max_depth > 1 && n < 0) { \
  437. SKIP_BITS(name, gb, bits); \
  438. if (need_update) { \
  439. UPDATE_CACHE(name, gb); \
  440. } \
  441. \
  442. nb_bits = -n; \
  443. \
  444. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  445. level = table[index].level; \
  446. n = table[index].len; \
  447. if (max_depth > 2 && n < 0) { \
  448. LAST_SKIP_BITS(name, gb, nb_bits); \
  449. if (need_update) { \
  450. UPDATE_CACHE(name, gb); \
  451. } \
  452. nb_bits = -n; \
  453. \
  454. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  455. level = table[index].level; \
  456. n = table[index].len; \
  457. } \
  458. } \
  459. run = table[index].run; \
  460. SKIP_BITS(name, gb, n); \
  461. } while (0)
  462. /**
  463. * Parse a vlc code.
  464. * @param bits is the number of bits which will be read at once, must be
  465. * identical to nb_bits in init_vlc()
  466. * @param max_depth is the number of times bits bits must be read to completely
  467. * read the longest vlc code
  468. * = (max_vlc_length + bits - 1) / bits
  469. */
  470. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  471. int bits, int max_depth)
  472. {
  473. int code;
  474. OPEN_READER(re, s);
  475. UPDATE_CACHE(re, s);
  476. GET_VLC(code, re, s, table, bits, max_depth);
  477. CLOSE_READER(re, s);
  478. return code;
  479. }
  480. static inline int decode012(GetBitContext *gb)
  481. {
  482. int n;
  483. n = get_bits1(gb);
  484. if (n == 0)
  485. return 0;
  486. else
  487. return get_bits1(gb) + 1;
  488. }
  489. static inline int decode210(GetBitContext *gb)
  490. {
  491. if (get_bits1(gb))
  492. return 0;
  493. else
  494. return 2 - get_bits1(gb);
  495. }
  496. static inline int get_bits_left(GetBitContext *gb)
  497. {
  498. return gb->size_in_bits - get_bits_count(gb);
  499. }
  500. static inline int skip_1stop_8data_bits(GetBitContext *gb)
  501. {
  502. if (get_bits_left(gb) <= 0)
  503. return AVERROR_INVALIDDATA;
  504. while (get_bits1(gb)) {
  505. skip_bits(gb, 8);
  506. if (get_bits_left(gb) <= 0)
  507. return AVERROR_INVALIDDATA;
  508. }
  509. return 0;
  510. }
  511. #endif /* AVCODEC_GET_BITS_H */