]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpegls.c
dashenc: Simplify code by using a local variable
[ffmpeg] / libavcodec / jpegls.c
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 /**
24  * @file
25  * JPEG-LS common code.
26  */
27
28 #include "jpegls.h"
29
30 void ff_jpegls_init_state(JLSState *state)
31 {
32     int i;
33
34     state->twonear = state->near * 2 + 1;
35     state->range   = (state->maxval + state->twonear - 1) / state->twonear + 1;
36
37     // QBPP = ceil(log2(RANGE))
38     for (state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++)
39         ;
40
41     if (state->bpp < 8)
42         state->limit = 2 * state->bpp - state->qbpp + 16;
43     else
44         state->limit = 4 * state->bpp - state->qbpp;
45
46     for (i = 0; i < 367; i++) {
47         state->A[i] = FFMAX(state->range + 32 >> 6, 2);
48         state->N[i] = 1;
49     }
50 }
51
52 /**
53  * Custom value clipping function used in T1, T2, T3 calculation
54  */
55 static inline int iso_clip(int v, int vmin, int vmax)
56 {
57     if (v > vmax || v < vmin)
58         return vmin;
59     else
60         return v;
61 }
62
63 void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
64 {
65     const int basic_t1 = 3;
66     const int basic_t2 = 7;
67     const int basic_t3 = 21;
68     int factor;
69
70     if (s->maxval == 0 || reset_all)
71         s->maxval = (1 << s->bpp) - 1;
72
73     if (s->maxval >= 128) {
74         factor = FFMIN(s->maxval, 4095) + 128 >> 8;
75
76         if (s->T1 == 0 || reset_all)
77             s->T1 = iso_clip(factor * (basic_t1 - 2) + 2 + 3 * s->near,
78                              s->near + 1, s->maxval);
79         if (s->T2 == 0 || reset_all)
80             s->T2 = iso_clip(factor * (basic_t2 - 3) + 3 + 5 * s->near,
81                              s->T1, s->maxval);
82         if (s->T3 == 0 || reset_all)
83             s->T3 = iso_clip(factor * (basic_t3 - 4) + 4 + 7 * s->near,
84                              s->T2, s->maxval);
85     } else {
86         factor = 256 / (s->maxval + 1);
87
88         if (s->T1 == 0 || reset_all)
89             s->T1 = iso_clip(FFMAX(2, basic_t1 / factor + 3 * s->near),
90                              s->near + 1, s->maxval);
91         if (s->T2 == 0 || reset_all)
92             s->T2 = iso_clip(FFMAX(3, basic_t2 / factor + 5 * s->near),
93                              s->T1, s->maxval);
94         if (s->T3 == 0 || reset_all)
95             s->T3 = iso_clip(FFMAX(4, basic_t3 / factor + 7 * s->near),
96                              s->T2, s->maxval);
97     }
98
99     if (s->reset == 0 || reset_all)
100         s->reset = 64;
101     av_dlog(NULL, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
102 }