golomb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * exp golomb vlc stuff
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2004 Alex Beregszaszi
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/golomb.h
  24. * @brief
  25. * exp golomb vlc stuff
  26. * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
  27. */
  28. #ifndef AVCODEC_GOLOMB_H
  29. #define AVCODEC_GOLOMB_H
  30. #include <stdint.h>
  31. #include "bitstream.h"
  32. #define INVALID_VLC 0x80000000
  33. extern const uint8_t ff_golomb_vlc_len[512];
  34. extern const uint8_t ff_ue_golomb_vlc_code[512];
  35. extern const int8_t ff_se_golomb_vlc_code[512];
  36. extern const uint8_t ff_ue_golomb_len[256];
  37. extern const uint8_t ff_interleaved_golomb_vlc_len[256];
  38. extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
  39. extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
  40. extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
  41. /**
  42. * read unsigned exp golomb code.
  43. */
  44. static inline int get_ue_golomb(GetBitContext *gb){
  45. unsigned int buf;
  46. int log;
  47. OPEN_READER(re, gb);
  48. UPDATE_CACHE(re, gb);
  49. buf=GET_CACHE(re, gb);
  50. if(buf >= (1<<27)){
  51. buf >>= 32 - 9;
  52. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  53. CLOSE_READER(re, gb);
  54. return ff_ue_golomb_vlc_code[buf];
  55. }else{
  56. log= 2*av_log2(buf) - 31;
  57. buf>>= log;
  58. buf--;
  59. LAST_SKIP_BITS(re, gb, 32 - log);
  60. CLOSE_READER(re, gb);
  61. return buf;
  62. }
  63. }
  64. /**
  65. * read unsigned exp golomb code, constraint to a max of 31.
  66. * the return value is undefined if the stored value exceeds 31.
  67. */
  68. static inline int get_ue_golomb_31(GetBitContext *gb){
  69. unsigned int buf;
  70. OPEN_READER(re, gb);
  71. UPDATE_CACHE(re, gb);
  72. buf=GET_CACHE(re, gb);
  73. buf >>= 32 - 9;
  74. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  75. CLOSE_READER(re, gb);
  76. return ff_ue_golomb_vlc_code[buf];
  77. }
  78. static inline int svq3_get_ue_golomb(GetBitContext *gb){
  79. uint32_t buf;
  80. OPEN_READER(re, gb);
  81. UPDATE_CACHE(re, gb);
  82. buf=GET_CACHE(re, gb);
  83. if(buf&0xAA800000){
  84. buf >>= 32 - 8;
  85. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  86. CLOSE_READER(re, gb);
  87. return ff_interleaved_ue_golomb_vlc_code[buf];
  88. }else{
  89. int ret = 1;
  90. while (1) {
  91. buf >>= 32 - 8;
  92. LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  93. if (ff_interleaved_golomb_vlc_len[buf] != 9){
  94. ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  95. ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
  96. break;
  97. }
  98. ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  99. UPDATE_CACHE(re, gb);
  100. buf = GET_CACHE(re, gb);
  101. }
  102. CLOSE_READER(re, gb);
  103. return ret - 1;
  104. }
  105. }
  106. /**
  107. * read unsigned truncated exp golomb code.
  108. */
  109. static inline int get_te0_golomb(GetBitContext *gb, int range){
  110. assert(range >= 1);
  111. if(range==1) return 0;
  112. else if(range==2) return get_bits1(gb)^1;
  113. else return get_ue_golomb(gb);
  114. }
  115. /**
  116. * read unsigned truncated exp golomb code.
  117. */
  118. static inline int get_te_golomb(GetBitContext *gb, int range){
  119. assert(range >= 1);
  120. if(range==2) return get_bits1(gb)^1;
  121. else return get_ue_golomb(gb);
  122. }
  123. /**
  124. * read signed exp golomb code.
  125. */
  126. static inline int get_se_golomb(GetBitContext *gb){
  127. unsigned int buf;
  128. int log;
  129. OPEN_READER(re, gb);
  130. UPDATE_CACHE(re, gb);
  131. buf=GET_CACHE(re, gb);
  132. if(buf >= (1<<27)){
  133. buf >>= 32 - 9;
  134. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  135. CLOSE_READER(re, gb);
  136. return ff_se_golomb_vlc_code[buf];
  137. }else{
  138. log= 2*av_log2(buf) - 31;
  139. buf>>= log;
  140. LAST_SKIP_BITS(re, gb, 32 - log);
  141. CLOSE_READER(re, gb);
  142. if(buf&1) buf= -(buf>>1);
  143. else buf= (buf>>1);
  144. return buf;
  145. }
  146. }
  147. static inline int svq3_get_se_golomb(GetBitContext *gb){
  148. unsigned int buf;
  149. int log;
  150. OPEN_READER(re, gb);
  151. UPDATE_CACHE(re, gb);
  152. buf=GET_CACHE(re, gb);
  153. if(buf&0xAA800000){
  154. buf >>= 32 - 8;
  155. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  156. CLOSE_READER(re, gb);
  157. return ff_interleaved_se_golomb_vlc_code[buf];
  158. }else{
  159. LAST_SKIP_BITS(re, gb, 8);
  160. UPDATE_CACHE(re, gb);
  161. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  162. if((buf & 0xAAAAAAAA) == 0)
  163. return INVALID_VLC;
  164. for(log=31; (buf & 0x80000000) == 0; log--){
  165. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  166. }
  167. LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
  168. CLOSE_READER(re, gb);
  169. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  170. }
  171. }
  172. static inline int dirac_get_se_golomb(GetBitContext *gb){
  173. uint32_t buf;
  174. uint32_t ret;
  175. ret = svq3_get_ue_golomb(gb);
  176. if (ret) {
  177. OPEN_READER(re, gb);
  178. UPDATE_CACHE(re, gb);
  179. buf = SHOW_SBITS(re, gb, 1);
  180. LAST_SKIP_BITS(re, gb, 1);
  181. ret = (ret ^ buf) - buf;
  182. CLOSE_READER(re, gb);
  183. }
  184. return ret;
  185. }
  186. /**
  187. * read unsigned golomb rice code (ffv1).
  188. */
  189. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  190. unsigned int buf;
  191. int log;
  192. OPEN_READER(re, gb);
  193. UPDATE_CACHE(re, gb);
  194. buf=GET_CACHE(re, gb);
  195. log= av_log2(buf);
  196. if(log > 31-limit){
  197. buf >>= log - k;
  198. buf += (30-log)<<k;
  199. LAST_SKIP_BITS(re, gb, 32 + k - log);
  200. CLOSE_READER(re, gb);
  201. return buf;
  202. }else{
  203. buf >>= 32 - limit - esc_len;
  204. LAST_SKIP_BITS(re, gb, esc_len + limit);
  205. CLOSE_READER(re, gb);
  206. return buf + limit - 1;
  207. }
  208. }
  209. /**
  210. * read unsigned golomb rice code (jpegls).
  211. */
  212. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
  213. unsigned int buf;
  214. int log;
  215. OPEN_READER(re, gb);
  216. UPDATE_CACHE(re, gb);
  217. buf=GET_CACHE(re, gb);
  218. log= av_log2(buf);
  219. if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
  220. buf >>= log - k;
  221. buf += (30-log)<<k;
  222. LAST_SKIP_BITS(re, gb, 32 + k - log);
  223. CLOSE_READER(re, gb);
  224. return buf;
  225. }else{
  226. int i;
  227. for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
  228. LAST_SKIP_BITS(re, gb, 1);
  229. UPDATE_CACHE(re, gb);
  230. }
  231. SKIP_BITS(re, gb, 1);
  232. if(i < limit - 1){
  233. if(k){
  234. buf = SHOW_UBITS(re, gb, k);
  235. LAST_SKIP_BITS(re, gb, k);
  236. }else{
  237. buf=0;
  238. }
  239. CLOSE_READER(re, gb);
  240. return buf + (i<<k);
  241. }else if(i == limit - 1){
  242. buf = SHOW_UBITS(re, gb, esc_len);
  243. LAST_SKIP_BITS(re, gb, esc_len);
  244. CLOSE_READER(re, gb);
  245. return buf + 1;
  246. }else
  247. return -1;
  248. }
  249. }
  250. /**
  251. * read signed golomb rice code (ffv1).
  252. */
  253. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  254. int v= get_ur_golomb(gb, k, limit, esc_len);
  255. v++;
  256. if (v&1) return v>>1;
  257. else return -(v>>1);
  258. // return (v>>1) ^ -(v&1);
  259. }
  260. /**
  261. * read signed golomb rice code (flac).
  262. */
  263. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
  264. int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
  265. return (v>>1) ^ -(v&1);
  266. }
  267. /**
  268. * read unsigned golomb rice code (shorten).
  269. */
  270. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
  271. return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  272. }
  273. /**
  274. * read signed golomb rice code (shorten).
  275. */
  276. static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
  277. {
  278. int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  279. if (uvar & 1)
  280. return ~(uvar >> 1);
  281. else
  282. return uvar >> 1;
  283. }
  284. #ifdef TRACE
  285. static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
  286. int show= show_bits(s, 24);
  287. int pos= get_bits_count(s);
  288. int i= get_ue_golomb(s);
  289. int len= get_bits_count(s) - pos;
  290. int bits= show>>(24-len);
  291. print_bin(bits, len);
  292. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  293. return i;
  294. }
  295. static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
  296. int show= show_bits(s, 24);
  297. int pos= get_bits_count(s);
  298. int i= get_se_golomb(s);
  299. int len= get_bits_count(s) - pos;
  300. int bits= show>>(24-len);
  301. print_bin(bits, len);
  302. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  303. return i;
  304. }
  305. static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
  306. int show= show_bits(s, 24);
  307. int pos= get_bits_count(s);
  308. int i= get_te0_golomb(s, r);
  309. int len= get_bits_count(s) - pos;
  310. int bits= show>>(24-len);
  311. print_bin(bits, len);
  312. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  313. return i;
  314. }
  315. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  316. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  317. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  318. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  319. #endif
  320. /**
  321. * write unsigned exp golomb code.
  322. */
  323. static inline void set_ue_golomb(PutBitContext *pb, int i){
  324. int e;
  325. assert(i>=0);
  326. #if 0
  327. if(i=0){
  328. put_bits(pb, 1, 1);
  329. return;
  330. }
  331. #endif
  332. if(i<256)
  333. put_bits(pb, ff_ue_golomb_len[i], i+1);
  334. else{
  335. e= av_log2(i+1);
  336. put_bits(pb, 2*e+1, i+1);
  337. }
  338. }
  339. /**
  340. * write truncated unsigned exp golomb code.
  341. */
  342. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  343. assert(range >= 1);
  344. assert(i<=range);
  345. if(range==2) put_bits(pb, 1, i^1);
  346. else set_ue_golomb(pb, i);
  347. }
  348. /**
  349. * write signed exp golomb code. 16 bits at most.
  350. */
  351. static inline void set_se_golomb(PutBitContext *pb, int i){
  352. // if (i>32767 || i<-32767)
  353. // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
  354. #if 0
  355. if(i<=0) i= -2*i;
  356. else i= 2*i-1;
  357. #elif 1
  358. i= 2*i-1;
  359. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  360. #else
  361. i= 2*i-1;
  362. i^= (i>>31);
  363. #endif
  364. set_ue_golomb(pb, i);
  365. }
  366. /**
  367. * write unsigned golomb rice code (ffv1).
  368. */
  369. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  370. int e;
  371. assert(i>=0);
  372. e= i>>k;
  373. if(e<limit){
  374. put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
  375. }else{
  376. put_bits(pb, limit + esc_len, i - limit + 1);
  377. }
  378. }
  379. /**
  380. * write unsigned golomb rice code (jpegls).
  381. */
  382. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
  383. int e;
  384. assert(i>=0);
  385. e= (i>>k) + 1;
  386. if(e<limit){
  387. while(e > 31) {
  388. put_bits(pb, 31, 0);
  389. e -= 31;
  390. }
  391. put_bits(pb, e, 1);
  392. if(k)
  393. put_sbits(pb, k, i);
  394. }else{
  395. while(limit > 31) {
  396. put_bits(pb, 31, 0);
  397. limit -= 31;
  398. }
  399. put_bits(pb, limit , 1);
  400. put_bits(pb, esc_len, i - 1);
  401. }
  402. }
  403. /**
  404. * write signed golomb rice code (ffv1).
  405. */
  406. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  407. int v;
  408. v = -2*i-1;
  409. v ^= (v>>31);
  410. set_ur_golomb(pb, v, k, limit, esc_len);
  411. }
  412. /**
  413. * write signed golomb rice code (flac).
  414. */
  415. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
  416. int v;
  417. v = -2*i-1;
  418. v ^= (v>>31);
  419. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  420. }
  421. #endif /* AVCODEC_GOLOMB_H */