get_bits.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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, int is_le)
  188. {
  189. #if !UNCHECKED_BITSTREAM_READER
  190. if (s->index >> 3 >= s->buffer_end - s->buffer)
  191. return;
  192. #endif
  193. if (is_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. s->index += 32;
  198. s->bits_left += 32;
  199. }
  200. static inline void refill_64(GetBitContext *s, int is_le)
  201. {
  202. #if !UNCHECKED_BITSTREAM_READER
  203. if (s->index >> 3 >= s->buffer_end - s->buffer)
  204. return;
  205. #endif
  206. if (is_le)
  207. s->cache = AV_RL64(s->buffer + (s->index >> 3));
  208. else
  209. s->cache = AV_RB64(s->buffer + (s->index >> 3));
  210. s->index += 64;
  211. s->bits_left = 64;
  212. }
  213. static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le)
  214. {
  215. uint64_t ret;
  216. av_assert2(n>0 && n<=63);
  217. if (is_le) {
  218. ret = s->cache & ((UINT64_C(1) << n) - 1);
  219. s->cache >>= n;
  220. } else {
  221. ret = s->cache >> (64 - n);
  222. s->cache <<= n;
  223. }
  224. s->bits_left -= n;
  225. return ret;
  226. }
  227. static inline unsigned show_val(const GetBitContext *s, unsigned n)
  228. {
  229. #ifdef BITSTREAM_READER_LE
  230. return s->cache & ((UINT64_C(1) << n) - 1);
  231. #else
  232. return s->cache >> (64 - n);
  233. #endif
  234. }
  235. #endif
  236. /**
  237. * Skips the specified number of bits.
  238. * @param n the number of bits to skip,
  239. * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
  240. * from the start to overflow int32_t. Staying within the bitstream + padding
  241. * is sufficient, too.
  242. */
  243. static inline void skip_bits_long(GetBitContext *s, int n)
  244. {
  245. #if CACHED_BITSTREAM_READER
  246. skip_bits(s, n);
  247. #else
  248. #if UNCHECKED_BITSTREAM_READER
  249. s->index += n;
  250. #else
  251. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  252. #endif
  253. #endif
  254. }
  255. #if CACHED_BITSTREAM_READER
  256. static inline void skip_remaining(GetBitContext *s, unsigned n)
  257. {
  258. #ifdef BITSTREAM_READER_LE
  259. s->cache >>= n;
  260. #else
  261. s->cache <<= n;
  262. #endif
  263. s->bits_left -= n;
  264. }
  265. #endif
  266. /**
  267. * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
  268. * if MSB not set it is negative
  269. * @param n length in bits
  270. */
  271. static inline int get_xbits(GetBitContext *s, int n)
  272. {
  273. #if CACHED_BITSTREAM_READER
  274. int32_t cache = show_bits(s, 32);
  275. int sign = ~cache >> 31;
  276. skip_remaining(s, n);
  277. return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
  278. #else
  279. register int sign;
  280. register int32_t cache;
  281. OPEN_READER(re, s);
  282. av_assert2(n>0 && n<=25);
  283. UPDATE_CACHE(re, s);
  284. cache = GET_CACHE(re, s);
  285. sign = ~cache >> 31;
  286. LAST_SKIP_BITS(re, s, n);
  287. CLOSE_READER(re, s);
  288. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  289. #endif
  290. }
  291. #if !CACHED_BITSTREAM_READER
  292. static inline int get_xbits_le(GetBitContext *s, int n)
  293. {
  294. register int sign;
  295. register int32_t cache;
  296. OPEN_READER(re, s);
  297. av_assert2(n>0 && n<=25);
  298. UPDATE_CACHE_LE(re, s);
  299. cache = GET_CACHE(re, s);
  300. sign = sign_extend(~cache, n) >> 31;
  301. LAST_SKIP_BITS(re, s, n);
  302. CLOSE_READER(re, s);
  303. return (zero_extend(sign ^ cache, n) ^ sign) - sign;
  304. }
  305. #endif
  306. static inline int get_sbits(GetBitContext *s, int n)
  307. {
  308. register int tmp;
  309. #if CACHED_BITSTREAM_READER
  310. av_assert2(n>0 && n<=25);
  311. tmp = sign_extend(get_bits(s, n), n);
  312. #else
  313. OPEN_READER(re, s);
  314. av_assert2(n>0 && n<=25);
  315. UPDATE_CACHE(re, s);
  316. tmp = SHOW_SBITS(re, s, n);
  317. LAST_SKIP_BITS(re, s, n);
  318. CLOSE_READER(re, s);
  319. #endif
  320. return tmp;
  321. }
  322. /**
  323. * Read 1-25 bits.
  324. */
  325. static inline unsigned int get_bits(GetBitContext *s, int n)
  326. {
  327. register unsigned int tmp;
  328. #if CACHED_BITSTREAM_READER
  329. av_assert2(n>0 && n<=32);
  330. if (n > s->bits_left) {
  331. #ifdef BITSTREAM_READER_LE
  332. refill_32(s, 1);
  333. #else
  334. refill_32(s, 0);
  335. #endif
  336. if (s->bits_left < 32)
  337. s->bits_left = n;
  338. }
  339. #ifdef BITSTREAM_READER_LE
  340. tmp = get_val(s, n, 1);
  341. #else
  342. tmp = get_val(s, n, 0);
  343. #endif
  344. #else
  345. OPEN_READER(re, s);
  346. av_assert2(n>0 && n<=25);
  347. UPDATE_CACHE(re, s);
  348. tmp = SHOW_UBITS(re, s, n);
  349. LAST_SKIP_BITS(re, s, n);
  350. CLOSE_READER(re, s);
  351. #endif
  352. av_assert2(tmp < UINT64_C(1) << n);
  353. return tmp;
  354. }
  355. /**
  356. * Read 0-25 bits.
  357. */
  358. static av_always_inline int get_bitsz(GetBitContext *s, int n)
  359. {
  360. return n ? get_bits(s, n) : 0;
  361. }
  362. static inline unsigned int get_bits_le(GetBitContext *s, int n)
  363. {
  364. #if CACHED_BITSTREAM_READER
  365. av_assert2(n>0 && n<=32);
  366. if (n > s->bits_left) {
  367. refill_32(s, 1);
  368. if (s->bits_left < 32)
  369. s->bits_left = n;
  370. }
  371. return get_val(s, n, 1);
  372. #else
  373. register int tmp;
  374. OPEN_READER(re, s);
  375. av_assert2(n>0 && n<=25);
  376. UPDATE_CACHE_LE(re, s);
  377. tmp = SHOW_UBITS_LE(re, s, n);
  378. LAST_SKIP_BITS(re, s, n);
  379. CLOSE_READER(re, s);
  380. return tmp;
  381. #endif
  382. }
  383. /**
  384. * Show 1-25 bits.
  385. */
  386. static inline unsigned int show_bits(GetBitContext *s, int n)
  387. {
  388. register unsigned int tmp;
  389. #if CACHED_BITSTREAM_READER
  390. if (n > s->bits_left)
  391. #ifdef BITSTREAM_READER_LE
  392. refill_32(s, 1);
  393. #else
  394. refill_32(s, 0);
  395. #endif
  396. tmp = show_val(s, n);
  397. #else
  398. OPEN_READER_NOSIZE(re, s);
  399. av_assert2(n>0 && n<=25);
  400. UPDATE_CACHE(re, s);
  401. tmp = SHOW_UBITS(re, s, n);
  402. #endif
  403. return tmp;
  404. }
  405. static inline void skip_bits(GetBitContext *s, int n)
  406. {
  407. #if CACHED_BITSTREAM_READER
  408. if (n < s->bits_left)
  409. skip_remaining(s, n);
  410. else {
  411. n -= s->bits_left;
  412. s->cache = 0;
  413. s->bits_left = 0;
  414. if (n >= 64) {
  415. unsigned skip = (n / 8) * 8;
  416. n -= skip;
  417. s->index += skip;
  418. }
  419. #ifdef BITSTREAM_READER_LE
  420. refill_64(s, 1);
  421. #else
  422. refill_64(s, 0);
  423. #endif
  424. if (n)
  425. skip_remaining(s, n);
  426. }
  427. #else
  428. OPEN_READER(re, s);
  429. LAST_SKIP_BITS(re, s, n);
  430. CLOSE_READER(re, s);
  431. #endif
  432. }
  433. static inline unsigned int get_bits1(GetBitContext *s)
  434. {
  435. #if CACHED_BITSTREAM_READER
  436. if (!s->bits_left)
  437. #ifdef BITSTREAM_READER_LE
  438. refill_64(s, 1);
  439. #else
  440. refill_64(s, 0);
  441. #endif
  442. #ifdef BITSTREAM_READER_LE
  443. return get_val(s, 1, 1);
  444. #else
  445. return get_val(s, 1, 0);
  446. #endif
  447. #else
  448. unsigned int index = s->index;
  449. uint8_t result = s->buffer[index >> 3];
  450. #ifdef BITSTREAM_READER_LE
  451. result >>= index & 7;
  452. result &= 1;
  453. #else
  454. result <<= index & 7;
  455. result >>= 8 - 1;
  456. #endif
  457. #if !UNCHECKED_BITSTREAM_READER
  458. if (s->index < s->size_in_bits_plus8)
  459. #endif
  460. index++;
  461. s->index = index;
  462. return result;
  463. #endif
  464. }
  465. static inline unsigned int show_bits1(GetBitContext *s)
  466. {
  467. return show_bits(s, 1);
  468. }
  469. static inline void skip_bits1(GetBitContext *s)
  470. {
  471. skip_bits(s, 1);
  472. }
  473. /**
  474. * Read 0-32 bits.
  475. */
  476. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  477. {
  478. av_assert2(n>=0 && n<=32);
  479. if (!n) {
  480. return 0;
  481. #if CACHED_BITSTREAM_READER
  482. }
  483. return get_bits(s, n);
  484. #else
  485. } else if (n <= MIN_CACHE_BITS) {
  486. return get_bits(s, n);
  487. } else {
  488. #ifdef BITSTREAM_READER_LE
  489. unsigned ret = get_bits(s, 16);
  490. return ret | (get_bits(s, n - 16) << 16);
  491. #else
  492. unsigned ret = get_bits(s, 16) << (n - 16);
  493. return ret | get_bits(s, n - 16);
  494. #endif
  495. }
  496. #endif
  497. }
  498. /**
  499. * Read 0-64 bits.
  500. */
  501. static inline uint64_t get_bits64(GetBitContext *s, int n)
  502. {
  503. if (n <= 32) {
  504. return get_bits_long(s, n);
  505. } else {
  506. #ifdef BITSTREAM_READER_LE
  507. uint64_t ret = get_bits_long(s, 32);
  508. return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
  509. #else
  510. uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
  511. return ret | get_bits_long(s, 32);
  512. #endif
  513. }
  514. }
  515. /**
  516. * Read 0-32 bits as a signed integer.
  517. */
  518. static inline int get_sbits_long(GetBitContext *s, int n)
  519. {
  520. // sign_extend(x, 0) is undefined
  521. if (!n)
  522. return 0;
  523. return sign_extend(get_bits_long(s, n), n);
  524. }
  525. /**
  526. * Show 0-32 bits.
  527. */
  528. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  529. {
  530. if (n <= MIN_CACHE_BITS) {
  531. return show_bits(s, n);
  532. } else {
  533. GetBitContext gb = *s;
  534. return get_bits_long(&gb, n);
  535. }
  536. }
  537. static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
  538. {
  539. int bit = get_bits1(s);
  540. if (!bit)
  541. av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
  542. get_bits_count(s) - 1, s->size_in_bits, msg);
  543. return bit;
  544. }
  545. static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer,
  546. int bit_size, int is_le)
  547. {
  548. int buffer_size;
  549. int ret = 0;
  550. if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
  551. bit_size = 0;
  552. buffer = NULL;
  553. ret = AVERROR_INVALIDDATA;
  554. }
  555. buffer_size = (bit_size + 7) >> 3;
  556. s->buffer = buffer;
  557. s->size_in_bits = bit_size;
  558. s->size_in_bits_plus8 = bit_size + 8;
  559. s->buffer_end = buffer + buffer_size;
  560. s->index = 0;
  561. #if CACHED_BITSTREAM_READER
  562. s->cache = 0;
  563. s->bits_left = 0;
  564. refill_64(s, is_le);
  565. #endif
  566. return ret;
  567. }
  568. /**
  569. * Initialize GetBitContext.
  570. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  571. * larger than the actual read bits because some optimized bitstream
  572. * readers read 32 or 64 bit at once and could read over the end
  573. * @param bit_size the size of the buffer in bits
  574. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  575. */
  576. static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
  577. int bit_size)
  578. {
  579. #ifdef BITSTREAM_READER_LE
  580. return init_get_bits_xe(s, buffer, bit_size, 1);
  581. #else
  582. return init_get_bits_xe(s, buffer, bit_size, 0);
  583. #endif
  584. }
  585. /**
  586. * Initialize GetBitContext.
  587. * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
  588. * larger than the actual read bits because some optimized bitstream
  589. * readers read 32 or 64 bit at once and could read over the end
  590. * @param byte_size the size of the buffer in bytes
  591. * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
  592. */
  593. static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
  594. int byte_size)
  595. {
  596. if (byte_size > INT_MAX / 8 || byte_size < 0)
  597. byte_size = -1;
  598. return init_get_bits(s, buffer, byte_size * 8);
  599. }
  600. static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
  601. int byte_size)
  602. {
  603. if (byte_size > INT_MAX / 8 || byte_size < 0)
  604. byte_size = -1;
  605. return init_get_bits_xe(s, buffer, byte_size * 8, 1);
  606. }
  607. static inline const uint8_t *align_get_bits(GetBitContext *s)
  608. {
  609. int n = -get_bits_count(s) & 7;
  610. if (n)
  611. skip_bits(s, n);
  612. return s->buffer + (s->index >> 3);
  613. }
  614. /**
  615. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  616. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  617. * is undefined.
  618. */
  619. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  620. do { \
  621. int n, nb_bits; \
  622. unsigned int index; \
  623. \
  624. index = SHOW_UBITS(name, gb, bits); \
  625. code = table[index][0]; \
  626. n = table[index][1]; \
  627. \
  628. if (max_depth > 1 && n < 0) { \
  629. LAST_SKIP_BITS(name, gb, bits); \
  630. UPDATE_CACHE(name, gb); \
  631. \
  632. nb_bits = -n; \
  633. \
  634. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  635. code = table[index][0]; \
  636. n = table[index][1]; \
  637. if (max_depth > 2 && n < 0) { \
  638. LAST_SKIP_BITS(name, gb, nb_bits); \
  639. UPDATE_CACHE(name, gb); \
  640. \
  641. nb_bits = -n; \
  642. \
  643. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  644. code = table[index][0]; \
  645. n = table[index][1]; \
  646. } \
  647. } \
  648. SKIP_BITS(name, gb, n); \
  649. } while (0)
  650. #define GET_RL_VLC(level, run, name, gb, table, bits, \
  651. max_depth, need_update) \
  652. do { \
  653. int n, nb_bits; \
  654. unsigned int index; \
  655. \
  656. index = SHOW_UBITS(name, gb, bits); \
  657. level = table[index].level; \
  658. n = table[index].len; \
  659. \
  660. if (max_depth > 1 && n < 0) { \
  661. SKIP_BITS(name, gb, bits); \
  662. if (need_update) { \
  663. UPDATE_CACHE(name, gb); \
  664. } \
  665. \
  666. nb_bits = -n; \
  667. \
  668. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  669. level = table[index].level; \
  670. n = table[index].len; \
  671. if (max_depth > 2 && n < 0) { \
  672. LAST_SKIP_BITS(name, gb, nb_bits); \
  673. if (need_update) { \
  674. UPDATE_CACHE(name, gb); \
  675. } \
  676. nb_bits = -n; \
  677. \
  678. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  679. level = table[index].level; \
  680. n = table[index].len; \
  681. } \
  682. } \
  683. run = table[index].run; \
  684. SKIP_BITS(name, gb, n); \
  685. } while (0)
  686. /* Return the LUT element for the given bitstream configuration. */
  687. static inline int set_idx(GetBitContext *s, int code, int *n, int *nb_bits,
  688. VLC_TYPE (*table)[2])
  689. {
  690. unsigned idx;
  691. *nb_bits = -*n;
  692. idx = show_bits(s, *nb_bits) + code;
  693. *n = table[idx][1];
  694. return table[idx][0];
  695. }
  696. /**
  697. * Parse a vlc code.
  698. * @param bits is the number of bits which will be read at once, must be
  699. * identical to nb_bits in init_vlc()
  700. * @param max_depth is the number of times bits bits must be read to completely
  701. * read the longest vlc code
  702. * = (max_vlc_length + bits - 1) / bits
  703. * @returns the code parsed or -1 if no vlc matches
  704. */
  705. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  706. int bits, int max_depth)
  707. {
  708. #if CACHED_BITSTREAM_READER
  709. int nb_bits;
  710. unsigned idx = show_bits(s, bits);
  711. int code = table[idx][0];
  712. int n = table[idx][1];
  713. if (max_depth > 1 && n < 0) {
  714. skip_remaining(s, bits);
  715. code = set_idx(s, code, &n, &nb_bits, table);
  716. if (max_depth > 2 && n < 0) {
  717. skip_remaining(s, nb_bits);
  718. code = set_idx(s, code, &n, &nb_bits, table);
  719. }
  720. }
  721. skip_remaining(s, n);
  722. return code;
  723. #else
  724. int code;
  725. OPEN_READER(re, s);
  726. UPDATE_CACHE(re, s);
  727. GET_VLC(code, re, s, table, bits, max_depth);
  728. CLOSE_READER(re, s);
  729. return code;
  730. #endif
  731. }
  732. static inline int decode012(GetBitContext *gb)
  733. {
  734. int n;
  735. n = get_bits1(gb);
  736. if (n == 0)
  737. return 0;
  738. else
  739. return get_bits1(gb) + 1;
  740. }
  741. static inline int decode210(GetBitContext *gb)
  742. {
  743. if (get_bits1(gb))
  744. return 0;
  745. else
  746. return 2 - get_bits1(gb);
  747. }
  748. static inline int get_bits_left(GetBitContext *gb)
  749. {
  750. return gb->size_in_bits - get_bits_count(gb);
  751. }
  752. static inline int skip_1stop_8data_bits(GetBitContext *gb)
  753. {
  754. if (get_bits_left(gb) <= 0)
  755. return AVERROR_INVALIDDATA;
  756. while (get_bits1(gb)) {
  757. skip_bits(gb, 8);
  758. if (get_bits_left(gb) <= 0)
  759. return AVERROR_INVALIDDATA;
  760. }
  761. return 0;
  762. }
  763. #endif /* AVCODEC_GET_BITS_H */