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.

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