opus_rc.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef AVCODEC_OPUS_RC_H
  23. #define AVCODEC_OPUS_RC_H
  24. #include <stdint.h>
  25. #include "get_bits.h"
  26. #define OPUS_MAX_PACKET_SIZE 1275
  27. #define opus_ilog(i) (av_log2(i) + !!(i))
  28. typedef struct RawBitsContext {
  29. const uint8_t *position;
  30. uint32_t bytes;
  31. uint32_t cachelen;
  32. uint32_t cacheval;
  33. } RawBitsContext;
  34. typedef struct OpusRangeCoder {
  35. GetBitContext gb;
  36. RawBitsContext rb;
  37. uint32_t range;
  38. uint32_t value;
  39. uint32_t total_bits;
  40. /* Encoder */
  41. uint8_t buf[OPUS_MAX_PACKET_SIZE + 12]; /* memcpy vs (memmove + overreading) */
  42. uint8_t *rng_cur; /* Current range coded byte */
  43. int ext; /* Awaiting propagation */
  44. int rem; /* Carryout flag */
  45. /* Encoding stats */
  46. int waste;
  47. } OpusRangeCoder;
  48. /**
  49. * CELT: estimate bits of entropy that have thus far been consumed for the
  50. * current CELT frame, to integer and fractional (1/8th bit) precision
  51. */
  52. static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
  53. {
  54. return rc->total_bits - av_log2(rc->range) - 1;
  55. }
  56. static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
  57. {
  58. uint32_t i, total_bits, rcbuffer, range;
  59. total_bits = rc->total_bits << 3;
  60. rcbuffer = av_log2(rc->range) + 1;
  61. range = rc->range >> (rcbuffer-16);
  62. for (i = 0; i < 3; i++) {
  63. int bit;
  64. range = range * range >> 15;
  65. bit = range >> 16;
  66. rcbuffer = rcbuffer << 1 | bit;
  67. range >>= bit;
  68. }
  69. return total_bits - rcbuffer;
  70. }
  71. uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf);
  72. void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf);
  73. uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits);
  74. void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits);
  75. uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0);
  76. void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0);
  77. uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn);
  78. void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn);
  79. uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size);
  80. void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size);
  81. uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count);
  82. void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count);
  83. int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay);
  84. void ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay);
  85. int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size);
  86. void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes);
  87. void ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size);
  88. void ff_opus_rc_enc_init(OpusRangeCoder *rc);
  89. #define OPUS_RC_CHECKPOINT_UPDATE(rc) \
  90. rc_rollback_bits = opus_rc_tell_frac(rc); \
  91. rc_rollback_ctx = *rc
  92. #define OPUS_RC_CHECKPOINT_SPAWN(rc) \
  93. uint32_t rc_rollback_bits = opus_rc_tell_frac(rc); \
  94. OpusRangeCoder rc_rollback_ctx = *rc \
  95. #define OPUS_RC_CHECKPOINT_BITS(rc) \
  96. (opus_rc_tell_frac(rc) - rc_rollback_bits)
  97. #define OPUS_RC_CHECKPOINT_ROLLBACK(rc) \
  98. memcpy(rc, &rc_rollback_ctx, sizeof(OpusRangeCoder)); \
  99. #endif /* AVCODEC_OPUS_RC_H */