golomb.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. skip_bits_long(gb, 32 - log);
  59. if (log < 7)
  60. return AVERROR_INVALIDDATA;
  61. buf >>= log;
  62. buf--;
  63. return buf;
  64. }
  65. #else
  66. OPEN_READER(re, gb);
  67. UPDATE_CACHE(re, gb);
  68. buf = GET_CACHE(re, gb);
  69. if (buf >= (1 << 27)) {
  70. buf >>= 32 - 9;
  71. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  72. CLOSE_READER(re, gb);
  73. return ff_ue_golomb_vlc_code[buf];
  74. } else {
  75. int log = 2 * av_log2(buf) - 31;
  76. LAST_SKIP_BITS(re, gb, 32 - log);
  77. CLOSE_READER(re, gb);
  78. if (log < 7)
  79. return AVERROR_INVALIDDATA;
  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. * If the value encountered is not in 0..31, the return value
  100. * is outside the range 0..30.
  101. */
  102. static inline int get_ue_golomb_31(GetBitContext *gb)
  103. {
  104. unsigned int buf;
  105. #if CACHED_BITSTREAM_READER
  106. buf = show_bits_long(gb, 32);
  107. buf >>= 32 - 9;
  108. skip_bits_long(gb, ff_golomb_vlc_len[buf]);
  109. #else
  110. OPEN_READER(re, gb);
  111. UPDATE_CACHE(re, gb);
  112. buf = GET_CACHE(re, gb);
  113. buf >>= 32 - 9;
  114. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  115. CLOSE_READER(re, gb);
  116. #endif
  117. return ff_ue_golomb_vlc_code[buf];
  118. }
  119. static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
  120. {
  121. uint32_t buf;
  122. #if CACHED_BITSTREAM_READER
  123. buf = show_bits_long(gb, 32);
  124. if (buf & 0xAA800000) {
  125. buf >>= 32 - 8;
  126. skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
  127. return ff_interleaved_ue_golomb_vlc_code[buf];
  128. } else {
  129. unsigned ret = 1;
  130. do {
  131. buf >>= 32 - 8;
  132. skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  133. if (ff_interleaved_golomb_vlc_len[buf] != 9) {
  134. ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  135. ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
  136. break;
  137. }
  138. ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  139. buf = show_bits_long(gb, 32);
  140. } while (get_bits_left(gb) > 0);
  141. return ret - 1;
  142. }
  143. #else
  144. OPEN_READER(re, gb);
  145. UPDATE_CACHE(re, gb);
  146. buf = GET_CACHE(re, gb);
  147. if (buf & 0xAA800000) {
  148. buf >>= 32 - 8;
  149. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  150. CLOSE_READER(re, gb);
  151. return ff_interleaved_ue_golomb_vlc_code[buf];
  152. } else {
  153. unsigned ret = 1;
  154. do {
  155. buf >>= 32 - 8;
  156. LAST_SKIP_BITS(re, gb,
  157. FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  158. if (ff_interleaved_golomb_vlc_len[buf] != 9) {
  159. ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  160. ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
  161. break;
  162. }
  163. ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  164. UPDATE_CACHE(re, gb);
  165. buf = GET_CACHE(re, gb);
  166. } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
  167. CLOSE_READER(re, gb);
  168. return ret - 1;
  169. }
  170. #endif
  171. }
  172. /**
  173. * read unsigned truncated exp golomb code.
  174. */
  175. static inline int get_te0_golomb(GetBitContext *gb, int range)
  176. {
  177. av_assert2(range >= 1);
  178. if (range == 1)
  179. return 0;
  180. else if (range == 2)
  181. return get_bits1(gb) ^ 1;
  182. else
  183. return get_ue_golomb(gb);
  184. }
  185. /**
  186. * read unsigned truncated exp golomb code.
  187. */
  188. static inline int get_te_golomb(GetBitContext *gb, int range)
  189. {
  190. av_assert2(range >= 1);
  191. if (range == 2)
  192. return get_bits1(gb) ^ 1;
  193. else
  194. return get_ue_golomb(gb);
  195. }
  196. /**
  197. * read signed exp golomb code.
  198. */
  199. static inline int get_se_golomb(GetBitContext *gb)
  200. {
  201. unsigned int buf;
  202. #if CACHED_BITSTREAM_READER
  203. buf = show_bits_long(gb, 32);
  204. if (buf >= (1 << 27)) {
  205. buf >>= 32 - 9;
  206. skip_bits_long(gb, ff_golomb_vlc_len[buf]);
  207. return ff_se_golomb_vlc_code[buf];
  208. } else {
  209. int log = 2 * av_log2(buf) - 31;
  210. buf >>= log;
  211. skip_bits_long(gb, 32 - log);
  212. if (buf & 1)
  213. buf = -(buf >> 1);
  214. else
  215. buf = (buf >> 1);
  216. return buf;
  217. }
  218. #else
  219. OPEN_READER(re, gb);
  220. UPDATE_CACHE(re, gb);
  221. buf = GET_CACHE(re, gb);
  222. if (buf >= (1 << 27)) {
  223. buf >>= 32 - 9;
  224. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  225. CLOSE_READER(re, gb);
  226. return ff_se_golomb_vlc_code[buf];
  227. } else {
  228. int log = av_log2(buf), sign;
  229. LAST_SKIP_BITS(re, gb, 31 - log);
  230. UPDATE_CACHE(re, gb);
  231. buf = GET_CACHE(re, gb);
  232. buf >>= log;
  233. LAST_SKIP_BITS(re, gb, 32 - log);
  234. CLOSE_READER(re, gb);
  235. sign = -(buf & 1);
  236. buf = ((buf >> 1) ^ sign) - sign;
  237. return buf;
  238. }
  239. #endif
  240. }
  241. static inline int get_se_golomb_long(GetBitContext *gb)
  242. {
  243. unsigned int buf = get_ue_golomb_long(gb);
  244. int sign = (buf & 1) - 1;
  245. return ((buf >> 1) ^ sign) + 1;
  246. }
  247. static inline int get_interleaved_se_golomb(GetBitContext *gb)
  248. {
  249. unsigned int buf;
  250. #if CACHED_BITSTREAM_READER
  251. buf = show_bits_long(gb, 32);
  252. if (buf & 0xAA800000) {
  253. buf >>= 32 - 8;
  254. skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
  255. return ff_interleaved_se_golomb_vlc_code[buf];
  256. } else {
  257. int log;
  258. skip_bits(gb, 8);
  259. buf |= 1 | show_bits(gb, 24);
  260. if ((buf & 0xAAAAAAAA) == 0)
  261. return INVALID_VLC;
  262. for (log = 31; (buf & 0x80000000) == 0; log--)
  263. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  264. skip_bits_long(gb, 63 - 2 * log - 8);
  265. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  266. }
  267. #else
  268. OPEN_READER(re, gb);
  269. UPDATE_CACHE(re, gb);
  270. buf = GET_CACHE(re, gb);
  271. if (buf & 0xAA800000) {
  272. buf >>= 32 - 8;
  273. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  274. CLOSE_READER(re, gb);
  275. return ff_interleaved_se_golomb_vlc_code[buf];
  276. } else {
  277. int log;
  278. LAST_SKIP_BITS(re, gb, 8);
  279. UPDATE_CACHE(re, gb);
  280. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  281. if ((buf & 0xAAAAAAAA) == 0)
  282. return INVALID_VLC;
  283. for (log = 31; (buf & 0x80000000) == 0; log--)
  284. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  285. LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
  286. CLOSE_READER(re, gb);
  287. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  288. }
  289. #endif
  290. }
  291. static inline int dirac_get_se_golomb(GetBitContext *gb)
  292. {
  293. uint32_t ret = get_interleaved_ue_golomb(gb);
  294. if (ret) {
  295. int sign = -get_bits1(gb);
  296. ret = (ret ^ sign) - sign;
  297. }
  298. return ret;
  299. }
  300. /**
  301. * read unsigned golomb rice code (ffv1).
  302. */
  303. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
  304. int esc_len)
  305. {
  306. unsigned int buf;
  307. int log;
  308. #if CACHED_BITSTREAM_READER
  309. buf = show_bits_long(gb, 32);
  310. log = av_log2(buf);
  311. if (log > 31 - limit) {
  312. buf >>= log - k;
  313. buf += (30 - log) << k;
  314. skip_bits_long(gb, 32 + k - log);
  315. return buf;
  316. } else {
  317. skip_bits_long(gb, limit);
  318. buf = get_bits_long(gb, esc_len);
  319. return buf + limit - 1;
  320. }
  321. #else
  322. OPEN_READER(re, gb);
  323. UPDATE_CACHE(re, gb);
  324. buf = GET_CACHE(re, gb);
  325. log = av_log2(buf);
  326. if (log > 31 - limit) {
  327. buf >>= log - k;
  328. buf += (30U - log) << k;
  329. LAST_SKIP_BITS(re, gb, 32 + k - log);
  330. CLOSE_READER(re, gb);
  331. return buf;
  332. } else {
  333. LAST_SKIP_BITS(re, gb, limit);
  334. UPDATE_CACHE(re, gb);
  335. buf = SHOW_UBITS(re, gb, esc_len);
  336. LAST_SKIP_BITS(re, gb, esc_len);
  337. CLOSE_READER(re, gb);
  338. return buf + limit - 1;
  339. }
  340. #endif
  341. }
  342. /**
  343. * read unsigned golomb rice code (jpegls).
  344. */
  345. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
  346. int esc_len)
  347. {
  348. unsigned int buf;
  349. int log;
  350. #if CACHED_BITSTREAM_READER
  351. buf = show_bits_long(gb, 32);
  352. log = av_log2(buf);
  353. if (log - k >= 1 && 32 - log < limit) {
  354. buf >>= log - k;
  355. buf += (30 - log) << k;
  356. skip_bits_long(gb, 32 + k - log);
  357. return buf;
  358. } else {
  359. int i;
  360. for (i = 0;
  361. i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
  362. i++);
  363. if (i < limit - 1) {
  364. buf = get_bits_long(gb, k);
  365. return buf + (i << k);
  366. } else if (i == limit - 1) {
  367. buf = get_bits_long(gb, esc_len);
  368. return buf + 1;
  369. } else
  370. return -1;
  371. }
  372. #else
  373. OPEN_READER(re, gb);
  374. UPDATE_CACHE(re, gb);
  375. buf = GET_CACHE(re, gb);
  376. log = av_log2(buf);
  377. av_assert2(k <= 31);
  378. if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
  379. 32 - log < limit) {
  380. buf >>= log - k;
  381. buf += (30U - log) << k;
  382. LAST_SKIP_BITS(re, gb, 32 + k - log);
  383. CLOSE_READER(re, gb);
  384. return buf;
  385. } else {
  386. int i;
  387. for (i = 0; i + MIN_CACHE_BITS <= limit && SHOW_UBITS(re, gb, MIN_CACHE_BITS) == 0; i += MIN_CACHE_BITS) {
  388. if (gb->size_in_bits <= re_index) {
  389. CLOSE_READER(re, gb);
  390. return -1;
  391. }
  392. LAST_SKIP_BITS(re, gb, MIN_CACHE_BITS);
  393. UPDATE_CACHE(re, gb);
  394. }
  395. for (; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
  396. SKIP_BITS(re, gb, 1);
  397. }
  398. LAST_SKIP_BITS(re, gb, 1);
  399. UPDATE_CACHE(re, gb);
  400. if (i < limit - 1) {
  401. if (k) {
  402. if (k > MIN_CACHE_BITS - 1) {
  403. buf = SHOW_UBITS(re, gb, 16) << (k-16);
  404. LAST_SKIP_BITS(re, gb, 16);
  405. UPDATE_CACHE(re, gb);
  406. buf |= SHOW_UBITS(re, gb, k-16);
  407. LAST_SKIP_BITS(re, gb, k-16);
  408. } else {
  409. buf = SHOW_UBITS(re, gb, k);
  410. LAST_SKIP_BITS(re, gb, k);
  411. }
  412. } else {
  413. buf = 0;
  414. }
  415. buf += ((SUINT)i << k);
  416. } else if (i == limit - 1) {
  417. buf = SHOW_UBITS(re, gb, esc_len);
  418. LAST_SKIP_BITS(re, gb, esc_len);
  419. buf ++;
  420. } else {
  421. buf = -1;
  422. }
  423. CLOSE_READER(re, gb);
  424. return buf;
  425. }
  426. #endif
  427. }
  428. /**
  429. * read signed golomb rice code (ffv1).
  430. */
  431. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
  432. int esc_len)
  433. {
  434. unsigned v = get_ur_golomb(gb, k, limit, esc_len);
  435. return (v >> 1) ^ -(v & 1);
  436. }
  437. /**
  438. * read signed golomb rice code (flac).
  439. */
  440. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
  441. int esc_len)
  442. {
  443. unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
  444. return (v >> 1) ^ -(v & 1);
  445. }
  446. /**
  447. * read unsigned golomb rice code (shorten).
  448. */
  449. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
  450. {
  451. return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  452. }
  453. /**
  454. * read signed golomb rice code (shorten).
  455. */
  456. static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
  457. {
  458. int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  459. return (uvar >> 1) ^ -(uvar & 1);
  460. }
  461. #ifdef TRACE
  462. static inline int get_ue(GetBitContext *s, const char *file, const char *func,
  463. int line)
  464. {
  465. int show = show_bits(s, 24);
  466. int pos = get_bits_count(s);
  467. int i = get_ue_golomb(s);
  468. int len = get_bits_count(s) - pos;
  469. int bits = show >> (24 - len);
  470. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
  471. bits, len, i, pos, file, func, line);
  472. return i;
  473. }
  474. static inline int get_se(GetBitContext *s, const char *file, const char *func,
  475. int line)
  476. {
  477. int show = show_bits(s, 24);
  478. int pos = get_bits_count(s);
  479. int i = get_se_golomb(s);
  480. int len = get_bits_count(s) - pos;
  481. int bits = show >> (24 - len);
  482. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
  483. bits, len, i, pos, file, func, line);
  484. return i;
  485. }
  486. static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
  487. int line)
  488. {
  489. int show = show_bits(s, 24);
  490. int pos = get_bits_count(s);
  491. int i = get_te0_golomb(s, r);
  492. int len = get_bits_count(s) - pos;
  493. int bits = show >> (24 - len);
  494. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
  495. bits, len, i, pos, file, func, line);
  496. return i;
  497. }
  498. #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
  499. #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
  500. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
  501. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
  502. #endif /* TRACE */
  503. /**
  504. * write unsigned exp golomb code. 2^16 - 2 at most
  505. */
  506. static inline void set_ue_golomb(PutBitContext *pb, int i)
  507. {
  508. av_assert2(i >= 0);
  509. av_assert2(i <= 0xFFFE);
  510. if (i < 256)
  511. put_bits(pb, ff_ue_golomb_len[i], i + 1);
  512. else {
  513. int e = av_log2(i + 1);
  514. put_bits(pb, 2 * e + 1, i + 1);
  515. }
  516. }
  517. /**
  518. * write unsigned exp golomb code. 2^32-2 at most.
  519. */
  520. static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
  521. {
  522. av_assert2(i <= (UINT32_MAX - 1));
  523. if (i < 256)
  524. put_bits(pb, ff_ue_golomb_len[i], i + 1);
  525. else {
  526. int e = av_log2(i + 1);
  527. put_bits64(pb, 2 * e + 1, i + 1);
  528. }
  529. }
  530. /**
  531. * write truncated unsigned exp golomb code.
  532. */
  533. static inline void set_te_golomb(PutBitContext *pb, int i, int range)
  534. {
  535. av_assert2(range >= 1);
  536. av_assert2(i <= range);
  537. if (range == 2)
  538. put_bits(pb, 1, i ^ 1);
  539. else
  540. set_ue_golomb(pb, i);
  541. }
  542. /**
  543. * write signed exp golomb code. 16 bits at most.
  544. */
  545. static inline void set_se_golomb(PutBitContext *pb, int i)
  546. {
  547. i = 2 * i - 1;
  548. if (i < 0)
  549. i ^= -1; //FIXME check if gcc does the right thing
  550. set_ue_golomb(pb, i);
  551. }
  552. /**
  553. * write unsigned golomb rice code (ffv1).
  554. */
  555. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
  556. int esc_len)
  557. {
  558. int e;
  559. av_assert2(i >= 0);
  560. e = i >> k;
  561. if (e < limit)
  562. put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
  563. else
  564. put_bits(pb, limit + esc_len, i - limit + 1);
  565. }
  566. /**
  567. * write unsigned golomb rice code (jpegls).
  568. */
  569. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
  570. int limit, int esc_len)
  571. {
  572. int e;
  573. av_assert2(i >= 0);
  574. e = (i >> k) + 1;
  575. if (e < limit) {
  576. while (e > 31) {
  577. put_bits(pb, 31, 0);
  578. e -= 31;
  579. }
  580. put_bits(pb, e, 1);
  581. if (k)
  582. put_sbits(pb, k, i);
  583. } else {
  584. while (limit > 31) {
  585. put_bits(pb, 31, 0);
  586. limit -= 31;
  587. }
  588. put_bits(pb, limit, 1);
  589. put_bits(pb, esc_len, i - 1);
  590. }
  591. }
  592. /**
  593. * write signed golomb rice code (ffv1).
  594. */
  595. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
  596. int esc_len)
  597. {
  598. int v;
  599. v = -2 * i - 1;
  600. v ^= (v >> 31);
  601. set_ur_golomb(pb, v, k, limit, esc_len);
  602. }
  603. /**
  604. * write signed golomb rice code (flac).
  605. */
  606. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
  607. int limit, int esc_len)
  608. {
  609. int v;
  610. v = -2 * i - 1;
  611. v ^= (v >> 31);
  612. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  613. }
  614. #endif /* AVCODEC_GOLOMB_H */