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.

562 lines
17KB

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