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.

282 lines
6.9KB

  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. #ifdef TRACE
  145. static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
  146. int show= show_bits(s, 24);
  147. int pos= get_bits_count(s);
  148. int i= get_ue_golomb(s);
  149. int len= get_bits_count(s) - pos;
  150. int bits= show>>(24-len);
  151. print_bin(bits, len);
  152. printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  153. return i;
  154. }
  155. static inline int get_se(GetBitContext *s, char *file, char *func, int line){
  156. int show= show_bits(s, 24);
  157. int pos= get_bits_count(s);
  158. int i= get_se_golomb(s);
  159. int len= get_bits_count(s) - pos;
  160. int bits= show>>(24-len);
  161. print_bin(bits, len);
  162. printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  163. return i;
  164. }
  165. static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
  166. int show= show_bits(s, 24);
  167. int pos= get_bits_count(s);
  168. int i= get_te0_golomb(s, r);
  169. int len= get_bits_count(s) - pos;
  170. int bits= show>>(24-len);
  171. print_bin(bits, len);
  172. printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  173. return i;
  174. }
  175. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  176. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  177. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  178. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  179. #endif
  180. /**
  181. * write unsigned exp golomb code.
  182. */
  183. static inline void set_ue_golomb(PutBitContext *pb, int i){
  184. int e;
  185. assert(i>=0);
  186. #if 0
  187. if(i=0){
  188. put_bits(pb, 1, 1);
  189. return;
  190. }
  191. #endif
  192. if(i<256)
  193. put_bits(pb, ff_ue_golomb_len[i], i+1);
  194. else{
  195. e= av_log2(i+1);
  196. put_bits(pb, 2*e+1, i+1);
  197. }
  198. }
  199. /**
  200. * write truncated unsigned exp golomb code.
  201. */
  202. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  203. assert(range >= 1);
  204. assert(i<=range);
  205. if(range==2) put_bits(pb, 1, i^1);
  206. else set_ue_golomb(pb, i);
  207. }
  208. /**
  209. * write signed exp golomb code.
  210. */
  211. static inline void set_se_golomb(PutBitContext *pb, int i){
  212. #if 0
  213. if(i<=0) i= -2*i;
  214. else i= 2*i-1;
  215. #elif 1
  216. i= 2*i-1;
  217. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  218. #else
  219. i= 2*i-1;
  220. i^= (i>>31);
  221. #endif
  222. set_ue_golomb(pb, i);
  223. }