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.

593 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. // sign_extend(x, 0) is undefined
  321. if (!n)
  322. return 0;
  323. return sign_extend(get_bits_long(s, n), n);
  324. }
  325. /**
  326. * Show 0-32 bits.
  327. */
  328. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  329. {
  330. if (n <= MIN_CACHE_BITS) {
  331. return show_bits(s, n);
  332. } else {
  333. GetBitContext gb = *s;
  334. return get_bits_long(&gb, n);
  335. }
  336. }
  337. static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
  338. {
  339. int bit = get_bits1(s);
  340. if (!bit)
  341. av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
  342. get_bits_count(s) - 1, s->size_in_bits, msg);
  343. return bit;
  344. }
  345. /**
  346. * Initialize GetBitContext.
  347. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  348. * larger than the actual read bits because some optimized bitstream
  349. * readers read 32 or 64 bit at once and could read over the end
  350. * @param bit_size the size of the buffer in bits
  351. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  352. */
  353. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  354. int bit_size)
  355. {
  356. int buffer_size;
  357. int ret = 0;
  358. if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
  359. bit_size = 0;
  360. buffer = NULL;
  361. ret = AVERROR_INVALIDDATA;
  362. }
  363. buffer_size = (bit_size + 7) >> 3;
  364. s->buffer = buffer;
  365. s->size_in_bits = bit_size;
  366. s->size_in_bits_plus8 = bit_size + 8;
  367. s->buffer_end = buffer + buffer_size;
  368. s->index = 0;
  369. return ret;
  370. }
  371. /**
  372. * Initialize GetBitContext.
  373. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  374. * larger than the actual read bits because some optimized bitstream
  375. * readers read 32 or 64 bit at once and could read over the end
  376. * @param byte_size the size of the buffer in bytes
  377. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  378. */
  379. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  380. int byte_size)
  381. {
  382. if (byte_size > INT_MAX / 8 || byte_size < 0)
  383. byte_size = -1;
  384. return init_get_bits(s, buffer, byte_size * 8);
  385. }
  386. static inline const uint8_t *align_get_bits(GetBitContext *s)
  387. {
  388. int n = -get_bits_count(s) & 7;
  389. if (n)
  390. skip_bits(s, n);
  391. return s->buffer + (s->index >> 3);
  392. }
  393. /**
  394. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  395. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  396. * is undefined.
  397. */
  398. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  399. do { \
  400. int n, nb_bits; \
  401. unsigned int index; \
  402. \
  403. index = SHOW_UBITS(name, gb, bits); \
  404. code = table[index][0]; \
  405. n = table[index][1]; \
  406. \
  407. if (max_depth > 1 && n < 0) { \
  408. LAST_SKIP_BITS(name, gb, bits); \
  409. UPDATE_CACHE(name, gb); \
  410. \
  411. nb_bits = -n; \
  412. \
  413. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  414. code = table[index][0]; \
  415. n = table[index][1]; \
  416. if (max_depth > 2 && n < 0) { \
  417. LAST_SKIP_BITS(name, gb, nb_bits); \
  418. UPDATE_CACHE(name, gb); \
  419. \
  420. nb_bits = -n; \
  421. \
  422. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  423. code = table[index][0]; \
  424. n = table[index][1]; \
  425. } \
  426. } \
  427. SKIP_BITS(name, gb, n); \
  428. } while (0)
  429. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  430. max_depth, need_update) \
  431. do { \
  432. int n, nb_bits; \
  433. unsigned int index; \
  434. \
  435. index = SHOW_UBITS(name, gb, bits); \
  436. level = table[index].level; \
  437. n = table[index].len; \
  438. \
  439. if (max_depth > 1 && n < 0) { \
  440. SKIP_BITS(name, gb, bits); \
  441. if (need_update) { \
  442. UPDATE_CACHE(name, gb); \
  443. } \
  444. \
  445. nb_bits = -n; \
  446. \
  447. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  448. level = table[index].level; \
  449. n = table[index].len; \
  450. if (max_depth > 2 && n < 0) { \
  451. LAST_SKIP_BITS(name, gb, nb_bits); \
  452. if (need_update) { \
  453. UPDATE_CACHE(name, gb); \
  454. } \
  455. nb_bits = -n; \
  456. \
  457. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  458. level = table[index].level; \
  459. n = table[index].len; \
  460. } \
  461. } \
  462. run = table[index].run; \
  463. SKIP_BITS(name, gb, n); \
  464. } while (0)
  465. /**
  466. * Parse a vlc code.
  467. * @param bits is the number of bits which will be read at once, must be
  468. * identical to nb_bits in init_vlc()
  469. * @param max_depth is the number of times bits bits must be read to completely
  470. * read the longest vlc code
  471. * = (max_vlc_length + bits - 1) / bits
  472. * @returns the code parsed or -1 if no vlc matches
  473. */
  474. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  475. int bits, int max_depth)
  476. {
  477. int code;
  478. OPEN_READER(re, s);
  479. UPDATE_CACHE(re, s);
  480. GET_VLC(code, re, s, table, bits, max_depth);
  481. CLOSE_READER(re, s);
  482. return code;
  483. }
  484. static inline int decode012(GetBitContext *gb)
  485. {
  486. int n;
  487. n = get_bits1(gb);
  488. if (n == 0)
  489. return 0;
  490. else
  491. return get_bits1(gb) + 1;
  492. }
  493. static inline int decode210(GetBitContext *gb)
  494. {
  495. if (get_bits1(gb))
  496. return 0;
  497. else
  498. return 2 - get_bits1(gb);
  499. }
  500. static inline int get_bits_left(GetBitContext *gb)
  501. {
  502. return gb->size_in_bits - get_bits_count(gb);
  503. }
  504. static inline int skip_1stop_8data_bits(GetBitContext *gb)
  505. {
  506. if (get_bits_left(gb) <= 0)
  507. return AVERROR_INVALIDDATA;
  508. while (get_bits1(gb)) {
  509. skip_bits(gb, 8);
  510. if (get_bits_left(gb) <= 0)
  511. return AVERROR_INVALIDDATA;
  512. }
  513. return 0;
  514. }
  515. #endif /* AVCODEC_GET_BITS_H */