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.

550 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 <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. /*
  35. * Safe bitstream reading:
  36. * optionally, the get_bits API can check to ensure that we
  37. * don't read past input buffer boundaries. This is protected
  38. * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
  39. * then below that with UNCHECKED_BITSTREAM_READER at the per-
  40. * decoder level. This means that decoders that check internally
  41. * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
  42. * overread checks.
  43. * Boundary checking causes a minor performance penalty so for
  44. * applications that won't want/need this, it can be disabled
  45. * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
  46. */
  47. #ifndef UNCHECKED_BITSTREAM_READER
  48. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  49. #endif
  50. /* bit input */
  51. /* buffer, buffer_end and size_in_bits must be present and used by every reader */
  52. typedef struct GetBitContext {
  53. const uint8_t *buffer, *buffer_end;
  54. int index;
  55. int size_in_bits;
  56. int size_in_bits_plus8;
  57. } GetBitContext;
  58. #define VLC_TYPE int16_t
  59. typedef struct VLC {
  60. int bits;
  61. VLC_TYPE (*table)[2]; ///< code, bits
  62. int table_size, table_allocated;
  63. } VLC;
  64. typedef struct RL_VLC_ELEM {
  65. int16_t level;
  66. int8_t len;
  67. uint8_t run;
  68. } RL_VLC_ELEM;
  69. /* Bitstream reader API docs:
  70. name
  71. arbitrary name which is used as prefix for the internal variables
  72. gb
  73. getbitcontext
  74. OPEN_READER(name, gb)
  75. loads gb into local variables
  76. CLOSE_READER(name, gb)
  77. stores local vars in gb
  78. UPDATE_CACHE(name, gb)
  79. refills the internal cache from the bitstream
  80. after this call at least MIN_CACHE_BITS will be available,
  81. GET_CACHE(name, gb)
  82. will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
  83. SHOW_UBITS(name, gb, num)
  84. will return the next num bits
  85. SHOW_SBITS(name, gb, num)
  86. will return the next num bits and do sign extension
  87. SKIP_BITS(name, gb, num)
  88. will skip over the next num bits
  89. note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
  90. SKIP_CACHE(name, gb, num)
  91. will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
  92. SKIP_COUNTER(name, gb, num)
  93. will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
  94. LAST_SKIP_CACHE(name, gb, num)
  95. will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
  96. LAST_SKIP_BITS(name, gb, num)
  97. is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
  98. for examples see get_bits, show_bits, skip_bits, get_vlc
  99. */
  100. #ifdef LONG_BITSTREAM_READER
  101. # define MIN_CACHE_BITS 32
  102. #else
  103. # define MIN_CACHE_BITS 25
  104. #endif
  105. # define OPEN_READER(name, gb) \
  106. unsigned int name##_index = (gb)->index; \
  107. av_unused unsigned int name##_cache
  108. # define CLOSE_READER(name, gb) (gb)->index = name##_index
  109. # ifdef ALT_BITSTREAM_READER_LE
  110. # ifdef LONG_BITSTREAM_READER
  111. # define UPDATE_CACHE(name, gb) \
  112. name##_cache = AV_RL64((gb)->buffer+(name##_index>>3)) >> (name##_index&0x07)
  113. # else
  114. # define UPDATE_CACHE(name, gb) \
  115. name##_cache = AV_RL32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) >> (name##_index&0x07)
  116. # endif
  117. # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
  118. # else
  119. # ifdef LONG_BITSTREAM_READER
  120. # define UPDATE_CACHE(name, gb) \
  121. name##_cache = AV_RB64((gb)->buffer+(name##_index >> 3)) >> (32 - (name##_index & 0x07))
  122. # else
  123. # define UPDATE_CACHE(name, gb) \
  124. name##_cache = AV_RB32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) << (name##_index&0x07)
  125. # endif
  126. # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
  127. # endif
  128. // FIXME name?
  129. #if UNCHECKED_BITSTREAM_READER
  130. # define SKIP_COUNTER(name, gb, num) name##_index += (num)
  131. #else
  132. # define SKIP_COUNTER(name, gb, num) \
  133. name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
  134. #endif
  135. # define SKIP_BITS(name, gb, num) do { \
  136. SKIP_CACHE(name, gb, num); \
  137. SKIP_COUNTER(name, gb, num); \
  138. } while (0)
  139. # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  140. # define LAST_SKIP_CACHE(name, gb, num)
  141. # ifdef ALT_BITSTREAM_READER_LE
  142. # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
  143. # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
  144. # else
  145. # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
  146. # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
  147. # endif
  148. # define GET_CACHE(name, gb) ((uint32_t)name##_cache)
  149. static inline int get_bits_count(const GetBitContext *s){
  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. register int sign;
  167. register int32_t cache;
  168. OPEN_READER(re, s);
  169. UPDATE_CACHE(re, s);
  170. cache = GET_CACHE(re, s);
  171. sign = ~cache >> 31;
  172. LAST_SKIP_BITS(re, s, n);
  173. CLOSE_READER(re, s);
  174. return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
  175. }
  176. static inline int get_sbits(GetBitContext *s, int n){
  177. register int tmp;
  178. OPEN_READER(re, s);
  179. UPDATE_CACHE(re, s);
  180. tmp = SHOW_SBITS(re, s, n);
  181. LAST_SKIP_BITS(re, s, n);
  182. CLOSE_READER(re, s);
  183. return tmp;
  184. }
  185. /**
  186. * Read 1-25 bits.
  187. */
  188. static inline unsigned int get_bits(GetBitContext *s, int n){
  189. register int tmp;
  190. OPEN_READER(re, s);
  191. UPDATE_CACHE(re, s);
  192. tmp = SHOW_UBITS(re, s, n);
  193. LAST_SKIP_BITS(re, s, n);
  194. CLOSE_READER(re, s);
  195. return tmp;
  196. }
  197. /**
  198. * Shows 1-25 bits.
  199. */
  200. static inline unsigned int show_bits(GetBitContext *s, int n){
  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. //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
  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. unsigned int index = s->index;
  216. uint8_t result = s->buffer[index>>3];
  217. #ifdef ALT_BITSTREAM_READER_LE
  218. result >>= index & 7;
  219. result &= 1;
  220. #else
  221. result <<= index & 7;
  222. result >>= 8 - 1;
  223. #endif
  224. #if !UNCHECKED_BITSTREAM_READER
  225. if (s->index < s->size_in_bits_plus8)
  226. #endif
  227. index++;
  228. s->index = index;
  229. return result;
  230. }
  231. static inline unsigned int show_bits1(GetBitContext *s){
  232. return show_bits(s, 1);
  233. }
  234. static inline void skip_bits1(GetBitContext *s){
  235. skip_bits(s, 1);
  236. }
  237. /**
  238. * reads 0-32 bits.
  239. */
  240. static inline unsigned int get_bits_long(GetBitContext *s, int n){
  241. if (n <= MIN_CACHE_BITS) return get_bits(s, n);
  242. else {
  243. #ifdef ALT_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. * reads 0-32 bits as a signed integer.
  254. */
  255. static inline int get_sbits_long(GetBitContext *s, int n) {
  256. return sign_extend(get_bits_long(s, n), n);
  257. }
  258. /**
  259. * shows 0-32 bits.
  260. */
  261. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  262. if (n <= MIN_CACHE_BITS) return show_bits(s, n);
  263. else {
  264. GetBitContext gb = *s;
  265. return get_bits_long(&gb, n);
  266. }
  267. }
  268. static inline int check_marker(GetBitContext *s, const char *msg)
  269. {
  270. int bit = get_bits1(s);
  271. if (!bit)
  272. av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
  273. return bit;
  274. }
  275. /**
  276. * init GetBitContext.
  277. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
  278. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  279. * @param bit_size the size of the buffer in bits
  280. *
  281. * While GetBitContext stores the buffer size, for performance reasons you are
  282. * responsible for checking for the buffer end yourself (take advantage of the padding)!
  283. */
  284. static inline void init_get_bits(GetBitContext *s,
  285. const uint8_t *buffer, 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. *
  327. * If the vlc code is invalid and max_depth=1, then no bits will be removed.
  328. * If the vlc code is invalid and max_depth>1, then the number of bits removed
  329. * is undefined.
  330. */
  331. #define GET_VLC(code, name, gb, table, bits, max_depth) 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) do { \
  362. int n, nb_bits; \
  363. unsigned int index; \
  364. \
  365. index = SHOW_UBITS(name, gb, bits); \
  366. level = table[index].level; \
  367. n = table[index].len; \
  368. \
  369. if (max_depth > 1 && n < 0) { \
  370. SKIP_BITS(name, gb, bits); \
  371. if (need_update) { \
  372. UPDATE_CACHE(name, gb); \
  373. } \
  374. \
  375. nb_bits = -n; \
  376. \
  377. index = SHOW_UBITS(name, gb, nb_bits) + level; \
  378. level = table[index].level; \
  379. n = table[index].len; \
  380. } \
  381. run = table[index].run; \
  382. SKIP_BITS(name, gb, n); \
  383. } while (0)
  384. /**
  385. * parses a vlc code, faster than get_vlc()
  386. * @param bits is the number of bits which will be read at once, must be
  387. * identical to nb_bits in init_vlc()
  388. * @param max_depth is the number of times bits bits must be read to completely
  389. * read the longest vlc code
  390. * = (max_vlc_length + bits - 1) / bits
  391. */
  392. static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
  393. int bits, int max_depth)
  394. {
  395. int code;
  396. OPEN_READER(re, s);
  397. UPDATE_CACHE(re, s);
  398. GET_VLC(code, re, s, table, bits, max_depth);
  399. CLOSE_READER(re, s);
  400. return code;
  401. }
  402. static inline int decode012(GetBitContext *gb){
  403. int n;
  404. n = get_bits1(gb);
  405. if (n == 0)
  406. return 0;
  407. else
  408. return get_bits1(gb) + 1;
  409. }
  410. static inline int decode210(GetBitContext *gb){
  411. if (get_bits1(gb))
  412. return 0;
  413. else
  414. return 2 - get_bits1(gb);
  415. }
  416. static inline int get_bits_left(GetBitContext *gb)
  417. {
  418. return gb->size_in_bits - get_bits_count(gb);
  419. }
  420. //#define TRACE
  421. #ifdef TRACE
  422. static inline void print_bin(int bits, int n){
  423. int i;
  424. for (i = n-1; i >= 0; i--) {
  425. av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
  426. }
  427. for (i = n; i < 24; i++)
  428. av_log(NULL, AV_LOG_DEBUG, " ");
  429. }
  430. static inline int get_bits_trace(GetBitContext *s, int n, char *file,
  431. const char *func, int line){
  432. int r = get_bits(s, n);
  433. print_bin(r, n);
  434. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
  435. r, n, r, get_bits_count(s)-n, file, func, line);
  436. return r;
  437. }
  438. static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
  439. int bits, int max_depth, char *file,
  440. const char *func, int line){
  441. int show = show_bits(s, 24);
  442. int pos = get_bits_count(s);
  443. int r = get_vlc2(s, table, bits, max_depth);
  444. int len = get_bits_count(s) - pos;
  445. int bits2 = show >> (24-len);
  446. print_bin(bits2, len);
  447. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
  448. bits2, len, r, pos, file, func, line);
  449. return r;
  450. }
  451. static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
  452. const char *func, int line){
  453. int show = show_bits(s, n);
  454. int r = get_xbits(s, n);
  455. print_bin(show, n);
  456. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
  457. show, n, r, get_bits_count(s)-n, file, func, line);
  458. return r;
  459. }
  460. #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  461. #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  462. #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  463. #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  464. #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  465. #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
  466. #else //TRACE
  467. #define tprintf(p, ...) {}
  468. #endif
  469. #endif /* AVCODEC_GET_BITS_H */