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.

574 lines
18KB

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