jpegls.h 3.2 KB

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