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.

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