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.

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