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.

406 lines
9.7KB

  1. /*
  2. * exp golomb vlc stuff
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file golomb.h
  22. * @brief
  23. * exp golomb vlc stuff
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define INVALID_VLC 0x80000000
  27. extern const uint8_t ff_golomb_vlc_len[512];
  28. extern const uint8_t ff_ue_golomb_vlc_code[512];
  29. extern const int8_t ff_se_golomb_vlc_code[512];
  30. extern const uint8_t ff_ue_golomb_len[256];
  31. extern const uint8_t ff_interleaved_golomb_vlc_len[256];
  32. extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
  33. extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
  34. /**
  35. * read unsigned exp golomb code.
  36. */
  37. static inline int get_ue_golomb(GetBitContext *gb){
  38. unsigned int buf;
  39. int log;
  40. OPEN_READER(re, gb);
  41. UPDATE_CACHE(re, gb);
  42. buf=GET_CACHE(re, gb);
  43. if(buf >= (1<<27)){
  44. buf >>= 32 - 9;
  45. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  46. CLOSE_READER(re, gb);
  47. return ff_ue_golomb_vlc_code[buf];
  48. }else{
  49. log= 2*av_log2(buf) - 31;
  50. buf>>= log;
  51. buf--;
  52. LAST_SKIP_BITS(re, gb, 32 - log);
  53. CLOSE_READER(re, gb);
  54. return buf;
  55. }
  56. }
  57. static inline int svq3_get_ue_golomb(GetBitContext *gb){
  58. uint32_t buf;
  59. int log;
  60. OPEN_READER(re, gb);
  61. UPDATE_CACHE(re, gb);
  62. buf=GET_CACHE(re, gb);
  63. if(buf&0xAA800000){
  64. buf >>= 32 - 8;
  65. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  66. CLOSE_READER(re, gb);
  67. return ff_interleaved_ue_golomb_vlc_code[buf];
  68. }else{
  69. LAST_SKIP_BITS(re, gb, 8);
  70. UPDATE_CACHE(re, gb);
  71. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  72. if((buf & 0xAAAAAAAA) == 0)
  73. return INVALID_VLC;
  74. for(log=31; (buf & 0x80000000) == 0; log--){
  75. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  76. }
  77. LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
  78. CLOSE_READER(re, gb);
  79. return ((buf << log) >> log) - 1;
  80. }
  81. }
  82. /**
  83. * read unsigned truncated exp golomb code.
  84. */
  85. static inline int get_te0_golomb(GetBitContext *gb, int range){
  86. assert(range >= 1);
  87. if(range==1) return 0;
  88. else if(range==2) return get_bits1(gb)^1;
  89. else return get_ue_golomb(gb);
  90. }
  91. /**
  92. * read unsigned truncated exp golomb code.
  93. */
  94. static inline int get_te_golomb(GetBitContext *gb, int range){
  95. assert(range >= 1);
  96. if(range==2) return get_bits1(gb)^1;
  97. else return get_ue_golomb(gb);
  98. }
  99. /**
  100. * read signed exp golomb code.
  101. */
  102. static inline int get_se_golomb(GetBitContext *gb){
  103. unsigned int buf;
  104. int log;
  105. OPEN_READER(re, gb);
  106. UPDATE_CACHE(re, gb);
  107. buf=GET_CACHE(re, gb);
  108. if(buf >= (1<<27)){
  109. buf >>= 32 - 9;
  110. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  111. CLOSE_READER(re, gb);
  112. return ff_se_golomb_vlc_code[buf];
  113. }else{
  114. log= 2*av_log2(buf) - 31;
  115. buf>>= log;
  116. LAST_SKIP_BITS(re, gb, 32 - log);
  117. CLOSE_READER(re, gb);
  118. if(buf&1) buf= -(buf>>1);
  119. else buf= (buf>>1);
  120. return buf;
  121. }
  122. }
  123. static inline int svq3_get_se_golomb(GetBitContext *gb){
  124. unsigned int buf;
  125. int log;
  126. OPEN_READER(re, gb);
  127. UPDATE_CACHE(re, gb);
  128. buf=GET_CACHE(re, gb);
  129. if(buf&0xAA800000){
  130. buf >>= 32 - 8;
  131. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  132. CLOSE_READER(re, gb);
  133. return ff_interleaved_se_golomb_vlc_code[buf];
  134. }else{
  135. buf |=1;
  136. if((buf & 0xAAAAAAAA) == 0)
  137. return INVALID_VLC;
  138. for(log=31; (buf & 0x80000000) == 0; log--){
  139. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  140. }
  141. LAST_SKIP_BITS(re, gb, 63 - 2*log);
  142. CLOSE_READER(re, gb);
  143. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  144. }
  145. }
  146. /**
  147. * read unsigned golomb rice code (ffv1).
  148. */
  149. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  150. unsigned int buf;
  151. int log;
  152. OPEN_READER(re, gb);
  153. UPDATE_CACHE(re, gb);
  154. buf=GET_CACHE(re, gb);
  155. log= av_log2(buf);
  156. if(log > 31-limit){
  157. buf >>= log - k;
  158. buf += (30-log)<<k;
  159. LAST_SKIP_BITS(re, gb, 32 + k - log);
  160. CLOSE_READER(re, gb);
  161. return buf;
  162. }else{
  163. buf >>= 32 - limit - esc_len;
  164. LAST_SKIP_BITS(re, gb, esc_len + limit);
  165. CLOSE_READER(re, gb);
  166. return buf + limit - 1;
  167. }
  168. }
  169. /**
  170. * read unsigned golomb rice code (jpegls).
  171. */
  172. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
  173. unsigned int buf;
  174. int log;
  175. OPEN_READER(re, gb);
  176. UPDATE_CACHE(re, gb);
  177. buf=GET_CACHE(re, gb);
  178. log= av_log2(buf);
  179. if(log > 31-11){
  180. buf >>= log - k;
  181. buf += (30-log)<<k;
  182. LAST_SKIP_BITS(re, gb, 32 + k - log);
  183. CLOSE_READER(re, gb);
  184. return buf;
  185. }else{
  186. int i;
  187. for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
  188. LAST_SKIP_BITS(re, gb, 1);
  189. UPDATE_CACHE(re, gb);
  190. }
  191. SKIP_BITS(re, gb, 1);
  192. if(i < limit - 1){
  193. if(k){
  194. buf = SHOW_UBITS(re, gb, k);
  195. LAST_SKIP_BITS(re, gb, k);
  196. }else{
  197. buf=0;
  198. }
  199. CLOSE_READER(re, gb);
  200. return buf + (i<<k);
  201. }else if(i == limit - 1){
  202. buf = SHOW_UBITS(re, gb, esc_len);
  203. LAST_SKIP_BITS(re, gb, esc_len);
  204. CLOSE_READER(re, gb);
  205. return buf + 1;
  206. }else
  207. return -1;
  208. }
  209. }
  210. /**
  211. * read unsigned golomb rice code (flac).
  212. */
  213. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
  214. int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
  215. return (v>>1) ^ -(v&1);
  216. }
  217. #ifdef TRACE
  218. static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
  219. int show= show_bits(s, 24);
  220. int pos= get_bits_count(s);
  221. int i= get_ue_golomb(s);
  222. int len= get_bits_count(s) - pos;
  223. int bits= show>>(24-len);
  224. print_bin(bits, len);
  225. printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  226. return i;
  227. }
  228. static inline int get_se(GetBitContext *s, char *file, char *func, int line){
  229. int show= show_bits(s, 24);
  230. int pos= get_bits_count(s);
  231. int i= get_se_golomb(s);
  232. int len= get_bits_count(s) - pos;
  233. int bits= show>>(24-len);
  234. print_bin(bits, len);
  235. printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  236. return i;
  237. }
  238. static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
  239. int show= show_bits(s, 24);
  240. int pos= get_bits_count(s);
  241. int i= get_te0_golomb(s, r);
  242. int len= get_bits_count(s) - pos;
  243. int bits= show>>(24-len);
  244. print_bin(bits, len);
  245. printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  246. return i;
  247. }
  248. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  249. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  250. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  251. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  252. #endif
  253. /**
  254. * write unsigned exp golomb code.
  255. */
  256. static inline void set_ue_golomb(PutBitContext *pb, int i){
  257. int e;
  258. assert(i>=0);
  259. #if 0
  260. if(i=0){
  261. put_bits(pb, 1, 1);
  262. return;
  263. }
  264. #endif
  265. if(i<256)
  266. put_bits(pb, ff_ue_golomb_len[i], i+1);
  267. else{
  268. e= av_log2(i+1);
  269. put_bits(pb, 2*e+1, i+1);
  270. }
  271. }
  272. /**
  273. * write truncated unsigned exp golomb code.
  274. */
  275. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  276. assert(range >= 1);
  277. assert(i<=range);
  278. if(range==2) put_bits(pb, 1, i^1);
  279. else set_ue_golomb(pb, i);
  280. }
  281. /**
  282. * write signed exp golomb code.
  283. */
  284. static inline void set_se_golomb(PutBitContext *pb, int i){
  285. #if 0
  286. if(i<=0) i= -2*i;
  287. else i= 2*i-1;
  288. #elif 1
  289. i= 2*i-1;
  290. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  291. #else
  292. i= 2*i-1;
  293. i^= (i>>31);
  294. #endif
  295. set_ue_golomb(pb, i);
  296. }
  297. /**
  298. * write unsigned golomb rice code (ffv1).
  299. */
  300. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  301. int e;
  302. assert(i>=0);
  303. e= i>>k;
  304. if(e<limit){
  305. put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
  306. }else{
  307. put_bits(pb, limit + esc_len, i - limit + 1);
  308. }
  309. }
  310. /**
  311. * write unsigned golomb rice code (jpegls).
  312. */
  313. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
  314. int e;
  315. assert(i>=0);
  316. e= (i>>k) + 1;
  317. if(e<limit){
  318. put_bits(pb, e, 1);
  319. if(k)
  320. put_bits(pb, k, i&((1<<k)-1));
  321. }else{
  322. put_bits(pb, limit , 1);
  323. put_bits(pb, esc_len, i - 1);
  324. }
  325. }