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.

518 lines
16KB

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