vp9_writer.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 VP9_ENCODER_VP9_WRITER_H_
  11. #define VP9_ENCODER_VP9_WRITER_H_
  12. #include "vpx_ports/mem.h"
  13. #include "vp9/common/vp9_prob.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef struct vp9_writer {
  18. unsigned int lowvalue;
  19. unsigned int range;
  20. int count;
  21. unsigned int pos;
  22. uint8_t *buffer;
  23. } vp9_writer;
  24. void vp9_start_encode(vp9_writer *bc, uint8_t *buffer);
  25. void vp9_stop_encode(vp9_writer *bc);
  26. static INLINE void vp9_write(vp9_writer *br, int bit, int probability) {
  27. unsigned int split;
  28. int count = br->count;
  29. unsigned int range = br->range;
  30. unsigned int lowvalue = br->lowvalue;
  31. register unsigned int shift;
  32. split = 1 + (((range - 1) * probability) >> 8);
  33. range = split;
  34. if (bit) {
  35. lowvalue += split;
  36. range = br->range - split;
  37. }
  38. shift = vp9_norm[range];
  39. range <<= shift;
  40. count += shift;
  41. if (count >= 0) {
  42. int offset = shift - count;
  43. if ((lowvalue << (offset - 1)) & 0x80000000) {
  44. int x = br->pos - 1;
  45. while (x >= 0 && br->buffer[x] == 0xff) {
  46. br->buffer[x] = 0;
  47. x--;
  48. }
  49. br->buffer[x] += 1;
  50. }
  51. br->buffer[br->pos++] = (lowvalue >> (24 - offset));
  52. lowvalue <<= offset;
  53. shift = count;
  54. lowvalue &= 0xffffff;
  55. count -= 8;
  56. }
  57. lowvalue <<= shift;
  58. br->count = count;
  59. br->lowvalue = lowvalue;
  60. br->range = range;
  61. }
  62. static INLINE void vp9_write_bit(vp9_writer *w, int bit) {
  63. vp9_write(w, bit, 128); // vp9_prob_half
  64. }
  65. static INLINE void vp9_write_literal(vp9_writer *w, int data, int bits) {
  66. int bit;
  67. for (bit = bits - 1; bit >= 0; bit--)
  68. vp9_write_bit(w, 1 & (data >> bit));
  69. }
  70. #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8)
  71. #ifdef __cplusplus
  72. } // extern "C"
  73. #endif
  74. #endif // VP9_ENCODER_VP9_WRITER_H_