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.

216 lines
5.2KB

  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. extern const uint8_t ff_golomb_vlc_len[512];
  27. extern const uint8_t ff_ue_golomb_vlc_code[512];
  28. extern const int8_t ff_se_golomb_vlc_code[512];
  29. extern const uint8_t ff_ue_golomb_len[256];
  30. /**
  31. * read unsigned exp golomb code.
  32. */
  33. static inline int get_ue_golomb(GetBitContext *gb){
  34. unsigned int buf;
  35. int log;
  36. OPEN_READER(re, gb);
  37. UPDATE_CACHE(re, gb);
  38. buf=GET_CACHE(re, gb);
  39. if(buf >= (1<<27)){
  40. buf >>= 32 - 9;
  41. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  42. CLOSE_READER(re, gb);
  43. return ff_ue_golomb_vlc_code[buf];
  44. }else{
  45. log= 2*av_log2(buf) - 31;
  46. buf>>= log;
  47. buf--;
  48. LAST_SKIP_BITS(re, gb, 32 - log);
  49. CLOSE_READER(re, gb);
  50. return buf;
  51. }
  52. }
  53. /**
  54. * read unsigned truncated exp golomb code.
  55. */
  56. static inline int get_te0_golomb(GetBitContext *gb, int range){
  57. assert(range >= 1);
  58. if(range==1) return 0;
  59. else if(range==2) return get_bits1(gb)^1;
  60. else return get_ue_golomb(gb);
  61. }
  62. /**
  63. * read unsigned truncated exp golomb code.
  64. */
  65. static inline int get_te_golomb(GetBitContext *gb, int range){
  66. assert(range >= 1);
  67. if(range==2) return get_bits1(gb)^1;
  68. else return get_ue_golomb(gb);
  69. }
  70. /**
  71. * read signed exp golomb code.
  72. */
  73. static inline int get_se_golomb(GetBitContext *gb){
  74. unsigned int buf;
  75. int log;
  76. OPEN_READER(re, gb);
  77. UPDATE_CACHE(re, gb);
  78. buf=GET_CACHE(re, gb);
  79. if(buf >= (1<<27)){
  80. buf >>= 32 - 9;
  81. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  82. CLOSE_READER(re, gb);
  83. return ff_se_golomb_vlc_code[buf];
  84. }else{
  85. log= 2*av_log2(buf) - 31;
  86. buf>>= log;
  87. LAST_SKIP_BITS(re, gb, 32 - log);
  88. CLOSE_READER(re, gb);
  89. if(buf&1) buf= -(buf>>1);
  90. else buf= (buf>>1);
  91. return buf;
  92. }
  93. }
  94. #ifdef TRACE
  95. static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
  96. int show= show_bits(s, 24);
  97. int pos= get_bits_count(s);
  98. int i= get_ue_golomb(s);
  99. int len= get_bits_count(s) - pos;
  100. int bits= show>>(24-len);
  101. print_bin(bits, len);
  102. printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  103. return i;
  104. }
  105. static inline int get_se(GetBitContext *s, char *file, char *func, int line){
  106. int show= show_bits(s, 24);
  107. int pos= get_bits_count(s);
  108. int i= get_se_golomb(s);
  109. int len= get_bits_count(s) - pos;
  110. int bits= show>>(24-len);
  111. print_bin(bits, len);
  112. printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  113. return i;
  114. }
  115. static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
  116. int show= show_bits(s, 24);
  117. int pos= get_bits_count(s);
  118. int i= get_te0_golomb(s, r);
  119. int len= get_bits_count(s) - pos;
  120. int bits= show>>(24-len);
  121. print_bin(bits, len);
  122. printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
  123. return i;
  124. }
  125. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  126. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  127. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  128. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  129. #endif
  130. /**
  131. * write unsigned exp golomb code.
  132. */
  133. static inline void set_ue_golomb(PutBitContext *pb, int i){
  134. int e;
  135. assert(i>=0);
  136. #if 0
  137. if(i=0){
  138. put_bits(pb, 1, 1);
  139. return;
  140. }
  141. #endif
  142. if(i<256)
  143. put_bits(pb, ff_ue_golomb_len[i], i+1);
  144. else{
  145. e= av_log2(i+1);
  146. put_bits(pb, 2*e+1, i+1);
  147. }
  148. }
  149. /**
  150. * write truncated unsigned exp golomb code.
  151. */
  152. static inline void set_te_golomb(PutBitContext *pb, int i, int range){
  153. assert(range >= 1);
  154. assert(i<=range);
  155. if(range==2) put_bits(pb, 1, i^1);
  156. else set_ue_golomb(pb, i);
  157. }
  158. /**
  159. * write signed exp golomb code.
  160. */
  161. static inline void set_se_golomb(PutBitContext *pb, int i){
  162. #if 0
  163. if(i<=0) i= -2*i;
  164. else i= 2*i-1;
  165. #elif 1
  166. i= 2*i-1;
  167. if(i<0) i^= -1; //FIXME check if gcc does the right thing
  168. #else
  169. i= 2*i-1;
  170. i^= (i>>31);
  171. #endif
  172. set_ue_golomb(pb, i);
  173. }