2
0

treewriter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_VP8_ENCODER_TREEWRITER_H_
  11. #define VPX_VP8_ENCODER_TREEWRITER_H_
  12. /* Trees map alphabets into huffman-like codes suitable for an arithmetic
  13. bit coder. Timothy S Murphy 11 October 2004 */
  14. #include "./vpx_config.h"
  15. #include "vp8/common/treecoder.h"
  16. #include "boolhuff.h" /* for now */
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. typedef BOOL_CODER vp8_writer;
  21. #define vp8_write vp8_encode_bool
  22. #define vp8_write_literal vp8_encode_value
  23. #define vp8_write_bit(W, V) vp8_write(W, V, vp8_prob_half)
  24. #define vp8bc_write vp8bc_write_bool
  25. #define vp8bc_write_literal vp8bc_write_bits
  26. #define vp8bc_write_bit(W, V) vp8bc_write_bits(W, V, 1)
  27. /* Approximate length of an encoded bool in 256ths of a bit at given prob */
  28. #define vp8_cost_zero(x) (vp8_prob_cost[x])
  29. #define vp8_cost_one(x) vp8_cost_zero(vp8_complement(x))
  30. #define vp8_cost_bit(x, b) vp8_cost_zero((b) ? vp8_complement(x) : (x))
  31. /* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */
  32. /* Both of these return bits, not scaled bits. */
  33. static INLINE unsigned int vp8_cost_branch(const unsigned int ct[2],
  34. vp8_prob p) {
  35. /* Imitate existing calculation */
  36. return ((ct[0] * vp8_cost_zero(p)) + (ct[1] * vp8_cost_one(p))) >> 8;
  37. }
  38. /* Small functions to write explicit values and tokens, as well as
  39. estimate their lengths. */
  40. static void vp8_treed_write(vp8_writer *const w, vp8_tree t,
  41. const vp8_prob *const p, int v,
  42. int n) { /* number of bits in v, assumed nonzero */
  43. vp8_tree_index i = 0;
  44. do {
  45. const int b = (v >> --n) & 1;
  46. vp8_write(w, b, p[i >> 1]);
  47. i = t[i + b];
  48. } while (n);
  49. }
  50. static INLINE void vp8_write_token(vp8_writer *const w, vp8_tree t,
  51. const vp8_prob *const p,
  52. vp8_token *const x) {
  53. vp8_treed_write(w, t, p, x->value, x->Len);
  54. }
  55. static int vp8_treed_cost(vp8_tree t, const vp8_prob *const p, int v,
  56. int n) { /* number of bits in v, assumed nonzero */
  57. int c = 0;
  58. vp8_tree_index i = 0;
  59. do {
  60. const int b = (v >> --n) & 1;
  61. c += vp8_cost_bit(p[i >> 1], b);
  62. i = t[i + b];
  63. } while (n);
  64. return c;
  65. }
  66. static INLINE int vp8_cost_token(vp8_tree t, const vp8_prob *const p,
  67. vp8_token *const x) {
  68. return vp8_treed_cost(t, p, x->value, x->Len);
  69. }
  70. /* Fill array of costs for all possible token values. */
  71. void vp8_cost_tokens(int *c, const vp8_prob *, vp8_tree);
  72. void vp8_cost_tokens2(int *c, const vp8_prob *, vp8_tree, int);
  73. #ifdef __cplusplus
  74. } // extern "C"
  75. #endif
  76. #endif // VPX_VP8_ENCODER_TREEWRITER_H_