opus_rc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2012 Andrew D'Addesio
  3. * Copyright (c) 2013-2014 Mozilla Corporation
  4. * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
  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. #include "opus_rc.h"
  23. #define OPUS_RC_BITS 32
  24. #define OPUS_RC_SYM 8
  25. #define OPUS_RC_CEIL ((1 << OPUS_RC_SYM) - 1)
  26. #define OPUS_RC_TOP (1u << 31)
  27. #define OPUS_RC_BOT (OPUS_RC_TOP >> OPUS_RC_SYM)
  28. #define OPUS_RC_SHIFT (OPUS_RC_BITS - OPUS_RC_SYM - 1)
  29. static av_always_inline void opus_rc_enc_carryout(OpusRangeCoder *rc, int cbuf)
  30. {
  31. const int cb = cbuf >> OPUS_RC_SYM, mb = (OPUS_RC_CEIL + cb) & OPUS_RC_CEIL;
  32. if (cbuf == OPUS_RC_CEIL) {
  33. rc->ext++;
  34. return;
  35. }
  36. rc->rng_cur[0] = rc->rem + cb;
  37. rc->rng_cur += (rc->rem >= 0);
  38. for (; rc->ext > 0; rc->ext--)
  39. *rc->rng_cur++ = mb;
  40. av_assert0(rc->rng_cur < rc->rb.position);
  41. rc->rem = cbuf & OPUS_RC_CEIL; /* Propagate */
  42. }
  43. static av_always_inline void opus_rc_dec_normalize(OpusRangeCoder *rc)
  44. {
  45. while (rc->range <= OPUS_RC_BOT) {
  46. rc->value = ((rc->value << OPUS_RC_SYM) | (get_bits(&rc->gb, OPUS_RC_SYM) ^ OPUS_RC_CEIL)) & (OPUS_RC_TOP - 1);
  47. rc->range <<= OPUS_RC_SYM;
  48. rc->total_bits += OPUS_RC_SYM;
  49. }
  50. }
  51. static av_always_inline void opus_rc_enc_normalize(OpusRangeCoder *rc)
  52. {
  53. while (rc->range <= OPUS_RC_BOT) {
  54. opus_rc_enc_carryout(rc, rc->value >> OPUS_RC_SHIFT);
  55. rc->value = (rc->value << OPUS_RC_SYM) & (OPUS_RC_TOP - 1);
  56. rc->range <<= OPUS_RC_SYM;
  57. rc->total_bits += OPUS_RC_SYM;
  58. }
  59. }
  60. static av_always_inline void opus_rc_dec_update(OpusRangeCoder *rc, uint32_t scale,
  61. uint32_t low, uint32_t high,
  62. uint32_t total)
  63. {
  64. rc->value -= scale * (total - high);
  65. rc->range = low ? scale * (high - low)
  66. : rc->range - scale * (total - high);
  67. opus_rc_dec_normalize(rc);
  68. }
  69. /* Main encoding function, this needs to go fast */
  70. static av_always_inline void opus_rc_enc_update(OpusRangeCoder *rc, uint32_t b, uint32_t p,
  71. uint32_t p_tot, const int ptwo)
  72. {
  73. uint32_t rscaled, cnd = !!b;
  74. if (ptwo) /* Whole function is inlined so hopefully branch is optimized out */
  75. rscaled = rc->range >> ff_log2(p_tot);
  76. else
  77. rscaled = rc->range/p_tot;
  78. rc->value += cnd*(rc->range - rscaled*(p_tot - b));
  79. rc->range = (!cnd)*(rc->range - rscaled*(p_tot - p)) + cnd*rscaled*(p - b);
  80. opus_rc_enc_normalize(rc);
  81. }
  82. uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
  83. {
  84. unsigned int k, scale, total, symbol, low, high;
  85. total = *cdf++;
  86. scale = rc->range / total;
  87. symbol = rc->value / scale + 1;
  88. symbol = total - FFMIN(symbol, total);
  89. for (k = 0; cdf[k] <= symbol; k++);
  90. high = cdf[k];
  91. low = k ? cdf[k-1] : 0;
  92. opus_rc_dec_update(rc, scale, low, high, total);
  93. return k;
  94. }
  95. void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
  96. {
  97. opus_rc_enc_update(rc, (!!val)*cdf[val], cdf[val + 1], cdf[0], 1);
  98. }
  99. uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
  100. {
  101. uint32_t k, scale;
  102. scale = rc->range >> bits; // in this case, scale = symbol
  103. if (rc->value >= scale) {
  104. rc->value -= scale;
  105. rc->range -= scale;
  106. k = 0;
  107. } else {
  108. rc->range = scale;
  109. k = 1;
  110. }
  111. opus_rc_dec_normalize(rc);
  112. return k;
  113. }
  114. void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
  115. {
  116. bits = (1 << bits) - 1;
  117. opus_rc_enc_update(rc, (!!val)*bits, bits + !!val, bits + 1, 1);
  118. }
  119. /**
  120. * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
  121. */
  122. uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count)
  123. {
  124. uint32_t value = 0;
  125. while (rc->rb.bytes && rc->rb.cachelen < count) {
  126. rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
  127. rc->rb.cachelen += 8;
  128. rc->rb.bytes--;
  129. }
  130. value = av_mod_uintp2(rc->rb.cacheval, count);
  131. rc->rb.cacheval >>= count;
  132. rc->rb.cachelen -= count;
  133. rc->total_bits += count;
  134. return value;
  135. }
  136. /**
  137. * CELT: write 0 - 31 bits to the rawbits buffer
  138. */
  139. void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count)
  140. {
  141. const int to_write = FFMIN(32 - rc->rb.cachelen, count);
  142. rc->total_bits += count;
  143. rc->rb.cacheval |= av_mod_uintp2(val, to_write) << rc->rb.cachelen;
  144. rc->rb.cachelen = (rc->rb.cachelen + to_write) % 32;
  145. if (!rc->rb.cachelen && count) {
  146. AV_WB32(rc->rb.position, rc->rb.cacheval);
  147. rc->rb.bytes += 4;
  148. rc->rb.position -= 4;
  149. rc->rb.cachelen = count - to_write;
  150. rc->rb.cacheval = av_mod_uintp2(val >> to_write, rc->rb.cachelen);
  151. av_assert0(rc->rng_cur < rc->rb.position);
  152. }
  153. }
  154. /**
  155. * CELT: read a uniform distribution
  156. */
  157. uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
  158. {
  159. uint32_t bits, k, scale, total;
  160. bits = opus_ilog(size - 1);
  161. total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
  162. scale = rc->range / total;
  163. k = rc->value / scale + 1;
  164. k = total - FFMIN(k, total);
  165. opus_rc_dec_update(rc, scale, k, k + 1, total);
  166. if (bits > 8) {
  167. k = k << (bits - 8) | ff_opus_rc_get_raw(rc, bits - 8);
  168. return FFMIN(k, size - 1);
  169. } else
  170. return k;
  171. }
  172. /**
  173. * CELT: write a uniformly distributed integer
  174. */
  175. void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
  176. {
  177. const int ps = FFMAX(opus_ilog(size - 1) - 8, 0);
  178. opus_rc_enc_update(rc, val >> ps, (val >> ps) + 1, ((size - 1) >> ps) + 1, 0);
  179. ff_opus_rc_put_raw(rc, val, ps);
  180. }
  181. uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
  182. {
  183. /* Use a probability of 3 up to itheta=8192 and then use 1 after */
  184. uint32_t k, scale, symbol, total = (k0+1)*3 + k0;
  185. scale = rc->range / total;
  186. symbol = rc->value / scale + 1;
  187. symbol = total - FFMIN(symbol, total);
  188. k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
  189. opus_rc_dec_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
  190. (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
  191. return k;
  192. }
  193. void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0)
  194. {
  195. const uint32_t a = val <= k0, b = 2*a + 1;
  196. k0 = (k0 + 1) << 1;
  197. val = b*(val + k0) - 3*a*k0;
  198. opus_rc_enc_update(rc, val, val + b, (k0 << 1) - 1, 0);
  199. }
  200. uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
  201. {
  202. uint32_t k, scale, symbol, total, low, center;
  203. total = ((qn>>1) + 1) * ((qn>>1) + 1);
  204. scale = rc->range / total;
  205. center = rc->value / scale + 1;
  206. center = total - FFMIN(center, total);
  207. if (center < total >> 1) {
  208. k = (ff_sqrt(8 * center + 1) - 1) >> 1;
  209. low = k * (k + 1) >> 1;
  210. symbol = k + 1;
  211. } else {
  212. k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
  213. low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
  214. symbol = qn + 1 - k;
  215. }
  216. opus_rc_dec_update(rc, scale, low, low + symbol, total);
  217. return k;
  218. }
  219. void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn)
  220. {
  221. uint32_t symbol, low, total;
  222. total = ((qn>>1) + 1) * ((qn>>1) + 1);
  223. if (k <= qn >> 1) {
  224. low = k * (k + 1) >> 1;
  225. symbol = k + 1;
  226. } else {
  227. low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
  228. symbol = qn + 1 - k;
  229. }
  230. opus_rc_enc_update(rc, low, low + symbol, total, 0);
  231. }
  232. int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
  233. {
  234. /* extends the range coder to model a Laplace distribution */
  235. int value = 0;
  236. uint32_t scale, low = 0, center;
  237. scale = rc->range >> 15;
  238. center = rc->value / scale + 1;
  239. center = (1 << 15) - FFMIN(center, 1 << 15);
  240. if (center >= symbol) {
  241. value++;
  242. low = symbol;
  243. symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
  244. while (symbol > 1 && center >= low + 2 * symbol) {
  245. value++;
  246. symbol *= 2;
  247. low += symbol;
  248. symbol = (((symbol - 2) * decay) >> 15) + 1;
  249. }
  250. if (symbol <= 1) {
  251. int distance = (center - low) >> 1;
  252. value += distance;
  253. low += 2 * distance;
  254. }
  255. if (center < low + symbol)
  256. value *= -1;
  257. else
  258. low += symbol;
  259. }
  260. opus_rc_dec_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
  261. return value;
  262. }
  263. void ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay)
  264. {
  265. uint32_t low = symbol;
  266. int i = 1, val = FFABS(*value), pos = *value > 0;
  267. if (!val) {
  268. opus_rc_enc_update(rc, 0, symbol, 1 << 15, 1);
  269. return;
  270. }
  271. symbol = ((32768 - 32 - symbol)*(16384 - decay)) >> 15;
  272. for (; i < val && symbol; i++) {
  273. low += (symbol << 1) + 2;
  274. symbol = (symbol*decay) >> 14;
  275. }
  276. if (symbol) {
  277. low += (++symbol)*pos;
  278. } else {
  279. const int distance = FFMIN(val - i, (((32768 - low) - !pos) >> 1) - 1);
  280. low += pos + (distance << 1);
  281. symbol = FFMIN(1, 32768 - low);
  282. *value = FFSIGN(*value)*(distance + i);
  283. }
  284. opus_rc_enc_update(rc, low, low + symbol, 1 << 15, 1);
  285. }
  286. int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
  287. {
  288. int ret = init_get_bits8(&rc->gb, data, size);
  289. if (ret < 0)
  290. return ret;
  291. rc->range = 128;
  292. rc->value = 127 - get_bits(&rc->gb, 7);
  293. rc->total_bits = 9;
  294. opus_rc_dec_normalize(rc);
  295. return 0;
  296. }
  297. void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
  298. {
  299. rc->rb.position = rightend;
  300. rc->rb.bytes = bytes;
  301. rc->rb.cachelen = 0;
  302. rc->rb.cacheval = 0;
  303. }
  304. void ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size)
  305. {
  306. int rng_bytes, bits = OPUS_RC_BITS - opus_ilog(rc->range);
  307. uint32_t mask = (OPUS_RC_TOP - 1) >> bits;
  308. uint32_t end = (rc->value + mask) & ~mask;
  309. if ((end | mask) >= rc->value + rc->range) {
  310. bits++;
  311. mask >>= 1;
  312. end = (rc->value + mask) & ~mask;
  313. }
  314. /* Finish what's left */
  315. while (bits > 0) {
  316. opus_rc_enc_carryout(rc, end >> OPUS_RC_SHIFT);
  317. end = (end << OPUS_RC_SYM) & (OPUS_RC_TOP - 1);
  318. bits -= OPUS_RC_SYM;
  319. }
  320. /* Flush out anything left or marked */
  321. if (rc->rem >= 0 || rc->ext > 0)
  322. opus_rc_enc_carryout(rc, 0);
  323. rng_bytes = rc->rng_cur - rc->buf;
  324. memcpy(dst, rc->buf, rng_bytes);
  325. rc->waste = size*8 - (rc->rb.bytes*8 + rc->rb.cachelen) - rng_bytes*8;
  326. /* Put the rawbits part, if any */
  327. if (rc->rb.bytes || rc->rb.cachelen) {
  328. int i, lap;
  329. uint8_t *rb_src, *rb_dst;
  330. ff_opus_rc_put_raw(rc, 0, 32 - rc->rb.cachelen);
  331. rb_src = rc->buf + OPUS_MAX_PACKET_SIZE + 12 - rc->rb.bytes;
  332. rb_dst = dst + FFMAX(size - rc->rb.bytes, 0);
  333. lap = &dst[rng_bytes] - rb_dst;
  334. for (i = 0; i < lap; i++)
  335. rb_dst[i] |= rb_src[i];
  336. memcpy(&rb_dst[lap], &rb_src[lap], FFMAX(rc->rb.bytes - lap, 0));
  337. }
  338. }
  339. void ff_opus_rc_enc_init(OpusRangeCoder *rc)
  340. {
  341. rc->value = 0;
  342. rc->range = OPUS_RC_TOP;
  343. rc->total_bits = OPUS_RC_BITS + 1;
  344. rc->rem = -1;
  345. rc->ext = 0;
  346. rc->rng_cur = rc->buf;
  347. ff_opus_rc_dec_raw_init(rc, rc->buf + OPUS_MAX_PACKET_SIZE + 8, 0);
  348. }