2
0

boolhuff.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 VPX_VP8_ENCODER_BOOLHUFF_H_
  18. #define VPX_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. void vp8_start_encode(BOOL_CODER *bc, unsigned char *source,
  34. unsigned char *source_end);
  35. void vp8_encode_value(BOOL_CODER *bc, int data, int bits);
  36. 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 *bc, int bit, int probability) {
  51. unsigned int split;
  52. int count = bc->count;
  53. unsigned int range = bc->range;
  54. unsigned int lowvalue = bc->lowvalue;
  55. int shift;
  56. split = 1 + (((range - 1) * probability) >> 8);
  57. range = split;
  58. if (bit) {
  59. lowvalue += split;
  60. range = bc->range - split;
  61. }
  62. shift = vp8_norm[range];
  63. range <<= shift;
  64. count += shift;
  65. if (count >= 0) {
  66. int offset = shift - count;
  67. if ((lowvalue << (offset - 1)) & 0x80000000) {
  68. int x = bc->pos - 1;
  69. while (x >= 0 && bc->buffer[x] == 0xff) {
  70. bc->buffer[x] = (unsigned char)0;
  71. x--;
  72. }
  73. bc->buffer[x] += 1;
  74. }
  75. validate_buffer(bc->buffer + bc->pos, 1, bc->buffer_end, bc->error);
  76. bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
  77. lowvalue <<= offset;
  78. shift = count;
  79. lowvalue &= 0xffffff;
  80. count -= 8;
  81. }
  82. lowvalue <<= shift;
  83. bc->count = count;
  84. bc->lowvalue = lowvalue;
  85. bc->range = range;
  86. }
  87. #ifdef __cplusplus
  88. } // extern "C"
  89. #endif
  90. #endif // VPX_VP8_ENCODER_BOOLHUFF_H_