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.

595 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. /*
  33. * Safe bitstream reading:
  34. * optionally, the get_bits API can check to ensure that we
  35. * don't read past input buffer boundaries. This is protected
  36. * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
  37. * then below that with UNCHECKED_BITSTREAM_READER at the per-
  38. * decoder level. This means that decoders that check internally
  39. * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
  40. * overread checks.
  41. * Boundary checking causes a minor performance penalty so for
  42. * applications that won't want/need this, it can be disabled
  43. * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
  44. */
  45. #ifndef UNCHECKED_BITSTREAM_READER
  46. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  47. #endif
  48. typedef struct GetBitContext {
  49. const uint8_t *buffer, *buffer_end;
  50. int index;
  51. int size_in_bits;
  52. int size_in_bits_plus8;
  53. } GetBitContext;
  54. #define VLC_TYPE int16_t
  55. typedef struct VLC {
  56. int bits;
  57. VLC_TYPE (*table)[2]; ///< code, bits
  58. int table_size, table_allocated;
  59. } VLC;
  60. typedef struct RL_VLC_ELEM {
  61. int16_t level;
  62. int8_t len;
  63. uint8_t run;
  64. } RL_VLC_ELEM;
  65. /* Bitstream reader API docs:
  66. name
  67. arbitrary name which is used as prefix for the internal variables
  68. gb
  69. getbitcontext
  70. OPEN_READER(name, gb)
  71. load gb into local variables
  72. CLOSE_READER(name, gb)
  73. store local vars in gb
  74. UPDATE_CACHE(name, gb)
  75. refill the internal cache from the bitstream
  76. after this call at least MIN_CACHE_BITS will be available,
  77. GET_CACHE(name, gb)
  78. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  79. SHOW_UBITS(name, gb, num)
  80. will return the next num bits
  81. SHOW_SBITS(name, gb, num)
  82. will return the next num bits and do sign extension
  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. SKIP_CACHE(name, gb, num)
  87. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  88. SKIP_COUNTER(name, gb, num)
  89. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  90. LAST_SKIP_BITS(name, gb, num)
  91. like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER
  92. for examples see get_bits, show_bits, skip_bits, get_vlc
  93. */
  94. #ifdef LONG_BITSTREAM_READER
  95. # define MIN_CACHE_BITS 32
  96. #else
  97. # define MIN_CACHE_BITS 25
  98. #endif
  99. #if UNCHECKED_BITSTREAM_READER
  100. #define OPEN_READER(name, gb) \
  101. unsigned int name##_index = (gb)->index; \
  102. av_unused unsigned int name##_cache
  103. #define HAVE_BITS_REMAINING(name, gb) 1
  104. #else
  105. #define OPEN_READER(name, gb) \
  106. unsigned int name##_index = (gb)->index; \
  107. unsigned int av_unused name##_cache = 0; \
  108. unsigned int av_unused name##_size_plus8 = \
  109. (gb)->size_in_bits_plus8
  110. #define HAVE_BITS_REMAINING(name, gb) \
  111. name##_index < name##_size_plus8
  112. #endif
  113. #define CLOSE_READER(name, gb) (gb)->index = name##_index
  114. #ifdef BITSTREAM_READER_LE
  115. # ifdef LONG_BITSTREAM_READER
  116. # define UPDATE_CACHE(name, gb) name##_cache = \
  117. AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
  118. # else
  119. # define UPDATE_CACHE(name, gb) name##_cache = \
  120. AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
  121. # endif
  122. # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
  123. #else
  124. # ifdef LONG_BITSTREAM_READER
  125. # define UPDATE_CACHE(name, gb) name##_cache = \
  126. AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
  127. # else
  128. # define UPDATE_CACHE(name, gb) name##_cache = \
  129. AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
  130. # endif
  131. # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
  132. #endif
  133. #if UNCHECKED_BITSTREAM_READER
  134. # define SKIP_COUNTER(name, gb, num) name##_index += (num)
  135. #else
  136. # define SKIP_COUNTER(name, gb, num) \
  137. name##_index = FFMIN(name##_size_plus8, name##_index + (num))
  138. #endif
  139. #define SKIP_BITS(name, gb, num) do { \
  140. SKIP_CACHE(name, gb, num); \
  141. SKIP_COUNTER(name, gb, num); \
  142. } while (0)
  143. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  144. #ifdef BITSTREAM_READER_LE
  145. # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
  146. # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
  147. #else
  148. # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
  149. # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
  150. #endif
  151. #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
  152. static inline int get_bits_count(const GetBitContext *s)
  153. {
  154. return s->index;
  155. }
  156. static inline void skip_bits_long(GetBitContext *s, int n){
  157. #if UNCHECKED_BITSTREAM_READER
  158. s->index += n;
  159. #else
  160. s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
  161. #endif
  162. }
  163. /**
  164. * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
  165. * if MSB not set it is negative
  166. * @param n length in bits
  167. */
  168. static inline int get_xbits(GetBitContext *s, int n)
  169. {
  170. register int sign;
  171. register int32_t cache;
  172. OPEN_READER(re, s);
  173. UPDATE_CACHE(re, s);
  174. cache = GET_CACHE(re, s);
  175. sign = ~cache >> 31;
  176. LAST_SKIP_BITS(re, s, n);
  177. CLOSE_READER(re, s);
  178. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  179. }
  180. static inline int get_sbits(GetBitContext *s, int n)
  181. {
  182. register int tmp;
  183. OPEN_READER(re, s);
  184. av_assert2(n>0 && n<=25);
  185. UPDATE_CACHE(re, s);
  186. tmp = SHOW_SBITS(re, s, n);
  187. LAST_SKIP_BITS(re, s, n);
  188. CLOSE_READER(re, s);
  189. return tmp;
  190. }
  191. /**
  192. * Read 1-25 bits.
  193. */
  194. static inline unsigned int get_bits(GetBitContext *s, int n)
  195. {
  196. register int tmp;
  197. OPEN_READER(re, s);
  198. av_assert2(n>0 && n<=25);
  199. UPDATE_CACHE(re, s);
  200. tmp = SHOW_UBITS(re, s, n);
  201. LAST_SKIP_BITS(re, s, n);
  202. CLOSE_READER(re, s);
  203. return tmp;
  204. }
  205. /**
  206. * Show 1-25 bits.
  207. */
  208. static inline unsigned int show_bits(GetBitContext *s, int n)
  209. {
  210. register int tmp;
  211. OPEN_READER(re, s);
  212. av_assert2(n>0 && n<=25);
  213. UPDATE_CACHE(re, s);
  214. tmp = SHOW_UBITS(re, s, n);
  215. return tmp;
  216. }
  217. static inline void skip_bits(GetBitContext *s, int n)
  218. {
  219. OPEN_READER(re, s);
  220. UPDATE_CACHE(re, s);
  221. LAST_SKIP_BITS(re, s, n);
  222. CLOSE_READER(re, s);
  223. }
  224. static inline unsigned int get_bits1(GetBitContext *s)
  225. {
  226. unsigned int index = s->index;
  227. uint8_t result = s->buffer[index>>3];
  228. #ifdef BITSTREAM_READER_LE
  229. result >>= index & 7;
  230. result &= 1;
  231. #else
  232. result <<= index & 7;
  233. result >>= 8 - 1;
  234. #endif
  235. #if !UNCHECKED_BITSTREAM_READER
  236. if (s->index < s->size_in_bits_plus8)
  237. #endif
  238. index++;
  239. s->index = index;
  240. return result;
  241. }
  242. static inline unsigned int show_bits1(GetBitContext *s)
  243. {
  244. return show_bits(s, 1);
  245. }
  246. static inline void skip_bits1(GetBitContext *s)
  247. {
  248. skip_bits(s, 1);
  249. }
  250. /**
  251. * Read 0-32 bits.
  252. */
  253. static inline unsigned int get_bits_long(GetBitContext *s, int n)
  254. {
  255. if (!n) {
  256. return 0;
  257. } else if (n <= MIN_CACHE_BITS)
  258. return get_bits(s, n);
  259. else {
  260. #ifdef BITSTREAM_READER_LE
  261. unsigned ret = get_bits(s, 16);
  262. return ret | (get_bits(s, n-16) << 16);
  263. #else
  264. unsigned ret = get_bits(s, 16) << (n-16);
  265. return ret | get_bits(s, n-16);
  266. #endif
  267. }
  268. }
  269. /**
  270. * Read 0-64 bits.
  271. */
  272. static inline uint64_t get_bits_longlong(GetBitContext *s, int n)
  273. {
  274. if (n <= 32)
  275. return get_bits_long(s, n);
  276. else {
  277. #ifdef BITSTREAM_READER_LE
  278. uint64_t ret = get_bits_long(s, 32);
  279. return ret | (((uint64_t)get_bits_long(s, n-32)) << 32);
  280. #else
  281. uint64_t ret = ((uint64_t)get_bits_long(s, 32)) << (n-32);
  282. return ret | get_bits_long(s, n-32);
  283. #endif
  284. }
  285. }
  286. /**
  287. * Read 0-32 bits as a signed integer.
  288. */
  289. static inline int get_sbits_long(GetBitContext *s, int n)
  290. {
  291. return sign_extend(get_bits_long(s, n), n);
  292. }
  293. /**
  294. * Show 0-32 bits.
  295. */
  296. static inline unsigned int show_bits_long(GetBitContext *s, int n)
  297. {
  298. if (n <= MIN_CACHE_BITS)
  299. return show_bits(s, n);
  300. else {
  301. GetBitContext gb = *s;
  302. return get_bits_long(&gb, n);
  303. }
  304. }
  305. static inline int check_marker(GetBitContext *s, const char *msg)
  306. {
  307. int bit = get_bits1(s);
  308. if (!bit)
  309. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  310. return bit;
  311. }
  312. /**
  313. * Inititalize GetBitContext.
  314. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
  315. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  316. * @param bit_size the size of the buffer in bits
  317. */
  318. static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
  319. int bit_size)
  320. {
  321. int buffer_size = (bit_size+7)>>3;
  322. if (buffer_size < 0 || bit_size < 0) {
  323. buffer_size = bit_size = 0;
  324. buffer = NULL;
  325. }
  326. s->buffer = buffer;
  327. s->size_in_bits = bit_size;
  328. s->size_in_bits_plus8 = bit_size + 8;
  329. s->buffer_end = buffer + buffer_size;
  330. s->index = 0;
  331. }
  332. static inline void align_get_bits(GetBitContext *s)
  333. {
  334. int n = -get_bits_count(s) & 7;
  335. if (n) skip_bits(s, n);
  336. }
  337. #define init_vlc(vlc, nb_bits, nb_codes, \
  338. bits, bits_wrap, bits_size, \
  339. codes, codes_wrap, codes_size, \
  340. flags) \
  341. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  342. bits, bits_wrap, bits_size, \
  343. codes, codes_wrap, codes_size, \
  344. NULL, 0, 0, flags)
  345. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  346. const void *bits, int bits_wrap, int bits_size,
  347. const void *codes, int codes_wrap, int codes_size,
  348. const void *symbols, int symbols_wrap, int symbols_size,
  349. int flags);
  350. #define INIT_VLC_LE 2
  351. #define INIT_VLC_USE_NEW_STATIC 4
  352. void ff_free_vlc(VLC *vlc);
  353. #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
  354. static VLC_TYPE table[static_size][2]; \
  355. (vlc)->table = table; \
  356. (vlc)->table_allocated = static_size; \
  357. init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
  358. } while (0)
  359. /**
  360. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  361. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  362. * is undefined.
  363. */
  364. #define GET_VLC(code, name, gb, table, bits, max_depth) \
  365. do { \
  366. int n, nb_bits; \
  367. unsigned int index; \
  368. \
  369. index = SHOW_UBITS(name, gb, bits); \
  370. code = table[index][0]; \
  371. n = table[index][1]; \
  372. \
  373. if (max_depth > 1 && n < 0) { \
  374. LAST_SKIP_BITS(name, gb, bits); \
  375. UPDATE_CACHE(name, gb); \
  376. \
  377. nb_bits = -n; \
  378. \
  379. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  380. code = table[index][0]; \
  381. n = table[index][1]; \
  382. if (max_depth > 2 && n < 0) { \
  383. LAST_SKIP_BITS(name, gb, nb_bits); \
  384. UPDATE_CACHE(name, gb); \
  385. \
  386. nb_bits = -n; \
  387. \
  388. index = SHOW_UBITS(name, gb, nb_bits) + code; \
  389. code = table[index][0]; \
  390. n = table[index][1]; \
  391. } \
  392. } \
  393. SKIP_BITS(name, gb, n); \
  394. } while (0)
  395. #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
  396. do { \
  397. int n, nb_bits; \
  398. unsigned int index; \
  399. \
  400. index = SHOW_UBITS(name, gb, bits); \
  401. level = table[index].level; \
  402. n = table[index].len; \
  403. \
  404. if (max_depth > 1 && n < 0) { \
  405. SKIP_BITS(name, gb, bits); \
  406. if (need_update) { \
  407. UPDATE_CACHE(name, gb); \
  408. } \
  409. \
  410. nb_bits = -n; \
  411. \
  412. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  413. level = table[index].level; \
  414. n = table[index].len; \
  415. } \
  416. run = table[index].run; \
  417. SKIP_BITS(name, gb, n); \
  418. } while (0)
  419. /**
  420. * Parse a vlc code.
  421. * @param bits is the number of bits which will be read at once, must be
  422. * identical to nb_bits in init_vlc()
  423. * @param max_depth is the number of times bits bits must be read to completely
  424. * read the longest vlc code
  425. * = (max_vlc_length + bits - 1) / bits
  426. */
  427. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  428. int bits, int max_depth)
  429. {
  430. int code;
  431. OPEN_READER(re, s);
  432. UPDATE_CACHE(re, s);
  433. GET_VLC(code, re, s, table, bits, max_depth);
  434. CLOSE_READER(re, s);
  435. return code;
  436. }
  437. static inline int decode012(GetBitContext *gb)
  438. {
  439. int n;
  440. n = get_bits1(gb);
  441. if (n == 0)
  442. return 0;
  443. else
  444. return get_bits1(gb) + 1;
  445. }
  446. static inline int decode210(GetBitContext *gb)
  447. {
  448. if (get_bits1(gb))
  449. return 0;
  450. else
  451. return 2 - get_bits1(gb);
  452. }
  453. static inline int get_bits_left(GetBitContext *gb)
  454. {
  455. return gb->size_in_bits - get_bits_count(gb);
  456. }
  457. //#define TRACE
  458. #ifdef TRACE
  459. static inline void print_bin(int bits, int n)
  460. {
  461. int i;
  462. for (i = n-1; i >= 0; i--) {
  463. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  464. }
  465. for (i = n; i < 24; i++)
  466. av_log(NULL, AV_LOG_DEBUG, " ");
  467. }
  468. static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
  469. const char *func, int line)
  470. {
  471. int r = get_bits(s, n);
  472. print_bin(r, n);
  473. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  474. r, n, r, get_bits_count(s)-n, file, func, line);
  475. return r;
  476. }
  477. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  478. int bits, int max_depth, const char *file,
  479. const char *func, int line)
  480. {
  481. int show = show_bits(s, 24);
  482. int pos = get_bits_count(s);
  483. int r = get_vlc2(s, table, bits, max_depth);
  484. int len = get_bits_count(s) - pos;
  485. int bits2 = show >> (24-len);
  486. print_bin(bits2, len);
  487. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  488. bits2, len, r, pos, file, func, line);
  489. return r;
  490. }
  491. static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
  492. const char *func, int line)
  493. {
  494. int show = show_bits(s, n);
  495. int r = get_xbits(s, n);
  496. print_bin(show, n);
  497. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  498. show, n, r, get_bits_count(s)-n, file, func, line);
  499. return r;
  500. }
  501. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  502. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  503. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  504. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  505. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  506. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  507. #else //TRACE
  508. #define tprintf(p, ...) {}
  509. #endif
  510. #endif /* AVCODEC_GET_BITS_H */