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.

115 lines
3.0KB

  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 "avcodec.h"
  29. #include "libavutil/common.h"
  30. typedef struct JpeglsContext{
  31. AVCodecContext *avctx;
  32. AVFrame picture;
  33. }JpeglsContext;
  34. typedef struct JLSState{
  35. int T1, T2, T3;
  36. int A[367], B[367], C[365], N[367];
  37. int limit, reset, bpp, qbpp, maxval, range;
  38. int near, twonear;
  39. int run_index[3];
  40. }JLSState;
  41. extern const uint8_t ff_log2_run[32];
  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){ //FIXME optimize
  50. if(v==0) return 0;
  51. if(v < 0){
  52. if(v <= -s->T3) return -4;
  53. if(v <= -s->T2) return -3;
  54. if(v <= -s->T1) return -2;
  55. if(v < -s->near) return -1;
  56. return 0;
  57. }else{
  58. if(v <= s->near) return 0;
  59. if(v < s->T1) return 1;
  60. if(v < s->T2) return 2;
  61. if(v < s->T3) return 3;
  62. return 4;
  63. }
  64. }
  65. /**
  66. * Calculate JPEG-LS codec values
  67. */
  68. void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);
  69. static inline void ff_jpegls_downscale_state(JLSState *state, int Q){
  70. if(state->N[Q] == state->reset){
  71. state->A[Q] >>=1;
  72. state->B[Q] >>=1;
  73. state->N[Q] >>=1;
  74. }
  75. state->N[Q]++;
  76. }
  77. static inline int ff_jpegls_update_state_regular(JLSState *state, int Q, int err){
  78. if(FFABS(err) > 0xFFFF)
  79. return -0x10000;
  80. state->A[Q] += FFABS(err);
  81. err *= state->twonear;
  82. state->B[Q] += err;
  83. ff_jpegls_downscale_state(state, Q);
  84. if(state->B[Q] <= -state->N[Q]) {
  85. state->B[Q]= FFMAX(state->B[Q] + state->N[Q], 1-state->N[Q]);
  86. if(state->C[Q] > -128)
  87. state->C[Q]--;
  88. }else if(state->B[Q] > 0){
  89. state->B[Q]= FFMIN(state->B[Q] - state->N[Q], 0);
  90. if(state->C[Q] < 127)
  91. state->C[Q]++;
  92. }
  93. return err;
  94. }
  95. #define R(a, i ) (bits == 8 ? ((uint8_t*)(a))[i] : ((uint16_t*)(a))[i] )
  96. #define W(a, i, v) (bits == 8 ? (((uint8_t*)(a))[i]=v) : (((uint16_t*)(a))[i]=v))
  97. #endif /* AVCODEC_JPEGLS_H */