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.

558 lines
17KB

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