2
0

golomb.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * exp golomb vlc stuff
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2004 Alex Beregszaszi
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * @brief
  25. * exp golomb vlc stuff
  26. * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
  27. */
  28. #ifndef AVCODEC_GOLOMB_H
  29. #define AVCODEC_GOLOMB_H
  30. #include <stdint.h>
  31. #include "get_bits.h"
  32. #include "put_bits.h"
  33. #define INVALID_VLC 0x80000000
  34. extern const uint8_t ff_golomb_vlc_len[512];
  35. extern const uint8_t ff_ue_golomb_vlc_code[512];
  36. extern const int8_t ff_se_golomb_vlc_code[512];
  37. extern const uint8_t ff_ue_golomb_len[256];
  38. extern const uint8_t ff_interleaved_golomb_vlc_len[256];
  39. extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
  40. extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
  41. extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
  42. /**
  43. * Read an unsigned Exp-Golomb code in the range 0 to 8190.
  44. *
  45. * @returns the read value or a negative error code.
  46. */
  47. static inline int get_ue_golomb(GetBitContext *gb)
  48. {
  49. unsigned int buf;
  50. #if CACHED_BITSTREAM_READER
  51. buf = show_bits_long(gb, 32);
  52. if (buf >= (1 << 27)) {
  53. buf >>= 32 - 9;
  54. skip_bits_long(gb, ff_golomb_vlc_len[buf]);
  55. return ff_ue_golomb_vlc_code[buf];
  56. } else {
  57. int log = 2 * av_log2(buf) - 31;
  58. buf >>= log;
  59. buf--;
  60. skip_bits_long(gb, 32 - log);
  61. return buf;
  62. }
  63. #else
  64. OPEN_READER(re, gb);
  65. UPDATE_CACHE(re, gb);
  66. buf = GET_CACHE(re, gb);
  67. if (buf >= (1 << 27)) {
  68. buf >>= 32 - 9;
  69. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  70. CLOSE_READER(re, gb);
  71. return ff_ue_golomb_vlc_code[buf];
  72. } else {
  73. int log = 2 * av_log2(buf) - 31;
  74. LAST_SKIP_BITS(re, gb, 32 - log);
  75. CLOSE_READER(re, gb);
  76. if (log < 7) {
  77. av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
  78. return AVERROR_INVALIDDATA;
  79. }
  80. buf >>= log;
  81. buf--;
  82. return buf;
  83. }
  84. #endif
  85. }
  86. /**
  87. * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
  88. */
  89. static inline unsigned get_ue_golomb_long(GetBitContext *gb)
  90. {
  91. unsigned buf, log;
  92. buf = show_bits_long(gb, 32);
  93. log = 31 - av_log2(buf);
  94. skip_bits_long(gb, log);
  95. return get_bits_long(gb, log + 1) - 1;
  96. }
  97. /**
  98. * read unsigned exp golomb code, constraint to a max of 31.
  99. * the return value is undefined if the stored value exceeds 31.
  100. */
  101. static inline int get_ue_golomb_31(GetBitContext *gb)
  102. {
  103. unsigned int buf;
  104. #if CACHED_BITSTREAM_READER
  105. buf = show_bits_long(gb, 32);
  106. buf >>= 32 - 9;
  107. skip_bits_long(gb, ff_golomb_vlc_len[buf]);
  108. #else
  109. OPEN_READER(re, gb);
  110. UPDATE_CACHE(re, gb);
  111. buf = GET_CACHE(re, gb);
  112. buf >>= 32 - 9;
  113. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  114. CLOSE_READER(re, gb);
  115. #endif
  116. return ff_ue_golomb_vlc_code[buf];
  117. }
  118. static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
  119. {
  120. uint32_t buf;
  121. #if CACHED_BITSTREAM_READER
  122. buf = show_bits_long(gb, 32);
  123. if (buf & 0xAA800000) {
  124. buf >>= 32 - 8;
  125. skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
  126. return ff_interleaved_ue_golomb_vlc_code[buf];
  127. } else {
  128. unsigned ret = 1;
  129. do {
  130. buf >>= 32 - 8;
  131. skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  132. if (ff_interleaved_golomb_vlc_len[buf] != 9) {
  133. ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  134. ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
  135. break;
  136. }
  137. ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  138. buf = show_bits_long(gb, 32);
  139. } while (get_bits_left(gb) > 0);
  140. return ret - 1;
  141. }
  142. #else
  143. OPEN_READER(re, gb);
  144. UPDATE_CACHE(re, gb);
  145. buf = GET_CACHE(re, gb);
  146. if (buf & 0xAA800000) {
  147. buf >>= 32 - 8;
  148. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  149. CLOSE_READER(re, gb);
  150. return ff_interleaved_ue_golomb_vlc_code[buf];
  151. } else {
  152. unsigned ret = 1;
  153. do {
  154. buf >>= 32 - 8;
  155. LAST_SKIP_BITS(re, gb,
  156. FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  157. if (ff_interleaved_golomb_vlc_len[buf] != 9) {
  158. ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  159. ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
  160. break;
  161. }
  162. ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  163. UPDATE_CACHE(re, gb);
  164. buf = GET_CACHE(re, gb);
  165. } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
  166. CLOSE_READER(re, gb);
  167. return ret - 1;
  168. }
  169. #endif
  170. }
  171. /**
  172. * read unsigned truncated exp golomb code.
  173. */
  174. static inline int get_te0_golomb(GetBitContext *gb, int range)
  175. {
  176. av_assert2(range >= 1);
  177. if (range == 1)
  178. return 0;
  179. else if (range == 2)
  180. return get_bits1(gb) ^ 1;
  181. else
  182. return get_ue_golomb(gb);
  183. }
  184. /**
  185. * read unsigned truncated exp golomb code.
  186. */
  187. static inline int get_te_golomb(GetBitContext *gb, int range)
  188. {
  189. av_assert2(range >= 1);
  190. if (range == 2)
  191. return get_bits1(gb) ^ 1;
  192. else
  193. return get_ue_golomb(gb);
  194. }
  195. /**
  196. * read signed exp golomb code.
  197. */
  198. static inline int get_se_golomb(GetBitContext *gb)
  199. {
  200. unsigned int buf;
  201. #if CACHED_BITSTREAM_READER
  202. buf = show_bits_long(gb, 32);
  203. if (buf >= (1 << 27)) {
  204. buf >>= 32 - 9;
  205. skip_bits_long(gb, ff_golomb_vlc_len[buf]);
  206. return ff_se_golomb_vlc_code[buf];
  207. } else {
  208. int log = 2 * av_log2(buf) - 31;
  209. buf >>= log;
  210. skip_bits_long(gb, 32 - log);
  211. if (buf & 1)
  212. buf = -(buf >> 1);
  213. else
  214. buf = (buf >> 1);
  215. return buf;
  216. }
  217. #else
  218. OPEN_READER(re, gb);
  219. UPDATE_CACHE(re, gb);
  220. buf = GET_CACHE(re, gb);
  221. if (buf >= (1 << 27)) {
  222. buf >>= 32 - 9;
  223. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  224. CLOSE_READER(re, gb);
  225. return ff_se_golomb_vlc_code[buf];
  226. } else {
  227. int log = av_log2(buf), sign;
  228. LAST_SKIP_BITS(re, gb, 31 - log);
  229. UPDATE_CACHE(re, gb);
  230. buf = GET_CACHE(re, gb);
  231. buf >>= log;
  232. LAST_SKIP_BITS(re, gb, 32 - log);
  233. CLOSE_READER(re, gb);
  234. sign = -(buf & 1);
  235. buf = ((buf >> 1) ^ sign) - sign;
  236. return buf;
  237. }
  238. #endif
  239. }
  240. static inline int get_se_golomb_long(GetBitContext *gb)
  241. {
  242. unsigned int buf = get_ue_golomb_long(gb);
  243. int sign = (buf & 1) - 1;
  244. return ((buf >> 1) ^ sign) + 1;
  245. }
  246. static inline int get_interleaved_se_golomb(GetBitContext *gb)
  247. {
  248. unsigned int buf;
  249. #if CACHED_BITSTREAM_READER
  250. buf = show_bits_long(gb, 32);
  251. if (buf & 0xAA800000) {
  252. buf >>= 32 - 8;
  253. skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
  254. return ff_interleaved_se_golomb_vlc_code[buf];
  255. } else {
  256. int log;
  257. skip_bits(gb, 8);
  258. buf |= 1 | show_bits_long(gb, 24);
  259. if ((buf & 0xAAAAAAAA) == 0)
  260. return INVALID_VLC;
  261. for (log = 31; (buf & 0x80000000) == 0; log--)
  262. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  263. skip_bits_long(gb, 63 - 2 * log - 8);
  264. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  265. }
  266. #else
  267. OPEN_READER(re, gb);
  268. UPDATE_CACHE(re, gb);
  269. buf = GET_CACHE(re, gb);
  270. if (buf & 0xAA800000) {
  271. buf >>= 32 - 8;
  272. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  273. CLOSE_READER(re, gb);
  274. return ff_interleaved_se_golomb_vlc_code[buf];
  275. } else {
  276. int log;
  277. LAST_SKIP_BITS(re, gb, 8);
  278. UPDATE_CACHE(re, gb);
  279. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  280. if ((buf & 0xAAAAAAAA) == 0)
  281. return INVALID_VLC;
  282. for (log = 31; (buf & 0x80000000) == 0; log--)
  283. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  284. LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
  285. CLOSE_READER(re, gb);
  286. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  287. }
  288. #endif
  289. }
  290. static inline int dirac_get_se_golomb(GetBitContext *gb)
  291. {
  292. uint32_t ret = get_interleaved_ue_golomb(gb);
  293. if (ret) {
  294. int sign = -get_bits1(gb);
  295. ret = (ret ^ sign) - sign;
  296. }
  297. return ret;
  298. }
  299. /**
  300. * read unsigned golomb rice code (ffv1).
  301. */
  302. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
  303. int esc_len)
  304. {
  305. unsigned int buf;
  306. int log;
  307. #if CACHED_BITSTREAM_READER
  308. buf = show_bits_long(gb, 32);
  309. log = av_log2(buf);
  310. if (log > 31 - limit) {
  311. buf >>= log - k;
  312. buf += (30 - log) << k;
  313. skip_bits_long(gb, 32 + k - log);
  314. return buf;
  315. } else {
  316. skip_bits_long(gb, limit);
  317. buf = get_bits_long(gb, esc_len);
  318. return buf + limit - 1;
  319. }
  320. #else
  321. OPEN_READER(re, gb);
  322. UPDATE_CACHE(re, gb);
  323. buf = GET_CACHE(re, gb);
  324. log = av_log2(buf);
  325. if (log > 31 - limit) {
  326. buf >>= log - k;
  327. buf += (30U - log) << k;
  328. LAST_SKIP_BITS(re, gb, 32 + k - log);
  329. CLOSE_READER(re, gb);
  330. return buf;
  331. } else {
  332. LAST_SKIP_BITS(re, gb, limit);
  333. UPDATE_CACHE(re, gb);
  334. buf = SHOW_UBITS(re, gb, esc_len);
  335. LAST_SKIP_BITS(re, gb, esc_len);
  336. CLOSE_READER(re, gb);
  337. return buf + limit - 1;
  338. }
  339. #endif
  340. }
  341. /**
  342. * read unsigned golomb rice code (jpegls).
  343. */
  344. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
  345. int esc_len)
  346. {
  347. unsigned int buf;
  348. int log;
  349. #if CACHED_BITSTREAM_READER
  350. buf = show_bits_long(gb, 32);
  351. log = av_log2(buf);
  352. if (log - k >= 1 && 32 - log < limit) {
  353. buf >>= log - k;
  354. buf += (30 - log) << k;
  355. skip_bits_long(gb, 32 + k - log);
  356. return buf;
  357. } else {
  358. int i;
  359. for (i = 0;
  360. i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
  361. i++);
  362. if (i < limit - 1) {
  363. buf = get_bits_long(gb, k);
  364. return buf + (i << k);
  365. } else if (i == limit - 1) {
  366. buf = get_bits_long(gb, esc_len);
  367. return buf + 1;
  368. } else
  369. return -1;
  370. }
  371. #else
  372. OPEN_READER(re, gb);
  373. UPDATE_CACHE(re, gb);
  374. buf = GET_CACHE(re, gb);
  375. log = av_log2(buf);
  376. av_assert2(k <= 31);
  377. if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
  378. 32 - log < limit) {
  379. buf >>= log - k;
  380. buf += (30U - log) << k;
  381. LAST_SKIP_BITS(re, gb, 32 + k - log);
  382. CLOSE_READER(re, gb);
  383. return buf;
  384. } else {
  385. int i;
  386. for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
  387. if (gb->size_in_bits <= re_index) {
  388. CLOSE_READER(re, gb);
  389. return -1;
  390. }
  391. LAST_SKIP_BITS(re, gb, 1);
  392. UPDATE_CACHE(re, gb);
  393. }
  394. SKIP_BITS(re, gb, 1);
  395. if (i < limit - 1) {
  396. if (k) {
  397. if (k > MIN_CACHE_BITS - 1) {
  398. buf = SHOW_UBITS(re, gb, 16) << (k-16);
  399. LAST_SKIP_BITS(re, gb, 16);
  400. UPDATE_CACHE(re, gb);
  401. buf |= SHOW_UBITS(re, gb, k-16);
  402. LAST_SKIP_BITS(re, gb, k-16);
  403. } else {
  404. buf = SHOW_UBITS(re, gb, k);
  405. LAST_SKIP_BITS(re, gb, k);
  406. }
  407. } else {
  408. buf = 0;
  409. }
  410. buf += ((SUINT)i << k);
  411. } else if (i == limit - 1) {
  412. buf = SHOW_UBITS(re, gb, esc_len);
  413. LAST_SKIP_BITS(re, gb, esc_len);
  414. buf ++;
  415. } else {
  416. buf = -1;
  417. }
  418. CLOSE_READER(re, gb);
  419. return buf;
  420. }
  421. #endif
  422. }
  423. /**
  424. * read signed golomb rice code (ffv1).
  425. */
  426. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
  427. int esc_len)
  428. {
  429. unsigned v = get_ur_golomb(gb, k, limit, esc_len);
  430. return (v >> 1) ^ -(v & 1);
  431. }
  432. /**
  433. * read signed golomb rice code (flac).
  434. */
  435. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
  436. int esc_len)
  437. {
  438. unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
  439. return (v >> 1) ^ -(v & 1);
  440. }
  441. /**
  442. * read unsigned golomb rice code (shorten).
  443. */
  444. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
  445. {
  446. return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  447. }
  448. /**
  449. * read signed golomb rice code (shorten).
  450. */
  451. static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
  452. {
  453. int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  454. return (uvar >> 1) ^ -(uvar & 1);
  455. }
  456. #ifdef TRACE
  457. static inline int get_ue(GetBitContext *s, const char *file, const char *func,
  458. int line)
  459. {
  460. int show = show_bits(s, 24);
  461. int pos = get_bits_count(s);
  462. int i = get_ue_golomb(s);
  463. int len = get_bits_count(s) - pos;
  464. int bits = show >> (24 - len);
  465. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
  466. bits, len, i, pos, file, func, line);
  467. return i;
  468. }
  469. static inline int get_se(GetBitContext *s, const char *file, const char *func,
  470. int line)
  471. {
  472. int show = show_bits(s, 24);
  473. int pos = get_bits_count(s);
  474. int i = get_se_golomb(s);
  475. int len = get_bits_count(s) - pos;
  476. int bits = show >> (24 - len);
  477. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
  478. bits, len, i, pos, file, func, line);
  479. return i;
  480. }
  481. static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
  482. int line)
  483. {
  484. int show = show_bits(s, 24);
  485. int pos = get_bits_count(s);
  486. int i = get_te0_golomb(s, r);
  487. int len = get_bits_count(s) - pos;
  488. int bits = show >> (24 - len);
  489. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
  490. bits, len, i, pos, file, func, line);
  491. return i;
  492. }
  493. #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
  494. #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
  495. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
  496. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
  497. #endif /* TRACE */
  498. /**
  499. * write unsigned exp golomb code. 2^16 - 2 at most
  500. */
  501. static inline void set_ue_golomb(PutBitContext *pb, int i)
  502. {
  503. av_assert2(i >= 0);
  504. av_assert2(i <= 0xFFFE);
  505. if (i < 256)
  506. put_bits(pb, ff_ue_golomb_len[i], i + 1);
  507. else {
  508. int e = av_log2(i + 1);
  509. put_bits(pb, 2 * e + 1, i + 1);
  510. }
  511. }
  512. /**
  513. * write unsigned exp golomb code. 2^32-2 at most.
  514. */
  515. static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
  516. {
  517. av_assert2(i <= (UINT32_MAX - 1));
  518. if (i < 256)
  519. put_bits(pb, ff_ue_golomb_len[i], i + 1);
  520. else {
  521. int e = av_log2(i + 1);
  522. put_bits64(pb, 2 * e + 1, i + 1);
  523. }
  524. }
  525. /**
  526. * write truncated unsigned exp golomb code.
  527. */
  528. static inline void set_te_golomb(PutBitContext *pb, int i, int range)
  529. {
  530. av_assert2(range >= 1);
  531. av_assert2(i <= range);
  532. if (range == 2)
  533. put_bits(pb, 1, i ^ 1);
  534. else
  535. set_ue_golomb(pb, i);
  536. }
  537. /**
  538. * write signed exp golomb code. 16 bits at most.
  539. */
  540. static inline void set_se_golomb(PutBitContext *pb, int i)
  541. {
  542. i = 2 * i - 1;
  543. if (i < 0)
  544. i ^= -1; //FIXME check if gcc does the right thing
  545. set_ue_golomb(pb, i);
  546. }
  547. /**
  548. * write unsigned golomb rice code (ffv1).
  549. */
  550. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
  551. int esc_len)
  552. {
  553. int e;
  554. av_assert2(i >= 0);
  555. e = i >> k;
  556. if (e < limit)
  557. put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
  558. else
  559. put_bits(pb, limit + esc_len, i - limit + 1);
  560. }
  561. /**
  562. * write unsigned golomb rice code (jpegls).
  563. */
  564. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
  565. int limit, int esc_len)
  566. {
  567. int e;
  568. av_assert2(i >= 0);
  569. e = (i >> k) + 1;
  570. if (e < limit) {
  571. while (e > 31) {
  572. put_bits(pb, 31, 0);
  573. e -= 31;
  574. }
  575. put_bits(pb, e, 1);
  576. if (k)
  577. put_sbits(pb, k, i);
  578. } else {
  579. while (limit > 31) {
  580. put_bits(pb, 31, 0);
  581. limit -= 31;
  582. }
  583. put_bits(pb, limit, 1);
  584. put_bits(pb, esc_len, i - 1);
  585. }
  586. }
  587. /**
  588. * write signed golomb rice code (ffv1).
  589. */
  590. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
  591. int esc_len)
  592. {
  593. int v;
  594. v = -2 * i - 1;
  595. v ^= (v >> 31);
  596. set_ur_golomb(pb, v, k, limit, esc_len);
  597. }
  598. /**
  599. * write signed golomb rice code (flac).
  600. */
  601. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
  602. int limit, int esc_len)
  603. {
  604. int v;
  605. v = -2 * i - 1;
  606. v ^= (v >> 31);
  607. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  608. }
  609. #endif /* AVCODEC_GOLOMB_H */