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.

470 lines
11KB

  1. /*
  2. * exp golomb vlc stuff
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2004 Alex Beregszaszi
  5. *
  6. * This library 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 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /**
  22. * @file golomb.h
  23. * @brief
  24. * exp golomb vlc stuff
  25. * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
  26. */
  27. #define INVALID_VLC 0x80000000
  28. extern const uint8_t ff_golomb_vlc_len[512];
  29. extern const uint8_t ff_ue_golomb_vlc_code[512];
  30. extern const int8_t ff_se_golomb_vlc_code[512];
  31. extern const uint8_t ff_ue_golomb_len[256];
  32. extern const uint8_t ff_interleaved_golomb_vlc_len[256];
  33. extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
  34. extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
  35. /**
  36. * read unsigned exp golomb code.
  37. */
  38. static inline int get_ue_golomb(GetBitContext *gb){
  39. unsigned int buf;
  40. int log;
  41. OPEN_READER(re, gb);
  42. UPDATE_CACHE(re, gb);
  43. buf=GET_CACHE(re, gb);
  44. if(buf >= (1<<27)){
  45. buf >>= 32 - 9;
  46. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  47. CLOSE_READER(re, gb);
  48. return ff_ue_golomb_vlc_code[buf];
  49. }else{
  50. log= 2*av_log2(buf) - 31;
  51. buf>>= log;
  52. buf--;
  53. LAST_SKIP_BITS(re, gb, 32 - log);
  54. CLOSE_READER(re, gb);
  55. return buf;
  56. }
  57. }
  58. static inline int svq3_get_ue_golomb(GetBitContext *gb){
  59. uint32_t buf;
  60. int log;
  61. OPEN_READER(re, gb);
  62. UPDATE_CACHE(re, gb);
  63. buf=GET_CACHE(re, gb);
  64. if(buf&0xAA800000){
  65. buf >>= 32 - 8;
  66. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  67. CLOSE_READER(re, gb);
  68. return ff_interleaved_ue_golomb_vlc_code[buf];
  69. }else{
  70. LAST_SKIP_BITS(re, gb, 8);
  71. UPDATE_CACHE(re, gb);
  72. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  73. if((buf & 0xAAAAAAAA) == 0)
  74. return INVALID_VLC;
  75. for(log=31; (buf & 0x80000000) == 0; log--){
  76. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  77. }
  78. LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
  79. CLOSE_READER(re, gb);
  80. return ((buf << log) >> log) - 1;
  81. }
  82. }
  83. /**
  84. * read unsigned truncated exp golomb code.
  85. */
  86. static inline int get_te0_golomb(GetBitContext *gb, int range){
  87. assert(range >= 1);
  88. if(range==1) return 0;
  89. else if(range==2) return get_bits1(gb)^1;
  90. else return get_ue_golomb(gb);
  91. }
  92. /**
  93. * read unsigned truncated exp golomb code.
  94. */
  95. static inline int get_te_golomb(GetBitContext *gb, int range){
  96. assert(range >= 1);
  97. if(range==2) return get_bits1(gb)^1;
  98. else return get_ue_golomb(gb);
  99. }
  100. /**
  101. * read signed exp golomb code.
  102. */
  103. static inline int get_se_golomb(GetBitContext *gb){
  104. unsigned int buf;
  105. int log;
  106. OPEN_READER(re, gb);
  107. UPDATE_CACHE(re, gb);
  108. buf=GET_CACHE(re, gb);
  109. if(buf >= (1<<27)){
  110. buf >>= 32 - 9;
  111. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  112. CLOSE_READER(re, gb);
  113. return ff_se_golomb_vlc_code[buf];
  114. }else{
  115. log= 2*av_log2(buf) - 31;
  116. buf>>= log;
  117. LAST_SKIP_BITS(re, gb, 32 - log);
  118. CLOSE_READER(re, gb);
  119. if(buf&1) buf= -(buf>>1);
  120. else buf= (buf>>1);
  121. return buf;
  122. }
  123. }
  124. static inline int svq3_get_se_golomb(GetBitContext *gb){
  125. unsigned int buf;
  126. int log;
  127. OPEN_READER(re, gb);
  128. UPDATE_CACHE(re, gb);
  129. buf=GET_CACHE(re, gb);
  130. if(buf&0xAA800000){
  131. buf >>= 32 - 8;
  132. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  133. CLOSE_READER(re, gb);
  134. return ff_interleaved_se_golomb_vlc_code[buf];
  135. }else{
  136. LAST_SKIP_BITS(re, gb, 8);
  137. UPDATE_CACHE(re, gb);
  138. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  139. if((buf & 0xAAAAAAAA) == 0)
  140. return INVALID_VLC;
  141. for(log=31; (buf & 0x80000000) == 0; log--){
  142. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  143. }
  144. LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
  145. CLOSE_READER(re, gb);
  146. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  147. }
  148. }
  149. /**
  150. * read unsigned golomb rice code (ffv1).
  151. */
  152. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  153. unsigned int buf;
  154. int log;
  155. OPEN_READER(re, gb);
  156. UPDATE_CACHE(re, gb);
  157. buf=GET_CACHE(re, gb);
  158. log= av_log2(buf);
  159. if(log > 31-limit){
  160. buf >>= log - k;
  161. buf += (30-log)<<k;
  162. LAST_SKIP_BITS(re, gb, 32 + k - log);
  163. CLOSE_READER(re, gb);
  164. return buf;
  165. }else{
  166. buf >>= 32 - limit - esc_len;
  167. LAST_SKIP_BITS(re, gb, esc_len + limit);
  168. CLOSE_READER(re, gb);
  169. return buf + limit - 1;
  170. }
  171. }
  172. /**
  173. * read unsigned golomb rice code (jpegls).
  174. */
  175. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
  176. unsigned int buf;
  177. int log;
  178. OPEN_READER(re, gb);
  179. UPDATE_CACHE(re, gb);
  180. buf=GET_CACHE(re, gb);
  181. log= av_log2(buf);
  182. if(log > 31-11){
  183. buf >>= log - k;
  184. buf += (30-log)<<k;
  185. LAST_SKIP_BITS(re, gb, 32 + k - log);
  186. CLOSE_READER(re, gb);
  187. return buf;
  188. }else{
  189. int i;
  190. for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
  191. LAST_SKIP_BITS(re, gb, 1);
  192. UPDATE_CACHE(re, gb);
  193. }
  194. SKIP_BITS(re, gb, 1);
  195. if(i < limit - 1){
  196. if(k){
  197. buf = SHOW_UBITS(re, gb, k);
  198. LAST_SKIP_BITS(re, gb, k);
  199. }else{
  200. buf=0;
  201. }
  202. CLOSE_READER(re, gb);
  203. return buf + (i<<k);
  204. }else if(i == limit - 1){
  205. buf = SHOW_UBITS(re, gb, esc_len);
  206. LAST_SKIP_BITS(re, gb, esc_len);
  207. CLOSE_READER(re, gb);
  208. return buf + 1;
  209. }else
  210. return -1;
  211. }
  212. }
  213. /**
  214. * read signed golomb rice code (ffv1).
  215. */
  216. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  217. int v= get_ur_golomb(gb, k, limit, esc_len);
  218. v++;
  219. if (v&1) return v>>1;
  220. else return -(v>>1);
  221. // return (v>>1) ^ -(v&1);
  222. }
  223. /**
  224. * read signed golomb rice code (flac).
  225. */
  226. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
  227. int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
  228. return (v>>1) ^ -(v&1);
  229. }
  230. /**
  231. * read unsigned golomb rice code (shorten).
  232. */
  233. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
  234. return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  235. }
  236. /**
  237. * read signed golomb rice code (shorten).
  238. */
  239. static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
  240. {
  241. int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  242. if (uvar & 1)
  243. return ~(uvar >> 1);
  244. else
  245. return uvar >> 1;
  246. }
  247. #ifdef TRACE
  248. static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
  249. int show= show_bits(s, 24);
  250. int pos= get_bits_count(s);
  251. int i= get_ue_golomb(s);
  252. int len= get_bits_count(s) - pos;
  253. int bits= show>>(24-len);
  254. print_bin(bits, len);
  255. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  256. return i;
  257. }
  258. static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
  259. int show= show_bits(s, 24);
  260. int pos= get_bits_count(s);
  261. int i= get_se_golomb(s);
  262. int len= get_bits_count(s) - pos;
  263. int bits= show>>(24-len);
  264. print_bin(bits, len);
  265. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  266. return i;
  267. }
  268. static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
  269. int show= show_bits(s, 24);
  270. int pos= get_bits_count(s);
  271. int i= get_te0_golomb(s, r);
  272. int len= get_bits_count(s) - pos;
  273. int bits= show>>(24-len);
  274. print_bin(bits, len);
  275. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  276. return i;
  277. }
  278. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  279. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  280. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  281. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  282. #endif
  283. /**
  284. * write unsigned exp golomb code.
  285. */
  286. static inline void set_ue_golomb(PutBitContext *pb, int i){
  287. int e;
  288. assert(i>=0);
  289. #if 0
  290. if(i=0){
  291. put_bits(pb, 1, 1);
  292. return;
  293. }
  294. #endif
  295. if(i<256)
  296. put_bits(pb, ff_ue_golomb_len[i], i+1);
  297. else{
  298. e= av_log2(i+1);
  299. put_bits(pb, 2*e+1, i+1);
  300. }
  301. }
  302. /**
  303. * write truncated unsigned exp golomb code.
  304. */
  305. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  306. assert(range >= 1);
  307. assert(i<=range);
  308. if(range==2) put_bits(pb, 1, i^1);
  309. else set_ue_golomb(pb, i);
  310. }
  311. /**
  312. * write signed exp golomb code. 16 bits at most.
  313. */
  314. static inline void set_se_golomb(PutBitContext *pb, int i){
  315. // if (i>32767 || i<-32767)
  316. // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
  317. #if 0
  318. if(i<=0) i= -2*i;
  319. else i= 2*i-1;
  320. #elif 1
  321. i= 2*i-1;
  322. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  323. #else
  324. i= 2*i-1;
  325. i^= (i>>31);
  326. #endif
  327. set_ue_golomb(pb, i);
  328. }
  329. /**
  330. * write unsigned golomb rice code (ffv1).
  331. */
  332. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  333. int e;
  334. assert(i>=0);
  335. e= i>>k;
  336. if(e<limit){
  337. put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
  338. }else{
  339. put_bits(pb, limit + esc_len, i - limit + 1);
  340. }
  341. }
  342. /**
  343. * write unsigned golomb rice code (jpegls).
  344. */
  345. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
  346. int e;
  347. assert(i>=0);
  348. e= (i>>k) + 1;
  349. if(e<limit){
  350. put_bits(pb, e, 1);
  351. if(k)
  352. put_bits(pb, k, i&((1<<k)-1));
  353. }else{
  354. put_bits(pb, limit , 1);
  355. put_bits(pb, esc_len, i - 1);
  356. }
  357. }
  358. /**
  359. * write signed golomb rice code (ffv1).
  360. */
  361. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  362. int v;
  363. v = -2*i-1;
  364. v ^= (v>>31);
  365. set_ur_golomb(pb, v, k, limit, esc_len);
  366. }
  367. /**
  368. * write signed golomb rice code (flac).
  369. */
  370. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
  371. int v;
  372. v = -2*i-1;
  373. v ^= (v>>31);
  374. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  375. }