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.

583 lines
14KB

  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. static inline int get_ue_golomb(GetBitContext *gb)
  46. {
  47. unsigned int buf;
  48. OPEN_READER(re, gb);
  49. UPDATE_CACHE(re, gb);
  50. buf = GET_CACHE(re, gb);
  51. if (buf >= (1 << 27)) {
  52. buf >>= 32 - 9;
  53. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  54. CLOSE_READER(re, gb);
  55. return ff_ue_golomb_vlc_code[buf];
  56. } else {
  57. int log = 2 * av_log2(buf) - 31;
  58. LAST_SKIP_BITS(re, gb, 32 - log);
  59. CLOSE_READER(re, gb);
  60. if (log < 7) {
  61. av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
  62. return AVERROR_INVALIDDATA;
  63. }
  64. buf >>= log;
  65. buf--;
  66. return buf;
  67. }
  68. }
  69. /**
  70. * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
  71. */
  72. static inline unsigned get_ue_golomb_long(GetBitContext *gb)
  73. {
  74. unsigned buf, log;
  75. buf = show_bits_long(gb, 32);
  76. log = 31 - av_log2(buf);
  77. skip_bits_long(gb, log);
  78. return get_bits_long(gb, log + 1) - 1;
  79. }
  80. /**
  81. * read unsigned exp golomb code, constraint to a max of 31.
  82. * the return value is undefined if the stored value exceeds 31.
  83. */
  84. static inline int get_ue_golomb_31(GetBitContext *gb)
  85. {
  86. unsigned int buf;
  87. OPEN_READER(re, gb);
  88. UPDATE_CACHE(re, gb);
  89. buf = GET_CACHE(re, gb);
  90. buf >>= 32 - 9;
  91. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  92. CLOSE_READER(re, gb);
  93. return ff_ue_golomb_vlc_code[buf];
  94. }
  95. static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
  96. {
  97. uint32_t buf;
  98. OPEN_READER(re, gb);
  99. UPDATE_CACHE(re, gb);
  100. buf = GET_CACHE(re, gb);
  101. if (buf & 0xAA800000) {
  102. buf >>= 32 - 8;
  103. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  104. CLOSE_READER(re, gb);
  105. return ff_interleaved_ue_golomb_vlc_code[buf];
  106. } else {
  107. unsigned ret = 1;
  108. do {
  109. buf >>= 32 - 8;
  110. LAST_SKIP_BITS(re, gb,
  111. FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
  112. if (ff_interleaved_golomb_vlc_len[buf] != 9) {
  113. ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
  114. ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
  115. break;
  116. }
  117. ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
  118. UPDATE_CACHE(re, gb);
  119. buf = GET_CACHE(re, gb);
  120. } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
  121. CLOSE_READER(re, gb);
  122. return ret - 1;
  123. }
  124. }
  125. /**
  126. * read unsigned truncated exp golomb code.
  127. */
  128. static inline int get_te0_golomb(GetBitContext *gb, int range)
  129. {
  130. av_assert2(range >= 1);
  131. if (range == 1)
  132. return 0;
  133. else if (range == 2)
  134. return get_bits1(gb) ^ 1;
  135. else
  136. return get_ue_golomb(gb);
  137. }
  138. /**
  139. * read unsigned truncated exp golomb code.
  140. */
  141. static inline int get_te_golomb(GetBitContext *gb, int range)
  142. {
  143. av_assert2(range >= 1);
  144. if (range == 2)
  145. return get_bits1(gb) ^ 1;
  146. else
  147. return get_ue_golomb(gb);
  148. }
  149. /**
  150. * read signed exp golomb code.
  151. */
  152. static inline int get_se_golomb(GetBitContext *gb)
  153. {
  154. unsigned int buf;
  155. OPEN_READER(re, gb);
  156. UPDATE_CACHE(re, gb);
  157. buf = GET_CACHE(re, gb);
  158. if (buf >= (1 << 27)) {
  159. buf >>= 32 - 9;
  160. LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
  161. CLOSE_READER(re, gb);
  162. return ff_se_golomb_vlc_code[buf];
  163. } else {
  164. int log = av_log2(buf), sign;
  165. LAST_SKIP_BITS(re, gb, 31 - log);
  166. UPDATE_CACHE(re, gb);
  167. buf = GET_CACHE(re, gb);
  168. buf >>= log;
  169. LAST_SKIP_BITS(re, gb, 32 - log);
  170. CLOSE_READER(re, gb);
  171. sign = -(buf & 1);
  172. buf = ((buf >> 1) ^ sign) - sign;
  173. return buf;
  174. }
  175. }
  176. static inline int get_se_golomb_long(GetBitContext *gb)
  177. {
  178. unsigned int buf = get_ue_golomb_long(gb);
  179. int sign = (buf & 1) - 1;
  180. return ((buf >> 1) ^ sign) + 1;
  181. }
  182. static inline int get_interleaved_se_golomb(GetBitContext *gb)
  183. {
  184. unsigned int buf;
  185. OPEN_READER(re, gb);
  186. UPDATE_CACHE(re, gb);
  187. buf = GET_CACHE(re, gb);
  188. if (buf & 0xAA800000) {
  189. buf >>= 32 - 8;
  190. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  191. CLOSE_READER(re, gb);
  192. return ff_interleaved_se_golomb_vlc_code[buf];
  193. } else {
  194. int log;
  195. LAST_SKIP_BITS(re, gb, 8);
  196. UPDATE_CACHE(re, gb);
  197. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  198. if ((buf & 0xAAAAAAAA) == 0)
  199. return INVALID_VLC;
  200. for (log = 31; (buf & 0x80000000) == 0; log--)
  201. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  202. LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
  203. CLOSE_READER(re, gb);
  204. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  205. }
  206. }
  207. static inline int dirac_get_se_golomb(GetBitContext *gb)
  208. {
  209. uint32_t ret = get_interleaved_ue_golomb(gb);
  210. if (ret) {
  211. int sign = -get_bits1(gb);
  212. ret = (ret ^ sign) - sign;
  213. }
  214. return ret;
  215. }
  216. /**
  217. * read unsigned golomb rice code (ffv1).
  218. */
  219. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
  220. int esc_len)
  221. {
  222. unsigned int buf;
  223. int log;
  224. OPEN_READER(re, gb);
  225. UPDATE_CACHE(re, gb);
  226. buf = GET_CACHE(re, gb);
  227. log = av_log2(buf);
  228. if (log > 31 - limit) {
  229. buf >>= log - k;
  230. buf += (30U - log) << k;
  231. LAST_SKIP_BITS(re, gb, 32 + k - log);
  232. CLOSE_READER(re, gb);
  233. return buf;
  234. } else {
  235. LAST_SKIP_BITS(re, gb, limit);
  236. UPDATE_CACHE(re, gb);
  237. buf = SHOW_UBITS(re, gb, esc_len);
  238. LAST_SKIP_BITS(re, gb, esc_len);
  239. CLOSE_READER(re, gb);
  240. return buf + limit - 1;
  241. }
  242. }
  243. /**
  244. * read unsigned golomb rice code (jpegls).
  245. */
  246. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
  247. int esc_len)
  248. {
  249. unsigned int buf;
  250. int log;
  251. OPEN_READER(re, gb);
  252. UPDATE_CACHE(re, gb);
  253. buf = GET_CACHE(re, gb);
  254. log = av_log2(buf);
  255. if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
  256. 32 - log < limit) {
  257. buf >>= log - k;
  258. buf += (30U - log) << k;
  259. LAST_SKIP_BITS(re, gb, 32 + k - log);
  260. CLOSE_READER(re, gb);
  261. return buf;
  262. } else {
  263. int i;
  264. for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
  265. if (gb->size_in_bits <= re_index) {
  266. CLOSE_READER(re, gb);
  267. return -1;
  268. }
  269. LAST_SKIP_BITS(re, gb, 1);
  270. UPDATE_CACHE(re, gb);
  271. }
  272. SKIP_BITS(re, gb, 1);
  273. if (i < limit - 1) {
  274. if (k) {
  275. if (k > MIN_CACHE_BITS - 1) {
  276. buf = SHOW_UBITS(re, gb, 16) << (k-16);
  277. LAST_SKIP_BITS(re, gb, 16);
  278. UPDATE_CACHE(re, gb);
  279. buf |= SHOW_UBITS(re, gb, k-16);
  280. LAST_SKIP_BITS(re, gb, k-16);
  281. } else {
  282. buf = SHOW_UBITS(re, gb, k);
  283. LAST_SKIP_BITS(re, gb, k);
  284. }
  285. } else {
  286. buf = 0;
  287. }
  288. buf += (i << k);
  289. } else if (i == limit - 1) {
  290. buf = SHOW_UBITS(re, gb, esc_len);
  291. LAST_SKIP_BITS(re, gb, esc_len);
  292. buf ++;
  293. } else {
  294. buf = -1;
  295. }
  296. CLOSE_READER(re, gb);
  297. return buf;
  298. }
  299. }
  300. /**
  301. * read signed golomb rice code (ffv1).
  302. */
  303. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
  304. int esc_len)
  305. {
  306. unsigned v = get_ur_golomb(gb, k, limit, esc_len);
  307. return (v >> 1) ^ -(v & 1);
  308. }
  309. /**
  310. * read signed golomb rice code (flac).
  311. */
  312. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
  313. int esc_len)
  314. {
  315. unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
  316. return (v >> 1) ^ -(v & 1);
  317. }
  318. /**
  319. * read unsigned golomb rice code (shorten).
  320. */
  321. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
  322. {
  323. return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  324. }
  325. /**
  326. * read signed golomb rice code (shorten).
  327. */
  328. static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
  329. {
  330. int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  331. return (uvar >> 1) ^ -(uvar & 1);
  332. }
  333. #ifdef TRACE
  334. static inline int get_ue(GetBitContext *s, const char *file, const char *func,
  335. int line)
  336. {
  337. int show = show_bits(s, 24);
  338. int pos = get_bits_count(s);
  339. int i = get_ue_golomb(s);
  340. int len = get_bits_count(s) - pos;
  341. int bits = show >> (24 - len);
  342. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
  343. bits, len, i, pos, file, func, line);
  344. return i;
  345. }
  346. static inline int get_se(GetBitContext *s, const char *file, const char *func,
  347. int line)
  348. {
  349. int show = show_bits(s, 24);
  350. int pos = get_bits_count(s);
  351. int i = get_se_golomb(s);
  352. int len = get_bits_count(s) - pos;
  353. int bits = show >> (24 - len);
  354. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
  355. bits, len, i, pos, file, func, line);
  356. return i;
  357. }
  358. static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
  359. int line)
  360. {
  361. int show = show_bits(s, 24);
  362. int pos = get_bits_count(s);
  363. int i = get_te0_golomb(s, r);
  364. int len = get_bits_count(s) - pos;
  365. int bits = show >> (24 - len);
  366. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
  367. bits, len, i, pos, file, func, line);
  368. return i;
  369. }
  370. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  371. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  372. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  373. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  374. #endif /* TRACE */
  375. /**
  376. * write unsigned exp golomb code.
  377. */
  378. static inline void set_ue_golomb(PutBitContext *pb, int i)
  379. {
  380. av_assert2(i >= 0);
  381. if (i < 256)
  382. put_bits(pb, ff_ue_golomb_len[i], i + 1);
  383. else {
  384. int e = av_log2(i + 1);
  385. put_bits(pb, 2 * e + 1, i + 1);
  386. }
  387. }
  388. /**
  389. * write truncated unsigned exp golomb code.
  390. */
  391. static inline void set_te_golomb(PutBitContext *pb, int i, int range)
  392. {
  393. av_assert2(range >= 1);
  394. av_assert2(i <= range);
  395. if (range == 2)
  396. put_bits(pb, 1, i ^ 1);
  397. else
  398. set_ue_golomb(pb, i);
  399. }
  400. /**
  401. * write signed exp golomb code. 16 bits at most.
  402. */
  403. static inline void set_se_golomb(PutBitContext *pb, int i)
  404. {
  405. #if 0
  406. if (i <= 0)
  407. i = -2 * i;
  408. else
  409. i = 2 * i - 1;
  410. #elif 1
  411. i = 2 * i - 1;
  412. if (i < 0)
  413. i ^= -1; //FIXME check if gcc does the right thing
  414. #else
  415. i = 2 * i - 1;
  416. i ^= (i >> 31);
  417. #endif
  418. set_ue_golomb(pb, i);
  419. }
  420. /**
  421. * write unsigned golomb rice code (ffv1).
  422. */
  423. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
  424. int esc_len)
  425. {
  426. int e;
  427. av_assert2(i >= 0);
  428. e = i >> k;
  429. if (e < limit)
  430. put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
  431. else
  432. put_bits(pb, limit + esc_len, i - limit + 1);
  433. }
  434. /**
  435. * write unsigned golomb rice code (jpegls).
  436. */
  437. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
  438. int limit, int esc_len)
  439. {
  440. int e;
  441. av_assert2(i >= 0);
  442. e = (i >> k) + 1;
  443. if (e < limit) {
  444. while (e > 31) {
  445. put_bits(pb, 31, 0);
  446. e -= 31;
  447. }
  448. put_bits(pb, e, 1);
  449. if (k)
  450. put_sbits(pb, k, i);
  451. } else {
  452. while (limit > 31) {
  453. put_bits(pb, 31, 0);
  454. limit -= 31;
  455. }
  456. put_bits(pb, limit, 1);
  457. put_bits(pb, esc_len, i - 1);
  458. }
  459. }
  460. /**
  461. * write signed golomb rice code (ffv1).
  462. */
  463. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
  464. int esc_len)
  465. {
  466. int v;
  467. v = -2 * i - 1;
  468. v ^= (v >> 31);
  469. set_ur_golomb(pb, v, k, limit, esc_len);
  470. }
  471. /**
  472. * write signed golomb rice code (flac).
  473. */
  474. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
  475. int limit, int esc_len)
  476. {
  477. int v;
  478. v = -2 * i - 1;
  479. v ^= (v >> 31);
  480. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  481. }
  482. #endif /* AVCODEC_GOLOMB_H */