2 * Discrete wavelet transform
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
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.
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.
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
25 * Discrete wavelet transform
28 #include "libavutil/common.h"
29 #include "libavutil/mem.h"
30 #include "jpeg2000dwt.h"
33 /* Defines for 9/7 DWT lifting parameters.
34 * Parameters are in float. */
35 #define F_LFTG_ALPHA 1.586134342059924f
36 #define F_LFTG_BETA 0.052980118572961f
37 #define F_LFTG_GAMMA 0.882911075530934f
38 #define F_LFTG_DELTA 0.443506852043971f
39 #define F_LFTG_K 1.230174104914001f
40 #define F_LFTG_X 1.625732422f
41 /* FIXME: Why use 1.625732422 instead of 1/F_LFTG_K?
42 * Incorrect value in JPEG2000 norm.
43 * see (ISO/IEC 15444:1 (version 2002) F.3.8.2 */
45 /* Lifting parameters in integer format.
46 * Computed as param = (float param) * (1 << 16) */
47 #define I_LFTG_ALPHA 103949
48 #define I_LFTG_BETA 3472
49 #define I_LFTG_GAMMA 57862
50 #define I_LFTG_DELTA 29066
51 #define I_LFTG_K 80621
52 #define I_LFTG_X 106544
55 static inline void extend53(int *p, int i0, int i1)
57 p[i0 - 1] = p[i0 + 1];
59 p[i0 - 2] = p[i0 + 2];
60 p[i1 + 1] = p[i1 - 3];
63 static inline void extend97_float(float *p, int i0, int i1)
67 for (i = 1; i <= 4; i++) {
68 p[i0 - i] = p[i0 + i];
69 p[i1 + i - 1] = p[i1 - i - 1];
73 static inline void extend97_int(int32_t *p, int i0, int i1)
77 for (i = 1; i <= 4; i++) {
78 p[i0 - i] = p[i0 + i];
79 p[i1 + i - 1] = p[i1 - i - 1];
83 static void sr_1d53(int *p, int i0, int i1)
92 for (i = i0 / 2; i < i1 / 2 + 1; i++)
93 p[2 * i] -= (p[2 * i - 1] + p[2 * i + 1] + 2) >> 2;
94 for (i = i0 / 2; i < i1 / 2; i++)
95 p[2 * i + 1] += (p[2 * i] + p[2 * i + 2]) >> 1;
98 static void dwt_decode53(DWTContext *s, int *t)
101 int w = s->linelen[s->ndeclevels - 1][0];
102 int32_t *line = s->i_linebuf;
105 for (lev = 0; lev < s->ndeclevels; lev++) {
106 int lh = s->linelen[lev][0],
107 lv = s->linelen[lev][1],
115 for (lp = 0; lp < lv; lp++) {
117 // copy with interleaving
118 for (i = mh; i < lh; i += 2, j++)
119 l[i] = t[w * lp + j];
120 for (i = 1 - mh; i < lh; i += 2, j++)
121 l[i] = t[w * lp + j];
123 sr_1d53(line, mh, mh + lh);
125 for (i = 0; i < lh; i++)
126 t[w * lp + i] = l[i];
131 for (lp = 0; lp < lh; lp++) {
133 // copy with interleaving
134 for (i = mv; i < lv; i += 2, j++)
135 l[i] = t[w * j + lp];
136 for (i = 1 - mv; i < lv; i += 2, j++)
137 l[i] = t[w * j + lp];
139 sr_1d53(line, mv, mv + lv);
141 for (i = 0; i < lv; i++)
142 t[w * i + lp] = l[i];
147 static void sr_1d97_float(float *p, int i0, int i1)
154 extend97_float(p, i0, i1);
157 for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
158 p[2 * i] *= F_LFTG_K;
160 for (i = i0 / 2 - 2; i < i1 / 2 + 2; i++)
161 p[2 * i + 1] *= F_LFTG_X;
163 for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
164 p[2 * i] -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]);
166 for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++)
167 p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]);
169 for (i = i0 / 2; i < i1 / 2 + 1; i++)
170 p[2 * i] += F_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]);
172 for (i = i0 / 2; i < i1 / 2; i++)
173 p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]);
176 static void dwt_decode97_float(DWTContext *s, float *t)
179 int w = s->linelen[s->ndeclevels - 1][0];
180 float *line = s->f_linebuf;
182 /* position at index O of line range [0-5,w+5] cf. extend function */
185 for (lev = 0; lev < s->ndeclevels; lev++) {
186 int lh = s->linelen[lev][0],
187 lv = s->linelen[lev][1],
194 for (lp = 0; lp < lv; lp++) {
196 // copy with interleaving
197 for (i = mh; i < lh; i += 2, j++)
198 l[i] = data[w * lp + j];
199 for (i = 1 - mh; i < lh; i += 2, j++)
200 l[i] = data[w * lp + j];
202 sr_1d97_float(line, mh, mh + lh);
204 for (i = 0; i < lh; i++)
205 data[w * lp + i] = l[i];
210 for (lp = 0; lp < lh; lp++) {
212 // copy with interleaving
213 for (i = mv; i < lv; i += 2, j++)
214 l[i] = data[w * j + lp];
215 for (i = 1 - mv; i < lv; i += 2, j++)
216 l[i] = data[w * j + lp];
218 sr_1d97_float(line, mv, mv + lv);
220 for (i = 0; i < lv; i++)
221 data[w * i + lp] = l[i];
226 static void sr_1d97_int(int32_t *p, int i0, int i1)
233 extend97_int(p, i0, i1);
236 for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
237 p[2 * i] = ((p[2 * i] * I_LFTG_K) + (1 << 15)) >> 16;
239 for (i = i0 / 2 - 2; i < i1 / 2 + 2; i++)
240 p[2 * i + 1] = ((p[2 * i + 1] * I_LFTG_X) + (1 << 15)) >> 16;
242 for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
243 p[2 * i] -= (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
245 for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++)
246 p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
248 for (i = i0 / 2; i < i1 / 2 + 1; i++)
249 p[2 * i] += (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
251 for (i = i0 / 2; i < i1 / 2; i++)
252 p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16;
255 static void dwt_decode97_int(DWTContext *s, int32_t *t)
258 int w = s->linelen[s->ndeclevels - 1][0];
259 int32_t *line = s->i_linebuf;
261 /* position at index O of line range [0-5,w+5] cf. extend function */
264 for (lev = 0; lev < s->ndeclevels; lev++) {
265 int lh = s->linelen[lev][0],
266 lv = s->linelen[lev][1],
273 for (lp = 0; lp < lv; lp++) {
275 // copy with interleaving
276 for (i = mh; i < lh; i += 2, j++)
277 l[i] = data[w * lp + j];
278 for (i = 1 - mh; i < lh; i += 2, j++)
279 l[i] = data[w * lp + j];
281 sr_1d97_int(line, mh, mh + lh);
283 for (i = 0; i < lh; i++)
284 data[w * lp + i] = l[i];
289 for (lp = 0; lp < lh; lp++) {
291 // copy with interleaving
292 for (i = mv; i < lv; i += 2, j++)
293 l[i] = data[w * j + lp];
294 for (i = 1 - mv; i < lv; i += 2, j++)
295 l[i] = data[w * j + lp];
297 sr_1d97_int(line, mv, mv + lv);
299 for (i = 0; i < lv; i++)
300 data[w * i + lp] = l[i];
305 int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2],
306 int decomp_levels, int type)
308 int i, j, lev = decomp_levels, maxlen,
311 s->ndeclevels = decomp_levels;
314 for (i = 0; i < 2; i++)
315 for (j = 0; j < 2; j++)
316 b[i][j] = border[i][j];
318 maxlen = FFMAX(b[0][1] - b[0][0],
321 for (i = 0; i < 2; i++) {
322 s->linelen[lev][i] = b[i][1] - b[i][0];
323 s->mod[lev][i] = b[i][0] & 1;
324 for (j = 0; j < 2; j++)
325 b[i][j] = (b[i][j] + 1) >> 1;
329 s->f_linebuf = av_malloc((maxlen + 12) * sizeof(*s->f_linebuf));
331 return AVERROR(ENOMEM);
334 s->i_linebuf = av_malloc((maxlen + 12) * sizeof(*s->i_linebuf));
336 return AVERROR(ENOMEM);
339 s->i_linebuf = av_malloc((maxlen + 6) * sizeof(*s->i_linebuf));
341 return AVERROR(ENOMEM);
349 int ff_dwt_decode(DWTContext *s, void *t)
353 dwt_decode97_float(s, t);
356 dwt_decode97_int(s, t);
367 void ff_dwt_destroy(DWTContext *s)
369 av_freep(&s->f_linebuf);
370 av_freep(&s->i_linebuf);