boolhuff.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /****************************************************************************
  11. *
  12. * Module Title : boolhuff.h
  13. *
  14. * Description : Bool Coder header file.
  15. *
  16. ****************************************************************************/
  17. #ifndef VP8_ENCODER_BOOLHUFF_H_
  18. #define VP8_ENCODER_BOOLHUFF_H_
  19. #include "vpx_ports/mem.h"
  20. #include "vpx/internal/vpx_codec_internal.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. typedef struct
  25. {
  26. unsigned int lowvalue;
  27. unsigned int range;
  28. int count;
  29. unsigned int pos;
  30. unsigned char *buffer;
  31. unsigned char *buffer_end;
  32. struct vpx_internal_error_info *error;
  33. } BOOL_CODER;
  34. extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end);
  35. extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
  36. extern void vp8_stop_encode(BOOL_CODER *bc);
  37. extern const unsigned int vp8_prob_cost[256];
  38. DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
  39. static int validate_buffer(const unsigned char *start,
  40. size_t len,
  41. const unsigned char *end,
  42. struct vpx_internal_error_info *error)
  43. {
  44. if (start + len > start && start + len < end)
  45. return 1;
  46. else
  47. vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
  48. "Truncated packet or corrupt partition ");
  49. return 0;
  50. }
  51. static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability)
  52. {
  53. unsigned int split;
  54. int count = br->count;
  55. unsigned int range = br->range;
  56. unsigned int lowvalue = br->lowvalue;
  57. register unsigned int shift;
  58. #ifdef VP8_ENTROPY_STATS
  59. #if defined(SECTIONBITS_OUTPUT)
  60. if (bit)
  61. Sectionbits[active_section] += vp8_prob_cost[255-probability];
  62. else
  63. Sectionbits[active_section] += vp8_prob_cost[probability];
  64. #endif
  65. #endif
  66. split = 1 + (((range - 1) * probability) >> 8);
  67. range = split;
  68. if (bit)
  69. {
  70. lowvalue += split;
  71. range = br->range - split;
  72. }
  73. shift = vp8_norm[range];
  74. range <<= shift;
  75. count += shift;
  76. if (count >= 0)
  77. {
  78. int offset = shift - count;
  79. if ((lowvalue << (offset - 1)) & 0x80000000)
  80. {
  81. int x = br->pos - 1;
  82. while (x >= 0 && br->buffer[x] == 0xff)
  83. {
  84. br->buffer[x] = (unsigned char)0;
  85. x--;
  86. }
  87. br->buffer[x] += 1;
  88. }
  89. validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
  90. br->buffer[br->pos++] = (lowvalue >> (24 - offset));
  91. lowvalue <<= offset;
  92. shift = count;
  93. lowvalue &= 0xffffff;
  94. count -= 8 ;
  95. }
  96. lowvalue <<= shift;
  97. br->count = count;
  98. br->lowvalue = lowvalue;
  99. br->range = range;
  100. }
  101. #ifdef __cplusplus
  102. } // extern "C"
  103. #endif
  104. #endif // VP8_ENCODER_BOOLHUFF_H_