jpegls.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 libavcodec/jpegls.c
  24. * JPEG-LS common code.
  25. */
  26. #include "jpegls.h"
  27. void ff_jpegls_init_state(JLSState *state){
  28. int i;
  29. state->twonear = state->near * 2 + 1;
  30. state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
  31. // QBPP = ceil(log2(RANGE))
  32. for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
  33. if(state->bpp < 8)
  34. state->limit = 16 + 2 * state->bpp - state->qbpp;
  35. else
  36. state->limit = (4 * state->bpp) - state->qbpp;
  37. for(i = 0; i < 367; i++) {
  38. state->A[i] = FFMAX((state->range + 32) >> 6, 2);
  39. state->N[i] = 1;
  40. }
  41. }
  42. /**
  43. * Custom value clipping function used in T1, T2, T3 calculation
  44. */
  45. static inline int iso_clip(int v, int vmin, int vmax){
  46. if(v > vmax || v < vmin) return vmin;
  47. else return v;
  48. }
  49. void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all){
  50. const int basic_t1= 3;
  51. const int basic_t2= 7;
  52. const int basic_t3= 21;
  53. int factor;
  54. if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
  55. if(s->maxval >=128){
  56. factor= (FFMIN(s->maxval, 4095) + 128)>>8;
  57. if(s->T1==0 || reset_all)
  58. s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
  59. if(s->T2==0 || reset_all)
  60. s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
  61. if(s->T3==0 || reset_all)
  62. s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
  63. }else{
  64. factor= 256 / (s->maxval + 1);
  65. if(s->T1==0 || reset_all)
  66. s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
  67. if(s->T2==0 || reset_all)
  68. s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
  69. if(s->T3==0 || reset_all)
  70. s->T3= iso_clip(FFMAX(4, basic_t3/factor + 7*s->near), s->T2, s->maxval);
  71. }
  72. if(s->reset==0 || reset_all) s->reset= 64;
  73. // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
  74. }