2
0

bitwriter.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_VPX_DSP_BITWRITER_H_
  11. #define VPX_VPX_DSP_BITWRITER_H_
  12. #include <stdio.h>
  13. #include "vpx_ports/mem.h"
  14. #include "vpx_dsp/prob.h"
  15. #if CONFIG_BITSTREAM_DEBUG
  16. #include "vpx_util/vpx_debug_util.h"
  17. #endif // CONFIG_BITSTREAM_DEBUG
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef struct vpx_writer {
  22. unsigned int lowvalue;
  23. unsigned int range;
  24. int count;
  25. unsigned int pos;
  26. uint8_t *buffer;
  27. } vpx_writer;
  28. void vpx_start_encode(vpx_writer *br, uint8_t *source);
  29. void vpx_stop_encode(vpx_writer *br);
  30. static INLINE void vpx_write(vpx_writer *br, int bit, int probability) {
  31. unsigned int split;
  32. int count = br->count;
  33. unsigned int range = br->range;
  34. unsigned int lowvalue = br->lowvalue;
  35. int shift;
  36. #if CONFIG_BITSTREAM_DEBUG
  37. /*
  38. int queue_r = 0;
  39. int frame_idx_r = 0;
  40. int queue_w = bitstream_queue_get_write();
  41. int frame_idx_w = bitstream_queue_get_frame_write();
  42. if (frame_idx_w == frame_idx_r && queue_w == queue_r) {
  43. fprintf(stderr, "\n *** bitstream queue at frame_idx_w %d queue_w %d\n",
  44. frame_idx_w, queue_w);
  45. assert(0);
  46. }
  47. */
  48. bitstream_queue_push(bit, probability);
  49. #endif
  50. split = 1 + (((range - 1) * probability) >> 8);
  51. range = split;
  52. if (bit) {
  53. lowvalue += split;
  54. range = br->range - split;
  55. }
  56. shift = vpx_norm[range];
  57. range <<= shift;
  58. count += shift;
  59. if (count >= 0) {
  60. int offset = shift - count;
  61. if ((lowvalue << (offset - 1)) & 0x80000000) {
  62. int x = br->pos - 1;
  63. while (x >= 0 && br->buffer[x] == 0xff) {
  64. br->buffer[x] = 0;
  65. x--;
  66. }
  67. br->buffer[x] += 1;
  68. }
  69. br->buffer[br->pos++] = (lowvalue >> (24 - offset));
  70. lowvalue <<= offset;
  71. shift = count;
  72. lowvalue &= 0xffffff;
  73. count -= 8;
  74. }
  75. lowvalue <<= shift;
  76. br->count = count;
  77. br->lowvalue = lowvalue;
  78. br->range = range;
  79. }
  80. static INLINE void vpx_write_bit(vpx_writer *w, int bit) {
  81. vpx_write(w, bit, 128); // vpx_prob_half
  82. }
  83. static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
  84. int bit;
  85. for (bit = bits - 1; bit >= 0; bit--) vpx_write_bit(w, 1 & (data >> bit));
  86. }
  87. #define vpx_write_prob(w, v) vpx_write_literal((w), (v), 8)
  88. #ifdef __cplusplus
  89. } // extern "C"
  90. #endif
  91. #endif // VPX_VPX_DSP_BITWRITER_H_