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.

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