boolhuff.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. unsigned int lowvalue;
  26. unsigned int range;
  27. int count;
  28. unsigned int pos;
  29. unsigned char *buffer;
  30. unsigned char *buffer_end;
  31. struct vpx_internal_error_info *error;
  32. } BOOL_CODER;
  33. extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer,
  34. 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, size_t len,
  40. const unsigned char *end,
  41. struct vpx_internal_error_info *error) {
  42. if (start + len > start && start + len < end) {
  43. return 1;
  44. } else {
  45. vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
  46. "Truncated packet or corrupt partition ");
  47. }
  48. return 0;
  49. }
  50. static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability) {
  51. unsigned int split;
  52. int count = br->count;
  53. unsigned int range = br->range;
  54. unsigned int lowvalue = br->lowvalue;
  55. register int shift;
  56. #ifdef VP8_ENTROPY_STATS
  57. #if defined(SECTIONBITS_OUTPUT)
  58. if (bit)
  59. Sectionbits[active_section] += vp8_prob_cost[255 - probability];
  60. else
  61. Sectionbits[active_section] += vp8_prob_cost[probability];
  62. #endif
  63. #endif
  64. split = 1 + (((range - 1) * probability) >> 8);
  65. range = split;
  66. if (bit) {
  67. lowvalue += split;
  68. range = br->range - split;
  69. }
  70. shift = vp8_norm[range];
  71. range <<= shift;
  72. count += shift;
  73. if (count >= 0) {
  74. int offset = shift - count;
  75. if ((lowvalue << (offset - 1)) & 0x80000000) {
  76. int x = br->pos - 1;
  77. while (x >= 0 && br->buffer[x] == 0xff) {
  78. br->buffer[x] = (unsigned char)0;
  79. x--;
  80. }
  81. br->buffer[x] += 1;
  82. }
  83. validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
  84. br->buffer[br->pos++] = (lowvalue >> (24 - offset));
  85. lowvalue <<= offset;
  86. shift = count;
  87. lowvalue &= 0xffffff;
  88. count -= 8;
  89. }
  90. lowvalue <<= shift;
  91. br->count = count;
  92. br->lowvalue = lowvalue;
  93. br->range = range;
  94. }
  95. #ifdef __cplusplus
  96. } // extern "C"
  97. #endif
  98. #endif // VP8_ENCODER_BOOLHUFF_H_