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.

395 lines
9.3KB

  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. buf|=1;
  70. if((buf & 0xAAAAAAAA) == 0)
  71. return INVALID_VLC;
  72. for(log=31; (buf & 0x80000000) == 0; log--){
  73. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  74. }
  75. LAST_SKIP_BITS(re, gb, 63 - 2*log);
  76. CLOSE_READER(re, gb);
  77. return ((buf << log) >> log) - 1;
  78. }
  79. }
  80. /**
  81. * read unsigned truncated exp golomb code.
  82. */
  83. static inline int get_te0_golomb(GetBitContext *gb, int range){
  84. assert(range >= 1);
  85. if(range==1) return 0;
  86. else if(range==2) return get_bits1(gb)^1;
  87. else return get_ue_golomb(gb);
  88. }
  89. /**
  90. * read unsigned truncated exp golomb code.
  91. */
  92. static inline int get_te_golomb(GetBitContext *gb, int range){
  93. assert(range >= 1);
  94. if(range==2) return get_bits1(gb)^1;
  95. else return get_ue_golomb(gb);
  96. }
  97. /**
  98. * read signed exp golomb code.
  99. */
  100. static inline int get_se_golomb(GetBitContext *gb){
  101. unsigned int buf;
  102. int log;
  103. OPEN_READER(re, gb);
  104. UPDATE_CACHE(re, gb);
  105. buf=GET_CACHE(re, gb);
  106. if(buf >= (1<<27)){
  107. buf >>= 32 - 9;
  108. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  109. CLOSE_READER(re, gb);
  110. return ff_se_golomb_vlc_code[buf];
  111. }else{
  112. log= 2*av_log2(buf) - 31;
  113. buf>>= log;
  114. LAST_SKIP_BITS(re, gb, 32 - log);
  115. CLOSE_READER(re, gb);
  116. if(buf&1) buf= -(buf>>1);
  117. else buf= (buf>>1);
  118. return buf;
  119. }
  120. }
  121. static inline int svq3_get_se_golomb(GetBitContext *gb){
  122. unsigned int buf;
  123. int log;
  124. OPEN_READER(re, gb);
  125. UPDATE_CACHE(re, gb);
  126. buf=GET_CACHE(re, gb);
  127. if(buf&0xAA800000){
  128. buf >>= 32 - 8;
  129. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  130. CLOSE_READER(re, gb);
  131. return ff_interleaved_se_golomb_vlc_code[buf];
  132. }else{
  133. buf |=1;
  134. if((buf & 0xAAAAAAAA) == 0)
  135. return INVALID_VLC;
  136. for(log=31; (buf & 0x80000000) == 0; log--){
  137. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  138. }
  139. LAST_SKIP_BITS(re, gb, 63 - 2*log);
  140. CLOSE_READER(re, gb);
  141. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  142. }
  143. }
  144. /**
  145. * read unsigned golomb rice code (ffv1).
  146. */
  147. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  148. unsigned int buf;
  149. int log;
  150. OPEN_READER(re, gb);
  151. UPDATE_CACHE(re, gb);
  152. buf=GET_CACHE(re, gb);
  153. log= av_log2(buf);
  154. if(log > 31-limit){
  155. buf >>= log - k;
  156. buf += (30-log)<<k;
  157. LAST_SKIP_BITS(re, gb, 32 + k - log);
  158. CLOSE_READER(re, gb);
  159. return buf;
  160. }else{
  161. buf >>= 32 - limit - esc_len;
  162. LAST_SKIP_BITS(re, gb, esc_len + limit);
  163. CLOSE_READER(re, gb);
  164. return buf + limit - 1;
  165. }
  166. }
  167. /**
  168. * read unsigned golomb rice code (jpegls).
  169. */
  170. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
  171. unsigned int buf;
  172. int log;
  173. OPEN_READER(re, gb);
  174. UPDATE_CACHE(re, gb);
  175. buf=GET_CACHE(re, gb);
  176. log= av_log2(buf);
  177. if(log > 31-11){
  178. buf >>= log - k;
  179. buf += (30-log)<<k;
  180. LAST_SKIP_BITS(re, gb, 32 + k - log);
  181. CLOSE_READER(re, gb);
  182. return buf;
  183. }else{
  184. int i;
  185. for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
  186. LAST_SKIP_BITS(re, gb, 1);
  187. UPDATE_CACHE(re, gb);
  188. }
  189. SKIP_BITS(re, gb, 1);
  190. if(i < limit - 1){
  191. if(k){
  192. buf = SHOW_UBITS(re, gb, k);
  193. LAST_SKIP_BITS(re, gb, k);
  194. }else{
  195. buf=0;
  196. }
  197. CLOSE_READER(re, gb);
  198. return buf + (i<<k);
  199. }else if(i == limit - 1){
  200. buf = SHOW_UBITS(re, gb, esc_len);
  201. LAST_SKIP_BITS(re, gb, esc_len);
  202. CLOSE_READER(re, gb);
  203. return buf + 1;
  204. }else
  205. return -1;
  206. }
  207. }
  208. #ifdef TRACE
  209. static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
  210. int show= show_bits(s, 24);
  211. int pos= get_bits_count(s);
  212. int i= get_ue_golomb(s);
  213. int len= get_bits_count(s) - pos;
  214. int bits= show>>(24-len);
  215. print_bin(bits, len);
  216. printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  217. return i;
  218. }
  219. static inline int get_se(GetBitContext *s, char *file, char *func, int line){
  220. int show= show_bits(s, 24);
  221. int pos= get_bits_count(s);
  222. int i= get_se_golomb(s);
  223. int len= get_bits_count(s) - pos;
  224. int bits= show>>(24-len);
  225. print_bin(bits, len);
  226. printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  227. return i;
  228. }
  229. static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
  230. int show= show_bits(s, 24);
  231. int pos= get_bits_count(s);
  232. int i= get_te0_golomb(s, r);
  233. int len= get_bits_count(s) - pos;
  234. int bits= show>>(24-len);
  235. print_bin(bits, len);
  236. printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  237. return i;
  238. }
  239. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  240. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  241. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  242. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  243. #endif
  244. /**
  245. * write unsigned exp golomb code.
  246. */
  247. static inline void set_ue_golomb(PutBitContext *pb, int i){
  248. int e;
  249. assert(i>=0);
  250. #if 0
  251. if(i=0){
  252. put_bits(pb, 1, 1);
  253. return;
  254. }
  255. #endif
  256. if(i<256)
  257. put_bits(pb, ff_ue_golomb_len[i], i+1);
  258. else{
  259. e= av_log2(i+1);
  260. put_bits(pb, 2*e+1, i+1);
  261. }
  262. }
  263. /**
  264. * write truncated unsigned exp golomb code.
  265. */
  266. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  267. assert(range >= 1);
  268. assert(i<=range);
  269. if(range==2) put_bits(pb, 1, i^1);
  270. else set_ue_golomb(pb, i);
  271. }
  272. /**
  273. * write signed exp golomb code.
  274. */
  275. static inline void set_se_golomb(PutBitContext *pb, int i){
  276. #if 0
  277. if(i<=0) i= -2*i;
  278. else i= 2*i-1;
  279. #elif 1
  280. i= 2*i-1;
  281. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  282. #else
  283. i= 2*i-1;
  284. i^= (i>>31);
  285. #endif
  286. set_ue_golomb(pb, i);
  287. }
  288. /**
  289. * write unsigned golomb rice code (ffv1).
  290. */
  291. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  292. int e;
  293. assert(i>=0);
  294. e= i>>k;
  295. if(e<limit){
  296. put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
  297. }else{
  298. put_bits(pb, limit + esc_len, i - limit + 1);
  299. }
  300. }
  301. /**
  302. * write unsigned golomb rice code (jpegls).
  303. */
  304. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
  305. int e;
  306. assert(i>=0);
  307. e= (i>>k) + 1;
  308. if(e<limit){
  309. put_bits(pb, e, 1);
  310. if(k)
  311. put_bits(pb, k, i&((1<<k)-1));
  312. }else{
  313. put_bits(pb, limit , 1);
  314. put_bits(pb, esc_len, i - 1);
  315. }
  316. }