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