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.

444 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. buf |=1;
  137. if((buf & 0xAAAAAAAA) == 0)
  138. return INVALID_VLC;
  139. for(log=31; (buf & 0x80000000) == 0; log--){
  140. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  141. }
  142. LAST_SKIP_BITS(re, gb, 63 - 2*log);
  143. CLOSE_READER(re, gb);
  144. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  145. }
  146. }
  147. /**
  148. * read unsigned golomb rice code (ffv1).
  149. */
  150. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  151. unsigned int buf;
  152. int log;
  153. OPEN_READER(re, gb);
  154. UPDATE_CACHE(re, gb);
  155. buf=GET_CACHE(re, gb);
  156. log= av_log2(buf);
  157. if(log > 31-limit){
  158. buf >>= log - k;
  159. buf += (30-log)<<k;
  160. LAST_SKIP_BITS(re, gb, 32 + k - log);
  161. CLOSE_READER(re, gb);
  162. return buf;
  163. }else{
  164. buf >>= 32 - limit - esc_len;
  165. LAST_SKIP_BITS(re, gb, esc_len + limit);
  166. CLOSE_READER(re, gb);
  167. return buf + limit - 1;
  168. }
  169. }
  170. /**
  171. * read unsigned golomb rice code (jpegls).
  172. */
  173. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
  174. unsigned int buf;
  175. int log;
  176. OPEN_READER(re, gb);
  177. UPDATE_CACHE(re, gb);
  178. buf=GET_CACHE(re, gb);
  179. log= av_log2(buf);
  180. if(log > 31-11){
  181. buf >>= log - k;
  182. buf += (30-log)<<k;
  183. LAST_SKIP_BITS(re, gb, 32 + k - log);
  184. CLOSE_READER(re, gb);
  185. return buf;
  186. }else{
  187. int i;
  188. for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
  189. LAST_SKIP_BITS(re, gb, 1);
  190. UPDATE_CACHE(re, gb);
  191. }
  192. SKIP_BITS(re, gb, 1);
  193. if(i < limit - 1){
  194. if(k){
  195. buf = SHOW_UBITS(re, gb, k);
  196. LAST_SKIP_BITS(re, gb, k);
  197. }else{
  198. buf=0;
  199. }
  200. CLOSE_READER(re, gb);
  201. return buf + (i<<k);
  202. }else if(i == limit - 1){
  203. buf = SHOW_UBITS(re, gb, esc_len);
  204. LAST_SKIP_BITS(re, gb, esc_len);
  205. CLOSE_READER(re, gb);
  206. return buf + 1;
  207. }else
  208. return -1;
  209. }
  210. }
  211. /**
  212. * read signed golomb rice code (ffv1).
  213. */
  214. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
  215. int v= get_ur_golomb(gb, k, limit, esc_len);
  216. v++;
  217. if (v&1) return v>>1;
  218. else return -(v>>1);
  219. // return (v>>1) ^ -(v&1);
  220. }
  221. /**
  222. * read signed golomb rice code (flac).
  223. */
  224. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
  225. int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
  226. return (v>>1) ^ -(v&1);
  227. }
  228. #ifdef TRACE
  229. static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
  230. int show= show_bits(s, 24);
  231. int pos= get_bits_count(s);
  232. int i= get_ue_golomb(s);
  233. int len= get_bits_count(s) - pos;
  234. int bits= show>>(24-len);
  235. print_bin(bits, len);
  236. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  237. return i;
  238. }
  239. static inline int get_se(GetBitContext *s, char *file, char *func, int line){
  240. int show= show_bits(s, 24);
  241. int pos= get_bits_count(s);
  242. int i= get_se_golomb(s);
  243. int len= get_bits_count(s) - pos;
  244. int bits= show>>(24-len);
  245. print_bin(bits, len);
  246. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  247. return i;
  248. }
  249. static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
  250. int show= show_bits(s, 24);
  251. int pos= get_bits_count(s);
  252. int i= get_te0_golomb(s, r);
  253. int len= get_bits_count(s) - pos;
  254. int bits= show>>(24-len);
  255. print_bin(bits, len);
  256. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  257. return i;
  258. }
  259. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  260. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  261. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  262. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  263. #endif
  264. /**
  265. * write unsigned exp golomb code.
  266. */
  267. static inline void set_ue_golomb(PutBitContext *pb, int i){
  268. int e;
  269. assert(i>=0);
  270. #if 0
  271. if(i=0){
  272. put_bits(pb, 1, 1);
  273. return;
  274. }
  275. #endif
  276. if(i<256)
  277. put_bits(pb, ff_ue_golomb_len[i], i+1);
  278. else{
  279. e= av_log2(i+1);
  280. put_bits(pb, 2*e+1, i+1);
  281. }
  282. }
  283. /**
  284. * write truncated unsigned exp golomb code.
  285. */
  286. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  287. assert(range >= 1);
  288. assert(i<=range);
  289. if(range==2) put_bits(pb, 1, i^1);
  290. else set_ue_golomb(pb, i);
  291. }
  292. /**
  293. * write signed exp golomb code.
  294. */
  295. static inline void set_se_golomb(PutBitContext *pb, int i){
  296. #if 0
  297. if(i<=0) i= -2*i;
  298. else i= 2*i-1;
  299. #elif 1
  300. i= 2*i-1;
  301. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  302. #else
  303. i= 2*i-1;
  304. i^= (i>>31);
  305. #endif
  306. set_ue_golomb(pb, i);
  307. }
  308. /**
  309. * write unsigned golomb rice code (ffv1).
  310. */
  311. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  312. int e;
  313. assert(i>=0);
  314. e= i>>k;
  315. if(e<limit){
  316. put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
  317. }else{
  318. put_bits(pb, limit + esc_len, i - limit + 1);
  319. }
  320. }
  321. /**
  322. * write unsigned golomb rice code (jpegls).
  323. */
  324. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
  325. int e;
  326. assert(i>=0);
  327. e= (i>>k) + 1;
  328. if(e<limit){
  329. put_bits(pb, e, 1);
  330. if(k)
  331. put_bits(pb, k, i&((1<<k)-1));
  332. }else{
  333. put_bits(pb, limit , 1);
  334. put_bits(pb, esc_len, i - 1);
  335. }
  336. }
  337. /**
  338. * write signed golomb rice code (ffv1).
  339. */
  340. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
  341. int v;
  342. v = -2*i-1;
  343. v ^= (v>>31);
  344. set_ur_golomb(pb, v, k, limit, esc_len);
  345. }
  346. /**
  347. * write signed golomb rice code (flac).
  348. */
  349. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
  350. int v;
  351. v = -2*i-1;
  352. v ^= (v>>31);
  353. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  354. }