You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1KB

  1. /*
  2. * JPEG-LS common code
  3. * Copyright (c) 2003 Michael Niedermayer
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. * JPEG-LS common code.
  25. */
  26. #ifndef AVCODEC_JPEGLS_H
  27. #define AVCODEC_JPEGLS_H
  28. #include "libavutil/common.h"
  29. #include "avcodec.h"
  30. typedef struct JpeglsContext {
  31. AVCodecContext *avctx;
  32. } JpeglsContext;
  33. typedef struct JLSState {
  34. int T1, T2, T3;
  35. int A[367], B[367], C[365], N[367];
  36. int limit, reset, bpp, qbpp, maxval, range;
  37. int near, twonear;
  38. int run_index[3];
  39. } JLSState;
  40. extern const uint8_t ff_log2_run[32];
  41. /**
  42. * Calculate initial JPEG-LS parameters
  43. */
  44. void ff_jpegls_init_state(JLSState *state);
  45. /**
  46. * Calculate quantized gradient value, used for context determination
  47. */
  48. static inline int ff_jpegls_quantize(JLSState *s, int v)
  49. {
  50. if (v == 0)
  51. return 0;
  52. if (v < 0) {
  53. if (v <= -s->T3)
  54. return -4;
  55. if (v <= -s->T2)
  56. return -3;
  57. if (v <= -s->T1)
  58. return -2;
  59. if (v < -s->near)
  60. return -1;
  61. return 0;
  62. } else {
  63. if (v <= s->near)
  64. return 0;
  65. if (v < s->T1)
  66. return 1;
  67. if (v < s->T2)
  68. return 2;
  69. if (v < s->T3)
  70. return 3;
  71. return 4;
  72. }
  73. }
  74. /**
  75. * Calculate JPEG-LS codec values
  76. */
  77. void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);
  78. static inline void ff_jpegls_downscale_state(JLSState *state, int Q)
  79. {
  80. if (state->N[Q] == state->reset) {
  81. state->A[Q] >>= 1;
  82. state->B[Q] >>= 1;
  83. state->N[Q] >>= 1;
  84. }
  85. state->N[Q]++;
  86. }
  87. static inline int ff_jpegls_update_state_regular(JLSState *state,
  88. int Q, int err)
  89. {
  90. state->A[Q] += FFABS(err);
  91. err *= state->twonear;
  92. state->B[Q] += err;
  93. ff_jpegls_downscale_state(state, Q);
  94. if (state->B[Q] <= -state->N[Q]) {
  95. state->B[Q] = FFMAX(state->B[Q] + state->N[Q], 1 - state->N[Q]);
  96. if (state->C[Q] > -128)
  97. state->C[Q]--;
  98. } else if (state->B[Q] > 0) {
  99. state->B[Q] = FFMIN(state->B[Q] - state->N[Q], 0);
  100. if (state->C[Q] < 127)
  101. state->C[Q]++;
  102. }
  103. return err;
  104. }
  105. #define R(a, i) (bits == 8 ? ((uint8_t *)(a))[i] : ((uint16_t *)(a))[i])
  106. #define W(a, i, v) (bits == 8 ? (((uint8_t *)(a))[i] = v) : (((uint16_t *)(a))[i] = v))
  107. #endif /* AVCODEC_JPEGLS_H */