Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Discrete wavelet transform | ||
3 | * Copyright (c) 2007 Kamil Nowosad | ||
4 | * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com> | ||
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 | * Discrete wavelet transform | ||
26 | */ | ||
27 | |||
28 | #include "libavutil/error.h" | ||
29 | #include "libavutil/macros.h" | ||
30 | #include "libavutil/mem.h" | ||
31 | #include "jpeg2000dwt.h" | ||
32 | |||
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 | |||
40 | /* Lifting parameters in integer format. | ||
41 | * Computed as param = (float param) * (1 << 16) */ | ||
42 | #define I_LFTG_ALPHA 103949ll | ||
43 | #define I_LFTG_BETA 3472ll | ||
44 | #define I_LFTG_GAMMA 57862ll | ||
45 | #define I_LFTG_DELTA 29066ll | ||
46 | #define I_LFTG_K 80621ll | ||
47 | #define I_LFTG_X 53274ll | ||
48 | #define I_PRESHIFT 8 | ||
49 | |||
50 | 8040345 | static inline void extend53(int *p, int i0, int i1) | |
51 | { | ||
52 | 8040345 | p[i0 - 1] = p[i0 + 1]; | |
53 | 8040345 | p[i1] = p[i1 - 2]; | |
54 | 8040345 | p[i0 - 2] = p[i0 + 2]; | |
55 | 8040345 | p[i1 + 1] = p[i1 - 3]; | |
56 | 8040345 | } | |
57 | |||
58 | 12364 | static inline void extend97_float(float *p, int i0, int i1) | |
59 | { | ||
60 | int i; | ||
61 | |||
62 |
2/2✓ Branch 0 taken 49456 times.
✓ Branch 1 taken 12364 times.
|
61820 | for (i = 1; i <= 4; i++) { |
63 | 49456 | p[i0 - i] = p[i0 + i]; | |
64 | 49456 | p[i1 + i - 1] = p[i1 - i - 1]; | |
65 | } | ||
66 | 12364 | } | |
67 | |||
68 | 2363699 | static inline void extend97_int(int32_t *p, int i0, int i1) | |
69 | { | ||
70 | int i; | ||
71 | |||
72 |
2/2✓ Branch 0 taken 9454796 times.
✓ Branch 1 taken 2363699 times.
|
11818495 | for (i = 1; i <= 4; i++) { |
73 | 9454796 | p[i0 - i] = p[i0 + i]; | |
74 | 9454796 | p[i1 + i - 1] = p[i1 - i - 1]; | |
75 | } | ||
76 | 2363699 | } | |
77 | |||
78 | 3856290 | static void sd_1d53(int *p, int i0, int i1) | |
79 | { | ||
80 | int i; | ||
81 | |||
82 |
2/2✓ Branch 0 taken 16608 times.
✓ Branch 1 taken 3839682 times.
|
3856290 | if (i1 <= i0 + 1) { |
83 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16544 times.
|
16608 | if (i0 == 1) |
84 | 64 | p[1] *= 2; | |
85 | 16608 | return; | |
86 | } | ||
87 | |||
88 | 3839682 | extend53(p, i0, i1); | |
89 | |||
90 |
2/2✓ Branch 0 taken 207535651 times.
✓ Branch 1 taken 3839682 times.
|
211375333 | for (i = ((i0+1)>>1) - 1; i < (i1+1)>>1; i++) |
91 | 207535651 | p[2*i+1] -= (p[2*i] + p[2*i+2]) >> 1; | |
92 |
2/2✓ Branch 0 taken 203695969 times.
✓ Branch 1 taken 3839682 times.
|
207535651 | for (i = ((i0+1)>>1); i < (i1+1)>>1; i++) |
93 | 203695969 | p[2*i] += (p[2*i-1] + p[2*i+1] + 2) >> 2; | |
94 | } | ||
95 | |||
96 | 6520 | static void dwt_encode53(DWTContext *s, int *t) | |
97 | { | ||
98 | int lev, | ||
99 | 6520 | w = s->linelen[s->ndeclevels-1][0]; | |
100 | 6520 | int *line = s->i_linebuf; | |
101 | 6520 | line += 3; | |
102 | |||
103 |
2/2✓ Branch 0 taken 39338 times.
✓ Branch 1 taken 6520 times.
|
45858 | for (lev = s->ndeclevels-1; lev >= 0; lev--){ |
104 | 39338 | int lh = s->linelen[lev][0], | |
105 | 39338 | lv = s->linelen[lev][1], | |
106 | 39338 | mh = s->mod[lev][0], | |
107 | 39338 | mv = s->mod[lev][1], | |
108 | lp; | ||
109 | int *l; | ||
110 | |||
111 | // VER_SD | ||
112 | 39338 | l = line + mv; | |
113 |
2/2✓ Branch 0 taken 2116676 times.
✓ Branch 1 taken 39338 times.
|
2156014 | for (lp = 0; lp < lh; lp++) { |
114 | 2116676 | int i, j = 0; | |
115 | |||
116 |
2/2✓ Branch 0 taken 203680547 times.
✓ Branch 1 taken 2116676 times.
|
205797223 | for (i = 0; i < lv; i++) |
117 | 203680547 | l[i] = t[w*i + lp]; | |
118 | |||
119 | 2116676 | sd_1d53(line, mv, mv + lv); | |
120 | |||
121 | // copy back and deinterleave | ||
122 |
2/2✓ Branch 0 taken 101857016 times.
✓ Branch 1 taken 2116676 times.
|
103973692 | for (i = mv; i < lv; i+=2, j++) |
123 | 101857016 | t[w*j + lp] = l[i]; | |
124 |
2/2✓ Branch 0 taken 101823531 times.
✓ Branch 1 taken 2116676 times.
|
103940207 | for (i = 1-mv; i < lv; i+=2, j++) |
125 | 101823531 | t[w*j + lp] = l[i]; | |
126 | } | ||
127 | |||
128 | // HOR_SD | ||
129 | 39338 | l = line + mh; | |
130 |
2/2✓ Branch 0 taken 1739614 times.
✓ Branch 1 taken 39338 times.
|
1778952 | for (lp = 0; lp < lv; lp++){ |
131 | 1739614 | int i, j = 0; | |
132 | |||
133 |
2/2✓ Branch 0 taken 203680547 times.
✓ Branch 1 taken 1739614 times.
|
205420161 | for (i = 0; i < lh; i++) |
134 | 203680547 | l[i] = t[w*lp + i]; | |
135 | |||
136 | 1739614 | sd_1d53(line, mh, mh + lh); | |
137 | |||
138 | // copy back and deinterleave | ||
139 |
2/2✓ Branch 0 taken 101855479 times.
✓ Branch 1 taken 1739614 times.
|
103595093 | for (i = mh; i < lh; i+=2, j++) |
140 | 101855479 | t[w*lp + j] = l[i]; | |
141 |
2/2✓ Branch 0 taken 101825068 times.
✓ Branch 1 taken 1739614 times.
|
103564682 | for (i = 1-mh; i < lh; i+=2, j++) |
142 | 101825068 | t[w*lp + j] = l[i]; | |
143 | } | ||
144 | } | ||
145 | 6520 | } | |
146 | 6290 | static void sd_1d97_float(float *p, int i0, int i1) | |
147 | { | ||
148 | int i; | ||
149 | |||
150 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 6182 times.
|
6290 | if (i1 <= i0 + 1) { |
151 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 44 times.
|
108 | if (i0 == 1) |
152 | 64 | p[1] *= F_LFTG_X * 2; | |
153 | else | ||
154 | 44 | p[0] *= F_LFTG_K; | |
155 | 108 | return; | |
156 | } | ||
157 | |||
158 | 6182 | extend97_float(p, i0, i1); | |
159 | 6182 | i0++; i1++; | |
160 | |||
161 |
2/2✓ Branch 0 taken 214515 times.
✓ Branch 1 taken 6182 times.
|
220697 | for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++) |
162 | 214515 | p[2*i+1] -= 1.586134 * (p[2*i] + p[2*i+2]); | |
163 |
2/2✓ Branch 0 taken 208333 times.
✓ Branch 1 taken 6182 times.
|
214515 | for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++) |
164 | 208333 | p[2*i] -= 0.052980 * (p[2*i-1] + p[2*i+1]); | |
165 |
2/2✓ Branch 0 taken 202151 times.
✓ Branch 1 taken 6182 times.
|
208333 | for (i = (i0>>1) - 1; i < (i1>>1); i++) |
166 | 202151 | p[2*i+1] += 0.882911 * (p[2*i] + p[2*i+2]); | |
167 |
2/2✓ Branch 0 taken 195969 times.
✓ Branch 1 taken 6182 times.
|
202151 | for (i = (i0>>1); i < (i1>>1); i++) |
168 | 195969 | p[2*i] += 0.443506 * (p[2*i-1] + p[2*i+1]); | |
169 | } | ||
170 | |||
171 | 20 | static void dwt_encode97_float(DWTContext *s, float *t) | |
172 | { | ||
173 | int lev, | ||
174 | 20 | w = s->linelen[s->ndeclevels-1][0]; | |
175 | 20 | float *line = s->f_linebuf; | |
176 | 20 | line += 5; | |
177 | |||
178 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 20 times.
|
358 | for (lev = s->ndeclevels-1; lev >= 0; lev--){ |
179 | 338 | int lh = s->linelen[lev][0], | |
180 | 338 | lv = s->linelen[lev][1], | |
181 | 338 | mh = s->mod[lev][0], | |
182 | 338 | mv = s->mod[lev][1], | |
183 | lp; | ||
184 | float *l; | ||
185 | |||
186 | // HOR_SD | ||
187 | 338 | l = line + mh; | |
188 |
2/2✓ Branch 0 taken 3614 times.
✓ Branch 1 taken 338 times.
|
3952 | for (lp = 0; lp < lv; lp++){ |
189 | 3614 | int i, j = 0; | |
190 | |||
191 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 3614 times.
|
199661 | for (i = 0; i < lh; i++) |
192 | 196047 | l[i] = t[w*lp + i]; | |
193 | |||
194 | 3614 | sd_1d97_float(line, mh, mh + lh); | |
195 | |||
196 | // copy back and deinterleave | ||
197 |
2/2✓ Branch 0 taken 97979 times.
✓ Branch 1 taken 3614 times.
|
101593 | for (i = mh; i < lh; i+=2, j++) |
198 | 97979 | t[w*lp + j] = l[i]; | |
199 |
2/2✓ Branch 0 taken 98068 times.
✓ Branch 1 taken 3614 times.
|
101682 | for (i = 1-mh; i < lh; i+=2, j++) |
200 | 98068 | t[w*lp + j] = l[i]; | |
201 | } | ||
202 | |||
203 | // VER_SD | ||
204 | 338 | l = line + mv; | |
205 |
2/2✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 338 times.
|
3014 | for (lp = 0; lp < lh; lp++) { |
206 | 2676 | int i, j = 0; | |
207 | |||
208 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 2676 times.
|
198723 | for (i = 0; i < lv; i++) |
209 | 196047 | l[i] = t[w*i + lp]; | |
210 | |||
211 | 2676 | sd_1d97_float(line, mv, mv + lv); | |
212 | |||
213 | // copy back and deinterleave | ||
214 |
2/2✓ Branch 0 taken 98016 times.
✓ Branch 1 taken 2676 times.
|
100692 | for (i = mv; i < lv; i+=2, j++) |
215 | 98016 | t[w*j + lp] = l[i]; | |
216 |
2/2✓ Branch 0 taken 98031 times.
✓ Branch 1 taken 2676 times.
|
100707 | for (i = 1-mv; i < lv; i+=2, j++) |
217 | 98031 | t[w*j + lp] = l[i]; | |
218 | } | ||
219 | } | ||
220 | 20 | } | |
221 | |||
222 | 1161290 | static void sd_1d97_int(int *p, int i0, int i1) | |
223 | { | ||
224 | int i; | ||
225 | |||
226 |
2/2✓ Branch 0 taken 5058 times.
✓ Branch 1 taken 1156232 times.
|
1161290 | if (i1 <= i0 + 1) { |
227 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4994 times.
|
5058 | if (i0 == 1) |
228 | 64 | p[1] = (p[1] * I_LFTG_X + (1<<14)) >> 15; | |
229 | else | ||
230 | 4994 | p[0] = (p[0] * I_LFTG_K + (1<<15)) >> 16; | |
231 | 5058 | return; | |
232 | } | ||
233 | |||
234 | 1156232 | extend97_int(p, i0, i1); | |
235 | 1156232 | i0++; i1++; | |
236 | |||
237 |
2/2✓ Branch 0 taken 64714665 times.
✓ Branch 1 taken 1156232 times.
|
65870897 | for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++) |
238 | 64714665 | p[2 * i + 1] -= (I_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16; | |
239 |
2/2✓ Branch 0 taken 63558433 times.
✓ Branch 1 taken 1156232 times.
|
64714665 | for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++) |
240 | 63558433 | p[2 * i] -= (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; | |
241 |
2/2✓ Branch 0 taken 62402201 times.
✓ Branch 1 taken 1156232 times.
|
63558433 | for (i = (i0>>1) - 1; i < (i1>>1); i++) |
242 | 62402201 | p[2 * i + 1] += (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16; | |
243 |
2/2✓ Branch 0 taken 61245969 times.
✓ Branch 1 taken 1156232 times.
|
62402201 | for (i = (i0>>1); i < (i1>>1); i++) |
244 | 61245969 | p[2 * i] += (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; | |
245 | } | ||
246 | |||
247 | 1970 | static void dwt_encode97_int(DWTContext *s, int *t) | |
248 | { | ||
249 | int lev; | ||
250 | 1970 | int w = s->linelen[s->ndeclevels-1][0]; | |
251 | 1970 | int h = s->linelen[s->ndeclevels-1][1]; | |
252 | int i; | ||
253 | 1970 | int *line = s->i_linebuf; | |
254 | 1970 | line += 5; | |
255 | |||
256 |
2/2✓ Branch 0 taken 45940533 times.
✓ Branch 1 taken 1970 times.
|
45942503 | for (i = 0; i < w * h; i++) |
257 | 45940533 | t[i] *= 1 << I_PRESHIFT; | |
258 | |||
259 |
2/2✓ Branch 0 taken 12038 times.
✓ Branch 1 taken 1970 times.
|
14008 | for (lev = s->ndeclevels-1; lev >= 0; lev--){ |
260 | 12038 | int lh = s->linelen[lev][0], | |
261 | 12038 | lv = s->linelen[lev][1], | |
262 | 12038 | mh = s->mod[lev][0], | |
263 | 12038 | mv = s->mod[lev][1], | |
264 | lp; | ||
265 | int *l; | ||
266 | |||
267 | // VER_SD | ||
268 | 12038 | l = line + mv; | |
269 |
2/2✓ Branch 0 taken 636876 times.
✓ Branch 1 taken 12038 times.
|
648914 | for (lp = 0; lp < lh; lp++) { |
270 | 636876 | int i, j = 0; | |
271 | |||
272 |
2/2✓ Branch 0 taken 61241397 times.
✓ Branch 1 taken 636876 times.
|
61878273 | for (i = 0; i < lv; i++) |
273 | 61241397 | l[i] = t[w*i + lp]; | |
274 | |||
275 | 636876 | sd_1d97_int(line, mv, mv + lv); | |
276 | |||
277 | // copy back and deinterleave | ||
278 |
2/2✓ Branch 0 taken 30625716 times.
✓ Branch 1 taken 636876 times.
|
31262592 | for (i = mv; i < lv; i+=2, j++) |
279 | 30625716 | t[w*j + lp] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16; | |
280 |
2/2✓ Branch 0 taken 30615681 times.
✓ Branch 1 taken 636876 times.
|
31252557 | for (i = 1-mv; i < lv; i+=2, j++) |
281 | 30615681 | t[w*j + lp] = l[i]; | |
282 | } | ||
283 | |||
284 | // HOR_SD | ||
285 | 12038 | l = line + mh; | |
286 |
2/2✓ Branch 0 taken 524414 times.
✓ Branch 1 taken 12038 times.
|
536452 | for (lp = 0; lp < lv; lp++){ |
287 | 524414 | int i, j = 0; | |
288 | |||
289 |
2/2✓ Branch 0 taken 61241397 times.
✓ Branch 1 taken 524414 times.
|
61765811 | for (i = 0; i < lh; i++) |
290 | 61241397 | l[i] = t[w*lp + i]; | |
291 | |||
292 | 524414 | sd_1d97_int(line, mh, mh + lh); | |
293 | |||
294 | // copy back and deinterleave | ||
295 |
2/2✓ Branch 0 taken 30625229 times.
✓ Branch 1 taken 524414 times.
|
31149643 | for (i = mh; i < lh; i+=2, j++) |
296 | 30625229 | t[w*lp + j] = ((l[i] * I_LFTG_X) + (1 << 15)) >> 16; | |
297 |
2/2✓ Branch 0 taken 30616168 times.
✓ Branch 1 taken 524414 times.
|
31140582 | for (i = 1-mh; i < lh; i+=2, j++) |
298 | 30616168 | t[w*lp + j] = l[i]; | |
299 | } | ||
300 | |||
301 | } | ||
302 | |||
303 |
2/2✓ Branch 0 taken 45940533 times.
✓ Branch 1 taken 1970 times.
|
45942503 | for (i = 0; i < w * h; i++) |
304 | 45940533 | t[i] = (t[i] + ((1<<I_PRESHIFT)>>1)) >> I_PRESHIFT; | |
305 | 1970 | } | |
306 | |||
307 | 4217273 | static void sr_1d53(unsigned *p, int i0, int i1) | |
308 | { | ||
309 | int i; | ||
310 | |||
311 |
2/2✓ Branch 0 taken 16610 times.
✓ Branch 1 taken 4200663 times.
|
4217273 | if (i1 <= i0 + 1) { |
312 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16546 times.
|
16610 | if (i0 == 1) |
313 | 64 | p[1] = (int)p[1] >> 1; | |
314 | 16610 | return; | |
315 | } | ||
316 | |||
317 | 4200663 | extend53(p, i0, i1); | |
318 | |||
319 |
2/2✓ Branch 0 taken 226506111 times.
✓ Branch 1 taken 4200663 times.
|
230706774 | for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) |
320 | 226506111 | p[2 * i] -= (int)(p[2 * i - 1] + p[2 * i + 1] + 2) >> 2; | |
321 |
2/2✓ Branch 0 taken 222305448 times.
✓ Branch 1 taken 4200663 times.
|
226506111 | for (i = (i0 >> 1); i < (i1 >> 1); i++) |
322 | 222305448 | p[2 * i + 1] += (int)(p[2 * i] + p[2 * i + 2]) >> 1; | |
323 | } | ||
324 | |||
325 | 7308 | static void dwt_decode53(DWTContext *s, int *t) | |
326 | { | ||
327 | int lev; | ||
328 | 7308 | int w = s->linelen[s->ndeclevels - 1][0]; | |
329 | 7308 | int32_t *line = s->i_linebuf; | |
330 | 7308 | line += 3; | |
331 | |||
332 |
2/2✓ Branch 0 taken 41704 times.
✓ Branch 1 taken 7308 times.
|
49012 | for (lev = 0; lev < s->ndeclevels; lev++) { |
333 | 41704 | int lh = s->linelen[lev][0], | |
334 | 41704 | lv = s->linelen[lev][1], | |
335 | 41704 | mh = s->mod[lev][0], | |
336 | 41704 | mv = s->mod[lev][1], | |
337 | lp; | ||
338 | int *l; | ||
339 | |||
340 | // HOR_SD | ||
341 | 41704 | l = line + mh; | |
342 |
2/2✓ Branch 0 taken 1923923 times.
✓ Branch 1 taken 41704 times.
|
1965627 | for (lp = 0; lp < lv; lp++) { |
343 | 1923923 | int i, j = 0; | |
344 | // copy with interleaving | ||
345 |
2/2✓ Branch 0 taken 111191013 times.
✓ Branch 1 taken 1923923 times.
|
113114936 | for (i = mh; i < lh; i += 2, j++) |
346 | 111191013 | l[i] = t[w * lp + j]; | |
347 |
2/2✓ Branch 0 taken 111151265 times.
✓ Branch 1 taken 1923923 times.
|
113075188 | for (i = 1 - mh; i < lh; i += 2, j++) |
348 | 111151265 | l[i] = t[w * lp + j]; | |
349 | |||
350 | 1923923 | sr_1d53(line, mh, mh + lh); | |
351 | |||
352 |
2/2✓ Branch 0 taken 222342278 times.
✓ Branch 1 taken 1923923 times.
|
224266201 | for (i = 0; i < lh; i++) |
353 | 222342278 | t[w * lp + i] = l[i]; | |
354 | } | ||
355 | |||
356 | // VER_SD | ||
357 | 41704 | l = line + mv; | |
358 |
2/2✓ Branch 0 taken 2293350 times.
✓ Branch 1 taken 41704 times.
|
2335054 | for (lp = 0; lp < lh; lp++) { |
359 | 2293350 | int i, j = 0; | |
360 | // copy with interleaving | ||
361 |
2/2✓ Branch 0 taken 111188041 times.
✓ Branch 1 taken 2293350 times.
|
113481391 | for (i = mv; i < lv; i += 2, j++) |
362 | 111188041 | l[i] = t[w * j + lp]; | |
363 |
2/2✓ Branch 0 taken 111154237 times.
✓ Branch 1 taken 2293350 times.
|
113447587 | for (i = 1 - mv; i < lv; i += 2, j++) |
364 | 111154237 | l[i] = t[w * j + lp]; | |
365 | |||
366 | 2293350 | sr_1d53(line, mv, mv + lv); | |
367 | |||
368 |
2/2✓ Branch 0 taken 222342278 times.
✓ Branch 1 taken 2293350 times.
|
224635628 | for (i = 0; i < lv; i++) |
369 | 222342278 | t[w * i + lp] = l[i]; | |
370 | } | ||
371 | } | ||
372 | 7308 | } | |
373 | |||
374 | 6290 | static void sr_1d97_float(float *p, int i0, int i1) | |
375 | { | ||
376 | int i; | ||
377 | |||
378 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 6182 times.
|
6290 | if (i1 <= i0 + 1) { |
379 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 44 times.
|
108 | if (i0 == 1) |
380 | 64 | p[1] *= F_LFTG_K/2; | |
381 | else | ||
382 | 44 | p[0] *= F_LFTG_X; | |
383 | 108 | return; | |
384 | } | ||
385 | |||
386 | 6182 | extend97_float(p, i0, i1); | |
387 | |||
388 |
2/2✓ Branch 0 taken 214591 times.
✓ Branch 1 taken 6182 times.
|
220773 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++) |
389 | 214591 | p[2 * i] -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]); | |
390 | /* step 4 */ | ||
391 |
2/2✓ Branch 0 taken 208409 times.
✓ Branch 1 taken 6182 times.
|
214591 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++) |
392 | 208409 | p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]); | |
393 | /*step 5*/ | ||
394 |
2/2✓ Branch 0 taken 202227 times.
✓ Branch 1 taken 6182 times.
|
208409 | for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) |
395 | 202227 | p[2 * i] += F_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]); | |
396 | /* step 6 */ | ||
397 |
2/2✓ Branch 0 taken 196045 times.
✓ Branch 1 taken 6182 times.
|
202227 | for (i = (i0 >> 1); i < (i1 >> 1); i++) |
398 | 196045 | p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]); | |
399 | } | ||
400 | |||
401 | 20 | static void dwt_decode97_float(DWTContext *s, float *t) | |
402 | { | ||
403 | int lev; | ||
404 | 20 | int w = s->linelen[s->ndeclevels - 1][0]; | |
405 | 20 | float *line = s->f_linebuf; | |
406 | 20 | float *data = t; | |
407 | /* position at index O of line range [0-5,w+5] cf. extend function */ | ||
408 | 20 | line += 5; | |
409 | |||
410 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 20 times.
|
358 | for (lev = 0; lev < s->ndeclevels; lev++) { |
411 | 338 | int lh = s->linelen[lev][0], | |
412 | 338 | lv = s->linelen[lev][1], | |
413 | 338 | mh = s->mod[lev][0], | |
414 | 338 | mv = s->mod[lev][1], | |
415 | lp; | ||
416 | float *l; | ||
417 | // HOR_SD | ||
418 | 338 | l = line + mh; | |
419 |
2/2✓ Branch 0 taken 3614 times.
✓ Branch 1 taken 338 times.
|
3952 | for (lp = 0; lp < lv; lp++) { |
420 | 3614 | int i, j = 0; | |
421 | // copy with interleaving | ||
422 |
2/2✓ Branch 0 taken 97979 times.
✓ Branch 1 taken 3614 times.
|
101593 | for (i = mh; i < lh; i += 2, j++) |
423 | 97979 | l[i] = data[w * lp + j]; | |
424 |
2/2✓ Branch 0 taken 98068 times.
✓ Branch 1 taken 3614 times.
|
101682 | for (i = 1 - mh; i < lh; i += 2, j++) |
425 | 98068 | l[i] = data[w * lp + j]; | |
426 | |||
427 | 3614 | sr_1d97_float(line, mh, mh + lh); | |
428 | |||
429 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 3614 times.
|
199661 | for (i = 0; i < lh; i++) |
430 | 196047 | data[w * lp + i] = l[i]; | |
431 | } | ||
432 | |||
433 | // VER_SD | ||
434 | 338 | l = line + mv; | |
435 |
2/2✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 338 times.
|
3014 | for (lp = 0; lp < lh; lp++) { |
436 | 2676 | int i, j = 0; | |
437 | // copy with interleaving | ||
438 |
2/2✓ Branch 0 taken 98016 times.
✓ Branch 1 taken 2676 times.
|
100692 | for (i = mv; i < lv; i += 2, j++) |
439 | 98016 | l[i] = data[w * j + lp]; | |
440 |
2/2✓ Branch 0 taken 98031 times.
✓ Branch 1 taken 2676 times.
|
100707 | for (i = 1 - mv; i < lv; i += 2, j++) |
441 | 98031 | l[i] = data[w * j + lp]; | |
442 | |||
443 | 2676 | sr_1d97_float(line, mv, mv + lv); | |
444 | |||
445 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 2676 times.
|
198723 | for (i = 0; i < lv; i++) |
446 | 196047 | data[w * i + lp] = l[i]; | |
447 | } | ||
448 | } | ||
449 | 20 | } | |
450 | |||
451 | 1212525 | static void sr_1d97_int(int32_t *p, int i0, int i1) | |
452 | { | ||
453 | int i; | ||
454 | |||
455 |
2/2✓ Branch 0 taken 5058 times.
✓ Branch 1 taken 1207467 times.
|
1212525 | if (i1 <= i0 + 1) { |
456 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4994 times.
|
5058 | if (i0 == 1) |
457 | 64 | p[1] = (p[1] * I_LFTG_K + (1<<16)) >> 17; | |
458 | else | ||
459 | 4994 | p[0] = (p[0] * I_LFTG_X + (1<<15)) >> 16; | |
460 | 5058 | return; | |
461 | } | ||
462 | |||
463 | 1207467 | extend97_int(p, i0, i1); | |
464 | |||
465 |
2/2✓ Branch 0 taken 85779182 times.
✓ Branch 1 taken 1207467 times.
|
86986649 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++) |
466 | 85779182 | p[2 * i] -= (I_LFTG_DELTA * (p[2 * i - 1] + (int64_t)p[2 * i + 1]) + (1 << 15)) >> 16; | |
467 | /* step 4 */ | ||
468 |
2/2✓ Branch 0 taken 84571715 times.
✓ Branch 1 taken 1207467 times.
|
85779182 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++) |
469 | 84571715 | p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i] + (int64_t)p[2 * i + 2]) + (1 << 15)) >> 16; | |
470 | /*step 5*/ | ||
471 |
2/2✓ Branch 0 taken 83364248 times.
✓ Branch 1 taken 1207467 times.
|
84571715 | for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) |
472 | 83364248 | p[2 * i] += (I_LFTG_BETA * (p[2 * i - 1] + (int64_t)p[2 * i + 1]) + (1 << 15)) >> 16; | |
473 | /* step 6 */ | ||
474 |
2/2✓ Branch 0 taken 82156781 times.
✓ Branch 1 taken 1207467 times.
|
83364248 | for (i = (i0 >> 1); i < (i1 >> 1); i++) |
475 | 82156781 | p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i] + (int64_t)p[2 * i + 2]) + (1 << 15)) >> 16; | |
476 | } | ||
477 | |||
478 | 1983 | static void dwt_decode97_int(DWTContext *s, int32_t *t) | |
479 | { | ||
480 | int lev; | ||
481 | 1983 | int w = s->linelen[s->ndeclevels - 1][0]; | |
482 | 1983 | int h = s->linelen[s->ndeclevels - 1][1]; | |
483 | int i; | ||
484 | 1983 | int32_t *line = s->i_linebuf; | |
485 | 1983 | int32_t *data = t; | |
486 | /* position at index O of line range [0-5,w+5] cf. extend function */ | ||
487 | 1983 | line += 5; | |
488 | |||
489 |
2/2✓ Branch 0 taken 61663658 times.
✓ Branch 1 taken 1983 times.
|
61665641 | for (i = 0; i < w * h; i++) |
490 | 61663658 | data[i] *= 1LL << I_PRESHIFT; | |
491 | |||
492 |
2/2✓ Branch 0 taken 12106 times.
✓ Branch 1 taken 1983 times.
|
14089 | for (lev = 0; lev < s->ndeclevels; lev++) { |
493 | 12106 | int lh = s->linelen[lev][0], | |
494 | 12106 | lv = s->linelen[lev][1], | |
495 | 12106 | mh = s->mod[lev][0], | |
496 | 12106 | mv = s->mod[lev][1], | |
497 | lp; | ||
498 | int32_t *l; | ||
499 | // HOR_SD | ||
500 | 12106 | l = line + mh; | |
501 |
2/2✓ Branch 0 taken 544697 times.
✓ Branch 1 taken 12106 times.
|
556803 | for (lp = 0; lp < lv; lp++) { |
502 | 544697 | int i, j = 0; | |
503 | // rescale with interleaving | ||
504 |
2/2✓ Branch 0 taken 41088158 times.
✓ Branch 1 taken 544697 times.
|
41632855 | for (i = mh; i < lh; i += 2, j++) |
505 | 41088158 | l[i] = ((data[w * lp + j] * I_LFTG_K) + (1 << 15)) >> 16; | |
506 |
2/2✓ Branch 0 taken 41079026 times.
✓ Branch 1 taken 544697 times.
|
41623723 | for (i = 1 - mh; i < lh; i += 2, j++) |
507 | 41079026 | l[i] = data[w * lp + j]; | |
508 | |||
509 | 544697 | sr_1d97_int(line, mh, mh + lh); | |
510 | |||
511 |
2/2✓ Branch 0 taken 82167184 times.
✓ Branch 1 taken 544697 times.
|
82711881 | for (i = 0; i < lh; i++) |
512 | 82167184 | data[w * lp + i] = l[i]; | |
513 | } | ||
514 | |||
515 | // VER_SD | ||
516 | 12106 | l = line + mv; | |
517 |
2/2✓ Branch 0 taken 667828 times.
✓ Branch 1 taken 12106 times.
|
679934 | for (lp = 0; lp < lh; lp++) { |
518 | 667828 | int i, j = 0; | |
519 | // rescale with interleaving | ||
520 |
2/2✓ Branch 0 taken 41089375 times.
✓ Branch 1 taken 667828 times.
|
41757203 | for (i = mv; i < lv; i += 2, j++) |
521 | 41089375 | l[i] = ((data[w * j + lp] * I_LFTG_K) + (1 << 15)) >> 16; | |
522 |
2/2✓ Branch 0 taken 41077809 times.
✓ Branch 1 taken 667828 times.
|
41745637 | for (i = 1 - mv; i < lv; i += 2, j++) |
523 | 41077809 | l[i] = data[w * j + lp]; | |
524 | |||
525 | 667828 | sr_1d97_int(line, mv, mv + lv); | |
526 | |||
527 |
2/2✓ Branch 0 taken 82167184 times.
✓ Branch 1 taken 667828 times.
|
82835012 | for (i = 0; i < lv; i++) |
528 | 82167184 | data[w * i + lp] = l[i]; | |
529 | } | ||
530 | } | ||
531 | |||
532 |
2/2✓ Branch 0 taken 61663658 times.
✓ Branch 1 taken 1983 times.
|
61665641 | for (i = 0; i < w * h; i++) |
533 | 61663658 | data[i] = (data[i] + ((1LL<<I_PRESHIFT)>>1)) >> I_PRESHIFT; | |
534 | 1983 | } | |
535 | |||
536 | 9481 | int ff_jpeg2000_dwt_init(DWTContext *s, int border[2][2], | |
537 | int decomp_levels, int type) | ||
538 | { | ||
539 | 9481 | int i, j, lev = decomp_levels, maxlen, | |
540 | b[2][2]; | ||
541 | |||
542 | 9481 | s->ndeclevels = decomp_levels; | |
543 | 9481 | s->type = type; | |
544 | |||
545 |
2/2✓ Branch 0 taken 18962 times.
✓ Branch 1 taken 9481 times.
|
28443 | for (i = 0; i < 2; i++) |
546 |
2/2✓ Branch 0 taken 37924 times.
✓ Branch 1 taken 18962 times.
|
56886 | for (j = 0; j < 2; j++) |
547 | 37924 | b[i][j] = border[i][j]; | |
548 | |||
549 | 9481 | maxlen = FFMAX(b[0][1] - b[0][0], | |
550 | b[1][1] - b[1][0]); | ||
551 |
2/2✓ Branch 0 taken 55162 times.
✓ Branch 1 taken 9481 times.
|
64643 | while (--lev >= 0) |
552 |
2/2✓ Branch 0 taken 110324 times.
✓ Branch 1 taken 55162 times.
|
165486 | for (i = 0; i < 2; i++) { |
553 | 110324 | s->linelen[lev][i] = b[i][1] - b[i][0]; | |
554 | 110324 | s->mod[lev][i] = b[i][0] & 1; | |
555 |
2/2✓ Branch 0 taken 220648 times.
✓ Branch 1 taken 110324 times.
|
330972 | for (j = 0; j < 2; j++) |
556 | 220648 | b[i][j] = (b[i][j] + 1) >> 1; | |
557 | } | ||
558 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2022 times.
✓ Branch 2 taken 7439 times.
✗ Branch 3 not taken.
|
9481 | switch (type) { |
559 | 20 | case FF_DWT97: | |
560 | 20 | s->f_linebuf = av_malloc_array((maxlen + 12), sizeof(*s->f_linebuf)); | |
561 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!s->f_linebuf) |
562 | ✗ | return AVERROR(ENOMEM); | |
563 | 20 | break; | |
564 | 2022 | case FF_DWT97_INT: | |
565 | 2022 | s->i_linebuf = av_malloc_array((maxlen + 12), sizeof(*s->i_linebuf)); | |
566 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2022 times.
|
2022 | if (!s->i_linebuf) |
567 | ✗ | return AVERROR(ENOMEM); | |
568 | 2022 | break; | |
569 | 7439 | case FF_DWT53: | |
570 | 7439 | s->i_linebuf = av_malloc_array((maxlen + 6), sizeof(*s->i_linebuf)); | |
571 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7439 times.
|
7439 | if (!s->i_linebuf) |
572 | ✗ | return AVERROR(ENOMEM); | |
573 | 7439 | break; | |
574 | ✗ | default: | |
575 | ✗ | return -1; | |
576 | } | ||
577 | 9481 | return 0; | |
578 | } | ||
579 | |||
580 | 8510 | int ff_dwt_encode(DWTContext *s, void *t) | |
581 | { | ||
582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8510 times.
|
8510 | if (s->ndeclevels == 0) |
583 | ✗ | return 0; | |
584 | |||
585 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1970 times.
✓ Branch 2 taken 6520 times.
✗ Branch 3 not taken.
|
8510 | switch(s->type){ |
586 | 20 | case FF_DWT97: | |
587 | 20 | dwt_encode97_float(s, t); break; | |
588 | 1970 | case FF_DWT97_INT: | |
589 | 1970 | dwt_encode97_int(s, t); break; | |
590 | 6520 | case FF_DWT53: | |
591 | 6520 | dwt_encode53(s, t); break; | |
592 | ✗ | default: | |
593 | ✗ | return -1; | |
594 | } | ||
595 | 8510 | return 0; | |
596 | } | ||
597 | |||
598 | 9312 | int ff_dwt_decode(DWTContext *s, void *t) | |
599 | { | ||
600 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9311 times.
|
9312 | if (s->ndeclevels == 0) |
601 | 1 | return 0; | |
602 | |||
603 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1983 times.
✓ Branch 2 taken 7308 times.
✗ Branch 3 not taken.
|
9311 | switch (s->type) { |
604 | 20 | case FF_DWT97: | |
605 | 20 | dwt_decode97_float(s, t); | |
606 | 20 | break; | |
607 | 1983 | case FF_DWT97_INT: | |
608 | 1983 | dwt_decode97_int(s, t); | |
609 | 1983 | break; | |
610 | 7308 | case FF_DWT53: | |
611 | 7308 | dwt_decode53(s, t); | |
612 | 7308 | break; | |
613 | ✗ | default: | |
614 | ✗ | return -1; | |
615 | } | ||
616 | 9311 | return 0; | |
617 | } | ||
618 | |||
619 | 10460 | void ff_dwt_destroy(DWTContext *s) | |
620 | { | ||
621 | 10460 | av_freep(&s->f_linebuf); | |
622 | 10460 | av_freep(&s->i_linebuf); | |
623 | 10460 | } | |
624 |