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.

447 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. #ifdef TRACE
  231. static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
  232. int show= show_bits(s, 24);
  233. int pos= get_bits_count(s);
  234. int i= get_ue_golomb(s);
  235. int len= get_bits_count(s) - pos;
  236. int bits= show>>(24-len);
  237. print_bin(bits, len);
  238. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  239. return i;
  240. }
  241. static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
  242. int show= show_bits(s, 24);
  243. int pos= get_bits_count(s);
  244. int i= get_se_golomb(s);
  245. int len= get_bits_count(s) - pos;
  246. int bits= show>>(24-len);
  247. print_bin(bits, len);
  248. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  249. return i;
  250. }
  251. static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
  252. int show= show_bits(s, 24);
  253. int pos= get_bits_count(s);
  254. int i= get_te0_golomb(s, r);
  255. int len= get_bits_count(s) - pos;
  256. int bits= show>>(24-len);
  257. print_bin(bits, len);
  258. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  259. return i;
  260. }
  261. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  262. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  263. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  264. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  265. #endif
  266. /**
  267. * write unsigned exp golomb code.
  268. */
  269. static inline void set_ue_golomb(PutBitContext *pb, int i){
  270. int e;
  271. assert(i>=0);
  272. #if 0
  273. if(i=0){
  274. put_bits(pb, 1, 1);
  275. return;
  276. }
  277. #endif
  278. if(i<256)
  279. put_bits(pb, ff_ue_golomb_len[i], i+1);
  280. else{
  281. e= av_log2(i+1);
  282. put_bits(pb, 2*e+1, i+1);
  283. }
  284. }
  285. /**
  286. * write truncated unsigned exp golomb code.
  287. */
  288. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  289. assert(range >= 1);
  290. assert(i<=range);
  291. if(range==2) put_bits(pb, 1, i^1);
  292. else set_ue_golomb(pb, i);
  293. }
  294. /**
  295. * write signed exp golomb code.
  296. */
  297. static inline void set_se_golomb(PutBitContext *pb, int i){
  298. #if 0
  299. if(i<=0) i= -2*i;
  300. else i= 2*i-1;
  301. #elif 1
  302. i= 2*i-1;
  303. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  304. #else
  305. i= 2*i-1;
  306. i^= (i>>31);
  307. #endif
  308. set_ue_golomb(pb, i);
  309. }
  310. /**
  311. * write unsigned golomb rice code (ffv1).
  312. */
  313. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  314. int e;
  315. assert(i>=0);
  316. e= i>>k;
  317. if(e<limit){
  318. put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
  319. }else{
  320. put_bits(pb, limit + esc_len, i - limit + 1);
  321. }
  322. }
  323. /**
  324. * write unsigned golomb rice code (jpegls).
  325. */
  326. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
  327. int e;
  328. assert(i>=0);
  329. e= (i>>k) + 1;
  330. if(e<limit){
  331. put_bits(pb, e, 1);
  332. if(k)
  333. put_bits(pb, k, i&((1<<k)-1));
  334. }else{
  335. put_bits(pb, limit , 1);
  336. put_bits(pb, esc_len, i - 1);
  337. }
  338. }
  339. /**
  340. * write signed golomb rice code (ffv1).
  341. */
  342. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  343. int v;
  344. v = -2*i-1;
  345. v ^= (v>>31);
  346. set_ur_golomb(pb, v, k, limit, esc_len);
  347. }
  348. /**
  349. * write signed golomb rice code (flac).
  350. */
  351. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
  352. int v;
  353. v = -2*i-1;
  354. v ^= (v>>31);
  355. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  356. }