entenc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved
  3. *
  4. * This source code is subject to the terms of the BSD 2 Clause License and
  5. * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
  6. * was not distributed with this source code in the LICENSE file, you can
  7. * obtain it at www.aomedia.org/license/software. If the Alliance for Open
  8. * Media Patent License 1.0 was not distributed with this source code in the
  9. * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  10. */
  11. #ifndef AOM_AOM_DSP_ENTENC_H_
  12. #define AOM_AOM_DSP_ENTENC_H_
  13. #include <stddef.h>
  14. #include "aom_dsp/entcode.h"
  15. #include "aom_ports/bitops.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef uint64_t od_ec_enc_window;
  20. typedef struct od_ec_enc od_ec_enc;
  21. #define OD_MEASURE_EC_OVERHEAD (0)
  22. /*The entropy encoder context.*/
  23. struct od_ec_enc {
  24. /*Buffered output.
  25. This contains only the raw bits until the final call to od_ec_enc_done(),
  26. where all the arithmetic-coded data gets prepended to it.*/
  27. unsigned char *buf;
  28. /*The size of the buffer.*/
  29. uint32_t storage;
  30. /*The offset at which the next entropy-coded byte will be written.*/
  31. uint32_t offs;
  32. /*The low end of the current range.*/
  33. od_ec_enc_window low;
  34. /*The number of values in the current range.*/
  35. uint16_t rng;
  36. /*The number of bits of data in the current value.*/
  37. int16_t cnt;
  38. /*Nonzero if an error occurred.*/
  39. int error;
  40. #if OD_MEASURE_EC_OVERHEAD
  41. double entropy;
  42. int nb_symbols;
  43. #endif
  44. };
  45. /*See entenc.c for further documentation.*/
  46. void od_ec_enc_init(od_ec_enc *enc, uint32_t size) OD_ARG_NONNULL(1);
  47. void od_ec_enc_reset(od_ec_enc *enc) OD_ARG_NONNULL(1);
  48. void od_ec_enc_clear(od_ec_enc *enc) OD_ARG_NONNULL(1);
  49. void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f_q15)
  50. OD_ARG_NONNULL(1);
  51. void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *cdf, int nsyms)
  52. OD_ARG_NONNULL(1) OD_ARG_NONNULL(3);
  53. void od_ec_enc_bits(od_ec_enc *enc, uint32_t fl, unsigned ftb)
  54. OD_ARG_NONNULL(1);
  55. void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits)
  56. OD_ARG_NONNULL(1);
  57. OD_WARN_UNUSED_RESULT unsigned char *od_ec_enc_done(od_ec_enc *enc,
  58. uint32_t *nbytes)
  59. OD_ARG_NONNULL(1) OD_ARG_NONNULL(2);
  60. OD_WARN_UNUSED_RESULT int od_ec_enc_tell(const od_ec_enc *enc)
  61. OD_ARG_NONNULL(1);
  62. OD_WARN_UNUSED_RESULT uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc)
  63. OD_ARG_NONNULL(1);
  64. void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src);
  65. void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src);
  66. // buf is the frame bitbuffer, offs is where carry to be added
  67. static AOM_INLINE void propagate_carry_bwd(unsigned char *buf, uint32_t offs) {
  68. uint16_t sum, carry = 1;
  69. do {
  70. sum = (uint16_t)buf[offs] + 1;
  71. buf[offs--] = (unsigned char)sum;
  72. carry = sum >> 8;
  73. } while (carry);
  74. }
  75. // Reverse byte order and write data to buffer adding the carry-bit
  76. static AOM_INLINE void write_enc_data_to_out_buf(unsigned char *out,
  77. uint32_t offs, uint64_t output,
  78. uint64_t carry,
  79. uint32_t *enc_offs,
  80. uint8_t num_bytes_ready) {
  81. const uint64_t reg = get_byteswap64(output) >> ((8 - num_bytes_ready) << 3);
  82. memcpy(&out[offs], &reg, 8);
  83. // Propagate carry backwards if exists
  84. if (carry) {
  85. assert(offs > 0);
  86. propagate_carry_bwd(out, offs - 1);
  87. }
  88. *enc_offs = offs + num_bytes_ready;
  89. }
  90. #ifdef __cplusplus
  91. } // extern "C"
  92. #endif
  93. #endif // AOM_AOM_DSP_ENTENC_H_