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.

610 lines
20KB

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