FFmpeg
jpegls.h
Go to the documentation of this file.
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 JpeglsContext {
40 
41 typedef struct JLSState {
42  int T1, T2, T3;
43  int A[367], B[367], C[365], N[367];
45  int near, twonear;
46  int run_index[4];
47 } JLSState;
48 
49 /**
50  * Calculate initial JPEG-LS parameters
51  */
53 
54 /**
55  * Calculate quantized gradient value, used for context determination
56  */
57 static inline int ff_jpegls_quantize(JLSState *s, int v)
58 {
59  if (v == 0)
60  return 0;
61  if (v < 0) {
62  if (v <= -s->T3)
63  return -4;
64  if (v <= -s->T2)
65  return -3;
66  if (v <= -s->T1)
67  return -2;
68  if (v < -s->near)
69  return -1;
70  return 0;
71  } else {
72  if (v <= s->near)
73  return 0;
74  if (v < s->T1)
75  return 1;
76  if (v < s->T2)
77  return 2;
78  if (v < s->T3)
79  return 3;
80  return 4;
81  }
82 }
83 
84 /**
85  * Calculate JPEG-LS codec values
86  */
87 void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);
88 
89 static inline void ff_jpegls_downscale_state(JLSState *state, int Q)
90 {
91  if (state->N[Q] == state->reset) {
92  state->A[Q] >>= 1;
93  state->B[Q] >>= 1;
94  state->N[Q] >>= 1;
95  }
96  state->N[Q]++;
97 }
98 
100  int Q, int err)
101 {
102  if(FFABS(err) > 0xFFFF || FFABS(err) > INT_MAX - state->A[Q])
103  return -0x10000;
104  state->A[Q] += FFABS(err);
105  err *= state->twonear;
106  state->B[Q] += err;
107 
109 
110  if (state->B[Q] <= -state->N[Q]) {
111  state->B[Q] = FFMAX(state->B[Q] + state->N[Q], 1 - state->N[Q]);
112  if (state->C[Q] > -128)
113  state->C[Q]--;
114  } else if (state->B[Q] > 0) {
115  state->B[Q] = FFMIN(state->B[Q] - state->N[Q], 0);
116  if (state->C[Q] < 127)
117  state->C[Q]++;
118  }
119 
120  return err;
121 }
122 
123 #define R(a, i) (bits == 8 ? ((uint8_t *)(a))[i] : ((uint16_t *)(a))[i])
124 #define W(a, i, v) (bits == 8 ? (((uint8_t *)(a))[i] = v) : (((uint16_t *)(a))[i] = v))
125 
126 #endif /* AVCODEC_JPEGLS_H */
JLSState::bpp
int bpp
Definition: jpegls.h:44
ff_jpegls_reset_coding_parameters
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
Calculate JPEG-LS codec values.
Definition: jpegls.c:62
JLSState::T1
int T1
Definition: jpegls.h:42
internal.h
JLSState::T2
int T2
Definition: jpegls.h:42
JLSState::run_index
int run_index[4]
Definition: jpegls.h:46
JLSState::B
int B[367]
Definition: jpegls.h:43
JLSState
Definition: jpegls.h:41
s
#define s(width, name)
Definition: cbs_vp9.c:257
JpeglsContext::avctx
AVCodecContext * avctx
Definition: jpegls.h:38
JLSState::maxval
int maxval
Definition: jpegls.h:44
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
JLSState::near
int near
Definition: jpegls.h:45
ff_jpegls_downscale_state
static void ff_jpegls_downscale_state(JLSState *state, int Q)
Definition: jpegls.h:89
JLSState::A
int A[367]
Definition: jpegls.h:43
JLSState::range
int range
Definition: jpegls.h:44
JLSState::T3
int T3
Definition: jpegls.h:42
JLSState::twonear
int twonear
Definition: jpegls.h:45
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
state
static struct @314 state
JLSState::reset
int reset
Definition: jpegls.h:44
ff_jpegls_quantize
static int ff_jpegls_quantize(JLSState *s, int v)
Calculate quantized gradient value, used for context determination.
Definition: jpegls.h:57
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
JpeglsContext
Definition: jpegls.h:37
ff_jpegls_update_state_regular
static int ff_jpegls_update_state_regular(JLSState *state, int Q, int err)
Definition: jpegls.h:99
common.h
avcodec.h
JLSState::qbpp
int qbpp
Definition: jpegls.h:44
AVCodecContext
main external API structure.
Definition: avcodec.h:526
JLSState::C
int C[365]
Definition: jpegls.h:43
ff_jpegls_init_state
void ff_jpegls_init_state(JLSState *state)
Calculate initial JPEG-LS parameters.
Definition: jpegls.c:31
JLSState::N
int N[367]
Definition: jpegls.h:43
JLSState::limit
int limit
Definition: jpegls.h:44