lagarithrac.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Lagarith range decoder
  3. * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
  4. * Copyright (c) 2009 David Conrad
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Lagarith range decoder
  25. * @author Nathan Caldwell
  26. * @author David Conrad
  27. */
  28. #ifndef AVCODEC_LAGARITHRAC_H
  29. #define AVCODEC_LAGARITHRAC_H
  30. #include <stdint.h>
  31. #include "libavutil/common.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "avcodec.h"
  34. #include "get_bits.h"
  35. typedef struct lag_rac {
  36. AVCodecContext *avctx;
  37. unsigned low;
  38. unsigned range;
  39. unsigned scale; /**< Number of bits of precision in range. */
  40. unsigned hash_shift; /**< Number of bits to shift to calculate hash for radix search. */
  41. const uint8_t *bytestream_start; /**< Start of input bytestream. */
  42. const uint8_t *bytestream; /**< Current position in input bytestream. */
  43. const uint8_t *bytestream_end; /**< End position of input bytestream. */
  44. int overread;
  45. #define MAX_OVERREAD 4
  46. uint32_t prob[258]; /**< Table of cumulative probability for each symbol. */
  47. uint8_t range_hash[1024]; /**< Hash table mapping upper byte to approximate symbol. */
  48. } lag_rac;
  49. void ff_lag_rac_init(lag_rac *l, GetBitContext *gb, int length);
  50. /* TODO: Optimize */
  51. static inline void lag_rac_refill(lag_rac *l)
  52. {
  53. while (l->range <= 0x800000) {
  54. l->low <<= 8;
  55. l->range <<= 8;
  56. l->low |= 0xff & (AV_RB16(l->bytestream) >> 1);
  57. if (l->bytestream < l->bytestream_end)
  58. l->bytestream++;
  59. else
  60. l->overread++;
  61. }
  62. }
  63. /**
  64. * Decode a single byte from the compressed plane described by *l.
  65. * @param l pointer to lag_rac for the current plane
  66. * @return next byte of decoded data
  67. */
  68. static inline uint8_t lag_get_rac(lag_rac *l)
  69. {
  70. unsigned range_scaled, low_scaled;
  71. int val;
  72. lag_rac_refill(l);
  73. range_scaled = l->range >> l->scale;
  74. if (l->low < range_scaled * l->prob[255]) {
  75. /* val = 0 is frequent enough to deserve a shortcut */
  76. if (l->low < range_scaled * l->prob[1]) {
  77. val = 0;
  78. } else {
  79. low_scaled = l->low / (range_scaled<<(l->hash_shift));
  80. val = l->range_hash[low_scaled];
  81. while (l->low >= range_scaled * l->prob[val + 1])
  82. val++;
  83. }
  84. l->range = range_scaled * (l->prob[val + 1] - l->prob[val]);
  85. } else {
  86. val = 255;
  87. l->range -= range_scaled * l->prob[255];
  88. }
  89. if (!l->range)
  90. l->range = 0x80;
  91. l->low -= range_scaled * l->prob[val];
  92. return val;
  93. }
  94. #endif /* AVCODEC_LAGARITHRAC_H */