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.

614 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 unsigned exp golomb code.
  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 (CONFIG_FTRAPV && log < 0) {
  61. av_log(0, 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 svq3_get_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);
  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. if (buf & 1)
  172. buf = -(buf >> 1);
  173. else
  174. buf = (buf >> 1);
  175. return buf;
  176. }
  177. }
  178. static inline int get_se_golomb_long(GetBitContext *gb)
  179. {
  180. unsigned int buf = get_ue_golomb_long(gb);
  181. if (buf & 1)
  182. buf = (buf + 1) >> 1;
  183. else
  184. buf = -(buf >> 1);
  185. return buf;
  186. }
  187. static inline int svq3_get_se_golomb(GetBitContext *gb)
  188. {
  189. unsigned int buf;
  190. OPEN_READER(re, gb);
  191. UPDATE_CACHE(re, gb);
  192. buf = GET_CACHE(re, gb);
  193. if (buf & 0xAA800000) {
  194. buf >>= 32 - 8;
  195. LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
  196. CLOSE_READER(re, gb);
  197. return ff_interleaved_se_golomb_vlc_code[buf];
  198. } else {
  199. int log;
  200. LAST_SKIP_BITS(re, gb, 8);
  201. UPDATE_CACHE(re, gb);
  202. buf |= 1 | (GET_CACHE(re, gb) >> 8);
  203. if ((buf & 0xAAAAAAAA) == 0)
  204. return INVALID_VLC;
  205. for (log = 31; (buf & 0x80000000) == 0; log--)
  206. buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
  207. LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
  208. CLOSE_READER(re, gb);
  209. return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
  210. }
  211. }
  212. static inline int dirac_get_se_golomb(GetBitContext *gb)
  213. {
  214. uint32_t ret = svq3_get_ue_golomb(gb);
  215. if (ret) {
  216. uint32_t buf;
  217. OPEN_READER(re, gb);
  218. UPDATE_CACHE(re, gb);
  219. buf = SHOW_SBITS(re, gb, 1);
  220. LAST_SKIP_BITS(re, gb, 1);
  221. ret = (ret ^ buf) - buf;
  222. CLOSE_READER(re, gb);
  223. }
  224. return ret;
  225. }
  226. /**
  227. * read unsigned golomb rice code (ffv1).
  228. */
  229. static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
  230. int esc_len)
  231. {
  232. unsigned int buf;
  233. int log;
  234. OPEN_READER(re, gb);
  235. UPDATE_CACHE(re, gb);
  236. buf = GET_CACHE(re, gb);
  237. log = av_log2(buf);
  238. if (log > 31 - limit) {
  239. buf >>= log - k;
  240. buf += (30 - log) << k;
  241. LAST_SKIP_BITS(re, gb, 32 + k - log);
  242. CLOSE_READER(re, gb);
  243. return buf;
  244. } else {
  245. LAST_SKIP_BITS(re, gb, limit);
  246. UPDATE_CACHE(re, gb);
  247. buf = SHOW_UBITS(re, gb, esc_len);
  248. LAST_SKIP_BITS(re, gb, esc_len);
  249. CLOSE_READER(re, gb);
  250. return buf + limit - 1;
  251. }
  252. }
  253. /**
  254. * read unsigned golomb rice code (jpegls).
  255. */
  256. static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
  257. int esc_len)
  258. {
  259. unsigned int buf;
  260. int log;
  261. OPEN_READER(re, gb);
  262. UPDATE_CACHE(re, gb);
  263. buf = GET_CACHE(re, gb);
  264. log = av_log2(buf);
  265. if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
  266. 32 - log < limit) {
  267. buf >>= log - k;
  268. buf += (30 - log) << k;
  269. LAST_SKIP_BITS(re, gb, 32 + k - log);
  270. CLOSE_READER(re, gb);
  271. return buf;
  272. } else {
  273. int i;
  274. for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
  275. if (gb->size_in_bits <= re_index)
  276. return -1;
  277. LAST_SKIP_BITS(re, gb, 1);
  278. UPDATE_CACHE(re, gb);
  279. }
  280. SKIP_BITS(re, gb, 1);
  281. if (i < limit - 1) {
  282. if (k) {
  283. if (k > MIN_CACHE_BITS - 1) {
  284. buf = SHOW_UBITS(re, gb, 16) << (k-16);
  285. LAST_SKIP_BITS(re, gb, 16);
  286. UPDATE_CACHE(re, gb);
  287. buf |= SHOW_UBITS(re, gb, k-16);
  288. LAST_SKIP_BITS(re, gb, k-16);
  289. } else {
  290. buf = SHOW_UBITS(re, gb, k);
  291. LAST_SKIP_BITS(re, gb, k);
  292. }
  293. } else {
  294. buf = 0;
  295. }
  296. CLOSE_READER(re, gb);
  297. return buf + (i << k);
  298. } else if (i == limit - 1) {
  299. buf = SHOW_UBITS(re, gb, esc_len);
  300. LAST_SKIP_BITS(re, gb, esc_len);
  301. CLOSE_READER(re, gb);
  302. return buf + 1;
  303. } else
  304. return -1;
  305. }
  306. }
  307. /**
  308. * read signed golomb rice code (ffv1).
  309. */
  310. static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
  311. int esc_len)
  312. {
  313. int v = get_ur_golomb(gb, k, limit, esc_len);
  314. v++;
  315. if (v & 1)
  316. return v >> 1;
  317. else
  318. return -(v >> 1);
  319. // return (v>>1) ^ -(v&1);
  320. }
  321. /**
  322. * read signed golomb rice code (flac).
  323. */
  324. static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
  325. int esc_len)
  326. {
  327. int v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
  328. return (v >> 1) ^ -(v & 1);
  329. }
  330. /**
  331. * read unsigned golomb rice code (shorten).
  332. */
  333. static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
  334. {
  335. return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
  336. }
  337. /**
  338. * read signed golomb rice code (shorten).
  339. */
  340. static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
  341. {
  342. int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
  343. if (uvar & 1)
  344. return ~(uvar >> 1);
  345. else
  346. return uvar >> 1;
  347. }
  348. #ifdef TRACE
  349. static inline int get_ue(GetBitContext *s, const char *file, const char *func,
  350. int line)
  351. {
  352. int show = show_bits(s, 24);
  353. int pos = get_bits_count(s);
  354. int i = get_ue_golomb(s);
  355. int len = get_bits_count(s) - pos;
  356. int bits = show >> (24 - len);
  357. print_bin(bits, len);
  358. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
  359. bits, len, i, pos, file, func, line);
  360. return i;
  361. }
  362. static inline int get_se(GetBitContext *s, const char *file, const char *func,
  363. int line)
  364. {
  365. int show = show_bits(s, 24);
  366. int pos = get_bits_count(s);
  367. int i = get_se_golomb(s);
  368. int len = get_bits_count(s) - pos;
  369. int bits = show >> (24 - len);
  370. print_bin(bits, len);
  371. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
  372. bits, len, i, pos, file, func, line);
  373. return i;
  374. }
  375. static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
  376. int line)
  377. {
  378. int show = show_bits(s, 24);
  379. int pos = get_bits_count(s);
  380. int i = get_te0_golomb(s, r);
  381. int len = get_bits_count(s) - pos;
  382. int bits = show >> (24 - len);
  383. print_bin(bits, len);
  384. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
  385. bits, len, i, pos, file, func, line);
  386. return i;
  387. }
  388. #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  389. #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  390. #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  391. #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  392. #endif /* TRACE */
  393. /**
  394. * write unsigned exp golomb code.
  395. */
  396. static inline void set_ue_golomb(PutBitContext *pb, int i)
  397. {
  398. av_assert2(i >= 0);
  399. #if 0
  400. if (i = 0) {
  401. put_bits(pb, 1, 1);
  402. return;
  403. }
  404. #endif
  405. if (i < 256)
  406. put_bits(pb, ff_ue_golomb_len[i], i + 1);
  407. else {
  408. int e = av_log2(i + 1);
  409. put_bits(pb, 2 * e + 1, i + 1);
  410. }
  411. }
  412. /**
  413. * write truncated unsigned exp golomb code.
  414. */
  415. static inline void set_te_golomb(PutBitContext *pb, int i, int range)
  416. {
  417. av_assert2(range >= 1);
  418. av_assert2(i <= range);
  419. if (range == 2)
  420. put_bits(pb, 1, i ^ 1);
  421. else
  422. set_ue_golomb(pb, i);
  423. }
  424. /**
  425. * write signed exp golomb code. 16 bits at most.
  426. */
  427. static inline void set_se_golomb(PutBitContext *pb, int i)
  428. {
  429. #if 0
  430. if (i <= 0)
  431. i = -2 * i;
  432. else
  433. i = 2 * i - 1;
  434. #elif 1
  435. i = 2 * i - 1;
  436. if (i < 0)
  437. i ^= -1; //FIXME check if gcc does the right thing
  438. #else
  439. i = 2 * i - 1;
  440. i ^= (i >> 31);
  441. #endif
  442. set_ue_golomb(pb, i);
  443. }
  444. /**
  445. * write unsigned golomb rice code (ffv1).
  446. */
  447. static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
  448. int esc_len)
  449. {
  450. int e;
  451. av_assert2(i >= 0);
  452. e = i >> k;
  453. if (e < limit)
  454. put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1)));
  455. else
  456. put_bits(pb, limit + esc_len, i - limit + 1);
  457. }
  458. /**
  459. * write unsigned golomb rice code (jpegls).
  460. */
  461. static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
  462. int limit, int esc_len)
  463. {
  464. int e;
  465. av_assert2(i >= 0);
  466. e = (i >> k) + 1;
  467. if (e < limit) {
  468. while (e > 31) {
  469. put_bits(pb, 31, 0);
  470. e -= 31;
  471. }
  472. put_bits(pb, e, 1);
  473. if (k)
  474. put_sbits(pb, k, i);
  475. } else {
  476. while (limit > 31) {
  477. put_bits(pb, 31, 0);
  478. limit -= 31;
  479. }
  480. put_bits(pb, limit, 1);
  481. put_bits(pb, esc_len, i - 1);
  482. }
  483. }
  484. /**
  485. * write signed golomb rice code (ffv1).
  486. */
  487. static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
  488. int esc_len)
  489. {
  490. int v;
  491. v = -2 * i - 1;
  492. v ^= (v >> 31);
  493. set_ur_golomb(pb, v, k, limit, esc_len);
  494. }
  495. /**
  496. * write signed golomb rice code (flac).
  497. */
  498. static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
  499. int limit, int esc_len)
  500. {
  501. int v;
  502. v = -2 * i - 1;
  503. v ^= (v >> 31);
  504. set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
  505. }
  506. #endif /* AVCODEC_GOLOMB_H */