get_bits.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2016 Alexandra Hájková
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * bitstream reader API header.
  24. */
  25. #ifndef AVCODEC_GET_BITS_H
  26. #define AVCODEC_GET_BITS_H
  27. #include <stdint.h>
  28. #include "libavutil/common.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/log.h"
  31. #include "libavutil/avassert.h"
  32. #include "avcodec.h"
  33. #include "mathops.h"
  34. #include "vlc.h"
  35. /*
  36. * Safe bitstream reading:
  37. * optionally, the get_bits API can check to ensure that we
  38. * don't read past input buffer boundaries. This is protected
  39. * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
  40. * then below that with UNCHECKED_BITSTREAM_READER at the per-
  41. * decoder level. This means that decoders that check internally
  42. * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
  43. * overread checks.
  44. * Boundary checking causes a minor performance penalty so for
  45. * applications that won't want/need this, it can be disabled
  46. * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
  47. */
  48. #ifndef UNCHECKED_BITSTREAM_READER
  49. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  50. #endif
  51. #ifndef CACHED_BITSTREAM_READER
  52. #define CACHED_BITSTREAM_READER 0
  53. #endif
  54. typedef struct GetBitContext {
  55. const uint8_t *buffer, *buffer_end;
  56. #if CACHED_BITSTREAM_READER
  57. uint64_t cache;
  58. unsigned bits_left;
  59. #endif
  60. int index;
  61. int size_in_bits;
  62. int size_in_bits_plus8;
  63. } GetBitContext;
  64. static inline unsigned int get_bits(GetBitContext *s, int n);
  65. static inline void skip_bits(GetBitContext *s, int n);
  66. static inline unsigned int show_bits(GetBitContext *s, int n);
  67. /* Bitstream reader API docs:
  68. * name
  69. * arbitrary name which is used as prefix for the internal variables
  70. *
  71. * gb
  72. * getbitcontext
  73. *
  74. * OPEN_READER(name, gb)
  75. * load gb into local variables
  76. *
  77. * CLOSE_READER(name, gb)
  78. * store local vars in gb
  79. *
  80. * UPDATE_CACHE(name, gb)
  81. * Refill the internal cache from the bitstream.
  82. * After this call at least MIN_CACHE_BITS will be available.
  83. *
  84. * GET_CACHE(name, gb)
  85. * Will output the contents of the internal cache,
  86. * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
  87. *
  88. * SHOW_UBITS(name, gb, num)
  89. * Will return the next num bits.
  90. *
  91. * SHOW_SBITS(name, gb, num)
  92. * Will return the next num bits and do sign extension.
  93. *
  94. * SKIP_BITS(name, gb, num)
  95. * Will skip over the next num bits.
  96. * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
  97. *
  98. * SKIP_CACHE(name, gb, num)
  99. * Will remove the next num bits from the cache (note SKIP_COUNTER
  100. * MUST be called before UPDATE_CACHE / CLOSE_READER).
  101. *
  102. * SKIP_COUNTER(name, gb, num)
  103. * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
  104. *
  105. * LAST_SKIP_BITS(name, gb, num)
  106. * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
  107. *
  108. * BITS_LEFT(name, gb)
  109. * Return the number of bits left
  110. *
  111. * For examples see get_bits, show_bits, skip_bits, get_vlc.
  112. */
  113. #if CACHED_BITSTREAM_READER
  114. # define MIN_CACHE_BITS 64
  115. #elif defined LONG_BITSTREAM_READER
  116. # define MIN_CACHE_BITS 32
  117. #else
  118. # define MIN_CACHE_BITS 25
  119. #endif
  120. #if !CACHED_BITSTREAM_READER
  121. #define OPEN_READER_NOSIZE(name, gb) \
  122. unsigned int name ## _index = (gb)->index; \
  123. unsigned int av_unused name ## _cache
  124. #if UNCHECKED_BITSTREAM_READER
  125. #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
  126. #define BITS_AVAILABLE(name, gb) 1
  127. #else
  128. #define OPEN_READER(name, gb) \
  129. OPEN_READER_NOSIZE(name, gb); \
  130. unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
  131. #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
  132. #endif
  133. #define CLOSE_READER(name, gb) (gb)->index = name ## _index
  134. # ifdef LONG_BITSTREAM_READER
  135. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  136. AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  137. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  138. AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
  139. #else
  140. # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
  141. AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
  142. # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
  143. AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
  144. #endif
  145. #ifdef BITSTREAM_READER_LE
  146. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
  147. # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
  148. #else
  149. # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
  150. # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
  151. #endif
  152. #if UNCHECKED_BITSTREAM_READER
  153. # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
  154. #else
  155. # define SKIP_COUNTER(name, gb, num) \
  156. name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
  157. #endif
  158. #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
  159. #define SKIP_BITS(name, gb, num) \
  160. do { \
  161. SKIP_CACHE(name, gb, num); \
  162. SKIP_COUNTER(name, gb, num); \
  163. } while (0)
  164. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  165. #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
  166. #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
  167. #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
  168. #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
  169. #ifdef BITSTREAM_READER_LE
  170. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
  171. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
  172. #else
  173. # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
  174. # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
  175. #endif
  176. #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
  177. #endif
  178. static inline int get_bits_count(const GetBitContext *s)
  179. {
  180. #if CACHED_BITSTREAM_READER
  181. return s->index - s->bits_left;
  182. #else
  183. return s->index;
  184. #endif
  185. }
  186. #if CACHED_BITSTREAM_READER
  187. static inline void refill_32(GetBitContext *s)
  188. {
  189. #if !UNCHECKED_BITSTREAM_READER
  190. if (s->index >> 3 >= s->buffer_end - s->buffer)
  191. return;
  192. #endif
  193. #ifdef BITSTREAM_READER_LE
  194. s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache;
  195. #else
  196. s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left);
  197. #endif
  198. s->index += 32;
  199. s->bits_left += 32;
  200. }
  201. static inline void refill_64(GetBitContext *s)
  202. {
  203. #if !UNCHECKED_BITSTREAM_READER
  204. if (s->index >> 3 >= s->buffer_end - s->buffer)
  205. return;
  206. #endif
  207. #ifdef BITSTREAM_READER_LE
  208. s->cache = AV_RL64(s->buffer + (s->index >> 3));
  209. #else
  210. s->cache = AV_RB64(s->buffer + (s->index >> 3));
  211. #endif
  212. s->index += 64;
  213. s->bits_left = 64;
  214. }
  215. static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le)
  216. {
  217. uint64_t ret;
  218. av_assert2(n>0 && n<=63);
  219. if (is_le) {
  220. ret = s->cache & ((UINT64_C(1) << n) - 1);
  221. s->cache >>= n;
  222. } else {
  223. ret = s->cache >> (64 - n);
  224. s->cache <<= n;
  225. }
  226. s->bits_left -= n;
  227. return ret;
  228. }
  229. static inline unsigned show_val(const GetBitContext *s, unsigned n)
  230. {
  231. #ifdef BITSTREAM_READER_LE
  232. return s->cache & ((UINT64_C(1) << n) - 1);
  233. #else
  234. return s->cache >> (64 - n);
  235. #endif
  236. }
  237. #endif
  238. /**
  239. * Skips the specified number of bits.
  240. * @param n the number of bits to skip,
  241. * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
  242. * from the start to overflow int32_t. Staying within the bitstream + padding
  243. * is sufficient, too.
  244. */
  245. static inline void skip_bits_long(GetBitContext *s, int n)
  246. {
  247. #if CACHED_BITSTREAM_READER
  248. skip_bits(s, n);
  249. #else
  250. #if UNCHECKED_BITSTREAM_READER
  251. s->index += n;
  252. #else
  253. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  254. #endif
  255. #endif
  256. }
  257. #if CACHED_BITSTREAM_READER
  258. static inline void skip_remaining(GetBitContext *s, unsigned n)
  259. {
  260. #ifdef BITSTREAM_READER_LE
  261. s->cache >>= n;
  262. #else
  263. s->cache <<= n;
  264. #endif
  265. s->bits_left -= n;
  266. }
  267. #endif
  268. /**
  269. * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
  270. * if MSB not set it is negative
  271. * @param n length in bits
  272. */
  273. static inline int get_xbits(GetBitContext *s, int n)
  274. {
  275. #if CACHED_BITSTREAM_READER
  276. int32_t cache = show_bits(s, 32);
  277. int sign = ~cache >> 31;
  278. skip_remaining(s, n);
  279. return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
  280. #else
  281. register int sign;
  282. register int32_t cache;
  283. OPEN_READER(re, s);
  284. av_assert2(n>0 && n<=25);
  285. UPDATE_CACHE(re, s);
  286. cache = GET_CACHE(re, s);
  287. sign = ~cache >> 31;
  288. LAST_SKIP_BITS(re, s, n);
  289. CLOSE_READER(re, s);
  290. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  291. #endif
  292. }
  293. #if !CACHED_BITSTREAM_READER
  294. static inline int get_xbits_le(GetBitContext *s, int n)
  295. {
  296. register int sign;
  297. register int32_t cache;
  298. OPEN_READER(re, s);
  299. av_assert2(n>0 && n<=25);
  300. UPDATE_CACHE_LE(re, s);
  301. cache = GET_CACHE(re, s);
  302. sign = sign_extend(~cache, n) >> 31;
  303. LAST_SKIP_BITS(re, s, n);
  304. CLOSE_READER(re, s);
  305. return (zero_extend(sign ^ cache, n) ^ sign) - sign;
  306. }
  307. #endif
  308. static inline int get_sbits(GetBitContext *s, int n)
  309. {
  310. register int tmp;
  311. #if CACHED_BITSTREAM_READER
  312. av_assert2(n>0 && n<=25);
  313. tmp = sign_extend(get_bits(s, n), n);
  314. #else
  315. OPEN_READER(re, s);
  316. av_assert2(n>0 && n<=25);
  317. UPDATE_CACHE(re, s);
  318. tmp = SHOW_SBITS(re, s, n);
  319. LAST_SKIP_BITS(re, s, n);
  320. CLOSE_READER(re, s);
  321. #endif
  322. return tmp;
  323. }
  324. /**
  325. * Read 1-25 bits.
  326. */
  327. static inline unsigned int get_bits(GetBitContext *s, int n)
  328. {
  329. register int tmp;
  330. #if CACHED_BITSTREAM_READER
  331. av_assert2(n>0 && n<=32);
  332. if (n > s->bits_left) {
  333. refill_32(s);
  334. if (s->bits_left < 32)
  335. s->bits_left = n;
  336. }
  337. #ifdef BITSTREAM_READER_LE
  338. tmp = get_val(s, n, 1);
  339. #else
  340. tmp = get_val(s, n, 0);
  341. #endif
  342. #else
  343. OPEN_READER(re, s);
  344. av_assert2(n>0 && n<=25);
  345. UPDATE_CACHE(re, s);
  346. tmp = SHOW_UBITS(re, s, n);
  347. LAST_SKIP_BITS(re, s, n);
  348. CLOSE_READER(re, s);
  349. #endif
  350. return tmp;
  351. }
  352. /**
  353. * Read 0-25 bits.
  354. */
  355. static av_always_inline int get_bitsz(GetBitContext *s, int n)
  356. {
  357. return n ? get_bits(s, n) : 0;
  358. }
  359. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  360. {
  361. #if CACHED_BITSTREAM_READER
  362. av_assert2(n>0 && n<=32);
  363. if (n > s->bits_left) {
  364. refill_32(s);
  365. if (s->bits_left < 32)
  366. s->bits_left = n;
  367. }
  368. return get_val(s, n, 1);
  369. #else
  370. register int tmp;
  371. OPEN_READER(re, s);
  372. av_assert2(n>0 && n<=25);
  373. UPDATE_CACHE_LE(re, s);
  374. tmp = SHOW_UBITS_LE(re, s, n);
  375. LAST_SKIP_BITS(re, s, n);
  376. CLOSE_READER(re, s);
  377. return tmp;
  378. #endif
  379. }
  380. /**
  381. * Show 1-25 bits.
  382. */
  383. static inline unsigned int show_bits(GetBitContext *s, int n)
  384. {
  385. register int tmp;
  386. #if CACHED_BITSTREAM_READER
  387. if (n > s->bits_left)
  388. refill_32(s);
  389. tmp = show_val(s, n);
  390. #else
  391. OPEN_READER_NOSIZE(re, s);
  392. av_assert2(n>0 && n<=25);
  393. UPDATE_CACHE(re, s);
  394. tmp = SHOW_UBITS(re, s, n);
  395. #endif
  396. return tmp;
  397. }
  398. static inline void skip_bits(GetBitContext *s, int n)
  399. {
  400. #if CACHED_BITSTREAM_READER
  401. if (n < s->bits_left)
  402. skip_remaining(s, n);
  403. else {
  404. n -= s->bits_left;
  405. s->cache = 0;
  406. s->bits_left = 0;
  407. if (n >= 64) {
  408. unsigned skip = (n / 8) * 8;
  409. n -= skip;
  410. s->index += skip;
  411. }
  412. refill_64(s);
  413. if (n)
  414. skip_remaining(s, n);
  415. }
  416. #else
  417. OPEN_READER(re, s);
  418. LAST_SKIP_BITS(re, s, n);
  419. CLOSE_READER(re, s);
  420. #endif
  421. }
  422. static inline unsigned int get_bits1(GetBitContext *s)
  423. {
  424. #if CACHED_BITSTREAM_READER
  425. if (!s->bits_left)
  426. refill_64(s);
  427. #ifdef BITSTREAM_READER_LE
  428. return get_val(s, 1, 1);
  429. #else
  430. return get_val(s, 1, 0);
  431. #endif
  432. #else
  433. unsigned int index = s->index;
  434. uint8_t result = s->buffer[index >> 3];
  435. #ifdef BITSTREAM_READER_LE
  436. result >>= index & 7;
  437. result &= 1;
  438. #else
  439. result <<= index & 7;
  440. result >>= 8 - 1;
  441. #endif
  442. #if !UNCHECKED_BITSTREAM_READER
  443. if (s->index < s->size_in_bits_plus8)
  444. #endif
  445. index++;
  446. s->index = index;
  447. return result;
  448. #endif
  449. }
  450. static inline unsigned int show_bits1(GetBitContext *s)
  451. {
  452. return show_bits(s, 1);
  453. }
  454. static inline void skip_bits1(GetBitContext *s)
  455. {
  456. skip_bits(s, 1);
  457. }
  458. /**
  459. * Read 0-32 bits.
  460. */
  461. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  462. {
  463. av_assert2(n>=0 && n<=32);
  464. if (!n) {
  465. return 0;
  466. #if CACHED_BITSTREAM_READER
  467. }
  468. return get_bits(s, n);
  469. #else
  470. } else if (n <= MIN_CACHE_BITS) {
  471. return get_bits(s, n);
  472. } else {
  473. #ifdef BITSTREAM_READER_LE
  474. unsigned ret = get_bits(s, 16);
  475. return ret | (get_bits(s, n - 16) << 16);
  476. #else
  477. unsigned ret = get_bits(s, 16) << (n - 16);
  478. return ret | get_bits(s, n - 16);
  479. #endif
  480. }
  481. #endif
  482. }
  483. /**
  484. * Read 0-64 bits.
  485. */
  486. static inline uint64_t get_bits64(GetBitContext *s, int n)
  487. {
  488. if (n <= 32) {
  489. return get_bits_long(s, n);
  490. } else {
  491. #ifdef BITSTREAM_READER_LE
  492. uint64_t ret = get_bits_long(s, 32);
  493. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  494. #else
  495. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  496. return ret | get_bits_long(s, 32);
  497. #endif
  498. }
  499. }
  500. /**
  501. * Read 0-32 bits as a signed integer.
  502. */
  503. static inline int get_sbits_long(GetBitContext *s, int n)
  504. {
  505. // sign_extend(x, 0) is undefined
  506. if (!n)
  507. return 0;
  508. return sign_extend(get_bits_long(s, n), n);
  509. }
  510. /**
  511. * Show 0-32 bits.
  512. */
  513. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  514. {
  515. if (n <= MIN_CACHE_BITS) {
  516. return show_bits(s, n);
  517. } else {
  518. GetBitContext gb = *s;
  519. return get_bits_long(&gb, n);
  520. }
  521. }
  522. static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
  523. {
  524. int bit = get_bits1(s);
  525. if (!bit)
  526. av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
  527. get_bits_count(s) - 1, s->size_in_bits, msg);
  528. return bit;
  529. }
  530. /**
  531. * Initialize GetBitContext.
  532. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  533. * larger than the actual read bits because some optimized bitstream
  534. * readers read 32 or 64 bit at once and could read over the end
  535. * @param bit_size the size of the buffer in bits
  536. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  537. */
  538. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  539. int bit_size)
  540. {
  541. int buffer_size;
  542. int ret = 0;
  543. if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
  544. bit_size = 0;
  545. buffer = NULL;
  546. ret = AVERROR_INVALIDDATA;
  547. }
  548. buffer_size = (bit_size + 7) >> 3;
  549. s->buffer = buffer;
  550. s->size_in_bits = bit_size;
  551. s->size_in_bits_plus8 = bit_size + 8;
  552. s->buffer_end = buffer + buffer_size;
  553. s->index = 0;
  554. #if CACHED_BITSTREAM_READER
  555. refill_64(s);
  556. #endif
  557. return ret;
  558. }
  559. /**
  560. * Initialize GetBitContext.
  561. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  562. * larger than the actual read bits because some optimized bitstream
  563. * readers read 32 or 64 bit at once and could read over the end
  564. * @param byte_size the size of the buffer in bytes
  565. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  566. */
  567. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  568. int byte_size)
  569. {
  570. if (byte_size > INT_MAX / 8 || byte_size < 0)
  571. byte_size = -1;
  572. return init_get_bits(s, buffer, byte_size * 8);
  573. }
  574. static inline const uint8_t *align_get_bits(GetBitContext *s)
  575. {
  576. int n = -get_bits_count(s) & 7;
  577. if (n)
  578. skip_bits(s, n);
  579. return s->buffer + (s->index >> 3);
  580. }
  581. /**
  582. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  583. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  584. * is undefined.
  585. */
  586. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  587. do { \
  588. int n, nb_bits; \
  589. unsigned int index; \
  590. \
  591. index = SHOW_UBITS(name, gb, bits); \
  592. code = table[index][0]; \
  593. n = table[index][1]; \
  594. \
  595. if (max_depth > 1 && n < 0) { \
  596. LAST_SKIP_BITS(name, gb, bits); \
  597. UPDATE_CACHE(name, gb); \
  598. \
  599. nb_bits = -n; \
  600. \
  601. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  602. code = table[index][0]; \
  603. n = table[index][1]; \
  604. if (max_depth > 2 && n < 0) { \
  605. LAST_SKIP_BITS(name, gb, nb_bits); \
  606. UPDATE_CACHE(name, gb); \
  607. \
  608. nb_bits = -n; \
  609. \
  610. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  611. code = table[index][0]; \
  612. n = table[index][1]; \
  613. } \
  614. } \
  615. SKIP_BITS(name, gb, n); \
  616. } while (0)
  617. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  618. max_depth, need_update) \
  619. do { \
  620. int n, nb_bits; \
  621. unsigned int index; \
  622. \
  623. index = SHOW_UBITS(name, gb, bits); \
  624. level = table[index].level; \
  625. n = table[index].len; \
  626. \
  627. if (max_depth > 1 && n < 0) { \
  628. SKIP_BITS(name, gb, bits); \
  629. if (need_update) { \
  630. UPDATE_CACHE(name, gb); \
  631. } \
  632. \
  633. nb_bits = -n; \
  634. \
  635. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  636. level = table[index].level; \
  637. n = table[index].len; \
  638. if (max_depth > 2 && n < 0) { \
  639. LAST_SKIP_BITS(name, gb, nb_bits); \
  640. if (need_update) { \
  641. UPDATE_CACHE(name, gb); \
  642. } \
  643. nb_bits = -n; \
  644. \
  645. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  646. level = table[index].level; \
  647. n = table[index].len; \
  648. } \
  649. } \
  650. run = table[index].run; \
  651. SKIP_BITS(name, gb, n); \
  652. } while (0)
  653. /* Return the LUT element for the given bitstream configuration. */
  654. static inline int set_idx(GetBitContext *s, int code, int *n, int *nb_bits,
  655. VLC_TYPE (*table)[2])
  656. {
  657. unsigned idx;
  658. *nb_bits = -*n;
  659. idx = show_bits(s, *nb_bits) + code;
  660. *n = table[idx][1];
  661. return table[idx][0];
  662. }
  663. /**
  664. * Parse a vlc code.
  665. * @param bits is the number of bits which will be read at once, must be
  666. * identical to nb_bits in init_vlc()
  667. * @param max_depth is the number of times bits bits must be read to completely
  668. * read the longest vlc code
  669. * = (max_vlc_length + bits - 1) / bits
  670. * @returns the code parsed or -1 if no vlc matches
  671. */
  672. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  673. int bits, int max_depth)
  674. {
  675. #if CACHED_BITSTREAM_READER
  676. int nb_bits;
  677. unsigned idx = show_bits(s, bits);
  678. int code = table[idx][0];
  679. int n = table[idx][1];
  680. if (max_depth > 1 && n < 0) {
  681. skip_remaining(s, bits);
  682. code = set_idx(s, code, &n, &nb_bits, table);
  683. if (max_depth > 2 && n < 0) {
  684. skip_remaining(s, nb_bits);
  685. code = set_idx(s, code, &n, &nb_bits, table);
  686. }
  687. }
  688. skip_remaining(s, n);
  689. return code;
  690. #else
  691. int code;
  692. OPEN_READER(re, s);
  693. UPDATE_CACHE(re, s);
  694. GET_VLC(code, re, s, table, bits, max_depth);
  695. CLOSE_READER(re, s);
  696. return code;
  697. #endif
  698. }
  699. static inline int decode012(GetBitContext *gb)
  700. {
  701. int n;
  702. n = get_bits1(gb);
  703. if (n == 0)
  704. return 0;
  705. else
  706. return get_bits1(gb) + 1;
  707. }
  708. static inline int decode210(GetBitContext *gb)
  709. {
  710. if (get_bits1(gb))
  711. return 0;
  712. else
  713. return 2 - get_bits1(gb);
  714. }
  715. static inline int get_bits_left(GetBitContext *gb)
  716. {
  717. return gb->size_in_bits - get_bits_count(gb);
  718. }
  719. static inline int skip_1stop_8data_bits(GetBitContext *gb)
  720. {
  721. if (get_bits_left(gb) <= 0)
  722. return AVERROR_INVALIDDATA;
  723. while (get_bits1(gb)) {
  724. skip_bits(gb, 8);
  725. if (get_bits_left(gb) <= 0)
  726. return AVERROR_INVALIDDATA;
  727. }
  728. return 0;
  729. }
  730. #endif /* AVCODEC_GET_BITS_H */