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.

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